blob: 9faa89ff4eb309bc0c7c9c7fc7367e09acff873d [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 Moolenaar110bd602018-09-16 18:46:59 +0200402static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200403static void f_synID(typval_T *argvars, typval_T *rettv);
404static void f_synIDattr(typval_T *argvars, typval_T *rettv);
405static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
406static void f_synstack(typval_T *argvars, typval_T *rettv);
407static void f_synconcealed(typval_T *argvars, typval_T *rettv);
408static void f_system(typval_T *argvars, typval_T *rettv);
409static void f_systemlist(typval_T *argvars, typval_T *rettv);
410static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
411static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
412static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
413static void f_taglist(typval_T *argvars, typval_T *rettv);
414static void f_tagfiles(typval_T *argvars, typval_T *rettv);
415static void f_tempname(typval_T *argvars, typval_T *rettv);
416static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
417static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200418static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200419static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100420static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200421static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100422static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200423#ifdef FEAT_JOB_CHANNEL
424static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
425#endif
426static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
427#ifdef FEAT_JOB_CHANNEL
428static void f_test_null_job(typval_T *argvars, typval_T *rettv);
429#endif
430static void f_test_null_list(typval_T *argvars, typval_T *rettv);
431static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
432static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200433#ifdef FEAT_GUI
434static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
435#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200436static void f_test_settime(typval_T *argvars, typval_T *rettv);
437#ifdef FEAT_FLOAT
438static void f_tan(typval_T *argvars, typval_T *rettv);
439static void f_tanh(typval_T *argvars, typval_T *rettv);
440#endif
441#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200442static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200443static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200444static void f_timer_start(typval_T *argvars, typval_T *rettv);
445static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200446static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447#endif
448static void f_tolower(typval_T *argvars, typval_T *rettv);
449static void f_toupper(typval_T *argvars, typval_T *rettv);
450static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100451static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_FLOAT
453static void f_trunc(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_type(typval_T *argvars, typval_T *rettv);
456static void f_undofile(typval_T *argvars, typval_T *rettv);
457static void f_undotree(typval_T *argvars, typval_T *rettv);
458static void f_uniq(typval_T *argvars, typval_T *rettv);
459static void f_values(typval_T *argvars, typval_T *rettv);
460static void f_virtcol(typval_T *argvars, typval_T *rettv);
461static void f_visualmode(typval_T *argvars, typval_T *rettv);
462static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
463static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
464static void f_win_getid(typval_T *argvars, typval_T *rettv);
465static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
466static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
467static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100468static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_winbufnr(typval_T *argvars, typval_T *rettv);
470static void f_wincol(typval_T *argvars, typval_T *rettv);
471static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200472static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200473static void f_winline(typval_T *argvars, typval_T *rettv);
474static void f_winnr(typval_T *argvars, typval_T *rettv);
475static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
476static void f_winrestview(typval_T *argvars, typval_T *rettv);
477static void f_winsaveview(typval_T *argvars, typval_T *rettv);
478static void f_winwidth(typval_T *argvars, typval_T *rettv);
479static void f_writefile(typval_T *argvars, typval_T *rettv);
480static void f_wordcount(typval_T *argvars, typval_T *rettv);
481static void f_xor(typval_T *argvars, typval_T *rettv);
482
483/*
484 * Array with names and number of arguments of all internal functions
485 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
486 */
487static struct fst
488{
489 char *f_name; /* function name */
490 char f_min_argc; /* minimal number of arguments */
491 char f_max_argc; /* maximal number of arguments */
492 void (*f_func)(typval_T *args, typval_T *rvar);
493 /* implementation of function */
494} functions[] =
495{
496#ifdef FEAT_FLOAT
497 {"abs", 1, 1, f_abs},
498 {"acos", 1, 1, f_acos}, /* WJMc */
499#endif
500 {"add", 2, 2, f_add},
501 {"and", 2, 2, f_and},
502 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200503 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200504 {"argc", 0, 0, f_argc},
505 {"argidx", 0, 0, f_argidx},
506 {"arglistid", 0, 2, f_arglistid},
507 {"argv", 0, 1, f_argv},
508#ifdef FEAT_FLOAT
509 {"asin", 1, 1, f_asin}, /* WJMc */
510#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100511 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200512 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100513 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200514 {"assert_exception", 1, 2, f_assert_exception},
515 {"assert_fails", 1, 2, f_assert_fails},
516 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100517 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200518 {"assert_match", 2, 3, f_assert_match},
519 {"assert_notequal", 2, 3, f_assert_notequal},
520 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100521 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200522 {"assert_true", 1, 2, f_assert_true},
523#ifdef FEAT_FLOAT
524 {"atan", 1, 1, f_atan},
525 {"atan2", 2, 2, f_atan2},
526#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100527#ifdef FEAT_BEVAL
528 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100529# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100530 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100531# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100532#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"browse", 4, 4, f_browse},
534 {"browsedir", 2, 2, f_browsedir},
535 {"bufexists", 1, 1, f_bufexists},
536 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
537 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
538 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
539 {"buflisted", 1, 1, f_buflisted},
540 {"bufloaded", 1, 1, f_bufloaded},
541 {"bufname", 1, 1, f_bufname},
542 {"bufnr", 1, 2, f_bufnr},
543 {"bufwinid", 1, 1, f_bufwinid},
544 {"bufwinnr", 1, 1, f_bufwinnr},
545 {"byte2line", 1, 1, f_byte2line},
546 {"byteidx", 2, 2, f_byteidx},
547 {"byteidxcomp", 2, 2, f_byteidxcomp},
548 {"call", 2, 3, f_call},
549#ifdef FEAT_FLOAT
550 {"ceil", 1, 1, f_ceil},
551#endif
552#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100553 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200555 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
557 {"ch_evalraw", 2, 3, f_ch_evalraw},
558 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
559 {"ch_getjob", 1, 1, f_ch_getjob},
560 {"ch_info", 1, 1, f_ch_info},
561 {"ch_log", 1, 2, f_ch_log},
562 {"ch_logfile", 1, 2, f_ch_logfile},
563 {"ch_open", 1, 2, f_ch_open},
564 {"ch_read", 1, 2, f_ch_read},
565 {"ch_readraw", 1, 2, f_ch_readraw},
566 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
567 {"ch_sendraw", 2, 3, f_ch_sendraw},
568 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200569 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200570#endif
571 {"changenr", 0, 0, f_changenr},
572 {"char2nr", 1, 2, f_char2nr},
573 {"cindent", 1, 1, f_cindent},
574 {"clearmatches", 0, 0, f_clearmatches},
575 {"col", 1, 1, f_col},
576#if defined(FEAT_INS_EXPAND)
577 {"complete", 2, 2, f_complete},
578 {"complete_add", 1, 1, f_complete_add},
579 {"complete_check", 0, 0, f_complete_check},
580#endif
581 {"confirm", 1, 4, f_confirm},
582 {"copy", 1, 1, f_copy},
583#ifdef FEAT_FLOAT
584 {"cos", 1, 1, f_cos},
585 {"cosh", 1, 1, f_cosh},
586#endif
587 {"count", 2, 4, f_count},
588 {"cscope_connection",0,3, f_cscope_connection},
589 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200590#ifdef WIN3264
591 {"debugbreak", 1, 1, f_debugbreak},
592#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200593 {"deepcopy", 1, 2, f_deepcopy},
594 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200595 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200596 {"did_filetype", 0, 0, f_did_filetype},
597 {"diff_filler", 1, 1, f_diff_filler},
598 {"diff_hlID", 2, 2, f_diff_hlID},
599 {"empty", 1, 1, f_empty},
600 {"escape", 2, 2, f_escape},
601 {"eval", 1, 1, f_eval},
602 {"eventhandler", 0, 0, f_eventhandler},
603 {"executable", 1, 1, f_executable},
604 {"execute", 1, 2, f_execute},
605 {"exepath", 1, 1, f_exepath},
606 {"exists", 1, 1, f_exists},
607#ifdef FEAT_FLOAT
608 {"exp", 1, 1, f_exp},
609#endif
610 {"expand", 1, 3, f_expand},
611 {"extend", 2, 3, f_extend},
612 {"feedkeys", 1, 2, f_feedkeys},
613 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
614 {"filereadable", 1, 1, f_filereadable},
615 {"filewritable", 1, 1, f_filewritable},
616 {"filter", 2, 2, f_filter},
617 {"finddir", 1, 3, f_finddir},
618 {"findfile", 1, 3, f_findfile},
619#ifdef FEAT_FLOAT
620 {"float2nr", 1, 1, f_float2nr},
621 {"floor", 1, 1, f_floor},
622 {"fmod", 2, 2, f_fmod},
623#endif
624 {"fnameescape", 1, 1, f_fnameescape},
625 {"fnamemodify", 2, 2, f_fnamemodify},
626 {"foldclosed", 1, 1, f_foldclosed},
627 {"foldclosedend", 1, 1, f_foldclosedend},
628 {"foldlevel", 1, 1, f_foldlevel},
629 {"foldtext", 0, 0, f_foldtext},
630 {"foldtextresult", 1, 1, f_foldtextresult},
631 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200632 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"function", 1, 3, f_function},
634 {"garbagecollect", 0, 1, f_garbagecollect},
635 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200636 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"getbufline", 2, 3, f_getbufline},
638 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100639 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200640 {"getchar", 0, 1, f_getchar},
641 {"getcharmod", 0, 0, f_getcharmod},
642 {"getcharsearch", 0, 0, f_getcharsearch},
643 {"getcmdline", 0, 0, f_getcmdline},
644 {"getcmdpos", 0, 0, f_getcmdpos},
645 {"getcmdtype", 0, 0, f_getcmdtype},
646 {"getcmdwintype", 0, 0, f_getcmdwintype},
647#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200648 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649#endif
650 {"getcurpos", 0, 0, f_getcurpos},
651 {"getcwd", 0, 2, f_getcwd},
652 {"getfontname", 0, 1, f_getfontname},
653 {"getfperm", 1, 1, f_getfperm},
654 {"getfsize", 1, 1, f_getfsize},
655 {"getftime", 1, 1, f_getftime},
656 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100657 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200658 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200659 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200660 {"getmatches", 0, 0, f_getmatches},
661 {"getpid", 0, 0, f_getpid},
662 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200663 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200664 {"getreg", 0, 3, f_getreg},
665 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200666 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200667 {"gettabvar", 2, 3, f_gettabvar},
668 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200669 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100670 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"getwinposx", 0, 0, f_getwinposx},
672 {"getwinposy", 0, 0, f_getwinposy},
673 {"getwinvar", 2, 3, f_getwinvar},
674 {"glob", 1, 4, f_glob},
675 {"glob2regpat", 1, 1, f_glob2regpat},
676 {"globpath", 2, 5, f_globpath},
677 {"has", 1, 1, f_has},
678 {"has_key", 2, 2, f_has_key},
679 {"haslocaldir", 0, 2, f_haslocaldir},
680 {"hasmapto", 1, 3, f_hasmapto},
681 {"highlightID", 1, 1, f_hlID}, /* obsolete */
682 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
683 {"histadd", 2, 2, f_histadd},
684 {"histdel", 1, 2, f_histdel},
685 {"histget", 1, 2, f_histget},
686 {"histnr", 1, 1, f_histnr},
687 {"hlID", 1, 1, f_hlID},
688 {"hlexists", 1, 1, f_hlexists},
689 {"hostname", 0, 0, f_hostname},
690 {"iconv", 3, 3, f_iconv},
691 {"indent", 1, 1, f_indent},
692 {"index", 2, 4, f_index},
693 {"input", 1, 3, f_input},
694 {"inputdialog", 1, 3, f_inputdialog},
695 {"inputlist", 1, 1, f_inputlist},
696 {"inputrestore", 0, 0, f_inputrestore},
697 {"inputsave", 0, 0, f_inputsave},
698 {"inputsecret", 1, 2, f_inputsecret},
699 {"insert", 2, 3, f_insert},
700 {"invert", 1, 1, f_invert},
701 {"isdirectory", 1, 1, f_isdirectory},
702 {"islocked", 1, 1, f_islocked},
703#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
704 {"isnan", 1, 1, f_isnan},
705#endif
706 {"items", 1, 1, f_items},
707#ifdef FEAT_JOB_CHANNEL
708 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200709 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"job_setoptions", 2, 2, f_job_setoptions},
711 {"job_start", 1, 2, f_job_start},
712 {"job_status", 1, 1, f_job_status},
713 {"job_stop", 1, 2, f_job_stop},
714#endif
715 {"join", 1, 2, f_join},
716 {"js_decode", 1, 1, f_js_decode},
717 {"js_encode", 1, 1, f_js_encode},
718 {"json_decode", 1, 1, f_json_decode},
719 {"json_encode", 1, 1, f_json_encode},
720 {"keys", 1, 1, f_keys},
721 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
722 {"len", 1, 1, f_len},
723 {"libcall", 3, 3, f_libcall},
724 {"libcallnr", 3, 3, f_libcallnr},
725 {"line", 1, 1, f_line},
726 {"line2byte", 1, 1, f_line2byte},
727 {"lispindent", 1, 1, f_lispindent},
728 {"localtime", 0, 0, f_localtime},
729#ifdef FEAT_FLOAT
730 {"log", 1, 1, f_log},
731 {"log10", 1, 1, f_log10},
732#endif
733#ifdef FEAT_LUA
734 {"luaeval", 1, 2, f_luaeval},
735#endif
736 {"map", 2, 2, f_map},
737 {"maparg", 1, 4, f_maparg},
738 {"mapcheck", 1, 3, f_mapcheck},
739 {"match", 2, 4, f_match},
740 {"matchadd", 2, 5, f_matchadd},
741 {"matchaddpos", 2, 5, f_matchaddpos},
742 {"matcharg", 1, 1, f_matcharg},
743 {"matchdelete", 1, 1, f_matchdelete},
744 {"matchend", 2, 4, f_matchend},
745 {"matchlist", 2, 4, f_matchlist},
746 {"matchstr", 2, 4, f_matchstr},
747 {"matchstrpos", 2, 4, f_matchstrpos},
748 {"max", 1, 1, f_max},
749 {"min", 1, 1, f_min},
750#ifdef vim_mkdir
751 {"mkdir", 1, 3, f_mkdir},
752#endif
753 {"mode", 0, 1, f_mode},
754#ifdef FEAT_MZSCHEME
755 {"mzeval", 1, 1, f_mzeval},
756#endif
757 {"nextnonblank", 1, 1, f_nextnonblank},
758 {"nr2char", 1, 2, f_nr2char},
759 {"or", 2, 2, f_or},
760 {"pathshorten", 1, 1, f_pathshorten},
761#ifdef FEAT_PERL
762 {"perleval", 1, 1, f_perleval},
763#endif
764#ifdef FEAT_FLOAT
765 {"pow", 2, 2, f_pow},
766#endif
767 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100768 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200769#ifdef FEAT_JOB_CHANNEL
770 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200771 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200772 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
773#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"pumvisible", 0, 0, f_pumvisible},
775#ifdef FEAT_PYTHON3
776 {"py3eval", 1, 1, f_py3eval},
777#endif
778#ifdef FEAT_PYTHON
779 {"pyeval", 1, 1, f_pyeval},
780#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100781#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
782 {"pyxeval", 1, 1, f_pyxeval},
783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200784 {"range", 1, 3, f_range},
785 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200786 {"reg_executing", 0, 0, f_reg_executing},
787 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200788 {"reltime", 0, 2, f_reltime},
789#ifdef FEAT_FLOAT
790 {"reltimefloat", 1, 1, f_reltimefloat},
791#endif
792 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100793 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200794 {"remote_foreground", 1, 1, f_remote_foreground},
795 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100796 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200797 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100798 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200799 {"remove", 2, 3, f_remove},
800 {"rename", 2, 2, f_rename},
801 {"repeat", 2, 2, f_repeat},
802 {"resolve", 1, 1, f_resolve},
803 {"reverse", 1, 1, f_reverse},
804#ifdef FEAT_FLOAT
805 {"round", 1, 1, f_round},
806#endif
807 {"screenattr", 2, 2, f_screenattr},
808 {"screenchar", 2, 2, f_screenchar},
809 {"screencol", 0, 0, f_screencol},
810 {"screenrow", 0, 0, f_screenrow},
811 {"search", 1, 4, f_search},
812 {"searchdecl", 1, 3, f_searchdecl},
813 {"searchpair", 3, 7, f_searchpair},
814 {"searchpairpos", 3, 7, f_searchpairpos},
815 {"searchpos", 1, 4, f_searchpos},
816 {"server2client", 2, 2, f_server2client},
817 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200818 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200819 {"setbufvar", 3, 3, f_setbufvar},
820 {"setcharsearch", 1, 1, f_setcharsearch},
821 {"setcmdpos", 1, 1, f_setcmdpos},
822 {"setfperm", 2, 2, f_setfperm},
823 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200824 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825 {"setmatches", 1, 1, f_setmatches},
826 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200827 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828 {"setreg", 2, 3, f_setreg},
829 {"settabvar", 3, 3, f_settabvar},
830 {"settabwinvar", 4, 4, f_settabwinvar},
831 {"setwinvar", 3, 3, f_setwinvar},
832#ifdef FEAT_CRYPT
833 {"sha256", 1, 1, f_sha256},
834#endif
835 {"shellescape", 1, 2, f_shellescape},
836 {"shiftwidth", 0, 0, f_shiftwidth},
837 {"simplify", 1, 1, f_simplify},
838#ifdef FEAT_FLOAT
839 {"sin", 1, 1, f_sin},
840 {"sinh", 1, 1, f_sinh},
841#endif
842 {"sort", 1, 3, f_sort},
843 {"soundfold", 1, 1, f_soundfold},
844 {"spellbadword", 0, 1, f_spellbadword},
845 {"spellsuggest", 1, 3, f_spellsuggest},
846 {"split", 1, 3, f_split},
847#ifdef FEAT_FLOAT
848 {"sqrt", 1, 1, f_sqrt},
849 {"str2float", 1, 1, f_str2float},
850#endif
851 {"str2nr", 1, 2, f_str2nr},
852 {"strcharpart", 2, 3, f_strcharpart},
853 {"strchars", 1, 2, f_strchars},
854 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
855#ifdef HAVE_STRFTIME
856 {"strftime", 1, 2, f_strftime},
857#endif
858 {"strgetchar", 2, 2, f_strgetchar},
859 {"stridx", 2, 3, f_stridx},
860 {"string", 1, 1, f_string},
861 {"strlen", 1, 1, f_strlen},
862 {"strpart", 2, 3, f_strpart},
863 {"strridx", 2, 3, f_strridx},
864 {"strtrans", 1, 1, f_strtrans},
865 {"strwidth", 1, 1, f_strwidth},
866 {"submatch", 1, 2, f_submatch},
867 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200868 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200869 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"synID", 3, 3, f_synID},
871 {"synIDattr", 2, 3, f_synIDattr},
872 {"synIDtrans", 1, 1, f_synIDtrans},
873 {"synconcealed", 2, 2, f_synconcealed},
874 {"synstack", 2, 2, f_synstack},
875 {"system", 1, 2, f_system},
876 {"systemlist", 1, 2, f_systemlist},
877 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
878 {"tabpagenr", 0, 1, f_tabpagenr},
879 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
880 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100881 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882#ifdef FEAT_FLOAT
883 {"tan", 1, 1, f_tan},
884 {"tanh", 1, 1, f_tanh},
885#endif
886 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200887#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100888 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
889 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100890 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200891 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200892# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
893 {"term_getansicolors", 1, 1, f_term_getansicolors},
894# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200895 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200896 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200897 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200898 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200899 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200900 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200901 {"term_getstatus", 1, 1, f_term_getstatus},
902 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200903 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200904 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200905 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200906 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200907# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
908 {"term_setansicolors", 2, 2, f_term_setansicolors},
909# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100910 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100911 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200912 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200914 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200915#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
917 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200918 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200919 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100920 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200921#ifdef FEAT_JOB_CHANNEL
922 {"test_null_channel", 0, 0, f_test_null_channel},
923#endif
924 {"test_null_dict", 0, 0, f_test_null_dict},
925#ifdef FEAT_JOB_CHANNEL
926 {"test_null_job", 0, 0, f_test_null_job},
927#endif
928 {"test_null_list", 0, 0, f_test_null_list},
929 {"test_null_partial", 0, 0, f_test_null_partial},
930 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200931 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100932 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200933#ifdef FEAT_GUI
934 {"test_scrollbar", 3, 3, f_test_scrollbar},
935#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200936 {"test_settime", 1, 1, f_test_settime},
937#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200938 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200939 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200940 {"timer_start", 2, 3, f_timer_start},
941 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200942 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943#endif
944 {"tolower", 1, 1, f_tolower},
945 {"toupper", 1, 1, f_toupper},
946 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100947 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200948#ifdef FEAT_FLOAT
949 {"trunc", 1, 1, f_trunc},
950#endif
951 {"type", 1, 1, f_type},
952 {"undofile", 1, 1, f_undofile},
953 {"undotree", 0, 0, f_undotree},
954 {"uniq", 1, 3, f_uniq},
955 {"values", 1, 1, f_values},
956 {"virtcol", 1, 1, f_virtcol},
957 {"visualmode", 0, 1, f_visualmode},
958 {"wildmenumode", 0, 0, f_wildmenumode},
959 {"win_findbuf", 1, 1, f_win_findbuf},
960 {"win_getid", 0, 2, f_win_getid},
961 {"win_gotoid", 1, 1, f_win_gotoid},
962 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
963 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100964 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965 {"winbufnr", 1, 1, f_winbufnr},
966 {"wincol", 0, 0, f_wincol},
967 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200968 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969 {"winline", 0, 0, f_winline},
970 {"winnr", 0, 1, f_winnr},
971 {"winrestcmd", 0, 0, f_winrestcmd},
972 {"winrestview", 1, 1, f_winrestview},
973 {"winsaveview", 0, 0, f_winsaveview},
974 {"winwidth", 1, 1, f_winwidth},
975 {"wordcount", 0, 0, f_wordcount},
976 {"writefile", 2, 3, f_writefile},
977 {"xor", 2, 2, f_xor},
978};
979
980#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
981
982/*
983 * Function given to ExpandGeneric() to obtain the list of internal
984 * or user defined function names.
985 */
986 char_u *
987get_function_name(expand_T *xp, int idx)
988{
989 static int intidx = -1;
990 char_u *name;
991
992 if (idx == 0)
993 intidx = -1;
994 if (intidx < 0)
995 {
996 name = get_user_func_name(xp, idx);
997 if (name != NULL)
998 return name;
999 }
1000 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1001 {
1002 STRCPY(IObuff, functions[intidx].f_name);
1003 STRCAT(IObuff, "(");
1004 if (functions[intidx].f_max_argc == 0)
1005 STRCAT(IObuff, ")");
1006 return IObuff;
1007 }
1008
1009 return NULL;
1010}
1011
1012/*
1013 * Function given to ExpandGeneric() to obtain the list of internal or
1014 * user defined variable or function names.
1015 */
1016 char_u *
1017get_expr_name(expand_T *xp, int idx)
1018{
1019 static int intidx = -1;
1020 char_u *name;
1021
1022 if (idx == 0)
1023 intidx = -1;
1024 if (intidx < 0)
1025 {
1026 name = get_function_name(xp, idx);
1027 if (name != NULL)
1028 return name;
1029 }
1030 return get_user_var_name(xp, ++intidx);
1031}
1032
1033#endif /* FEAT_CMDL_COMPL */
1034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001035/*
1036 * Find internal function in table above.
1037 * Return index, or -1 if not found
1038 */
1039 int
1040find_internal_func(
1041 char_u *name) /* name of the function */
1042{
1043 int first = 0;
1044 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1045 int cmp;
1046 int x;
1047
1048 /*
1049 * Find the function name in the table. Binary search.
1050 */
1051 while (first <= last)
1052 {
1053 x = first + ((unsigned)(last - first) >> 1);
1054 cmp = STRCMP(name, functions[x].f_name);
1055 if (cmp < 0)
1056 last = x - 1;
1057 else if (cmp > 0)
1058 first = x + 1;
1059 else
1060 return x;
1061 }
1062 return -1;
1063}
1064
1065 int
1066call_internal_func(
1067 char_u *name,
1068 int argcount,
1069 typval_T *argvars,
1070 typval_T *rettv)
1071{
1072 int i;
1073
1074 i = find_internal_func(name);
1075 if (i < 0)
1076 return ERROR_UNKNOWN;
1077 if (argcount < functions[i].f_min_argc)
1078 return ERROR_TOOFEW;
1079 if (argcount > functions[i].f_max_argc)
1080 return ERROR_TOOMANY;
1081 argvars[argcount].v_type = VAR_UNKNOWN;
1082 functions[i].f_func(argvars, rettv);
1083 return ERROR_NONE;
1084}
1085
1086/*
1087 * Return TRUE for a non-zero Number and a non-empty String.
1088 */
1089 static int
1090non_zero_arg(typval_T *argvars)
1091{
1092 return ((argvars[0].v_type == VAR_NUMBER
1093 && argvars[0].vval.v_number != 0)
1094 || (argvars[0].v_type == VAR_SPECIAL
1095 && argvars[0].vval.v_number == VVAL_TRUE)
1096 || (argvars[0].v_type == VAR_STRING
1097 && argvars[0].vval.v_string != NULL
1098 && *argvars[0].vval.v_string != NUL));
1099}
1100
1101/*
1102 * Get the lnum from the first argument.
1103 * Also accepts ".", "$", etc., but that only works for the current buffer.
1104 * Returns -1 on error.
1105 */
1106 static linenr_T
1107get_tv_lnum(typval_T *argvars)
1108{
1109 typval_T rettv;
1110 linenr_T lnum;
1111
1112 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1113 if (lnum == 0) /* no valid number, try using line() */
1114 {
1115 rettv.v_type = VAR_NUMBER;
1116 f_line(argvars, &rettv);
1117 lnum = (linenr_T)rettv.vval.v_number;
1118 clear_tv(&rettv);
1119 }
1120 return lnum;
1121}
1122
1123#ifdef FEAT_FLOAT
1124static int get_float_arg(typval_T *argvars, float_T *f);
1125
1126/*
1127 * Get the float value of "argvars[0]" into "f".
1128 * Returns FAIL when the argument is not a Number or Float.
1129 */
1130 static int
1131get_float_arg(typval_T *argvars, float_T *f)
1132{
1133 if (argvars[0].v_type == VAR_FLOAT)
1134 {
1135 *f = argvars[0].vval.v_float;
1136 return OK;
1137 }
1138 if (argvars[0].v_type == VAR_NUMBER)
1139 {
1140 *f = (float_T)argvars[0].vval.v_number;
1141 return OK;
1142 }
1143 EMSG(_("E808: Number or Float required"));
1144 return FAIL;
1145}
1146
1147/*
1148 * "abs(expr)" function
1149 */
1150 static void
1151f_abs(typval_T *argvars, typval_T *rettv)
1152{
1153 if (argvars[0].v_type == VAR_FLOAT)
1154 {
1155 rettv->v_type = VAR_FLOAT;
1156 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1157 }
1158 else
1159 {
1160 varnumber_T n;
1161 int error = FALSE;
1162
1163 n = get_tv_number_chk(&argvars[0], &error);
1164 if (error)
1165 rettv->vval.v_number = -1;
1166 else if (n > 0)
1167 rettv->vval.v_number = n;
1168 else
1169 rettv->vval.v_number = -n;
1170 }
1171}
1172
1173/*
1174 * "acos()" function
1175 */
1176 static void
1177f_acos(typval_T *argvars, typval_T *rettv)
1178{
1179 float_T f = 0.0;
1180
1181 rettv->v_type = VAR_FLOAT;
1182 if (get_float_arg(argvars, &f) == OK)
1183 rettv->vval.v_float = acos(f);
1184 else
1185 rettv->vval.v_float = 0.0;
1186}
1187#endif
1188
1189/*
1190 * "add(list, item)" function
1191 */
1192 static void
1193f_add(typval_T *argvars, typval_T *rettv)
1194{
1195 list_T *l;
1196
1197 rettv->vval.v_number = 1; /* Default: Failed */
1198 if (argvars[0].v_type == VAR_LIST)
1199 {
1200 if ((l = argvars[0].vval.v_list) != NULL
1201 && !tv_check_lock(l->lv_lock,
1202 (char_u *)N_("add() argument"), TRUE)
1203 && list_append_tv(l, &argvars[1]) == OK)
1204 copy_tv(&argvars[0], rettv);
1205 }
1206 else
1207 EMSG(_(e_listreq));
1208}
1209
1210/*
1211 * "and(expr, expr)" function
1212 */
1213 static void
1214f_and(typval_T *argvars, typval_T *rettv)
1215{
1216 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1217 & get_tv_number_chk(&argvars[1], NULL);
1218}
1219
1220/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001221 * Get the lnum from the first argument.
1222 * Also accepts "$", then "buf" is used.
1223 * Returns 0 on error.
1224 */
1225 static linenr_T
1226get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1227{
1228 if (argvars[0].v_type == VAR_STRING
1229 && argvars[0].vval.v_string != NULL
1230 && argvars[0].vval.v_string[0] == '$'
1231 && buf != NULL)
1232 return buf->b_ml.ml_line_count;
1233 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1234}
1235
1236/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001237 * If there is a window for "curbuf", make it the current window.
1238 */
1239 static void
1240find_win_for_curbuf(void)
1241{
1242 wininfo_T *wip;
1243
1244 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1245 {
1246 if (wip->wi_win != NULL)
1247 {
1248 curwin = wip->wi_win;
1249 break;
1250 }
1251 }
1252}
1253
1254/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001255 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001256 */
1257 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001258set_buffer_lines(
1259 buf_T *buf,
1260 linenr_T lnum_arg,
1261 int append,
1262 typval_T *lines,
1263 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264{
Bram Moolenaarca851592018-06-06 21:04:07 +02001265 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1266 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 list_T *l = NULL;
1268 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001270 linenr_T append_lnum;
1271 buf_T *curbuf_save = NULL;
1272 win_T *curwin_save = NULL;
1273 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274
Bram Moolenaarca851592018-06-06 21:04:07 +02001275 /* When using the current buffer ml_mfp will be set if needed. Useful when
1276 * setline() is used on startup. For other buffers the buffer must be
1277 * loaded. */
1278 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001279 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001280 rettv->vval.v_number = 1; /* FAIL */
1281 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001282 }
1283
Bram Moolenaarca851592018-06-06 21:04:07 +02001284 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001285 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001286 curbuf_save = curbuf;
1287 curwin_save = curwin;
1288 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001289 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001290 }
1291
1292 if (append)
1293 // appendbufline() uses the line number below which we insert
1294 append_lnum = lnum - 1;
1295 else
1296 // setbufline() uses the line number above which we insert, we only
1297 // append if it's below the last line
1298 append_lnum = curbuf->b_ml.ml_line_count;
1299
1300 if (lines->v_type == VAR_LIST)
1301 {
1302 l = lines->vval.v_list;
1303 li = l->lv_first;
1304 }
1305 else
1306 line = get_tv_string_chk(lines);
1307
1308 /* default result is zero == OK */
1309 for (;;)
1310 {
1311 if (l != NULL)
1312 {
1313 /* list argument, get next string */
1314 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001316 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 li = li->li_next;
1318 }
1319
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 rettv->vval.v_number = 1; /* FAIL */
1321 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1322 break;
1323
1324 /* When coming here from Insert mode, sync undo, so that this can be
1325 * undone separately from what was previously inserted. */
1326 if (u_sync_once == 2)
1327 {
1328 u_sync_once = 1; /* notify that u_sync() was called */
1329 u_sync(TRUE);
1330 }
1331
1332 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1333 {
1334 /* existing line, replace it */
1335 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1336 {
1337 changed_bytes(lnum, 0);
1338 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1339 check_cursor_col();
1340 rettv->vval.v_number = 0; /* OK */
1341 }
1342 }
1343 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1344 {
1345 /* append the line */
1346 ++added;
1347 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1348 rettv->vval.v_number = 0; /* OK */
1349 }
1350
1351 if (l == NULL) /* only one string argument */
1352 break;
1353 ++lnum;
1354 }
1355
1356 if (added > 0)
1357 {
1358 win_T *wp;
1359 tabpage_T *tp;
1360
1361 appended_lines_mark(append_lnum, added);
1362 FOR_ALL_TAB_WINDOWS(tp, wp)
1363 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1364 wp->w_cursor.lnum += added;
1365 check_cursor_col();
1366
Bram Moolenaarf2732452018-06-03 14:47:35 +02001367#ifdef FEAT_JOB_CHANNEL
1368 if (bt_prompt(curbuf) && (State & INSERT))
1369 // show the line with the prompt
1370 update_topline();
1371#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001373
1374 if (!is_curbuf)
1375 {
1376 curbuf = curbuf_save;
1377 curwin = curwin_save;
1378 }
1379}
1380
1381/*
1382 * "append(lnum, string/list)" function
1383 */
1384 static void
1385f_append(typval_T *argvars, typval_T *rettv)
1386{
1387 linenr_T lnum = get_tv_lnum(&argvars[0]);
1388
1389 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1390}
1391
1392/*
1393 * "appendbufline(buf, lnum, string/list)" function
1394 */
1395 static void
1396f_appendbufline(typval_T *argvars, typval_T *rettv)
1397{
1398 linenr_T lnum;
1399 buf_T *buf;
1400
1401 buf = get_buf_tv(&argvars[0], FALSE);
1402 if (buf == NULL)
1403 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001404 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001405 {
1406 lnum = get_tv_lnum_buf(&argvars[1], buf);
1407 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1408 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001409}
1410
1411/*
1412 * "argc()" function
1413 */
1414 static void
1415f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1416{
1417 rettv->vval.v_number = ARGCOUNT;
1418}
1419
1420/*
1421 * "argidx()" function
1422 */
1423 static void
1424f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1425{
1426 rettv->vval.v_number = curwin->w_arg_idx;
1427}
1428
1429/*
1430 * "arglistid()" function
1431 */
1432 static void
1433f_arglistid(typval_T *argvars, typval_T *rettv)
1434{
1435 win_T *wp;
1436
1437 rettv->vval.v_number = -1;
1438 wp = find_tabwin(&argvars[0], &argvars[1]);
1439 if (wp != NULL)
1440 rettv->vval.v_number = wp->w_alist->id;
1441}
1442
1443/*
1444 * "argv(nr)" function
1445 */
1446 static void
1447f_argv(typval_T *argvars, typval_T *rettv)
1448{
1449 int idx;
1450
1451 if (argvars[0].v_type != VAR_UNKNOWN)
1452 {
1453 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1454 if (idx >= 0 && idx < ARGCOUNT)
1455 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1456 else
1457 rettv->vval.v_string = NULL;
1458 rettv->v_type = VAR_STRING;
1459 }
1460 else if (rettv_list_alloc(rettv) == OK)
1461 for (idx = 0; idx < ARGCOUNT; ++idx)
1462 list_append_string(rettv->vval.v_list,
1463 alist_name(&ARGLIST[idx]), -1);
1464}
1465
1466/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001467 * "assert_beeps(cmd [, error])" function
1468 */
1469 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001470f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001471{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001472 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001473}
1474
1475/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001476 * "assert_equal(expected, actual[, msg])" function
1477 */
1478 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001479f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001481 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001482}
1483
1484/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001485 * "assert_equalfile(fname-one, fname-two)" function
1486 */
1487 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001488f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001489{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001490 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001491}
1492
1493/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001494 * "assert_notequal(expected, actual[, msg])" function
1495 */
1496 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001497f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001499 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001500}
1501
1502/*
1503 * "assert_exception(string[, msg])" function
1504 */
1505 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001506f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001508 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509}
1510
1511/*
1512 * "assert_fails(cmd [, error])" function
1513 */
1514 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001515f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001517 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001518}
1519
1520/*
1521 * "assert_false(actual[, msg])" function
1522 */
1523 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001524f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001526 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527}
1528
1529/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001530 * "assert_inrange(lower, upper[, msg])" function
1531 */
1532 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001533f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001534{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001535 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001536}
1537
1538/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539 * "assert_match(pattern, actual[, msg])" function
1540 */
1541 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001542f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001543{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001544 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545}
1546
1547/*
1548 * "assert_notmatch(pattern, actual[, msg])" function
1549 */
1550 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001553 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554}
1555
1556/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001557 * "assert_report(msg)" function
1558 */
1559 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001560f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001561{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001562 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001563}
1564
1565/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566 * "assert_true(actual[, msg])" function
1567 */
1568 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001569f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001571 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001572}
1573
1574#ifdef FEAT_FLOAT
1575/*
1576 * "asin()" function
1577 */
1578 static void
1579f_asin(typval_T *argvars, typval_T *rettv)
1580{
1581 float_T f = 0.0;
1582
1583 rettv->v_type = VAR_FLOAT;
1584 if (get_float_arg(argvars, &f) == OK)
1585 rettv->vval.v_float = asin(f);
1586 else
1587 rettv->vval.v_float = 0.0;
1588}
1589
1590/*
1591 * "atan()" function
1592 */
1593 static void
1594f_atan(typval_T *argvars, typval_T *rettv)
1595{
1596 float_T f = 0.0;
1597
1598 rettv->v_type = VAR_FLOAT;
1599 if (get_float_arg(argvars, &f) == OK)
1600 rettv->vval.v_float = atan(f);
1601 else
1602 rettv->vval.v_float = 0.0;
1603}
1604
1605/*
1606 * "atan2()" function
1607 */
1608 static void
1609f_atan2(typval_T *argvars, typval_T *rettv)
1610{
1611 float_T fx = 0.0, fy = 0.0;
1612
1613 rettv->v_type = VAR_FLOAT;
1614 if (get_float_arg(argvars, &fx) == OK
1615 && get_float_arg(&argvars[1], &fy) == OK)
1616 rettv->vval.v_float = atan2(fx, fy);
1617 else
1618 rettv->vval.v_float = 0.0;
1619}
1620#endif
1621
1622/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001623 * "balloon_show()" function
1624 */
1625#ifdef FEAT_BEVAL
1626 static void
1627f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1628{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001629 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001630 {
1631 if (argvars[0].v_type == VAR_LIST
1632# ifdef FEAT_GUI
1633 && !gui.in_use
1634# endif
1635 )
1636 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1637 else
1638 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1639 }
1640}
1641
Bram Moolenaar669a8282017-11-19 20:13:05 +01001642# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001643 static void
1644f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1645{
1646 if (rettv_list_alloc(rettv) == OK)
1647 {
1648 char_u *msg = get_tv_string_chk(&argvars[0]);
1649
1650 if (msg != NULL)
1651 {
1652 pumitem_T *array;
1653 int size = split_message(msg, &array);
1654 int i;
1655
1656 /* Skip the first and last item, they are always empty. */
1657 for (i = 1; i < size - 1; ++i)
1658 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001659 while (size > 0)
1660 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001661 vim_free(array);
1662 }
1663 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001664}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001665# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001666#endif
1667
1668/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669 * "browse(save, title, initdir, default)" function
1670 */
1671 static void
1672f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1673{
1674#ifdef FEAT_BROWSE
1675 int save;
1676 char_u *title;
1677 char_u *initdir;
1678 char_u *defname;
1679 char_u buf[NUMBUFLEN];
1680 char_u buf2[NUMBUFLEN];
1681 int error = FALSE;
1682
1683 save = (int)get_tv_number_chk(&argvars[0], &error);
1684 title = get_tv_string_chk(&argvars[1]);
1685 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1686 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1687
1688 if (error || title == NULL || initdir == NULL || defname == NULL)
1689 rettv->vval.v_string = NULL;
1690 else
1691 rettv->vval.v_string =
1692 do_browse(save ? BROWSE_SAVE : 0,
1693 title, defname, NULL, initdir, NULL, curbuf);
1694#else
1695 rettv->vval.v_string = NULL;
1696#endif
1697 rettv->v_type = VAR_STRING;
1698}
1699
1700/*
1701 * "browsedir(title, initdir)" function
1702 */
1703 static void
1704f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1705{
1706#ifdef FEAT_BROWSE
1707 char_u *title;
1708 char_u *initdir;
1709 char_u buf[NUMBUFLEN];
1710
1711 title = get_tv_string_chk(&argvars[0]);
1712 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1713
1714 if (title == NULL || initdir == NULL)
1715 rettv->vval.v_string = NULL;
1716 else
1717 rettv->vval.v_string = do_browse(BROWSE_DIR,
1718 title, NULL, NULL, initdir, NULL, curbuf);
1719#else
1720 rettv->vval.v_string = NULL;
1721#endif
1722 rettv->v_type = VAR_STRING;
1723}
1724
1725static buf_T *find_buffer(typval_T *avar);
1726
1727/*
1728 * Find a buffer by number or exact name.
1729 */
1730 static buf_T *
1731find_buffer(typval_T *avar)
1732{
1733 buf_T *buf = NULL;
1734
1735 if (avar->v_type == VAR_NUMBER)
1736 buf = buflist_findnr((int)avar->vval.v_number);
1737 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1738 {
1739 buf = buflist_findname_exp(avar->vval.v_string);
1740 if (buf == NULL)
1741 {
1742 /* No full path name match, try a match with a URL or a "nofile"
1743 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001744 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001745 if (buf->b_fname != NULL
1746 && (path_with_url(buf->b_fname)
1747#ifdef FEAT_QUICKFIX
1748 || bt_nofile(buf)
1749#endif
1750 )
1751 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1752 break;
1753 }
1754 }
1755 return buf;
1756}
1757
1758/*
1759 * "bufexists(expr)" function
1760 */
1761 static void
1762f_bufexists(typval_T *argvars, typval_T *rettv)
1763{
1764 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1765}
1766
1767/*
1768 * "buflisted(expr)" function
1769 */
1770 static void
1771f_buflisted(typval_T *argvars, typval_T *rettv)
1772{
1773 buf_T *buf;
1774
1775 buf = find_buffer(&argvars[0]);
1776 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1777}
1778
1779/*
1780 * "bufloaded(expr)" function
1781 */
1782 static void
1783f_bufloaded(typval_T *argvars, typval_T *rettv)
1784{
1785 buf_T *buf;
1786
1787 buf = find_buffer(&argvars[0]);
1788 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1789}
1790
1791 buf_T *
1792buflist_find_by_name(char_u *name, int curtab_only)
1793{
1794 int save_magic;
1795 char_u *save_cpo;
1796 buf_T *buf;
1797
1798 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1799 save_magic = p_magic;
1800 p_magic = TRUE;
1801 save_cpo = p_cpo;
1802 p_cpo = (char_u *)"";
1803
1804 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1805 TRUE, FALSE, curtab_only));
1806
1807 p_magic = save_magic;
1808 p_cpo = save_cpo;
1809 return buf;
1810}
1811
1812/*
1813 * Get buffer by number or pattern.
1814 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001815 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001816get_buf_tv(typval_T *tv, int curtab_only)
1817{
1818 char_u *name = tv->vval.v_string;
1819 buf_T *buf;
1820
1821 if (tv->v_type == VAR_NUMBER)
1822 return buflist_findnr((int)tv->vval.v_number);
1823 if (tv->v_type != VAR_STRING)
1824 return NULL;
1825 if (name == NULL || *name == NUL)
1826 return curbuf;
1827 if (name[0] == '$' && name[1] == NUL)
1828 return lastbuf;
1829
1830 buf = buflist_find_by_name(name, curtab_only);
1831
1832 /* If not found, try expanding the name, like done for bufexists(). */
1833 if (buf == NULL)
1834 buf = find_buffer(tv);
1835
1836 return buf;
1837}
1838
1839/*
1840 * "bufname(expr)" function
1841 */
1842 static void
1843f_bufname(typval_T *argvars, typval_T *rettv)
1844{
1845 buf_T *buf;
1846
1847 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1848 ++emsg_off;
1849 buf = get_buf_tv(&argvars[0], FALSE);
1850 rettv->v_type = VAR_STRING;
1851 if (buf != NULL && buf->b_fname != NULL)
1852 rettv->vval.v_string = vim_strsave(buf->b_fname);
1853 else
1854 rettv->vval.v_string = NULL;
1855 --emsg_off;
1856}
1857
1858/*
1859 * "bufnr(expr)" function
1860 */
1861 static void
1862f_bufnr(typval_T *argvars, typval_T *rettv)
1863{
1864 buf_T *buf;
1865 int error = FALSE;
1866 char_u *name;
1867
1868 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1869 ++emsg_off;
1870 buf = get_buf_tv(&argvars[0], FALSE);
1871 --emsg_off;
1872
1873 /* If the buffer isn't found and the second argument is not zero create a
1874 * new buffer. */
1875 if (buf == NULL
1876 && argvars[1].v_type != VAR_UNKNOWN
1877 && get_tv_number_chk(&argvars[1], &error) != 0
1878 && !error
1879 && (name = get_tv_string_chk(&argvars[0])) != NULL
1880 && !error)
1881 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1882
1883 if (buf != NULL)
1884 rettv->vval.v_number = buf->b_fnum;
1885 else
1886 rettv->vval.v_number = -1;
1887}
1888
1889 static void
1890buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1891{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892 win_T *wp;
1893 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001894 buf_T *buf;
1895
1896 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1897 ++emsg_off;
1898 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001899 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001900 {
1901 ++winnr;
1902 if (wp->w_buffer == buf)
1903 break;
1904 }
1905 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001906 --emsg_off;
1907}
1908
1909/*
1910 * "bufwinid(nr)" function
1911 */
1912 static void
1913f_bufwinid(typval_T *argvars, typval_T *rettv)
1914{
1915 buf_win_common(argvars, rettv, FALSE);
1916}
1917
1918/*
1919 * "bufwinnr(nr)" function
1920 */
1921 static void
1922f_bufwinnr(typval_T *argvars, typval_T *rettv)
1923{
1924 buf_win_common(argvars, rettv, TRUE);
1925}
1926
1927/*
1928 * "byte2line(byte)" function
1929 */
1930 static void
1931f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1932{
1933#ifndef FEAT_BYTEOFF
1934 rettv->vval.v_number = -1;
1935#else
1936 long boff = 0;
1937
1938 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1939 if (boff < 0)
1940 rettv->vval.v_number = -1;
1941 else
1942 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1943 (linenr_T)0, &boff);
1944#endif
1945}
1946
1947 static void
1948byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1949{
1950#ifdef FEAT_MBYTE
1951 char_u *t;
1952#endif
1953 char_u *str;
1954 varnumber_T idx;
1955
1956 str = get_tv_string_chk(&argvars[0]);
1957 idx = get_tv_number_chk(&argvars[1], NULL);
1958 rettv->vval.v_number = -1;
1959 if (str == NULL || idx < 0)
1960 return;
1961
1962#ifdef FEAT_MBYTE
1963 t = str;
1964 for ( ; idx > 0; idx--)
1965 {
1966 if (*t == NUL) /* EOL reached */
1967 return;
1968 if (enc_utf8 && comp)
1969 t += utf_ptr2len(t);
1970 else
1971 t += (*mb_ptr2len)(t);
1972 }
1973 rettv->vval.v_number = (varnumber_T)(t - str);
1974#else
1975 if ((size_t)idx <= STRLEN(str))
1976 rettv->vval.v_number = idx;
1977#endif
1978}
1979
1980/*
1981 * "byteidx()" function
1982 */
1983 static void
1984f_byteidx(typval_T *argvars, typval_T *rettv)
1985{
1986 byteidx(argvars, rettv, FALSE);
1987}
1988
1989/*
1990 * "byteidxcomp()" function
1991 */
1992 static void
1993f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1994{
1995 byteidx(argvars, rettv, TRUE);
1996}
1997
1998/*
1999 * "call(func, arglist [, dict])" function
2000 */
2001 static void
2002f_call(typval_T *argvars, typval_T *rettv)
2003{
2004 char_u *func;
2005 partial_T *partial = NULL;
2006 dict_T *selfdict = NULL;
2007
2008 if (argvars[1].v_type != VAR_LIST)
2009 {
2010 EMSG(_(e_listreq));
2011 return;
2012 }
2013 if (argvars[1].vval.v_list == NULL)
2014 return;
2015
2016 if (argvars[0].v_type == VAR_FUNC)
2017 func = argvars[0].vval.v_string;
2018 else if (argvars[0].v_type == VAR_PARTIAL)
2019 {
2020 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002021 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002022 }
2023 else
2024 func = get_tv_string(&argvars[0]);
2025 if (*func == NUL)
2026 return; /* type error or empty name */
2027
2028 if (argvars[2].v_type != VAR_UNKNOWN)
2029 {
2030 if (argvars[2].v_type != VAR_DICT)
2031 {
2032 EMSG(_(e_dictreq));
2033 return;
2034 }
2035 selfdict = argvars[2].vval.v_dict;
2036 }
2037
2038 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2039}
2040
2041#ifdef FEAT_FLOAT
2042/*
2043 * "ceil({float})" function
2044 */
2045 static void
2046f_ceil(typval_T *argvars, typval_T *rettv)
2047{
2048 float_T f = 0.0;
2049
2050 rettv->v_type = VAR_FLOAT;
2051 if (get_float_arg(argvars, &f) == OK)
2052 rettv->vval.v_float = ceil(f);
2053 else
2054 rettv->vval.v_float = 0.0;
2055}
2056#endif
2057
2058#ifdef FEAT_JOB_CHANNEL
2059/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002060 * "ch_canread()" function
2061 */
2062 static void
2063f_ch_canread(typval_T *argvars, typval_T *rettv)
2064{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002065 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002066
2067 rettv->vval.v_number = 0;
2068 if (channel != NULL)
2069 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2070 || channel_has_readahead(channel, PART_OUT)
2071 || channel_has_readahead(channel, PART_ERR);
2072}
2073
2074/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 * "ch_close()" function
2076 */
2077 static void
2078f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2079{
2080 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2081
2082 if (channel != NULL)
2083 {
2084 channel_close(channel, FALSE);
2085 channel_clear(channel);
2086 }
2087}
2088
2089/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002090 * "ch_close()" function
2091 */
2092 static void
2093f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2094{
2095 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2096
2097 if (channel != NULL)
2098 channel_close_in(channel);
2099}
2100
2101/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002102 * "ch_getbufnr()" function
2103 */
2104 static void
2105f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2106{
2107 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2108
2109 rettv->vval.v_number = -1;
2110 if (channel != NULL)
2111 {
2112 char_u *what = get_tv_string(&argvars[1]);
2113 int part;
2114
2115 if (STRCMP(what, "err") == 0)
2116 part = PART_ERR;
2117 else if (STRCMP(what, "out") == 0)
2118 part = PART_OUT;
2119 else if (STRCMP(what, "in") == 0)
2120 part = PART_IN;
2121 else
2122 part = PART_SOCK;
2123 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2124 rettv->vval.v_number =
2125 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2126 }
2127}
2128
2129/*
2130 * "ch_getjob()" function
2131 */
2132 static void
2133f_ch_getjob(typval_T *argvars, typval_T *rettv)
2134{
2135 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2136
2137 if (channel != NULL)
2138 {
2139 rettv->v_type = VAR_JOB;
2140 rettv->vval.v_job = channel->ch_job;
2141 if (channel->ch_job != NULL)
2142 ++channel->ch_job->jv_refcount;
2143 }
2144}
2145
2146/*
2147 * "ch_info()" function
2148 */
2149 static void
2150f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2151{
2152 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2153
2154 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2155 channel_info(channel, rettv->vval.v_dict);
2156}
2157
2158/*
2159 * "ch_log()" function
2160 */
2161 static void
2162f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2163{
2164 char_u *msg = get_tv_string(&argvars[0]);
2165 channel_T *channel = NULL;
2166
2167 if (argvars[1].v_type != VAR_UNKNOWN)
2168 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2169
Bram Moolenaard5359b22018-04-05 22:44:39 +02002170 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171}
2172
2173/*
2174 * "ch_logfile()" function
2175 */
2176 static void
2177f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2178{
2179 char_u *fname;
2180 char_u *opt = (char_u *)"";
2181 char_u buf[NUMBUFLEN];
2182
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002183 /* Don't open a file in restricted mode. */
2184 if (check_restricted() || check_secure())
2185 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186 fname = get_tv_string(&argvars[0]);
2187 if (argvars[1].v_type == VAR_STRING)
2188 opt = get_tv_string_buf(&argvars[1], buf);
2189 ch_logfile(fname, opt);
2190}
2191
2192/*
2193 * "ch_open()" function
2194 */
2195 static void
2196f_ch_open(typval_T *argvars, typval_T *rettv)
2197{
2198 rettv->v_type = VAR_CHANNEL;
2199 if (check_restricted() || check_secure())
2200 return;
2201 rettv->vval.v_channel = channel_open_func(argvars);
2202}
2203
2204/*
2205 * "ch_read()" function
2206 */
2207 static void
2208f_ch_read(typval_T *argvars, typval_T *rettv)
2209{
2210 common_channel_read(argvars, rettv, FALSE);
2211}
2212
2213/*
2214 * "ch_readraw()" function
2215 */
2216 static void
2217f_ch_readraw(typval_T *argvars, typval_T *rettv)
2218{
2219 common_channel_read(argvars, rettv, TRUE);
2220}
2221
2222/*
2223 * "ch_evalexpr()" function
2224 */
2225 static void
2226f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2227{
2228 ch_expr_common(argvars, rettv, TRUE);
2229}
2230
2231/*
2232 * "ch_sendexpr()" function
2233 */
2234 static void
2235f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2236{
2237 ch_expr_common(argvars, rettv, FALSE);
2238}
2239
2240/*
2241 * "ch_evalraw()" function
2242 */
2243 static void
2244f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2245{
2246 ch_raw_common(argvars, rettv, TRUE);
2247}
2248
2249/*
2250 * "ch_sendraw()" function
2251 */
2252 static void
2253f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2254{
2255 ch_raw_common(argvars, rettv, FALSE);
2256}
2257
2258/*
2259 * "ch_setoptions()" function
2260 */
2261 static void
2262f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2263{
2264 channel_T *channel;
2265 jobopt_T opt;
2266
2267 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2268 if (channel == NULL)
2269 return;
2270 clear_job_options(&opt);
2271 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002272 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 channel_set_options(channel, &opt);
2274 free_job_options(&opt);
2275}
2276
2277/*
2278 * "ch_status()" function
2279 */
2280 static void
2281f_ch_status(typval_T *argvars, typval_T *rettv)
2282{
2283 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002284 jobopt_T opt;
2285 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286
2287 /* return an empty string by default */
2288 rettv->v_type = VAR_STRING;
2289 rettv->vval.v_string = NULL;
2290
2291 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002292
2293 if (argvars[1].v_type != VAR_UNKNOWN)
2294 {
2295 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002296 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002297 && (opt.jo_set & JO_PART))
2298 part = opt.jo_part;
2299 }
2300
2301 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002302}
2303#endif
2304
2305/*
2306 * "changenr()" function
2307 */
2308 static void
2309f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2310{
2311 rettv->vval.v_number = curbuf->b_u_seq_cur;
2312}
2313
2314/*
2315 * "char2nr(string)" function
2316 */
2317 static void
2318f_char2nr(typval_T *argvars, typval_T *rettv)
2319{
2320#ifdef FEAT_MBYTE
2321 if (has_mbyte)
2322 {
2323 int utf8 = 0;
2324
2325 if (argvars[1].v_type != VAR_UNKNOWN)
2326 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2327
2328 if (utf8)
2329 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2330 else
2331 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2332 }
2333 else
2334#endif
2335 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2336}
2337
2338/*
2339 * "cindent(lnum)" function
2340 */
2341 static void
2342f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2343{
2344#ifdef FEAT_CINDENT
2345 pos_T pos;
2346 linenr_T lnum;
2347
2348 pos = curwin->w_cursor;
2349 lnum = get_tv_lnum(argvars);
2350 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2351 {
2352 curwin->w_cursor.lnum = lnum;
2353 rettv->vval.v_number = get_c_indent();
2354 curwin->w_cursor = pos;
2355 }
2356 else
2357#endif
2358 rettv->vval.v_number = -1;
2359}
2360
2361/*
2362 * "clearmatches()" function
2363 */
2364 static void
2365f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2366{
2367#ifdef FEAT_SEARCH_EXTRA
2368 clear_matches(curwin);
2369#endif
2370}
2371
2372/*
2373 * "col(string)" function
2374 */
2375 static void
2376f_col(typval_T *argvars, typval_T *rettv)
2377{
2378 colnr_T col = 0;
2379 pos_T *fp;
2380 int fnum = curbuf->b_fnum;
2381
2382 fp = var2fpos(&argvars[0], FALSE, &fnum);
2383 if (fp != NULL && fnum == curbuf->b_fnum)
2384 {
2385 if (fp->col == MAXCOL)
2386 {
2387 /* '> can be MAXCOL, get the length of the line then */
2388 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2389 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2390 else
2391 col = MAXCOL;
2392 }
2393 else
2394 {
2395 col = fp->col + 1;
2396#ifdef FEAT_VIRTUALEDIT
2397 /* col(".") when the cursor is on the NUL at the end of the line
2398 * because of "coladd" can be seen as an extra column. */
2399 if (virtual_active() && fp == &curwin->w_cursor)
2400 {
2401 char_u *p = ml_get_cursor();
2402
2403 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2404 curwin->w_virtcol - curwin->w_cursor.coladd))
2405 {
2406# ifdef FEAT_MBYTE
2407 int l;
2408
2409 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2410 col += l;
2411# else
2412 if (*p != NUL && p[1] == NUL)
2413 ++col;
2414# endif
2415 }
2416 }
2417#endif
2418 }
2419 }
2420 rettv->vval.v_number = col;
2421}
2422
2423#if defined(FEAT_INS_EXPAND)
2424/*
2425 * "complete()" function
2426 */
2427 static void
2428f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2429{
2430 int startcol;
2431
2432 if ((State & INSERT) == 0)
2433 {
2434 EMSG(_("E785: complete() can only be used in Insert mode"));
2435 return;
2436 }
2437
2438 /* Check for undo allowed here, because if something was already inserted
2439 * the line was already saved for undo and this check isn't done. */
2440 if (!undo_allowed())
2441 return;
2442
2443 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2444 {
2445 EMSG(_(e_invarg));
2446 return;
2447 }
2448
2449 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2450 if (startcol <= 0)
2451 return;
2452
2453 set_completion(startcol - 1, argvars[1].vval.v_list);
2454}
2455
2456/*
2457 * "complete_add()" function
2458 */
2459 static void
2460f_complete_add(typval_T *argvars, typval_T *rettv)
2461{
2462 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2463}
2464
2465/*
2466 * "complete_check()" function
2467 */
2468 static void
2469f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2470{
2471 int saved = RedrawingDisabled;
2472
2473 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002474 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002475 rettv->vval.v_number = compl_interrupted;
2476 RedrawingDisabled = saved;
2477}
2478#endif
2479
2480/*
2481 * "confirm(message, buttons[, default [, type]])" function
2482 */
2483 static void
2484f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2485{
2486#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2487 char_u *message;
2488 char_u *buttons = NULL;
2489 char_u buf[NUMBUFLEN];
2490 char_u buf2[NUMBUFLEN];
2491 int def = 1;
2492 int type = VIM_GENERIC;
2493 char_u *typestr;
2494 int error = FALSE;
2495
2496 message = get_tv_string_chk(&argvars[0]);
2497 if (message == NULL)
2498 error = TRUE;
2499 if (argvars[1].v_type != VAR_UNKNOWN)
2500 {
2501 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2502 if (buttons == NULL)
2503 error = TRUE;
2504 if (argvars[2].v_type != VAR_UNKNOWN)
2505 {
2506 def = (int)get_tv_number_chk(&argvars[2], &error);
2507 if (argvars[3].v_type != VAR_UNKNOWN)
2508 {
2509 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2510 if (typestr == NULL)
2511 error = TRUE;
2512 else
2513 {
2514 switch (TOUPPER_ASC(*typestr))
2515 {
2516 case 'E': type = VIM_ERROR; break;
2517 case 'Q': type = VIM_QUESTION; break;
2518 case 'I': type = VIM_INFO; break;
2519 case 'W': type = VIM_WARNING; break;
2520 case 'G': type = VIM_GENERIC; break;
2521 }
2522 }
2523 }
2524 }
2525 }
2526
2527 if (buttons == NULL || *buttons == NUL)
2528 buttons = (char_u *)_("&Ok");
2529
2530 if (!error)
2531 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2532 def, NULL, FALSE);
2533#endif
2534}
2535
2536/*
2537 * "copy()" function
2538 */
2539 static void
2540f_copy(typval_T *argvars, typval_T *rettv)
2541{
2542 item_copy(&argvars[0], rettv, FALSE, 0);
2543}
2544
2545#ifdef FEAT_FLOAT
2546/*
2547 * "cos()" function
2548 */
2549 static void
2550f_cos(typval_T *argvars, typval_T *rettv)
2551{
2552 float_T f = 0.0;
2553
2554 rettv->v_type = VAR_FLOAT;
2555 if (get_float_arg(argvars, &f) == OK)
2556 rettv->vval.v_float = cos(f);
2557 else
2558 rettv->vval.v_float = 0.0;
2559}
2560
2561/*
2562 * "cosh()" function
2563 */
2564 static void
2565f_cosh(typval_T *argvars, typval_T *rettv)
2566{
2567 float_T f = 0.0;
2568
2569 rettv->v_type = VAR_FLOAT;
2570 if (get_float_arg(argvars, &f) == OK)
2571 rettv->vval.v_float = cosh(f);
2572 else
2573 rettv->vval.v_float = 0.0;
2574}
2575#endif
2576
2577/*
2578 * "count()" function
2579 */
2580 static void
2581f_count(typval_T *argvars, typval_T *rettv)
2582{
2583 long n = 0;
2584 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002585 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002586
Bram Moolenaar9966b212017-07-28 16:46:57 +02002587 if (argvars[2].v_type != VAR_UNKNOWN)
2588 ic = (int)get_tv_number_chk(&argvars[2], &error);
2589
2590 if (argvars[0].v_type == VAR_STRING)
2591 {
2592 char_u *expr = get_tv_string_chk(&argvars[1]);
2593 char_u *p = argvars[0].vval.v_string;
2594 char_u *next;
2595
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002596 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002597 {
2598 if (ic)
2599 {
2600 size_t len = STRLEN(expr);
2601
2602 while (*p != NUL)
2603 {
2604 if (MB_STRNICMP(p, expr, len) == 0)
2605 {
2606 ++n;
2607 p += len;
2608 }
2609 else
2610 MB_PTR_ADV(p);
2611 }
2612 }
2613 else
2614 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2615 != NULL)
2616 {
2617 ++n;
2618 p = next + STRLEN(expr);
2619 }
2620 }
2621
2622 }
2623 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 {
2625 listitem_T *li;
2626 list_T *l;
2627 long idx;
2628
2629 if ((l = argvars[0].vval.v_list) != NULL)
2630 {
2631 li = l->lv_first;
2632 if (argvars[2].v_type != VAR_UNKNOWN)
2633 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002634 if (argvars[3].v_type != VAR_UNKNOWN)
2635 {
2636 idx = (long)get_tv_number_chk(&argvars[3], &error);
2637 if (!error)
2638 {
2639 li = list_find(l, idx);
2640 if (li == NULL)
2641 EMSGN(_(e_listidx), idx);
2642 }
2643 }
2644 if (error)
2645 li = NULL;
2646 }
2647
2648 for ( ; li != NULL; li = li->li_next)
2649 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2650 ++n;
2651 }
2652 }
2653 else if (argvars[0].v_type == VAR_DICT)
2654 {
2655 int todo;
2656 dict_T *d;
2657 hashitem_T *hi;
2658
2659 if ((d = argvars[0].vval.v_dict) != NULL)
2660 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 if (argvars[2].v_type != VAR_UNKNOWN)
2662 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 if (argvars[3].v_type != VAR_UNKNOWN)
2664 EMSG(_(e_invarg));
2665 }
2666
2667 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2668 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2669 {
2670 if (!HASHITEM_EMPTY(hi))
2671 {
2672 --todo;
2673 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2674 ++n;
2675 }
2676 }
2677 }
2678 }
2679 else
2680 EMSG2(_(e_listdictarg), "count()");
2681 rettv->vval.v_number = n;
2682}
2683
2684/*
2685 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2686 *
2687 * Checks the existence of a cscope connection.
2688 */
2689 static void
2690f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2691{
2692#ifdef FEAT_CSCOPE
2693 int num = 0;
2694 char_u *dbpath = NULL;
2695 char_u *prepend = NULL;
2696 char_u buf[NUMBUFLEN];
2697
2698 if (argvars[0].v_type != VAR_UNKNOWN
2699 && argvars[1].v_type != VAR_UNKNOWN)
2700 {
2701 num = (int)get_tv_number(&argvars[0]);
2702 dbpath = get_tv_string(&argvars[1]);
2703 if (argvars[2].v_type != VAR_UNKNOWN)
2704 prepend = get_tv_string_buf(&argvars[2], buf);
2705 }
2706
2707 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2708#endif
2709}
2710
2711/*
2712 * "cursor(lnum, col)" function, or
2713 * "cursor(list)"
2714 *
2715 * Moves the cursor to the specified line and column.
2716 * Returns 0 when the position could be set, -1 otherwise.
2717 */
2718 static void
2719f_cursor(typval_T *argvars, typval_T *rettv)
2720{
2721 long line, col;
2722#ifdef FEAT_VIRTUALEDIT
2723 long coladd = 0;
2724#endif
2725 int set_curswant = TRUE;
2726
2727 rettv->vval.v_number = -1;
2728 if (argvars[1].v_type == VAR_UNKNOWN)
2729 {
2730 pos_T pos;
2731 colnr_T curswant = -1;
2732
2733 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2734 {
2735 EMSG(_(e_invarg));
2736 return;
2737 }
2738 line = pos.lnum;
2739 col = pos.col;
2740#ifdef FEAT_VIRTUALEDIT
2741 coladd = pos.coladd;
2742#endif
2743 if (curswant >= 0)
2744 {
2745 curwin->w_curswant = curswant - 1;
2746 set_curswant = FALSE;
2747 }
2748 }
2749 else
2750 {
2751 line = get_tv_lnum(argvars);
2752 col = (long)get_tv_number_chk(&argvars[1], NULL);
2753#ifdef FEAT_VIRTUALEDIT
2754 if (argvars[2].v_type != VAR_UNKNOWN)
2755 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2756#endif
2757 }
2758 if (line < 0 || col < 0
2759#ifdef FEAT_VIRTUALEDIT
2760 || coladd < 0
2761#endif
2762 )
2763 return; /* type error; errmsg already given */
2764 if (line > 0)
2765 curwin->w_cursor.lnum = line;
2766 if (col > 0)
2767 curwin->w_cursor.col = col - 1;
2768#ifdef FEAT_VIRTUALEDIT
2769 curwin->w_cursor.coladd = coladd;
2770#endif
2771
2772 /* Make sure the cursor is in a valid position. */
2773 check_cursor();
2774#ifdef FEAT_MBYTE
2775 /* Correct cursor for multi-byte character. */
2776 if (has_mbyte)
2777 mb_adjust_cursor();
2778#endif
2779
2780 curwin->w_set_curswant = set_curswant;
2781 rettv->vval.v_number = 0;
2782}
2783
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002784#ifdef WIN3264
2785/*
2786 * "debugbreak()" function
2787 */
2788 static void
2789f_debugbreak(typval_T *argvars, typval_T *rettv)
2790{
2791 int pid;
2792
2793 rettv->vval.v_number = FAIL;
2794 pid = (int)get_tv_number(&argvars[0]);
2795 if (pid == 0)
2796 EMSG(_(e_invarg));
2797 else
2798 {
2799 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2800
2801 if (hProcess != NULL)
2802 {
2803 DebugBreakProcess(hProcess);
2804 CloseHandle(hProcess);
2805 rettv->vval.v_number = OK;
2806 }
2807 }
2808}
2809#endif
2810
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002811/*
2812 * "deepcopy()" function
2813 */
2814 static void
2815f_deepcopy(typval_T *argvars, typval_T *rettv)
2816{
2817 int noref = 0;
2818 int copyID;
2819
2820 if (argvars[1].v_type != VAR_UNKNOWN)
2821 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2822 if (noref < 0 || noref > 1)
2823 EMSG(_(e_invarg));
2824 else
2825 {
2826 copyID = get_copyID();
2827 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2828 }
2829}
2830
2831/*
2832 * "delete()" function
2833 */
2834 static void
2835f_delete(typval_T *argvars, typval_T *rettv)
2836{
2837 char_u nbuf[NUMBUFLEN];
2838 char_u *name;
2839 char_u *flags;
2840
2841 rettv->vval.v_number = -1;
2842 if (check_restricted() || check_secure())
2843 return;
2844
2845 name = get_tv_string(&argvars[0]);
2846 if (name == NULL || *name == NUL)
2847 {
2848 EMSG(_(e_invarg));
2849 return;
2850 }
2851
2852 if (argvars[1].v_type != VAR_UNKNOWN)
2853 flags = get_tv_string_buf(&argvars[1], nbuf);
2854 else
2855 flags = (char_u *)"";
2856
2857 if (*flags == NUL)
2858 /* delete a file */
2859 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2860 else if (STRCMP(flags, "d") == 0)
2861 /* delete an empty directory */
2862 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2863 else if (STRCMP(flags, "rf") == 0)
2864 /* delete a directory recursively */
2865 rettv->vval.v_number = delete_recursive(name);
2866 else
2867 EMSG2(_(e_invexpr2), flags);
2868}
2869
2870/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002871 * "deletebufline()" function
2872 */
2873 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002874f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002875{
2876 buf_T *buf;
2877 linenr_T first, last;
2878 linenr_T lnum;
2879 long count;
2880 int is_curbuf;
2881 buf_T *curbuf_save = NULL;
2882 win_T *curwin_save = NULL;
2883 tabpage_T *tp;
2884 win_T *wp;
2885
2886 buf = get_buf_tv(&argvars[0], FALSE);
2887 if (buf == NULL)
2888 {
2889 rettv->vval.v_number = 1; /* FAIL */
2890 return;
2891 }
2892 is_curbuf = buf == curbuf;
2893
2894 first = get_tv_lnum_buf(&argvars[1], buf);
2895 if (argvars[2].v_type != VAR_UNKNOWN)
2896 last = get_tv_lnum_buf(&argvars[2], buf);
2897 else
2898 last = first;
2899
2900 if (buf->b_ml.ml_mfp == NULL || first < 1
2901 || first > buf->b_ml.ml_line_count || last < first)
2902 {
2903 rettv->vval.v_number = 1; /* FAIL */
2904 return;
2905 }
2906
2907 if (!is_curbuf)
2908 {
2909 curbuf_save = curbuf;
2910 curwin_save = curwin;
2911 curbuf = buf;
2912 find_win_for_curbuf();
2913 }
2914 if (last > curbuf->b_ml.ml_line_count)
2915 last = curbuf->b_ml.ml_line_count;
2916 count = last - first + 1;
2917
2918 // When coming here from Insert mode, sync undo, so that this can be
2919 // undone separately from what was previously inserted.
2920 if (u_sync_once == 2)
2921 {
2922 u_sync_once = 1; // notify that u_sync() was called
2923 u_sync(TRUE);
2924 }
2925
2926 if (u_save(first - 1, last + 1) == FAIL)
2927 {
2928 rettv->vval.v_number = 1; /* FAIL */
2929 return;
2930 }
2931
2932 for (lnum = first; lnum <= last; ++lnum)
2933 ml_delete(first, TRUE);
2934
2935 FOR_ALL_TAB_WINDOWS(tp, wp)
2936 if (wp->w_buffer == buf)
2937 {
2938 if (wp->w_cursor.lnum > last)
2939 wp->w_cursor.lnum -= count;
2940 else if (wp->w_cursor.lnum> first)
2941 wp->w_cursor.lnum = first;
2942 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2943 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2944 }
2945 check_cursor_col();
2946 deleted_lines_mark(first, count);
2947
2948 if (!is_curbuf)
2949 {
2950 curbuf = curbuf_save;
2951 curwin = curwin_save;
2952 }
2953}
2954
2955/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 * "did_filetype()" function
2957 */
2958 static void
2959f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2960{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002961 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002962}
2963
2964/*
2965 * "diff_filler()" function
2966 */
2967 static void
2968f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2969{
2970#ifdef FEAT_DIFF
2971 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2972#endif
2973}
2974
2975/*
2976 * "diff_hlID()" function
2977 */
2978 static void
2979f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2980{
2981#ifdef FEAT_DIFF
2982 linenr_T lnum = get_tv_lnum(argvars);
2983 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002984 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 static int fnum = 0;
2986 static int change_start = 0;
2987 static int change_end = 0;
2988 static hlf_T hlID = (hlf_T)0;
2989 int filler_lines;
2990 int col;
2991
2992 if (lnum < 0) /* ignore type error in {lnum} arg */
2993 lnum = 0;
2994 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002995 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 || fnum != curbuf->b_fnum)
2997 {
2998 /* New line, buffer, change: need to get the values. */
2999 filler_lines = diff_check(curwin, lnum);
3000 if (filler_lines < 0)
3001 {
3002 if (filler_lines == -1)
3003 {
3004 change_start = MAXCOL;
3005 change_end = -1;
3006 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3007 hlID = HLF_ADD; /* added line */
3008 else
3009 hlID = HLF_CHD; /* changed line */
3010 }
3011 else
3012 hlID = HLF_ADD; /* added line */
3013 }
3014 else
3015 hlID = (hlf_T)0;
3016 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003017 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 fnum = curbuf->b_fnum;
3019 }
3020
3021 if (hlID == HLF_CHD || hlID == HLF_TXD)
3022 {
3023 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3024 if (col >= change_start && col <= change_end)
3025 hlID = HLF_TXD; /* changed text */
3026 else
3027 hlID = HLF_CHD; /* changed line */
3028 }
3029 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3030#endif
3031}
3032
3033/*
3034 * "empty({expr})" function
3035 */
3036 static void
3037f_empty(typval_T *argvars, typval_T *rettv)
3038{
3039 int n = FALSE;
3040
3041 switch (argvars[0].v_type)
3042 {
3043 case VAR_STRING:
3044 case VAR_FUNC:
3045 n = argvars[0].vval.v_string == NULL
3046 || *argvars[0].vval.v_string == NUL;
3047 break;
3048 case VAR_PARTIAL:
3049 n = FALSE;
3050 break;
3051 case VAR_NUMBER:
3052 n = argvars[0].vval.v_number == 0;
3053 break;
3054 case VAR_FLOAT:
3055#ifdef FEAT_FLOAT
3056 n = argvars[0].vval.v_float == 0.0;
3057 break;
3058#endif
3059 case VAR_LIST:
3060 n = argvars[0].vval.v_list == NULL
3061 || argvars[0].vval.v_list->lv_first == NULL;
3062 break;
3063 case VAR_DICT:
3064 n = argvars[0].vval.v_dict == NULL
3065 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3066 break;
3067 case VAR_SPECIAL:
3068 n = argvars[0].vval.v_number != VVAL_TRUE;
3069 break;
3070
3071 case VAR_JOB:
3072#ifdef FEAT_JOB_CHANNEL
3073 n = argvars[0].vval.v_job == NULL
3074 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3075 break;
3076#endif
3077 case VAR_CHANNEL:
3078#ifdef FEAT_JOB_CHANNEL
3079 n = argvars[0].vval.v_channel == NULL
3080 || !channel_is_open(argvars[0].vval.v_channel);
3081 break;
3082#endif
3083 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003084 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 n = TRUE;
3086 break;
3087 }
3088
3089 rettv->vval.v_number = n;
3090}
3091
3092/*
3093 * "escape({string}, {chars})" function
3094 */
3095 static void
3096f_escape(typval_T *argvars, typval_T *rettv)
3097{
3098 char_u buf[NUMBUFLEN];
3099
3100 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3101 get_tv_string_buf(&argvars[1], buf));
3102 rettv->v_type = VAR_STRING;
3103}
3104
3105/*
3106 * "eval()" function
3107 */
3108 static void
3109f_eval(typval_T *argvars, typval_T *rettv)
3110{
3111 char_u *s, *p;
3112
3113 s = get_tv_string_chk(&argvars[0]);
3114 if (s != NULL)
3115 s = skipwhite(s);
3116
3117 p = s;
3118 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3119 {
3120 if (p != NULL && !aborting())
3121 EMSG2(_(e_invexpr2), p);
3122 need_clr_eos = FALSE;
3123 rettv->v_type = VAR_NUMBER;
3124 rettv->vval.v_number = 0;
3125 }
3126 else if (*s != NUL)
3127 EMSG(_(e_trailing));
3128}
3129
3130/*
3131 * "eventhandler()" function
3132 */
3133 static void
3134f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3135{
3136 rettv->vval.v_number = vgetc_busy;
3137}
3138
3139/*
3140 * "executable()" function
3141 */
3142 static void
3143f_executable(typval_T *argvars, typval_T *rettv)
3144{
3145 char_u *name = get_tv_string(&argvars[0]);
3146
3147 /* Check in $PATH and also check directly if there is a directory name. */
3148 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3149 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3150}
3151
3152static garray_T redir_execute_ga;
3153
3154/*
3155 * Append "value[value_len]" to the execute() output.
3156 */
3157 void
3158execute_redir_str(char_u *value, int value_len)
3159{
3160 int len;
3161
3162 if (value_len == -1)
3163 len = (int)STRLEN(value); /* Append the entire string */
3164 else
3165 len = value_len; /* Append only "value_len" characters */
3166 if (ga_grow(&redir_execute_ga, len) == OK)
3167 {
3168 mch_memmove((char *)redir_execute_ga.ga_data
3169 + redir_execute_ga.ga_len, value, len);
3170 redir_execute_ga.ga_len += len;
3171 }
3172}
3173
3174/*
3175 * Get next line from a list.
3176 * Called by do_cmdline() to get the next line.
3177 * Returns allocated string, or NULL for end of function.
3178 */
3179
3180 static char_u *
3181get_list_line(
3182 int c UNUSED,
3183 void *cookie,
3184 int indent UNUSED)
3185{
3186 listitem_T **p = (listitem_T **)cookie;
3187 listitem_T *item = *p;
3188 char_u buf[NUMBUFLEN];
3189 char_u *s;
3190
3191 if (item == NULL)
3192 return NULL;
3193 s = get_tv_string_buf_chk(&item->li_tv, buf);
3194 *p = item->li_next;
3195 return s == NULL ? NULL : vim_strsave(s);
3196}
3197
3198/*
3199 * "execute()" function
3200 */
3201 static void
3202f_execute(typval_T *argvars, typval_T *rettv)
3203{
3204 char_u *cmd = NULL;
3205 list_T *list = NULL;
3206 int save_msg_silent = msg_silent;
3207 int save_emsg_silent = emsg_silent;
3208 int save_emsg_noredir = emsg_noredir;
3209 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003210 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003211 garray_T save_ga;
3212
3213 rettv->vval.v_string = NULL;
3214 rettv->v_type = VAR_STRING;
3215
3216 if (argvars[0].v_type == VAR_LIST)
3217 {
3218 list = argvars[0].vval.v_list;
3219 if (list == NULL || list->lv_first == NULL)
3220 /* empty list, no commands, empty output */
3221 return;
3222 ++list->lv_refcount;
3223 }
3224 else
3225 {
3226 cmd = get_tv_string_chk(&argvars[0]);
3227 if (cmd == NULL)
3228 return;
3229 }
3230
3231 if (argvars[1].v_type != VAR_UNKNOWN)
3232 {
3233 char_u buf[NUMBUFLEN];
3234 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3235
3236 if (s == NULL)
3237 return;
3238 if (STRNCMP(s, "silent", 6) == 0)
3239 ++msg_silent;
3240 if (STRCMP(s, "silent!") == 0)
3241 {
3242 emsg_silent = TRUE;
3243 emsg_noredir = TRUE;
3244 }
3245 }
3246 else
3247 ++msg_silent;
3248
3249 if (redir_execute)
3250 save_ga = redir_execute_ga;
3251 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3252 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003253 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254
3255 if (cmd != NULL)
3256 do_cmdline_cmd(cmd);
3257 else
3258 {
3259 listitem_T *item = list->lv_first;
3260
3261 do_cmdline(NULL, get_list_line, (void *)&item,
3262 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3263 --list->lv_refcount;
3264 }
3265
Bram Moolenaard297f352017-01-29 20:31:21 +01003266 /* Need to append a NUL to the result. */
3267 if (ga_grow(&redir_execute_ga, 1) == OK)
3268 {
3269 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3270 rettv->vval.v_string = redir_execute_ga.ga_data;
3271 }
3272 else
3273 {
3274 ga_clear(&redir_execute_ga);
3275 rettv->vval.v_string = NULL;
3276 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 msg_silent = save_msg_silent;
3278 emsg_silent = save_emsg_silent;
3279 emsg_noredir = save_emsg_noredir;
3280
3281 redir_execute = save_redir_execute;
3282 if (redir_execute)
3283 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003284 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285
3286 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3287 * line. Put it back in the first column. */
3288 msg_col = 0;
3289}
3290
3291/*
3292 * "exepath()" function
3293 */
3294 static void
3295f_exepath(typval_T *argvars, typval_T *rettv)
3296{
3297 char_u *p = NULL;
3298
3299 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3300 rettv->v_type = VAR_STRING;
3301 rettv->vval.v_string = p;
3302}
3303
3304/*
3305 * "exists()" function
3306 */
3307 static void
3308f_exists(typval_T *argvars, typval_T *rettv)
3309{
3310 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003311 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003312
3313 p = get_tv_string(&argvars[0]);
3314 if (*p == '$') /* environment variable */
3315 {
3316 /* first try "normal" environment variables (fast) */
3317 if (mch_getenv(p + 1) != NULL)
3318 n = TRUE;
3319 else
3320 {
3321 /* try expanding things like $VIM and ${HOME} */
3322 p = expand_env_save(p);
3323 if (p != NULL && *p != '$')
3324 n = TRUE;
3325 vim_free(p);
3326 }
3327 }
3328 else if (*p == '&' || *p == '+') /* option */
3329 {
3330 n = (get_option_tv(&p, NULL, TRUE) == OK);
3331 if (*skipwhite(p) != NUL)
3332 n = FALSE; /* trailing garbage */
3333 }
3334 else if (*p == '*') /* internal or user defined function */
3335 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003336 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 }
3338 else if (*p == ':')
3339 {
3340 n = cmd_exists(p + 1);
3341 }
3342 else if (*p == '#')
3343 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 if (p[1] == '#')
3345 n = autocmd_supported(p + 2);
3346 else
3347 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 }
3349 else /* internal variable */
3350 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003351 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 }
3353
3354 rettv->vval.v_number = n;
3355}
3356
3357#ifdef FEAT_FLOAT
3358/*
3359 * "exp()" function
3360 */
3361 static void
3362f_exp(typval_T *argvars, typval_T *rettv)
3363{
3364 float_T f = 0.0;
3365
3366 rettv->v_type = VAR_FLOAT;
3367 if (get_float_arg(argvars, &f) == OK)
3368 rettv->vval.v_float = exp(f);
3369 else
3370 rettv->vval.v_float = 0.0;
3371}
3372#endif
3373
3374/*
3375 * "expand()" function
3376 */
3377 static void
3378f_expand(typval_T *argvars, typval_T *rettv)
3379{
3380 char_u *s;
3381 int len;
3382 char_u *errormsg;
3383 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3384 expand_T xpc;
3385 int error = FALSE;
3386 char_u *result;
3387
3388 rettv->v_type = VAR_STRING;
3389 if (argvars[1].v_type != VAR_UNKNOWN
3390 && argvars[2].v_type != VAR_UNKNOWN
3391 && get_tv_number_chk(&argvars[2], &error)
3392 && !error)
3393 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003394 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003395 }
3396
3397 s = get_tv_string(&argvars[0]);
3398 if (*s == '%' || *s == '#' || *s == '<')
3399 {
3400 ++emsg_off;
3401 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3402 --emsg_off;
3403 if (rettv->v_type == VAR_LIST)
3404 {
3405 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3406 list_append_string(rettv->vval.v_list, result, -1);
3407 else
3408 vim_free(result);
3409 }
3410 else
3411 rettv->vval.v_string = result;
3412 }
3413 else
3414 {
3415 /* When the optional second argument is non-zero, don't remove matches
3416 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3417 if (argvars[1].v_type != VAR_UNKNOWN
3418 && get_tv_number_chk(&argvars[1], &error))
3419 options |= WILD_KEEP_ALL;
3420 if (!error)
3421 {
3422 ExpandInit(&xpc);
3423 xpc.xp_context = EXPAND_FILES;
3424 if (p_wic)
3425 options += WILD_ICASE;
3426 if (rettv->v_type == VAR_STRING)
3427 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3428 options, WILD_ALL);
3429 else if (rettv_list_alloc(rettv) != FAIL)
3430 {
3431 int i;
3432
3433 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3434 for (i = 0; i < xpc.xp_numfiles; i++)
3435 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3436 ExpandCleanup(&xpc);
3437 }
3438 }
3439 else
3440 rettv->vval.v_string = NULL;
3441 }
3442}
3443
3444/*
3445 * "extend(list, list [, idx])" function
3446 * "extend(dict, dict [, action])" function
3447 */
3448 static void
3449f_extend(typval_T *argvars, typval_T *rettv)
3450{
3451 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3452
3453 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3454 {
3455 list_T *l1, *l2;
3456 listitem_T *item;
3457 long before;
3458 int error = FALSE;
3459
3460 l1 = argvars[0].vval.v_list;
3461 l2 = argvars[1].vval.v_list;
3462 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3463 && l2 != NULL)
3464 {
3465 if (argvars[2].v_type != VAR_UNKNOWN)
3466 {
3467 before = (long)get_tv_number_chk(&argvars[2], &error);
3468 if (error)
3469 return; /* type error; errmsg already given */
3470
3471 if (before == l1->lv_len)
3472 item = NULL;
3473 else
3474 {
3475 item = list_find(l1, before);
3476 if (item == NULL)
3477 {
3478 EMSGN(_(e_listidx), before);
3479 return;
3480 }
3481 }
3482 }
3483 else
3484 item = NULL;
3485 list_extend(l1, l2, item);
3486
3487 copy_tv(&argvars[0], rettv);
3488 }
3489 }
3490 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3491 {
3492 dict_T *d1, *d2;
3493 char_u *action;
3494 int i;
3495
3496 d1 = argvars[0].vval.v_dict;
3497 d2 = argvars[1].vval.v_dict;
3498 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3499 && d2 != NULL)
3500 {
3501 /* Check the third argument. */
3502 if (argvars[2].v_type != VAR_UNKNOWN)
3503 {
3504 static char *(av[]) = {"keep", "force", "error"};
3505
3506 action = get_tv_string_chk(&argvars[2]);
3507 if (action == NULL)
3508 return; /* type error; errmsg already given */
3509 for (i = 0; i < 3; ++i)
3510 if (STRCMP(action, av[i]) == 0)
3511 break;
3512 if (i == 3)
3513 {
3514 EMSG2(_(e_invarg2), action);
3515 return;
3516 }
3517 }
3518 else
3519 action = (char_u *)"force";
3520
3521 dict_extend(d1, d2, action);
3522
3523 copy_tv(&argvars[0], rettv);
3524 }
3525 }
3526 else
3527 EMSG2(_(e_listdictarg), "extend()");
3528}
3529
3530/*
3531 * "feedkeys()" function
3532 */
3533 static void
3534f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3535{
3536 int remap = TRUE;
3537 int insert = FALSE;
3538 char_u *keys, *flags;
3539 char_u nbuf[NUMBUFLEN];
3540 int typed = FALSE;
3541 int execute = FALSE;
3542 int dangerous = FALSE;
3543 char_u *keys_esc;
3544
3545 /* This is not allowed in the sandbox. If the commands would still be
3546 * executed in the sandbox it would be OK, but it probably happens later,
3547 * when "sandbox" is no longer set. */
3548 if (check_secure())
3549 return;
3550
3551 keys = get_tv_string(&argvars[0]);
3552
3553 if (argvars[1].v_type != VAR_UNKNOWN)
3554 {
3555 flags = get_tv_string_buf(&argvars[1], nbuf);
3556 for ( ; *flags != NUL; ++flags)
3557 {
3558 switch (*flags)
3559 {
3560 case 'n': remap = FALSE; break;
3561 case 'm': remap = TRUE; break;
3562 case 't': typed = TRUE; break;
3563 case 'i': insert = TRUE; break;
3564 case 'x': execute = TRUE; break;
3565 case '!': dangerous = TRUE; break;
3566 }
3567 }
3568 }
3569
3570 if (*keys != NUL || execute)
3571 {
3572 /* Need to escape K_SPECIAL and CSI before putting the string in the
3573 * typeahead buffer. */
3574 keys_esc = vim_strsave_escape_csi(keys);
3575 if (keys_esc != NULL)
3576 {
3577 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3578 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3579 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003580 if (vgetc_busy
3581#ifdef FEAT_TIMERS
3582 || timer_busy
3583#endif
3584 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003585 typebuf_was_filled = TRUE;
3586 if (execute)
3587 {
3588 int save_msg_scroll = msg_scroll;
3589
3590 /* Avoid a 1 second delay when the keys start Insert mode. */
3591 msg_scroll = FALSE;
3592
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003593 if (!dangerous)
3594 ++ex_normal_busy;
Bram Moolenaar655a82a2018-05-08 22:01:07 +02003595 exec_normal(TRUE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003596 if (!dangerous)
3597 --ex_normal_busy;
3598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 msg_scroll |= save_msg_scroll;
3600 }
3601 }
3602 }
3603}
3604
3605/*
3606 * "filereadable()" function
3607 */
3608 static void
3609f_filereadable(typval_T *argvars, typval_T *rettv)
3610{
3611 int fd;
3612 char_u *p;
3613 int n;
3614
3615#ifndef O_NONBLOCK
3616# define O_NONBLOCK 0
3617#endif
3618 p = get_tv_string(&argvars[0]);
3619 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3620 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3621 {
3622 n = TRUE;
3623 close(fd);
3624 }
3625 else
3626 n = FALSE;
3627
3628 rettv->vval.v_number = n;
3629}
3630
3631/*
3632 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3633 * rights to write into.
3634 */
3635 static void
3636f_filewritable(typval_T *argvars, typval_T *rettv)
3637{
3638 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3639}
3640
3641 static void
3642findfilendir(
3643 typval_T *argvars UNUSED,
3644 typval_T *rettv,
3645 int find_what UNUSED)
3646{
3647#ifdef FEAT_SEARCHPATH
3648 char_u *fname;
3649 char_u *fresult = NULL;
3650 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3651 char_u *p;
3652 char_u pathbuf[NUMBUFLEN];
3653 int count = 1;
3654 int first = TRUE;
3655 int error = FALSE;
3656#endif
3657
3658 rettv->vval.v_string = NULL;
3659 rettv->v_type = VAR_STRING;
3660
3661#ifdef FEAT_SEARCHPATH
3662 fname = get_tv_string(&argvars[0]);
3663
3664 if (argvars[1].v_type != VAR_UNKNOWN)
3665 {
3666 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3667 if (p == NULL)
3668 error = TRUE;
3669 else
3670 {
3671 if (*p != NUL)
3672 path = p;
3673
3674 if (argvars[2].v_type != VAR_UNKNOWN)
3675 count = (int)get_tv_number_chk(&argvars[2], &error);
3676 }
3677 }
3678
3679 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3680 error = TRUE;
3681
3682 if (*fname != NUL && !error)
3683 {
3684 do
3685 {
3686 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3687 vim_free(fresult);
3688 fresult = find_file_in_path_option(first ? fname : NULL,
3689 first ? (int)STRLEN(fname) : 0,
3690 0, first, path,
3691 find_what,
3692 curbuf->b_ffname,
3693 find_what == FINDFILE_DIR
3694 ? (char_u *)"" : curbuf->b_p_sua);
3695 first = FALSE;
3696
3697 if (fresult != NULL && rettv->v_type == VAR_LIST)
3698 list_append_string(rettv->vval.v_list, fresult, -1);
3699
3700 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3701 }
3702
3703 if (rettv->v_type == VAR_STRING)
3704 rettv->vval.v_string = fresult;
3705#endif
3706}
3707
3708/*
3709 * "filter()" function
3710 */
3711 static void
3712f_filter(typval_T *argvars, typval_T *rettv)
3713{
3714 filter_map(argvars, rettv, FALSE);
3715}
3716
3717/*
3718 * "finddir({fname}[, {path}[, {count}]])" function
3719 */
3720 static void
3721f_finddir(typval_T *argvars, typval_T *rettv)
3722{
3723 findfilendir(argvars, rettv, FINDFILE_DIR);
3724}
3725
3726/*
3727 * "findfile({fname}[, {path}[, {count}]])" function
3728 */
3729 static void
3730f_findfile(typval_T *argvars, typval_T *rettv)
3731{
3732 findfilendir(argvars, rettv, FINDFILE_FILE);
3733}
3734
3735#ifdef FEAT_FLOAT
3736/*
3737 * "float2nr({float})" function
3738 */
3739 static void
3740f_float2nr(typval_T *argvars, typval_T *rettv)
3741{
3742 float_T f = 0.0;
3743
3744 if (get_float_arg(argvars, &f) == OK)
3745 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003746 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003747 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003748 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003749 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 else
3751 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 }
3753}
3754
3755/*
3756 * "floor({float})" function
3757 */
3758 static void
3759f_floor(typval_T *argvars, typval_T *rettv)
3760{
3761 float_T f = 0.0;
3762
3763 rettv->v_type = VAR_FLOAT;
3764 if (get_float_arg(argvars, &f) == OK)
3765 rettv->vval.v_float = floor(f);
3766 else
3767 rettv->vval.v_float = 0.0;
3768}
3769
3770/*
3771 * "fmod()" function
3772 */
3773 static void
3774f_fmod(typval_T *argvars, typval_T *rettv)
3775{
3776 float_T fx = 0.0, fy = 0.0;
3777
3778 rettv->v_type = VAR_FLOAT;
3779 if (get_float_arg(argvars, &fx) == OK
3780 && get_float_arg(&argvars[1], &fy) == OK)
3781 rettv->vval.v_float = fmod(fx, fy);
3782 else
3783 rettv->vval.v_float = 0.0;
3784}
3785#endif
3786
3787/*
3788 * "fnameescape({string})" function
3789 */
3790 static void
3791f_fnameescape(typval_T *argvars, typval_T *rettv)
3792{
3793 rettv->vval.v_string = vim_strsave_fnameescape(
3794 get_tv_string(&argvars[0]), FALSE);
3795 rettv->v_type = VAR_STRING;
3796}
3797
3798/*
3799 * "fnamemodify({fname}, {mods})" function
3800 */
3801 static void
3802f_fnamemodify(typval_T *argvars, typval_T *rettv)
3803{
3804 char_u *fname;
3805 char_u *mods;
3806 int usedlen = 0;
3807 int len;
3808 char_u *fbuf = NULL;
3809 char_u buf[NUMBUFLEN];
3810
3811 fname = get_tv_string_chk(&argvars[0]);
3812 mods = get_tv_string_buf_chk(&argvars[1], buf);
3813 if (fname == NULL || mods == NULL)
3814 fname = NULL;
3815 else
3816 {
3817 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003818 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003819 }
3820
3821 rettv->v_type = VAR_STRING;
3822 if (fname == NULL)
3823 rettv->vval.v_string = NULL;
3824 else
3825 rettv->vval.v_string = vim_strnsave(fname, len);
3826 vim_free(fbuf);
3827}
3828
3829static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
3830
3831/*
3832 * "foldclosed()" function
3833 */
3834 static void
3835foldclosed_both(
3836 typval_T *argvars UNUSED,
3837 typval_T *rettv,
3838 int end UNUSED)
3839{
3840#ifdef FEAT_FOLDING
3841 linenr_T lnum;
3842 linenr_T first, last;
3843
3844 lnum = get_tv_lnum(argvars);
3845 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3846 {
3847 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3848 {
3849 if (end)
3850 rettv->vval.v_number = (varnumber_T)last;
3851 else
3852 rettv->vval.v_number = (varnumber_T)first;
3853 return;
3854 }
3855 }
3856#endif
3857 rettv->vval.v_number = -1;
3858}
3859
3860/*
3861 * "foldclosed()" function
3862 */
3863 static void
3864f_foldclosed(typval_T *argvars, typval_T *rettv)
3865{
3866 foldclosed_both(argvars, rettv, FALSE);
3867}
3868
3869/*
3870 * "foldclosedend()" function
3871 */
3872 static void
3873f_foldclosedend(typval_T *argvars, typval_T *rettv)
3874{
3875 foldclosed_both(argvars, rettv, TRUE);
3876}
3877
3878/*
3879 * "foldlevel()" function
3880 */
3881 static void
3882f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3883{
3884#ifdef FEAT_FOLDING
3885 linenr_T lnum;
3886
3887 lnum = get_tv_lnum(argvars);
3888 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3889 rettv->vval.v_number = foldLevel(lnum);
3890#endif
3891}
3892
3893/*
3894 * "foldtext()" function
3895 */
3896 static void
3897f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3898{
3899#ifdef FEAT_FOLDING
3900 linenr_T foldstart;
3901 linenr_T foldend;
3902 char_u *dashes;
3903 linenr_T lnum;
3904 char_u *s;
3905 char_u *r;
3906 int len;
3907 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003908 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909#endif
3910
3911 rettv->v_type = VAR_STRING;
3912 rettv->vval.v_string = NULL;
3913#ifdef FEAT_FOLDING
3914 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3915 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3916 dashes = get_vim_var_str(VV_FOLDDASHES);
3917 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3918 && dashes != NULL)
3919 {
3920 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003921 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 if (!linewhite(lnum))
3923 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003924
3925 /* Find interesting text in this line. */
3926 s = skipwhite(ml_get(lnum));
3927 /* skip C comment-start */
3928 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3929 {
3930 s = skipwhite(s + 2);
3931 if (*skipwhite(s) == NUL
3932 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3933 {
3934 s = skipwhite(ml_get(lnum + 1));
3935 if (*s == '*')
3936 s = skipwhite(s + 1);
3937 }
3938 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003939 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003940 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941 r = alloc((unsigned)(STRLEN(txt)
3942 + STRLEN(dashes) /* for %s */
3943 + 20 /* for %3ld */
3944 + STRLEN(s))); /* concatenated */
3945 if (r != NULL)
3946 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003947 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 len = (int)STRLEN(r);
3949 STRCAT(r, s);
3950 /* remove 'foldmarker' and 'commentstring' */
3951 foldtext_cleanup(r + len);
3952 rettv->vval.v_string = r;
3953 }
3954 }
3955#endif
3956}
3957
3958/*
3959 * "foldtextresult(lnum)" function
3960 */
3961 static void
3962f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3963{
3964#ifdef FEAT_FOLDING
3965 linenr_T lnum;
3966 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003967 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003968 foldinfo_T foldinfo;
3969 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003970 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971#endif
3972
3973 rettv->v_type = VAR_STRING;
3974 rettv->vval.v_string = NULL;
3975#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003976 if (entered)
3977 return; /* reject recursive use */
3978 entered = TRUE;
3979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003980 lnum = get_tv_lnum(argvars);
3981 /* treat illegal types and illegal string values for {lnum} the same */
3982 if (lnum < 0)
3983 lnum = 0;
3984 fold_count = foldedCount(curwin, lnum, &foldinfo);
3985 if (fold_count > 0)
3986 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003987 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3988 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003989 if (text == buf)
3990 text = vim_strsave(text);
3991 rettv->vval.v_string = text;
3992 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003993
3994 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003995#endif
3996}
3997
3998/*
3999 * "foreground()" function
4000 */
4001 static void
4002f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4003{
4004#ifdef FEAT_GUI
4005 if (gui.in_use)
4006 gui_mch_set_foreground();
4007#else
4008# ifdef WIN32
4009 win32_set_foreground();
4010# endif
4011#endif
4012}
4013
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004015common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004016{
4017 char_u *s;
4018 char_u *name;
4019 int use_string = FALSE;
4020 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004021 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022
4023 if (argvars[0].v_type == VAR_FUNC)
4024 {
4025 /* function(MyFunc, [arg], dict) */
4026 s = argvars[0].vval.v_string;
4027 }
4028 else if (argvars[0].v_type == VAR_PARTIAL
4029 && argvars[0].vval.v_partial != NULL)
4030 {
4031 /* function(dict.MyFunc, [arg]) */
4032 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004033 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034 }
4035 else
4036 {
4037 /* function('MyFunc', [arg], dict) */
4038 s = get_tv_string(&argvars[0]);
4039 use_string = TRUE;
4040 }
4041
Bram Moolenaar843b8842016-08-21 14:36:15 +02004042 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004043 {
4044 name = s;
4045 trans_name = trans_function_name(&name, FALSE,
4046 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4047 if (*name != NUL)
4048 s = NULL;
4049 }
4050
Bram Moolenaar843b8842016-08-21 14:36:15 +02004051 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4052 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004053 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004055 else if (trans_name != NULL && (is_funcref
4056 ? find_func(trans_name) == NULL
4057 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058 EMSG2(_("E700: Unknown function: %s"), s);
4059 else
4060 {
4061 int dict_idx = 0;
4062 int arg_idx = 0;
4063 list_T *list = NULL;
4064
4065 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4066 {
4067 char sid_buf[25];
4068 int off = *s == 's' ? 2 : 5;
4069
4070 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4071 * also be called from another script. Using trans_function_name()
4072 * would also work, but some plugins depend on the name being
4073 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004074 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004075 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4076 if (name != NULL)
4077 {
4078 STRCPY(name, sid_buf);
4079 STRCAT(name, s + off);
4080 }
4081 }
4082 else
4083 name = vim_strsave(s);
4084
4085 if (argvars[1].v_type != VAR_UNKNOWN)
4086 {
4087 if (argvars[2].v_type != VAR_UNKNOWN)
4088 {
4089 /* function(name, [args], dict) */
4090 arg_idx = 1;
4091 dict_idx = 2;
4092 }
4093 else if (argvars[1].v_type == VAR_DICT)
4094 /* function(name, dict) */
4095 dict_idx = 1;
4096 else
4097 /* function(name, [args]) */
4098 arg_idx = 1;
4099 if (dict_idx > 0)
4100 {
4101 if (argvars[dict_idx].v_type != VAR_DICT)
4102 {
4103 EMSG(_("E922: expected a dict"));
4104 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004105 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004106 }
4107 if (argvars[dict_idx].vval.v_dict == NULL)
4108 dict_idx = 0;
4109 }
4110 if (arg_idx > 0)
4111 {
4112 if (argvars[arg_idx].v_type != VAR_LIST)
4113 {
4114 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4115 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004116 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004117 }
4118 list = argvars[arg_idx].vval.v_list;
4119 if (list == NULL || list->lv_len == 0)
4120 arg_idx = 0;
4121 }
4122 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004123 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004124 {
4125 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4126
4127 /* result is a VAR_PARTIAL */
4128 if (pt == NULL)
4129 vim_free(name);
4130 else
4131 {
4132 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4133 {
4134 listitem_T *li;
4135 int i = 0;
4136 int arg_len = 0;
4137 int lv_len = 0;
4138
4139 if (arg_pt != NULL)
4140 arg_len = arg_pt->pt_argc;
4141 if (list != NULL)
4142 lv_len = list->lv_len;
4143 pt->pt_argc = arg_len + lv_len;
4144 pt->pt_argv = (typval_T *)alloc(
4145 sizeof(typval_T) * pt->pt_argc);
4146 if (pt->pt_argv == NULL)
4147 {
4148 vim_free(pt);
4149 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004152 for (i = 0; i < arg_len; i++)
4153 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4154 if (lv_len > 0)
4155 for (li = list->lv_first; li != NULL;
4156 li = li->li_next)
4157 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004158 }
4159
4160 /* For "function(dict.func, [], dict)" and "func" is a partial
4161 * use "dict". That is backwards compatible. */
4162 if (dict_idx > 0)
4163 {
4164 /* The dict is bound explicitly, pt_auto is FALSE. */
4165 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4166 ++pt->pt_dict->dv_refcount;
4167 }
4168 else if (arg_pt != NULL)
4169 {
4170 /* If the dict was bound automatically the result is also
4171 * bound automatically. */
4172 pt->pt_dict = arg_pt->pt_dict;
4173 pt->pt_auto = arg_pt->pt_auto;
4174 if (pt->pt_dict != NULL)
4175 ++pt->pt_dict->dv_refcount;
4176 }
4177
4178 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004179 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4180 {
4181 pt->pt_func = arg_pt->pt_func;
4182 func_ptr_ref(pt->pt_func);
4183 vim_free(name);
4184 }
4185 else if (is_funcref)
4186 {
4187 pt->pt_func = find_func(trans_name);
4188 func_ptr_ref(pt->pt_func);
4189 vim_free(name);
4190 }
4191 else
4192 {
4193 pt->pt_name = name;
4194 func_ref(name);
4195 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004196 }
4197 rettv->v_type = VAR_PARTIAL;
4198 rettv->vval.v_partial = pt;
4199 }
4200 else
4201 {
4202 /* result is a VAR_FUNC */
4203 rettv->v_type = VAR_FUNC;
4204 rettv->vval.v_string = name;
4205 func_ref(name);
4206 }
4207 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004208theend:
4209 vim_free(trans_name);
4210}
4211
4212/*
4213 * "funcref()" function
4214 */
4215 static void
4216f_funcref(typval_T *argvars, typval_T *rettv)
4217{
4218 common_function(argvars, rettv, TRUE);
4219}
4220
4221/*
4222 * "function()" function
4223 */
4224 static void
4225f_function(typval_T *argvars, typval_T *rettv)
4226{
4227 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228}
4229
4230/*
4231 * "garbagecollect()" function
4232 */
4233 static void
4234f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4235{
4236 /* This is postponed until we are back at the toplevel, because we may be
4237 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4238 want_garbage_collect = TRUE;
4239
4240 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4241 garbage_collect_at_exit = TRUE;
4242}
4243
4244/*
4245 * "get()" function
4246 */
4247 static void
4248f_get(typval_T *argvars, typval_T *rettv)
4249{
4250 listitem_T *li;
4251 list_T *l;
4252 dictitem_T *di;
4253 dict_T *d;
4254 typval_T *tv = NULL;
4255
4256 if (argvars[0].v_type == VAR_LIST)
4257 {
4258 if ((l = argvars[0].vval.v_list) != NULL)
4259 {
4260 int error = FALSE;
4261
4262 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4263 if (!error && li != NULL)
4264 tv = &li->li_tv;
4265 }
4266 }
4267 else if (argvars[0].v_type == VAR_DICT)
4268 {
4269 if ((d = argvars[0].vval.v_dict) != NULL)
4270 {
4271 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4272 if (di != NULL)
4273 tv = &di->di_tv;
4274 }
4275 }
4276 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4277 {
4278 partial_T *pt;
4279 partial_T fref_pt;
4280
4281 if (argvars[0].v_type == VAR_PARTIAL)
4282 pt = argvars[0].vval.v_partial;
4283 else
4284 {
4285 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4286 fref_pt.pt_name = argvars[0].vval.v_string;
4287 pt = &fref_pt;
4288 }
4289
4290 if (pt != NULL)
4291 {
4292 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004293 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294
4295 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4296 {
4297 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004298 n = partial_name(pt);
4299 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 rettv->vval.v_string = NULL;
4301 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004302 {
4303 rettv->vval.v_string = vim_strsave(n);
4304 if (rettv->v_type == VAR_FUNC)
4305 func_ref(rettv->vval.v_string);
4306 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004307 }
4308 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004309 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310 else if (STRCMP(what, "args") == 0)
4311 {
4312 rettv->v_type = VAR_LIST;
4313 if (rettv_list_alloc(rettv) == OK)
4314 {
4315 int i;
4316
4317 for (i = 0; i < pt->pt_argc; ++i)
4318 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4319 }
4320 }
4321 else
4322 EMSG2(_(e_invarg2), what);
4323 return;
4324 }
4325 }
4326 else
4327 EMSG2(_(e_listdictarg), "get()");
4328
4329 if (tv == NULL)
4330 {
4331 if (argvars[2].v_type != VAR_UNKNOWN)
4332 copy_tv(&argvars[2], rettv);
4333 }
4334 else
4335 copy_tv(tv, rettv);
4336}
4337
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004338#ifdef FEAT_SIGNS
4339/*
4340 * Returns information about signs placed in a buffer as list of dicts.
4341 */
4342 static void
4343get_buffer_signs(buf_T *buf, list_T *l)
4344{
4345 signlist_T *sign;
4346
4347 for (sign = buf->b_signlist; sign; sign = sign->next)
4348 {
4349 dict_T *d = dict_alloc();
4350
4351 if (d != NULL)
4352 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004353 dict_add_number(d, "id", sign->id);
4354 dict_add_number(d, "lnum", sign->lnum);
4355 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004356
4357 list_append_dict(l, d);
4358 }
4359 }
4360}
4361#endif
4362
4363/*
4364 * Returns buffer options, variables and other attributes in a dictionary.
4365 */
4366 static dict_T *
4367get_buffer_info(buf_T *buf)
4368{
4369 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004370 tabpage_T *tp;
4371 win_T *wp;
4372 list_T *windows;
4373
4374 dict = dict_alloc();
4375 if (dict == NULL)
4376 return NULL;
4377
Bram Moolenaare0be1672018-07-08 16:50:37 +02004378 dict_add_number(dict, "bufnr", buf->b_fnum);
4379 dict_add_string(dict, "name", buf->b_ffname);
4380 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4381 : buflist_findlnum(buf));
4382 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4383 dict_add_number(dict, "listed", buf->b_p_bl);
4384 dict_add_number(dict, "changed", bufIsChanged(buf));
4385 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4386 dict_add_number(dict, "hidden",
4387 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004388
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004389 /* Get a reference to buffer variables */
4390 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004391
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004392 /* List of windows displaying this buffer */
4393 windows = list_alloc();
4394 if (windows != NULL)
4395 {
4396 FOR_ALL_TAB_WINDOWS(tp, wp)
4397 if (wp->w_buffer == buf)
4398 list_append_number(windows, (varnumber_T)wp->w_id);
4399 dict_add_list(dict, "windows", windows);
4400 }
4401
4402#ifdef FEAT_SIGNS
4403 if (buf->b_signlist != NULL)
4404 {
4405 /* List of signs placed in this buffer */
4406 list_T *signs = list_alloc();
4407 if (signs != NULL)
4408 {
4409 get_buffer_signs(buf, signs);
4410 dict_add_list(dict, "signs", signs);
4411 }
4412 }
4413#endif
4414
4415 return dict;
4416}
4417
4418/*
4419 * "getbufinfo()" function
4420 */
4421 static void
4422f_getbufinfo(typval_T *argvars, typval_T *rettv)
4423{
4424 buf_T *buf = NULL;
4425 buf_T *argbuf = NULL;
4426 dict_T *d;
4427 int filtered = FALSE;
4428 int sel_buflisted = FALSE;
4429 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004430 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004431
4432 if (rettv_list_alloc(rettv) != OK)
4433 return;
4434
4435 /* List of all the buffers or selected buffers */
4436 if (argvars[0].v_type == VAR_DICT)
4437 {
4438 dict_T *sel_d = argvars[0].vval.v_dict;
4439
4440 if (sel_d != NULL)
4441 {
4442 dictitem_T *di;
4443
4444 filtered = TRUE;
4445
4446 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4447 if (di != NULL && get_tv_number(&di->di_tv))
4448 sel_buflisted = TRUE;
4449
4450 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4451 if (di != NULL && get_tv_number(&di->di_tv))
4452 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004453
4454 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4455 if (di != NULL && get_tv_number(&di->di_tv))
4456 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004457 }
4458 }
4459 else if (argvars[0].v_type != VAR_UNKNOWN)
4460 {
4461 /* Information about one buffer. Argument specifies the buffer */
4462 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4463 ++emsg_off;
4464 argbuf = get_buf_tv(&argvars[0], FALSE);
4465 --emsg_off;
4466 if (argbuf == NULL)
4467 return;
4468 }
4469
4470 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004471 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004472 {
4473 if (argbuf != NULL && argbuf != buf)
4474 continue;
4475 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004476 || (sel_buflisted && !buf->b_p_bl)
4477 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004478 continue;
4479
4480 d = get_buffer_info(buf);
4481 if (d != NULL)
4482 list_append_dict(rettv->vval.v_list, d);
4483 if (argbuf != NULL)
4484 return;
4485 }
4486}
4487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv);
4489
4490/*
4491 * Get line or list of lines from buffer "buf" into "rettv".
4492 * Return a range (from start to end) of lines in rettv from the specified
4493 * buffer.
4494 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4495 */
4496 static void
4497get_buffer_lines(
4498 buf_T *buf,
4499 linenr_T start,
4500 linenr_T end,
4501 int retlist,
4502 typval_T *rettv)
4503{
4504 char_u *p;
4505
4506 rettv->v_type = VAR_STRING;
4507 rettv->vval.v_string = NULL;
4508 if (retlist && rettv_list_alloc(rettv) == FAIL)
4509 return;
4510
4511 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4512 return;
4513
4514 if (!retlist)
4515 {
4516 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4517 p = ml_get_buf(buf, start, FALSE);
4518 else
4519 p = (char_u *)"";
4520 rettv->vval.v_string = vim_strsave(p);
4521 }
4522 else
4523 {
4524 if (end < start)
4525 return;
4526
4527 if (start < 1)
4528 start = 1;
4529 if (end > buf->b_ml.ml_line_count)
4530 end = buf->b_ml.ml_line_count;
4531 while (start <= end)
4532 if (list_append_string(rettv->vval.v_list,
4533 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4534 break;
4535 }
4536}
4537
4538/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539 * "getbufline()" function
4540 */
4541 static void
4542f_getbufline(typval_T *argvars, typval_T *rettv)
4543{
4544 linenr_T lnum;
4545 linenr_T end;
4546 buf_T *buf;
4547
4548 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4549 ++emsg_off;
4550 buf = get_buf_tv(&argvars[0], FALSE);
4551 --emsg_off;
4552
4553 lnum = get_tv_lnum_buf(&argvars[1], buf);
4554 if (argvars[2].v_type == VAR_UNKNOWN)
4555 end = lnum;
4556 else
4557 end = get_tv_lnum_buf(&argvars[2], buf);
4558
4559 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4560}
4561
4562/*
4563 * "getbufvar()" function
4564 */
4565 static void
4566f_getbufvar(typval_T *argvars, typval_T *rettv)
4567{
4568 buf_T *buf;
4569 buf_T *save_curbuf;
4570 char_u *varname;
4571 dictitem_T *v;
4572 int done = FALSE;
4573
4574 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4575 varname = get_tv_string_chk(&argvars[1]);
4576 ++emsg_off;
4577 buf = get_buf_tv(&argvars[0], FALSE);
4578
4579 rettv->v_type = VAR_STRING;
4580 rettv->vval.v_string = NULL;
4581
4582 if (buf != NULL && varname != NULL)
4583 {
4584 /* set curbuf to be our buf, temporarily */
4585 save_curbuf = curbuf;
4586 curbuf = buf;
4587
Bram Moolenaar30567352016-08-27 21:25:44 +02004588 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004590 if (varname[1] == NUL)
4591 {
4592 /* get all buffer-local options in a dict */
4593 dict_T *opts = get_winbuf_options(TRUE);
4594
4595 if (opts != NULL)
4596 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004597 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004598 done = TRUE;
4599 }
4600 }
4601 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4602 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004603 done = TRUE;
4604 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004605 else
4606 {
4607 /* Look up the variable. */
4608 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4609 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4610 'b', varname, FALSE);
4611 if (v != NULL)
4612 {
4613 copy_tv(&v->di_tv, rettv);
4614 done = TRUE;
4615 }
4616 }
4617
4618 /* restore previous notion of curbuf */
4619 curbuf = save_curbuf;
4620 }
4621
4622 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4623 /* use the default value */
4624 copy_tv(&argvars[2], rettv);
4625
4626 --emsg_off;
4627}
4628
4629/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004630 * "getchangelist()" function
4631 */
4632 static void
4633f_getchangelist(typval_T *argvars, typval_T *rettv)
4634{
4635#ifdef FEAT_JUMPLIST
4636 buf_T *buf;
4637 int i;
4638 list_T *l;
4639 dict_T *d;
4640#endif
4641
4642 if (rettv_list_alloc(rettv) != OK)
4643 return;
4644
4645#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4647 ++emsg_off;
4648 buf = get_buf_tv(&argvars[0], FALSE);
4649 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004650 if (buf == NULL)
4651 return;
4652
4653 l = list_alloc();
4654 if (l == NULL)
4655 return;
4656
4657 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4658 return;
4659 /*
4660 * The current window change list index tracks only the position in the
4661 * current buffer change list. For other buffers, use the change list
4662 * length as the current index.
4663 */
4664 list_append_number(rettv->vval.v_list,
4665 (varnumber_T)((buf == curwin->w_buffer)
4666 ? curwin->w_changelistidx : buf->b_changelistlen));
4667
4668 for (i = 0; i < buf->b_changelistlen; ++i)
4669 {
4670 if (buf->b_changelist[i].lnum == 0)
4671 continue;
4672 if ((d = dict_alloc()) == NULL)
4673 return;
4674 if (list_append_dict(l, d) == FAIL)
4675 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004676 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4677 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004678# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004679 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004680# endif
4681 }
4682#endif
4683}
4684/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 * "getchar()" function
4686 */
4687 static void
4688f_getchar(typval_T *argvars, typval_T *rettv)
4689{
4690 varnumber_T n;
4691 int error = FALSE;
4692
Bram Moolenaar84d93902018-09-11 20:10:20 +02004693#ifdef MESSAGE_QUEUE
4694 // vpeekc() used to check for messages, but that caused problems, invoking
4695 // a callback where it was not expected. Some plugins use getchar(1) in a
4696 // loop to await a message, therefore make sure we check for messages here.
4697 parse_queued_messages();
4698#endif
4699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004700 /* Position the cursor. Needed after a message that ends in a space. */
4701 windgoto(msg_row, msg_col);
4702
4703 ++no_mapping;
4704 ++allow_keys;
4705 for (;;)
4706 {
4707 if (argvars[0].v_type == VAR_UNKNOWN)
4708 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004709 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4711 /* getchar(1): only check if char avail */
4712 n = vpeekc_any();
4713 else if (error || vpeekc_any() == NUL)
4714 /* illegal argument or getchar(0) and no char avail: return zero */
4715 n = 0;
4716 else
4717 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004718 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719
4720 if (n == K_IGNORE)
4721 continue;
4722 break;
4723 }
4724 --no_mapping;
4725 --allow_keys;
4726
4727 set_vim_var_nr(VV_MOUSE_WIN, 0);
4728 set_vim_var_nr(VV_MOUSE_WINID, 0);
4729 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4730 set_vim_var_nr(VV_MOUSE_COL, 0);
4731
4732 rettv->vval.v_number = n;
4733 if (IS_SPECIAL(n) || mod_mask != 0)
4734 {
4735 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4736 int i = 0;
4737
4738 /* Turn a special key into three bytes, plus modifier. */
4739 if (mod_mask != 0)
4740 {
4741 temp[i++] = K_SPECIAL;
4742 temp[i++] = KS_MODIFIER;
4743 temp[i++] = mod_mask;
4744 }
4745 if (IS_SPECIAL(n))
4746 {
4747 temp[i++] = K_SPECIAL;
4748 temp[i++] = K_SECOND(n);
4749 temp[i++] = K_THIRD(n);
4750 }
4751#ifdef FEAT_MBYTE
4752 else if (has_mbyte)
4753 i += (*mb_char2bytes)(n, temp + i);
4754#endif
4755 else
4756 temp[i++] = n;
4757 temp[i++] = NUL;
4758 rettv->v_type = VAR_STRING;
4759 rettv->vval.v_string = vim_strsave(temp);
4760
4761#ifdef FEAT_MOUSE
4762 if (is_mouse_key(n))
4763 {
4764 int row = mouse_row;
4765 int col = mouse_col;
4766 win_T *win;
4767 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004768 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004769 int winnr = 1;
4770
4771 if (row >= 0 && col >= 0)
4772 {
4773 /* Find the window at the mouse coordinates and compute the
4774 * text position. */
4775 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004776 if (win == NULL)
4777 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004778 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004779 for (wp = firstwin; wp != win; wp = wp->w_next)
4780 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004781 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4782 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4783 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4784 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4785 }
4786 }
4787#endif
4788 }
4789}
4790
4791/*
4792 * "getcharmod()" function
4793 */
4794 static void
4795f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4796{
4797 rettv->vval.v_number = mod_mask;
4798}
4799
4800/*
4801 * "getcharsearch()" function
4802 */
4803 static void
4804f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4805{
4806 if (rettv_dict_alloc(rettv) != FAIL)
4807 {
4808 dict_T *dict = rettv->vval.v_dict;
4809
Bram Moolenaare0be1672018-07-08 16:50:37 +02004810 dict_add_string(dict, "char", last_csearch());
4811 dict_add_number(dict, "forward", last_csearch_forward());
4812 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004813 }
4814}
4815
4816/*
4817 * "getcmdline()" function
4818 */
4819 static void
4820f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4821{
4822 rettv->v_type = VAR_STRING;
4823 rettv->vval.v_string = get_cmdline_str();
4824}
4825
4826/*
4827 * "getcmdpos()" function
4828 */
4829 static void
4830f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4831{
4832 rettv->vval.v_number = get_cmdline_pos() + 1;
4833}
4834
4835/*
4836 * "getcmdtype()" function
4837 */
4838 static void
4839f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4840{
4841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = alloc(2);
4843 if (rettv->vval.v_string != NULL)
4844 {
4845 rettv->vval.v_string[0] = get_cmdline_type();
4846 rettv->vval.v_string[1] = NUL;
4847 }
4848}
4849
4850/*
4851 * "getcmdwintype()" function
4852 */
4853 static void
4854f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4855{
4856 rettv->v_type = VAR_STRING;
4857 rettv->vval.v_string = NULL;
4858#ifdef FEAT_CMDWIN
4859 rettv->vval.v_string = alloc(2);
4860 if (rettv->vval.v_string != NULL)
4861 {
4862 rettv->vval.v_string[0] = cmdwin_type;
4863 rettv->vval.v_string[1] = NUL;
4864 }
4865#endif
4866}
4867
4868#if defined(FEAT_CMDL_COMPL)
4869/*
4870 * "getcompletion()" function
4871 */
4872 static void
4873f_getcompletion(typval_T *argvars, typval_T *rettv)
4874{
4875 char_u *pat;
4876 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004877 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004878 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4879 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004881 if (argvars[2].v_type != VAR_UNKNOWN)
4882 filtered = get_tv_number_chk(&argvars[2], NULL);
4883
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884 if (p_wic)
4885 options |= WILD_ICASE;
4886
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004887 /* For filtered results, 'wildignore' is used */
4888 if (!filtered)
4889 options |= WILD_KEEP_ALL;
4890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004891 ExpandInit(&xpc);
4892 xpc.xp_pattern = get_tv_string(&argvars[0]);
4893 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4894 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4895 if (xpc.xp_context == EXPAND_NOTHING)
4896 {
4897 if (argvars[1].v_type == VAR_STRING)
4898 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4899 else
4900 EMSG(_(e_invarg));
4901 return;
4902 }
4903
4904# if defined(FEAT_MENU)
4905 if (xpc.xp_context == EXPAND_MENUS)
4906 {
4907 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4908 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4909 }
4910# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004911#ifdef FEAT_CSCOPE
4912 if (xpc.xp_context == EXPAND_CSCOPE)
4913 {
4914 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4915 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4916 }
4917#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004918#ifdef FEAT_SIGNS
4919 if (xpc.xp_context == EXPAND_SIGN)
4920 {
4921 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4922 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4923 }
4924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925
4926 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4927 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4928 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004929 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004930
4931 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4932
4933 for (i = 0; i < xpc.xp_numfiles; i++)
4934 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4935 }
4936 vim_free(pat);
4937 ExpandCleanup(&xpc);
4938}
4939#endif
4940
4941/*
4942 * "getcwd()" function
4943 */
4944 static void
4945f_getcwd(typval_T *argvars, typval_T *rettv)
4946{
4947 win_T *wp = NULL;
4948 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004949 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950
4951 rettv->v_type = VAR_STRING;
4952 rettv->vval.v_string = NULL;
4953
Bram Moolenaar54591292018-02-09 20:53:59 +01004954 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4955 global = TRUE;
4956 else
4957 wp = find_tabwin(&argvars[0], &argvars[1]);
4958
4959 if (wp != NULL && wp->w_localdir != NULL)
4960 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4961 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004963 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964 rettv->vval.v_string = vim_strsave(globaldir);
4965 else
4966 {
4967 cwd = alloc(MAXPATHL);
4968 if (cwd != NULL)
4969 {
4970 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4971 rettv->vval.v_string = vim_strsave(cwd);
4972 vim_free(cwd);
4973 }
4974 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004976#ifdef BACKSLASH_IN_FILENAME
4977 if (rettv->vval.v_string != NULL)
4978 slash_adjust(rettv->vval.v_string);
4979#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004980}
4981
4982/*
4983 * "getfontname()" function
4984 */
4985 static void
4986f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4987{
4988 rettv->v_type = VAR_STRING;
4989 rettv->vval.v_string = NULL;
4990#ifdef FEAT_GUI
4991 if (gui.in_use)
4992 {
4993 GuiFont font;
4994 char_u *name = NULL;
4995
4996 if (argvars[0].v_type == VAR_UNKNOWN)
4997 {
4998 /* Get the "Normal" font. Either the name saved by
4999 * hl_set_font_name() or from the font ID. */
5000 font = gui.norm_font;
5001 name = hl_get_font_name();
5002 }
5003 else
5004 {
5005 name = get_tv_string(&argvars[0]);
5006 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5007 return;
5008 font = gui_mch_get_font(name, FALSE);
5009 if (font == NOFONT)
5010 return; /* Invalid font name, return empty string. */
5011 }
5012 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5013 if (argvars[0].v_type != VAR_UNKNOWN)
5014 gui_mch_free_font(font);
5015 }
5016#endif
5017}
5018
5019/*
5020 * "getfperm({fname})" function
5021 */
5022 static void
5023f_getfperm(typval_T *argvars, typval_T *rettv)
5024{
5025 char_u *fname;
5026 stat_T st;
5027 char_u *perm = NULL;
5028 char_u flags[] = "rwx";
5029 int i;
5030
5031 fname = get_tv_string(&argvars[0]);
5032
5033 rettv->v_type = VAR_STRING;
5034 if (mch_stat((char *)fname, &st) >= 0)
5035 {
5036 perm = vim_strsave((char_u *)"---------");
5037 if (perm != NULL)
5038 {
5039 for (i = 0; i < 9; i++)
5040 {
5041 if (st.st_mode & (1 << (8 - i)))
5042 perm[i] = flags[i % 3];
5043 }
5044 }
5045 }
5046 rettv->vval.v_string = perm;
5047}
5048
5049/*
5050 * "getfsize({fname})" function
5051 */
5052 static void
5053f_getfsize(typval_T *argvars, typval_T *rettv)
5054{
5055 char_u *fname;
5056 stat_T st;
5057
5058 fname = get_tv_string(&argvars[0]);
5059
5060 rettv->v_type = VAR_NUMBER;
5061
5062 if (mch_stat((char *)fname, &st) >= 0)
5063 {
5064 if (mch_isdir(fname))
5065 rettv->vval.v_number = 0;
5066 else
5067 {
5068 rettv->vval.v_number = (varnumber_T)st.st_size;
5069
5070 /* non-perfect check for overflow */
5071 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5072 rettv->vval.v_number = -2;
5073 }
5074 }
5075 else
5076 rettv->vval.v_number = -1;
5077}
5078
5079/*
5080 * "getftime({fname})" function
5081 */
5082 static void
5083f_getftime(typval_T *argvars, typval_T *rettv)
5084{
5085 char_u *fname;
5086 stat_T st;
5087
5088 fname = get_tv_string(&argvars[0]);
5089
5090 if (mch_stat((char *)fname, &st) >= 0)
5091 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5092 else
5093 rettv->vval.v_number = -1;
5094}
5095
5096/*
5097 * "getftype({fname})" function
5098 */
5099 static void
5100f_getftype(typval_T *argvars, typval_T *rettv)
5101{
5102 char_u *fname;
5103 stat_T st;
5104 char_u *type = NULL;
5105 char *t;
5106
5107 fname = get_tv_string(&argvars[0]);
5108
5109 rettv->v_type = VAR_STRING;
5110 if (mch_lstat((char *)fname, &st) >= 0)
5111 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 if (S_ISREG(st.st_mode))
5113 t = "file";
5114 else if (S_ISDIR(st.st_mode))
5115 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 else if (S_ISLNK(st.st_mode))
5117 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else if (S_ISBLK(st.st_mode))
5119 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 else if (S_ISCHR(st.st_mode))
5121 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 else if (S_ISFIFO(st.st_mode))
5123 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005124 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005125 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005126 else
5127 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 type = vim_strsave((char_u *)t);
5129 }
5130 rettv->vval.v_string = type;
5131}
5132
5133/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005134 * "getjumplist()" function
5135 */
5136 static void
5137f_getjumplist(typval_T *argvars, typval_T *rettv)
5138{
5139#ifdef FEAT_JUMPLIST
5140 win_T *wp;
5141 int i;
5142 list_T *l;
5143 dict_T *d;
5144#endif
5145
5146 if (rettv_list_alloc(rettv) != OK)
5147 return;
5148
5149#ifdef FEAT_JUMPLIST
5150 wp = find_tabwin(&argvars[0], &argvars[1]);
5151 if (wp == NULL)
5152 return;
5153
5154 l = list_alloc();
5155 if (l == NULL)
5156 return;
5157
5158 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5159 return;
5160 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5161
Bram Moolenaar48679742018-02-13 13:33:29 +01005162 cleanup_jumplist(wp, TRUE);
5163
Bram Moolenaar4f505882018-02-10 21:06:32 +01005164 for (i = 0; i < wp->w_jumplistlen; ++i)
5165 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005166 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5167 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005168 if ((d = dict_alloc()) == NULL)
5169 return;
5170 if (list_append_dict(l, d) == FAIL)
5171 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005172 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5173 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005174# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005175 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005176# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005177 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005178 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005179 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005180 }
5181#endif
5182}
5183
5184/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005185 * "getline(lnum, [end])" function
5186 */
5187 static void
5188f_getline(typval_T *argvars, typval_T *rettv)
5189{
5190 linenr_T lnum;
5191 linenr_T end;
5192 int retlist;
5193
5194 lnum = get_tv_lnum(argvars);
5195 if (argvars[1].v_type == VAR_UNKNOWN)
5196 {
5197 end = 0;
5198 retlist = FALSE;
5199 }
5200 else
5201 {
5202 end = get_tv_lnum(&argvars[1]);
5203 retlist = TRUE;
5204 }
5205
5206 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5207}
5208
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005209#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005210 static void
5211get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5212{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005213 if (what_arg->v_type == VAR_UNKNOWN)
5214 {
5215 if (rettv_list_alloc(rettv) == OK)
5216 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005217 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005218 }
5219 else
5220 {
5221 if (rettv_dict_alloc(rettv) == OK)
5222 if (is_qf || (wp != NULL))
5223 {
5224 if (what_arg->v_type == VAR_DICT)
5225 {
5226 dict_T *d = what_arg->vval.v_dict;
5227
5228 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005229 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005230 }
5231 else
5232 EMSG(_(e_dictreq));
5233 }
5234 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005235}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005236#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005237
5238/*
5239 * "getloclist()" function
5240 */
5241 static void
5242f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5243{
5244#ifdef FEAT_QUICKFIX
5245 win_T *wp;
5246
5247 wp = find_win_by_nr(&argvars[0], NULL);
5248 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5249#endif
5250}
5251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252/*
5253 * "getmatches()" function
5254 */
5255 static void
5256f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5257{
5258#ifdef FEAT_SEARCH_EXTRA
5259 dict_T *dict;
5260 matchitem_T *cur = curwin->w_match_head;
5261 int i;
5262
5263 if (rettv_list_alloc(rettv) == OK)
5264 {
5265 while (cur != NULL)
5266 {
5267 dict = dict_alloc();
5268 if (dict == NULL)
5269 return;
5270 if (cur->match.regprog == NULL)
5271 {
5272 /* match added with matchaddpos() */
5273 for (i = 0; i < MAXPOSMATCH; ++i)
5274 {
5275 llpos_T *llpos;
5276 char buf[6];
5277 list_T *l;
5278
5279 llpos = &cur->pos.pos[i];
5280 if (llpos->lnum == 0)
5281 break;
5282 l = list_alloc();
5283 if (l == NULL)
5284 break;
5285 list_append_number(l, (varnumber_T)llpos->lnum);
5286 if (llpos->col > 0)
5287 {
5288 list_append_number(l, (varnumber_T)llpos->col);
5289 list_append_number(l, (varnumber_T)llpos->len);
5290 }
5291 sprintf(buf, "pos%d", i + 1);
5292 dict_add_list(dict, buf, l);
5293 }
5294 }
5295 else
5296 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005297 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005298 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005299 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5300 dict_add_number(dict, "priority", (long)cur->priority);
5301 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005302# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5303 if (cur->conceal_char)
5304 {
5305 char_u buf[MB_MAXBYTES + 1];
5306
5307 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005308 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309 }
5310# endif
5311 list_append_dict(rettv->vval.v_list, dict);
5312 cur = cur->next;
5313 }
5314 }
5315#endif
5316}
5317
5318/*
5319 * "getpid()" function
5320 */
5321 static void
5322f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5323{
5324 rettv->vval.v_number = mch_get_pid();
5325}
5326
5327 static void
5328getpos_both(
5329 typval_T *argvars,
5330 typval_T *rettv,
5331 int getcurpos)
5332{
5333 pos_T *fp;
5334 list_T *l;
5335 int fnum = -1;
5336
5337 if (rettv_list_alloc(rettv) == OK)
5338 {
5339 l = rettv->vval.v_list;
5340 if (getcurpos)
5341 fp = &curwin->w_cursor;
5342 else
5343 fp = var2fpos(&argvars[0], TRUE, &fnum);
5344 if (fnum != -1)
5345 list_append_number(l, (varnumber_T)fnum);
5346 else
5347 list_append_number(l, (varnumber_T)0);
5348 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5349 : (varnumber_T)0);
5350 list_append_number(l, (fp != NULL)
5351 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5352 : (varnumber_T)0);
5353 list_append_number(l,
5354#ifdef FEAT_VIRTUALEDIT
5355 (fp != NULL) ? (varnumber_T)fp->coladd :
5356#endif
5357 (varnumber_T)0);
5358 if (getcurpos)
5359 {
5360 update_curswant();
5361 list_append_number(l, curwin->w_curswant == MAXCOL ?
5362 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5363 }
5364 }
5365 else
5366 rettv->vval.v_number = FALSE;
5367}
5368
5369
5370/*
5371 * "getcurpos()" function
5372 */
5373 static void
5374f_getcurpos(typval_T *argvars, typval_T *rettv)
5375{
5376 getpos_both(argvars, rettv, TRUE);
5377}
5378
5379/*
5380 * "getpos(string)" function
5381 */
5382 static void
5383f_getpos(typval_T *argvars, typval_T *rettv)
5384{
5385 getpos_both(argvars, rettv, FALSE);
5386}
5387
5388/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005389 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390 */
5391 static void
5392f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5393{
5394#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005395 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396#endif
5397}
5398
5399/*
5400 * "getreg()" function
5401 */
5402 static void
5403f_getreg(typval_T *argvars, typval_T *rettv)
5404{
5405 char_u *strregname;
5406 int regname;
5407 int arg2 = FALSE;
5408 int return_list = FALSE;
5409 int error = FALSE;
5410
5411 if (argvars[0].v_type != VAR_UNKNOWN)
5412 {
5413 strregname = get_tv_string_chk(&argvars[0]);
5414 error = strregname == NULL;
5415 if (argvars[1].v_type != VAR_UNKNOWN)
5416 {
5417 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5418 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5419 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5420 }
5421 }
5422 else
5423 strregname = get_vim_var_str(VV_REG);
5424
5425 if (error)
5426 return;
5427
5428 regname = (strregname == NULL ? '"' : *strregname);
5429 if (regname == 0)
5430 regname = '"';
5431
5432 if (return_list)
5433 {
5434 rettv->v_type = VAR_LIST;
5435 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5436 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5437 if (rettv->vval.v_list == NULL)
5438 (void)rettv_list_alloc(rettv);
5439 else
5440 ++rettv->vval.v_list->lv_refcount;
5441 }
5442 else
5443 {
5444 rettv->v_type = VAR_STRING;
5445 rettv->vval.v_string = get_reg_contents(regname,
5446 arg2 ? GREG_EXPR_SRC : 0);
5447 }
5448}
5449
5450/*
5451 * "getregtype()" function
5452 */
5453 static void
5454f_getregtype(typval_T *argvars, typval_T *rettv)
5455{
5456 char_u *strregname;
5457 int regname;
5458 char_u buf[NUMBUFLEN + 2];
5459 long reglen = 0;
5460
5461 if (argvars[0].v_type != VAR_UNKNOWN)
5462 {
5463 strregname = get_tv_string_chk(&argvars[0]);
5464 if (strregname == NULL) /* type error; errmsg already given */
5465 {
5466 rettv->v_type = VAR_STRING;
5467 rettv->vval.v_string = NULL;
5468 return;
5469 }
5470 }
5471 else
5472 /* Default to v:register */
5473 strregname = get_vim_var_str(VV_REG);
5474
5475 regname = (strregname == NULL ? '"' : *strregname);
5476 if (regname == 0)
5477 regname = '"';
5478
5479 buf[0] = NUL;
5480 buf[1] = NUL;
5481 switch (get_reg_type(regname, &reglen))
5482 {
5483 case MLINE: buf[0] = 'V'; break;
5484 case MCHAR: buf[0] = 'v'; break;
5485 case MBLOCK:
5486 buf[0] = Ctrl_V;
5487 sprintf((char *)buf + 1, "%ld", reglen + 1);
5488 break;
5489 }
5490 rettv->v_type = VAR_STRING;
5491 rettv->vval.v_string = vim_strsave(buf);
5492}
5493
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005494/*
5495 * Returns information (variables, options, etc.) about a tab page
5496 * as a dictionary.
5497 */
5498 static dict_T *
5499get_tabpage_info(tabpage_T *tp, int tp_idx)
5500{
5501 win_T *wp;
5502 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005503 list_T *l;
5504
5505 dict = dict_alloc();
5506 if (dict == NULL)
5507 return NULL;
5508
Bram Moolenaare0be1672018-07-08 16:50:37 +02005509 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005510
5511 l = list_alloc();
5512 if (l != NULL)
5513 {
5514 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5515 wp; wp = wp->w_next)
5516 list_append_number(l, (varnumber_T)wp->w_id);
5517 dict_add_list(dict, "windows", l);
5518 }
5519
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005520 /* Make a reference to tabpage variables */
5521 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005522
5523 return dict;
5524}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005525
5526/*
5527 * "gettabinfo()" function
5528 */
5529 static void
5530f_gettabinfo(typval_T *argvars, typval_T *rettv)
5531{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005532 tabpage_T *tp, *tparg = NULL;
5533 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005534 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005535
5536 if (rettv_list_alloc(rettv) != OK)
5537 return;
5538
5539 if (argvars[0].v_type != VAR_UNKNOWN)
5540 {
5541 /* Information about one tab page */
5542 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5543 if (tparg == NULL)
5544 return;
5545 }
5546
5547 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005548 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005549 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005550 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551 if (tparg != NULL && tp != tparg)
5552 continue;
5553 d = get_tabpage_info(tp, tpnr);
5554 if (d != NULL)
5555 list_append_dict(rettv->vval.v_list, d);
5556 if (tparg != NULL)
5557 return;
5558 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005559}
5560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005561/*
5562 * "gettabvar()" function
5563 */
5564 static void
5565f_gettabvar(typval_T *argvars, typval_T *rettv)
5566{
5567 win_T *oldcurwin;
5568 tabpage_T *tp, *oldtabpage;
5569 dictitem_T *v;
5570 char_u *varname;
5571 int done = FALSE;
5572
5573 rettv->v_type = VAR_STRING;
5574 rettv->vval.v_string = NULL;
5575
5576 varname = get_tv_string_chk(&argvars[1]);
5577 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5578 if (tp != NULL && varname != NULL)
5579 {
5580 /* Set tp to be our tabpage, temporarily. Also set the window to the
5581 * first window in the tabpage, otherwise the window is not valid. */
5582 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005583 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5584 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 {
5586 /* look up the variable */
5587 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5588 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5589 if (v != NULL)
5590 {
5591 copy_tv(&v->di_tv, rettv);
5592 done = TRUE;
5593 }
5594 }
5595
5596 /* restore previous notion of curwin */
5597 restore_win(oldcurwin, oldtabpage, TRUE);
5598 }
5599
5600 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5601 copy_tv(&argvars[2], rettv);
5602}
5603
5604/*
5605 * "gettabwinvar()" function
5606 */
5607 static void
5608f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5609{
5610 getwinvar(argvars, rettv, 1);
5611}
5612
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005613/*
5614 * Returns information about a window as a dictionary.
5615 */
5616 static dict_T *
5617get_win_info(win_T *wp, short tpnr, short winnr)
5618{
5619 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005620
5621 dict = dict_alloc();
5622 if (dict == NULL)
5623 return NULL;
5624
Bram Moolenaare0be1672018-07-08 16:50:37 +02005625 dict_add_number(dict, "tabnr", tpnr);
5626 dict_add_number(dict, "winnr", winnr);
5627 dict_add_number(dict, "winid", wp->w_id);
5628 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005629 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005630#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005631 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005632#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005633 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005634 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005635 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636
Bram Moolenaar69905d12017-08-13 18:14:47 +02005637#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005638 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005639#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005640#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005641 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5642 dict_add_number(dict, "loclist",
5643 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005644#endif
5645
Bram Moolenaar30567352016-08-27 21:25:44 +02005646 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005647 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005648
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649 return dict;
5650}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005651
5652/*
5653 * "getwininfo()" function
5654 */
5655 static void
5656f_getwininfo(typval_T *argvars, typval_T *rettv)
5657{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658 tabpage_T *tp;
5659 win_T *wp = NULL, *wparg = NULL;
5660 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005661 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005662
5663 if (rettv_list_alloc(rettv) != OK)
5664 return;
5665
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005666 if (argvars[0].v_type != VAR_UNKNOWN)
5667 {
5668 wparg = win_id2wp(argvars);
5669 if (wparg == NULL)
5670 return;
5671 }
5672
5673 /* Collect information about either all the windows across all the tab
5674 * pages or one particular window.
5675 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005676 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005677 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005678 tabnr++;
5679 winnr = 0;
5680 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005681 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005682 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005683 if (wparg != NULL && wp != wparg)
5684 continue;
5685 d = get_win_info(wp, tabnr, winnr);
5686 if (d != NULL)
5687 list_append_dict(rettv->vval.v_list, d);
5688 if (wparg != NULL)
5689 /* found information about a specific window */
5690 return;
5691 }
5692 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005693}
5694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 * "win_findbuf()" function
5697 */
5698 static void
5699f_win_findbuf(typval_T *argvars, typval_T *rettv)
5700{
5701 if (rettv_list_alloc(rettv) != FAIL)
5702 win_findbuf(argvars, rettv->vval.v_list);
5703}
5704
5705/*
5706 * "win_getid()" function
5707 */
5708 static void
5709f_win_getid(typval_T *argvars, typval_T *rettv)
5710{
5711 rettv->vval.v_number = win_getid(argvars);
5712}
5713
5714/*
5715 * "win_gotoid()" function
5716 */
5717 static void
5718f_win_gotoid(typval_T *argvars, typval_T *rettv)
5719{
5720 rettv->vval.v_number = win_gotoid(argvars);
5721}
5722
5723/*
5724 * "win_id2tabwin()" function
5725 */
5726 static void
5727f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5728{
5729 if (rettv_list_alloc(rettv) != FAIL)
5730 win_id2tabwin(argvars, rettv->vval.v_list);
5731}
5732
5733/*
5734 * "win_id2win()" function
5735 */
5736 static void
5737f_win_id2win(typval_T *argvars, typval_T *rettv)
5738{
5739 rettv->vval.v_number = win_id2win(argvars);
5740}
5741
5742/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005743 * "win_screenpos()" function
5744 */
5745 static void
5746f_win_screenpos(typval_T *argvars, typval_T *rettv)
5747{
5748 win_T *wp;
5749
5750 if (rettv_list_alloc(rettv) == FAIL)
5751 return;
5752
5753 wp = find_win_by_nr(&argvars[0], NULL);
5754 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5755 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5756}
5757
5758/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005759 * "getwinpos({timeout})" function
5760 */
5761 static void
5762f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5763{
5764 int x = -1;
5765 int y = -1;
5766
5767 if (rettv_list_alloc(rettv) == FAIL)
5768 return;
5769#ifdef FEAT_GUI
5770 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005771 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005772# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5773 else
5774# endif
5775#endif
5776#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5777 {
5778 varnumber_T timeout = 100;
5779
5780 if (argvars[0].v_type != VAR_UNKNOWN)
5781 timeout = get_tv_number(&argvars[0]);
5782 term_get_winpos(&x, &y, timeout);
5783 }
5784#endif
5785 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5786 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5787}
5788
5789
5790/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005791 * "getwinposx()" function
5792 */
5793 static void
5794f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5795{
5796 rettv->vval.v_number = -1;
5797#ifdef FEAT_GUI
5798 if (gui.in_use)
5799 {
5800 int x, y;
5801
5802 if (gui_mch_get_winpos(&x, &y) == OK)
5803 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005804 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005805 }
5806#endif
5807#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5808 {
5809 int x, y;
5810
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005811 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005812 rettv->vval.v_number = x;
5813 }
5814#endif
5815}
5816
5817/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005818 * "getwinposy()" function
5819 */
5820 static void
5821f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5822{
5823 rettv->vval.v_number = -1;
5824#ifdef FEAT_GUI
5825 if (gui.in_use)
5826 {
5827 int x, y;
5828
5829 if (gui_mch_get_winpos(&x, &y) == OK)
5830 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005831 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 }
5833#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005834#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5835 {
5836 int x, y;
5837
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005838 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005839 rettv->vval.v_number = y;
5840 }
5841#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005842}
5843
5844/*
5845 * "getwinvar()" function
5846 */
5847 static void
5848f_getwinvar(typval_T *argvars, typval_T *rettv)
5849{
5850 getwinvar(argvars, rettv, 0);
5851}
5852
5853/*
5854 * "glob()" function
5855 */
5856 static void
5857f_glob(typval_T *argvars, typval_T *rettv)
5858{
5859 int options = WILD_SILENT|WILD_USE_NL;
5860 expand_T xpc;
5861 int error = FALSE;
5862
5863 /* When the optional second argument is non-zero, don't remove matches
5864 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5865 rettv->v_type = VAR_STRING;
5866 if (argvars[1].v_type != VAR_UNKNOWN)
5867 {
5868 if (get_tv_number_chk(&argvars[1], &error))
5869 options |= WILD_KEEP_ALL;
5870 if (argvars[2].v_type != VAR_UNKNOWN)
5871 {
5872 if (get_tv_number_chk(&argvars[2], &error))
5873 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005874 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005875 }
5876 if (argvars[3].v_type != VAR_UNKNOWN
5877 && get_tv_number_chk(&argvars[3], &error))
5878 options |= WILD_ALLLINKS;
5879 }
5880 }
5881 if (!error)
5882 {
5883 ExpandInit(&xpc);
5884 xpc.xp_context = EXPAND_FILES;
5885 if (p_wic)
5886 options += WILD_ICASE;
5887 if (rettv->v_type == VAR_STRING)
5888 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5889 NULL, options, WILD_ALL);
5890 else if (rettv_list_alloc(rettv) != FAIL)
5891 {
5892 int i;
5893
5894 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5895 NULL, options, WILD_ALL_KEEP);
5896 for (i = 0; i < xpc.xp_numfiles; i++)
5897 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5898
5899 ExpandCleanup(&xpc);
5900 }
5901 }
5902 else
5903 rettv->vval.v_string = NULL;
5904}
5905
5906/*
5907 * "globpath()" function
5908 */
5909 static void
5910f_globpath(typval_T *argvars, typval_T *rettv)
5911{
5912 int flags = 0;
5913 char_u buf1[NUMBUFLEN];
5914 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5915 int error = FALSE;
5916 garray_T ga;
5917 int i;
5918
5919 /* When the optional second argument is non-zero, don't remove matches
5920 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5921 rettv->v_type = VAR_STRING;
5922 if (argvars[2].v_type != VAR_UNKNOWN)
5923 {
5924 if (get_tv_number_chk(&argvars[2], &error))
5925 flags |= WILD_KEEP_ALL;
5926 if (argvars[3].v_type != VAR_UNKNOWN)
5927 {
5928 if (get_tv_number_chk(&argvars[3], &error))
5929 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005930 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005931 }
5932 if (argvars[4].v_type != VAR_UNKNOWN
5933 && get_tv_number_chk(&argvars[4], &error))
5934 flags |= WILD_ALLLINKS;
5935 }
5936 }
5937 if (file != NULL && !error)
5938 {
5939 ga_init2(&ga, (int)sizeof(char_u *), 10);
5940 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5941 if (rettv->v_type == VAR_STRING)
5942 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5943 else if (rettv_list_alloc(rettv) != FAIL)
5944 for (i = 0; i < ga.ga_len; ++i)
5945 list_append_string(rettv->vval.v_list,
5946 ((char_u **)(ga.ga_data))[i], -1);
5947 ga_clear_strings(&ga);
5948 }
5949 else
5950 rettv->vval.v_string = NULL;
5951}
5952
5953/*
5954 * "glob2regpat()" function
5955 */
5956 static void
5957f_glob2regpat(typval_T *argvars, typval_T *rettv)
5958{
5959 char_u *pat = get_tv_string_chk(&argvars[0]);
5960
5961 rettv->v_type = VAR_STRING;
5962 rettv->vval.v_string = (pat == NULL)
5963 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5964}
5965
5966/* for VIM_VERSION_ defines */
5967#include "version.h"
5968
5969/*
5970 * "has()" function
5971 */
5972 static void
5973f_has(typval_T *argvars, typval_T *rettv)
5974{
5975 int i;
5976 char_u *name;
5977 int n = FALSE;
5978 static char *(has_list[]) =
5979 {
5980#ifdef AMIGA
5981 "amiga",
5982# ifdef FEAT_ARP
5983 "arp",
5984# endif
5985#endif
5986#ifdef __BEOS__
5987 "beos",
5988#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005989#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005990 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5991 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005992# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005993 "macunix", /* Mac OS X, with the darwin feature */
5994 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005995# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005996#endif
5997#ifdef __QNX__
5998 "qnx",
5999#endif
6000#ifdef UNIX
6001 "unix",
6002#endif
6003#ifdef VMS
6004 "vms",
6005#endif
6006#ifdef WIN32
6007 "win32",
6008#endif
6009#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6010 "win32unix",
6011#endif
6012#if defined(WIN64) || defined(_WIN64)
6013 "win64",
6014#endif
6015#ifdef EBCDIC
6016 "ebcdic",
6017#endif
6018#ifndef CASE_INSENSITIVE_FILENAME
6019 "fname_case",
6020#endif
6021#ifdef HAVE_ACL
6022 "acl",
6023#endif
6024#ifdef FEAT_ARABIC
6025 "arabic",
6026#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006028#ifdef FEAT_AUTOCHDIR
6029 "autochdir",
6030#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006031#ifdef FEAT_AUTOSERVERNAME
6032 "autoservername",
6033#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006034#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 "balloon_eval",
6036# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6037 "balloon_multiline",
6038# endif
6039#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006040#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006041 "balloon_eval_term",
6042#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006043#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6044 "builtin_terms",
6045# ifdef ALL_BUILTIN_TCAPS
6046 "all_builtin_terms",
6047# endif
6048#endif
6049#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6050 || defined(FEAT_GUI_W32) \
6051 || defined(FEAT_GUI_MOTIF))
6052 "browsefilter",
6053#endif
6054#ifdef FEAT_BYTEOFF
6055 "byte_offset",
6056#endif
6057#ifdef FEAT_JOB_CHANNEL
6058 "channel",
6059#endif
6060#ifdef FEAT_CINDENT
6061 "cindent",
6062#endif
6063#ifdef FEAT_CLIENTSERVER
6064 "clientserver",
6065#endif
6066#ifdef FEAT_CLIPBOARD
6067 "clipboard",
6068#endif
6069#ifdef FEAT_CMDL_COMPL
6070 "cmdline_compl",
6071#endif
6072#ifdef FEAT_CMDHIST
6073 "cmdline_hist",
6074#endif
6075#ifdef FEAT_COMMENTS
6076 "comments",
6077#endif
6078#ifdef FEAT_CONCEAL
6079 "conceal",
6080#endif
6081#ifdef FEAT_CRYPT
6082 "cryptv",
6083 "crypt-blowfish",
6084 "crypt-blowfish2",
6085#endif
6086#ifdef FEAT_CSCOPE
6087 "cscope",
6088#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006090#ifdef CURSOR_SHAPE
6091 "cursorshape",
6092#endif
6093#ifdef DEBUG
6094 "debug",
6095#endif
6096#ifdef FEAT_CON_DIALOG
6097 "dialog_con",
6098#endif
6099#ifdef FEAT_GUI_DIALOG
6100 "dialog_gui",
6101#endif
6102#ifdef FEAT_DIFF
6103 "diff",
6104#endif
6105#ifdef FEAT_DIGRAPHS
6106 "digraphs",
6107#endif
6108#ifdef FEAT_DIRECTX
6109 "directx",
6110#endif
6111#ifdef FEAT_DND
6112 "dnd",
6113#endif
6114#ifdef FEAT_EMACS_TAGS
6115 "emacs_tags",
6116#endif
6117 "eval", /* always present, of course! */
6118 "ex_extra", /* graduated feature */
6119#ifdef FEAT_SEARCH_EXTRA
6120 "extra_search",
6121#endif
6122#ifdef FEAT_FKMAP
6123 "farsi",
6124#endif
6125#ifdef FEAT_SEARCHPATH
6126 "file_in_path",
6127#endif
6128#ifdef FEAT_FILTERPIPE
6129 "filterpipe",
6130#endif
6131#ifdef FEAT_FIND_ID
6132 "find_in_path",
6133#endif
6134#ifdef FEAT_FLOAT
6135 "float",
6136#endif
6137#ifdef FEAT_FOLDING
6138 "folding",
6139#endif
6140#ifdef FEAT_FOOTER
6141 "footer",
6142#endif
6143#if !defined(USE_SYSTEM) && defined(UNIX)
6144 "fork",
6145#endif
6146#ifdef FEAT_GETTEXT
6147 "gettext",
6148#endif
6149#ifdef FEAT_GUI
6150 "gui",
6151#endif
6152#ifdef FEAT_GUI_ATHENA
6153# ifdef FEAT_GUI_NEXTAW
6154 "gui_neXtaw",
6155# else
6156 "gui_athena",
6157# endif
6158#endif
6159#ifdef FEAT_GUI_GTK
6160 "gui_gtk",
6161# ifdef USE_GTK3
6162 "gui_gtk3",
6163# else
6164 "gui_gtk2",
6165# endif
6166#endif
6167#ifdef FEAT_GUI_GNOME
6168 "gui_gnome",
6169#endif
6170#ifdef FEAT_GUI_MAC
6171 "gui_mac",
6172#endif
6173#ifdef FEAT_GUI_MOTIF
6174 "gui_motif",
6175#endif
6176#ifdef FEAT_GUI_PHOTON
6177 "gui_photon",
6178#endif
6179#ifdef FEAT_GUI_W32
6180 "gui_win32",
6181#endif
6182#ifdef FEAT_HANGULIN
6183 "hangul_input",
6184#endif
6185#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6186 "iconv",
6187#endif
6188#ifdef FEAT_INS_EXPAND
6189 "insert_expand",
6190#endif
6191#ifdef FEAT_JOB_CHANNEL
6192 "job",
6193#endif
6194#ifdef FEAT_JUMPLIST
6195 "jumplist",
6196#endif
6197#ifdef FEAT_KEYMAP
6198 "keymap",
6199#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006200 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006201#ifdef FEAT_LANGMAP
6202 "langmap",
6203#endif
6204#ifdef FEAT_LIBCALL
6205 "libcall",
6206#endif
6207#ifdef FEAT_LINEBREAK
6208 "linebreak",
6209#endif
6210#ifdef FEAT_LISP
6211 "lispindent",
6212#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006214#ifdef FEAT_LOCALMAP
6215 "localmap",
6216#endif
6217#ifdef FEAT_LUA
6218# ifndef DYNAMIC_LUA
6219 "lua",
6220# endif
6221#endif
6222#ifdef FEAT_MENU
6223 "menu",
6224#endif
6225#ifdef FEAT_SESSION
6226 "mksession",
6227#endif
6228#ifdef FEAT_MODIFY_FNAME
6229 "modify_fname",
6230#endif
6231#ifdef FEAT_MOUSE
6232 "mouse",
6233#endif
6234#ifdef FEAT_MOUSESHAPE
6235 "mouseshape",
6236#endif
6237#if defined(UNIX) || defined(VMS)
6238# ifdef FEAT_MOUSE_DEC
6239 "mouse_dec",
6240# endif
6241# ifdef FEAT_MOUSE_GPM
6242 "mouse_gpm",
6243# endif
6244# ifdef FEAT_MOUSE_JSB
6245 "mouse_jsbterm",
6246# endif
6247# ifdef FEAT_MOUSE_NET
6248 "mouse_netterm",
6249# endif
6250# ifdef FEAT_MOUSE_PTERM
6251 "mouse_pterm",
6252# endif
6253# ifdef FEAT_MOUSE_SGR
6254 "mouse_sgr",
6255# endif
6256# ifdef FEAT_SYSMOUSE
6257 "mouse_sysmouse",
6258# endif
6259# ifdef FEAT_MOUSE_URXVT
6260 "mouse_urxvt",
6261# endif
6262# ifdef FEAT_MOUSE_XTERM
6263 "mouse_xterm",
6264# endif
6265#endif
6266#ifdef FEAT_MBYTE
6267 "multi_byte",
6268#endif
6269#ifdef FEAT_MBYTE_IME
6270 "multi_byte_ime",
6271#endif
6272#ifdef FEAT_MULTI_LANG
6273 "multi_lang",
6274#endif
6275#ifdef FEAT_MZSCHEME
6276#ifndef DYNAMIC_MZSCHEME
6277 "mzscheme",
6278#endif
6279#endif
6280#ifdef FEAT_NUM64
6281 "num64",
6282#endif
6283#ifdef FEAT_OLE
6284 "ole",
6285#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006286#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006287 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006288#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289#ifdef FEAT_PATH_EXTRA
6290 "path_extra",
6291#endif
6292#ifdef FEAT_PERL
6293#ifndef DYNAMIC_PERL
6294 "perl",
6295#endif
6296#endif
6297#ifdef FEAT_PERSISTENT_UNDO
6298 "persistent_undo",
6299#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006300#if defined(FEAT_PYTHON)
6301 "python_compiled",
6302# if defined(DYNAMIC_PYTHON)
6303 "python_dynamic",
6304# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006305 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006306 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006307# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006309#if defined(FEAT_PYTHON3)
6310 "python3_compiled",
6311# if defined(DYNAMIC_PYTHON3)
6312 "python3_dynamic",
6313# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006315 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006316# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006317#endif
6318#ifdef FEAT_POSTSCRIPT
6319 "postscript",
6320#endif
6321#ifdef FEAT_PRINTER
6322 "printer",
6323#endif
6324#ifdef FEAT_PROFILE
6325 "profile",
6326#endif
6327#ifdef FEAT_RELTIME
6328 "reltime",
6329#endif
6330#ifdef FEAT_QUICKFIX
6331 "quickfix",
6332#endif
6333#ifdef FEAT_RIGHTLEFT
6334 "rightleft",
6335#endif
6336#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6337 "ruby",
6338#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340#ifdef FEAT_CMDL_INFO
6341 "showcmd",
6342 "cmdline_info",
6343#endif
6344#ifdef FEAT_SIGNS
6345 "signs",
6346#endif
6347#ifdef FEAT_SMARTINDENT
6348 "smartindent",
6349#endif
6350#ifdef STARTUPTIME
6351 "startuptime",
6352#endif
6353#ifdef FEAT_STL_OPT
6354 "statusline",
6355#endif
6356#ifdef FEAT_SUN_WORKSHOP
6357 "sun_workshop",
6358#endif
6359#ifdef FEAT_NETBEANS_INTG
6360 "netbeans_intg",
6361#endif
6362#ifdef FEAT_SPELL
6363 "spell",
6364#endif
6365#ifdef FEAT_SYN_HL
6366 "syntax",
6367#endif
6368#if defined(USE_SYSTEM) || !defined(UNIX)
6369 "system",
6370#endif
6371#ifdef FEAT_TAG_BINS
6372 "tag_binary",
6373#endif
6374#ifdef FEAT_TAG_OLDSTATIC
6375 "tag_old_static",
6376#endif
6377#ifdef FEAT_TAG_ANYWHITE
6378 "tag_any_white",
6379#endif
6380#ifdef FEAT_TCL
6381# ifndef DYNAMIC_TCL
6382 "tcl",
6383# endif
6384#endif
6385#ifdef FEAT_TERMGUICOLORS
6386 "termguicolors",
6387#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006388#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006389 "terminal",
6390#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006391#ifdef TERMINFO
6392 "terminfo",
6393#endif
6394#ifdef FEAT_TERMRESPONSE
6395 "termresponse",
6396#endif
6397#ifdef FEAT_TEXTOBJ
6398 "textobjects",
6399#endif
6400#ifdef HAVE_TGETENT
6401 "tgetent",
6402#endif
6403#ifdef FEAT_TIMERS
6404 "timers",
6405#endif
6406#ifdef FEAT_TITLE
6407 "title",
6408#endif
6409#ifdef FEAT_TOOLBAR
6410 "toolbar",
6411#endif
6412#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6413 "unnamedplus",
6414#endif
6415#ifdef FEAT_USR_CMDS
6416 "user-commands", /* was accidentally included in 5.4 */
6417 "user_commands",
6418#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006419#ifdef FEAT_VARTABS
6420 "vartabs",
6421#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422#ifdef FEAT_VIMINFO
6423 "viminfo",
6424#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006426#ifdef FEAT_VIRTUALEDIT
6427 "virtualedit",
6428#endif
6429 "visual",
6430#ifdef FEAT_VISUALEXTRA
6431 "visualextra",
6432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006434#ifdef FEAT_VTP
6435 "vtp",
6436#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437#ifdef FEAT_WILDIGN
6438 "wildignore",
6439#endif
6440#ifdef FEAT_WILDMENU
6441 "wildmenu",
6442#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444#ifdef FEAT_WAK
6445 "winaltkeys",
6446#endif
6447#ifdef FEAT_WRITEBACKUP
6448 "writebackup",
6449#endif
6450#ifdef FEAT_XIM
6451 "xim",
6452#endif
6453#ifdef FEAT_XFONTSET
6454 "xfontset",
6455#endif
6456#ifdef FEAT_XPM_W32
6457 "xpm",
6458 "xpm_w32", /* for backward compatibility */
6459#else
6460# if defined(HAVE_XPM)
6461 "xpm",
6462# endif
6463#endif
6464#ifdef USE_XSMP
6465 "xsmp",
6466#endif
6467#ifdef USE_XSMP_INTERACT
6468 "xsmp_interact",
6469#endif
6470#ifdef FEAT_XCLIPBOARD
6471 "xterm_clipboard",
6472#endif
6473#ifdef FEAT_XTERM_SAVE
6474 "xterm_save",
6475#endif
6476#if defined(UNIX) && defined(FEAT_X11)
6477 "X11",
6478#endif
6479 NULL
6480 };
6481
6482 name = get_tv_string(&argvars[0]);
6483 for (i = 0; has_list[i] != NULL; ++i)
6484 if (STRICMP(name, has_list[i]) == 0)
6485 {
6486 n = TRUE;
6487 break;
6488 }
6489
6490 if (n == FALSE)
6491 {
6492 if (STRNICMP(name, "patch", 5) == 0)
6493 {
6494 if (name[5] == '-'
6495 && STRLEN(name) >= 11
6496 && vim_isdigit(name[6])
6497 && vim_isdigit(name[8])
6498 && vim_isdigit(name[10]))
6499 {
6500 int major = atoi((char *)name + 6);
6501 int minor = atoi((char *)name + 8);
6502
6503 /* Expect "patch-9.9.01234". */
6504 n = (major < VIM_VERSION_MAJOR
6505 || (major == VIM_VERSION_MAJOR
6506 && (minor < VIM_VERSION_MINOR
6507 || (minor == VIM_VERSION_MINOR
6508 && has_patch(atoi((char *)name + 10))))));
6509 }
6510 else
6511 n = has_patch(atoi((char *)name + 5));
6512 }
6513 else if (STRICMP(name, "vim_starting") == 0)
6514 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006515 else if (STRICMP(name, "ttyin") == 0)
6516 n = mch_input_isatty();
6517 else if (STRICMP(name, "ttyout") == 0)
6518 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519#ifdef FEAT_MBYTE
6520 else if (STRICMP(name, "multi_byte_encoding") == 0)
6521 n = has_mbyte;
6522#endif
6523#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6524 else if (STRICMP(name, "balloon_multiline") == 0)
6525 n = multiline_balloon_available();
6526#endif
6527#ifdef DYNAMIC_TCL
6528 else if (STRICMP(name, "tcl") == 0)
6529 n = tcl_enabled(FALSE);
6530#endif
6531#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6532 else if (STRICMP(name, "iconv") == 0)
6533 n = iconv_enabled(FALSE);
6534#endif
6535#ifdef DYNAMIC_LUA
6536 else if (STRICMP(name, "lua") == 0)
6537 n = lua_enabled(FALSE);
6538#endif
6539#ifdef DYNAMIC_MZSCHEME
6540 else if (STRICMP(name, "mzscheme") == 0)
6541 n = mzscheme_enabled(FALSE);
6542#endif
6543#ifdef DYNAMIC_RUBY
6544 else if (STRICMP(name, "ruby") == 0)
6545 n = ruby_enabled(FALSE);
6546#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006547#ifdef DYNAMIC_PYTHON
6548 else if (STRICMP(name, "python") == 0)
6549 n = python_enabled(FALSE);
6550#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551#ifdef DYNAMIC_PYTHON3
6552 else if (STRICMP(name, "python3") == 0)
6553 n = python3_enabled(FALSE);
6554#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006555#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6556 else if (STRICMP(name, "pythonx") == 0)
6557 {
6558# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6559 if (p_pyx == 0)
6560 n = python3_enabled(FALSE) || python_enabled(FALSE);
6561 else if (p_pyx == 3)
6562 n = python3_enabled(FALSE);
6563 else if (p_pyx == 2)
6564 n = python_enabled(FALSE);
6565# elif defined(DYNAMIC_PYTHON)
6566 n = python_enabled(FALSE);
6567# elif defined(DYNAMIC_PYTHON3)
6568 n = python3_enabled(FALSE);
6569# endif
6570 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571#endif
6572#ifdef DYNAMIC_PERL
6573 else if (STRICMP(name, "perl") == 0)
6574 n = perl_enabled(FALSE);
6575#endif
6576#ifdef FEAT_GUI
6577 else if (STRICMP(name, "gui_running") == 0)
6578 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006579# ifdef FEAT_BROWSE
6580 else if (STRICMP(name, "browse") == 0)
6581 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6582# endif
6583#endif
6584#ifdef FEAT_SYN_HL
6585 else if (STRICMP(name, "syntax_items") == 0)
6586 n = syntax_present(curwin);
6587#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006588#ifdef FEAT_VTP
6589 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006590 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591#endif
6592#ifdef FEAT_NETBEANS_INTG
6593 else if (STRICMP(name, "netbeans_enabled") == 0)
6594 n = netbeans_active();
6595#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006596#if defined(FEAT_TERMINAL) && defined(WIN3264)
6597 else if (STRICMP(name, "terminal") == 0)
6598 n = terminal_enabled();
6599#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006600 }
6601
6602 rettv->vval.v_number = n;
6603}
6604
6605/*
6606 * "has_key()" function
6607 */
6608 static void
6609f_has_key(typval_T *argvars, typval_T *rettv)
6610{
6611 if (argvars[0].v_type != VAR_DICT)
6612 {
6613 EMSG(_(e_dictreq));
6614 return;
6615 }
6616 if (argvars[0].vval.v_dict == NULL)
6617 return;
6618
6619 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6620 get_tv_string(&argvars[1]), -1) != NULL;
6621}
6622
6623/*
6624 * "haslocaldir()" function
6625 */
6626 static void
6627f_haslocaldir(typval_T *argvars, typval_T *rettv)
6628{
6629 win_T *wp = NULL;
6630
6631 wp = find_tabwin(&argvars[0], &argvars[1]);
6632 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6633}
6634
6635/*
6636 * "hasmapto()" function
6637 */
6638 static void
6639f_hasmapto(typval_T *argvars, typval_T *rettv)
6640{
6641 char_u *name;
6642 char_u *mode;
6643 char_u buf[NUMBUFLEN];
6644 int abbr = FALSE;
6645
6646 name = get_tv_string(&argvars[0]);
6647 if (argvars[1].v_type == VAR_UNKNOWN)
6648 mode = (char_u *)"nvo";
6649 else
6650 {
6651 mode = get_tv_string_buf(&argvars[1], buf);
6652 if (argvars[2].v_type != VAR_UNKNOWN)
6653 abbr = (int)get_tv_number(&argvars[2]);
6654 }
6655
6656 if (map_to_exists(name, mode, abbr))
6657 rettv->vval.v_number = TRUE;
6658 else
6659 rettv->vval.v_number = FALSE;
6660}
6661
6662/*
6663 * "histadd()" function
6664 */
6665 static void
6666f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6667{
6668#ifdef FEAT_CMDHIST
6669 int histype;
6670 char_u *str;
6671 char_u buf[NUMBUFLEN];
6672#endif
6673
6674 rettv->vval.v_number = FALSE;
6675 if (check_restricted() || check_secure())
6676 return;
6677#ifdef FEAT_CMDHIST
6678 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6679 histype = str != NULL ? get_histtype(str) : -1;
6680 if (histype >= 0)
6681 {
6682 str = get_tv_string_buf(&argvars[1], buf);
6683 if (*str != NUL)
6684 {
6685 init_history();
6686 add_to_history(histype, str, FALSE, NUL);
6687 rettv->vval.v_number = TRUE;
6688 return;
6689 }
6690 }
6691#endif
6692}
6693
6694/*
6695 * "histdel()" function
6696 */
6697 static void
6698f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6699{
6700#ifdef FEAT_CMDHIST
6701 int n;
6702 char_u buf[NUMBUFLEN];
6703 char_u *str;
6704
6705 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6706 if (str == NULL)
6707 n = 0;
6708 else if (argvars[1].v_type == VAR_UNKNOWN)
6709 /* only one argument: clear entire history */
6710 n = clr_history(get_histtype(str));
6711 else if (argvars[1].v_type == VAR_NUMBER)
6712 /* index given: remove that entry */
6713 n = del_history_idx(get_histtype(str),
6714 (int)get_tv_number(&argvars[1]));
6715 else
6716 /* string given: remove all matching entries */
6717 n = del_history_entry(get_histtype(str),
6718 get_tv_string_buf(&argvars[1], buf));
6719 rettv->vval.v_number = n;
6720#endif
6721}
6722
6723/*
6724 * "histget()" function
6725 */
6726 static void
6727f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6728{
6729#ifdef FEAT_CMDHIST
6730 int type;
6731 int idx;
6732 char_u *str;
6733
6734 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6735 if (str == NULL)
6736 rettv->vval.v_string = NULL;
6737 else
6738 {
6739 type = get_histtype(str);
6740 if (argvars[1].v_type == VAR_UNKNOWN)
6741 idx = get_history_idx(type);
6742 else
6743 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6744 /* -1 on type error */
6745 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6746 }
6747#else
6748 rettv->vval.v_string = NULL;
6749#endif
6750 rettv->v_type = VAR_STRING;
6751}
6752
6753/*
6754 * "histnr()" function
6755 */
6756 static void
6757f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6758{
6759 int i;
6760
6761#ifdef FEAT_CMDHIST
6762 char_u *history = get_tv_string_chk(&argvars[0]);
6763
6764 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6765 if (i >= HIST_CMD && i < HIST_COUNT)
6766 i = get_history_idx(i);
6767 else
6768#endif
6769 i = -1;
6770 rettv->vval.v_number = i;
6771}
6772
6773/*
6774 * "highlightID(name)" function
6775 */
6776 static void
6777f_hlID(typval_T *argvars, typval_T *rettv)
6778{
6779 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6780}
6781
6782/*
6783 * "highlight_exists()" function
6784 */
6785 static void
6786f_hlexists(typval_T *argvars, typval_T *rettv)
6787{
6788 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6789}
6790
6791/*
6792 * "hostname()" function
6793 */
6794 static void
6795f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6796{
6797 char_u hostname[256];
6798
6799 mch_get_host_name(hostname, 256);
6800 rettv->v_type = VAR_STRING;
6801 rettv->vval.v_string = vim_strsave(hostname);
6802}
6803
6804/*
6805 * iconv() function
6806 */
6807 static void
6808f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6809{
6810#ifdef FEAT_MBYTE
6811 char_u buf1[NUMBUFLEN];
6812 char_u buf2[NUMBUFLEN];
6813 char_u *from, *to, *str;
6814 vimconv_T vimconv;
6815#endif
6816
6817 rettv->v_type = VAR_STRING;
6818 rettv->vval.v_string = NULL;
6819
6820#ifdef FEAT_MBYTE
6821 str = get_tv_string(&argvars[0]);
6822 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6823 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6824 vimconv.vc_type = CONV_NONE;
6825 convert_setup(&vimconv, from, to);
6826
6827 /* If the encodings are equal, no conversion needed. */
6828 if (vimconv.vc_type == CONV_NONE)
6829 rettv->vval.v_string = vim_strsave(str);
6830 else
6831 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6832
6833 convert_setup(&vimconv, NULL, NULL);
6834 vim_free(from);
6835 vim_free(to);
6836#endif
6837}
6838
6839/*
6840 * "indent()" function
6841 */
6842 static void
6843f_indent(typval_T *argvars, typval_T *rettv)
6844{
6845 linenr_T lnum;
6846
6847 lnum = get_tv_lnum(argvars);
6848 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6849 rettv->vval.v_number = get_indent_lnum(lnum);
6850 else
6851 rettv->vval.v_number = -1;
6852}
6853
6854/*
6855 * "index()" function
6856 */
6857 static void
6858f_index(typval_T *argvars, typval_T *rettv)
6859{
6860 list_T *l;
6861 listitem_T *item;
6862 long idx = 0;
6863 int ic = FALSE;
6864
6865 rettv->vval.v_number = -1;
6866 if (argvars[0].v_type != VAR_LIST)
6867 {
6868 EMSG(_(e_listreq));
6869 return;
6870 }
6871 l = argvars[0].vval.v_list;
6872 if (l != NULL)
6873 {
6874 item = l->lv_first;
6875 if (argvars[2].v_type != VAR_UNKNOWN)
6876 {
6877 int error = FALSE;
6878
6879 /* Start at specified item. Use the cached index that list_find()
6880 * sets, so that a negative number also works. */
6881 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6882 idx = l->lv_idx;
6883 if (argvars[3].v_type != VAR_UNKNOWN)
6884 ic = (int)get_tv_number_chk(&argvars[3], &error);
6885 if (error)
6886 item = NULL;
6887 }
6888
6889 for ( ; item != NULL; item = item->li_next, ++idx)
6890 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6891 {
6892 rettv->vval.v_number = idx;
6893 break;
6894 }
6895 }
6896}
6897
6898static int inputsecret_flag = 0;
6899
6900/*
6901 * "input()" function
6902 * Also handles inputsecret() when inputsecret is set.
6903 */
6904 static void
6905f_input(typval_T *argvars, typval_T *rettv)
6906{
6907 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6908}
6909
6910/*
6911 * "inputdialog()" function
6912 */
6913 static void
6914f_inputdialog(typval_T *argvars, typval_T *rettv)
6915{
6916#if defined(FEAT_GUI_TEXTDIALOG)
6917 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6918 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6919 {
6920 char_u *message;
6921 char_u buf[NUMBUFLEN];
6922 char_u *defstr = (char_u *)"";
6923
6924 message = get_tv_string_chk(&argvars[0]);
6925 if (argvars[1].v_type != VAR_UNKNOWN
6926 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6927 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6928 else
6929 IObuff[0] = NUL;
6930 if (message != NULL && defstr != NULL
6931 && do_dialog(VIM_QUESTION, NULL, message,
6932 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6933 rettv->vval.v_string = vim_strsave(IObuff);
6934 else
6935 {
6936 if (message != NULL && defstr != NULL
6937 && argvars[1].v_type != VAR_UNKNOWN
6938 && argvars[2].v_type != VAR_UNKNOWN)
6939 rettv->vval.v_string = vim_strsave(
6940 get_tv_string_buf(&argvars[2], buf));
6941 else
6942 rettv->vval.v_string = NULL;
6943 }
6944 rettv->v_type = VAR_STRING;
6945 }
6946 else
6947#endif
6948 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6949}
6950
6951/*
6952 * "inputlist()" function
6953 */
6954 static void
6955f_inputlist(typval_T *argvars, typval_T *rettv)
6956{
6957 listitem_T *li;
6958 int selected;
6959 int mouse_used;
6960
6961#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006962 /* While starting up, there is no place to enter text. When running tests
6963 * with --not-a-term we assume feedkeys() will be used. */
6964 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965 return;
6966#endif
6967 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6968 {
6969 EMSG2(_(e_listarg), "inputlist()");
6970 return;
6971 }
6972
6973 msg_start();
6974 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6975 lines_left = Rows; /* avoid more prompt */
6976 msg_scroll = TRUE;
6977 msg_clr_eos();
6978
6979 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6980 {
6981 msg_puts(get_tv_string(&li->li_tv));
6982 msg_putchar('\n');
6983 }
6984
6985 /* Ask for choice. */
6986 selected = prompt_for_number(&mouse_used);
6987 if (mouse_used)
6988 selected -= lines_left;
6989
6990 rettv->vval.v_number = selected;
6991}
6992
6993
6994static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6995
6996/*
6997 * "inputrestore()" function
6998 */
6999 static void
7000f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7001{
7002 if (ga_userinput.ga_len > 0)
7003 {
7004 --ga_userinput.ga_len;
7005 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7006 + ga_userinput.ga_len);
7007 /* default return is zero == OK */
7008 }
7009 else if (p_verbose > 1)
7010 {
7011 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7012 rettv->vval.v_number = 1; /* Failed */
7013 }
7014}
7015
7016/*
7017 * "inputsave()" function
7018 */
7019 static void
7020f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7021{
7022 /* Add an entry to the stack of typeahead storage. */
7023 if (ga_grow(&ga_userinput, 1) == OK)
7024 {
7025 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7026 + ga_userinput.ga_len);
7027 ++ga_userinput.ga_len;
7028 /* default return is zero == OK */
7029 }
7030 else
7031 rettv->vval.v_number = 1; /* Failed */
7032}
7033
7034/*
7035 * "inputsecret()" function
7036 */
7037 static void
7038f_inputsecret(typval_T *argvars, typval_T *rettv)
7039{
7040 ++cmdline_star;
7041 ++inputsecret_flag;
7042 f_input(argvars, rettv);
7043 --cmdline_star;
7044 --inputsecret_flag;
7045}
7046
7047/*
7048 * "insert()" function
7049 */
7050 static void
7051f_insert(typval_T *argvars, typval_T *rettv)
7052{
7053 long before = 0;
7054 listitem_T *item;
7055 list_T *l;
7056 int error = FALSE;
7057
7058 if (argvars[0].v_type != VAR_LIST)
7059 EMSG2(_(e_listarg), "insert()");
7060 else if ((l = argvars[0].vval.v_list) != NULL
7061 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7062 {
7063 if (argvars[2].v_type != VAR_UNKNOWN)
7064 before = (long)get_tv_number_chk(&argvars[2], &error);
7065 if (error)
7066 return; /* type error; errmsg already given */
7067
7068 if (before == l->lv_len)
7069 item = NULL;
7070 else
7071 {
7072 item = list_find(l, before);
7073 if (item == NULL)
7074 {
7075 EMSGN(_(e_listidx), before);
7076 l = NULL;
7077 }
7078 }
7079 if (l != NULL)
7080 {
7081 list_insert_tv(l, &argvars[1], item);
7082 copy_tv(&argvars[0], rettv);
7083 }
7084 }
7085}
7086
7087/*
7088 * "invert(expr)" function
7089 */
7090 static void
7091f_invert(typval_T *argvars, typval_T *rettv)
7092{
7093 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7094}
7095
7096/*
7097 * "isdirectory()" function
7098 */
7099 static void
7100f_isdirectory(typval_T *argvars, typval_T *rettv)
7101{
7102 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7103}
7104
7105/*
7106 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7107 * or it refers to a List or Dictionary that is locked.
7108 */
7109 static int
7110tv_islocked(typval_T *tv)
7111{
7112 return (tv->v_lock & VAR_LOCKED)
7113 || (tv->v_type == VAR_LIST
7114 && tv->vval.v_list != NULL
7115 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7116 || (tv->v_type == VAR_DICT
7117 && tv->vval.v_dict != NULL
7118 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7119}
7120
7121/*
7122 * "islocked()" function
7123 */
7124 static void
7125f_islocked(typval_T *argvars, typval_T *rettv)
7126{
7127 lval_T lv;
7128 char_u *end;
7129 dictitem_T *di;
7130
7131 rettv->vval.v_number = -1;
7132 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007133 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134 if (end != NULL && lv.ll_name != NULL)
7135 {
7136 if (*end != NUL)
7137 EMSG(_(e_trailing));
7138 else
7139 {
7140 if (lv.ll_tv == NULL)
7141 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007142 di = find_var(lv.ll_name, NULL, TRUE);
7143 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007145 /* Consider a variable locked when:
7146 * 1. the variable itself is locked
7147 * 2. the value of the variable is locked.
7148 * 3. the List or Dict value is locked.
7149 */
7150 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7151 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007152 }
7153 }
7154 else if (lv.ll_range)
7155 EMSG(_("E786: Range not allowed"));
7156 else if (lv.ll_newkey != NULL)
7157 EMSG2(_(e_dictkey), lv.ll_newkey);
7158 else if (lv.ll_list != NULL)
7159 /* List item. */
7160 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7161 else
7162 /* Dictionary item. */
7163 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7164 }
7165 }
7166
7167 clear_lval(&lv);
7168}
7169
7170#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7171/*
7172 * "isnan()" function
7173 */
7174 static void
7175f_isnan(typval_T *argvars, typval_T *rettv)
7176{
7177 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7178 && isnan(argvars[0].vval.v_float);
7179}
7180#endif
7181
7182/*
7183 * "items(dict)" function
7184 */
7185 static void
7186f_items(typval_T *argvars, typval_T *rettv)
7187{
7188 dict_list(argvars, rettv, 2);
7189}
7190
7191#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7192/*
7193 * Get the job from the argument.
7194 * Returns NULL if the job is invalid.
7195 */
7196 static job_T *
7197get_job_arg(typval_T *tv)
7198{
7199 job_T *job;
7200
7201 if (tv->v_type != VAR_JOB)
7202 {
7203 EMSG2(_(e_invarg2), get_tv_string(tv));
7204 return NULL;
7205 }
7206 job = tv->vval.v_job;
7207
7208 if (job == NULL)
7209 EMSG(_("E916: not a valid job"));
7210 return job;
7211}
7212
7213/*
7214 * "job_getchannel()" function
7215 */
7216 static void
7217f_job_getchannel(typval_T *argvars, typval_T *rettv)
7218{
7219 job_T *job = get_job_arg(&argvars[0]);
7220
7221 if (job != NULL)
7222 {
7223 rettv->v_type = VAR_CHANNEL;
7224 rettv->vval.v_channel = job->jv_channel;
7225 if (job->jv_channel != NULL)
7226 ++job->jv_channel->ch_refcount;
7227 }
7228}
7229
7230/*
7231 * "job_info()" function
7232 */
7233 static void
7234f_job_info(typval_T *argvars, typval_T *rettv)
7235{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007236 if (argvars[0].v_type != VAR_UNKNOWN)
7237 {
7238 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007240 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7241 job_info(job, rettv->vval.v_dict);
7242 }
7243 else if (rettv_list_alloc(rettv) == OK)
7244 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245}
7246
7247/*
7248 * "job_setoptions()" function
7249 */
7250 static void
7251f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7252{
7253 job_T *job = get_job_arg(&argvars[0]);
7254 jobopt_T opt;
7255
7256 if (job == NULL)
7257 return;
7258 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007259 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260 job_set_options(job, &opt);
7261 free_job_options(&opt);
7262}
7263
7264/*
7265 * "job_start()" function
7266 */
7267 static void
7268f_job_start(typval_T *argvars, typval_T *rettv)
7269{
7270 rettv->v_type = VAR_JOB;
7271 if (check_restricted() || check_secure())
7272 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007273 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274}
7275
7276/*
7277 * "job_status()" function
7278 */
7279 static void
7280f_job_status(typval_T *argvars, typval_T *rettv)
7281{
7282 job_T *job = get_job_arg(&argvars[0]);
7283
7284 if (job != NULL)
7285 {
7286 rettv->v_type = VAR_STRING;
7287 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7288 }
7289}
7290
7291/*
7292 * "job_stop()" function
7293 */
7294 static void
7295f_job_stop(typval_T *argvars, typval_T *rettv)
7296{
7297 job_T *job = get_job_arg(&argvars[0]);
7298
7299 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007300 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007301}
7302#endif
7303
7304/*
7305 * "join()" function
7306 */
7307 static void
7308f_join(typval_T *argvars, typval_T *rettv)
7309{
7310 garray_T ga;
7311 char_u *sep;
7312
7313 if (argvars[0].v_type != VAR_LIST)
7314 {
7315 EMSG(_(e_listreq));
7316 return;
7317 }
7318 if (argvars[0].vval.v_list == NULL)
7319 return;
7320 if (argvars[1].v_type == VAR_UNKNOWN)
7321 sep = (char_u *)" ";
7322 else
7323 sep = get_tv_string_chk(&argvars[1]);
7324
7325 rettv->v_type = VAR_STRING;
7326
7327 if (sep != NULL)
7328 {
7329 ga_init2(&ga, (int)sizeof(char), 80);
7330 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7331 ga_append(&ga, NUL);
7332 rettv->vval.v_string = (char_u *)ga.ga_data;
7333 }
7334 else
7335 rettv->vval.v_string = NULL;
7336}
7337
7338/*
7339 * "js_decode()" function
7340 */
7341 static void
7342f_js_decode(typval_T *argvars, typval_T *rettv)
7343{
7344 js_read_T reader;
7345
7346 reader.js_buf = get_tv_string(&argvars[0]);
7347 reader.js_fill = NULL;
7348 reader.js_used = 0;
7349 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7350 EMSG(_(e_invarg));
7351}
7352
7353/*
7354 * "js_encode()" function
7355 */
7356 static void
7357f_js_encode(typval_T *argvars, typval_T *rettv)
7358{
7359 rettv->v_type = VAR_STRING;
7360 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7361}
7362
7363/*
7364 * "json_decode()" function
7365 */
7366 static void
7367f_json_decode(typval_T *argvars, typval_T *rettv)
7368{
7369 js_read_T reader;
7370
7371 reader.js_buf = get_tv_string(&argvars[0]);
7372 reader.js_fill = NULL;
7373 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007374 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375}
7376
7377/*
7378 * "json_encode()" function
7379 */
7380 static void
7381f_json_encode(typval_T *argvars, typval_T *rettv)
7382{
7383 rettv->v_type = VAR_STRING;
7384 rettv->vval.v_string = json_encode(&argvars[0], 0);
7385}
7386
7387/*
7388 * "keys()" function
7389 */
7390 static void
7391f_keys(typval_T *argvars, typval_T *rettv)
7392{
7393 dict_list(argvars, rettv, 0);
7394}
7395
7396/*
7397 * "last_buffer_nr()" function.
7398 */
7399 static void
7400f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7401{
7402 int n = 0;
7403 buf_T *buf;
7404
Bram Moolenaar29323592016-07-24 22:04:11 +02007405 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406 if (n < buf->b_fnum)
7407 n = buf->b_fnum;
7408
7409 rettv->vval.v_number = n;
7410}
7411
7412/*
7413 * "len()" function
7414 */
7415 static void
7416f_len(typval_T *argvars, typval_T *rettv)
7417{
7418 switch (argvars[0].v_type)
7419 {
7420 case VAR_STRING:
7421 case VAR_NUMBER:
7422 rettv->vval.v_number = (varnumber_T)STRLEN(
7423 get_tv_string(&argvars[0]));
7424 break;
7425 case VAR_LIST:
7426 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7427 break;
7428 case VAR_DICT:
7429 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7430 break;
7431 case VAR_UNKNOWN:
7432 case VAR_SPECIAL:
7433 case VAR_FLOAT:
7434 case VAR_FUNC:
7435 case VAR_PARTIAL:
7436 case VAR_JOB:
7437 case VAR_CHANNEL:
7438 EMSG(_("E701: Invalid type for len()"));
7439 break;
7440 }
7441}
7442
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007444libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445{
7446#ifdef FEAT_LIBCALL
7447 char_u *string_in;
7448 char_u **string_result;
7449 int nr_result;
7450#endif
7451
7452 rettv->v_type = type;
7453 if (type != VAR_NUMBER)
7454 rettv->vval.v_string = NULL;
7455
7456 if (check_restricted() || check_secure())
7457 return;
7458
7459#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007460 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007461 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7462 {
7463 string_in = NULL;
7464 if (argvars[2].v_type == VAR_STRING)
7465 string_in = argvars[2].vval.v_string;
7466 if (type == VAR_NUMBER)
7467 string_result = NULL;
7468 else
7469 string_result = &rettv->vval.v_string;
7470 if (mch_libcall(argvars[0].vval.v_string,
7471 argvars[1].vval.v_string,
7472 string_in,
7473 argvars[2].vval.v_number,
7474 string_result,
7475 &nr_result) == OK
7476 && type == VAR_NUMBER)
7477 rettv->vval.v_number = nr_result;
7478 }
7479#endif
7480}
7481
7482/*
7483 * "libcall()" function
7484 */
7485 static void
7486f_libcall(typval_T *argvars, typval_T *rettv)
7487{
7488 libcall_common(argvars, rettv, VAR_STRING);
7489}
7490
7491/*
7492 * "libcallnr()" function
7493 */
7494 static void
7495f_libcallnr(typval_T *argvars, typval_T *rettv)
7496{
7497 libcall_common(argvars, rettv, VAR_NUMBER);
7498}
7499
7500/*
7501 * "line(string)" function
7502 */
7503 static void
7504f_line(typval_T *argvars, typval_T *rettv)
7505{
7506 linenr_T lnum = 0;
7507 pos_T *fp;
7508 int fnum;
7509
7510 fp = var2fpos(&argvars[0], TRUE, &fnum);
7511 if (fp != NULL)
7512 lnum = fp->lnum;
7513 rettv->vval.v_number = lnum;
7514}
7515
7516/*
7517 * "line2byte(lnum)" function
7518 */
7519 static void
7520f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7521{
7522#ifndef FEAT_BYTEOFF
7523 rettv->vval.v_number = -1;
7524#else
7525 linenr_T lnum;
7526
7527 lnum = get_tv_lnum(argvars);
7528 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7529 rettv->vval.v_number = -1;
7530 else
7531 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7532 if (rettv->vval.v_number >= 0)
7533 ++rettv->vval.v_number;
7534#endif
7535}
7536
7537/*
7538 * "lispindent(lnum)" function
7539 */
7540 static void
7541f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7542{
7543#ifdef FEAT_LISP
7544 pos_T pos;
7545 linenr_T lnum;
7546
7547 pos = curwin->w_cursor;
7548 lnum = get_tv_lnum(argvars);
7549 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7550 {
7551 curwin->w_cursor.lnum = lnum;
7552 rettv->vval.v_number = get_lisp_indent();
7553 curwin->w_cursor = pos;
7554 }
7555 else
7556#endif
7557 rettv->vval.v_number = -1;
7558}
7559
7560/*
7561 * "localtime()" function
7562 */
7563 static void
7564f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7565{
7566 rettv->vval.v_number = (varnumber_T)time(NULL);
7567}
7568
7569static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
7570
7571 static void
7572get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7573{
7574 char_u *keys;
7575 char_u *which;
7576 char_u buf[NUMBUFLEN];
7577 char_u *keys_buf = NULL;
7578 char_u *rhs;
7579 int mode;
7580 int abbr = FALSE;
7581 int get_dict = FALSE;
7582 mapblock_T *mp;
7583 int buffer_local;
7584
7585 /* return empty string for failure */
7586 rettv->v_type = VAR_STRING;
7587 rettv->vval.v_string = NULL;
7588
7589 keys = get_tv_string(&argvars[0]);
7590 if (*keys == NUL)
7591 return;
7592
7593 if (argvars[1].v_type != VAR_UNKNOWN)
7594 {
7595 which = get_tv_string_buf_chk(&argvars[1], buf);
7596 if (argvars[2].v_type != VAR_UNKNOWN)
7597 {
7598 abbr = (int)get_tv_number(&argvars[2]);
7599 if (argvars[3].v_type != VAR_UNKNOWN)
7600 get_dict = (int)get_tv_number(&argvars[3]);
7601 }
7602 }
7603 else
7604 which = (char_u *)"";
7605 if (which == NULL)
7606 return;
7607
7608 mode = get_map_mode(&which, 0);
7609
7610 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7611 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7612 vim_free(keys_buf);
7613
7614 if (!get_dict)
7615 {
7616 /* Return a string. */
7617 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007618 {
7619 if (*rhs == NUL)
7620 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7621 else
7622 rettv->vval.v_string = str2special_save(rhs, FALSE);
7623 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624
7625 }
7626 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7627 {
7628 /* Return a dictionary. */
7629 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7630 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7631 dict_T *dict = rettv->vval.v_dict;
7632
Bram Moolenaare0be1672018-07-08 16:50:37 +02007633 dict_add_string(dict, "lhs", lhs);
7634 dict_add_string(dict, "rhs", mp->m_orig_str);
7635 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7636 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7637 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007638 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7639 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007640 dict_add_number(dict, "buffer", (long)buffer_local);
7641 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7642 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643
7644 vim_free(lhs);
7645 vim_free(mapmode);
7646 }
7647}
7648
7649#ifdef FEAT_FLOAT
7650/*
7651 * "log()" function
7652 */
7653 static void
7654f_log(typval_T *argvars, typval_T *rettv)
7655{
7656 float_T f = 0.0;
7657
7658 rettv->v_type = VAR_FLOAT;
7659 if (get_float_arg(argvars, &f) == OK)
7660 rettv->vval.v_float = log(f);
7661 else
7662 rettv->vval.v_float = 0.0;
7663}
7664
7665/*
7666 * "log10()" function
7667 */
7668 static void
7669f_log10(typval_T *argvars, typval_T *rettv)
7670{
7671 float_T f = 0.0;
7672
7673 rettv->v_type = VAR_FLOAT;
7674 if (get_float_arg(argvars, &f) == OK)
7675 rettv->vval.v_float = log10(f);
7676 else
7677 rettv->vval.v_float = 0.0;
7678}
7679#endif
7680
7681#ifdef FEAT_LUA
7682/*
7683 * "luaeval()" function
7684 */
7685 static void
7686f_luaeval(typval_T *argvars, typval_T *rettv)
7687{
7688 char_u *str;
7689 char_u buf[NUMBUFLEN];
7690
7691 str = get_tv_string_buf(&argvars[0], buf);
7692 do_luaeval(str, argvars + 1, rettv);
7693}
7694#endif
7695
7696/*
7697 * "map()" function
7698 */
7699 static void
7700f_map(typval_T *argvars, typval_T *rettv)
7701{
7702 filter_map(argvars, rettv, TRUE);
7703}
7704
7705/*
7706 * "maparg()" function
7707 */
7708 static void
7709f_maparg(typval_T *argvars, typval_T *rettv)
7710{
7711 get_maparg(argvars, rettv, TRUE);
7712}
7713
7714/*
7715 * "mapcheck()" function
7716 */
7717 static void
7718f_mapcheck(typval_T *argvars, typval_T *rettv)
7719{
7720 get_maparg(argvars, rettv, FALSE);
7721}
7722
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007723typedef enum
7724{
7725 MATCH_END, /* matchend() */
7726 MATCH_MATCH, /* match() */
7727 MATCH_STR, /* matchstr() */
7728 MATCH_LIST, /* matchlist() */
7729 MATCH_POS /* matchstrpos() */
7730} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731
7732 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007733find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007734{
7735 char_u *str = NULL;
7736 long len = 0;
7737 char_u *expr = NULL;
7738 char_u *pat;
7739 regmatch_T regmatch;
7740 char_u patbuf[NUMBUFLEN];
7741 char_u strbuf[NUMBUFLEN];
7742 char_u *save_cpo;
7743 long start = 0;
7744 long nth = 1;
7745 colnr_T startcol = 0;
7746 int match = 0;
7747 list_T *l = NULL;
7748 listitem_T *li = NULL;
7749 long idx = 0;
7750 char_u *tofree = NULL;
7751
7752 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7753 save_cpo = p_cpo;
7754 p_cpo = (char_u *)"";
7755
7756 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007757 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007759 /* type MATCH_LIST: return empty list when there are no matches.
7760 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 if (rettv_list_alloc(rettv) == FAIL)
7762 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007763 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 && (list_append_string(rettv->vval.v_list,
7765 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
7771 (varnumber_T)-1) == FAIL))
7772 {
7773 list_free(rettv->vval.v_list);
7774 rettv->vval.v_list = NULL;
7775 goto theend;
7776 }
7777 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007778 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 {
7780 rettv->v_type = VAR_STRING;
7781 rettv->vval.v_string = NULL;
7782 }
7783
7784 if (argvars[0].v_type == VAR_LIST)
7785 {
7786 if ((l = argvars[0].vval.v_list) == NULL)
7787 goto theend;
7788 li = l->lv_first;
7789 }
7790 else
7791 {
7792 expr = str = get_tv_string(&argvars[0]);
7793 len = (long)STRLEN(str);
7794 }
7795
7796 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7797 if (pat == NULL)
7798 goto theend;
7799
7800 if (argvars[2].v_type != VAR_UNKNOWN)
7801 {
7802 int error = FALSE;
7803
7804 start = (long)get_tv_number_chk(&argvars[2], &error);
7805 if (error)
7806 goto theend;
7807 if (l != NULL)
7808 {
7809 li = list_find(l, start);
7810 if (li == NULL)
7811 goto theend;
7812 idx = l->lv_idx; /* use the cached index */
7813 }
7814 else
7815 {
7816 if (start < 0)
7817 start = 0;
7818 if (start > len)
7819 goto theend;
7820 /* When "count" argument is there ignore matches before "start",
7821 * otherwise skip part of the string. Differs when pattern is "^"
7822 * or "\<". */
7823 if (argvars[3].v_type != VAR_UNKNOWN)
7824 startcol = start;
7825 else
7826 {
7827 str += start;
7828 len -= start;
7829 }
7830 }
7831
7832 if (argvars[3].v_type != VAR_UNKNOWN)
7833 nth = (long)get_tv_number_chk(&argvars[3], &error);
7834 if (error)
7835 goto theend;
7836 }
7837
7838 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7839 if (regmatch.regprog != NULL)
7840 {
7841 regmatch.rm_ic = p_ic;
7842
7843 for (;;)
7844 {
7845 if (l != NULL)
7846 {
7847 if (li == NULL)
7848 {
7849 match = FALSE;
7850 break;
7851 }
7852 vim_free(tofree);
7853 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7854 if (str == NULL)
7855 break;
7856 }
7857
7858 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7859
7860 if (match && --nth <= 0)
7861 break;
7862 if (l == NULL && !match)
7863 break;
7864
7865 /* Advance to just after the match. */
7866 if (l != NULL)
7867 {
7868 li = li->li_next;
7869 ++idx;
7870 }
7871 else
7872 {
7873#ifdef FEAT_MBYTE
7874 startcol = (colnr_T)(regmatch.startp[0]
7875 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7876#else
7877 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7878#endif
7879 if (startcol > (colnr_T)len
7880 || str + startcol <= regmatch.startp[0])
7881 {
7882 match = FALSE;
7883 break;
7884 }
7885 }
7886 }
7887
7888 if (match)
7889 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007890 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 {
7892 listitem_T *li1 = rettv->vval.v_list->lv_first;
7893 listitem_T *li2 = li1->li_next;
7894 listitem_T *li3 = li2->li_next;
7895 listitem_T *li4 = li3->li_next;
7896
7897 vim_free(li1->li_tv.vval.v_string);
7898 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7899 (int)(regmatch.endp[0] - regmatch.startp[0]));
7900 li3->li_tv.vval.v_number =
7901 (varnumber_T)(regmatch.startp[0] - expr);
7902 li4->li_tv.vval.v_number =
7903 (varnumber_T)(regmatch.endp[0] - expr);
7904 if (l != NULL)
7905 li2->li_tv.vval.v_number = (varnumber_T)idx;
7906 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007907 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908 {
7909 int i;
7910
7911 /* return list with matched string and submatches */
7912 for (i = 0; i < NSUBEXP; ++i)
7913 {
7914 if (regmatch.endp[i] == NULL)
7915 {
7916 if (list_append_string(rettv->vval.v_list,
7917 (char_u *)"", 0) == FAIL)
7918 break;
7919 }
7920 else if (list_append_string(rettv->vval.v_list,
7921 regmatch.startp[i],
7922 (int)(regmatch.endp[i] - regmatch.startp[i]))
7923 == FAIL)
7924 break;
7925 }
7926 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007927 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928 {
7929 /* return matched string */
7930 if (l != NULL)
7931 copy_tv(&li->li_tv, rettv);
7932 else
7933 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7934 (int)(regmatch.endp[0] - regmatch.startp[0]));
7935 }
7936 else if (l != NULL)
7937 rettv->vval.v_number = idx;
7938 else
7939 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007940 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007941 rettv->vval.v_number =
7942 (varnumber_T)(regmatch.startp[0] - str);
7943 else
7944 rettv->vval.v_number =
7945 (varnumber_T)(regmatch.endp[0] - str);
7946 rettv->vval.v_number += (varnumber_T)(str - expr);
7947 }
7948 }
7949 vim_regfree(regmatch.regprog);
7950 }
7951
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007952theend:
7953 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954 /* matchstrpos() without a list: drop the second item. */
7955 listitem_remove(rettv->vval.v_list,
7956 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007957 vim_free(tofree);
7958 p_cpo = save_cpo;
7959}
7960
7961/*
7962 * "match()" function
7963 */
7964 static void
7965f_match(typval_T *argvars, typval_T *rettv)
7966{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007967 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007968}
7969
Bram Moolenaar95e51472018-07-28 16:55:56 +02007970#ifdef FEAT_SEARCH_EXTRA
7971 static int
7972matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7973{
7974 dictitem_T *di;
7975
7976 if (tv->v_type != VAR_DICT)
7977 {
7978 EMSG(_(e_dictreq));
7979 return FAIL;
7980 }
7981
7982 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
7983 *conceal_char = get_dict_string(tv->vval.v_dict,
7984 (char_u *)"conceal", FALSE);
7985
7986 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
7987 {
7988 *win = find_win_by_nr(&di->di_tv, NULL);
7989 if (*win == NULL)
7990 {
7991 EMSG(_("E957: Invalid window number"));
7992 return FAIL;
7993 }
7994 }
7995
7996 return OK;
7997}
7998#endif
7999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000/*
8001 * "matchadd()" function
8002 */
8003 static void
8004f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8005{
8006#ifdef FEAT_SEARCH_EXTRA
8007 char_u buf[NUMBUFLEN];
8008 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
8009 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8010 int prio = 10; /* default priority */
8011 int id = -1;
8012 int error = FALSE;
8013 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008014 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015
8016 rettv->vval.v_number = -1;
8017
8018 if (grp == NULL || pat == NULL)
8019 return;
8020 if (argvars[2].v_type != VAR_UNKNOWN)
8021 {
8022 prio = (int)get_tv_number_chk(&argvars[2], &error);
8023 if (argvars[3].v_type != VAR_UNKNOWN)
8024 {
8025 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008026 if (argvars[4].v_type != VAR_UNKNOWN
8027 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8028 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008029 }
8030 }
8031 if (error == TRUE)
8032 return;
8033 if (id >= 1 && id <= 3)
8034 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008035 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 return;
8037 }
8038
Bram Moolenaar95e51472018-07-28 16:55:56 +02008039 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008040 conceal_char);
8041#endif
8042}
8043
8044/*
8045 * "matchaddpos()" function
8046 */
8047 static void
8048f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8049{
8050#ifdef FEAT_SEARCH_EXTRA
8051 char_u buf[NUMBUFLEN];
8052 char_u *group;
8053 int prio = 10;
8054 int id = -1;
8055 int error = FALSE;
8056 list_T *l;
8057 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008058 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008059
8060 rettv->vval.v_number = -1;
8061
8062 group = get_tv_string_buf_chk(&argvars[0], buf);
8063 if (group == NULL)
8064 return;
8065
8066 if (argvars[1].v_type != VAR_LIST)
8067 {
8068 EMSG2(_(e_listarg), "matchaddpos()");
8069 return;
8070 }
8071 l = argvars[1].vval.v_list;
8072 if (l == NULL)
8073 return;
8074
8075 if (argvars[2].v_type != VAR_UNKNOWN)
8076 {
8077 prio = (int)get_tv_number_chk(&argvars[2], &error);
8078 if (argvars[3].v_type != VAR_UNKNOWN)
8079 {
8080 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008081
8082 if (argvars[4].v_type != VAR_UNKNOWN
8083 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8084 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 }
8086 }
8087 if (error == TRUE)
8088 return;
8089
8090 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8091 if (id == 1 || id == 2)
8092 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008093 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008094 return;
8095 }
8096
Bram Moolenaar95e51472018-07-28 16:55:56 +02008097 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 conceal_char);
8099#endif
8100}
8101
8102/*
8103 * "matcharg()" function
8104 */
8105 static void
8106f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8107{
8108 if (rettv_list_alloc(rettv) == OK)
8109 {
8110#ifdef FEAT_SEARCH_EXTRA
8111 int id = (int)get_tv_number(&argvars[0]);
8112 matchitem_T *m;
8113
8114 if (id >= 1 && id <= 3)
8115 {
8116 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8117 {
8118 list_append_string(rettv->vval.v_list,
8119 syn_id2name(m->hlg_id), -1);
8120 list_append_string(rettv->vval.v_list, m->pattern, -1);
8121 }
8122 else
8123 {
8124 list_append_string(rettv->vval.v_list, NULL, -1);
8125 list_append_string(rettv->vval.v_list, NULL, -1);
8126 }
8127 }
8128#endif
8129 }
8130}
8131
8132/*
8133 * "matchdelete()" function
8134 */
8135 static void
8136f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8137{
8138#ifdef FEAT_SEARCH_EXTRA
8139 rettv->vval.v_number = match_delete(curwin,
8140 (int)get_tv_number(&argvars[0]), TRUE);
8141#endif
8142}
8143
8144/*
8145 * "matchend()" function
8146 */
8147 static void
8148f_matchend(typval_T *argvars, typval_T *rettv)
8149{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008150 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151}
8152
8153/*
8154 * "matchlist()" function
8155 */
8156 static void
8157f_matchlist(typval_T *argvars, typval_T *rettv)
8158{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008159 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160}
8161
8162/*
8163 * "matchstr()" function
8164 */
8165 static void
8166f_matchstr(typval_T *argvars, typval_T *rettv)
8167{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008168 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169}
8170
8171/*
8172 * "matchstrpos()" function
8173 */
8174 static void
8175f_matchstrpos(typval_T *argvars, typval_T *rettv)
8176{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008177 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178}
8179
8180static void max_min(typval_T *argvars, typval_T *rettv, int domax);
8181
8182 static void
8183max_min(typval_T *argvars, typval_T *rettv, int domax)
8184{
8185 varnumber_T n = 0;
8186 varnumber_T i;
8187 int error = FALSE;
8188
8189 if (argvars[0].v_type == VAR_LIST)
8190 {
8191 list_T *l;
8192 listitem_T *li;
8193
8194 l = argvars[0].vval.v_list;
8195 if (l != NULL)
8196 {
8197 li = l->lv_first;
8198 if (li != NULL)
8199 {
8200 n = get_tv_number_chk(&li->li_tv, &error);
8201 for (;;)
8202 {
8203 li = li->li_next;
8204 if (li == NULL)
8205 break;
8206 i = get_tv_number_chk(&li->li_tv, &error);
8207 if (domax ? i > n : i < n)
8208 n = i;
8209 }
8210 }
8211 }
8212 }
8213 else if (argvars[0].v_type == VAR_DICT)
8214 {
8215 dict_T *d;
8216 int first = TRUE;
8217 hashitem_T *hi;
8218 int todo;
8219
8220 d = argvars[0].vval.v_dict;
8221 if (d != NULL)
8222 {
8223 todo = (int)d->dv_hashtab.ht_used;
8224 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8225 {
8226 if (!HASHITEM_EMPTY(hi))
8227 {
8228 --todo;
8229 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8230 if (first)
8231 {
8232 n = i;
8233 first = FALSE;
8234 }
8235 else if (domax ? i > n : i < n)
8236 n = i;
8237 }
8238 }
8239 }
8240 }
8241 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008242 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008243 rettv->vval.v_number = error ? 0 : n;
8244}
8245
8246/*
8247 * "max()" function
8248 */
8249 static void
8250f_max(typval_T *argvars, typval_T *rettv)
8251{
8252 max_min(argvars, rettv, TRUE);
8253}
8254
8255/*
8256 * "min()" function
8257 */
8258 static void
8259f_min(typval_T *argvars, typval_T *rettv)
8260{
8261 max_min(argvars, rettv, FALSE);
8262}
8263
8264static int mkdir_recurse(char_u *dir, int prot);
8265
8266/*
8267 * Create the directory in which "dir" is located, and higher levels when
8268 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008269 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008270 */
8271 static int
8272mkdir_recurse(char_u *dir, int prot)
8273{
8274 char_u *p;
8275 char_u *updir;
8276 int r = FAIL;
8277
8278 /* Get end of directory name in "dir".
8279 * We're done when it's "/" or "c:/". */
8280 p = gettail_sep(dir);
8281 if (p <= get_past_head(dir))
8282 return OK;
8283
8284 /* If the directory exists we're done. Otherwise: create it.*/
8285 updir = vim_strnsave(dir, (int)(p - dir));
8286 if (updir == NULL)
8287 return FAIL;
8288 if (mch_isdir(updir))
8289 r = OK;
8290 else if (mkdir_recurse(updir, prot) == OK)
8291 r = vim_mkdir_emsg(updir, prot);
8292 vim_free(updir);
8293 return r;
8294}
8295
8296#ifdef vim_mkdir
8297/*
8298 * "mkdir()" function
8299 */
8300 static void
8301f_mkdir(typval_T *argvars, typval_T *rettv)
8302{
8303 char_u *dir;
8304 char_u buf[NUMBUFLEN];
8305 int prot = 0755;
8306
8307 rettv->vval.v_number = FAIL;
8308 if (check_restricted() || check_secure())
8309 return;
8310
8311 dir = get_tv_string_buf(&argvars[0], buf);
8312 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008313 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008314
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008315 if (*gettail(dir) == NUL)
8316 /* remove trailing slashes */
8317 *gettail_sep(dir) = NUL;
8318
8319 if (argvars[1].v_type != VAR_UNKNOWN)
8320 {
8321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008323 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8324 if (prot == -1)
8325 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008327 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8328 {
8329 if (mch_isdir(dir))
8330 {
8331 /* With the "p" flag it's OK if the dir already exists. */
8332 rettv->vval.v_number = OK;
8333 return;
8334 }
8335 mkdir_recurse(dir, prot);
8336 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008338 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339}
8340#endif
8341
8342/*
8343 * "mode()" function
8344 */
8345 static void
8346f_mode(typval_T *argvars, typval_T *rettv)
8347{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008348 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349
Bram Moolenaar612cc382018-07-29 15:34:26 +02008350 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008351
8352 if (time_for_testing == 93784)
8353 {
8354 /* Testing the two-character code. */
8355 buf[0] = 'x';
8356 buf[1] = '!';
8357 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008358#ifdef FEAT_TERMINAL
8359 else if (term_use_loop())
8360 buf[0] = 't';
8361#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 else if (VIsual_active)
8363 {
8364 if (VIsual_select)
8365 buf[0] = VIsual_mode + 's' - 'v';
8366 else
8367 buf[0] = VIsual_mode;
8368 }
8369 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8370 || State == CONFIRM)
8371 {
8372 buf[0] = 'r';
8373 if (State == ASKMORE)
8374 buf[1] = 'm';
8375 else if (State == CONFIRM)
8376 buf[1] = '?';
8377 }
8378 else if (State == EXTERNCMD)
8379 buf[0] = '!';
8380 else if (State & INSERT)
8381 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 if (State & VREPLACE_FLAG)
8383 {
8384 buf[0] = 'R';
8385 buf[1] = 'v';
8386 }
8387 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008388 {
8389 if (State & REPLACE_FLAG)
8390 buf[0] = 'R';
8391 else
8392 buf[0] = 'i';
8393#ifdef FEAT_INS_EXPAND
8394 if (ins_compl_active())
8395 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008396 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008397 buf[1] = 'x';
8398#endif
8399 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008400 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008401 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 {
8403 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008404 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008406 else if (exmode_active == EXMODE_NORMAL)
8407 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008408 }
8409 else
8410 {
8411 buf[0] = 'n';
8412 if (finish_op)
8413 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008414 else if (restart_edit == 'I' || restart_edit == 'R'
8415 || restart_edit == 'V')
8416 {
8417 buf[1] = 'i';
8418 buf[2] = restart_edit;
8419 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008420 }
8421
8422 /* Clear out the minor mode when the argument is not a non-zero number or
8423 * non-empty string. */
8424 if (!non_zero_arg(&argvars[0]))
8425 buf[1] = NUL;
8426
8427 rettv->vval.v_string = vim_strsave(buf);
8428 rettv->v_type = VAR_STRING;
8429}
8430
8431#if defined(FEAT_MZSCHEME) || defined(PROTO)
8432/*
8433 * "mzeval()" function
8434 */
8435 static void
8436f_mzeval(typval_T *argvars, typval_T *rettv)
8437{
8438 char_u *str;
8439 char_u buf[NUMBUFLEN];
8440
8441 str = get_tv_string_buf(&argvars[0], buf);
8442 do_mzeval(str, rettv);
8443}
8444
8445 void
8446mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8447{
8448 typval_T argvars[3];
8449
8450 argvars[0].v_type = VAR_STRING;
8451 argvars[0].vval.v_string = name;
8452 copy_tv(args, &argvars[1]);
8453 argvars[2].v_type = VAR_UNKNOWN;
8454 f_call(argvars, rettv);
8455 clear_tv(&argvars[1]);
8456}
8457#endif
8458
8459/*
8460 * "nextnonblank()" function
8461 */
8462 static void
8463f_nextnonblank(typval_T *argvars, typval_T *rettv)
8464{
8465 linenr_T lnum;
8466
8467 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8468 {
8469 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8470 {
8471 lnum = 0;
8472 break;
8473 }
8474 if (*skipwhite(ml_get(lnum)) != NUL)
8475 break;
8476 }
8477 rettv->vval.v_number = lnum;
8478}
8479
8480/*
8481 * "nr2char()" function
8482 */
8483 static void
8484f_nr2char(typval_T *argvars, typval_T *rettv)
8485{
8486 char_u buf[NUMBUFLEN];
8487
8488#ifdef FEAT_MBYTE
8489 if (has_mbyte)
8490 {
8491 int utf8 = 0;
8492
8493 if (argvars[1].v_type != VAR_UNKNOWN)
8494 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8495 if (utf8)
8496 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8497 else
8498 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8499 }
8500 else
8501#endif
8502 {
8503 buf[0] = (char_u)get_tv_number(&argvars[0]);
8504 buf[1] = NUL;
8505 }
8506 rettv->v_type = VAR_STRING;
8507 rettv->vval.v_string = vim_strsave(buf);
8508}
8509
8510/*
8511 * "or(expr, expr)" function
8512 */
8513 static void
8514f_or(typval_T *argvars, typval_T *rettv)
8515{
8516 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8517 | get_tv_number_chk(&argvars[1], NULL);
8518}
8519
8520/*
8521 * "pathshorten()" function
8522 */
8523 static void
8524f_pathshorten(typval_T *argvars, typval_T *rettv)
8525{
8526 char_u *p;
8527
8528 rettv->v_type = VAR_STRING;
8529 p = get_tv_string_chk(&argvars[0]);
8530 if (p == NULL)
8531 rettv->vval.v_string = NULL;
8532 else
8533 {
8534 p = vim_strsave(p);
8535 rettv->vval.v_string = p;
8536 if (p != NULL)
8537 shorten_dir(p);
8538 }
8539}
8540
8541#ifdef FEAT_PERL
8542/*
8543 * "perleval()" function
8544 */
8545 static void
8546f_perleval(typval_T *argvars, typval_T *rettv)
8547{
8548 char_u *str;
8549 char_u buf[NUMBUFLEN];
8550
8551 str = get_tv_string_buf(&argvars[0], buf);
8552 do_perleval(str, rettv);
8553}
8554#endif
8555
8556#ifdef FEAT_FLOAT
8557/*
8558 * "pow()" function
8559 */
8560 static void
8561f_pow(typval_T *argvars, typval_T *rettv)
8562{
8563 float_T fx = 0.0, fy = 0.0;
8564
8565 rettv->v_type = VAR_FLOAT;
8566 if (get_float_arg(argvars, &fx) == OK
8567 && get_float_arg(&argvars[1], &fy) == OK)
8568 rettv->vval.v_float = pow(fx, fy);
8569 else
8570 rettv->vval.v_float = 0.0;
8571}
8572#endif
8573
8574/*
8575 * "prevnonblank()" function
8576 */
8577 static void
8578f_prevnonblank(typval_T *argvars, typval_T *rettv)
8579{
8580 linenr_T lnum;
8581
8582 lnum = get_tv_lnum(argvars);
8583 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8584 lnum = 0;
8585 else
8586 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8587 --lnum;
8588 rettv->vval.v_number = lnum;
8589}
8590
8591/* This dummy va_list is here because:
8592 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8593 * - locally in the function results in a "used before set" warning
8594 * - using va_start() to initialize it gives "function with fixed args" error */
8595static va_list ap;
8596
8597/*
8598 * "printf()" function
8599 */
8600 static void
8601f_printf(typval_T *argvars, typval_T *rettv)
8602{
8603 char_u buf[NUMBUFLEN];
8604 int len;
8605 char_u *s;
8606 int saved_did_emsg = did_emsg;
8607 char *fmt;
8608
8609 rettv->v_type = VAR_STRING;
8610 rettv->vval.v_string = NULL;
8611
8612 /* Get the required length, allocate the buffer and do it for real. */
8613 did_emsg = FALSE;
8614 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008615 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 if (!did_emsg)
8617 {
8618 s = alloc(len + 1);
8619 if (s != NULL)
8620 {
8621 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008622 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8623 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 }
8625 }
8626 did_emsg |= saved_did_emsg;
8627}
8628
Bram Moolenaarf2732452018-06-03 14:47:35 +02008629#ifdef FEAT_JOB_CHANNEL
8630/*
8631 * "prompt_setcallback({buffer}, {callback})" function
8632 */
8633 static void
8634f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8635{
8636 buf_T *buf;
8637 char_u *callback;
8638 partial_T *partial;
8639
8640 if (check_secure())
8641 return;
8642 buf = get_buf_tv(&argvars[0], FALSE);
8643 if (buf == NULL)
8644 return;
8645
8646 callback = get_callback(&argvars[1], &partial);
8647 if (callback == NULL)
8648 return;
8649
8650 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8651 if (partial == NULL)
8652 buf->b_prompt_callback = vim_strsave(callback);
8653 else
8654 /* pointer into the partial */
8655 buf->b_prompt_callback = callback;
8656 buf->b_prompt_partial = partial;
8657}
8658
8659/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008660 * "prompt_setinterrupt({buffer}, {callback})" function
8661 */
8662 static void
8663f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8664{
8665 buf_T *buf;
8666 char_u *callback;
8667 partial_T *partial;
8668
8669 if (check_secure())
8670 return;
8671 buf = get_buf_tv(&argvars[0], FALSE);
8672 if (buf == NULL)
8673 return;
8674
8675 callback = get_callback(&argvars[1], &partial);
8676 if (callback == NULL)
8677 return;
8678
8679 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8680 if (partial == NULL)
8681 buf->b_prompt_interrupt = vim_strsave(callback);
8682 else
8683 /* pointer into the partial */
8684 buf->b_prompt_interrupt = callback;
8685 buf->b_prompt_int_partial = partial;
8686}
8687
8688/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008689 * "prompt_setprompt({buffer}, {text})" function
8690 */
8691 static void
8692f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8693{
8694 buf_T *buf;
8695 char_u *text;
8696
8697 if (check_secure())
8698 return;
8699 buf = get_buf_tv(&argvars[0], FALSE);
8700 if (buf == NULL)
8701 return;
8702
8703 text = get_tv_string(&argvars[1]);
8704 vim_free(buf->b_prompt_text);
8705 buf->b_prompt_text = vim_strsave(text);
8706}
8707#endif
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709/*
8710 * "pumvisible()" function
8711 */
8712 static void
8713f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8714{
8715#ifdef FEAT_INS_EXPAND
8716 if (pum_visible())
8717 rettv->vval.v_number = 1;
8718#endif
8719}
8720
8721#ifdef FEAT_PYTHON3
8722/*
8723 * "py3eval()" function
8724 */
8725 static void
8726f_py3eval(typval_T *argvars, typval_T *rettv)
8727{
8728 char_u *str;
8729 char_u buf[NUMBUFLEN];
8730
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008731 if (p_pyx == 0)
8732 p_pyx = 3;
8733
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 str = get_tv_string_buf(&argvars[0], buf);
8735 do_py3eval(str, rettv);
8736}
8737#endif
8738
8739#ifdef FEAT_PYTHON
8740/*
8741 * "pyeval()" function
8742 */
8743 static void
8744f_pyeval(typval_T *argvars, typval_T *rettv)
8745{
8746 char_u *str;
8747 char_u buf[NUMBUFLEN];
8748
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008749 if (p_pyx == 0)
8750 p_pyx = 2;
8751
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008752 str = get_tv_string_buf(&argvars[0], buf);
8753 do_pyeval(str, rettv);
8754}
8755#endif
8756
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008757#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8758/*
8759 * "pyxeval()" function
8760 */
8761 static void
8762f_pyxeval(typval_T *argvars, typval_T *rettv)
8763{
8764# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8765 init_pyxversion();
8766 if (p_pyx == 2)
8767 f_pyeval(argvars, rettv);
8768 else
8769 f_py3eval(argvars, rettv);
8770# elif defined(FEAT_PYTHON)
8771 f_pyeval(argvars, rettv);
8772# elif defined(FEAT_PYTHON3)
8773 f_py3eval(argvars, rettv);
8774# endif
8775}
8776#endif
8777
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778/*
8779 * "range()" function
8780 */
8781 static void
8782f_range(typval_T *argvars, typval_T *rettv)
8783{
8784 varnumber_T start;
8785 varnumber_T end;
8786 varnumber_T stride = 1;
8787 varnumber_T i;
8788 int error = FALSE;
8789
8790 start = get_tv_number_chk(&argvars[0], &error);
8791 if (argvars[1].v_type == VAR_UNKNOWN)
8792 {
8793 end = start - 1;
8794 start = 0;
8795 }
8796 else
8797 {
8798 end = get_tv_number_chk(&argvars[1], &error);
8799 if (argvars[2].v_type != VAR_UNKNOWN)
8800 stride = get_tv_number_chk(&argvars[2], &error);
8801 }
8802
8803 if (error)
8804 return; /* type error; errmsg already given */
8805 if (stride == 0)
8806 EMSG(_("E726: Stride is zero"));
8807 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8808 EMSG(_("E727: Start past end"));
8809 else
8810 {
8811 if (rettv_list_alloc(rettv) == OK)
8812 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8813 if (list_append_number(rettv->vval.v_list,
8814 (varnumber_T)i) == FAIL)
8815 break;
8816 }
8817}
8818
8819/*
8820 * "readfile()" function
8821 */
8822 static void
8823f_readfile(typval_T *argvars, typval_T *rettv)
8824{
8825 int binary = FALSE;
8826 int failed = FALSE;
8827 char_u *fname;
8828 FILE *fd;
8829 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8830 int io_size = sizeof(buf);
8831 int readlen; /* size of last fread() */
8832 char_u *prev = NULL; /* previously read bytes, if any */
8833 long prevlen = 0; /* length of data in prev */
8834 long prevsize = 0; /* size of prev buffer */
8835 long maxline = MAXLNUM;
8836 long cnt = 0;
8837 char_u *p; /* position in buf */
8838 char_u *start; /* start of current line */
8839
8840 if (argvars[1].v_type != VAR_UNKNOWN)
8841 {
8842 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8843 binary = TRUE;
8844 if (argvars[2].v_type != VAR_UNKNOWN)
8845 maxline = (long)get_tv_number(&argvars[2]);
8846 }
8847
8848 if (rettv_list_alloc(rettv) == FAIL)
8849 return;
8850
8851 /* Always open the file in binary mode, library functions have a mind of
8852 * their own about CR-LF conversion. */
8853 fname = get_tv_string(&argvars[0]);
8854 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8855 {
8856 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8857 return;
8858 }
8859
8860 while (cnt < maxline || maxline < 0)
8861 {
8862 readlen = (int)fread(buf, 1, io_size, fd);
8863
8864 /* This for loop processes what was read, but is also entered at end
8865 * of file so that either:
8866 * - an incomplete line gets written
8867 * - a "binary" file gets an empty line at the end if it ends in a
8868 * newline. */
8869 for (p = buf, start = buf;
8870 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8871 ++p)
8872 {
8873 if (*p == '\n' || readlen <= 0)
8874 {
8875 listitem_T *li;
8876 char_u *s = NULL;
8877 long_u len = p - start;
8878
8879 /* Finished a line. Remove CRs before NL. */
8880 if (readlen > 0 && !binary)
8881 {
8882 while (len > 0 && start[len - 1] == '\r')
8883 --len;
8884 /* removal may cross back to the "prev" string */
8885 if (len == 0)
8886 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8887 --prevlen;
8888 }
8889 if (prevlen == 0)
8890 s = vim_strnsave(start, (int)len);
8891 else
8892 {
8893 /* Change "prev" buffer to be the right size. This way
8894 * the bytes are only copied once, and very long lines are
8895 * allocated only once. */
8896 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8897 {
8898 mch_memmove(s + prevlen, start, len);
8899 s[prevlen + len] = NUL;
8900 prev = NULL; /* the list will own the string */
8901 prevlen = prevsize = 0;
8902 }
8903 }
8904 if (s == NULL)
8905 {
8906 do_outofmem_msg((long_u) prevlen + len + 1);
8907 failed = TRUE;
8908 break;
8909 }
8910
8911 if ((li = listitem_alloc()) == NULL)
8912 {
8913 vim_free(s);
8914 failed = TRUE;
8915 break;
8916 }
8917 li->li_tv.v_type = VAR_STRING;
8918 li->li_tv.v_lock = 0;
8919 li->li_tv.vval.v_string = s;
8920 list_append(rettv->vval.v_list, li);
8921
8922 start = p + 1; /* step over newline */
8923 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8924 break;
8925 }
8926 else if (*p == NUL)
8927 *p = '\n';
8928#ifdef FEAT_MBYTE
8929 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8930 * when finding the BF and check the previous two bytes. */
8931 else if (*p == 0xbf && enc_utf8 && !binary)
8932 {
8933 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8934 * + 1, these may be in the "prev" string. */
8935 char_u back1 = p >= buf + 1 ? p[-1]
8936 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8937 char_u back2 = p >= buf + 2 ? p[-2]
8938 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8939 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8940
8941 if (back2 == 0xef && back1 == 0xbb)
8942 {
8943 char_u *dest = p - 2;
8944
8945 /* Usually a BOM is at the beginning of a file, and so at
8946 * the beginning of a line; then we can just step over it.
8947 */
8948 if (start == dest)
8949 start = p + 1;
8950 else
8951 {
8952 /* have to shuffle buf to close gap */
8953 int adjust_prevlen = 0;
8954
8955 if (dest < buf)
8956 {
8957 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8958 dest = buf;
8959 }
8960 if (readlen > p - buf + 1)
8961 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8962 readlen -= 3 - adjust_prevlen;
8963 prevlen -= adjust_prevlen;
8964 p = dest - 1;
8965 }
8966 }
8967 }
8968#endif
8969 } /* for */
8970
8971 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8972 break;
8973 if (start < p)
8974 {
8975 /* There's part of a line in buf, store it in "prev". */
8976 if (p - start + prevlen >= prevsize)
8977 {
8978 /* need bigger "prev" buffer */
8979 char_u *newprev;
8980
8981 /* A common use case is ordinary text files and "prev" gets a
8982 * fragment of a line, so the first allocation is made
8983 * small, to avoid repeatedly 'allocing' large and
8984 * 'reallocing' small. */
8985 if (prevsize == 0)
8986 prevsize = (long)(p - start);
8987 else
8988 {
8989 long grow50pc = (prevsize * 3) / 2;
8990 long growmin = (long)((p - start) * 2 + prevlen);
8991 prevsize = grow50pc > growmin ? grow50pc : growmin;
8992 }
8993 newprev = prev == NULL ? alloc(prevsize)
8994 : vim_realloc(prev, prevsize);
8995 if (newprev == NULL)
8996 {
8997 do_outofmem_msg((long_u)prevsize);
8998 failed = TRUE;
8999 break;
9000 }
9001 prev = newprev;
9002 }
9003 /* Add the line part to end of "prev". */
9004 mch_memmove(prev + prevlen, start, p - start);
9005 prevlen += (long)(p - start);
9006 }
9007 } /* while */
9008
9009 /*
9010 * For a negative line count use only the lines at the end of the file,
9011 * free the rest.
9012 */
9013 if (!failed && maxline < 0)
9014 while (cnt > -maxline)
9015 {
9016 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9017 --cnt;
9018 }
9019
9020 if (failed)
9021 {
9022 list_free(rettv->vval.v_list);
9023 /* readfile doc says an empty list is returned on error */
9024 rettv->vval.v_list = list_alloc();
9025 }
9026
9027 vim_free(prev);
9028 fclose(fd);
9029}
9030
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009031 static void
9032return_register(int regname, typval_T *rettv)
9033{
9034 char_u buf[2] = {0, 0};
9035
9036 buf[0] = (char_u)regname;
9037 rettv->v_type = VAR_STRING;
9038 rettv->vval.v_string = vim_strsave(buf);
9039}
9040
9041/*
9042 * "reg_executing()" function
9043 */
9044 static void
9045f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9046{
9047 return_register(reg_executing, rettv);
9048}
9049
9050/*
9051 * "reg_recording()" function
9052 */
9053 static void
9054f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9055{
9056 return_register(reg_recording, rettv);
9057}
9058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059#if defined(FEAT_RELTIME)
9060static int list2proftime(typval_T *arg, proftime_T *tm);
9061
9062/*
9063 * Convert a List to proftime_T.
9064 * Return FAIL when there is something wrong.
9065 */
9066 static int
9067list2proftime(typval_T *arg, proftime_T *tm)
9068{
9069 long n1, n2;
9070 int error = FALSE;
9071
9072 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9073 || arg->vval.v_list->lv_len != 2)
9074 return FAIL;
9075 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9076 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9077# ifdef WIN3264
9078 tm->HighPart = n1;
9079 tm->LowPart = n2;
9080# else
9081 tm->tv_sec = n1;
9082 tm->tv_usec = n2;
9083# endif
9084 return error ? FAIL : OK;
9085}
9086#endif /* FEAT_RELTIME */
9087
9088/*
9089 * "reltime()" function
9090 */
9091 static void
9092f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9093{
9094#ifdef FEAT_RELTIME
9095 proftime_T res;
9096 proftime_T start;
9097
9098 if (argvars[0].v_type == VAR_UNKNOWN)
9099 {
9100 /* No arguments: get current time. */
9101 profile_start(&res);
9102 }
9103 else if (argvars[1].v_type == VAR_UNKNOWN)
9104 {
9105 if (list2proftime(&argvars[0], &res) == FAIL)
9106 return;
9107 profile_end(&res);
9108 }
9109 else
9110 {
9111 /* Two arguments: compute the difference. */
9112 if (list2proftime(&argvars[0], &start) == FAIL
9113 || list2proftime(&argvars[1], &res) == FAIL)
9114 return;
9115 profile_sub(&res, &start);
9116 }
9117
9118 if (rettv_list_alloc(rettv) == OK)
9119 {
9120 long n1, n2;
9121
9122# ifdef WIN3264
9123 n1 = res.HighPart;
9124 n2 = res.LowPart;
9125# else
9126 n1 = res.tv_sec;
9127 n2 = res.tv_usec;
9128# endif
9129 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9130 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9131 }
9132#endif
9133}
9134
9135#ifdef FEAT_FLOAT
9136/*
9137 * "reltimefloat()" function
9138 */
9139 static void
9140f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9141{
9142# ifdef FEAT_RELTIME
9143 proftime_T tm;
9144# endif
9145
9146 rettv->v_type = VAR_FLOAT;
9147 rettv->vval.v_float = 0;
9148# ifdef FEAT_RELTIME
9149 if (list2proftime(&argvars[0], &tm) == OK)
9150 rettv->vval.v_float = profile_float(&tm);
9151# endif
9152}
9153#endif
9154
9155/*
9156 * "reltimestr()" function
9157 */
9158 static void
9159f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9160{
9161#ifdef FEAT_RELTIME
9162 proftime_T tm;
9163#endif
9164
9165 rettv->v_type = VAR_STRING;
9166 rettv->vval.v_string = NULL;
9167#ifdef FEAT_RELTIME
9168 if (list2proftime(&argvars[0], &tm) == OK)
9169 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9170#endif
9171}
9172
9173#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9174static void make_connection(void);
9175static int check_connection(void);
9176
9177 static void
9178make_connection(void)
9179{
9180 if (X_DISPLAY == NULL
9181# ifdef FEAT_GUI
9182 && !gui.in_use
9183# endif
9184 )
9185 {
9186 x_force_connect = TRUE;
9187 setup_term_clip();
9188 x_force_connect = FALSE;
9189 }
9190}
9191
9192 static int
9193check_connection(void)
9194{
9195 make_connection();
9196 if (X_DISPLAY == NULL)
9197 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009198 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009199 return FAIL;
9200 }
9201 return OK;
9202}
9203#endif
9204
9205#ifdef FEAT_CLIENTSERVER
9206 static void
9207remote_common(typval_T *argvars, typval_T *rettv, int expr)
9208{
9209 char_u *server_name;
9210 char_u *keys;
9211 char_u *r = NULL;
9212 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009213 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009214# ifdef WIN32
9215 HWND w;
9216# else
9217 Window w;
9218# endif
9219
9220 if (check_restricted() || check_secure())
9221 return;
9222
9223# ifdef FEAT_X11
9224 if (check_connection() == FAIL)
9225 return;
9226# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009227 if (argvars[2].v_type != VAR_UNKNOWN
9228 && argvars[3].v_type != VAR_UNKNOWN)
9229 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230
9231 server_name = get_tv_string_chk(&argvars[0]);
9232 if (server_name == NULL)
9233 return; /* type error; errmsg already given */
9234 keys = get_tv_string_buf(&argvars[1], buf);
9235# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009236 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009238 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9239 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009240# endif
9241 {
9242 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009243 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009244 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009245 vim_free(r);
9246 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 else
9248 EMSG2(_("E241: Unable to send to %s"), server_name);
9249 return;
9250 }
9251
9252 rettv->vval.v_string = r;
9253
9254 if (argvars[2].v_type != VAR_UNKNOWN)
9255 {
9256 dictitem_T v;
9257 char_u str[30];
9258 char_u *idvar;
9259
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009260 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009261 if (idvar != NULL && *idvar != NUL)
9262 {
9263 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9264 v.di_tv.v_type = VAR_STRING;
9265 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009266 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009267 vim_free(v.di_tv.vval.v_string);
9268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009269 }
9270}
9271#endif
9272
9273/*
9274 * "remote_expr()" function
9275 */
9276 static void
9277f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9278{
9279 rettv->v_type = VAR_STRING;
9280 rettv->vval.v_string = NULL;
9281#ifdef FEAT_CLIENTSERVER
9282 remote_common(argvars, rettv, TRUE);
9283#endif
9284}
9285
9286/*
9287 * "remote_foreground()" function
9288 */
9289 static void
9290f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9291{
9292#ifdef FEAT_CLIENTSERVER
9293# ifdef WIN32
9294 /* On Win32 it's done in this application. */
9295 {
9296 char_u *server_name = get_tv_string_chk(&argvars[0]);
9297
9298 if (server_name != NULL)
9299 serverForeground(server_name);
9300 }
9301# else
9302 /* Send a foreground() expression to the server. */
9303 argvars[1].v_type = VAR_STRING;
9304 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9305 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009306 rettv->v_type = VAR_STRING;
9307 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009308 remote_common(argvars, rettv, TRUE);
9309 vim_free(argvars[1].vval.v_string);
9310# endif
9311#endif
9312}
9313
9314 static void
9315f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9316{
9317#ifdef FEAT_CLIENTSERVER
9318 dictitem_T v;
9319 char_u *s = NULL;
9320# ifdef WIN32
9321 long_u n = 0;
9322# endif
9323 char_u *serverid;
9324
9325 if (check_restricted() || check_secure())
9326 {
9327 rettv->vval.v_number = -1;
9328 return;
9329 }
9330 serverid = get_tv_string_chk(&argvars[0]);
9331 if (serverid == NULL)
9332 {
9333 rettv->vval.v_number = -1;
9334 return; /* type error; errmsg already given */
9335 }
9336# ifdef WIN32
9337 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9338 if (n == 0)
9339 rettv->vval.v_number = -1;
9340 else
9341 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009342 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 rettv->vval.v_number = (s != NULL);
9344 }
9345# else
9346 if (check_connection() == FAIL)
9347 return;
9348
9349 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9350 serverStrToWin(serverid), &s);
9351# endif
9352
9353 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9354 {
9355 char_u *retvar;
9356
9357 v.di_tv.v_type = VAR_STRING;
9358 v.di_tv.vval.v_string = vim_strsave(s);
9359 retvar = get_tv_string_chk(&argvars[1]);
9360 if (retvar != NULL)
9361 set_var(retvar, &v.di_tv, FALSE);
9362 vim_free(v.di_tv.vval.v_string);
9363 }
9364#else
9365 rettv->vval.v_number = -1;
9366#endif
9367}
9368
9369 static void
9370f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9371{
9372 char_u *r = NULL;
9373
9374#ifdef FEAT_CLIENTSERVER
9375 char_u *serverid = get_tv_string_chk(&argvars[0]);
9376
9377 if (serverid != NULL && !check_restricted() && !check_secure())
9378 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009379 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009380# ifdef WIN32
9381 /* The server's HWND is encoded in the 'id' parameter */
9382 long_u n = 0;
9383# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009384
9385 if (argvars[1].v_type != VAR_UNKNOWN)
9386 timeout = get_tv_number(&argvars[1]);
9387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9390 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009391 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392 if (r == NULL)
9393# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009394 if (check_connection() == FAIL
9395 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9396 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009397# endif
9398 EMSG(_("E277: Unable to read a server reply"));
9399 }
9400#endif
9401 rettv->v_type = VAR_STRING;
9402 rettv->vval.v_string = r;
9403}
9404
9405/*
9406 * "remote_send()" function
9407 */
9408 static void
9409f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9410{
9411 rettv->v_type = VAR_STRING;
9412 rettv->vval.v_string = NULL;
9413#ifdef FEAT_CLIENTSERVER
9414 remote_common(argvars, rettv, FALSE);
9415#endif
9416}
9417
9418/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009419 * "remote_startserver()" function
9420 */
9421 static void
9422f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9423{
9424#ifdef FEAT_CLIENTSERVER
9425 char_u *server = get_tv_string_chk(&argvars[0]);
9426
9427 if (server == NULL)
9428 return; /* type error; errmsg already given */
9429 if (serverName != NULL)
9430 EMSG(_("E941: already started a server"));
9431 else
9432 {
9433# ifdef FEAT_X11
9434 if (check_connection() == OK)
9435 serverRegisterName(X_DISPLAY, server);
9436# else
9437 serverSetName(server);
9438# endif
9439 }
9440#else
9441 EMSG(_("E942: +clientserver feature not available"));
9442#endif
9443}
9444
9445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446 * "remove()" function
9447 */
9448 static void
9449f_remove(typval_T *argvars, typval_T *rettv)
9450{
9451 list_T *l;
9452 listitem_T *item, *item2;
9453 listitem_T *li;
9454 long idx;
9455 long end;
9456 char_u *key;
9457 dict_T *d;
9458 dictitem_T *di;
9459 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9460
9461 if (argvars[0].v_type == VAR_DICT)
9462 {
9463 if (argvars[2].v_type != VAR_UNKNOWN)
9464 EMSG2(_(e_toomanyarg), "remove()");
9465 else if ((d = argvars[0].vval.v_dict) != NULL
9466 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9467 {
9468 key = get_tv_string_chk(&argvars[1]);
9469 if (key != NULL)
9470 {
9471 di = dict_find(d, key, -1);
9472 if (di == NULL)
9473 EMSG2(_(e_dictkey), key);
9474 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9475 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9476 {
9477 *rettv = di->di_tv;
9478 init_tv(&di->di_tv);
9479 dictitem_remove(d, di);
9480 }
9481 }
9482 }
9483 }
9484 else if (argvars[0].v_type != VAR_LIST)
9485 EMSG2(_(e_listdictarg), "remove()");
9486 else if ((l = argvars[0].vval.v_list) != NULL
9487 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9488 {
9489 int error = FALSE;
9490
9491 idx = (long)get_tv_number_chk(&argvars[1], &error);
9492 if (error)
9493 ; /* type error: do nothing, errmsg already given */
9494 else if ((item = list_find(l, idx)) == NULL)
9495 EMSGN(_(e_listidx), idx);
9496 else
9497 {
9498 if (argvars[2].v_type == VAR_UNKNOWN)
9499 {
9500 /* Remove one item, return its value. */
9501 vimlist_remove(l, item, item);
9502 *rettv = item->li_tv;
9503 vim_free(item);
9504 }
9505 else
9506 {
9507 /* Remove range of items, return list with values. */
9508 end = (long)get_tv_number_chk(&argvars[2], &error);
9509 if (error)
9510 ; /* type error: do nothing */
9511 else if ((item2 = list_find(l, end)) == NULL)
9512 EMSGN(_(e_listidx), end);
9513 else
9514 {
9515 int cnt = 0;
9516
9517 for (li = item; li != NULL; li = li->li_next)
9518 {
9519 ++cnt;
9520 if (li == item2)
9521 break;
9522 }
9523 if (li == NULL) /* didn't find "item2" after "item" */
9524 EMSG(_(e_invrange));
9525 else
9526 {
9527 vimlist_remove(l, item, item2);
9528 if (rettv_list_alloc(rettv) == OK)
9529 {
9530 l = rettv->vval.v_list;
9531 l->lv_first = item;
9532 l->lv_last = item2;
9533 item->li_prev = NULL;
9534 item2->li_next = NULL;
9535 l->lv_len = cnt;
9536 }
9537 }
9538 }
9539 }
9540 }
9541 }
9542}
9543
9544/*
9545 * "rename({from}, {to})" function
9546 */
9547 static void
9548f_rename(typval_T *argvars, typval_T *rettv)
9549{
9550 char_u buf[NUMBUFLEN];
9551
9552 if (check_restricted() || check_secure())
9553 rettv->vval.v_number = -1;
9554 else
9555 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9556 get_tv_string_buf(&argvars[1], buf));
9557}
9558
9559/*
9560 * "repeat()" function
9561 */
9562 static void
9563f_repeat(typval_T *argvars, typval_T *rettv)
9564{
9565 char_u *p;
9566 int n;
9567 int slen;
9568 int len;
9569 char_u *r;
9570 int i;
9571
9572 n = (int)get_tv_number(&argvars[1]);
9573 if (argvars[0].v_type == VAR_LIST)
9574 {
9575 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9576 while (n-- > 0)
9577 if (list_extend(rettv->vval.v_list,
9578 argvars[0].vval.v_list, NULL) == FAIL)
9579 break;
9580 }
9581 else
9582 {
9583 p = get_tv_string(&argvars[0]);
9584 rettv->v_type = VAR_STRING;
9585 rettv->vval.v_string = NULL;
9586
9587 slen = (int)STRLEN(p);
9588 len = slen * n;
9589 if (len <= 0)
9590 return;
9591
9592 r = alloc(len + 1);
9593 if (r != NULL)
9594 {
9595 for (i = 0; i < n; i++)
9596 mch_memmove(r + i * slen, p, (size_t)slen);
9597 r[len] = NUL;
9598 }
9599
9600 rettv->vval.v_string = r;
9601 }
9602}
9603
9604/*
9605 * "resolve()" function
9606 */
9607 static void
9608f_resolve(typval_T *argvars, typval_T *rettv)
9609{
9610 char_u *p;
9611#ifdef HAVE_READLINK
9612 char_u *buf = NULL;
9613#endif
9614
9615 p = get_tv_string(&argvars[0]);
9616#ifdef FEAT_SHORTCUT
9617 {
9618 char_u *v = NULL;
9619
9620 v = mch_resolve_shortcut(p);
9621 if (v != NULL)
9622 rettv->vval.v_string = v;
9623 else
9624 rettv->vval.v_string = vim_strsave(p);
9625 }
9626#else
9627# ifdef HAVE_READLINK
9628 {
9629 char_u *cpy;
9630 int len;
9631 char_u *remain = NULL;
9632 char_u *q;
9633 int is_relative_to_current = FALSE;
9634 int has_trailing_pathsep = FALSE;
9635 int limit = 100;
9636
9637 p = vim_strsave(p);
9638
9639 if (p[0] == '.' && (vim_ispathsep(p[1])
9640 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9641 is_relative_to_current = TRUE;
9642
9643 len = STRLEN(p);
9644 if (len > 0 && after_pathsep(p, p + len))
9645 {
9646 has_trailing_pathsep = TRUE;
9647 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9648 }
9649
9650 q = getnextcomp(p);
9651 if (*q != NUL)
9652 {
9653 /* Separate the first path component in "p", and keep the
9654 * remainder (beginning with the path separator). */
9655 remain = vim_strsave(q - 1);
9656 q[-1] = NUL;
9657 }
9658
9659 buf = alloc(MAXPATHL + 1);
9660 if (buf == NULL)
9661 goto fail;
9662
9663 for (;;)
9664 {
9665 for (;;)
9666 {
9667 len = readlink((char *)p, (char *)buf, MAXPATHL);
9668 if (len <= 0)
9669 break;
9670 buf[len] = NUL;
9671
9672 if (limit-- == 0)
9673 {
9674 vim_free(p);
9675 vim_free(remain);
9676 EMSG(_("E655: Too many symbolic links (cycle?)"));
9677 rettv->vval.v_string = NULL;
9678 goto fail;
9679 }
9680
9681 /* Ensure that the result will have a trailing path separator
9682 * if the argument has one. */
9683 if (remain == NULL && has_trailing_pathsep)
9684 add_pathsep(buf);
9685
9686 /* Separate the first path component in the link value and
9687 * concatenate the remainders. */
9688 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9689 if (*q != NUL)
9690 {
9691 if (remain == NULL)
9692 remain = vim_strsave(q - 1);
9693 else
9694 {
9695 cpy = concat_str(q - 1, remain);
9696 if (cpy != NULL)
9697 {
9698 vim_free(remain);
9699 remain = cpy;
9700 }
9701 }
9702 q[-1] = NUL;
9703 }
9704
9705 q = gettail(p);
9706 if (q > p && *q == NUL)
9707 {
9708 /* Ignore trailing path separator. */
9709 q[-1] = NUL;
9710 q = gettail(p);
9711 }
9712 if (q > p && !mch_isFullName(buf))
9713 {
9714 /* symlink is relative to directory of argument */
9715 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9716 if (cpy != NULL)
9717 {
9718 STRCPY(cpy, p);
9719 STRCPY(gettail(cpy), buf);
9720 vim_free(p);
9721 p = cpy;
9722 }
9723 }
9724 else
9725 {
9726 vim_free(p);
9727 p = vim_strsave(buf);
9728 }
9729 }
9730
9731 if (remain == NULL)
9732 break;
9733
9734 /* Append the first path component of "remain" to "p". */
9735 q = getnextcomp(remain + 1);
9736 len = q - remain - (*q != NUL);
9737 cpy = vim_strnsave(p, STRLEN(p) + len);
9738 if (cpy != NULL)
9739 {
9740 STRNCAT(cpy, remain, len);
9741 vim_free(p);
9742 p = cpy;
9743 }
9744 /* Shorten "remain". */
9745 if (*q != NUL)
9746 STRMOVE(remain, q - 1);
9747 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009748 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749 }
9750
9751 /* If the result is a relative path name, make it explicitly relative to
9752 * the current directory if and only if the argument had this form. */
9753 if (!vim_ispathsep(*p))
9754 {
9755 if (is_relative_to_current
9756 && *p != NUL
9757 && !(p[0] == '.'
9758 && (p[1] == NUL
9759 || vim_ispathsep(p[1])
9760 || (p[1] == '.'
9761 && (p[2] == NUL
9762 || vim_ispathsep(p[2]))))))
9763 {
9764 /* Prepend "./". */
9765 cpy = concat_str((char_u *)"./", p);
9766 if (cpy != NULL)
9767 {
9768 vim_free(p);
9769 p = cpy;
9770 }
9771 }
9772 else if (!is_relative_to_current)
9773 {
9774 /* Strip leading "./". */
9775 q = p;
9776 while (q[0] == '.' && vim_ispathsep(q[1]))
9777 q += 2;
9778 if (q > p)
9779 STRMOVE(p, p + 2);
9780 }
9781 }
9782
9783 /* Ensure that the result will have no trailing path separator
9784 * if the argument had none. But keep "/" or "//". */
9785 if (!has_trailing_pathsep)
9786 {
9787 q = p + STRLEN(p);
9788 if (after_pathsep(p, q))
9789 *gettail_sep(p) = NUL;
9790 }
9791
9792 rettv->vval.v_string = p;
9793 }
9794# else
9795 rettv->vval.v_string = vim_strsave(p);
9796# endif
9797#endif
9798
9799 simplify_filename(rettv->vval.v_string);
9800
9801#ifdef HAVE_READLINK
9802fail:
9803 vim_free(buf);
9804#endif
9805 rettv->v_type = VAR_STRING;
9806}
9807
9808/*
9809 * "reverse({list})" function
9810 */
9811 static void
9812f_reverse(typval_T *argvars, typval_T *rettv)
9813{
9814 list_T *l;
9815 listitem_T *li, *ni;
9816
9817 if (argvars[0].v_type != VAR_LIST)
9818 EMSG2(_(e_listarg), "reverse()");
9819 else if ((l = argvars[0].vval.v_list) != NULL
9820 && !tv_check_lock(l->lv_lock,
9821 (char_u *)N_("reverse() argument"), TRUE))
9822 {
9823 li = l->lv_last;
9824 l->lv_first = l->lv_last = NULL;
9825 l->lv_len = 0;
9826 while (li != NULL)
9827 {
9828 ni = li->li_prev;
9829 list_append(l, li);
9830 li = ni;
9831 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009832 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 l->lv_idx = l->lv_len - l->lv_idx - 1;
9834 }
9835}
9836
9837#define SP_NOMOVE 0x01 /* don't move cursor */
9838#define SP_REPEAT 0x02 /* repeat to find outer pair */
9839#define SP_RETCOUNT 0x04 /* return matchcount */
9840#define SP_SETPCMARK 0x08 /* set previous context mark */
9841#define SP_START 0x10 /* accept match at start position */
9842#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9843#define SP_END 0x40 /* leave cursor at end of match */
9844#define SP_COLUMN 0x80 /* start at cursor column */
9845
9846static int get_search_arg(typval_T *varp, int *flagsp);
9847
9848/*
9849 * Get flags for a search function.
9850 * Possibly sets "p_ws".
9851 * Returns BACKWARD, FORWARD or zero (for an error).
9852 */
9853 static int
9854get_search_arg(typval_T *varp, int *flagsp)
9855{
9856 int dir = FORWARD;
9857 char_u *flags;
9858 char_u nbuf[NUMBUFLEN];
9859 int mask;
9860
9861 if (varp->v_type != VAR_UNKNOWN)
9862 {
9863 flags = get_tv_string_buf_chk(varp, nbuf);
9864 if (flags == NULL)
9865 return 0; /* type error; errmsg already given */
9866 while (*flags != NUL)
9867 {
9868 switch (*flags)
9869 {
9870 case 'b': dir = BACKWARD; break;
9871 case 'w': p_ws = TRUE; break;
9872 case 'W': p_ws = FALSE; break;
9873 default: mask = 0;
9874 if (flagsp != NULL)
9875 switch (*flags)
9876 {
9877 case 'c': mask = SP_START; break;
9878 case 'e': mask = SP_END; break;
9879 case 'm': mask = SP_RETCOUNT; break;
9880 case 'n': mask = SP_NOMOVE; break;
9881 case 'p': mask = SP_SUBPAT; break;
9882 case 'r': mask = SP_REPEAT; break;
9883 case 's': mask = SP_SETPCMARK; break;
9884 case 'z': mask = SP_COLUMN; break;
9885 }
9886 if (mask == 0)
9887 {
9888 EMSG2(_(e_invarg2), flags);
9889 dir = 0;
9890 }
9891 else
9892 *flagsp |= mask;
9893 }
9894 if (dir == 0)
9895 break;
9896 ++flags;
9897 }
9898 }
9899 return dir;
9900}
9901
9902/*
9903 * Shared by search() and searchpos() functions.
9904 */
9905 static int
9906search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9907{
9908 int flags;
9909 char_u *pat;
9910 pos_T pos;
9911 pos_T save_cursor;
9912 int save_p_ws = p_ws;
9913 int dir;
9914 int retval = 0; /* default: FAIL */
9915 long lnum_stop = 0;
9916 proftime_T tm;
9917#ifdef FEAT_RELTIME
9918 long time_limit = 0;
9919#endif
9920 int options = SEARCH_KEEP;
9921 int subpatnum;
9922
9923 pat = get_tv_string(&argvars[0]);
9924 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9925 if (dir == 0)
9926 goto theend;
9927 flags = *flagsp;
9928 if (flags & SP_START)
9929 options |= SEARCH_START;
9930 if (flags & SP_END)
9931 options |= SEARCH_END;
9932 if (flags & SP_COLUMN)
9933 options |= SEARCH_COL;
9934
9935 /* Optional arguments: line number to stop searching and timeout. */
9936 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9937 {
9938 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9939 if (lnum_stop < 0)
9940 goto theend;
9941#ifdef FEAT_RELTIME
9942 if (argvars[3].v_type != VAR_UNKNOWN)
9943 {
9944 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9945 if (time_limit < 0)
9946 goto theend;
9947 }
9948#endif
9949 }
9950
9951#ifdef FEAT_RELTIME
9952 /* Set the time limit, if there is one. */
9953 profile_setlimit(time_limit, &tm);
9954#endif
9955
9956 /*
9957 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9958 * Check to make sure only those flags are set.
9959 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9960 * flags cannot be set. Check for that condition also.
9961 */
9962 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9963 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9964 {
9965 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9966 goto theend;
9967 }
9968
9969 pos = save_cursor = curwin->w_cursor;
9970 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009971 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009972 if (subpatnum != FAIL)
9973 {
9974 if (flags & SP_SUBPAT)
9975 retval = subpatnum;
9976 else
9977 retval = pos.lnum;
9978 if (flags & SP_SETPCMARK)
9979 setpcmark();
9980 curwin->w_cursor = pos;
9981 if (match_pos != NULL)
9982 {
9983 /* Store the match cursor position */
9984 match_pos->lnum = pos.lnum;
9985 match_pos->col = pos.col + 1;
9986 }
9987 /* "/$" will put the cursor after the end of the line, may need to
9988 * correct that here */
9989 check_cursor();
9990 }
9991
9992 /* If 'n' flag is used: restore cursor position. */
9993 if (flags & SP_NOMOVE)
9994 curwin->w_cursor = save_cursor;
9995 else
9996 curwin->w_set_curswant = TRUE;
9997theend:
9998 p_ws = save_p_ws;
9999
10000 return retval;
10001}
10002
10003#ifdef FEAT_FLOAT
10004
10005/*
10006 * round() is not in C90, use ceil() or floor() instead.
10007 */
10008 float_T
10009vim_round(float_T f)
10010{
10011 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10012}
10013
10014/*
10015 * "round({float})" function
10016 */
10017 static void
10018f_round(typval_T *argvars, typval_T *rettv)
10019{
10020 float_T f = 0.0;
10021
10022 rettv->v_type = VAR_FLOAT;
10023 if (get_float_arg(argvars, &f) == OK)
10024 rettv->vval.v_float = vim_round(f);
10025 else
10026 rettv->vval.v_float = 0.0;
10027}
10028#endif
10029
10030/*
10031 * "screenattr()" function
10032 */
10033 static void
10034f_screenattr(typval_T *argvars, typval_T *rettv)
10035{
10036 int row;
10037 int col;
10038 int c;
10039
10040 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10041 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10042 if (row < 0 || row >= screen_Rows
10043 || col < 0 || col >= screen_Columns)
10044 c = -1;
10045 else
10046 c = ScreenAttrs[LineOffset[row] + col];
10047 rettv->vval.v_number = c;
10048}
10049
10050/*
10051 * "screenchar()" function
10052 */
10053 static void
10054f_screenchar(typval_T *argvars, typval_T *rettv)
10055{
10056 int row;
10057 int col;
10058 int off;
10059 int c;
10060
10061 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10062 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10063 if (row < 0 || row >= screen_Rows
10064 || col < 0 || col >= screen_Columns)
10065 c = -1;
10066 else
10067 {
10068 off = LineOffset[row] + col;
10069#ifdef FEAT_MBYTE
10070 if (enc_utf8 && ScreenLinesUC[off] != 0)
10071 c = ScreenLinesUC[off];
10072 else
10073#endif
10074 c = ScreenLines[off];
10075 }
10076 rettv->vval.v_number = c;
10077}
10078
10079/*
10080 * "screencol()" function
10081 *
10082 * First column is 1 to be consistent with virtcol().
10083 */
10084 static void
10085f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10086{
10087 rettv->vval.v_number = screen_screencol() + 1;
10088}
10089
10090/*
10091 * "screenrow()" function
10092 */
10093 static void
10094f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10095{
10096 rettv->vval.v_number = screen_screenrow() + 1;
10097}
10098
10099/*
10100 * "search()" function
10101 */
10102 static void
10103f_search(typval_T *argvars, typval_T *rettv)
10104{
10105 int flags = 0;
10106
10107 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10108}
10109
10110/*
10111 * "searchdecl()" function
10112 */
10113 static void
10114f_searchdecl(typval_T *argvars, typval_T *rettv)
10115{
10116 int locally = 1;
10117 int thisblock = 0;
10118 int error = FALSE;
10119 char_u *name;
10120
10121 rettv->vval.v_number = 1; /* default: FAIL */
10122
10123 name = get_tv_string_chk(&argvars[0]);
10124 if (argvars[1].v_type != VAR_UNKNOWN)
10125 {
10126 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10127 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10128 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10129 }
10130 if (!error && name != NULL)
10131 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10132 locally, thisblock, SEARCH_KEEP) == FAIL;
10133}
10134
10135/*
10136 * Used by searchpair() and searchpairpos()
10137 */
10138 static int
10139searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10140{
10141 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010142 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010143 int save_p_ws = p_ws;
10144 int dir;
10145 int flags = 0;
10146 char_u nbuf1[NUMBUFLEN];
10147 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010148 int retval = 0; /* default: FAIL */
10149 long lnum_stop = 0;
10150 long time_limit = 0;
10151
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010152 /* Get the three pattern arguments: start, middle, end. Will result in an
10153 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 spat = get_tv_string_chk(&argvars[0]);
10155 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10156 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10157 if (spat == NULL || mpat == NULL || epat == NULL)
10158 goto theend; /* type error */
10159
10160 /* Handle the optional fourth argument: flags */
10161 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10162 if (dir == 0)
10163 goto theend;
10164
10165 /* Don't accept SP_END or SP_SUBPAT.
10166 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10167 */
10168 if ((flags & (SP_END | SP_SUBPAT)) != 0
10169 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10170 {
10171 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10172 goto theend;
10173 }
10174
10175 /* Using 'r' implies 'W', otherwise it doesn't work. */
10176 if (flags & SP_REPEAT)
10177 p_ws = FALSE;
10178
10179 /* Optional fifth argument: skip expression */
10180 if (argvars[3].v_type == VAR_UNKNOWN
10181 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010182 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010183 else
10184 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010185 skip = &argvars[4];
10186 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10187 && skip->v_type != VAR_STRING)
10188 {
10189 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010190 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010191 goto theend;
10192 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 if (argvars[5].v_type != VAR_UNKNOWN)
10194 {
10195 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10196 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010197 {
10198 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010199 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010200 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201#ifdef FEAT_RELTIME
10202 if (argvars[6].v_type != VAR_UNKNOWN)
10203 {
10204 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10205 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010206 {
10207 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010208 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010209 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 }
10211#endif
10212 }
10213 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010214
10215 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10216 match_pos, lnum_stop, time_limit);
10217
10218theend:
10219 p_ws = save_p_ws;
10220
10221 return retval;
10222}
10223
10224/*
10225 * "searchpair()" function
10226 */
10227 static void
10228f_searchpair(typval_T *argvars, typval_T *rettv)
10229{
10230 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10231}
10232
10233/*
10234 * "searchpairpos()" function
10235 */
10236 static void
10237f_searchpairpos(typval_T *argvars, typval_T *rettv)
10238{
10239 pos_T match_pos;
10240 int lnum = 0;
10241 int col = 0;
10242
10243 if (rettv_list_alloc(rettv) == FAIL)
10244 return;
10245
10246 if (searchpair_cmn(argvars, &match_pos) > 0)
10247 {
10248 lnum = match_pos.lnum;
10249 col = match_pos.col;
10250 }
10251
10252 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10253 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10254}
10255
10256/*
10257 * Search for a start/middle/end thing.
10258 * Used by searchpair(), see its documentation for the details.
10259 * Returns 0 or -1 for no match,
10260 */
10261 long
10262do_searchpair(
10263 char_u *spat, /* start pattern */
10264 char_u *mpat, /* middle pattern */
10265 char_u *epat, /* end pattern */
10266 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010267 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 int flags, /* SP_SETPCMARK and other SP_ values */
10269 pos_T *match_pos,
10270 linenr_T lnum_stop, /* stop at this line if not zero */
10271 long time_limit UNUSED) /* stop after this many msec */
10272{
10273 char_u *save_cpo;
10274 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10275 long retval = 0;
10276 pos_T pos;
10277 pos_T firstpos;
10278 pos_T foundpos;
10279 pos_T save_cursor;
10280 pos_T save_pos;
10281 int n;
10282 int r;
10283 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010284 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010285 int err;
10286 int options = SEARCH_KEEP;
10287 proftime_T tm;
10288
10289 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10290 save_cpo = p_cpo;
10291 p_cpo = empty_option;
10292
10293#ifdef FEAT_RELTIME
10294 /* Set the time limit, if there is one. */
10295 profile_setlimit(time_limit, &tm);
10296#endif
10297
10298 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10299 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010300 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10301 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302 if (pat2 == NULL || pat3 == NULL)
10303 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010304 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010305 if (*mpat == NUL)
10306 STRCPY(pat3, pat2);
10307 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010308 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309 spat, epat, mpat);
10310 if (flags & SP_START)
10311 options |= SEARCH_START;
10312
Bram Moolenaar48570482017-10-30 21:48:41 +010010313 if (skip != NULL)
10314 {
10315 /* Empty string means to not use the skip expression. */
10316 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10317 use_skip = skip->vval.v_string != NULL
10318 && *skip->vval.v_string != NUL;
10319 }
10320
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010321 save_cursor = curwin->w_cursor;
10322 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010323 CLEAR_POS(&firstpos);
10324 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 pat = pat3;
10326 for (;;)
10327 {
10328 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010329 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010330 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331 /* didn't find it or found the first match again: FAIL */
10332 break;
10333
10334 if (firstpos.lnum == 0)
10335 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010336 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010337 {
10338 /* Found the same position again. Can happen with a pattern that
10339 * has "\zs" at the end and searching backwards. Advance one
10340 * character and try again. */
10341 if (dir == BACKWARD)
10342 decl(&pos);
10343 else
10344 incl(&pos);
10345 }
10346 foundpos = pos;
10347
10348 /* clear the start flag to avoid getting stuck here */
10349 options &= ~SEARCH_START;
10350
10351 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010352 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010353 {
10354 save_pos = curwin->w_cursor;
10355 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010356 err = FALSE;
10357 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010358 curwin->w_cursor = save_pos;
10359 if (err)
10360 {
10361 /* Evaluating {skip} caused an error, break here. */
10362 curwin->w_cursor = save_cursor;
10363 retval = -1;
10364 break;
10365 }
10366 if (r)
10367 continue;
10368 }
10369
10370 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10371 {
10372 /* Found end when searching backwards or start when searching
10373 * forward: nested pair. */
10374 ++nest;
10375 pat = pat2; /* nested, don't search for middle */
10376 }
10377 else
10378 {
10379 /* Found end when searching forward or start when searching
10380 * backward: end of (nested) pair; or found middle in outer pair. */
10381 if (--nest == 1)
10382 pat = pat3; /* outer level, search for middle */
10383 }
10384
10385 if (nest == 0)
10386 {
10387 /* Found the match: return matchcount or line number. */
10388 if (flags & SP_RETCOUNT)
10389 ++retval;
10390 else
10391 retval = pos.lnum;
10392 if (flags & SP_SETPCMARK)
10393 setpcmark();
10394 curwin->w_cursor = pos;
10395 if (!(flags & SP_REPEAT))
10396 break;
10397 nest = 1; /* search for next unmatched */
10398 }
10399 }
10400
10401 if (match_pos != NULL)
10402 {
10403 /* Store the match cursor position */
10404 match_pos->lnum = curwin->w_cursor.lnum;
10405 match_pos->col = curwin->w_cursor.col + 1;
10406 }
10407
10408 /* If 'n' flag is used or search failed: restore cursor position. */
10409 if ((flags & SP_NOMOVE) || retval == 0)
10410 curwin->w_cursor = save_cursor;
10411
10412theend:
10413 vim_free(pat2);
10414 vim_free(pat3);
10415 if (p_cpo == empty_option)
10416 p_cpo = save_cpo;
10417 else
10418 /* Darn, evaluating the {skip} expression changed the value. */
10419 free_string_option(save_cpo);
10420
10421 return retval;
10422}
10423
10424/*
10425 * "searchpos()" function
10426 */
10427 static void
10428f_searchpos(typval_T *argvars, typval_T *rettv)
10429{
10430 pos_T match_pos;
10431 int lnum = 0;
10432 int col = 0;
10433 int n;
10434 int flags = 0;
10435
10436 if (rettv_list_alloc(rettv) == FAIL)
10437 return;
10438
10439 n = search_cmn(argvars, &match_pos, &flags);
10440 if (n > 0)
10441 {
10442 lnum = match_pos.lnum;
10443 col = match_pos.col;
10444 }
10445
10446 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10447 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10448 if (flags & SP_SUBPAT)
10449 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10450}
10451
10452 static void
10453f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10454{
10455#ifdef FEAT_CLIENTSERVER
10456 char_u buf[NUMBUFLEN];
10457 char_u *server = get_tv_string_chk(&argvars[0]);
10458 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10459
10460 rettv->vval.v_number = -1;
10461 if (server == NULL || reply == NULL)
10462 return;
10463 if (check_restricted() || check_secure())
10464 return;
10465# ifdef FEAT_X11
10466 if (check_connection() == FAIL)
10467 return;
10468# endif
10469
10470 if (serverSendReply(server, reply) < 0)
10471 {
10472 EMSG(_("E258: Unable to send to client"));
10473 return;
10474 }
10475 rettv->vval.v_number = 0;
10476#else
10477 rettv->vval.v_number = -1;
10478#endif
10479}
10480
10481 static void
10482f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10483{
10484 char_u *r = NULL;
10485
10486#ifdef FEAT_CLIENTSERVER
10487# ifdef WIN32
10488 r = serverGetVimNames();
10489# else
10490 make_connection();
10491 if (X_DISPLAY != NULL)
10492 r = serverGetVimNames(X_DISPLAY);
10493# endif
10494#endif
10495 rettv->v_type = VAR_STRING;
10496 rettv->vval.v_string = r;
10497}
10498
10499/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010500 * "setbufline()" function
10501 */
10502 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010503f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010504{
10505 linenr_T lnum;
10506 buf_T *buf;
10507
10508 buf = get_buf_tv(&argvars[0], FALSE);
10509 if (buf == NULL)
10510 rettv->vval.v_number = 1; /* FAIL */
10511 else
10512 {
10513 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010514 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010515 }
10516}
10517
10518/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010519 * "setbufvar()" function
10520 */
10521 static void
10522f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10523{
10524 buf_T *buf;
10525 char_u *varname, *bufvarname;
10526 typval_T *varp;
10527 char_u nbuf[NUMBUFLEN];
10528
10529 if (check_restricted() || check_secure())
10530 return;
10531 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10532 varname = get_tv_string_chk(&argvars[1]);
10533 buf = get_buf_tv(&argvars[0], FALSE);
10534 varp = &argvars[2];
10535
10536 if (buf != NULL && varname != NULL && varp != NULL)
10537 {
10538 if (*varname == '&')
10539 {
10540 long numval;
10541 char_u *strval;
10542 int error = FALSE;
10543 aco_save_T aco;
10544
10545 /* set curbuf to be our buf, temporarily */
10546 aucmd_prepbuf(&aco, buf);
10547
10548 ++varname;
10549 numval = (long)get_tv_number_chk(varp, &error);
10550 strval = get_tv_string_buf_chk(varp, nbuf);
10551 if (!error && strval != NULL)
10552 set_option_value(varname, numval, strval, OPT_LOCAL);
10553
10554 /* reset notion of buffer */
10555 aucmd_restbuf(&aco);
10556 }
10557 else
10558 {
10559 buf_T *save_curbuf = curbuf;
10560
10561 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10562 if (bufvarname != NULL)
10563 {
10564 curbuf = buf;
10565 STRCPY(bufvarname, "b:");
10566 STRCPY(bufvarname + 2, varname);
10567 set_var(bufvarname, varp, TRUE);
10568 vim_free(bufvarname);
10569 curbuf = save_curbuf;
10570 }
10571 }
10572 }
10573}
10574
10575 static void
10576f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10577{
10578 dict_T *d;
10579 dictitem_T *di;
10580 char_u *csearch;
10581
10582 if (argvars[0].v_type != VAR_DICT)
10583 {
10584 EMSG(_(e_dictreq));
10585 return;
10586 }
10587
10588 if ((d = argvars[0].vval.v_dict) != NULL)
10589 {
10590 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10591 if (csearch != NULL)
10592 {
10593#ifdef FEAT_MBYTE
10594 if (enc_utf8)
10595 {
10596 int pcc[MAX_MCO];
10597 int c = utfc_ptr2char(csearch, pcc);
10598
10599 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10600 }
10601 else
10602#endif
10603 set_last_csearch(PTR2CHAR(csearch),
10604 csearch, MB_PTR2LEN(csearch));
10605 }
10606
10607 di = dict_find(d, (char_u *)"forward", -1);
10608 if (di != NULL)
10609 set_csearch_direction((int)get_tv_number(&di->di_tv)
10610 ? FORWARD : BACKWARD);
10611
10612 di = dict_find(d, (char_u *)"until", -1);
10613 if (di != NULL)
10614 set_csearch_until(!!get_tv_number(&di->di_tv));
10615 }
10616}
10617
10618/*
10619 * "setcmdpos()" function
10620 */
10621 static void
10622f_setcmdpos(typval_T *argvars, typval_T *rettv)
10623{
10624 int pos = (int)get_tv_number(&argvars[0]) - 1;
10625
10626 if (pos >= 0)
10627 rettv->vval.v_number = set_cmdline_pos(pos);
10628}
10629
10630/*
10631 * "setfperm({fname}, {mode})" function
10632 */
10633 static void
10634f_setfperm(typval_T *argvars, typval_T *rettv)
10635{
10636 char_u *fname;
10637 char_u modebuf[NUMBUFLEN];
10638 char_u *mode_str;
10639 int i;
10640 int mask;
10641 int mode = 0;
10642
10643 rettv->vval.v_number = 0;
10644 fname = get_tv_string_chk(&argvars[0]);
10645 if (fname == NULL)
10646 return;
10647 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10648 if (mode_str == NULL)
10649 return;
10650 if (STRLEN(mode_str) != 9)
10651 {
10652 EMSG2(_(e_invarg2), mode_str);
10653 return;
10654 }
10655
10656 mask = 1;
10657 for (i = 8; i >= 0; --i)
10658 {
10659 if (mode_str[i] != '-')
10660 mode |= mask;
10661 mask = mask << 1;
10662 }
10663 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10664}
10665
10666/*
10667 * "setline()" function
10668 */
10669 static void
10670f_setline(typval_T *argvars, typval_T *rettv)
10671{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010672 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673
Bram Moolenaarca851592018-06-06 21:04:07 +020010674 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010675}
10676
Bram Moolenaard823fa92016-08-12 16:29:27 +020010677static 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 +020010678
10679/*
10680 * Used by "setqflist()" and "setloclist()" functions
10681 */
10682 static void
10683set_qf_ll_list(
10684 win_T *wp UNUSED,
10685 typval_T *list_arg UNUSED,
10686 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010687 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010688 typval_T *rettv)
10689{
10690#ifdef FEAT_QUICKFIX
10691 static char *e_invact = N_("E927: Invalid action: '%s'");
10692 char_u *act;
10693 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010694 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695#endif
10696
10697 rettv->vval.v_number = -1;
10698
10699#ifdef FEAT_QUICKFIX
10700 if (list_arg->v_type != VAR_LIST)
10701 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010702 else if (recursive != 0)
10703 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010704 else
10705 {
10706 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010707 dict_T *d = NULL;
10708 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010709
10710 if (action_arg->v_type == VAR_STRING)
10711 {
10712 act = get_tv_string_chk(action_arg);
10713 if (act == NULL)
10714 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010715 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10716 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010717 action = *act;
10718 else
10719 EMSG2(_(e_invact), act);
10720 }
10721 else if (action_arg->v_type == VAR_UNKNOWN)
10722 action = ' ';
10723 else
10724 EMSG(_(e_stringreq));
10725
Bram Moolenaard823fa92016-08-12 16:29:27 +020010726 if (action_arg->v_type != VAR_UNKNOWN
10727 && what_arg->v_type != VAR_UNKNOWN)
10728 {
10729 if (what_arg->v_type == VAR_DICT)
10730 d = what_arg->vval.v_dict;
10731 else
10732 {
10733 EMSG(_(e_dictreq));
10734 valid_dict = FALSE;
10735 }
10736 }
10737
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010738 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010739 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010740 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10741 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010742 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010743 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010744 }
10745#endif
10746}
10747
10748/*
10749 * "setloclist()" function
10750 */
10751 static void
10752f_setloclist(typval_T *argvars, typval_T *rettv)
10753{
10754 win_T *win;
10755
10756 rettv->vval.v_number = -1;
10757
10758 win = find_win_by_nr(&argvars[0], NULL);
10759 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010760 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010761}
10762
10763/*
10764 * "setmatches()" function
10765 */
10766 static void
10767f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10768{
10769#ifdef FEAT_SEARCH_EXTRA
10770 list_T *l;
10771 listitem_T *li;
10772 dict_T *d;
10773 list_T *s = NULL;
10774
10775 rettv->vval.v_number = -1;
10776 if (argvars[0].v_type != VAR_LIST)
10777 {
10778 EMSG(_(e_listreq));
10779 return;
10780 }
10781 if ((l = argvars[0].vval.v_list) != NULL)
10782 {
10783
10784 /* To some extent make sure that we are dealing with a list from
10785 * "getmatches()". */
10786 li = l->lv_first;
10787 while (li != NULL)
10788 {
10789 if (li->li_tv.v_type != VAR_DICT
10790 || (d = li->li_tv.vval.v_dict) == NULL)
10791 {
10792 EMSG(_(e_invarg));
10793 return;
10794 }
10795 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10796 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10797 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10798 && dict_find(d, (char_u *)"priority", -1) != NULL
10799 && dict_find(d, (char_u *)"id", -1) != NULL))
10800 {
10801 EMSG(_(e_invarg));
10802 return;
10803 }
10804 li = li->li_next;
10805 }
10806
10807 clear_matches(curwin);
10808 li = l->lv_first;
10809 while (li != NULL)
10810 {
10811 int i = 0;
10812 char_u buf[5];
10813 dictitem_T *di;
10814 char_u *group;
10815 int priority;
10816 int id;
10817 char_u *conceal;
10818
10819 d = li->li_tv.vval.v_dict;
10820 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10821 {
10822 if (s == NULL)
10823 {
10824 s = list_alloc();
10825 if (s == NULL)
10826 return;
10827 }
10828
10829 /* match from matchaddpos() */
10830 for (i = 1; i < 9; i++)
10831 {
10832 sprintf((char *)buf, (char *)"pos%d", i);
10833 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10834 {
10835 if (di->di_tv.v_type != VAR_LIST)
10836 return;
10837
10838 list_append_tv(s, &di->di_tv);
10839 s->lv_refcount++;
10840 }
10841 else
10842 break;
10843 }
10844 }
10845
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010846 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010847 priority = (int)get_dict_number(d, (char_u *)"priority");
10848 id = (int)get_dict_number(d, (char_u *)"id");
10849 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010850 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 : NULL;
10852 if (i == 0)
10853 {
10854 match_add(curwin, group,
10855 get_dict_string(d, (char_u *)"pattern", FALSE),
10856 priority, id, NULL, conceal);
10857 }
10858 else
10859 {
10860 match_add(curwin, group, NULL, priority, id, s, conceal);
10861 list_unref(s);
10862 s = NULL;
10863 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010864 vim_free(group);
10865 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010866
10867 li = li->li_next;
10868 }
10869 rettv->vval.v_number = 0;
10870 }
10871#endif
10872}
10873
10874/*
10875 * "setpos()" function
10876 */
10877 static void
10878f_setpos(typval_T *argvars, typval_T *rettv)
10879{
10880 pos_T pos;
10881 int fnum;
10882 char_u *name;
10883 colnr_T curswant = -1;
10884
10885 rettv->vval.v_number = -1;
10886 name = get_tv_string_chk(argvars);
10887 if (name != NULL)
10888 {
10889 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10890 {
10891 if (--pos.col < 0)
10892 pos.col = 0;
10893 if (name[0] == '.' && name[1] == NUL)
10894 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010895 /* set cursor; "fnum" is ignored */
10896 curwin->w_cursor = pos;
10897 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010898 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010899 curwin->w_curswant = curswant - 1;
10900 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010902 check_cursor();
10903 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 }
10905 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10906 {
10907 /* set mark */
10908 if (setmark_pos(name[1], &pos, fnum) == OK)
10909 rettv->vval.v_number = 0;
10910 }
10911 else
10912 EMSG(_(e_invarg));
10913 }
10914 }
10915}
10916
10917/*
10918 * "setqflist()" function
10919 */
10920 static void
10921f_setqflist(typval_T *argvars, typval_T *rettv)
10922{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010923 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924}
10925
10926/*
10927 * "setreg()" function
10928 */
10929 static void
10930f_setreg(typval_T *argvars, typval_T *rettv)
10931{
10932 int regname;
10933 char_u *strregname;
10934 char_u *stropt;
10935 char_u *strval;
10936 int append;
10937 char_u yank_type;
10938 long block_len;
10939
10940 block_len = -1;
10941 yank_type = MAUTO;
10942 append = FALSE;
10943
10944 strregname = get_tv_string_chk(argvars);
10945 rettv->vval.v_number = 1; /* FAIL is default */
10946
10947 if (strregname == NULL)
10948 return; /* type error; errmsg already given */
10949 regname = *strregname;
10950 if (regname == 0 || regname == '@')
10951 regname = '"';
10952
10953 if (argvars[2].v_type != VAR_UNKNOWN)
10954 {
10955 stropt = get_tv_string_chk(&argvars[2]);
10956 if (stropt == NULL)
10957 return; /* type error */
10958 for (; *stropt != NUL; ++stropt)
10959 switch (*stropt)
10960 {
10961 case 'a': case 'A': /* append */
10962 append = TRUE;
10963 break;
10964 case 'v': case 'c': /* character-wise selection */
10965 yank_type = MCHAR;
10966 break;
10967 case 'V': case 'l': /* line-wise selection */
10968 yank_type = MLINE;
10969 break;
10970 case 'b': case Ctrl_V: /* block-wise selection */
10971 yank_type = MBLOCK;
10972 if (VIM_ISDIGIT(stropt[1]))
10973 {
10974 ++stropt;
10975 block_len = getdigits(&stropt) - 1;
10976 --stropt;
10977 }
10978 break;
10979 }
10980 }
10981
10982 if (argvars[1].v_type == VAR_LIST)
10983 {
10984 char_u **lstval;
10985 char_u **allocval;
10986 char_u buf[NUMBUFLEN];
10987 char_u **curval;
10988 char_u **curallocval;
10989 list_T *ll = argvars[1].vval.v_list;
10990 listitem_T *li;
10991 int len;
10992
10993 /* If the list is NULL handle like an empty list. */
10994 len = ll == NULL ? 0 : ll->lv_len;
10995
10996 /* First half: use for pointers to result lines; second half: use for
10997 * pointers to allocated copies. */
10998 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10999 if (lstval == NULL)
11000 return;
11001 curval = lstval;
11002 allocval = lstval + len + 2;
11003 curallocval = allocval;
11004
11005 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11006 li = li->li_next)
11007 {
11008 strval = get_tv_string_buf_chk(&li->li_tv, buf);
11009 if (strval == NULL)
11010 goto free_lstval;
11011 if (strval == buf)
11012 {
11013 /* Need to make a copy, next get_tv_string_buf_chk() will
11014 * overwrite the string. */
11015 strval = vim_strsave(buf);
11016 if (strval == NULL)
11017 goto free_lstval;
11018 *curallocval++ = strval;
11019 }
11020 *curval++ = strval;
11021 }
11022 *curval++ = NULL;
11023
11024 write_reg_contents_lst(regname, lstval, -1,
11025 append, yank_type, block_len);
11026free_lstval:
11027 while (curallocval > allocval)
11028 vim_free(*--curallocval);
11029 vim_free(lstval);
11030 }
11031 else
11032 {
11033 strval = get_tv_string_chk(&argvars[1]);
11034 if (strval == NULL)
11035 return;
11036 write_reg_contents_ex(regname, strval, -1,
11037 append, yank_type, block_len);
11038 }
11039 rettv->vval.v_number = 0;
11040}
11041
11042/*
11043 * "settabvar()" function
11044 */
11045 static void
11046f_settabvar(typval_T *argvars, typval_T *rettv)
11047{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 tabpage_T *save_curtab;
11049 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 char_u *varname, *tabvarname;
11051 typval_T *varp;
11052
11053 rettv->vval.v_number = 0;
11054
11055 if (check_restricted() || check_secure())
11056 return;
11057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 varname = get_tv_string_chk(&argvars[1]);
11060 varp = &argvars[2];
11061
Bram Moolenaar4033c552017-09-16 20:54:51 +020011062 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011064 save_curtab = curtab;
11065 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011066
11067 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11068 if (tabvarname != NULL)
11069 {
11070 STRCPY(tabvarname, "t:");
11071 STRCPY(tabvarname + 2, varname);
11072 set_var(tabvarname, varp, TRUE);
11073 vim_free(tabvarname);
11074 }
11075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011076 /* Restore current tabpage */
11077 if (valid_tabpage(save_curtab))
11078 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 }
11080}
11081
11082/*
11083 * "settabwinvar()" function
11084 */
11085 static void
11086f_settabwinvar(typval_T *argvars, typval_T *rettv)
11087{
11088 setwinvar(argvars, rettv, 1);
11089}
11090
11091/*
11092 * "setwinvar()" function
11093 */
11094 static void
11095f_setwinvar(typval_T *argvars, typval_T *rettv)
11096{
11097 setwinvar(argvars, rettv, 0);
11098}
11099
11100#ifdef FEAT_CRYPT
11101/*
11102 * "sha256({string})" function
11103 */
11104 static void
11105f_sha256(typval_T *argvars, typval_T *rettv)
11106{
11107 char_u *p;
11108
11109 p = get_tv_string(&argvars[0]);
11110 rettv->vval.v_string = vim_strsave(
11111 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11112 rettv->v_type = VAR_STRING;
11113}
11114#endif /* FEAT_CRYPT */
11115
11116/*
11117 * "shellescape({string})" function
11118 */
11119 static void
11120f_shellescape(typval_T *argvars, typval_T *rettv)
11121{
Bram Moolenaar20615522017-06-05 18:46:26 +020011122 int do_special = non_zero_arg(&argvars[1]);
11123
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011124 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011125 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011126 rettv->v_type = VAR_STRING;
11127}
11128
11129/*
11130 * shiftwidth() function
11131 */
11132 static void
11133f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11134{
11135 rettv->vval.v_number = get_sw_value(curbuf);
11136}
11137
11138/*
11139 * "simplify()" function
11140 */
11141 static void
11142f_simplify(typval_T *argvars, typval_T *rettv)
11143{
11144 char_u *p;
11145
11146 p = get_tv_string(&argvars[0]);
11147 rettv->vval.v_string = vim_strsave(p);
11148 simplify_filename(rettv->vval.v_string); /* simplify in place */
11149 rettv->v_type = VAR_STRING;
11150}
11151
11152#ifdef FEAT_FLOAT
11153/*
11154 * "sin()" function
11155 */
11156 static void
11157f_sin(typval_T *argvars, typval_T *rettv)
11158{
11159 float_T f = 0.0;
11160
11161 rettv->v_type = VAR_FLOAT;
11162 if (get_float_arg(argvars, &f) == OK)
11163 rettv->vval.v_float = sin(f);
11164 else
11165 rettv->vval.v_float = 0.0;
11166}
11167
11168/*
11169 * "sinh()" function
11170 */
11171 static void
11172f_sinh(typval_T *argvars, typval_T *rettv)
11173{
11174 float_T f = 0.0;
11175
11176 rettv->v_type = VAR_FLOAT;
11177 if (get_float_arg(argvars, &f) == OK)
11178 rettv->vval.v_float = sinh(f);
11179 else
11180 rettv->vval.v_float = 0.0;
11181}
11182#endif
11183
11184static int
11185#ifdef __BORLANDC__
11186 _RTLENTRYF
11187#endif
11188 item_compare(const void *s1, const void *s2);
11189static int
11190#ifdef __BORLANDC__
11191 _RTLENTRYF
11192#endif
11193 item_compare2(const void *s1, const void *s2);
11194
11195/* struct used in the array that's given to qsort() */
11196typedef struct
11197{
11198 listitem_T *item;
11199 int idx;
11200} sortItem_T;
11201
11202/* struct storing information about current sort */
11203typedef struct
11204{
11205 int item_compare_ic;
11206 int item_compare_numeric;
11207 int item_compare_numbers;
11208#ifdef FEAT_FLOAT
11209 int item_compare_float;
11210#endif
11211 char_u *item_compare_func;
11212 partial_T *item_compare_partial;
11213 dict_T *item_compare_selfdict;
11214 int item_compare_func_err;
11215 int item_compare_keep_zero;
11216} sortinfo_T;
11217static sortinfo_T *sortinfo = NULL;
11218static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
11219#define ITEM_COMPARE_FAIL 999
11220
11221/*
11222 * Compare functions for f_sort() and f_uniq() below.
11223 */
11224 static int
11225#ifdef __BORLANDC__
11226_RTLENTRYF
11227#endif
11228item_compare(const void *s1, const void *s2)
11229{
11230 sortItem_T *si1, *si2;
11231 typval_T *tv1, *tv2;
11232 char_u *p1, *p2;
11233 char_u *tofree1 = NULL, *tofree2 = NULL;
11234 int res;
11235 char_u numbuf1[NUMBUFLEN];
11236 char_u numbuf2[NUMBUFLEN];
11237
11238 si1 = (sortItem_T *)s1;
11239 si2 = (sortItem_T *)s2;
11240 tv1 = &si1->item->li_tv;
11241 tv2 = &si2->item->li_tv;
11242
11243 if (sortinfo->item_compare_numbers)
11244 {
11245 varnumber_T v1 = get_tv_number(tv1);
11246 varnumber_T v2 = get_tv_number(tv2);
11247
11248 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11249 }
11250
11251#ifdef FEAT_FLOAT
11252 if (sortinfo->item_compare_float)
11253 {
11254 float_T v1 = get_tv_float(tv1);
11255 float_T v2 = get_tv_float(tv2);
11256
11257 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11258 }
11259#endif
11260
11261 /* tv2string() puts quotes around a string and allocates memory. Don't do
11262 * that for string variables. Use a single quote when comparing with a
11263 * non-string to do what the docs promise. */
11264 if (tv1->v_type == VAR_STRING)
11265 {
11266 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11267 p1 = (char_u *)"'";
11268 else
11269 p1 = tv1->vval.v_string;
11270 }
11271 else
11272 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11273 if (tv2->v_type == VAR_STRING)
11274 {
11275 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11276 p2 = (char_u *)"'";
11277 else
11278 p2 = tv2->vval.v_string;
11279 }
11280 else
11281 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11282 if (p1 == NULL)
11283 p1 = (char_u *)"";
11284 if (p2 == NULL)
11285 p2 = (char_u *)"";
11286 if (!sortinfo->item_compare_numeric)
11287 {
11288 if (sortinfo->item_compare_ic)
11289 res = STRICMP(p1, p2);
11290 else
11291 res = STRCMP(p1, p2);
11292 }
11293 else
11294 {
11295 double n1, n2;
11296 n1 = strtod((char *)p1, (char **)&p1);
11297 n2 = strtod((char *)p2, (char **)&p2);
11298 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11299 }
11300
11301 /* When the result would be zero, compare the item indexes. Makes the
11302 * sort stable. */
11303 if (res == 0 && !sortinfo->item_compare_keep_zero)
11304 res = si1->idx > si2->idx ? 1 : -1;
11305
11306 vim_free(tofree1);
11307 vim_free(tofree2);
11308 return res;
11309}
11310
11311 static int
11312#ifdef __BORLANDC__
11313_RTLENTRYF
11314#endif
11315item_compare2(const void *s1, const void *s2)
11316{
11317 sortItem_T *si1, *si2;
11318 int res;
11319 typval_T rettv;
11320 typval_T argv[3];
11321 int dummy;
11322 char_u *func_name;
11323 partial_T *partial = sortinfo->item_compare_partial;
11324
11325 /* shortcut after failure in previous call; compare all items equal */
11326 if (sortinfo->item_compare_func_err)
11327 return 0;
11328
11329 si1 = (sortItem_T *)s1;
11330 si2 = (sortItem_T *)s2;
11331
11332 if (partial == NULL)
11333 func_name = sortinfo->item_compare_func;
11334 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011335 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011336
11337 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11338 * in the copy without changing the original list items. */
11339 copy_tv(&si1->item->li_tv, &argv[0]);
11340 copy_tv(&si2->item->li_tv, &argv[1]);
11341
11342 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11343 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011344 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011345 partial, sortinfo->item_compare_selfdict);
11346 clear_tv(&argv[0]);
11347 clear_tv(&argv[1]);
11348
11349 if (res == FAIL)
11350 res = ITEM_COMPARE_FAIL;
11351 else
11352 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11353 if (sortinfo->item_compare_func_err)
11354 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11355 clear_tv(&rettv);
11356
11357 /* When the result would be zero, compare the pointers themselves. Makes
11358 * the sort stable. */
11359 if (res == 0 && !sortinfo->item_compare_keep_zero)
11360 res = si1->idx > si2->idx ? 1 : -1;
11361
11362 return res;
11363}
11364
11365/*
11366 * "sort({list})" function
11367 */
11368 static void
11369do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11370{
11371 list_T *l;
11372 listitem_T *li;
11373 sortItem_T *ptrs;
11374 sortinfo_T *old_sortinfo;
11375 sortinfo_T info;
11376 long len;
11377 long i;
11378
11379 /* Pointer to current info struct used in compare function. Save and
11380 * restore the current one for nested calls. */
11381 old_sortinfo = sortinfo;
11382 sortinfo = &info;
11383
11384 if (argvars[0].v_type != VAR_LIST)
11385 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11386 else
11387 {
11388 l = argvars[0].vval.v_list;
11389 if (l == NULL || tv_check_lock(l->lv_lock,
11390 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11391 TRUE))
11392 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011393 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011394
11395 len = list_len(l);
11396 if (len <= 1)
11397 goto theend; /* short list sorts pretty quickly */
11398
11399 info.item_compare_ic = FALSE;
11400 info.item_compare_numeric = FALSE;
11401 info.item_compare_numbers = FALSE;
11402#ifdef FEAT_FLOAT
11403 info.item_compare_float = FALSE;
11404#endif
11405 info.item_compare_func = NULL;
11406 info.item_compare_partial = NULL;
11407 info.item_compare_selfdict = NULL;
11408 if (argvars[1].v_type != VAR_UNKNOWN)
11409 {
11410 /* optional second argument: {func} */
11411 if (argvars[1].v_type == VAR_FUNC)
11412 info.item_compare_func = argvars[1].vval.v_string;
11413 else if (argvars[1].v_type == VAR_PARTIAL)
11414 info.item_compare_partial = argvars[1].vval.v_partial;
11415 else
11416 {
11417 int error = FALSE;
11418
11419 i = (long)get_tv_number_chk(&argvars[1], &error);
11420 if (error)
11421 goto theend; /* type error; errmsg already given */
11422 if (i == 1)
11423 info.item_compare_ic = TRUE;
11424 else if (argvars[1].v_type != VAR_NUMBER)
11425 info.item_compare_func = get_tv_string(&argvars[1]);
11426 else if (i != 0)
11427 {
11428 EMSG(_(e_invarg));
11429 goto theend;
11430 }
11431 if (info.item_compare_func != NULL)
11432 {
11433 if (*info.item_compare_func == NUL)
11434 {
11435 /* empty string means default sort */
11436 info.item_compare_func = NULL;
11437 }
11438 else if (STRCMP(info.item_compare_func, "n") == 0)
11439 {
11440 info.item_compare_func = NULL;
11441 info.item_compare_numeric = TRUE;
11442 }
11443 else if (STRCMP(info.item_compare_func, "N") == 0)
11444 {
11445 info.item_compare_func = NULL;
11446 info.item_compare_numbers = TRUE;
11447 }
11448#ifdef FEAT_FLOAT
11449 else if (STRCMP(info.item_compare_func, "f") == 0)
11450 {
11451 info.item_compare_func = NULL;
11452 info.item_compare_float = TRUE;
11453 }
11454#endif
11455 else if (STRCMP(info.item_compare_func, "i") == 0)
11456 {
11457 info.item_compare_func = NULL;
11458 info.item_compare_ic = TRUE;
11459 }
11460 }
11461 }
11462
11463 if (argvars[2].v_type != VAR_UNKNOWN)
11464 {
11465 /* optional third argument: {dict} */
11466 if (argvars[2].v_type != VAR_DICT)
11467 {
11468 EMSG(_(e_dictreq));
11469 goto theend;
11470 }
11471 info.item_compare_selfdict = argvars[2].vval.v_dict;
11472 }
11473 }
11474
11475 /* Make an array with each entry pointing to an item in the List. */
11476 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11477 if (ptrs == NULL)
11478 goto theend;
11479
11480 i = 0;
11481 if (sort)
11482 {
11483 /* sort(): ptrs will be the list to sort */
11484 for (li = l->lv_first; li != NULL; li = li->li_next)
11485 {
11486 ptrs[i].item = li;
11487 ptrs[i].idx = i;
11488 ++i;
11489 }
11490
11491 info.item_compare_func_err = FALSE;
11492 info.item_compare_keep_zero = FALSE;
11493 /* test the compare function */
11494 if ((info.item_compare_func != NULL
11495 || info.item_compare_partial != NULL)
11496 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11497 == ITEM_COMPARE_FAIL)
11498 EMSG(_("E702: Sort compare function failed"));
11499 else
11500 {
11501 /* Sort the array with item pointers. */
11502 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11503 info.item_compare_func == NULL
11504 && info.item_compare_partial == NULL
11505 ? item_compare : item_compare2);
11506
11507 if (!info.item_compare_func_err)
11508 {
11509 /* Clear the List and append the items in sorted order. */
11510 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11511 l->lv_len = 0;
11512 for (i = 0; i < len; ++i)
11513 list_append(l, ptrs[i].item);
11514 }
11515 }
11516 }
11517 else
11518 {
11519 int (*item_compare_func_ptr)(const void *, const void *);
11520
11521 /* f_uniq(): ptrs will be a stack of items to remove */
11522 info.item_compare_func_err = FALSE;
11523 info.item_compare_keep_zero = TRUE;
11524 item_compare_func_ptr = info.item_compare_func != NULL
11525 || info.item_compare_partial != NULL
11526 ? item_compare2 : item_compare;
11527
11528 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11529 li = li->li_next)
11530 {
11531 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11532 == 0)
11533 ptrs[i++].item = li;
11534 if (info.item_compare_func_err)
11535 {
11536 EMSG(_("E882: Uniq compare function failed"));
11537 break;
11538 }
11539 }
11540
11541 if (!info.item_compare_func_err)
11542 {
11543 while (--i >= 0)
11544 {
11545 li = ptrs[i].item->li_next;
11546 ptrs[i].item->li_next = li->li_next;
11547 if (li->li_next != NULL)
11548 li->li_next->li_prev = ptrs[i].item;
11549 else
11550 l->lv_last = ptrs[i].item;
11551 list_fix_watch(l, li);
11552 listitem_free(li);
11553 l->lv_len--;
11554 }
11555 }
11556 }
11557
11558 vim_free(ptrs);
11559 }
11560theend:
11561 sortinfo = old_sortinfo;
11562}
11563
11564/*
11565 * "sort({list})" function
11566 */
11567 static void
11568f_sort(typval_T *argvars, typval_T *rettv)
11569{
11570 do_sort_uniq(argvars, rettv, TRUE);
11571}
11572
11573/*
11574 * "uniq({list})" function
11575 */
11576 static void
11577f_uniq(typval_T *argvars, typval_T *rettv)
11578{
11579 do_sort_uniq(argvars, rettv, FALSE);
11580}
11581
11582/*
11583 * "soundfold({word})" function
11584 */
11585 static void
11586f_soundfold(typval_T *argvars, typval_T *rettv)
11587{
11588 char_u *s;
11589
11590 rettv->v_type = VAR_STRING;
11591 s = get_tv_string(&argvars[0]);
11592#ifdef FEAT_SPELL
11593 rettv->vval.v_string = eval_soundfold(s);
11594#else
11595 rettv->vval.v_string = vim_strsave(s);
11596#endif
11597}
11598
11599/*
11600 * "spellbadword()" function
11601 */
11602 static void
11603f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11604{
11605 char_u *word = (char_u *)"";
11606 hlf_T attr = HLF_COUNT;
11607 int len = 0;
11608
11609 if (rettv_list_alloc(rettv) == FAIL)
11610 return;
11611
11612#ifdef FEAT_SPELL
11613 if (argvars[0].v_type == VAR_UNKNOWN)
11614 {
11615 /* Find the start and length of the badly spelled word. */
11616 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11617 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011618 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011620 curwin->w_set_curswant = TRUE;
11621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622 }
11623 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11624 {
11625 char_u *str = get_tv_string_chk(&argvars[0]);
11626 int capcol = -1;
11627
11628 if (str != NULL)
11629 {
11630 /* Check the argument for spelling. */
11631 while (*str != NUL)
11632 {
11633 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11634 if (attr != HLF_COUNT)
11635 {
11636 word = str;
11637 break;
11638 }
11639 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011640 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 }
11642 }
11643 }
11644#endif
11645
11646 list_append_string(rettv->vval.v_list, word, len);
11647 list_append_string(rettv->vval.v_list, (char_u *)(
11648 attr == HLF_SPB ? "bad" :
11649 attr == HLF_SPR ? "rare" :
11650 attr == HLF_SPL ? "local" :
11651 attr == HLF_SPC ? "caps" :
11652 ""), -1);
11653}
11654
11655/*
11656 * "spellsuggest()" function
11657 */
11658 static void
11659f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11660{
11661#ifdef FEAT_SPELL
11662 char_u *str;
11663 int typeerr = FALSE;
11664 int maxcount;
11665 garray_T ga;
11666 int i;
11667 listitem_T *li;
11668 int need_capital = FALSE;
11669#endif
11670
11671 if (rettv_list_alloc(rettv) == FAIL)
11672 return;
11673
11674#ifdef FEAT_SPELL
11675 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11676 {
11677 str = get_tv_string(&argvars[0]);
11678 if (argvars[1].v_type != VAR_UNKNOWN)
11679 {
11680 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11681 if (maxcount <= 0)
11682 return;
11683 if (argvars[2].v_type != VAR_UNKNOWN)
11684 {
11685 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11686 if (typeerr)
11687 return;
11688 }
11689 }
11690 else
11691 maxcount = 25;
11692
11693 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11694
11695 for (i = 0; i < ga.ga_len; ++i)
11696 {
11697 str = ((char_u **)ga.ga_data)[i];
11698
11699 li = listitem_alloc();
11700 if (li == NULL)
11701 vim_free(str);
11702 else
11703 {
11704 li->li_tv.v_type = VAR_STRING;
11705 li->li_tv.v_lock = 0;
11706 li->li_tv.vval.v_string = str;
11707 list_append(rettv->vval.v_list, li);
11708 }
11709 }
11710 ga_clear(&ga);
11711 }
11712#endif
11713}
11714
11715 static void
11716f_split(typval_T *argvars, typval_T *rettv)
11717{
11718 char_u *str;
11719 char_u *end;
11720 char_u *pat = NULL;
11721 regmatch_T regmatch;
11722 char_u patbuf[NUMBUFLEN];
11723 char_u *save_cpo;
11724 int match;
11725 colnr_T col = 0;
11726 int keepempty = FALSE;
11727 int typeerr = FALSE;
11728
11729 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11730 save_cpo = p_cpo;
11731 p_cpo = (char_u *)"";
11732
11733 str = get_tv_string(&argvars[0]);
11734 if (argvars[1].v_type != VAR_UNKNOWN)
11735 {
11736 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11737 if (pat == NULL)
11738 typeerr = TRUE;
11739 if (argvars[2].v_type != VAR_UNKNOWN)
11740 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11741 }
11742 if (pat == NULL || *pat == NUL)
11743 pat = (char_u *)"[\\x01- ]\\+";
11744
11745 if (rettv_list_alloc(rettv) == FAIL)
11746 return;
11747 if (typeerr)
11748 return;
11749
11750 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11751 if (regmatch.regprog != NULL)
11752 {
11753 regmatch.rm_ic = FALSE;
11754 while (*str != NUL || keepempty)
11755 {
11756 if (*str == NUL)
11757 match = FALSE; /* empty item at the end */
11758 else
11759 match = vim_regexec_nl(&regmatch, str, col);
11760 if (match)
11761 end = regmatch.startp[0];
11762 else
11763 end = str + STRLEN(str);
11764 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11765 && *str != NUL && match && end < regmatch.endp[0]))
11766 {
11767 if (list_append_string(rettv->vval.v_list, str,
11768 (int)(end - str)) == FAIL)
11769 break;
11770 }
11771 if (!match)
11772 break;
11773 /* Advance to just after the match. */
11774 if (regmatch.endp[0] > str)
11775 col = 0;
11776 else
11777 {
11778 /* Don't get stuck at the same match. */
11779#ifdef FEAT_MBYTE
11780 col = (*mb_ptr2len)(regmatch.endp[0]);
11781#else
11782 col = 1;
11783#endif
11784 }
11785 str = regmatch.endp[0];
11786 }
11787
11788 vim_regfree(regmatch.regprog);
11789 }
11790
11791 p_cpo = save_cpo;
11792}
11793
11794#ifdef FEAT_FLOAT
11795/*
11796 * "sqrt()" function
11797 */
11798 static void
11799f_sqrt(typval_T *argvars, typval_T *rettv)
11800{
11801 float_T f = 0.0;
11802
11803 rettv->v_type = VAR_FLOAT;
11804 if (get_float_arg(argvars, &f) == OK)
11805 rettv->vval.v_float = sqrt(f);
11806 else
11807 rettv->vval.v_float = 0.0;
11808}
11809
11810/*
11811 * "str2float()" function
11812 */
11813 static void
11814f_str2float(typval_T *argvars, typval_T *rettv)
11815{
11816 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011817 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011818
Bram Moolenaar08243d22017-01-10 16:12:29 +010011819 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011820 p = skipwhite(p + 1);
11821 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011822 if (isneg)
11823 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011824 rettv->v_type = VAR_FLOAT;
11825}
11826#endif
11827
11828/*
11829 * "str2nr()" function
11830 */
11831 static void
11832f_str2nr(typval_T *argvars, typval_T *rettv)
11833{
11834 int base = 10;
11835 char_u *p;
11836 varnumber_T n;
11837 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011838 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839
11840 if (argvars[1].v_type != VAR_UNKNOWN)
11841 {
11842 base = (int)get_tv_number(&argvars[1]);
11843 if (base != 2 && base != 8 && base != 10 && base != 16)
11844 {
11845 EMSG(_(e_invarg));
11846 return;
11847 }
11848 }
11849
11850 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011851 isneg = (*p == '-');
11852 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 p = skipwhite(p + 1);
11854 switch (base)
11855 {
11856 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11857 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11858 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11859 default: what = 0;
11860 }
11861 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011862 if (isneg)
11863 rettv->vval.v_number = -n;
11864 else
11865 rettv->vval.v_number = n;
11866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011867}
11868
11869#ifdef HAVE_STRFTIME
11870/*
11871 * "strftime({format}[, {time}])" function
11872 */
11873 static void
11874f_strftime(typval_T *argvars, typval_T *rettv)
11875{
11876 char_u result_buf[256];
11877 struct tm *curtime;
11878 time_t seconds;
11879 char_u *p;
11880
11881 rettv->v_type = VAR_STRING;
11882
11883 p = get_tv_string(&argvars[0]);
11884 if (argvars[1].v_type == VAR_UNKNOWN)
11885 seconds = time(NULL);
11886 else
11887 seconds = (time_t)get_tv_number(&argvars[1]);
11888 curtime = localtime(&seconds);
11889 /* MSVC returns NULL for an invalid value of seconds. */
11890 if (curtime == NULL)
11891 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11892 else
11893 {
11894# ifdef FEAT_MBYTE
11895 vimconv_T conv;
11896 char_u *enc;
11897
11898 conv.vc_type = CONV_NONE;
11899 enc = enc_locale();
11900 convert_setup(&conv, p_enc, enc);
11901 if (conv.vc_type != CONV_NONE)
11902 p = string_convert(&conv, p, NULL);
11903# endif
11904 if (p != NULL)
11905 (void)strftime((char *)result_buf, sizeof(result_buf),
11906 (char *)p, curtime);
11907 else
11908 result_buf[0] = NUL;
11909
11910# ifdef FEAT_MBYTE
11911 if (conv.vc_type != CONV_NONE)
11912 vim_free(p);
11913 convert_setup(&conv, enc, p_enc);
11914 if (conv.vc_type != CONV_NONE)
11915 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11916 else
11917# endif
11918 rettv->vval.v_string = vim_strsave(result_buf);
11919
11920# ifdef FEAT_MBYTE
11921 /* Release conversion descriptors */
11922 convert_setup(&conv, NULL, NULL);
11923 vim_free(enc);
11924# endif
11925 }
11926}
11927#endif
11928
11929/*
11930 * "strgetchar()" function
11931 */
11932 static void
11933f_strgetchar(typval_T *argvars, typval_T *rettv)
11934{
11935 char_u *str;
11936 int len;
11937 int error = FALSE;
11938 int charidx;
11939
11940 rettv->vval.v_number = -1;
11941 str = get_tv_string_chk(&argvars[0]);
11942 if (str == NULL)
11943 return;
11944 len = (int)STRLEN(str);
11945 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11946 if (error)
11947 return;
11948#ifdef FEAT_MBYTE
11949 {
11950 int byteidx = 0;
11951
11952 while (charidx >= 0 && byteidx < len)
11953 {
11954 if (charidx == 0)
11955 {
11956 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11957 break;
11958 }
11959 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011960 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961 }
11962 }
11963#else
11964 if (charidx < len)
11965 rettv->vval.v_number = str[charidx];
11966#endif
11967}
11968
11969/*
11970 * "stridx()" function
11971 */
11972 static void
11973f_stridx(typval_T *argvars, typval_T *rettv)
11974{
11975 char_u buf[NUMBUFLEN];
11976 char_u *needle;
11977 char_u *haystack;
11978 char_u *save_haystack;
11979 char_u *pos;
11980 int start_idx;
11981
11982 needle = get_tv_string_chk(&argvars[1]);
11983 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11984 rettv->vval.v_number = -1;
11985 if (needle == NULL || haystack == NULL)
11986 return; /* type error; errmsg already given */
11987
11988 if (argvars[2].v_type != VAR_UNKNOWN)
11989 {
11990 int error = FALSE;
11991
11992 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11993 if (error || start_idx >= (int)STRLEN(haystack))
11994 return;
11995 if (start_idx >= 0)
11996 haystack += start_idx;
11997 }
11998
11999 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12000 if (pos != NULL)
12001 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12002}
12003
12004/*
12005 * "string()" function
12006 */
12007 static void
12008f_string(typval_T *argvars, typval_T *rettv)
12009{
12010 char_u *tofree;
12011 char_u numbuf[NUMBUFLEN];
12012
12013 rettv->v_type = VAR_STRING;
12014 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12015 get_copyID());
12016 /* Make a copy if we have a value but it's not in allocated memory. */
12017 if (rettv->vval.v_string != NULL && tofree == NULL)
12018 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12019}
12020
12021/*
12022 * "strlen()" function
12023 */
12024 static void
12025f_strlen(typval_T *argvars, typval_T *rettv)
12026{
12027 rettv->vval.v_number = (varnumber_T)(STRLEN(
12028 get_tv_string(&argvars[0])));
12029}
12030
12031/*
12032 * "strchars()" function
12033 */
12034 static void
12035f_strchars(typval_T *argvars, typval_T *rettv)
12036{
12037 char_u *s = get_tv_string(&argvars[0]);
12038 int skipcc = 0;
12039#ifdef FEAT_MBYTE
12040 varnumber_T len = 0;
12041 int (*func_mb_ptr2char_adv)(char_u **pp);
12042#endif
12043
12044 if (argvars[1].v_type != VAR_UNKNOWN)
12045 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12046 if (skipcc < 0 || skipcc > 1)
12047 EMSG(_(e_invarg));
12048 else
12049 {
12050#ifdef FEAT_MBYTE
12051 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12052 while (*s != NUL)
12053 {
12054 func_mb_ptr2char_adv(&s);
12055 ++len;
12056 }
12057 rettv->vval.v_number = len;
12058#else
12059 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12060#endif
12061 }
12062}
12063
12064/*
12065 * "strdisplaywidth()" function
12066 */
12067 static void
12068f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12069{
12070 char_u *s = get_tv_string(&argvars[0]);
12071 int col = 0;
12072
12073 if (argvars[1].v_type != VAR_UNKNOWN)
12074 col = (int)get_tv_number(&argvars[1]);
12075
12076 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12077}
12078
12079/*
12080 * "strwidth()" function
12081 */
12082 static void
12083f_strwidth(typval_T *argvars, typval_T *rettv)
12084{
12085 char_u *s = get_tv_string(&argvars[0]);
12086
12087 rettv->vval.v_number = (varnumber_T)(
12088#ifdef FEAT_MBYTE
12089 mb_string2cells(s, -1)
12090#else
12091 STRLEN(s)
12092#endif
12093 );
12094}
12095
12096/*
12097 * "strcharpart()" function
12098 */
12099 static void
12100f_strcharpart(typval_T *argvars, typval_T *rettv)
12101{
12102#ifdef FEAT_MBYTE
12103 char_u *p;
12104 int nchar;
12105 int nbyte = 0;
12106 int charlen;
12107 int len = 0;
12108 int slen;
12109 int error = FALSE;
12110
12111 p = get_tv_string(&argvars[0]);
12112 slen = (int)STRLEN(p);
12113
12114 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12115 if (!error)
12116 {
12117 if (nchar > 0)
12118 while (nchar > 0 && nbyte < slen)
12119 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012120 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012121 --nchar;
12122 }
12123 else
12124 nbyte = nchar;
12125 if (argvars[2].v_type != VAR_UNKNOWN)
12126 {
12127 charlen = (int)get_tv_number(&argvars[2]);
12128 while (charlen > 0 && nbyte + len < slen)
12129 {
12130 int off = nbyte + len;
12131
12132 if (off < 0)
12133 len += 1;
12134 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012135 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012136 --charlen;
12137 }
12138 }
12139 else
12140 len = slen - nbyte; /* default: all bytes that are available. */
12141 }
12142
12143 /*
12144 * Only return the overlap between the specified part and the actual
12145 * string.
12146 */
12147 if (nbyte < 0)
12148 {
12149 len += nbyte;
12150 nbyte = 0;
12151 }
12152 else if (nbyte > slen)
12153 nbyte = slen;
12154 if (len < 0)
12155 len = 0;
12156 else if (nbyte + len > slen)
12157 len = slen - nbyte;
12158
12159 rettv->v_type = VAR_STRING;
12160 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12161#else
12162 f_strpart(argvars, rettv);
12163#endif
12164}
12165
12166/*
12167 * "strpart()" function
12168 */
12169 static void
12170f_strpart(typval_T *argvars, typval_T *rettv)
12171{
12172 char_u *p;
12173 int n;
12174 int len;
12175 int slen;
12176 int error = FALSE;
12177
12178 p = get_tv_string(&argvars[0]);
12179 slen = (int)STRLEN(p);
12180
12181 n = (int)get_tv_number_chk(&argvars[1], &error);
12182 if (error)
12183 len = 0;
12184 else if (argvars[2].v_type != VAR_UNKNOWN)
12185 len = (int)get_tv_number(&argvars[2]);
12186 else
12187 len = slen - n; /* default len: all bytes that are available. */
12188
12189 /*
12190 * Only return the overlap between the specified part and the actual
12191 * string.
12192 */
12193 if (n < 0)
12194 {
12195 len += n;
12196 n = 0;
12197 }
12198 else if (n > slen)
12199 n = slen;
12200 if (len < 0)
12201 len = 0;
12202 else if (n + len > slen)
12203 len = slen - n;
12204
12205 rettv->v_type = VAR_STRING;
12206 rettv->vval.v_string = vim_strnsave(p + n, len);
12207}
12208
12209/*
12210 * "strridx()" function
12211 */
12212 static void
12213f_strridx(typval_T *argvars, typval_T *rettv)
12214{
12215 char_u buf[NUMBUFLEN];
12216 char_u *needle;
12217 char_u *haystack;
12218 char_u *rest;
12219 char_u *lastmatch = NULL;
12220 int haystack_len, end_idx;
12221
12222 needle = get_tv_string_chk(&argvars[1]);
12223 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12224
12225 rettv->vval.v_number = -1;
12226 if (needle == NULL || haystack == NULL)
12227 return; /* type error; errmsg already given */
12228
12229 haystack_len = (int)STRLEN(haystack);
12230 if (argvars[2].v_type != VAR_UNKNOWN)
12231 {
12232 /* Third argument: upper limit for index */
12233 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12234 if (end_idx < 0)
12235 return; /* can never find a match */
12236 }
12237 else
12238 end_idx = haystack_len;
12239
12240 if (*needle == NUL)
12241 {
12242 /* Empty string matches past the end. */
12243 lastmatch = haystack + end_idx;
12244 }
12245 else
12246 {
12247 for (rest = haystack; *rest != '\0'; ++rest)
12248 {
12249 rest = (char_u *)strstr((char *)rest, (char *)needle);
12250 if (rest == NULL || rest > haystack + end_idx)
12251 break;
12252 lastmatch = rest;
12253 }
12254 }
12255
12256 if (lastmatch == NULL)
12257 rettv->vval.v_number = -1;
12258 else
12259 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12260}
12261
12262/*
12263 * "strtrans()" function
12264 */
12265 static void
12266f_strtrans(typval_T *argvars, typval_T *rettv)
12267{
12268 rettv->v_type = VAR_STRING;
12269 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12270}
12271
12272/*
12273 * "submatch()" function
12274 */
12275 static void
12276f_submatch(typval_T *argvars, typval_T *rettv)
12277{
12278 int error = FALSE;
12279 int no;
12280 int retList = 0;
12281
12282 no = (int)get_tv_number_chk(&argvars[0], &error);
12283 if (error)
12284 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012285 if (no < 0 || no >= NSUBEXP)
12286 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012287 EMSGN(_("E935: invalid submatch number: %d"), no);
12288 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012289 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012290 if (argvars[1].v_type != VAR_UNKNOWN)
12291 retList = (int)get_tv_number_chk(&argvars[1], &error);
12292 if (error)
12293 return;
12294
12295 if (retList == 0)
12296 {
12297 rettv->v_type = VAR_STRING;
12298 rettv->vval.v_string = reg_submatch(no);
12299 }
12300 else
12301 {
12302 rettv->v_type = VAR_LIST;
12303 rettv->vval.v_list = reg_submatch_list(no);
12304 }
12305}
12306
12307/*
12308 * "substitute()" function
12309 */
12310 static void
12311f_substitute(typval_T *argvars, typval_T *rettv)
12312{
12313 char_u patbuf[NUMBUFLEN];
12314 char_u subbuf[NUMBUFLEN];
12315 char_u flagsbuf[NUMBUFLEN];
12316
12317 char_u *str = get_tv_string_chk(&argvars[0]);
12318 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012319 char_u *sub = NULL;
12320 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012321 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12322
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012323 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12324 expr = &argvars[2];
12325 else
12326 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12327
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012328 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012329 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12330 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331 rettv->vval.v_string = NULL;
12332 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012333 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012334}
12335
12336/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012337 * "swapinfo(swap_filename)" function
12338 */
12339 static void
12340f_swapinfo(typval_T *argvars, typval_T *rettv)
12341{
12342 if (rettv_dict_alloc(rettv) == OK)
12343 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12344}
12345
12346/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012347 * "swapname(expr)" function
12348 */
12349 static void
12350f_swapname(typval_T *argvars, typval_T *rettv)
12351{
12352 buf_T *buf;
12353
12354 rettv->v_type = VAR_STRING;
12355 buf = get_buf_tv(&argvars[0], FALSE);
12356 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12357 || buf->b_ml.ml_mfp->mf_fname == NULL)
12358 rettv->vval.v_string = NULL;
12359 else
12360 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12361}
12362
12363/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012364 * "synID(lnum, col, trans)" function
12365 */
12366 static void
12367f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12368{
12369 int id = 0;
12370#ifdef FEAT_SYN_HL
12371 linenr_T lnum;
12372 colnr_T col;
12373 int trans;
12374 int transerr = FALSE;
12375
12376 lnum = get_tv_lnum(argvars); /* -1 on type error */
12377 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12378 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12379
12380 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12381 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12382 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12383#endif
12384
12385 rettv->vval.v_number = id;
12386}
12387
12388/*
12389 * "synIDattr(id, what [, mode])" function
12390 */
12391 static void
12392f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12393{
12394 char_u *p = NULL;
12395#ifdef FEAT_SYN_HL
12396 int id;
12397 char_u *what;
12398 char_u *mode;
12399 char_u modebuf[NUMBUFLEN];
12400 int modec;
12401
12402 id = (int)get_tv_number(&argvars[0]);
12403 what = get_tv_string(&argvars[1]);
12404 if (argvars[2].v_type != VAR_UNKNOWN)
12405 {
12406 mode = get_tv_string_buf(&argvars[2], modebuf);
12407 modec = TOLOWER_ASC(mode[0]);
12408 if (modec != 't' && modec != 'c' && modec != 'g')
12409 modec = 0; /* replace invalid with current */
12410 }
12411 else
12412 {
12413#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12414 if (USE_24BIT)
12415 modec = 'g';
12416 else
12417#endif
12418 if (t_colors > 1)
12419 modec = 'c';
12420 else
12421 modec = 't';
12422 }
12423
12424
12425 switch (TOLOWER_ASC(what[0]))
12426 {
12427 case 'b':
12428 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12429 p = highlight_color(id, what, modec);
12430 else /* bold */
12431 p = highlight_has_attr(id, HL_BOLD, modec);
12432 break;
12433
12434 case 'f': /* fg[#] or font */
12435 p = highlight_color(id, what, modec);
12436 break;
12437
12438 case 'i':
12439 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12440 p = highlight_has_attr(id, HL_INVERSE, modec);
12441 else /* italic */
12442 p = highlight_has_attr(id, HL_ITALIC, modec);
12443 break;
12444
12445 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012446 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012447 break;
12448
12449 case 'r': /* reverse */
12450 p = highlight_has_attr(id, HL_INVERSE, modec);
12451 break;
12452
12453 case 's':
12454 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12455 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012456 /* strikeout */
12457 else if (TOLOWER_ASC(what[1]) == 't' &&
12458 TOLOWER_ASC(what[2]) == 'r')
12459 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460 else /* standout */
12461 p = highlight_has_attr(id, HL_STANDOUT, modec);
12462 break;
12463
12464 case 'u':
12465 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12466 /* underline */
12467 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12468 else
12469 /* undercurl */
12470 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12471 break;
12472 }
12473
12474 if (p != NULL)
12475 p = vim_strsave(p);
12476#endif
12477 rettv->v_type = VAR_STRING;
12478 rettv->vval.v_string = p;
12479}
12480
12481/*
12482 * "synIDtrans(id)" function
12483 */
12484 static void
12485f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12486{
12487 int id;
12488
12489#ifdef FEAT_SYN_HL
12490 id = (int)get_tv_number(&argvars[0]);
12491
12492 if (id > 0)
12493 id = syn_get_final_id(id);
12494 else
12495#endif
12496 id = 0;
12497
12498 rettv->vval.v_number = id;
12499}
12500
12501/*
12502 * "synconcealed(lnum, col)" function
12503 */
12504 static void
12505f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12506{
12507#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12508 linenr_T lnum;
12509 colnr_T col;
12510 int syntax_flags = 0;
12511 int cchar;
12512 int matchid = 0;
12513 char_u str[NUMBUFLEN];
12514#endif
12515
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012516 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517
12518#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12519 lnum = get_tv_lnum(argvars); /* -1 on type error */
12520 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12521
12522 vim_memset(str, NUL, sizeof(str));
12523
12524 if (rettv_list_alloc(rettv) != FAIL)
12525 {
12526 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12527 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12528 && curwin->w_p_cole > 0)
12529 {
12530 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12531 syntax_flags = get_syntax_info(&matchid);
12532
12533 /* get the conceal character */
12534 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12535 {
12536 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012537 if (cchar == NUL && curwin->w_p_cole == 1)
12538 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 if (cchar != NUL)
12540 {
12541# ifdef FEAT_MBYTE
12542 if (has_mbyte)
12543 (*mb_char2bytes)(cchar, str);
12544 else
12545# endif
12546 str[0] = cchar;
12547 }
12548 }
12549 }
12550
12551 list_append_number(rettv->vval.v_list,
12552 (syntax_flags & HL_CONCEAL) != 0);
12553 /* -1 to auto-determine strlen */
12554 list_append_string(rettv->vval.v_list, str, -1);
12555 list_append_number(rettv->vval.v_list, matchid);
12556 }
12557#endif
12558}
12559
12560/*
12561 * "synstack(lnum, col)" function
12562 */
12563 static void
12564f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12565{
12566#ifdef FEAT_SYN_HL
12567 linenr_T lnum;
12568 colnr_T col;
12569 int i;
12570 int id;
12571#endif
12572
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012573 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012574
12575#ifdef FEAT_SYN_HL
12576 lnum = get_tv_lnum(argvars); /* -1 on type error */
12577 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12578
12579 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12580 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12581 && rettv_list_alloc(rettv) != FAIL)
12582 {
12583 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12584 for (i = 0; ; ++i)
12585 {
12586 id = syn_get_stack_item(i);
12587 if (id < 0)
12588 break;
12589 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12590 break;
12591 }
12592 }
12593#endif
12594}
12595
12596 static void
12597get_cmd_output_as_rettv(
12598 typval_T *argvars,
12599 typval_T *rettv,
12600 int retlist)
12601{
12602 char_u *res = NULL;
12603 char_u *p;
12604 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 int err = FALSE;
12606 FILE *fd;
12607 list_T *list = NULL;
12608 int flags = SHELL_SILENT;
12609
12610 rettv->v_type = VAR_STRING;
12611 rettv->vval.v_string = NULL;
12612 if (check_restricted() || check_secure())
12613 goto errret;
12614
12615 if (argvars[1].v_type != VAR_UNKNOWN)
12616 {
12617 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012618 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012619 * command.
12620 */
12621 if ((infile = vim_tempname('i', TRUE)) == NULL)
12622 {
12623 EMSG(_(e_notmp));
12624 goto errret;
12625 }
12626
12627 fd = mch_fopen((char *)infile, WRITEBIN);
12628 if (fd == NULL)
12629 {
12630 EMSG2(_(e_notopen), infile);
12631 goto errret;
12632 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012633 if (argvars[1].v_type == VAR_NUMBER)
12634 {
12635 linenr_T lnum;
12636 buf_T *buf;
12637
12638 buf = buflist_findnr(argvars[1].vval.v_number);
12639 if (buf == NULL)
12640 {
12641 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012642 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012643 goto errret;
12644 }
12645
12646 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12647 {
12648 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12649 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12650 {
12651 err = TRUE;
12652 break;
12653 }
12654 if (putc(NL, fd) == EOF)
12655 {
12656 err = TRUE;
12657 break;
12658 }
12659 }
12660 }
12661 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 {
12663 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12664 err = TRUE;
12665 }
12666 else
12667 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012668 size_t len;
12669 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012670
12671 p = get_tv_string_buf_chk(&argvars[1], buf);
12672 if (p == NULL)
12673 {
12674 fclose(fd);
12675 goto errret; /* type error; errmsg already given */
12676 }
12677 len = STRLEN(p);
12678 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12679 err = TRUE;
12680 }
12681 if (fclose(fd) != 0)
12682 err = TRUE;
12683 if (err)
12684 {
12685 EMSG(_("E677: Error writing temp file"));
12686 goto errret;
12687 }
12688 }
12689
12690 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12691 * echoes typeahead, that messes up the display. */
12692 if (!msg_silent)
12693 flags += SHELL_COOKED;
12694
12695 if (retlist)
12696 {
12697 int len;
12698 listitem_T *li;
12699 char_u *s = NULL;
12700 char_u *start;
12701 char_u *end;
12702 int i;
12703
12704 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12705 if (res == NULL)
12706 goto errret;
12707
12708 list = list_alloc();
12709 if (list == NULL)
12710 goto errret;
12711
12712 for (i = 0; i < len; ++i)
12713 {
12714 start = res + i;
12715 while (i < len && res[i] != NL)
12716 ++i;
12717 end = res + i;
12718
12719 s = alloc((unsigned)(end - start + 1));
12720 if (s == NULL)
12721 goto errret;
12722
12723 for (p = s; start < end; ++p, ++start)
12724 *p = *start == NUL ? NL : *start;
12725 *p = NUL;
12726
12727 li = listitem_alloc();
12728 if (li == NULL)
12729 {
12730 vim_free(s);
12731 goto errret;
12732 }
12733 li->li_tv.v_type = VAR_STRING;
12734 li->li_tv.v_lock = 0;
12735 li->li_tv.vval.v_string = s;
12736 list_append(list, li);
12737 }
12738
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012739 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 list = NULL;
12741 }
12742 else
12743 {
12744 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12745#ifdef USE_CR
12746 /* translate <CR> into <NL> */
12747 if (res != NULL)
12748 {
12749 char_u *s;
12750
12751 for (s = res; *s; ++s)
12752 {
12753 if (*s == CAR)
12754 *s = NL;
12755 }
12756 }
12757#else
12758# ifdef USE_CRNL
12759 /* translate <CR><NL> into <NL> */
12760 if (res != NULL)
12761 {
12762 char_u *s, *d;
12763
12764 d = res;
12765 for (s = res; *s; ++s)
12766 {
12767 if (s[0] == CAR && s[1] == NL)
12768 ++s;
12769 *d++ = *s;
12770 }
12771 *d = NUL;
12772 }
12773# endif
12774#endif
12775 rettv->vval.v_string = res;
12776 res = NULL;
12777 }
12778
12779errret:
12780 if (infile != NULL)
12781 {
12782 mch_remove(infile);
12783 vim_free(infile);
12784 }
12785 if (res != NULL)
12786 vim_free(res);
12787 if (list != NULL)
12788 list_free(list);
12789}
12790
12791/*
12792 * "system()" function
12793 */
12794 static void
12795f_system(typval_T *argvars, typval_T *rettv)
12796{
12797 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12798}
12799
12800/*
12801 * "systemlist()" function
12802 */
12803 static void
12804f_systemlist(typval_T *argvars, typval_T *rettv)
12805{
12806 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12807}
12808
12809/*
12810 * "tabpagebuflist()" function
12811 */
12812 static void
12813f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12814{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012815 tabpage_T *tp;
12816 win_T *wp = NULL;
12817
12818 if (argvars[0].v_type == VAR_UNKNOWN)
12819 wp = firstwin;
12820 else
12821 {
12822 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12823 if (tp != NULL)
12824 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12825 }
12826 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12827 {
12828 for (; wp != NULL; wp = wp->w_next)
12829 if (list_append_number(rettv->vval.v_list,
12830 wp->w_buffer->b_fnum) == FAIL)
12831 break;
12832 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012833}
12834
12835
12836/*
12837 * "tabpagenr()" function
12838 */
12839 static void
12840f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12841{
12842 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012843 char_u *arg;
12844
12845 if (argvars[0].v_type != VAR_UNKNOWN)
12846 {
12847 arg = get_tv_string_chk(&argvars[0]);
12848 nr = 0;
12849 if (arg != NULL)
12850 {
12851 if (STRCMP(arg, "$") == 0)
12852 nr = tabpage_index(NULL) - 1;
12853 else
12854 EMSG2(_(e_invexpr2), arg);
12855 }
12856 }
12857 else
12858 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012859 rettv->vval.v_number = nr;
12860}
12861
12862
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012863static int get_winnr(tabpage_T *tp, typval_T *argvar);
12864
12865/*
12866 * Common code for tabpagewinnr() and winnr().
12867 */
12868 static int
12869get_winnr(tabpage_T *tp, typval_T *argvar)
12870{
12871 win_T *twin;
12872 int nr = 1;
12873 win_T *wp;
12874 char_u *arg;
12875
12876 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12877 if (argvar->v_type != VAR_UNKNOWN)
12878 {
12879 arg = get_tv_string_chk(argvar);
12880 if (arg == NULL)
12881 nr = 0; /* type error; errmsg already given */
12882 else if (STRCMP(arg, "$") == 0)
12883 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12884 else if (STRCMP(arg, "#") == 0)
12885 {
12886 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12887 if (twin == NULL)
12888 nr = 0;
12889 }
12890 else
12891 {
12892 EMSG2(_(e_invexpr2), arg);
12893 nr = 0;
12894 }
12895 }
12896
12897 if (nr > 0)
12898 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12899 wp != twin; wp = wp->w_next)
12900 {
12901 if (wp == NULL)
12902 {
12903 /* didn't find it in this tabpage */
12904 nr = 0;
12905 break;
12906 }
12907 ++nr;
12908 }
12909 return nr;
12910}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012911
12912/*
12913 * "tabpagewinnr()" function
12914 */
12915 static void
12916f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12917{
12918 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012919 tabpage_T *tp;
12920
12921 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12922 if (tp == NULL)
12923 nr = 0;
12924 else
12925 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012926 rettv->vval.v_number = nr;
12927}
12928
12929
12930/*
12931 * "tagfiles()" function
12932 */
12933 static void
12934f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12935{
12936 char_u *fname;
12937 tagname_T tn;
12938 int first;
12939
12940 if (rettv_list_alloc(rettv) == FAIL)
12941 return;
12942 fname = alloc(MAXPATHL);
12943 if (fname == NULL)
12944 return;
12945
12946 for (first = TRUE; ; first = FALSE)
12947 if (get_tagfname(&tn, first, fname) == FAIL
12948 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12949 break;
12950 tagname_free(&tn);
12951 vim_free(fname);
12952}
12953
12954/*
12955 * "taglist()" function
12956 */
12957 static void
12958f_taglist(typval_T *argvars, typval_T *rettv)
12959{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012960 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 char_u *tag_pattern;
12962
12963 tag_pattern = get_tv_string(&argvars[0]);
12964
12965 rettv->vval.v_number = FALSE;
12966 if (*tag_pattern == NUL)
12967 return;
12968
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012969 if (argvars[1].v_type != VAR_UNKNOWN)
12970 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012972 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973}
12974
12975/*
12976 * "tempname()" function
12977 */
12978 static void
12979f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12980{
12981 static int x = 'A';
12982
12983 rettv->v_type = VAR_STRING;
12984 rettv->vval.v_string = vim_tempname(x, FALSE);
12985
12986 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12987 * names. Skip 'I' and 'O', they are used for shell redirection. */
12988 do
12989 {
12990 if (x == 'Z')
12991 x = '0';
12992 else if (x == '9')
12993 x = 'A';
12994 else
12995 {
12996#ifdef EBCDIC
12997 if (x == 'I')
12998 x = 'J';
12999 else if (x == 'R')
13000 x = 'S';
13001 else
13002#endif
13003 ++x;
13004 }
13005 } while (x == 'I' || x == 'O');
13006}
13007
13008#ifdef FEAT_FLOAT
13009/*
13010 * "tan()" function
13011 */
13012 static void
13013f_tan(typval_T *argvars, typval_T *rettv)
13014{
13015 float_T f = 0.0;
13016
13017 rettv->v_type = VAR_FLOAT;
13018 if (get_float_arg(argvars, &f) == OK)
13019 rettv->vval.v_float = tan(f);
13020 else
13021 rettv->vval.v_float = 0.0;
13022}
13023
13024/*
13025 * "tanh()" function
13026 */
13027 static void
13028f_tanh(typval_T *argvars, typval_T *rettv)
13029{
13030 float_T f = 0.0;
13031
13032 rettv->v_type = VAR_FLOAT;
13033 if (get_float_arg(argvars, &f) == OK)
13034 rettv->vval.v_float = tanh(f);
13035 else
13036 rettv->vval.v_float = 0.0;
13037}
13038#endif
13039
13040/*
13041 * "test_alloc_fail(id, countdown, repeat)" function
13042 */
13043 static void
13044f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13045{
13046 if (argvars[0].v_type != VAR_NUMBER
13047 || argvars[0].vval.v_number <= 0
13048 || argvars[1].v_type != VAR_NUMBER
13049 || argvars[1].vval.v_number < 0
13050 || argvars[2].v_type != VAR_NUMBER)
13051 EMSG(_(e_invarg));
13052 else
13053 {
13054 alloc_fail_id = argvars[0].vval.v_number;
13055 if (alloc_fail_id >= aid_last)
13056 EMSG(_(e_invarg));
13057 alloc_fail_countdown = argvars[1].vval.v_number;
13058 alloc_fail_repeat = argvars[2].vval.v_number;
13059 did_outofmem_msg = FALSE;
13060 }
13061}
13062
13063/*
13064 * "test_autochdir()"
13065 */
13066 static void
13067f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13068{
13069#if defined(FEAT_AUTOCHDIR)
13070 test_autochdir = TRUE;
13071#endif
13072}
13073
13074/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013075 * "test_feedinput()"
13076 */
13077 static void
13078f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13079{
13080#ifdef USE_INPUT_BUF
13081 char_u *val = get_tv_string_chk(&argvars[0]);
13082
13083 if (val != NULL)
13084 {
13085 trash_input_buf();
13086 add_to_input_buf_csi(val, (int)STRLEN(val));
13087 }
13088#endif
13089}
13090
13091/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013092 * "test_option_not_set({name})" function
13093 */
13094 static void
13095f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13096{
13097 char_u *name = (char_u *)"";
13098
13099 if (argvars[0].v_type != VAR_STRING)
13100 EMSG(_(e_invarg));
13101 else
13102 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013103 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013104 if (reset_option_was_set(name) == FAIL)
13105 EMSG2(_(e_invarg2), name);
13106 }
13107}
13108
13109/*
13110 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013111 */
13112 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013113f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013114{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013115 char_u *name = (char_u *)"";
13116 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013117 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013118
13119 if (argvars[0].v_type != VAR_STRING
13120 || (argvars[1].v_type) != VAR_NUMBER)
13121 EMSG(_(e_invarg));
13122 else
13123 {
13124 name = get_tv_string_chk(&argvars[0]);
13125 val = (int)get_tv_number(&argvars[1]);
13126
13127 if (STRCMP(name, (char_u *)"redraw") == 0)
13128 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013129 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13130 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013131 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13132 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013133 else if (STRCMP(name, (char_u *)"starting") == 0)
13134 {
13135 if (val)
13136 {
13137 if (save_starting < 0)
13138 save_starting = starting;
13139 starting = 0;
13140 }
13141 else
13142 {
13143 starting = save_starting;
13144 save_starting = -1;
13145 }
13146 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013147 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13148 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013149 else if (STRCMP(name, (char_u *)"ALL") == 0)
13150 {
13151 disable_char_avail_for_testing = FALSE;
13152 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013153 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013154 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013155 if (save_starting >= 0)
13156 {
13157 starting = save_starting;
13158 save_starting = -1;
13159 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013160 }
13161 else
13162 EMSG2(_(e_invarg2), name);
13163 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013164}
13165
13166/*
13167 * "test_garbagecollect_now()" function
13168 */
13169 static void
13170f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13171{
13172 /* This is dangerous, any Lists and Dicts used internally may be freed
13173 * while still in use. */
13174 garbage_collect(TRUE);
13175}
13176
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013177/*
13178 * "test_ignore_error()" function
13179 */
13180 static void
13181f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13182{
13183 ignore_error_for_testing(get_tv_string(&argvars[0]));
13184}
13185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186#ifdef FEAT_JOB_CHANNEL
13187 static void
13188f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13189{
13190 rettv->v_type = VAR_CHANNEL;
13191 rettv->vval.v_channel = NULL;
13192}
13193#endif
13194
13195 static void
13196f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13197{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013198 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199}
13200
13201#ifdef FEAT_JOB_CHANNEL
13202 static void
13203f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13204{
13205 rettv->v_type = VAR_JOB;
13206 rettv->vval.v_job = NULL;
13207}
13208#endif
13209
13210 static void
13211f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13212{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013213 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214}
13215
13216 static void
13217f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13218{
13219 rettv->v_type = VAR_PARTIAL;
13220 rettv->vval.v_partial = NULL;
13221}
13222
13223 static void
13224f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13225{
13226 rettv->v_type = VAR_STRING;
13227 rettv->vval.v_string = NULL;
13228}
13229
Bram Moolenaarab186732018-09-14 21:27:06 +020013230#ifdef FEAT_GUI
13231 static void
13232f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13233{
13234 char_u *which;
13235 long value;
13236 int dragging;
13237 scrollbar_T *sb = NULL;
13238
13239 if (argvars[0].v_type != VAR_STRING
13240 || (argvars[1].v_type) != VAR_NUMBER
13241 || (argvars[2].v_type) != VAR_NUMBER)
13242 {
13243 EMSG(_(e_invarg));
13244 return;
13245 }
13246 which = get_tv_string(&argvars[0]);
13247 value = get_tv_number(&argvars[1]);
13248 dragging = get_tv_number(&argvars[2]);
13249
13250 if (STRCMP(which, "left") == 0)
13251 sb = &curwin->w_scrollbars[SBAR_LEFT];
13252 else if (STRCMP(which, "right") == 0)
13253 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13254 else if (STRCMP(which, "hor") == 0)
13255 sb = &gui.bottom_sbar;
13256 if (sb == NULL)
13257 {
13258 EMSG2(_(e_invarg2), which);
13259 return;
13260 }
13261 gui_drag_scrollbar(sb, value, dragging);
13262}
13263#endif
13264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 static void
13266f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13267{
13268 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13269}
13270
13271#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13272/*
13273 * Get a callback from "arg". It can be a Funcref or a function name.
13274 * When "arg" is zero return an empty string.
13275 * Return NULL for an invalid argument.
13276 */
13277 char_u *
13278get_callback(typval_T *arg, partial_T **pp)
13279{
13280 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13281 {
13282 *pp = arg->vval.v_partial;
13283 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013284 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 }
13286 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013287 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 {
13289 func_ref(arg->vval.v_string);
13290 return arg->vval.v_string;
13291 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13293 return (char_u *)"";
13294 EMSG(_("E921: Invalid callback argument"));
13295 return NULL;
13296}
13297
13298/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013299 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 */
13301 void
13302free_callback(char_u *callback, partial_T *partial)
13303{
13304 if (partial != NULL)
13305 partial_unref(partial);
13306 else if (callback != NULL)
13307 {
13308 func_unref(callback);
13309 vim_free(callback);
13310 }
13311}
13312#endif
13313
13314#ifdef FEAT_TIMERS
13315/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013316 * "timer_info([timer])" function
13317 */
13318 static void
13319f_timer_info(typval_T *argvars, typval_T *rettv)
13320{
13321 timer_T *timer = NULL;
13322
13323 if (rettv_list_alloc(rettv) != OK)
13324 return;
13325 if (argvars[0].v_type != VAR_UNKNOWN)
13326 {
13327 if (argvars[0].v_type != VAR_NUMBER)
13328 EMSG(_(e_number_exp));
13329 else
13330 {
13331 timer = find_timer((int)get_tv_number(&argvars[0]));
13332 if (timer != NULL)
13333 add_timer_info(rettv, timer);
13334 }
13335 }
13336 else
13337 add_timer_info_all(rettv);
13338}
13339
13340/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013341 * "timer_pause(timer, paused)" function
13342 */
13343 static void
13344f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13345{
13346 timer_T *timer = NULL;
13347 int paused = (int)get_tv_number(&argvars[1]);
13348
13349 if (argvars[0].v_type != VAR_NUMBER)
13350 EMSG(_(e_number_exp));
13351 else
13352 {
13353 timer = find_timer((int)get_tv_number(&argvars[0]));
13354 if (timer != NULL)
13355 timer->tr_paused = paused;
13356 }
13357}
13358
13359/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013360 * "timer_start(time, callback [, options])" function
13361 */
13362 static void
13363f_timer_start(typval_T *argvars, typval_T *rettv)
13364{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013365 long msec = (long)get_tv_number(&argvars[0]);
13366 timer_T *timer;
13367 int repeat = 0;
13368 char_u *callback;
13369 dict_T *dict;
13370 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013371
Bram Moolenaar75537a92016-09-05 22:45:28 +020013372 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373 if (check_secure())
13374 return;
13375 if (argvars[2].v_type != VAR_UNKNOWN)
13376 {
13377 if (argvars[2].v_type != VAR_DICT
13378 || (dict = argvars[2].vval.v_dict) == NULL)
13379 {
13380 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13381 return;
13382 }
13383 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13384 repeat = get_dict_number(dict, (char_u *)"repeat");
13385 }
13386
Bram Moolenaar75537a92016-09-05 22:45:28 +020013387 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013389 return;
13390
13391 timer = create_timer(msec, repeat);
13392 if (timer == NULL)
13393 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 else
13395 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013396 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013397 timer->tr_callback = vim_strsave(callback);
13398 else
13399 /* pointer into the partial */
13400 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013401 timer->tr_partial = partial;
13402 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 }
13404}
13405
13406/*
13407 * "timer_stop(timer)" function
13408 */
13409 static void
13410f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13411{
13412 timer_T *timer;
13413
13414 if (argvars[0].v_type != VAR_NUMBER)
13415 {
13416 EMSG(_(e_number_exp));
13417 return;
13418 }
13419 timer = find_timer((int)get_tv_number(&argvars[0]));
13420 if (timer != NULL)
13421 stop_timer(timer);
13422}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013423
13424/*
13425 * "timer_stopall()" function
13426 */
13427 static void
13428f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13429{
13430 stop_all_timers();
13431}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432#endif
13433
13434/*
13435 * "tolower(string)" function
13436 */
13437 static void
13438f_tolower(typval_T *argvars, typval_T *rettv)
13439{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013440 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013441 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442}
13443
13444/*
13445 * "toupper(string)" function
13446 */
13447 static void
13448f_toupper(typval_T *argvars, typval_T *rettv)
13449{
13450 rettv->v_type = VAR_STRING;
13451 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13452}
13453
13454/*
13455 * "tr(string, fromstr, tostr)" function
13456 */
13457 static void
13458f_tr(typval_T *argvars, typval_T *rettv)
13459{
13460 char_u *in_str;
13461 char_u *fromstr;
13462 char_u *tostr;
13463 char_u *p;
13464#ifdef FEAT_MBYTE
13465 int inlen;
13466 int fromlen;
13467 int tolen;
13468 int idx;
13469 char_u *cpstr;
13470 int cplen;
13471 int first = TRUE;
13472#endif
13473 char_u buf[NUMBUFLEN];
13474 char_u buf2[NUMBUFLEN];
13475 garray_T ga;
13476
13477 in_str = get_tv_string(&argvars[0]);
13478 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13479 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13480
13481 /* Default return value: empty string. */
13482 rettv->v_type = VAR_STRING;
13483 rettv->vval.v_string = NULL;
13484 if (fromstr == NULL || tostr == NULL)
13485 return; /* type error; errmsg already given */
13486 ga_init2(&ga, (int)sizeof(char), 80);
13487
13488#ifdef FEAT_MBYTE
13489 if (!has_mbyte)
13490#endif
13491 /* not multi-byte: fromstr and tostr must be the same length */
13492 if (STRLEN(fromstr) != STRLEN(tostr))
13493 {
13494#ifdef FEAT_MBYTE
13495error:
13496#endif
13497 EMSG2(_(e_invarg2), fromstr);
13498 ga_clear(&ga);
13499 return;
13500 }
13501
13502 /* fromstr and tostr have to contain the same number of chars */
13503 while (*in_str != NUL)
13504 {
13505#ifdef FEAT_MBYTE
13506 if (has_mbyte)
13507 {
13508 inlen = (*mb_ptr2len)(in_str);
13509 cpstr = in_str;
13510 cplen = inlen;
13511 idx = 0;
13512 for (p = fromstr; *p != NUL; p += fromlen)
13513 {
13514 fromlen = (*mb_ptr2len)(p);
13515 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13516 {
13517 for (p = tostr; *p != NUL; p += tolen)
13518 {
13519 tolen = (*mb_ptr2len)(p);
13520 if (idx-- == 0)
13521 {
13522 cplen = tolen;
13523 cpstr = p;
13524 break;
13525 }
13526 }
13527 if (*p == NUL) /* tostr is shorter than fromstr */
13528 goto error;
13529 break;
13530 }
13531 ++idx;
13532 }
13533
13534 if (first && cpstr == in_str)
13535 {
13536 /* Check that fromstr and tostr have the same number of
13537 * (multi-byte) characters. Done only once when a character
13538 * of in_str doesn't appear in fromstr. */
13539 first = FALSE;
13540 for (p = tostr; *p != NUL; p += tolen)
13541 {
13542 tolen = (*mb_ptr2len)(p);
13543 --idx;
13544 }
13545 if (idx != 0)
13546 goto error;
13547 }
13548
13549 (void)ga_grow(&ga, cplen);
13550 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13551 ga.ga_len += cplen;
13552
13553 in_str += inlen;
13554 }
13555 else
13556#endif
13557 {
13558 /* When not using multi-byte chars we can do it faster. */
13559 p = vim_strchr(fromstr, *in_str);
13560 if (p != NULL)
13561 ga_append(&ga, tostr[p - fromstr]);
13562 else
13563 ga_append(&ga, *in_str);
13564 ++in_str;
13565 }
13566 }
13567
13568 /* add a terminating NUL */
13569 (void)ga_grow(&ga, 1);
13570 ga_append(&ga, NUL);
13571
13572 rettv->vval.v_string = ga.ga_data;
13573}
13574
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013575/*
13576 * "trim({expr})" function
13577 */
13578 static void
13579f_trim(typval_T *argvars, typval_T *rettv)
13580{
13581 char_u buf1[NUMBUFLEN];
13582 char_u buf2[NUMBUFLEN];
13583 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13584 char_u *mask = NULL;
13585 char_u *tail;
13586 char_u *prev;
13587 char_u *p;
13588 int c1;
13589
13590 rettv->v_type = VAR_STRING;
13591 if (head == NULL)
13592 {
13593 rettv->vval.v_string = NULL;
13594 return;
13595 }
13596
13597 if (argvars[1].v_type == VAR_STRING)
13598 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13599
13600 while (*head != NUL)
13601 {
13602 c1 = PTR2CHAR(head);
13603 if (mask == NULL)
13604 {
13605 if (c1 > ' ' && c1 != 0xa0)
13606 break;
13607 }
13608 else
13609 {
13610 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13611 if (c1 == PTR2CHAR(p))
13612 break;
13613 if (*p == NUL)
13614 break;
13615 }
13616 MB_PTR_ADV(head);
13617 }
13618
13619 for (tail = head + STRLEN(head); tail > head; tail = prev)
13620 {
13621 prev = tail;
13622 MB_PTR_BACK(head, prev);
13623 c1 = PTR2CHAR(prev);
13624 if (mask == NULL)
13625 {
13626 if (c1 > ' ' && c1 != 0xa0)
13627 break;
13628 }
13629 else
13630 {
13631 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13632 if (c1 == PTR2CHAR(p))
13633 break;
13634 if (*p == NUL)
13635 break;
13636 }
13637 }
13638 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13639}
13640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013641#ifdef FEAT_FLOAT
13642/*
13643 * "trunc({float})" function
13644 */
13645 static void
13646f_trunc(typval_T *argvars, typval_T *rettv)
13647{
13648 float_T f = 0.0;
13649
13650 rettv->v_type = VAR_FLOAT;
13651 if (get_float_arg(argvars, &f) == OK)
13652 /* trunc() is not in C90, use floor() or ceil() instead. */
13653 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13654 else
13655 rettv->vval.v_float = 0.0;
13656}
13657#endif
13658
13659/*
13660 * "type(expr)" function
13661 */
13662 static void
13663f_type(typval_T *argvars, typval_T *rettv)
13664{
13665 int n = -1;
13666
13667 switch (argvars[0].v_type)
13668 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013669 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13670 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013671 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013672 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13673 case VAR_LIST: n = VAR_TYPE_LIST; break;
13674 case VAR_DICT: n = VAR_TYPE_DICT; break;
13675 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013676 case VAR_SPECIAL:
13677 if (argvars[0].vval.v_number == VVAL_FALSE
13678 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013679 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013680 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013681 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013682 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013683 case VAR_JOB: n = VAR_TYPE_JOB; break;
13684 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013686 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013687 n = -1;
13688 break;
13689 }
13690 rettv->vval.v_number = n;
13691}
13692
13693/*
13694 * "undofile(name)" function
13695 */
13696 static void
13697f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13698{
13699 rettv->v_type = VAR_STRING;
13700#ifdef FEAT_PERSISTENT_UNDO
13701 {
13702 char_u *fname = get_tv_string(&argvars[0]);
13703
13704 if (*fname == NUL)
13705 {
13706 /* If there is no file name there will be no undo file. */
13707 rettv->vval.v_string = NULL;
13708 }
13709 else
13710 {
13711 char_u *ffname = FullName_save(fname, FALSE);
13712
13713 if (ffname != NULL)
13714 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13715 vim_free(ffname);
13716 }
13717 }
13718#else
13719 rettv->vval.v_string = NULL;
13720#endif
13721}
13722
13723/*
13724 * "undotree()" function
13725 */
13726 static void
13727f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13728{
13729 if (rettv_dict_alloc(rettv) == OK)
13730 {
13731 dict_T *dict = rettv->vval.v_dict;
13732 list_T *list;
13733
Bram Moolenaare0be1672018-07-08 16:50:37 +020013734 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13735 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13736 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13737 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13738 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13739 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013740
13741 list = list_alloc();
13742 if (list != NULL)
13743 {
13744 u_eval_tree(curbuf->b_u_oldhead, list);
13745 dict_add_list(dict, "entries", list);
13746 }
13747 }
13748}
13749
13750/*
13751 * "values(dict)" function
13752 */
13753 static void
13754f_values(typval_T *argvars, typval_T *rettv)
13755{
13756 dict_list(argvars, rettv, 1);
13757}
13758
13759/*
13760 * "virtcol(string)" function
13761 */
13762 static void
13763f_virtcol(typval_T *argvars, typval_T *rettv)
13764{
13765 colnr_T vcol = 0;
13766 pos_T *fp;
13767 int fnum = curbuf->b_fnum;
13768
13769 fp = var2fpos(&argvars[0], FALSE, &fnum);
13770 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13771 && fnum == curbuf->b_fnum)
13772 {
13773 getvvcol(curwin, fp, NULL, NULL, &vcol);
13774 ++vcol;
13775 }
13776
13777 rettv->vval.v_number = vcol;
13778}
13779
13780/*
13781 * "visualmode()" function
13782 */
13783 static void
13784f_visualmode(typval_T *argvars, typval_T *rettv)
13785{
13786 char_u str[2];
13787
13788 rettv->v_type = VAR_STRING;
13789 str[0] = curbuf->b_visual_mode_eval;
13790 str[1] = NUL;
13791 rettv->vval.v_string = vim_strsave(str);
13792
13793 /* A non-zero number or non-empty string argument: reset mode. */
13794 if (non_zero_arg(&argvars[0]))
13795 curbuf->b_visual_mode_eval = NUL;
13796}
13797
13798/*
13799 * "wildmenumode()" function
13800 */
13801 static void
13802f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13803{
13804#ifdef FEAT_WILDMENU
13805 if (wild_menu_showing)
13806 rettv->vval.v_number = 1;
13807#endif
13808}
13809
13810/*
13811 * "winbufnr(nr)" function
13812 */
13813 static void
13814f_winbufnr(typval_T *argvars, typval_T *rettv)
13815{
13816 win_T *wp;
13817
13818 wp = find_win_by_nr(&argvars[0], NULL);
13819 if (wp == NULL)
13820 rettv->vval.v_number = -1;
13821 else
13822 rettv->vval.v_number = wp->w_buffer->b_fnum;
13823}
13824
13825/*
13826 * "wincol()" function
13827 */
13828 static void
13829f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13830{
13831 validate_cursor();
13832 rettv->vval.v_number = curwin->w_wcol + 1;
13833}
13834
13835/*
13836 * "winheight(nr)" function
13837 */
13838 static void
13839f_winheight(typval_T *argvars, typval_T *rettv)
13840{
13841 win_T *wp;
13842
13843 wp = find_win_by_nr(&argvars[0], NULL);
13844 if (wp == NULL)
13845 rettv->vval.v_number = -1;
13846 else
13847 rettv->vval.v_number = wp->w_height;
13848}
13849
13850/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013851 * "winlayout()" function
13852 */
13853 static void
13854f_winlayout(typval_T *argvars, typval_T *rettv)
13855{
13856 tabpage_T *tp;
13857
13858 if (rettv_list_alloc(rettv) != OK)
13859 return;
13860
13861 if (argvars[0].v_type == VAR_UNKNOWN)
13862 tp = curtab;
13863 else
13864 {
13865 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13866 if (tp == NULL)
13867 return;
13868 }
13869
13870 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13871}
13872
13873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013874 * "winline()" function
13875 */
13876 static void
13877f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13878{
13879 validate_cursor();
13880 rettv->vval.v_number = curwin->w_wrow + 1;
13881}
13882
13883/*
13884 * "winnr()" function
13885 */
13886 static void
13887f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13888{
13889 int nr = 1;
13890
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013892 rettv->vval.v_number = nr;
13893}
13894
13895/*
13896 * "winrestcmd()" function
13897 */
13898 static void
13899f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13900{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013901 win_T *wp;
13902 int winnr = 1;
13903 garray_T ga;
13904 char_u buf[50];
13905
13906 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013907 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 {
13909 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13910 ga_concat(&ga, buf);
13911 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13912 ga_concat(&ga, buf);
13913 ++winnr;
13914 }
13915 ga_append(&ga, NUL);
13916
13917 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 rettv->v_type = VAR_STRING;
13919}
13920
13921/*
13922 * "winrestview()" function
13923 */
13924 static void
13925f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13926{
13927 dict_T *dict;
13928
13929 if (argvars[0].v_type != VAR_DICT
13930 || (dict = argvars[0].vval.v_dict) == NULL)
13931 EMSG(_(e_invarg));
13932 else
13933 {
13934 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13935 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13936 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13937 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13938#ifdef FEAT_VIRTUALEDIT
13939 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13940 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13941#endif
13942 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13943 {
13944 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13945 curwin->w_set_curswant = FALSE;
13946 }
13947
13948 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13949 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13950#ifdef FEAT_DIFF
13951 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13952 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13953#endif
13954 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13955 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13956 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13957 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13958
13959 check_cursor();
13960 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013961 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013962 changed_window_setting();
13963
13964 if (curwin->w_topline <= 0)
13965 curwin->w_topline = 1;
13966 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13967 curwin->w_topline = curbuf->b_ml.ml_line_count;
13968#ifdef FEAT_DIFF
13969 check_topfill(curwin, TRUE);
13970#endif
13971 }
13972}
13973
13974/*
13975 * "winsaveview()" function
13976 */
13977 static void
13978f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13979{
13980 dict_T *dict;
13981
13982 if (rettv_dict_alloc(rettv) == FAIL)
13983 return;
13984 dict = rettv->vval.v_dict;
13985
Bram Moolenaare0be1672018-07-08 16:50:37 +020013986 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13987 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013988#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013989 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013990#endif
13991 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013992 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993
Bram Moolenaare0be1672018-07-08 16:50:37 +020013994 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013995#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013996 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013997#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013998 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13999 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014000}
14001
14002/*
14003 * "winwidth(nr)" function
14004 */
14005 static void
14006f_winwidth(typval_T *argvars, typval_T *rettv)
14007{
14008 win_T *wp;
14009
14010 wp = find_win_by_nr(&argvars[0], NULL);
14011 if (wp == NULL)
14012 rettv->vval.v_number = -1;
14013 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014014 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014015}
14016
14017/*
14018 * "wordcount()" function
14019 */
14020 static void
14021f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14022{
14023 if (rettv_dict_alloc(rettv) == FAIL)
14024 return;
14025 cursor_pos_info(rettv->vval.v_dict);
14026}
14027
14028/*
14029 * "writefile()" function
14030 */
14031 static void
14032f_writefile(typval_T *argvars, typval_T *rettv)
14033{
14034 int binary = FALSE;
14035 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014036#ifdef HAVE_FSYNC
14037 int do_fsync = p_fs;
14038#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 char_u *fname;
14040 FILE *fd;
14041 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014042 listitem_T *li;
14043 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014045 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014046 if (check_restricted() || check_secure())
14047 return;
14048
14049 if (argvars[0].v_type != VAR_LIST)
14050 {
14051 EMSG2(_(e_listarg), "writefile()");
14052 return;
14053 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014054 list = argvars[0].vval.v_list;
14055 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014057 for (li = list->lv_first; li != NULL; li = li->li_next)
14058 if (get_tv_string_chk(&li->li_tv) == NULL)
14059 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060
14061 if (argvars[2].v_type != VAR_UNKNOWN)
14062 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014063 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14064
14065 if (arg2 == NULL)
14066 return;
14067 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014069 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014070 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014071#ifdef HAVE_FSYNC
14072 if (vim_strchr(arg2, 's') != NULL)
14073 do_fsync = TRUE;
14074 else if (vim_strchr(arg2, 'S') != NULL)
14075 do_fsync = FALSE;
14076#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 }
14078
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014079 fname = get_tv_string_chk(&argvars[1]);
14080 if (fname == NULL)
14081 return;
14082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014083 /* Always open the file in binary mode, library functions have a mind of
14084 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014085 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14086 append ? APPENDBIN : WRITEBIN)) == NULL)
14087 {
14088 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14089 ret = -1;
14090 }
14091 else
14092 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014093 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014095#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014096 else if (do_fsync)
14097 /* Ignore the error, the user wouldn't know what to do about it.
14098 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014099 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014100#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014101 fclose(fd);
14102 }
14103
14104 rettv->vval.v_number = ret;
14105}
14106
14107/*
14108 * "xor(expr, expr)" function
14109 */
14110 static void
14111f_xor(typval_T *argvars, typval_T *rettv)
14112{
14113 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14114 ^ get_tv_number_chk(&argvars[1], NULL);
14115}
14116
14117
14118#endif /* FEAT_EVAL */