blob: c9f4c4581c4311f5df95b6669a5c8049e7f6deec [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
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001124/*
1125 * Get the float value of "argvars[0]" into "f".
1126 * Returns FAIL when the argument is not a Number or Float.
1127 */
1128 static int
1129get_float_arg(typval_T *argvars, float_T *f)
1130{
1131 if (argvars[0].v_type == VAR_FLOAT)
1132 {
1133 *f = argvars[0].vval.v_float;
1134 return OK;
1135 }
1136 if (argvars[0].v_type == VAR_NUMBER)
1137 {
1138 *f = (float_T)argvars[0].vval.v_number;
1139 return OK;
1140 }
1141 EMSG(_("E808: Number or Float required"));
1142 return FAIL;
1143}
1144
1145/*
1146 * "abs(expr)" function
1147 */
1148 static void
1149f_abs(typval_T *argvars, typval_T *rettv)
1150{
1151 if (argvars[0].v_type == VAR_FLOAT)
1152 {
1153 rettv->v_type = VAR_FLOAT;
1154 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1155 }
1156 else
1157 {
1158 varnumber_T n;
1159 int error = FALSE;
1160
1161 n = get_tv_number_chk(&argvars[0], &error);
1162 if (error)
1163 rettv->vval.v_number = -1;
1164 else if (n > 0)
1165 rettv->vval.v_number = n;
1166 else
1167 rettv->vval.v_number = -n;
1168 }
1169}
1170
1171/*
1172 * "acos()" function
1173 */
1174 static void
1175f_acos(typval_T *argvars, typval_T *rettv)
1176{
1177 float_T f = 0.0;
1178
1179 rettv->v_type = VAR_FLOAT;
1180 if (get_float_arg(argvars, &f) == OK)
1181 rettv->vval.v_float = acos(f);
1182 else
1183 rettv->vval.v_float = 0.0;
1184}
1185#endif
1186
1187/*
1188 * "add(list, item)" function
1189 */
1190 static void
1191f_add(typval_T *argvars, typval_T *rettv)
1192{
1193 list_T *l;
1194
1195 rettv->vval.v_number = 1; /* Default: Failed */
1196 if (argvars[0].v_type == VAR_LIST)
1197 {
1198 if ((l = argvars[0].vval.v_list) != NULL
1199 && !tv_check_lock(l->lv_lock,
1200 (char_u *)N_("add() argument"), TRUE)
1201 && list_append_tv(l, &argvars[1]) == OK)
1202 copy_tv(&argvars[0], rettv);
1203 }
1204 else
1205 EMSG(_(e_listreq));
1206}
1207
1208/*
1209 * "and(expr, expr)" function
1210 */
1211 static void
1212f_and(typval_T *argvars, typval_T *rettv)
1213{
1214 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
1215 & get_tv_number_chk(&argvars[1], NULL);
1216}
1217
1218/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001219 * Get the lnum from the first argument.
1220 * Also accepts "$", then "buf" is used.
1221 * Returns 0 on error.
1222 */
1223 static linenr_T
1224get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
1225{
1226 if (argvars[0].v_type == VAR_STRING
1227 && argvars[0].vval.v_string != NULL
1228 && argvars[0].vval.v_string[0] == '$'
1229 && buf != NULL)
1230 return buf->b_ml.ml_line_count;
1231 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
1232}
1233
1234/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001235 * If there is a window for "curbuf", make it the current window.
1236 */
1237 static void
1238find_win_for_curbuf(void)
1239{
1240 wininfo_T *wip;
1241
1242 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1243 {
1244 if (wip->wi_win != NULL)
1245 {
1246 curwin = wip->wi_win;
1247 break;
1248 }
1249 }
1250}
1251
1252/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001253 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001254 */
1255 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001256set_buffer_lines(
1257 buf_T *buf,
1258 linenr_T lnum_arg,
1259 int append,
1260 typval_T *lines,
1261 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001262{
Bram Moolenaarca851592018-06-06 21:04:07 +02001263 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1264 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001265 list_T *l = NULL;
1266 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001268 linenr_T append_lnum;
1269 buf_T *curbuf_save = NULL;
1270 win_T *curwin_save = NULL;
1271 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272
Bram Moolenaarca851592018-06-06 21:04:07 +02001273 /* When using the current buffer ml_mfp will be set if needed. Useful when
1274 * setline() is used on startup. For other buffers the buffer must be
1275 * loaded. */
1276 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001277 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001278 rettv->vval.v_number = 1; /* FAIL */
1279 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280 }
1281
Bram Moolenaarca851592018-06-06 21:04:07 +02001282 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001283 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001284 curbuf_save = curbuf;
1285 curwin_save = curwin;
1286 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001287 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001288 }
1289
1290 if (append)
1291 // appendbufline() uses the line number below which we insert
1292 append_lnum = lnum - 1;
1293 else
1294 // setbufline() uses the line number above which we insert, we only
1295 // append if it's below the last line
1296 append_lnum = curbuf->b_ml.ml_line_count;
1297
1298 if (lines->v_type == VAR_LIST)
1299 {
1300 l = lines->vval.v_list;
1301 li = l->lv_first;
1302 }
1303 else
1304 line = get_tv_string_chk(lines);
1305
1306 /* default result is zero == OK */
1307 for (;;)
1308 {
1309 if (l != NULL)
1310 {
1311 /* list argument, get next string */
1312 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313 break;
Bram Moolenaarca851592018-06-06 21:04:07 +02001314 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001315 li = li->li_next;
1316 }
1317
Bram Moolenaarca851592018-06-06 21:04:07 +02001318 rettv->vval.v_number = 1; /* FAIL */
1319 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1320 break;
1321
1322 /* When coming here from Insert mode, sync undo, so that this can be
1323 * undone separately from what was previously inserted. */
1324 if (u_sync_once == 2)
1325 {
1326 u_sync_once = 1; /* notify that u_sync() was called */
1327 u_sync(TRUE);
1328 }
1329
1330 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1331 {
1332 /* existing line, replace it */
1333 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
1334 {
1335 changed_bytes(lnum, 0);
1336 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1337 check_cursor_col();
1338 rettv->vval.v_number = 0; /* OK */
1339 }
1340 }
1341 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1342 {
1343 /* append the line */
1344 ++added;
1345 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1346 rettv->vval.v_number = 0; /* OK */
1347 }
1348
1349 if (l == NULL) /* only one string argument */
1350 break;
1351 ++lnum;
1352 }
1353
1354 if (added > 0)
1355 {
1356 win_T *wp;
1357 tabpage_T *tp;
1358
1359 appended_lines_mark(append_lnum, added);
1360 FOR_ALL_TAB_WINDOWS(tp, wp)
1361 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1362 wp->w_cursor.lnum += added;
1363 check_cursor_col();
1364
Bram Moolenaarf2732452018-06-03 14:47:35 +02001365#ifdef FEAT_JOB_CHANNEL
1366 if (bt_prompt(curbuf) && (State & INSERT))
1367 // show the line with the prompt
1368 update_topline();
1369#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001371
1372 if (!is_curbuf)
1373 {
1374 curbuf = curbuf_save;
1375 curwin = curwin_save;
1376 }
1377}
1378
1379/*
1380 * "append(lnum, string/list)" function
1381 */
1382 static void
1383f_append(typval_T *argvars, typval_T *rettv)
1384{
1385 linenr_T lnum = get_tv_lnum(&argvars[0]);
1386
1387 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1388}
1389
1390/*
1391 * "appendbufline(buf, lnum, string/list)" function
1392 */
1393 static void
1394f_appendbufline(typval_T *argvars, typval_T *rettv)
1395{
1396 linenr_T lnum;
1397 buf_T *buf;
1398
1399 buf = get_buf_tv(&argvars[0], FALSE);
1400 if (buf == NULL)
1401 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 {
1404 lnum = get_tv_lnum_buf(&argvars[1], buf);
1405 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001407}
1408
1409/*
1410 * "argc()" function
1411 */
1412 static void
1413f_argc(typval_T *argvars UNUSED, typval_T *rettv)
1414{
1415 rettv->vval.v_number = ARGCOUNT;
1416}
1417
1418/*
1419 * "argidx()" function
1420 */
1421 static void
1422f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1423{
1424 rettv->vval.v_number = curwin->w_arg_idx;
1425}
1426
1427/*
1428 * "arglistid()" function
1429 */
1430 static void
1431f_arglistid(typval_T *argvars, typval_T *rettv)
1432{
1433 win_T *wp;
1434
1435 rettv->vval.v_number = -1;
1436 wp = find_tabwin(&argvars[0], &argvars[1]);
1437 if (wp != NULL)
1438 rettv->vval.v_number = wp->w_alist->id;
1439}
1440
1441/*
1442 * "argv(nr)" function
1443 */
1444 static void
1445f_argv(typval_T *argvars, typval_T *rettv)
1446{
1447 int idx;
1448
1449 if (argvars[0].v_type != VAR_UNKNOWN)
1450 {
1451 idx = (int)get_tv_number_chk(&argvars[0], NULL);
1452 if (idx >= 0 && idx < ARGCOUNT)
1453 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
1454 else
1455 rettv->vval.v_string = NULL;
1456 rettv->v_type = VAR_STRING;
1457 }
1458 else if (rettv_list_alloc(rettv) == OK)
1459 for (idx = 0; idx < ARGCOUNT; ++idx)
1460 list_append_string(rettv->vval.v_list,
1461 alist_name(&ARGLIST[idx]), -1);
1462}
1463
1464/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001465 * "assert_beeps(cmd [, error])" function
1466 */
1467 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001468f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001469{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001470 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001471}
1472
1473/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001474 * "assert_equal(expected, actual[, msg])" function
1475 */
1476 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001477f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001478{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001479 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480}
1481
1482/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001483 * "assert_equalfile(fname-one, fname-two)" function
1484 */
1485 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001486f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001487{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001488 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001489}
1490
1491/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492 * "assert_notequal(expected, actual[, msg])" function
1493 */
1494 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001495f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001496{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001497 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001498}
1499
1500/*
1501 * "assert_exception(string[, msg])" function
1502 */
1503 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001504f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001505{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001506 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507}
1508
1509/*
1510 * "assert_fails(cmd [, error])" function
1511 */
1512 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001513f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001515 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001516}
1517
1518/*
1519 * "assert_false(actual[, msg])" function
1520 */
1521 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001522f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001524 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001525}
1526
1527/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001528 * "assert_inrange(lower, upper[, msg])" function
1529 */
1530 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001531f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001532{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001533 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001534}
1535
1536/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001537 * "assert_match(pattern, actual[, msg])" function
1538 */
1539 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001540f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001542 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001543}
1544
1545/*
1546 * "assert_notmatch(pattern, actual[, msg])" function
1547 */
1548 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001549f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001550{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001551 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001552}
1553
1554/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001555 * "assert_report(msg)" function
1556 */
1557 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001558f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001559{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001560 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001561}
1562
1563/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564 * "assert_true(actual[, msg])" function
1565 */
1566 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001567f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001569 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001570}
1571
1572#ifdef FEAT_FLOAT
1573/*
1574 * "asin()" function
1575 */
1576 static void
1577f_asin(typval_T *argvars, typval_T *rettv)
1578{
1579 float_T f = 0.0;
1580
1581 rettv->v_type = VAR_FLOAT;
1582 if (get_float_arg(argvars, &f) == OK)
1583 rettv->vval.v_float = asin(f);
1584 else
1585 rettv->vval.v_float = 0.0;
1586}
1587
1588/*
1589 * "atan()" function
1590 */
1591 static void
1592f_atan(typval_T *argvars, typval_T *rettv)
1593{
1594 float_T f = 0.0;
1595
1596 rettv->v_type = VAR_FLOAT;
1597 if (get_float_arg(argvars, &f) == OK)
1598 rettv->vval.v_float = atan(f);
1599 else
1600 rettv->vval.v_float = 0.0;
1601}
1602
1603/*
1604 * "atan2()" function
1605 */
1606 static void
1607f_atan2(typval_T *argvars, typval_T *rettv)
1608{
1609 float_T fx = 0.0, fy = 0.0;
1610
1611 rettv->v_type = VAR_FLOAT;
1612 if (get_float_arg(argvars, &fx) == OK
1613 && get_float_arg(&argvars[1], &fy) == OK)
1614 rettv->vval.v_float = atan2(fx, fy);
1615 else
1616 rettv->vval.v_float = 0.0;
1617}
1618#endif
1619
1620/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001621 * "balloon_show()" function
1622 */
1623#ifdef FEAT_BEVAL
1624 static void
1625f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1626{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001627 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001628 {
1629 if (argvars[0].v_type == VAR_LIST
1630# ifdef FEAT_GUI
1631 && !gui.in_use
1632# endif
1633 )
1634 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1635 else
1636 post_balloon(balloonEval, get_tv_string_chk(&argvars[0]), NULL);
1637 }
1638}
1639
Bram Moolenaar669a8282017-11-19 20:13:05 +01001640# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001641 static void
1642f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1643{
1644 if (rettv_list_alloc(rettv) == OK)
1645 {
1646 char_u *msg = get_tv_string_chk(&argvars[0]);
1647
1648 if (msg != NULL)
1649 {
1650 pumitem_T *array;
1651 int size = split_message(msg, &array);
1652 int i;
1653
1654 /* Skip the first and last item, they are always empty. */
1655 for (i = 1; i < size - 1; ++i)
1656 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001657 while (size > 0)
1658 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001659 vim_free(array);
1660 }
1661 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001662}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001663# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001664#endif
1665
1666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667 * "browse(save, title, initdir, default)" function
1668 */
1669 static void
1670f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1671{
1672#ifdef FEAT_BROWSE
1673 int save;
1674 char_u *title;
1675 char_u *initdir;
1676 char_u *defname;
1677 char_u buf[NUMBUFLEN];
1678 char_u buf2[NUMBUFLEN];
1679 int error = FALSE;
1680
1681 save = (int)get_tv_number_chk(&argvars[0], &error);
1682 title = get_tv_string_chk(&argvars[1]);
1683 initdir = get_tv_string_buf_chk(&argvars[2], buf);
1684 defname = get_tv_string_buf_chk(&argvars[3], buf2);
1685
1686 if (error || title == NULL || initdir == NULL || defname == NULL)
1687 rettv->vval.v_string = NULL;
1688 else
1689 rettv->vval.v_string =
1690 do_browse(save ? BROWSE_SAVE : 0,
1691 title, defname, NULL, initdir, NULL, curbuf);
1692#else
1693 rettv->vval.v_string = NULL;
1694#endif
1695 rettv->v_type = VAR_STRING;
1696}
1697
1698/*
1699 * "browsedir(title, initdir)" function
1700 */
1701 static void
1702f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1703{
1704#ifdef FEAT_BROWSE
1705 char_u *title;
1706 char_u *initdir;
1707 char_u buf[NUMBUFLEN];
1708
1709 title = get_tv_string_chk(&argvars[0]);
1710 initdir = get_tv_string_buf_chk(&argvars[1], buf);
1711
1712 if (title == NULL || initdir == NULL)
1713 rettv->vval.v_string = NULL;
1714 else
1715 rettv->vval.v_string = do_browse(BROWSE_DIR,
1716 title, NULL, NULL, initdir, NULL, curbuf);
1717#else
1718 rettv->vval.v_string = NULL;
1719#endif
1720 rettv->v_type = VAR_STRING;
1721}
1722
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723/*
1724 * Find a buffer by number or exact name.
1725 */
1726 static buf_T *
1727find_buffer(typval_T *avar)
1728{
1729 buf_T *buf = NULL;
1730
1731 if (avar->v_type == VAR_NUMBER)
1732 buf = buflist_findnr((int)avar->vval.v_number);
1733 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1734 {
1735 buf = buflist_findname_exp(avar->vval.v_string);
1736 if (buf == NULL)
1737 {
1738 /* No full path name match, try a match with a URL or a "nofile"
1739 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001740 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001741 if (buf->b_fname != NULL
1742 && (path_with_url(buf->b_fname)
1743#ifdef FEAT_QUICKFIX
1744 || bt_nofile(buf)
1745#endif
1746 )
1747 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1748 break;
1749 }
1750 }
1751 return buf;
1752}
1753
1754/*
1755 * "bufexists(expr)" function
1756 */
1757 static void
1758f_bufexists(typval_T *argvars, typval_T *rettv)
1759{
1760 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1761}
1762
1763/*
1764 * "buflisted(expr)" function
1765 */
1766 static void
1767f_buflisted(typval_T *argvars, typval_T *rettv)
1768{
1769 buf_T *buf;
1770
1771 buf = find_buffer(&argvars[0]);
1772 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1773}
1774
1775/*
1776 * "bufloaded(expr)" function
1777 */
1778 static void
1779f_bufloaded(typval_T *argvars, typval_T *rettv)
1780{
1781 buf_T *buf;
1782
1783 buf = find_buffer(&argvars[0]);
1784 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1785}
1786
1787 buf_T *
1788buflist_find_by_name(char_u *name, int curtab_only)
1789{
1790 int save_magic;
1791 char_u *save_cpo;
1792 buf_T *buf;
1793
1794 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1795 save_magic = p_magic;
1796 p_magic = TRUE;
1797 save_cpo = p_cpo;
1798 p_cpo = (char_u *)"";
1799
1800 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1801 TRUE, FALSE, curtab_only));
1802
1803 p_magic = save_magic;
1804 p_cpo = save_cpo;
1805 return buf;
1806}
1807
1808/*
1809 * Get buffer by number or pattern.
1810 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001811 buf_T *
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001812get_buf_tv(typval_T *tv, int curtab_only)
1813{
1814 char_u *name = tv->vval.v_string;
1815 buf_T *buf;
1816
1817 if (tv->v_type == VAR_NUMBER)
1818 return buflist_findnr((int)tv->vval.v_number);
1819 if (tv->v_type != VAR_STRING)
1820 return NULL;
1821 if (name == NULL || *name == NUL)
1822 return curbuf;
1823 if (name[0] == '$' && name[1] == NUL)
1824 return lastbuf;
1825
1826 buf = buflist_find_by_name(name, curtab_only);
1827
1828 /* If not found, try expanding the name, like done for bufexists(). */
1829 if (buf == NULL)
1830 buf = find_buffer(tv);
1831
1832 return buf;
1833}
1834
1835/*
1836 * "bufname(expr)" function
1837 */
1838 static void
1839f_bufname(typval_T *argvars, typval_T *rettv)
1840{
1841 buf_T *buf;
1842
1843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1844 ++emsg_off;
1845 buf = get_buf_tv(&argvars[0], FALSE);
1846 rettv->v_type = VAR_STRING;
1847 if (buf != NULL && buf->b_fname != NULL)
1848 rettv->vval.v_string = vim_strsave(buf->b_fname);
1849 else
1850 rettv->vval.v_string = NULL;
1851 --emsg_off;
1852}
1853
1854/*
1855 * "bufnr(expr)" function
1856 */
1857 static void
1858f_bufnr(typval_T *argvars, typval_T *rettv)
1859{
1860 buf_T *buf;
1861 int error = FALSE;
1862 char_u *name;
1863
1864 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1865 ++emsg_off;
1866 buf = get_buf_tv(&argvars[0], FALSE);
1867 --emsg_off;
1868
1869 /* If the buffer isn't found and the second argument is not zero create a
1870 * new buffer. */
1871 if (buf == NULL
1872 && argvars[1].v_type != VAR_UNKNOWN
1873 && get_tv_number_chk(&argvars[1], &error) != 0
1874 && !error
1875 && (name = get_tv_string_chk(&argvars[0])) != NULL
1876 && !error)
1877 buf = buflist_new(name, NULL, (linenr_T)1, 0);
1878
1879 if (buf != NULL)
1880 rettv->vval.v_number = buf->b_fnum;
1881 else
1882 rettv->vval.v_number = -1;
1883}
1884
1885 static void
1886buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
1887{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001888 win_T *wp;
1889 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890 buf_T *buf;
1891
1892 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1893 ++emsg_off;
1894 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02001895 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001896 {
1897 ++winnr;
1898 if (wp->w_buffer == buf)
1899 break;
1900 }
1901 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001902 --emsg_off;
1903}
1904
1905/*
1906 * "bufwinid(nr)" function
1907 */
1908 static void
1909f_bufwinid(typval_T *argvars, typval_T *rettv)
1910{
1911 buf_win_common(argvars, rettv, FALSE);
1912}
1913
1914/*
1915 * "bufwinnr(nr)" function
1916 */
1917 static void
1918f_bufwinnr(typval_T *argvars, typval_T *rettv)
1919{
1920 buf_win_common(argvars, rettv, TRUE);
1921}
1922
1923/*
1924 * "byte2line(byte)" function
1925 */
1926 static void
1927f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
1928{
1929#ifndef FEAT_BYTEOFF
1930 rettv->vval.v_number = -1;
1931#else
1932 long boff = 0;
1933
1934 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
1935 if (boff < 0)
1936 rettv->vval.v_number = -1;
1937 else
1938 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
1939 (linenr_T)0, &boff);
1940#endif
1941}
1942
1943 static void
1944byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
1945{
1946#ifdef FEAT_MBYTE
1947 char_u *t;
1948#endif
1949 char_u *str;
1950 varnumber_T idx;
1951
1952 str = get_tv_string_chk(&argvars[0]);
1953 idx = get_tv_number_chk(&argvars[1], NULL);
1954 rettv->vval.v_number = -1;
1955 if (str == NULL || idx < 0)
1956 return;
1957
1958#ifdef FEAT_MBYTE
1959 t = str;
1960 for ( ; idx > 0; idx--)
1961 {
1962 if (*t == NUL) /* EOL reached */
1963 return;
1964 if (enc_utf8 && comp)
1965 t += utf_ptr2len(t);
1966 else
1967 t += (*mb_ptr2len)(t);
1968 }
1969 rettv->vval.v_number = (varnumber_T)(t - str);
1970#else
1971 if ((size_t)idx <= STRLEN(str))
1972 rettv->vval.v_number = idx;
1973#endif
1974}
1975
1976/*
1977 * "byteidx()" function
1978 */
1979 static void
1980f_byteidx(typval_T *argvars, typval_T *rettv)
1981{
1982 byteidx(argvars, rettv, FALSE);
1983}
1984
1985/*
1986 * "byteidxcomp()" function
1987 */
1988 static void
1989f_byteidxcomp(typval_T *argvars, typval_T *rettv)
1990{
1991 byteidx(argvars, rettv, TRUE);
1992}
1993
1994/*
1995 * "call(func, arglist [, dict])" function
1996 */
1997 static void
1998f_call(typval_T *argvars, typval_T *rettv)
1999{
2000 char_u *func;
2001 partial_T *partial = NULL;
2002 dict_T *selfdict = NULL;
2003
2004 if (argvars[1].v_type != VAR_LIST)
2005 {
2006 EMSG(_(e_listreq));
2007 return;
2008 }
2009 if (argvars[1].vval.v_list == NULL)
2010 return;
2011
2012 if (argvars[0].v_type == VAR_FUNC)
2013 func = argvars[0].vval.v_string;
2014 else if (argvars[0].v_type == VAR_PARTIAL)
2015 {
2016 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002017 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 }
2019 else
2020 func = get_tv_string(&argvars[0]);
2021 if (*func == NUL)
2022 return; /* type error or empty name */
2023
2024 if (argvars[2].v_type != VAR_UNKNOWN)
2025 {
2026 if (argvars[2].v_type != VAR_DICT)
2027 {
2028 EMSG(_(e_dictreq));
2029 return;
2030 }
2031 selfdict = argvars[2].vval.v_dict;
2032 }
2033
2034 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2035}
2036
2037#ifdef FEAT_FLOAT
2038/*
2039 * "ceil({float})" function
2040 */
2041 static void
2042f_ceil(typval_T *argvars, typval_T *rettv)
2043{
2044 float_T f = 0.0;
2045
2046 rettv->v_type = VAR_FLOAT;
2047 if (get_float_arg(argvars, &f) == OK)
2048 rettv->vval.v_float = ceil(f);
2049 else
2050 rettv->vval.v_float = 0.0;
2051}
2052#endif
2053
2054#ifdef FEAT_JOB_CHANNEL
2055/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002056 * "ch_canread()" function
2057 */
2058 static void
2059f_ch_canread(typval_T *argvars, typval_T *rettv)
2060{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002061 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002062
2063 rettv->vval.v_number = 0;
2064 if (channel != NULL)
2065 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2066 || channel_has_readahead(channel, PART_OUT)
2067 || channel_has_readahead(channel, PART_ERR);
2068}
2069
2070/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 * "ch_close()" function
2072 */
2073 static void
2074f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2075{
2076 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2077
2078 if (channel != NULL)
2079 {
2080 channel_close(channel, FALSE);
2081 channel_clear(channel);
2082 }
2083}
2084
2085/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002086 * "ch_close()" function
2087 */
2088 static void
2089f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2090{
2091 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2092
2093 if (channel != NULL)
2094 channel_close_in(channel);
2095}
2096
2097/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 * "ch_getbufnr()" function
2099 */
2100 static void
2101f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2102{
2103 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2104
2105 rettv->vval.v_number = -1;
2106 if (channel != NULL)
2107 {
2108 char_u *what = get_tv_string(&argvars[1]);
2109 int part;
2110
2111 if (STRCMP(what, "err") == 0)
2112 part = PART_ERR;
2113 else if (STRCMP(what, "out") == 0)
2114 part = PART_OUT;
2115 else if (STRCMP(what, "in") == 0)
2116 part = PART_IN;
2117 else
2118 part = PART_SOCK;
2119 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2120 rettv->vval.v_number =
2121 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2122 }
2123}
2124
2125/*
2126 * "ch_getjob()" function
2127 */
2128 static void
2129f_ch_getjob(typval_T *argvars, typval_T *rettv)
2130{
2131 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2132
2133 if (channel != NULL)
2134 {
2135 rettv->v_type = VAR_JOB;
2136 rettv->vval.v_job = channel->ch_job;
2137 if (channel->ch_job != NULL)
2138 ++channel->ch_job->jv_refcount;
2139 }
2140}
2141
2142/*
2143 * "ch_info()" function
2144 */
2145 static void
2146f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2147{
2148 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2149
2150 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2151 channel_info(channel, rettv->vval.v_dict);
2152}
2153
2154/*
2155 * "ch_log()" function
2156 */
2157 static void
2158f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2159{
2160 char_u *msg = get_tv_string(&argvars[0]);
2161 channel_T *channel = NULL;
2162
2163 if (argvars[1].v_type != VAR_UNKNOWN)
2164 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2165
Bram Moolenaard5359b22018-04-05 22:44:39 +02002166 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002167}
2168
2169/*
2170 * "ch_logfile()" function
2171 */
2172 static void
2173f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2174{
2175 char_u *fname;
2176 char_u *opt = (char_u *)"";
2177 char_u buf[NUMBUFLEN];
2178
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002179 /* Don't open a file in restricted mode. */
2180 if (check_restricted() || check_secure())
2181 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002182 fname = get_tv_string(&argvars[0]);
2183 if (argvars[1].v_type == VAR_STRING)
2184 opt = get_tv_string_buf(&argvars[1], buf);
2185 ch_logfile(fname, opt);
2186}
2187
2188/*
2189 * "ch_open()" function
2190 */
2191 static void
2192f_ch_open(typval_T *argvars, typval_T *rettv)
2193{
2194 rettv->v_type = VAR_CHANNEL;
2195 if (check_restricted() || check_secure())
2196 return;
2197 rettv->vval.v_channel = channel_open_func(argvars);
2198}
2199
2200/*
2201 * "ch_read()" function
2202 */
2203 static void
2204f_ch_read(typval_T *argvars, typval_T *rettv)
2205{
2206 common_channel_read(argvars, rettv, FALSE);
2207}
2208
2209/*
2210 * "ch_readraw()" function
2211 */
2212 static void
2213f_ch_readraw(typval_T *argvars, typval_T *rettv)
2214{
2215 common_channel_read(argvars, rettv, TRUE);
2216}
2217
2218/*
2219 * "ch_evalexpr()" function
2220 */
2221 static void
2222f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2223{
2224 ch_expr_common(argvars, rettv, TRUE);
2225}
2226
2227/*
2228 * "ch_sendexpr()" function
2229 */
2230 static void
2231f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2232{
2233 ch_expr_common(argvars, rettv, FALSE);
2234}
2235
2236/*
2237 * "ch_evalraw()" function
2238 */
2239 static void
2240f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2241{
2242 ch_raw_common(argvars, rettv, TRUE);
2243}
2244
2245/*
2246 * "ch_sendraw()" function
2247 */
2248 static void
2249f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2250{
2251 ch_raw_common(argvars, rettv, FALSE);
2252}
2253
2254/*
2255 * "ch_setoptions()" function
2256 */
2257 static void
2258f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2259{
2260 channel_T *channel;
2261 jobopt_T opt;
2262
2263 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2264 if (channel == NULL)
2265 return;
2266 clear_job_options(&opt);
2267 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002268 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002269 channel_set_options(channel, &opt);
2270 free_job_options(&opt);
2271}
2272
2273/*
2274 * "ch_status()" function
2275 */
2276 static void
2277f_ch_status(typval_T *argvars, typval_T *rettv)
2278{
2279 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002280 jobopt_T opt;
2281 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282
2283 /* return an empty string by default */
2284 rettv->v_type = VAR_STRING;
2285 rettv->vval.v_string = NULL;
2286
2287 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002288
2289 if (argvars[1].v_type != VAR_UNKNOWN)
2290 {
2291 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002292 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002293 && (opt.jo_set & JO_PART))
2294 part = opt.jo_part;
2295 }
2296
2297 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298}
2299#endif
2300
2301/*
2302 * "changenr()" function
2303 */
2304 static void
2305f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2306{
2307 rettv->vval.v_number = curbuf->b_u_seq_cur;
2308}
2309
2310/*
2311 * "char2nr(string)" function
2312 */
2313 static void
2314f_char2nr(typval_T *argvars, typval_T *rettv)
2315{
2316#ifdef FEAT_MBYTE
2317 if (has_mbyte)
2318 {
2319 int utf8 = 0;
2320
2321 if (argvars[1].v_type != VAR_UNKNOWN)
2322 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
2323
2324 if (utf8)
2325 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
2326 else
2327 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
2328 }
2329 else
2330#endif
2331 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
2332}
2333
2334/*
2335 * "cindent(lnum)" function
2336 */
2337 static void
2338f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2339{
2340#ifdef FEAT_CINDENT
2341 pos_T pos;
2342 linenr_T lnum;
2343
2344 pos = curwin->w_cursor;
2345 lnum = get_tv_lnum(argvars);
2346 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2347 {
2348 curwin->w_cursor.lnum = lnum;
2349 rettv->vval.v_number = get_c_indent();
2350 curwin->w_cursor = pos;
2351 }
2352 else
2353#endif
2354 rettv->vval.v_number = -1;
2355}
2356
2357/*
2358 * "clearmatches()" function
2359 */
2360 static void
2361f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2362{
2363#ifdef FEAT_SEARCH_EXTRA
2364 clear_matches(curwin);
2365#endif
2366}
2367
2368/*
2369 * "col(string)" function
2370 */
2371 static void
2372f_col(typval_T *argvars, typval_T *rettv)
2373{
2374 colnr_T col = 0;
2375 pos_T *fp;
2376 int fnum = curbuf->b_fnum;
2377
2378 fp = var2fpos(&argvars[0], FALSE, &fnum);
2379 if (fp != NULL && fnum == curbuf->b_fnum)
2380 {
2381 if (fp->col == MAXCOL)
2382 {
2383 /* '> can be MAXCOL, get the length of the line then */
2384 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2385 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2386 else
2387 col = MAXCOL;
2388 }
2389 else
2390 {
2391 col = fp->col + 1;
2392#ifdef FEAT_VIRTUALEDIT
2393 /* col(".") when the cursor is on the NUL at the end of the line
2394 * because of "coladd" can be seen as an extra column. */
2395 if (virtual_active() && fp == &curwin->w_cursor)
2396 {
2397 char_u *p = ml_get_cursor();
2398
2399 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2400 curwin->w_virtcol - curwin->w_cursor.coladd))
2401 {
2402# ifdef FEAT_MBYTE
2403 int l;
2404
2405 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2406 col += l;
2407# else
2408 if (*p != NUL && p[1] == NUL)
2409 ++col;
2410# endif
2411 }
2412 }
2413#endif
2414 }
2415 }
2416 rettv->vval.v_number = col;
2417}
2418
2419#if defined(FEAT_INS_EXPAND)
2420/*
2421 * "complete()" function
2422 */
2423 static void
2424f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2425{
2426 int startcol;
2427
2428 if ((State & INSERT) == 0)
2429 {
2430 EMSG(_("E785: complete() can only be used in Insert mode"));
2431 return;
2432 }
2433
2434 /* Check for undo allowed here, because if something was already inserted
2435 * the line was already saved for undo and this check isn't done. */
2436 if (!undo_allowed())
2437 return;
2438
2439 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2440 {
2441 EMSG(_(e_invarg));
2442 return;
2443 }
2444
2445 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
2446 if (startcol <= 0)
2447 return;
2448
2449 set_completion(startcol - 1, argvars[1].vval.v_list);
2450}
2451
2452/*
2453 * "complete_add()" function
2454 */
2455 static void
2456f_complete_add(typval_T *argvars, typval_T *rettv)
2457{
2458 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2459}
2460
2461/*
2462 * "complete_check()" function
2463 */
2464 static void
2465f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2466{
2467 int saved = RedrawingDisabled;
2468
2469 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002470 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002471 rettv->vval.v_number = compl_interrupted;
2472 RedrawingDisabled = saved;
2473}
2474#endif
2475
2476/*
2477 * "confirm(message, buttons[, default [, type]])" function
2478 */
2479 static void
2480f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2481{
2482#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2483 char_u *message;
2484 char_u *buttons = NULL;
2485 char_u buf[NUMBUFLEN];
2486 char_u buf2[NUMBUFLEN];
2487 int def = 1;
2488 int type = VIM_GENERIC;
2489 char_u *typestr;
2490 int error = FALSE;
2491
2492 message = get_tv_string_chk(&argvars[0]);
2493 if (message == NULL)
2494 error = TRUE;
2495 if (argvars[1].v_type != VAR_UNKNOWN)
2496 {
2497 buttons = get_tv_string_buf_chk(&argvars[1], buf);
2498 if (buttons == NULL)
2499 error = TRUE;
2500 if (argvars[2].v_type != VAR_UNKNOWN)
2501 {
2502 def = (int)get_tv_number_chk(&argvars[2], &error);
2503 if (argvars[3].v_type != VAR_UNKNOWN)
2504 {
2505 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
2506 if (typestr == NULL)
2507 error = TRUE;
2508 else
2509 {
2510 switch (TOUPPER_ASC(*typestr))
2511 {
2512 case 'E': type = VIM_ERROR; break;
2513 case 'Q': type = VIM_QUESTION; break;
2514 case 'I': type = VIM_INFO; break;
2515 case 'W': type = VIM_WARNING; break;
2516 case 'G': type = VIM_GENERIC; break;
2517 }
2518 }
2519 }
2520 }
2521 }
2522
2523 if (buttons == NULL || *buttons == NUL)
2524 buttons = (char_u *)_("&Ok");
2525
2526 if (!error)
2527 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2528 def, NULL, FALSE);
2529#endif
2530}
2531
2532/*
2533 * "copy()" function
2534 */
2535 static void
2536f_copy(typval_T *argvars, typval_T *rettv)
2537{
2538 item_copy(&argvars[0], rettv, FALSE, 0);
2539}
2540
2541#ifdef FEAT_FLOAT
2542/*
2543 * "cos()" function
2544 */
2545 static void
2546f_cos(typval_T *argvars, typval_T *rettv)
2547{
2548 float_T f = 0.0;
2549
2550 rettv->v_type = VAR_FLOAT;
2551 if (get_float_arg(argvars, &f) == OK)
2552 rettv->vval.v_float = cos(f);
2553 else
2554 rettv->vval.v_float = 0.0;
2555}
2556
2557/*
2558 * "cosh()" function
2559 */
2560 static void
2561f_cosh(typval_T *argvars, typval_T *rettv)
2562{
2563 float_T f = 0.0;
2564
2565 rettv->v_type = VAR_FLOAT;
2566 if (get_float_arg(argvars, &f) == OK)
2567 rettv->vval.v_float = cosh(f);
2568 else
2569 rettv->vval.v_float = 0.0;
2570}
2571#endif
2572
2573/*
2574 * "count()" function
2575 */
2576 static void
2577f_count(typval_T *argvars, typval_T *rettv)
2578{
2579 long n = 0;
2580 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002581 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582
Bram Moolenaar9966b212017-07-28 16:46:57 +02002583 if (argvars[2].v_type != VAR_UNKNOWN)
2584 ic = (int)get_tv_number_chk(&argvars[2], &error);
2585
2586 if (argvars[0].v_type == VAR_STRING)
2587 {
2588 char_u *expr = get_tv_string_chk(&argvars[1]);
2589 char_u *p = argvars[0].vval.v_string;
2590 char_u *next;
2591
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002592 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002593 {
2594 if (ic)
2595 {
2596 size_t len = STRLEN(expr);
2597
2598 while (*p != NUL)
2599 {
2600 if (MB_STRNICMP(p, expr, len) == 0)
2601 {
2602 ++n;
2603 p += len;
2604 }
2605 else
2606 MB_PTR_ADV(p);
2607 }
2608 }
2609 else
2610 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2611 != NULL)
2612 {
2613 ++n;
2614 p = next + STRLEN(expr);
2615 }
2616 }
2617
2618 }
2619 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620 {
2621 listitem_T *li;
2622 list_T *l;
2623 long idx;
2624
2625 if ((l = argvars[0].vval.v_list) != NULL)
2626 {
2627 li = l->lv_first;
2628 if (argvars[2].v_type != VAR_UNKNOWN)
2629 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 if (argvars[3].v_type != VAR_UNKNOWN)
2631 {
2632 idx = (long)get_tv_number_chk(&argvars[3], &error);
2633 if (!error)
2634 {
2635 li = list_find(l, idx);
2636 if (li == NULL)
2637 EMSGN(_(e_listidx), idx);
2638 }
2639 }
2640 if (error)
2641 li = NULL;
2642 }
2643
2644 for ( ; li != NULL; li = li->li_next)
2645 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2646 ++n;
2647 }
2648 }
2649 else if (argvars[0].v_type == VAR_DICT)
2650 {
2651 int todo;
2652 dict_T *d;
2653 hashitem_T *hi;
2654
2655 if ((d = argvars[0].vval.v_dict) != NULL)
2656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 if (argvars[2].v_type != VAR_UNKNOWN)
2658 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002659 if (argvars[3].v_type != VAR_UNKNOWN)
2660 EMSG(_(e_invarg));
2661 }
2662
2663 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2664 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2665 {
2666 if (!HASHITEM_EMPTY(hi))
2667 {
2668 --todo;
2669 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2670 ++n;
2671 }
2672 }
2673 }
2674 }
2675 else
2676 EMSG2(_(e_listdictarg), "count()");
2677 rettv->vval.v_number = n;
2678}
2679
2680/*
2681 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2682 *
2683 * Checks the existence of a cscope connection.
2684 */
2685 static void
2686f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2687{
2688#ifdef FEAT_CSCOPE
2689 int num = 0;
2690 char_u *dbpath = NULL;
2691 char_u *prepend = NULL;
2692 char_u buf[NUMBUFLEN];
2693
2694 if (argvars[0].v_type != VAR_UNKNOWN
2695 && argvars[1].v_type != VAR_UNKNOWN)
2696 {
2697 num = (int)get_tv_number(&argvars[0]);
2698 dbpath = get_tv_string(&argvars[1]);
2699 if (argvars[2].v_type != VAR_UNKNOWN)
2700 prepend = get_tv_string_buf(&argvars[2], buf);
2701 }
2702
2703 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2704#endif
2705}
2706
2707/*
2708 * "cursor(lnum, col)" function, or
2709 * "cursor(list)"
2710 *
2711 * Moves the cursor to the specified line and column.
2712 * Returns 0 when the position could be set, -1 otherwise.
2713 */
2714 static void
2715f_cursor(typval_T *argvars, typval_T *rettv)
2716{
2717 long line, col;
2718#ifdef FEAT_VIRTUALEDIT
2719 long coladd = 0;
2720#endif
2721 int set_curswant = TRUE;
2722
2723 rettv->vval.v_number = -1;
2724 if (argvars[1].v_type == VAR_UNKNOWN)
2725 {
2726 pos_T pos;
2727 colnr_T curswant = -1;
2728
2729 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2730 {
2731 EMSG(_(e_invarg));
2732 return;
2733 }
2734 line = pos.lnum;
2735 col = pos.col;
2736#ifdef FEAT_VIRTUALEDIT
2737 coladd = pos.coladd;
2738#endif
2739 if (curswant >= 0)
2740 {
2741 curwin->w_curswant = curswant - 1;
2742 set_curswant = FALSE;
2743 }
2744 }
2745 else
2746 {
2747 line = get_tv_lnum(argvars);
2748 col = (long)get_tv_number_chk(&argvars[1], NULL);
2749#ifdef FEAT_VIRTUALEDIT
2750 if (argvars[2].v_type != VAR_UNKNOWN)
2751 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
2752#endif
2753 }
2754 if (line < 0 || col < 0
2755#ifdef FEAT_VIRTUALEDIT
2756 || coladd < 0
2757#endif
2758 )
2759 return; /* type error; errmsg already given */
2760 if (line > 0)
2761 curwin->w_cursor.lnum = line;
2762 if (col > 0)
2763 curwin->w_cursor.col = col - 1;
2764#ifdef FEAT_VIRTUALEDIT
2765 curwin->w_cursor.coladd = coladd;
2766#endif
2767
2768 /* Make sure the cursor is in a valid position. */
2769 check_cursor();
2770#ifdef FEAT_MBYTE
2771 /* Correct cursor for multi-byte character. */
2772 if (has_mbyte)
2773 mb_adjust_cursor();
2774#endif
2775
2776 curwin->w_set_curswant = set_curswant;
2777 rettv->vval.v_number = 0;
2778}
2779
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002780#ifdef WIN3264
2781/*
2782 * "debugbreak()" function
2783 */
2784 static void
2785f_debugbreak(typval_T *argvars, typval_T *rettv)
2786{
2787 int pid;
2788
2789 rettv->vval.v_number = FAIL;
2790 pid = (int)get_tv_number(&argvars[0]);
2791 if (pid == 0)
2792 EMSG(_(e_invarg));
2793 else
2794 {
2795 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2796
2797 if (hProcess != NULL)
2798 {
2799 DebugBreakProcess(hProcess);
2800 CloseHandle(hProcess);
2801 rettv->vval.v_number = OK;
2802 }
2803 }
2804}
2805#endif
2806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002807/*
2808 * "deepcopy()" function
2809 */
2810 static void
2811f_deepcopy(typval_T *argvars, typval_T *rettv)
2812{
2813 int noref = 0;
2814 int copyID;
2815
2816 if (argvars[1].v_type != VAR_UNKNOWN)
2817 noref = (int)get_tv_number_chk(&argvars[1], NULL);
2818 if (noref < 0 || noref > 1)
2819 EMSG(_(e_invarg));
2820 else
2821 {
2822 copyID = get_copyID();
2823 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2824 }
2825}
2826
2827/*
2828 * "delete()" function
2829 */
2830 static void
2831f_delete(typval_T *argvars, typval_T *rettv)
2832{
2833 char_u nbuf[NUMBUFLEN];
2834 char_u *name;
2835 char_u *flags;
2836
2837 rettv->vval.v_number = -1;
2838 if (check_restricted() || check_secure())
2839 return;
2840
2841 name = get_tv_string(&argvars[0]);
2842 if (name == NULL || *name == NUL)
2843 {
2844 EMSG(_(e_invarg));
2845 return;
2846 }
2847
2848 if (argvars[1].v_type != VAR_UNKNOWN)
2849 flags = get_tv_string_buf(&argvars[1], nbuf);
2850 else
2851 flags = (char_u *)"";
2852
2853 if (*flags == NUL)
2854 /* delete a file */
2855 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2856 else if (STRCMP(flags, "d") == 0)
2857 /* delete an empty directory */
2858 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2859 else if (STRCMP(flags, "rf") == 0)
2860 /* delete a directory recursively */
2861 rettv->vval.v_number = delete_recursive(name);
2862 else
2863 EMSG2(_(e_invexpr2), flags);
2864}
2865
2866/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002867 * "deletebufline()" function
2868 */
2869 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002870f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002871{
2872 buf_T *buf;
2873 linenr_T first, last;
2874 linenr_T lnum;
2875 long count;
2876 int is_curbuf;
2877 buf_T *curbuf_save = NULL;
2878 win_T *curwin_save = NULL;
2879 tabpage_T *tp;
2880 win_T *wp;
2881
2882 buf = get_buf_tv(&argvars[0], FALSE);
2883 if (buf == NULL)
2884 {
2885 rettv->vval.v_number = 1; /* FAIL */
2886 return;
2887 }
2888 is_curbuf = buf == curbuf;
2889
2890 first = get_tv_lnum_buf(&argvars[1], buf);
2891 if (argvars[2].v_type != VAR_UNKNOWN)
2892 last = get_tv_lnum_buf(&argvars[2], buf);
2893 else
2894 last = first;
2895
2896 if (buf->b_ml.ml_mfp == NULL || first < 1
2897 || first > buf->b_ml.ml_line_count || last < first)
2898 {
2899 rettv->vval.v_number = 1; /* FAIL */
2900 return;
2901 }
2902
2903 if (!is_curbuf)
2904 {
2905 curbuf_save = curbuf;
2906 curwin_save = curwin;
2907 curbuf = buf;
2908 find_win_for_curbuf();
2909 }
2910 if (last > curbuf->b_ml.ml_line_count)
2911 last = curbuf->b_ml.ml_line_count;
2912 count = last - first + 1;
2913
2914 // When coming here from Insert mode, sync undo, so that this can be
2915 // undone separately from what was previously inserted.
2916 if (u_sync_once == 2)
2917 {
2918 u_sync_once = 1; // notify that u_sync() was called
2919 u_sync(TRUE);
2920 }
2921
2922 if (u_save(first - 1, last + 1) == FAIL)
2923 {
2924 rettv->vval.v_number = 1; /* FAIL */
2925 return;
2926 }
2927
2928 for (lnum = first; lnum <= last; ++lnum)
2929 ml_delete(first, TRUE);
2930
2931 FOR_ALL_TAB_WINDOWS(tp, wp)
2932 if (wp->w_buffer == buf)
2933 {
2934 if (wp->w_cursor.lnum > last)
2935 wp->w_cursor.lnum -= count;
2936 else if (wp->w_cursor.lnum> first)
2937 wp->w_cursor.lnum = first;
2938 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
2939 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
2940 }
2941 check_cursor_col();
2942 deleted_lines_mark(first, count);
2943
2944 if (!is_curbuf)
2945 {
2946 curbuf = curbuf_save;
2947 curwin = curwin_save;
2948 }
2949}
2950
2951/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 * "did_filetype()" function
2953 */
2954 static void
2955f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2956{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958}
2959
2960/*
2961 * "diff_filler()" function
2962 */
2963 static void
2964f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2965{
2966#ifdef FEAT_DIFF
2967 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
2968#endif
2969}
2970
2971/*
2972 * "diff_hlID()" function
2973 */
2974 static void
2975f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2976{
2977#ifdef FEAT_DIFF
2978 linenr_T lnum = get_tv_lnum(argvars);
2979 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01002980 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 static int fnum = 0;
2982 static int change_start = 0;
2983 static int change_end = 0;
2984 static hlf_T hlID = (hlf_T)0;
2985 int filler_lines;
2986 int col;
2987
2988 if (lnum < 0) /* ignore type error in {lnum} arg */
2989 lnum = 0;
2990 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01002991 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 || fnum != curbuf->b_fnum)
2993 {
2994 /* New line, buffer, change: need to get the values. */
2995 filler_lines = diff_check(curwin, lnum);
2996 if (filler_lines < 0)
2997 {
2998 if (filler_lines == -1)
2999 {
3000 change_start = MAXCOL;
3001 change_end = -1;
3002 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3003 hlID = HLF_ADD; /* added line */
3004 else
3005 hlID = HLF_CHD; /* changed line */
3006 }
3007 else
3008 hlID = HLF_ADD; /* added line */
3009 }
3010 else
3011 hlID = (hlf_T)0;
3012 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003013 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 fnum = curbuf->b_fnum;
3015 }
3016
3017 if (hlID == HLF_CHD || hlID == HLF_TXD)
3018 {
3019 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
3020 if (col >= change_start && col <= change_end)
3021 hlID = HLF_TXD; /* changed text */
3022 else
3023 hlID = HLF_CHD; /* changed line */
3024 }
3025 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3026#endif
3027}
3028
3029/*
3030 * "empty({expr})" function
3031 */
3032 static void
3033f_empty(typval_T *argvars, typval_T *rettv)
3034{
3035 int n = FALSE;
3036
3037 switch (argvars[0].v_type)
3038 {
3039 case VAR_STRING:
3040 case VAR_FUNC:
3041 n = argvars[0].vval.v_string == NULL
3042 || *argvars[0].vval.v_string == NUL;
3043 break;
3044 case VAR_PARTIAL:
3045 n = FALSE;
3046 break;
3047 case VAR_NUMBER:
3048 n = argvars[0].vval.v_number == 0;
3049 break;
3050 case VAR_FLOAT:
3051#ifdef FEAT_FLOAT
3052 n = argvars[0].vval.v_float == 0.0;
3053 break;
3054#endif
3055 case VAR_LIST:
3056 n = argvars[0].vval.v_list == NULL
3057 || argvars[0].vval.v_list->lv_first == NULL;
3058 break;
3059 case VAR_DICT:
3060 n = argvars[0].vval.v_dict == NULL
3061 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3062 break;
3063 case VAR_SPECIAL:
3064 n = argvars[0].vval.v_number != VVAL_TRUE;
3065 break;
3066
3067 case VAR_JOB:
3068#ifdef FEAT_JOB_CHANNEL
3069 n = argvars[0].vval.v_job == NULL
3070 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3071 break;
3072#endif
3073 case VAR_CHANNEL:
3074#ifdef FEAT_JOB_CHANNEL
3075 n = argvars[0].vval.v_channel == NULL
3076 || !channel_is_open(argvars[0].vval.v_channel);
3077 break;
3078#endif
3079 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003080 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003081 n = TRUE;
3082 break;
3083 }
3084
3085 rettv->vval.v_number = n;
3086}
3087
3088/*
3089 * "escape({string}, {chars})" function
3090 */
3091 static void
3092f_escape(typval_T *argvars, typval_T *rettv)
3093{
3094 char_u buf[NUMBUFLEN];
3095
3096 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
3097 get_tv_string_buf(&argvars[1], buf));
3098 rettv->v_type = VAR_STRING;
3099}
3100
3101/*
3102 * "eval()" function
3103 */
3104 static void
3105f_eval(typval_T *argvars, typval_T *rettv)
3106{
3107 char_u *s, *p;
3108
3109 s = get_tv_string_chk(&argvars[0]);
3110 if (s != NULL)
3111 s = skipwhite(s);
3112
3113 p = s;
3114 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3115 {
3116 if (p != NULL && !aborting())
3117 EMSG2(_(e_invexpr2), p);
3118 need_clr_eos = FALSE;
3119 rettv->v_type = VAR_NUMBER;
3120 rettv->vval.v_number = 0;
3121 }
3122 else if (*s != NUL)
3123 EMSG(_(e_trailing));
3124}
3125
3126/*
3127 * "eventhandler()" function
3128 */
3129 static void
3130f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3131{
3132 rettv->vval.v_number = vgetc_busy;
3133}
3134
3135/*
3136 * "executable()" function
3137 */
3138 static void
3139f_executable(typval_T *argvars, typval_T *rettv)
3140{
3141 char_u *name = get_tv_string(&argvars[0]);
3142
3143 /* Check in $PATH and also check directly if there is a directory name. */
3144 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3145 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3146}
3147
3148static garray_T redir_execute_ga;
3149
3150/*
3151 * Append "value[value_len]" to the execute() output.
3152 */
3153 void
3154execute_redir_str(char_u *value, int value_len)
3155{
3156 int len;
3157
3158 if (value_len == -1)
3159 len = (int)STRLEN(value); /* Append the entire string */
3160 else
3161 len = value_len; /* Append only "value_len" characters */
3162 if (ga_grow(&redir_execute_ga, len) == OK)
3163 {
3164 mch_memmove((char *)redir_execute_ga.ga_data
3165 + redir_execute_ga.ga_len, value, len);
3166 redir_execute_ga.ga_len += len;
3167 }
3168}
3169
3170/*
3171 * Get next line from a list.
3172 * Called by do_cmdline() to get the next line.
3173 * Returns allocated string, or NULL for end of function.
3174 */
3175
3176 static char_u *
3177get_list_line(
3178 int c UNUSED,
3179 void *cookie,
3180 int indent UNUSED)
3181{
3182 listitem_T **p = (listitem_T **)cookie;
3183 listitem_T *item = *p;
3184 char_u buf[NUMBUFLEN];
3185 char_u *s;
3186
3187 if (item == NULL)
3188 return NULL;
3189 s = get_tv_string_buf_chk(&item->li_tv, buf);
3190 *p = item->li_next;
3191 return s == NULL ? NULL : vim_strsave(s);
3192}
3193
3194/*
3195 * "execute()" function
3196 */
3197 static void
3198f_execute(typval_T *argvars, typval_T *rettv)
3199{
3200 char_u *cmd = NULL;
3201 list_T *list = NULL;
3202 int save_msg_silent = msg_silent;
3203 int save_emsg_silent = emsg_silent;
3204 int save_emsg_noredir = emsg_noredir;
3205 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003206 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003207 garray_T save_ga;
3208
3209 rettv->vval.v_string = NULL;
3210 rettv->v_type = VAR_STRING;
3211
3212 if (argvars[0].v_type == VAR_LIST)
3213 {
3214 list = argvars[0].vval.v_list;
3215 if (list == NULL || list->lv_first == NULL)
3216 /* empty list, no commands, empty output */
3217 return;
3218 ++list->lv_refcount;
3219 }
3220 else
3221 {
3222 cmd = get_tv_string_chk(&argvars[0]);
3223 if (cmd == NULL)
3224 return;
3225 }
3226
3227 if (argvars[1].v_type != VAR_UNKNOWN)
3228 {
3229 char_u buf[NUMBUFLEN];
3230 char_u *s = get_tv_string_buf_chk(&argvars[1], buf);
3231
3232 if (s == NULL)
3233 return;
3234 if (STRNCMP(s, "silent", 6) == 0)
3235 ++msg_silent;
3236 if (STRCMP(s, "silent!") == 0)
3237 {
3238 emsg_silent = TRUE;
3239 emsg_noredir = TRUE;
3240 }
3241 }
3242 else
3243 ++msg_silent;
3244
3245 if (redir_execute)
3246 save_ga = redir_execute_ga;
3247 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3248 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003249 redir_off = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250
3251 if (cmd != NULL)
3252 do_cmdline_cmd(cmd);
3253 else
3254 {
3255 listitem_T *item = list->lv_first;
3256
3257 do_cmdline(NULL, get_list_line, (void *)&item,
3258 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3259 --list->lv_refcount;
3260 }
3261
Bram Moolenaard297f352017-01-29 20:31:21 +01003262 /* Need to append a NUL to the result. */
3263 if (ga_grow(&redir_execute_ga, 1) == OK)
3264 {
3265 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3266 rettv->vval.v_string = redir_execute_ga.ga_data;
3267 }
3268 else
3269 {
3270 ga_clear(&redir_execute_ga);
3271 rettv->vval.v_string = NULL;
3272 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003273 msg_silent = save_msg_silent;
3274 emsg_silent = save_emsg_silent;
3275 emsg_noredir = save_emsg_noredir;
3276
3277 redir_execute = save_redir_execute;
3278 if (redir_execute)
3279 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003280 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003281
3282 /* "silent reg" or "silent echo x" leaves msg_col somewhere in the
3283 * line. Put it back in the first column. */
3284 msg_col = 0;
3285}
3286
3287/*
3288 * "exepath()" function
3289 */
3290 static void
3291f_exepath(typval_T *argvars, typval_T *rettv)
3292{
3293 char_u *p = NULL;
3294
3295 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
3296 rettv->v_type = VAR_STRING;
3297 rettv->vval.v_string = p;
3298}
3299
3300/*
3301 * "exists()" function
3302 */
3303 static void
3304f_exists(typval_T *argvars, typval_T *rettv)
3305{
3306 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003307 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003308
3309 p = get_tv_string(&argvars[0]);
3310 if (*p == '$') /* environment variable */
3311 {
3312 /* first try "normal" environment variables (fast) */
3313 if (mch_getenv(p + 1) != NULL)
3314 n = TRUE;
3315 else
3316 {
3317 /* try expanding things like $VIM and ${HOME} */
3318 p = expand_env_save(p);
3319 if (p != NULL && *p != '$')
3320 n = TRUE;
3321 vim_free(p);
3322 }
3323 }
3324 else if (*p == '&' || *p == '+') /* option */
3325 {
3326 n = (get_option_tv(&p, NULL, TRUE) == OK);
3327 if (*skipwhite(p) != NUL)
3328 n = FALSE; /* trailing garbage */
3329 }
3330 else if (*p == '*') /* internal or user defined function */
3331 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003332 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333 }
3334 else if (*p == ':')
3335 {
3336 n = cmd_exists(p + 1);
3337 }
3338 else if (*p == '#')
3339 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 if (p[1] == '#')
3341 n = autocmd_supported(p + 2);
3342 else
3343 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 }
3345 else /* internal variable */
3346 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003347 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003348 }
3349
3350 rettv->vval.v_number = n;
3351}
3352
3353#ifdef FEAT_FLOAT
3354/*
3355 * "exp()" function
3356 */
3357 static void
3358f_exp(typval_T *argvars, typval_T *rettv)
3359{
3360 float_T f = 0.0;
3361
3362 rettv->v_type = VAR_FLOAT;
3363 if (get_float_arg(argvars, &f) == OK)
3364 rettv->vval.v_float = exp(f);
3365 else
3366 rettv->vval.v_float = 0.0;
3367}
3368#endif
3369
3370/*
3371 * "expand()" function
3372 */
3373 static void
3374f_expand(typval_T *argvars, typval_T *rettv)
3375{
3376 char_u *s;
3377 int len;
3378 char_u *errormsg;
3379 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3380 expand_T xpc;
3381 int error = FALSE;
3382 char_u *result;
3383
3384 rettv->v_type = VAR_STRING;
3385 if (argvars[1].v_type != VAR_UNKNOWN
3386 && argvars[2].v_type != VAR_UNKNOWN
3387 && get_tv_number_chk(&argvars[2], &error)
3388 && !error)
3389 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003390 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 }
3392
3393 s = get_tv_string(&argvars[0]);
3394 if (*s == '%' || *s == '#' || *s == '<')
3395 {
3396 ++emsg_off;
3397 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3398 --emsg_off;
3399 if (rettv->v_type == VAR_LIST)
3400 {
3401 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3402 list_append_string(rettv->vval.v_list, result, -1);
3403 else
3404 vim_free(result);
3405 }
3406 else
3407 rettv->vval.v_string = result;
3408 }
3409 else
3410 {
3411 /* When the optional second argument is non-zero, don't remove matches
3412 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3413 if (argvars[1].v_type != VAR_UNKNOWN
3414 && get_tv_number_chk(&argvars[1], &error))
3415 options |= WILD_KEEP_ALL;
3416 if (!error)
3417 {
3418 ExpandInit(&xpc);
3419 xpc.xp_context = EXPAND_FILES;
3420 if (p_wic)
3421 options += WILD_ICASE;
3422 if (rettv->v_type == VAR_STRING)
3423 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3424 options, WILD_ALL);
3425 else if (rettv_list_alloc(rettv) != FAIL)
3426 {
3427 int i;
3428
3429 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3430 for (i = 0; i < xpc.xp_numfiles; i++)
3431 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3432 ExpandCleanup(&xpc);
3433 }
3434 }
3435 else
3436 rettv->vval.v_string = NULL;
3437 }
3438}
3439
3440/*
3441 * "extend(list, list [, idx])" function
3442 * "extend(dict, dict [, action])" function
3443 */
3444 static void
3445f_extend(typval_T *argvars, typval_T *rettv)
3446{
3447 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3448
3449 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3450 {
3451 list_T *l1, *l2;
3452 listitem_T *item;
3453 long before;
3454 int error = FALSE;
3455
3456 l1 = argvars[0].vval.v_list;
3457 l2 = argvars[1].vval.v_list;
3458 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3459 && l2 != NULL)
3460 {
3461 if (argvars[2].v_type != VAR_UNKNOWN)
3462 {
3463 before = (long)get_tv_number_chk(&argvars[2], &error);
3464 if (error)
3465 return; /* type error; errmsg already given */
3466
3467 if (before == l1->lv_len)
3468 item = NULL;
3469 else
3470 {
3471 item = list_find(l1, before);
3472 if (item == NULL)
3473 {
3474 EMSGN(_(e_listidx), before);
3475 return;
3476 }
3477 }
3478 }
3479 else
3480 item = NULL;
3481 list_extend(l1, l2, item);
3482
3483 copy_tv(&argvars[0], rettv);
3484 }
3485 }
3486 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3487 {
3488 dict_T *d1, *d2;
3489 char_u *action;
3490 int i;
3491
3492 d1 = argvars[0].vval.v_dict;
3493 d2 = argvars[1].vval.v_dict;
3494 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3495 && d2 != NULL)
3496 {
3497 /* Check the third argument. */
3498 if (argvars[2].v_type != VAR_UNKNOWN)
3499 {
3500 static char *(av[]) = {"keep", "force", "error"};
3501
3502 action = get_tv_string_chk(&argvars[2]);
3503 if (action == NULL)
3504 return; /* type error; errmsg already given */
3505 for (i = 0; i < 3; ++i)
3506 if (STRCMP(action, av[i]) == 0)
3507 break;
3508 if (i == 3)
3509 {
3510 EMSG2(_(e_invarg2), action);
3511 return;
3512 }
3513 }
3514 else
3515 action = (char_u *)"force";
3516
3517 dict_extend(d1, d2, action);
3518
3519 copy_tv(&argvars[0], rettv);
3520 }
3521 }
3522 else
3523 EMSG2(_(e_listdictarg), "extend()");
3524}
3525
3526/*
3527 * "feedkeys()" function
3528 */
3529 static void
3530f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3531{
3532 int remap = TRUE;
3533 int insert = FALSE;
3534 char_u *keys, *flags;
3535 char_u nbuf[NUMBUFLEN];
3536 int typed = FALSE;
3537 int execute = FALSE;
3538 int dangerous = FALSE;
3539 char_u *keys_esc;
3540
3541 /* This is not allowed in the sandbox. If the commands would still be
3542 * executed in the sandbox it would be OK, but it probably happens later,
3543 * when "sandbox" is no longer set. */
3544 if (check_secure())
3545 return;
3546
3547 keys = get_tv_string(&argvars[0]);
3548
3549 if (argvars[1].v_type != VAR_UNKNOWN)
3550 {
3551 flags = get_tv_string_buf(&argvars[1], nbuf);
3552 for ( ; *flags != NUL; ++flags)
3553 {
3554 switch (*flags)
3555 {
3556 case 'n': remap = FALSE; break;
3557 case 'm': remap = TRUE; break;
3558 case 't': typed = TRUE; break;
3559 case 'i': insert = TRUE; break;
3560 case 'x': execute = TRUE; break;
3561 case '!': dangerous = TRUE; break;
3562 }
3563 }
3564 }
3565
3566 if (*keys != NUL || execute)
3567 {
3568 /* Need to escape K_SPECIAL and CSI before putting the string in the
3569 * typeahead buffer. */
3570 keys_esc = vim_strsave_escape_csi(keys);
3571 if (keys_esc != NULL)
3572 {
3573 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3574 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3575 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003576 if (vgetc_busy
3577#ifdef FEAT_TIMERS
3578 || timer_busy
3579#endif
3580 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581 typebuf_was_filled = TRUE;
3582 if (execute)
3583 {
3584 int save_msg_scroll = msg_scroll;
3585
3586 /* Avoid a 1 second delay when the keys start Insert mode. */
3587 msg_scroll = FALSE;
3588
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003589 if (!dangerous)
3590 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003591 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003592 if (!dangerous)
3593 --ex_normal_busy;
3594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 msg_scroll |= save_msg_scroll;
3596 }
3597 }
3598 }
3599}
3600
3601/*
3602 * "filereadable()" function
3603 */
3604 static void
3605f_filereadable(typval_T *argvars, typval_T *rettv)
3606{
3607 int fd;
3608 char_u *p;
3609 int n;
3610
3611#ifndef O_NONBLOCK
3612# define O_NONBLOCK 0
3613#endif
3614 p = get_tv_string(&argvars[0]);
3615 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3616 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3617 {
3618 n = TRUE;
3619 close(fd);
3620 }
3621 else
3622 n = FALSE;
3623
3624 rettv->vval.v_number = n;
3625}
3626
3627/*
3628 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3629 * rights to write into.
3630 */
3631 static void
3632f_filewritable(typval_T *argvars, typval_T *rettv)
3633{
3634 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
3635}
3636
3637 static void
3638findfilendir(
3639 typval_T *argvars UNUSED,
3640 typval_T *rettv,
3641 int find_what UNUSED)
3642{
3643#ifdef FEAT_SEARCHPATH
3644 char_u *fname;
3645 char_u *fresult = NULL;
3646 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3647 char_u *p;
3648 char_u pathbuf[NUMBUFLEN];
3649 int count = 1;
3650 int first = TRUE;
3651 int error = FALSE;
3652#endif
3653
3654 rettv->vval.v_string = NULL;
3655 rettv->v_type = VAR_STRING;
3656
3657#ifdef FEAT_SEARCHPATH
3658 fname = get_tv_string(&argvars[0]);
3659
3660 if (argvars[1].v_type != VAR_UNKNOWN)
3661 {
3662 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
3663 if (p == NULL)
3664 error = TRUE;
3665 else
3666 {
3667 if (*p != NUL)
3668 path = p;
3669
3670 if (argvars[2].v_type != VAR_UNKNOWN)
3671 count = (int)get_tv_number_chk(&argvars[2], &error);
3672 }
3673 }
3674
3675 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3676 error = TRUE;
3677
3678 if (*fname != NUL && !error)
3679 {
3680 do
3681 {
3682 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3683 vim_free(fresult);
3684 fresult = find_file_in_path_option(first ? fname : NULL,
3685 first ? (int)STRLEN(fname) : 0,
3686 0, first, path,
3687 find_what,
3688 curbuf->b_ffname,
3689 find_what == FINDFILE_DIR
3690 ? (char_u *)"" : curbuf->b_p_sua);
3691 first = FALSE;
3692
3693 if (fresult != NULL && rettv->v_type == VAR_LIST)
3694 list_append_string(rettv->vval.v_list, fresult, -1);
3695
3696 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3697 }
3698
3699 if (rettv->v_type == VAR_STRING)
3700 rettv->vval.v_string = fresult;
3701#endif
3702}
3703
3704/*
3705 * "filter()" function
3706 */
3707 static void
3708f_filter(typval_T *argvars, typval_T *rettv)
3709{
3710 filter_map(argvars, rettv, FALSE);
3711}
3712
3713/*
3714 * "finddir({fname}[, {path}[, {count}]])" function
3715 */
3716 static void
3717f_finddir(typval_T *argvars, typval_T *rettv)
3718{
3719 findfilendir(argvars, rettv, FINDFILE_DIR);
3720}
3721
3722/*
3723 * "findfile({fname}[, {path}[, {count}]])" function
3724 */
3725 static void
3726f_findfile(typval_T *argvars, typval_T *rettv)
3727{
3728 findfilendir(argvars, rettv, FINDFILE_FILE);
3729}
3730
3731#ifdef FEAT_FLOAT
3732/*
3733 * "float2nr({float})" function
3734 */
3735 static void
3736f_float2nr(typval_T *argvars, typval_T *rettv)
3737{
3738 float_T f = 0.0;
3739
3740 if (get_float_arg(argvars, &f) == OK)
3741 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003742 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003743 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003744 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003745 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 else
3747 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 }
3749}
3750
3751/*
3752 * "floor({float})" function
3753 */
3754 static void
3755f_floor(typval_T *argvars, typval_T *rettv)
3756{
3757 float_T f = 0.0;
3758
3759 rettv->v_type = VAR_FLOAT;
3760 if (get_float_arg(argvars, &f) == OK)
3761 rettv->vval.v_float = floor(f);
3762 else
3763 rettv->vval.v_float = 0.0;
3764}
3765
3766/*
3767 * "fmod()" function
3768 */
3769 static void
3770f_fmod(typval_T *argvars, typval_T *rettv)
3771{
3772 float_T fx = 0.0, fy = 0.0;
3773
3774 rettv->v_type = VAR_FLOAT;
3775 if (get_float_arg(argvars, &fx) == OK
3776 && get_float_arg(&argvars[1], &fy) == OK)
3777 rettv->vval.v_float = fmod(fx, fy);
3778 else
3779 rettv->vval.v_float = 0.0;
3780}
3781#endif
3782
3783/*
3784 * "fnameescape({string})" function
3785 */
3786 static void
3787f_fnameescape(typval_T *argvars, typval_T *rettv)
3788{
3789 rettv->vval.v_string = vim_strsave_fnameescape(
3790 get_tv_string(&argvars[0]), FALSE);
3791 rettv->v_type = VAR_STRING;
3792}
3793
3794/*
3795 * "fnamemodify({fname}, {mods})" function
3796 */
3797 static void
3798f_fnamemodify(typval_T *argvars, typval_T *rettv)
3799{
3800 char_u *fname;
3801 char_u *mods;
3802 int usedlen = 0;
3803 int len;
3804 char_u *fbuf = NULL;
3805 char_u buf[NUMBUFLEN];
3806
3807 fname = get_tv_string_chk(&argvars[0]);
3808 mods = get_tv_string_buf_chk(&argvars[1], buf);
3809 if (fname == NULL || mods == NULL)
3810 fname = NULL;
3811 else
3812 {
3813 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003814 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003815 }
3816
3817 rettv->v_type = VAR_STRING;
3818 if (fname == NULL)
3819 rettv->vval.v_string = NULL;
3820 else
3821 rettv->vval.v_string = vim_strnsave(fname, len);
3822 vim_free(fbuf);
3823}
3824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825/*
3826 * "foldclosed()" function
3827 */
3828 static void
3829foldclosed_both(
3830 typval_T *argvars UNUSED,
3831 typval_T *rettv,
3832 int end UNUSED)
3833{
3834#ifdef FEAT_FOLDING
3835 linenr_T lnum;
3836 linenr_T first, last;
3837
3838 lnum = get_tv_lnum(argvars);
3839 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3840 {
3841 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3842 {
3843 if (end)
3844 rettv->vval.v_number = (varnumber_T)last;
3845 else
3846 rettv->vval.v_number = (varnumber_T)first;
3847 return;
3848 }
3849 }
3850#endif
3851 rettv->vval.v_number = -1;
3852}
3853
3854/*
3855 * "foldclosed()" function
3856 */
3857 static void
3858f_foldclosed(typval_T *argvars, typval_T *rettv)
3859{
3860 foldclosed_both(argvars, rettv, FALSE);
3861}
3862
3863/*
3864 * "foldclosedend()" function
3865 */
3866 static void
3867f_foldclosedend(typval_T *argvars, typval_T *rettv)
3868{
3869 foldclosed_both(argvars, rettv, TRUE);
3870}
3871
3872/*
3873 * "foldlevel()" function
3874 */
3875 static void
3876f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3877{
3878#ifdef FEAT_FOLDING
3879 linenr_T lnum;
3880
3881 lnum = get_tv_lnum(argvars);
3882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3883 rettv->vval.v_number = foldLevel(lnum);
3884#endif
3885}
3886
3887/*
3888 * "foldtext()" function
3889 */
3890 static void
3891f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
3892{
3893#ifdef FEAT_FOLDING
3894 linenr_T foldstart;
3895 linenr_T foldend;
3896 char_u *dashes;
3897 linenr_T lnum;
3898 char_u *s;
3899 char_u *r;
3900 int len;
3901 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003902 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003903#endif
3904
3905 rettv->v_type = VAR_STRING;
3906 rettv->vval.v_string = NULL;
3907#ifdef FEAT_FOLDING
3908 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
3909 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
3910 dashes = get_vim_var_str(VV_FOLDDASHES);
3911 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
3912 && dashes != NULL)
3913 {
3914 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02003915 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003916 if (!linewhite(lnum))
3917 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003918
3919 /* Find interesting text in this line. */
3920 s = skipwhite(ml_get(lnum));
3921 /* skip C comment-start */
3922 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
3923 {
3924 s = skipwhite(s + 2);
3925 if (*skipwhite(s) == NUL
3926 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
3927 {
3928 s = skipwhite(ml_get(lnum + 1));
3929 if (*s == '*')
3930 s = skipwhite(s + 1);
3931 }
3932 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02003933 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003934 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 r = alloc((unsigned)(STRLEN(txt)
3936 + STRLEN(dashes) /* for %s */
3937 + 20 /* for %3ld */
3938 + STRLEN(s))); /* concatenated */
3939 if (r != NULL)
3940 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02003941 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 len = (int)STRLEN(r);
3943 STRCAT(r, s);
3944 /* remove 'foldmarker' and 'commentstring' */
3945 foldtext_cleanup(r + len);
3946 rettv->vval.v_string = r;
3947 }
3948 }
3949#endif
3950}
3951
3952/*
3953 * "foldtextresult(lnum)" function
3954 */
3955 static void
3956f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
3957{
3958#ifdef FEAT_FOLDING
3959 linenr_T lnum;
3960 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02003961 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 foldinfo_T foldinfo;
3963 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003964 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003965#endif
3966
3967 rettv->v_type = VAR_STRING;
3968 rettv->vval.v_string = NULL;
3969#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003970 if (entered)
3971 return; /* reject recursive use */
3972 entered = TRUE;
3973
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974 lnum = get_tv_lnum(argvars);
3975 /* treat illegal types and illegal string values for {lnum} the same */
3976 if (lnum < 0)
3977 lnum = 0;
3978 fold_count = foldedCount(curwin, lnum, &foldinfo);
3979 if (fold_count > 0)
3980 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01003981 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
3982 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003983 if (text == buf)
3984 text = vim_strsave(text);
3985 rettv->vval.v_string = text;
3986 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02003987
3988 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003989#endif
3990}
3991
3992/*
3993 * "foreground()" function
3994 */
3995 static void
3996f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3997{
3998#ifdef FEAT_GUI
3999 if (gui.in_use)
4000 gui_mch_set_foreground();
4001#else
4002# ifdef WIN32
4003 win32_set_foreground();
4004# endif
4005#endif
4006}
4007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004008 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004009common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010{
4011 char_u *s;
4012 char_u *name;
4013 int use_string = FALSE;
4014 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004015 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004016
4017 if (argvars[0].v_type == VAR_FUNC)
4018 {
4019 /* function(MyFunc, [arg], dict) */
4020 s = argvars[0].vval.v_string;
4021 }
4022 else if (argvars[0].v_type == VAR_PARTIAL
4023 && argvars[0].vval.v_partial != NULL)
4024 {
4025 /* function(dict.MyFunc, [arg]) */
4026 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004027 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004028 }
4029 else
4030 {
4031 /* function('MyFunc', [arg], dict) */
4032 s = get_tv_string(&argvars[0]);
4033 use_string = TRUE;
4034 }
4035
Bram Moolenaar843b8842016-08-21 14:36:15 +02004036 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004037 {
4038 name = s;
4039 trans_name = trans_function_name(&name, FALSE,
4040 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4041 if (*name != NUL)
4042 s = NULL;
4043 }
4044
Bram Moolenaar843b8842016-08-21 14:36:15 +02004045 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4046 || (is_funcref && trans_name == NULL))
Bram Moolenaar5582ef12016-09-14 22:16:13 +02004047 EMSG2(_(e_invarg2), use_string ? get_tv_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004048 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004049 else if (trans_name != NULL && (is_funcref
4050 ? find_func(trans_name) == NULL
4051 : !translated_function_exists(trans_name)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052 EMSG2(_("E700: Unknown function: %s"), s);
4053 else
4054 {
4055 int dict_idx = 0;
4056 int arg_idx = 0;
4057 list_T *list = NULL;
4058
4059 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4060 {
4061 char sid_buf[25];
4062 int off = *s == 's' ? 2 : 5;
4063
4064 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4065 * also be called from another script. Using trans_function_name()
4066 * would also work, but some plugins depend on the name being
4067 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004068 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004069 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4070 if (name != NULL)
4071 {
4072 STRCPY(name, sid_buf);
4073 STRCAT(name, s + off);
4074 }
4075 }
4076 else
4077 name = vim_strsave(s);
4078
4079 if (argvars[1].v_type != VAR_UNKNOWN)
4080 {
4081 if (argvars[2].v_type != VAR_UNKNOWN)
4082 {
4083 /* function(name, [args], dict) */
4084 arg_idx = 1;
4085 dict_idx = 2;
4086 }
4087 else if (argvars[1].v_type == VAR_DICT)
4088 /* function(name, dict) */
4089 dict_idx = 1;
4090 else
4091 /* function(name, [args]) */
4092 arg_idx = 1;
4093 if (dict_idx > 0)
4094 {
4095 if (argvars[dict_idx].v_type != VAR_DICT)
4096 {
4097 EMSG(_("E922: expected a dict"));
4098 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004099 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 }
4101 if (argvars[dict_idx].vval.v_dict == NULL)
4102 dict_idx = 0;
4103 }
4104 if (arg_idx > 0)
4105 {
4106 if (argvars[arg_idx].v_type != VAR_LIST)
4107 {
4108 EMSG(_("E923: Second argument of function() must be a list or a dict"));
4109 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004110 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 }
4112 list = argvars[arg_idx].vval.v_list;
4113 if (list == NULL || list->lv_len == 0)
4114 arg_idx = 0;
4115 }
4116 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004117 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118 {
4119 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4120
4121 /* result is a VAR_PARTIAL */
4122 if (pt == NULL)
4123 vim_free(name);
4124 else
4125 {
4126 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4127 {
4128 listitem_T *li;
4129 int i = 0;
4130 int arg_len = 0;
4131 int lv_len = 0;
4132
4133 if (arg_pt != NULL)
4134 arg_len = arg_pt->pt_argc;
4135 if (list != NULL)
4136 lv_len = list->lv_len;
4137 pt->pt_argc = arg_len + lv_len;
4138 pt->pt_argv = (typval_T *)alloc(
4139 sizeof(typval_T) * pt->pt_argc);
4140 if (pt->pt_argv == NULL)
4141 {
4142 vim_free(pt);
4143 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004146 for (i = 0; i < arg_len; i++)
4147 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4148 if (lv_len > 0)
4149 for (li = list->lv_first; li != NULL;
4150 li = li->li_next)
4151 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 }
4153
4154 /* For "function(dict.func, [], dict)" and "func" is a partial
4155 * use "dict". That is backwards compatible. */
4156 if (dict_idx > 0)
4157 {
4158 /* The dict is bound explicitly, pt_auto is FALSE. */
4159 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4160 ++pt->pt_dict->dv_refcount;
4161 }
4162 else if (arg_pt != NULL)
4163 {
4164 /* If the dict was bound automatically the result is also
4165 * bound automatically. */
4166 pt->pt_dict = arg_pt->pt_dict;
4167 pt->pt_auto = arg_pt->pt_auto;
4168 if (pt->pt_dict != NULL)
4169 ++pt->pt_dict->dv_refcount;
4170 }
4171
4172 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004173 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4174 {
4175 pt->pt_func = arg_pt->pt_func;
4176 func_ptr_ref(pt->pt_func);
4177 vim_free(name);
4178 }
4179 else if (is_funcref)
4180 {
4181 pt->pt_func = find_func(trans_name);
4182 func_ptr_ref(pt->pt_func);
4183 vim_free(name);
4184 }
4185 else
4186 {
4187 pt->pt_name = name;
4188 func_ref(name);
4189 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004190 }
4191 rettv->v_type = VAR_PARTIAL;
4192 rettv->vval.v_partial = pt;
4193 }
4194 else
4195 {
4196 /* result is a VAR_FUNC */
4197 rettv->v_type = VAR_FUNC;
4198 rettv->vval.v_string = name;
4199 func_ref(name);
4200 }
4201 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004202theend:
4203 vim_free(trans_name);
4204}
4205
4206/*
4207 * "funcref()" function
4208 */
4209 static void
4210f_funcref(typval_T *argvars, typval_T *rettv)
4211{
4212 common_function(argvars, rettv, TRUE);
4213}
4214
4215/*
4216 * "function()" function
4217 */
4218 static void
4219f_function(typval_T *argvars, typval_T *rettv)
4220{
4221 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222}
4223
4224/*
4225 * "garbagecollect()" function
4226 */
4227 static void
4228f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4229{
4230 /* This is postponed until we are back at the toplevel, because we may be
4231 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4232 want_garbage_collect = TRUE;
4233
4234 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
4235 garbage_collect_at_exit = TRUE;
4236}
4237
4238/*
4239 * "get()" function
4240 */
4241 static void
4242f_get(typval_T *argvars, typval_T *rettv)
4243{
4244 listitem_T *li;
4245 list_T *l;
4246 dictitem_T *di;
4247 dict_T *d;
4248 typval_T *tv = NULL;
4249
4250 if (argvars[0].v_type == VAR_LIST)
4251 {
4252 if ((l = argvars[0].vval.v_list) != NULL)
4253 {
4254 int error = FALSE;
4255
4256 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
4257 if (!error && li != NULL)
4258 tv = &li->li_tv;
4259 }
4260 }
4261 else if (argvars[0].v_type == VAR_DICT)
4262 {
4263 if ((d = argvars[0].vval.v_dict) != NULL)
4264 {
4265 di = dict_find(d, get_tv_string(&argvars[1]), -1);
4266 if (di != NULL)
4267 tv = &di->di_tv;
4268 }
4269 }
4270 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4271 {
4272 partial_T *pt;
4273 partial_T fref_pt;
4274
4275 if (argvars[0].v_type == VAR_PARTIAL)
4276 pt = argvars[0].vval.v_partial;
4277 else
4278 {
4279 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4280 fref_pt.pt_name = argvars[0].vval.v_string;
4281 pt = &fref_pt;
4282 }
4283
4284 if (pt != NULL)
4285 {
4286 char_u *what = get_tv_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004287 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004288
4289 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4290 {
4291 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004292 n = partial_name(pt);
4293 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294 rettv->vval.v_string = NULL;
4295 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004296 {
4297 rettv->vval.v_string = vim_strsave(n);
4298 if (rettv->v_type == VAR_FUNC)
4299 func_ref(rettv->vval.v_string);
4300 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 }
4302 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004303 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004304 else if (STRCMP(what, "args") == 0)
4305 {
4306 rettv->v_type = VAR_LIST;
4307 if (rettv_list_alloc(rettv) == OK)
4308 {
4309 int i;
4310
4311 for (i = 0; i < pt->pt_argc; ++i)
4312 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4313 }
4314 }
4315 else
4316 EMSG2(_(e_invarg2), what);
4317 return;
4318 }
4319 }
4320 else
4321 EMSG2(_(e_listdictarg), "get()");
4322
4323 if (tv == NULL)
4324 {
4325 if (argvars[2].v_type != VAR_UNKNOWN)
4326 copy_tv(&argvars[2], rettv);
4327 }
4328 else
4329 copy_tv(tv, rettv);
4330}
4331
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004332#ifdef FEAT_SIGNS
4333/*
4334 * Returns information about signs placed in a buffer as list of dicts.
4335 */
4336 static void
4337get_buffer_signs(buf_T *buf, list_T *l)
4338{
4339 signlist_T *sign;
4340
4341 for (sign = buf->b_signlist; sign; sign = sign->next)
4342 {
4343 dict_T *d = dict_alloc();
4344
4345 if (d != NULL)
4346 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004347 dict_add_number(d, "id", sign->id);
4348 dict_add_number(d, "lnum", sign->lnum);
4349 dict_add_string(d, "name", sign_typenr2name(sign->typenr));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004350
4351 list_append_dict(l, d);
4352 }
4353 }
4354}
4355#endif
4356
4357/*
4358 * Returns buffer options, variables and other attributes in a dictionary.
4359 */
4360 static dict_T *
4361get_buffer_info(buf_T *buf)
4362{
4363 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004364 tabpage_T *tp;
4365 win_T *wp;
4366 list_T *windows;
4367
4368 dict = dict_alloc();
4369 if (dict == NULL)
4370 return NULL;
4371
Bram Moolenaare0be1672018-07-08 16:50:37 +02004372 dict_add_number(dict, "bufnr", buf->b_fnum);
4373 dict_add_string(dict, "name", buf->b_ffname);
4374 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4375 : buflist_findlnum(buf));
4376 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4377 dict_add_number(dict, "listed", buf->b_p_bl);
4378 dict_add_number(dict, "changed", bufIsChanged(buf));
4379 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4380 dict_add_number(dict, "hidden",
4381 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004382
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004383 /* Get a reference to buffer variables */
4384 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004385
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004386 /* List of windows displaying this buffer */
4387 windows = list_alloc();
4388 if (windows != NULL)
4389 {
4390 FOR_ALL_TAB_WINDOWS(tp, wp)
4391 if (wp->w_buffer == buf)
4392 list_append_number(windows, (varnumber_T)wp->w_id);
4393 dict_add_list(dict, "windows", windows);
4394 }
4395
4396#ifdef FEAT_SIGNS
4397 if (buf->b_signlist != NULL)
4398 {
4399 /* List of signs placed in this buffer */
4400 list_T *signs = list_alloc();
4401 if (signs != NULL)
4402 {
4403 get_buffer_signs(buf, signs);
4404 dict_add_list(dict, "signs", signs);
4405 }
4406 }
4407#endif
4408
4409 return dict;
4410}
4411
4412/*
4413 * "getbufinfo()" function
4414 */
4415 static void
4416f_getbufinfo(typval_T *argvars, typval_T *rettv)
4417{
4418 buf_T *buf = NULL;
4419 buf_T *argbuf = NULL;
4420 dict_T *d;
4421 int filtered = FALSE;
4422 int sel_buflisted = FALSE;
4423 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004424 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004425
4426 if (rettv_list_alloc(rettv) != OK)
4427 return;
4428
4429 /* List of all the buffers or selected buffers */
4430 if (argvars[0].v_type == VAR_DICT)
4431 {
4432 dict_T *sel_d = argvars[0].vval.v_dict;
4433
4434 if (sel_d != NULL)
4435 {
4436 dictitem_T *di;
4437
4438 filtered = TRUE;
4439
4440 di = dict_find(sel_d, (char_u *)"buflisted", -1);
4441 if (di != NULL && get_tv_number(&di->di_tv))
4442 sel_buflisted = TRUE;
4443
4444 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
4445 if (di != NULL && get_tv_number(&di->di_tv))
4446 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004447
4448 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
4449 if (di != NULL && get_tv_number(&di->di_tv))
4450 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004451 }
4452 }
4453 else if (argvars[0].v_type != VAR_UNKNOWN)
4454 {
4455 /* Information about one buffer. Argument specifies the buffer */
4456 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4457 ++emsg_off;
4458 argbuf = get_buf_tv(&argvars[0], FALSE);
4459 --emsg_off;
4460 if (argbuf == NULL)
4461 return;
4462 }
4463
4464 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004465 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004466 {
4467 if (argbuf != NULL && argbuf != buf)
4468 continue;
4469 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004470 || (sel_buflisted && !buf->b_p_bl)
4471 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004472 continue;
4473
4474 d = get_buffer_info(buf);
4475 if (d != NULL)
4476 list_append_dict(rettv->vval.v_list, d);
4477 if (argbuf != NULL)
4478 return;
4479 }
4480}
4481
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004482/*
4483 * Get line or list of lines from buffer "buf" into "rettv".
4484 * Return a range (from start to end) of lines in rettv from the specified
4485 * buffer.
4486 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4487 */
4488 static void
4489get_buffer_lines(
4490 buf_T *buf,
4491 linenr_T start,
4492 linenr_T end,
4493 int retlist,
4494 typval_T *rettv)
4495{
4496 char_u *p;
4497
4498 rettv->v_type = VAR_STRING;
4499 rettv->vval.v_string = NULL;
4500 if (retlist && rettv_list_alloc(rettv) == FAIL)
4501 return;
4502
4503 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4504 return;
4505
4506 if (!retlist)
4507 {
4508 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4509 p = ml_get_buf(buf, start, FALSE);
4510 else
4511 p = (char_u *)"";
4512 rettv->vval.v_string = vim_strsave(p);
4513 }
4514 else
4515 {
4516 if (end < start)
4517 return;
4518
4519 if (start < 1)
4520 start = 1;
4521 if (end > buf->b_ml.ml_line_count)
4522 end = buf->b_ml.ml_line_count;
4523 while (start <= end)
4524 if (list_append_string(rettv->vval.v_list,
4525 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4526 break;
4527 }
4528}
4529
4530/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004531 * "getbufline()" function
4532 */
4533 static void
4534f_getbufline(typval_T *argvars, typval_T *rettv)
4535{
4536 linenr_T lnum;
4537 linenr_T end;
4538 buf_T *buf;
4539
4540 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4541 ++emsg_off;
4542 buf = get_buf_tv(&argvars[0], FALSE);
4543 --emsg_off;
4544
4545 lnum = get_tv_lnum_buf(&argvars[1], buf);
4546 if (argvars[2].v_type == VAR_UNKNOWN)
4547 end = lnum;
4548 else
4549 end = get_tv_lnum_buf(&argvars[2], buf);
4550
4551 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4552}
4553
4554/*
4555 * "getbufvar()" function
4556 */
4557 static void
4558f_getbufvar(typval_T *argvars, typval_T *rettv)
4559{
4560 buf_T *buf;
4561 buf_T *save_curbuf;
4562 char_u *varname;
4563 dictitem_T *v;
4564 int done = FALSE;
4565
4566 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4567 varname = get_tv_string_chk(&argvars[1]);
4568 ++emsg_off;
4569 buf = get_buf_tv(&argvars[0], FALSE);
4570
4571 rettv->v_type = VAR_STRING;
4572 rettv->vval.v_string = NULL;
4573
4574 if (buf != NULL && varname != NULL)
4575 {
4576 /* set curbuf to be our buf, temporarily */
4577 save_curbuf = curbuf;
4578 curbuf = buf;
4579
Bram Moolenaar30567352016-08-27 21:25:44 +02004580 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004581 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004582 if (varname[1] == NUL)
4583 {
4584 /* get all buffer-local options in a dict */
4585 dict_T *opts = get_winbuf_options(TRUE);
4586
4587 if (opts != NULL)
4588 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004589 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004590 done = TRUE;
4591 }
4592 }
4593 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4594 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 done = TRUE;
4596 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597 else
4598 {
4599 /* Look up the variable. */
4600 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4601 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4602 'b', varname, FALSE);
4603 if (v != NULL)
4604 {
4605 copy_tv(&v->di_tv, rettv);
4606 done = TRUE;
4607 }
4608 }
4609
4610 /* restore previous notion of curbuf */
4611 curbuf = save_curbuf;
4612 }
4613
4614 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4615 /* use the default value */
4616 copy_tv(&argvars[2], rettv);
4617
4618 --emsg_off;
4619}
4620
4621/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004622 * "getchangelist()" function
4623 */
4624 static void
4625f_getchangelist(typval_T *argvars, typval_T *rettv)
4626{
4627#ifdef FEAT_JUMPLIST
4628 buf_T *buf;
4629 int i;
4630 list_T *l;
4631 dict_T *d;
4632#endif
4633
4634 if (rettv_list_alloc(rettv) != OK)
4635 return;
4636
4637#ifdef FEAT_JUMPLIST
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004638 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
4639 ++emsg_off;
4640 buf = get_buf_tv(&argvars[0], FALSE);
4641 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004642 if (buf == NULL)
4643 return;
4644
4645 l = list_alloc();
4646 if (l == NULL)
4647 return;
4648
4649 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4650 return;
4651 /*
4652 * The current window change list index tracks only the position in the
4653 * current buffer change list. For other buffers, use the change list
4654 * length as the current index.
4655 */
4656 list_append_number(rettv->vval.v_list,
4657 (varnumber_T)((buf == curwin->w_buffer)
4658 ? curwin->w_changelistidx : buf->b_changelistlen));
4659
4660 for (i = 0; i < buf->b_changelistlen; ++i)
4661 {
4662 if (buf->b_changelist[i].lnum == 0)
4663 continue;
4664 if ((d = dict_alloc()) == NULL)
4665 return;
4666 if (list_append_dict(l, d) == FAIL)
4667 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004668 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4669 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004670# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004671 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004672# endif
4673 }
4674#endif
4675}
4676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677 * "getchar()" function
4678 */
4679 static void
4680f_getchar(typval_T *argvars, typval_T *rettv)
4681{
4682 varnumber_T n;
4683 int error = FALSE;
4684
Bram Moolenaar84d93902018-09-11 20:10:20 +02004685#ifdef MESSAGE_QUEUE
4686 // vpeekc() used to check for messages, but that caused problems, invoking
4687 // a callback where it was not expected. Some plugins use getchar(1) in a
4688 // loop to await a message, therefore make sure we check for messages here.
4689 parse_queued_messages();
4690#endif
4691
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004692 /* Position the cursor. Needed after a message that ends in a space. */
4693 windgoto(msg_row, msg_col);
4694
4695 ++no_mapping;
4696 ++allow_keys;
4697 for (;;)
4698 {
4699 if (argvars[0].v_type == VAR_UNKNOWN)
4700 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004701 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004702 else if (get_tv_number_chk(&argvars[0], &error) == 1)
4703 /* getchar(1): only check if char avail */
4704 n = vpeekc_any();
4705 else if (error || vpeekc_any() == NUL)
4706 /* illegal argument or getchar(0) and no char avail: return zero */
4707 n = 0;
4708 else
4709 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004710 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711
4712 if (n == K_IGNORE)
4713 continue;
4714 break;
4715 }
4716 --no_mapping;
4717 --allow_keys;
4718
4719 set_vim_var_nr(VV_MOUSE_WIN, 0);
4720 set_vim_var_nr(VV_MOUSE_WINID, 0);
4721 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4722 set_vim_var_nr(VV_MOUSE_COL, 0);
4723
4724 rettv->vval.v_number = n;
4725 if (IS_SPECIAL(n) || mod_mask != 0)
4726 {
4727 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4728 int i = 0;
4729
4730 /* Turn a special key into three bytes, plus modifier. */
4731 if (mod_mask != 0)
4732 {
4733 temp[i++] = K_SPECIAL;
4734 temp[i++] = KS_MODIFIER;
4735 temp[i++] = mod_mask;
4736 }
4737 if (IS_SPECIAL(n))
4738 {
4739 temp[i++] = K_SPECIAL;
4740 temp[i++] = K_SECOND(n);
4741 temp[i++] = K_THIRD(n);
4742 }
4743#ifdef FEAT_MBYTE
4744 else if (has_mbyte)
4745 i += (*mb_char2bytes)(n, temp + i);
4746#endif
4747 else
4748 temp[i++] = n;
4749 temp[i++] = NUL;
4750 rettv->v_type = VAR_STRING;
4751 rettv->vval.v_string = vim_strsave(temp);
4752
4753#ifdef FEAT_MOUSE
4754 if (is_mouse_key(n))
4755 {
4756 int row = mouse_row;
4757 int col = mouse_col;
4758 win_T *win;
4759 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004760 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004761 int winnr = 1;
4762
4763 if (row >= 0 && col >= 0)
4764 {
4765 /* Find the window at the mouse coordinates and compute the
4766 * text position. */
4767 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004768 if (win == NULL)
4769 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004770 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004771 for (wp = firstwin; wp != win; wp = wp->w_next)
4772 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004773 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4774 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4775 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4776 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4777 }
4778 }
4779#endif
4780 }
4781}
4782
4783/*
4784 * "getcharmod()" function
4785 */
4786 static void
4787f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4788{
4789 rettv->vval.v_number = mod_mask;
4790}
4791
4792/*
4793 * "getcharsearch()" function
4794 */
4795 static void
4796f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4797{
4798 if (rettv_dict_alloc(rettv) != FAIL)
4799 {
4800 dict_T *dict = rettv->vval.v_dict;
4801
Bram Moolenaare0be1672018-07-08 16:50:37 +02004802 dict_add_string(dict, "char", last_csearch());
4803 dict_add_number(dict, "forward", last_csearch_forward());
4804 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004805 }
4806}
4807
4808/*
4809 * "getcmdline()" function
4810 */
4811 static void
4812f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4813{
4814 rettv->v_type = VAR_STRING;
4815 rettv->vval.v_string = get_cmdline_str();
4816}
4817
4818/*
4819 * "getcmdpos()" function
4820 */
4821 static void
4822f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4823{
4824 rettv->vval.v_number = get_cmdline_pos() + 1;
4825}
4826
4827/*
4828 * "getcmdtype()" function
4829 */
4830 static void
4831f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4832{
4833 rettv->v_type = VAR_STRING;
4834 rettv->vval.v_string = alloc(2);
4835 if (rettv->vval.v_string != NULL)
4836 {
4837 rettv->vval.v_string[0] = get_cmdline_type();
4838 rettv->vval.v_string[1] = NUL;
4839 }
4840}
4841
4842/*
4843 * "getcmdwintype()" function
4844 */
4845 static void
4846f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4847{
4848 rettv->v_type = VAR_STRING;
4849 rettv->vval.v_string = NULL;
4850#ifdef FEAT_CMDWIN
4851 rettv->vval.v_string = alloc(2);
4852 if (rettv->vval.v_string != NULL)
4853 {
4854 rettv->vval.v_string[0] = cmdwin_type;
4855 rettv->vval.v_string[1] = NUL;
4856 }
4857#endif
4858}
4859
4860#if defined(FEAT_CMDL_COMPL)
4861/*
4862 * "getcompletion()" function
4863 */
4864 static void
4865f_getcompletion(typval_T *argvars, typval_T *rettv)
4866{
4867 char_u *pat;
4868 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004869 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004870 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4871 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004873 if (argvars[2].v_type != VAR_UNKNOWN)
4874 filtered = get_tv_number_chk(&argvars[2], NULL);
4875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876 if (p_wic)
4877 options |= WILD_ICASE;
4878
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004879 /* For filtered results, 'wildignore' is used */
4880 if (!filtered)
4881 options |= WILD_KEEP_ALL;
4882
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004883 ExpandInit(&xpc);
4884 xpc.xp_pattern = get_tv_string(&argvars[0]);
4885 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4886 xpc.xp_context = cmdcomplete_str_to_type(get_tv_string(&argvars[1]));
4887 if (xpc.xp_context == EXPAND_NOTHING)
4888 {
4889 if (argvars[1].v_type == VAR_STRING)
4890 EMSG2(_(e_invarg2), argvars[1].vval.v_string);
4891 else
4892 EMSG(_(e_invarg));
4893 return;
4894 }
4895
4896# if defined(FEAT_MENU)
4897 if (xpc.xp_context == EXPAND_MENUS)
4898 {
4899 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
4900 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4901 }
4902# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02004903#ifdef FEAT_CSCOPE
4904 if (xpc.xp_context == EXPAND_CSCOPE)
4905 {
4906 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
4907 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4908 }
4909#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02004910#ifdef FEAT_SIGNS
4911 if (xpc.xp_context == EXPAND_SIGN)
4912 {
4913 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
4914 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
4915 }
4916#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917
4918 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
4919 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
4920 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004921 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922
4923 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
4924
4925 for (i = 0; i < xpc.xp_numfiles; i++)
4926 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
4927 }
4928 vim_free(pat);
4929 ExpandCleanup(&xpc);
4930}
4931#endif
4932
4933/*
4934 * "getcwd()" function
4935 */
4936 static void
4937f_getcwd(typval_T *argvars, typval_T *rettv)
4938{
4939 win_T *wp = NULL;
4940 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01004941 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004942
4943 rettv->v_type = VAR_STRING;
4944 rettv->vval.v_string = NULL;
4945
Bram Moolenaar54591292018-02-09 20:53:59 +01004946 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
4947 global = TRUE;
4948 else
4949 wp = find_tabwin(&argvars[0], &argvars[1]);
4950
4951 if (wp != NULL && wp->w_localdir != NULL)
4952 rettv->vval.v_string = vim_strsave(wp->w_localdir);
4953 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004954 {
Bram Moolenaar54591292018-02-09 20:53:59 +01004955 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004956 rettv->vval.v_string = vim_strsave(globaldir);
4957 else
4958 {
4959 cwd = alloc(MAXPATHL);
4960 if (cwd != NULL)
4961 {
4962 if (mch_dirname(cwd, MAXPATHL) != FAIL)
4963 rettv->vval.v_string = vim_strsave(cwd);
4964 vim_free(cwd);
4965 }
4966 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004967 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02004968#ifdef BACKSLASH_IN_FILENAME
4969 if (rettv->vval.v_string != NULL)
4970 slash_adjust(rettv->vval.v_string);
4971#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004972}
4973
4974/*
4975 * "getfontname()" function
4976 */
4977 static void
4978f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
4979{
4980 rettv->v_type = VAR_STRING;
4981 rettv->vval.v_string = NULL;
4982#ifdef FEAT_GUI
4983 if (gui.in_use)
4984 {
4985 GuiFont font;
4986 char_u *name = NULL;
4987
4988 if (argvars[0].v_type == VAR_UNKNOWN)
4989 {
4990 /* Get the "Normal" font. Either the name saved by
4991 * hl_set_font_name() or from the font ID. */
4992 font = gui.norm_font;
4993 name = hl_get_font_name();
4994 }
4995 else
4996 {
4997 name = get_tv_string(&argvars[0]);
4998 if (STRCMP(name, "*") == 0) /* don't use font dialog */
4999 return;
5000 font = gui_mch_get_font(name, FALSE);
5001 if (font == NOFONT)
5002 return; /* Invalid font name, return empty string. */
5003 }
5004 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5005 if (argvars[0].v_type != VAR_UNKNOWN)
5006 gui_mch_free_font(font);
5007 }
5008#endif
5009}
5010
5011/*
5012 * "getfperm({fname})" function
5013 */
5014 static void
5015f_getfperm(typval_T *argvars, typval_T *rettv)
5016{
5017 char_u *fname;
5018 stat_T st;
5019 char_u *perm = NULL;
5020 char_u flags[] = "rwx";
5021 int i;
5022
5023 fname = get_tv_string(&argvars[0]);
5024
5025 rettv->v_type = VAR_STRING;
5026 if (mch_stat((char *)fname, &st) >= 0)
5027 {
5028 perm = vim_strsave((char_u *)"---------");
5029 if (perm != NULL)
5030 {
5031 for (i = 0; i < 9; i++)
5032 {
5033 if (st.st_mode & (1 << (8 - i)))
5034 perm[i] = flags[i % 3];
5035 }
5036 }
5037 }
5038 rettv->vval.v_string = perm;
5039}
5040
5041/*
5042 * "getfsize({fname})" function
5043 */
5044 static void
5045f_getfsize(typval_T *argvars, typval_T *rettv)
5046{
5047 char_u *fname;
5048 stat_T st;
5049
5050 fname = get_tv_string(&argvars[0]);
5051
5052 rettv->v_type = VAR_NUMBER;
5053
5054 if (mch_stat((char *)fname, &st) >= 0)
5055 {
5056 if (mch_isdir(fname))
5057 rettv->vval.v_number = 0;
5058 else
5059 {
5060 rettv->vval.v_number = (varnumber_T)st.st_size;
5061
5062 /* non-perfect check for overflow */
5063 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5064 rettv->vval.v_number = -2;
5065 }
5066 }
5067 else
5068 rettv->vval.v_number = -1;
5069}
5070
5071/*
5072 * "getftime({fname})" function
5073 */
5074 static void
5075f_getftime(typval_T *argvars, typval_T *rettv)
5076{
5077 char_u *fname;
5078 stat_T st;
5079
5080 fname = get_tv_string(&argvars[0]);
5081
5082 if (mch_stat((char *)fname, &st) >= 0)
5083 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5084 else
5085 rettv->vval.v_number = -1;
5086}
5087
5088/*
5089 * "getftype({fname})" function
5090 */
5091 static void
5092f_getftype(typval_T *argvars, typval_T *rettv)
5093{
5094 char_u *fname;
5095 stat_T st;
5096 char_u *type = NULL;
5097 char *t;
5098
5099 fname = get_tv_string(&argvars[0]);
5100
5101 rettv->v_type = VAR_STRING;
5102 if (mch_lstat((char *)fname, &st) >= 0)
5103 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 if (S_ISREG(st.st_mode))
5105 t = "file";
5106 else if (S_ISDIR(st.st_mode))
5107 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 else if (S_ISLNK(st.st_mode))
5109 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 else if (S_ISBLK(st.st_mode))
5111 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 else if (S_ISCHR(st.st_mode))
5113 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005114 else if (S_ISFIFO(st.st_mode))
5115 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005116 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005117 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005118 else
5119 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 type = vim_strsave((char_u *)t);
5121 }
5122 rettv->vval.v_string = type;
5123}
5124
5125/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005126 * "getjumplist()" function
5127 */
5128 static void
5129f_getjumplist(typval_T *argvars, typval_T *rettv)
5130{
5131#ifdef FEAT_JUMPLIST
5132 win_T *wp;
5133 int i;
5134 list_T *l;
5135 dict_T *d;
5136#endif
5137
5138 if (rettv_list_alloc(rettv) != OK)
5139 return;
5140
5141#ifdef FEAT_JUMPLIST
5142 wp = find_tabwin(&argvars[0], &argvars[1]);
5143 if (wp == NULL)
5144 return;
5145
5146 l = list_alloc();
5147 if (l == NULL)
5148 return;
5149
5150 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5151 return;
5152 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5153
Bram Moolenaar48679742018-02-13 13:33:29 +01005154 cleanup_jumplist(wp, TRUE);
5155
Bram Moolenaar4f505882018-02-10 21:06:32 +01005156 for (i = 0; i < wp->w_jumplistlen; ++i)
5157 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005158 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5159 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005160 if ((d = dict_alloc()) == NULL)
5161 return;
5162 if (list_append_dict(l, d) == FAIL)
5163 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005164 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5165 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005166# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005167 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005168# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005169 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005170 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005171 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005172 }
5173#endif
5174}
5175
5176/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005177 * "getline(lnum, [end])" function
5178 */
5179 static void
5180f_getline(typval_T *argvars, typval_T *rettv)
5181{
5182 linenr_T lnum;
5183 linenr_T end;
5184 int retlist;
5185
5186 lnum = get_tv_lnum(argvars);
5187 if (argvars[1].v_type == VAR_UNKNOWN)
5188 {
5189 end = 0;
5190 retlist = FALSE;
5191 }
5192 else
5193 {
5194 end = get_tv_lnum(&argvars[1]);
5195 retlist = TRUE;
5196 }
5197
5198 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5199}
5200
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005201#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005202 static void
5203get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5204{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005205 if (what_arg->v_type == VAR_UNKNOWN)
5206 {
5207 if (rettv_list_alloc(rettv) == OK)
5208 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005209 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005210 }
5211 else
5212 {
5213 if (rettv_dict_alloc(rettv) == OK)
5214 if (is_qf || (wp != NULL))
5215 {
5216 if (what_arg->v_type == VAR_DICT)
5217 {
5218 dict_T *d = what_arg->vval.v_dict;
5219
5220 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005221 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005222 }
5223 else
5224 EMSG(_(e_dictreq));
5225 }
5226 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005227}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005228#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005229
5230/*
5231 * "getloclist()" function
5232 */
5233 static void
5234f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5235{
5236#ifdef FEAT_QUICKFIX
5237 win_T *wp;
5238
5239 wp = find_win_by_nr(&argvars[0], NULL);
5240 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5241#endif
5242}
5243
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244/*
5245 * "getmatches()" function
5246 */
5247 static void
5248f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5249{
5250#ifdef FEAT_SEARCH_EXTRA
5251 dict_T *dict;
5252 matchitem_T *cur = curwin->w_match_head;
5253 int i;
5254
5255 if (rettv_list_alloc(rettv) == OK)
5256 {
5257 while (cur != NULL)
5258 {
5259 dict = dict_alloc();
5260 if (dict == NULL)
5261 return;
5262 if (cur->match.regprog == NULL)
5263 {
5264 /* match added with matchaddpos() */
5265 for (i = 0; i < MAXPOSMATCH; ++i)
5266 {
5267 llpos_T *llpos;
5268 char buf[6];
5269 list_T *l;
5270
5271 llpos = &cur->pos.pos[i];
5272 if (llpos->lnum == 0)
5273 break;
5274 l = list_alloc();
5275 if (l == NULL)
5276 break;
5277 list_append_number(l, (varnumber_T)llpos->lnum);
5278 if (llpos->col > 0)
5279 {
5280 list_append_number(l, (varnumber_T)llpos->col);
5281 list_append_number(l, (varnumber_T)llpos->len);
5282 }
5283 sprintf(buf, "pos%d", i + 1);
5284 dict_add_list(dict, buf, l);
5285 }
5286 }
5287 else
5288 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005289 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005291 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5292 dict_add_number(dict, "priority", (long)cur->priority);
5293 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5295 if (cur->conceal_char)
5296 {
5297 char_u buf[MB_MAXBYTES + 1];
5298
5299 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005300 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 }
5302# endif
5303 list_append_dict(rettv->vval.v_list, dict);
5304 cur = cur->next;
5305 }
5306 }
5307#endif
5308}
5309
5310/*
5311 * "getpid()" function
5312 */
5313 static void
5314f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5315{
5316 rettv->vval.v_number = mch_get_pid();
5317}
5318
5319 static void
5320getpos_both(
5321 typval_T *argvars,
5322 typval_T *rettv,
5323 int getcurpos)
5324{
5325 pos_T *fp;
5326 list_T *l;
5327 int fnum = -1;
5328
5329 if (rettv_list_alloc(rettv) == OK)
5330 {
5331 l = rettv->vval.v_list;
5332 if (getcurpos)
5333 fp = &curwin->w_cursor;
5334 else
5335 fp = var2fpos(&argvars[0], TRUE, &fnum);
5336 if (fnum != -1)
5337 list_append_number(l, (varnumber_T)fnum);
5338 else
5339 list_append_number(l, (varnumber_T)0);
5340 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5341 : (varnumber_T)0);
5342 list_append_number(l, (fp != NULL)
5343 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5344 : (varnumber_T)0);
5345 list_append_number(l,
5346#ifdef FEAT_VIRTUALEDIT
5347 (fp != NULL) ? (varnumber_T)fp->coladd :
5348#endif
5349 (varnumber_T)0);
5350 if (getcurpos)
5351 {
5352 update_curswant();
5353 list_append_number(l, curwin->w_curswant == MAXCOL ?
5354 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5355 }
5356 }
5357 else
5358 rettv->vval.v_number = FALSE;
5359}
5360
5361
5362/*
5363 * "getcurpos()" function
5364 */
5365 static void
5366f_getcurpos(typval_T *argvars, typval_T *rettv)
5367{
5368 getpos_both(argvars, rettv, TRUE);
5369}
5370
5371/*
5372 * "getpos(string)" function
5373 */
5374 static void
5375f_getpos(typval_T *argvars, typval_T *rettv)
5376{
5377 getpos_both(argvars, rettv, FALSE);
5378}
5379
5380/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005381 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 */
5383 static void
5384f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5385{
5386#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005387 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388#endif
5389}
5390
5391/*
5392 * "getreg()" function
5393 */
5394 static void
5395f_getreg(typval_T *argvars, typval_T *rettv)
5396{
5397 char_u *strregname;
5398 int regname;
5399 int arg2 = FALSE;
5400 int return_list = FALSE;
5401 int error = FALSE;
5402
5403 if (argvars[0].v_type != VAR_UNKNOWN)
5404 {
5405 strregname = get_tv_string_chk(&argvars[0]);
5406 error = strregname == NULL;
5407 if (argvars[1].v_type != VAR_UNKNOWN)
5408 {
5409 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
5410 if (!error && argvars[2].v_type != VAR_UNKNOWN)
5411 return_list = (int)get_tv_number_chk(&argvars[2], &error);
5412 }
5413 }
5414 else
5415 strregname = get_vim_var_str(VV_REG);
5416
5417 if (error)
5418 return;
5419
5420 regname = (strregname == NULL ? '"' : *strregname);
5421 if (regname == 0)
5422 regname = '"';
5423
5424 if (return_list)
5425 {
5426 rettv->v_type = VAR_LIST;
5427 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5428 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5429 if (rettv->vval.v_list == NULL)
5430 (void)rettv_list_alloc(rettv);
5431 else
5432 ++rettv->vval.v_list->lv_refcount;
5433 }
5434 else
5435 {
5436 rettv->v_type = VAR_STRING;
5437 rettv->vval.v_string = get_reg_contents(regname,
5438 arg2 ? GREG_EXPR_SRC : 0);
5439 }
5440}
5441
5442/*
5443 * "getregtype()" function
5444 */
5445 static void
5446f_getregtype(typval_T *argvars, typval_T *rettv)
5447{
5448 char_u *strregname;
5449 int regname;
5450 char_u buf[NUMBUFLEN + 2];
5451 long reglen = 0;
5452
5453 if (argvars[0].v_type != VAR_UNKNOWN)
5454 {
5455 strregname = get_tv_string_chk(&argvars[0]);
5456 if (strregname == NULL) /* type error; errmsg already given */
5457 {
5458 rettv->v_type = VAR_STRING;
5459 rettv->vval.v_string = NULL;
5460 return;
5461 }
5462 }
5463 else
5464 /* Default to v:register */
5465 strregname = get_vim_var_str(VV_REG);
5466
5467 regname = (strregname == NULL ? '"' : *strregname);
5468 if (regname == 0)
5469 regname = '"';
5470
5471 buf[0] = NUL;
5472 buf[1] = NUL;
5473 switch (get_reg_type(regname, &reglen))
5474 {
5475 case MLINE: buf[0] = 'V'; break;
5476 case MCHAR: buf[0] = 'v'; break;
5477 case MBLOCK:
5478 buf[0] = Ctrl_V;
5479 sprintf((char *)buf + 1, "%ld", reglen + 1);
5480 break;
5481 }
5482 rettv->v_type = VAR_STRING;
5483 rettv->vval.v_string = vim_strsave(buf);
5484}
5485
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005486/*
5487 * Returns information (variables, options, etc.) about a tab page
5488 * as a dictionary.
5489 */
5490 static dict_T *
5491get_tabpage_info(tabpage_T *tp, int tp_idx)
5492{
5493 win_T *wp;
5494 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005495 list_T *l;
5496
5497 dict = dict_alloc();
5498 if (dict == NULL)
5499 return NULL;
5500
Bram Moolenaare0be1672018-07-08 16:50:37 +02005501 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005502
5503 l = list_alloc();
5504 if (l != NULL)
5505 {
5506 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5507 wp; wp = wp->w_next)
5508 list_append_number(l, (varnumber_T)wp->w_id);
5509 dict_add_list(dict, "windows", l);
5510 }
5511
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005512 /* Make a reference to tabpage variables */
5513 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005514
5515 return dict;
5516}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005517
5518/*
5519 * "gettabinfo()" function
5520 */
5521 static void
5522f_gettabinfo(typval_T *argvars, typval_T *rettv)
5523{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005524 tabpage_T *tp, *tparg = NULL;
5525 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005526 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005527
5528 if (rettv_list_alloc(rettv) != OK)
5529 return;
5530
5531 if (argvars[0].v_type != VAR_UNKNOWN)
5532 {
5533 /* Information about one tab page */
5534 tparg = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5535 if (tparg == NULL)
5536 return;
5537 }
5538
5539 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005540 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005541 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005542 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005543 if (tparg != NULL && tp != tparg)
5544 continue;
5545 d = get_tabpage_info(tp, tpnr);
5546 if (d != NULL)
5547 list_append_dict(rettv->vval.v_list, d);
5548 if (tparg != NULL)
5549 return;
5550 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005551}
5552
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553/*
5554 * "gettabvar()" function
5555 */
5556 static void
5557f_gettabvar(typval_T *argvars, typval_T *rettv)
5558{
5559 win_T *oldcurwin;
5560 tabpage_T *tp, *oldtabpage;
5561 dictitem_T *v;
5562 char_u *varname;
5563 int done = FALSE;
5564
5565 rettv->v_type = VAR_STRING;
5566 rettv->vval.v_string = NULL;
5567
5568 varname = get_tv_string_chk(&argvars[1]);
5569 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
5570 if (tp != NULL && varname != NULL)
5571 {
5572 /* Set tp to be our tabpage, temporarily. Also set the window to the
5573 * first window in the tabpage, otherwise the window is not valid. */
5574 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005575 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5576 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577 {
5578 /* look up the variable */
5579 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5580 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5581 if (v != NULL)
5582 {
5583 copy_tv(&v->di_tv, rettv);
5584 done = TRUE;
5585 }
5586 }
5587
5588 /* restore previous notion of curwin */
5589 restore_win(oldcurwin, oldtabpage, TRUE);
5590 }
5591
5592 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5593 copy_tv(&argvars[2], rettv);
5594}
5595
5596/*
5597 * "gettabwinvar()" function
5598 */
5599 static void
5600f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5601{
5602 getwinvar(argvars, rettv, 1);
5603}
5604
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005605/*
5606 * Returns information about a window as a dictionary.
5607 */
5608 static dict_T *
5609get_win_info(win_T *wp, short tpnr, short winnr)
5610{
5611 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005612
5613 dict = dict_alloc();
5614 if (dict == NULL)
5615 return NULL;
5616
Bram Moolenaare0be1672018-07-08 16:50:37 +02005617 dict_add_number(dict, "tabnr", tpnr);
5618 dict_add_number(dict, "winnr", winnr);
5619 dict_add_number(dict, "winid", wp->w_id);
5620 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005621 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005622#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005623 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005624#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005625 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005626 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005627 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005628
Bram Moolenaar69905d12017-08-13 18:14:47 +02005629#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005630 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005631#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005632#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005633 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5634 dict_add_number(dict, "loclist",
5635 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005636#endif
5637
Bram Moolenaar30567352016-08-27 21:25:44 +02005638 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005639 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005640
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005641 return dict;
5642}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643
5644/*
5645 * "getwininfo()" function
5646 */
5647 static void
5648f_getwininfo(typval_T *argvars, typval_T *rettv)
5649{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005650 tabpage_T *tp;
5651 win_T *wp = NULL, *wparg = NULL;
5652 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005653 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654
5655 if (rettv_list_alloc(rettv) != OK)
5656 return;
5657
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005658 if (argvars[0].v_type != VAR_UNKNOWN)
5659 {
5660 wparg = win_id2wp(argvars);
5661 if (wparg == NULL)
5662 return;
5663 }
5664
5665 /* Collect information about either all the windows across all the tab
5666 * pages or one particular window.
5667 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005668 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005669 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005670 tabnr++;
5671 winnr = 0;
5672 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005673 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005674 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675 if (wparg != NULL && wp != wparg)
5676 continue;
5677 d = get_win_info(wp, tabnr, winnr);
5678 if (d != NULL)
5679 list_append_dict(rettv->vval.v_list, d);
5680 if (wparg != NULL)
5681 /* found information about a specific window */
5682 return;
5683 }
5684 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005685}
5686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 * "win_findbuf()" function
5689 */
5690 static void
5691f_win_findbuf(typval_T *argvars, typval_T *rettv)
5692{
5693 if (rettv_list_alloc(rettv) != FAIL)
5694 win_findbuf(argvars, rettv->vval.v_list);
5695}
5696
5697/*
5698 * "win_getid()" function
5699 */
5700 static void
5701f_win_getid(typval_T *argvars, typval_T *rettv)
5702{
5703 rettv->vval.v_number = win_getid(argvars);
5704}
5705
5706/*
5707 * "win_gotoid()" function
5708 */
5709 static void
5710f_win_gotoid(typval_T *argvars, typval_T *rettv)
5711{
5712 rettv->vval.v_number = win_gotoid(argvars);
5713}
5714
5715/*
5716 * "win_id2tabwin()" function
5717 */
5718 static void
5719f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5720{
5721 if (rettv_list_alloc(rettv) != FAIL)
5722 win_id2tabwin(argvars, rettv->vval.v_list);
5723}
5724
5725/*
5726 * "win_id2win()" function
5727 */
5728 static void
5729f_win_id2win(typval_T *argvars, typval_T *rettv)
5730{
5731 rettv->vval.v_number = win_id2win(argvars);
5732}
5733
5734/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005735 * "win_screenpos()" function
5736 */
5737 static void
5738f_win_screenpos(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *wp;
5741
5742 if (rettv_list_alloc(rettv) == FAIL)
5743 return;
5744
5745 wp = find_win_by_nr(&argvars[0], NULL);
5746 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5747 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5748}
5749
5750/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005751 * "getwinpos({timeout})" function
5752 */
5753 static void
5754f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 int x = -1;
5757 int y = -1;
5758
5759 if (rettv_list_alloc(rettv) == FAIL)
5760 return;
5761#ifdef FEAT_GUI
5762 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005763 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005764# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5765 else
5766# endif
5767#endif
5768#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5769 {
5770 varnumber_T timeout = 100;
5771
5772 if (argvars[0].v_type != VAR_UNKNOWN)
5773 timeout = get_tv_number(&argvars[0]);
5774 term_get_winpos(&x, &y, timeout);
5775 }
5776#endif
5777 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5778 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5779}
5780
5781
5782/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005783 * "getwinposx()" function
5784 */
5785 static void
5786f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5787{
5788 rettv->vval.v_number = -1;
5789#ifdef FEAT_GUI
5790 if (gui.in_use)
5791 {
5792 int x, y;
5793
5794 if (gui_mch_get_winpos(&x, &y) == OK)
5795 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005796 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005797 }
5798#endif
5799#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5800 {
5801 int x, y;
5802
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005803 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005804 rettv->vval.v_number = x;
5805 }
5806#endif
5807}
5808
5809/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810 * "getwinposy()" function
5811 */
5812 static void
5813f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5814{
5815 rettv->vval.v_number = -1;
5816#ifdef FEAT_GUI
5817 if (gui.in_use)
5818 {
5819 int x, y;
5820
5821 if (gui_mch_get_winpos(&x, &y) == OK)
5822 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005823 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005824 }
5825#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005826#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5827 {
5828 int x, y;
5829
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005830 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005831 rettv->vval.v_number = y;
5832 }
5833#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834}
5835
5836/*
5837 * "getwinvar()" function
5838 */
5839 static void
5840f_getwinvar(typval_T *argvars, typval_T *rettv)
5841{
5842 getwinvar(argvars, rettv, 0);
5843}
5844
5845/*
5846 * "glob()" function
5847 */
5848 static void
5849f_glob(typval_T *argvars, typval_T *rettv)
5850{
5851 int options = WILD_SILENT|WILD_USE_NL;
5852 expand_T xpc;
5853 int error = FALSE;
5854
5855 /* When the optional second argument is non-zero, don't remove matches
5856 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5857 rettv->v_type = VAR_STRING;
5858 if (argvars[1].v_type != VAR_UNKNOWN)
5859 {
5860 if (get_tv_number_chk(&argvars[1], &error))
5861 options |= WILD_KEEP_ALL;
5862 if (argvars[2].v_type != VAR_UNKNOWN)
5863 {
5864 if (get_tv_number_chk(&argvars[2], &error))
5865 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005866 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005867 }
5868 if (argvars[3].v_type != VAR_UNKNOWN
5869 && get_tv_number_chk(&argvars[3], &error))
5870 options |= WILD_ALLLINKS;
5871 }
5872 }
5873 if (!error)
5874 {
5875 ExpandInit(&xpc);
5876 xpc.xp_context = EXPAND_FILES;
5877 if (p_wic)
5878 options += WILD_ICASE;
5879 if (rettv->v_type == VAR_STRING)
5880 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
5881 NULL, options, WILD_ALL);
5882 else if (rettv_list_alloc(rettv) != FAIL)
5883 {
5884 int i;
5885
5886 ExpandOne(&xpc, get_tv_string(&argvars[0]),
5887 NULL, options, WILD_ALL_KEEP);
5888 for (i = 0; i < xpc.xp_numfiles; i++)
5889 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5890
5891 ExpandCleanup(&xpc);
5892 }
5893 }
5894 else
5895 rettv->vval.v_string = NULL;
5896}
5897
5898/*
5899 * "globpath()" function
5900 */
5901 static void
5902f_globpath(typval_T *argvars, typval_T *rettv)
5903{
5904 int flags = 0;
5905 char_u buf1[NUMBUFLEN];
5906 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
5907 int error = FALSE;
5908 garray_T ga;
5909 int i;
5910
5911 /* When the optional second argument is non-zero, don't remove matches
5912 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5913 rettv->v_type = VAR_STRING;
5914 if (argvars[2].v_type != VAR_UNKNOWN)
5915 {
5916 if (get_tv_number_chk(&argvars[2], &error))
5917 flags |= WILD_KEEP_ALL;
5918 if (argvars[3].v_type != VAR_UNKNOWN)
5919 {
5920 if (get_tv_number_chk(&argvars[3], &error))
5921 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02005922 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005923 }
5924 if (argvars[4].v_type != VAR_UNKNOWN
5925 && get_tv_number_chk(&argvars[4], &error))
5926 flags |= WILD_ALLLINKS;
5927 }
5928 }
5929 if (file != NULL && !error)
5930 {
5931 ga_init2(&ga, (int)sizeof(char_u *), 10);
5932 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
5933 if (rettv->v_type == VAR_STRING)
5934 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
5935 else if (rettv_list_alloc(rettv) != FAIL)
5936 for (i = 0; i < ga.ga_len; ++i)
5937 list_append_string(rettv->vval.v_list,
5938 ((char_u **)(ga.ga_data))[i], -1);
5939 ga_clear_strings(&ga);
5940 }
5941 else
5942 rettv->vval.v_string = NULL;
5943}
5944
5945/*
5946 * "glob2regpat()" function
5947 */
5948 static void
5949f_glob2regpat(typval_T *argvars, typval_T *rettv)
5950{
5951 char_u *pat = get_tv_string_chk(&argvars[0]);
5952
5953 rettv->v_type = VAR_STRING;
5954 rettv->vval.v_string = (pat == NULL)
5955 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
5956}
5957
5958/* for VIM_VERSION_ defines */
5959#include "version.h"
5960
5961/*
5962 * "has()" function
5963 */
5964 static void
5965f_has(typval_T *argvars, typval_T *rettv)
5966{
5967 int i;
5968 char_u *name;
5969 int n = FALSE;
5970 static char *(has_list[]) =
5971 {
5972#ifdef AMIGA
5973 "amiga",
5974# ifdef FEAT_ARP
5975 "arp",
5976# endif
5977#endif
5978#ifdef __BEOS__
5979 "beos",
5980#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02005981#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01005982 "mac", /* Mac OS X (and, once, Mac OS Classic) */
5983 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02005984# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01005985 "macunix", /* Mac OS X, with the darwin feature */
5986 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02005987# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005988#endif
5989#ifdef __QNX__
5990 "qnx",
5991#endif
5992#ifdef UNIX
5993 "unix",
5994#endif
5995#ifdef VMS
5996 "vms",
5997#endif
5998#ifdef WIN32
5999 "win32",
6000#endif
6001#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6002 "win32unix",
6003#endif
6004#if defined(WIN64) || defined(_WIN64)
6005 "win64",
6006#endif
6007#ifdef EBCDIC
6008 "ebcdic",
6009#endif
6010#ifndef CASE_INSENSITIVE_FILENAME
6011 "fname_case",
6012#endif
6013#ifdef HAVE_ACL
6014 "acl",
6015#endif
6016#ifdef FEAT_ARABIC
6017 "arabic",
6018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006019 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006020#ifdef FEAT_AUTOCHDIR
6021 "autochdir",
6022#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006023#ifdef FEAT_AUTOSERVERNAME
6024 "autoservername",
6025#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006026#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 "balloon_eval",
6028# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6029 "balloon_multiline",
6030# endif
6031#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006032#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006033 "balloon_eval_term",
6034#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6036 "builtin_terms",
6037# ifdef ALL_BUILTIN_TCAPS
6038 "all_builtin_terms",
6039# endif
6040#endif
6041#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6042 || defined(FEAT_GUI_W32) \
6043 || defined(FEAT_GUI_MOTIF))
6044 "browsefilter",
6045#endif
6046#ifdef FEAT_BYTEOFF
6047 "byte_offset",
6048#endif
6049#ifdef FEAT_JOB_CHANNEL
6050 "channel",
6051#endif
6052#ifdef FEAT_CINDENT
6053 "cindent",
6054#endif
6055#ifdef FEAT_CLIENTSERVER
6056 "clientserver",
6057#endif
6058#ifdef FEAT_CLIPBOARD
6059 "clipboard",
6060#endif
6061#ifdef FEAT_CMDL_COMPL
6062 "cmdline_compl",
6063#endif
6064#ifdef FEAT_CMDHIST
6065 "cmdline_hist",
6066#endif
6067#ifdef FEAT_COMMENTS
6068 "comments",
6069#endif
6070#ifdef FEAT_CONCEAL
6071 "conceal",
6072#endif
6073#ifdef FEAT_CRYPT
6074 "cryptv",
6075 "crypt-blowfish",
6076 "crypt-blowfish2",
6077#endif
6078#ifdef FEAT_CSCOPE
6079 "cscope",
6080#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006081 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082#ifdef CURSOR_SHAPE
6083 "cursorshape",
6084#endif
6085#ifdef DEBUG
6086 "debug",
6087#endif
6088#ifdef FEAT_CON_DIALOG
6089 "dialog_con",
6090#endif
6091#ifdef FEAT_GUI_DIALOG
6092 "dialog_gui",
6093#endif
6094#ifdef FEAT_DIFF
6095 "diff",
6096#endif
6097#ifdef FEAT_DIGRAPHS
6098 "digraphs",
6099#endif
6100#ifdef FEAT_DIRECTX
6101 "directx",
6102#endif
6103#ifdef FEAT_DND
6104 "dnd",
6105#endif
6106#ifdef FEAT_EMACS_TAGS
6107 "emacs_tags",
6108#endif
6109 "eval", /* always present, of course! */
6110 "ex_extra", /* graduated feature */
6111#ifdef FEAT_SEARCH_EXTRA
6112 "extra_search",
6113#endif
6114#ifdef FEAT_FKMAP
6115 "farsi",
6116#endif
6117#ifdef FEAT_SEARCHPATH
6118 "file_in_path",
6119#endif
6120#ifdef FEAT_FILTERPIPE
6121 "filterpipe",
6122#endif
6123#ifdef FEAT_FIND_ID
6124 "find_in_path",
6125#endif
6126#ifdef FEAT_FLOAT
6127 "float",
6128#endif
6129#ifdef FEAT_FOLDING
6130 "folding",
6131#endif
6132#ifdef FEAT_FOOTER
6133 "footer",
6134#endif
6135#if !defined(USE_SYSTEM) && defined(UNIX)
6136 "fork",
6137#endif
6138#ifdef FEAT_GETTEXT
6139 "gettext",
6140#endif
6141#ifdef FEAT_GUI
6142 "gui",
6143#endif
6144#ifdef FEAT_GUI_ATHENA
6145# ifdef FEAT_GUI_NEXTAW
6146 "gui_neXtaw",
6147# else
6148 "gui_athena",
6149# endif
6150#endif
6151#ifdef FEAT_GUI_GTK
6152 "gui_gtk",
6153# ifdef USE_GTK3
6154 "gui_gtk3",
6155# else
6156 "gui_gtk2",
6157# endif
6158#endif
6159#ifdef FEAT_GUI_GNOME
6160 "gui_gnome",
6161#endif
6162#ifdef FEAT_GUI_MAC
6163 "gui_mac",
6164#endif
6165#ifdef FEAT_GUI_MOTIF
6166 "gui_motif",
6167#endif
6168#ifdef FEAT_GUI_PHOTON
6169 "gui_photon",
6170#endif
6171#ifdef FEAT_GUI_W32
6172 "gui_win32",
6173#endif
6174#ifdef FEAT_HANGULIN
6175 "hangul_input",
6176#endif
6177#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6178 "iconv",
6179#endif
6180#ifdef FEAT_INS_EXPAND
6181 "insert_expand",
6182#endif
6183#ifdef FEAT_JOB_CHANNEL
6184 "job",
6185#endif
6186#ifdef FEAT_JUMPLIST
6187 "jumplist",
6188#endif
6189#ifdef FEAT_KEYMAP
6190 "keymap",
6191#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006192 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006193#ifdef FEAT_LANGMAP
6194 "langmap",
6195#endif
6196#ifdef FEAT_LIBCALL
6197 "libcall",
6198#endif
6199#ifdef FEAT_LINEBREAK
6200 "linebreak",
6201#endif
6202#ifdef FEAT_LISP
6203 "lispindent",
6204#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006205 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006206#ifdef FEAT_LOCALMAP
6207 "localmap",
6208#endif
6209#ifdef FEAT_LUA
6210# ifndef DYNAMIC_LUA
6211 "lua",
6212# endif
6213#endif
6214#ifdef FEAT_MENU
6215 "menu",
6216#endif
6217#ifdef FEAT_SESSION
6218 "mksession",
6219#endif
6220#ifdef FEAT_MODIFY_FNAME
6221 "modify_fname",
6222#endif
6223#ifdef FEAT_MOUSE
6224 "mouse",
6225#endif
6226#ifdef FEAT_MOUSESHAPE
6227 "mouseshape",
6228#endif
6229#if defined(UNIX) || defined(VMS)
6230# ifdef FEAT_MOUSE_DEC
6231 "mouse_dec",
6232# endif
6233# ifdef FEAT_MOUSE_GPM
6234 "mouse_gpm",
6235# endif
6236# ifdef FEAT_MOUSE_JSB
6237 "mouse_jsbterm",
6238# endif
6239# ifdef FEAT_MOUSE_NET
6240 "mouse_netterm",
6241# endif
6242# ifdef FEAT_MOUSE_PTERM
6243 "mouse_pterm",
6244# endif
6245# ifdef FEAT_MOUSE_SGR
6246 "mouse_sgr",
6247# endif
6248# ifdef FEAT_SYSMOUSE
6249 "mouse_sysmouse",
6250# endif
6251# ifdef FEAT_MOUSE_URXVT
6252 "mouse_urxvt",
6253# endif
6254# ifdef FEAT_MOUSE_XTERM
6255 "mouse_xterm",
6256# endif
6257#endif
6258#ifdef FEAT_MBYTE
6259 "multi_byte",
6260#endif
6261#ifdef FEAT_MBYTE_IME
6262 "multi_byte_ime",
6263#endif
6264#ifdef FEAT_MULTI_LANG
6265 "multi_lang",
6266#endif
6267#ifdef FEAT_MZSCHEME
6268#ifndef DYNAMIC_MZSCHEME
6269 "mzscheme",
6270#endif
6271#endif
6272#ifdef FEAT_NUM64
6273 "num64",
6274#endif
6275#ifdef FEAT_OLE
6276 "ole",
6277#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006278#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006279 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006280#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006281#ifdef FEAT_PATH_EXTRA
6282 "path_extra",
6283#endif
6284#ifdef FEAT_PERL
6285#ifndef DYNAMIC_PERL
6286 "perl",
6287#endif
6288#endif
6289#ifdef FEAT_PERSISTENT_UNDO
6290 "persistent_undo",
6291#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006292#if defined(FEAT_PYTHON)
6293 "python_compiled",
6294# if defined(DYNAMIC_PYTHON)
6295 "python_dynamic",
6296# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006298 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006299# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006301#if defined(FEAT_PYTHON3)
6302 "python3_compiled",
6303# if defined(DYNAMIC_PYTHON3)
6304 "python3_dynamic",
6305# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006307 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006308# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006309#endif
6310#ifdef FEAT_POSTSCRIPT
6311 "postscript",
6312#endif
6313#ifdef FEAT_PRINTER
6314 "printer",
6315#endif
6316#ifdef FEAT_PROFILE
6317 "profile",
6318#endif
6319#ifdef FEAT_RELTIME
6320 "reltime",
6321#endif
6322#ifdef FEAT_QUICKFIX
6323 "quickfix",
6324#endif
6325#ifdef FEAT_RIGHTLEFT
6326 "rightleft",
6327#endif
6328#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6329 "ruby",
6330#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006331 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332#ifdef FEAT_CMDL_INFO
6333 "showcmd",
6334 "cmdline_info",
6335#endif
6336#ifdef FEAT_SIGNS
6337 "signs",
6338#endif
6339#ifdef FEAT_SMARTINDENT
6340 "smartindent",
6341#endif
6342#ifdef STARTUPTIME
6343 "startuptime",
6344#endif
6345#ifdef FEAT_STL_OPT
6346 "statusline",
6347#endif
6348#ifdef FEAT_SUN_WORKSHOP
6349 "sun_workshop",
6350#endif
6351#ifdef FEAT_NETBEANS_INTG
6352 "netbeans_intg",
6353#endif
6354#ifdef FEAT_SPELL
6355 "spell",
6356#endif
6357#ifdef FEAT_SYN_HL
6358 "syntax",
6359#endif
6360#if defined(USE_SYSTEM) || !defined(UNIX)
6361 "system",
6362#endif
6363#ifdef FEAT_TAG_BINS
6364 "tag_binary",
6365#endif
6366#ifdef FEAT_TAG_OLDSTATIC
6367 "tag_old_static",
6368#endif
6369#ifdef FEAT_TAG_ANYWHITE
6370 "tag_any_white",
6371#endif
6372#ifdef FEAT_TCL
6373# ifndef DYNAMIC_TCL
6374 "tcl",
6375# endif
6376#endif
6377#ifdef FEAT_TERMGUICOLORS
6378 "termguicolors",
6379#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006380#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006381 "terminal",
6382#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006383#ifdef TERMINFO
6384 "terminfo",
6385#endif
6386#ifdef FEAT_TERMRESPONSE
6387 "termresponse",
6388#endif
6389#ifdef FEAT_TEXTOBJ
6390 "textobjects",
6391#endif
6392#ifdef HAVE_TGETENT
6393 "tgetent",
6394#endif
6395#ifdef FEAT_TIMERS
6396 "timers",
6397#endif
6398#ifdef FEAT_TITLE
6399 "title",
6400#endif
6401#ifdef FEAT_TOOLBAR
6402 "toolbar",
6403#endif
6404#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6405 "unnamedplus",
6406#endif
6407#ifdef FEAT_USR_CMDS
6408 "user-commands", /* was accidentally included in 5.4 */
6409 "user_commands",
6410#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006411#ifdef FEAT_VARTABS
6412 "vartabs",
6413#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414#ifdef FEAT_VIMINFO
6415 "viminfo",
6416#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006417 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006418#ifdef FEAT_VIRTUALEDIT
6419 "virtualedit",
6420#endif
6421 "visual",
6422#ifdef FEAT_VISUALEXTRA
6423 "visualextra",
6424#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006426#ifdef FEAT_VTP
6427 "vtp",
6428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429#ifdef FEAT_WILDIGN
6430 "wildignore",
6431#endif
6432#ifdef FEAT_WILDMENU
6433 "wildmenu",
6434#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006435 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006436#ifdef FEAT_WAK
6437 "winaltkeys",
6438#endif
6439#ifdef FEAT_WRITEBACKUP
6440 "writebackup",
6441#endif
6442#ifdef FEAT_XIM
6443 "xim",
6444#endif
6445#ifdef FEAT_XFONTSET
6446 "xfontset",
6447#endif
6448#ifdef FEAT_XPM_W32
6449 "xpm",
6450 "xpm_w32", /* for backward compatibility */
6451#else
6452# if defined(HAVE_XPM)
6453 "xpm",
6454# endif
6455#endif
6456#ifdef USE_XSMP
6457 "xsmp",
6458#endif
6459#ifdef USE_XSMP_INTERACT
6460 "xsmp_interact",
6461#endif
6462#ifdef FEAT_XCLIPBOARD
6463 "xterm_clipboard",
6464#endif
6465#ifdef FEAT_XTERM_SAVE
6466 "xterm_save",
6467#endif
6468#if defined(UNIX) && defined(FEAT_X11)
6469 "X11",
6470#endif
6471 NULL
6472 };
6473
6474 name = get_tv_string(&argvars[0]);
6475 for (i = 0; has_list[i] != NULL; ++i)
6476 if (STRICMP(name, has_list[i]) == 0)
6477 {
6478 n = TRUE;
6479 break;
6480 }
6481
6482 if (n == FALSE)
6483 {
6484 if (STRNICMP(name, "patch", 5) == 0)
6485 {
6486 if (name[5] == '-'
6487 && STRLEN(name) >= 11
6488 && vim_isdigit(name[6])
6489 && vim_isdigit(name[8])
6490 && vim_isdigit(name[10]))
6491 {
6492 int major = atoi((char *)name + 6);
6493 int minor = atoi((char *)name + 8);
6494
6495 /* Expect "patch-9.9.01234". */
6496 n = (major < VIM_VERSION_MAJOR
6497 || (major == VIM_VERSION_MAJOR
6498 && (minor < VIM_VERSION_MINOR
6499 || (minor == VIM_VERSION_MINOR
6500 && has_patch(atoi((char *)name + 10))))));
6501 }
6502 else
6503 n = has_patch(atoi((char *)name + 5));
6504 }
6505 else if (STRICMP(name, "vim_starting") == 0)
6506 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006507 else if (STRICMP(name, "ttyin") == 0)
6508 n = mch_input_isatty();
6509 else if (STRICMP(name, "ttyout") == 0)
6510 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511#ifdef FEAT_MBYTE
6512 else if (STRICMP(name, "multi_byte_encoding") == 0)
6513 n = has_mbyte;
6514#endif
6515#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6516 else if (STRICMP(name, "balloon_multiline") == 0)
6517 n = multiline_balloon_available();
6518#endif
6519#ifdef DYNAMIC_TCL
6520 else if (STRICMP(name, "tcl") == 0)
6521 n = tcl_enabled(FALSE);
6522#endif
6523#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6524 else if (STRICMP(name, "iconv") == 0)
6525 n = iconv_enabled(FALSE);
6526#endif
6527#ifdef DYNAMIC_LUA
6528 else if (STRICMP(name, "lua") == 0)
6529 n = lua_enabled(FALSE);
6530#endif
6531#ifdef DYNAMIC_MZSCHEME
6532 else if (STRICMP(name, "mzscheme") == 0)
6533 n = mzscheme_enabled(FALSE);
6534#endif
6535#ifdef DYNAMIC_RUBY
6536 else if (STRICMP(name, "ruby") == 0)
6537 n = ruby_enabled(FALSE);
6538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef DYNAMIC_PYTHON
6540 else if (STRICMP(name, "python") == 0)
6541 n = python_enabled(FALSE);
6542#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006543#ifdef DYNAMIC_PYTHON3
6544 else if (STRICMP(name, "python3") == 0)
6545 n = python3_enabled(FALSE);
6546#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006547#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6548 else if (STRICMP(name, "pythonx") == 0)
6549 {
6550# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6551 if (p_pyx == 0)
6552 n = python3_enabled(FALSE) || python_enabled(FALSE);
6553 else if (p_pyx == 3)
6554 n = python3_enabled(FALSE);
6555 else if (p_pyx == 2)
6556 n = python_enabled(FALSE);
6557# elif defined(DYNAMIC_PYTHON)
6558 n = python_enabled(FALSE);
6559# elif defined(DYNAMIC_PYTHON3)
6560 n = python3_enabled(FALSE);
6561# endif
6562 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006563#endif
6564#ifdef DYNAMIC_PERL
6565 else if (STRICMP(name, "perl") == 0)
6566 n = perl_enabled(FALSE);
6567#endif
6568#ifdef FEAT_GUI
6569 else if (STRICMP(name, "gui_running") == 0)
6570 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571# ifdef FEAT_BROWSE
6572 else if (STRICMP(name, "browse") == 0)
6573 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6574# endif
6575#endif
6576#ifdef FEAT_SYN_HL
6577 else if (STRICMP(name, "syntax_items") == 0)
6578 n = syntax_present(curwin);
6579#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006580#ifdef FEAT_VTP
6581 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006582 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583#endif
6584#ifdef FEAT_NETBEANS_INTG
6585 else if (STRICMP(name, "netbeans_enabled") == 0)
6586 n = netbeans_active();
6587#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006588#if defined(FEAT_TERMINAL) && defined(WIN3264)
6589 else if (STRICMP(name, "terminal") == 0)
6590 n = terminal_enabled();
6591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 }
6593
6594 rettv->vval.v_number = n;
6595}
6596
6597/*
6598 * "has_key()" function
6599 */
6600 static void
6601f_has_key(typval_T *argvars, typval_T *rettv)
6602{
6603 if (argvars[0].v_type != VAR_DICT)
6604 {
6605 EMSG(_(e_dictreq));
6606 return;
6607 }
6608 if (argvars[0].vval.v_dict == NULL)
6609 return;
6610
6611 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
6612 get_tv_string(&argvars[1]), -1) != NULL;
6613}
6614
6615/*
6616 * "haslocaldir()" function
6617 */
6618 static void
6619f_haslocaldir(typval_T *argvars, typval_T *rettv)
6620{
6621 win_T *wp = NULL;
6622
6623 wp = find_tabwin(&argvars[0], &argvars[1]);
6624 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6625}
6626
6627/*
6628 * "hasmapto()" function
6629 */
6630 static void
6631f_hasmapto(typval_T *argvars, typval_T *rettv)
6632{
6633 char_u *name;
6634 char_u *mode;
6635 char_u buf[NUMBUFLEN];
6636 int abbr = FALSE;
6637
6638 name = get_tv_string(&argvars[0]);
6639 if (argvars[1].v_type == VAR_UNKNOWN)
6640 mode = (char_u *)"nvo";
6641 else
6642 {
6643 mode = get_tv_string_buf(&argvars[1], buf);
6644 if (argvars[2].v_type != VAR_UNKNOWN)
6645 abbr = (int)get_tv_number(&argvars[2]);
6646 }
6647
6648 if (map_to_exists(name, mode, abbr))
6649 rettv->vval.v_number = TRUE;
6650 else
6651 rettv->vval.v_number = FALSE;
6652}
6653
6654/*
6655 * "histadd()" function
6656 */
6657 static void
6658f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6659{
6660#ifdef FEAT_CMDHIST
6661 int histype;
6662 char_u *str;
6663 char_u buf[NUMBUFLEN];
6664#endif
6665
6666 rettv->vval.v_number = FALSE;
6667 if (check_restricted() || check_secure())
6668 return;
6669#ifdef FEAT_CMDHIST
6670 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6671 histype = str != NULL ? get_histtype(str) : -1;
6672 if (histype >= 0)
6673 {
6674 str = get_tv_string_buf(&argvars[1], buf);
6675 if (*str != NUL)
6676 {
6677 init_history();
6678 add_to_history(histype, str, FALSE, NUL);
6679 rettv->vval.v_number = TRUE;
6680 return;
6681 }
6682 }
6683#endif
6684}
6685
6686/*
6687 * "histdel()" function
6688 */
6689 static void
6690f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6691{
6692#ifdef FEAT_CMDHIST
6693 int n;
6694 char_u buf[NUMBUFLEN];
6695 char_u *str;
6696
6697 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6698 if (str == NULL)
6699 n = 0;
6700 else if (argvars[1].v_type == VAR_UNKNOWN)
6701 /* only one argument: clear entire history */
6702 n = clr_history(get_histtype(str));
6703 else if (argvars[1].v_type == VAR_NUMBER)
6704 /* index given: remove that entry */
6705 n = del_history_idx(get_histtype(str),
6706 (int)get_tv_number(&argvars[1]));
6707 else
6708 /* string given: remove all matching entries */
6709 n = del_history_entry(get_histtype(str),
6710 get_tv_string_buf(&argvars[1], buf));
6711 rettv->vval.v_number = n;
6712#endif
6713}
6714
6715/*
6716 * "histget()" function
6717 */
6718 static void
6719f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6720{
6721#ifdef FEAT_CMDHIST
6722 int type;
6723 int idx;
6724 char_u *str;
6725
6726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
6727 if (str == NULL)
6728 rettv->vval.v_string = NULL;
6729 else
6730 {
6731 type = get_histtype(str);
6732 if (argvars[1].v_type == VAR_UNKNOWN)
6733 idx = get_history_idx(type);
6734 else
6735 idx = (int)get_tv_number_chk(&argvars[1], NULL);
6736 /* -1 on type error */
6737 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6738 }
6739#else
6740 rettv->vval.v_string = NULL;
6741#endif
6742 rettv->v_type = VAR_STRING;
6743}
6744
6745/*
6746 * "histnr()" function
6747 */
6748 static void
6749f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6750{
6751 int i;
6752
6753#ifdef FEAT_CMDHIST
6754 char_u *history = get_tv_string_chk(&argvars[0]);
6755
6756 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6757 if (i >= HIST_CMD && i < HIST_COUNT)
6758 i = get_history_idx(i);
6759 else
6760#endif
6761 i = -1;
6762 rettv->vval.v_number = i;
6763}
6764
6765/*
6766 * "highlightID(name)" function
6767 */
6768 static void
6769f_hlID(typval_T *argvars, typval_T *rettv)
6770{
6771 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
6772}
6773
6774/*
6775 * "highlight_exists()" function
6776 */
6777 static void
6778f_hlexists(typval_T *argvars, typval_T *rettv)
6779{
6780 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
6781}
6782
6783/*
6784 * "hostname()" function
6785 */
6786 static void
6787f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6788{
6789 char_u hostname[256];
6790
6791 mch_get_host_name(hostname, 256);
6792 rettv->v_type = VAR_STRING;
6793 rettv->vval.v_string = vim_strsave(hostname);
6794}
6795
6796/*
6797 * iconv() function
6798 */
6799 static void
6800f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6801{
6802#ifdef FEAT_MBYTE
6803 char_u buf1[NUMBUFLEN];
6804 char_u buf2[NUMBUFLEN];
6805 char_u *from, *to, *str;
6806 vimconv_T vimconv;
6807#endif
6808
6809 rettv->v_type = VAR_STRING;
6810 rettv->vval.v_string = NULL;
6811
6812#ifdef FEAT_MBYTE
6813 str = get_tv_string(&argvars[0]);
6814 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
6815 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
6816 vimconv.vc_type = CONV_NONE;
6817 convert_setup(&vimconv, from, to);
6818
6819 /* If the encodings are equal, no conversion needed. */
6820 if (vimconv.vc_type == CONV_NONE)
6821 rettv->vval.v_string = vim_strsave(str);
6822 else
6823 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6824
6825 convert_setup(&vimconv, NULL, NULL);
6826 vim_free(from);
6827 vim_free(to);
6828#endif
6829}
6830
6831/*
6832 * "indent()" function
6833 */
6834 static void
6835f_indent(typval_T *argvars, typval_T *rettv)
6836{
6837 linenr_T lnum;
6838
6839 lnum = get_tv_lnum(argvars);
6840 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6841 rettv->vval.v_number = get_indent_lnum(lnum);
6842 else
6843 rettv->vval.v_number = -1;
6844}
6845
6846/*
6847 * "index()" function
6848 */
6849 static void
6850f_index(typval_T *argvars, typval_T *rettv)
6851{
6852 list_T *l;
6853 listitem_T *item;
6854 long idx = 0;
6855 int ic = FALSE;
6856
6857 rettv->vval.v_number = -1;
6858 if (argvars[0].v_type != VAR_LIST)
6859 {
6860 EMSG(_(e_listreq));
6861 return;
6862 }
6863 l = argvars[0].vval.v_list;
6864 if (l != NULL)
6865 {
6866 item = l->lv_first;
6867 if (argvars[2].v_type != VAR_UNKNOWN)
6868 {
6869 int error = FALSE;
6870
6871 /* Start at specified item. Use the cached index that list_find()
6872 * sets, so that a negative number also works. */
6873 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
6874 idx = l->lv_idx;
6875 if (argvars[3].v_type != VAR_UNKNOWN)
6876 ic = (int)get_tv_number_chk(&argvars[3], &error);
6877 if (error)
6878 item = NULL;
6879 }
6880
6881 for ( ; item != NULL; item = item->li_next, ++idx)
6882 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
6883 {
6884 rettv->vval.v_number = idx;
6885 break;
6886 }
6887 }
6888}
6889
6890static int inputsecret_flag = 0;
6891
6892/*
6893 * "input()" function
6894 * Also handles inputsecret() when inputsecret is set.
6895 */
6896 static void
6897f_input(typval_T *argvars, typval_T *rettv)
6898{
6899 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
6900}
6901
6902/*
6903 * "inputdialog()" function
6904 */
6905 static void
6906f_inputdialog(typval_T *argvars, typval_T *rettv)
6907{
6908#if defined(FEAT_GUI_TEXTDIALOG)
6909 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
6910 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
6911 {
6912 char_u *message;
6913 char_u buf[NUMBUFLEN];
6914 char_u *defstr = (char_u *)"";
6915
6916 message = get_tv_string_chk(&argvars[0]);
6917 if (argvars[1].v_type != VAR_UNKNOWN
6918 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
6919 vim_strncpy(IObuff, defstr, IOSIZE - 1);
6920 else
6921 IObuff[0] = NUL;
6922 if (message != NULL && defstr != NULL
6923 && do_dialog(VIM_QUESTION, NULL, message,
6924 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
6925 rettv->vval.v_string = vim_strsave(IObuff);
6926 else
6927 {
6928 if (message != NULL && defstr != NULL
6929 && argvars[1].v_type != VAR_UNKNOWN
6930 && argvars[2].v_type != VAR_UNKNOWN)
6931 rettv->vval.v_string = vim_strsave(
6932 get_tv_string_buf(&argvars[2], buf));
6933 else
6934 rettv->vval.v_string = NULL;
6935 }
6936 rettv->v_type = VAR_STRING;
6937 }
6938 else
6939#endif
6940 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
6941}
6942
6943/*
6944 * "inputlist()" function
6945 */
6946 static void
6947f_inputlist(typval_T *argvars, typval_T *rettv)
6948{
6949 listitem_T *li;
6950 int selected;
6951 int mouse_used;
6952
6953#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006954 /* While starting up, there is no place to enter text. When running tests
6955 * with --not-a-term we assume feedkeys() will be used. */
6956 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 return;
6958#endif
6959 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
6960 {
6961 EMSG2(_(e_listarg), "inputlist()");
6962 return;
6963 }
6964
6965 msg_start();
6966 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
6967 lines_left = Rows; /* avoid more prompt */
6968 msg_scroll = TRUE;
6969 msg_clr_eos();
6970
6971 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
6972 {
6973 msg_puts(get_tv_string(&li->li_tv));
6974 msg_putchar('\n');
6975 }
6976
6977 /* Ask for choice. */
6978 selected = prompt_for_number(&mouse_used);
6979 if (mouse_used)
6980 selected -= lines_left;
6981
6982 rettv->vval.v_number = selected;
6983}
6984
6985
6986static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
6987
6988/*
6989 * "inputrestore()" function
6990 */
6991 static void
6992f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
6993{
6994 if (ga_userinput.ga_len > 0)
6995 {
6996 --ga_userinput.ga_len;
6997 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
6998 + ga_userinput.ga_len);
6999 /* default return is zero == OK */
7000 }
7001 else if (p_verbose > 1)
7002 {
7003 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
7004 rettv->vval.v_number = 1; /* Failed */
7005 }
7006}
7007
7008/*
7009 * "inputsave()" function
7010 */
7011 static void
7012f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7013{
7014 /* Add an entry to the stack of typeahead storage. */
7015 if (ga_grow(&ga_userinput, 1) == OK)
7016 {
7017 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7018 + ga_userinput.ga_len);
7019 ++ga_userinput.ga_len;
7020 /* default return is zero == OK */
7021 }
7022 else
7023 rettv->vval.v_number = 1; /* Failed */
7024}
7025
7026/*
7027 * "inputsecret()" function
7028 */
7029 static void
7030f_inputsecret(typval_T *argvars, typval_T *rettv)
7031{
7032 ++cmdline_star;
7033 ++inputsecret_flag;
7034 f_input(argvars, rettv);
7035 --cmdline_star;
7036 --inputsecret_flag;
7037}
7038
7039/*
7040 * "insert()" function
7041 */
7042 static void
7043f_insert(typval_T *argvars, typval_T *rettv)
7044{
7045 long before = 0;
7046 listitem_T *item;
7047 list_T *l;
7048 int error = FALSE;
7049
7050 if (argvars[0].v_type != VAR_LIST)
7051 EMSG2(_(e_listarg), "insert()");
7052 else if ((l = argvars[0].vval.v_list) != NULL
7053 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
7054 {
7055 if (argvars[2].v_type != VAR_UNKNOWN)
7056 before = (long)get_tv_number_chk(&argvars[2], &error);
7057 if (error)
7058 return; /* type error; errmsg already given */
7059
7060 if (before == l->lv_len)
7061 item = NULL;
7062 else
7063 {
7064 item = list_find(l, before);
7065 if (item == NULL)
7066 {
7067 EMSGN(_(e_listidx), before);
7068 l = NULL;
7069 }
7070 }
7071 if (l != NULL)
7072 {
7073 list_insert_tv(l, &argvars[1], item);
7074 copy_tv(&argvars[0], rettv);
7075 }
7076 }
7077}
7078
7079/*
7080 * "invert(expr)" function
7081 */
7082 static void
7083f_invert(typval_T *argvars, typval_T *rettv)
7084{
7085 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
7086}
7087
7088/*
7089 * "isdirectory()" function
7090 */
7091 static void
7092f_isdirectory(typval_T *argvars, typval_T *rettv)
7093{
7094 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
7095}
7096
7097/*
7098 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7099 * or it refers to a List or Dictionary that is locked.
7100 */
7101 static int
7102tv_islocked(typval_T *tv)
7103{
7104 return (tv->v_lock & VAR_LOCKED)
7105 || (tv->v_type == VAR_LIST
7106 && tv->vval.v_list != NULL
7107 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7108 || (tv->v_type == VAR_DICT
7109 && tv->vval.v_dict != NULL
7110 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7111}
7112
7113/*
7114 * "islocked()" function
7115 */
7116 static void
7117f_islocked(typval_T *argvars, typval_T *rettv)
7118{
7119 lval_T lv;
7120 char_u *end;
7121 dictitem_T *di;
7122
7123 rettv->vval.v_number = -1;
7124 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007125 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 if (end != NULL && lv.ll_name != NULL)
7127 {
7128 if (*end != NUL)
7129 EMSG(_(e_trailing));
7130 else
7131 {
7132 if (lv.ll_tv == NULL)
7133 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007134 di = find_var(lv.ll_name, NULL, TRUE);
7135 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007137 /* Consider a variable locked when:
7138 * 1. the variable itself is locked
7139 * 2. the value of the variable is locked.
7140 * 3. the List or Dict value is locked.
7141 */
7142 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7143 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007144 }
7145 }
7146 else if (lv.ll_range)
7147 EMSG(_("E786: Range not allowed"));
7148 else if (lv.ll_newkey != NULL)
7149 EMSG2(_(e_dictkey), lv.ll_newkey);
7150 else if (lv.ll_list != NULL)
7151 /* List item. */
7152 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7153 else
7154 /* Dictionary item. */
7155 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7156 }
7157 }
7158
7159 clear_lval(&lv);
7160}
7161
7162#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7163/*
7164 * "isnan()" function
7165 */
7166 static void
7167f_isnan(typval_T *argvars, typval_T *rettv)
7168{
7169 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7170 && isnan(argvars[0].vval.v_float);
7171}
7172#endif
7173
7174/*
7175 * "items(dict)" function
7176 */
7177 static void
7178f_items(typval_T *argvars, typval_T *rettv)
7179{
7180 dict_list(argvars, rettv, 2);
7181}
7182
7183#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7184/*
7185 * Get the job from the argument.
7186 * Returns NULL if the job is invalid.
7187 */
7188 static job_T *
7189get_job_arg(typval_T *tv)
7190{
7191 job_T *job;
7192
7193 if (tv->v_type != VAR_JOB)
7194 {
7195 EMSG2(_(e_invarg2), get_tv_string(tv));
7196 return NULL;
7197 }
7198 job = tv->vval.v_job;
7199
7200 if (job == NULL)
7201 EMSG(_("E916: not a valid job"));
7202 return job;
7203}
7204
7205/*
7206 * "job_getchannel()" function
7207 */
7208 static void
7209f_job_getchannel(typval_T *argvars, typval_T *rettv)
7210{
7211 job_T *job = get_job_arg(&argvars[0]);
7212
7213 if (job != NULL)
7214 {
7215 rettv->v_type = VAR_CHANNEL;
7216 rettv->vval.v_channel = job->jv_channel;
7217 if (job->jv_channel != NULL)
7218 ++job->jv_channel->ch_refcount;
7219 }
7220}
7221
7222/*
7223 * "job_info()" function
7224 */
7225 static void
7226f_job_info(typval_T *argvars, typval_T *rettv)
7227{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007228 if (argvars[0].v_type != VAR_UNKNOWN)
7229 {
7230 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007231
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007232 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7233 job_info(job, rettv->vval.v_dict);
7234 }
7235 else if (rettv_list_alloc(rettv) == OK)
7236 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007237}
7238
7239/*
7240 * "job_setoptions()" function
7241 */
7242 static void
7243f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7244{
7245 job_T *job = get_job_arg(&argvars[0]);
7246 jobopt_T opt;
7247
7248 if (job == NULL)
7249 return;
7250 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007251 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 job_set_options(job, &opt);
7253 free_job_options(&opt);
7254}
7255
7256/*
7257 * "job_start()" function
7258 */
7259 static void
7260f_job_start(typval_T *argvars, typval_T *rettv)
7261{
7262 rettv->v_type = VAR_JOB;
7263 if (check_restricted() || check_secure())
7264 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007265 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007266}
7267
7268/*
7269 * "job_status()" function
7270 */
7271 static void
7272f_job_status(typval_T *argvars, typval_T *rettv)
7273{
7274 job_T *job = get_job_arg(&argvars[0]);
7275
7276 if (job != NULL)
7277 {
7278 rettv->v_type = VAR_STRING;
7279 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7280 }
7281}
7282
7283/*
7284 * "job_stop()" function
7285 */
7286 static void
7287f_job_stop(typval_T *argvars, typval_T *rettv)
7288{
7289 job_T *job = get_job_arg(&argvars[0]);
7290
7291 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007292 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293}
7294#endif
7295
7296/*
7297 * "join()" function
7298 */
7299 static void
7300f_join(typval_T *argvars, typval_T *rettv)
7301{
7302 garray_T ga;
7303 char_u *sep;
7304
7305 if (argvars[0].v_type != VAR_LIST)
7306 {
7307 EMSG(_(e_listreq));
7308 return;
7309 }
7310 if (argvars[0].vval.v_list == NULL)
7311 return;
7312 if (argvars[1].v_type == VAR_UNKNOWN)
7313 sep = (char_u *)" ";
7314 else
7315 sep = get_tv_string_chk(&argvars[1]);
7316
7317 rettv->v_type = VAR_STRING;
7318
7319 if (sep != NULL)
7320 {
7321 ga_init2(&ga, (int)sizeof(char), 80);
7322 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7323 ga_append(&ga, NUL);
7324 rettv->vval.v_string = (char_u *)ga.ga_data;
7325 }
7326 else
7327 rettv->vval.v_string = NULL;
7328}
7329
7330/*
7331 * "js_decode()" function
7332 */
7333 static void
7334f_js_decode(typval_T *argvars, typval_T *rettv)
7335{
7336 js_read_T reader;
7337
7338 reader.js_buf = get_tv_string(&argvars[0]);
7339 reader.js_fill = NULL;
7340 reader.js_used = 0;
7341 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
7342 EMSG(_(e_invarg));
7343}
7344
7345/*
7346 * "js_encode()" function
7347 */
7348 static void
7349f_js_encode(typval_T *argvars, typval_T *rettv)
7350{
7351 rettv->v_type = VAR_STRING;
7352 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7353}
7354
7355/*
7356 * "json_decode()" function
7357 */
7358 static void
7359f_json_decode(typval_T *argvars, typval_T *rettv)
7360{
7361 js_read_T reader;
7362
7363 reader.js_buf = get_tv_string(&argvars[0]);
7364 reader.js_fill = NULL;
7365 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007366 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367}
7368
7369/*
7370 * "json_encode()" function
7371 */
7372 static void
7373f_json_encode(typval_T *argvars, typval_T *rettv)
7374{
7375 rettv->v_type = VAR_STRING;
7376 rettv->vval.v_string = json_encode(&argvars[0], 0);
7377}
7378
7379/*
7380 * "keys()" function
7381 */
7382 static void
7383f_keys(typval_T *argvars, typval_T *rettv)
7384{
7385 dict_list(argvars, rettv, 0);
7386}
7387
7388/*
7389 * "last_buffer_nr()" function.
7390 */
7391 static void
7392f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7393{
7394 int n = 0;
7395 buf_T *buf;
7396
Bram Moolenaar29323592016-07-24 22:04:11 +02007397 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007398 if (n < buf->b_fnum)
7399 n = buf->b_fnum;
7400
7401 rettv->vval.v_number = n;
7402}
7403
7404/*
7405 * "len()" function
7406 */
7407 static void
7408f_len(typval_T *argvars, typval_T *rettv)
7409{
7410 switch (argvars[0].v_type)
7411 {
7412 case VAR_STRING:
7413 case VAR_NUMBER:
7414 rettv->vval.v_number = (varnumber_T)STRLEN(
7415 get_tv_string(&argvars[0]));
7416 break;
7417 case VAR_LIST:
7418 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7419 break;
7420 case VAR_DICT:
7421 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7422 break;
7423 case VAR_UNKNOWN:
7424 case VAR_SPECIAL:
7425 case VAR_FLOAT:
7426 case VAR_FUNC:
7427 case VAR_PARTIAL:
7428 case VAR_JOB:
7429 case VAR_CHANNEL:
7430 EMSG(_("E701: Invalid type for len()"));
7431 break;
7432 }
7433}
7434
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007436libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437{
7438#ifdef FEAT_LIBCALL
7439 char_u *string_in;
7440 char_u **string_result;
7441 int nr_result;
7442#endif
7443
7444 rettv->v_type = type;
7445 if (type != VAR_NUMBER)
7446 rettv->vval.v_string = NULL;
7447
7448 if (check_restricted() || check_secure())
7449 return;
7450
7451#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007452 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7454 {
7455 string_in = NULL;
7456 if (argvars[2].v_type == VAR_STRING)
7457 string_in = argvars[2].vval.v_string;
7458 if (type == VAR_NUMBER)
7459 string_result = NULL;
7460 else
7461 string_result = &rettv->vval.v_string;
7462 if (mch_libcall(argvars[0].vval.v_string,
7463 argvars[1].vval.v_string,
7464 string_in,
7465 argvars[2].vval.v_number,
7466 string_result,
7467 &nr_result) == OK
7468 && type == VAR_NUMBER)
7469 rettv->vval.v_number = nr_result;
7470 }
7471#endif
7472}
7473
7474/*
7475 * "libcall()" function
7476 */
7477 static void
7478f_libcall(typval_T *argvars, typval_T *rettv)
7479{
7480 libcall_common(argvars, rettv, VAR_STRING);
7481}
7482
7483/*
7484 * "libcallnr()" function
7485 */
7486 static void
7487f_libcallnr(typval_T *argvars, typval_T *rettv)
7488{
7489 libcall_common(argvars, rettv, VAR_NUMBER);
7490}
7491
7492/*
7493 * "line(string)" function
7494 */
7495 static void
7496f_line(typval_T *argvars, typval_T *rettv)
7497{
7498 linenr_T lnum = 0;
7499 pos_T *fp;
7500 int fnum;
7501
7502 fp = var2fpos(&argvars[0], TRUE, &fnum);
7503 if (fp != NULL)
7504 lnum = fp->lnum;
7505 rettv->vval.v_number = lnum;
7506}
7507
7508/*
7509 * "line2byte(lnum)" function
7510 */
7511 static void
7512f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7513{
7514#ifndef FEAT_BYTEOFF
7515 rettv->vval.v_number = -1;
7516#else
7517 linenr_T lnum;
7518
7519 lnum = get_tv_lnum(argvars);
7520 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7521 rettv->vval.v_number = -1;
7522 else
7523 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7524 if (rettv->vval.v_number >= 0)
7525 ++rettv->vval.v_number;
7526#endif
7527}
7528
7529/*
7530 * "lispindent(lnum)" function
7531 */
7532 static void
7533f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7534{
7535#ifdef FEAT_LISP
7536 pos_T pos;
7537 linenr_T lnum;
7538
7539 pos = curwin->w_cursor;
7540 lnum = get_tv_lnum(argvars);
7541 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7542 {
7543 curwin->w_cursor.lnum = lnum;
7544 rettv->vval.v_number = get_lisp_indent();
7545 curwin->w_cursor = pos;
7546 }
7547 else
7548#endif
7549 rettv->vval.v_number = -1;
7550}
7551
7552/*
7553 * "localtime()" function
7554 */
7555 static void
7556f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7557{
7558 rettv->vval.v_number = (varnumber_T)time(NULL);
7559}
7560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 static void
7562get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7563{
7564 char_u *keys;
7565 char_u *which;
7566 char_u buf[NUMBUFLEN];
7567 char_u *keys_buf = NULL;
7568 char_u *rhs;
7569 int mode;
7570 int abbr = FALSE;
7571 int get_dict = FALSE;
7572 mapblock_T *mp;
7573 int buffer_local;
7574
7575 /* return empty string for failure */
7576 rettv->v_type = VAR_STRING;
7577 rettv->vval.v_string = NULL;
7578
7579 keys = get_tv_string(&argvars[0]);
7580 if (*keys == NUL)
7581 return;
7582
7583 if (argvars[1].v_type != VAR_UNKNOWN)
7584 {
7585 which = get_tv_string_buf_chk(&argvars[1], buf);
7586 if (argvars[2].v_type != VAR_UNKNOWN)
7587 {
7588 abbr = (int)get_tv_number(&argvars[2]);
7589 if (argvars[3].v_type != VAR_UNKNOWN)
7590 get_dict = (int)get_tv_number(&argvars[3]);
7591 }
7592 }
7593 else
7594 which = (char_u *)"";
7595 if (which == NULL)
7596 return;
7597
7598 mode = get_map_mode(&which, 0);
7599
7600 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7601 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7602 vim_free(keys_buf);
7603
7604 if (!get_dict)
7605 {
7606 /* Return a string. */
7607 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007608 {
7609 if (*rhs == NUL)
7610 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7611 else
7612 rettv->vval.v_string = str2special_save(rhs, FALSE);
7613 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614
7615 }
7616 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7617 {
7618 /* Return a dictionary. */
7619 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7620 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7621 dict_T *dict = rettv->vval.v_dict;
7622
Bram Moolenaare0be1672018-07-08 16:50:37 +02007623 dict_add_string(dict, "lhs", lhs);
7624 dict_add_string(dict, "rhs", mp->m_orig_str);
7625 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7626 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7627 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007628 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7629 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007630 dict_add_number(dict, "buffer", (long)buffer_local);
7631 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7632 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633
7634 vim_free(lhs);
7635 vim_free(mapmode);
7636 }
7637}
7638
7639#ifdef FEAT_FLOAT
7640/*
7641 * "log()" function
7642 */
7643 static void
7644f_log(typval_T *argvars, typval_T *rettv)
7645{
7646 float_T f = 0.0;
7647
7648 rettv->v_type = VAR_FLOAT;
7649 if (get_float_arg(argvars, &f) == OK)
7650 rettv->vval.v_float = log(f);
7651 else
7652 rettv->vval.v_float = 0.0;
7653}
7654
7655/*
7656 * "log10()" function
7657 */
7658 static void
7659f_log10(typval_T *argvars, typval_T *rettv)
7660{
7661 float_T f = 0.0;
7662
7663 rettv->v_type = VAR_FLOAT;
7664 if (get_float_arg(argvars, &f) == OK)
7665 rettv->vval.v_float = log10(f);
7666 else
7667 rettv->vval.v_float = 0.0;
7668}
7669#endif
7670
7671#ifdef FEAT_LUA
7672/*
7673 * "luaeval()" function
7674 */
7675 static void
7676f_luaeval(typval_T *argvars, typval_T *rettv)
7677{
7678 char_u *str;
7679 char_u buf[NUMBUFLEN];
7680
7681 str = get_tv_string_buf(&argvars[0], buf);
7682 do_luaeval(str, argvars + 1, rettv);
7683}
7684#endif
7685
7686/*
7687 * "map()" function
7688 */
7689 static void
7690f_map(typval_T *argvars, typval_T *rettv)
7691{
7692 filter_map(argvars, rettv, TRUE);
7693}
7694
7695/*
7696 * "maparg()" function
7697 */
7698 static void
7699f_maparg(typval_T *argvars, typval_T *rettv)
7700{
7701 get_maparg(argvars, rettv, TRUE);
7702}
7703
7704/*
7705 * "mapcheck()" function
7706 */
7707 static void
7708f_mapcheck(typval_T *argvars, typval_T *rettv)
7709{
7710 get_maparg(argvars, rettv, FALSE);
7711}
7712
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007713typedef enum
7714{
7715 MATCH_END, /* matchend() */
7716 MATCH_MATCH, /* match() */
7717 MATCH_STR, /* matchstr() */
7718 MATCH_LIST, /* matchlist() */
7719 MATCH_POS /* matchstrpos() */
7720} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007721
7722 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007723find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007724{
7725 char_u *str = NULL;
7726 long len = 0;
7727 char_u *expr = NULL;
7728 char_u *pat;
7729 regmatch_T regmatch;
7730 char_u patbuf[NUMBUFLEN];
7731 char_u strbuf[NUMBUFLEN];
7732 char_u *save_cpo;
7733 long start = 0;
7734 long nth = 1;
7735 colnr_T startcol = 0;
7736 int match = 0;
7737 list_T *l = NULL;
7738 listitem_T *li = NULL;
7739 long idx = 0;
7740 char_u *tofree = NULL;
7741
7742 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7743 save_cpo = p_cpo;
7744 p_cpo = (char_u *)"";
7745
7746 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007747 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007748 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007749 /* type MATCH_LIST: return empty list when there are no matches.
7750 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 if (rettv_list_alloc(rettv) == FAIL)
7752 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007753 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 && (list_append_string(rettv->vval.v_list,
7755 (char_u *)"", 0) == FAIL
7756 || list_append_number(rettv->vval.v_list,
7757 (varnumber_T)-1) == FAIL
7758 || list_append_number(rettv->vval.v_list,
7759 (varnumber_T)-1) == FAIL
7760 || list_append_number(rettv->vval.v_list,
7761 (varnumber_T)-1) == FAIL))
7762 {
7763 list_free(rettv->vval.v_list);
7764 rettv->vval.v_list = NULL;
7765 goto theend;
7766 }
7767 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007768 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 {
7770 rettv->v_type = VAR_STRING;
7771 rettv->vval.v_string = NULL;
7772 }
7773
7774 if (argvars[0].v_type == VAR_LIST)
7775 {
7776 if ((l = argvars[0].vval.v_list) == NULL)
7777 goto theend;
7778 li = l->lv_first;
7779 }
7780 else
7781 {
7782 expr = str = get_tv_string(&argvars[0]);
7783 len = (long)STRLEN(str);
7784 }
7785
7786 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
7787 if (pat == NULL)
7788 goto theend;
7789
7790 if (argvars[2].v_type != VAR_UNKNOWN)
7791 {
7792 int error = FALSE;
7793
7794 start = (long)get_tv_number_chk(&argvars[2], &error);
7795 if (error)
7796 goto theend;
7797 if (l != NULL)
7798 {
7799 li = list_find(l, start);
7800 if (li == NULL)
7801 goto theend;
7802 idx = l->lv_idx; /* use the cached index */
7803 }
7804 else
7805 {
7806 if (start < 0)
7807 start = 0;
7808 if (start > len)
7809 goto theend;
7810 /* When "count" argument is there ignore matches before "start",
7811 * otherwise skip part of the string. Differs when pattern is "^"
7812 * or "\<". */
7813 if (argvars[3].v_type != VAR_UNKNOWN)
7814 startcol = start;
7815 else
7816 {
7817 str += start;
7818 len -= start;
7819 }
7820 }
7821
7822 if (argvars[3].v_type != VAR_UNKNOWN)
7823 nth = (long)get_tv_number_chk(&argvars[3], &error);
7824 if (error)
7825 goto theend;
7826 }
7827
7828 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7829 if (regmatch.regprog != NULL)
7830 {
7831 regmatch.rm_ic = p_ic;
7832
7833 for (;;)
7834 {
7835 if (l != NULL)
7836 {
7837 if (li == NULL)
7838 {
7839 match = FALSE;
7840 break;
7841 }
7842 vim_free(tofree);
7843 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
7844 if (str == NULL)
7845 break;
7846 }
7847
7848 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
7849
7850 if (match && --nth <= 0)
7851 break;
7852 if (l == NULL && !match)
7853 break;
7854
7855 /* Advance to just after the match. */
7856 if (l != NULL)
7857 {
7858 li = li->li_next;
7859 ++idx;
7860 }
7861 else
7862 {
7863#ifdef FEAT_MBYTE
7864 startcol = (colnr_T)(regmatch.startp[0]
7865 + (*mb_ptr2len)(regmatch.startp[0]) - str);
7866#else
7867 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
7868#endif
7869 if (startcol > (colnr_T)len
7870 || str + startcol <= regmatch.startp[0])
7871 {
7872 match = FALSE;
7873 break;
7874 }
7875 }
7876 }
7877
7878 if (match)
7879 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007880 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881 {
7882 listitem_T *li1 = rettv->vval.v_list->lv_first;
7883 listitem_T *li2 = li1->li_next;
7884 listitem_T *li3 = li2->li_next;
7885 listitem_T *li4 = li3->li_next;
7886
7887 vim_free(li1->li_tv.vval.v_string);
7888 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
7889 (int)(regmatch.endp[0] - regmatch.startp[0]));
7890 li3->li_tv.vval.v_number =
7891 (varnumber_T)(regmatch.startp[0] - expr);
7892 li4->li_tv.vval.v_number =
7893 (varnumber_T)(regmatch.endp[0] - expr);
7894 if (l != NULL)
7895 li2->li_tv.vval.v_number = (varnumber_T)idx;
7896 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007897 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 {
7899 int i;
7900
7901 /* return list with matched string and submatches */
7902 for (i = 0; i < NSUBEXP; ++i)
7903 {
7904 if (regmatch.endp[i] == NULL)
7905 {
7906 if (list_append_string(rettv->vval.v_list,
7907 (char_u *)"", 0) == FAIL)
7908 break;
7909 }
7910 else if (list_append_string(rettv->vval.v_list,
7911 regmatch.startp[i],
7912 (int)(regmatch.endp[i] - regmatch.startp[i]))
7913 == FAIL)
7914 break;
7915 }
7916 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007917 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918 {
7919 /* return matched string */
7920 if (l != NULL)
7921 copy_tv(&li->li_tv, rettv);
7922 else
7923 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
7924 (int)(regmatch.endp[0] - regmatch.startp[0]));
7925 }
7926 else if (l != NULL)
7927 rettv->vval.v_number = idx;
7928 else
7929 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007930 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 rettv->vval.v_number =
7932 (varnumber_T)(regmatch.startp[0] - str);
7933 else
7934 rettv->vval.v_number =
7935 (varnumber_T)(regmatch.endp[0] - str);
7936 rettv->vval.v_number += (varnumber_T)(str - expr);
7937 }
7938 }
7939 vim_regfree(regmatch.regprog);
7940 }
7941
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007942theend:
7943 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007944 /* matchstrpos() without a list: drop the second item. */
7945 listitem_remove(rettv->vval.v_list,
7946 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947 vim_free(tofree);
7948 p_cpo = save_cpo;
7949}
7950
7951/*
7952 * "match()" function
7953 */
7954 static void
7955f_match(typval_T *argvars, typval_T *rettv)
7956{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007957 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958}
7959
Bram Moolenaar95e51472018-07-28 16:55:56 +02007960#ifdef FEAT_SEARCH_EXTRA
7961 static int
7962matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
7963{
7964 dictitem_T *di;
7965
7966 if (tv->v_type != VAR_DICT)
7967 {
7968 EMSG(_(e_dictreq));
7969 return FAIL;
7970 }
7971
7972 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
7973 *conceal_char = get_dict_string(tv->vval.v_dict,
7974 (char_u *)"conceal", FALSE);
7975
7976 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
7977 {
7978 *win = find_win_by_nr(&di->di_tv, NULL);
7979 if (*win == NULL)
7980 {
7981 EMSG(_("E957: Invalid window number"));
7982 return FAIL;
7983 }
7984 }
7985
7986 return OK;
7987}
7988#endif
7989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007990/*
7991 * "matchadd()" function
7992 */
7993 static void
7994f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7995{
7996#ifdef FEAT_SEARCH_EXTRA
7997 char_u buf[NUMBUFLEN];
7998 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
7999 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
8000 int prio = 10; /* default priority */
8001 int id = -1;
8002 int error = FALSE;
8003 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008004 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005
8006 rettv->vval.v_number = -1;
8007
8008 if (grp == NULL || pat == NULL)
8009 return;
8010 if (argvars[2].v_type != VAR_UNKNOWN)
8011 {
8012 prio = (int)get_tv_number_chk(&argvars[2], &error);
8013 if (argvars[3].v_type != VAR_UNKNOWN)
8014 {
8015 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008016 if (argvars[4].v_type != VAR_UNKNOWN
8017 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8018 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008019 }
8020 }
8021 if (error == TRUE)
8022 return;
8023 if (id >= 1 && id <= 3)
8024 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008025 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008026 return;
8027 }
8028
Bram Moolenaar95e51472018-07-28 16:55:56 +02008029 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008030 conceal_char);
8031#endif
8032}
8033
8034/*
8035 * "matchaddpos()" function
8036 */
8037 static void
8038f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8039{
8040#ifdef FEAT_SEARCH_EXTRA
8041 char_u buf[NUMBUFLEN];
8042 char_u *group;
8043 int prio = 10;
8044 int id = -1;
8045 int error = FALSE;
8046 list_T *l;
8047 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008048 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008049
8050 rettv->vval.v_number = -1;
8051
8052 group = get_tv_string_buf_chk(&argvars[0], buf);
8053 if (group == NULL)
8054 return;
8055
8056 if (argvars[1].v_type != VAR_LIST)
8057 {
8058 EMSG2(_(e_listarg), "matchaddpos()");
8059 return;
8060 }
8061 l = argvars[1].vval.v_list;
8062 if (l == NULL)
8063 return;
8064
8065 if (argvars[2].v_type != VAR_UNKNOWN)
8066 {
8067 prio = (int)get_tv_number_chk(&argvars[2], &error);
8068 if (argvars[3].v_type != VAR_UNKNOWN)
8069 {
8070 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008071
8072 if (argvars[4].v_type != VAR_UNKNOWN
8073 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8074 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008075 }
8076 }
8077 if (error == TRUE)
8078 return;
8079
8080 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8081 if (id == 1 || id == 2)
8082 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02008083 EMSGN(_("E798: ID is reserved for \":match\": %ld"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 return;
8085 }
8086
Bram Moolenaar95e51472018-07-28 16:55:56 +02008087 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 conceal_char);
8089#endif
8090}
8091
8092/*
8093 * "matcharg()" function
8094 */
8095 static void
8096f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8097{
8098 if (rettv_list_alloc(rettv) == OK)
8099 {
8100#ifdef FEAT_SEARCH_EXTRA
8101 int id = (int)get_tv_number(&argvars[0]);
8102 matchitem_T *m;
8103
8104 if (id >= 1 && id <= 3)
8105 {
8106 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8107 {
8108 list_append_string(rettv->vval.v_list,
8109 syn_id2name(m->hlg_id), -1);
8110 list_append_string(rettv->vval.v_list, m->pattern, -1);
8111 }
8112 else
8113 {
8114 list_append_string(rettv->vval.v_list, NULL, -1);
8115 list_append_string(rettv->vval.v_list, NULL, -1);
8116 }
8117 }
8118#endif
8119 }
8120}
8121
8122/*
8123 * "matchdelete()" function
8124 */
8125 static void
8126f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8127{
8128#ifdef FEAT_SEARCH_EXTRA
8129 rettv->vval.v_number = match_delete(curwin,
8130 (int)get_tv_number(&argvars[0]), TRUE);
8131#endif
8132}
8133
8134/*
8135 * "matchend()" function
8136 */
8137 static void
8138f_matchend(typval_T *argvars, typval_T *rettv)
8139{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008140 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008141}
8142
8143/*
8144 * "matchlist()" function
8145 */
8146 static void
8147f_matchlist(typval_T *argvars, typval_T *rettv)
8148{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008149 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150}
8151
8152/*
8153 * "matchstr()" function
8154 */
8155 static void
8156f_matchstr(typval_T *argvars, typval_T *rettv)
8157{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008158 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008159}
8160
8161/*
8162 * "matchstrpos()" function
8163 */
8164 static void
8165f_matchstrpos(typval_T *argvars, typval_T *rettv)
8166{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008167 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168}
8169
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008170 static void
8171max_min(typval_T *argvars, typval_T *rettv, int domax)
8172{
8173 varnumber_T n = 0;
8174 varnumber_T i;
8175 int error = FALSE;
8176
8177 if (argvars[0].v_type == VAR_LIST)
8178 {
8179 list_T *l;
8180 listitem_T *li;
8181
8182 l = argvars[0].vval.v_list;
8183 if (l != NULL)
8184 {
8185 li = l->lv_first;
8186 if (li != NULL)
8187 {
8188 n = get_tv_number_chk(&li->li_tv, &error);
8189 for (;;)
8190 {
8191 li = li->li_next;
8192 if (li == NULL)
8193 break;
8194 i = get_tv_number_chk(&li->li_tv, &error);
8195 if (domax ? i > n : i < n)
8196 n = i;
8197 }
8198 }
8199 }
8200 }
8201 else if (argvars[0].v_type == VAR_DICT)
8202 {
8203 dict_T *d;
8204 int first = TRUE;
8205 hashitem_T *hi;
8206 int todo;
8207
8208 d = argvars[0].vval.v_dict;
8209 if (d != NULL)
8210 {
8211 todo = (int)d->dv_hashtab.ht_used;
8212 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8213 {
8214 if (!HASHITEM_EMPTY(hi))
8215 {
8216 --todo;
8217 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
8218 if (first)
8219 {
8220 n = i;
8221 first = FALSE;
8222 }
8223 else if (domax ? i > n : i < n)
8224 n = i;
8225 }
8226 }
8227 }
8228 }
8229 else
Bram Moolenaar26b84332016-09-04 21:42:36 +02008230 EMSG2(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 rettv->vval.v_number = error ? 0 : n;
8232}
8233
8234/*
8235 * "max()" function
8236 */
8237 static void
8238f_max(typval_T *argvars, typval_T *rettv)
8239{
8240 max_min(argvars, rettv, TRUE);
8241}
8242
8243/*
8244 * "min()" function
8245 */
8246 static void
8247f_min(typval_T *argvars, typval_T *rettv)
8248{
8249 max_min(argvars, rettv, FALSE);
8250}
8251
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252/*
8253 * Create the directory in which "dir" is located, and higher levels when
8254 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008255 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 */
8257 static int
8258mkdir_recurse(char_u *dir, int prot)
8259{
8260 char_u *p;
8261 char_u *updir;
8262 int r = FAIL;
8263
8264 /* Get end of directory name in "dir".
8265 * We're done when it's "/" or "c:/". */
8266 p = gettail_sep(dir);
8267 if (p <= get_past_head(dir))
8268 return OK;
8269
8270 /* If the directory exists we're done. Otherwise: create it.*/
8271 updir = vim_strnsave(dir, (int)(p - dir));
8272 if (updir == NULL)
8273 return FAIL;
8274 if (mch_isdir(updir))
8275 r = OK;
8276 else if (mkdir_recurse(updir, prot) == OK)
8277 r = vim_mkdir_emsg(updir, prot);
8278 vim_free(updir);
8279 return r;
8280}
8281
8282#ifdef vim_mkdir
8283/*
8284 * "mkdir()" function
8285 */
8286 static void
8287f_mkdir(typval_T *argvars, typval_T *rettv)
8288{
8289 char_u *dir;
8290 char_u buf[NUMBUFLEN];
8291 int prot = 0755;
8292
8293 rettv->vval.v_number = FAIL;
8294 if (check_restricted() || check_secure())
8295 return;
8296
8297 dir = get_tv_string_buf(&argvars[0], buf);
8298 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008299 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008300
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008301 if (*gettail(dir) == NUL)
8302 /* remove trailing slashes */
8303 *gettail_sep(dir) = NUL;
8304
8305 if (argvars[1].v_type != VAR_UNKNOWN)
8306 {
8307 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 {
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008309 prot = (int)get_tv_number_chk(&argvars[2], NULL);
8310 if (prot == -1)
8311 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008313 if (STRCMP(get_tv_string(&argvars[1]), "p") == 0)
8314 {
8315 if (mch_isdir(dir))
8316 {
8317 /* With the "p" flag it's OK if the dir already exists. */
8318 rettv->vval.v_number = OK;
8319 return;
8320 }
8321 mkdir_recurse(dir, prot);
8322 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008324 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325}
8326#endif
8327
8328/*
8329 * "mode()" function
8330 */
8331 static void
8332f_mode(typval_T *argvars, typval_T *rettv)
8333{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008334 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008335
Bram Moolenaar612cc382018-07-29 15:34:26 +02008336 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337
8338 if (time_for_testing == 93784)
8339 {
8340 /* Testing the two-character code. */
8341 buf[0] = 'x';
8342 buf[1] = '!';
8343 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008344#ifdef FEAT_TERMINAL
8345 else if (term_use_loop())
8346 buf[0] = 't';
8347#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348 else if (VIsual_active)
8349 {
8350 if (VIsual_select)
8351 buf[0] = VIsual_mode + 's' - 'v';
8352 else
8353 buf[0] = VIsual_mode;
8354 }
8355 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8356 || State == CONFIRM)
8357 {
8358 buf[0] = 'r';
8359 if (State == ASKMORE)
8360 buf[1] = 'm';
8361 else if (State == CONFIRM)
8362 buf[1] = '?';
8363 }
8364 else if (State == EXTERNCMD)
8365 buf[0] = '!';
8366 else if (State & INSERT)
8367 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368 if (State & VREPLACE_FLAG)
8369 {
8370 buf[0] = 'R';
8371 buf[1] = 'v';
8372 }
8373 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008374 {
8375 if (State & REPLACE_FLAG)
8376 buf[0] = 'R';
8377 else
8378 buf[0] = 'i';
8379#ifdef FEAT_INS_EXPAND
8380 if (ins_compl_active())
8381 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008382 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008383 buf[1] = 'x';
8384#endif
8385 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008387 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 {
8389 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008390 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008391 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008392 else if (exmode_active == EXMODE_NORMAL)
8393 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008394 }
8395 else
8396 {
8397 buf[0] = 'n';
8398 if (finish_op)
8399 buf[1] = 'o';
Bram Moolenaar612cc382018-07-29 15:34:26 +02008400 else if (restart_edit == 'I' || restart_edit == 'R'
8401 || restart_edit == 'V')
8402 {
8403 buf[1] = 'i';
8404 buf[2] = restart_edit;
8405 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 }
8407
8408 /* Clear out the minor mode when the argument is not a non-zero number or
8409 * non-empty string. */
8410 if (!non_zero_arg(&argvars[0]))
8411 buf[1] = NUL;
8412
8413 rettv->vval.v_string = vim_strsave(buf);
8414 rettv->v_type = VAR_STRING;
8415}
8416
8417#if defined(FEAT_MZSCHEME) || defined(PROTO)
8418/*
8419 * "mzeval()" function
8420 */
8421 static void
8422f_mzeval(typval_T *argvars, typval_T *rettv)
8423{
8424 char_u *str;
8425 char_u buf[NUMBUFLEN];
8426
8427 str = get_tv_string_buf(&argvars[0], buf);
8428 do_mzeval(str, rettv);
8429}
8430
8431 void
8432mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8433{
8434 typval_T argvars[3];
8435
8436 argvars[0].v_type = VAR_STRING;
8437 argvars[0].vval.v_string = name;
8438 copy_tv(args, &argvars[1]);
8439 argvars[2].v_type = VAR_UNKNOWN;
8440 f_call(argvars, rettv);
8441 clear_tv(&argvars[1]);
8442}
8443#endif
8444
8445/*
8446 * "nextnonblank()" function
8447 */
8448 static void
8449f_nextnonblank(typval_T *argvars, typval_T *rettv)
8450{
8451 linenr_T lnum;
8452
8453 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8454 {
8455 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8456 {
8457 lnum = 0;
8458 break;
8459 }
8460 if (*skipwhite(ml_get(lnum)) != NUL)
8461 break;
8462 }
8463 rettv->vval.v_number = lnum;
8464}
8465
8466/*
8467 * "nr2char()" function
8468 */
8469 static void
8470f_nr2char(typval_T *argvars, typval_T *rettv)
8471{
8472 char_u buf[NUMBUFLEN];
8473
8474#ifdef FEAT_MBYTE
8475 if (has_mbyte)
8476 {
8477 int utf8 = 0;
8478
8479 if (argvars[1].v_type != VAR_UNKNOWN)
8480 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
8481 if (utf8)
8482 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8483 else
8484 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
8485 }
8486 else
8487#endif
8488 {
8489 buf[0] = (char_u)get_tv_number(&argvars[0]);
8490 buf[1] = NUL;
8491 }
8492 rettv->v_type = VAR_STRING;
8493 rettv->vval.v_string = vim_strsave(buf);
8494}
8495
8496/*
8497 * "or(expr, expr)" function
8498 */
8499 static void
8500f_or(typval_T *argvars, typval_T *rettv)
8501{
8502 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8503 | get_tv_number_chk(&argvars[1], NULL);
8504}
8505
8506/*
8507 * "pathshorten()" function
8508 */
8509 static void
8510f_pathshorten(typval_T *argvars, typval_T *rettv)
8511{
8512 char_u *p;
8513
8514 rettv->v_type = VAR_STRING;
8515 p = get_tv_string_chk(&argvars[0]);
8516 if (p == NULL)
8517 rettv->vval.v_string = NULL;
8518 else
8519 {
8520 p = vim_strsave(p);
8521 rettv->vval.v_string = p;
8522 if (p != NULL)
8523 shorten_dir(p);
8524 }
8525}
8526
8527#ifdef FEAT_PERL
8528/*
8529 * "perleval()" function
8530 */
8531 static void
8532f_perleval(typval_T *argvars, typval_T *rettv)
8533{
8534 char_u *str;
8535 char_u buf[NUMBUFLEN];
8536
8537 str = get_tv_string_buf(&argvars[0], buf);
8538 do_perleval(str, rettv);
8539}
8540#endif
8541
8542#ifdef FEAT_FLOAT
8543/*
8544 * "pow()" function
8545 */
8546 static void
8547f_pow(typval_T *argvars, typval_T *rettv)
8548{
8549 float_T fx = 0.0, fy = 0.0;
8550
8551 rettv->v_type = VAR_FLOAT;
8552 if (get_float_arg(argvars, &fx) == OK
8553 && get_float_arg(&argvars[1], &fy) == OK)
8554 rettv->vval.v_float = pow(fx, fy);
8555 else
8556 rettv->vval.v_float = 0.0;
8557}
8558#endif
8559
8560/*
8561 * "prevnonblank()" function
8562 */
8563 static void
8564f_prevnonblank(typval_T *argvars, typval_T *rettv)
8565{
8566 linenr_T lnum;
8567
8568 lnum = get_tv_lnum(argvars);
8569 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8570 lnum = 0;
8571 else
8572 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8573 --lnum;
8574 rettv->vval.v_number = lnum;
8575}
8576
8577/* This dummy va_list is here because:
8578 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8579 * - locally in the function results in a "used before set" warning
8580 * - using va_start() to initialize it gives "function with fixed args" error */
8581static va_list ap;
8582
8583/*
8584 * "printf()" function
8585 */
8586 static void
8587f_printf(typval_T *argvars, typval_T *rettv)
8588{
8589 char_u buf[NUMBUFLEN];
8590 int len;
8591 char_u *s;
8592 int saved_did_emsg = did_emsg;
8593 char *fmt;
8594
8595 rettv->v_type = VAR_STRING;
8596 rettv->vval.v_string = NULL;
8597
8598 /* Get the required length, allocate the buffer and do it for real. */
8599 did_emsg = FALSE;
8600 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008601 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (!did_emsg)
8603 {
8604 s = alloc(len + 1);
8605 if (s != NULL)
8606 {
8607 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008608 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8609 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 }
8611 }
8612 did_emsg |= saved_did_emsg;
8613}
8614
Bram Moolenaarf2732452018-06-03 14:47:35 +02008615#ifdef FEAT_JOB_CHANNEL
8616/*
8617 * "prompt_setcallback({buffer}, {callback})" function
8618 */
8619 static void
8620f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8621{
8622 buf_T *buf;
8623 char_u *callback;
8624 partial_T *partial;
8625
8626 if (check_secure())
8627 return;
8628 buf = get_buf_tv(&argvars[0], FALSE);
8629 if (buf == NULL)
8630 return;
8631
8632 callback = get_callback(&argvars[1], &partial);
8633 if (callback == NULL)
8634 return;
8635
8636 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8637 if (partial == NULL)
8638 buf->b_prompt_callback = vim_strsave(callback);
8639 else
8640 /* pointer into the partial */
8641 buf->b_prompt_callback = callback;
8642 buf->b_prompt_partial = partial;
8643}
8644
8645/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008646 * "prompt_setinterrupt({buffer}, {callback})" function
8647 */
8648 static void
8649f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8650{
8651 buf_T *buf;
8652 char_u *callback;
8653 partial_T *partial;
8654
8655 if (check_secure())
8656 return;
8657 buf = get_buf_tv(&argvars[0], FALSE);
8658 if (buf == NULL)
8659 return;
8660
8661 callback = get_callback(&argvars[1], &partial);
8662 if (callback == NULL)
8663 return;
8664
8665 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8666 if (partial == NULL)
8667 buf->b_prompt_interrupt = vim_strsave(callback);
8668 else
8669 /* pointer into the partial */
8670 buf->b_prompt_interrupt = callback;
8671 buf->b_prompt_int_partial = partial;
8672}
8673
8674/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008675 * "prompt_setprompt({buffer}, {text})" function
8676 */
8677 static void
8678f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8679{
8680 buf_T *buf;
8681 char_u *text;
8682
8683 if (check_secure())
8684 return;
8685 buf = get_buf_tv(&argvars[0], FALSE);
8686 if (buf == NULL)
8687 return;
8688
8689 text = get_tv_string(&argvars[1]);
8690 vim_free(buf->b_prompt_text);
8691 buf->b_prompt_text = vim_strsave(text);
8692}
8693#endif
8694
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695/*
8696 * "pumvisible()" function
8697 */
8698 static void
8699f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8700{
8701#ifdef FEAT_INS_EXPAND
8702 if (pum_visible())
8703 rettv->vval.v_number = 1;
8704#endif
8705}
8706
8707#ifdef FEAT_PYTHON3
8708/*
8709 * "py3eval()" function
8710 */
8711 static void
8712f_py3eval(typval_T *argvars, typval_T *rettv)
8713{
8714 char_u *str;
8715 char_u buf[NUMBUFLEN];
8716
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008717 if (p_pyx == 0)
8718 p_pyx = 3;
8719
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 str = get_tv_string_buf(&argvars[0], buf);
8721 do_py3eval(str, rettv);
8722}
8723#endif
8724
8725#ifdef FEAT_PYTHON
8726/*
8727 * "pyeval()" function
8728 */
8729 static void
8730f_pyeval(typval_T *argvars, typval_T *rettv)
8731{
8732 char_u *str;
8733 char_u buf[NUMBUFLEN];
8734
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008735 if (p_pyx == 0)
8736 p_pyx = 2;
8737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008738 str = get_tv_string_buf(&argvars[0], buf);
8739 do_pyeval(str, rettv);
8740}
8741#endif
8742
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008743#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8744/*
8745 * "pyxeval()" function
8746 */
8747 static void
8748f_pyxeval(typval_T *argvars, typval_T *rettv)
8749{
8750# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8751 init_pyxversion();
8752 if (p_pyx == 2)
8753 f_pyeval(argvars, rettv);
8754 else
8755 f_py3eval(argvars, rettv);
8756# elif defined(FEAT_PYTHON)
8757 f_pyeval(argvars, rettv);
8758# elif defined(FEAT_PYTHON3)
8759 f_py3eval(argvars, rettv);
8760# endif
8761}
8762#endif
8763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764/*
8765 * "range()" function
8766 */
8767 static void
8768f_range(typval_T *argvars, typval_T *rettv)
8769{
8770 varnumber_T start;
8771 varnumber_T end;
8772 varnumber_T stride = 1;
8773 varnumber_T i;
8774 int error = FALSE;
8775
8776 start = get_tv_number_chk(&argvars[0], &error);
8777 if (argvars[1].v_type == VAR_UNKNOWN)
8778 {
8779 end = start - 1;
8780 start = 0;
8781 }
8782 else
8783 {
8784 end = get_tv_number_chk(&argvars[1], &error);
8785 if (argvars[2].v_type != VAR_UNKNOWN)
8786 stride = get_tv_number_chk(&argvars[2], &error);
8787 }
8788
8789 if (error)
8790 return; /* type error; errmsg already given */
8791 if (stride == 0)
8792 EMSG(_("E726: Stride is zero"));
8793 else if (stride > 0 ? end + 1 < start : end - 1 > start)
8794 EMSG(_("E727: Start past end"));
8795 else
8796 {
8797 if (rettv_list_alloc(rettv) == OK)
8798 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
8799 if (list_append_number(rettv->vval.v_list,
8800 (varnumber_T)i) == FAIL)
8801 break;
8802 }
8803}
8804
8805/*
8806 * "readfile()" function
8807 */
8808 static void
8809f_readfile(typval_T *argvars, typval_T *rettv)
8810{
8811 int binary = FALSE;
8812 int failed = FALSE;
8813 char_u *fname;
8814 FILE *fd;
8815 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
8816 int io_size = sizeof(buf);
8817 int readlen; /* size of last fread() */
8818 char_u *prev = NULL; /* previously read bytes, if any */
8819 long prevlen = 0; /* length of data in prev */
8820 long prevsize = 0; /* size of prev buffer */
8821 long maxline = MAXLNUM;
8822 long cnt = 0;
8823 char_u *p; /* position in buf */
8824 char_u *start; /* start of current line */
8825
8826 if (argvars[1].v_type != VAR_UNKNOWN)
8827 {
8828 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
8829 binary = TRUE;
8830 if (argvars[2].v_type != VAR_UNKNOWN)
8831 maxline = (long)get_tv_number(&argvars[2]);
8832 }
8833
8834 if (rettv_list_alloc(rettv) == FAIL)
8835 return;
8836
8837 /* Always open the file in binary mode, library functions have a mind of
8838 * their own about CR-LF conversion. */
8839 fname = get_tv_string(&argvars[0]);
8840 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
8841 {
8842 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
8843 return;
8844 }
8845
8846 while (cnt < maxline || maxline < 0)
8847 {
8848 readlen = (int)fread(buf, 1, io_size, fd);
8849
8850 /* This for loop processes what was read, but is also entered at end
8851 * of file so that either:
8852 * - an incomplete line gets written
8853 * - a "binary" file gets an empty line at the end if it ends in a
8854 * newline. */
8855 for (p = buf, start = buf;
8856 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
8857 ++p)
8858 {
8859 if (*p == '\n' || readlen <= 0)
8860 {
8861 listitem_T *li;
8862 char_u *s = NULL;
8863 long_u len = p - start;
8864
8865 /* Finished a line. Remove CRs before NL. */
8866 if (readlen > 0 && !binary)
8867 {
8868 while (len > 0 && start[len - 1] == '\r')
8869 --len;
8870 /* removal may cross back to the "prev" string */
8871 if (len == 0)
8872 while (prevlen > 0 && prev[prevlen - 1] == '\r')
8873 --prevlen;
8874 }
8875 if (prevlen == 0)
8876 s = vim_strnsave(start, (int)len);
8877 else
8878 {
8879 /* Change "prev" buffer to be the right size. This way
8880 * the bytes are only copied once, and very long lines are
8881 * allocated only once. */
8882 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
8883 {
8884 mch_memmove(s + prevlen, start, len);
8885 s[prevlen + len] = NUL;
8886 prev = NULL; /* the list will own the string */
8887 prevlen = prevsize = 0;
8888 }
8889 }
8890 if (s == NULL)
8891 {
8892 do_outofmem_msg((long_u) prevlen + len + 1);
8893 failed = TRUE;
8894 break;
8895 }
8896
8897 if ((li = listitem_alloc()) == NULL)
8898 {
8899 vim_free(s);
8900 failed = TRUE;
8901 break;
8902 }
8903 li->li_tv.v_type = VAR_STRING;
8904 li->li_tv.v_lock = 0;
8905 li->li_tv.vval.v_string = s;
8906 list_append(rettv->vval.v_list, li);
8907
8908 start = p + 1; /* step over newline */
8909 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
8910 break;
8911 }
8912 else if (*p == NUL)
8913 *p = '\n';
8914#ifdef FEAT_MBYTE
8915 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
8916 * when finding the BF and check the previous two bytes. */
8917 else if (*p == 0xbf && enc_utf8 && !binary)
8918 {
8919 /* Find the two bytes before the 0xbf. If p is at buf, or buf
8920 * + 1, these may be in the "prev" string. */
8921 char_u back1 = p >= buf + 1 ? p[-1]
8922 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
8923 char_u back2 = p >= buf + 2 ? p[-2]
8924 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
8925 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
8926
8927 if (back2 == 0xef && back1 == 0xbb)
8928 {
8929 char_u *dest = p - 2;
8930
8931 /* Usually a BOM is at the beginning of a file, and so at
8932 * the beginning of a line; then we can just step over it.
8933 */
8934 if (start == dest)
8935 start = p + 1;
8936 else
8937 {
8938 /* have to shuffle buf to close gap */
8939 int adjust_prevlen = 0;
8940
8941 if (dest < buf)
8942 {
8943 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
8944 dest = buf;
8945 }
8946 if (readlen > p - buf + 1)
8947 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
8948 readlen -= 3 - adjust_prevlen;
8949 prevlen -= adjust_prevlen;
8950 p = dest - 1;
8951 }
8952 }
8953 }
8954#endif
8955 } /* for */
8956
8957 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
8958 break;
8959 if (start < p)
8960 {
8961 /* There's part of a line in buf, store it in "prev". */
8962 if (p - start + prevlen >= prevsize)
8963 {
8964 /* need bigger "prev" buffer */
8965 char_u *newprev;
8966
8967 /* A common use case is ordinary text files and "prev" gets a
8968 * fragment of a line, so the first allocation is made
8969 * small, to avoid repeatedly 'allocing' large and
8970 * 'reallocing' small. */
8971 if (prevsize == 0)
8972 prevsize = (long)(p - start);
8973 else
8974 {
8975 long grow50pc = (prevsize * 3) / 2;
8976 long growmin = (long)((p - start) * 2 + prevlen);
8977 prevsize = grow50pc > growmin ? grow50pc : growmin;
8978 }
8979 newprev = prev == NULL ? alloc(prevsize)
8980 : vim_realloc(prev, prevsize);
8981 if (newprev == NULL)
8982 {
8983 do_outofmem_msg((long_u)prevsize);
8984 failed = TRUE;
8985 break;
8986 }
8987 prev = newprev;
8988 }
8989 /* Add the line part to end of "prev". */
8990 mch_memmove(prev + prevlen, start, p - start);
8991 prevlen += (long)(p - start);
8992 }
8993 } /* while */
8994
8995 /*
8996 * For a negative line count use only the lines at the end of the file,
8997 * free the rest.
8998 */
8999 if (!failed && maxline < 0)
9000 while (cnt > -maxline)
9001 {
9002 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9003 --cnt;
9004 }
9005
9006 if (failed)
9007 {
9008 list_free(rettv->vval.v_list);
9009 /* readfile doc says an empty list is returned on error */
9010 rettv->vval.v_list = list_alloc();
9011 }
9012
9013 vim_free(prev);
9014 fclose(fd);
9015}
9016
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009017 static void
9018return_register(int regname, typval_T *rettv)
9019{
9020 char_u buf[2] = {0, 0};
9021
9022 buf[0] = (char_u)regname;
9023 rettv->v_type = VAR_STRING;
9024 rettv->vval.v_string = vim_strsave(buf);
9025}
9026
9027/*
9028 * "reg_executing()" function
9029 */
9030 static void
9031f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9032{
9033 return_register(reg_executing, rettv);
9034}
9035
9036/*
9037 * "reg_recording()" function
9038 */
9039 static void
9040f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9041{
9042 return_register(reg_recording, rettv);
9043}
9044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009046/*
9047 * Convert a List to proftime_T.
9048 * Return FAIL when there is something wrong.
9049 */
9050 static int
9051list2proftime(typval_T *arg, proftime_T *tm)
9052{
9053 long n1, n2;
9054 int error = FALSE;
9055
9056 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9057 || arg->vval.v_list->lv_len != 2)
9058 return FAIL;
9059 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9060 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9061# ifdef WIN3264
9062 tm->HighPart = n1;
9063 tm->LowPart = n2;
9064# else
9065 tm->tv_sec = n1;
9066 tm->tv_usec = n2;
9067# endif
9068 return error ? FAIL : OK;
9069}
9070#endif /* FEAT_RELTIME */
9071
9072/*
9073 * "reltime()" function
9074 */
9075 static void
9076f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9077{
9078#ifdef FEAT_RELTIME
9079 proftime_T res;
9080 proftime_T start;
9081
9082 if (argvars[0].v_type == VAR_UNKNOWN)
9083 {
9084 /* No arguments: get current time. */
9085 profile_start(&res);
9086 }
9087 else if (argvars[1].v_type == VAR_UNKNOWN)
9088 {
9089 if (list2proftime(&argvars[0], &res) == FAIL)
9090 return;
9091 profile_end(&res);
9092 }
9093 else
9094 {
9095 /* Two arguments: compute the difference. */
9096 if (list2proftime(&argvars[0], &start) == FAIL
9097 || list2proftime(&argvars[1], &res) == FAIL)
9098 return;
9099 profile_sub(&res, &start);
9100 }
9101
9102 if (rettv_list_alloc(rettv) == OK)
9103 {
9104 long n1, n2;
9105
9106# ifdef WIN3264
9107 n1 = res.HighPart;
9108 n2 = res.LowPart;
9109# else
9110 n1 = res.tv_sec;
9111 n2 = res.tv_usec;
9112# endif
9113 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9114 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9115 }
9116#endif
9117}
9118
9119#ifdef FEAT_FLOAT
9120/*
9121 * "reltimefloat()" function
9122 */
9123 static void
9124f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9125{
9126# ifdef FEAT_RELTIME
9127 proftime_T tm;
9128# endif
9129
9130 rettv->v_type = VAR_FLOAT;
9131 rettv->vval.v_float = 0;
9132# ifdef FEAT_RELTIME
9133 if (list2proftime(&argvars[0], &tm) == OK)
9134 rettv->vval.v_float = profile_float(&tm);
9135# endif
9136}
9137#endif
9138
9139/*
9140 * "reltimestr()" function
9141 */
9142 static void
9143f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9144{
9145#ifdef FEAT_RELTIME
9146 proftime_T tm;
9147#endif
9148
9149 rettv->v_type = VAR_STRING;
9150 rettv->vval.v_string = NULL;
9151#ifdef FEAT_RELTIME
9152 if (list2proftime(&argvars[0], &tm) == OK)
9153 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9154#endif
9155}
9156
9157#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 static void
9159make_connection(void)
9160{
9161 if (X_DISPLAY == NULL
9162# ifdef FEAT_GUI
9163 && !gui.in_use
9164# endif
9165 )
9166 {
9167 x_force_connect = TRUE;
9168 setup_term_clip();
9169 x_force_connect = FALSE;
9170 }
9171}
9172
9173 static int
9174check_connection(void)
9175{
9176 make_connection();
9177 if (X_DISPLAY == NULL)
9178 {
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009179 EMSG(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009180 return FAIL;
9181 }
9182 return OK;
9183}
9184#endif
9185
9186#ifdef FEAT_CLIENTSERVER
9187 static void
9188remote_common(typval_T *argvars, typval_T *rettv, int expr)
9189{
9190 char_u *server_name;
9191 char_u *keys;
9192 char_u *r = NULL;
9193 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009194 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009195# ifdef WIN32
9196 HWND w;
9197# else
9198 Window w;
9199# endif
9200
9201 if (check_restricted() || check_secure())
9202 return;
9203
9204# ifdef FEAT_X11
9205 if (check_connection() == FAIL)
9206 return;
9207# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009208 if (argvars[2].v_type != VAR_UNKNOWN
9209 && argvars[3].v_type != VAR_UNKNOWN)
9210 timeout = get_tv_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009211
9212 server_name = get_tv_string_chk(&argvars[0]);
9213 if (server_name == NULL)
9214 return; /* type error; errmsg already given */
9215 keys = get_tv_string_buf(&argvars[1], buf);
9216# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009217 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009218# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009219 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9220 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009221# endif
9222 {
9223 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009224 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009225 EMSG(r); /* sending worked but evaluation failed */
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009226 vim_free(r);
9227 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009228 else
9229 EMSG2(_("E241: Unable to send to %s"), server_name);
9230 return;
9231 }
9232
9233 rettv->vval.v_string = r;
9234
9235 if (argvars[2].v_type != VAR_UNKNOWN)
9236 {
9237 dictitem_T v;
9238 char_u str[30];
9239 char_u *idvar;
9240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241 idvar = get_tv_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009242 if (idvar != NULL && *idvar != NUL)
9243 {
9244 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9245 v.di_tv.v_type = VAR_STRING;
9246 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009247 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009248 vim_free(v.di_tv.vval.v_string);
9249 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009250 }
9251}
9252#endif
9253
9254/*
9255 * "remote_expr()" function
9256 */
9257 static void
9258f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9259{
9260 rettv->v_type = VAR_STRING;
9261 rettv->vval.v_string = NULL;
9262#ifdef FEAT_CLIENTSERVER
9263 remote_common(argvars, rettv, TRUE);
9264#endif
9265}
9266
9267/*
9268 * "remote_foreground()" function
9269 */
9270 static void
9271f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9272{
9273#ifdef FEAT_CLIENTSERVER
9274# ifdef WIN32
9275 /* On Win32 it's done in this application. */
9276 {
9277 char_u *server_name = get_tv_string_chk(&argvars[0]);
9278
9279 if (server_name != NULL)
9280 serverForeground(server_name);
9281 }
9282# else
9283 /* Send a foreground() expression to the server. */
9284 argvars[1].v_type = VAR_STRING;
9285 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9286 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009287 rettv->v_type = VAR_STRING;
9288 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 remote_common(argvars, rettv, TRUE);
9290 vim_free(argvars[1].vval.v_string);
9291# endif
9292#endif
9293}
9294
9295 static void
9296f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9297{
9298#ifdef FEAT_CLIENTSERVER
9299 dictitem_T v;
9300 char_u *s = NULL;
9301# ifdef WIN32
9302 long_u n = 0;
9303# endif
9304 char_u *serverid;
9305
9306 if (check_restricted() || check_secure())
9307 {
9308 rettv->vval.v_number = -1;
9309 return;
9310 }
9311 serverid = get_tv_string_chk(&argvars[0]);
9312 if (serverid == NULL)
9313 {
9314 rettv->vval.v_number = -1;
9315 return; /* type error; errmsg already given */
9316 }
9317# ifdef WIN32
9318 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9319 if (n == 0)
9320 rettv->vval.v_number = -1;
9321 else
9322 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009323 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 rettv->vval.v_number = (s != NULL);
9325 }
9326# else
9327 if (check_connection() == FAIL)
9328 return;
9329
9330 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9331 serverStrToWin(serverid), &s);
9332# endif
9333
9334 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9335 {
9336 char_u *retvar;
9337
9338 v.di_tv.v_type = VAR_STRING;
9339 v.di_tv.vval.v_string = vim_strsave(s);
9340 retvar = get_tv_string_chk(&argvars[1]);
9341 if (retvar != NULL)
9342 set_var(retvar, &v.di_tv, FALSE);
9343 vim_free(v.di_tv.vval.v_string);
9344 }
9345#else
9346 rettv->vval.v_number = -1;
9347#endif
9348}
9349
9350 static void
9351f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9352{
9353 char_u *r = NULL;
9354
9355#ifdef FEAT_CLIENTSERVER
9356 char_u *serverid = get_tv_string_chk(&argvars[0]);
9357
9358 if (serverid != NULL && !check_restricted() && !check_secure())
9359 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009360 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009361# ifdef WIN32
9362 /* The server's HWND is encoded in the 'id' parameter */
9363 long_u n = 0;
9364# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009365
9366 if (argvars[1].v_type != VAR_UNKNOWN)
9367 timeout = get_tv_number(&argvars[1]);
9368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009369# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9371 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009372 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009373 if (r == NULL)
9374# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009375 if (check_connection() == FAIL
9376 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9377 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009378# endif
9379 EMSG(_("E277: Unable to read a server reply"));
9380 }
9381#endif
9382 rettv->v_type = VAR_STRING;
9383 rettv->vval.v_string = r;
9384}
9385
9386/*
9387 * "remote_send()" function
9388 */
9389 static void
9390f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9391{
9392 rettv->v_type = VAR_STRING;
9393 rettv->vval.v_string = NULL;
9394#ifdef FEAT_CLIENTSERVER
9395 remote_common(argvars, rettv, FALSE);
9396#endif
9397}
9398
9399/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009400 * "remote_startserver()" function
9401 */
9402 static void
9403f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9404{
9405#ifdef FEAT_CLIENTSERVER
9406 char_u *server = get_tv_string_chk(&argvars[0]);
9407
9408 if (server == NULL)
9409 return; /* type error; errmsg already given */
9410 if (serverName != NULL)
9411 EMSG(_("E941: already started a server"));
9412 else
9413 {
9414# ifdef FEAT_X11
9415 if (check_connection() == OK)
9416 serverRegisterName(X_DISPLAY, server);
9417# else
9418 serverSetName(server);
9419# endif
9420 }
9421#else
9422 EMSG(_("E942: +clientserver feature not available"));
9423#endif
9424}
9425
9426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427 * "remove()" function
9428 */
9429 static void
9430f_remove(typval_T *argvars, typval_T *rettv)
9431{
9432 list_T *l;
9433 listitem_T *item, *item2;
9434 listitem_T *li;
9435 long idx;
9436 long end;
9437 char_u *key;
9438 dict_T *d;
9439 dictitem_T *di;
9440 char_u *arg_errmsg = (char_u *)N_("remove() argument");
9441
9442 if (argvars[0].v_type == VAR_DICT)
9443 {
9444 if (argvars[2].v_type != VAR_UNKNOWN)
9445 EMSG2(_(e_toomanyarg), "remove()");
9446 else if ((d = argvars[0].vval.v_dict) != NULL
9447 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9448 {
9449 key = get_tv_string_chk(&argvars[1]);
9450 if (key != NULL)
9451 {
9452 di = dict_find(d, key, -1);
9453 if (di == NULL)
9454 EMSG2(_(e_dictkey), key);
9455 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9456 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9457 {
9458 *rettv = di->di_tv;
9459 init_tv(&di->di_tv);
9460 dictitem_remove(d, di);
9461 }
9462 }
9463 }
9464 }
9465 else if (argvars[0].v_type != VAR_LIST)
9466 EMSG2(_(e_listdictarg), "remove()");
9467 else if ((l = argvars[0].vval.v_list) != NULL
9468 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
9469 {
9470 int error = FALSE;
9471
9472 idx = (long)get_tv_number_chk(&argvars[1], &error);
9473 if (error)
9474 ; /* type error: do nothing, errmsg already given */
9475 else if ((item = list_find(l, idx)) == NULL)
9476 EMSGN(_(e_listidx), idx);
9477 else
9478 {
9479 if (argvars[2].v_type == VAR_UNKNOWN)
9480 {
9481 /* Remove one item, return its value. */
9482 vimlist_remove(l, item, item);
9483 *rettv = item->li_tv;
9484 vim_free(item);
9485 }
9486 else
9487 {
9488 /* Remove range of items, return list with values. */
9489 end = (long)get_tv_number_chk(&argvars[2], &error);
9490 if (error)
9491 ; /* type error: do nothing */
9492 else if ((item2 = list_find(l, end)) == NULL)
9493 EMSGN(_(e_listidx), end);
9494 else
9495 {
9496 int cnt = 0;
9497
9498 for (li = item; li != NULL; li = li->li_next)
9499 {
9500 ++cnt;
9501 if (li == item2)
9502 break;
9503 }
9504 if (li == NULL) /* didn't find "item2" after "item" */
9505 EMSG(_(e_invrange));
9506 else
9507 {
9508 vimlist_remove(l, item, item2);
9509 if (rettv_list_alloc(rettv) == OK)
9510 {
9511 l = rettv->vval.v_list;
9512 l->lv_first = item;
9513 l->lv_last = item2;
9514 item->li_prev = NULL;
9515 item2->li_next = NULL;
9516 l->lv_len = cnt;
9517 }
9518 }
9519 }
9520 }
9521 }
9522 }
9523}
9524
9525/*
9526 * "rename({from}, {to})" function
9527 */
9528 static void
9529f_rename(typval_T *argvars, typval_T *rettv)
9530{
9531 char_u buf[NUMBUFLEN];
9532
9533 if (check_restricted() || check_secure())
9534 rettv->vval.v_number = -1;
9535 else
9536 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9537 get_tv_string_buf(&argvars[1], buf));
9538}
9539
9540/*
9541 * "repeat()" function
9542 */
9543 static void
9544f_repeat(typval_T *argvars, typval_T *rettv)
9545{
9546 char_u *p;
9547 int n;
9548 int slen;
9549 int len;
9550 char_u *r;
9551 int i;
9552
9553 n = (int)get_tv_number(&argvars[1]);
9554 if (argvars[0].v_type == VAR_LIST)
9555 {
9556 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9557 while (n-- > 0)
9558 if (list_extend(rettv->vval.v_list,
9559 argvars[0].vval.v_list, NULL) == FAIL)
9560 break;
9561 }
9562 else
9563 {
9564 p = get_tv_string(&argvars[0]);
9565 rettv->v_type = VAR_STRING;
9566 rettv->vval.v_string = NULL;
9567
9568 slen = (int)STRLEN(p);
9569 len = slen * n;
9570 if (len <= 0)
9571 return;
9572
9573 r = alloc(len + 1);
9574 if (r != NULL)
9575 {
9576 for (i = 0; i < n; i++)
9577 mch_memmove(r + i * slen, p, (size_t)slen);
9578 r[len] = NUL;
9579 }
9580
9581 rettv->vval.v_string = r;
9582 }
9583}
9584
9585/*
9586 * "resolve()" function
9587 */
9588 static void
9589f_resolve(typval_T *argvars, typval_T *rettv)
9590{
9591 char_u *p;
9592#ifdef HAVE_READLINK
9593 char_u *buf = NULL;
9594#endif
9595
9596 p = get_tv_string(&argvars[0]);
9597#ifdef FEAT_SHORTCUT
9598 {
9599 char_u *v = NULL;
9600
9601 v = mch_resolve_shortcut(p);
9602 if (v != NULL)
9603 rettv->vval.v_string = v;
9604 else
9605 rettv->vval.v_string = vim_strsave(p);
9606 }
9607#else
9608# ifdef HAVE_READLINK
9609 {
9610 char_u *cpy;
9611 int len;
9612 char_u *remain = NULL;
9613 char_u *q;
9614 int is_relative_to_current = FALSE;
9615 int has_trailing_pathsep = FALSE;
9616 int limit = 100;
9617
9618 p = vim_strsave(p);
9619
9620 if (p[0] == '.' && (vim_ispathsep(p[1])
9621 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9622 is_relative_to_current = TRUE;
9623
9624 len = STRLEN(p);
9625 if (len > 0 && after_pathsep(p, p + len))
9626 {
9627 has_trailing_pathsep = TRUE;
9628 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9629 }
9630
9631 q = getnextcomp(p);
9632 if (*q != NUL)
9633 {
9634 /* Separate the first path component in "p", and keep the
9635 * remainder (beginning with the path separator). */
9636 remain = vim_strsave(q - 1);
9637 q[-1] = NUL;
9638 }
9639
9640 buf = alloc(MAXPATHL + 1);
9641 if (buf == NULL)
9642 goto fail;
9643
9644 for (;;)
9645 {
9646 for (;;)
9647 {
9648 len = readlink((char *)p, (char *)buf, MAXPATHL);
9649 if (len <= 0)
9650 break;
9651 buf[len] = NUL;
9652
9653 if (limit-- == 0)
9654 {
9655 vim_free(p);
9656 vim_free(remain);
9657 EMSG(_("E655: Too many symbolic links (cycle?)"));
9658 rettv->vval.v_string = NULL;
9659 goto fail;
9660 }
9661
9662 /* Ensure that the result will have a trailing path separator
9663 * if the argument has one. */
9664 if (remain == NULL && has_trailing_pathsep)
9665 add_pathsep(buf);
9666
9667 /* Separate the first path component in the link value and
9668 * concatenate the remainders. */
9669 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9670 if (*q != NUL)
9671 {
9672 if (remain == NULL)
9673 remain = vim_strsave(q - 1);
9674 else
9675 {
9676 cpy = concat_str(q - 1, remain);
9677 if (cpy != NULL)
9678 {
9679 vim_free(remain);
9680 remain = cpy;
9681 }
9682 }
9683 q[-1] = NUL;
9684 }
9685
9686 q = gettail(p);
9687 if (q > p && *q == NUL)
9688 {
9689 /* Ignore trailing path separator. */
9690 q[-1] = NUL;
9691 q = gettail(p);
9692 }
9693 if (q > p && !mch_isFullName(buf))
9694 {
9695 /* symlink is relative to directory of argument */
9696 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9697 if (cpy != NULL)
9698 {
9699 STRCPY(cpy, p);
9700 STRCPY(gettail(cpy), buf);
9701 vim_free(p);
9702 p = cpy;
9703 }
9704 }
9705 else
9706 {
9707 vim_free(p);
9708 p = vim_strsave(buf);
9709 }
9710 }
9711
9712 if (remain == NULL)
9713 break;
9714
9715 /* Append the first path component of "remain" to "p". */
9716 q = getnextcomp(remain + 1);
9717 len = q - remain - (*q != NUL);
9718 cpy = vim_strnsave(p, STRLEN(p) + len);
9719 if (cpy != NULL)
9720 {
9721 STRNCAT(cpy, remain, len);
9722 vim_free(p);
9723 p = cpy;
9724 }
9725 /* Shorten "remain". */
9726 if (*q != NUL)
9727 STRMOVE(remain, q - 1);
9728 else
Bram Moolenaard23a8232018-02-10 18:45:26 +01009729 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009730 }
9731
9732 /* If the result is a relative path name, make it explicitly relative to
9733 * the current directory if and only if the argument had this form. */
9734 if (!vim_ispathsep(*p))
9735 {
9736 if (is_relative_to_current
9737 && *p != NUL
9738 && !(p[0] == '.'
9739 && (p[1] == NUL
9740 || vim_ispathsep(p[1])
9741 || (p[1] == '.'
9742 && (p[2] == NUL
9743 || vim_ispathsep(p[2]))))))
9744 {
9745 /* Prepend "./". */
9746 cpy = concat_str((char_u *)"./", p);
9747 if (cpy != NULL)
9748 {
9749 vim_free(p);
9750 p = cpy;
9751 }
9752 }
9753 else if (!is_relative_to_current)
9754 {
9755 /* Strip leading "./". */
9756 q = p;
9757 while (q[0] == '.' && vim_ispathsep(q[1]))
9758 q += 2;
9759 if (q > p)
9760 STRMOVE(p, p + 2);
9761 }
9762 }
9763
9764 /* Ensure that the result will have no trailing path separator
9765 * if the argument had none. But keep "/" or "//". */
9766 if (!has_trailing_pathsep)
9767 {
9768 q = p + STRLEN(p);
9769 if (after_pathsep(p, q))
9770 *gettail_sep(p) = NUL;
9771 }
9772
9773 rettv->vval.v_string = p;
9774 }
9775# else
9776 rettv->vval.v_string = vim_strsave(p);
9777# endif
9778#endif
9779
9780 simplify_filename(rettv->vval.v_string);
9781
9782#ifdef HAVE_READLINK
9783fail:
9784 vim_free(buf);
9785#endif
9786 rettv->v_type = VAR_STRING;
9787}
9788
9789/*
9790 * "reverse({list})" function
9791 */
9792 static void
9793f_reverse(typval_T *argvars, typval_T *rettv)
9794{
9795 list_T *l;
9796 listitem_T *li, *ni;
9797
9798 if (argvars[0].v_type != VAR_LIST)
9799 EMSG2(_(e_listarg), "reverse()");
9800 else if ((l = argvars[0].vval.v_list) != NULL
9801 && !tv_check_lock(l->lv_lock,
9802 (char_u *)N_("reverse() argument"), TRUE))
9803 {
9804 li = l->lv_last;
9805 l->lv_first = l->lv_last = NULL;
9806 l->lv_len = 0;
9807 while (li != NULL)
9808 {
9809 ni = li->li_prev;
9810 list_append(l, li);
9811 li = ni;
9812 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02009813 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009814 l->lv_idx = l->lv_len - l->lv_idx - 1;
9815 }
9816}
9817
9818#define SP_NOMOVE 0x01 /* don't move cursor */
9819#define SP_REPEAT 0x02 /* repeat to find outer pair */
9820#define SP_RETCOUNT 0x04 /* return matchcount */
9821#define SP_SETPCMARK 0x08 /* set previous context mark */
9822#define SP_START 0x10 /* accept match at start position */
9823#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
9824#define SP_END 0x40 /* leave cursor at end of match */
9825#define SP_COLUMN 0x80 /* start at cursor column */
9826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009827/*
9828 * Get flags for a search function.
9829 * Possibly sets "p_ws".
9830 * Returns BACKWARD, FORWARD or zero (for an error).
9831 */
9832 static int
9833get_search_arg(typval_T *varp, int *flagsp)
9834{
9835 int dir = FORWARD;
9836 char_u *flags;
9837 char_u nbuf[NUMBUFLEN];
9838 int mask;
9839
9840 if (varp->v_type != VAR_UNKNOWN)
9841 {
9842 flags = get_tv_string_buf_chk(varp, nbuf);
9843 if (flags == NULL)
9844 return 0; /* type error; errmsg already given */
9845 while (*flags != NUL)
9846 {
9847 switch (*flags)
9848 {
9849 case 'b': dir = BACKWARD; break;
9850 case 'w': p_ws = TRUE; break;
9851 case 'W': p_ws = FALSE; break;
9852 default: mask = 0;
9853 if (flagsp != NULL)
9854 switch (*flags)
9855 {
9856 case 'c': mask = SP_START; break;
9857 case 'e': mask = SP_END; break;
9858 case 'm': mask = SP_RETCOUNT; break;
9859 case 'n': mask = SP_NOMOVE; break;
9860 case 'p': mask = SP_SUBPAT; break;
9861 case 'r': mask = SP_REPEAT; break;
9862 case 's': mask = SP_SETPCMARK; break;
9863 case 'z': mask = SP_COLUMN; break;
9864 }
9865 if (mask == 0)
9866 {
9867 EMSG2(_(e_invarg2), flags);
9868 dir = 0;
9869 }
9870 else
9871 *flagsp |= mask;
9872 }
9873 if (dir == 0)
9874 break;
9875 ++flags;
9876 }
9877 }
9878 return dir;
9879}
9880
9881/*
9882 * Shared by search() and searchpos() functions.
9883 */
9884 static int
9885search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
9886{
9887 int flags;
9888 char_u *pat;
9889 pos_T pos;
9890 pos_T save_cursor;
9891 int save_p_ws = p_ws;
9892 int dir;
9893 int retval = 0; /* default: FAIL */
9894 long lnum_stop = 0;
9895 proftime_T tm;
9896#ifdef FEAT_RELTIME
9897 long time_limit = 0;
9898#endif
9899 int options = SEARCH_KEEP;
9900 int subpatnum;
9901
9902 pat = get_tv_string(&argvars[0]);
9903 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
9904 if (dir == 0)
9905 goto theend;
9906 flags = *flagsp;
9907 if (flags & SP_START)
9908 options |= SEARCH_START;
9909 if (flags & SP_END)
9910 options |= SEARCH_END;
9911 if (flags & SP_COLUMN)
9912 options |= SEARCH_COL;
9913
9914 /* Optional arguments: line number to stop searching and timeout. */
9915 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
9916 {
9917 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
9918 if (lnum_stop < 0)
9919 goto theend;
9920#ifdef FEAT_RELTIME
9921 if (argvars[3].v_type != VAR_UNKNOWN)
9922 {
9923 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
9924 if (time_limit < 0)
9925 goto theend;
9926 }
9927#endif
9928 }
9929
9930#ifdef FEAT_RELTIME
9931 /* Set the time limit, if there is one. */
9932 profile_setlimit(time_limit, &tm);
9933#endif
9934
9935 /*
9936 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
9937 * Check to make sure only those flags are set.
9938 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
9939 * flags cannot be set. Check for that condition also.
9940 */
9941 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
9942 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
9943 {
9944 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
9945 goto theend;
9946 }
9947
9948 pos = save_cursor = curwin->w_cursor;
9949 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02009950 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 if (subpatnum != FAIL)
9952 {
9953 if (flags & SP_SUBPAT)
9954 retval = subpatnum;
9955 else
9956 retval = pos.lnum;
9957 if (flags & SP_SETPCMARK)
9958 setpcmark();
9959 curwin->w_cursor = pos;
9960 if (match_pos != NULL)
9961 {
9962 /* Store the match cursor position */
9963 match_pos->lnum = pos.lnum;
9964 match_pos->col = pos.col + 1;
9965 }
9966 /* "/$" will put the cursor after the end of the line, may need to
9967 * correct that here */
9968 check_cursor();
9969 }
9970
9971 /* If 'n' flag is used: restore cursor position. */
9972 if (flags & SP_NOMOVE)
9973 curwin->w_cursor = save_cursor;
9974 else
9975 curwin->w_set_curswant = TRUE;
9976theend:
9977 p_ws = save_p_ws;
9978
9979 return retval;
9980}
9981
9982#ifdef FEAT_FLOAT
9983
9984/*
9985 * round() is not in C90, use ceil() or floor() instead.
9986 */
9987 float_T
9988vim_round(float_T f)
9989{
9990 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
9991}
9992
9993/*
9994 * "round({float})" function
9995 */
9996 static void
9997f_round(typval_T *argvars, typval_T *rettv)
9998{
9999 float_T f = 0.0;
10000
10001 rettv->v_type = VAR_FLOAT;
10002 if (get_float_arg(argvars, &f) == OK)
10003 rettv->vval.v_float = vim_round(f);
10004 else
10005 rettv->vval.v_float = 0.0;
10006}
10007#endif
10008
10009/*
10010 * "screenattr()" function
10011 */
10012 static void
10013f_screenattr(typval_T *argvars, typval_T *rettv)
10014{
10015 int row;
10016 int col;
10017 int c;
10018
10019 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
10020 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
10021 if (row < 0 || row >= screen_Rows
10022 || col < 0 || col >= screen_Columns)
10023 c = -1;
10024 else
10025 c = ScreenAttrs[LineOffset[row] + col];
10026 rettv->vval.v_number = c;
10027}
10028
10029/*
10030 * "screenchar()" function
10031 */
10032 static void
10033f_screenchar(typval_T *argvars, typval_T *rettv)
10034{
10035 int row;
10036 int col;
10037 int off;
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 {
10047 off = LineOffset[row] + col;
10048#ifdef FEAT_MBYTE
10049 if (enc_utf8 && ScreenLinesUC[off] != 0)
10050 c = ScreenLinesUC[off];
10051 else
10052#endif
10053 c = ScreenLines[off];
10054 }
10055 rettv->vval.v_number = c;
10056}
10057
10058/*
10059 * "screencol()" function
10060 *
10061 * First column is 1 to be consistent with virtcol().
10062 */
10063 static void
10064f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10065{
10066 rettv->vval.v_number = screen_screencol() + 1;
10067}
10068
10069/*
10070 * "screenrow()" function
10071 */
10072 static void
10073f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10074{
10075 rettv->vval.v_number = screen_screenrow() + 1;
10076}
10077
10078/*
10079 * "search()" function
10080 */
10081 static void
10082f_search(typval_T *argvars, typval_T *rettv)
10083{
10084 int flags = 0;
10085
10086 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10087}
10088
10089/*
10090 * "searchdecl()" function
10091 */
10092 static void
10093f_searchdecl(typval_T *argvars, typval_T *rettv)
10094{
10095 int locally = 1;
10096 int thisblock = 0;
10097 int error = FALSE;
10098 char_u *name;
10099
10100 rettv->vval.v_number = 1; /* default: FAIL */
10101
10102 name = get_tv_string_chk(&argvars[0]);
10103 if (argvars[1].v_type != VAR_UNKNOWN)
10104 {
10105 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
10106 if (!error && argvars[2].v_type != VAR_UNKNOWN)
10107 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
10108 }
10109 if (!error && name != NULL)
10110 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10111 locally, thisblock, SEARCH_KEEP) == FAIL;
10112}
10113
10114/*
10115 * Used by searchpair() and searchpairpos()
10116 */
10117 static int
10118searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10119{
10120 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010121 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010122 int save_p_ws = p_ws;
10123 int dir;
10124 int flags = 0;
10125 char_u nbuf1[NUMBUFLEN];
10126 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010127 int retval = 0; /* default: FAIL */
10128 long lnum_stop = 0;
10129 long time_limit = 0;
10130
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010131 /* Get the three pattern arguments: start, middle, end. Will result in an
10132 * error if not a valid argument. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010133 spat = get_tv_string_chk(&argvars[0]);
10134 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
10135 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
10136 if (spat == NULL || mpat == NULL || epat == NULL)
10137 goto theend; /* type error */
10138
10139 /* Handle the optional fourth argument: flags */
10140 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10141 if (dir == 0)
10142 goto theend;
10143
10144 /* Don't accept SP_END or SP_SUBPAT.
10145 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10146 */
10147 if ((flags & (SP_END | SP_SUBPAT)) != 0
10148 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10149 {
10150 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
10151 goto theend;
10152 }
10153
10154 /* Using 'r' implies 'W', otherwise it doesn't work. */
10155 if (flags & SP_REPEAT)
10156 p_ws = FALSE;
10157
10158 /* Optional fifth argument: skip expression */
10159 if (argvars[3].v_type == VAR_UNKNOWN
10160 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010161 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 else
10163 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010164 skip = &argvars[4];
10165 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10166 && skip->v_type != VAR_STRING)
10167 {
10168 /* Type error */
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010169 EMSG2(_(e_invarg2), get_tv_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010170 goto theend;
10171 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010172 if (argvars[5].v_type != VAR_UNKNOWN)
10173 {
10174 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
10175 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010176 {
10177 EMSG2(_(e_invarg2), get_tv_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010178 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010179 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010180#ifdef FEAT_RELTIME
10181 if (argvars[6].v_type != VAR_UNKNOWN)
10182 {
10183 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
10184 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010185 {
10186 EMSG2(_(e_invarg2), get_tv_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010188 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 }
10190#endif
10191 }
10192 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193
10194 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10195 match_pos, lnum_stop, time_limit);
10196
10197theend:
10198 p_ws = save_p_ws;
10199
10200 return retval;
10201}
10202
10203/*
10204 * "searchpair()" function
10205 */
10206 static void
10207f_searchpair(typval_T *argvars, typval_T *rettv)
10208{
10209 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10210}
10211
10212/*
10213 * "searchpairpos()" function
10214 */
10215 static void
10216f_searchpairpos(typval_T *argvars, typval_T *rettv)
10217{
10218 pos_T match_pos;
10219 int lnum = 0;
10220 int col = 0;
10221
10222 if (rettv_list_alloc(rettv) == FAIL)
10223 return;
10224
10225 if (searchpair_cmn(argvars, &match_pos) > 0)
10226 {
10227 lnum = match_pos.lnum;
10228 col = match_pos.col;
10229 }
10230
10231 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10232 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10233}
10234
10235/*
10236 * Search for a start/middle/end thing.
10237 * Used by searchpair(), see its documentation for the details.
10238 * Returns 0 or -1 for no match,
10239 */
10240 long
10241do_searchpair(
10242 char_u *spat, /* start pattern */
10243 char_u *mpat, /* middle pattern */
10244 char_u *epat, /* end pattern */
10245 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010246 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 int flags, /* SP_SETPCMARK and other SP_ values */
10248 pos_T *match_pos,
10249 linenr_T lnum_stop, /* stop at this line if not zero */
10250 long time_limit UNUSED) /* stop after this many msec */
10251{
10252 char_u *save_cpo;
10253 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10254 long retval = 0;
10255 pos_T pos;
10256 pos_T firstpos;
10257 pos_T foundpos;
10258 pos_T save_cursor;
10259 pos_T save_pos;
10260 int n;
10261 int r;
10262 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010263 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 int err;
10265 int options = SEARCH_KEEP;
10266 proftime_T tm;
10267
10268 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10269 save_cpo = p_cpo;
10270 p_cpo = empty_option;
10271
10272#ifdef FEAT_RELTIME
10273 /* Set the time limit, if there is one. */
10274 profile_setlimit(time_limit, &tm);
10275#endif
10276
10277 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10278 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010279 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10280 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010281 if (pat2 == NULL || pat3 == NULL)
10282 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010283 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010284 if (*mpat == NUL)
10285 STRCPY(pat3, pat2);
10286 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010287 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 spat, epat, mpat);
10289 if (flags & SP_START)
10290 options |= SEARCH_START;
10291
Bram Moolenaar48570482017-10-30 21:48:41 +010010292 if (skip != NULL)
10293 {
10294 /* Empty string means to not use the skip expression. */
10295 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10296 use_skip = skip->vval.v_string != NULL
10297 && *skip->vval.v_string != NUL;
10298 }
10299
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 save_cursor = curwin->w_cursor;
10301 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010302 CLEAR_POS(&firstpos);
10303 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010304 pat = pat3;
10305 for (;;)
10306 {
10307 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010308 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010309 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010310 /* didn't find it or found the first match again: FAIL */
10311 break;
10312
10313 if (firstpos.lnum == 0)
10314 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010315 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010316 {
10317 /* Found the same position again. Can happen with a pattern that
10318 * has "\zs" at the end and searching backwards. Advance one
10319 * character and try again. */
10320 if (dir == BACKWARD)
10321 decl(&pos);
10322 else
10323 incl(&pos);
10324 }
10325 foundpos = pos;
10326
10327 /* clear the start flag to avoid getting stuck here */
10328 options &= ~SEARCH_START;
10329
10330 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010331 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010332 {
10333 save_pos = curwin->w_cursor;
10334 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010335 err = FALSE;
10336 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010337 curwin->w_cursor = save_pos;
10338 if (err)
10339 {
10340 /* Evaluating {skip} caused an error, break here. */
10341 curwin->w_cursor = save_cursor;
10342 retval = -1;
10343 break;
10344 }
10345 if (r)
10346 continue;
10347 }
10348
10349 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10350 {
10351 /* Found end when searching backwards or start when searching
10352 * forward: nested pair. */
10353 ++nest;
10354 pat = pat2; /* nested, don't search for middle */
10355 }
10356 else
10357 {
10358 /* Found end when searching forward or start when searching
10359 * backward: end of (nested) pair; or found middle in outer pair. */
10360 if (--nest == 1)
10361 pat = pat3; /* outer level, search for middle */
10362 }
10363
10364 if (nest == 0)
10365 {
10366 /* Found the match: return matchcount or line number. */
10367 if (flags & SP_RETCOUNT)
10368 ++retval;
10369 else
10370 retval = pos.lnum;
10371 if (flags & SP_SETPCMARK)
10372 setpcmark();
10373 curwin->w_cursor = pos;
10374 if (!(flags & SP_REPEAT))
10375 break;
10376 nest = 1; /* search for next unmatched */
10377 }
10378 }
10379
10380 if (match_pos != NULL)
10381 {
10382 /* Store the match cursor position */
10383 match_pos->lnum = curwin->w_cursor.lnum;
10384 match_pos->col = curwin->w_cursor.col + 1;
10385 }
10386
10387 /* If 'n' flag is used or search failed: restore cursor position. */
10388 if ((flags & SP_NOMOVE) || retval == 0)
10389 curwin->w_cursor = save_cursor;
10390
10391theend:
10392 vim_free(pat2);
10393 vim_free(pat3);
10394 if (p_cpo == empty_option)
10395 p_cpo = save_cpo;
10396 else
10397 /* Darn, evaluating the {skip} expression changed the value. */
10398 free_string_option(save_cpo);
10399
10400 return retval;
10401}
10402
10403/*
10404 * "searchpos()" function
10405 */
10406 static void
10407f_searchpos(typval_T *argvars, typval_T *rettv)
10408{
10409 pos_T match_pos;
10410 int lnum = 0;
10411 int col = 0;
10412 int n;
10413 int flags = 0;
10414
10415 if (rettv_list_alloc(rettv) == FAIL)
10416 return;
10417
10418 n = search_cmn(argvars, &match_pos, &flags);
10419 if (n > 0)
10420 {
10421 lnum = match_pos.lnum;
10422 col = match_pos.col;
10423 }
10424
10425 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10426 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10427 if (flags & SP_SUBPAT)
10428 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10429}
10430
10431 static void
10432f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10433{
10434#ifdef FEAT_CLIENTSERVER
10435 char_u buf[NUMBUFLEN];
10436 char_u *server = get_tv_string_chk(&argvars[0]);
10437 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
10438
10439 rettv->vval.v_number = -1;
10440 if (server == NULL || reply == NULL)
10441 return;
10442 if (check_restricted() || check_secure())
10443 return;
10444# ifdef FEAT_X11
10445 if (check_connection() == FAIL)
10446 return;
10447# endif
10448
10449 if (serverSendReply(server, reply) < 0)
10450 {
10451 EMSG(_("E258: Unable to send to client"));
10452 return;
10453 }
10454 rettv->vval.v_number = 0;
10455#else
10456 rettv->vval.v_number = -1;
10457#endif
10458}
10459
10460 static void
10461f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10462{
10463 char_u *r = NULL;
10464
10465#ifdef FEAT_CLIENTSERVER
10466# ifdef WIN32
10467 r = serverGetVimNames();
10468# else
10469 make_connection();
10470 if (X_DISPLAY != NULL)
10471 r = serverGetVimNames(X_DISPLAY);
10472# endif
10473#endif
10474 rettv->v_type = VAR_STRING;
10475 rettv->vval.v_string = r;
10476}
10477
10478/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010479 * "setbufline()" function
10480 */
10481 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010482f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010483{
10484 linenr_T lnum;
10485 buf_T *buf;
10486
10487 buf = get_buf_tv(&argvars[0], FALSE);
10488 if (buf == NULL)
10489 rettv->vval.v_number = 1; /* FAIL */
10490 else
10491 {
10492 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010493 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010494 }
10495}
10496
10497/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010498 * "setbufvar()" function
10499 */
10500 static void
10501f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10502{
10503 buf_T *buf;
10504 char_u *varname, *bufvarname;
10505 typval_T *varp;
10506 char_u nbuf[NUMBUFLEN];
10507
10508 if (check_restricted() || check_secure())
10509 return;
10510 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10511 varname = get_tv_string_chk(&argvars[1]);
10512 buf = get_buf_tv(&argvars[0], FALSE);
10513 varp = &argvars[2];
10514
10515 if (buf != NULL && varname != NULL && varp != NULL)
10516 {
10517 if (*varname == '&')
10518 {
10519 long numval;
10520 char_u *strval;
10521 int error = FALSE;
10522 aco_save_T aco;
10523
10524 /* set curbuf to be our buf, temporarily */
10525 aucmd_prepbuf(&aco, buf);
10526
10527 ++varname;
10528 numval = (long)get_tv_number_chk(varp, &error);
10529 strval = get_tv_string_buf_chk(varp, nbuf);
10530 if (!error && strval != NULL)
10531 set_option_value(varname, numval, strval, OPT_LOCAL);
10532
10533 /* reset notion of buffer */
10534 aucmd_restbuf(&aco);
10535 }
10536 else
10537 {
10538 buf_T *save_curbuf = curbuf;
10539
10540 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10541 if (bufvarname != NULL)
10542 {
10543 curbuf = buf;
10544 STRCPY(bufvarname, "b:");
10545 STRCPY(bufvarname + 2, varname);
10546 set_var(bufvarname, varp, TRUE);
10547 vim_free(bufvarname);
10548 curbuf = save_curbuf;
10549 }
10550 }
10551 }
10552}
10553
10554 static void
10555f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10556{
10557 dict_T *d;
10558 dictitem_T *di;
10559 char_u *csearch;
10560
10561 if (argvars[0].v_type != VAR_DICT)
10562 {
10563 EMSG(_(e_dictreq));
10564 return;
10565 }
10566
10567 if ((d = argvars[0].vval.v_dict) != NULL)
10568 {
10569 csearch = get_dict_string(d, (char_u *)"char", FALSE);
10570 if (csearch != NULL)
10571 {
10572#ifdef FEAT_MBYTE
10573 if (enc_utf8)
10574 {
10575 int pcc[MAX_MCO];
10576 int c = utfc_ptr2char(csearch, pcc);
10577
10578 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10579 }
10580 else
10581#endif
10582 set_last_csearch(PTR2CHAR(csearch),
10583 csearch, MB_PTR2LEN(csearch));
10584 }
10585
10586 di = dict_find(d, (char_u *)"forward", -1);
10587 if (di != NULL)
10588 set_csearch_direction((int)get_tv_number(&di->di_tv)
10589 ? FORWARD : BACKWARD);
10590
10591 di = dict_find(d, (char_u *)"until", -1);
10592 if (di != NULL)
10593 set_csearch_until(!!get_tv_number(&di->di_tv));
10594 }
10595}
10596
10597/*
10598 * "setcmdpos()" function
10599 */
10600 static void
10601f_setcmdpos(typval_T *argvars, typval_T *rettv)
10602{
10603 int pos = (int)get_tv_number(&argvars[0]) - 1;
10604
10605 if (pos >= 0)
10606 rettv->vval.v_number = set_cmdline_pos(pos);
10607}
10608
10609/*
10610 * "setfperm({fname}, {mode})" function
10611 */
10612 static void
10613f_setfperm(typval_T *argvars, typval_T *rettv)
10614{
10615 char_u *fname;
10616 char_u modebuf[NUMBUFLEN];
10617 char_u *mode_str;
10618 int i;
10619 int mask;
10620 int mode = 0;
10621
10622 rettv->vval.v_number = 0;
10623 fname = get_tv_string_chk(&argvars[0]);
10624 if (fname == NULL)
10625 return;
10626 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
10627 if (mode_str == NULL)
10628 return;
10629 if (STRLEN(mode_str) != 9)
10630 {
10631 EMSG2(_(e_invarg2), mode_str);
10632 return;
10633 }
10634
10635 mask = 1;
10636 for (i = 8; i >= 0; --i)
10637 {
10638 if (mode_str[i] != '-')
10639 mode |= mask;
10640 mask = mask << 1;
10641 }
10642 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10643}
10644
10645/*
10646 * "setline()" function
10647 */
10648 static void
10649f_setline(typval_T *argvars, typval_T *rettv)
10650{
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010651 linenr_T lnum = get_tv_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010652
Bram Moolenaarca851592018-06-06 21:04:07 +020010653 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654}
10655
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010656/*
10657 * Used by "setqflist()" and "setloclist()" functions
10658 */
10659 static void
10660set_qf_ll_list(
10661 win_T *wp UNUSED,
10662 typval_T *list_arg UNUSED,
10663 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010664 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010665 typval_T *rettv)
10666{
10667#ifdef FEAT_QUICKFIX
10668 static char *e_invact = N_("E927: Invalid action: '%s'");
10669 char_u *act;
10670 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010671 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010672#endif
10673
10674 rettv->vval.v_number = -1;
10675
10676#ifdef FEAT_QUICKFIX
10677 if (list_arg->v_type != VAR_LIST)
10678 EMSG(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010679 else if (recursive != 0)
10680 EMSG(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010681 else
10682 {
10683 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010684 dict_T *d = NULL;
10685 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010686
10687 if (action_arg->v_type == VAR_STRING)
10688 {
10689 act = get_tv_string_chk(action_arg);
10690 if (act == NULL)
10691 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010692 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10693 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010694 action = *act;
10695 else
10696 EMSG2(_(e_invact), act);
10697 }
10698 else if (action_arg->v_type == VAR_UNKNOWN)
10699 action = ' ';
10700 else
10701 EMSG(_(e_stringreq));
10702
Bram Moolenaard823fa92016-08-12 16:29:27 +020010703 if (action_arg->v_type != VAR_UNKNOWN
10704 && what_arg->v_type != VAR_UNKNOWN)
10705 {
10706 if (what_arg->v_type == VAR_DICT)
10707 d = what_arg->vval.v_dict;
10708 else
10709 {
10710 EMSG(_(e_dictreq));
10711 valid_dict = FALSE;
10712 }
10713 }
10714
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010715 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010716 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010717 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
10718 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010719 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010720 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010721 }
10722#endif
10723}
10724
10725/*
10726 * "setloclist()" function
10727 */
10728 static void
10729f_setloclist(typval_T *argvars, typval_T *rettv)
10730{
10731 win_T *win;
10732
10733 rettv->vval.v_number = -1;
10734
10735 win = find_win_by_nr(&argvars[0], NULL);
10736 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020010737 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738}
10739
10740/*
10741 * "setmatches()" function
10742 */
10743 static void
10744f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10745{
10746#ifdef FEAT_SEARCH_EXTRA
10747 list_T *l;
10748 listitem_T *li;
10749 dict_T *d;
10750 list_T *s = NULL;
10751
10752 rettv->vval.v_number = -1;
10753 if (argvars[0].v_type != VAR_LIST)
10754 {
10755 EMSG(_(e_listreq));
10756 return;
10757 }
10758 if ((l = argvars[0].vval.v_list) != NULL)
10759 {
10760
10761 /* To some extent make sure that we are dealing with a list from
10762 * "getmatches()". */
10763 li = l->lv_first;
10764 while (li != NULL)
10765 {
10766 if (li->li_tv.v_type != VAR_DICT
10767 || (d = li->li_tv.vval.v_dict) == NULL)
10768 {
10769 EMSG(_(e_invarg));
10770 return;
10771 }
10772 if (!(dict_find(d, (char_u *)"group", -1) != NULL
10773 && (dict_find(d, (char_u *)"pattern", -1) != NULL
10774 || dict_find(d, (char_u *)"pos1", -1) != NULL)
10775 && dict_find(d, (char_u *)"priority", -1) != NULL
10776 && dict_find(d, (char_u *)"id", -1) != NULL))
10777 {
10778 EMSG(_(e_invarg));
10779 return;
10780 }
10781 li = li->li_next;
10782 }
10783
10784 clear_matches(curwin);
10785 li = l->lv_first;
10786 while (li != NULL)
10787 {
10788 int i = 0;
10789 char_u buf[5];
10790 dictitem_T *di;
10791 char_u *group;
10792 int priority;
10793 int id;
10794 char_u *conceal;
10795
10796 d = li->li_tv.vval.v_dict;
10797 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
10798 {
10799 if (s == NULL)
10800 {
10801 s = list_alloc();
10802 if (s == NULL)
10803 return;
10804 }
10805
10806 /* match from matchaddpos() */
10807 for (i = 1; i < 9; i++)
10808 {
10809 sprintf((char *)buf, (char *)"pos%d", i);
10810 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
10811 {
10812 if (di->di_tv.v_type != VAR_LIST)
10813 return;
10814
10815 list_append_tv(s, &di->di_tv);
10816 s->lv_refcount++;
10817 }
10818 else
10819 break;
10820 }
10821 }
10822
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010823 group = get_dict_string(d, (char_u *)"group", TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010824 priority = (int)get_dict_number(d, (char_u *)"priority");
10825 id = (int)get_dict_number(d, (char_u *)"id");
10826 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010827 ? get_dict_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010828 : NULL;
10829 if (i == 0)
10830 {
10831 match_add(curwin, group,
10832 get_dict_string(d, (char_u *)"pattern", FALSE),
10833 priority, id, NULL, conceal);
10834 }
10835 else
10836 {
10837 match_add(curwin, group, NULL, priority, id, s, conceal);
10838 list_unref(s);
10839 s = NULL;
10840 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020010841 vim_free(group);
10842 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010843
10844 li = li->li_next;
10845 }
10846 rettv->vval.v_number = 0;
10847 }
10848#endif
10849}
10850
10851/*
10852 * "setpos()" function
10853 */
10854 static void
10855f_setpos(typval_T *argvars, typval_T *rettv)
10856{
10857 pos_T pos;
10858 int fnum;
10859 char_u *name;
10860 colnr_T curswant = -1;
10861
10862 rettv->vval.v_number = -1;
10863 name = get_tv_string_chk(argvars);
10864 if (name != NULL)
10865 {
10866 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
10867 {
10868 if (--pos.col < 0)
10869 pos.col = 0;
10870 if (name[0] == '.' && name[1] == NUL)
10871 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010872 /* set cursor; "fnum" is ignored */
10873 curwin->w_cursor = pos;
10874 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010876 curwin->w_curswant = curswant - 1;
10877 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010878 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010010879 check_cursor();
10880 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010881 }
10882 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
10883 {
10884 /* set mark */
10885 if (setmark_pos(name[1], &pos, fnum) == OK)
10886 rettv->vval.v_number = 0;
10887 }
10888 else
10889 EMSG(_(e_invarg));
10890 }
10891 }
10892}
10893
10894/*
10895 * "setqflist()" function
10896 */
10897 static void
10898f_setqflist(typval_T *argvars, typval_T *rettv)
10899{
Bram Moolenaard823fa92016-08-12 16:29:27 +020010900 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010901}
10902
10903/*
10904 * "setreg()" function
10905 */
10906 static void
10907f_setreg(typval_T *argvars, typval_T *rettv)
10908{
10909 int regname;
10910 char_u *strregname;
10911 char_u *stropt;
10912 char_u *strval;
10913 int append;
10914 char_u yank_type;
10915 long block_len;
10916
10917 block_len = -1;
10918 yank_type = MAUTO;
10919 append = FALSE;
10920
10921 strregname = get_tv_string_chk(argvars);
10922 rettv->vval.v_number = 1; /* FAIL is default */
10923
10924 if (strregname == NULL)
10925 return; /* type error; errmsg already given */
10926 regname = *strregname;
10927 if (regname == 0 || regname == '@')
10928 regname = '"';
10929
10930 if (argvars[2].v_type != VAR_UNKNOWN)
10931 {
10932 stropt = get_tv_string_chk(&argvars[2]);
10933 if (stropt == NULL)
10934 return; /* type error */
10935 for (; *stropt != NUL; ++stropt)
10936 switch (*stropt)
10937 {
10938 case 'a': case 'A': /* append */
10939 append = TRUE;
10940 break;
10941 case 'v': case 'c': /* character-wise selection */
10942 yank_type = MCHAR;
10943 break;
10944 case 'V': case 'l': /* line-wise selection */
10945 yank_type = MLINE;
10946 break;
10947 case 'b': case Ctrl_V: /* block-wise selection */
10948 yank_type = MBLOCK;
10949 if (VIM_ISDIGIT(stropt[1]))
10950 {
10951 ++stropt;
10952 block_len = getdigits(&stropt) - 1;
10953 --stropt;
10954 }
10955 break;
10956 }
10957 }
10958
10959 if (argvars[1].v_type == VAR_LIST)
10960 {
10961 char_u **lstval;
10962 char_u **allocval;
10963 char_u buf[NUMBUFLEN];
10964 char_u **curval;
10965 char_u **curallocval;
10966 list_T *ll = argvars[1].vval.v_list;
10967 listitem_T *li;
10968 int len;
10969
10970 /* If the list is NULL handle like an empty list. */
10971 len = ll == NULL ? 0 : ll->lv_len;
10972
10973 /* First half: use for pointers to result lines; second half: use for
10974 * pointers to allocated copies. */
10975 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
10976 if (lstval == NULL)
10977 return;
10978 curval = lstval;
10979 allocval = lstval + len + 2;
10980 curallocval = allocval;
10981
10982 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
10983 li = li->li_next)
10984 {
10985 strval = get_tv_string_buf_chk(&li->li_tv, buf);
10986 if (strval == NULL)
10987 goto free_lstval;
10988 if (strval == buf)
10989 {
10990 /* Need to make a copy, next get_tv_string_buf_chk() will
10991 * overwrite the string. */
10992 strval = vim_strsave(buf);
10993 if (strval == NULL)
10994 goto free_lstval;
10995 *curallocval++ = strval;
10996 }
10997 *curval++ = strval;
10998 }
10999 *curval++ = NULL;
11000
11001 write_reg_contents_lst(regname, lstval, -1,
11002 append, yank_type, block_len);
11003free_lstval:
11004 while (curallocval > allocval)
11005 vim_free(*--curallocval);
11006 vim_free(lstval);
11007 }
11008 else
11009 {
11010 strval = get_tv_string_chk(&argvars[1]);
11011 if (strval == NULL)
11012 return;
11013 write_reg_contents_ex(regname, strval, -1,
11014 append, yank_type, block_len);
11015 }
11016 rettv->vval.v_number = 0;
11017}
11018
11019/*
11020 * "settabvar()" function
11021 */
11022 static void
11023f_settabvar(typval_T *argvars, typval_T *rettv)
11024{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 tabpage_T *save_curtab;
11026 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027 char_u *varname, *tabvarname;
11028 typval_T *varp;
11029
11030 rettv->vval.v_number = 0;
11031
11032 if (check_restricted() || check_secure())
11033 return;
11034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 varname = get_tv_string_chk(&argvars[1]);
11037 varp = &argvars[2];
11038
Bram Moolenaar4033c552017-09-16 20:54:51 +020011039 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041 save_curtab = curtab;
11042 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043
11044 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11045 if (tabvarname != NULL)
11046 {
11047 STRCPY(tabvarname, "t:");
11048 STRCPY(tabvarname + 2, varname);
11049 set_var(tabvarname, varp, TRUE);
11050 vim_free(tabvarname);
11051 }
11052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011053 /* Restore current tabpage */
11054 if (valid_tabpage(save_curtab))
11055 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 }
11057}
11058
11059/*
11060 * "settabwinvar()" function
11061 */
11062 static void
11063f_settabwinvar(typval_T *argvars, typval_T *rettv)
11064{
11065 setwinvar(argvars, rettv, 1);
11066}
11067
11068/*
11069 * "setwinvar()" function
11070 */
11071 static void
11072f_setwinvar(typval_T *argvars, typval_T *rettv)
11073{
11074 setwinvar(argvars, rettv, 0);
11075}
11076
11077#ifdef FEAT_CRYPT
11078/*
11079 * "sha256({string})" function
11080 */
11081 static void
11082f_sha256(typval_T *argvars, typval_T *rettv)
11083{
11084 char_u *p;
11085
11086 p = get_tv_string(&argvars[0]);
11087 rettv->vval.v_string = vim_strsave(
11088 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11089 rettv->v_type = VAR_STRING;
11090}
11091#endif /* FEAT_CRYPT */
11092
11093/*
11094 * "shellescape({string})" function
11095 */
11096 static void
11097f_shellescape(typval_T *argvars, typval_T *rettv)
11098{
Bram Moolenaar20615522017-06-05 18:46:26 +020011099 int do_special = non_zero_arg(&argvars[1]);
11100
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar20615522017-06-05 18:46:26 +020011102 get_tv_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011103 rettv->v_type = VAR_STRING;
11104}
11105
11106/*
11107 * shiftwidth() function
11108 */
11109 static void
11110f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11111{
11112 rettv->vval.v_number = get_sw_value(curbuf);
11113}
11114
11115/*
11116 * "simplify()" function
11117 */
11118 static void
11119f_simplify(typval_T *argvars, typval_T *rettv)
11120{
11121 char_u *p;
11122
11123 p = get_tv_string(&argvars[0]);
11124 rettv->vval.v_string = vim_strsave(p);
11125 simplify_filename(rettv->vval.v_string); /* simplify in place */
11126 rettv->v_type = VAR_STRING;
11127}
11128
11129#ifdef FEAT_FLOAT
11130/*
11131 * "sin()" function
11132 */
11133 static void
11134f_sin(typval_T *argvars, typval_T *rettv)
11135{
11136 float_T f = 0.0;
11137
11138 rettv->v_type = VAR_FLOAT;
11139 if (get_float_arg(argvars, &f) == OK)
11140 rettv->vval.v_float = sin(f);
11141 else
11142 rettv->vval.v_float = 0.0;
11143}
11144
11145/*
11146 * "sinh()" function
11147 */
11148 static void
11149f_sinh(typval_T *argvars, typval_T *rettv)
11150{
11151 float_T f = 0.0;
11152
11153 rettv->v_type = VAR_FLOAT;
11154 if (get_float_arg(argvars, &f) == OK)
11155 rettv->vval.v_float = sinh(f);
11156 else
11157 rettv->vval.v_float = 0.0;
11158}
11159#endif
11160
11161static int
11162#ifdef __BORLANDC__
11163 _RTLENTRYF
11164#endif
11165 item_compare(const void *s1, const void *s2);
11166static int
11167#ifdef __BORLANDC__
11168 _RTLENTRYF
11169#endif
11170 item_compare2(const void *s1, const void *s2);
11171
11172/* struct used in the array that's given to qsort() */
11173typedef struct
11174{
11175 listitem_T *item;
11176 int idx;
11177} sortItem_T;
11178
11179/* struct storing information about current sort */
11180typedef struct
11181{
11182 int item_compare_ic;
11183 int item_compare_numeric;
11184 int item_compare_numbers;
11185#ifdef FEAT_FLOAT
11186 int item_compare_float;
11187#endif
11188 char_u *item_compare_func;
11189 partial_T *item_compare_partial;
11190 dict_T *item_compare_selfdict;
11191 int item_compare_func_err;
11192 int item_compare_keep_zero;
11193} sortinfo_T;
11194static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195#define ITEM_COMPARE_FAIL 999
11196
11197/*
11198 * Compare functions for f_sort() and f_uniq() below.
11199 */
11200 static int
11201#ifdef __BORLANDC__
11202_RTLENTRYF
11203#endif
11204item_compare(const void *s1, const void *s2)
11205{
11206 sortItem_T *si1, *si2;
11207 typval_T *tv1, *tv2;
11208 char_u *p1, *p2;
11209 char_u *tofree1 = NULL, *tofree2 = NULL;
11210 int res;
11211 char_u numbuf1[NUMBUFLEN];
11212 char_u numbuf2[NUMBUFLEN];
11213
11214 si1 = (sortItem_T *)s1;
11215 si2 = (sortItem_T *)s2;
11216 tv1 = &si1->item->li_tv;
11217 tv2 = &si2->item->li_tv;
11218
11219 if (sortinfo->item_compare_numbers)
11220 {
11221 varnumber_T v1 = get_tv_number(tv1);
11222 varnumber_T v2 = get_tv_number(tv2);
11223
11224 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11225 }
11226
11227#ifdef FEAT_FLOAT
11228 if (sortinfo->item_compare_float)
11229 {
11230 float_T v1 = get_tv_float(tv1);
11231 float_T v2 = get_tv_float(tv2);
11232
11233 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11234 }
11235#endif
11236
11237 /* tv2string() puts quotes around a string and allocates memory. Don't do
11238 * that for string variables. Use a single quote when comparing with a
11239 * non-string to do what the docs promise. */
11240 if (tv1->v_type == VAR_STRING)
11241 {
11242 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11243 p1 = (char_u *)"'";
11244 else
11245 p1 = tv1->vval.v_string;
11246 }
11247 else
11248 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11249 if (tv2->v_type == VAR_STRING)
11250 {
11251 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11252 p2 = (char_u *)"'";
11253 else
11254 p2 = tv2->vval.v_string;
11255 }
11256 else
11257 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11258 if (p1 == NULL)
11259 p1 = (char_u *)"";
11260 if (p2 == NULL)
11261 p2 = (char_u *)"";
11262 if (!sortinfo->item_compare_numeric)
11263 {
11264 if (sortinfo->item_compare_ic)
11265 res = STRICMP(p1, p2);
11266 else
11267 res = STRCMP(p1, p2);
11268 }
11269 else
11270 {
11271 double n1, n2;
11272 n1 = strtod((char *)p1, (char **)&p1);
11273 n2 = strtod((char *)p2, (char **)&p2);
11274 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
11275 }
11276
11277 /* When the result would be zero, compare the item indexes. Makes the
11278 * sort stable. */
11279 if (res == 0 && !sortinfo->item_compare_keep_zero)
11280 res = si1->idx > si2->idx ? 1 : -1;
11281
11282 vim_free(tofree1);
11283 vim_free(tofree2);
11284 return res;
11285}
11286
11287 static int
11288#ifdef __BORLANDC__
11289_RTLENTRYF
11290#endif
11291item_compare2(const void *s1, const void *s2)
11292{
11293 sortItem_T *si1, *si2;
11294 int res;
11295 typval_T rettv;
11296 typval_T argv[3];
11297 int dummy;
11298 char_u *func_name;
11299 partial_T *partial = sortinfo->item_compare_partial;
11300
11301 /* shortcut after failure in previous call; compare all items equal */
11302 if (sortinfo->item_compare_func_err)
11303 return 0;
11304
11305 si1 = (sortItem_T *)s1;
11306 si2 = (sortItem_T *)s2;
11307
11308 if (partial == NULL)
11309 func_name = sortinfo->item_compare_func;
11310 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020011311 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011312
11313 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
11314 * in the copy without changing the original list items. */
11315 copy_tv(&si1->item->li_tv, &argv[0]);
11316 copy_tv(&si2->item->li_tv, &argv[1]);
11317
11318 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
11319 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020011320 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321 partial, sortinfo->item_compare_selfdict);
11322 clear_tv(&argv[0]);
11323 clear_tv(&argv[1]);
11324
11325 if (res == FAIL)
11326 res = ITEM_COMPARE_FAIL;
11327 else
11328 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
11329 if (sortinfo->item_compare_func_err)
11330 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
11331 clear_tv(&rettv);
11332
11333 /* When the result would be zero, compare the pointers themselves. Makes
11334 * the sort stable. */
11335 if (res == 0 && !sortinfo->item_compare_keep_zero)
11336 res = si1->idx > si2->idx ? 1 : -1;
11337
11338 return res;
11339}
11340
11341/*
11342 * "sort({list})" function
11343 */
11344 static void
11345do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
11346{
11347 list_T *l;
11348 listitem_T *li;
11349 sortItem_T *ptrs;
11350 sortinfo_T *old_sortinfo;
11351 sortinfo_T info;
11352 long len;
11353 long i;
11354
11355 /* Pointer to current info struct used in compare function. Save and
11356 * restore the current one for nested calls. */
11357 old_sortinfo = sortinfo;
11358 sortinfo = &info;
11359
11360 if (argvars[0].v_type != VAR_LIST)
11361 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
11362 else
11363 {
11364 l = argvars[0].vval.v_list;
11365 if (l == NULL || tv_check_lock(l->lv_lock,
11366 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
11367 TRUE))
11368 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020011369 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370
11371 len = list_len(l);
11372 if (len <= 1)
11373 goto theend; /* short list sorts pretty quickly */
11374
11375 info.item_compare_ic = FALSE;
11376 info.item_compare_numeric = FALSE;
11377 info.item_compare_numbers = FALSE;
11378#ifdef FEAT_FLOAT
11379 info.item_compare_float = FALSE;
11380#endif
11381 info.item_compare_func = NULL;
11382 info.item_compare_partial = NULL;
11383 info.item_compare_selfdict = NULL;
11384 if (argvars[1].v_type != VAR_UNKNOWN)
11385 {
11386 /* optional second argument: {func} */
11387 if (argvars[1].v_type == VAR_FUNC)
11388 info.item_compare_func = argvars[1].vval.v_string;
11389 else if (argvars[1].v_type == VAR_PARTIAL)
11390 info.item_compare_partial = argvars[1].vval.v_partial;
11391 else
11392 {
11393 int error = FALSE;
11394
11395 i = (long)get_tv_number_chk(&argvars[1], &error);
11396 if (error)
11397 goto theend; /* type error; errmsg already given */
11398 if (i == 1)
11399 info.item_compare_ic = TRUE;
11400 else if (argvars[1].v_type != VAR_NUMBER)
11401 info.item_compare_func = get_tv_string(&argvars[1]);
11402 else if (i != 0)
11403 {
11404 EMSG(_(e_invarg));
11405 goto theend;
11406 }
11407 if (info.item_compare_func != NULL)
11408 {
11409 if (*info.item_compare_func == NUL)
11410 {
11411 /* empty string means default sort */
11412 info.item_compare_func = NULL;
11413 }
11414 else if (STRCMP(info.item_compare_func, "n") == 0)
11415 {
11416 info.item_compare_func = NULL;
11417 info.item_compare_numeric = TRUE;
11418 }
11419 else if (STRCMP(info.item_compare_func, "N") == 0)
11420 {
11421 info.item_compare_func = NULL;
11422 info.item_compare_numbers = TRUE;
11423 }
11424#ifdef FEAT_FLOAT
11425 else if (STRCMP(info.item_compare_func, "f") == 0)
11426 {
11427 info.item_compare_func = NULL;
11428 info.item_compare_float = TRUE;
11429 }
11430#endif
11431 else if (STRCMP(info.item_compare_func, "i") == 0)
11432 {
11433 info.item_compare_func = NULL;
11434 info.item_compare_ic = TRUE;
11435 }
11436 }
11437 }
11438
11439 if (argvars[2].v_type != VAR_UNKNOWN)
11440 {
11441 /* optional third argument: {dict} */
11442 if (argvars[2].v_type != VAR_DICT)
11443 {
11444 EMSG(_(e_dictreq));
11445 goto theend;
11446 }
11447 info.item_compare_selfdict = argvars[2].vval.v_dict;
11448 }
11449 }
11450
11451 /* Make an array with each entry pointing to an item in the List. */
11452 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
11453 if (ptrs == NULL)
11454 goto theend;
11455
11456 i = 0;
11457 if (sort)
11458 {
11459 /* sort(): ptrs will be the list to sort */
11460 for (li = l->lv_first; li != NULL; li = li->li_next)
11461 {
11462 ptrs[i].item = li;
11463 ptrs[i].idx = i;
11464 ++i;
11465 }
11466
11467 info.item_compare_func_err = FALSE;
11468 info.item_compare_keep_zero = FALSE;
11469 /* test the compare function */
11470 if ((info.item_compare_func != NULL
11471 || info.item_compare_partial != NULL)
11472 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
11473 == ITEM_COMPARE_FAIL)
11474 EMSG(_("E702: Sort compare function failed"));
11475 else
11476 {
11477 /* Sort the array with item pointers. */
11478 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
11479 info.item_compare_func == NULL
11480 && info.item_compare_partial == NULL
11481 ? item_compare : item_compare2);
11482
11483 if (!info.item_compare_func_err)
11484 {
11485 /* Clear the List and append the items in sorted order. */
11486 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
11487 l->lv_len = 0;
11488 for (i = 0; i < len; ++i)
11489 list_append(l, ptrs[i].item);
11490 }
11491 }
11492 }
11493 else
11494 {
11495 int (*item_compare_func_ptr)(const void *, const void *);
11496
11497 /* f_uniq(): ptrs will be a stack of items to remove */
11498 info.item_compare_func_err = FALSE;
11499 info.item_compare_keep_zero = TRUE;
11500 item_compare_func_ptr = info.item_compare_func != NULL
11501 || info.item_compare_partial != NULL
11502 ? item_compare2 : item_compare;
11503
11504 for (li = l->lv_first; li != NULL && li->li_next != NULL;
11505 li = li->li_next)
11506 {
11507 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
11508 == 0)
11509 ptrs[i++].item = li;
11510 if (info.item_compare_func_err)
11511 {
11512 EMSG(_("E882: Uniq compare function failed"));
11513 break;
11514 }
11515 }
11516
11517 if (!info.item_compare_func_err)
11518 {
11519 while (--i >= 0)
11520 {
11521 li = ptrs[i].item->li_next;
11522 ptrs[i].item->li_next = li->li_next;
11523 if (li->li_next != NULL)
11524 li->li_next->li_prev = ptrs[i].item;
11525 else
11526 l->lv_last = ptrs[i].item;
11527 list_fix_watch(l, li);
11528 listitem_free(li);
11529 l->lv_len--;
11530 }
11531 }
11532 }
11533
11534 vim_free(ptrs);
11535 }
11536theend:
11537 sortinfo = old_sortinfo;
11538}
11539
11540/*
11541 * "sort({list})" function
11542 */
11543 static void
11544f_sort(typval_T *argvars, typval_T *rettv)
11545{
11546 do_sort_uniq(argvars, rettv, TRUE);
11547}
11548
11549/*
11550 * "uniq({list})" function
11551 */
11552 static void
11553f_uniq(typval_T *argvars, typval_T *rettv)
11554{
11555 do_sort_uniq(argvars, rettv, FALSE);
11556}
11557
11558/*
11559 * "soundfold({word})" function
11560 */
11561 static void
11562f_soundfold(typval_T *argvars, typval_T *rettv)
11563{
11564 char_u *s;
11565
11566 rettv->v_type = VAR_STRING;
11567 s = get_tv_string(&argvars[0]);
11568#ifdef FEAT_SPELL
11569 rettv->vval.v_string = eval_soundfold(s);
11570#else
11571 rettv->vval.v_string = vim_strsave(s);
11572#endif
11573}
11574
11575/*
11576 * "spellbadword()" function
11577 */
11578 static void
11579f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
11580{
11581 char_u *word = (char_u *)"";
11582 hlf_T attr = HLF_COUNT;
11583 int len = 0;
11584
11585 if (rettv_list_alloc(rettv) == FAIL)
11586 return;
11587
11588#ifdef FEAT_SPELL
11589 if (argvars[0].v_type == VAR_UNKNOWN)
11590 {
11591 /* Find the start and length of the badly spelled word. */
11592 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
11593 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011594 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011595 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010011596 curwin->w_set_curswant = TRUE;
11597 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011598 }
11599 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
11600 {
11601 char_u *str = get_tv_string_chk(&argvars[0]);
11602 int capcol = -1;
11603
11604 if (str != NULL)
11605 {
11606 /* Check the argument for spelling. */
11607 while (*str != NUL)
11608 {
11609 len = spell_check(curwin, str, &attr, &capcol, FALSE);
11610 if (attr != HLF_COUNT)
11611 {
11612 word = str;
11613 break;
11614 }
11615 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020011616 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011617 }
11618 }
11619 }
11620#endif
11621
11622 list_append_string(rettv->vval.v_list, word, len);
11623 list_append_string(rettv->vval.v_list, (char_u *)(
11624 attr == HLF_SPB ? "bad" :
11625 attr == HLF_SPR ? "rare" :
11626 attr == HLF_SPL ? "local" :
11627 attr == HLF_SPC ? "caps" :
11628 ""), -1);
11629}
11630
11631/*
11632 * "spellsuggest()" function
11633 */
11634 static void
11635f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
11636{
11637#ifdef FEAT_SPELL
11638 char_u *str;
11639 int typeerr = FALSE;
11640 int maxcount;
11641 garray_T ga;
11642 int i;
11643 listitem_T *li;
11644 int need_capital = FALSE;
11645#endif
11646
11647 if (rettv_list_alloc(rettv) == FAIL)
11648 return;
11649
11650#ifdef FEAT_SPELL
11651 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
11652 {
11653 str = get_tv_string(&argvars[0]);
11654 if (argvars[1].v_type != VAR_UNKNOWN)
11655 {
11656 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
11657 if (maxcount <= 0)
11658 return;
11659 if (argvars[2].v_type != VAR_UNKNOWN)
11660 {
11661 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
11662 if (typeerr)
11663 return;
11664 }
11665 }
11666 else
11667 maxcount = 25;
11668
11669 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
11670
11671 for (i = 0; i < ga.ga_len; ++i)
11672 {
11673 str = ((char_u **)ga.ga_data)[i];
11674
11675 li = listitem_alloc();
11676 if (li == NULL)
11677 vim_free(str);
11678 else
11679 {
11680 li->li_tv.v_type = VAR_STRING;
11681 li->li_tv.v_lock = 0;
11682 li->li_tv.vval.v_string = str;
11683 list_append(rettv->vval.v_list, li);
11684 }
11685 }
11686 ga_clear(&ga);
11687 }
11688#endif
11689}
11690
11691 static void
11692f_split(typval_T *argvars, typval_T *rettv)
11693{
11694 char_u *str;
11695 char_u *end;
11696 char_u *pat = NULL;
11697 regmatch_T regmatch;
11698 char_u patbuf[NUMBUFLEN];
11699 char_u *save_cpo;
11700 int match;
11701 colnr_T col = 0;
11702 int keepempty = FALSE;
11703 int typeerr = FALSE;
11704
11705 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11706 save_cpo = p_cpo;
11707 p_cpo = (char_u *)"";
11708
11709 str = get_tv_string(&argvars[0]);
11710 if (argvars[1].v_type != VAR_UNKNOWN)
11711 {
11712 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11713 if (pat == NULL)
11714 typeerr = TRUE;
11715 if (argvars[2].v_type != VAR_UNKNOWN)
11716 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
11717 }
11718 if (pat == NULL || *pat == NUL)
11719 pat = (char_u *)"[\\x01- ]\\+";
11720
11721 if (rettv_list_alloc(rettv) == FAIL)
11722 return;
11723 if (typeerr)
11724 return;
11725
11726 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11727 if (regmatch.regprog != NULL)
11728 {
11729 regmatch.rm_ic = FALSE;
11730 while (*str != NUL || keepempty)
11731 {
11732 if (*str == NUL)
11733 match = FALSE; /* empty item at the end */
11734 else
11735 match = vim_regexec_nl(&regmatch, str, col);
11736 if (match)
11737 end = regmatch.startp[0];
11738 else
11739 end = str + STRLEN(str);
11740 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
11741 && *str != NUL && match && end < regmatch.endp[0]))
11742 {
11743 if (list_append_string(rettv->vval.v_list, str,
11744 (int)(end - str)) == FAIL)
11745 break;
11746 }
11747 if (!match)
11748 break;
11749 /* Advance to just after the match. */
11750 if (regmatch.endp[0] > str)
11751 col = 0;
11752 else
11753 {
11754 /* Don't get stuck at the same match. */
11755#ifdef FEAT_MBYTE
11756 col = (*mb_ptr2len)(regmatch.endp[0]);
11757#else
11758 col = 1;
11759#endif
11760 }
11761 str = regmatch.endp[0];
11762 }
11763
11764 vim_regfree(regmatch.regprog);
11765 }
11766
11767 p_cpo = save_cpo;
11768}
11769
11770#ifdef FEAT_FLOAT
11771/*
11772 * "sqrt()" function
11773 */
11774 static void
11775f_sqrt(typval_T *argvars, typval_T *rettv)
11776{
11777 float_T f = 0.0;
11778
11779 rettv->v_type = VAR_FLOAT;
11780 if (get_float_arg(argvars, &f) == OK)
11781 rettv->vval.v_float = sqrt(f);
11782 else
11783 rettv->vval.v_float = 0.0;
11784}
11785
11786/*
11787 * "str2float()" function
11788 */
11789 static void
11790f_str2float(typval_T *argvars, typval_T *rettv)
11791{
11792 char_u *p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011793 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011794
Bram Moolenaar08243d22017-01-10 16:12:29 +010011795 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011796 p = skipwhite(p + 1);
11797 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011798 if (isneg)
11799 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011800 rettv->v_type = VAR_FLOAT;
11801}
11802#endif
11803
11804/*
11805 * "str2nr()" function
11806 */
11807 static void
11808f_str2nr(typval_T *argvars, typval_T *rettv)
11809{
11810 int base = 10;
11811 char_u *p;
11812 varnumber_T n;
11813 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010011814 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815
11816 if (argvars[1].v_type != VAR_UNKNOWN)
11817 {
11818 base = (int)get_tv_number(&argvars[1]);
11819 if (base != 2 && base != 8 && base != 10 && base != 16)
11820 {
11821 EMSG(_(e_invarg));
11822 return;
11823 }
11824 }
11825
11826 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010011827 isneg = (*p == '-');
11828 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011829 p = skipwhite(p + 1);
11830 switch (base)
11831 {
11832 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
11833 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
11834 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
11835 default: what = 0;
11836 }
11837 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010011838 if (isneg)
11839 rettv->vval.v_number = -n;
11840 else
11841 rettv->vval.v_number = n;
11842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011843}
11844
11845#ifdef HAVE_STRFTIME
11846/*
11847 * "strftime({format}[, {time}])" function
11848 */
11849 static void
11850f_strftime(typval_T *argvars, typval_T *rettv)
11851{
11852 char_u result_buf[256];
11853 struct tm *curtime;
11854 time_t seconds;
11855 char_u *p;
11856
11857 rettv->v_type = VAR_STRING;
11858
11859 p = get_tv_string(&argvars[0]);
11860 if (argvars[1].v_type == VAR_UNKNOWN)
11861 seconds = time(NULL);
11862 else
11863 seconds = (time_t)get_tv_number(&argvars[1]);
11864 curtime = localtime(&seconds);
11865 /* MSVC returns NULL for an invalid value of seconds. */
11866 if (curtime == NULL)
11867 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
11868 else
11869 {
11870# ifdef FEAT_MBYTE
11871 vimconv_T conv;
11872 char_u *enc;
11873
11874 conv.vc_type = CONV_NONE;
11875 enc = enc_locale();
11876 convert_setup(&conv, p_enc, enc);
11877 if (conv.vc_type != CONV_NONE)
11878 p = string_convert(&conv, p, NULL);
11879# endif
11880 if (p != NULL)
11881 (void)strftime((char *)result_buf, sizeof(result_buf),
11882 (char *)p, curtime);
11883 else
11884 result_buf[0] = NUL;
11885
11886# ifdef FEAT_MBYTE
11887 if (conv.vc_type != CONV_NONE)
11888 vim_free(p);
11889 convert_setup(&conv, enc, p_enc);
11890 if (conv.vc_type != CONV_NONE)
11891 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
11892 else
11893# endif
11894 rettv->vval.v_string = vim_strsave(result_buf);
11895
11896# ifdef FEAT_MBYTE
11897 /* Release conversion descriptors */
11898 convert_setup(&conv, NULL, NULL);
11899 vim_free(enc);
11900# endif
11901 }
11902}
11903#endif
11904
11905/*
11906 * "strgetchar()" function
11907 */
11908 static void
11909f_strgetchar(typval_T *argvars, typval_T *rettv)
11910{
11911 char_u *str;
11912 int len;
11913 int error = FALSE;
11914 int charidx;
11915
11916 rettv->vval.v_number = -1;
11917 str = get_tv_string_chk(&argvars[0]);
11918 if (str == NULL)
11919 return;
11920 len = (int)STRLEN(str);
11921 charidx = (int)get_tv_number_chk(&argvars[1], &error);
11922 if (error)
11923 return;
11924#ifdef FEAT_MBYTE
11925 {
11926 int byteidx = 0;
11927
11928 while (charidx >= 0 && byteidx < len)
11929 {
11930 if (charidx == 0)
11931 {
11932 rettv->vval.v_number = mb_ptr2char(str + byteidx);
11933 break;
11934 }
11935 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020011936 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011937 }
11938 }
11939#else
11940 if (charidx < len)
11941 rettv->vval.v_number = str[charidx];
11942#endif
11943}
11944
11945/*
11946 * "stridx()" function
11947 */
11948 static void
11949f_stridx(typval_T *argvars, typval_T *rettv)
11950{
11951 char_u buf[NUMBUFLEN];
11952 char_u *needle;
11953 char_u *haystack;
11954 char_u *save_haystack;
11955 char_u *pos;
11956 int start_idx;
11957
11958 needle = get_tv_string_chk(&argvars[1]);
11959 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
11960 rettv->vval.v_number = -1;
11961 if (needle == NULL || haystack == NULL)
11962 return; /* type error; errmsg already given */
11963
11964 if (argvars[2].v_type != VAR_UNKNOWN)
11965 {
11966 int error = FALSE;
11967
11968 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
11969 if (error || start_idx >= (int)STRLEN(haystack))
11970 return;
11971 if (start_idx >= 0)
11972 haystack += start_idx;
11973 }
11974
11975 pos = (char_u *)strstr((char *)haystack, (char *)needle);
11976 if (pos != NULL)
11977 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
11978}
11979
11980/*
11981 * "string()" function
11982 */
11983 static void
11984f_string(typval_T *argvars, typval_T *rettv)
11985{
11986 char_u *tofree;
11987 char_u numbuf[NUMBUFLEN];
11988
11989 rettv->v_type = VAR_STRING;
11990 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
11991 get_copyID());
11992 /* Make a copy if we have a value but it's not in allocated memory. */
11993 if (rettv->vval.v_string != NULL && tofree == NULL)
11994 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
11995}
11996
11997/*
11998 * "strlen()" function
11999 */
12000 static void
12001f_strlen(typval_T *argvars, typval_T *rettv)
12002{
12003 rettv->vval.v_number = (varnumber_T)(STRLEN(
12004 get_tv_string(&argvars[0])));
12005}
12006
12007/*
12008 * "strchars()" function
12009 */
12010 static void
12011f_strchars(typval_T *argvars, typval_T *rettv)
12012{
12013 char_u *s = get_tv_string(&argvars[0]);
12014 int skipcc = 0;
12015#ifdef FEAT_MBYTE
12016 varnumber_T len = 0;
12017 int (*func_mb_ptr2char_adv)(char_u **pp);
12018#endif
12019
12020 if (argvars[1].v_type != VAR_UNKNOWN)
12021 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
12022 if (skipcc < 0 || skipcc > 1)
12023 EMSG(_(e_invarg));
12024 else
12025 {
12026#ifdef FEAT_MBYTE
12027 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12028 while (*s != NUL)
12029 {
12030 func_mb_ptr2char_adv(&s);
12031 ++len;
12032 }
12033 rettv->vval.v_number = len;
12034#else
12035 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12036#endif
12037 }
12038}
12039
12040/*
12041 * "strdisplaywidth()" function
12042 */
12043 static void
12044f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12045{
12046 char_u *s = get_tv_string(&argvars[0]);
12047 int col = 0;
12048
12049 if (argvars[1].v_type != VAR_UNKNOWN)
12050 col = (int)get_tv_number(&argvars[1]);
12051
12052 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12053}
12054
12055/*
12056 * "strwidth()" function
12057 */
12058 static void
12059f_strwidth(typval_T *argvars, typval_T *rettv)
12060{
12061 char_u *s = get_tv_string(&argvars[0]);
12062
12063 rettv->vval.v_number = (varnumber_T)(
12064#ifdef FEAT_MBYTE
12065 mb_string2cells(s, -1)
12066#else
12067 STRLEN(s)
12068#endif
12069 );
12070}
12071
12072/*
12073 * "strcharpart()" function
12074 */
12075 static void
12076f_strcharpart(typval_T *argvars, typval_T *rettv)
12077{
12078#ifdef FEAT_MBYTE
12079 char_u *p;
12080 int nchar;
12081 int nbyte = 0;
12082 int charlen;
12083 int len = 0;
12084 int slen;
12085 int error = FALSE;
12086
12087 p = get_tv_string(&argvars[0]);
12088 slen = (int)STRLEN(p);
12089
12090 nchar = (int)get_tv_number_chk(&argvars[1], &error);
12091 if (!error)
12092 {
12093 if (nchar > 0)
12094 while (nchar > 0 && nbyte < slen)
12095 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012096 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097 --nchar;
12098 }
12099 else
12100 nbyte = nchar;
12101 if (argvars[2].v_type != VAR_UNKNOWN)
12102 {
12103 charlen = (int)get_tv_number(&argvars[2]);
12104 while (charlen > 0 && nbyte + len < slen)
12105 {
12106 int off = nbyte + len;
12107
12108 if (off < 0)
12109 len += 1;
12110 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012111 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012112 --charlen;
12113 }
12114 }
12115 else
12116 len = slen - nbyte; /* default: all bytes that are available. */
12117 }
12118
12119 /*
12120 * Only return the overlap between the specified part and the actual
12121 * string.
12122 */
12123 if (nbyte < 0)
12124 {
12125 len += nbyte;
12126 nbyte = 0;
12127 }
12128 else if (nbyte > slen)
12129 nbyte = slen;
12130 if (len < 0)
12131 len = 0;
12132 else if (nbyte + len > slen)
12133 len = slen - nbyte;
12134
12135 rettv->v_type = VAR_STRING;
12136 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12137#else
12138 f_strpart(argvars, rettv);
12139#endif
12140}
12141
12142/*
12143 * "strpart()" function
12144 */
12145 static void
12146f_strpart(typval_T *argvars, typval_T *rettv)
12147{
12148 char_u *p;
12149 int n;
12150 int len;
12151 int slen;
12152 int error = FALSE;
12153
12154 p = get_tv_string(&argvars[0]);
12155 slen = (int)STRLEN(p);
12156
12157 n = (int)get_tv_number_chk(&argvars[1], &error);
12158 if (error)
12159 len = 0;
12160 else if (argvars[2].v_type != VAR_UNKNOWN)
12161 len = (int)get_tv_number(&argvars[2]);
12162 else
12163 len = slen - n; /* default len: all bytes that are available. */
12164
12165 /*
12166 * Only return the overlap between the specified part and the actual
12167 * string.
12168 */
12169 if (n < 0)
12170 {
12171 len += n;
12172 n = 0;
12173 }
12174 else if (n > slen)
12175 n = slen;
12176 if (len < 0)
12177 len = 0;
12178 else if (n + len > slen)
12179 len = slen - n;
12180
12181 rettv->v_type = VAR_STRING;
12182 rettv->vval.v_string = vim_strnsave(p + n, len);
12183}
12184
12185/*
12186 * "strridx()" function
12187 */
12188 static void
12189f_strridx(typval_T *argvars, typval_T *rettv)
12190{
12191 char_u buf[NUMBUFLEN];
12192 char_u *needle;
12193 char_u *haystack;
12194 char_u *rest;
12195 char_u *lastmatch = NULL;
12196 int haystack_len, end_idx;
12197
12198 needle = get_tv_string_chk(&argvars[1]);
12199 haystack = get_tv_string_buf_chk(&argvars[0], buf);
12200
12201 rettv->vval.v_number = -1;
12202 if (needle == NULL || haystack == NULL)
12203 return; /* type error; errmsg already given */
12204
12205 haystack_len = (int)STRLEN(haystack);
12206 if (argvars[2].v_type != VAR_UNKNOWN)
12207 {
12208 /* Third argument: upper limit for index */
12209 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
12210 if (end_idx < 0)
12211 return; /* can never find a match */
12212 }
12213 else
12214 end_idx = haystack_len;
12215
12216 if (*needle == NUL)
12217 {
12218 /* Empty string matches past the end. */
12219 lastmatch = haystack + end_idx;
12220 }
12221 else
12222 {
12223 for (rest = haystack; *rest != '\0'; ++rest)
12224 {
12225 rest = (char_u *)strstr((char *)rest, (char *)needle);
12226 if (rest == NULL || rest > haystack + end_idx)
12227 break;
12228 lastmatch = rest;
12229 }
12230 }
12231
12232 if (lastmatch == NULL)
12233 rettv->vval.v_number = -1;
12234 else
12235 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12236}
12237
12238/*
12239 * "strtrans()" function
12240 */
12241 static void
12242f_strtrans(typval_T *argvars, typval_T *rettv)
12243{
12244 rettv->v_type = VAR_STRING;
12245 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
12246}
12247
12248/*
12249 * "submatch()" function
12250 */
12251 static void
12252f_submatch(typval_T *argvars, typval_T *rettv)
12253{
12254 int error = FALSE;
12255 int no;
12256 int retList = 0;
12257
12258 no = (int)get_tv_number_chk(&argvars[0], &error);
12259 if (error)
12260 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012261 if (no < 0 || no >= NSUBEXP)
12262 {
Bram Moolenaar79518e22017-02-17 16:31:35 +010012263 EMSGN(_("E935: invalid submatch number: %d"), no);
12264 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012265 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012266 if (argvars[1].v_type != VAR_UNKNOWN)
12267 retList = (int)get_tv_number_chk(&argvars[1], &error);
12268 if (error)
12269 return;
12270
12271 if (retList == 0)
12272 {
12273 rettv->v_type = VAR_STRING;
12274 rettv->vval.v_string = reg_submatch(no);
12275 }
12276 else
12277 {
12278 rettv->v_type = VAR_LIST;
12279 rettv->vval.v_list = reg_submatch_list(no);
12280 }
12281}
12282
12283/*
12284 * "substitute()" function
12285 */
12286 static void
12287f_substitute(typval_T *argvars, typval_T *rettv)
12288{
12289 char_u patbuf[NUMBUFLEN];
12290 char_u subbuf[NUMBUFLEN];
12291 char_u flagsbuf[NUMBUFLEN];
12292
12293 char_u *str = get_tv_string_chk(&argvars[0]);
12294 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012295 char_u *sub = NULL;
12296 typval_T *expr = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012297 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
12298
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012299 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12300 expr = &argvars[2];
12301 else
12302 sub = get_tv_string_buf_chk(&argvars[2], subbuf);
12303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012304 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012305 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12306 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012307 rettv->vval.v_string = NULL;
12308 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012309 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012310}
12311
12312/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020012313 * "swapinfo(swap_filename)" function
12314 */
12315 static void
12316f_swapinfo(typval_T *argvars, typval_T *rettv)
12317{
12318 if (rettv_dict_alloc(rettv) == OK)
12319 get_b0_dict(get_tv_string(argvars), rettv->vval.v_dict);
12320}
12321
12322/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020012323 * "swapname(expr)" function
12324 */
12325 static void
12326f_swapname(typval_T *argvars, typval_T *rettv)
12327{
12328 buf_T *buf;
12329
12330 rettv->v_type = VAR_STRING;
12331 buf = get_buf_tv(&argvars[0], FALSE);
12332 if (buf == NULL || buf->b_ml.ml_mfp == NULL
12333 || buf->b_ml.ml_mfp->mf_fname == NULL)
12334 rettv->vval.v_string = NULL;
12335 else
12336 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
12337}
12338
12339/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012340 * "synID(lnum, col, trans)" function
12341 */
12342 static void
12343f_synID(typval_T *argvars UNUSED, typval_T *rettv)
12344{
12345 int id = 0;
12346#ifdef FEAT_SYN_HL
12347 linenr_T lnum;
12348 colnr_T col;
12349 int trans;
12350 int transerr = FALSE;
12351
12352 lnum = get_tv_lnum(argvars); /* -1 on type error */
12353 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12354 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
12355
12356 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12357 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
12358 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
12359#endif
12360
12361 rettv->vval.v_number = id;
12362}
12363
12364/*
12365 * "synIDattr(id, what [, mode])" function
12366 */
12367 static void
12368f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
12369{
12370 char_u *p = NULL;
12371#ifdef FEAT_SYN_HL
12372 int id;
12373 char_u *what;
12374 char_u *mode;
12375 char_u modebuf[NUMBUFLEN];
12376 int modec;
12377
12378 id = (int)get_tv_number(&argvars[0]);
12379 what = get_tv_string(&argvars[1]);
12380 if (argvars[2].v_type != VAR_UNKNOWN)
12381 {
12382 mode = get_tv_string_buf(&argvars[2], modebuf);
12383 modec = TOLOWER_ASC(mode[0]);
12384 if (modec != 't' && modec != 'c' && modec != 'g')
12385 modec = 0; /* replace invalid with current */
12386 }
12387 else
12388 {
12389#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
12390 if (USE_24BIT)
12391 modec = 'g';
12392 else
12393#endif
12394 if (t_colors > 1)
12395 modec = 'c';
12396 else
12397 modec = 't';
12398 }
12399
12400
12401 switch (TOLOWER_ASC(what[0]))
12402 {
12403 case 'b':
12404 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12405 p = highlight_color(id, what, modec);
12406 else /* bold */
12407 p = highlight_has_attr(id, HL_BOLD, modec);
12408 break;
12409
12410 case 'f': /* fg[#] or font */
12411 p = highlight_color(id, what, modec);
12412 break;
12413
12414 case 'i':
12415 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12416 p = highlight_has_attr(id, HL_INVERSE, modec);
12417 else /* italic */
12418 p = highlight_has_attr(id, HL_ITALIC, modec);
12419 break;
12420
12421 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020012422 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012423 break;
12424
12425 case 'r': /* reverse */
12426 p = highlight_has_attr(id, HL_INVERSE, modec);
12427 break;
12428
12429 case 's':
12430 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
12431 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020012432 /* strikeout */
12433 else if (TOLOWER_ASC(what[1]) == 't' &&
12434 TOLOWER_ASC(what[2]) == 'r')
12435 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012436 else /* standout */
12437 p = highlight_has_attr(id, HL_STANDOUT, modec);
12438 break;
12439
12440 case 'u':
12441 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
12442 /* underline */
12443 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12444 else
12445 /* undercurl */
12446 p = highlight_has_attr(id, HL_UNDERCURL, modec);
12447 break;
12448 }
12449
12450 if (p != NULL)
12451 p = vim_strsave(p);
12452#endif
12453 rettv->v_type = VAR_STRING;
12454 rettv->vval.v_string = p;
12455}
12456
12457/*
12458 * "synIDtrans(id)" function
12459 */
12460 static void
12461f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
12462{
12463 int id;
12464
12465#ifdef FEAT_SYN_HL
12466 id = (int)get_tv_number(&argvars[0]);
12467
12468 if (id > 0)
12469 id = syn_get_final_id(id);
12470 else
12471#endif
12472 id = 0;
12473
12474 rettv->vval.v_number = id;
12475}
12476
12477/*
12478 * "synconcealed(lnum, col)" function
12479 */
12480 static void
12481f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
12482{
12483#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12484 linenr_T lnum;
12485 colnr_T col;
12486 int syntax_flags = 0;
12487 int cchar;
12488 int matchid = 0;
12489 char_u str[NUMBUFLEN];
12490#endif
12491
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012492 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493
12494#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
12495 lnum = get_tv_lnum(argvars); /* -1 on type error */
12496 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12497
12498 vim_memset(str, NUL, sizeof(str));
12499
12500 if (rettv_list_alloc(rettv) != FAIL)
12501 {
12502 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12503 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12504 && curwin->w_p_cole > 0)
12505 {
12506 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
12507 syntax_flags = get_syntax_info(&matchid);
12508
12509 /* get the conceal character */
12510 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
12511 {
12512 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020012513 if (cchar == NUL && curwin->w_p_cole == 1)
12514 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515 if (cchar != NUL)
12516 {
12517# ifdef FEAT_MBYTE
12518 if (has_mbyte)
12519 (*mb_char2bytes)(cchar, str);
12520 else
12521# endif
12522 str[0] = cchar;
12523 }
12524 }
12525 }
12526
12527 list_append_number(rettv->vval.v_list,
12528 (syntax_flags & HL_CONCEAL) != 0);
12529 /* -1 to auto-determine strlen */
12530 list_append_string(rettv->vval.v_list, str, -1);
12531 list_append_number(rettv->vval.v_list, matchid);
12532 }
12533#endif
12534}
12535
12536/*
12537 * "synstack(lnum, col)" function
12538 */
12539 static void
12540f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
12541{
12542#ifdef FEAT_SYN_HL
12543 linenr_T lnum;
12544 colnr_T col;
12545 int i;
12546 int id;
12547#endif
12548
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012549 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550
12551#ifdef FEAT_SYN_HL
12552 lnum = get_tv_lnum(argvars); /* -1 on type error */
12553 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
12554
12555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
12556 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
12557 && rettv_list_alloc(rettv) != FAIL)
12558 {
12559 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
12560 for (i = 0; ; ++i)
12561 {
12562 id = syn_get_stack_item(i);
12563 if (id < 0)
12564 break;
12565 if (list_append_number(rettv->vval.v_list, id) == FAIL)
12566 break;
12567 }
12568 }
12569#endif
12570}
12571
12572 static void
12573get_cmd_output_as_rettv(
12574 typval_T *argvars,
12575 typval_T *rettv,
12576 int retlist)
12577{
12578 char_u *res = NULL;
12579 char_u *p;
12580 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 int err = FALSE;
12582 FILE *fd;
12583 list_T *list = NULL;
12584 int flags = SHELL_SILENT;
12585
12586 rettv->v_type = VAR_STRING;
12587 rettv->vval.v_string = NULL;
12588 if (check_restricted() || check_secure())
12589 goto errret;
12590
12591 if (argvars[1].v_type != VAR_UNKNOWN)
12592 {
12593 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010012594 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012595 * command.
12596 */
12597 if ((infile = vim_tempname('i', TRUE)) == NULL)
12598 {
12599 EMSG(_(e_notmp));
12600 goto errret;
12601 }
12602
12603 fd = mch_fopen((char *)infile, WRITEBIN);
12604 if (fd == NULL)
12605 {
12606 EMSG2(_(e_notopen), infile);
12607 goto errret;
12608 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010012609 if (argvars[1].v_type == VAR_NUMBER)
12610 {
12611 linenr_T lnum;
12612 buf_T *buf;
12613
12614 buf = buflist_findnr(argvars[1].vval.v_number);
12615 if (buf == NULL)
12616 {
12617 EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010012618 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010012619 goto errret;
12620 }
12621
12622 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
12623 {
12624 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
12625 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
12626 {
12627 err = TRUE;
12628 break;
12629 }
12630 if (putc(NL, fd) == EOF)
12631 {
12632 err = TRUE;
12633 break;
12634 }
12635 }
12636 }
12637 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 {
12639 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
12640 err = TRUE;
12641 }
12642 else
12643 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010012644 size_t len;
12645 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012646
12647 p = get_tv_string_buf_chk(&argvars[1], buf);
12648 if (p == NULL)
12649 {
12650 fclose(fd);
12651 goto errret; /* type error; errmsg already given */
12652 }
12653 len = STRLEN(p);
12654 if (len > 0 && fwrite(p, len, 1, fd) != 1)
12655 err = TRUE;
12656 }
12657 if (fclose(fd) != 0)
12658 err = TRUE;
12659 if (err)
12660 {
12661 EMSG(_("E677: Error writing temp file"));
12662 goto errret;
12663 }
12664 }
12665
12666 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
12667 * echoes typeahead, that messes up the display. */
12668 if (!msg_silent)
12669 flags += SHELL_COOKED;
12670
12671 if (retlist)
12672 {
12673 int len;
12674 listitem_T *li;
12675 char_u *s = NULL;
12676 char_u *start;
12677 char_u *end;
12678 int i;
12679
12680 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
12681 if (res == NULL)
12682 goto errret;
12683
12684 list = list_alloc();
12685 if (list == NULL)
12686 goto errret;
12687
12688 for (i = 0; i < len; ++i)
12689 {
12690 start = res + i;
12691 while (i < len && res[i] != NL)
12692 ++i;
12693 end = res + i;
12694
12695 s = alloc((unsigned)(end - start + 1));
12696 if (s == NULL)
12697 goto errret;
12698
12699 for (p = s; start < end; ++p, ++start)
12700 *p = *start == NUL ? NL : *start;
12701 *p = NUL;
12702
12703 li = listitem_alloc();
12704 if (li == NULL)
12705 {
12706 vim_free(s);
12707 goto errret;
12708 }
12709 li->li_tv.v_type = VAR_STRING;
12710 li->li_tv.v_lock = 0;
12711 li->li_tv.vval.v_string = s;
12712 list_append(list, li);
12713 }
12714
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012715 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012716 list = NULL;
12717 }
12718 else
12719 {
12720 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
12721#ifdef USE_CR
12722 /* translate <CR> into <NL> */
12723 if (res != NULL)
12724 {
12725 char_u *s;
12726
12727 for (s = res; *s; ++s)
12728 {
12729 if (*s == CAR)
12730 *s = NL;
12731 }
12732 }
12733#else
12734# ifdef USE_CRNL
12735 /* translate <CR><NL> into <NL> */
12736 if (res != NULL)
12737 {
12738 char_u *s, *d;
12739
12740 d = res;
12741 for (s = res; *s; ++s)
12742 {
12743 if (s[0] == CAR && s[1] == NL)
12744 ++s;
12745 *d++ = *s;
12746 }
12747 *d = NUL;
12748 }
12749# endif
12750#endif
12751 rettv->vval.v_string = res;
12752 res = NULL;
12753 }
12754
12755errret:
12756 if (infile != NULL)
12757 {
12758 mch_remove(infile);
12759 vim_free(infile);
12760 }
12761 if (res != NULL)
12762 vim_free(res);
12763 if (list != NULL)
12764 list_free(list);
12765}
12766
12767/*
12768 * "system()" function
12769 */
12770 static void
12771f_system(typval_T *argvars, typval_T *rettv)
12772{
12773 get_cmd_output_as_rettv(argvars, rettv, FALSE);
12774}
12775
12776/*
12777 * "systemlist()" function
12778 */
12779 static void
12780f_systemlist(typval_T *argvars, typval_T *rettv)
12781{
12782 get_cmd_output_as_rettv(argvars, rettv, TRUE);
12783}
12784
12785/*
12786 * "tabpagebuflist()" function
12787 */
12788 static void
12789f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12790{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791 tabpage_T *tp;
12792 win_T *wp = NULL;
12793
12794 if (argvars[0].v_type == VAR_UNKNOWN)
12795 wp = firstwin;
12796 else
12797 {
12798 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12799 if (tp != NULL)
12800 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12801 }
12802 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
12803 {
12804 for (; wp != NULL; wp = wp->w_next)
12805 if (list_append_number(rettv->vval.v_list,
12806 wp->w_buffer->b_fnum) == FAIL)
12807 break;
12808 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809}
12810
12811
12812/*
12813 * "tabpagenr()" function
12814 */
12815 static void
12816f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
12817{
12818 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819 char_u *arg;
12820
12821 if (argvars[0].v_type != VAR_UNKNOWN)
12822 {
12823 arg = get_tv_string_chk(&argvars[0]);
12824 nr = 0;
12825 if (arg != NULL)
12826 {
12827 if (STRCMP(arg, "$") == 0)
12828 nr = tabpage_index(NULL) - 1;
12829 else
12830 EMSG2(_(e_invexpr2), arg);
12831 }
12832 }
12833 else
12834 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 rettv->vval.v_number = nr;
12836}
12837
12838
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012839/*
12840 * Common code for tabpagewinnr() and winnr().
12841 */
12842 static int
12843get_winnr(tabpage_T *tp, typval_T *argvar)
12844{
12845 win_T *twin;
12846 int nr = 1;
12847 win_T *wp;
12848 char_u *arg;
12849
12850 twin = (tp == curtab) ? curwin : tp->tp_curwin;
12851 if (argvar->v_type != VAR_UNKNOWN)
12852 {
12853 arg = get_tv_string_chk(argvar);
12854 if (arg == NULL)
12855 nr = 0; /* type error; errmsg already given */
12856 else if (STRCMP(arg, "$") == 0)
12857 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
12858 else if (STRCMP(arg, "#") == 0)
12859 {
12860 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
12861 if (twin == NULL)
12862 nr = 0;
12863 }
12864 else
12865 {
12866 EMSG2(_(e_invexpr2), arg);
12867 nr = 0;
12868 }
12869 }
12870
12871 if (nr > 0)
12872 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
12873 wp != twin; wp = wp->w_next)
12874 {
12875 if (wp == NULL)
12876 {
12877 /* didn't find it in this tabpage */
12878 nr = 0;
12879 break;
12880 }
12881 ++nr;
12882 }
12883 return nr;
12884}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012885
12886/*
12887 * "tabpagewinnr()" function
12888 */
12889 static void
12890f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
12891{
12892 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 tabpage_T *tp;
12894
12895 tp = find_tabpage((int)get_tv_number(&argvars[0]));
12896 if (tp == NULL)
12897 nr = 0;
12898 else
12899 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012900 rettv->vval.v_number = nr;
12901}
12902
12903
12904/*
12905 * "tagfiles()" function
12906 */
12907 static void
12908f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
12909{
12910 char_u *fname;
12911 tagname_T tn;
12912 int first;
12913
12914 if (rettv_list_alloc(rettv) == FAIL)
12915 return;
12916 fname = alloc(MAXPATHL);
12917 if (fname == NULL)
12918 return;
12919
12920 for (first = TRUE; ; first = FALSE)
12921 if (get_tagfname(&tn, first, fname) == FAIL
12922 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
12923 break;
12924 tagname_free(&tn);
12925 vim_free(fname);
12926}
12927
12928/*
12929 * "taglist()" function
12930 */
12931 static void
12932f_taglist(typval_T *argvars, typval_T *rettv)
12933{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012934 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 char_u *tag_pattern;
12936
12937 tag_pattern = get_tv_string(&argvars[0]);
12938
12939 rettv->vval.v_number = FALSE;
12940 if (*tag_pattern == NUL)
12941 return;
12942
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012943 if (argvars[1].v_type != VAR_UNKNOWN)
12944 fname = get_tv_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012945 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010012946 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012947}
12948
12949/*
12950 * "tempname()" function
12951 */
12952 static void
12953f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
12954{
12955 static int x = 'A';
12956
12957 rettv->v_type = VAR_STRING;
12958 rettv->vval.v_string = vim_tempname(x, FALSE);
12959
12960 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12961 * names. Skip 'I' and 'O', they are used for shell redirection. */
12962 do
12963 {
12964 if (x == 'Z')
12965 x = '0';
12966 else if (x == '9')
12967 x = 'A';
12968 else
12969 {
12970#ifdef EBCDIC
12971 if (x == 'I')
12972 x = 'J';
12973 else if (x == 'R')
12974 x = 'S';
12975 else
12976#endif
12977 ++x;
12978 }
12979 } while (x == 'I' || x == 'O');
12980}
12981
12982#ifdef FEAT_FLOAT
12983/*
12984 * "tan()" function
12985 */
12986 static void
12987f_tan(typval_T *argvars, typval_T *rettv)
12988{
12989 float_T f = 0.0;
12990
12991 rettv->v_type = VAR_FLOAT;
12992 if (get_float_arg(argvars, &f) == OK)
12993 rettv->vval.v_float = tan(f);
12994 else
12995 rettv->vval.v_float = 0.0;
12996}
12997
12998/*
12999 * "tanh()" function
13000 */
13001 static void
13002f_tanh(typval_T *argvars, typval_T *rettv)
13003{
13004 float_T f = 0.0;
13005
13006 rettv->v_type = VAR_FLOAT;
13007 if (get_float_arg(argvars, &f) == OK)
13008 rettv->vval.v_float = tanh(f);
13009 else
13010 rettv->vval.v_float = 0.0;
13011}
13012#endif
13013
13014/*
13015 * "test_alloc_fail(id, countdown, repeat)" function
13016 */
13017 static void
13018f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13019{
13020 if (argvars[0].v_type != VAR_NUMBER
13021 || argvars[0].vval.v_number <= 0
13022 || argvars[1].v_type != VAR_NUMBER
13023 || argvars[1].vval.v_number < 0
13024 || argvars[2].v_type != VAR_NUMBER)
13025 EMSG(_(e_invarg));
13026 else
13027 {
13028 alloc_fail_id = argvars[0].vval.v_number;
13029 if (alloc_fail_id >= aid_last)
13030 EMSG(_(e_invarg));
13031 alloc_fail_countdown = argvars[1].vval.v_number;
13032 alloc_fail_repeat = argvars[2].vval.v_number;
13033 did_outofmem_msg = FALSE;
13034 }
13035}
13036
13037/*
13038 * "test_autochdir()"
13039 */
13040 static void
13041f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13042{
13043#if defined(FEAT_AUTOCHDIR)
13044 test_autochdir = TRUE;
13045#endif
13046}
13047
13048/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013049 * "test_feedinput()"
13050 */
13051 static void
13052f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13053{
13054#ifdef USE_INPUT_BUF
13055 char_u *val = get_tv_string_chk(&argvars[0]);
13056
13057 if (val != NULL)
13058 {
13059 trash_input_buf();
13060 add_to_input_buf_csi(val, (int)STRLEN(val));
13061 }
13062#endif
13063}
13064
13065/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013066 * "test_option_not_set({name})" function
13067 */
13068 static void
13069f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13070{
13071 char_u *name = (char_u *)"";
13072
13073 if (argvars[0].v_type != VAR_STRING)
13074 EMSG(_(e_invarg));
13075 else
13076 {
Bram Moolenaarbf1c1b82018-09-13 21:30:05 +020013077 name = get_tv_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013078 if (reset_option_was_set(name) == FAIL)
13079 EMSG2(_(e_invarg2), name);
13080 }
13081}
13082
13083/*
13084 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013085 */
13086 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013087f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013089 char_u *name = (char_u *)"";
13090 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013091 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013092
13093 if (argvars[0].v_type != VAR_STRING
13094 || (argvars[1].v_type) != VAR_NUMBER)
13095 EMSG(_(e_invarg));
13096 else
13097 {
13098 name = get_tv_string_chk(&argvars[0]);
13099 val = (int)get_tv_number(&argvars[1]);
13100
13101 if (STRCMP(name, (char_u *)"redraw") == 0)
13102 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013103 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13104 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013105 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13106 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013107 else if (STRCMP(name, (char_u *)"starting") == 0)
13108 {
13109 if (val)
13110 {
13111 if (save_starting < 0)
13112 save_starting = starting;
13113 starting = 0;
13114 }
13115 else
13116 {
13117 starting = save_starting;
13118 save_starting = -1;
13119 }
13120 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013121 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13122 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013123 else if (STRCMP(name, (char_u *)"ALL") == 0)
13124 {
13125 disable_char_avail_for_testing = FALSE;
13126 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013127 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013128 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013129 if (save_starting >= 0)
13130 {
13131 starting = save_starting;
13132 save_starting = -1;
13133 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013134 }
13135 else
13136 EMSG2(_(e_invarg2), name);
13137 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138}
13139
13140/*
13141 * "test_garbagecollect_now()" function
13142 */
13143 static void
13144f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13145{
13146 /* This is dangerous, any Lists and Dicts used internally may be freed
13147 * while still in use. */
13148 garbage_collect(TRUE);
13149}
13150
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013151/*
13152 * "test_ignore_error()" function
13153 */
13154 static void
13155f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13156{
13157 ignore_error_for_testing(get_tv_string(&argvars[0]));
13158}
13159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013160#ifdef FEAT_JOB_CHANNEL
13161 static void
13162f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13163{
13164 rettv->v_type = VAR_CHANNEL;
13165 rettv->vval.v_channel = NULL;
13166}
13167#endif
13168
13169 static void
13170f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13171{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013172 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013173}
13174
13175#ifdef FEAT_JOB_CHANNEL
13176 static void
13177f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13178{
13179 rettv->v_type = VAR_JOB;
13180 rettv->vval.v_job = NULL;
13181}
13182#endif
13183
13184 static void
13185f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13186{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013187 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188}
13189
13190 static void
13191f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13192{
13193 rettv->v_type = VAR_PARTIAL;
13194 rettv->vval.v_partial = NULL;
13195}
13196
13197 static void
13198f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13199{
13200 rettv->v_type = VAR_STRING;
13201 rettv->vval.v_string = NULL;
13202}
13203
Bram Moolenaarab186732018-09-14 21:27:06 +020013204#ifdef FEAT_GUI
13205 static void
13206f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13207{
13208 char_u *which;
13209 long value;
13210 int dragging;
13211 scrollbar_T *sb = NULL;
13212
13213 if (argvars[0].v_type != VAR_STRING
13214 || (argvars[1].v_type) != VAR_NUMBER
13215 || (argvars[2].v_type) != VAR_NUMBER)
13216 {
13217 EMSG(_(e_invarg));
13218 return;
13219 }
13220 which = get_tv_string(&argvars[0]);
13221 value = get_tv_number(&argvars[1]);
13222 dragging = get_tv_number(&argvars[2]);
13223
13224 if (STRCMP(which, "left") == 0)
13225 sb = &curwin->w_scrollbars[SBAR_LEFT];
13226 else if (STRCMP(which, "right") == 0)
13227 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13228 else if (STRCMP(which, "hor") == 0)
13229 sb = &gui.bottom_sbar;
13230 if (sb == NULL)
13231 {
13232 EMSG2(_(e_invarg2), which);
13233 return;
13234 }
13235 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013236# ifndef USE_ON_FLY_SCROLL
13237 // need to loop through normal_cmd() to handle the scroll events
13238 exec_normal(FALSE, TRUE, FALSE);
13239# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013240}
13241#endif
13242
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013243 static void
13244f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13245{
13246 time_for_testing = (time_t)get_tv_number(&argvars[0]);
13247}
13248
13249#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13250/*
13251 * Get a callback from "arg". It can be a Funcref or a function name.
13252 * When "arg" is zero return an empty string.
13253 * Return NULL for an invalid argument.
13254 */
13255 char_u *
13256get_callback(typval_T *arg, partial_T **pp)
13257{
13258 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13259 {
13260 *pp = arg->vval.v_partial;
13261 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013262 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263 }
13264 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013265 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013266 {
13267 func_ref(arg->vval.v_string);
13268 return arg->vval.v_string;
13269 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13271 return (char_u *)"";
13272 EMSG(_("E921: Invalid callback argument"));
13273 return NULL;
13274}
13275
13276/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013277 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 */
13279 void
13280free_callback(char_u *callback, partial_T *partial)
13281{
13282 if (partial != NULL)
13283 partial_unref(partial);
13284 else if (callback != NULL)
13285 {
13286 func_unref(callback);
13287 vim_free(callback);
13288 }
13289}
13290#endif
13291
13292#ifdef FEAT_TIMERS
13293/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013294 * "timer_info([timer])" function
13295 */
13296 static void
13297f_timer_info(typval_T *argvars, typval_T *rettv)
13298{
13299 timer_T *timer = NULL;
13300
13301 if (rettv_list_alloc(rettv) != OK)
13302 return;
13303 if (argvars[0].v_type != VAR_UNKNOWN)
13304 {
13305 if (argvars[0].v_type != VAR_NUMBER)
13306 EMSG(_(e_number_exp));
13307 else
13308 {
13309 timer = find_timer((int)get_tv_number(&argvars[0]));
13310 if (timer != NULL)
13311 add_timer_info(rettv, timer);
13312 }
13313 }
13314 else
13315 add_timer_info_all(rettv);
13316}
13317
13318/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013319 * "timer_pause(timer, paused)" function
13320 */
13321 static void
13322f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
13323{
13324 timer_T *timer = NULL;
13325 int paused = (int)get_tv_number(&argvars[1]);
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 timer->tr_paused = paused;
13334 }
13335}
13336
13337/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 * "timer_start(time, callback [, options])" function
13339 */
13340 static void
13341f_timer_start(typval_T *argvars, typval_T *rettv)
13342{
Bram Moolenaar75537a92016-09-05 22:45:28 +020013343 long msec = (long)get_tv_number(&argvars[0]);
13344 timer_T *timer;
13345 int repeat = 0;
13346 char_u *callback;
13347 dict_T *dict;
13348 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013349
Bram Moolenaar75537a92016-09-05 22:45:28 +020013350 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 if (check_secure())
13352 return;
13353 if (argvars[2].v_type != VAR_UNKNOWN)
13354 {
13355 if (argvars[2].v_type != VAR_DICT
13356 || (dict = argvars[2].vval.v_dict) == NULL)
13357 {
13358 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
13359 return;
13360 }
13361 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
13362 repeat = get_dict_number(dict, (char_u *)"repeat");
13363 }
13364
Bram Moolenaar75537a92016-09-05 22:45:28 +020013365 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013366 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020013367 return;
13368
13369 timer = create_timer(msec, repeat);
13370 if (timer == NULL)
13371 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372 else
13373 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020013374 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020013375 timer->tr_callback = vim_strsave(callback);
13376 else
13377 /* pointer into the partial */
13378 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020013379 timer->tr_partial = partial;
13380 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 }
13382}
13383
13384/*
13385 * "timer_stop(timer)" function
13386 */
13387 static void
13388f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
13389{
13390 timer_T *timer;
13391
13392 if (argvars[0].v_type != VAR_NUMBER)
13393 {
13394 EMSG(_(e_number_exp));
13395 return;
13396 }
13397 timer = find_timer((int)get_tv_number(&argvars[0]));
13398 if (timer != NULL)
13399 stop_timer(timer);
13400}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020013401
13402/*
13403 * "timer_stopall()" function
13404 */
13405 static void
13406f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13407{
13408 stop_all_timers();
13409}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410#endif
13411
13412/*
13413 * "tolower(string)" function
13414 */
13415 static void
13416f_tolower(typval_T *argvars, typval_T *rettv)
13417{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 rettv->v_type = VAR_STRING;
Bram Moolenaarcc5b22b2017-01-26 22:51:56 +010013419 rettv->vval.v_string = strlow_save(get_tv_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420}
13421
13422/*
13423 * "toupper(string)" function
13424 */
13425 static void
13426f_toupper(typval_T *argvars, typval_T *rettv)
13427{
13428 rettv->v_type = VAR_STRING;
13429 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
13430}
13431
13432/*
13433 * "tr(string, fromstr, tostr)" function
13434 */
13435 static void
13436f_tr(typval_T *argvars, typval_T *rettv)
13437{
13438 char_u *in_str;
13439 char_u *fromstr;
13440 char_u *tostr;
13441 char_u *p;
13442#ifdef FEAT_MBYTE
13443 int inlen;
13444 int fromlen;
13445 int tolen;
13446 int idx;
13447 char_u *cpstr;
13448 int cplen;
13449 int first = TRUE;
13450#endif
13451 char_u buf[NUMBUFLEN];
13452 char_u buf2[NUMBUFLEN];
13453 garray_T ga;
13454
13455 in_str = get_tv_string(&argvars[0]);
13456 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
13457 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
13458
13459 /* Default return value: empty string. */
13460 rettv->v_type = VAR_STRING;
13461 rettv->vval.v_string = NULL;
13462 if (fromstr == NULL || tostr == NULL)
13463 return; /* type error; errmsg already given */
13464 ga_init2(&ga, (int)sizeof(char), 80);
13465
13466#ifdef FEAT_MBYTE
13467 if (!has_mbyte)
13468#endif
13469 /* not multi-byte: fromstr and tostr must be the same length */
13470 if (STRLEN(fromstr) != STRLEN(tostr))
13471 {
13472#ifdef FEAT_MBYTE
13473error:
13474#endif
13475 EMSG2(_(e_invarg2), fromstr);
13476 ga_clear(&ga);
13477 return;
13478 }
13479
13480 /* fromstr and tostr have to contain the same number of chars */
13481 while (*in_str != NUL)
13482 {
13483#ifdef FEAT_MBYTE
13484 if (has_mbyte)
13485 {
13486 inlen = (*mb_ptr2len)(in_str);
13487 cpstr = in_str;
13488 cplen = inlen;
13489 idx = 0;
13490 for (p = fromstr; *p != NUL; p += fromlen)
13491 {
13492 fromlen = (*mb_ptr2len)(p);
13493 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
13494 {
13495 for (p = tostr; *p != NUL; p += tolen)
13496 {
13497 tolen = (*mb_ptr2len)(p);
13498 if (idx-- == 0)
13499 {
13500 cplen = tolen;
13501 cpstr = p;
13502 break;
13503 }
13504 }
13505 if (*p == NUL) /* tostr is shorter than fromstr */
13506 goto error;
13507 break;
13508 }
13509 ++idx;
13510 }
13511
13512 if (first && cpstr == in_str)
13513 {
13514 /* Check that fromstr and tostr have the same number of
13515 * (multi-byte) characters. Done only once when a character
13516 * of in_str doesn't appear in fromstr. */
13517 first = FALSE;
13518 for (p = tostr; *p != NUL; p += tolen)
13519 {
13520 tolen = (*mb_ptr2len)(p);
13521 --idx;
13522 }
13523 if (idx != 0)
13524 goto error;
13525 }
13526
13527 (void)ga_grow(&ga, cplen);
13528 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
13529 ga.ga_len += cplen;
13530
13531 in_str += inlen;
13532 }
13533 else
13534#endif
13535 {
13536 /* When not using multi-byte chars we can do it faster. */
13537 p = vim_strchr(fromstr, *in_str);
13538 if (p != NULL)
13539 ga_append(&ga, tostr[p - fromstr]);
13540 else
13541 ga_append(&ga, *in_str);
13542 ++in_str;
13543 }
13544 }
13545
13546 /* add a terminating NUL */
13547 (void)ga_grow(&ga, 1);
13548 ga_append(&ga, NUL);
13549
13550 rettv->vval.v_string = ga.ga_data;
13551}
13552
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010013553/*
13554 * "trim({expr})" function
13555 */
13556 static void
13557f_trim(typval_T *argvars, typval_T *rettv)
13558{
13559 char_u buf1[NUMBUFLEN];
13560 char_u buf2[NUMBUFLEN];
13561 char_u *head = get_tv_string_buf_chk(&argvars[0], buf1);
13562 char_u *mask = NULL;
13563 char_u *tail;
13564 char_u *prev;
13565 char_u *p;
13566 int c1;
13567
13568 rettv->v_type = VAR_STRING;
13569 if (head == NULL)
13570 {
13571 rettv->vval.v_string = NULL;
13572 return;
13573 }
13574
13575 if (argvars[1].v_type == VAR_STRING)
13576 mask = get_tv_string_buf_chk(&argvars[1], buf2);
13577
13578 while (*head != NUL)
13579 {
13580 c1 = PTR2CHAR(head);
13581 if (mask == NULL)
13582 {
13583 if (c1 > ' ' && c1 != 0xa0)
13584 break;
13585 }
13586 else
13587 {
13588 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13589 if (c1 == PTR2CHAR(p))
13590 break;
13591 if (*p == NUL)
13592 break;
13593 }
13594 MB_PTR_ADV(head);
13595 }
13596
13597 for (tail = head + STRLEN(head); tail > head; tail = prev)
13598 {
13599 prev = tail;
13600 MB_PTR_BACK(head, prev);
13601 c1 = PTR2CHAR(prev);
13602 if (mask == NULL)
13603 {
13604 if (c1 > ' ' && c1 != 0xa0)
13605 break;
13606 }
13607 else
13608 {
13609 for (p = mask; *p != NUL; MB_PTR_ADV(p))
13610 if (c1 == PTR2CHAR(p))
13611 break;
13612 if (*p == NUL)
13613 break;
13614 }
13615 }
13616 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
13617}
13618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619#ifdef FEAT_FLOAT
13620/*
13621 * "trunc({float})" function
13622 */
13623 static void
13624f_trunc(typval_T *argvars, typval_T *rettv)
13625{
13626 float_T f = 0.0;
13627
13628 rettv->v_type = VAR_FLOAT;
13629 if (get_float_arg(argvars, &f) == OK)
13630 /* trunc() is not in C90, use floor() or ceil() instead. */
13631 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
13632 else
13633 rettv->vval.v_float = 0.0;
13634}
13635#endif
13636
13637/*
13638 * "type(expr)" function
13639 */
13640 static void
13641f_type(typval_T *argvars, typval_T *rettv)
13642{
13643 int n = -1;
13644
13645 switch (argvars[0].v_type)
13646 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020013647 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
13648 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013649 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020013650 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
13651 case VAR_LIST: n = VAR_TYPE_LIST; break;
13652 case VAR_DICT: n = VAR_TYPE_DICT; break;
13653 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013654 case VAR_SPECIAL:
13655 if (argvars[0].vval.v_number == VVAL_FALSE
13656 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020013657 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020013659 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020013661 case VAR_JOB: n = VAR_TYPE_JOB; break;
13662 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010013664 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665 n = -1;
13666 break;
13667 }
13668 rettv->vval.v_number = n;
13669}
13670
13671/*
13672 * "undofile(name)" function
13673 */
13674 static void
13675f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
13676{
13677 rettv->v_type = VAR_STRING;
13678#ifdef FEAT_PERSISTENT_UNDO
13679 {
13680 char_u *fname = get_tv_string(&argvars[0]);
13681
13682 if (*fname == NUL)
13683 {
13684 /* If there is no file name there will be no undo file. */
13685 rettv->vval.v_string = NULL;
13686 }
13687 else
13688 {
13689 char_u *ffname = FullName_save(fname, FALSE);
13690
13691 if (ffname != NULL)
13692 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
13693 vim_free(ffname);
13694 }
13695 }
13696#else
13697 rettv->vval.v_string = NULL;
13698#endif
13699}
13700
13701/*
13702 * "undotree()" function
13703 */
13704 static void
13705f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
13706{
13707 if (rettv_dict_alloc(rettv) == OK)
13708 {
13709 dict_T *dict = rettv->vval.v_dict;
13710 list_T *list;
13711
Bram Moolenaare0be1672018-07-08 16:50:37 +020013712 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
13713 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
13714 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
13715 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
13716 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
13717 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013718
13719 list = list_alloc();
13720 if (list != NULL)
13721 {
13722 u_eval_tree(curbuf->b_u_oldhead, list);
13723 dict_add_list(dict, "entries", list);
13724 }
13725 }
13726}
13727
13728/*
13729 * "values(dict)" function
13730 */
13731 static void
13732f_values(typval_T *argvars, typval_T *rettv)
13733{
13734 dict_list(argvars, rettv, 1);
13735}
13736
13737/*
13738 * "virtcol(string)" function
13739 */
13740 static void
13741f_virtcol(typval_T *argvars, typval_T *rettv)
13742{
13743 colnr_T vcol = 0;
13744 pos_T *fp;
13745 int fnum = curbuf->b_fnum;
13746
13747 fp = var2fpos(&argvars[0], FALSE, &fnum);
13748 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
13749 && fnum == curbuf->b_fnum)
13750 {
13751 getvvcol(curwin, fp, NULL, NULL, &vcol);
13752 ++vcol;
13753 }
13754
13755 rettv->vval.v_number = vcol;
13756}
13757
13758/*
13759 * "visualmode()" function
13760 */
13761 static void
13762f_visualmode(typval_T *argvars, typval_T *rettv)
13763{
13764 char_u str[2];
13765
13766 rettv->v_type = VAR_STRING;
13767 str[0] = curbuf->b_visual_mode_eval;
13768 str[1] = NUL;
13769 rettv->vval.v_string = vim_strsave(str);
13770
13771 /* A non-zero number or non-empty string argument: reset mode. */
13772 if (non_zero_arg(&argvars[0]))
13773 curbuf->b_visual_mode_eval = NUL;
13774}
13775
13776/*
13777 * "wildmenumode()" function
13778 */
13779 static void
13780f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13781{
13782#ifdef FEAT_WILDMENU
13783 if (wild_menu_showing)
13784 rettv->vval.v_number = 1;
13785#endif
13786}
13787
13788/*
13789 * "winbufnr(nr)" function
13790 */
13791 static void
13792f_winbufnr(typval_T *argvars, typval_T *rettv)
13793{
13794 win_T *wp;
13795
13796 wp = find_win_by_nr(&argvars[0], NULL);
13797 if (wp == NULL)
13798 rettv->vval.v_number = -1;
13799 else
13800 rettv->vval.v_number = wp->w_buffer->b_fnum;
13801}
13802
13803/*
13804 * "wincol()" function
13805 */
13806 static void
13807f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
13808{
13809 validate_cursor();
13810 rettv->vval.v_number = curwin->w_wcol + 1;
13811}
13812
13813/*
13814 * "winheight(nr)" function
13815 */
13816 static void
13817f_winheight(typval_T *argvars, typval_T *rettv)
13818{
13819 win_T *wp;
13820
13821 wp = find_win_by_nr(&argvars[0], NULL);
13822 if (wp == NULL)
13823 rettv->vval.v_number = -1;
13824 else
13825 rettv->vval.v_number = wp->w_height;
13826}
13827
13828/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020013829 * "winlayout()" function
13830 */
13831 static void
13832f_winlayout(typval_T *argvars, typval_T *rettv)
13833{
13834 tabpage_T *tp;
13835
13836 if (rettv_list_alloc(rettv) != OK)
13837 return;
13838
13839 if (argvars[0].v_type == VAR_UNKNOWN)
13840 tp = curtab;
13841 else
13842 {
13843 tp = find_tabpage((int)get_tv_number(&argvars[0]));
13844 if (tp == NULL)
13845 return;
13846 }
13847
13848 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
13849}
13850
13851/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013852 * "winline()" function
13853 */
13854 static void
13855f_winline(typval_T *argvars UNUSED, typval_T *rettv)
13856{
13857 validate_cursor();
13858 rettv->vval.v_number = curwin->w_wrow + 1;
13859}
13860
13861/*
13862 * "winnr()" function
13863 */
13864 static void
13865f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
13866{
13867 int nr = 1;
13868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013870 rettv->vval.v_number = nr;
13871}
13872
13873/*
13874 * "winrestcmd()" function
13875 */
13876 static void
13877f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
13878{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013879 win_T *wp;
13880 int winnr = 1;
13881 garray_T ga;
13882 char_u buf[50];
13883
13884 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020013885 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013886 {
13887 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13888 ga_concat(&ga, buf);
13889 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13890 ga_concat(&ga, buf);
13891 ++winnr;
13892 }
13893 ga_append(&ga, NUL);
13894
13895 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896 rettv->v_type = VAR_STRING;
13897}
13898
13899/*
13900 * "winrestview()" function
13901 */
13902 static void
13903f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
13904{
13905 dict_T *dict;
13906
13907 if (argvars[0].v_type != VAR_DICT
13908 || (dict = argvars[0].vval.v_dict) == NULL)
13909 EMSG(_(e_invarg));
13910 else
13911 {
13912 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
13913 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
13914 if (dict_find(dict, (char_u *)"col", -1) != NULL)
13915 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
13916#ifdef FEAT_VIRTUALEDIT
13917 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
13918 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
13919#endif
13920 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
13921 {
13922 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
13923 curwin->w_set_curswant = FALSE;
13924 }
13925
13926 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
13927 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
13928#ifdef FEAT_DIFF
13929 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
13930 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
13931#endif
13932 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
13933 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
13934 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
13935 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
13936
13937 check_cursor();
13938 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020013939 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940 changed_window_setting();
13941
13942 if (curwin->w_topline <= 0)
13943 curwin->w_topline = 1;
13944 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
13945 curwin->w_topline = curbuf->b_ml.ml_line_count;
13946#ifdef FEAT_DIFF
13947 check_topfill(curwin, TRUE);
13948#endif
13949 }
13950}
13951
13952/*
13953 * "winsaveview()" function
13954 */
13955 static void
13956f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
13957{
13958 dict_T *dict;
13959
13960 if (rettv_dict_alloc(rettv) == FAIL)
13961 return;
13962 dict = rettv->vval.v_dict;
13963
Bram Moolenaare0be1672018-07-08 16:50:37 +020013964 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
13965 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020013967 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968#endif
13969 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020013970 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971
Bram Moolenaare0be1672018-07-08 16:50:37 +020013972 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013973#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020013974 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013975#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020013976 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
13977 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978}
13979
13980/*
13981 * "winwidth(nr)" function
13982 */
13983 static void
13984f_winwidth(typval_T *argvars, typval_T *rettv)
13985{
13986 win_T *wp;
13987
13988 wp = find_win_by_nr(&argvars[0], NULL);
13989 if (wp == NULL)
13990 rettv->vval.v_number = -1;
13991 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013992 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993}
13994
13995/*
13996 * "wordcount()" function
13997 */
13998 static void
13999f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14000{
14001 if (rettv_dict_alloc(rettv) == FAIL)
14002 return;
14003 cursor_pos_info(rettv->vval.v_dict);
14004}
14005
14006/*
14007 * "writefile()" function
14008 */
14009 static void
14010f_writefile(typval_T *argvars, typval_T *rettv)
14011{
14012 int binary = FALSE;
14013 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014014#ifdef HAVE_FSYNC
14015 int do_fsync = p_fs;
14016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014017 char_u *fname;
14018 FILE *fd;
14019 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014020 listitem_T *li;
14021 list_T *list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014023 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014024 if (check_restricted() || check_secure())
14025 return;
14026
14027 if (argvars[0].v_type != VAR_LIST)
14028 {
14029 EMSG2(_(e_listarg), "writefile()");
14030 return;
14031 }
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014032 list = argvars[0].vval.v_list;
14033 if (list == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 return;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014035 for (li = list->lv_first; li != NULL; li = li->li_next)
14036 if (get_tv_string_chk(&li->li_tv) == NULL)
14037 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014038
14039 if (argvars[2].v_type != VAR_UNKNOWN)
14040 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014041 char_u *arg2 = get_tv_string_chk(&argvars[2]);
14042
14043 if (arg2 == NULL)
14044 return;
14045 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014046 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014047 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014048 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014049#ifdef HAVE_FSYNC
14050 if (vim_strchr(arg2, 's') != NULL)
14051 do_fsync = TRUE;
14052 else if (vim_strchr(arg2, 'S') != NULL)
14053 do_fsync = FALSE;
14054#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 }
14056
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014057 fname = get_tv_string_chk(&argvars[1]);
14058 if (fname == NULL)
14059 return;
14060
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014061 /* Always open the file in binary mode, library functions have a mind of
14062 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014063 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14064 append ? APPENDBIN : WRITEBIN)) == NULL)
14065 {
14066 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14067 ret = -1;
14068 }
14069 else
14070 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014071 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014072 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014073#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014074 else if (do_fsync)
14075 /* Ignore the error, the user wouldn't know what to do about it.
14076 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014077 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014078#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014079 fclose(fd);
14080 }
14081
14082 rettv->vval.v_number = ret;
14083}
14084
14085/*
14086 * "xor(expr, expr)" function
14087 */
14088 static void
14089f_xor(typval_T *argvars, typval_T *rettv)
14090{
14091 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14092 ^ get_tv_number_chk(&argvars[1], NULL);
14093}
14094
14095
14096#endif /* FEAT_EVAL */