blob: 0daf5903a77333a2a4c632824cf58773ed38128e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
32#ifdef FEAT_QUICKFIX
33static char *e_stringreq = N_("E928: String required");
34#endif
35
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
66static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010067# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010068static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010069# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020071static void f_browse(typval_T *argvars, typval_T *rettv);
72static void f_browsedir(typval_T *argvars, typval_T *rettv);
73static void f_bufexists(typval_T *argvars, typval_T *rettv);
74static void f_buflisted(typval_T *argvars, typval_T *rettv);
75static void f_bufloaded(typval_T *argvars, typval_T *rettv);
76static void f_bufname(typval_T *argvars, typval_T *rettv);
77static void f_bufnr(typval_T *argvars, typval_T *rettv);
78static void f_bufwinid(typval_T *argvars, typval_T *rettv);
79static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
80static void f_byte2line(typval_T *argvars, typval_T *rettv);
81static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
82static void f_byteidx(typval_T *argvars, typval_T *rettv);
83static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
84static void f_call(typval_T *argvars, typval_T *rettv);
85#ifdef FEAT_FLOAT
86static void f_ceil(typval_T *argvars, typval_T *rettv);
87#endif
88#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010089static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020090static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020091static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020092static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
93static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
94static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
95static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
96static void f_ch_info(typval_T *argvars, typval_T *rettv);
97static void f_ch_log(typval_T *argvars, typval_T *rettv);
98static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
99static void f_ch_open(typval_T *argvars, typval_T *rettv);
100static void f_ch_read(typval_T *argvars, typval_T *rettv);
101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200204static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100205static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200206static void f_getwinposx(typval_T *argvars, typval_T *rettv);
207static void f_getwinposy(typval_T *argvars, typval_T *rettv);
208static void f_getwinvar(typval_T *argvars, typval_T *rettv);
209static void f_glob(typval_T *argvars, typval_T *rettv);
210static void f_globpath(typval_T *argvars, typval_T *rettv);
211static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
212static void f_has(typval_T *argvars, typval_T *rettv);
213static void f_has_key(typval_T *argvars, typval_T *rettv);
214static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
215static void f_hasmapto(typval_T *argvars, typval_T *rettv);
216static void f_histadd(typval_T *argvars, typval_T *rettv);
217static void f_histdel(typval_T *argvars, typval_T *rettv);
218static void f_histget(typval_T *argvars, typval_T *rettv);
219static void f_histnr(typval_T *argvars, typval_T *rettv);
220static void f_hlID(typval_T *argvars, typval_T *rettv);
221static void f_hlexists(typval_T *argvars, typval_T *rettv);
222static void f_hostname(typval_T *argvars, typval_T *rettv);
223static void f_iconv(typval_T *argvars, typval_T *rettv);
224static void f_indent(typval_T *argvars, typval_T *rettv);
225static void f_index(typval_T *argvars, typval_T *rettv);
226static void f_input(typval_T *argvars, typval_T *rettv);
227static void f_inputdialog(typval_T *argvars, typval_T *rettv);
228static void f_inputlist(typval_T *argvars, typval_T *rettv);
229static void f_inputrestore(typval_T *argvars, typval_T *rettv);
230static void f_inputsave(typval_T *argvars, typval_T *rettv);
231static void f_inputsecret(typval_T *argvars, typval_T *rettv);
232static void f_insert(typval_T *argvars, typval_T *rettv);
233static void f_invert(typval_T *argvars, typval_T *rettv);
234static void f_isdirectory(typval_T *argvars, typval_T *rettv);
235static void f_islocked(typval_T *argvars, typval_T *rettv);
236#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
237static void f_isnan(typval_T *argvars, typval_T *rettv);
238#endif
239static void f_items(typval_T *argvars, typval_T *rettv);
240#ifdef FEAT_JOB_CHANNEL
241static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
242static void f_job_info(typval_T *argvars, typval_T *rettv);
243static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
244static void f_job_start(typval_T *argvars, typval_T *rettv);
245static void f_job_stop(typval_T *argvars, typval_T *rettv);
246static void f_job_status(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_join(typval_T *argvars, typval_T *rettv);
249static void f_js_decode(typval_T *argvars, typval_T *rettv);
250static void f_js_encode(typval_T *argvars, typval_T *rettv);
251static void f_json_decode(typval_T *argvars, typval_T *rettv);
252static void f_json_encode(typval_T *argvars, typval_T *rettv);
253static void f_keys(typval_T *argvars, typval_T *rettv);
254static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
255static void f_len(typval_T *argvars, typval_T *rettv);
256static void f_libcall(typval_T *argvars, typval_T *rettv);
257static void f_libcallnr(typval_T *argvars, typval_T *rettv);
258static void f_line(typval_T *argvars, typval_T *rettv);
259static void f_line2byte(typval_T *argvars, typval_T *rettv);
260static void f_lispindent(typval_T *argvars, typval_T *rettv);
261static void f_localtime(typval_T *argvars, typval_T *rettv);
262#ifdef FEAT_FLOAT
263static void f_log(typval_T *argvars, typval_T *rettv);
264static void f_log10(typval_T *argvars, typval_T *rettv);
265#endif
266#ifdef FEAT_LUA
267static void f_luaeval(typval_T *argvars, typval_T *rettv);
268#endif
269static void f_map(typval_T *argvars, typval_T *rettv);
270static void f_maparg(typval_T *argvars, typval_T *rettv);
271static void f_mapcheck(typval_T *argvars, typval_T *rettv);
272static void f_match(typval_T *argvars, typval_T *rettv);
273static void f_matchadd(typval_T *argvars, typval_T *rettv);
274static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
275static void f_matcharg(typval_T *argvars, typval_T *rettv);
276static void f_matchdelete(typval_T *argvars, typval_T *rettv);
277static void f_matchend(typval_T *argvars, typval_T *rettv);
278static void f_matchlist(typval_T *argvars, typval_T *rettv);
279static void f_matchstr(typval_T *argvars, typval_T *rettv);
280static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
281static void f_max(typval_T *argvars, typval_T *rettv);
282static void f_min(typval_T *argvars, typval_T *rettv);
283#ifdef vim_mkdir
284static void f_mkdir(typval_T *argvars, typval_T *rettv);
285#endif
286static void f_mode(typval_T *argvars, typval_T *rettv);
287#ifdef FEAT_MZSCHEME
288static void f_mzeval(typval_T *argvars, typval_T *rettv);
289#endif
290static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
291static void f_nr2char(typval_T *argvars, typval_T *rettv);
292static void f_or(typval_T *argvars, typval_T *rettv);
293static void f_pathshorten(typval_T *argvars, typval_T *rettv);
294#ifdef FEAT_PERL
295static void f_perleval(typval_T *argvars, typval_T *rettv);
296#endif
297#ifdef FEAT_FLOAT
298static void f_pow(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
301static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200302#ifdef FEAT_JOB_CHANNEL
303static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200304static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200305static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200307static void f_pumvisible(typval_T *argvars, typval_T *rettv);
308#ifdef FEAT_PYTHON3
309static void f_py3eval(typval_T *argvars, typval_T *rettv);
310#endif
311#ifdef FEAT_PYTHON
312static void f_pyeval(typval_T *argvars, typval_T *rettv);
313#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100314#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
315static void f_pyxeval(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_range(typval_T *argvars, typval_T *rettv);
318static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200319static void f_reg_executing(typval_T *argvars, typval_T *rettv);
320static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200321static void f_reltime(typval_T *argvars, typval_T *rettv);
322#ifdef FEAT_FLOAT
323static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
324#endif
325static void f_reltimestr(typval_T *argvars, typval_T *rettv);
326static void f_remote_expr(typval_T *argvars, typval_T *rettv);
327static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
328static void f_remote_peek(typval_T *argvars, typval_T *rettv);
329static void f_remote_read(typval_T *argvars, typval_T *rettv);
330static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100331static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_remove(typval_T *argvars, typval_T *rettv);
333static void f_rename(typval_T *argvars, typval_T *rettv);
334static void f_repeat(typval_T *argvars, typval_T *rettv);
335static void f_resolve(typval_T *argvars, typval_T *rettv);
336static void f_reverse(typval_T *argvars, typval_T *rettv);
337#ifdef FEAT_FLOAT
338static void f_round(typval_T *argvars, typval_T *rettv);
339#endif
340static void f_screenattr(typval_T *argvars, typval_T *rettv);
341static void f_screenchar(typval_T *argvars, typval_T *rettv);
342static void f_screencol(typval_T *argvars, typval_T *rettv);
343static void f_screenrow(typval_T *argvars, typval_T *rettv);
344static void f_search(typval_T *argvars, typval_T *rettv);
345static void f_searchdecl(typval_T *argvars, typval_T *rettv);
346static void f_searchpair(typval_T *argvars, typval_T *rettv);
347static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
348static void f_searchpos(typval_T *argvars, typval_T *rettv);
349static void f_server2client(typval_T *argvars, typval_T *rettv);
350static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200351static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200352static void f_setbufvar(typval_T *argvars, typval_T *rettv);
353static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
354static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
355static void f_setfperm(typval_T *argvars, typval_T *rettv);
356static void f_setline(typval_T *argvars, typval_T *rettv);
357static void f_setloclist(typval_T *argvars, typval_T *rettv);
358static void f_setmatches(typval_T *argvars, typval_T *rettv);
359static void f_setpos(typval_T *argvars, typval_T *rettv);
360static void f_setqflist(typval_T *argvars, typval_T *rettv);
361static void f_setreg(typval_T *argvars, typval_T *rettv);
362static void f_settabvar(typval_T *argvars, typval_T *rettv);
363static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
364static void f_setwinvar(typval_T *argvars, typval_T *rettv);
365#ifdef FEAT_CRYPT
366static void f_sha256(typval_T *argvars, typval_T *rettv);
367#endif /* FEAT_CRYPT */
368static void f_shellescape(typval_T *argvars, typval_T *rettv);
369static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
370static void f_simplify(typval_T *argvars, typval_T *rettv);
371#ifdef FEAT_FLOAT
372static void f_sin(typval_T *argvars, typval_T *rettv);
373static void f_sinh(typval_T *argvars, typval_T *rettv);
374#endif
375static void f_sort(typval_T *argvars, typval_T *rettv);
376static void f_soundfold(typval_T *argvars, typval_T *rettv);
377static void f_spellbadword(typval_T *argvars, typval_T *rettv);
378static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
379static void f_split(typval_T *argvars, typval_T *rettv);
380#ifdef FEAT_FLOAT
381static void f_sqrt(typval_T *argvars, typval_T *rettv);
382static void f_str2float(typval_T *argvars, typval_T *rettv);
383#endif
384static void f_str2nr(typval_T *argvars, typval_T *rettv);
385static void f_strchars(typval_T *argvars, typval_T *rettv);
386#ifdef HAVE_STRFTIME
387static void f_strftime(typval_T *argvars, typval_T *rettv);
388#endif
389static void f_strgetchar(typval_T *argvars, typval_T *rettv);
390static void f_stridx(typval_T *argvars, typval_T *rettv);
391static void f_string(typval_T *argvars, typval_T *rettv);
392static void f_strlen(typval_T *argvars, typval_T *rettv);
393static void f_strcharpart(typval_T *argvars, typval_T *rettv);
394static void f_strpart(typval_T *argvars, typval_T *rettv);
395static void f_strridx(typval_T *argvars, typval_T *rettv);
396static void f_strtrans(typval_T *argvars, typval_T *rettv);
397static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
398static void f_strwidth(typval_T *argvars, typval_T *rettv);
399static void f_submatch(typval_T *argvars, typval_T *rettv);
400static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200401static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_synID(typval_T *argvars, typval_T *rettv);
403static void f_synIDattr(typval_T *argvars, typval_T *rettv);
404static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
405static void f_synstack(typval_T *argvars, typval_T *rettv);
406static void f_synconcealed(typval_T *argvars, typval_T *rettv);
407static void f_system(typval_T *argvars, typval_T *rettv);
408static void f_systemlist(typval_T *argvars, typval_T *rettv);
409static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
411static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
412static void f_taglist(typval_T *argvars, typval_T *rettv);
413static void f_tagfiles(typval_T *argvars, typval_T *rettv);
414static void f_tempname(typval_T *argvars, typval_T *rettv);
415static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
416static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200417static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200418static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100419static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200420static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100421static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200422#ifdef FEAT_JOB_CHANNEL
423static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
424#endif
425static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
426#ifdef FEAT_JOB_CHANNEL
427static void f_test_null_job(typval_T *argvars, typval_T *rettv);
428#endif
429static void f_test_null_list(typval_T *argvars, typval_T *rettv);
430static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
431static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200432#ifdef FEAT_GUI
433static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200435static void f_test_settime(typval_T *argvars, typval_T *rettv);
436#ifdef FEAT_FLOAT
437static void f_tan(typval_T *argvars, typval_T *rettv);
438static void f_tanh(typval_T *argvars, typval_T *rettv);
439#endif
440#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200441static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200442static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200443static void f_timer_start(typval_T *argvars, typval_T *rettv);
444static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200445static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200446#endif
447static void f_tolower(typval_T *argvars, typval_T *rettv);
448static void f_toupper(typval_T *argvars, typval_T *rettv);
449static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100450static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200451#ifdef FEAT_FLOAT
452static void f_trunc(typval_T *argvars, typval_T *rettv);
453#endif
454static void f_type(typval_T *argvars, typval_T *rettv);
455static void f_undofile(typval_T *argvars, typval_T *rettv);
456static void f_undotree(typval_T *argvars, typval_T *rettv);
457static void f_uniq(typval_T *argvars, typval_T *rettv);
458static void f_values(typval_T *argvars, typval_T *rettv);
459static void f_virtcol(typval_T *argvars, typval_T *rettv);
460static void f_visualmode(typval_T *argvars, typval_T *rettv);
461static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
462static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
463static void f_win_getid(typval_T *argvars, typval_T *rettv);
464static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
465static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
466static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100467static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_winbufnr(typval_T *argvars, typval_T *rettv);
469static void f_wincol(typval_T *argvars, typval_T *rettv);
470static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200471static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200472static void f_winline(typval_T *argvars, typval_T *rettv);
473static void f_winnr(typval_T *argvars, typval_T *rettv);
474static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
475static void f_winrestview(typval_T *argvars, typval_T *rettv);
476static void f_winsaveview(typval_T *argvars, typval_T *rettv);
477static void f_winwidth(typval_T *argvars, typval_T *rettv);
478static void f_writefile(typval_T *argvars, typval_T *rettv);
479static void f_wordcount(typval_T *argvars, typval_T *rettv);
480static void f_xor(typval_T *argvars, typval_T *rettv);
481
482/*
483 * Array with names and number of arguments of all internal functions
484 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
485 */
486static struct fst
487{
488 char *f_name; /* function name */
489 char f_min_argc; /* minimal number of arguments */
490 char f_max_argc; /* maximal number of arguments */
491 void (*f_func)(typval_T *args, typval_T *rvar);
492 /* implementation of function */
493} functions[] =
494{
495#ifdef FEAT_FLOAT
496 {"abs", 1, 1, f_abs},
497 {"acos", 1, 1, f_acos}, /* WJMc */
498#endif
499 {"add", 2, 2, f_add},
500 {"and", 2, 2, f_and},
501 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200502 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503 {"argc", 0, 0, f_argc},
504 {"argidx", 0, 0, f_argidx},
505 {"arglistid", 0, 2, f_arglistid},
506 {"argv", 0, 1, f_argv},
507#ifdef FEAT_FLOAT
508 {"asin", 1, 1, f_asin}, /* WJMc */
509#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100510 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200511 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100512 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200513 {"assert_exception", 1, 2, f_assert_exception},
514 {"assert_fails", 1, 2, f_assert_fails},
515 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100516 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200517 {"assert_match", 2, 3, f_assert_match},
518 {"assert_notequal", 2, 3, f_assert_notequal},
519 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100520 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200521 {"assert_true", 1, 2, f_assert_true},
522#ifdef FEAT_FLOAT
523 {"atan", 1, 1, f_atan},
524 {"atan2", 2, 2, f_atan2},
525#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100526#ifdef FEAT_BEVAL
527 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100528# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100529 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100530# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100531#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200532 {"browse", 4, 4, f_browse},
533 {"browsedir", 2, 2, f_browsedir},
534 {"bufexists", 1, 1, f_bufexists},
535 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
536 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
537 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
538 {"buflisted", 1, 1, f_buflisted},
539 {"bufloaded", 1, 1, f_bufloaded},
540 {"bufname", 1, 1, f_bufname},
541 {"bufnr", 1, 2, f_bufnr},
542 {"bufwinid", 1, 1, f_bufwinid},
543 {"bufwinnr", 1, 1, f_bufwinnr},
544 {"byte2line", 1, 1, f_byte2line},
545 {"byteidx", 2, 2, f_byteidx},
546 {"byteidxcomp", 2, 2, f_byteidxcomp},
547 {"call", 2, 3, f_call},
548#ifdef FEAT_FLOAT
549 {"ceil", 1, 1, f_ceil},
550#endif
551#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100552 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200553 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200554 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200555 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
556 {"ch_evalraw", 2, 3, f_ch_evalraw},
557 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
558 {"ch_getjob", 1, 1, f_ch_getjob},
559 {"ch_info", 1, 1, f_ch_info},
560 {"ch_log", 1, 2, f_ch_log},
561 {"ch_logfile", 1, 2, f_ch_logfile},
562 {"ch_open", 1, 2, f_ch_open},
563 {"ch_read", 1, 2, f_ch_read},
564 {"ch_readraw", 1, 2, f_ch_readraw},
565 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
566 {"ch_sendraw", 2, 3, f_ch_sendraw},
567 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200568 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200569#endif
570 {"changenr", 0, 0, f_changenr},
571 {"char2nr", 1, 2, f_char2nr},
572 {"cindent", 1, 1, f_cindent},
573 {"clearmatches", 0, 0, f_clearmatches},
574 {"col", 1, 1, f_col},
575#if defined(FEAT_INS_EXPAND)
576 {"complete", 2, 2, f_complete},
577 {"complete_add", 1, 1, f_complete_add},
578 {"complete_check", 0, 0, f_complete_check},
579#endif
580 {"confirm", 1, 4, f_confirm},
581 {"copy", 1, 1, f_copy},
582#ifdef FEAT_FLOAT
583 {"cos", 1, 1, f_cos},
584 {"cosh", 1, 1, f_cosh},
585#endif
586 {"count", 2, 4, f_count},
587 {"cscope_connection",0,3, f_cscope_connection},
588 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200589#ifdef WIN3264
590 {"debugbreak", 1, 1, f_debugbreak},
591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200592 {"deepcopy", 1, 2, f_deepcopy},
593 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200594 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200595 {"did_filetype", 0, 0, f_did_filetype},
596 {"diff_filler", 1, 1, f_diff_filler},
597 {"diff_hlID", 2, 2, f_diff_hlID},
598 {"empty", 1, 1, f_empty},
599 {"escape", 2, 2, f_escape},
600 {"eval", 1, 1, f_eval},
601 {"eventhandler", 0, 0, f_eventhandler},
602 {"executable", 1, 1, f_executable},
603 {"execute", 1, 2, f_execute},
604 {"exepath", 1, 1, f_exepath},
605 {"exists", 1, 1, f_exists},
606#ifdef FEAT_FLOAT
607 {"exp", 1, 1, f_exp},
608#endif
609 {"expand", 1, 3, f_expand},
610 {"extend", 2, 3, f_extend},
611 {"feedkeys", 1, 2, f_feedkeys},
612 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
613 {"filereadable", 1, 1, f_filereadable},
614 {"filewritable", 1, 1, f_filewritable},
615 {"filter", 2, 2, f_filter},
616 {"finddir", 1, 3, f_finddir},
617 {"findfile", 1, 3, f_findfile},
618#ifdef FEAT_FLOAT
619 {"float2nr", 1, 1, f_float2nr},
620 {"floor", 1, 1, f_floor},
621 {"fmod", 2, 2, f_fmod},
622#endif
623 {"fnameescape", 1, 1, f_fnameescape},
624 {"fnamemodify", 2, 2, f_fnamemodify},
625 {"foldclosed", 1, 1, f_foldclosed},
626 {"foldclosedend", 1, 1, f_foldclosedend},
627 {"foldlevel", 1, 1, f_foldlevel},
628 {"foldtext", 0, 0, f_foldtext},
629 {"foldtextresult", 1, 1, f_foldtextresult},
630 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200631 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"function", 1, 3, f_function},
633 {"garbagecollect", 0, 1, f_garbagecollect},
634 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200635 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200636 {"getbufline", 2, 3, f_getbufline},
637 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100638 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"getchar", 0, 1, f_getchar},
640 {"getcharmod", 0, 0, f_getcharmod},
641 {"getcharsearch", 0, 0, f_getcharsearch},
642 {"getcmdline", 0, 0, f_getcmdline},
643 {"getcmdpos", 0, 0, f_getcmdpos},
644 {"getcmdtype", 0, 0, f_getcmdtype},
645 {"getcmdwintype", 0, 0, f_getcmdwintype},
646#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200647 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200648#endif
649 {"getcurpos", 0, 0, f_getcurpos},
650 {"getcwd", 0, 2, f_getcwd},
651 {"getfontname", 0, 1, f_getfontname},
652 {"getfperm", 1, 1, f_getfperm},
653 {"getfsize", 1, 1, f_getfsize},
654 {"getftime", 1, 1, f_getftime},
655 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100656 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200657 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200658 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200659 {"getmatches", 0, 0, f_getmatches},
660 {"getpid", 0, 0, f_getpid},
661 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200662 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200663 {"getreg", 0, 3, f_getreg},
664 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200665 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200666 {"gettabvar", 2, 3, f_gettabvar},
667 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200668 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100669 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"getwinposx", 0, 0, f_getwinposx},
671 {"getwinposy", 0, 0, f_getwinposy},
672 {"getwinvar", 2, 3, f_getwinvar},
673 {"glob", 1, 4, f_glob},
674 {"glob2regpat", 1, 1, f_glob2regpat},
675 {"globpath", 2, 5, f_globpath},
676 {"has", 1, 1, f_has},
677 {"has_key", 2, 2, f_has_key},
678 {"haslocaldir", 0, 2, f_haslocaldir},
679 {"hasmapto", 1, 3, f_hasmapto},
680 {"highlightID", 1, 1, f_hlID}, /* obsolete */
681 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
682 {"histadd", 2, 2, f_histadd},
683 {"histdel", 1, 2, f_histdel},
684 {"histget", 1, 2, f_histget},
685 {"histnr", 1, 1, f_histnr},
686 {"hlID", 1, 1, f_hlID},
687 {"hlexists", 1, 1, f_hlexists},
688 {"hostname", 0, 0, f_hostname},
689 {"iconv", 3, 3, f_iconv},
690 {"indent", 1, 1, f_indent},
691 {"index", 2, 4, f_index},
692 {"input", 1, 3, f_input},
693 {"inputdialog", 1, 3, f_inputdialog},
694 {"inputlist", 1, 1, f_inputlist},
695 {"inputrestore", 0, 0, f_inputrestore},
696 {"inputsave", 0, 0, f_inputsave},
697 {"inputsecret", 1, 2, f_inputsecret},
698 {"insert", 2, 3, f_insert},
699 {"invert", 1, 1, f_invert},
700 {"isdirectory", 1, 1, f_isdirectory},
701 {"islocked", 1, 1, f_islocked},
702#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
703 {"isnan", 1, 1, f_isnan},
704#endif
705 {"items", 1, 1, f_items},
706#ifdef FEAT_JOB_CHANNEL
707 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200708 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200709 {"job_setoptions", 2, 2, f_job_setoptions},
710 {"job_start", 1, 2, f_job_start},
711 {"job_status", 1, 1, f_job_status},
712 {"job_stop", 1, 2, f_job_stop},
713#endif
714 {"join", 1, 2, f_join},
715 {"js_decode", 1, 1, f_js_decode},
716 {"js_encode", 1, 1, f_js_encode},
717 {"json_decode", 1, 1, f_json_decode},
718 {"json_encode", 1, 1, f_json_encode},
719 {"keys", 1, 1, f_keys},
720 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
721 {"len", 1, 1, f_len},
722 {"libcall", 3, 3, f_libcall},
723 {"libcallnr", 3, 3, f_libcallnr},
724 {"line", 1, 1, f_line},
725 {"line2byte", 1, 1, f_line2byte},
726 {"lispindent", 1, 1, f_lispindent},
727 {"localtime", 0, 0, f_localtime},
728#ifdef FEAT_FLOAT
729 {"log", 1, 1, f_log},
730 {"log10", 1, 1, f_log10},
731#endif
732#ifdef FEAT_LUA
733 {"luaeval", 1, 2, f_luaeval},
734#endif
735 {"map", 2, 2, f_map},
736 {"maparg", 1, 4, f_maparg},
737 {"mapcheck", 1, 3, f_mapcheck},
738 {"match", 2, 4, f_match},
739 {"matchadd", 2, 5, f_matchadd},
740 {"matchaddpos", 2, 5, f_matchaddpos},
741 {"matcharg", 1, 1, f_matcharg},
742 {"matchdelete", 1, 1, f_matchdelete},
743 {"matchend", 2, 4, f_matchend},
744 {"matchlist", 2, 4, f_matchlist},
745 {"matchstr", 2, 4, f_matchstr},
746 {"matchstrpos", 2, 4, f_matchstrpos},
747 {"max", 1, 1, f_max},
748 {"min", 1, 1, f_min},
749#ifdef vim_mkdir
750 {"mkdir", 1, 3, f_mkdir},
751#endif
752 {"mode", 0, 1, f_mode},
753#ifdef FEAT_MZSCHEME
754 {"mzeval", 1, 1, f_mzeval},
755#endif
756 {"nextnonblank", 1, 1, f_nextnonblank},
757 {"nr2char", 1, 2, f_nr2char},
758 {"or", 2, 2, f_or},
759 {"pathshorten", 1, 1, f_pathshorten},
760#ifdef FEAT_PERL
761 {"perleval", 1, 1, f_perleval},
762#endif
763#ifdef FEAT_FLOAT
764 {"pow", 2, 2, f_pow},
765#endif
766 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100767 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200768#ifdef FEAT_JOB_CHANNEL
769 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200770 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200771 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
772#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200773 {"pumvisible", 0, 0, f_pumvisible},
774#ifdef FEAT_PYTHON3
775 {"py3eval", 1, 1, f_py3eval},
776#endif
777#ifdef FEAT_PYTHON
778 {"pyeval", 1, 1, f_pyeval},
779#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100780#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
781 {"pyxeval", 1, 1, f_pyxeval},
782#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200783 {"range", 1, 3, f_range},
784 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200785 {"reg_executing", 0, 0, f_reg_executing},
786 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200787 {"reltime", 0, 2, f_reltime},
788#ifdef FEAT_FLOAT
789 {"reltimefloat", 1, 1, f_reltimefloat},
790#endif
791 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100792 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"remote_foreground", 1, 1, f_remote_foreground},
794 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100795 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200796 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100797 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"remove", 2, 3, f_remove},
799 {"rename", 2, 2, f_rename},
800 {"repeat", 2, 2, f_repeat},
801 {"resolve", 1, 1, f_resolve},
802 {"reverse", 1, 1, f_reverse},
803#ifdef FEAT_FLOAT
804 {"round", 1, 1, f_round},
805#endif
806 {"screenattr", 2, 2, f_screenattr},
807 {"screenchar", 2, 2, f_screenchar},
808 {"screencol", 0, 0, f_screencol},
809 {"screenrow", 0, 0, f_screenrow},
810 {"search", 1, 4, f_search},
811 {"searchdecl", 1, 3, f_searchdecl},
812 {"searchpair", 3, 7, f_searchpair},
813 {"searchpairpos", 3, 7, f_searchpairpos},
814 {"searchpos", 1, 4, f_searchpos},
815 {"server2client", 2, 2, f_server2client},
816 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200817 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"setbufvar", 3, 3, f_setbufvar},
819 {"setcharsearch", 1, 1, f_setcharsearch},
820 {"setcmdpos", 1, 1, f_setcmdpos},
821 {"setfperm", 2, 2, f_setfperm},
822 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200823 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824 {"setmatches", 1, 1, f_setmatches},
825 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200826 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200827 {"setreg", 2, 3, f_setreg},
828 {"settabvar", 3, 3, f_settabvar},
829 {"settabwinvar", 4, 4, f_settabwinvar},
830 {"setwinvar", 3, 3, f_setwinvar},
831#ifdef FEAT_CRYPT
832 {"sha256", 1, 1, f_sha256},
833#endif
834 {"shellescape", 1, 2, f_shellescape},
835 {"shiftwidth", 0, 0, f_shiftwidth},
836 {"simplify", 1, 1, f_simplify},
837#ifdef FEAT_FLOAT
838 {"sin", 1, 1, f_sin},
839 {"sinh", 1, 1, f_sinh},
840#endif
841 {"sort", 1, 3, f_sort},
842 {"soundfold", 1, 1, f_soundfold},
843 {"spellbadword", 0, 1, f_spellbadword},
844 {"spellsuggest", 1, 3, f_spellsuggest},
845 {"split", 1, 3, f_split},
846#ifdef FEAT_FLOAT
847 {"sqrt", 1, 1, f_sqrt},
848 {"str2float", 1, 1, f_str2float},
849#endif
850 {"str2nr", 1, 2, f_str2nr},
851 {"strcharpart", 2, 3, f_strcharpart},
852 {"strchars", 1, 2, f_strchars},
853 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
854#ifdef HAVE_STRFTIME
855 {"strftime", 1, 2, f_strftime},
856#endif
857 {"strgetchar", 2, 2, f_strgetchar},
858 {"stridx", 2, 3, f_stridx},
859 {"string", 1, 1, f_string},
860 {"strlen", 1, 1, f_strlen},
861 {"strpart", 2, 3, f_strpart},
862 {"strridx", 2, 3, f_strridx},
863 {"strtrans", 1, 1, f_strtrans},
864 {"strwidth", 1, 1, f_strwidth},
865 {"submatch", 1, 2, f_submatch},
866 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200867 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"synID", 3, 3, f_synID},
869 {"synIDattr", 2, 3, f_synIDattr},
870 {"synIDtrans", 1, 1, f_synIDtrans},
871 {"synconcealed", 2, 2, f_synconcealed},
872 {"synstack", 2, 2, f_synstack},
873 {"system", 1, 2, f_system},
874 {"systemlist", 1, 2, f_systemlist},
875 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
876 {"tabpagenr", 0, 1, f_tabpagenr},
877 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
878 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100879 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880#ifdef FEAT_FLOAT
881 {"tan", 1, 1, f_tan},
882 {"tanh", 1, 1, f_tanh},
883#endif
884 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200885#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100886 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
887 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100888 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200889 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200890# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
891 {"term_getansicolors", 1, 1, f_term_getansicolors},
892# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200893 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200894 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200896 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200897 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200898 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200899 {"term_getstatus", 1, 1, f_term_getstatus},
900 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200901 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200902 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200903 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200905# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
906 {"term_setansicolors", 2, 2, f_term_setansicolors},
907# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100908 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100909 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200910 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200911 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200912 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
915 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200916 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200917 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100918 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200919#ifdef FEAT_JOB_CHANNEL
920 {"test_null_channel", 0, 0, f_test_null_channel},
921#endif
922 {"test_null_dict", 0, 0, f_test_null_dict},
923#ifdef FEAT_JOB_CHANNEL
924 {"test_null_job", 0, 0, f_test_null_job},
925#endif
926 {"test_null_list", 0, 0, f_test_null_list},
927 {"test_null_partial", 0, 0, f_test_null_partial},
928 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200929 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100930 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200931#ifdef FEAT_GUI
932 {"test_scrollbar", 3, 3, f_test_scrollbar},
933#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934 {"test_settime", 1, 1, f_test_settime},
935#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200936 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200937 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938 {"timer_start", 2, 3, f_timer_start},
939 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200940 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200941#endif
942 {"tolower", 1, 1, f_tolower},
943 {"toupper", 1, 1, f_toupper},
944 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100945 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946#ifdef FEAT_FLOAT
947 {"trunc", 1, 1, f_trunc},
948#endif
949 {"type", 1, 1, f_type},
950 {"undofile", 1, 1, f_undofile},
951 {"undotree", 0, 0, f_undotree},
952 {"uniq", 1, 3, f_uniq},
953 {"values", 1, 1, f_values},
954 {"virtcol", 1, 1, f_virtcol},
955 {"visualmode", 0, 1, f_visualmode},
956 {"wildmenumode", 0, 0, f_wildmenumode},
957 {"win_findbuf", 1, 1, f_win_findbuf},
958 {"win_getid", 0, 2, f_win_getid},
959 {"win_gotoid", 1, 1, f_win_gotoid},
960 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
961 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100962 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200963 {"winbufnr", 1, 1, f_winbufnr},
964 {"wincol", 0, 0, f_wincol},
965 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200966 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200967 {"winline", 0, 0, f_winline},
968 {"winnr", 0, 1, f_winnr},
969 {"winrestcmd", 0, 0, f_winrestcmd},
970 {"winrestview", 1, 1, f_winrestview},
971 {"winsaveview", 0, 0, f_winsaveview},
972 {"winwidth", 1, 1, f_winwidth},
973 {"wordcount", 0, 0, f_wordcount},
974 {"writefile", 2, 3, f_writefile},
975 {"xor", 2, 2, f_xor},
976};
977
978#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
979
980/*
981 * Function given to ExpandGeneric() to obtain the list of internal
982 * or user defined function names.
983 */
984 char_u *
985get_function_name(expand_T *xp, int idx)
986{
987 static int intidx = -1;
988 char_u *name;
989
990 if (idx == 0)
991 intidx = -1;
992 if (intidx < 0)
993 {
994 name = get_user_func_name(xp, idx);
995 if (name != NULL)
996 return name;
997 }
998 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
999 {
1000 STRCPY(IObuff, functions[intidx].f_name);
1001 STRCAT(IObuff, "(");
1002 if (functions[intidx].f_max_argc == 0)
1003 STRCAT(IObuff, ")");
1004 return IObuff;
1005 }
1006
1007 return NULL;
1008}
1009
1010/*
1011 * Function given to ExpandGeneric() to obtain the list of internal or
1012 * user defined variable or function names.
1013 */
1014 char_u *
1015get_expr_name(expand_T *xp, int idx)
1016{
1017 static int intidx = -1;
1018 char_u *name;
1019
1020 if (idx == 0)
1021 intidx = -1;
1022 if (intidx < 0)
1023 {
1024 name = get_function_name(xp, idx);
1025 if (name != NULL)
1026 return name;
1027 }
1028 return get_user_var_name(xp, ++intidx);
1029}
1030
1031#endif /* FEAT_CMDL_COMPL */
1032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001033/*
1034 * Find internal function in table above.
1035 * Return index, or -1 if not found
1036 */
1037 int
1038find_internal_func(
1039 char_u *name) /* name of the function */
1040{
1041 int first = 0;
1042 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1043 int cmp;
1044 int x;
1045
1046 /*
1047 * Find the function name in the table. Binary search.
1048 */
1049 while (first <= last)
1050 {
1051 x = first + ((unsigned)(last - first) >> 1);
1052 cmp = STRCMP(name, functions[x].f_name);
1053 if (cmp < 0)
1054 last = x - 1;
1055 else if (cmp > 0)
1056 first = x + 1;
1057 else
1058 return x;
1059 }
1060 return -1;
1061}
1062
1063 int
1064call_internal_func(
1065 char_u *name,
1066 int argcount,
1067 typval_T *argvars,
1068 typval_T *rettv)
1069{
1070 int i;
1071
1072 i = find_internal_func(name);
1073 if (i < 0)
1074 return ERROR_UNKNOWN;
1075 if (argcount < functions[i].f_min_argc)
1076 return ERROR_TOOFEW;
1077 if (argcount > functions[i].f_max_argc)
1078 return ERROR_TOOMANY;
1079 argvars[argcount].v_type = VAR_UNKNOWN;
1080 functions[i].f_func(argvars, rettv);
1081 return ERROR_NONE;
1082}
1083
1084/*
1085 * Return TRUE for a non-zero Number and a non-empty String.
1086 */
1087 static int
1088non_zero_arg(typval_T *argvars)
1089{
1090 return ((argvars[0].v_type == VAR_NUMBER
1091 && argvars[0].vval.v_number != 0)
1092 || (argvars[0].v_type == VAR_SPECIAL
1093 && argvars[0].vval.v_number == VVAL_TRUE)
1094 || (argvars[0].v_type == VAR_STRING
1095 && argvars[0].vval.v_string != NULL
1096 && *argvars[0].vval.v_string != NUL));
1097}
1098
1099/*
1100 * Get the lnum from the first argument.
1101 * Also accepts ".", "$", etc., but that only works for the current buffer.
1102 * Returns -1 on error.
1103 */
1104 static linenr_T
1105get_tv_lnum(typval_T *argvars)
1106{
1107 typval_T rettv;
1108 linenr_T lnum;
1109
1110 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1111 if (lnum == 0) /* no valid number, try using line() */
1112 {
1113 rettv.v_type = VAR_NUMBER;
1114 f_line(argvars, &rettv);
1115 lnum = (linenr_T)rettv.vval.v_number;
1116 clear_tv(&rettv);
1117 }
1118 return lnum;
1119}
1120
1121#ifdef FEAT_FLOAT
1122static int get_float_arg(typval_T *argvars, float_T *f);
1123
1124/*
1125 * Get the float value of "argvars[0]" into "f".
1126 * Returns FAIL when the argument is not a Number or Float.
1127 */
1128 static int
1129get_float_arg(typval_T *argvars, float_T *f)
1130{
1131 if (argvars[0].v_type == VAR_FLOAT)
1132 {
1133 *f = argvars[0].vval.v_float;
1134 return OK;
1135 }
1136 if (argvars[0].v_type == VAR_NUMBER)
1137 {
1138 *f = (float_T)argvars[0].vval.v_number;
1139 return OK;
1140 }
1141 EMSG(_("E808: Number or Float required"));
1142 return FAIL;
1143}
1144
1145/*
1146 * "abs(expr)" function
1147 */
1148 static void
1149f_abs(typval_T *argvars, typval_T *rettv)
1150{
1151 if (argvars[0].v_type == VAR_FLOAT)
1152 {
1153 rettv->v_type = VAR_FLOAT;
1154 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1155 }
1156 else
1157 {
1158 varnumber_T n;
1159 int error = FALSE;
1160
1161 n = get_tv_number_chk(&argvars[0], &error);
1162 if (error)
1163 rettv->vval.v_number = -1;
1164 else if (n > 0)
1165 rettv->vval.v_number = n;
1166 else
1167 rettv->vval.v_number = -n;
1168 }
1169}
1170
1171/*
1172 * "acos()" function
1173 */
1174 static void
1175f_acos(typval_T *argvars, typval_T *rettv)
1176{
1177 float_T f = 0.0;
1178
1179 rettv->v_type = VAR_FLOAT;
1180 if (get_float_arg(argvars, &f) == OK)
1181 rettv->vval.v_float = acos(f);
1182 else
1183 rettv->vval.v_float = 0.0;
1184}
1185#endif
1186
1187/*
1188 * "add(list, item)" function
1189 */
1190 static void
1191f_add(typval_T *argvars, typval_T *rettv)
1192{
1193 list_T *l;
1194
1195 rettv->vval.v_number = 1; /* Default: Failed */
1196 if (argvars[0].v_type == VAR_LIST)
1197 {
1198 if ((l = argvars[0].vval.v_list) != NULL
1199 && !tv_check_lock(l->lv_lock,
1200 (char_u *)N_("add() argument"), TRUE)
1201 && list_append_tv(l, &argvars[1]) == OK)
1202 copy_tv(&argvars[0], rettv);
1203 }
1204 else
1205 EMSG(_(e_listreq));
1206}
1207
1208/*
1209 * "and(expr, expr)" function
1210 */
1211 static void
1212f_and(typval_T *argvars, typval_T *rettv)
1213{
1214 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1215 & get_tv_number_chk(&argvars[1], NULL);
1216}
1217
1218/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001219 * Get the lnum from the first argument.
1220 * Also accepts "$", then "buf" is used.
1221 * Returns 0 on error.
1222 */
1223 static linenr_T
1224get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1225{
1226 if (argvars[0].v_type == VAR_STRING
1227 && argvars[0].vval.v_string != NULL
1228 && argvars[0].vval.v_string[0] == '$'
1229 && buf != NULL)
1230 return buf->b_ml.ml_line_count;
1231 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1232}
1233
1234/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001235 * If there is a window for "curbuf", make it the current window.
1236 */
1237 static void
1238find_win_for_curbuf(void)
1239{
1240 wininfo_T *wip;
1241
1242 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1243 {
1244 if (wip->wi_win != NULL)
1245 {
1246 curwin = wip->wi_win;
1247 break;
1248 }
1249 }
1250}
1251
1252/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001253 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001254 */
1255 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001256set_buffer_lines(
1257 buf_T *buf,
1258 linenr_T lnum_arg,
1259 int append,
1260 typval_T *lines,
1261 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262{
Bram Moolenaarca851592018-06-06 21:04:07 +02001263 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1264 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 list_T *l = NULL;
1266 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001268 linenr_T append_lnum;
1269 buf_T *curbuf_save = NULL;
1270 win_T *curwin_save = NULL;
1271 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272
Bram Moolenaarca851592018-06-06 21:04:07 +02001273 /* When using the current buffer ml_mfp will be set if needed. Useful when
1274 * setline() is used on startup. For other buffers the buffer must be
1275 * loaded. */
1276 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001278 rettv->vval.v_number = 1; /* FAIL */
1279 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280 }
1281
Bram Moolenaarca851592018-06-06 21:04:07 +02001282 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001284 curbuf_save = curbuf;
1285 curwin_save = curwin;
1286 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001287 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001288 }
1289
1290 if (append)
1291 // appendbufline() uses the line number below which we insert
1292 append_lnum = lnum - 1;
1293 else
1294 // setbufline() uses the line number above which we insert, we only
1295 // append if it's below the last line
1296 append_lnum = curbuf->b_ml.ml_line_count;
1297
1298 if (lines->v_type == VAR_LIST)
1299 {
1300 l = lines->vval.v_list;
1301 li = l->lv_first;
1302 }
1303 else
1304 line = get_tv_string_chk(lines);
1305
1306 /* default result is zero == OK */
1307 for (;;)
1308 {
1309 if (l != NULL)
1310 {
1311 /* list argument, get next string */
1312 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001314 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 li = li->li_next;
1316 }
1317
Bram Moolenaarca851592018-06-06 21:04:07 +02001318 rettv->vval.v_number = 1; /* FAIL */
1319 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1320 break;
1321
1322 /* When coming here from Insert mode, sync undo, so that this can be
1323 * undone separately from what was previously inserted. */
1324 if (u_sync_once == 2)
1325 {
1326 u_sync_once = 1; /* notify that u_sync() was called */
1327 u_sync(TRUE);
1328 }
1329
1330 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1331 {
1332 /* existing line, replace it */
1333 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1334 {
1335 changed_bytes(lnum, 0);
1336 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1337 check_cursor_col();
1338 rettv->vval.v_number = 0; /* OK */
1339 }
1340 }
1341 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1342 {
1343 /* append the line */
1344 ++added;
1345 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1346 rettv->vval.v_number = 0; /* OK */
1347 }
1348
1349 if (l == NULL) /* only one string argument */
1350 break;
1351 ++lnum;
1352 }
1353
1354 if (added > 0)
1355 {
1356 win_T *wp;
1357 tabpage_T *tp;
1358
1359 appended_lines_mark(append_lnum, added);
1360 FOR_ALL_TAB_WINDOWS(tp, wp)
1361 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1362 wp->w_cursor.lnum += added;
1363 check_cursor_col();
1364
Bram Moolenaarf2732452018-06-03 14:47:35 +02001365#ifdef FEAT_JOB_CHANNEL
1366 if (bt_prompt(curbuf) && (State & INSERT))
1367 // show the line with the prompt
1368 update_topline();
1369#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001371
1372 if (!is_curbuf)
1373 {
1374 curbuf = curbuf_save;
1375 curwin = curwin_save;
1376 }
1377}
1378
1379/*
1380 * "append(lnum, string/list)" function
1381 */
1382 static void
1383f_append(typval_T *argvars, typval_T *rettv)
1384{
1385 linenr_T lnum = get_tv_lnum(&argvars[0]);
1386
1387 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1388}
1389
1390/*
1391 * "appendbufline(buf, lnum, string/list)" function
1392 */
1393 static void
1394f_appendbufline(typval_T *argvars, typval_T *rettv)
1395{
1396 linenr_T lnum;
1397 buf_T *buf;
1398
1399 buf = get_buf_tv(&argvars[0], FALSE);
1400 if (buf == NULL)
1401 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 {
1404 lnum = get_tv_lnum_buf(&argvars[1], buf);
1405 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001407}
1408
1409/*
1410 * "argc()" function
1411 */
1412 static void
1413f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1414{
1415 rettv->vval.v_number = ARGCOUNT;
1416}
1417
1418/*
1419 * "argidx()" function
1420 */
1421 static void
1422f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1423{
1424 rettv->vval.v_number = curwin->w_arg_idx;
1425}
1426
1427/*
1428 * "arglistid()" function
1429 */
1430 static void
1431f_arglistid(typval_T *argvars, typval_T *rettv)
1432{
1433 win_T *wp;
1434
1435 rettv->vval.v_number = -1;
1436 wp = find_tabwin(&argvars[0], &argvars[1]);
1437 if (wp != NULL)
1438 rettv->vval.v_number = wp->w_alist->id;
1439}
1440
1441/*
1442 * "argv(nr)" function
1443 */
1444 static void
1445f_argv(typval_T *argvars, typval_T *rettv)
1446{
1447 int idx;
1448
1449 if (argvars[0].v_type != VAR_UNKNOWN)
1450 {
1451 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1452 if (idx >= 0 && idx < ARGCOUNT)
1453 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1454 else
1455 rettv->vval.v_string = NULL;
1456 rettv->v_type = VAR_STRING;
1457 }
1458 else if (rettv_list_alloc(rettv) == OK)
1459 for (idx = 0; idx < ARGCOUNT; ++idx)
1460 list_append_string(rettv->vval.v_list,
1461 alist_name(&ARGLIST[idx]), -1);
1462}
1463
1464/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001465 * "assert_beeps(cmd [, error])" function
1466 */
1467 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001468f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001469{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001470 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001471}
1472
1473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001474 * "assert_equal(expected, actual[, msg])" function
1475 */
1476 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001477f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001478{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001479 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480}
1481
1482/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001483 * "assert_equalfile(fname-one, fname-two)" function
1484 */
1485 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001486f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001487{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001488 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001489}
1490
1491/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492 * "assert_notequal(expected, actual[, msg])" function
1493 */
1494 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001495f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001496{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001497 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498}
1499
1500/*
1501 * "assert_exception(string[, msg])" function
1502 */
1503 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001504f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001506 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507}
1508
1509/*
1510 * "assert_fails(cmd [, error])" function
1511 */
1512 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001513f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001515 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516}
1517
1518/*
1519 * "assert_false(actual[, msg])" function
1520 */
1521 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001522f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001524 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525}
1526
1527/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001528 * "assert_inrange(lower, upper[, msg])" function
1529 */
1530 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001531f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001532{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001533 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001534}
1535
1536/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537 * "assert_match(pattern, actual[, msg])" function
1538 */
1539 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001540f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001542 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001543}
1544
1545/*
1546 * "assert_notmatch(pattern, actual[, msg])" function
1547 */
1548 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001549f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001550{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552}
1553
1554/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001555 * "assert_report(msg)" function
1556 */
1557 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001558f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001559{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001560 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001561}
1562
1563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564 * "assert_true(actual[, msg])" function
1565 */
1566 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001567f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001569 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570}
1571
1572#ifdef FEAT_FLOAT
1573/*
1574 * "asin()" function
1575 */
1576 static void
1577f_asin(typval_T *argvars, typval_T *rettv)
1578{
1579 float_T f = 0.0;
1580
1581 rettv->v_type = VAR_FLOAT;
1582 if (get_float_arg(argvars, &f) == OK)
1583 rettv->vval.v_float = asin(f);
1584 else
1585 rettv->vval.v_float = 0.0;
1586}
1587
1588/*
1589 * "atan()" function
1590 */
1591 static void
1592f_atan(typval_T *argvars, typval_T *rettv)
1593{
1594 float_T f = 0.0;
1595
1596 rettv->v_type = VAR_FLOAT;
1597 if (get_float_arg(argvars, &f) == OK)
1598 rettv->vval.v_float = atan(f);
1599 else
1600 rettv->vval.v_float = 0.0;
1601}
1602
1603/*
1604 * "atan2()" function
1605 */
1606 static void
1607f_atan2(typval_T *argvars, typval_T *rettv)
1608{
1609 float_T fx = 0.0, fy = 0.0;
1610
1611 rettv->v_type = VAR_FLOAT;
1612 if (get_float_arg(argvars, &fx) == OK
1613 && get_float_arg(&argvars[1], &fy) == OK)
1614 rettv->vval.v_float = atan2(fx, fy);
1615 else
1616 rettv->vval.v_float = 0.0;
1617}
1618#endif
1619
1620/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001621 * "balloon_show()" function
1622 */
1623#ifdef FEAT_BEVAL
1624 static void
1625f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1626{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001627 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001628 {
1629 if (argvars[0].v_type == VAR_LIST
1630# ifdef FEAT_GUI
1631 && !gui.in_use
1632# endif
1633 )
1634 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1635 else
1636 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1637 }
1638}
1639
Bram Moolenaar669a8282017-11-19 20:13:05 +01001640# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001641 static void
1642f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1643{
1644 if (rettv_list_alloc(rettv) == OK)
1645 {
1646 char_u *msg = get_tv_string_chk(&argvars[0]);
1647
1648 if (msg != NULL)
1649 {
1650 pumitem_T *array;
1651 int size = split_message(msg, &array);
1652 int i;
1653
1654 /* Skip the first and last item, they are always empty. */
1655 for (i = 1; i < size - 1; ++i)
1656 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001657 while (size > 0)
1658 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001659 vim_free(array);
1660 }
1661 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001662}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001663# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001664#endif
1665
1666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667 * "browse(save, title, initdir, default)" function
1668 */
1669 static void
1670f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1671{
1672#ifdef FEAT_BROWSE
1673 int save;
1674 char_u *title;
1675 char_u *initdir;
1676 char_u *defname;
1677 char_u buf[NUMBUFLEN];
1678 char_u buf2[NUMBUFLEN];
1679 int error = FALSE;
1680
1681 save = (int)get_tv_number_chk(&argvars[0], &error);
1682 title = get_tv_string_chk(&argvars[1]);
1683 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1684 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1685
1686 if (error || title == NULL || initdir == NULL || defname == NULL)
1687 rettv->vval.v_string = NULL;
1688 else
1689 rettv->vval.v_string =
1690 do_browse(save ? BROWSE_SAVE : 0,
1691 title, defname, NULL, initdir, NULL, curbuf);
1692#else
1693 rettv->vval.v_string = NULL;
1694#endif
1695 rettv->v_type = VAR_STRING;
1696}
1697
1698/*
1699 * "browsedir(title, initdir)" function
1700 */
1701 static void
1702f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1703{
1704#ifdef FEAT_BROWSE
1705 char_u *title;
1706 char_u *initdir;
1707 char_u buf[NUMBUFLEN];
1708
1709 title = get_tv_string_chk(&argvars[0]);
1710 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1711
1712 if (title == NULL || initdir == NULL)
1713 rettv->vval.v_string = NULL;
1714 else
1715 rettv->vval.v_string = do_browse(BROWSE_DIR,
1716 title, NULL, NULL, initdir, NULL, curbuf);
1717#else
1718 rettv->vval.v_string = NULL;
1719#endif
1720 rettv->v_type = VAR_STRING;
1721}
1722
1723static buf_T *find_buffer(typval_T *avar);
1724
1725/*
1726 * Find a buffer by number or exact name.
1727 */
1728 static buf_T *
1729find_buffer(typval_T *avar)
1730{
1731 buf_T *buf = NULL;
1732
1733 if (avar->v_type == VAR_NUMBER)
1734 buf = buflist_findnr((int)avar->vval.v_number);
1735 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1736 {
1737 buf = buflist_findname_exp(avar->vval.v_string);
1738 if (buf == NULL)
1739 {
1740 /* No full path name match, try a match with a URL or a "nofile"
1741 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001742 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743 if (buf->b_fname != NULL
1744 && (path_with_url(buf->b_fname)
1745#ifdef FEAT_QUICKFIX
1746 || bt_nofile(buf)
1747#endif
1748 )
1749 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1750 break;
1751 }
1752 }
1753 return buf;
1754}
1755
1756/*
1757 * "bufexists(expr)" function
1758 */
1759 static void
1760f_bufexists(typval_T *argvars, typval_T *rettv)
1761{
1762 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1763}
1764
1765/*
1766 * "buflisted(expr)" function
1767 */
1768 static void
1769f_buflisted(typval_T *argvars, typval_T *rettv)
1770{
1771 buf_T *buf;
1772
1773 buf = find_buffer(&argvars[0]);
1774 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1775}
1776
1777/*
1778 * "bufloaded(expr)" function
1779 */
1780 static void
1781f_bufloaded(typval_T *argvars, typval_T *rettv)
1782{
1783 buf_T *buf;
1784
1785 buf = find_buffer(&argvars[0]);
1786 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1787}
1788
1789 buf_T *
1790buflist_find_by_name(char_u *name, int curtab_only)
1791{
1792 int save_magic;
1793 char_u *save_cpo;
1794 buf_T *buf;
1795
1796 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1797 save_magic = p_magic;
1798 p_magic = TRUE;
1799 save_cpo = p_cpo;
1800 p_cpo = (char_u *)"";
1801
1802 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1803 TRUE, FALSE, curtab_only));
1804
1805 p_magic = save_magic;
1806 p_cpo = save_cpo;
1807 return buf;
1808}
1809
1810/*
1811 * Get buffer by number or pattern.
1812 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001813 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001814get_buf_tv(typval_T *tv, int curtab_only)
1815{
1816 char_u *name = tv->vval.v_string;
1817 buf_T *buf;
1818
1819 if (tv->v_type == VAR_NUMBER)
1820 return buflist_findnr((int)tv->vval.v_number);
1821 if (tv->v_type != VAR_STRING)
1822 return NULL;
1823 if (name == NULL || *name == NUL)
1824 return curbuf;
1825 if (name[0] == '$' && name[1] == NUL)
1826 return lastbuf;
1827
1828 buf = buflist_find_by_name(name, curtab_only);
1829
1830 /* If not found, try expanding the name, like done for bufexists(). */
1831 if (buf == NULL)
1832 buf = find_buffer(tv);
1833
1834 return buf;
1835}
1836
1837/*
1838 * "bufname(expr)" function
1839 */
1840 static void
1841f_bufname(typval_T *argvars, typval_T *rettv)
1842{
1843 buf_T *buf;
1844
1845 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1846 ++emsg_off;
1847 buf = get_buf_tv(&argvars[0], FALSE);
1848 rettv->v_type = VAR_STRING;
1849 if (buf != NULL && buf->b_fname != NULL)
1850 rettv->vval.v_string = vim_strsave(buf->b_fname);
1851 else
1852 rettv->vval.v_string = NULL;
1853 --emsg_off;
1854}
1855
1856/*
1857 * "bufnr(expr)" function
1858 */
1859 static void
1860f_bufnr(typval_T *argvars, typval_T *rettv)
1861{
1862 buf_T *buf;
1863 int error = FALSE;
1864 char_u *name;
1865
1866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1867 ++emsg_off;
1868 buf = get_buf_tv(&argvars[0], FALSE);
1869 --emsg_off;
1870
1871 /* If the buffer isn't found and the second argument is not zero create a
1872 * new buffer. */
1873 if (buf == NULL
1874 && argvars[1].v_type != VAR_UNKNOWN
1875 && get_tv_number_chk(&argvars[1], &error) != 0
1876 && !error
1877 && (name = get_tv_string_chk(&argvars[0])) != NULL
1878 && !error)
1879 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1880
1881 if (buf != NULL)
1882 rettv->vval.v_number = buf->b_fnum;
1883 else
1884 rettv->vval.v_number = -1;
1885}
1886
1887 static void
1888buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1889{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890 win_T *wp;
1891 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 buf_T *buf;
1893
1894 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1895 ++emsg_off;
1896 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001897 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898 {
1899 ++winnr;
1900 if (wp->w_buffer == buf)
1901 break;
1902 }
1903 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001904 --emsg_off;
1905}
1906
1907/*
1908 * "bufwinid(nr)" function
1909 */
1910 static void
1911f_bufwinid(typval_T *argvars, typval_T *rettv)
1912{
1913 buf_win_common(argvars, rettv, FALSE);
1914}
1915
1916/*
1917 * "bufwinnr(nr)" function
1918 */
1919 static void
1920f_bufwinnr(typval_T *argvars, typval_T *rettv)
1921{
1922 buf_win_common(argvars, rettv, TRUE);
1923}
1924
1925/*
1926 * "byte2line(byte)" function
1927 */
1928 static void
1929f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1930{
1931#ifndef FEAT_BYTEOFF
1932 rettv->vval.v_number = -1;
1933#else
1934 long boff = 0;
1935
1936 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1937 if (boff < 0)
1938 rettv->vval.v_number = -1;
1939 else
1940 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1941 (linenr_T)0, &boff);
1942#endif
1943}
1944
1945 static void
1946byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1947{
1948#ifdef FEAT_MBYTE
1949 char_u *t;
1950#endif
1951 char_u *str;
1952 varnumber_T idx;
1953
1954 str = get_tv_string_chk(&argvars[0]);
1955 idx = get_tv_number_chk(&argvars[1], NULL);
1956 rettv->vval.v_number = -1;
1957 if (str == NULL || idx < 0)
1958 return;
1959
1960#ifdef FEAT_MBYTE
1961 t = str;
1962 for ( ; idx > 0; idx--)
1963 {
1964 if (*t == NUL) /* EOL reached */
1965 return;
1966 if (enc_utf8 && comp)
1967 t += utf_ptr2len(t);
1968 else
1969 t += (*mb_ptr2len)(t);
1970 }
1971 rettv->vval.v_number = (varnumber_T)(t - str);
1972#else
1973 if ((size_t)idx <= STRLEN(str))
1974 rettv->vval.v_number = idx;
1975#endif
1976}
1977
1978/*
1979 * "byteidx()" function
1980 */
1981 static void
1982f_byteidx(typval_T *argvars, typval_T *rettv)
1983{
1984 byteidx(argvars, rettv, FALSE);
1985}
1986
1987/*
1988 * "byteidxcomp()" function
1989 */
1990 static void
1991f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1992{
1993 byteidx(argvars, rettv, TRUE);
1994}
1995
1996/*
1997 * "call(func, arglist [, dict])" function
1998 */
1999 static void
2000f_call(typval_T *argvars, typval_T *rettv)
2001{
2002 char_u *func;
2003 partial_T *partial = NULL;
2004 dict_T *selfdict = NULL;
2005
2006 if (argvars[1].v_type != VAR_LIST)
2007 {
2008 EMSG(_(e_listreq));
2009 return;
2010 }
2011 if (argvars[1].vval.v_list == NULL)
2012 return;
2013
2014 if (argvars[0].v_type == VAR_FUNC)
2015 func = argvars[0].vval.v_string;
2016 else if (argvars[0].v_type == VAR_PARTIAL)
2017 {
2018 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002019 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020 }
2021 else
2022 func = get_tv_string(&argvars[0]);
2023 if (*func == NUL)
2024 return; /* type error or empty name */
2025
2026 if (argvars[2].v_type != VAR_UNKNOWN)
2027 {
2028 if (argvars[2].v_type != VAR_DICT)
2029 {
2030 EMSG(_(e_dictreq));
2031 return;
2032 }
2033 selfdict = argvars[2].vval.v_dict;
2034 }
2035
2036 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2037}
2038
2039#ifdef FEAT_FLOAT
2040/*
2041 * "ceil({float})" function
2042 */
2043 static void
2044f_ceil(typval_T *argvars, typval_T *rettv)
2045{
2046 float_T f = 0.0;
2047
2048 rettv->v_type = VAR_FLOAT;
2049 if (get_float_arg(argvars, &f) == OK)
2050 rettv->vval.v_float = ceil(f);
2051 else
2052 rettv->vval.v_float = 0.0;
2053}
2054#endif
2055
2056#ifdef FEAT_JOB_CHANNEL
2057/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002058 * "ch_canread()" function
2059 */
2060 static void
2061f_ch_canread(typval_T *argvars, typval_T *rettv)
2062{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002063 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002064
2065 rettv->vval.v_number = 0;
2066 if (channel != NULL)
2067 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2068 || channel_has_readahead(channel, PART_OUT)
2069 || channel_has_readahead(channel, PART_ERR);
2070}
2071
2072/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 * "ch_close()" function
2074 */
2075 static void
2076f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2077{
2078 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2079
2080 if (channel != NULL)
2081 {
2082 channel_close(channel, FALSE);
2083 channel_clear(channel);
2084 }
2085}
2086
2087/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002088 * "ch_close()" function
2089 */
2090 static void
2091f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2092{
2093 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2094
2095 if (channel != NULL)
2096 channel_close_in(channel);
2097}
2098
2099/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 * "ch_getbufnr()" function
2101 */
2102 static void
2103f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2104{
2105 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2106
2107 rettv->vval.v_number = -1;
2108 if (channel != NULL)
2109 {
2110 char_u *what = get_tv_string(&argvars[1]);
2111 int part;
2112
2113 if (STRCMP(what, "err") == 0)
2114 part = PART_ERR;
2115 else if (STRCMP(what, "out") == 0)
2116 part = PART_OUT;
2117 else if (STRCMP(what, "in") == 0)
2118 part = PART_IN;
2119 else
2120 part = PART_SOCK;
2121 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2122 rettv->vval.v_number =
2123 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2124 }
2125}
2126
2127/*
2128 * "ch_getjob()" function
2129 */
2130 static void
2131f_ch_getjob(typval_T *argvars, typval_T *rettv)
2132{
2133 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2134
2135 if (channel != NULL)
2136 {
2137 rettv->v_type = VAR_JOB;
2138 rettv->vval.v_job = channel->ch_job;
2139 if (channel->ch_job != NULL)
2140 ++channel->ch_job->jv_refcount;
2141 }
2142}
2143
2144/*
2145 * "ch_info()" function
2146 */
2147 static void
2148f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2149{
2150 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2151
2152 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2153 channel_info(channel, rettv->vval.v_dict);
2154}
2155
2156/*
2157 * "ch_log()" function
2158 */
2159 static void
2160f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2161{
2162 char_u *msg = get_tv_string(&argvars[0]);
2163 channel_T *channel = NULL;
2164
2165 if (argvars[1].v_type != VAR_UNKNOWN)
2166 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2167
Bram Moolenaard5359b22018-04-05 22:44:39 +02002168 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169}
2170
2171/*
2172 * "ch_logfile()" function
2173 */
2174 static void
2175f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2176{
2177 char_u *fname;
2178 char_u *opt = (char_u *)"";
2179 char_u buf[NUMBUFLEN];
2180
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002181 /* Don't open a file in restricted mode. */
2182 if (check_restricted() || check_secure())
2183 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184 fname = get_tv_string(&argvars[0]);
2185 if (argvars[1].v_type == VAR_STRING)
2186 opt = get_tv_string_buf(&argvars[1], buf);
2187 ch_logfile(fname, opt);
2188}
2189
2190/*
2191 * "ch_open()" function
2192 */
2193 static void
2194f_ch_open(typval_T *argvars, typval_T *rettv)
2195{
2196 rettv->v_type = VAR_CHANNEL;
2197 if (check_restricted() || check_secure())
2198 return;
2199 rettv->vval.v_channel = channel_open_func(argvars);
2200}
2201
2202/*
2203 * "ch_read()" function
2204 */
2205 static void
2206f_ch_read(typval_T *argvars, typval_T *rettv)
2207{
2208 common_channel_read(argvars, rettv, FALSE);
2209}
2210
2211/*
2212 * "ch_readraw()" function
2213 */
2214 static void
2215f_ch_readraw(typval_T *argvars, typval_T *rettv)
2216{
2217 common_channel_read(argvars, rettv, TRUE);
2218}
2219
2220/*
2221 * "ch_evalexpr()" function
2222 */
2223 static void
2224f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2225{
2226 ch_expr_common(argvars, rettv, TRUE);
2227}
2228
2229/*
2230 * "ch_sendexpr()" function
2231 */
2232 static void
2233f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2234{
2235 ch_expr_common(argvars, rettv, FALSE);
2236}
2237
2238/*
2239 * "ch_evalraw()" function
2240 */
2241 static void
2242f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2243{
2244 ch_raw_common(argvars, rettv, TRUE);
2245}
2246
2247/*
2248 * "ch_sendraw()" function
2249 */
2250 static void
2251f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2252{
2253 ch_raw_common(argvars, rettv, FALSE);
2254}
2255
2256/*
2257 * "ch_setoptions()" function
2258 */
2259 static void
2260f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2261{
2262 channel_T *channel;
2263 jobopt_T opt;
2264
2265 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2266 if (channel == NULL)
2267 return;
2268 clear_job_options(&opt);
2269 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002270 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 channel_set_options(channel, &opt);
2272 free_job_options(&opt);
2273}
2274
2275/*
2276 * "ch_status()" function
2277 */
2278 static void
2279f_ch_status(typval_T *argvars, typval_T *rettv)
2280{
2281 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002282 jobopt_T opt;
2283 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002284
2285 /* return an empty string by default */
2286 rettv->v_type = VAR_STRING;
2287 rettv->vval.v_string = NULL;
2288
2289 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002290
2291 if (argvars[1].v_type != VAR_UNKNOWN)
2292 {
2293 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002294 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002295 && (opt.jo_set & JO_PART))
2296 part = opt.jo_part;
2297 }
2298
2299 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300}
2301#endif
2302
2303/*
2304 * "changenr()" function
2305 */
2306 static void
2307f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2308{
2309 rettv->vval.v_number = curbuf->b_u_seq_cur;
2310}
2311
2312/*
2313 * "char2nr(string)" function
2314 */
2315 static void
2316f_char2nr(typval_T *argvars, typval_T *rettv)
2317{
2318#ifdef FEAT_MBYTE
2319 if (has_mbyte)
2320 {
2321 int utf8 = 0;
2322
2323 if (argvars[1].v_type != VAR_UNKNOWN)
2324 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2325
2326 if (utf8)
2327 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2328 else
2329 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2330 }
2331 else
2332#endif
2333 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2334}
2335
2336/*
2337 * "cindent(lnum)" function
2338 */
2339 static void
2340f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2341{
2342#ifdef FEAT_CINDENT
2343 pos_T pos;
2344 linenr_T lnum;
2345
2346 pos = curwin->w_cursor;
2347 lnum = get_tv_lnum(argvars);
2348 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2349 {
2350 curwin->w_cursor.lnum = lnum;
2351 rettv->vval.v_number = get_c_indent();
2352 curwin->w_cursor = pos;
2353 }
2354 else
2355#endif
2356 rettv->vval.v_number = -1;
2357}
2358
2359/*
2360 * "clearmatches()" function
2361 */
2362 static void
2363f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2364{
2365#ifdef FEAT_SEARCH_EXTRA
2366 clear_matches(curwin);
2367#endif
2368}
2369
2370/*
2371 * "col(string)" function
2372 */
2373 static void
2374f_col(typval_T *argvars, typval_T *rettv)
2375{
2376 colnr_T col = 0;
2377 pos_T *fp;
2378 int fnum = curbuf->b_fnum;
2379
2380 fp = var2fpos(&argvars[0], FALSE, &fnum);
2381 if (fp != NULL && fnum == curbuf->b_fnum)
2382 {
2383 if (fp->col == MAXCOL)
2384 {
2385 /* '> can be MAXCOL, get the length of the line then */
2386 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2387 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2388 else
2389 col = MAXCOL;
2390 }
2391 else
2392 {
2393 col = fp->col + 1;
2394#ifdef FEAT_VIRTUALEDIT
2395 /* col(".") when the cursor is on the NUL at the end of the line
2396 * because of "coladd" can be seen as an extra column. */
2397 if (virtual_active() && fp == &curwin->w_cursor)
2398 {
2399 char_u *p = ml_get_cursor();
2400
2401 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2402 curwin->w_virtcol - curwin->w_cursor.coladd))
2403 {
2404# ifdef FEAT_MBYTE
2405 int l;
2406
2407 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2408 col += l;
2409# else
2410 if (*p != NUL && p[1] == NUL)
2411 ++col;
2412# endif
2413 }
2414 }
2415#endif
2416 }
2417 }
2418 rettv->vval.v_number = col;
2419}
2420
2421#if defined(FEAT_INS_EXPAND)
2422/*
2423 * "complete()" function
2424 */
2425 static void
2426f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2427{
2428 int startcol;
2429
2430 if ((State & INSERT) == 0)
2431 {
2432 EMSG(_("E785: complete() can only be used in Insert mode"));
2433 return;
2434 }
2435
2436 /* Check for undo allowed here, because if something was already inserted
2437 * the line was already saved for undo and this check isn't done. */
2438 if (!undo_allowed())
2439 return;
2440
2441 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2442 {
2443 EMSG(_(e_invarg));
2444 return;
2445 }
2446
2447 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2448 if (startcol <= 0)
2449 return;
2450
2451 set_completion(startcol - 1, argvars[1].vval.v_list);
2452}
2453
2454/*
2455 * "complete_add()" function
2456 */
2457 static void
2458f_complete_add(typval_T *argvars, typval_T *rettv)
2459{
2460 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2461}
2462
2463/*
2464 * "complete_check()" function
2465 */
2466 static void
2467f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2468{
2469 int saved = RedrawingDisabled;
2470
2471 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002472 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002473 rettv->vval.v_number = compl_interrupted;
2474 RedrawingDisabled = saved;
2475}
2476#endif
2477
2478/*
2479 * "confirm(message, buttons[, default [, type]])" function
2480 */
2481 static void
2482f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2483{
2484#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2485 char_u *message;
2486 char_u *buttons = NULL;
2487 char_u buf[NUMBUFLEN];
2488 char_u buf2[NUMBUFLEN];
2489 int def = 1;
2490 int type = VIM_GENERIC;
2491 char_u *typestr;
2492 int error = FALSE;
2493
2494 message = get_tv_string_chk(&argvars[0]);
2495 if (message == NULL)
2496 error = TRUE;
2497 if (argvars[1].v_type != VAR_UNKNOWN)
2498 {
2499 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2500 if (buttons == NULL)
2501 error = TRUE;
2502 if (argvars[2].v_type != VAR_UNKNOWN)
2503 {
2504 def = (int)get_tv_number_chk(&argvars[2], &error);
2505 if (argvars[3].v_type != VAR_UNKNOWN)
2506 {
2507 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2508 if (typestr == NULL)
2509 error = TRUE;
2510 else
2511 {
2512 switch (TOUPPER_ASC(*typestr))
2513 {
2514 case 'E': type = VIM_ERROR; break;
2515 case 'Q': type = VIM_QUESTION; break;
2516 case 'I': type = VIM_INFO; break;
2517 case 'W': type = VIM_WARNING; break;
2518 case 'G': type = VIM_GENERIC; break;
2519 }
2520 }
2521 }
2522 }
2523 }
2524
2525 if (buttons == NULL || *buttons == NUL)
2526 buttons = (char_u *)_("&Ok");
2527
2528 if (!error)
2529 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2530 def, NULL, FALSE);
2531#endif
2532}
2533
2534/*
2535 * "copy()" function
2536 */
2537 static void
2538f_copy(typval_T *argvars, typval_T *rettv)
2539{
2540 item_copy(&argvars[0], rettv, FALSE, 0);
2541}
2542
2543#ifdef FEAT_FLOAT
2544/*
2545 * "cos()" function
2546 */
2547 static void
2548f_cos(typval_T *argvars, typval_T *rettv)
2549{
2550 float_T f = 0.0;
2551
2552 rettv->v_type = VAR_FLOAT;
2553 if (get_float_arg(argvars, &f) == OK)
2554 rettv->vval.v_float = cos(f);
2555 else
2556 rettv->vval.v_float = 0.0;
2557}
2558
2559/*
2560 * "cosh()" function
2561 */
2562 static void
2563f_cosh(typval_T *argvars, typval_T *rettv)
2564{
2565 float_T f = 0.0;
2566
2567 rettv->v_type = VAR_FLOAT;
2568 if (get_float_arg(argvars, &f) == OK)
2569 rettv->vval.v_float = cosh(f);
2570 else
2571 rettv->vval.v_float = 0.0;
2572}
2573#endif
2574
2575/*
2576 * "count()" function
2577 */
2578 static void
2579f_count(typval_T *argvars, typval_T *rettv)
2580{
2581 long n = 0;
2582 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002583 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002584
Bram Moolenaar9966b212017-07-28 16:46:57 +02002585 if (argvars[2].v_type != VAR_UNKNOWN)
2586 ic = (int)get_tv_number_chk(&argvars[2], &error);
2587
2588 if (argvars[0].v_type == VAR_STRING)
2589 {
2590 char_u *expr = get_tv_string_chk(&argvars[1]);
2591 char_u *p = argvars[0].vval.v_string;
2592 char_u *next;
2593
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002594 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002595 {
2596 if (ic)
2597 {
2598 size_t len = STRLEN(expr);
2599
2600 while (*p != NUL)
2601 {
2602 if (MB_STRNICMP(p, expr, len) == 0)
2603 {
2604 ++n;
2605 p += len;
2606 }
2607 else
2608 MB_PTR_ADV(p);
2609 }
2610 }
2611 else
2612 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2613 != NULL)
2614 {
2615 ++n;
2616 p = next + STRLEN(expr);
2617 }
2618 }
2619
2620 }
2621 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002622 {
2623 listitem_T *li;
2624 list_T *l;
2625 long idx;
2626
2627 if ((l = argvars[0].vval.v_list) != NULL)
2628 {
2629 li = l->lv_first;
2630 if (argvars[2].v_type != VAR_UNKNOWN)
2631 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632 if (argvars[3].v_type != VAR_UNKNOWN)
2633 {
2634 idx = (long)get_tv_number_chk(&argvars[3], &error);
2635 if (!error)
2636 {
2637 li = list_find(l, idx);
2638 if (li == NULL)
2639 EMSGN(_(e_listidx), idx);
2640 }
2641 }
2642 if (error)
2643 li = NULL;
2644 }
2645
2646 for ( ; li != NULL; li = li->li_next)
2647 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2648 ++n;
2649 }
2650 }
2651 else if (argvars[0].v_type == VAR_DICT)
2652 {
2653 int todo;
2654 dict_T *d;
2655 hashitem_T *hi;
2656
2657 if ((d = argvars[0].vval.v_dict) != NULL)
2658 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 if (argvars[2].v_type != VAR_UNKNOWN)
2660 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (argvars[3].v_type != VAR_UNKNOWN)
2662 EMSG(_(e_invarg));
2663 }
2664
2665 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2666 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2667 {
2668 if (!HASHITEM_EMPTY(hi))
2669 {
2670 --todo;
2671 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2672 ++n;
2673 }
2674 }
2675 }
2676 }
2677 else
2678 EMSG2(_(e_listdictarg), "count()");
2679 rettv->vval.v_number = n;
2680}
2681
2682/*
2683 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2684 *
2685 * Checks the existence of a cscope connection.
2686 */
2687 static void
2688f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2689{
2690#ifdef FEAT_CSCOPE
2691 int num = 0;
2692 char_u *dbpath = NULL;
2693 char_u *prepend = NULL;
2694 char_u buf[NUMBUFLEN];
2695
2696 if (argvars[0].v_type != VAR_UNKNOWN
2697 && argvars[1].v_type != VAR_UNKNOWN)
2698 {
2699 num = (int)get_tv_number(&argvars[0]);
2700 dbpath = get_tv_string(&argvars[1]);
2701 if (argvars[2].v_type != VAR_UNKNOWN)
2702 prepend = get_tv_string_buf(&argvars[2], buf);
2703 }
2704
2705 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2706#endif
2707}
2708
2709/*
2710 * "cursor(lnum, col)" function, or
2711 * "cursor(list)"
2712 *
2713 * Moves the cursor to the specified line and column.
2714 * Returns 0 when the position could be set, -1 otherwise.
2715 */
2716 static void
2717f_cursor(typval_T *argvars, typval_T *rettv)
2718{
2719 long line, col;
2720#ifdef FEAT_VIRTUALEDIT
2721 long coladd = 0;
2722#endif
2723 int set_curswant = TRUE;
2724
2725 rettv->vval.v_number = -1;
2726 if (argvars[1].v_type == VAR_UNKNOWN)
2727 {
2728 pos_T pos;
2729 colnr_T curswant = -1;
2730
2731 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2732 {
2733 EMSG(_(e_invarg));
2734 return;
2735 }
2736 line = pos.lnum;
2737 col = pos.col;
2738#ifdef FEAT_VIRTUALEDIT
2739 coladd = pos.coladd;
2740#endif
2741 if (curswant >= 0)
2742 {
2743 curwin->w_curswant = curswant - 1;
2744 set_curswant = FALSE;
2745 }
2746 }
2747 else
2748 {
2749 line = get_tv_lnum(argvars);
2750 col = (long)get_tv_number_chk(&argvars[1], NULL);
2751#ifdef FEAT_VIRTUALEDIT
2752 if (argvars[2].v_type != VAR_UNKNOWN)
2753 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2754#endif
2755 }
2756 if (line < 0 || col < 0
2757#ifdef FEAT_VIRTUALEDIT
2758 || coladd < 0
2759#endif
2760 )
2761 return; /* type error; errmsg already given */
2762 if (line > 0)
2763 curwin->w_cursor.lnum = line;
2764 if (col > 0)
2765 curwin->w_cursor.col = col - 1;
2766#ifdef FEAT_VIRTUALEDIT
2767 curwin->w_cursor.coladd = coladd;
2768#endif
2769
2770 /* Make sure the cursor is in a valid position. */
2771 check_cursor();
2772#ifdef FEAT_MBYTE
2773 /* Correct cursor for multi-byte character. */
2774 if (has_mbyte)
2775 mb_adjust_cursor();
2776#endif
2777
2778 curwin->w_set_curswant = set_curswant;
2779 rettv->vval.v_number = 0;
2780}
2781
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002782#ifdef WIN3264
2783/*
2784 * "debugbreak()" function
2785 */
2786 static void
2787f_debugbreak(typval_T *argvars, typval_T *rettv)
2788{
2789 int pid;
2790
2791 rettv->vval.v_number = FAIL;
2792 pid = (int)get_tv_number(&argvars[0]);
2793 if (pid == 0)
2794 EMSG(_(e_invarg));
2795 else
2796 {
2797 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2798
2799 if (hProcess != NULL)
2800 {
2801 DebugBreakProcess(hProcess);
2802 CloseHandle(hProcess);
2803 rettv->vval.v_number = OK;
2804 }
2805 }
2806}
2807#endif
2808
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002809/*
2810 * "deepcopy()" function
2811 */
2812 static void
2813f_deepcopy(typval_T *argvars, typval_T *rettv)
2814{
2815 int noref = 0;
2816 int copyID;
2817
2818 if (argvars[1].v_type != VAR_UNKNOWN)
2819 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2820 if (noref < 0 || noref > 1)
2821 EMSG(_(e_invarg));
2822 else
2823 {
2824 copyID = get_copyID();
2825 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2826 }
2827}
2828
2829/*
2830 * "delete()" function
2831 */
2832 static void
2833f_delete(typval_T *argvars, typval_T *rettv)
2834{
2835 char_u nbuf[NUMBUFLEN];
2836 char_u *name;
2837 char_u *flags;
2838
2839 rettv->vval.v_number = -1;
2840 if (check_restricted() || check_secure())
2841 return;
2842
2843 name = get_tv_string(&argvars[0]);
2844 if (name == NULL || *name == NUL)
2845 {
2846 EMSG(_(e_invarg));
2847 return;
2848 }
2849
2850 if (argvars[1].v_type != VAR_UNKNOWN)
2851 flags = get_tv_string_buf(&argvars[1], nbuf);
2852 else
2853 flags = (char_u *)"";
2854
2855 if (*flags == NUL)
2856 /* delete a file */
2857 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2858 else if (STRCMP(flags, "d") == 0)
2859 /* delete an empty directory */
2860 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2861 else if (STRCMP(flags, "rf") == 0)
2862 /* delete a directory recursively */
2863 rettv->vval.v_number = delete_recursive(name);
2864 else
2865 EMSG2(_(e_invexpr2), flags);
2866}
2867
2868/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002869 * "deletebufline()" function
2870 */
2871 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002872f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002873{
2874 buf_T *buf;
2875 linenr_T first, last;
2876 linenr_T lnum;
2877 long count;
2878 int is_curbuf;
2879 buf_T *curbuf_save = NULL;
2880 win_T *curwin_save = NULL;
2881 tabpage_T *tp;
2882 win_T *wp;
2883
2884 buf = get_buf_tv(&argvars[0], FALSE);
2885 if (buf == NULL)
2886 {
2887 rettv->vval.v_number = 1; /* FAIL */
2888 return;
2889 }
2890 is_curbuf = buf == curbuf;
2891
2892 first = get_tv_lnum_buf(&argvars[1], buf);
2893 if (argvars[2].v_type != VAR_UNKNOWN)
2894 last = get_tv_lnum_buf(&argvars[2], buf);
2895 else
2896 last = first;
2897
2898 if (buf->b_ml.ml_mfp == NULL || first < 1
2899 || first > buf->b_ml.ml_line_count || last < first)
2900 {
2901 rettv->vval.v_number = 1; /* FAIL */
2902 return;
2903 }
2904
2905 if (!is_curbuf)
2906 {
2907 curbuf_save = curbuf;
2908 curwin_save = curwin;
2909 curbuf = buf;
2910 find_win_for_curbuf();
2911 }
2912 if (last > curbuf->b_ml.ml_line_count)
2913 last = curbuf->b_ml.ml_line_count;
2914 count = last - first + 1;
2915
2916 // When coming here from Insert mode, sync undo, so that this can be
2917 // undone separately from what was previously inserted.
2918 if (u_sync_once == 2)
2919 {
2920 u_sync_once = 1; // notify that u_sync() was called
2921 u_sync(TRUE);
2922 }
2923
2924 if (u_save(first - 1, last + 1) == FAIL)
2925 {
2926 rettv->vval.v_number = 1; /* FAIL */
2927 return;
2928 }
2929
2930 for (lnum = first; lnum <= last; ++lnum)
2931 ml_delete(first, TRUE);
2932
2933 FOR_ALL_TAB_WINDOWS(tp, wp)
2934 if (wp->w_buffer == buf)
2935 {
2936 if (wp->w_cursor.lnum > last)
2937 wp->w_cursor.lnum -= count;
2938 else if (wp->w_cursor.lnum> first)
2939 wp->w_cursor.lnum = first;
2940 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2941 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2942 }
2943 check_cursor_col();
2944 deleted_lines_mark(first, count);
2945
2946 if (!is_curbuf)
2947 {
2948 curbuf = curbuf_save;
2949 curwin = curwin_save;
2950 }
2951}
2952
2953/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 * "did_filetype()" function
2955 */
2956 static void
2957f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2958{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002959 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960}
2961
2962/*
2963 * "diff_filler()" function
2964 */
2965 static void
2966f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2967{
2968#ifdef FEAT_DIFF
2969 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2970#endif
2971}
2972
2973/*
2974 * "diff_hlID()" function
2975 */
2976 static void
2977f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2978{
2979#ifdef FEAT_DIFF
2980 linenr_T lnum = get_tv_lnum(argvars);
2981 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002982 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 static int fnum = 0;
2984 static int change_start = 0;
2985 static int change_end = 0;
2986 static hlf_T hlID = (hlf_T)0;
2987 int filler_lines;
2988 int col;
2989
2990 if (lnum < 0) /* ignore type error in {lnum} arg */
2991 lnum = 0;
2992 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002993 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 || fnum != curbuf->b_fnum)
2995 {
2996 /* New line, buffer, change: need to get the values. */
2997 filler_lines = diff_check(curwin, lnum);
2998 if (filler_lines < 0)
2999 {
3000 if (filler_lines == -1)
3001 {
3002 change_start = MAXCOL;
3003 change_end = -1;
3004 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3005 hlID = HLF_ADD; /* added line */
3006 else
3007 hlID = HLF_CHD; /* changed line */
3008 }
3009 else
3010 hlID = HLF_ADD; /* added line */
3011 }
3012 else
3013 hlID = (hlf_T)0;
3014 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003015 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 fnum = curbuf->b_fnum;
3017 }
3018
3019 if (hlID == HLF_CHD || hlID == HLF_TXD)
3020 {
3021 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3022 if (col >= change_start && col <= change_end)
3023 hlID = HLF_TXD; /* changed text */
3024 else
3025 hlID = HLF_CHD; /* changed line */
3026 }
3027 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3028#endif
3029}
3030
3031/*
3032 * "empty({expr})" function
3033 */
3034 static void
3035f_empty(typval_T *argvars, typval_T *rettv)
3036{
3037 int n = FALSE;
3038
3039 switch (argvars[0].v_type)
3040 {
3041 case VAR_STRING:
3042 case VAR_FUNC:
3043 n = argvars[0].vval.v_string == NULL
3044 || *argvars[0].vval.v_string == NUL;
3045 break;
3046 case VAR_PARTIAL:
3047 n = FALSE;
3048 break;
3049 case VAR_NUMBER:
3050 n = argvars[0].vval.v_number == 0;
3051 break;
3052 case VAR_FLOAT:
3053#ifdef FEAT_FLOAT
3054 n = argvars[0].vval.v_float == 0.0;
3055 break;
3056#endif
3057 case VAR_LIST:
3058 n = argvars[0].vval.v_list == NULL
3059 || argvars[0].vval.v_list->lv_first == NULL;
3060 break;
3061 case VAR_DICT:
3062 n = argvars[0].vval.v_dict == NULL
3063 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3064 break;
3065 case VAR_SPECIAL:
3066 n = argvars[0].vval.v_number != VVAL_TRUE;
3067 break;
3068
3069 case VAR_JOB:
3070#ifdef FEAT_JOB_CHANNEL
3071 n = argvars[0].vval.v_job == NULL
3072 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3073 break;
3074#endif
3075 case VAR_CHANNEL:
3076#ifdef FEAT_JOB_CHANNEL
3077 n = argvars[0].vval.v_channel == NULL
3078 || !channel_is_open(argvars[0].vval.v_channel);
3079 break;
3080#endif
3081 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003082 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 n = TRUE;
3084 break;
3085 }
3086
3087 rettv->vval.v_number = n;
3088}
3089
3090/*
3091 * "escape({string}, {chars})" function
3092 */
3093 static void
3094f_escape(typval_T *argvars, typval_T *rettv)
3095{
3096 char_u buf[NUMBUFLEN];
3097
3098 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3099 get_tv_string_buf(&argvars[1], buf));
3100 rettv->v_type = VAR_STRING;
3101}
3102
3103/*
3104 * "eval()" function
3105 */
3106 static void
3107f_eval(typval_T *argvars, typval_T *rettv)
3108{
3109 char_u *s, *p;
3110
3111 s = get_tv_string_chk(&argvars[0]);
3112 if (s != NULL)
3113 s = skipwhite(s);
3114
3115 p = s;
3116 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3117 {
3118 if (p != NULL && !aborting())
3119 EMSG2(_(e_invexpr2), p);
3120 need_clr_eos = FALSE;
3121 rettv->v_type = VAR_NUMBER;
3122 rettv->vval.v_number = 0;
3123 }
3124 else if (*s != NUL)
3125 EMSG(_(e_trailing));
3126}
3127
3128/*
3129 * "eventhandler()" function
3130 */
3131 static void
3132f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3133{
3134 rettv->vval.v_number = vgetc_busy;
3135}
3136
3137/*
3138 * "executable()" function
3139 */
3140 static void
3141f_executable(typval_T *argvars, typval_T *rettv)
3142{
3143 char_u *name = get_tv_string(&argvars[0]);
3144
3145 /* Check in $PATH and also check directly if there is a directory name. */
3146 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3147 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3148}
3149
3150static garray_T redir_execute_ga;
3151
3152/*
3153 * Append "value[value_len]" to the execute() output.
3154 */
3155 void
3156execute_redir_str(char_u *value, int value_len)
3157{
3158 int len;
3159
3160 if (value_len == -1)
3161 len = (int)STRLEN(value); /* Append the entire string */
3162 else
3163 len = value_len; /* Append only "value_len" characters */
3164 if (ga_grow(&redir_execute_ga, len) == OK)
3165 {
3166 mch_memmove((char *)redir_execute_ga.ga_data
3167 + redir_execute_ga.ga_len, value, len);
3168 redir_execute_ga.ga_len += len;
3169 }
3170}
3171
3172/*
3173 * Get next line from a list.
3174 * Called by do_cmdline() to get the next line.
3175 * Returns allocated string, or NULL for end of function.
3176 */
3177
3178 static char_u *
3179get_list_line(
3180 int c UNUSED,
3181 void *cookie,
3182 int indent UNUSED)
3183{
3184 listitem_T **p = (listitem_T **)cookie;
3185 listitem_T *item = *p;
3186 char_u buf[NUMBUFLEN];
3187 char_u *s;
3188
3189 if (item == NULL)
3190 return NULL;
3191 s = get_tv_string_buf_chk(&item->li_tv, buf);
3192 *p = item->li_next;
3193 return s == NULL ? NULL : vim_strsave(s);
3194}
3195
3196/*
3197 * "execute()" function
3198 */
3199 static void
3200f_execute(typval_T *argvars, typval_T *rettv)
3201{
3202 char_u *cmd = NULL;
3203 list_T *list = NULL;
3204 int save_msg_silent = msg_silent;
3205 int save_emsg_silent = emsg_silent;
3206 int save_emsg_noredir = emsg_noredir;
3207 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003208 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209 garray_T save_ga;
3210
3211 rettv->vval.v_string = NULL;
3212 rettv->v_type = VAR_STRING;
3213
3214 if (argvars[0].v_type == VAR_LIST)
3215 {
3216 list = argvars[0].vval.v_list;
3217 if (list == NULL || list->lv_first == NULL)
3218 /* empty list, no commands, empty output */
3219 return;
3220 ++list->lv_refcount;
3221 }
3222 else
3223 {
3224 cmd = get_tv_string_chk(&argvars[0]);
3225 if (cmd == NULL)
3226 return;
3227 }
3228
3229 if (argvars[1].v_type != VAR_UNKNOWN)
3230 {
3231 char_u buf[NUMBUFLEN];
3232 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3233
3234 if (s == NULL)
3235 return;
3236 if (STRNCMP(s, "silent", 6) == 0)
3237 ++msg_silent;
3238 if (STRCMP(s, "silent!") == 0)
3239 {
3240 emsg_silent = TRUE;
3241 emsg_noredir = TRUE;
3242 }
3243 }
3244 else
3245 ++msg_silent;
3246
3247 if (redir_execute)
3248 save_ga = redir_execute_ga;
3249 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3250 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003251 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252
3253 if (cmd != NULL)
3254 do_cmdline_cmd(cmd);
3255 else
3256 {
3257 listitem_T *item = list->lv_first;
3258
3259 do_cmdline(NULL, get_list_line, (void *)&item,
3260 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3261 --list->lv_refcount;
3262 }
3263
Bram Moolenaard297f352017-01-29 20:31:21 +01003264 /* Need to append a NUL to the result. */
3265 if (ga_grow(&redir_execute_ga, 1) == OK)
3266 {
3267 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3268 rettv->vval.v_string = redir_execute_ga.ga_data;
3269 }
3270 else
3271 {
3272 ga_clear(&redir_execute_ga);
3273 rettv->vval.v_string = NULL;
3274 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 msg_silent = save_msg_silent;
3276 emsg_silent = save_emsg_silent;
3277 emsg_noredir = save_emsg_noredir;
3278
3279 redir_execute = save_redir_execute;
3280 if (redir_execute)
3281 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003282 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003283
3284 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3285 * line. Put it back in the first column. */
3286 msg_col = 0;
3287}
3288
3289/*
3290 * "exepath()" function
3291 */
3292 static void
3293f_exepath(typval_T *argvars, typval_T *rettv)
3294{
3295 char_u *p = NULL;
3296
3297 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3298 rettv->v_type = VAR_STRING;
3299 rettv->vval.v_string = p;
3300}
3301
3302/*
3303 * "exists()" function
3304 */
3305 static void
3306f_exists(typval_T *argvars, typval_T *rettv)
3307{
3308 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003309 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003310
3311 p = get_tv_string(&argvars[0]);
3312 if (*p == '$') /* environment variable */
3313 {
3314 /* first try "normal" environment variables (fast) */
3315 if (mch_getenv(p + 1) != NULL)
3316 n = TRUE;
3317 else
3318 {
3319 /* try expanding things like $VIM and ${HOME} */
3320 p = expand_env_save(p);
3321 if (p != NULL && *p != '$')
3322 n = TRUE;
3323 vim_free(p);
3324 }
3325 }
3326 else if (*p == '&' || *p == '+') /* option */
3327 {
3328 n = (get_option_tv(&p, NULL, TRUE) == OK);
3329 if (*skipwhite(p) != NUL)
3330 n = FALSE; /* trailing garbage */
3331 }
3332 else if (*p == '*') /* internal or user defined function */
3333 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003334 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003335 }
3336 else if (*p == ':')
3337 {
3338 n = cmd_exists(p + 1);
3339 }
3340 else if (*p == '#')
3341 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 if (p[1] == '#')
3343 n = autocmd_supported(p + 2);
3344 else
3345 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003346 }
3347 else /* internal variable */
3348 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003349 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 }
3351
3352 rettv->vval.v_number = n;
3353}
3354
3355#ifdef FEAT_FLOAT
3356/*
3357 * "exp()" function
3358 */
3359 static void
3360f_exp(typval_T *argvars, typval_T *rettv)
3361{
3362 float_T f = 0.0;
3363
3364 rettv->v_type = VAR_FLOAT;
3365 if (get_float_arg(argvars, &f) == OK)
3366 rettv->vval.v_float = exp(f);
3367 else
3368 rettv->vval.v_float = 0.0;
3369}
3370#endif
3371
3372/*
3373 * "expand()" function
3374 */
3375 static void
3376f_expand(typval_T *argvars, typval_T *rettv)
3377{
3378 char_u *s;
3379 int len;
3380 char_u *errormsg;
3381 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3382 expand_T xpc;
3383 int error = FALSE;
3384 char_u *result;
3385
3386 rettv->v_type = VAR_STRING;
3387 if (argvars[1].v_type != VAR_UNKNOWN
3388 && argvars[2].v_type != VAR_UNKNOWN
3389 && get_tv_number_chk(&argvars[2], &error)
3390 && !error)
3391 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003392 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003393 }
3394
3395 s = get_tv_string(&argvars[0]);
3396 if (*s == '%' || *s == '#' || *s == '<')
3397 {
3398 ++emsg_off;
3399 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3400 --emsg_off;
3401 if (rettv->v_type == VAR_LIST)
3402 {
3403 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3404 list_append_string(rettv->vval.v_list, result, -1);
3405 else
3406 vim_free(result);
3407 }
3408 else
3409 rettv->vval.v_string = result;
3410 }
3411 else
3412 {
3413 /* When the optional second argument is non-zero, don't remove matches
3414 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3415 if (argvars[1].v_type != VAR_UNKNOWN
3416 && get_tv_number_chk(&argvars[1], &error))
3417 options |= WILD_KEEP_ALL;
3418 if (!error)
3419 {
3420 ExpandInit(&xpc);
3421 xpc.xp_context = EXPAND_FILES;
3422 if (p_wic)
3423 options += WILD_ICASE;
3424 if (rettv->v_type == VAR_STRING)
3425 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3426 options, WILD_ALL);
3427 else if (rettv_list_alloc(rettv) != FAIL)
3428 {
3429 int i;
3430
3431 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3432 for (i = 0; i < xpc.xp_numfiles; i++)
3433 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3434 ExpandCleanup(&xpc);
3435 }
3436 }
3437 else
3438 rettv->vval.v_string = NULL;
3439 }
3440}
3441
3442/*
3443 * "extend(list, list [, idx])" function
3444 * "extend(dict, dict [, action])" function
3445 */
3446 static void
3447f_extend(typval_T *argvars, typval_T *rettv)
3448{
3449 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3450
3451 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3452 {
3453 list_T *l1, *l2;
3454 listitem_T *item;
3455 long before;
3456 int error = FALSE;
3457
3458 l1 = argvars[0].vval.v_list;
3459 l2 = argvars[1].vval.v_list;
3460 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3461 && l2 != NULL)
3462 {
3463 if (argvars[2].v_type != VAR_UNKNOWN)
3464 {
3465 before = (long)get_tv_number_chk(&argvars[2], &error);
3466 if (error)
3467 return; /* type error; errmsg already given */
3468
3469 if (before == l1->lv_len)
3470 item = NULL;
3471 else
3472 {
3473 item = list_find(l1, before);
3474 if (item == NULL)
3475 {
3476 EMSGN(_(e_listidx), before);
3477 return;
3478 }
3479 }
3480 }
3481 else
3482 item = NULL;
3483 list_extend(l1, l2, item);
3484
3485 copy_tv(&argvars[0], rettv);
3486 }
3487 }
3488 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3489 {
3490 dict_T *d1, *d2;
3491 char_u *action;
3492 int i;
3493
3494 d1 = argvars[0].vval.v_dict;
3495 d2 = argvars[1].vval.v_dict;
3496 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3497 && d2 != NULL)
3498 {
3499 /* Check the third argument. */
3500 if (argvars[2].v_type != VAR_UNKNOWN)
3501 {
3502 static char *(av[]) = {"keep", "force", "error"};
3503
3504 action = get_tv_string_chk(&argvars[2]);
3505 if (action == NULL)
3506 return; /* type error; errmsg already given */
3507 for (i = 0; i < 3; ++i)
3508 if (STRCMP(action, av[i]) == 0)
3509 break;
3510 if (i == 3)
3511 {
3512 EMSG2(_(e_invarg2), action);
3513 return;
3514 }
3515 }
3516 else
3517 action = (char_u *)"force";
3518
3519 dict_extend(d1, d2, action);
3520
3521 copy_tv(&argvars[0], rettv);
3522 }
3523 }
3524 else
3525 EMSG2(_(e_listdictarg), "extend()");
3526}
3527
3528/*
3529 * "feedkeys()" function
3530 */
3531 static void
3532f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3533{
3534 int remap = TRUE;
3535 int insert = FALSE;
3536 char_u *keys, *flags;
3537 char_u nbuf[NUMBUFLEN];
3538 int typed = FALSE;
3539 int execute = FALSE;
3540 int dangerous = FALSE;
3541 char_u *keys_esc;
3542
3543 /* This is not allowed in the sandbox. If the commands would still be
3544 * executed in the sandbox it would be OK, but it probably happens later,
3545 * when "sandbox" is no longer set. */
3546 if (check_secure())
3547 return;
3548
3549 keys = get_tv_string(&argvars[0]);
3550
3551 if (argvars[1].v_type != VAR_UNKNOWN)
3552 {
3553 flags = get_tv_string_buf(&argvars[1], nbuf);
3554 for ( ; *flags != NUL; ++flags)
3555 {
3556 switch (*flags)
3557 {
3558 case 'n': remap = FALSE; break;
3559 case 'm': remap = TRUE; break;
3560 case 't': typed = TRUE; break;
3561 case 'i': insert = TRUE; break;
3562 case 'x': execute = TRUE; break;
3563 case '!': dangerous = TRUE; break;
3564 }
3565 }
3566 }
3567
3568 if (*keys != NUL || execute)
3569 {
3570 /* Need to escape K_SPECIAL and CSI before putting the string in the
3571 * typeahead buffer. */
3572 keys_esc = vim_strsave_escape_csi(keys);
3573 if (keys_esc != NULL)
3574 {
3575 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3576 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3577 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003578 if (vgetc_busy
3579#ifdef FEAT_TIMERS
3580 || timer_busy
3581#endif
3582 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003583 typebuf_was_filled = TRUE;
3584 if (execute)
3585 {
3586 int save_msg_scroll = msg_scroll;
3587
3588 /* Avoid a 1 second delay when the keys start Insert mode. */
3589 msg_scroll = FALSE;
3590
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003591 if (!dangerous)
3592 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003593 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003594 if (!dangerous)
3595 --ex_normal_busy;
3596
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597 msg_scroll |= save_msg_scroll;
3598 }
3599 }
3600 }
3601}
3602
3603/*
3604 * "filereadable()" function
3605 */
3606 static void
3607f_filereadable(typval_T *argvars, typval_T *rettv)
3608{
3609 int fd;
3610 char_u *p;
3611 int n;
3612
3613#ifndef O_NONBLOCK
3614# define O_NONBLOCK 0
3615#endif
3616 p = get_tv_string(&argvars[0]);
3617 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3618 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3619 {
3620 n = TRUE;
3621 close(fd);
3622 }
3623 else
3624 n = FALSE;
3625
3626 rettv->vval.v_number = n;
3627}
3628
3629/*
3630 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3631 * rights to write into.
3632 */
3633 static void
3634f_filewritable(typval_T *argvars, typval_T *rettv)
3635{
3636 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3637}
3638
3639 static void
3640findfilendir(
3641 typval_T *argvars UNUSED,
3642 typval_T *rettv,
3643 int find_what UNUSED)
3644{
3645#ifdef FEAT_SEARCHPATH
3646 char_u *fname;
3647 char_u *fresult = NULL;
3648 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3649 char_u *p;
3650 char_u pathbuf[NUMBUFLEN];
3651 int count = 1;
3652 int first = TRUE;
3653 int error = FALSE;
3654#endif
3655
3656 rettv->vval.v_string = NULL;
3657 rettv->v_type = VAR_STRING;
3658
3659#ifdef FEAT_SEARCHPATH
3660 fname = get_tv_string(&argvars[0]);
3661
3662 if (argvars[1].v_type != VAR_UNKNOWN)
3663 {
3664 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3665 if (p == NULL)
3666 error = TRUE;
3667 else
3668 {
3669 if (*p != NUL)
3670 path = p;
3671
3672 if (argvars[2].v_type != VAR_UNKNOWN)
3673 count = (int)get_tv_number_chk(&argvars[2], &error);
3674 }
3675 }
3676
3677 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3678 error = TRUE;
3679
3680 if (*fname != NUL && !error)
3681 {
3682 do
3683 {
3684 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3685 vim_free(fresult);
3686 fresult = find_file_in_path_option(first ? fname : NULL,
3687 first ? (int)STRLEN(fname) : 0,
3688 0, first, path,
3689 find_what,
3690 curbuf->b_ffname,
3691 find_what == FINDFILE_DIR
3692 ? (char_u *)"" : curbuf->b_p_sua);
3693 first = FALSE;
3694
3695 if (fresult != NULL && rettv->v_type == VAR_LIST)
3696 list_append_string(rettv->vval.v_list, fresult, -1);
3697
3698 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3699 }
3700
3701 if (rettv->v_type == VAR_STRING)
3702 rettv->vval.v_string = fresult;
3703#endif
3704}
3705
3706/*
3707 * "filter()" function
3708 */
3709 static void
3710f_filter(typval_T *argvars, typval_T *rettv)
3711{
3712 filter_map(argvars, rettv, FALSE);
3713}
3714
3715/*
3716 * "finddir({fname}[, {path}[, {count}]])" function
3717 */
3718 static void
3719f_finddir(typval_T *argvars, typval_T *rettv)
3720{
3721 findfilendir(argvars, rettv, FINDFILE_DIR);
3722}
3723
3724/*
3725 * "findfile({fname}[, {path}[, {count}]])" function
3726 */
3727 static void
3728f_findfile(typval_T *argvars, typval_T *rettv)
3729{
3730 findfilendir(argvars, rettv, FINDFILE_FILE);
3731}
3732
3733#ifdef FEAT_FLOAT
3734/*
3735 * "float2nr({float})" function
3736 */
3737 static void
3738f_float2nr(typval_T *argvars, typval_T *rettv)
3739{
3740 float_T f = 0.0;
3741
3742 if (get_float_arg(argvars, &f) == OK)
3743 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003744 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003745 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003746 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003747 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 else
3749 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 }
3751}
3752
3753/*
3754 * "floor({float})" function
3755 */
3756 static void
3757f_floor(typval_T *argvars, typval_T *rettv)
3758{
3759 float_T f = 0.0;
3760
3761 rettv->v_type = VAR_FLOAT;
3762 if (get_float_arg(argvars, &f) == OK)
3763 rettv->vval.v_float = floor(f);
3764 else
3765 rettv->vval.v_float = 0.0;
3766}
3767
3768/*
3769 * "fmod()" function
3770 */
3771 static void
3772f_fmod(typval_T *argvars, typval_T *rettv)
3773{
3774 float_T fx = 0.0, fy = 0.0;
3775
3776 rettv->v_type = VAR_FLOAT;
3777 if (get_float_arg(argvars, &fx) == OK
3778 && get_float_arg(&argvars[1], &fy) == OK)
3779 rettv->vval.v_float = fmod(fx, fy);
3780 else
3781 rettv->vval.v_float = 0.0;
3782}
3783#endif
3784
3785/*
3786 * "fnameescape({string})" function
3787 */
3788 static void
3789f_fnameescape(typval_T *argvars, typval_T *rettv)
3790{
3791 rettv->vval.v_string = vim_strsave_fnameescape(
3792 get_tv_string(&argvars[0]), FALSE);
3793 rettv->v_type = VAR_STRING;
3794}
3795
3796/*
3797 * "fnamemodify({fname}, {mods})" function
3798 */
3799 static void
3800f_fnamemodify(typval_T *argvars, typval_T *rettv)
3801{
3802 char_u *fname;
3803 char_u *mods;
3804 int usedlen = 0;
3805 int len;
3806 char_u *fbuf = NULL;
3807 char_u buf[NUMBUFLEN];
3808
3809 fname = get_tv_string_chk(&argvars[0]);
3810 mods = get_tv_string_buf_chk(&argvars[1], buf);
3811 if (fname == NULL || mods == NULL)
3812 fname = NULL;
3813 else
3814 {
3815 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003816 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003817 }
3818
3819 rettv->v_type = VAR_STRING;
3820 if (fname == NULL)
3821 rettv->vval.v_string = NULL;
3822 else
3823 rettv->vval.v_string = vim_strnsave(fname, len);
3824 vim_free(fbuf);
3825}
3826
3827static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3828
3829/*
3830 * "foldclosed()" function
3831 */
3832 static void
3833foldclosed_both(
3834 typval_T *argvars UNUSED,
3835 typval_T *rettv,
3836 int end UNUSED)
3837{
3838#ifdef FEAT_FOLDING
3839 linenr_T lnum;
3840 linenr_T first, last;
3841
3842 lnum = get_tv_lnum(argvars);
3843 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3844 {
3845 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3846 {
3847 if (end)
3848 rettv->vval.v_number = (varnumber_T)last;
3849 else
3850 rettv->vval.v_number = (varnumber_T)first;
3851 return;
3852 }
3853 }
3854#endif
3855 rettv->vval.v_number = -1;
3856}
3857
3858/*
3859 * "foldclosed()" function
3860 */
3861 static void
3862f_foldclosed(typval_T *argvars, typval_T *rettv)
3863{
3864 foldclosed_both(argvars, rettv, FALSE);
3865}
3866
3867/*
3868 * "foldclosedend()" function
3869 */
3870 static void
3871f_foldclosedend(typval_T *argvars, typval_T *rettv)
3872{
3873 foldclosed_both(argvars, rettv, TRUE);
3874}
3875
3876/*
3877 * "foldlevel()" function
3878 */
3879 static void
3880f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3881{
3882#ifdef FEAT_FOLDING
3883 linenr_T lnum;
3884
3885 lnum = get_tv_lnum(argvars);
3886 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3887 rettv->vval.v_number = foldLevel(lnum);
3888#endif
3889}
3890
3891/*
3892 * "foldtext()" function
3893 */
3894 static void
3895f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3896{
3897#ifdef FEAT_FOLDING
3898 linenr_T foldstart;
3899 linenr_T foldend;
3900 char_u *dashes;
3901 linenr_T lnum;
3902 char_u *s;
3903 char_u *r;
3904 int len;
3905 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003906 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003907#endif
3908
3909 rettv->v_type = VAR_STRING;
3910 rettv->vval.v_string = NULL;
3911#ifdef FEAT_FOLDING
3912 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3913 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3914 dashes = get_vim_var_str(VV_FOLDDASHES);
3915 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3916 && dashes != NULL)
3917 {
3918 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003919 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 if (!linewhite(lnum))
3921 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922
3923 /* Find interesting text in this line. */
3924 s = skipwhite(ml_get(lnum));
3925 /* skip C comment-start */
3926 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3927 {
3928 s = skipwhite(s + 2);
3929 if (*skipwhite(s) == NUL
3930 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3931 {
3932 s = skipwhite(ml_get(lnum + 1));
3933 if (*s == '*')
3934 s = skipwhite(s + 1);
3935 }
3936 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003937 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003938 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939 r = alloc((unsigned)(STRLEN(txt)
3940 + STRLEN(dashes) /* for %s */
3941 + 20 /* for %3ld */
3942 + STRLEN(s))); /* concatenated */
3943 if (r != NULL)
3944 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003945 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003946 len = (int)STRLEN(r);
3947 STRCAT(r, s);
3948 /* remove 'foldmarker' and 'commentstring' */
3949 foldtext_cleanup(r + len);
3950 rettv->vval.v_string = r;
3951 }
3952 }
3953#endif
3954}
3955
3956/*
3957 * "foldtextresult(lnum)" function
3958 */
3959 static void
3960f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3961{
3962#ifdef FEAT_FOLDING
3963 linenr_T lnum;
3964 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003965 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003966 foldinfo_T foldinfo;
3967 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003968 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969#endif
3970
3971 rettv->v_type = VAR_STRING;
3972 rettv->vval.v_string = NULL;
3973#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003974 if (entered)
3975 return; /* reject recursive use */
3976 entered = TRUE;
3977
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003978 lnum = get_tv_lnum(argvars);
3979 /* treat illegal types and illegal string values for {lnum} the same */
3980 if (lnum < 0)
3981 lnum = 0;
3982 fold_count = foldedCount(curwin, lnum, &foldinfo);
3983 if (fold_count > 0)
3984 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003985 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3986 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003987 if (text == buf)
3988 text = vim_strsave(text);
3989 rettv->vval.v_string = text;
3990 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003991
3992 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993#endif
3994}
3995
3996/*
3997 * "foreground()" function
3998 */
3999 static void
4000f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4001{
4002#ifdef FEAT_GUI
4003 if (gui.in_use)
4004 gui_mch_set_foreground();
4005#else
4006# ifdef WIN32
4007 win32_set_foreground();
4008# endif
4009#endif
4010}
4011
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004013common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014{
4015 char_u *s;
4016 char_u *name;
4017 int use_string = FALSE;
4018 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004019 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020
4021 if (argvars[0].v_type == VAR_FUNC)
4022 {
4023 /* function(MyFunc, [arg], dict) */
4024 s = argvars[0].vval.v_string;
4025 }
4026 else if (argvars[0].v_type == VAR_PARTIAL
4027 && argvars[0].vval.v_partial != NULL)
4028 {
4029 /* function(dict.MyFunc, [arg]) */
4030 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004031 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004032 }
4033 else
4034 {
4035 /* function('MyFunc', [arg], dict) */
4036 s = get_tv_string(&argvars[0]);
4037 use_string = TRUE;
4038 }
4039
Bram Moolenaar843b8842016-08-21 14:36:15 +02004040 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004041 {
4042 name = s;
4043 trans_name = trans_function_name(&name, FALSE,
4044 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4045 if (*name != NUL)
4046 s = NULL;
4047 }
4048
Bram Moolenaar843b8842016-08-21 14:36:15 +02004049 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4050 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004051 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004053 else if (trans_name != NULL && (is_funcref
4054 ? find_func(trans_name) == NULL
4055 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056 EMSG2(_("E700: Unknown function: %s"), s);
4057 else
4058 {
4059 int dict_idx = 0;
4060 int arg_idx = 0;
4061 list_T *list = NULL;
4062
4063 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4064 {
4065 char sid_buf[25];
4066 int off = *s == 's' ? 2 : 5;
4067
4068 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4069 * also be called from another script. Using trans_function_name()
4070 * would also work, but some plugins depend on the name being
4071 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004072 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4074 if (name != NULL)
4075 {
4076 STRCPY(name, sid_buf);
4077 STRCAT(name, s + off);
4078 }
4079 }
4080 else
4081 name = vim_strsave(s);
4082
4083 if (argvars[1].v_type != VAR_UNKNOWN)
4084 {
4085 if (argvars[2].v_type != VAR_UNKNOWN)
4086 {
4087 /* function(name, [args], dict) */
4088 arg_idx = 1;
4089 dict_idx = 2;
4090 }
4091 else if (argvars[1].v_type == VAR_DICT)
4092 /* function(name, dict) */
4093 dict_idx = 1;
4094 else
4095 /* function(name, [args]) */
4096 arg_idx = 1;
4097 if (dict_idx > 0)
4098 {
4099 if (argvars[dict_idx].v_type != VAR_DICT)
4100 {
4101 EMSG(_("E922: expected a dict"));
4102 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004103 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004104 }
4105 if (argvars[dict_idx].vval.v_dict == NULL)
4106 dict_idx = 0;
4107 }
4108 if (arg_idx > 0)
4109 {
4110 if (argvars[arg_idx].v_type != VAR_LIST)
4111 {
4112 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4113 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004114 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004115 }
4116 list = argvars[arg_idx].vval.v_list;
4117 if (list == NULL || list->lv_len == 0)
4118 arg_idx = 0;
4119 }
4120 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004121 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004122 {
4123 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4124
4125 /* result is a VAR_PARTIAL */
4126 if (pt == NULL)
4127 vim_free(name);
4128 else
4129 {
4130 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4131 {
4132 listitem_T *li;
4133 int i = 0;
4134 int arg_len = 0;
4135 int lv_len = 0;
4136
4137 if (arg_pt != NULL)
4138 arg_len = arg_pt->pt_argc;
4139 if (list != NULL)
4140 lv_len = list->lv_len;
4141 pt->pt_argc = arg_len + lv_len;
4142 pt->pt_argv = (typval_T *)alloc(
4143 sizeof(typval_T) * pt->pt_argc);
4144 if (pt->pt_argv == NULL)
4145 {
4146 vim_free(pt);
4147 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004148 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 for (i = 0; i < arg_len; i++)
4151 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4152 if (lv_len > 0)
4153 for (li = list->lv_first; li != NULL;
4154 li = li->li_next)
4155 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156 }
4157
4158 /* For "function(dict.func, [], dict)" and "func" is a partial
4159 * use "dict". That is backwards compatible. */
4160 if (dict_idx > 0)
4161 {
4162 /* The dict is bound explicitly, pt_auto is FALSE. */
4163 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4164 ++pt->pt_dict->dv_refcount;
4165 }
4166 else if (arg_pt != NULL)
4167 {
4168 /* If the dict was bound automatically the result is also
4169 * bound automatically. */
4170 pt->pt_dict = arg_pt->pt_dict;
4171 pt->pt_auto = arg_pt->pt_auto;
4172 if (pt->pt_dict != NULL)
4173 ++pt->pt_dict->dv_refcount;
4174 }
4175
4176 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004177 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4178 {
4179 pt->pt_func = arg_pt->pt_func;
4180 func_ptr_ref(pt->pt_func);
4181 vim_free(name);
4182 }
4183 else if (is_funcref)
4184 {
4185 pt->pt_func = find_func(trans_name);
4186 func_ptr_ref(pt->pt_func);
4187 vim_free(name);
4188 }
4189 else
4190 {
4191 pt->pt_name = name;
4192 func_ref(name);
4193 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 }
4195 rettv->v_type = VAR_PARTIAL;
4196 rettv->vval.v_partial = pt;
4197 }
4198 else
4199 {
4200 /* result is a VAR_FUNC */
4201 rettv->v_type = VAR_FUNC;
4202 rettv->vval.v_string = name;
4203 func_ref(name);
4204 }
4205 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004206theend:
4207 vim_free(trans_name);
4208}
4209
4210/*
4211 * "funcref()" function
4212 */
4213 static void
4214f_funcref(typval_T *argvars, typval_T *rettv)
4215{
4216 common_function(argvars, rettv, TRUE);
4217}
4218
4219/*
4220 * "function()" function
4221 */
4222 static void
4223f_function(typval_T *argvars, typval_T *rettv)
4224{
4225 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004226}
4227
4228/*
4229 * "garbagecollect()" function
4230 */
4231 static void
4232f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4233{
4234 /* This is postponed until we are back at the toplevel, because we may be
4235 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4236 want_garbage_collect = TRUE;
4237
4238 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4239 garbage_collect_at_exit = TRUE;
4240}
4241
4242/*
4243 * "get()" function
4244 */
4245 static void
4246f_get(typval_T *argvars, typval_T *rettv)
4247{
4248 listitem_T *li;
4249 list_T *l;
4250 dictitem_T *di;
4251 dict_T *d;
4252 typval_T *tv = NULL;
4253
4254 if (argvars[0].v_type == VAR_LIST)
4255 {
4256 if ((l = argvars[0].vval.v_list) != NULL)
4257 {
4258 int error = FALSE;
4259
4260 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4261 if (!error && li != NULL)
4262 tv = &li->li_tv;
4263 }
4264 }
4265 else if (argvars[0].v_type == VAR_DICT)
4266 {
4267 if ((d = argvars[0].vval.v_dict) != NULL)
4268 {
4269 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4270 if (di != NULL)
4271 tv = &di->di_tv;
4272 }
4273 }
4274 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4275 {
4276 partial_T *pt;
4277 partial_T fref_pt;
4278
4279 if (argvars[0].v_type == VAR_PARTIAL)
4280 pt = argvars[0].vval.v_partial;
4281 else
4282 {
4283 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4284 fref_pt.pt_name = argvars[0].vval.v_string;
4285 pt = &fref_pt;
4286 }
4287
4288 if (pt != NULL)
4289 {
4290 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004291 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004292
4293 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4294 {
4295 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004296 n = partial_name(pt);
4297 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 rettv->vval.v_string = NULL;
4299 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300 {
4301 rettv->vval.v_string = vim_strsave(n);
4302 if (rettv->v_type == VAR_FUNC)
4303 func_ref(rettv->vval.v_string);
4304 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305 }
4306 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004307 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004308 else if (STRCMP(what, "args") == 0)
4309 {
4310 rettv->v_type = VAR_LIST;
4311 if (rettv_list_alloc(rettv) == OK)
4312 {
4313 int i;
4314
4315 for (i = 0; i < pt->pt_argc; ++i)
4316 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4317 }
4318 }
4319 else
4320 EMSG2(_(e_invarg2), what);
4321 return;
4322 }
4323 }
4324 else
4325 EMSG2(_(e_listdictarg), "get()");
4326
4327 if (tv == NULL)
4328 {
4329 if (argvars[2].v_type != VAR_UNKNOWN)
4330 copy_tv(&argvars[2], rettv);
4331 }
4332 else
4333 copy_tv(tv, rettv);
4334}
4335
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004336#ifdef FEAT_SIGNS
4337/*
4338 * Returns information about signs placed in a buffer as list of dicts.
4339 */
4340 static void
4341get_buffer_signs(buf_T *buf, list_T *l)
4342{
4343 signlist_T *sign;
4344
4345 for (sign = buf->b_signlist; sign; sign = sign->next)
4346 {
4347 dict_T *d = dict_alloc();
4348
4349 if (d != NULL)
4350 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004351 dict_add_number(d, "id", sign->id);
4352 dict_add_number(d, "lnum", sign->lnum);
4353 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004354
4355 list_append_dict(l, d);
4356 }
4357 }
4358}
4359#endif
4360
4361/*
4362 * Returns buffer options, variables and other attributes in a dictionary.
4363 */
4364 static dict_T *
4365get_buffer_info(buf_T *buf)
4366{
4367 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004368 tabpage_T *tp;
4369 win_T *wp;
4370 list_T *windows;
4371
4372 dict = dict_alloc();
4373 if (dict == NULL)
4374 return NULL;
4375
Bram Moolenaare0be1672018-07-08 16:50:37 +02004376 dict_add_number(dict, "bufnr", buf->b_fnum);
4377 dict_add_string(dict, "name", buf->b_ffname);
4378 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4379 : buflist_findlnum(buf));
4380 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4381 dict_add_number(dict, "listed", buf->b_p_bl);
4382 dict_add_number(dict, "changed", bufIsChanged(buf));
4383 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4384 dict_add_number(dict, "hidden",
4385 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004386
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004387 /* Get a reference to buffer variables */
4388 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004389
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004390 /* List of windows displaying this buffer */
4391 windows = list_alloc();
4392 if (windows != NULL)
4393 {
4394 FOR_ALL_TAB_WINDOWS(tp, wp)
4395 if (wp->w_buffer == buf)
4396 list_append_number(windows, (varnumber_T)wp->w_id);
4397 dict_add_list(dict, "windows", windows);
4398 }
4399
4400#ifdef FEAT_SIGNS
4401 if (buf->b_signlist != NULL)
4402 {
4403 /* List of signs placed in this buffer */
4404 list_T *signs = list_alloc();
4405 if (signs != NULL)
4406 {
4407 get_buffer_signs(buf, signs);
4408 dict_add_list(dict, "signs", signs);
4409 }
4410 }
4411#endif
4412
4413 return dict;
4414}
4415
4416/*
4417 * "getbufinfo()" function
4418 */
4419 static void
4420f_getbufinfo(typval_T *argvars, typval_T *rettv)
4421{
4422 buf_T *buf = NULL;
4423 buf_T *argbuf = NULL;
4424 dict_T *d;
4425 int filtered = FALSE;
4426 int sel_buflisted = FALSE;
4427 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004428 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004429
4430 if (rettv_list_alloc(rettv) != OK)
4431 return;
4432
4433 /* List of all the buffers or selected buffers */
4434 if (argvars[0].v_type == VAR_DICT)
4435 {
4436 dict_T *sel_d = argvars[0].vval.v_dict;
4437
4438 if (sel_d != NULL)
4439 {
4440 dictitem_T *di;
4441
4442 filtered = TRUE;
4443
4444 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4445 if (di != NULL && get_tv_number(&di->di_tv))
4446 sel_buflisted = TRUE;
4447
4448 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4449 if (di != NULL && get_tv_number(&di->di_tv))
4450 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004451
4452 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4453 if (di != NULL && get_tv_number(&di->di_tv))
4454 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004455 }
4456 }
4457 else if (argvars[0].v_type != VAR_UNKNOWN)
4458 {
4459 /* Information about one buffer. Argument specifies the buffer */
4460 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4461 ++emsg_off;
4462 argbuf = get_buf_tv(&argvars[0], FALSE);
4463 --emsg_off;
4464 if (argbuf == NULL)
4465 return;
4466 }
4467
4468 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004469 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004470 {
4471 if (argbuf != NULL && argbuf != buf)
4472 continue;
4473 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004474 || (sel_buflisted && !buf->b_p_bl)
4475 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004476 continue;
4477
4478 d = get_buffer_info(buf);
4479 if (d != NULL)
4480 list_append_dict(rettv->vval.v_list, d);
4481 if (argbuf != NULL)
4482 return;
4483 }
4484}
4485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004486static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4487
4488/*
4489 * Get line or list of lines from buffer "buf" into "rettv".
4490 * Return a range (from start to end) of lines in rettv from the specified
4491 * buffer.
4492 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4493 */
4494 static void
4495get_buffer_lines(
4496 buf_T *buf,
4497 linenr_T start,
4498 linenr_T end,
4499 int retlist,
4500 typval_T *rettv)
4501{
4502 char_u *p;
4503
4504 rettv->v_type = VAR_STRING;
4505 rettv->vval.v_string = NULL;
4506 if (retlist && rettv_list_alloc(rettv) == FAIL)
4507 return;
4508
4509 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4510 return;
4511
4512 if (!retlist)
4513 {
4514 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4515 p = ml_get_buf(buf, start, FALSE);
4516 else
4517 p = (char_u *)"";
4518 rettv->vval.v_string = vim_strsave(p);
4519 }
4520 else
4521 {
4522 if (end < start)
4523 return;
4524
4525 if (start < 1)
4526 start = 1;
4527 if (end > buf->b_ml.ml_line_count)
4528 end = buf->b_ml.ml_line_count;
4529 while (start <= end)
4530 if (list_append_string(rettv->vval.v_list,
4531 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4532 break;
4533 }
4534}
4535
4536/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004537 * "getbufline()" function
4538 */
4539 static void
4540f_getbufline(typval_T *argvars, typval_T *rettv)
4541{
4542 linenr_T lnum;
4543 linenr_T end;
4544 buf_T *buf;
4545
4546 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4547 ++emsg_off;
4548 buf = get_buf_tv(&argvars[0], FALSE);
4549 --emsg_off;
4550
4551 lnum = get_tv_lnum_buf(&argvars[1], buf);
4552 if (argvars[2].v_type == VAR_UNKNOWN)
4553 end = lnum;
4554 else
4555 end = get_tv_lnum_buf(&argvars[2], buf);
4556
4557 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4558}
4559
4560/*
4561 * "getbufvar()" function
4562 */
4563 static void
4564f_getbufvar(typval_T *argvars, typval_T *rettv)
4565{
4566 buf_T *buf;
4567 buf_T *save_curbuf;
4568 char_u *varname;
4569 dictitem_T *v;
4570 int done = FALSE;
4571
4572 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4573 varname = get_tv_string_chk(&argvars[1]);
4574 ++emsg_off;
4575 buf = get_buf_tv(&argvars[0], FALSE);
4576
4577 rettv->v_type = VAR_STRING;
4578 rettv->vval.v_string = NULL;
4579
4580 if (buf != NULL && varname != NULL)
4581 {
4582 /* set curbuf to be our buf, temporarily */
4583 save_curbuf = curbuf;
4584 curbuf = buf;
4585
Bram Moolenaar30567352016-08-27 21:25:44 +02004586 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004587 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004588 if (varname[1] == NUL)
4589 {
4590 /* get all buffer-local options in a dict */
4591 dict_T *opts = get_winbuf_options(TRUE);
4592
4593 if (opts != NULL)
4594 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004595 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004596 done = TRUE;
4597 }
4598 }
4599 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4600 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004601 done = TRUE;
4602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603 else
4604 {
4605 /* Look up the variable. */
4606 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4607 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4608 'b', varname, FALSE);
4609 if (v != NULL)
4610 {
4611 copy_tv(&v->di_tv, rettv);
4612 done = TRUE;
4613 }
4614 }
4615
4616 /* restore previous notion of curbuf */
4617 curbuf = save_curbuf;
4618 }
4619
4620 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4621 /* use the default value */
4622 copy_tv(&argvars[2], rettv);
4623
4624 --emsg_off;
4625}
4626
4627/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004628 * "getchangelist()" function
4629 */
4630 static void
4631f_getchangelist(typval_T *argvars, typval_T *rettv)
4632{
4633#ifdef FEAT_JUMPLIST
4634 buf_T *buf;
4635 int i;
4636 list_T *l;
4637 dict_T *d;
4638#endif
4639
4640 if (rettv_list_alloc(rettv) != OK)
4641 return;
4642
4643#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004644 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4645 ++emsg_off;
4646 buf = get_buf_tv(&argvars[0], FALSE);
4647 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004648 if (buf == NULL)
4649 return;
4650
4651 l = list_alloc();
4652 if (l == NULL)
4653 return;
4654
4655 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4656 return;
4657 /*
4658 * The current window change list index tracks only the position in the
4659 * current buffer change list. For other buffers, use the change list
4660 * length as the current index.
4661 */
4662 list_append_number(rettv->vval.v_list,
4663 (varnumber_T)((buf == curwin->w_buffer)
4664 ? curwin->w_changelistidx : buf->b_changelistlen));
4665
4666 for (i = 0; i < buf->b_changelistlen; ++i)
4667 {
4668 if (buf->b_changelist[i].lnum == 0)
4669 continue;
4670 if ((d = dict_alloc()) == NULL)
4671 return;
4672 if (list_append_dict(l, d) == FAIL)
4673 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004674 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4675 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004676# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004677 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004678# endif
4679 }
4680#endif
4681}
4682/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 * "getchar()" function
4684 */
4685 static void
4686f_getchar(typval_T *argvars, typval_T *rettv)
4687{
4688 varnumber_T n;
4689 int error = FALSE;
4690
Bram Moolenaar84d93902018-09-11 20:10:20 +02004691#ifdef MESSAGE_QUEUE
4692 // vpeekc() used to check for messages, but that caused problems, invoking
4693 // a callback where it was not expected. Some plugins use getchar(1) in a
4694 // loop to await a message, therefore make sure we check for messages here.
4695 parse_queued_messages();
4696#endif
4697
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698 /* Position the cursor. Needed after a message that ends in a space. */
4699 windgoto(msg_row, msg_col);
4700
4701 ++no_mapping;
4702 ++allow_keys;
4703 for (;;)
4704 {
4705 if (argvars[0].v_type == VAR_UNKNOWN)
4706 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004707 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004708 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4709 /* getchar(1): only check if char avail */
4710 n = vpeekc_any();
4711 else if (error || vpeekc_any() == NUL)
4712 /* illegal argument or getchar(0) and no char avail: return zero */
4713 n = 0;
4714 else
4715 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004716 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717
4718 if (n == K_IGNORE)
4719 continue;
4720 break;
4721 }
4722 --no_mapping;
4723 --allow_keys;
4724
4725 set_vim_var_nr(VV_MOUSE_WIN, 0);
4726 set_vim_var_nr(VV_MOUSE_WINID, 0);
4727 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4728 set_vim_var_nr(VV_MOUSE_COL, 0);
4729
4730 rettv->vval.v_number = n;
4731 if (IS_SPECIAL(n) || mod_mask != 0)
4732 {
4733 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4734 int i = 0;
4735
4736 /* Turn a special key into three bytes, plus modifier. */
4737 if (mod_mask != 0)
4738 {
4739 temp[i++] = K_SPECIAL;
4740 temp[i++] = KS_MODIFIER;
4741 temp[i++] = mod_mask;
4742 }
4743 if (IS_SPECIAL(n))
4744 {
4745 temp[i++] = K_SPECIAL;
4746 temp[i++] = K_SECOND(n);
4747 temp[i++] = K_THIRD(n);
4748 }
4749#ifdef FEAT_MBYTE
4750 else if (has_mbyte)
4751 i += (*mb_char2bytes)(n, temp + i);
4752#endif
4753 else
4754 temp[i++] = n;
4755 temp[i++] = NUL;
4756 rettv->v_type = VAR_STRING;
4757 rettv->vval.v_string = vim_strsave(temp);
4758
4759#ifdef FEAT_MOUSE
4760 if (is_mouse_key(n))
4761 {
4762 int row = mouse_row;
4763 int col = mouse_col;
4764 win_T *win;
4765 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004766 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004767 int winnr = 1;
4768
4769 if (row >= 0 && col >= 0)
4770 {
4771 /* Find the window at the mouse coordinates and compute the
4772 * text position. */
4773 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004774 if (win == NULL)
4775 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004776 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004777 for (wp = firstwin; wp != win; wp = wp->w_next)
4778 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4780 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4781 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4782 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4783 }
4784 }
4785#endif
4786 }
4787}
4788
4789/*
4790 * "getcharmod()" function
4791 */
4792 static void
4793f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4794{
4795 rettv->vval.v_number = mod_mask;
4796}
4797
4798/*
4799 * "getcharsearch()" function
4800 */
4801 static void
4802f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4803{
4804 if (rettv_dict_alloc(rettv) != FAIL)
4805 {
4806 dict_T *dict = rettv->vval.v_dict;
4807
Bram Moolenaare0be1672018-07-08 16:50:37 +02004808 dict_add_string(dict, "char", last_csearch());
4809 dict_add_number(dict, "forward", last_csearch_forward());
4810 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004811 }
4812}
4813
4814/*
4815 * "getcmdline()" function
4816 */
4817 static void
4818f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4819{
4820 rettv->v_type = VAR_STRING;
4821 rettv->vval.v_string = get_cmdline_str();
4822}
4823
4824/*
4825 * "getcmdpos()" function
4826 */
4827 static void
4828f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4829{
4830 rettv->vval.v_number = get_cmdline_pos() + 1;
4831}
4832
4833/*
4834 * "getcmdtype()" function
4835 */
4836 static void
4837f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4838{
4839 rettv->v_type = VAR_STRING;
4840 rettv->vval.v_string = alloc(2);
4841 if (rettv->vval.v_string != NULL)
4842 {
4843 rettv->vval.v_string[0] = get_cmdline_type();
4844 rettv->vval.v_string[1] = NUL;
4845 }
4846}
4847
4848/*
4849 * "getcmdwintype()" function
4850 */
4851 static void
4852f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4853{
4854 rettv->v_type = VAR_STRING;
4855 rettv->vval.v_string = NULL;
4856#ifdef FEAT_CMDWIN
4857 rettv->vval.v_string = alloc(2);
4858 if (rettv->vval.v_string != NULL)
4859 {
4860 rettv->vval.v_string[0] = cmdwin_type;
4861 rettv->vval.v_string[1] = NUL;
4862 }
4863#endif
4864}
4865
4866#if defined(FEAT_CMDL_COMPL)
4867/*
4868 * "getcompletion()" function
4869 */
4870 static void
4871f_getcompletion(typval_T *argvars, typval_T *rettv)
4872{
4873 char_u *pat;
4874 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004875 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004876 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4877 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004878
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004879 if (argvars[2].v_type != VAR_UNKNOWN)
4880 filtered = get_tv_number_chk(&argvars[2], NULL);
4881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882 if (p_wic)
4883 options |= WILD_ICASE;
4884
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004885 /* For filtered results, 'wildignore' is used */
4886 if (!filtered)
4887 options |= WILD_KEEP_ALL;
4888
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 ExpandInit(&xpc);
4890 xpc.xp_pattern = get_tv_string(&argvars[0]);
4891 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4892 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4893 if (xpc.xp_context == EXPAND_NOTHING)
4894 {
4895 if (argvars[1].v_type == VAR_STRING)
4896 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4897 else
4898 EMSG(_(e_invarg));
4899 return;
4900 }
4901
4902# if defined(FEAT_MENU)
4903 if (xpc.xp_context == EXPAND_MENUS)
4904 {
4905 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4906 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4907 }
4908# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004909#ifdef FEAT_CSCOPE
4910 if (xpc.xp_context == EXPAND_CSCOPE)
4911 {
4912 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4913 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4914 }
4915#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004916#ifdef FEAT_SIGNS
4917 if (xpc.xp_context == EXPAND_SIGN)
4918 {
4919 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4920 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4921 }
4922#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923
4924 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4925 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4926 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004927 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004928
4929 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4930
4931 for (i = 0; i < xpc.xp_numfiles; i++)
4932 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4933 }
4934 vim_free(pat);
4935 ExpandCleanup(&xpc);
4936}
4937#endif
4938
4939/*
4940 * "getcwd()" function
4941 */
4942 static void
4943f_getcwd(typval_T *argvars, typval_T *rettv)
4944{
4945 win_T *wp = NULL;
4946 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004947 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004948
4949 rettv->v_type = VAR_STRING;
4950 rettv->vval.v_string = NULL;
4951
Bram Moolenaar54591292018-02-09 20:53:59 +01004952 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4953 global = TRUE;
4954 else
4955 wp = find_tabwin(&argvars[0], &argvars[1]);
4956
4957 if (wp != NULL && wp->w_localdir != NULL)
4958 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4959 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004960 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004961 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 rettv->vval.v_string = vim_strsave(globaldir);
4963 else
4964 {
4965 cwd = alloc(MAXPATHL);
4966 if (cwd != NULL)
4967 {
4968 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4969 rettv->vval.v_string = vim_strsave(cwd);
4970 vim_free(cwd);
4971 }
4972 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004973 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004974#ifdef BACKSLASH_IN_FILENAME
4975 if (rettv->vval.v_string != NULL)
4976 slash_adjust(rettv->vval.v_string);
4977#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004978}
4979
4980/*
4981 * "getfontname()" function
4982 */
4983 static void
4984f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4985{
4986 rettv->v_type = VAR_STRING;
4987 rettv->vval.v_string = NULL;
4988#ifdef FEAT_GUI
4989 if (gui.in_use)
4990 {
4991 GuiFont font;
4992 char_u *name = NULL;
4993
4994 if (argvars[0].v_type == VAR_UNKNOWN)
4995 {
4996 /* Get the "Normal" font. Either the name saved by
4997 * hl_set_font_name() or from the font ID. */
4998 font = gui.norm_font;
4999 name = hl_get_font_name();
5000 }
5001 else
5002 {
5003 name = get_tv_string(&argvars[0]);
5004 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5005 return;
5006 font = gui_mch_get_font(name, FALSE);
5007 if (font == NOFONT)
5008 return; /* Invalid font name, return empty string. */
5009 }
5010 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5011 if (argvars[0].v_type != VAR_UNKNOWN)
5012 gui_mch_free_font(font);
5013 }
5014#endif
5015}
5016
5017/*
5018 * "getfperm({fname})" function
5019 */
5020 static void
5021f_getfperm(typval_T *argvars, typval_T *rettv)
5022{
5023 char_u *fname;
5024 stat_T st;
5025 char_u *perm = NULL;
5026 char_u flags[] = "rwx";
5027 int i;
5028
5029 fname = get_tv_string(&argvars[0]);
5030
5031 rettv->v_type = VAR_STRING;
5032 if (mch_stat((char *)fname, &st) >= 0)
5033 {
5034 perm = vim_strsave((char_u *)"---------");
5035 if (perm != NULL)
5036 {
5037 for (i = 0; i < 9; i++)
5038 {
5039 if (st.st_mode & (1 << (8 - i)))
5040 perm[i] = flags[i % 3];
5041 }
5042 }
5043 }
5044 rettv->vval.v_string = perm;
5045}
5046
5047/*
5048 * "getfsize({fname})" function
5049 */
5050 static void
5051f_getfsize(typval_T *argvars, typval_T *rettv)
5052{
5053 char_u *fname;
5054 stat_T st;
5055
5056 fname = get_tv_string(&argvars[0]);
5057
5058 rettv->v_type = VAR_NUMBER;
5059
5060 if (mch_stat((char *)fname, &st) >= 0)
5061 {
5062 if (mch_isdir(fname))
5063 rettv->vval.v_number = 0;
5064 else
5065 {
5066 rettv->vval.v_number = (varnumber_T)st.st_size;
5067
5068 /* non-perfect check for overflow */
5069 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5070 rettv->vval.v_number = -2;
5071 }
5072 }
5073 else
5074 rettv->vval.v_number = -1;
5075}
5076
5077/*
5078 * "getftime({fname})" function
5079 */
5080 static void
5081f_getftime(typval_T *argvars, typval_T *rettv)
5082{
5083 char_u *fname;
5084 stat_T st;
5085
5086 fname = get_tv_string(&argvars[0]);
5087
5088 if (mch_stat((char *)fname, &st) >= 0)
5089 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5090 else
5091 rettv->vval.v_number = -1;
5092}
5093
5094/*
5095 * "getftype({fname})" function
5096 */
5097 static void
5098f_getftype(typval_T *argvars, typval_T *rettv)
5099{
5100 char_u *fname;
5101 stat_T st;
5102 char_u *type = NULL;
5103 char *t;
5104
5105 fname = get_tv_string(&argvars[0]);
5106
5107 rettv->v_type = VAR_STRING;
5108 if (mch_lstat((char *)fname, &st) >= 0)
5109 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 if (S_ISREG(st.st_mode))
5111 t = "file";
5112 else if (S_ISDIR(st.st_mode))
5113 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 else if (S_ISLNK(st.st_mode))
5115 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 else if (S_ISBLK(st.st_mode))
5117 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else if (S_ISCHR(st.st_mode))
5119 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 else if (S_ISFIFO(st.st_mode))
5121 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005123 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 else
5125 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 type = vim_strsave((char_u *)t);
5127 }
5128 rettv->vval.v_string = type;
5129}
5130
5131/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005132 * "getjumplist()" function
5133 */
5134 static void
5135f_getjumplist(typval_T *argvars, typval_T *rettv)
5136{
5137#ifdef FEAT_JUMPLIST
5138 win_T *wp;
5139 int i;
5140 list_T *l;
5141 dict_T *d;
5142#endif
5143
5144 if (rettv_list_alloc(rettv) != OK)
5145 return;
5146
5147#ifdef FEAT_JUMPLIST
5148 wp = find_tabwin(&argvars[0], &argvars[1]);
5149 if (wp == NULL)
5150 return;
5151
5152 l = list_alloc();
5153 if (l == NULL)
5154 return;
5155
5156 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5157 return;
5158 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5159
Bram Moolenaar48679742018-02-13 13:33:29 +01005160 cleanup_jumplist(wp, TRUE);
5161
Bram Moolenaar4f505882018-02-10 21:06:32 +01005162 for (i = 0; i < wp->w_jumplistlen; ++i)
5163 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005164 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5165 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005166 if ((d = dict_alloc()) == NULL)
5167 return;
5168 if (list_append_dict(l, d) == FAIL)
5169 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005170 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5171 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005172# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005173 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005174# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005175 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005176 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005177 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005178 }
5179#endif
5180}
5181
5182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005183 * "getline(lnum, [end])" function
5184 */
5185 static void
5186f_getline(typval_T *argvars, typval_T *rettv)
5187{
5188 linenr_T lnum;
5189 linenr_T end;
5190 int retlist;
5191
5192 lnum = get_tv_lnum(argvars);
5193 if (argvars[1].v_type == VAR_UNKNOWN)
5194 {
5195 end = 0;
5196 retlist = FALSE;
5197 }
5198 else
5199 {
5200 end = get_tv_lnum(&argvars[1]);
5201 retlist = TRUE;
5202 }
5203
5204 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5205}
5206
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005207#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005208 static void
5209get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5210{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005211 if (what_arg->v_type == VAR_UNKNOWN)
5212 {
5213 if (rettv_list_alloc(rettv) == OK)
5214 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005215 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005216 }
5217 else
5218 {
5219 if (rettv_dict_alloc(rettv) == OK)
5220 if (is_qf || (wp != NULL))
5221 {
5222 if (what_arg->v_type == VAR_DICT)
5223 {
5224 dict_T *d = what_arg->vval.v_dict;
5225
5226 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005227 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005228 }
5229 else
5230 EMSG(_(e_dictreq));
5231 }
5232 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005233}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005234#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235
5236/*
5237 * "getloclist()" function
5238 */
5239 static void
5240f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5241{
5242#ifdef FEAT_QUICKFIX
5243 win_T *wp;
5244
5245 wp = find_win_by_nr(&argvars[0], NULL);
5246 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5247#endif
5248}
5249
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005250/*
5251 * "getmatches()" function
5252 */
5253 static void
5254f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5255{
5256#ifdef FEAT_SEARCH_EXTRA
5257 dict_T *dict;
5258 matchitem_T *cur = curwin->w_match_head;
5259 int i;
5260
5261 if (rettv_list_alloc(rettv) == OK)
5262 {
5263 while (cur != NULL)
5264 {
5265 dict = dict_alloc();
5266 if (dict == NULL)
5267 return;
5268 if (cur->match.regprog == NULL)
5269 {
5270 /* match added with matchaddpos() */
5271 for (i = 0; i < MAXPOSMATCH; ++i)
5272 {
5273 llpos_T *llpos;
5274 char buf[6];
5275 list_T *l;
5276
5277 llpos = &cur->pos.pos[i];
5278 if (llpos->lnum == 0)
5279 break;
5280 l = list_alloc();
5281 if (l == NULL)
5282 break;
5283 list_append_number(l, (varnumber_T)llpos->lnum);
5284 if (llpos->col > 0)
5285 {
5286 list_append_number(l, (varnumber_T)llpos->col);
5287 list_append_number(l, (varnumber_T)llpos->len);
5288 }
5289 sprintf(buf, "pos%d", i + 1);
5290 dict_add_list(dict, buf, l);
5291 }
5292 }
5293 else
5294 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005295 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005297 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5298 dict_add_number(dict, "priority", (long)cur->priority);
5299 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5301 if (cur->conceal_char)
5302 {
5303 char_u buf[MB_MAXBYTES + 1];
5304
5305 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005306 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 }
5308# endif
5309 list_append_dict(rettv->vval.v_list, dict);
5310 cur = cur->next;
5311 }
5312 }
5313#endif
5314}
5315
5316/*
5317 * "getpid()" function
5318 */
5319 static void
5320f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5321{
5322 rettv->vval.v_number = mch_get_pid();
5323}
5324
5325 static void
5326getpos_both(
5327 typval_T *argvars,
5328 typval_T *rettv,
5329 int getcurpos)
5330{
5331 pos_T *fp;
5332 list_T *l;
5333 int fnum = -1;
5334
5335 if (rettv_list_alloc(rettv) == OK)
5336 {
5337 l = rettv->vval.v_list;
5338 if (getcurpos)
5339 fp = &curwin->w_cursor;
5340 else
5341 fp = var2fpos(&argvars[0], TRUE, &fnum);
5342 if (fnum != -1)
5343 list_append_number(l, (varnumber_T)fnum);
5344 else
5345 list_append_number(l, (varnumber_T)0);
5346 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5347 : (varnumber_T)0);
5348 list_append_number(l, (fp != NULL)
5349 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5350 : (varnumber_T)0);
5351 list_append_number(l,
5352#ifdef FEAT_VIRTUALEDIT
5353 (fp != NULL) ? (varnumber_T)fp->coladd :
5354#endif
5355 (varnumber_T)0);
5356 if (getcurpos)
5357 {
5358 update_curswant();
5359 list_append_number(l, curwin->w_curswant == MAXCOL ?
5360 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5361 }
5362 }
5363 else
5364 rettv->vval.v_number = FALSE;
5365}
5366
5367
5368/*
5369 * "getcurpos()" function
5370 */
5371 static void
5372f_getcurpos(typval_T *argvars, typval_T *rettv)
5373{
5374 getpos_both(argvars, rettv, TRUE);
5375}
5376
5377/*
5378 * "getpos(string)" function
5379 */
5380 static void
5381f_getpos(typval_T *argvars, typval_T *rettv)
5382{
5383 getpos_both(argvars, rettv, FALSE);
5384}
5385
5386/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388 */
5389 static void
5390f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5391{
5392#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005393 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005394#endif
5395}
5396
5397/*
5398 * "getreg()" function
5399 */
5400 static void
5401f_getreg(typval_T *argvars, typval_T *rettv)
5402{
5403 char_u *strregname;
5404 int regname;
5405 int arg2 = FALSE;
5406 int return_list = FALSE;
5407 int error = FALSE;
5408
5409 if (argvars[0].v_type != VAR_UNKNOWN)
5410 {
5411 strregname = get_tv_string_chk(&argvars[0]);
5412 error = strregname == NULL;
5413 if (argvars[1].v_type != VAR_UNKNOWN)
5414 {
5415 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5416 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5417 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5418 }
5419 }
5420 else
5421 strregname = get_vim_var_str(VV_REG);
5422
5423 if (error)
5424 return;
5425
5426 regname = (strregname == NULL ? '"' : *strregname);
5427 if (regname == 0)
5428 regname = '"';
5429
5430 if (return_list)
5431 {
5432 rettv->v_type = VAR_LIST;
5433 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5434 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5435 if (rettv->vval.v_list == NULL)
5436 (void)rettv_list_alloc(rettv);
5437 else
5438 ++rettv->vval.v_list->lv_refcount;
5439 }
5440 else
5441 {
5442 rettv->v_type = VAR_STRING;
5443 rettv->vval.v_string = get_reg_contents(regname,
5444 arg2 ? GREG_EXPR_SRC : 0);
5445 }
5446}
5447
5448/*
5449 * "getregtype()" function
5450 */
5451 static void
5452f_getregtype(typval_T *argvars, typval_T *rettv)
5453{
5454 char_u *strregname;
5455 int regname;
5456 char_u buf[NUMBUFLEN + 2];
5457 long reglen = 0;
5458
5459 if (argvars[0].v_type != VAR_UNKNOWN)
5460 {
5461 strregname = get_tv_string_chk(&argvars[0]);
5462 if (strregname == NULL) /* type error; errmsg already given */
5463 {
5464 rettv->v_type = VAR_STRING;
5465 rettv->vval.v_string = NULL;
5466 return;
5467 }
5468 }
5469 else
5470 /* Default to v:register */
5471 strregname = get_vim_var_str(VV_REG);
5472
5473 regname = (strregname == NULL ? '"' : *strregname);
5474 if (regname == 0)
5475 regname = '"';
5476
5477 buf[0] = NUL;
5478 buf[1] = NUL;
5479 switch (get_reg_type(regname, &reglen))
5480 {
5481 case MLINE: buf[0] = 'V'; break;
5482 case MCHAR: buf[0] = 'v'; break;
5483 case MBLOCK:
5484 buf[0] = Ctrl_V;
5485 sprintf((char *)buf + 1, "%ld", reglen + 1);
5486 break;
5487 }
5488 rettv->v_type = VAR_STRING;
5489 rettv->vval.v_string = vim_strsave(buf);
5490}
5491
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005492/*
5493 * Returns information (variables, options, etc.) about a tab page
5494 * as a dictionary.
5495 */
5496 static dict_T *
5497get_tabpage_info(tabpage_T *tp, int tp_idx)
5498{
5499 win_T *wp;
5500 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005501 list_T *l;
5502
5503 dict = dict_alloc();
5504 if (dict == NULL)
5505 return NULL;
5506
Bram Moolenaare0be1672018-07-08 16:50:37 +02005507 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005508
5509 l = list_alloc();
5510 if (l != NULL)
5511 {
5512 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5513 wp; wp = wp->w_next)
5514 list_append_number(l, (varnumber_T)wp->w_id);
5515 dict_add_list(dict, "windows", l);
5516 }
5517
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005518 /* Make a reference to tabpage variables */
5519 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005520
5521 return dict;
5522}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005523
5524/*
5525 * "gettabinfo()" function
5526 */
5527 static void
5528f_gettabinfo(typval_T *argvars, typval_T *rettv)
5529{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005530 tabpage_T *tp, *tparg = NULL;
5531 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005532 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005533
5534 if (rettv_list_alloc(rettv) != OK)
5535 return;
5536
5537 if (argvars[0].v_type != VAR_UNKNOWN)
5538 {
5539 /* Information about one tab page */
5540 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5541 if (tparg == NULL)
5542 return;
5543 }
5544
5545 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005546 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005547 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005548 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005549 if (tparg != NULL && tp != tparg)
5550 continue;
5551 d = get_tabpage_info(tp, tpnr);
5552 if (d != NULL)
5553 list_append_dict(rettv->vval.v_list, d);
5554 if (tparg != NULL)
5555 return;
5556 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005557}
5558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559/*
5560 * "gettabvar()" function
5561 */
5562 static void
5563f_gettabvar(typval_T *argvars, typval_T *rettv)
5564{
5565 win_T *oldcurwin;
5566 tabpage_T *tp, *oldtabpage;
5567 dictitem_T *v;
5568 char_u *varname;
5569 int done = FALSE;
5570
5571 rettv->v_type = VAR_STRING;
5572 rettv->vval.v_string = NULL;
5573
5574 varname = get_tv_string_chk(&argvars[1]);
5575 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5576 if (tp != NULL && varname != NULL)
5577 {
5578 /* Set tp to be our tabpage, temporarily. Also set the window to the
5579 * first window in the tabpage, otherwise the window is not valid. */
5580 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005581 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5582 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 {
5584 /* look up the variable */
5585 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5586 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5587 if (v != NULL)
5588 {
5589 copy_tv(&v->di_tv, rettv);
5590 done = TRUE;
5591 }
5592 }
5593
5594 /* restore previous notion of curwin */
5595 restore_win(oldcurwin, oldtabpage, TRUE);
5596 }
5597
5598 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5599 copy_tv(&argvars[2], rettv);
5600}
5601
5602/*
5603 * "gettabwinvar()" function
5604 */
5605 static void
5606f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5607{
5608 getwinvar(argvars, rettv, 1);
5609}
5610
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005611/*
5612 * Returns information about a window as a dictionary.
5613 */
5614 static dict_T *
5615get_win_info(win_T *wp, short tpnr, short winnr)
5616{
5617 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005618
5619 dict = dict_alloc();
5620 if (dict == NULL)
5621 return NULL;
5622
Bram Moolenaare0be1672018-07-08 16:50:37 +02005623 dict_add_number(dict, "tabnr", tpnr);
5624 dict_add_number(dict, "winnr", winnr);
5625 dict_add_number(dict, "winid", wp->w_id);
5626 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005627 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005628#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005629 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005630#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005631 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005632 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005633 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005634
Bram Moolenaar69905d12017-08-13 18:14:47 +02005635#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005636 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005637#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005638#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005639 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5640 dict_add_number(dict, "loclist",
5641 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005642#endif
5643
Bram Moolenaar30567352016-08-27 21:25:44 +02005644 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005645 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005646
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005647 return dict;
5648}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649
5650/*
5651 * "getwininfo()" function
5652 */
5653 static void
5654f_getwininfo(typval_T *argvars, typval_T *rettv)
5655{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005656 tabpage_T *tp;
5657 win_T *wp = NULL, *wparg = NULL;
5658 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005659 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660
5661 if (rettv_list_alloc(rettv) != OK)
5662 return;
5663
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005664 if (argvars[0].v_type != VAR_UNKNOWN)
5665 {
5666 wparg = win_id2wp(argvars);
5667 if (wparg == NULL)
5668 return;
5669 }
5670
5671 /* Collect information about either all the windows across all the tab
5672 * pages or one particular window.
5673 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005674 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005676 tabnr++;
5677 winnr = 0;
5678 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005679 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005680 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005681 if (wparg != NULL && wp != wparg)
5682 continue;
5683 d = get_win_info(wp, tabnr, winnr);
5684 if (d != NULL)
5685 list_append_dict(rettv->vval.v_list, d);
5686 if (wparg != NULL)
5687 /* found information about a specific window */
5688 return;
5689 }
5690 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005691}
5692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005694 * "win_findbuf()" function
5695 */
5696 static void
5697f_win_findbuf(typval_T *argvars, typval_T *rettv)
5698{
5699 if (rettv_list_alloc(rettv) != FAIL)
5700 win_findbuf(argvars, rettv->vval.v_list);
5701}
5702
5703/*
5704 * "win_getid()" function
5705 */
5706 static void
5707f_win_getid(typval_T *argvars, typval_T *rettv)
5708{
5709 rettv->vval.v_number = win_getid(argvars);
5710}
5711
5712/*
5713 * "win_gotoid()" function
5714 */
5715 static void
5716f_win_gotoid(typval_T *argvars, typval_T *rettv)
5717{
5718 rettv->vval.v_number = win_gotoid(argvars);
5719}
5720
5721/*
5722 * "win_id2tabwin()" function
5723 */
5724 static void
5725f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5726{
5727 if (rettv_list_alloc(rettv) != FAIL)
5728 win_id2tabwin(argvars, rettv->vval.v_list);
5729}
5730
5731/*
5732 * "win_id2win()" function
5733 */
5734 static void
5735f_win_id2win(typval_T *argvars, typval_T *rettv)
5736{
5737 rettv->vval.v_number = win_id2win(argvars);
5738}
5739
5740/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005741 * "win_screenpos()" function
5742 */
5743 static void
5744f_win_screenpos(typval_T *argvars, typval_T *rettv)
5745{
5746 win_T *wp;
5747
5748 if (rettv_list_alloc(rettv) == FAIL)
5749 return;
5750
5751 wp = find_win_by_nr(&argvars[0], NULL);
5752 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5753 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5754}
5755
5756/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005757 * "getwinpos({timeout})" function
5758 */
5759 static void
5760f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5761{
5762 int x = -1;
5763 int y = -1;
5764
5765 if (rettv_list_alloc(rettv) == FAIL)
5766 return;
5767#ifdef FEAT_GUI
5768 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005769 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005770# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5771 else
5772# endif
5773#endif
5774#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5775 {
5776 varnumber_T timeout = 100;
5777
5778 if (argvars[0].v_type != VAR_UNKNOWN)
5779 timeout = get_tv_number(&argvars[0]);
5780 term_get_winpos(&x, &y, timeout);
5781 }
5782#endif
5783 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5784 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5785}
5786
5787
5788/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005789 * "getwinposx()" function
5790 */
5791 static void
5792f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5793{
5794 rettv->vval.v_number = -1;
5795#ifdef FEAT_GUI
5796 if (gui.in_use)
5797 {
5798 int x, y;
5799
5800 if (gui_mch_get_winpos(&x, &y) == OK)
5801 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005802 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005803 }
5804#endif
5805#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5806 {
5807 int x, y;
5808
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005809 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005810 rettv->vval.v_number = x;
5811 }
5812#endif
5813}
5814
5815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005816 * "getwinposy()" function
5817 */
5818 static void
5819f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5820{
5821 rettv->vval.v_number = -1;
5822#ifdef FEAT_GUI
5823 if (gui.in_use)
5824 {
5825 int x, y;
5826
5827 if (gui_mch_get_winpos(&x, &y) == OK)
5828 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005829 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005830 }
5831#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005832#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5833 {
5834 int x, y;
5835
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005836 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005837 rettv->vval.v_number = y;
5838 }
5839#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840}
5841
5842/*
5843 * "getwinvar()" function
5844 */
5845 static void
5846f_getwinvar(typval_T *argvars, typval_T *rettv)
5847{
5848 getwinvar(argvars, rettv, 0);
5849}
5850
5851/*
5852 * "glob()" function
5853 */
5854 static void
5855f_glob(typval_T *argvars, typval_T *rettv)
5856{
5857 int options = WILD_SILENT|WILD_USE_NL;
5858 expand_T xpc;
5859 int error = FALSE;
5860
5861 /* When the optional second argument is non-zero, don't remove matches
5862 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5863 rettv->v_type = VAR_STRING;
5864 if (argvars[1].v_type != VAR_UNKNOWN)
5865 {
5866 if (get_tv_number_chk(&argvars[1], &error))
5867 options |= WILD_KEEP_ALL;
5868 if (argvars[2].v_type != VAR_UNKNOWN)
5869 {
5870 if (get_tv_number_chk(&argvars[2], &error))
5871 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005872 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005873 }
5874 if (argvars[3].v_type != VAR_UNKNOWN
5875 && get_tv_number_chk(&argvars[3], &error))
5876 options |= WILD_ALLLINKS;
5877 }
5878 }
5879 if (!error)
5880 {
5881 ExpandInit(&xpc);
5882 xpc.xp_context = EXPAND_FILES;
5883 if (p_wic)
5884 options += WILD_ICASE;
5885 if (rettv->v_type == VAR_STRING)
5886 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5887 NULL, options, WILD_ALL);
5888 else if (rettv_list_alloc(rettv) != FAIL)
5889 {
5890 int i;
5891
5892 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5893 NULL, options, WILD_ALL_KEEP);
5894 for (i = 0; i < xpc.xp_numfiles; i++)
5895 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5896
5897 ExpandCleanup(&xpc);
5898 }
5899 }
5900 else
5901 rettv->vval.v_string = NULL;
5902}
5903
5904/*
5905 * "globpath()" function
5906 */
5907 static void
5908f_globpath(typval_T *argvars, typval_T *rettv)
5909{
5910 int flags = 0;
5911 char_u buf1[NUMBUFLEN];
5912 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5913 int error = FALSE;
5914 garray_T ga;
5915 int i;
5916
5917 /* When the optional second argument is non-zero, don't remove matches
5918 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5919 rettv->v_type = VAR_STRING;
5920 if (argvars[2].v_type != VAR_UNKNOWN)
5921 {
5922 if (get_tv_number_chk(&argvars[2], &error))
5923 flags |= WILD_KEEP_ALL;
5924 if (argvars[3].v_type != VAR_UNKNOWN)
5925 {
5926 if (get_tv_number_chk(&argvars[3], &error))
5927 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005928 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005929 }
5930 if (argvars[4].v_type != VAR_UNKNOWN
5931 && get_tv_number_chk(&argvars[4], &error))
5932 flags |= WILD_ALLLINKS;
5933 }
5934 }
5935 if (file != NULL && !error)
5936 {
5937 ga_init2(&ga, (int)sizeof(char_u *), 10);
5938 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5939 if (rettv->v_type == VAR_STRING)
5940 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5941 else if (rettv_list_alloc(rettv) != FAIL)
5942 for (i = 0; i < ga.ga_len; ++i)
5943 list_append_string(rettv->vval.v_list,
5944 ((char_u **)(ga.ga_data))[i], -1);
5945 ga_clear_strings(&ga);
5946 }
5947 else
5948 rettv->vval.v_string = NULL;
5949}
5950
5951/*
5952 * "glob2regpat()" function
5953 */
5954 static void
5955f_glob2regpat(typval_T *argvars, typval_T *rettv)
5956{
5957 char_u *pat = get_tv_string_chk(&argvars[0]);
5958
5959 rettv->v_type = VAR_STRING;
5960 rettv->vval.v_string = (pat == NULL)
5961 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5962}
5963
5964/* for VIM_VERSION_ defines */
5965#include "version.h"
5966
5967/*
5968 * "has()" function
5969 */
5970 static void
5971f_has(typval_T *argvars, typval_T *rettv)
5972{
5973 int i;
5974 char_u *name;
5975 int n = FALSE;
5976 static char *(has_list[]) =
5977 {
5978#ifdef AMIGA
5979 "amiga",
5980# ifdef FEAT_ARP
5981 "arp",
5982# endif
5983#endif
5984#ifdef __BEOS__
5985 "beos",
5986#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005987#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005988 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5989 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005990# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005991 "macunix", /* Mac OS X, with the darwin feature */
5992 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005993# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005994#endif
5995#ifdef __QNX__
5996 "qnx",
5997#endif
5998#ifdef UNIX
5999 "unix",
6000#endif
6001#ifdef VMS
6002 "vms",
6003#endif
6004#ifdef WIN32
6005 "win32",
6006#endif
6007#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6008 "win32unix",
6009#endif
6010#if defined(WIN64) || defined(_WIN64)
6011 "win64",
6012#endif
6013#ifdef EBCDIC
6014 "ebcdic",
6015#endif
6016#ifndef CASE_INSENSITIVE_FILENAME
6017 "fname_case",
6018#endif
6019#ifdef HAVE_ACL
6020 "acl",
6021#endif
6022#ifdef FEAT_ARABIC
6023 "arabic",
6024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006025 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006026#ifdef FEAT_AUTOCHDIR
6027 "autochdir",
6028#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006029#ifdef FEAT_AUTOSERVERNAME
6030 "autoservername",
6031#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006032#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 "balloon_eval",
6034# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6035 "balloon_multiline",
6036# endif
6037#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006038#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006039 "balloon_eval_term",
6040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006041#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6042 "builtin_terms",
6043# ifdef ALL_BUILTIN_TCAPS
6044 "all_builtin_terms",
6045# endif
6046#endif
6047#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6048 || defined(FEAT_GUI_W32) \
6049 || defined(FEAT_GUI_MOTIF))
6050 "browsefilter",
6051#endif
6052#ifdef FEAT_BYTEOFF
6053 "byte_offset",
6054#endif
6055#ifdef FEAT_JOB_CHANNEL
6056 "channel",
6057#endif
6058#ifdef FEAT_CINDENT
6059 "cindent",
6060#endif
6061#ifdef FEAT_CLIENTSERVER
6062 "clientserver",
6063#endif
6064#ifdef FEAT_CLIPBOARD
6065 "clipboard",
6066#endif
6067#ifdef FEAT_CMDL_COMPL
6068 "cmdline_compl",
6069#endif
6070#ifdef FEAT_CMDHIST
6071 "cmdline_hist",
6072#endif
6073#ifdef FEAT_COMMENTS
6074 "comments",
6075#endif
6076#ifdef FEAT_CONCEAL
6077 "conceal",
6078#endif
6079#ifdef FEAT_CRYPT
6080 "cryptv",
6081 "crypt-blowfish",
6082 "crypt-blowfish2",
6083#endif
6084#ifdef FEAT_CSCOPE
6085 "cscope",
6086#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006088#ifdef CURSOR_SHAPE
6089 "cursorshape",
6090#endif
6091#ifdef DEBUG
6092 "debug",
6093#endif
6094#ifdef FEAT_CON_DIALOG
6095 "dialog_con",
6096#endif
6097#ifdef FEAT_GUI_DIALOG
6098 "dialog_gui",
6099#endif
6100#ifdef FEAT_DIFF
6101 "diff",
6102#endif
6103#ifdef FEAT_DIGRAPHS
6104 "digraphs",
6105#endif
6106#ifdef FEAT_DIRECTX
6107 "directx",
6108#endif
6109#ifdef FEAT_DND
6110 "dnd",
6111#endif
6112#ifdef FEAT_EMACS_TAGS
6113 "emacs_tags",
6114#endif
6115 "eval", /* always present, of course! */
6116 "ex_extra", /* graduated feature */
6117#ifdef FEAT_SEARCH_EXTRA
6118 "extra_search",
6119#endif
6120#ifdef FEAT_FKMAP
6121 "farsi",
6122#endif
6123#ifdef FEAT_SEARCHPATH
6124 "file_in_path",
6125#endif
6126#ifdef FEAT_FILTERPIPE
6127 "filterpipe",
6128#endif
6129#ifdef FEAT_FIND_ID
6130 "find_in_path",
6131#endif
6132#ifdef FEAT_FLOAT
6133 "float",
6134#endif
6135#ifdef FEAT_FOLDING
6136 "folding",
6137#endif
6138#ifdef FEAT_FOOTER
6139 "footer",
6140#endif
6141#if !defined(USE_SYSTEM) && defined(UNIX)
6142 "fork",
6143#endif
6144#ifdef FEAT_GETTEXT
6145 "gettext",
6146#endif
6147#ifdef FEAT_GUI
6148 "gui",
6149#endif
6150#ifdef FEAT_GUI_ATHENA
6151# ifdef FEAT_GUI_NEXTAW
6152 "gui_neXtaw",
6153# else
6154 "gui_athena",
6155# endif
6156#endif
6157#ifdef FEAT_GUI_GTK
6158 "gui_gtk",
6159# ifdef USE_GTK3
6160 "gui_gtk3",
6161# else
6162 "gui_gtk2",
6163# endif
6164#endif
6165#ifdef FEAT_GUI_GNOME
6166 "gui_gnome",
6167#endif
6168#ifdef FEAT_GUI_MAC
6169 "gui_mac",
6170#endif
6171#ifdef FEAT_GUI_MOTIF
6172 "gui_motif",
6173#endif
6174#ifdef FEAT_GUI_PHOTON
6175 "gui_photon",
6176#endif
6177#ifdef FEAT_GUI_W32
6178 "gui_win32",
6179#endif
6180#ifdef FEAT_HANGULIN
6181 "hangul_input",
6182#endif
6183#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6184 "iconv",
6185#endif
6186#ifdef FEAT_INS_EXPAND
6187 "insert_expand",
6188#endif
6189#ifdef FEAT_JOB_CHANNEL
6190 "job",
6191#endif
6192#ifdef FEAT_JUMPLIST
6193 "jumplist",
6194#endif
6195#ifdef FEAT_KEYMAP
6196 "keymap",
6197#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006198 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006199#ifdef FEAT_LANGMAP
6200 "langmap",
6201#endif
6202#ifdef FEAT_LIBCALL
6203 "libcall",
6204#endif
6205#ifdef FEAT_LINEBREAK
6206 "linebreak",
6207#endif
6208#ifdef FEAT_LISP
6209 "lispindent",
6210#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006212#ifdef FEAT_LOCALMAP
6213 "localmap",
6214#endif
6215#ifdef FEAT_LUA
6216# ifndef DYNAMIC_LUA
6217 "lua",
6218# endif
6219#endif
6220#ifdef FEAT_MENU
6221 "menu",
6222#endif
6223#ifdef FEAT_SESSION
6224 "mksession",
6225#endif
6226#ifdef FEAT_MODIFY_FNAME
6227 "modify_fname",
6228#endif
6229#ifdef FEAT_MOUSE
6230 "mouse",
6231#endif
6232#ifdef FEAT_MOUSESHAPE
6233 "mouseshape",
6234#endif
6235#if defined(UNIX) || defined(VMS)
6236# ifdef FEAT_MOUSE_DEC
6237 "mouse_dec",
6238# endif
6239# ifdef FEAT_MOUSE_GPM
6240 "mouse_gpm",
6241# endif
6242# ifdef FEAT_MOUSE_JSB
6243 "mouse_jsbterm",
6244# endif
6245# ifdef FEAT_MOUSE_NET
6246 "mouse_netterm",
6247# endif
6248# ifdef FEAT_MOUSE_PTERM
6249 "mouse_pterm",
6250# endif
6251# ifdef FEAT_MOUSE_SGR
6252 "mouse_sgr",
6253# endif
6254# ifdef FEAT_SYSMOUSE
6255 "mouse_sysmouse",
6256# endif
6257# ifdef FEAT_MOUSE_URXVT
6258 "mouse_urxvt",
6259# endif
6260# ifdef FEAT_MOUSE_XTERM
6261 "mouse_xterm",
6262# endif
6263#endif
6264#ifdef FEAT_MBYTE
6265 "multi_byte",
6266#endif
6267#ifdef FEAT_MBYTE_IME
6268 "multi_byte_ime",
6269#endif
6270#ifdef FEAT_MULTI_LANG
6271 "multi_lang",
6272#endif
6273#ifdef FEAT_MZSCHEME
6274#ifndef DYNAMIC_MZSCHEME
6275 "mzscheme",
6276#endif
6277#endif
6278#ifdef FEAT_NUM64
6279 "num64",
6280#endif
6281#ifdef FEAT_OLE
6282 "ole",
6283#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006284#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006286#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287#ifdef FEAT_PATH_EXTRA
6288 "path_extra",
6289#endif
6290#ifdef FEAT_PERL
6291#ifndef DYNAMIC_PERL
6292 "perl",
6293#endif
6294#endif
6295#ifdef FEAT_PERSISTENT_UNDO
6296 "persistent_undo",
6297#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006298#if defined(FEAT_PYTHON)
6299 "python_compiled",
6300# if defined(DYNAMIC_PYTHON)
6301 "python_dynamic",
6302# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006303 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006304 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006305# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006307#if defined(FEAT_PYTHON3)
6308 "python3_compiled",
6309# if defined(DYNAMIC_PYTHON3)
6310 "python3_dynamic",
6311# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006312 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006313 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006314# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315#endif
6316#ifdef FEAT_POSTSCRIPT
6317 "postscript",
6318#endif
6319#ifdef FEAT_PRINTER
6320 "printer",
6321#endif
6322#ifdef FEAT_PROFILE
6323 "profile",
6324#endif
6325#ifdef FEAT_RELTIME
6326 "reltime",
6327#endif
6328#ifdef FEAT_QUICKFIX
6329 "quickfix",
6330#endif
6331#ifdef FEAT_RIGHTLEFT
6332 "rightleft",
6333#endif
6334#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6335 "ruby",
6336#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006337 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338#ifdef FEAT_CMDL_INFO
6339 "showcmd",
6340 "cmdline_info",
6341#endif
6342#ifdef FEAT_SIGNS
6343 "signs",
6344#endif
6345#ifdef FEAT_SMARTINDENT
6346 "smartindent",
6347#endif
6348#ifdef STARTUPTIME
6349 "startuptime",
6350#endif
6351#ifdef FEAT_STL_OPT
6352 "statusline",
6353#endif
6354#ifdef FEAT_SUN_WORKSHOP
6355 "sun_workshop",
6356#endif
6357#ifdef FEAT_NETBEANS_INTG
6358 "netbeans_intg",
6359#endif
6360#ifdef FEAT_SPELL
6361 "spell",
6362#endif
6363#ifdef FEAT_SYN_HL
6364 "syntax",
6365#endif
6366#if defined(USE_SYSTEM) || !defined(UNIX)
6367 "system",
6368#endif
6369#ifdef FEAT_TAG_BINS
6370 "tag_binary",
6371#endif
6372#ifdef FEAT_TAG_OLDSTATIC
6373 "tag_old_static",
6374#endif
6375#ifdef FEAT_TAG_ANYWHITE
6376 "tag_any_white",
6377#endif
6378#ifdef FEAT_TCL
6379# ifndef DYNAMIC_TCL
6380 "tcl",
6381# endif
6382#endif
6383#ifdef FEAT_TERMGUICOLORS
6384 "termguicolors",
6385#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006386#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006387 "terminal",
6388#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389#ifdef TERMINFO
6390 "terminfo",
6391#endif
6392#ifdef FEAT_TERMRESPONSE
6393 "termresponse",
6394#endif
6395#ifdef FEAT_TEXTOBJ
6396 "textobjects",
6397#endif
6398#ifdef HAVE_TGETENT
6399 "tgetent",
6400#endif
6401#ifdef FEAT_TIMERS
6402 "timers",
6403#endif
6404#ifdef FEAT_TITLE
6405 "title",
6406#endif
6407#ifdef FEAT_TOOLBAR
6408 "toolbar",
6409#endif
6410#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6411 "unnamedplus",
6412#endif
6413#ifdef FEAT_USR_CMDS
6414 "user-commands", /* was accidentally included in 5.4 */
6415 "user_commands",
6416#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006417#ifdef FEAT_VARTABS
6418 "vartabs",
6419#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420#ifdef FEAT_VIMINFO
6421 "viminfo",
6422#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006424#ifdef FEAT_VIRTUALEDIT
6425 "virtualedit",
6426#endif
6427 "visual",
6428#ifdef FEAT_VISUALEXTRA
6429 "visualextra",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006432#ifdef FEAT_VTP
6433 "vtp",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435#ifdef FEAT_WILDIGN
6436 "wildignore",
6437#endif
6438#ifdef FEAT_WILDMENU
6439 "wildmenu",
6440#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006442#ifdef FEAT_WAK
6443 "winaltkeys",
6444#endif
6445#ifdef FEAT_WRITEBACKUP
6446 "writebackup",
6447#endif
6448#ifdef FEAT_XIM
6449 "xim",
6450#endif
6451#ifdef FEAT_XFONTSET
6452 "xfontset",
6453#endif
6454#ifdef FEAT_XPM_W32
6455 "xpm",
6456 "xpm_w32", /* for backward compatibility */
6457#else
6458# if defined(HAVE_XPM)
6459 "xpm",
6460# endif
6461#endif
6462#ifdef USE_XSMP
6463 "xsmp",
6464#endif
6465#ifdef USE_XSMP_INTERACT
6466 "xsmp_interact",
6467#endif
6468#ifdef FEAT_XCLIPBOARD
6469 "xterm_clipboard",
6470#endif
6471#ifdef FEAT_XTERM_SAVE
6472 "xterm_save",
6473#endif
6474#if defined(UNIX) && defined(FEAT_X11)
6475 "X11",
6476#endif
6477 NULL
6478 };
6479
6480 name = get_tv_string(&argvars[0]);
6481 for (i = 0; has_list[i] != NULL; ++i)
6482 if (STRICMP(name, has_list[i]) == 0)
6483 {
6484 n = TRUE;
6485 break;
6486 }
6487
6488 if (n == FALSE)
6489 {
6490 if (STRNICMP(name, "patch", 5) == 0)
6491 {
6492 if (name[5] == '-'
6493 && STRLEN(name) >= 11
6494 && vim_isdigit(name[6])
6495 && vim_isdigit(name[8])
6496 && vim_isdigit(name[10]))
6497 {
6498 int major = atoi((char *)name + 6);
6499 int minor = atoi((char *)name + 8);
6500
6501 /* Expect "patch-9.9.01234". */
6502 n = (major < VIM_VERSION_MAJOR
6503 || (major == VIM_VERSION_MAJOR
6504 && (minor < VIM_VERSION_MINOR
6505 || (minor == VIM_VERSION_MINOR
6506 && has_patch(atoi((char *)name + 10))))));
6507 }
6508 else
6509 n = has_patch(atoi((char *)name + 5));
6510 }
6511 else if (STRICMP(name, "vim_starting") == 0)
6512 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006513 else if (STRICMP(name, "ttyin") == 0)
6514 n = mch_input_isatty();
6515 else if (STRICMP(name, "ttyout") == 0)
6516 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef FEAT_MBYTE
6518 else if (STRICMP(name, "multi_byte_encoding") == 0)
6519 n = has_mbyte;
6520#endif
6521#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6522 else if (STRICMP(name, "balloon_multiline") == 0)
6523 n = multiline_balloon_available();
6524#endif
6525#ifdef DYNAMIC_TCL
6526 else if (STRICMP(name, "tcl") == 0)
6527 n = tcl_enabled(FALSE);
6528#endif
6529#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6530 else if (STRICMP(name, "iconv") == 0)
6531 n = iconv_enabled(FALSE);
6532#endif
6533#ifdef DYNAMIC_LUA
6534 else if (STRICMP(name, "lua") == 0)
6535 n = lua_enabled(FALSE);
6536#endif
6537#ifdef DYNAMIC_MZSCHEME
6538 else if (STRICMP(name, "mzscheme") == 0)
6539 n = mzscheme_enabled(FALSE);
6540#endif
6541#ifdef DYNAMIC_RUBY
6542 else if (STRICMP(name, "ruby") == 0)
6543 n = ruby_enabled(FALSE);
6544#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006545#ifdef DYNAMIC_PYTHON
6546 else if (STRICMP(name, "python") == 0)
6547 n = python_enabled(FALSE);
6548#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006549#ifdef DYNAMIC_PYTHON3
6550 else if (STRICMP(name, "python3") == 0)
6551 n = python3_enabled(FALSE);
6552#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006553#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6554 else if (STRICMP(name, "pythonx") == 0)
6555 {
6556# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6557 if (p_pyx == 0)
6558 n = python3_enabled(FALSE) || python_enabled(FALSE);
6559 else if (p_pyx == 3)
6560 n = python3_enabled(FALSE);
6561 else if (p_pyx == 2)
6562 n = python_enabled(FALSE);
6563# elif defined(DYNAMIC_PYTHON)
6564 n = python_enabled(FALSE);
6565# elif defined(DYNAMIC_PYTHON3)
6566 n = python3_enabled(FALSE);
6567# endif
6568 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#endif
6570#ifdef DYNAMIC_PERL
6571 else if (STRICMP(name, "perl") == 0)
6572 n = perl_enabled(FALSE);
6573#endif
6574#ifdef FEAT_GUI
6575 else if (STRICMP(name, "gui_running") == 0)
6576 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577# ifdef FEAT_BROWSE
6578 else if (STRICMP(name, "browse") == 0)
6579 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6580# endif
6581#endif
6582#ifdef FEAT_SYN_HL
6583 else if (STRICMP(name, "syntax_items") == 0)
6584 n = syntax_present(curwin);
6585#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006586#ifdef FEAT_VTP
6587 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006588 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006589#endif
6590#ifdef FEAT_NETBEANS_INTG
6591 else if (STRICMP(name, "netbeans_enabled") == 0)
6592 n = netbeans_active();
6593#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006594#if defined(FEAT_TERMINAL) && defined(WIN3264)
6595 else if (STRICMP(name, "terminal") == 0)
6596 n = terminal_enabled();
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 }
6599
6600 rettv->vval.v_number = n;
6601}
6602
6603/*
6604 * "has_key()" function
6605 */
6606 static void
6607f_has_key(typval_T *argvars, typval_T *rettv)
6608{
6609 if (argvars[0].v_type != VAR_DICT)
6610 {
6611 EMSG(_(e_dictreq));
6612 return;
6613 }
6614 if (argvars[0].vval.v_dict == NULL)
6615 return;
6616
6617 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6618 get_tv_string(&argvars[1]), -1) != NULL;
6619}
6620
6621/*
6622 * "haslocaldir()" function
6623 */
6624 static void
6625f_haslocaldir(typval_T *argvars, typval_T *rettv)
6626{
6627 win_T *wp = NULL;
6628
6629 wp = find_tabwin(&argvars[0], &argvars[1]);
6630 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6631}
6632
6633/*
6634 * "hasmapto()" function
6635 */
6636 static void
6637f_hasmapto(typval_T *argvars, typval_T *rettv)
6638{
6639 char_u *name;
6640 char_u *mode;
6641 char_u buf[NUMBUFLEN];
6642 int abbr = FALSE;
6643
6644 name = get_tv_string(&argvars[0]);
6645 if (argvars[1].v_type == VAR_UNKNOWN)
6646 mode = (char_u *)"nvo";
6647 else
6648 {
6649 mode = get_tv_string_buf(&argvars[1], buf);
6650 if (argvars[2].v_type != VAR_UNKNOWN)
6651 abbr = (int)get_tv_number(&argvars[2]);
6652 }
6653
6654 if (map_to_exists(name, mode, abbr))
6655 rettv->vval.v_number = TRUE;
6656 else
6657 rettv->vval.v_number = FALSE;
6658}
6659
6660/*
6661 * "histadd()" function
6662 */
6663 static void
6664f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6665{
6666#ifdef FEAT_CMDHIST
6667 int histype;
6668 char_u *str;
6669 char_u buf[NUMBUFLEN];
6670#endif
6671
6672 rettv->vval.v_number = FALSE;
6673 if (check_restricted() || check_secure())
6674 return;
6675#ifdef FEAT_CMDHIST
6676 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6677 histype = str != NULL ? get_histtype(str) : -1;
6678 if (histype >= 0)
6679 {
6680 str = get_tv_string_buf(&argvars[1], buf);
6681 if (*str != NUL)
6682 {
6683 init_history();
6684 add_to_history(histype, str, FALSE, NUL);
6685 rettv->vval.v_number = TRUE;
6686 return;
6687 }
6688 }
6689#endif
6690}
6691
6692/*
6693 * "histdel()" function
6694 */
6695 static void
6696f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6697{
6698#ifdef FEAT_CMDHIST
6699 int n;
6700 char_u buf[NUMBUFLEN];
6701 char_u *str;
6702
6703 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6704 if (str == NULL)
6705 n = 0;
6706 else if (argvars[1].v_type == VAR_UNKNOWN)
6707 /* only one argument: clear entire history */
6708 n = clr_history(get_histtype(str));
6709 else if (argvars[1].v_type == VAR_NUMBER)
6710 /* index given: remove that entry */
6711 n = del_history_idx(get_histtype(str),
6712 (int)get_tv_number(&argvars[1]));
6713 else
6714 /* string given: remove all matching entries */
6715 n = del_history_entry(get_histtype(str),
6716 get_tv_string_buf(&argvars[1], buf));
6717 rettv->vval.v_number = n;
6718#endif
6719}
6720
6721/*
6722 * "histget()" function
6723 */
6724 static void
6725f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6726{
6727#ifdef FEAT_CMDHIST
6728 int type;
6729 int idx;
6730 char_u *str;
6731
6732 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6733 if (str == NULL)
6734 rettv->vval.v_string = NULL;
6735 else
6736 {
6737 type = get_histtype(str);
6738 if (argvars[1].v_type == VAR_UNKNOWN)
6739 idx = get_history_idx(type);
6740 else
6741 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6742 /* -1 on type error */
6743 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6744 }
6745#else
6746 rettv->vval.v_string = NULL;
6747#endif
6748 rettv->v_type = VAR_STRING;
6749}
6750
6751/*
6752 * "histnr()" function
6753 */
6754 static void
6755f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6756{
6757 int i;
6758
6759#ifdef FEAT_CMDHIST
6760 char_u *history = get_tv_string_chk(&argvars[0]);
6761
6762 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6763 if (i >= HIST_CMD && i < HIST_COUNT)
6764 i = get_history_idx(i);
6765 else
6766#endif
6767 i = -1;
6768 rettv->vval.v_number = i;
6769}
6770
6771/*
6772 * "highlightID(name)" function
6773 */
6774 static void
6775f_hlID(typval_T *argvars, typval_T *rettv)
6776{
6777 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6778}
6779
6780/*
6781 * "highlight_exists()" function
6782 */
6783 static void
6784f_hlexists(typval_T *argvars, typval_T *rettv)
6785{
6786 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6787}
6788
6789/*
6790 * "hostname()" function
6791 */
6792 static void
6793f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6794{
6795 char_u hostname[256];
6796
6797 mch_get_host_name(hostname, 256);
6798 rettv->v_type = VAR_STRING;
6799 rettv->vval.v_string = vim_strsave(hostname);
6800}
6801
6802/*
6803 * iconv() function
6804 */
6805 static void
6806f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6807{
6808#ifdef FEAT_MBYTE
6809 char_u buf1[NUMBUFLEN];
6810 char_u buf2[NUMBUFLEN];
6811 char_u *from, *to, *str;
6812 vimconv_T vimconv;
6813#endif
6814
6815 rettv->v_type = VAR_STRING;
6816 rettv->vval.v_string = NULL;
6817
6818#ifdef FEAT_MBYTE
6819 str = get_tv_string(&argvars[0]);
6820 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6821 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6822 vimconv.vc_type = CONV_NONE;
6823 convert_setup(&vimconv, from, to);
6824
6825 /* If the encodings are equal, no conversion needed. */
6826 if (vimconv.vc_type == CONV_NONE)
6827 rettv->vval.v_string = vim_strsave(str);
6828 else
6829 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6830
6831 convert_setup(&vimconv, NULL, NULL);
6832 vim_free(from);
6833 vim_free(to);
6834#endif
6835}
6836
6837/*
6838 * "indent()" function
6839 */
6840 static void
6841f_indent(typval_T *argvars, typval_T *rettv)
6842{
6843 linenr_T lnum;
6844
6845 lnum = get_tv_lnum(argvars);
6846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6847 rettv->vval.v_number = get_indent_lnum(lnum);
6848 else
6849 rettv->vval.v_number = -1;
6850}
6851
6852/*
6853 * "index()" function
6854 */
6855 static void
6856f_index(typval_T *argvars, typval_T *rettv)
6857{
6858 list_T *l;
6859 listitem_T *item;
6860 long idx = 0;
6861 int ic = FALSE;
6862
6863 rettv->vval.v_number = -1;
6864 if (argvars[0].v_type != VAR_LIST)
6865 {
6866 EMSG(_(e_listreq));
6867 return;
6868 }
6869 l = argvars[0].vval.v_list;
6870 if (l != NULL)
6871 {
6872 item = l->lv_first;
6873 if (argvars[2].v_type != VAR_UNKNOWN)
6874 {
6875 int error = FALSE;
6876
6877 /* Start at specified item. Use the cached index that list_find()
6878 * sets, so that a negative number also works. */
6879 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6880 idx = l->lv_idx;
6881 if (argvars[3].v_type != VAR_UNKNOWN)
6882 ic = (int)get_tv_number_chk(&argvars[3], &error);
6883 if (error)
6884 item = NULL;
6885 }
6886
6887 for ( ; item != NULL; item = item->li_next, ++idx)
6888 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6889 {
6890 rettv->vval.v_number = idx;
6891 break;
6892 }
6893 }
6894}
6895
6896static int inputsecret_flag = 0;
6897
6898/*
6899 * "input()" function
6900 * Also handles inputsecret() when inputsecret is set.
6901 */
6902 static void
6903f_input(typval_T *argvars, typval_T *rettv)
6904{
6905 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6906}
6907
6908/*
6909 * "inputdialog()" function
6910 */
6911 static void
6912f_inputdialog(typval_T *argvars, typval_T *rettv)
6913{
6914#if defined(FEAT_GUI_TEXTDIALOG)
6915 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6916 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6917 {
6918 char_u *message;
6919 char_u buf[NUMBUFLEN];
6920 char_u *defstr = (char_u *)"";
6921
6922 message = get_tv_string_chk(&argvars[0]);
6923 if (argvars[1].v_type != VAR_UNKNOWN
6924 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6925 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6926 else
6927 IObuff[0] = NUL;
6928 if (message != NULL && defstr != NULL
6929 && do_dialog(VIM_QUESTION, NULL, message,
6930 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6931 rettv->vval.v_string = vim_strsave(IObuff);
6932 else
6933 {
6934 if (message != NULL && defstr != NULL
6935 && argvars[1].v_type != VAR_UNKNOWN
6936 && argvars[2].v_type != VAR_UNKNOWN)
6937 rettv->vval.v_string = vim_strsave(
6938 get_tv_string_buf(&argvars[2], buf));
6939 else
6940 rettv->vval.v_string = NULL;
6941 }
6942 rettv->v_type = VAR_STRING;
6943 }
6944 else
6945#endif
6946 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6947}
6948
6949/*
6950 * "inputlist()" function
6951 */
6952 static void
6953f_inputlist(typval_T *argvars, typval_T *rettv)
6954{
6955 listitem_T *li;
6956 int selected;
6957 int mouse_used;
6958
6959#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006960 /* While starting up, there is no place to enter text. When running tests
6961 * with --not-a-term we assume feedkeys() will be used. */
6962 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963 return;
6964#endif
6965 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6966 {
6967 EMSG2(_(e_listarg), "inputlist()");
6968 return;
6969 }
6970
6971 msg_start();
6972 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6973 lines_left = Rows; /* avoid more prompt */
6974 msg_scroll = TRUE;
6975 msg_clr_eos();
6976
6977 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6978 {
6979 msg_puts(get_tv_string(&li->li_tv));
6980 msg_putchar('\n');
6981 }
6982
6983 /* Ask for choice. */
6984 selected = prompt_for_number(&mouse_used);
6985 if (mouse_used)
6986 selected -= lines_left;
6987
6988 rettv->vval.v_number = selected;
6989}
6990
6991
6992static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6993
6994/*
6995 * "inputrestore()" function
6996 */
6997 static void
6998f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6999{
7000 if (ga_userinput.ga_len > 0)
7001 {
7002 --ga_userinput.ga_len;
7003 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7004 + ga_userinput.ga_len);
7005 /* default return is zero == OK */
7006 }
7007 else if (p_verbose > 1)
7008 {
7009 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7010 rettv->vval.v_number = 1; /* Failed */
7011 }
7012}
7013
7014/*
7015 * "inputsave()" function
7016 */
7017 static void
7018f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7019{
7020 /* Add an entry to the stack of typeahead storage. */
7021 if (ga_grow(&ga_userinput, 1) == OK)
7022 {
7023 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7024 + ga_userinput.ga_len);
7025 ++ga_userinput.ga_len;
7026 /* default return is zero == OK */
7027 }
7028 else
7029 rettv->vval.v_number = 1; /* Failed */
7030}
7031
7032/*
7033 * "inputsecret()" function
7034 */
7035 static void
7036f_inputsecret(typval_T *argvars, typval_T *rettv)
7037{
7038 ++cmdline_star;
7039 ++inputsecret_flag;
7040 f_input(argvars, rettv);
7041 --cmdline_star;
7042 --inputsecret_flag;
7043}
7044
7045/*
7046 * "insert()" function
7047 */
7048 static void
7049f_insert(typval_T *argvars, typval_T *rettv)
7050{
7051 long before = 0;
7052 listitem_T *item;
7053 list_T *l;
7054 int error = FALSE;
7055
7056 if (argvars[0].v_type != VAR_LIST)
7057 EMSG2(_(e_listarg), "insert()");
7058 else if ((l = argvars[0].vval.v_list) != NULL
7059 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7060 {
7061 if (argvars[2].v_type != VAR_UNKNOWN)
7062 before = (long)get_tv_number_chk(&argvars[2], &error);
7063 if (error)
7064 return; /* type error; errmsg already given */
7065
7066 if (before == l->lv_len)
7067 item = NULL;
7068 else
7069 {
7070 item = list_find(l, before);
7071 if (item == NULL)
7072 {
7073 EMSGN(_(e_listidx), before);
7074 l = NULL;
7075 }
7076 }
7077 if (l != NULL)
7078 {
7079 list_insert_tv(l, &argvars[1], item);
7080 copy_tv(&argvars[0], rettv);
7081 }
7082 }
7083}
7084
7085/*
7086 * "invert(expr)" function
7087 */
7088 static void
7089f_invert(typval_T *argvars, typval_T *rettv)
7090{
7091 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7092}
7093
7094/*
7095 * "isdirectory()" function
7096 */
7097 static void
7098f_isdirectory(typval_T *argvars, typval_T *rettv)
7099{
7100 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7101}
7102
7103/*
7104 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7105 * or it refers to a List or Dictionary that is locked.
7106 */
7107 static int
7108tv_islocked(typval_T *tv)
7109{
7110 return (tv->v_lock & VAR_LOCKED)
7111 || (tv->v_type == VAR_LIST
7112 && tv->vval.v_list != NULL
7113 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7114 || (tv->v_type == VAR_DICT
7115 && tv->vval.v_dict != NULL
7116 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7117}
7118
7119/*
7120 * "islocked()" function
7121 */
7122 static void
7123f_islocked(typval_T *argvars, typval_T *rettv)
7124{
7125 lval_T lv;
7126 char_u *end;
7127 dictitem_T *di;
7128
7129 rettv->vval.v_number = -1;
7130 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007131 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 if (end != NULL && lv.ll_name != NULL)
7133 {
7134 if (*end != NUL)
7135 EMSG(_(e_trailing));
7136 else
7137 {
7138 if (lv.ll_tv == NULL)
7139 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007140 di = find_var(lv.ll_name, NULL, TRUE);
7141 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007143 /* Consider a variable locked when:
7144 * 1. the variable itself is locked
7145 * 2. the value of the variable is locked.
7146 * 3. the List or Dict value is locked.
7147 */
7148 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7149 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 }
7151 }
7152 else if (lv.ll_range)
7153 EMSG(_("E786: Range not allowed"));
7154 else if (lv.ll_newkey != NULL)
7155 EMSG2(_(e_dictkey), lv.ll_newkey);
7156 else if (lv.ll_list != NULL)
7157 /* List item. */
7158 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7159 else
7160 /* Dictionary item. */
7161 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7162 }
7163 }
7164
7165 clear_lval(&lv);
7166}
7167
7168#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7169/*
7170 * "isnan()" function
7171 */
7172 static void
7173f_isnan(typval_T *argvars, typval_T *rettv)
7174{
7175 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7176 && isnan(argvars[0].vval.v_float);
7177}
7178#endif
7179
7180/*
7181 * "items(dict)" function
7182 */
7183 static void
7184f_items(typval_T *argvars, typval_T *rettv)
7185{
7186 dict_list(argvars, rettv, 2);
7187}
7188
7189#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7190/*
7191 * Get the job from the argument.
7192 * Returns NULL if the job is invalid.
7193 */
7194 static job_T *
7195get_job_arg(typval_T *tv)
7196{
7197 job_T *job;
7198
7199 if (tv->v_type != VAR_JOB)
7200 {
7201 EMSG2(_(e_invarg2), get_tv_string(tv));
7202 return NULL;
7203 }
7204 job = tv->vval.v_job;
7205
7206 if (job == NULL)
7207 EMSG(_("E916: not a valid job"));
7208 return job;
7209}
7210
7211/*
7212 * "job_getchannel()" function
7213 */
7214 static void
7215f_job_getchannel(typval_T *argvars, typval_T *rettv)
7216{
7217 job_T *job = get_job_arg(&argvars[0]);
7218
7219 if (job != NULL)
7220 {
7221 rettv->v_type = VAR_CHANNEL;
7222 rettv->vval.v_channel = job->jv_channel;
7223 if (job->jv_channel != NULL)
7224 ++job->jv_channel->ch_refcount;
7225 }
7226}
7227
7228/*
7229 * "job_info()" function
7230 */
7231 static void
7232f_job_info(typval_T *argvars, typval_T *rettv)
7233{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007234 if (argvars[0].v_type != VAR_UNKNOWN)
7235 {
7236 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007238 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7239 job_info(job, rettv->vval.v_dict);
7240 }
7241 else if (rettv_list_alloc(rettv) == OK)
7242 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007243}
7244
7245/*
7246 * "job_setoptions()" function
7247 */
7248 static void
7249f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7250{
7251 job_T *job = get_job_arg(&argvars[0]);
7252 jobopt_T opt;
7253
7254 if (job == NULL)
7255 return;
7256 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007257 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258 job_set_options(job, &opt);
7259 free_job_options(&opt);
7260}
7261
7262/*
7263 * "job_start()" function
7264 */
7265 static void
7266f_job_start(typval_T *argvars, typval_T *rettv)
7267{
7268 rettv->v_type = VAR_JOB;
7269 if (check_restricted() || check_secure())
7270 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007271 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007272}
7273
7274/*
7275 * "job_status()" function
7276 */
7277 static void
7278f_job_status(typval_T *argvars, typval_T *rettv)
7279{
7280 job_T *job = get_job_arg(&argvars[0]);
7281
7282 if (job != NULL)
7283 {
7284 rettv->v_type = VAR_STRING;
7285 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7286 }
7287}
7288
7289/*
7290 * "job_stop()" function
7291 */
7292 static void
7293f_job_stop(typval_T *argvars, typval_T *rettv)
7294{
7295 job_T *job = get_job_arg(&argvars[0]);
7296
7297 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007298 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299}
7300#endif
7301
7302/*
7303 * "join()" function
7304 */
7305 static void
7306f_join(typval_T *argvars, typval_T *rettv)
7307{
7308 garray_T ga;
7309 char_u *sep;
7310
7311 if (argvars[0].v_type != VAR_LIST)
7312 {
7313 EMSG(_(e_listreq));
7314 return;
7315 }
7316 if (argvars[0].vval.v_list == NULL)
7317 return;
7318 if (argvars[1].v_type == VAR_UNKNOWN)
7319 sep = (char_u *)" ";
7320 else
7321 sep = get_tv_string_chk(&argvars[1]);
7322
7323 rettv->v_type = VAR_STRING;
7324
7325 if (sep != NULL)
7326 {
7327 ga_init2(&ga, (int)sizeof(char), 80);
7328 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7329 ga_append(&ga, NUL);
7330 rettv->vval.v_string = (char_u *)ga.ga_data;
7331 }
7332 else
7333 rettv->vval.v_string = NULL;
7334}
7335
7336/*
7337 * "js_decode()" function
7338 */
7339 static void
7340f_js_decode(typval_T *argvars, typval_T *rettv)
7341{
7342 js_read_T reader;
7343
7344 reader.js_buf = get_tv_string(&argvars[0]);
7345 reader.js_fill = NULL;
7346 reader.js_used = 0;
7347 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7348 EMSG(_(e_invarg));
7349}
7350
7351/*
7352 * "js_encode()" function
7353 */
7354 static void
7355f_js_encode(typval_T *argvars, typval_T *rettv)
7356{
7357 rettv->v_type = VAR_STRING;
7358 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7359}
7360
7361/*
7362 * "json_decode()" function
7363 */
7364 static void
7365f_json_decode(typval_T *argvars, typval_T *rettv)
7366{
7367 js_read_T reader;
7368
7369 reader.js_buf = get_tv_string(&argvars[0]);
7370 reader.js_fill = NULL;
7371 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007372 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007373}
7374
7375/*
7376 * "json_encode()" function
7377 */
7378 static void
7379f_json_encode(typval_T *argvars, typval_T *rettv)
7380{
7381 rettv->v_type = VAR_STRING;
7382 rettv->vval.v_string = json_encode(&argvars[0], 0);
7383}
7384
7385/*
7386 * "keys()" function
7387 */
7388 static void
7389f_keys(typval_T *argvars, typval_T *rettv)
7390{
7391 dict_list(argvars, rettv, 0);
7392}
7393
7394/*
7395 * "last_buffer_nr()" function.
7396 */
7397 static void
7398f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7399{
7400 int n = 0;
7401 buf_T *buf;
7402
Bram Moolenaar29323592016-07-24 22:04:11 +02007403 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404 if (n < buf->b_fnum)
7405 n = buf->b_fnum;
7406
7407 rettv->vval.v_number = n;
7408}
7409
7410/*
7411 * "len()" function
7412 */
7413 static void
7414f_len(typval_T *argvars, typval_T *rettv)
7415{
7416 switch (argvars[0].v_type)
7417 {
7418 case VAR_STRING:
7419 case VAR_NUMBER:
7420 rettv->vval.v_number = (varnumber_T)STRLEN(
7421 get_tv_string(&argvars[0]));
7422 break;
7423 case VAR_LIST:
7424 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7425 break;
7426 case VAR_DICT:
7427 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7428 break;
7429 case VAR_UNKNOWN:
7430 case VAR_SPECIAL:
7431 case VAR_FLOAT:
7432 case VAR_FUNC:
7433 case VAR_PARTIAL:
7434 case VAR_JOB:
7435 case VAR_CHANNEL:
7436 EMSG(_("E701: Invalid type for len()"));
7437 break;
7438 }
7439}
7440
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007442libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443{
7444#ifdef FEAT_LIBCALL
7445 char_u *string_in;
7446 char_u **string_result;
7447 int nr_result;
7448#endif
7449
7450 rettv->v_type = type;
7451 if (type != VAR_NUMBER)
7452 rettv->vval.v_string = NULL;
7453
7454 if (check_restricted() || check_secure())
7455 return;
7456
7457#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007458 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007459 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7460 {
7461 string_in = NULL;
7462 if (argvars[2].v_type == VAR_STRING)
7463 string_in = argvars[2].vval.v_string;
7464 if (type == VAR_NUMBER)
7465 string_result = NULL;
7466 else
7467 string_result = &rettv->vval.v_string;
7468 if (mch_libcall(argvars[0].vval.v_string,
7469 argvars[1].vval.v_string,
7470 string_in,
7471 argvars[2].vval.v_number,
7472 string_result,
7473 &nr_result) == OK
7474 && type == VAR_NUMBER)
7475 rettv->vval.v_number = nr_result;
7476 }
7477#endif
7478}
7479
7480/*
7481 * "libcall()" function
7482 */
7483 static void
7484f_libcall(typval_T *argvars, typval_T *rettv)
7485{
7486 libcall_common(argvars, rettv, VAR_STRING);
7487}
7488
7489/*
7490 * "libcallnr()" function
7491 */
7492 static void
7493f_libcallnr(typval_T *argvars, typval_T *rettv)
7494{
7495 libcall_common(argvars, rettv, VAR_NUMBER);
7496}
7497
7498/*
7499 * "line(string)" function
7500 */
7501 static void
7502f_line(typval_T *argvars, typval_T *rettv)
7503{
7504 linenr_T lnum = 0;
7505 pos_T *fp;
7506 int fnum;
7507
7508 fp = var2fpos(&argvars[0], TRUE, &fnum);
7509 if (fp != NULL)
7510 lnum = fp->lnum;
7511 rettv->vval.v_number = lnum;
7512}
7513
7514/*
7515 * "line2byte(lnum)" function
7516 */
7517 static void
7518f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7519{
7520#ifndef FEAT_BYTEOFF
7521 rettv->vval.v_number = -1;
7522#else
7523 linenr_T lnum;
7524
7525 lnum = get_tv_lnum(argvars);
7526 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7527 rettv->vval.v_number = -1;
7528 else
7529 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7530 if (rettv->vval.v_number >= 0)
7531 ++rettv->vval.v_number;
7532#endif
7533}
7534
7535/*
7536 * "lispindent(lnum)" function
7537 */
7538 static void
7539f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7540{
7541#ifdef FEAT_LISP
7542 pos_T pos;
7543 linenr_T lnum;
7544
7545 pos = curwin->w_cursor;
7546 lnum = get_tv_lnum(argvars);
7547 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7548 {
7549 curwin->w_cursor.lnum = lnum;
7550 rettv->vval.v_number = get_lisp_indent();
7551 curwin->w_cursor = pos;
7552 }
7553 else
7554#endif
7555 rettv->vval.v_number = -1;
7556}
7557
7558/*
7559 * "localtime()" function
7560 */
7561 static void
7562f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7563{
7564 rettv->vval.v_number = (varnumber_T)time(NULL);
7565}
7566
7567static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7568
7569 static void
7570get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7571{
7572 char_u *keys;
7573 char_u *which;
7574 char_u buf[NUMBUFLEN];
7575 char_u *keys_buf = NULL;
7576 char_u *rhs;
7577 int mode;
7578 int abbr = FALSE;
7579 int get_dict = FALSE;
7580 mapblock_T *mp;
7581 int buffer_local;
7582
7583 /* return empty string for failure */
7584 rettv->v_type = VAR_STRING;
7585 rettv->vval.v_string = NULL;
7586
7587 keys = get_tv_string(&argvars[0]);
7588 if (*keys == NUL)
7589 return;
7590
7591 if (argvars[1].v_type != VAR_UNKNOWN)
7592 {
7593 which = get_tv_string_buf_chk(&argvars[1], buf);
7594 if (argvars[2].v_type != VAR_UNKNOWN)
7595 {
7596 abbr = (int)get_tv_number(&argvars[2]);
7597 if (argvars[3].v_type != VAR_UNKNOWN)
7598 get_dict = (int)get_tv_number(&argvars[3]);
7599 }
7600 }
7601 else
7602 which = (char_u *)"";
7603 if (which == NULL)
7604 return;
7605
7606 mode = get_map_mode(&which, 0);
7607
7608 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7609 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7610 vim_free(keys_buf);
7611
7612 if (!get_dict)
7613 {
7614 /* Return a string. */
7615 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007616 {
7617 if (*rhs == NUL)
7618 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7619 else
7620 rettv->vval.v_string = str2special_save(rhs, FALSE);
7621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007622
7623 }
7624 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7625 {
7626 /* Return a dictionary. */
7627 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7628 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7629 dict_T *dict = rettv->vval.v_dict;
7630
Bram Moolenaare0be1672018-07-08 16:50:37 +02007631 dict_add_string(dict, "lhs", lhs);
7632 dict_add_string(dict, "rhs", mp->m_orig_str);
7633 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7634 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7635 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007636 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7637 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007638 dict_add_number(dict, "buffer", (long)buffer_local);
7639 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7640 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641
7642 vim_free(lhs);
7643 vim_free(mapmode);
7644 }
7645}
7646
7647#ifdef FEAT_FLOAT
7648/*
7649 * "log()" function
7650 */
7651 static void
7652f_log(typval_T *argvars, typval_T *rettv)
7653{
7654 float_T f = 0.0;
7655
7656 rettv->v_type = VAR_FLOAT;
7657 if (get_float_arg(argvars, &f) == OK)
7658 rettv->vval.v_float = log(f);
7659 else
7660 rettv->vval.v_float = 0.0;
7661}
7662
7663/*
7664 * "log10()" function
7665 */
7666 static void
7667f_log10(typval_T *argvars, typval_T *rettv)
7668{
7669 float_T f = 0.0;
7670
7671 rettv->v_type = VAR_FLOAT;
7672 if (get_float_arg(argvars, &f) == OK)
7673 rettv->vval.v_float = log10(f);
7674 else
7675 rettv->vval.v_float = 0.0;
7676}
7677#endif
7678
7679#ifdef FEAT_LUA
7680/*
7681 * "luaeval()" function
7682 */
7683 static void
7684f_luaeval(typval_T *argvars, typval_T *rettv)
7685{
7686 char_u *str;
7687 char_u buf[NUMBUFLEN];
7688
7689 str = get_tv_string_buf(&argvars[0], buf);
7690 do_luaeval(str, argvars + 1, rettv);
7691}
7692#endif
7693
7694/*
7695 * "map()" function
7696 */
7697 static void
7698f_map(typval_T *argvars, typval_T *rettv)
7699{
7700 filter_map(argvars, rettv, TRUE);
7701}
7702
7703/*
7704 * "maparg()" function
7705 */
7706 static void
7707f_maparg(typval_T *argvars, typval_T *rettv)
7708{
7709 get_maparg(argvars, rettv, TRUE);
7710}
7711
7712/*
7713 * "mapcheck()" function
7714 */
7715 static void
7716f_mapcheck(typval_T *argvars, typval_T *rettv)
7717{
7718 get_maparg(argvars, rettv, FALSE);
7719}
7720
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007721typedef enum
7722{
7723 MATCH_END, /* matchend() */
7724 MATCH_MATCH, /* match() */
7725 MATCH_STR, /* matchstr() */
7726 MATCH_LIST, /* matchlist() */
7727 MATCH_POS /* matchstrpos() */
7728} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729
7730 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007731find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007732{
7733 char_u *str = NULL;
7734 long len = 0;
7735 char_u *expr = NULL;
7736 char_u *pat;
7737 regmatch_T regmatch;
7738 char_u patbuf[NUMBUFLEN];
7739 char_u strbuf[NUMBUFLEN];
7740 char_u *save_cpo;
7741 long start = 0;
7742 long nth = 1;
7743 colnr_T startcol = 0;
7744 int match = 0;
7745 list_T *l = NULL;
7746 listitem_T *li = NULL;
7747 long idx = 0;
7748 char_u *tofree = NULL;
7749
7750 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7751 save_cpo = p_cpo;
7752 p_cpo = (char_u *)"";
7753
7754 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007755 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007757 /* type MATCH_LIST: return empty list when there are no matches.
7758 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 if (rettv_list_alloc(rettv) == FAIL)
7760 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007761 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007762 && (list_append_string(rettv->vval.v_list,
7763 (char_u *)"", 0) == FAIL
7764 || list_append_number(rettv->vval.v_list,
7765 (varnumber_T)-1) == FAIL
7766 || list_append_number(rettv->vval.v_list,
7767 (varnumber_T)-1) == FAIL
7768 || list_append_number(rettv->vval.v_list,
7769 (varnumber_T)-1) == FAIL))
7770 {
7771 list_free(rettv->vval.v_list);
7772 rettv->vval.v_list = NULL;
7773 goto theend;
7774 }
7775 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007776 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007777 {
7778 rettv->v_type = VAR_STRING;
7779 rettv->vval.v_string = NULL;
7780 }
7781
7782 if (argvars[0].v_type == VAR_LIST)
7783 {
7784 if ((l = argvars[0].vval.v_list) == NULL)
7785 goto theend;
7786 li = l->lv_first;
7787 }
7788 else
7789 {
7790 expr = str = get_tv_string(&argvars[0]);
7791 len = (long)STRLEN(str);
7792 }
7793
7794 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7795 if (pat == NULL)
7796 goto theend;
7797
7798 if (argvars[2].v_type != VAR_UNKNOWN)
7799 {
7800 int error = FALSE;
7801
7802 start = (long)get_tv_number_chk(&argvars[2], &error);
7803 if (error)
7804 goto theend;
7805 if (l != NULL)
7806 {
7807 li = list_find(l, start);
7808 if (li == NULL)
7809 goto theend;
7810 idx = l->lv_idx; /* use the cached index */
7811 }
7812 else
7813 {
7814 if (start < 0)
7815 start = 0;
7816 if (start > len)
7817 goto theend;
7818 /* When "count" argument is there ignore matches before "start",
7819 * otherwise skip part of the string. Differs when pattern is "^"
7820 * or "\<". */
7821 if (argvars[3].v_type != VAR_UNKNOWN)
7822 startcol = start;
7823 else
7824 {
7825 str += start;
7826 len -= start;
7827 }
7828 }
7829
7830 if (argvars[3].v_type != VAR_UNKNOWN)
7831 nth = (long)get_tv_number_chk(&argvars[3], &error);
7832 if (error)
7833 goto theend;
7834 }
7835
7836 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7837 if (regmatch.regprog != NULL)
7838 {
7839 regmatch.rm_ic = p_ic;
7840
7841 for (;;)
7842 {
7843 if (l != NULL)
7844 {
7845 if (li == NULL)
7846 {
7847 match = FALSE;
7848 break;
7849 }
7850 vim_free(tofree);
7851 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7852 if (str == NULL)
7853 break;
7854 }
7855
7856 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7857
7858 if (match && --nth <= 0)
7859 break;
7860 if (l == NULL && !match)
7861 break;
7862
7863 /* Advance to just after the match. */
7864 if (l != NULL)
7865 {
7866 li = li->li_next;
7867 ++idx;
7868 }
7869 else
7870 {
7871#ifdef FEAT_MBYTE
7872 startcol = (colnr_T)(regmatch.startp[0]
7873 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7874#else
7875 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7876#endif
7877 if (startcol > (colnr_T)len
7878 || str + startcol <= regmatch.startp[0])
7879 {
7880 match = FALSE;
7881 break;
7882 }
7883 }
7884 }
7885
7886 if (match)
7887 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007888 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007889 {
7890 listitem_T *li1 = rettv->vval.v_list->lv_first;
7891 listitem_T *li2 = li1->li_next;
7892 listitem_T *li3 = li2->li_next;
7893 listitem_T *li4 = li3->li_next;
7894
7895 vim_free(li1->li_tv.vval.v_string);
7896 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7897 (int)(regmatch.endp[0] - regmatch.startp[0]));
7898 li3->li_tv.vval.v_number =
7899 (varnumber_T)(regmatch.startp[0] - expr);
7900 li4->li_tv.vval.v_number =
7901 (varnumber_T)(regmatch.endp[0] - expr);
7902 if (l != NULL)
7903 li2->li_tv.vval.v_number = (varnumber_T)idx;
7904 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007905 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 {
7907 int i;
7908
7909 /* return list with matched string and submatches */
7910 for (i = 0; i < NSUBEXP; ++i)
7911 {
7912 if (regmatch.endp[i] == NULL)
7913 {
7914 if (list_append_string(rettv->vval.v_list,
7915 (char_u *)"", 0) == FAIL)
7916 break;
7917 }
7918 else if (list_append_string(rettv->vval.v_list,
7919 regmatch.startp[i],
7920 (int)(regmatch.endp[i] - regmatch.startp[i]))
7921 == FAIL)
7922 break;
7923 }
7924 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007925 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 {
7927 /* return matched string */
7928 if (l != NULL)
7929 copy_tv(&li->li_tv, rettv);
7930 else
7931 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7932 (int)(regmatch.endp[0] - regmatch.startp[0]));
7933 }
7934 else if (l != NULL)
7935 rettv->vval.v_number = idx;
7936 else
7937 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007938 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007939 rettv->vval.v_number =
7940 (varnumber_T)(regmatch.startp[0] - str);
7941 else
7942 rettv->vval.v_number =
7943 (varnumber_T)(regmatch.endp[0] - str);
7944 rettv->vval.v_number += (varnumber_T)(str - expr);
7945 }
7946 }
7947 vim_regfree(regmatch.regprog);
7948 }
7949
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007950theend:
7951 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 /* matchstrpos() without a list: drop the second item. */
7953 listitem_remove(rettv->vval.v_list,
7954 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007955 vim_free(tofree);
7956 p_cpo = save_cpo;
7957}
7958
7959/*
7960 * "match()" function
7961 */
7962 static void
7963f_match(typval_T *argvars, typval_T *rettv)
7964{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007965 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007966}
7967
Bram Moolenaar95e51472018-07-28 16:55:56 +02007968#ifdef FEAT_SEARCH_EXTRA
7969 static int
7970matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7971{
7972 dictitem_T *di;
7973
7974 if (tv->v_type != VAR_DICT)
7975 {
7976 EMSG(_(e_dictreq));
7977 return FAIL;
7978 }
7979
7980 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
7981 *conceal_char = get_dict_string(tv->vval.v_dict,
7982 (char_u *)"conceal", FALSE);
7983
7984 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
7985 {
7986 *win = find_win_by_nr(&di->di_tv, NULL);
7987 if (*win == NULL)
7988 {
7989 EMSG(_("E957: Invalid window number"));
7990 return FAIL;
7991 }
7992 }
7993
7994 return OK;
7995}
7996#endif
7997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998/*
7999 * "matchadd()" function
8000 */
8001 static void
8002f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8003{
8004#ifdef FEAT_SEARCH_EXTRA
8005 char_u buf[NUMBUFLEN];
8006 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8007 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8008 int prio = 10; /* default priority */
8009 int id = -1;
8010 int error = FALSE;
8011 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008012 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008013
8014 rettv->vval.v_number = -1;
8015
8016 if (grp == NULL || pat == NULL)
8017 return;
8018 if (argvars[2].v_type != VAR_UNKNOWN)
8019 {
8020 prio = (int)get_tv_number_chk(&argvars[2], &error);
8021 if (argvars[3].v_type != VAR_UNKNOWN)
8022 {
8023 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008024 if (argvars[4].v_type != VAR_UNKNOWN
8025 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8026 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008027 }
8028 }
8029 if (error == TRUE)
8030 return;
8031 if (id >= 1 && id <= 3)
8032 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008033 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008034 return;
8035 }
8036
Bram Moolenaar95e51472018-07-28 16:55:56 +02008037 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008038 conceal_char);
8039#endif
8040}
8041
8042/*
8043 * "matchaddpos()" function
8044 */
8045 static void
8046f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8047{
8048#ifdef FEAT_SEARCH_EXTRA
8049 char_u buf[NUMBUFLEN];
8050 char_u *group;
8051 int prio = 10;
8052 int id = -1;
8053 int error = FALSE;
8054 list_T *l;
8055 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008056 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057
8058 rettv->vval.v_number = -1;
8059
8060 group = get_tv_string_buf_chk(&argvars[0], buf);
8061 if (group == NULL)
8062 return;
8063
8064 if (argvars[1].v_type != VAR_LIST)
8065 {
8066 EMSG2(_(e_listarg), "matchaddpos()");
8067 return;
8068 }
8069 l = argvars[1].vval.v_list;
8070 if (l == NULL)
8071 return;
8072
8073 if (argvars[2].v_type != VAR_UNKNOWN)
8074 {
8075 prio = (int)get_tv_number_chk(&argvars[2], &error);
8076 if (argvars[3].v_type != VAR_UNKNOWN)
8077 {
8078 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008079
8080 if (argvars[4].v_type != VAR_UNKNOWN
8081 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8082 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008083 }
8084 }
8085 if (error == TRUE)
8086 return;
8087
8088 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8089 if (id == 1 || id == 2)
8090 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008091 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008092 return;
8093 }
8094
Bram Moolenaar95e51472018-07-28 16:55:56 +02008095 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 conceal_char);
8097#endif
8098}
8099
8100/*
8101 * "matcharg()" function
8102 */
8103 static void
8104f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8105{
8106 if (rettv_list_alloc(rettv) == OK)
8107 {
8108#ifdef FEAT_SEARCH_EXTRA
8109 int id = (int)get_tv_number(&argvars[0]);
8110 matchitem_T *m;
8111
8112 if (id >= 1 && id <= 3)
8113 {
8114 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8115 {
8116 list_append_string(rettv->vval.v_list,
8117 syn_id2name(m->hlg_id), -1);
8118 list_append_string(rettv->vval.v_list, m->pattern, -1);
8119 }
8120 else
8121 {
8122 list_append_string(rettv->vval.v_list, NULL, -1);
8123 list_append_string(rettv->vval.v_list, NULL, -1);
8124 }
8125 }
8126#endif
8127 }
8128}
8129
8130/*
8131 * "matchdelete()" function
8132 */
8133 static void
8134f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8135{
8136#ifdef FEAT_SEARCH_EXTRA
8137 rettv->vval.v_number = match_delete(curwin,
8138 (int)get_tv_number(&argvars[0]), TRUE);
8139#endif
8140}
8141
8142/*
8143 * "matchend()" function
8144 */
8145 static void
8146f_matchend(typval_T *argvars, typval_T *rettv)
8147{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008148 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149}
8150
8151/*
8152 * "matchlist()" function
8153 */
8154 static void
8155f_matchlist(typval_T *argvars, typval_T *rettv)
8156{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008157 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158}
8159
8160/*
8161 * "matchstr()" function
8162 */
8163 static void
8164f_matchstr(typval_T *argvars, typval_T *rettv)
8165{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008166 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167}
8168
8169/*
8170 * "matchstrpos()" function
8171 */
8172 static void
8173f_matchstrpos(typval_T *argvars, typval_T *rettv)
8174{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008175 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176}
8177
8178static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8179
8180 static void
8181max_min(typval_T *argvars, typval_T *rettv, int domax)
8182{
8183 varnumber_T n = 0;
8184 varnumber_T i;
8185 int error = FALSE;
8186
8187 if (argvars[0].v_type == VAR_LIST)
8188 {
8189 list_T *l;
8190 listitem_T *li;
8191
8192 l = argvars[0].vval.v_list;
8193 if (l != NULL)
8194 {
8195 li = l->lv_first;
8196 if (li != NULL)
8197 {
8198 n = get_tv_number_chk(&li->li_tv, &error);
8199 for (;;)
8200 {
8201 li = li->li_next;
8202 if (li == NULL)
8203 break;
8204 i = get_tv_number_chk(&li->li_tv, &error);
8205 if (domax ? i > n : i < n)
8206 n = i;
8207 }
8208 }
8209 }
8210 }
8211 else if (argvars[0].v_type == VAR_DICT)
8212 {
8213 dict_T *d;
8214 int first = TRUE;
8215 hashitem_T *hi;
8216 int todo;
8217
8218 d = argvars[0].vval.v_dict;
8219 if (d != NULL)
8220 {
8221 todo = (int)d->dv_hashtab.ht_used;
8222 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8223 {
8224 if (!HASHITEM_EMPTY(hi))
8225 {
8226 --todo;
8227 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8228 if (first)
8229 {
8230 n = i;
8231 first = FALSE;
8232 }
8233 else if (domax ? i > n : i < n)
8234 n = i;
8235 }
8236 }
8237 }
8238 }
8239 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008240 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008241 rettv->vval.v_number = error ? 0 : n;
8242}
8243
8244/*
8245 * "max()" function
8246 */
8247 static void
8248f_max(typval_T *argvars, typval_T *rettv)
8249{
8250 max_min(argvars, rettv, TRUE);
8251}
8252
8253/*
8254 * "min()" function
8255 */
8256 static void
8257f_min(typval_T *argvars, typval_T *rettv)
8258{
8259 max_min(argvars, rettv, FALSE);
8260}
8261
8262static int mkdir_recurse(char_u *dir, int prot);
8263
8264/*
8265 * Create the directory in which "dir" is located, and higher levels when
8266 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008267 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 */
8269 static int
8270mkdir_recurse(char_u *dir, int prot)
8271{
8272 char_u *p;
8273 char_u *updir;
8274 int r = FAIL;
8275
8276 /* Get end of directory name in "dir".
8277 * We're done when it's "/" or "c:/". */
8278 p = gettail_sep(dir);
8279 if (p <= get_past_head(dir))
8280 return OK;
8281
8282 /* If the directory exists we're done. Otherwise: create it.*/
8283 updir = vim_strnsave(dir, (int)(p - dir));
8284 if (updir == NULL)
8285 return FAIL;
8286 if (mch_isdir(updir))
8287 r = OK;
8288 else if (mkdir_recurse(updir, prot) == OK)
8289 r = vim_mkdir_emsg(updir, prot);
8290 vim_free(updir);
8291 return r;
8292}
8293
8294#ifdef vim_mkdir
8295/*
8296 * "mkdir()" function
8297 */
8298 static void
8299f_mkdir(typval_T *argvars, typval_T *rettv)
8300{
8301 char_u *dir;
8302 char_u buf[NUMBUFLEN];
8303 int prot = 0755;
8304
8305 rettv->vval.v_number = FAIL;
8306 if (check_restricted() || check_secure())
8307 return;
8308
8309 dir = get_tv_string_buf(&argvars[0], buf);
8310 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008311 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008313 if (*gettail(dir) == NUL)
8314 /* remove trailing slashes */
8315 *gettail_sep(dir) = NUL;
8316
8317 if (argvars[1].v_type != VAR_UNKNOWN)
8318 {
8319 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008321 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8322 if (prot == -1)
8323 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008325 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8326 {
8327 if (mch_isdir(dir))
8328 {
8329 /* With the "p" flag it's OK if the dir already exists. */
8330 rettv->vval.v_number = OK;
8331 return;
8332 }
8333 mkdir_recurse(dir, prot);
8334 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008336 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337}
8338#endif
8339
8340/*
8341 * "mode()" function
8342 */
8343 static void
8344f_mode(typval_T *argvars, typval_T *rettv)
8345{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008346 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347
Bram Moolenaar612cc382018-07-29 15:34:26 +02008348 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349
8350 if (time_for_testing == 93784)
8351 {
8352 /* Testing the two-character code. */
8353 buf[0] = 'x';
8354 buf[1] = '!';
8355 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008356#ifdef FEAT_TERMINAL
8357 else if (term_use_loop())
8358 buf[0] = 't';
8359#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008360 else if (VIsual_active)
8361 {
8362 if (VIsual_select)
8363 buf[0] = VIsual_mode + 's' - 'v';
8364 else
8365 buf[0] = VIsual_mode;
8366 }
8367 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8368 || State == CONFIRM)
8369 {
8370 buf[0] = 'r';
8371 if (State == ASKMORE)
8372 buf[1] = 'm';
8373 else if (State == CONFIRM)
8374 buf[1] = '?';
8375 }
8376 else if (State == EXTERNCMD)
8377 buf[0] = '!';
8378 else if (State & INSERT)
8379 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 if (State & VREPLACE_FLAG)
8381 {
8382 buf[0] = 'R';
8383 buf[1] = 'v';
8384 }
8385 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008386 {
8387 if (State & REPLACE_FLAG)
8388 buf[0] = 'R';
8389 else
8390 buf[0] = 'i';
8391#ifdef FEAT_INS_EXPAND
8392 if (ins_compl_active())
8393 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008394 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008395 buf[1] = 'x';
8396#endif
8397 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008399 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 {
8401 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008402 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008404 else if (exmode_active == EXMODE_NORMAL)
8405 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 }
8407 else
8408 {
8409 buf[0] = 'n';
8410 if (finish_op)
8411 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008412 else if (restart_edit == 'I' || restart_edit == 'R'
8413 || restart_edit == 'V')
8414 {
8415 buf[1] = 'i';
8416 buf[2] = restart_edit;
8417 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 }
8419
8420 /* Clear out the minor mode when the argument is not a non-zero number or
8421 * non-empty string. */
8422 if (!non_zero_arg(&argvars[0]))
8423 buf[1] = NUL;
8424
8425 rettv->vval.v_string = vim_strsave(buf);
8426 rettv->v_type = VAR_STRING;
8427}
8428
8429#if defined(FEAT_MZSCHEME) || defined(PROTO)
8430/*
8431 * "mzeval()" function
8432 */
8433 static void
8434f_mzeval(typval_T *argvars, typval_T *rettv)
8435{
8436 char_u *str;
8437 char_u buf[NUMBUFLEN];
8438
8439 str = get_tv_string_buf(&argvars[0], buf);
8440 do_mzeval(str, rettv);
8441}
8442
8443 void
8444mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8445{
8446 typval_T argvars[3];
8447
8448 argvars[0].v_type = VAR_STRING;
8449 argvars[0].vval.v_string = name;
8450 copy_tv(args, &argvars[1]);
8451 argvars[2].v_type = VAR_UNKNOWN;
8452 f_call(argvars, rettv);
8453 clear_tv(&argvars[1]);
8454}
8455#endif
8456
8457/*
8458 * "nextnonblank()" function
8459 */
8460 static void
8461f_nextnonblank(typval_T *argvars, typval_T *rettv)
8462{
8463 linenr_T lnum;
8464
8465 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8466 {
8467 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8468 {
8469 lnum = 0;
8470 break;
8471 }
8472 if (*skipwhite(ml_get(lnum)) != NUL)
8473 break;
8474 }
8475 rettv->vval.v_number = lnum;
8476}
8477
8478/*
8479 * "nr2char()" function
8480 */
8481 static void
8482f_nr2char(typval_T *argvars, typval_T *rettv)
8483{
8484 char_u buf[NUMBUFLEN];
8485
8486#ifdef FEAT_MBYTE
8487 if (has_mbyte)
8488 {
8489 int utf8 = 0;
8490
8491 if (argvars[1].v_type != VAR_UNKNOWN)
8492 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8493 if (utf8)
8494 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8495 else
8496 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8497 }
8498 else
8499#endif
8500 {
8501 buf[0] = (char_u)get_tv_number(&argvars[0]);
8502 buf[1] = NUL;
8503 }
8504 rettv->v_type = VAR_STRING;
8505 rettv->vval.v_string = vim_strsave(buf);
8506}
8507
8508/*
8509 * "or(expr, expr)" function
8510 */
8511 static void
8512f_or(typval_T *argvars, typval_T *rettv)
8513{
8514 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8515 | get_tv_number_chk(&argvars[1], NULL);
8516}
8517
8518/*
8519 * "pathshorten()" function
8520 */
8521 static void
8522f_pathshorten(typval_T *argvars, typval_T *rettv)
8523{
8524 char_u *p;
8525
8526 rettv->v_type = VAR_STRING;
8527 p = get_tv_string_chk(&argvars[0]);
8528 if (p == NULL)
8529 rettv->vval.v_string = NULL;
8530 else
8531 {
8532 p = vim_strsave(p);
8533 rettv->vval.v_string = p;
8534 if (p != NULL)
8535 shorten_dir(p);
8536 }
8537}
8538
8539#ifdef FEAT_PERL
8540/*
8541 * "perleval()" function
8542 */
8543 static void
8544f_perleval(typval_T *argvars, typval_T *rettv)
8545{
8546 char_u *str;
8547 char_u buf[NUMBUFLEN];
8548
8549 str = get_tv_string_buf(&argvars[0], buf);
8550 do_perleval(str, rettv);
8551}
8552#endif
8553
8554#ifdef FEAT_FLOAT
8555/*
8556 * "pow()" function
8557 */
8558 static void
8559f_pow(typval_T *argvars, typval_T *rettv)
8560{
8561 float_T fx = 0.0, fy = 0.0;
8562
8563 rettv->v_type = VAR_FLOAT;
8564 if (get_float_arg(argvars, &fx) == OK
8565 && get_float_arg(&argvars[1], &fy) == OK)
8566 rettv->vval.v_float = pow(fx, fy);
8567 else
8568 rettv->vval.v_float = 0.0;
8569}
8570#endif
8571
8572/*
8573 * "prevnonblank()" function
8574 */
8575 static void
8576f_prevnonblank(typval_T *argvars, typval_T *rettv)
8577{
8578 linenr_T lnum;
8579
8580 lnum = get_tv_lnum(argvars);
8581 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8582 lnum = 0;
8583 else
8584 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8585 --lnum;
8586 rettv->vval.v_number = lnum;
8587}
8588
8589/* This dummy va_list is here because:
8590 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8591 * - locally in the function results in a "used before set" warning
8592 * - using va_start() to initialize it gives "function with fixed args" error */
8593static va_list ap;
8594
8595/*
8596 * "printf()" function
8597 */
8598 static void
8599f_printf(typval_T *argvars, typval_T *rettv)
8600{
8601 char_u buf[NUMBUFLEN];
8602 int len;
8603 char_u *s;
8604 int saved_did_emsg = did_emsg;
8605 char *fmt;
8606
8607 rettv->v_type = VAR_STRING;
8608 rettv->vval.v_string = NULL;
8609
8610 /* Get the required length, allocate the buffer and do it for real. */
8611 did_emsg = FALSE;
8612 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008613 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614 if (!did_emsg)
8615 {
8616 s = alloc(len + 1);
8617 if (s != NULL)
8618 {
8619 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008620 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8621 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 }
8623 }
8624 did_emsg |= saved_did_emsg;
8625}
8626
Bram Moolenaarf2732452018-06-03 14:47:35 +02008627#ifdef FEAT_JOB_CHANNEL
8628/*
8629 * "prompt_setcallback({buffer}, {callback})" function
8630 */
8631 static void
8632f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8633{
8634 buf_T *buf;
8635 char_u *callback;
8636 partial_T *partial;
8637
8638 if (check_secure())
8639 return;
8640 buf = get_buf_tv(&argvars[0], FALSE);
8641 if (buf == NULL)
8642 return;
8643
8644 callback = get_callback(&argvars[1], &partial);
8645 if (callback == NULL)
8646 return;
8647
8648 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8649 if (partial == NULL)
8650 buf->b_prompt_callback = vim_strsave(callback);
8651 else
8652 /* pointer into the partial */
8653 buf->b_prompt_callback = callback;
8654 buf->b_prompt_partial = partial;
8655}
8656
8657/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008658 * "prompt_setinterrupt({buffer}, {callback})" function
8659 */
8660 static void
8661f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8662{
8663 buf_T *buf;
8664 char_u *callback;
8665 partial_T *partial;
8666
8667 if (check_secure())
8668 return;
8669 buf = get_buf_tv(&argvars[0], FALSE);
8670 if (buf == NULL)
8671 return;
8672
8673 callback = get_callback(&argvars[1], &partial);
8674 if (callback == NULL)
8675 return;
8676
8677 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8678 if (partial == NULL)
8679 buf->b_prompt_interrupt = vim_strsave(callback);
8680 else
8681 /* pointer into the partial */
8682 buf->b_prompt_interrupt = callback;
8683 buf->b_prompt_int_partial = partial;
8684}
8685
8686/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008687 * "prompt_setprompt({buffer}, {text})" function
8688 */
8689 static void
8690f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8691{
8692 buf_T *buf;
8693 char_u *text;
8694
8695 if (check_secure())
8696 return;
8697 buf = get_buf_tv(&argvars[0], FALSE);
8698 if (buf == NULL)
8699 return;
8700
8701 text = get_tv_string(&argvars[1]);
8702 vim_free(buf->b_prompt_text);
8703 buf->b_prompt_text = vim_strsave(text);
8704}
8705#endif
8706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707/*
8708 * "pumvisible()" function
8709 */
8710 static void
8711f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8712{
8713#ifdef FEAT_INS_EXPAND
8714 if (pum_visible())
8715 rettv->vval.v_number = 1;
8716#endif
8717}
8718
8719#ifdef FEAT_PYTHON3
8720/*
8721 * "py3eval()" function
8722 */
8723 static void
8724f_py3eval(typval_T *argvars, typval_T *rettv)
8725{
8726 char_u *str;
8727 char_u buf[NUMBUFLEN];
8728
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008729 if (p_pyx == 0)
8730 p_pyx = 3;
8731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008732 str = get_tv_string_buf(&argvars[0], buf);
8733 do_py3eval(str, rettv);
8734}
8735#endif
8736
8737#ifdef FEAT_PYTHON
8738/*
8739 * "pyeval()" function
8740 */
8741 static void
8742f_pyeval(typval_T *argvars, typval_T *rettv)
8743{
8744 char_u *str;
8745 char_u buf[NUMBUFLEN];
8746
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008747 if (p_pyx == 0)
8748 p_pyx = 2;
8749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 str = get_tv_string_buf(&argvars[0], buf);
8751 do_pyeval(str, rettv);
8752}
8753#endif
8754
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008755#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8756/*
8757 * "pyxeval()" function
8758 */
8759 static void
8760f_pyxeval(typval_T *argvars, typval_T *rettv)
8761{
8762# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8763 init_pyxversion();
8764 if (p_pyx == 2)
8765 f_pyeval(argvars, rettv);
8766 else
8767 f_py3eval(argvars, rettv);
8768# elif defined(FEAT_PYTHON)
8769 f_pyeval(argvars, rettv);
8770# elif defined(FEAT_PYTHON3)
8771 f_py3eval(argvars, rettv);
8772# endif
8773}
8774#endif
8775
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776/*
8777 * "range()" function
8778 */
8779 static void
8780f_range(typval_T *argvars, typval_T *rettv)
8781{
8782 varnumber_T start;
8783 varnumber_T end;
8784 varnumber_T stride = 1;
8785 varnumber_T i;
8786 int error = FALSE;
8787
8788 start = get_tv_number_chk(&argvars[0], &error);
8789 if (argvars[1].v_type == VAR_UNKNOWN)
8790 {
8791 end = start - 1;
8792 start = 0;
8793 }
8794 else
8795 {
8796 end = get_tv_number_chk(&argvars[1], &error);
8797 if (argvars[2].v_type != VAR_UNKNOWN)
8798 stride = get_tv_number_chk(&argvars[2], &error);
8799 }
8800
8801 if (error)
8802 return; /* type error; errmsg already given */
8803 if (stride == 0)
8804 EMSG(_("E726: Stride is zero"));
8805 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8806 EMSG(_("E727: Start past end"));
8807 else
8808 {
8809 if (rettv_list_alloc(rettv) == OK)
8810 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8811 if (list_append_number(rettv->vval.v_list,
8812 (varnumber_T)i) == FAIL)
8813 break;
8814 }
8815}
8816
8817/*
8818 * "readfile()" function
8819 */
8820 static void
8821f_readfile(typval_T *argvars, typval_T *rettv)
8822{
8823 int binary = FALSE;
8824 int failed = FALSE;
8825 char_u *fname;
8826 FILE *fd;
8827 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8828 int io_size = sizeof(buf);
8829 int readlen; /* size of last fread() */
8830 char_u *prev = NULL; /* previously read bytes, if any */
8831 long prevlen = 0; /* length of data in prev */
8832 long prevsize = 0; /* size of prev buffer */
8833 long maxline = MAXLNUM;
8834 long cnt = 0;
8835 char_u *p; /* position in buf */
8836 char_u *start; /* start of current line */
8837
8838 if (argvars[1].v_type != VAR_UNKNOWN)
8839 {
8840 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8841 binary = TRUE;
8842 if (argvars[2].v_type != VAR_UNKNOWN)
8843 maxline = (long)get_tv_number(&argvars[2]);
8844 }
8845
8846 if (rettv_list_alloc(rettv) == FAIL)
8847 return;
8848
8849 /* Always open the file in binary mode, library functions have a mind of
8850 * their own about CR-LF conversion. */
8851 fname = get_tv_string(&argvars[0]);
8852 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8853 {
8854 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8855 return;
8856 }
8857
8858 while (cnt < maxline || maxline < 0)
8859 {
8860 readlen = (int)fread(buf, 1, io_size, fd);
8861
8862 /* This for loop processes what was read, but is also entered at end
8863 * of file so that either:
8864 * - an incomplete line gets written
8865 * - a "binary" file gets an empty line at the end if it ends in a
8866 * newline. */
8867 for (p = buf, start = buf;
8868 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8869 ++p)
8870 {
8871 if (*p == '\n' || readlen <= 0)
8872 {
8873 listitem_T *li;
8874 char_u *s = NULL;
8875 long_u len = p - start;
8876
8877 /* Finished a line. Remove CRs before NL. */
8878 if (readlen > 0 && !binary)
8879 {
8880 while (len > 0 && start[len - 1] == '\r')
8881 --len;
8882 /* removal may cross back to the "prev" string */
8883 if (len == 0)
8884 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8885 --prevlen;
8886 }
8887 if (prevlen == 0)
8888 s = vim_strnsave(start, (int)len);
8889 else
8890 {
8891 /* Change "prev" buffer to be the right size. This way
8892 * the bytes are only copied once, and very long lines are
8893 * allocated only once. */
8894 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8895 {
8896 mch_memmove(s + prevlen, start, len);
8897 s[prevlen + len] = NUL;
8898 prev = NULL; /* the list will own the string */
8899 prevlen = prevsize = 0;
8900 }
8901 }
8902 if (s == NULL)
8903 {
8904 do_outofmem_msg((long_u) prevlen + len + 1);
8905 failed = TRUE;
8906 break;
8907 }
8908
8909 if ((li = listitem_alloc()) == NULL)
8910 {
8911 vim_free(s);
8912 failed = TRUE;
8913 break;
8914 }
8915 li->li_tv.v_type = VAR_STRING;
8916 li->li_tv.v_lock = 0;
8917 li->li_tv.vval.v_string = s;
8918 list_append(rettv->vval.v_list, li);
8919
8920 start = p + 1; /* step over newline */
8921 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8922 break;
8923 }
8924 else if (*p == NUL)
8925 *p = '\n';
8926#ifdef FEAT_MBYTE
8927 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8928 * when finding the BF and check the previous two bytes. */
8929 else if (*p == 0xbf && enc_utf8 && !binary)
8930 {
8931 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8932 * + 1, these may be in the "prev" string. */
8933 char_u back1 = p >= buf + 1 ? p[-1]
8934 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8935 char_u back2 = p >= buf + 2 ? p[-2]
8936 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8937 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8938
8939 if (back2 == 0xef && back1 == 0xbb)
8940 {
8941 char_u *dest = p - 2;
8942
8943 /* Usually a BOM is at the beginning of a file, and so at
8944 * the beginning of a line; then we can just step over it.
8945 */
8946 if (start == dest)
8947 start = p + 1;
8948 else
8949 {
8950 /* have to shuffle buf to close gap */
8951 int adjust_prevlen = 0;
8952
8953 if (dest < buf)
8954 {
8955 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8956 dest = buf;
8957 }
8958 if (readlen > p - buf + 1)
8959 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8960 readlen -= 3 - adjust_prevlen;
8961 prevlen -= adjust_prevlen;
8962 p = dest - 1;
8963 }
8964 }
8965 }
8966#endif
8967 } /* for */
8968
8969 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8970 break;
8971 if (start < p)
8972 {
8973 /* There's part of a line in buf, store it in "prev". */
8974 if (p - start + prevlen >= prevsize)
8975 {
8976 /* need bigger "prev" buffer */
8977 char_u *newprev;
8978
8979 /* A common use case is ordinary text files and "prev" gets a
8980 * fragment of a line, so the first allocation is made
8981 * small, to avoid repeatedly 'allocing' large and
8982 * 'reallocing' small. */
8983 if (prevsize == 0)
8984 prevsize = (long)(p - start);
8985 else
8986 {
8987 long grow50pc = (prevsize * 3) / 2;
8988 long growmin = (long)((p - start) * 2 + prevlen);
8989 prevsize = grow50pc > growmin ? grow50pc : growmin;
8990 }
8991 newprev = prev == NULL ? alloc(prevsize)
8992 : vim_realloc(prev, prevsize);
8993 if (newprev == NULL)
8994 {
8995 do_outofmem_msg((long_u)prevsize);
8996 failed = TRUE;
8997 break;
8998 }
8999 prev = newprev;
9000 }
9001 /* Add the line part to end of "prev". */
9002 mch_memmove(prev + prevlen, start, p - start);
9003 prevlen += (long)(p - start);
9004 }
9005 } /* while */
9006
9007 /*
9008 * For a negative line count use only the lines at the end of the file,
9009 * free the rest.
9010 */
9011 if (!failed && maxline < 0)
9012 while (cnt > -maxline)
9013 {
9014 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9015 --cnt;
9016 }
9017
9018 if (failed)
9019 {
9020 list_free(rettv->vval.v_list);
9021 /* readfile doc says an empty list is returned on error */
9022 rettv->vval.v_list = list_alloc();
9023 }
9024
9025 vim_free(prev);
9026 fclose(fd);
9027}
9028
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009029 static void
9030return_register(int regname, typval_T *rettv)
9031{
9032 char_u buf[2] = {0, 0};
9033
9034 buf[0] = (char_u)regname;
9035 rettv->v_type = VAR_STRING;
9036 rettv->vval.v_string = vim_strsave(buf);
9037}
9038
9039/*
9040 * "reg_executing()" function
9041 */
9042 static void
9043f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9044{
9045 return_register(reg_executing, rettv);
9046}
9047
9048/*
9049 * "reg_recording()" function
9050 */
9051 static void
9052f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9053{
9054 return_register(reg_recording, rettv);
9055}
9056
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057#if defined(FEAT_RELTIME)
9058static int list2proftime(typval_T *arg, proftime_T *tm);
9059
9060/*
9061 * Convert a List to proftime_T.
9062 * Return FAIL when there is something wrong.
9063 */
9064 static int
9065list2proftime(typval_T *arg, proftime_T *tm)
9066{
9067 long n1, n2;
9068 int error = FALSE;
9069
9070 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9071 || arg->vval.v_list->lv_len != 2)
9072 return FAIL;
9073 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9074 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9075# ifdef WIN3264
9076 tm->HighPart = n1;
9077 tm->LowPart = n2;
9078# else
9079 tm->tv_sec = n1;
9080 tm->tv_usec = n2;
9081# endif
9082 return error ? FAIL : OK;
9083}
9084#endif /* FEAT_RELTIME */
9085
9086/*
9087 * "reltime()" function
9088 */
9089 static void
9090f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9091{
9092#ifdef FEAT_RELTIME
9093 proftime_T res;
9094 proftime_T start;
9095
9096 if (argvars[0].v_type == VAR_UNKNOWN)
9097 {
9098 /* No arguments: get current time. */
9099 profile_start(&res);
9100 }
9101 else if (argvars[1].v_type == VAR_UNKNOWN)
9102 {
9103 if (list2proftime(&argvars[0], &res) == FAIL)
9104 return;
9105 profile_end(&res);
9106 }
9107 else
9108 {
9109 /* Two arguments: compute the difference. */
9110 if (list2proftime(&argvars[0], &start) == FAIL
9111 || list2proftime(&argvars[1], &res) == FAIL)
9112 return;
9113 profile_sub(&res, &start);
9114 }
9115
9116 if (rettv_list_alloc(rettv) == OK)
9117 {
9118 long n1, n2;
9119
9120# ifdef WIN3264
9121 n1 = res.HighPart;
9122 n2 = res.LowPart;
9123# else
9124 n1 = res.tv_sec;
9125 n2 = res.tv_usec;
9126# endif
9127 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9128 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9129 }
9130#endif
9131}
9132
9133#ifdef FEAT_FLOAT
9134/*
9135 * "reltimefloat()" function
9136 */
9137 static void
9138f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9139{
9140# ifdef FEAT_RELTIME
9141 proftime_T tm;
9142# endif
9143
9144 rettv->v_type = VAR_FLOAT;
9145 rettv->vval.v_float = 0;
9146# ifdef FEAT_RELTIME
9147 if (list2proftime(&argvars[0], &tm) == OK)
9148 rettv->vval.v_float = profile_float(&tm);
9149# endif
9150}
9151#endif
9152
9153/*
9154 * "reltimestr()" function
9155 */
9156 static void
9157f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9158{
9159#ifdef FEAT_RELTIME
9160 proftime_T tm;
9161#endif
9162
9163 rettv->v_type = VAR_STRING;
9164 rettv->vval.v_string = NULL;
9165#ifdef FEAT_RELTIME
9166 if (list2proftime(&argvars[0], &tm) == OK)
9167 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9168#endif
9169}
9170
9171#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9172static void make_connection(void);
9173static int check_connection(void);
9174
9175 static void
9176make_connection(void)
9177{
9178 if (X_DISPLAY == NULL
9179# ifdef FEAT_GUI
9180 && !gui.in_use
9181# endif
9182 )
9183 {
9184 x_force_connect = TRUE;
9185 setup_term_clip();
9186 x_force_connect = FALSE;
9187 }
9188}
9189
9190 static int
9191check_connection(void)
9192{
9193 make_connection();
9194 if (X_DISPLAY == NULL)
9195 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009196 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 return FAIL;
9198 }
9199 return OK;
9200}
9201#endif
9202
9203#ifdef FEAT_CLIENTSERVER
9204 static void
9205remote_common(typval_T *argvars, typval_T *rettv, int expr)
9206{
9207 char_u *server_name;
9208 char_u *keys;
9209 char_u *r = NULL;
9210 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009211 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009212# ifdef WIN32
9213 HWND w;
9214# else
9215 Window w;
9216# endif
9217
9218 if (check_restricted() || check_secure())
9219 return;
9220
9221# ifdef FEAT_X11
9222 if (check_connection() == FAIL)
9223 return;
9224# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009225 if (argvars[2].v_type != VAR_UNKNOWN
9226 && argvars[3].v_type != VAR_UNKNOWN)
9227 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228
9229 server_name = get_tv_string_chk(&argvars[0]);
9230 if (server_name == NULL)
9231 return; /* type error; errmsg already given */
9232 keys = get_tv_string_buf(&argvars[1], buf);
9233# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009234 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009236 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9237 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238# endif
9239 {
9240 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009241 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009242 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009243 vim_free(r);
9244 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009245 else
9246 EMSG2(_("E241: Unable to send to %s"), server_name);
9247 return;
9248 }
9249
9250 rettv->vval.v_string = r;
9251
9252 if (argvars[2].v_type != VAR_UNKNOWN)
9253 {
9254 dictitem_T v;
9255 char_u str[30];
9256 char_u *idvar;
9257
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009258 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009259 if (idvar != NULL && *idvar != NUL)
9260 {
9261 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9262 v.di_tv.v_type = VAR_STRING;
9263 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009264 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009265 vim_free(v.di_tv.vval.v_string);
9266 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 }
9268}
9269#endif
9270
9271/*
9272 * "remote_expr()" function
9273 */
9274 static void
9275f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9276{
9277 rettv->v_type = VAR_STRING;
9278 rettv->vval.v_string = NULL;
9279#ifdef FEAT_CLIENTSERVER
9280 remote_common(argvars, rettv, TRUE);
9281#endif
9282}
9283
9284/*
9285 * "remote_foreground()" function
9286 */
9287 static void
9288f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9289{
9290#ifdef FEAT_CLIENTSERVER
9291# ifdef WIN32
9292 /* On Win32 it's done in this application. */
9293 {
9294 char_u *server_name = get_tv_string_chk(&argvars[0]);
9295
9296 if (server_name != NULL)
9297 serverForeground(server_name);
9298 }
9299# else
9300 /* Send a foreground() expression to the server. */
9301 argvars[1].v_type = VAR_STRING;
9302 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9303 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009304 rettv->v_type = VAR_STRING;
9305 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009306 remote_common(argvars, rettv, TRUE);
9307 vim_free(argvars[1].vval.v_string);
9308# endif
9309#endif
9310}
9311
9312 static void
9313f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9314{
9315#ifdef FEAT_CLIENTSERVER
9316 dictitem_T v;
9317 char_u *s = NULL;
9318# ifdef WIN32
9319 long_u n = 0;
9320# endif
9321 char_u *serverid;
9322
9323 if (check_restricted() || check_secure())
9324 {
9325 rettv->vval.v_number = -1;
9326 return;
9327 }
9328 serverid = get_tv_string_chk(&argvars[0]);
9329 if (serverid == NULL)
9330 {
9331 rettv->vval.v_number = -1;
9332 return; /* type error; errmsg already given */
9333 }
9334# ifdef WIN32
9335 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9336 if (n == 0)
9337 rettv->vval.v_number = -1;
9338 else
9339 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009340 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 rettv->vval.v_number = (s != NULL);
9342 }
9343# else
9344 if (check_connection() == FAIL)
9345 return;
9346
9347 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9348 serverStrToWin(serverid), &s);
9349# endif
9350
9351 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9352 {
9353 char_u *retvar;
9354
9355 v.di_tv.v_type = VAR_STRING;
9356 v.di_tv.vval.v_string = vim_strsave(s);
9357 retvar = get_tv_string_chk(&argvars[1]);
9358 if (retvar != NULL)
9359 set_var(retvar, &v.di_tv, FALSE);
9360 vim_free(v.di_tv.vval.v_string);
9361 }
9362#else
9363 rettv->vval.v_number = -1;
9364#endif
9365}
9366
9367 static void
9368f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9369{
9370 char_u *r = NULL;
9371
9372#ifdef FEAT_CLIENTSERVER
9373 char_u *serverid = get_tv_string_chk(&argvars[0]);
9374
9375 if (serverid != NULL && !check_restricted() && !check_secure())
9376 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009377 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009378# ifdef WIN32
9379 /* The server's HWND is encoded in the 'id' parameter */
9380 long_u n = 0;
9381# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009382
9383 if (argvars[1].v_type != VAR_UNKNOWN)
9384 timeout = get_tv_number(&argvars[1]);
9385
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009386# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009387 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9388 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009389 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 if (r == NULL)
9391# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009392 if (check_connection() == FAIL
9393 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9394 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395# endif
9396 EMSG(_("E277: Unable to read a server reply"));
9397 }
9398#endif
9399 rettv->v_type = VAR_STRING;
9400 rettv->vval.v_string = r;
9401}
9402
9403/*
9404 * "remote_send()" function
9405 */
9406 static void
9407f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9408{
9409 rettv->v_type = VAR_STRING;
9410 rettv->vval.v_string = NULL;
9411#ifdef FEAT_CLIENTSERVER
9412 remote_common(argvars, rettv, FALSE);
9413#endif
9414}
9415
9416/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009417 * "remote_startserver()" function
9418 */
9419 static void
9420f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9421{
9422#ifdef FEAT_CLIENTSERVER
9423 char_u *server = get_tv_string_chk(&argvars[0]);
9424
9425 if (server == NULL)
9426 return; /* type error; errmsg already given */
9427 if (serverName != NULL)
9428 EMSG(_("E941: already started a server"));
9429 else
9430 {
9431# ifdef FEAT_X11
9432 if (check_connection() == OK)
9433 serverRegisterName(X_DISPLAY, server);
9434# else
9435 serverSetName(server);
9436# endif
9437 }
9438#else
9439 EMSG(_("E942: +clientserver feature not available"));
9440#endif
9441}
9442
9443/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444 * "remove()" function
9445 */
9446 static void
9447f_remove(typval_T *argvars, typval_T *rettv)
9448{
9449 list_T *l;
9450 listitem_T *item, *item2;
9451 listitem_T *li;
9452 long idx;
9453 long end;
9454 char_u *key;
9455 dict_T *d;
9456 dictitem_T *di;
9457 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9458
9459 if (argvars[0].v_type == VAR_DICT)
9460 {
9461 if (argvars[2].v_type != VAR_UNKNOWN)
9462 EMSG2(_(e_toomanyarg), "remove()");
9463 else if ((d = argvars[0].vval.v_dict) != NULL
9464 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9465 {
9466 key = get_tv_string_chk(&argvars[1]);
9467 if (key != NULL)
9468 {
9469 di = dict_find(d, key, -1);
9470 if (di == NULL)
9471 EMSG2(_(e_dictkey), key);
9472 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9473 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9474 {
9475 *rettv = di->di_tv;
9476 init_tv(&di->di_tv);
9477 dictitem_remove(d, di);
9478 }
9479 }
9480 }
9481 }
9482 else if (argvars[0].v_type != VAR_LIST)
9483 EMSG2(_(e_listdictarg), "remove()");
9484 else if ((l = argvars[0].vval.v_list) != NULL
9485 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9486 {
9487 int error = FALSE;
9488
9489 idx = (long)get_tv_number_chk(&argvars[1], &error);
9490 if (error)
9491 ; /* type error: do nothing, errmsg already given */
9492 else if ((item = list_find(l, idx)) == NULL)
9493 EMSGN(_(e_listidx), idx);
9494 else
9495 {
9496 if (argvars[2].v_type == VAR_UNKNOWN)
9497 {
9498 /* Remove one item, return its value. */
9499 vimlist_remove(l, item, item);
9500 *rettv = item->li_tv;
9501 vim_free(item);
9502 }
9503 else
9504 {
9505 /* Remove range of items, return list with values. */
9506 end = (long)get_tv_number_chk(&argvars[2], &error);
9507 if (error)
9508 ; /* type error: do nothing */
9509 else if ((item2 = list_find(l, end)) == NULL)
9510 EMSGN(_(e_listidx), end);
9511 else
9512 {
9513 int cnt = 0;
9514
9515 for (li = item; li != NULL; li = li->li_next)
9516 {
9517 ++cnt;
9518 if (li == item2)
9519 break;
9520 }
9521 if (li == NULL) /* didn't find "item2" after "item" */
9522 EMSG(_(e_invrange));
9523 else
9524 {
9525 vimlist_remove(l, item, item2);
9526 if (rettv_list_alloc(rettv) == OK)
9527 {
9528 l = rettv->vval.v_list;
9529 l->lv_first = item;
9530 l->lv_last = item2;
9531 item->li_prev = NULL;
9532 item2->li_next = NULL;
9533 l->lv_len = cnt;
9534 }
9535 }
9536 }
9537 }
9538 }
9539 }
9540}
9541
9542/*
9543 * "rename({from}, {to})" function
9544 */
9545 static void
9546f_rename(typval_T *argvars, typval_T *rettv)
9547{
9548 char_u buf[NUMBUFLEN];
9549
9550 if (check_restricted() || check_secure())
9551 rettv->vval.v_number = -1;
9552 else
9553 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9554 get_tv_string_buf(&argvars[1], buf));
9555}
9556
9557/*
9558 * "repeat()" function
9559 */
9560 static void
9561f_repeat(typval_T *argvars, typval_T *rettv)
9562{
9563 char_u *p;
9564 int n;
9565 int slen;
9566 int len;
9567 char_u *r;
9568 int i;
9569
9570 n = (int)get_tv_number(&argvars[1]);
9571 if (argvars[0].v_type == VAR_LIST)
9572 {
9573 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9574 while (n-- > 0)
9575 if (list_extend(rettv->vval.v_list,
9576 argvars[0].vval.v_list, NULL) == FAIL)
9577 break;
9578 }
9579 else
9580 {
9581 p = get_tv_string(&argvars[0]);
9582 rettv->v_type = VAR_STRING;
9583 rettv->vval.v_string = NULL;
9584
9585 slen = (int)STRLEN(p);
9586 len = slen * n;
9587 if (len <= 0)
9588 return;
9589
9590 r = alloc(len + 1);
9591 if (r != NULL)
9592 {
9593 for (i = 0; i < n; i++)
9594 mch_memmove(r + i * slen, p, (size_t)slen);
9595 r[len] = NUL;
9596 }
9597
9598 rettv->vval.v_string = r;
9599 }
9600}
9601
9602/*
9603 * "resolve()" function
9604 */
9605 static void
9606f_resolve(typval_T *argvars, typval_T *rettv)
9607{
9608 char_u *p;
9609#ifdef HAVE_READLINK
9610 char_u *buf = NULL;
9611#endif
9612
9613 p = get_tv_string(&argvars[0]);
9614#ifdef FEAT_SHORTCUT
9615 {
9616 char_u *v = NULL;
9617
9618 v = mch_resolve_shortcut(p);
9619 if (v != NULL)
9620 rettv->vval.v_string = v;
9621 else
9622 rettv->vval.v_string = vim_strsave(p);
9623 }
9624#else
9625# ifdef HAVE_READLINK
9626 {
9627 char_u *cpy;
9628 int len;
9629 char_u *remain = NULL;
9630 char_u *q;
9631 int is_relative_to_current = FALSE;
9632 int has_trailing_pathsep = FALSE;
9633 int limit = 100;
9634
9635 p = vim_strsave(p);
9636
9637 if (p[0] == '.' && (vim_ispathsep(p[1])
9638 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9639 is_relative_to_current = TRUE;
9640
9641 len = STRLEN(p);
9642 if (len > 0 && after_pathsep(p, p + len))
9643 {
9644 has_trailing_pathsep = TRUE;
9645 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9646 }
9647
9648 q = getnextcomp(p);
9649 if (*q != NUL)
9650 {
9651 /* Separate the first path component in "p", and keep the
9652 * remainder (beginning with the path separator). */
9653 remain = vim_strsave(q - 1);
9654 q[-1] = NUL;
9655 }
9656
9657 buf = alloc(MAXPATHL + 1);
9658 if (buf == NULL)
9659 goto fail;
9660
9661 for (;;)
9662 {
9663 for (;;)
9664 {
9665 len = readlink((char *)p, (char *)buf, MAXPATHL);
9666 if (len <= 0)
9667 break;
9668 buf[len] = NUL;
9669
9670 if (limit-- == 0)
9671 {
9672 vim_free(p);
9673 vim_free(remain);
9674 EMSG(_("E655: Too many symbolic links (cycle?)"));
9675 rettv->vval.v_string = NULL;
9676 goto fail;
9677 }
9678
9679 /* Ensure that the result will have a trailing path separator
9680 * if the argument has one. */
9681 if (remain == NULL && has_trailing_pathsep)
9682 add_pathsep(buf);
9683
9684 /* Separate the first path component in the link value and
9685 * concatenate the remainders. */
9686 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9687 if (*q != NUL)
9688 {
9689 if (remain == NULL)
9690 remain = vim_strsave(q - 1);
9691 else
9692 {
9693 cpy = concat_str(q - 1, remain);
9694 if (cpy != NULL)
9695 {
9696 vim_free(remain);
9697 remain = cpy;
9698 }
9699 }
9700 q[-1] = NUL;
9701 }
9702
9703 q = gettail(p);
9704 if (q > p && *q == NUL)
9705 {
9706 /* Ignore trailing path separator. */
9707 q[-1] = NUL;
9708 q = gettail(p);
9709 }
9710 if (q > p && !mch_isFullName(buf))
9711 {
9712 /* symlink is relative to directory of argument */
9713 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9714 if (cpy != NULL)
9715 {
9716 STRCPY(cpy, p);
9717 STRCPY(gettail(cpy), buf);
9718 vim_free(p);
9719 p = cpy;
9720 }
9721 }
9722 else
9723 {
9724 vim_free(p);
9725 p = vim_strsave(buf);
9726 }
9727 }
9728
9729 if (remain == NULL)
9730 break;
9731
9732 /* Append the first path component of "remain" to "p". */
9733 q = getnextcomp(remain + 1);
9734 len = q - remain - (*q != NUL);
9735 cpy = vim_strnsave(p, STRLEN(p) + len);
9736 if (cpy != NULL)
9737 {
9738 STRNCAT(cpy, remain, len);
9739 vim_free(p);
9740 p = cpy;
9741 }
9742 /* Shorten "remain". */
9743 if (*q != NUL)
9744 STRMOVE(remain, q - 1);
9745 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009746 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009747 }
9748
9749 /* If the result is a relative path name, make it explicitly relative to
9750 * the current directory if and only if the argument had this form. */
9751 if (!vim_ispathsep(*p))
9752 {
9753 if (is_relative_to_current
9754 && *p != NUL
9755 && !(p[0] == '.'
9756 && (p[1] == NUL
9757 || vim_ispathsep(p[1])
9758 || (p[1] == '.'
9759 && (p[2] == NUL
9760 || vim_ispathsep(p[2]))))))
9761 {
9762 /* Prepend "./". */
9763 cpy = concat_str((char_u *)"./", p);
9764 if (cpy != NULL)
9765 {
9766 vim_free(p);
9767 p = cpy;
9768 }
9769 }
9770 else if (!is_relative_to_current)
9771 {
9772 /* Strip leading "./". */
9773 q = p;
9774 while (q[0] == '.' && vim_ispathsep(q[1]))
9775 q += 2;
9776 if (q > p)
9777 STRMOVE(p, p + 2);
9778 }
9779 }
9780
9781 /* Ensure that the result will have no trailing path separator
9782 * if the argument had none. But keep "/" or "//". */
9783 if (!has_trailing_pathsep)
9784 {
9785 q = p + STRLEN(p);
9786 if (after_pathsep(p, q))
9787 *gettail_sep(p) = NUL;
9788 }
9789
9790 rettv->vval.v_string = p;
9791 }
9792# else
9793 rettv->vval.v_string = vim_strsave(p);
9794# endif
9795#endif
9796
9797 simplify_filename(rettv->vval.v_string);
9798
9799#ifdef HAVE_READLINK
9800fail:
9801 vim_free(buf);
9802#endif
9803 rettv->v_type = VAR_STRING;
9804}
9805
9806/*
9807 * "reverse({list})" function
9808 */
9809 static void
9810f_reverse(typval_T *argvars, typval_T *rettv)
9811{
9812 list_T *l;
9813 listitem_T *li, *ni;
9814
9815 if (argvars[0].v_type != VAR_LIST)
9816 EMSG2(_(e_listarg), "reverse()");
9817 else if ((l = argvars[0].vval.v_list) != NULL
9818 && !tv_check_lock(l->lv_lock,
9819 (char_u *)N_("reverse() argument"), TRUE))
9820 {
9821 li = l->lv_last;
9822 l->lv_first = l->lv_last = NULL;
9823 l->lv_len = 0;
9824 while (li != NULL)
9825 {
9826 ni = li->li_prev;
9827 list_append(l, li);
9828 li = ni;
9829 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009830 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 l->lv_idx = l->lv_len - l->lv_idx - 1;
9832 }
9833}
9834
9835#define SP_NOMOVE 0x01 /* don't move cursor */
9836#define SP_REPEAT 0x02 /* repeat to find outer pair */
9837#define SP_RETCOUNT 0x04 /* return matchcount */
9838#define SP_SETPCMARK 0x08 /* set previous context mark */
9839#define SP_START 0x10 /* accept match at start position */
9840#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9841#define SP_END 0x40 /* leave cursor at end of match */
9842#define SP_COLUMN 0x80 /* start at cursor column */
9843
9844static int get_search_arg(typval_T *varp, int *flagsp);
9845
9846/*
9847 * Get flags for a search function.
9848 * Possibly sets "p_ws".
9849 * Returns BACKWARD, FORWARD or zero (for an error).
9850 */
9851 static int
9852get_search_arg(typval_T *varp, int *flagsp)
9853{
9854 int dir = FORWARD;
9855 char_u *flags;
9856 char_u nbuf[NUMBUFLEN];
9857 int mask;
9858
9859 if (varp->v_type != VAR_UNKNOWN)
9860 {
9861 flags = get_tv_string_buf_chk(varp, nbuf);
9862 if (flags == NULL)
9863 return 0; /* type error; errmsg already given */
9864 while (*flags != NUL)
9865 {
9866 switch (*flags)
9867 {
9868 case 'b': dir = BACKWARD; break;
9869 case 'w': p_ws = TRUE; break;
9870 case 'W': p_ws = FALSE; break;
9871 default: mask = 0;
9872 if (flagsp != NULL)
9873 switch (*flags)
9874 {
9875 case 'c': mask = SP_START; break;
9876 case 'e': mask = SP_END; break;
9877 case 'm': mask = SP_RETCOUNT; break;
9878 case 'n': mask = SP_NOMOVE; break;
9879 case 'p': mask = SP_SUBPAT; break;
9880 case 'r': mask = SP_REPEAT; break;
9881 case 's': mask = SP_SETPCMARK; break;
9882 case 'z': mask = SP_COLUMN; break;
9883 }
9884 if (mask == 0)
9885 {
9886 EMSG2(_(e_invarg2), flags);
9887 dir = 0;
9888 }
9889 else
9890 *flagsp |= mask;
9891 }
9892 if (dir == 0)
9893 break;
9894 ++flags;
9895 }
9896 }
9897 return dir;
9898}
9899
9900/*
9901 * Shared by search() and searchpos() functions.
9902 */
9903 static int
9904search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9905{
9906 int flags;
9907 char_u *pat;
9908 pos_T pos;
9909 pos_T save_cursor;
9910 int save_p_ws = p_ws;
9911 int dir;
9912 int retval = 0; /* default: FAIL */
9913 long lnum_stop = 0;
9914 proftime_T tm;
9915#ifdef FEAT_RELTIME
9916 long time_limit = 0;
9917#endif
9918 int options = SEARCH_KEEP;
9919 int subpatnum;
9920
9921 pat = get_tv_string(&argvars[0]);
9922 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9923 if (dir == 0)
9924 goto theend;
9925 flags = *flagsp;
9926 if (flags & SP_START)
9927 options |= SEARCH_START;
9928 if (flags & SP_END)
9929 options |= SEARCH_END;
9930 if (flags & SP_COLUMN)
9931 options |= SEARCH_COL;
9932
9933 /* Optional arguments: line number to stop searching and timeout. */
9934 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9935 {
9936 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9937 if (lnum_stop < 0)
9938 goto theend;
9939#ifdef FEAT_RELTIME
9940 if (argvars[3].v_type != VAR_UNKNOWN)
9941 {
9942 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9943 if (time_limit < 0)
9944 goto theend;
9945 }
9946#endif
9947 }
9948
9949#ifdef FEAT_RELTIME
9950 /* Set the time limit, if there is one. */
9951 profile_setlimit(time_limit, &tm);
9952#endif
9953
9954 /*
9955 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9956 * Check to make sure only those flags are set.
9957 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9958 * flags cannot be set. Check for that condition also.
9959 */
9960 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9961 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9962 {
9963 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9964 goto theend;
9965 }
9966
9967 pos = save_cursor = curwin->w_cursor;
9968 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009969 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009970 if (subpatnum != FAIL)
9971 {
9972 if (flags & SP_SUBPAT)
9973 retval = subpatnum;
9974 else
9975 retval = pos.lnum;
9976 if (flags & SP_SETPCMARK)
9977 setpcmark();
9978 curwin->w_cursor = pos;
9979 if (match_pos != NULL)
9980 {
9981 /* Store the match cursor position */
9982 match_pos->lnum = pos.lnum;
9983 match_pos->col = pos.col + 1;
9984 }
9985 /* "/$" will put the cursor after the end of the line, may need to
9986 * correct that here */
9987 check_cursor();
9988 }
9989
9990 /* If 'n' flag is used: restore cursor position. */
9991 if (flags & SP_NOMOVE)
9992 curwin->w_cursor = save_cursor;
9993 else
9994 curwin->w_set_curswant = TRUE;
9995theend:
9996 p_ws = save_p_ws;
9997
9998 return retval;
9999}
10000
10001#ifdef FEAT_FLOAT
10002
10003/*
10004 * round() is not in C90, use ceil() or floor() instead.
10005 */
10006 float_T
10007vim_round(float_T f)
10008{
10009 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10010}
10011
10012/*
10013 * "round({float})" function
10014 */
10015 static void
10016f_round(typval_T *argvars, typval_T *rettv)
10017{
10018 float_T f = 0.0;
10019
10020 rettv->v_type = VAR_FLOAT;
10021 if (get_float_arg(argvars, &f) == OK)
10022 rettv->vval.v_float = vim_round(f);
10023 else
10024 rettv->vval.v_float = 0.0;
10025}
10026#endif
10027
10028/*
10029 * "screenattr()" function
10030 */
10031 static void
10032f_screenattr(typval_T *argvars, typval_T *rettv)
10033{
10034 int row;
10035 int col;
10036 int c;
10037
10038 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10039 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10040 if (row < 0 || row >= screen_Rows
10041 || col < 0 || col >= screen_Columns)
10042 c = -1;
10043 else
10044 c = ScreenAttrs[LineOffset[row] + col];
10045 rettv->vval.v_number = c;
10046}
10047
10048/*
10049 * "screenchar()" function
10050 */
10051 static void
10052f_screenchar(typval_T *argvars, typval_T *rettv)
10053{
10054 int row;
10055 int col;
10056 int off;
10057 int c;
10058
10059 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10060 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10061 if (row < 0 || row >= screen_Rows
10062 || col < 0 || col >= screen_Columns)
10063 c = -1;
10064 else
10065 {
10066 off = LineOffset[row] + col;
10067#ifdef FEAT_MBYTE
10068 if (enc_utf8 && ScreenLinesUC[off] != 0)
10069 c = ScreenLinesUC[off];
10070 else
10071#endif
10072 c = ScreenLines[off];
10073 }
10074 rettv->vval.v_number = c;
10075}
10076
10077/*
10078 * "screencol()" function
10079 *
10080 * First column is 1 to be consistent with virtcol().
10081 */
10082 static void
10083f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10084{
10085 rettv->vval.v_number = screen_screencol() + 1;
10086}
10087
10088/*
10089 * "screenrow()" function
10090 */
10091 static void
10092f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10093{
10094 rettv->vval.v_number = screen_screenrow() + 1;
10095}
10096
10097/*
10098 * "search()" function
10099 */
10100 static void
10101f_search(typval_T *argvars, typval_T *rettv)
10102{
10103 int flags = 0;
10104
10105 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10106}
10107
10108/*
10109 * "searchdecl()" function
10110 */
10111 static void
10112f_searchdecl(typval_T *argvars, typval_T *rettv)
10113{
10114 int locally = 1;
10115 int thisblock = 0;
10116 int error = FALSE;
10117 char_u *name;
10118
10119 rettv->vval.v_number = 1; /* default: FAIL */
10120
10121 name = get_tv_string_chk(&argvars[0]);
10122 if (argvars[1].v_type != VAR_UNKNOWN)
10123 {
10124 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10125 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10126 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10127 }
10128 if (!error && name != NULL)
10129 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10130 locally, thisblock, SEARCH_KEEP) == FAIL;
10131}
10132
10133/*
10134 * Used by searchpair() and searchpairpos()
10135 */
10136 static int
10137searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10138{
10139 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010140 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010141 int save_p_ws = p_ws;
10142 int dir;
10143 int flags = 0;
10144 char_u nbuf1[NUMBUFLEN];
10145 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010146 int retval = 0; /* default: FAIL */
10147 long lnum_stop = 0;
10148 long time_limit = 0;
10149
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010150 /* Get the three pattern arguments: start, middle, end. Will result in an
10151 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 spat = get_tv_string_chk(&argvars[0]);
10153 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10154 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10155 if (spat == NULL || mpat == NULL || epat == NULL)
10156 goto theend; /* type error */
10157
10158 /* Handle the optional fourth argument: flags */
10159 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10160 if (dir == 0)
10161 goto theend;
10162
10163 /* Don't accept SP_END or SP_SUBPAT.
10164 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10165 */
10166 if ((flags & (SP_END | SP_SUBPAT)) != 0
10167 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10168 {
10169 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10170 goto theend;
10171 }
10172
10173 /* Using 'r' implies 'W', otherwise it doesn't work. */
10174 if (flags & SP_REPEAT)
10175 p_ws = FALSE;
10176
10177 /* Optional fifth argument: skip expression */
10178 if (argvars[3].v_type == VAR_UNKNOWN
10179 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010180 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010181 else
10182 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010183 skip = &argvars[4];
10184 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10185 && skip->v_type != VAR_STRING)
10186 {
10187 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010188 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010189 goto theend;
10190 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 if (argvars[5].v_type != VAR_UNKNOWN)
10192 {
10193 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10194 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010195 {
10196 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010197 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010198 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010199#ifdef FEAT_RELTIME
10200 if (argvars[6].v_type != VAR_UNKNOWN)
10201 {
10202 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10203 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010204 {
10205 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010206 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010207 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 }
10209#endif
10210 }
10211 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212
10213 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10214 match_pos, lnum_stop, time_limit);
10215
10216theend:
10217 p_ws = save_p_ws;
10218
10219 return retval;
10220}
10221
10222/*
10223 * "searchpair()" function
10224 */
10225 static void
10226f_searchpair(typval_T *argvars, typval_T *rettv)
10227{
10228 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10229}
10230
10231/*
10232 * "searchpairpos()" function
10233 */
10234 static void
10235f_searchpairpos(typval_T *argvars, typval_T *rettv)
10236{
10237 pos_T match_pos;
10238 int lnum = 0;
10239 int col = 0;
10240
10241 if (rettv_list_alloc(rettv) == FAIL)
10242 return;
10243
10244 if (searchpair_cmn(argvars, &match_pos) > 0)
10245 {
10246 lnum = match_pos.lnum;
10247 col = match_pos.col;
10248 }
10249
10250 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10251 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10252}
10253
10254/*
10255 * Search for a start/middle/end thing.
10256 * Used by searchpair(), see its documentation for the details.
10257 * Returns 0 or -1 for no match,
10258 */
10259 long
10260do_searchpair(
10261 char_u *spat, /* start pattern */
10262 char_u *mpat, /* middle pattern */
10263 char_u *epat, /* end pattern */
10264 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010265 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 int flags, /* SP_SETPCMARK and other SP_ values */
10267 pos_T *match_pos,
10268 linenr_T lnum_stop, /* stop at this line if not zero */
10269 long time_limit UNUSED) /* stop after this many msec */
10270{
10271 char_u *save_cpo;
10272 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10273 long retval = 0;
10274 pos_T pos;
10275 pos_T firstpos;
10276 pos_T foundpos;
10277 pos_T save_cursor;
10278 pos_T save_pos;
10279 int n;
10280 int r;
10281 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010282 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010283 int err;
10284 int options = SEARCH_KEEP;
10285 proftime_T tm;
10286
10287 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10288 save_cpo = p_cpo;
10289 p_cpo = empty_option;
10290
10291#ifdef FEAT_RELTIME
10292 /* Set the time limit, if there is one. */
10293 profile_setlimit(time_limit, &tm);
10294#endif
10295
10296 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10297 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010298 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10299 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 if (pat2 == NULL || pat3 == NULL)
10301 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010302 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010303 if (*mpat == NUL)
10304 STRCPY(pat3, pat2);
10305 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010306 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010307 spat, epat, mpat);
10308 if (flags & SP_START)
10309 options |= SEARCH_START;
10310
Bram Moolenaar48570482017-10-30 21:48:41 +010010311 if (skip != NULL)
10312 {
10313 /* Empty string means to not use the skip expression. */
10314 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10315 use_skip = skip->vval.v_string != NULL
10316 && *skip->vval.v_string != NUL;
10317 }
10318
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010319 save_cursor = curwin->w_cursor;
10320 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010321 CLEAR_POS(&firstpos);
10322 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010323 pat = pat3;
10324 for (;;)
10325 {
10326 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010327 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010328 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010329 /* didn't find it or found the first match again: FAIL */
10330 break;
10331
10332 if (firstpos.lnum == 0)
10333 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010334 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010335 {
10336 /* Found the same position again. Can happen with a pattern that
10337 * has "\zs" at the end and searching backwards. Advance one
10338 * character and try again. */
10339 if (dir == BACKWARD)
10340 decl(&pos);
10341 else
10342 incl(&pos);
10343 }
10344 foundpos = pos;
10345
10346 /* clear the start flag to avoid getting stuck here */
10347 options &= ~SEARCH_START;
10348
10349 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010350 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351 {
10352 save_pos = curwin->w_cursor;
10353 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010354 err = FALSE;
10355 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010356 curwin->w_cursor = save_pos;
10357 if (err)
10358 {
10359 /* Evaluating {skip} caused an error, break here. */
10360 curwin->w_cursor = save_cursor;
10361 retval = -1;
10362 break;
10363 }
10364 if (r)
10365 continue;
10366 }
10367
10368 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10369 {
10370 /* Found end when searching backwards or start when searching
10371 * forward: nested pair. */
10372 ++nest;
10373 pat = pat2; /* nested, don't search for middle */
10374 }
10375 else
10376 {
10377 /* Found end when searching forward or start when searching
10378 * backward: end of (nested) pair; or found middle in outer pair. */
10379 if (--nest == 1)
10380 pat = pat3; /* outer level, search for middle */
10381 }
10382
10383 if (nest == 0)
10384 {
10385 /* Found the match: return matchcount or line number. */
10386 if (flags & SP_RETCOUNT)
10387 ++retval;
10388 else
10389 retval = pos.lnum;
10390 if (flags & SP_SETPCMARK)
10391 setpcmark();
10392 curwin->w_cursor = pos;
10393 if (!(flags & SP_REPEAT))
10394 break;
10395 nest = 1; /* search for next unmatched */
10396 }
10397 }
10398
10399 if (match_pos != NULL)
10400 {
10401 /* Store the match cursor position */
10402 match_pos->lnum = curwin->w_cursor.lnum;
10403 match_pos->col = curwin->w_cursor.col + 1;
10404 }
10405
10406 /* If 'n' flag is used or search failed: restore cursor position. */
10407 if ((flags & SP_NOMOVE) || retval == 0)
10408 curwin->w_cursor = save_cursor;
10409
10410theend:
10411 vim_free(pat2);
10412 vim_free(pat3);
10413 if (p_cpo == empty_option)
10414 p_cpo = save_cpo;
10415 else
10416 /* Darn, evaluating the {skip} expression changed the value. */
10417 free_string_option(save_cpo);
10418
10419 return retval;
10420}
10421
10422/*
10423 * "searchpos()" function
10424 */
10425 static void
10426f_searchpos(typval_T *argvars, typval_T *rettv)
10427{
10428 pos_T match_pos;
10429 int lnum = 0;
10430 int col = 0;
10431 int n;
10432 int flags = 0;
10433
10434 if (rettv_list_alloc(rettv) == FAIL)
10435 return;
10436
10437 n = search_cmn(argvars, &match_pos, &flags);
10438 if (n > 0)
10439 {
10440 lnum = match_pos.lnum;
10441 col = match_pos.col;
10442 }
10443
10444 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10445 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10446 if (flags & SP_SUBPAT)
10447 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10448}
10449
10450 static void
10451f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10452{
10453#ifdef FEAT_CLIENTSERVER
10454 char_u buf[NUMBUFLEN];
10455 char_u *server = get_tv_string_chk(&argvars[0]);
10456 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10457
10458 rettv->vval.v_number = -1;
10459 if (server == NULL || reply == NULL)
10460 return;
10461 if (check_restricted() || check_secure())
10462 return;
10463# ifdef FEAT_X11
10464 if (check_connection() == FAIL)
10465 return;
10466# endif
10467
10468 if (serverSendReply(server, reply) < 0)
10469 {
10470 EMSG(_("E258: Unable to send to client"));
10471 return;
10472 }
10473 rettv->vval.v_number = 0;
10474#else
10475 rettv->vval.v_number = -1;
10476#endif
10477}
10478
10479 static void
10480f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10481{
10482 char_u *r = NULL;
10483
10484#ifdef FEAT_CLIENTSERVER
10485# ifdef WIN32
10486 r = serverGetVimNames();
10487# else
10488 make_connection();
10489 if (X_DISPLAY != NULL)
10490 r = serverGetVimNames(X_DISPLAY);
10491# endif
10492#endif
10493 rettv->v_type = VAR_STRING;
10494 rettv->vval.v_string = r;
10495}
10496
10497/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010498 * "setbufline()" function
10499 */
10500 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010501f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010502{
10503 linenr_T lnum;
10504 buf_T *buf;
10505
10506 buf = get_buf_tv(&argvars[0], FALSE);
10507 if (buf == NULL)
10508 rettv->vval.v_number = 1; /* FAIL */
10509 else
10510 {
10511 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010512 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010513 }
10514}
10515
10516/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517 * "setbufvar()" function
10518 */
10519 static void
10520f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10521{
10522 buf_T *buf;
10523 char_u *varname, *bufvarname;
10524 typval_T *varp;
10525 char_u nbuf[NUMBUFLEN];
10526
10527 if (check_restricted() || check_secure())
10528 return;
10529 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10530 varname = get_tv_string_chk(&argvars[1]);
10531 buf = get_buf_tv(&argvars[0], FALSE);
10532 varp = &argvars[2];
10533
10534 if (buf != NULL && varname != NULL && varp != NULL)
10535 {
10536 if (*varname == '&')
10537 {
10538 long numval;
10539 char_u *strval;
10540 int error = FALSE;
10541 aco_save_T aco;
10542
10543 /* set curbuf to be our buf, temporarily */
10544 aucmd_prepbuf(&aco, buf);
10545
10546 ++varname;
10547 numval = (long)get_tv_number_chk(varp, &error);
10548 strval = get_tv_string_buf_chk(varp, nbuf);
10549 if (!error && strval != NULL)
10550 set_option_value(varname, numval, strval, OPT_LOCAL);
10551
10552 /* reset notion of buffer */
10553 aucmd_restbuf(&aco);
10554 }
10555 else
10556 {
10557 buf_T *save_curbuf = curbuf;
10558
10559 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10560 if (bufvarname != NULL)
10561 {
10562 curbuf = buf;
10563 STRCPY(bufvarname, "b:");
10564 STRCPY(bufvarname + 2, varname);
10565 set_var(bufvarname, varp, TRUE);
10566 vim_free(bufvarname);
10567 curbuf = save_curbuf;
10568 }
10569 }
10570 }
10571}
10572
10573 static void
10574f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10575{
10576 dict_T *d;
10577 dictitem_T *di;
10578 char_u *csearch;
10579
10580 if (argvars[0].v_type != VAR_DICT)
10581 {
10582 EMSG(_(e_dictreq));
10583 return;
10584 }
10585
10586 if ((d = argvars[0].vval.v_dict) != NULL)
10587 {
10588 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10589 if (csearch != NULL)
10590 {
10591#ifdef FEAT_MBYTE
10592 if (enc_utf8)
10593 {
10594 int pcc[MAX_MCO];
10595 int c = utfc_ptr2char(csearch, pcc);
10596
10597 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10598 }
10599 else
10600#endif
10601 set_last_csearch(PTR2CHAR(csearch),
10602 csearch, MB_PTR2LEN(csearch));
10603 }
10604
10605 di = dict_find(d, (char_u *)"forward", -1);
10606 if (di != NULL)
10607 set_csearch_direction((int)get_tv_number(&di->di_tv)
10608 ? FORWARD : BACKWARD);
10609
10610 di = dict_find(d, (char_u *)"until", -1);
10611 if (di != NULL)
10612 set_csearch_until(!!get_tv_number(&di->di_tv));
10613 }
10614}
10615
10616/*
10617 * "setcmdpos()" function
10618 */
10619 static void
10620f_setcmdpos(typval_T *argvars, typval_T *rettv)
10621{
10622 int pos = (int)get_tv_number(&argvars[0]) - 1;
10623
10624 if (pos >= 0)
10625 rettv->vval.v_number = set_cmdline_pos(pos);
10626}
10627
10628/*
10629 * "setfperm({fname}, {mode})" function
10630 */
10631 static void
10632f_setfperm(typval_T *argvars, typval_T *rettv)
10633{
10634 char_u *fname;
10635 char_u modebuf[NUMBUFLEN];
10636 char_u *mode_str;
10637 int i;
10638 int mask;
10639 int mode = 0;
10640
10641 rettv->vval.v_number = 0;
10642 fname = get_tv_string_chk(&argvars[0]);
10643 if (fname == NULL)
10644 return;
10645 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10646 if (mode_str == NULL)
10647 return;
10648 if (STRLEN(mode_str) != 9)
10649 {
10650 EMSG2(_(e_invarg2), mode_str);
10651 return;
10652 }
10653
10654 mask = 1;
10655 for (i = 8; i >= 0; --i)
10656 {
10657 if (mode_str[i] != '-')
10658 mode |= mask;
10659 mask = mask << 1;
10660 }
10661 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10662}
10663
10664/*
10665 * "setline()" function
10666 */
10667 static void
10668f_setline(typval_T *argvars, typval_T *rettv)
10669{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010670 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010671
Bram Moolenaarca851592018-06-06 21:04:07 +020010672 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673}
10674
Bram Moolenaard823fa92016-08-12 16:29:27 +020010675static void set_qf_ll_list(win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *what_arg, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010676
10677/*
10678 * Used by "setqflist()" and "setloclist()" functions
10679 */
10680 static void
10681set_qf_ll_list(
10682 win_T *wp UNUSED,
10683 typval_T *list_arg UNUSED,
10684 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010685 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686 typval_T *rettv)
10687{
10688#ifdef FEAT_QUICKFIX
10689 static char *e_invact = N_("E927: Invalid action: '%s'");
10690 char_u *act;
10691 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010692 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693#endif
10694
10695 rettv->vval.v_number = -1;
10696
10697#ifdef FEAT_QUICKFIX
10698 if (list_arg->v_type != VAR_LIST)
10699 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010700 else if (recursive != 0)
10701 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010702 else
10703 {
10704 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010705 dict_T *d = NULL;
10706 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010707
10708 if (action_arg->v_type == VAR_STRING)
10709 {
10710 act = get_tv_string_chk(action_arg);
10711 if (act == NULL)
10712 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010713 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10714 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010715 action = *act;
10716 else
10717 EMSG2(_(e_invact), act);
10718 }
10719 else if (action_arg->v_type == VAR_UNKNOWN)
10720 action = ' ';
10721 else
10722 EMSG(_(e_stringreq));
10723
Bram Moolenaard823fa92016-08-12 16:29:27 +020010724 if (action_arg->v_type != VAR_UNKNOWN
10725 && what_arg->v_type != VAR_UNKNOWN)
10726 {
10727 if (what_arg->v_type == VAR_DICT)
10728 d = what_arg->vval.v_dict;
10729 else
10730 {
10731 EMSG(_(e_dictreq));
10732 valid_dict = FALSE;
10733 }
10734 }
10735
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010736 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010737 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010738 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10739 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010740 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010741 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 }
10743#endif
10744}
10745
10746/*
10747 * "setloclist()" function
10748 */
10749 static void
10750f_setloclist(typval_T *argvars, typval_T *rettv)
10751{
10752 win_T *win;
10753
10754 rettv->vval.v_number = -1;
10755
10756 win = find_win_by_nr(&argvars[0], NULL);
10757 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010758 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759}
10760
10761/*
10762 * "setmatches()" function
10763 */
10764 static void
10765f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10766{
10767#ifdef FEAT_SEARCH_EXTRA
10768 list_T *l;
10769 listitem_T *li;
10770 dict_T *d;
10771 list_T *s = NULL;
10772
10773 rettv->vval.v_number = -1;
10774 if (argvars[0].v_type != VAR_LIST)
10775 {
10776 EMSG(_(e_listreq));
10777 return;
10778 }
10779 if ((l = argvars[0].vval.v_list) != NULL)
10780 {
10781
10782 /* To some extent make sure that we are dealing with a list from
10783 * "getmatches()". */
10784 li = l->lv_first;
10785 while (li != NULL)
10786 {
10787 if (li->li_tv.v_type != VAR_DICT
10788 || (d = li->li_tv.vval.v_dict) == NULL)
10789 {
10790 EMSG(_(e_invarg));
10791 return;
10792 }
10793 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10794 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10795 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10796 && dict_find(d, (char_u *)"priority", -1) != NULL
10797 && dict_find(d, (char_u *)"id", -1) != NULL))
10798 {
10799 EMSG(_(e_invarg));
10800 return;
10801 }
10802 li = li->li_next;
10803 }
10804
10805 clear_matches(curwin);
10806 li = l->lv_first;
10807 while (li != NULL)
10808 {
10809 int i = 0;
10810 char_u buf[5];
10811 dictitem_T *di;
10812 char_u *group;
10813 int priority;
10814 int id;
10815 char_u *conceal;
10816
10817 d = li->li_tv.vval.v_dict;
10818 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10819 {
10820 if (s == NULL)
10821 {
10822 s = list_alloc();
10823 if (s == NULL)
10824 return;
10825 }
10826
10827 /* match from matchaddpos() */
10828 for (i = 1; i < 9; i++)
10829 {
10830 sprintf((char *)buf, (char *)"pos%d", i);
10831 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10832 {
10833 if (di->di_tv.v_type != VAR_LIST)
10834 return;
10835
10836 list_append_tv(s, &di->di_tv);
10837 s->lv_refcount++;
10838 }
10839 else
10840 break;
10841 }
10842 }
10843
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010844 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 priority = (int)get_dict_number(d, (char_u *)"priority");
10846 id = (int)get_dict_number(d, (char_u *)"id");
10847 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010848 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010849 : NULL;
10850 if (i == 0)
10851 {
10852 match_add(curwin, group,
10853 get_dict_string(d, (char_u *)"pattern", FALSE),
10854 priority, id, NULL, conceal);
10855 }
10856 else
10857 {
10858 match_add(curwin, group, NULL, priority, id, s, conceal);
10859 list_unref(s);
10860 s = NULL;
10861 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010862 vim_free(group);
10863 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010864
10865 li = li->li_next;
10866 }
10867 rettv->vval.v_number = 0;
10868 }
10869#endif
10870}
10871
10872/*
10873 * "setpos()" function
10874 */
10875 static void
10876f_setpos(typval_T *argvars, typval_T *rettv)
10877{
10878 pos_T pos;
10879 int fnum;
10880 char_u *name;
10881 colnr_T curswant = -1;
10882
10883 rettv->vval.v_number = -1;
10884 name = get_tv_string_chk(argvars);
10885 if (name != NULL)
10886 {
10887 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10888 {
10889 if (--pos.col < 0)
10890 pos.col = 0;
10891 if (name[0] == '.' && name[1] == NUL)
10892 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010893 /* set cursor; "fnum" is ignored */
10894 curwin->w_cursor = pos;
10895 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010897 curwin->w_curswant = curswant - 1;
10898 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010900 check_cursor();
10901 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 }
10903 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10904 {
10905 /* set mark */
10906 if (setmark_pos(name[1], &pos, fnum) == OK)
10907 rettv->vval.v_number = 0;
10908 }
10909 else
10910 EMSG(_(e_invarg));
10911 }
10912 }
10913}
10914
10915/*
10916 * "setqflist()" function
10917 */
10918 static void
10919f_setqflist(typval_T *argvars, typval_T *rettv)
10920{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010921 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010922}
10923
10924/*
10925 * "setreg()" function
10926 */
10927 static void
10928f_setreg(typval_T *argvars, typval_T *rettv)
10929{
10930 int regname;
10931 char_u *strregname;
10932 char_u *stropt;
10933 char_u *strval;
10934 int append;
10935 char_u yank_type;
10936 long block_len;
10937
10938 block_len = -1;
10939 yank_type = MAUTO;
10940 append = FALSE;
10941
10942 strregname = get_tv_string_chk(argvars);
10943 rettv->vval.v_number = 1; /* FAIL is default */
10944
10945 if (strregname == NULL)
10946 return; /* type error; errmsg already given */
10947 regname = *strregname;
10948 if (regname == 0 || regname == '@')
10949 regname = '"';
10950
10951 if (argvars[2].v_type != VAR_UNKNOWN)
10952 {
10953 stropt = get_tv_string_chk(&argvars[2]);
10954 if (stropt == NULL)
10955 return; /* type error */
10956 for (; *stropt != NUL; ++stropt)
10957 switch (*stropt)
10958 {
10959 case 'a': case 'A': /* append */
10960 append = TRUE;
10961 break;
10962 case 'v': case 'c': /* character-wise selection */
10963 yank_type = MCHAR;
10964 break;
10965 case 'V': case 'l': /* line-wise selection */
10966 yank_type = MLINE;
10967 break;
10968 case 'b': case Ctrl_V: /* block-wise selection */
10969 yank_type = MBLOCK;
10970 if (VIM_ISDIGIT(stropt[1]))
10971 {
10972 ++stropt;
10973 block_len = getdigits(&stropt) - 1;
10974 --stropt;
10975 }
10976 break;
10977 }
10978 }
10979
10980 if (argvars[1].v_type == VAR_LIST)
10981 {
10982 char_u **lstval;
10983 char_u **allocval;
10984 char_u buf[NUMBUFLEN];
10985 char_u **curval;
10986 char_u **curallocval;
10987 list_T *ll = argvars[1].vval.v_list;
10988 listitem_T *li;
10989 int len;
10990
10991 /* If the list is NULL handle like an empty list. */
10992 len = ll == NULL ? 0 : ll->lv_len;
10993
10994 /* First half: use for pointers to result lines; second half: use for
10995 * pointers to allocated copies. */
10996 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10997 if (lstval == NULL)
10998 return;
10999 curval = lstval;
11000 allocval = lstval + len + 2;
11001 curallocval = allocval;
11002
11003 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11004 li = li->li_next)
11005 {
11006 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11007 if (strval == NULL)
11008 goto free_lstval;
11009 if (strval == buf)
11010 {
11011 /* Need to make a copy, next get_tv_string_buf_chk() will
11012 * overwrite the string. */
11013 strval = vim_strsave(buf);
11014 if (strval == NULL)
11015 goto free_lstval;
11016 *curallocval++ = strval;
11017 }
11018 *curval++ = strval;
11019 }
11020 *curval++ = NULL;
11021
11022 write_reg_contents_lst(regname, lstval, -1,
11023 append, yank_type, block_len);
11024free_lstval:
11025 while (curallocval > allocval)
11026 vim_free(*--curallocval);
11027 vim_free(lstval);
11028 }
11029 else
11030 {
11031 strval = get_tv_string_chk(&argvars[1]);
11032 if (strval == NULL)
11033 return;
11034 write_reg_contents_ex(regname, strval, -1,
11035 append, yank_type, block_len);
11036 }
11037 rettv->vval.v_number = 0;
11038}
11039
11040/*
11041 * "settabvar()" function
11042 */
11043 static void
11044f_settabvar(typval_T *argvars, typval_T *rettv)
11045{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 tabpage_T *save_curtab;
11047 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 char_u *varname, *tabvarname;
11049 typval_T *varp;
11050
11051 rettv->vval.v_number = 0;
11052
11053 if (check_restricted() || check_secure())
11054 return;
11055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 varname = get_tv_string_chk(&argvars[1]);
11058 varp = &argvars[2];
11059
Bram Moolenaar4033c552017-09-16 20:54:51 +020011060 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 save_curtab = curtab;
11063 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064
11065 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11066 if (tabvarname != NULL)
11067 {
11068 STRCPY(tabvarname, "t:");
11069 STRCPY(tabvarname + 2, varname);
11070 set_var(tabvarname, varp, TRUE);
11071 vim_free(tabvarname);
11072 }
11073
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011074 /* Restore current tabpage */
11075 if (valid_tabpage(save_curtab))
11076 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011077 }
11078}
11079
11080/*
11081 * "settabwinvar()" function
11082 */
11083 static void
11084f_settabwinvar(typval_T *argvars, typval_T *rettv)
11085{
11086 setwinvar(argvars, rettv, 1);
11087}
11088
11089/*
11090 * "setwinvar()" function
11091 */
11092 static void
11093f_setwinvar(typval_T *argvars, typval_T *rettv)
11094{
11095 setwinvar(argvars, rettv, 0);
11096}
11097
11098#ifdef FEAT_CRYPT
11099/*
11100 * "sha256({string})" function
11101 */
11102 static void
11103f_sha256(typval_T *argvars, typval_T *rettv)
11104{
11105 char_u *p;
11106
11107 p = get_tv_string(&argvars[0]);
11108 rettv->vval.v_string = vim_strsave(
11109 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11110 rettv->v_type = VAR_STRING;
11111}
11112#endif /* FEAT_CRYPT */
11113
11114/*
11115 * "shellescape({string})" function
11116 */
11117 static void
11118f_shellescape(typval_T *argvars, typval_T *rettv)
11119{
Bram Moolenaar20615522017-06-05 18:46:26 +020011120 int do_special = non_zero_arg(&argvars[1]);
11121
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011122 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011123 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 rettv->v_type = VAR_STRING;
11125}
11126
11127/*
11128 * shiftwidth() function
11129 */
11130 static void
11131f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11132{
11133 rettv->vval.v_number = get_sw_value(curbuf);
11134}
11135
11136/*
11137 * "simplify()" function
11138 */
11139 static void
11140f_simplify(typval_T *argvars, typval_T *rettv)
11141{
11142 char_u *p;
11143
11144 p = get_tv_string(&argvars[0]);
11145 rettv->vval.v_string = vim_strsave(p);
11146 simplify_filename(rettv->vval.v_string); /* simplify in place */
11147 rettv->v_type = VAR_STRING;
11148}
11149
11150#ifdef FEAT_FLOAT
11151/*
11152 * "sin()" function
11153 */
11154 static void
11155f_sin(typval_T *argvars, typval_T *rettv)
11156{
11157 float_T f = 0.0;
11158
11159 rettv->v_type = VAR_FLOAT;
11160 if (get_float_arg(argvars, &f) == OK)
11161 rettv->vval.v_float = sin(f);
11162 else
11163 rettv->vval.v_float = 0.0;
11164}
11165
11166/*
11167 * "sinh()" function
11168 */
11169 static void
11170f_sinh(typval_T *argvars, typval_T *rettv)
11171{
11172 float_T f = 0.0;
11173
11174 rettv->v_type = VAR_FLOAT;
11175 if (get_float_arg(argvars, &f) == OK)
11176 rettv->vval.v_float = sinh(f);
11177 else
11178 rettv->vval.v_float = 0.0;
11179}
11180#endif
11181
11182static int
11183#ifdef __BORLANDC__
11184 _RTLENTRYF
11185#endif
11186 item_compare(const void *s1, const void *s2);
11187static int
11188#ifdef __BORLANDC__
11189 _RTLENTRYF
11190#endif
11191 item_compare2(const void *s1, const void *s2);
11192
11193/* struct used in the array that's given to qsort() */
11194typedef struct
11195{
11196 listitem_T *item;
11197 int idx;
11198} sortItem_T;
11199
11200/* struct storing information about current sort */
11201typedef struct
11202{
11203 int item_compare_ic;
11204 int item_compare_numeric;
11205 int item_compare_numbers;
11206#ifdef FEAT_FLOAT
11207 int item_compare_float;
11208#endif
11209 char_u *item_compare_func;
11210 partial_T *item_compare_partial;
11211 dict_T *item_compare_selfdict;
11212 int item_compare_func_err;
11213 int item_compare_keep_zero;
11214} sortinfo_T;
11215static sortinfo_T *sortinfo = NULL;
11216static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11217#define ITEM_COMPARE_FAIL 999
11218
11219/*
11220 * Compare functions for f_sort() and f_uniq() below.
11221 */
11222 static int
11223#ifdef __BORLANDC__
11224_RTLENTRYF
11225#endif
11226item_compare(const void *s1, const void *s2)
11227{
11228 sortItem_T *si1, *si2;
11229 typval_T *tv1, *tv2;
11230 char_u *p1, *p2;
11231 char_u *tofree1 = NULL, *tofree2 = NULL;
11232 int res;
11233 char_u numbuf1[NUMBUFLEN];
11234 char_u numbuf2[NUMBUFLEN];
11235
11236 si1 = (sortItem_T *)s1;
11237 si2 = (sortItem_T *)s2;
11238 tv1 = &si1->item->li_tv;
11239 tv2 = &si2->item->li_tv;
11240
11241 if (sortinfo->item_compare_numbers)
11242 {
11243 varnumber_T v1 = get_tv_number(tv1);
11244 varnumber_T v2 = get_tv_number(tv2);
11245
11246 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11247 }
11248
11249#ifdef FEAT_FLOAT
11250 if (sortinfo->item_compare_float)
11251 {
11252 float_T v1 = get_tv_float(tv1);
11253 float_T v2 = get_tv_float(tv2);
11254
11255 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11256 }
11257#endif
11258
11259 /* tv2string() puts quotes around a string and allocates memory. Don't do
11260 * that for string variables. Use a single quote when comparing with a
11261 * non-string to do what the docs promise. */
11262 if (tv1->v_type == VAR_STRING)
11263 {
11264 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11265 p1 = (char_u *)"'";
11266 else
11267 p1 = tv1->vval.v_string;
11268 }
11269 else
11270 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11271 if (tv2->v_type == VAR_STRING)
11272 {
11273 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11274 p2 = (char_u *)"'";
11275 else
11276 p2 = tv2->vval.v_string;
11277 }
11278 else
11279 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11280 if (p1 == NULL)
11281 p1 = (char_u *)"";
11282 if (p2 == NULL)
11283 p2 = (char_u *)"";
11284 if (!sortinfo->item_compare_numeric)
11285 {
11286 if (sortinfo->item_compare_ic)
11287 res = STRICMP(p1, p2);
11288 else
11289 res = STRCMP(p1, p2);
11290 }
11291 else
11292 {
11293 double n1, n2;
11294 n1 = strtod((char *)p1, (char **)&p1);
11295 n2 = strtod((char *)p2, (char **)&p2);
11296 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11297 }
11298
11299 /* When the result would be zero, compare the item indexes. Makes the
11300 * sort stable. */
11301 if (res == 0 && !sortinfo->item_compare_keep_zero)
11302 res = si1->idx > si2->idx ? 1 : -1;
11303
11304 vim_free(tofree1);
11305 vim_free(tofree2);
11306 return res;
11307}
11308
11309 static int
11310#ifdef __BORLANDC__
11311_RTLENTRYF
11312#endif
11313item_compare2(const void *s1, const void *s2)
11314{
11315 sortItem_T *si1, *si2;
11316 int res;
11317 typval_T rettv;
11318 typval_T argv[3];
11319 int dummy;
11320 char_u *func_name;
11321 partial_T *partial = sortinfo->item_compare_partial;
11322
11323 /* shortcut after failure in previous call; compare all items equal */
11324 if (sortinfo->item_compare_func_err)
11325 return 0;
11326
11327 si1 = (sortItem_T *)s1;
11328 si2 = (sortItem_T *)s2;
11329
11330 if (partial == NULL)
11331 func_name = sortinfo->item_compare_func;
11332 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011333 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334
11335 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11336 * in the copy without changing the original list items. */
11337 copy_tv(&si1->item->li_tv, &argv[0]);
11338 copy_tv(&si2->item->li_tv, &argv[1]);
11339
11340 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11341 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011342 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 partial, sortinfo->item_compare_selfdict);
11344 clear_tv(&argv[0]);
11345 clear_tv(&argv[1]);
11346
11347 if (res == FAIL)
11348 res = ITEM_COMPARE_FAIL;
11349 else
11350 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11351 if (sortinfo->item_compare_func_err)
11352 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11353 clear_tv(&rettv);
11354
11355 /* When the result would be zero, compare the pointers themselves. Makes
11356 * the sort stable. */
11357 if (res == 0 && !sortinfo->item_compare_keep_zero)
11358 res = si1->idx > si2->idx ? 1 : -1;
11359
11360 return res;
11361}
11362
11363/*
11364 * "sort({list})" function
11365 */
11366 static void
11367do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11368{
11369 list_T *l;
11370 listitem_T *li;
11371 sortItem_T *ptrs;
11372 sortinfo_T *old_sortinfo;
11373 sortinfo_T info;
11374 long len;
11375 long i;
11376
11377 /* Pointer to current info struct used in compare function. Save and
11378 * restore the current one for nested calls. */
11379 old_sortinfo = sortinfo;
11380 sortinfo = &info;
11381
11382 if (argvars[0].v_type != VAR_LIST)
11383 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11384 else
11385 {
11386 l = argvars[0].vval.v_list;
11387 if (l == NULL || tv_check_lock(l->lv_lock,
11388 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11389 TRUE))
11390 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011391 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392
11393 len = list_len(l);
11394 if (len <= 1)
11395 goto theend; /* short list sorts pretty quickly */
11396
11397 info.item_compare_ic = FALSE;
11398 info.item_compare_numeric = FALSE;
11399 info.item_compare_numbers = FALSE;
11400#ifdef FEAT_FLOAT
11401 info.item_compare_float = FALSE;
11402#endif
11403 info.item_compare_func = NULL;
11404 info.item_compare_partial = NULL;
11405 info.item_compare_selfdict = NULL;
11406 if (argvars[1].v_type != VAR_UNKNOWN)
11407 {
11408 /* optional second argument: {func} */
11409 if (argvars[1].v_type == VAR_FUNC)
11410 info.item_compare_func = argvars[1].vval.v_string;
11411 else if (argvars[1].v_type == VAR_PARTIAL)
11412 info.item_compare_partial = argvars[1].vval.v_partial;
11413 else
11414 {
11415 int error = FALSE;
11416
11417 i = (long)get_tv_number_chk(&argvars[1], &error);
11418 if (error)
11419 goto theend; /* type error; errmsg already given */
11420 if (i == 1)
11421 info.item_compare_ic = TRUE;
11422 else if (argvars[1].v_type != VAR_NUMBER)
11423 info.item_compare_func = get_tv_string(&argvars[1]);
11424 else if (i != 0)
11425 {
11426 EMSG(_(e_invarg));
11427 goto theend;
11428 }
11429 if (info.item_compare_func != NULL)
11430 {
11431 if (*info.item_compare_func == NUL)
11432 {
11433 /* empty string means default sort */
11434 info.item_compare_func = NULL;
11435 }
11436 else if (STRCMP(info.item_compare_func, "n") == 0)
11437 {
11438 info.item_compare_func = NULL;
11439 info.item_compare_numeric = TRUE;
11440 }
11441 else if (STRCMP(info.item_compare_func, "N") == 0)
11442 {
11443 info.item_compare_func = NULL;
11444 info.item_compare_numbers = TRUE;
11445 }
11446#ifdef FEAT_FLOAT
11447 else if (STRCMP(info.item_compare_func, "f") == 0)
11448 {
11449 info.item_compare_func = NULL;
11450 info.item_compare_float = TRUE;
11451 }
11452#endif
11453 else if (STRCMP(info.item_compare_func, "i") == 0)
11454 {
11455 info.item_compare_func = NULL;
11456 info.item_compare_ic = TRUE;
11457 }
11458 }
11459 }
11460
11461 if (argvars[2].v_type != VAR_UNKNOWN)
11462 {
11463 /* optional third argument: {dict} */
11464 if (argvars[2].v_type != VAR_DICT)
11465 {
11466 EMSG(_(e_dictreq));
11467 goto theend;
11468 }
11469 info.item_compare_selfdict = argvars[2].vval.v_dict;
11470 }
11471 }
11472
11473 /* Make an array with each entry pointing to an item in the List. */
11474 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11475 if (ptrs == NULL)
11476 goto theend;
11477
11478 i = 0;
11479 if (sort)
11480 {
11481 /* sort(): ptrs will be the list to sort */
11482 for (li = l->lv_first; li != NULL; li = li->li_next)
11483 {
11484 ptrs[i].item = li;
11485 ptrs[i].idx = i;
11486 ++i;
11487 }
11488
11489 info.item_compare_func_err = FALSE;
11490 info.item_compare_keep_zero = FALSE;
11491 /* test the compare function */
11492 if ((info.item_compare_func != NULL
11493 || info.item_compare_partial != NULL)
11494 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11495 == ITEM_COMPARE_FAIL)
11496 EMSG(_("E702: Sort compare function failed"));
11497 else
11498 {
11499 /* Sort the array with item pointers. */
11500 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11501 info.item_compare_func == NULL
11502 && info.item_compare_partial == NULL
11503 ? item_compare : item_compare2);
11504
11505 if (!info.item_compare_func_err)
11506 {
11507 /* Clear the List and append the items in sorted order. */
11508 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11509 l->lv_len = 0;
11510 for (i = 0; i < len; ++i)
11511 list_append(l, ptrs[i].item);
11512 }
11513 }
11514 }
11515 else
11516 {
11517 int (*item_compare_func_ptr)(const void *, const void *);
11518
11519 /* f_uniq(): ptrs will be a stack of items to remove */
11520 info.item_compare_func_err = FALSE;
11521 info.item_compare_keep_zero = TRUE;
11522 item_compare_func_ptr = info.item_compare_func != NULL
11523 || info.item_compare_partial != NULL
11524 ? item_compare2 : item_compare;
11525
11526 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11527 li = li->li_next)
11528 {
11529 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11530 == 0)
11531 ptrs[i++].item = li;
11532 if (info.item_compare_func_err)
11533 {
11534 EMSG(_("E882: Uniq compare function failed"));
11535 break;
11536 }
11537 }
11538
11539 if (!info.item_compare_func_err)
11540 {
11541 while (--i >= 0)
11542 {
11543 li = ptrs[i].item->li_next;
11544 ptrs[i].item->li_next = li->li_next;
11545 if (li->li_next != NULL)
11546 li->li_next->li_prev = ptrs[i].item;
11547 else
11548 l->lv_last = ptrs[i].item;
11549 list_fix_watch(l, li);
11550 listitem_free(li);
11551 l->lv_len--;
11552 }
11553 }
11554 }
11555
11556 vim_free(ptrs);
11557 }
11558theend:
11559 sortinfo = old_sortinfo;
11560}
11561
11562/*
11563 * "sort({list})" function
11564 */
11565 static void
11566f_sort(typval_T *argvars, typval_T *rettv)
11567{
11568 do_sort_uniq(argvars, rettv, TRUE);
11569}
11570
11571/*
11572 * "uniq({list})" function
11573 */
11574 static void
11575f_uniq(typval_T *argvars, typval_T *rettv)
11576{
11577 do_sort_uniq(argvars, rettv, FALSE);
11578}
11579
11580/*
11581 * "soundfold({word})" function
11582 */
11583 static void
11584f_soundfold(typval_T *argvars, typval_T *rettv)
11585{
11586 char_u *s;
11587
11588 rettv->v_type = VAR_STRING;
11589 s = get_tv_string(&argvars[0]);
11590#ifdef FEAT_SPELL
11591 rettv->vval.v_string = eval_soundfold(s);
11592#else
11593 rettv->vval.v_string = vim_strsave(s);
11594#endif
11595}
11596
11597/*
11598 * "spellbadword()" function
11599 */
11600 static void
11601f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11602{
11603 char_u *word = (char_u *)"";
11604 hlf_T attr = HLF_COUNT;
11605 int len = 0;
11606
11607 if (rettv_list_alloc(rettv) == FAIL)
11608 return;
11609
11610#ifdef FEAT_SPELL
11611 if (argvars[0].v_type == VAR_UNKNOWN)
11612 {
11613 /* Find the start and length of the badly spelled word. */
11614 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11615 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011616 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011618 curwin->w_set_curswant = TRUE;
11619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011620 }
11621 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11622 {
11623 char_u *str = get_tv_string_chk(&argvars[0]);
11624 int capcol = -1;
11625
11626 if (str != NULL)
11627 {
11628 /* Check the argument for spelling. */
11629 while (*str != NUL)
11630 {
11631 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11632 if (attr != HLF_COUNT)
11633 {
11634 word = str;
11635 break;
11636 }
11637 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011638 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011639 }
11640 }
11641 }
11642#endif
11643
11644 list_append_string(rettv->vval.v_list, word, len);
11645 list_append_string(rettv->vval.v_list, (char_u *)(
11646 attr == HLF_SPB ? "bad" :
11647 attr == HLF_SPR ? "rare" :
11648 attr == HLF_SPL ? "local" :
11649 attr == HLF_SPC ? "caps" :
11650 ""), -1);
11651}
11652
11653/*
11654 * "spellsuggest()" function
11655 */
11656 static void
11657f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11658{
11659#ifdef FEAT_SPELL
11660 char_u *str;
11661 int typeerr = FALSE;
11662 int maxcount;
11663 garray_T ga;
11664 int i;
11665 listitem_T *li;
11666 int need_capital = FALSE;
11667#endif
11668
11669 if (rettv_list_alloc(rettv) == FAIL)
11670 return;
11671
11672#ifdef FEAT_SPELL
11673 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11674 {
11675 str = get_tv_string(&argvars[0]);
11676 if (argvars[1].v_type != VAR_UNKNOWN)
11677 {
11678 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11679 if (maxcount <= 0)
11680 return;
11681 if (argvars[2].v_type != VAR_UNKNOWN)
11682 {
11683 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11684 if (typeerr)
11685 return;
11686 }
11687 }
11688 else
11689 maxcount = 25;
11690
11691 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11692
11693 for (i = 0; i < ga.ga_len; ++i)
11694 {
11695 str = ((char_u **)ga.ga_data)[i];
11696
11697 li = listitem_alloc();
11698 if (li == NULL)
11699 vim_free(str);
11700 else
11701 {
11702 li->li_tv.v_type = VAR_STRING;
11703 li->li_tv.v_lock = 0;
11704 li->li_tv.vval.v_string = str;
11705 list_append(rettv->vval.v_list, li);
11706 }
11707 }
11708 ga_clear(&ga);
11709 }
11710#endif
11711}
11712
11713 static void
11714f_split(typval_T *argvars, typval_T *rettv)
11715{
11716 char_u *str;
11717 char_u *end;
11718 char_u *pat = NULL;
11719 regmatch_T regmatch;
11720 char_u patbuf[NUMBUFLEN];
11721 char_u *save_cpo;
11722 int match;
11723 colnr_T col = 0;
11724 int keepempty = FALSE;
11725 int typeerr = FALSE;
11726
11727 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11728 save_cpo = p_cpo;
11729 p_cpo = (char_u *)"";
11730
11731 str = get_tv_string(&argvars[0]);
11732 if (argvars[1].v_type != VAR_UNKNOWN)
11733 {
11734 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11735 if (pat == NULL)
11736 typeerr = TRUE;
11737 if (argvars[2].v_type != VAR_UNKNOWN)
11738 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11739 }
11740 if (pat == NULL || *pat == NUL)
11741 pat = (char_u *)"[\\x01- ]\\+";
11742
11743 if (rettv_list_alloc(rettv) == FAIL)
11744 return;
11745 if (typeerr)
11746 return;
11747
11748 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11749 if (regmatch.regprog != NULL)
11750 {
11751 regmatch.rm_ic = FALSE;
11752 while (*str != NUL || keepempty)
11753 {
11754 if (*str == NUL)
11755 match = FALSE; /* empty item at the end */
11756 else
11757 match = vim_regexec_nl(&regmatch, str, col);
11758 if (match)
11759 end = regmatch.startp[0];
11760 else
11761 end = str + STRLEN(str);
11762 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11763 && *str != NUL && match && end < regmatch.endp[0]))
11764 {
11765 if (list_append_string(rettv->vval.v_list, str,
11766 (int)(end - str)) == FAIL)
11767 break;
11768 }
11769 if (!match)
11770 break;
11771 /* Advance to just after the match. */
11772 if (regmatch.endp[0] > str)
11773 col = 0;
11774 else
11775 {
11776 /* Don't get stuck at the same match. */
11777#ifdef FEAT_MBYTE
11778 col = (*mb_ptr2len)(regmatch.endp[0]);
11779#else
11780 col = 1;
11781#endif
11782 }
11783 str = regmatch.endp[0];
11784 }
11785
11786 vim_regfree(regmatch.regprog);
11787 }
11788
11789 p_cpo = save_cpo;
11790}
11791
11792#ifdef FEAT_FLOAT
11793/*
11794 * "sqrt()" function
11795 */
11796 static void
11797f_sqrt(typval_T *argvars, typval_T *rettv)
11798{
11799 float_T f = 0.0;
11800
11801 rettv->v_type = VAR_FLOAT;
11802 if (get_float_arg(argvars, &f) == OK)
11803 rettv->vval.v_float = sqrt(f);
11804 else
11805 rettv->vval.v_float = 0.0;
11806}
11807
11808/*
11809 * "str2float()" function
11810 */
11811 static void
11812f_str2float(typval_T *argvars, typval_T *rettv)
11813{
11814 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011815 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011816
Bram Moolenaar08243d22017-01-10 16:12:29 +010011817 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011818 p = skipwhite(p + 1);
11819 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011820 if (isneg)
11821 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011822 rettv->v_type = VAR_FLOAT;
11823}
11824#endif
11825
11826/*
11827 * "str2nr()" function
11828 */
11829 static void
11830f_str2nr(typval_T *argvars, typval_T *rettv)
11831{
11832 int base = 10;
11833 char_u *p;
11834 varnumber_T n;
11835 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011836 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011837
11838 if (argvars[1].v_type != VAR_UNKNOWN)
11839 {
11840 base = (int)get_tv_number(&argvars[1]);
11841 if (base != 2 && base != 8 && base != 10 && base != 16)
11842 {
11843 EMSG(_(e_invarg));
11844 return;
11845 }
11846 }
11847
11848 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011849 isneg = (*p == '-');
11850 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011851 p = skipwhite(p + 1);
11852 switch (base)
11853 {
11854 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11855 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11856 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11857 default: what = 0;
11858 }
11859 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011860 if (isneg)
11861 rettv->vval.v_number = -n;
11862 else
11863 rettv->vval.v_number = n;
11864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865}
11866
11867#ifdef HAVE_STRFTIME
11868/*
11869 * "strftime({format}[, {time}])" function
11870 */
11871 static void
11872f_strftime(typval_T *argvars, typval_T *rettv)
11873{
11874 char_u result_buf[256];
11875 struct tm *curtime;
11876 time_t seconds;
11877 char_u *p;
11878
11879 rettv->v_type = VAR_STRING;
11880
11881 p = get_tv_string(&argvars[0]);
11882 if (argvars[1].v_type == VAR_UNKNOWN)
11883 seconds = time(NULL);
11884 else
11885 seconds = (time_t)get_tv_number(&argvars[1]);
11886 curtime = localtime(&seconds);
11887 /* MSVC returns NULL for an invalid value of seconds. */
11888 if (curtime == NULL)
11889 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11890 else
11891 {
11892# ifdef FEAT_MBYTE
11893 vimconv_T conv;
11894 char_u *enc;
11895
11896 conv.vc_type = CONV_NONE;
11897 enc = enc_locale();
11898 convert_setup(&conv, p_enc, enc);
11899 if (conv.vc_type != CONV_NONE)
11900 p = string_convert(&conv, p, NULL);
11901# endif
11902 if (p != NULL)
11903 (void)strftime((char *)result_buf, sizeof(result_buf),
11904 (char *)p, curtime);
11905 else
11906 result_buf[0] = NUL;
11907
11908# ifdef FEAT_MBYTE
11909 if (conv.vc_type != CONV_NONE)
11910 vim_free(p);
11911 convert_setup(&conv, enc, p_enc);
11912 if (conv.vc_type != CONV_NONE)
11913 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11914 else
11915# endif
11916 rettv->vval.v_string = vim_strsave(result_buf);
11917
11918# ifdef FEAT_MBYTE
11919 /* Release conversion descriptors */
11920 convert_setup(&conv, NULL, NULL);
11921 vim_free(enc);
11922# endif
11923 }
11924}
11925#endif
11926
11927/*
11928 * "strgetchar()" function
11929 */
11930 static void
11931f_strgetchar(typval_T *argvars, typval_T *rettv)
11932{
11933 char_u *str;
11934 int len;
11935 int error = FALSE;
11936 int charidx;
11937
11938 rettv->vval.v_number = -1;
11939 str = get_tv_string_chk(&argvars[0]);
11940 if (str == NULL)
11941 return;
11942 len = (int)STRLEN(str);
11943 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11944 if (error)
11945 return;
11946#ifdef FEAT_MBYTE
11947 {
11948 int byteidx = 0;
11949
11950 while (charidx >= 0 && byteidx < len)
11951 {
11952 if (charidx == 0)
11953 {
11954 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11955 break;
11956 }
11957 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011958 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959 }
11960 }
11961#else
11962 if (charidx < len)
11963 rettv->vval.v_number = str[charidx];
11964#endif
11965}
11966
11967/*
11968 * "stridx()" function
11969 */
11970 static void
11971f_stridx(typval_T *argvars, typval_T *rettv)
11972{
11973 char_u buf[NUMBUFLEN];
11974 char_u *needle;
11975 char_u *haystack;
11976 char_u *save_haystack;
11977 char_u *pos;
11978 int start_idx;
11979
11980 needle = get_tv_string_chk(&argvars[1]);
11981 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11982 rettv->vval.v_number = -1;
11983 if (needle == NULL || haystack == NULL)
11984 return; /* type error; errmsg already given */
11985
11986 if (argvars[2].v_type != VAR_UNKNOWN)
11987 {
11988 int error = FALSE;
11989
11990 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11991 if (error || start_idx >= (int)STRLEN(haystack))
11992 return;
11993 if (start_idx >= 0)
11994 haystack += start_idx;
11995 }
11996
11997 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11998 if (pos != NULL)
11999 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12000}
12001
12002/*
12003 * "string()" function
12004 */
12005 static void
12006f_string(typval_T *argvars, typval_T *rettv)
12007{
12008 char_u *tofree;
12009 char_u numbuf[NUMBUFLEN];
12010
12011 rettv->v_type = VAR_STRING;
12012 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12013 get_copyID());
12014 /* Make a copy if we have a value but it's not in allocated memory. */
12015 if (rettv->vval.v_string != NULL && tofree == NULL)
12016 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12017}
12018
12019/*
12020 * "strlen()" function
12021 */
12022 static void
12023f_strlen(typval_T *argvars, typval_T *rettv)
12024{
12025 rettv->vval.v_number = (varnumber_T)(STRLEN(
12026 get_tv_string(&argvars[0])));
12027}
12028
12029/*
12030 * "strchars()" function
12031 */
12032 static void
12033f_strchars(typval_T *argvars, typval_T *rettv)
12034{
12035 char_u *s = get_tv_string(&argvars[0]);
12036 int skipcc = 0;
12037#ifdef FEAT_MBYTE
12038 varnumber_T len = 0;
12039 int (*func_mb_ptr2char_adv)(char_u **pp);
12040#endif
12041
12042 if (argvars[1].v_type != VAR_UNKNOWN)
12043 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12044 if (skipcc < 0 || skipcc > 1)
12045 EMSG(_(e_invarg));
12046 else
12047 {
12048#ifdef FEAT_MBYTE
12049 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12050 while (*s != NUL)
12051 {
12052 func_mb_ptr2char_adv(&s);
12053 ++len;
12054 }
12055 rettv->vval.v_number = len;
12056#else
12057 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12058#endif
12059 }
12060}
12061
12062/*
12063 * "strdisplaywidth()" function
12064 */
12065 static void
12066f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12067{
12068 char_u *s = get_tv_string(&argvars[0]);
12069 int col = 0;
12070
12071 if (argvars[1].v_type != VAR_UNKNOWN)
12072 col = (int)get_tv_number(&argvars[1]);
12073
12074 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12075}
12076
12077/*
12078 * "strwidth()" function
12079 */
12080 static void
12081f_strwidth(typval_T *argvars, typval_T *rettv)
12082{
12083 char_u *s = get_tv_string(&argvars[0]);
12084
12085 rettv->vval.v_number = (varnumber_T)(
12086#ifdef FEAT_MBYTE
12087 mb_string2cells(s, -1)
12088#else
12089 STRLEN(s)
12090#endif
12091 );
12092}
12093
12094/*
12095 * "strcharpart()" function
12096 */
12097 static void
12098f_strcharpart(typval_T *argvars, typval_T *rettv)
12099{
12100#ifdef FEAT_MBYTE
12101 char_u *p;
12102 int nchar;
12103 int nbyte = 0;
12104 int charlen;
12105 int len = 0;
12106 int slen;
12107 int error = FALSE;
12108
12109 p = get_tv_string(&argvars[0]);
12110 slen = (int)STRLEN(p);
12111
12112 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12113 if (!error)
12114 {
12115 if (nchar > 0)
12116 while (nchar > 0 && nbyte < slen)
12117 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012118 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012119 --nchar;
12120 }
12121 else
12122 nbyte = nchar;
12123 if (argvars[2].v_type != VAR_UNKNOWN)
12124 {
12125 charlen = (int)get_tv_number(&argvars[2]);
12126 while (charlen > 0 && nbyte + len < slen)
12127 {
12128 int off = nbyte + len;
12129
12130 if (off < 0)
12131 len += 1;
12132 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012133 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012134 --charlen;
12135 }
12136 }
12137 else
12138 len = slen - nbyte; /* default: all bytes that are available. */
12139 }
12140
12141 /*
12142 * Only return the overlap between the specified part and the actual
12143 * string.
12144 */
12145 if (nbyte < 0)
12146 {
12147 len += nbyte;
12148 nbyte = 0;
12149 }
12150 else if (nbyte > slen)
12151 nbyte = slen;
12152 if (len < 0)
12153 len = 0;
12154 else if (nbyte + len > slen)
12155 len = slen - nbyte;
12156
12157 rettv->v_type = VAR_STRING;
12158 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12159#else
12160 f_strpart(argvars, rettv);
12161#endif
12162}
12163
12164/*
12165 * "strpart()" function
12166 */
12167 static void
12168f_strpart(typval_T *argvars, typval_T *rettv)
12169{
12170 char_u *p;
12171 int n;
12172 int len;
12173 int slen;
12174 int error = FALSE;
12175
12176 p = get_tv_string(&argvars[0]);
12177 slen = (int)STRLEN(p);
12178
12179 n = (int)get_tv_number_chk(&argvars[1], &error);
12180 if (error)
12181 len = 0;
12182 else if (argvars[2].v_type != VAR_UNKNOWN)
12183 len = (int)get_tv_number(&argvars[2]);
12184 else
12185 len = slen - n; /* default len: all bytes that are available. */
12186
12187 /*
12188 * Only return the overlap between the specified part and the actual
12189 * string.
12190 */
12191 if (n < 0)
12192 {
12193 len += n;
12194 n = 0;
12195 }
12196 else if (n > slen)
12197 n = slen;
12198 if (len < 0)
12199 len = 0;
12200 else if (n + len > slen)
12201 len = slen - n;
12202
12203 rettv->v_type = VAR_STRING;
12204 rettv->vval.v_string = vim_strnsave(p + n, len);
12205}
12206
12207/*
12208 * "strridx()" function
12209 */
12210 static void
12211f_strridx(typval_T *argvars, typval_T *rettv)
12212{
12213 char_u buf[NUMBUFLEN];
12214 char_u *needle;
12215 char_u *haystack;
12216 char_u *rest;
12217 char_u *lastmatch = NULL;
12218 int haystack_len, end_idx;
12219
12220 needle = get_tv_string_chk(&argvars[1]);
12221 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12222
12223 rettv->vval.v_number = -1;
12224 if (needle == NULL || haystack == NULL)
12225 return; /* type error; errmsg already given */
12226
12227 haystack_len = (int)STRLEN(haystack);
12228 if (argvars[2].v_type != VAR_UNKNOWN)
12229 {
12230 /* Third argument: upper limit for index */
12231 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12232 if (end_idx < 0)
12233 return; /* can never find a match */
12234 }
12235 else
12236 end_idx = haystack_len;
12237
12238 if (*needle == NUL)
12239 {
12240 /* Empty string matches past the end. */
12241 lastmatch = haystack + end_idx;
12242 }
12243 else
12244 {
12245 for (rest = haystack; *rest != '\0'; ++rest)
12246 {
12247 rest = (char_u *)strstr((char *)rest, (char *)needle);
12248 if (rest == NULL || rest > haystack + end_idx)
12249 break;
12250 lastmatch = rest;
12251 }
12252 }
12253
12254 if (lastmatch == NULL)
12255 rettv->vval.v_number = -1;
12256 else
12257 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12258}
12259
12260/*
12261 * "strtrans()" function
12262 */
12263 static void
12264f_strtrans(typval_T *argvars, typval_T *rettv)
12265{
12266 rettv->v_type = VAR_STRING;
12267 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12268}
12269
12270/*
12271 * "submatch()" function
12272 */
12273 static void
12274f_submatch(typval_T *argvars, typval_T *rettv)
12275{
12276 int error = FALSE;
12277 int no;
12278 int retList = 0;
12279
12280 no = (int)get_tv_number_chk(&argvars[0], &error);
12281 if (error)
12282 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012283 if (no < 0 || no >= NSUBEXP)
12284 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012285 EMSGN(_("E935: invalid submatch number: %d"), no);
12286 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012287 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012288 if (argvars[1].v_type != VAR_UNKNOWN)
12289 retList = (int)get_tv_number_chk(&argvars[1], &error);
12290 if (error)
12291 return;
12292
12293 if (retList == 0)
12294 {
12295 rettv->v_type = VAR_STRING;
12296 rettv->vval.v_string = reg_submatch(no);
12297 }
12298 else
12299 {
12300 rettv->v_type = VAR_LIST;
12301 rettv->vval.v_list = reg_submatch_list(no);
12302 }
12303}
12304
12305/*
12306 * "substitute()" function
12307 */
12308 static void
12309f_substitute(typval_T *argvars, typval_T *rettv)
12310{
12311 char_u patbuf[NUMBUFLEN];
12312 char_u subbuf[NUMBUFLEN];
12313 char_u flagsbuf[NUMBUFLEN];
12314
12315 char_u *str = get_tv_string_chk(&argvars[0]);
12316 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012317 char_u *sub = NULL;
12318 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012319 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12320
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012321 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12322 expr = &argvars[2];
12323 else
12324 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012326 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012327 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12328 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012329 rettv->vval.v_string = NULL;
12330 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012331 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012332}
12333
12334/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012335 * "swapinfo(swap_filename)" function
12336 */
12337 static void
12338f_swapinfo(typval_T *argvars, typval_T *rettv)
12339{
12340 if (rettv_dict_alloc(rettv) == OK)
12341 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12342}
12343
12344/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012345 * "synID(lnum, col, trans)" function
12346 */
12347 static void
12348f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12349{
12350 int id = 0;
12351#ifdef FEAT_SYN_HL
12352 linenr_T lnum;
12353 colnr_T col;
12354 int trans;
12355 int transerr = FALSE;
12356
12357 lnum = get_tv_lnum(argvars); /* -1 on type error */
12358 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12359 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12360
12361 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12362 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12363 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12364#endif
12365
12366 rettv->vval.v_number = id;
12367}
12368
12369/*
12370 * "synIDattr(id, what [, mode])" function
12371 */
12372 static void
12373f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12374{
12375 char_u *p = NULL;
12376#ifdef FEAT_SYN_HL
12377 int id;
12378 char_u *what;
12379 char_u *mode;
12380 char_u modebuf[NUMBUFLEN];
12381 int modec;
12382
12383 id = (int)get_tv_number(&argvars[0]);
12384 what = get_tv_string(&argvars[1]);
12385 if (argvars[2].v_type != VAR_UNKNOWN)
12386 {
12387 mode = get_tv_string_buf(&argvars[2], modebuf);
12388 modec = TOLOWER_ASC(mode[0]);
12389 if (modec != 't' && modec != 'c' && modec != 'g')
12390 modec = 0; /* replace invalid with current */
12391 }
12392 else
12393 {
12394#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12395 if (USE_24BIT)
12396 modec = 'g';
12397 else
12398#endif
12399 if (t_colors > 1)
12400 modec = 'c';
12401 else
12402 modec = 't';
12403 }
12404
12405
12406 switch (TOLOWER_ASC(what[0]))
12407 {
12408 case 'b':
12409 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12410 p = highlight_color(id, what, modec);
12411 else /* bold */
12412 p = highlight_has_attr(id, HL_BOLD, modec);
12413 break;
12414
12415 case 'f': /* fg[#] or font */
12416 p = highlight_color(id, what, modec);
12417 break;
12418
12419 case 'i':
12420 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12421 p = highlight_has_attr(id, HL_INVERSE, modec);
12422 else /* italic */
12423 p = highlight_has_attr(id, HL_ITALIC, modec);
12424 break;
12425
12426 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012427 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428 break;
12429
12430 case 'r': /* reverse */
12431 p = highlight_has_attr(id, HL_INVERSE, modec);
12432 break;
12433
12434 case 's':
12435 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12436 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012437 /* strikeout */
12438 else if (TOLOWER_ASC(what[1]) == 't' &&
12439 TOLOWER_ASC(what[2]) == 'r')
12440 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012441 else /* standout */
12442 p = highlight_has_attr(id, HL_STANDOUT, modec);
12443 break;
12444
12445 case 'u':
12446 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12447 /* underline */
12448 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12449 else
12450 /* undercurl */
12451 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12452 break;
12453 }
12454
12455 if (p != NULL)
12456 p = vim_strsave(p);
12457#endif
12458 rettv->v_type = VAR_STRING;
12459 rettv->vval.v_string = p;
12460}
12461
12462/*
12463 * "synIDtrans(id)" function
12464 */
12465 static void
12466f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12467{
12468 int id;
12469
12470#ifdef FEAT_SYN_HL
12471 id = (int)get_tv_number(&argvars[0]);
12472
12473 if (id > 0)
12474 id = syn_get_final_id(id);
12475 else
12476#endif
12477 id = 0;
12478
12479 rettv->vval.v_number = id;
12480}
12481
12482/*
12483 * "synconcealed(lnum, col)" function
12484 */
12485 static void
12486f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12487{
12488#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12489 linenr_T lnum;
12490 colnr_T col;
12491 int syntax_flags = 0;
12492 int cchar;
12493 int matchid = 0;
12494 char_u str[NUMBUFLEN];
12495#endif
12496
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012497 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498
12499#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12500 lnum = get_tv_lnum(argvars); /* -1 on type error */
12501 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12502
12503 vim_memset(str, NUL, sizeof(str));
12504
12505 if (rettv_list_alloc(rettv) != FAIL)
12506 {
12507 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12508 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12509 && curwin->w_p_cole > 0)
12510 {
12511 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12512 syntax_flags = get_syntax_info(&matchid);
12513
12514 /* get the conceal character */
12515 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12516 {
12517 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012518 if (cchar == NUL && curwin->w_p_cole == 1)
12519 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012520 if (cchar != NUL)
12521 {
12522# ifdef FEAT_MBYTE
12523 if (has_mbyte)
12524 (*mb_char2bytes)(cchar, str);
12525 else
12526# endif
12527 str[0] = cchar;
12528 }
12529 }
12530 }
12531
12532 list_append_number(rettv->vval.v_list,
12533 (syntax_flags & HL_CONCEAL) != 0);
12534 /* -1 to auto-determine strlen */
12535 list_append_string(rettv->vval.v_list, str, -1);
12536 list_append_number(rettv->vval.v_list, matchid);
12537 }
12538#endif
12539}
12540
12541/*
12542 * "synstack(lnum, col)" function
12543 */
12544 static void
12545f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12546{
12547#ifdef FEAT_SYN_HL
12548 linenr_T lnum;
12549 colnr_T col;
12550 int i;
12551 int id;
12552#endif
12553
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012554 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012555
12556#ifdef FEAT_SYN_HL
12557 lnum = get_tv_lnum(argvars); /* -1 on type error */
12558 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12559
12560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12561 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12562 && rettv_list_alloc(rettv) != FAIL)
12563 {
12564 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12565 for (i = 0; ; ++i)
12566 {
12567 id = syn_get_stack_item(i);
12568 if (id < 0)
12569 break;
12570 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12571 break;
12572 }
12573 }
12574#endif
12575}
12576
12577 static void
12578get_cmd_output_as_rettv(
12579 typval_T *argvars,
12580 typval_T *rettv,
12581 int retlist)
12582{
12583 char_u *res = NULL;
12584 char_u *p;
12585 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 int err = FALSE;
12587 FILE *fd;
12588 list_T *list = NULL;
12589 int flags = SHELL_SILENT;
12590
12591 rettv->v_type = VAR_STRING;
12592 rettv->vval.v_string = NULL;
12593 if (check_restricted() || check_secure())
12594 goto errret;
12595
12596 if (argvars[1].v_type != VAR_UNKNOWN)
12597 {
12598 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012599 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012600 * command.
12601 */
12602 if ((infile = vim_tempname('i', TRUE)) == NULL)
12603 {
12604 EMSG(_(e_notmp));
12605 goto errret;
12606 }
12607
12608 fd = mch_fopen((char *)infile, WRITEBIN);
12609 if (fd == NULL)
12610 {
12611 EMSG2(_(e_notopen), infile);
12612 goto errret;
12613 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012614 if (argvars[1].v_type == VAR_NUMBER)
12615 {
12616 linenr_T lnum;
12617 buf_T *buf;
12618
12619 buf = buflist_findnr(argvars[1].vval.v_number);
12620 if (buf == NULL)
12621 {
12622 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012623 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012624 goto errret;
12625 }
12626
12627 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12628 {
12629 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12630 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12631 {
12632 err = TRUE;
12633 break;
12634 }
12635 if (putc(NL, fd) == EOF)
12636 {
12637 err = TRUE;
12638 break;
12639 }
12640 }
12641 }
12642 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012643 {
12644 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12645 err = TRUE;
12646 }
12647 else
12648 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012649 size_t len;
12650 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651
12652 p = get_tv_string_buf_chk(&argvars[1], buf);
12653 if (p == NULL)
12654 {
12655 fclose(fd);
12656 goto errret; /* type error; errmsg already given */
12657 }
12658 len = STRLEN(p);
12659 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12660 err = TRUE;
12661 }
12662 if (fclose(fd) != 0)
12663 err = TRUE;
12664 if (err)
12665 {
12666 EMSG(_("E677: Error writing temp file"));
12667 goto errret;
12668 }
12669 }
12670
12671 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12672 * echoes typeahead, that messes up the display. */
12673 if (!msg_silent)
12674 flags += SHELL_COOKED;
12675
12676 if (retlist)
12677 {
12678 int len;
12679 listitem_T *li;
12680 char_u *s = NULL;
12681 char_u *start;
12682 char_u *end;
12683 int i;
12684
12685 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12686 if (res == NULL)
12687 goto errret;
12688
12689 list = list_alloc();
12690 if (list == NULL)
12691 goto errret;
12692
12693 for (i = 0; i < len; ++i)
12694 {
12695 start = res + i;
12696 while (i < len && res[i] != NL)
12697 ++i;
12698 end = res + i;
12699
12700 s = alloc((unsigned)(end - start + 1));
12701 if (s == NULL)
12702 goto errret;
12703
12704 for (p = s; start < end; ++p, ++start)
12705 *p = *start == NUL ? NL : *start;
12706 *p = NUL;
12707
12708 li = listitem_alloc();
12709 if (li == NULL)
12710 {
12711 vim_free(s);
12712 goto errret;
12713 }
12714 li->li_tv.v_type = VAR_STRING;
12715 li->li_tv.v_lock = 0;
12716 li->li_tv.vval.v_string = s;
12717 list_append(list, li);
12718 }
12719
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012720 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012721 list = NULL;
12722 }
12723 else
12724 {
12725 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12726#ifdef USE_CR
12727 /* translate <CR> into <NL> */
12728 if (res != NULL)
12729 {
12730 char_u *s;
12731
12732 for (s = res; *s; ++s)
12733 {
12734 if (*s == CAR)
12735 *s = NL;
12736 }
12737 }
12738#else
12739# ifdef USE_CRNL
12740 /* translate <CR><NL> into <NL> */
12741 if (res != NULL)
12742 {
12743 char_u *s, *d;
12744
12745 d = res;
12746 for (s = res; *s; ++s)
12747 {
12748 if (s[0] == CAR && s[1] == NL)
12749 ++s;
12750 *d++ = *s;
12751 }
12752 *d = NUL;
12753 }
12754# endif
12755#endif
12756 rettv->vval.v_string = res;
12757 res = NULL;
12758 }
12759
12760errret:
12761 if (infile != NULL)
12762 {
12763 mch_remove(infile);
12764 vim_free(infile);
12765 }
12766 if (res != NULL)
12767 vim_free(res);
12768 if (list != NULL)
12769 list_free(list);
12770}
12771
12772/*
12773 * "system()" function
12774 */
12775 static void
12776f_system(typval_T *argvars, typval_T *rettv)
12777{
12778 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12779}
12780
12781/*
12782 * "systemlist()" function
12783 */
12784 static void
12785f_systemlist(typval_T *argvars, typval_T *rettv)
12786{
12787 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12788}
12789
12790/*
12791 * "tabpagebuflist()" function
12792 */
12793 static void
12794f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12795{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012796 tabpage_T *tp;
12797 win_T *wp = NULL;
12798
12799 if (argvars[0].v_type == VAR_UNKNOWN)
12800 wp = firstwin;
12801 else
12802 {
12803 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12804 if (tp != NULL)
12805 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12806 }
12807 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12808 {
12809 for (; wp != NULL; wp = wp->w_next)
12810 if (list_append_number(rettv->vval.v_list,
12811 wp->w_buffer->b_fnum) == FAIL)
12812 break;
12813 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814}
12815
12816
12817/*
12818 * "tabpagenr()" function
12819 */
12820 static void
12821f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12822{
12823 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012824 char_u *arg;
12825
12826 if (argvars[0].v_type != VAR_UNKNOWN)
12827 {
12828 arg = get_tv_string_chk(&argvars[0]);
12829 nr = 0;
12830 if (arg != NULL)
12831 {
12832 if (STRCMP(arg, "$") == 0)
12833 nr = tabpage_index(NULL) - 1;
12834 else
12835 EMSG2(_(e_invexpr2), arg);
12836 }
12837 }
12838 else
12839 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012840 rettv->vval.v_number = nr;
12841}
12842
12843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012844static int get_winnr(tabpage_T *tp, typval_T *argvar);
12845
12846/*
12847 * Common code for tabpagewinnr() and winnr().
12848 */
12849 static int
12850get_winnr(tabpage_T *tp, typval_T *argvar)
12851{
12852 win_T *twin;
12853 int nr = 1;
12854 win_T *wp;
12855 char_u *arg;
12856
12857 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12858 if (argvar->v_type != VAR_UNKNOWN)
12859 {
12860 arg = get_tv_string_chk(argvar);
12861 if (arg == NULL)
12862 nr = 0; /* type error; errmsg already given */
12863 else if (STRCMP(arg, "$") == 0)
12864 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12865 else if (STRCMP(arg, "#") == 0)
12866 {
12867 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12868 if (twin == NULL)
12869 nr = 0;
12870 }
12871 else
12872 {
12873 EMSG2(_(e_invexpr2), arg);
12874 nr = 0;
12875 }
12876 }
12877
12878 if (nr > 0)
12879 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12880 wp != twin; wp = wp->w_next)
12881 {
12882 if (wp == NULL)
12883 {
12884 /* didn't find it in this tabpage */
12885 nr = 0;
12886 break;
12887 }
12888 ++nr;
12889 }
12890 return nr;
12891}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892
12893/*
12894 * "tabpagewinnr()" function
12895 */
12896 static void
12897f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12898{
12899 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 tabpage_T *tp;
12901
12902 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12903 if (tp == NULL)
12904 nr = 0;
12905 else
12906 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012907 rettv->vval.v_number = nr;
12908}
12909
12910
12911/*
12912 * "tagfiles()" function
12913 */
12914 static void
12915f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12916{
12917 char_u *fname;
12918 tagname_T tn;
12919 int first;
12920
12921 if (rettv_list_alloc(rettv) == FAIL)
12922 return;
12923 fname = alloc(MAXPATHL);
12924 if (fname == NULL)
12925 return;
12926
12927 for (first = TRUE; ; first = FALSE)
12928 if (get_tagfname(&tn, first, fname) == FAIL
12929 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12930 break;
12931 tagname_free(&tn);
12932 vim_free(fname);
12933}
12934
12935/*
12936 * "taglist()" function
12937 */
12938 static void
12939f_taglist(typval_T *argvars, typval_T *rettv)
12940{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012941 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942 char_u *tag_pattern;
12943
12944 tag_pattern = get_tv_string(&argvars[0]);
12945
12946 rettv->vval.v_number = FALSE;
12947 if (*tag_pattern == NUL)
12948 return;
12949
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012950 if (argvars[1].v_type != VAR_UNKNOWN)
12951 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012953 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012954}
12955
12956/*
12957 * "tempname()" function
12958 */
12959 static void
12960f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12961{
12962 static int x = 'A';
12963
12964 rettv->v_type = VAR_STRING;
12965 rettv->vval.v_string = vim_tempname(x, FALSE);
12966
12967 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12968 * names. Skip 'I' and 'O', they are used for shell redirection. */
12969 do
12970 {
12971 if (x == 'Z')
12972 x = '0';
12973 else if (x == '9')
12974 x = 'A';
12975 else
12976 {
12977#ifdef EBCDIC
12978 if (x == 'I')
12979 x = 'J';
12980 else if (x == 'R')
12981 x = 'S';
12982 else
12983#endif
12984 ++x;
12985 }
12986 } while (x == 'I' || x == 'O');
12987}
12988
12989#ifdef FEAT_FLOAT
12990/*
12991 * "tan()" function
12992 */
12993 static void
12994f_tan(typval_T *argvars, typval_T *rettv)
12995{
12996 float_T f = 0.0;
12997
12998 rettv->v_type = VAR_FLOAT;
12999 if (get_float_arg(argvars, &f) == OK)
13000 rettv->vval.v_float = tan(f);
13001 else
13002 rettv->vval.v_float = 0.0;
13003}
13004
13005/*
13006 * "tanh()" function
13007 */
13008 static void
13009f_tanh(typval_T *argvars, typval_T *rettv)
13010{
13011 float_T f = 0.0;
13012
13013 rettv->v_type = VAR_FLOAT;
13014 if (get_float_arg(argvars, &f) == OK)
13015 rettv->vval.v_float = tanh(f);
13016 else
13017 rettv->vval.v_float = 0.0;
13018}
13019#endif
13020
13021/*
13022 * "test_alloc_fail(id, countdown, repeat)" function
13023 */
13024 static void
13025f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13026{
13027 if (argvars[0].v_type != VAR_NUMBER
13028 || argvars[0].vval.v_number <= 0
13029 || argvars[1].v_type != VAR_NUMBER
13030 || argvars[1].vval.v_number < 0
13031 || argvars[2].v_type != VAR_NUMBER)
13032 EMSG(_(e_invarg));
13033 else
13034 {
13035 alloc_fail_id = argvars[0].vval.v_number;
13036 if (alloc_fail_id >= aid_last)
13037 EMSG(_(e_invarg));
13038 alloc_fail_countdown = argvars[1].vval.v_number;
13039 alloc_fail_repeat = argvars[2].vval.v_number;
13040 did_outofmem_msg = FALSE;
13041 }
13042}
13043
13044/*
13045 * "test_autochdir()"
13046 */
13047 static void
13048f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13049{
13050#if defined(FEAT_AUTOCHDIR)
13051 test_autochdir = TRUE;
13052#endif
13053}
13054
13055/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013056 * "test_feedinput()"
13057 */
13058 static void
13059f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13060{
13061#ifdef USE_INPUT_BUF
13062 char_u *val = get_tv_string_chk(&argvars[0]);
13063
13064 if (val != NULL)
13065 {
13066 trash_input_buf();
13067 add_to_input_buf_csi(val, (int)STRLEN(val));
13068 }
13069#endif
13070}
13071
13072/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013073 * "test_option_not_set({name})" function
13074 */
13075 static void
13076f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13077{
13078 char_u *name = (char_u *)"";
13079
13080 if (argvars[0].v_type != VAR_STRING)
13081 EMSG(_(e_invarg));
13082 else
13083 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013084 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013085 if (reset_option_was_set(name) == FAIL)
13086 EMSG2(_(e_invarg2), name);
13087 }
13088}
13089
13090/*
13091 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 */
13093 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013094f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013095{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013096 char_u *name = (char_u *)"";
13097 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013098 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013099
13100 if (argvars[0].v_type != VAR_STRING
13101 || (argvars[1].v_type) != VAR_NUMBER)
13102 EMSG(_(e_invarg));
13103 else
13104 {
13105 name = get_tv_string_chk(&argvars[0]);
13106 val = (int)get_tv_number(&argvars[1]);
13107
13108 if (STRCMP(name, (char_u *)"redraw") == 0)
13109 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013110 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13111 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013112 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13113 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013114 else if (STRCMP(name, (char_u *)"starting") == 0)
13115 {
13116 if (val)
13117 {
13118 if (save_starting < 0)
13119 save_starting = starting;
13120 starting = 0;
13121 }
13122 else
13123 {
13124 starting = save_starting;
13125 save_starting = -1;
13126 }
13127 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013128 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13129 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013130 else if (STRCMP(name, (char_u *)"ALL") == 0)
13131 {
13132 disable_char_avail_for_testing = FALSE;
13133 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013134 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013135 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013136 if (save_starting >= 0)
13137 {
13138 starting = save_starting;
13139 save_starting = -1;
13140 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013141 }
13142 else
13143 EMSG2(_(e_invarg2), name);
13144 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145}
13146
13147/*
13148 * "test_garbagecollect_now()" function
13149 */
13150 static void
13151f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13152{
13153 /* This is dangerous, any Lists and Dicts used internally may be freed
13154 * while still in use. */
13155 garbage_collect(TRUE);
13156}
13157
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013158/*
13159 * "test_ignore_error()" function
13160 */
13161 static void
13162f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13163{
13164 ignore_error_for_testing(get_tv_string(&argvars[0]));
13165}
13166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167#ifdef FEAT_JOB_CHANNEL
13168 static void
13169f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13170{
13171 rettv->v_type = VAR_CHANNEL;
13172 rettv->vval.v_channel = NULL;
13173}
13174#endif
13175
13176 static void
13177f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13178{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013179 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180}
13181
13182#ifdef FEAT_JOB_CHANNEL
13183 static void
13184f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13185{
13186 rettv->v_type = VAR_JOB;
13187 rettv->vval.v_job = NULL;
13188}
13189#endif
13190
13191 static void
13192f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13193{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013194 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195}
13196
13197 static void
13198f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13199{
13200 rettv->v_type = VAR_PARTIAL;
13201 rettv->vval.v_partial = NULL;
13202}
13203
13204 static void
13205f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13206{
13207 rettv->v_type = VAR_STRING;
13208 rettv->vval.v_string = NULL;
13209}
13210
Bram Moolenaarab186732018-09-14 21:27:06 +020013211#ifdef FEAT_GUI
13212 static void
13213f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13214{
13215 char_u *which;
13216 long value;
13217 int dragging;
13218 scrollbar_T *sb = NULL;
13219
13220 if (argvars[0].v_type != VAR_STRING
13221 || (argvars[1].v_type) != VAR_NUMBER
13222 || (argvars[2].v_type) != VAR_NUMBER)
13223 {
13224 EMSG(_(e_invarg));
13225 return;
13226 }
13227 which = get_tv_string(&argvars[0]);
13228 value = get_tv_number(&argvars[1]);
13229 dragging = get_tv_number(&argvars[2]);
13230
13231 if (STRCMP(which, "left") == 0)
13232 sb = &curwin->w_scrollbars[SBAR_LEFT];
13233 else if (STRCMP(which, "right") == 0)
13234 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13235 else if (STRCMP(which, "hor") == 0)
13236 sb = &gui.bottom_sbar;
13237 if (sb == NULL)
13238 {
13239 EMSG2(_(e_invarg2), which);
13240 return;
13241 }
13242 gui_drag_scrollbar(sb, value, dragging);
13243}
13244#endif
13245
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013246 static void
13247f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13248{
13249 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13250}
13251
13252#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13253/*
13254 * Get a callback from "arg". It can be a Funcref or a function name.
13255 * When "arg" is zero return an empty string.
13256 * Return NULL for an invalid argument.
13257 */
13258 char_u *
13259get_callback(typval_T *arg, partial_T **pp)
13260{
13261 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13262 {
13263 *pp = arg->vval.v_partial;
13264 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013265 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013266 }
13267 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013268 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269 {
13270 func_ref(arg->vval.v_string);
13271 return arg->vval.v_string;
13272 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13274 return (char_u *)"";
13275 EMSG(_("E921: Invalid callback argument"));
13276 return NULL;
13277}
13278
13279/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013280 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 */
13282 void
13283free_callback(char_u *callback, partial_T *partial)
13284{
13285 if (partial != NULL)
13286 partial_unref(partial);
13287 else if (callback != NULL)
13288 {
13289 func_unref(callback);
13290 vim_free(callback);
13291 }
13292}
13293#endif
13294
13295#ifdef FEAT_TIMERS
13296/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013297 * "timer_info([timer])" function
13298 */
13299 static void
13300f_timer_info(typval_T *argvars, typval_T *rettv)
13301{
13302 timer_T *timer = NULL;
13303
13304 if (rettv_list_alloc(rettv) != OK)
13305 return;
13306 if (argvars[0].v_type != VAR_UNKNOWN)
13307 {
13308 if (argvars[0].v_type != VAR_NUMBER)
13309 EMSG(_(e_number_exp));
13310 else
13311 {
13312 timer = find_timer((int)get_tv_number(&argvars[0]));
13313 if (timer != NULL)
13314 add_timer_info(rettv, timer);
13315 }
13316 }
13317 else
13318 add_timer_info_all(rettv);
13319}
13320
13321/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013322 * "timer_pause(timer, paused)" function
13323 */
13324 static void
13325f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13326{
13327 timer_T *timer = NULL;
13328 int paused = (int)get_tv_number(&argvars[1]);
13329
13330 if (argvars[0].v_type != VAR_NUMBER)
13331 EMSG(_(e_number_exp));
13332 else
13333 {
13334 timer = find_timer((int)get_tv_number(&argvars[0]));
13335 if (timer != NULL)
13336 timer->tr_paused = paused;
13337 }
13338}
13339
13340/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 * "timer_start(time, callback [, options])" function
13342 */
13343 static void
13344f_timer_start(typval_T *argvars, typval_T *rettv)
13345{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013346 long msec = (long)get_tv_number(&argvars[0]);
13347 timer_T *timer;
13348 int repeat = 0;
13349 char_u *callback;
13350 dict_T *dict;
13351 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352
Bram Moolenaar75537a92016-09-05 22:45:28 +020013353 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013354 if (check_secure())
13355 return;
13356 if (argvars[2].v_type != VAR_UNKNOWN)
13357 {
13358 if (argvars[2].v_type != VAR_DICT
13359 || (dict = argvars[2].vval.v_dict) == NULL)
13360 {
13361 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13362 return;
13363 }
13364 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13365 repeat = get_dict_number(dict, (char_u *)"repeat");
13366 }
13367
Bram Moolenaar75537a92016-09-05 22:45:28 +020013368 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013369 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013370 return;
13371
13372 timer = create_timer(msec, repeat);
13373 if (timer == NULL)
13374 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013375 else
13376 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013377 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013378 timer->tr_callback = vim_strsave(callback);
13379 else
13380 /* pointer into the partial */
13381 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013382 timer->tr_partial = partial;
13383 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384 }
13385}
13386
13387/*
13388 * "timer_stop(timer)" function
13389 */
13390 static void
13391f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13392{
13393 timer_T *timer;
13394
13395 if (argvars[0].v_type != VAR_NUMBER)
13396 {
13397 EMSG(_(e_number_exp));
13398 return;
13399 }
13400 timer = find_timer((int)get_tv_number(&argvars[0]));
13401 if (timer != NULL)
13402 stop_timer(timer);
13403}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013404
13405/*
13406 * "timer_stopall()" function
13407 */
13408 static void
13409f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13410{
13411 stop_all_timers();
13412}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413#endif
13414
13415/*
13416 * "tolower(string)" function
13417 */
13418 static void
13419f_tolower(typval_T *argvars, typval_T *rettv)
13420{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013422 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013423}
13424
13425/*
13426 * "toupper(string)" function
13427 */
13428 static void
13429f_toupper(typval_T *argvars, typval_T *rettv)
13430{
13431 rettv->v_type = VAR_STRING;
13432 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13433}
13434
13435/*
13436 * "tr(string, fromstr, tostr)" function
13437 */
13438 static void
13439f_tr(typval_T *argvars, typval_T *rettv)
13440{
13441 char_u *in_str;
13442 char_u *fromstr;
13443 char_u *tostr;
13444 char_u *p;
13445#ifdef FEAT_MBYTE
13446 int inlen;
13447 int fromlen;
13448 int tolen;
13449 int idx;
13450 char_u *cpstr;
13451 int cplen;
13452 int first = TRUE;
13453#endif
13454 char_u buf[NUMBUFLEN];
13455 char_u buf2[NUMBUFLEN];
13456 garray_T ga;
13457
13458 in_str = get_tv_string(&argvars[0]);
13459 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13460 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13461
13462 /* Default return value: empty string. */
13463 rettv->v_type = VAR_STRING;
13464 rettv->vval.v_string = NULL;
13465 if (fromstr == NULL || tostr == NULL)
13466 return; /* type error; errmsg already given */
13467 ga_init2(&ga, (int)sizeof(char), 80);
13468
13469#ifdef FEAT_MBYTE
13470 if (!has_mbyte)
13471#endif
13472 /* not multi-byte: fromstr and tostr must be the same length */
13473 if (STRLEN(fromstr) != STRLEN(tostr))
13474 {
13475#ifdef FEAT_MBYTE
13476error:
13477#endif
13478 EMSG2(_(e_invarg2), fromstr);
13479 ga_clear(&ga);
13480 return;
13481 }
13482
13483 /* fromstr and tostr have to contain the same number of chars */
13484 while (*in_str != NUL)
13485 {
13486#ifdef FEAT_MBYTE
13487 if (has_mbyte)
13488 {
13489 inlen = (*mb_ptr2len)(in_str);
13490 cpstr = in_str;
13491 cplen = inlen;
13492 idx = 0;
13493 for (p = fromstr; *p != NUL; p += fromlen)
13494 {
13495 fromlen = (*mb_ptr2len)(p);
13496 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13497 {
13498 for (p = tostr; *p != NUL; p += tolen)
13499 {
13500 tolen = (*mb_ptr2len)(p);
13501 if (idx-- == 0)
13502 {
13503 cplen = tolen;
13504 cpstr = p;
13505 break;
13506 }
13507 }
13508 if (*p == NUL) /* tostr is shorter than fromstr */
13509 goto error;
13510 break;
13511 }
13512 ++idx;
13513 }
13514
13515 if (first && cpstr == in_str)
13516 {
13517 /* Check that fromstr and tostr have the same number of
13518 * (multi-byte) characters. Done only once when a character
13519 * of in_str doesn't appear in fromstr. */
13520 first = FALSE;
13521 for (p = tostr; *p != NUL; p += tolen)
13522 {
13523 tolen = (*mb_ptr2len)(p);
13524 --idx;
13525 }
13526 if (idx != 0)
13527 goto error;
13528 }
13529
13530 (void)ga_grow(&ga, cplen);
13531 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13532 ga.ga_len += cplen;
13533
13534 in_str += inlen;
13535 }
13536 else
13537#endif
13538 {
13539 /* When not using multi-byte chars we can do it faster. */
13540 p = vim_strchr(fromstr, *in_str);
13541 if (p != NULL)
13542 ga_append(&ga, tostr[p - fromstr]);
13543 else
13544 ga_append(&ga, *in_str);
13545 ++in_str;
13546 }
13547 }
13548
13549 /* add a terminating NUL */
13550 (void)ga_grow(&ga, 1);
13551 ga_append(&ga, NUL);
13552
13553 rettv->vval.v_string = ga.ga_data;
13554}
13555
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013556/*
13557 * "trim({expr})" function
13558 */
13559 static void
13560f_trim(typval_T *argvars, typval_T *rettv)
13561{
13562 char_u buf1[NUMBUFLEN];
13563 char_u buf2[NUMBUFLEN];
13564 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13565 char_u *mask = NULL;
13566 char_u *tail;
13567 char_u *prev;
13568 char_u *p;
13569 int c1;
13570
13571 rettv->v_type = VAR_STRING;
13572 if (head == NULL)
13573 {
13574 rettv->vval.v_string = NULL;
13575 return;
13576 }
13577
13578 if (argvars[1].v_type == VAR_STRING)
13579 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13580
13581 while (*head != NUL)
13582 {
13583 c1 = PTR2CHAR(head);
13584 if (mask == NULL)
13585 {
13586 if (c1 > ' ' && c1 != 0xa0)
13587 break;
13588 }
13589 else
13590 {
13591 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13592 if (c1 == PTR2CHAR(p))
13593 break;
13594 if (*p == NUL)
13595 break;
13596 }
13597 MB_PTR_ADV(head);
13598 }
13599
13600 for (tail = head + STRLEN(head); tail > head; tail = prev)
13601 {
13602 prev = tail;
13603 MB_PTR_BACK(head, prev);
13604 c1 = PTR2CHAR(prev);
13605 if (mask == NULL)
13606 {
13607 if (c1 > ' ' && c1 != 0xa0)
13608 break;
13609 }
13610 else
13611 {
13612 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13613 if (c1 == PTR2CHAR(p))
13614 break;
13615 if (*p == NUL)
13616 break;
13617 }
13618 }
13619 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13620}
13621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622#ifdef FEAT_FLOAT
13623/*
13624 * "trunc({float})" function
13625 */
13626 static void
13627f_trunc(typval_T *argvars, typval_T *rettv)
13628{
13629 float_T f = 0.0;
13630
13631 rettv->v_type = VAR_FLOAT;
13632 if (get_float_arg(argvars, &f) == OK)
13633 /* trunc() is not in C90, use floor() or ceil() instead. */
13634 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13635 else
13636 rettv->vval.v_float = 0.0;
13637}
13638#endif
13639
13640/*
13641 * "type(expr)" function
13642 */
13643 static void
13644f_type(typval_T *argvars, typval_T *rettv)
13645{
13646 int n = -1;
13647
13648 switch (argvars[0].v_type)
13649 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013650 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13651 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013652 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013653 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13654 case VAR_LIST: n = VAR_TYPE_LIST; break;
13655 case VAR_DICT: n = VAR_TYPE_DICT; break;
13656 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657 case VAR_SPECIAL:
13658 if (argvars[0].vval.v_number == VVAL_FALSE
13659 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013660 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013661 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013662 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013664 case VAR_JOB: n = VAR_TYPE_JOB; break;
13665 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013666 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013667 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013668 n = -1;
13669 break;
13670 }
13671 rettv->vval.v_number = n;
13672}
13673
13674/*
13675 * "undofile(name)" function
13676 */
13677 static void
13678f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13679{
13680 rettv->v_type = VAR_STRING;
13681#ifdef FEAT_PERSISTENT_UNDO
13682 {
13683 char_u *fname = get_tv_string(&argvars[0]);
13684
13685 if (*fname == NUL)
13686 {
13687 /* If there is no file name there will be no undo file. */
13688 rettv->vval.v_string = NULL;
13689 }
13690 else
13691 {
13692 char_u *ffname = FullName_save(fname, FALSE);
13693
13694 if (ffname != NULL)
13695 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13696 vim_free(ffname);
13697 }
13698 }
13699#else
13700 rettv->vval.v_string = NULL;
13701#endif
13702}
13703
13704/*
13705 * "undotree()" function
13706 */
13707 static void
13708f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13709{
13710 if (rettv_dict_alloc(rettv) == OK)
13711 {
13712 dict_T *dict = rettv->vval.v_dict;
13713 list_T *list;
13714
Bram Moolenaare0be1672018-07-08 16:50:37 +020013715 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13716 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13717 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13718 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13719 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13720 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013721
13722 list = list_alloc();
13723 if (list != NULL)
13724 {
13725 u_eval_tree(curbuf->b_u_oldhead, list);
13726 dict_add_list(dict, "entries", list);
13727 }
13728 }
13729}
13730
13731/*
13732 * "values(dict)" function
13733 */
13734 static void
13735f_values(typval_T *argvars, typval_T *rettv)
13736{
13737 dict_list(argvars, rettv, 1);
13738}
13739
13740/*
13741 * "virtcol(string)" function
13742 */
13743 static void
13744f_virtcol(typval_T *argvars, typval_T *rettv)
13745{
13746 colnr_T vcol = 0;
13747 pos_T *fp;
13748 int fnum = curbuf->b_fnum;
13749
13750 fp = var2fpos(&argvars[0], FALSE, &fnum);
13751 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13752 && fnum == curbuf->b_fnum)
13753 {
13754 getvvcol(curwin, fp, NULL, NULL, &vcol);
13755 ++vcol;
13756 }
13757
13758 rettv->vval.v_number = vcol;
13759}
13760
13761/*
13762 * "visualmode()" function
13763 */
13764 static void
13765f_visualmode(typval_T *argvars, typval_T *rettv)
13766{
13767 char_u str[2];
13768
13769 rettv->v_type = VAR_STRING;
13770 str[0] = curbuf->b_visual_mode_eval;
13771 str[1] = NUL;
13772 rettv->vval.v_string = vim_strsave(str);
13773
13774 /* A non-zero number or non-empty string argument: reset mode. */
13775 if (non_zero_arg(&argvars[0]))
13776 curbuf->b_visual_mode_eval = NUL;
13777}
13778
13779/*
13780 * "wildmenumode()" function
13781 */
13782 static void
13783f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13784{
13785#ifdef FEAT_WILDMENU
13786 if (wild_menu_showing)
13787 rettv->vval.v_number = 1;
13788#endif
13789}
13790
13791/*
13792 * "winbufnr(nr)" function
13793 */
13794 static void
13795f_winbufnr(typval_T *argvars, typval_T *rettv)
13796{
13797 win_T *wp;
13798
13799 wp = find_win_by_nr(&argvars[0], NULL);
13800 if (wp == NULL)
13801 rettv->vval.v_number = -1;
13802 else
13803 rettv->vval.v_number = wp->w_buffer->b_fnum;
13804}
13805
13806/*
13807 * "wincol()" function
13808 */
13809 static void
13810f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13811{
13812 validate_cursor();
13813 rettv->vval.v_number = curwin->w_wcol + 1;
13814}
13815
13816/*
13817 * "winheight(nr)" function
13818 */
13819 static void
13820f_winheight(typval_T *argvars, typval_T *rettv)
13821{
13822 win_T *wp;
13823
13824 wp = find_win_by_nr(&argvars[0], NULL);
13825 if (wp == NULL)
13826 rettv->vval.v_number = -1;
13827 else
13828 rettv->vval.v_number = wp->w_height;
13829}
13830
13831/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013832 * "winlayout()" function
13833 */
13834 static void
13835f_winlayout(typval_T *argvars, typval_T *rettv)
13836{
13837 tabpage_T *tp;
13838
13839 if (rettv_list_alloc(rettv) != OK)
13840 return;
13841
13842 if (argvars[0].v_type == VAR_UNKNOWN)
13843 tp = curtab;
13844 else
13845 {
13846 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13847 if (tp == NULL)
13848 return;
13849 }
13850
13851 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13852}
13853
13854/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013855 * "winline()" function
13856 */
13857 static void
13858f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13859{
13860 validate_cursor();
13861 rettv->vval.v_number = curwin->w_wrow + 1;
13862}
13863
13864/*
13865 * "winnr()" function
13866 */
13867 static void
13868f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13869{
13870 int nr = 1;
13871
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013873 rettv->vval.v_number = nr;
13874}
13875
13876/*
13877 * "winrestcmd()" function
13878 */
13879 static void
13880f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13881{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013882 win_T *wp;
13883 int winnr = 1;
13884 garray_T ga;
13885 char_u buf[50];
13886
13887 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013888 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013889 {
13890 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13891 ga_concat(&ga, buf);
13892 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13893 ga_concat(&ga, buf);
13894 ++winnr;
13895 }
13896 ga_append(&ga, NUL);
13897
13898 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013899 rettv->v_type = VAR_STRING;
13900}
13901
13902/*
13903 * "winrestview()" function
13904 */
13905 static void
13906f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13907{
13908 dict_T *dict;
13909
13910 if (argvars[0].v_type != VAR_DICT
13911 || (dict = argvars[0].vval.v_dict) == NULL)
13912 EMSG(_(e_invarg));
13913 else
13914 {
13915 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13916 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13917 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13918 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13919#ifdef FEAT_VIRTUALEDIT
13920 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13921 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13922#endif
13923 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13924 {
13925 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13926 curwin->w_set_curswant = FALSE;
13927 }
13928
13929 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13930 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13931#ifdef FEAT_DIFF
13932 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13933 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13934#endif
13935 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13936 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13937 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13938 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13939
13940 check_cursor();
13941 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013942 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943 changed_window_setting();
13944
13945 if (curwin->w_topline <= 0)
13946 curwin->w_topline = 1;
13947 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13948 curwin->w_topline = curbuf->b_ml.ml_line_count;
13949#ifdef FEAT_DIFF
13950 check_topfill(curwin, TRUE);
13951#endif
13952 }
13953}
13954
13955/*
13956 * "winsaveview()" function
13957 */
13958 static void
13959f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13960{
13961 dict_T *dict;
13962
13963 if (rettv_dict_alloc(rettv) == FAIL)
13964 return;
13965 dict = rettv->vval.v_dict;
13966
Bram Moolenaare0be1672018-07-08 16:50:37 +020013967 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13968 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013969#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013970 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971#endif
13972 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013973 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974
Bram Moolenaare0be1672018-07-08 16:50:37 +020013975 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013976#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013977 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013979 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13980 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013981}
13982
13983/*
13984 * "winwidth(nr)" function
13985 */
13986 static void
13987f_winwidth(typval_T *argvars, typval_T *rettv)
13988{
13989 win_T *wp;
13990
13991 wp = find_win_by_nr(&argvars[0], NULL);
13992 if (wp == NULL)
13993 rettv->vval.v_number = -1;
13994 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013995 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996}
13997
13998/*
13999 * "wordcount()" function
14000 */
14001 static void
14002f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14003{
14004 if (rettv_dict_alloc(rettv) == FAIL)
14005 return;
14006 cursor_pos_info(rettv->vval.v_dict);
14007}
14008
14009/*
14010 * "writefile()" function
14011 */
14012 static void
14013f_writefile(typval_T *argvars, typval_T *rettv)
14014{
14015 int binary = FALSE;
14016 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014017#ifdef HAVE_FSYNC
14018 int do_fsync = p_fs;
14019#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014020 char_u *fname;
14021 FILE *fd;
14022 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014023 listitem_T *li;
14024 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014025
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014026 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014027 if (check_restricted() || check_secure())
14028 return;
14029
14030 if (argvars[0].v_type != VAR_LIST)
14031 {
14032 EMSG2(_(e_listarg), "writefile()");
14033 return;
14034 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014035 list = argvars[0].vval.v_list;
14036 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014037 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014038 for (li = list->lv_first; li != NULL; li = li->li_next)
14039 if (get_tv_string_chk(&li->li_tv) == NULL)
14040 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014041
14042 if (argvars[2].v_type != VAR_UNKNOWN)
14043 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014044 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14045
14046 if (arg2 == NULL)
14047 return;
14048 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014050 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014051 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014052#ifdef HAVE_FSYNC
14053 if (vim_strchr(arg2, 's') != NULL)
14054 do_fsync = TRUE;
14055 else if (vim_strchr(arg2, 'S') != NULL)
14056 do_fsync = FALSE;
14057#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058 }
14059
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014060 fname = get_tv_string_chk(&argvars[1]);
14061 if (fname == NULL)
14062 return;
14063
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014064 /* Always open the file in binary mode, library functions have a mind of
14065 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014066 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14067 append ? APPENDBIN : WRITEBIN)) == NULL)
14068 {
14069 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14070 ret = -1;
14071 }
14072 else
14073 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014074 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014076#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014077 else if (do_fsync)
14078 /* Ignore the error, the user wouldn't know what to do about it.
14079 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014080 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014081#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014082 fclose(fd);
14083 }
14084
14085 rettv->vval.v_number = ret;
14086}
14087
14088/*
14089 * "xor(expr, expr)" function
14090 */
14091 static void
14092f_xor(typval_T *argvars, typval_T *rettv)
14093{
14094 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14095 ^ get_tv_number_chk(&argvars[1], NULL);
14096}
14097
14098
14099#endif /* FEAT_EVAL */