blob: 5bba7eaf067c50b9618520bd6fc8acbf90e391d6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020034
35#ifdef FEAT_FLOAT
36static void f_abs(typval_T *argvars, typval_T *rettv);
37static void f_acos(typval_T *argvars, typval_T *rettv);
38#endif
39static void f_add(typval_T *argvars, typval_T *rettv);
40static void f_and(typval_T *argvars, typval_T *rettv);
41static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020042static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100100static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static 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 Moolenaarf49cc602018-11-11 15:21:05 +0100204static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200205static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100206static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_getwinposx(typval_T *argvars, typval_T *rettv);
208static void f_getwinposy(typval_T *argvars, typval_T *rettv);
209static void f_getwinvar(typval_T *argvars, typval_T *rettv);
210static void f_glob(typval_T *argvars, typval_T *rettv);
211static void f_globpath(typval_T *argvars, typval_T *rettv);
212static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
213static void f_has(typval_T *argvars, typval_T *rettv);
214static void f_has_key(typval_T *argvars, typval_T *rettv);
215static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
216static void f_hasmapto(typval_T *argvars, typval_T *rettv);
217static void f_histadd(typval_T *argvars, typval_T *rettv);
218static void f_histdel(typval_T *argvars, typval_T *rettv);
219static void f_histget(typval_T *argvars, typval_T *rettv);
220static void f_histnr(typval_T *argvars, typval_T *rettv);
221static void f_hlID(typval_T *argvars, typval_T *rettv);
222static void f_hlexists(typval_T *argvars, typval_T *rettv);
223static void f_hostname(typval_T *argvars, typval_T *rettv);
224static void f_iconv(typval_T *argvars, typval_T *rettv);
225static void f_indent(typval_T *argvars, typval_T *rettv);
226static void f_index(typval_T *argvars, typval_T *rettv);
227static void f_input(typval_T *argvars, typval_T *rettv);
228static void f_inputdialog(typval_T *argvars, typval_T *rettv);
229static void f_inputlist(typval_T *argvars, typval_T *rettv);
230static void f_inputrestore(typval_T *argvars, typval_T *rettv);
231static void f_inputsave(typval_T *argvars, typval_T *rettv);
232static void f_inputsecret(typval_T *argvars, typval_T *rettv);
233static void f_insert(typval_T *argvars, typval_T *rettv);
234static void f_invert(typval_T *argvars, typval_T *rettv);
235static void f_isdirectory(typval_T *argvars, typval_T *rettv);
236static void f_islocked(typval_T *argvars, typval_T *rettv);
237#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
238static void f_isnan(typval_T *argvars, typval_T *rettv);
239#endif
240static void f_items(typval_T *argvars, typval_T *rettv);
241#ifdef FEAT_JOB_CHANNEL
242static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
243static void f_job_info(typval_T *argvars, typval_T *rettv);
244static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
245static void f_job_start(typval_T *argvars, typval_T *rettv);
246static void f_job_stop(typval_T *argvars, typval_T *rettv);
247static void f_job_status(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_join(typval_T *argvars, typval_T *rettv);
250static void f_js_decode(typval_T *argvars, typval_T *rettv);
251static void f_js_encode(typval_T *argvars, typval_T *rettv);
252static void f_json_decode(typval_T *argvars, typval_T *rettv);
253static void f_json_encode(typval_T *argvars, typval_T *rettv);
254static void f_keys(typval_T *argvars, typval_T *rettv);
255static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
256static void f_len(typval_T *argvars, typval_T *rettv);
257static void f_libcall(typval_T *argvars, typval_T *rettv);
258static void f_libcallnr(typval_T *argvars, typval_T *rettv);
259static void f_line(typval_T *argvars, typval_T *rettv);
260static void f_line2byte(typval_T *argvars, typval_T *rettv);
261static void f_lispindent(typval_T *argvars, typval_T *rettv);
262static void f_localtime(typval_T *argvars, typval_T *rettv);
263#ifdef FEAT_FLOAT
264static void f_log(typval_T *argvars, typval_T *rettv);
265static void f_log10(typval_T *argvars, typval_T *rettv);
266#endif
267#ifdef FEAT_LUA
268static void f_luaeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_map(typval_T *argvars, typval_T *rettv);
271static void f_maparg(typval_T *argvars, typval_T *rettv);
272static void f_mapcheck(typval_T *argvars, typval_T *rettv);
273static void f_match(typval_T *argvars, typval_T *rettv);
274static void f_matchadd(typval_T *argvars, typval_T *rettv);
275static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
276static void f_matcharg(typval_T *argvars, typval_T *rettv);
277static void f_matchdelete(typval_T *argvars, typval_T *rettv);
278static void f_matchend(typval_T *argvars, typval_T *rettv);
279static void f_matchlist(typval_T *argvars, typval_T *rettv);
280static void f_matchstr(typval_T *argvars, typval_T *rettv);
281static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
282static void f_max(typval_T *argvars, typval_T *rettv);
283static void f_min(typval_T *argvars, typval_T *rettv);
284#ifdef vim_mkdir
285static void f_mkdir(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_mode(typval_T *argvars, typval_T *rettv);
288#ifdef FEAT_MZSCHEME
289static void f_mzeval(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
292static void f_nr2char(typval_T *argvars, typval_T *rettv);
293static void f_or(typval_T *argvars, typval_T *rettv);
294static void f_pathshorten(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_PERL
296static void f_perleval(typval_T *argvars, typval_T *rettv);
297#endif
298#ifdef FEAT_FLOAT
299static void f_pow(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
302static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200303#ifdef FEAT_JOB_CHANNEL
304static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200305static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200306static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_pumvisible(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_PYTHON3
310static void f_py3eval(typval_T *argvars, typval_T *rettv);
311#endif
312#ifdef FEAT_PYTHON
313static void f_pyeval(typval_T *argvars, typval_T *rettv);
314#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100315#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
316static void f_pyxeval(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_range(typval_T *argvars, typval_T *rettv);
319static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200320static void f_reg_executing(typval_T *argvars, typval_T *rettv);
321static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_reltime(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_FLOAT
324static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_reltimestr(typval_T *argvars, typval_T *rettv);
327static void f_remote_expr(typval_T *argvars, typval_T *rettv);
328static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
329static void f_remote_peek(typval_T *argvars, typval_T *rettv);
330static void f_remote_read(typval_T *argvars, typval_T *rettv);
331static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100332static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_remove(typval_T *argvars, typval_T *rettv);
334static void f_rename(typval_T *argvars, typval_T *rettv);
335static void f_repeat(typval_T *argvars, typval_T *rettv);
336static void f_resolve(typval_T *argvars, typval_T *rettv);
337static void f_reverse(typval_T *argvars, typval_T *rettv);
338#ifdef FEAT_FLOAT
339static void f_round(typval_T *argvars, typval_T *rettv);
340#endif
341static void f_screenattr(typval_T *argvars, typval_T *rettv);
342static void f_screenchar(typval_T *argvars, typval_T *rettv);
343static void f_screencol(typval_T *argvars, typval_T *rettv);
344static void f_screenrow(typval_T *argvars, typval_T *rettv);
345static void f_search(typval_T *argvars, typval_T *rettv);
346static void f_searchdecl(typval_T *argvars, typval_T *rettv);
347static void f_searchpair(typval_T *argvars, typval_T *rettv);
348static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
349static void f_searchpos(typval_T *argvars, typval_T *rettv);
350static void f_server2client(typval_T *argvars, typval_T *rettv);
351static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200352static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_setbufvar(typval_T *argvars, typval_T *rettv);
354static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
355static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
356static void f_setfperm(typval_T *argvars, typval_T *rettv);
357static void f_setline(typval_T *argvars, typval_T *rettv);
358static void f_setloclist(typval_T *argvars, typval_T *rettv);
359static void f_setmatches(typval_T *argvars, typval_T *rettv);
360static void f_setpos(typval_T *argvars, typval_T *rettv);
361static void f_setqflist(typval_T *argvars, typval_T *rettv);
362static void f_setreg(typval_T *argvars, typval_T *rettv);
363static void f_settabvar(typval_T *argvars, typval_T *rettv);
364static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100365static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366static void f_setwinvar(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_CRYPT
368static void f_sha256(typval_T *argvars, typval_T *rettv);
369#endif /* FEAT_CRYPT */
370static void f_shellescape(typval_T *argvars, typval_T *rettv);
371static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100372#ifdef FEAT_SIGNS
373static void f_sign_define(typval_T *argvars, typval_T *rettv);
374static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
375static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100376static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100377static void f_sign_place(typval_T *argvars, typval_T *rettv);
378static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
379static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
380#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_simplify(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_FLOAT
383static void f_sin(typval_T *argvars, typval_T *rettv);
384static void f_sinh(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_sort(typval_T *argvars, typval_T *rettv);
387static void f_soundfold(typval_T *argvars, typval_T *rettv);
388static void f_spellbadword(typval_T *argvars, typval_T *rettv);
389static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
390static void f_split(typval_T *argvars, typval_T *rettv);
391#ifdef FEAT_FLOAT
392static void f_sqrt(typval_T *argvars, typval_T *rettv);
393static void f_str2float(typval_T *argvars, typval_T *rettv);
394#endif
395static void f_str2nr(typval_T *argvars, typval_T *rettv);
396static void f_strchars(typval_T *argvars, typval_T *rettv);
397#ifdef HAVE_STRFTIME
398static void f_strftime(typval_T *argvars, typval_T *rettv);
399#endif
400static void f_strgetchar(typval_T *argvars, typval_T *rettv);
401static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_strlen(typval_T *argvars, typval_T *rettv);
403static void f_strcharpart(typval_T *argvars, typval_T *rettv);
404static void f_strpart(typval_T *argvars, typval_T *rettv);
405static void f_strridx(typval_T *argvars, typval_T *rettv);
406static void f_strtrans(typval_T *argvars, typval_T *rettv);
407static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
408static void f_strwidth(typval_T *argvars, typval_T *rettv);
409static void f_submatch(typval_T *argvars, typval_T *rettv);
410static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200411static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200412static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_synID(typval_T *argvars, typval_T *rettv);
414static void f_synIDattr(typval_T *argvars, typval_T *rettv);
415static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
416static void f_synstack(typval_T *argvars, typval_T *rettv);
417static void f_synconcealed(typval_T *argvars, typval_T *rettv);
418static void f_system(typval_T *argvars, typval_T *rettv);
419static void f_systemlist(typval_T *argvars, typval_T *rettv);
420static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
421static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
422static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
423static void f_taglist(typval_T *argvars, typval_T *rettv);
424static void f_tagfiles(typval_T *argvars, typval_T *rettv);
425static void f_tempname(typval_T *argvars, typval_T *rettv);
426static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
427static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200428static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200429static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100430static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100432static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100433static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
438#ifdef FEAT_JOB_CHANNEL
439static void f_test_null_job(typval_T *argvars, typval_T *rettv);
440#endif
441static void f_test_null_list(typval_T *argvars, typval_T *rettv);
442static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
443static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200444#ifdef FEAT_GUI
445static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
446#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447static void f_test_settime(typval_T *argvars, typval_T *rettv);
448#ifdef FEAT_FLOAT
449static void f_tan(typval_T *argvars, typval_T *rettv);
450static void f_tanh(typval_T *argvars, typval_T *rettv);
451#endif
452#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200453static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200454static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200455static void f_timer_start(typval_T *argvars, typval_T *rettv);
456static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200457static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200458#endif
459static void f_tolower(typval_T *argvars, typval_T *rettv);
460static void f_toupper(typval_T *argvars, typval_T *rettv);
461static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100462static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463#ifdef FEAT_FLOAT
464static void f_trunc(typval_T *argvars, typval_T *rettv);
465#endif
466static void f_type(typval_T *argvars, typval_T *rettv);
467static void f_undofile(typval_T *argvars, typval_T *rettv);
468static void f_undotree(typval_T *argvars, typval_T *rettv);
469static void f_uniq(typval_T *argvars, typval_T *rettv);
470static void f_values(typval_T *argvars, typval_T *rettv);
471static void f_virtcol(typval_T *argvars, typval_T *rettv);
472static void f_visualmode(typval_T *argvars, typval_T *rettv);
473static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
474static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
475static void f_win_getid(typval_T *argvars, typval_T *rettv);
476static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
477static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
478static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100479static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winbufnr(typval_T *argvars, typval_T *rettv);
481static void f_wincol(typval_T *argvars, typval_T *rettv);
482static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200483static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484static void f_winline(typval_T *argvars, typval_T *rettv);
485static void f_winnr(typval_T *argvars, typval_T *rettv);
486static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
487static void f_winrestview(typval_T *argvars, typval_T *rettv);
488static void f_winsaveview(typval_T *argvars, typval_T *rettv);
489static void f_winwidth(typval_T *argvars, typval_T *rettv);
490static void f_writefile(typval_T *argvars, typval_T *rettv);
491static void f_wordcount(typval_T *argvars, typval_T *rettv);
492static void f_xor(typval_T *argvars, typval_T *rettv);
493
494/*
495 * Array with names and number of arguments of all internal functions
496 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
497 */
498static struct fst
499{
500 char *f_name; /* function name */
501 char f_min_argc; /* minimal number of arguments */
502 char f_max_argc; /* maximal number of arguments */
503 void (*f_func)(typval_T *args, typval_T *rvar);
504 /* implementation of function */
505} functions[] =
506{
507#ifdef FEAT_FLOAT
508 {"abs", 1, 1, f_abs},
509 {"acos", 1, 1, f_acos}, /* WJMc */
510#endif
511 {"add", 2, 2, f_add},
512 {"and", 2, 2, f_and},
513 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200514 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200515 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"argidx", 0, 0, f_argidx},
517 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200518 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519#ifdef FEAT_FLOAT
520 {"asin", 1, 1, f_asin}, /* WJMc */
521#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100522 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100524 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200526 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100528 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_match", 2, 3, f_assert_match},
530 {"assert_notequal", 2, 3, f_assert_notequal},
531 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100532 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"assert_true", 1, 2, f_assert_true},
534#ifdef FEAT_FLOAT
535 {"atan", 1, 1, f_atan},
536 {"atan2", 2, 2, f_atan2},
537#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100538#ifdef FEAT_BEVAL
539 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100540# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100541 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100542# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"browse", 4, 4, f_browse},
545 {"browsedir", 2, 2, f_browsedir},
546 {"bufexists", 1, 1, f_bufexists},
547 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
548 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
549 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
550 {"buflisted", 1, 1, f_buflisted},
551 {"bufloaded", 1, 1, f_bufloaded},
552 {"bufname", 1, 1, f_bufname},
553 {"bufnr", 1, 2, f_bufnr},
554 {"bufwinid", 1, 1, f_bufwinid},
555 {"bufwinnr", 1, 1, f_bufwinnr},
556 {"byte2line", 1, 1, f_byte2line},
557 {"byteidx", 2, 2, f_byteidx},
558 {"byteidxcomp", 2, 2, f_byteidxcomp},
559 {"call", 2, 3, f_call},
560#ifdef FEAT_FLOAT
561 {"ceil", 1, 1, f_ceil},
562#endif
563#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100564 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200566 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
568 {"ch_evalraw", 2, 3, f_ch_evalraw},
569 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
570 {"ch_getjob", 1, 1, f_ch_getjob},
571 {"ch_info", 1, 1, f_ch_info},
572 {"ch_log", 1, 2, f_ch_log},
573 {"ch_logfile", 1, 2, f_ch_logfile},
574 {"ch_open", 1, 2, f_ch_open},
575 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100576 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_readraw", 1, 2, f_ch_readraw},
578 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
579 {"ch_sendraw", 2, 3, f_ch_sendraw},
580 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200581 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582#endif
583 {"changenr", 0, 0, f_changenr},
584 {"char2nr", 1, 2, f_char2nr},
585 {"cindent", 1, 1, f_cindent},
586 {"clearmatches", 0, 0, f_clearmatches},
587 {"col", 1, 1, f_col},
588#if defined(FEAT_INS_EXPAND)
589 {"complete", 2, 2, f_complete},
590 {"complete_add", 1, 1, f_complete_add},
591 {"complete_check", 0, 0, f_complete_check},
592#endif
593 {"confirm", 1, 4, f_confirm},
594 {"copy", 1, 1, f_copy},
595#ifdef FEAT_FLOAT
596 {"cos", 1, 1, f_cos},
597 {"cosh", 1, 1, f_cosh},
598#endif
599 {"count", 2, 4, f_count},
600 {"cscope_connection",0,3, f_cscope_connection},
601 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200602#ifdef WIN3264
603 {"debugbreak", 1, 1, f_debugbreak},
604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"deepcopy", 1, 2, f_deepcopy},
606 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200607 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"did_filetype", 0, 0, f_did_filetype},
609 {"diff_filler", 1, 1, f_diff_filler},
610 {"diff_hlID", 2, 2, f_diff_hlID},
611 {"empty", 1, 1, f_empty},
612 {"escape", 2, 2, f_escape},
613 {"eval", 1, 1, f_eval},
614 {"eventhandler", 0, 0, f_eventhandler},
615 {"executable", 1, 1, f_executable},
616 {"execute", 1, 2, f_execute},
617 {"exepath", 1, 1, f_exepath},
618 {"exists", 1, 1, f_exists},
619#ifdef FEAT_FLOAT
620 {"exp", 1, 1, f_exp},
621#endif
622 {"expand", 1, 3, f_expand},
623 {"extend", 2, 3, f_extend},
624 {"feedkeys", 1, 2, f_feedkeys},
625 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
626 {"filereadable", 1, 1, f_filereadable},
627 {"filewritable", 1, 1, f_filewritable},
628 {"filter", 2, 2, f_filter},
629 {"finddir", 1, 3, f_finddir},
630 {"findfile", 1, 3, f_findfile},
631#ifdef FEAT_FLOAT
632 {"float2nr", 1, 1, f_float2nr},
633 {"floor", 1, 1, f_floor},
634 {"fmod", 2, 2, f_fmod},
635#endif
636 {"fnameescape", 1, 1, f_fnameescape},
637 {"fnamemodify", 2, 2, f_fnamemodify},
638 {"foldclosed", 1, 1, f_foldclosed},
639 {"foldclosedend", 1, 1, f_foldclosedend},
640 {"foldlevel", 1, 1, f_foldlevel},
641 {"foldtext", 0, 0, f_foldtext},
642 {"foldtextresult", 1, 1, f_foldtextresult},
643 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200644 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645 {"function", 1, 3, f_function},
646 {"garbagecollect", 0, 1, f_garbagecollect},
647 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200648 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 {"getbufline", 2, 3, f_getbufline},
650 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100651 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getchar", 0, 1, f_getchar},
653 {"getcharmod", 0, 0, f_getcharmod},
654 {"getcharsearch", 0, 0, f_getcharsearch},
655 {"getcmdline", 0, 0, f_getcmdline},
656 {"getcmdpos", 0, 0, f_getcmdpos},
657 {"getcmdtype", 0, 0, f_getcmdtype},
658 {"getcmdwintype", 0, 0, f_getcmdwintype},
659#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200660 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661#endif
662 {"getcurpos", 0, 0, f_getcurpos},
663 {"getcwd", 0, 2, f_getcwd},
664 {"getfontname", 0, 1, f_getfontname},
665 {"getfperm", 1, 1, f_getfperm},
666 {"getfsize", 1, 1, f_getfsize},
667 {"getftime", 1, 1, f_getftime},
668 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100669 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200671 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getmatches", 0, 0, f_getmatches},
673 {"getpid", 0, 0, f_getpid},
674 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200675 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200676 {"getreg", 0, 3, f_getreg},
677 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200678 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"gettabvar", 2, 3, f_gettabvar},
680 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100681 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200682 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100683 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200684 {"getwinposx", 0, 0, f_getwinposx},
685 {"getwinposy", 0, 0, f_getwinposy},
686 {"getwinvar", 2, 3, f_getwinvar},
687 {"glob", 1, 4, f_glob},
688 {"glob2regpat", 1, 1, f_glob2regpat},
689 {"globpath", 2, 5, f_globpath},
690 {"has", 1, 1, f_has},
691 {"has_key", 2, 2, f_has_key},
692 {"haslocaldir", 0, 2, f_haslocaldir},
693 {"hasmapto", 1, 3, f_hasmapto},
694 {"highlightID", 1, 1, f_hlID}, /* obsolete */
695 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
696 {"histadd", 2, 2, f_histadd},
697 {"histdel", 1, 2, f_histdel},
698 {"histget", 1, 2, f_histget},
699 {"histnr", 1, 1, f_histnr},
700 {"hlID", 1, 1, f_hlID},
701 {"hlexists", 1, 1, f_hlexists},
702 {"hostname", 0, 0, f_hostname},
703 {"iconv", 3, 3, f_iconv},
704 {"indent", 1, 1, f_indent},
705 {"index", 2, 4, f_index},
706 {"input", 1, 3, f_input},
707 {"inputdialog", 1, 3, f_inputdialog},
708 {"inputlist", 1, 1, f_inputlist},
709 {"inputrestore", 0, 0, f_inputrestore},
710 {"inputsave", 0, 0, f_inputsave},
711 {"inputsecret", 1, 2, f_inputsecret},
712 {"insert", 2, 3, f_insert},
713 {"invert", 1, 1, f_invert},
714 {"isdirectory", 1, 1, f_isdirectory},
715 {"islocked", 1, 1, f_islocked},
716#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
717 {"isnan", 1, 1, f_isnan},
718#endif
719 {"items", 1, 1, f_items},
720#ifdef FEAT_JOB_CHANNEL
721 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200722 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200723 {"job_setoptions", 2, 2, f_job_setoptions},
724 {"job_start", 1, 2, f_job_start},
725 {"job_status", 1, 1, f_job_status},
726 {"job_stop", 1, 2, f_job_stop},
727#endif
728 {"join", 1, 2, f_join},
729 {"js_decode", 1, 1, f_js_decode},
730 {"js_encode", 1, 1, f_js_encode},
731 {"json_decode", 1, 1, f_json_decode},
732 {"json_encode", 1, 1, f_json_encode},
733 {"keys", 1, 1, f_keys},
734 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
735 {"len", 1, 1, f_len},
736 {"libcall", 3, 3, f_libcall},
737 {"libcallnr", 3, 3, f_libcallnr},
738 {"line", 1, 1, f_line},
739 {"line2byte", 1, 1, f_line2byte},
740 {"lispindent", 1, 1, f_lispindent},
741 {"localtime", 0, 0, f_localtime},
742#ifdef FEAT_FLOAT
743 {"log", 1, 1, f_log},
744 {"log10", 1, 1, f_log10},
745#endif
746#ifdef FEAT_LUA
747 {"luaeval", 1, 2, f_luaeval},
748#endif
749 {"map", 2, 2, f_map},
750 {"maparg", 1, 4, f_maparg},
751 {"mapcheck", 1, 3, f_mapcheck},
752 {"match", 2, 4, f_match},
753 {"matchadd", 2, 5, f_matchadd},
754 {"matchaddpos", 2, 5, f_matchaddpos},
755 {"matcharg", 1, 1, f_matcharg},
756 {"matchdelete", 1, 1, f_matchdelete},
757 {"matchend", 2, 4, f_matchend},
758 {"matchlist", 2, 4, f_matchlist},
759 {"matchstr", 2, 4, f_matchstr},
760 {"matchstrpos", 2, 4, f_matchstrpos},
761 {"max", 1, 1, f_max},
762 {"min", 1, 1, f_min},
763#ifdef vim_mkdir
764 {"mkdir", 1, 3, f_mkdir},
765#endif
766 {"mode", 0, 1, f_mode},
767#ifdef FEAT_MZSCHEME
768 {"mzeval", 1, 1, f_mzeval},
769#endif
770 {"nextnonblank", 1, 1, f_nextnonblank},
771 {"nr2char", 1, 2, f_nr2char},
772 {"or", 2, 2, f_or},
773 {"pathshorten", 1, 1, f_pathshorten},
774#ifdef FEAT_PERL
775 {"perleval", 1, 1, f_perleval},
776#endif
777#ifdef FEAT_FLOAT
778 {"pow", 2, 2, f_pow},
779#endif
780 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100781 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200782#ifdef FEAT_JOB_CHANNEL
783 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200784 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200785 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
786#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100787#ifdef FEAT_TEXT_PROP
788 {"prop_add", 3, 3, f_prop_add},
789 {"prop_clear", 1, 3, f_prop_clear},
790 {"prop_list", 1, 2, f_prop_list},
791 {"prop_remove", 2, 3, f_prop_remove},
792 {"prop_type_add", 2, 2, f_prop_type_add},
793 {"prop_type_change", 2, 2, f_prop_type_change},
794 {"prop_type_delete", 1, 2, f_prop_type_delete},
795 {"prop_type_get", 1, 2, f_prop_type_get},
796 {"prop_type_list", 0, 1, f_prop_type_list},
797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"pumvisible", 0, 0, f_pumvisible},
799#ifdef FEAT_PYTHON3
800 {"py3eval", 1, 1, f_py3eval},
801#endif
802#ifdef FEAT_PYTHON
803 {"pyeval", 1, 1, f_pyeval},
804#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100805#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
806 {"pyxeval", 1, 1, f_pyxeval},
807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808 {"range", 1, 3, f_range},
809 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200810 {"reg_executing", 0, 0, f_reg_executing},
811 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"reltime", 0, 2, f_reltime},
813#ifdef FEAT_FLOAT
814 {"reltimefloat", 1, 1, f_reltimefloat},
815#endif
816 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100817 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remote_foreground", 1, 1, f_remote_foreground},
819 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100820 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100822 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"remove", 2, 3, f_remove},
824 {"rename", 2, 2, f_rename},
825 {"repeat", 2, 2, f_repeat},
826 {"resolve", 1, 1, f_resolve},
827 {"reverse", 1, 1, f_reverse},
828#ifdef FEAT_FLOAT
829 {"round", 1, 1, f_round},
830#endif
831 {"screenattr", 2, 2, f_screenattr},
832 {"screenchar", 2, 2, f_screenchar},
833 {"screencol", 0, 0, f_screencol},
834 {"screenrow", 0, 0, f_screenrow},
835 {"search", 1, 4, f_search},
836 {"searchdecl", 1, 3, f_searchdecl},
837 {"searchpair", 3, 7, f_searchpair},
838 {"searchpairpos", 3, 7, f_searchpairpos},
839 {"searchpos", 1, 4, f_searchpos},
840 {"server2client", 2, 2, f_server2client},
841 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200842 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"setbufvar", 3, 3, f_setbufvar},
844 {"setcharsearch", 1, 1, f_setcharsearch},
845 {"setcmdpos", 1, 1, f_setcmdpos},
846 {"setfperm", 2, 2, f_setfperm},
847 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200848 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"setmatches", 1, 1, f_setmatches},
850 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200851 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"setreg", 2, 3, f_setreg},
853 {"settabvar", 3, 3, f_settabvar},
854 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100855 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"setwinvar", 3, 3, f_setwinvar},
857#ifdef FEAT_CRYPT
858 {"sha256", 1, 1, f_sha256},
859#endif
860 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100861 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100862#ifdef FEAT_SIGNS
863 {"sign_define", 1, 2, f_sign_define},
864 {"sign_getdefined", 0, 1, f_sign_getdefined},
865 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100866 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100867 {"sign_place", 4, 5, f_sign_place},
868 {"sign_undefine", 0, 1, f_sign_undefine},
869 {"sign_unplace", 1, 2, f_sign_unplace},
870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"simplify", 1, 1, f_simplify},
872#ifdef FEAT_FLOAT
873 {"sin", 1, 1, f_sin},
874 {"sinh", 1, 1, f_sinh},
875#endif
876 {"sort", 1, 3, f_sort},
877 {"soundfold", 1, 1, f_soundfold},
878 {"spellbadword", 0, 1, f_spellbadword},
879 {"spellsuggest", 1, 3, f_spellsuggest},
880 {"split", 1, 3, f_split},
881#ifdef FEAT_FLOAT
882 {"sqrt", 1, 1, f_sqrt},
883 {"str2float", 1, 1, f_str2float},
884#endif
885 {"str2nr", 1, 2, f_str2nr},
886 {"strcharpart", 2, 3, f_strcharpart},
887 {"strchars", 1, 2, f_strchars},
888 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
889#ifdef HAVE_STRFTIME
890 {"strftime", 1, 2, f_strftime},
891#endif
892 {"strgetchar", 2, 2, f_strgetchar},
893 {"stridx", 2, 3, f_stridx},
894 {"string", 1, 1, f_string},
895 {"strlen", 1, 1, f_strlen},
896 {"strpart", 2, 3, f_strpart},
897 {"strridx", 2, 3, f_strridx},
898 {"strtrans", 1, 1, f_strtrans},
899 {"strwidth", 1, 1, f_strwidth},
900 {"submatch", 1, 2, f_submatch},
901 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200902 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200903 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"synID", 3, 3, f_synID},
905 {"synIDattr", 2, 3, f_synIDattr},
906 {"synIDtrans", 1, 1, f_synIDtrans},
907 {"synconcealed", 2, 2, f_synconcealed},
908 {"synstack", 2, 2, f_synstack},
909 {"system", 1, 2, f_system},
910 {"systemlist", 1, 2, f_systemlist},
911 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
912 {"tabpagenr", 0, 1, f_tabpagenr},
913 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
914 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100915 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916#ifdef FEAT_FLOAT
917 {"tan", 1, 1, f_tan},
918 {"tanh", 1, 1, f_tanh},
919#endif
920 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200921#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100922 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
923 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100924 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200925 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200926# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
927 {"term_getansicolors", 1, 1, f_term_getansicolors},
928# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200929 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200930 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200931 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200932 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200933 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200935 {"term_getstatus", 1, 1, f_term_getstatus},
936 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200937 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200939 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200940 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200941# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
942 {"term_setansicolors", 2, 2, f_term_setansicolors},
943# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100944 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100945 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200946 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200948 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200949#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
951 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200952 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100954 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100955 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956#ifdef FEAT_JOB_CHANNEL
957 {"test_null_channel", 0, 0, f_test_null_channel},
958#endif
959 {"test_null_dict", 0, 0, f_test_null_dict},
960#ifdef FEAT_JOB_CHANNEL
961 {"test_null_job", 0, 0, f_test_null_job},
962#endif
963 {"test_null_list", 0, 0, f_test_null_list},
964 {"test_null_partial", 0, 0, f_test_null_partial},
965 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200966 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100967 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200968#ifdef FEAT_GUI
969 {"test_scrollbar", 3, 3, f_test_scrollbar},
970#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"test_settime", 1, 1, f_test_settime},
972#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200973 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200974 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975 {"timer_start", 2, 3, f_timer_start},
976 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200977 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978#endif
979 {"tolower", 1, 1, f_tolower},
980 {"toupper", 1, 1, f_toupper},
981 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100982 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983#ifdef FEAT_FLOAT
984 {"trunc", 1, 1, f_trunc},
985#endif
986 {"type", 1, 1, f_type},
987 {"undofile", 1, 1, f_undofile},
988 {"undotree", 0, 0, f_undotree},
989 {"uniq", 1, 3, f_uniq},
990 {"values", 1, 1, f_values},
991 {"virtcol", 1, 1, f_virtcol},
992 {"visualmode", 0, 1, f_visualmode},
993 {"wildmenumode", 0, 0, f_wildmenumode},
994 {"win_findbuf", 1, 1, f_win_findbuf},
995 {"win_getid", 0, 2, f_win_getid},
996 {"win_gotoid", 1, 1, f_win_gotoid},
997 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
998 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100999 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"winbufnr", 1, 1, f_winbufnr},
1001 {"wincol", 0, 0, f_wincol},
1002 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001003 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"winline", 0, 0, f_winline},
1005 {"winnr", 0, 1, f_winnr},
1006 {"winrestcmd", 0, 0, f_winrestcmd},
1007 {"winrestview", 1, 1, f_winrestview},
1008 {"winsaveview", 0, 0, f_winsaveview},
1009 {"winwidth", 1, 1, f_winwidth},
1010 {"wordcount", 0, 0, f_wordcount},
1011 {"writefile", 2, 3, f_writefile},
1012 {"xor", 2, 2, f_xor},
1013};
1014
1015#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1016
1017/*
1018 * Function given to ExpandGeneric() to obtain the list of internal
1019 * or user defined function names.
1020 */
1021 char_u *
1022get_function_name(expand_T *xp, int idx)
1023{
1024 static int intidx = -1;
1025 char_u *name;
1026
1027 if (idx == 0)
1028 intidx = -1;
1029 if (intidx < 0)
1030 {
1031 name = get_user_func_name(xp, idx);
1032 if (name != NULL)
1033 return name;
1034 }
1035 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1036 {
1037 STRCPY(IObuff, functions[intidx].f_name);
1038 STRCAT(IObuff, "(");
1039 if (functions[intidx].f_max_argc == 0)
1040 STRCAT(IObuff, ")");
1041 return IObuff;
1042 }
1043
1044 return NULL;
1045}
1046
1047/*
1048 * Function given to ExpandGeneric() to obtain the list of internal or
1049 * user defined variable or function names.
1050 */
1051 char_u *
1052get_expr_name(expand_T *xp, int idx)
1053{
1054 static int intidx = -1;
1055 char_u *name;
1056
1057 if (idx == 0)
1058 intidx = -1;
1059 if (intidx < 0)
1060 {
1061 name = get_function_name(xp, idx);
1062 if (name != NULL)
1063 return name;
1064 }
1065 return get_user_var_name(xp, ++intidx);
1066}
1067
1068#endif /* FEAT_CMDL_COMPL */
1069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001070/*
1071 * Find internal function in table above.
1072 * Return index, or -1 if not found
1073 */
1074 int
1075find_internal_func(
1076 char_u *name) /* name of the function */
1077{
1078 int first = 0;
1079 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1080 int cmp;
1081 int x;
1082
1083 /*
1084 * Find the function name in the table. Binary search.
1085 */
1086 while (first <= last)
1087 {
1088 x = first + ((unsigned)(last - first) >> 1);
1089 cmp = STRCMP(name, functions[x].f_name);
1090 if (cmp < 0)
1091 last = x - 1;
1092 else if (cmp > 0)
1093 first = x + 1;
1094 else
1095 return x;
1096 }
1097 return -1;
1098}
1099
1100 int
1101call_internal_func(
1102 char_u *name,
1103 int argcount,
1104 typval_T *argvars,
1105 typval_T *rettv)
1106{
1107 int i;
1108
1109 i = find_internal_func(name);
1110 if (i < 0)
1111 return ERROR_UNKNOWN;
1112 if (argcount < functions[i].f_min_argc)
1113 return ERROR_TOOFEW;
1114 if (argcount > functions[i].f_max_argc)
1115 return ERROR_TOOMANY;
1116 argvars[argcount].v_type = VAR_UNKNOWN;
1117 functions[i].f_func(argvars, rettv);
1118 return ERROR_NONE;
1119}
1120
1121/*
1122 * Return TRUE for a non-zero Number and a non-empty String.
1123 */
1124 static int
1125non_zero_arg(typval_T *argvars)
1126{
1127 return ((argvars[0].v_type == VAR_NUMBER
1128 && argvars[0].vval.v_number != 0)
1129 || (argvars[0].v_type == VAR_SPECIAL
1130 && argvars[0].vval.v_number == VVAL_TRUE)
1131 || (argvars[0].v_type == VAR_STRING
1132 && argvars[0].vval.v_string != NULL
1133 && *argvars[0].vval.v_string != NUL));
1134}
1135
1136/*
1137 * Get the lnum from the first argument.
1138 * Also accepts ".", "$", etc., but that only works for the current buffer.
1139 * Returns -1 on error.
1140 */
1141 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001143{
1144 typval_T rettv;
1145 linenr_T lnum;
1146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001147 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148 if (lnum == 0) /* no valid number, try using line() */
1149 {
1150 rettv.v_type = VAR_NUMBER;
1151 f_line(argvars, &rettv);
1152 lnum = (linenr_T)rettv.vval.v_number;
1153 clear_tv(&rettv);
1154 }
1155 return lnum;
1156}
1157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001158/*
1159 * Get the lnum from the first argument.
1160 * Also accepts "$", then "buf" is used.
1161 * Returns 0 on error.
1162 */
1163 static linenr_T
1164tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1165{
1166 if (argvars[0].v_type == VAR_STRING
1167 && argvars[0].vval.v_string != NULL
1168 && argvars[0].vval.v_string[0] == '$'
1169 && buf != NULL)
1170 return buf->b_ml.ml_line_count;
1171 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1172}
1173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001175/*
1176 * Get the float value of "argvars[0]" into "f".
1177 * Returns FAIL when the argument is not a Number or Float.
1178 */
1179 static int
1180get_float_arg(typval_T *argvars, float_T *f)
1181{
1182 if (argvars[0].v_type == VAR_FLOAT)
1183 {
1184 *f = argvars[0].vval.v_float;
1185 return OK;
1186 }
1187 if (argvars[0].v_type == VAR_NUMBER)
1188 {
1189 *f = (float_T)argvars[0].vval.v_number;
1190 return OK;
1191 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001193 return FAIL;
1194}
1195
1196/*
1197 * "abs(expr)" function
1198 */
1199 static void
1200f_abs(typval_T *argvars, typval_T *rettv)
1201{
1202 if (argvars[0].v_type == VAR_FLOAT)
1203 {
1204 rettv->v_type = VAR_FLOAT;
1205 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1206 }
1207 else
1208 {
1209 varnumber_T n;
1210 int error = FALSE;
1211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213 if (error)
1214 rettv->vval.v_number = -1;
1215 else if (n > 0)
1216 rettv->vval.v_number = n;
1217 else
1218 rettv->vval.v_number = -n;
1219 }
1220}
1221
1222/*
1223 * "acos()" function
1224 */
1225 static void
1226f_acos(typval_T *argvars, typval_T *rettv)
1227{
1228 float_T f = 0.0;
1229
1230 rettv->v_type = VAR_FLOAT;
1231 if (get_float_arg(argvars, &f) == OK)
1232 rettv->vval.v_float = acos(f);
1233 else
1234 rettv->vval.v_float = 0.0;
1235}
1236#endif
1237
1238/*
1239 * "add(list, item)" function
1240 */
1241 static void
1242f_add(typval_T *argvars, typval_T *rettv)
1243{
1244 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246
1247 rettv->vval.v_number = 1; /* Default: Failed */
1248 if (argvars[0].v_type == VAR_LIST)
1249 {
1250 if ((l = argvars[0].vval.v_list) != NULL
1251 && !tv_check_lock(l->lv_lock,
1252 (char_u *)N_("add() argument"), TRUE)
1253 && list_append_tv(l, &argvars[1]) == OK)
1254 copy_tv(&argvars[0], rettv);
1255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001256 else if (argvars[0].v_type == VAR_BLOB)
1257 {
1258 if ((b = argvars[0].vval.v_blob) != NULL
1259 && !tv_check_lock(b->bv_lock,
1260 (char_u *)N_("add() argument"), TRUE))
1261 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001262 int error = FALSE;
1263 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1264
1265 if (!error)
1266 {
1267 ga_append(&b->bv_ga, (int)n);
1268 copy_tv(&argvars[0], rettv);
1269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 }
1271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001273 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274}
1275
1276/*
1277 * "and(expr, expr)" function
1278 */
1279 static void
1280f_and(typval_T *argvars, typval_T *rettv)
1281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001282 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1283 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001284}
1285
1286/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001287 * If there is a window for "curbuf", make it the current window.
1288 */
1289 static void
1290find_win_for_curbuf(void)
1291{
1292 wininfo_T *wip;
1293
1294 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1295 {
1296 if (wip->wi_win != NULL)
1297 {
1298 curwin = wip->wi_win;
1299 break;
1300 }
1301 }
1302}
1303
1304/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001305 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 */
1307 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001308set_buffer_lines(
1309 buf_T *buf,
1310 linenr_T lnum_arg,
1311 int append,
1312 typval_T *lines,
1313 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314{
Bram Moolenaarca851592018-06-06 21:04:07 +02001315 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1316 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 list_T *l = NULL;
1318 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 linenr_T append_lnum;
1321 buf_T *curbuf_save = NULL;
1322 win_T *curwin_save = NULL;
1323 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001324
Bram Moolenaarca851592018-06-06 21:04:07 +02001325 /* When using the current buffer ml_mfp will be set if needed. Useful when
1326 * setline() is used on startup. For other buffers the buffer must be
1327 * loaded. */
1328 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001329 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001330 rettv->vval.v_number = 1; /* FAIL */
1331 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 }
1333
Bram Moolenaarca851592018-06-06 21:04:07 +02001334 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001336 curbuf_save = curbuf;
1337 curwin_save = curwin;
1338 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001339 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001340 }
1341
1342 if (append)
1343 // appendbufline() uses the line number below which we insert
1344 append_lnum = lnum - 1;
1345 else
1346 // setbufline() uses the line number above which we insert, we only
1347 // append if it's below the last line
1348 append_lnum = curbuf->b_ml.ml_line_count;
1349
1350 if (lines->v_type == VAR_LIST)
1351 {
1352 l = lines->vval.v_list;
1353 li = l->lv_first;
1354 }
1355 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001356 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001357
1358 /* default result is zero == OK */
1359 for (;;)
1360 {
1361 if (l != NULL)
1362 {
1363 /* list argument, get next string */
1364 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001366 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367 li = li->li_next;
1368 }
1369
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 rettv->vval.v_number = 1; /* FAIL */
1371 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1372 break;
1373
1374 /* When coming here from Insert mode, sync undo, so that this can be
1375 * undone separately from what was previously inserted. */
1376 if (u_sync_once == 2)
1377 {
1378 u_sync_once = 1; /* notify that u_sync() was called */
1379 u_sync(TRUE);
1380 }
1381
1382 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1383 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001384 // Existing line, replace it.
1385 // Removes any existing text properties.
1386 if (u_savesub(lnum) == OK && ml_replace_len(
1387 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 {
1389 changed_bytes(lnum, 0);
1390 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1391 check_cursor_col();
1392 rettv->vval.v_number = 0; /* OK */
1393 }
1394 }
1395 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1396 {
1397 /* append the line */
1398 ++added;
1399 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1400 rettv->vval.v_number = 0; /* OK */
1401 }
1402
1403 if (l == NULL) /* only one string argument */
1404 break;
1405 ++lnum;
1406 }
1407
1408 if (added > 0)
1409 {
1410 win_T *wp;
1411 tabpage_T *tp;
1412
1413 appended_lines_mark(append_lnum, added);
1414 FOR_ALL_TAB_WINDOWS(tp, wp)
1415 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1416 wp->w_cursor.lnum += added;
1417 check_cursor_col();
1418
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419#ifdef FEAT_JOB_CHANNEL
1420 if (bt_prompt(curbuf) && (State & INSERT))
1421 // show the line with the prompt
1422 update_topline();
1423#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001424 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001425
1426 if (!is_curbuf)
1427 {
1428 curbuf = curbuf_save;
1429 curwin = curwin_save;
1430 }
1431}
1432
1433/*
1434 * "append(lnum, string/list)" function
1435 */
1436 static void
1437f_append(typval_T *argvars, typval_T *rettv)
1438{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001440
1441 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1442}
1443
1444/*
1445 * "appendbufline(buf, lnum, string/list)" function
1446 */
1447 static void
1448f_appendbufline(typval_T *argvars, typval_T *rettv)
1449{
1450 linenr_T lnum;
1451 buf_T *buf;
1452
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001453 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001454 if (buf == NULL)
1455 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001458 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001459 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461}
1462
1463/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001464 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001465 */
1466 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001467f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001469 win_T *wp;
1470
1471 if (argvars[0].v_type == VAR_UNKNOWN)
1472 // use the current window
1473 rettv->vval.v_number = ARGCOUNT;
1474 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001475 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001476 // use the global argument list
1477 rettv->vval.v_number = GARGCOUNT;
1478 else
1479 {
1480 // use the argument list of the specified window
1481 wp = find_win_by_nr_or_id(&argvars[0]);
1482 if (wp != NULL)
1483 rettv->vval.v_number = WARGCOUNT(wp);
1484 else
1485 rettv->vval.v_number = -1;
1486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487}
1488
1489/*
1490 * "argidx()" function
1491 */
1492 static void
1493f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495 rettv->vval.v_number = curwin->w_arg_idx;
1496}
1497
1498/*
1499 * "arglistid()" function
1500 */
1501 static void
1502f_arglistid(typval_T *argvars, typval_T *rettv)
1503{
1504 win_T *wp;
1505
1506 rettv->vval.v_number = -1;
1507 wp = find_tabwin(&argvars[0], &argvars[1]);
1508 if (wp != NULL)
1509 rettv->vval.v_number = wp->w_alist->id;
1510}
1511
1512/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513 * Get the argument list for a given window
1514 */
1515 static void
1516get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1517{
1518 int idx;
1519
1520 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1521 for (idx = 0; idx < argcount; ++idx)
1522 list_append_string(rettv->vval.v_list,
1523 alist_name(&arglist[idx]), -1);
1524}
1525
1526/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527 * "argv(nr)" function
1528 */
1529 static void
1530f_argv(typval_T *argvars, typval_T *rettv)
1531{
1532 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001533 aentry_T *arglist = NULL;
1534 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535
1536 if (argvars[0].v_type != VAR_UNKNOWN)
1537 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 if (argvars[1].v_type == VAR_UNKNOWN)
1539 {
1540 arglist = ARGLIST;
1541 argcount = ARGCOUNT;
1542 }
1543 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001545 {
1546 arglist = GARGLIST;
1547 argcount = GARGCOUNT;
1548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 {
1551 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1552
1553 if (wp != NULL)
1554 {
1555 /* Use the argument list of the specified window */
1556 arglist = WARGLIST(wp);
1557 argcount = WARGCOUNT(wp);
1558 }
1559 }
1560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001562 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001563 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001564 if (arglist != NULL && idx >= 0 && idx < argcount)
1565 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1566 else if (idx == -1)
1567 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 else
1570 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571}
1572
1573/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001574 * "assert_beeps(cmd [, error])" function
1575 */
1576 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001577f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001578{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001579 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001580}
1581
1582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 * "assert_equal(expected, actual[, msg])" function
1584 */
1585 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001586f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001588 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589}
1590
1591/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001592 * "assert_equalfile(fname-one, fname-two)" function
1593 */
1594 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001595f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001596{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001597 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001598}
1599
1600/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 * "assert_notequal(expected, actual[, msg])" function
1602 */
1603 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001604f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001606 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607}
1608
1609/*
1610 * "assert_exception(string[, msg])" function
1611 */
1612 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616}
1617
1618/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001619 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 */
1621 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625}
1626
1627/*
1628 * "assert_false(actual[, msg])" function
1629 */
1630 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634}
1635
1636/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001637 * "assert_inrange(lower, upper[, msg])" function
1638 */
1639 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001641{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001643}
1644
1645/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646 * "assert_match(pattern, actual[, msg])" function
1647 */
1648 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652}
1653
1654/*
1655 * "assert_notmatch(pattern, actual[, msg])" function
1656 */
1657 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661}
1662
1663/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001664 * "assert_report(msg)" function
1665 */
1666 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001668{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001670}
1671
1672/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 * "assert_true(actual[, msg])" function
1674 */
1675 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679}
1680
1681#ifdef FEAT_FLOAT
1682/*
1683 * "asin()" function
1684 */
1685 static void
1686f_asin(typval_T *argvars, typval_T *rettv)
1687{
1688 float_T f = 0.0;
1689
1690 rettv->v_type = VAR_FLOAT;
1691 if (get_float_arg(argvars, &f) == OK)
1692 rettv->vval.v_float = asin(f);
1693 else
1694 rettv->vval.v_float = 0.0;
1695}
1696
1697/*
1698 * "atan()" function
1699 */
1700 static void
1701f_atan(typval_T *argvars, typval_T *rettv)
1702{
1703 float_T f = 0.0;
1704
1705 rettv->v_type = VAR_FLOAT;
1706 if (get_float_arg(argvars, &f) == OK)
1707 rettv->vval.v_float = atan(f);
1708 else
1709 rettv->vval.v_float = 0.0;
1710}
1711
1712/*
1713 * "atan2()" function
1714 */
1715 static void
1716f_atan2(typval_T *argvars, typval_T *rettv)
1717{
1718 float_T fx = 0.0, fy = 0.0;
1719
1720 rettv->v_type = VAR_FLOAT;
1721 if (get_float_arg(argvars, &fx) == OK
1722 && get_float_arg(&argvars[1], &fy) == OK)
1723 rettv->vval.v_float = atan2(fx, fy);
1724 else
1725 rettv->vval.v_float = 0.0;
1726}
1727#endif
1728
1729/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001730 * "balloon_show()" function
1731 */
1732#ifdef FEAT_BEVAL
1733 static void
1734f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1735{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001736 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001737 {
1738 if (argvars[0].v_type == VAR_LIST
1739# ifdef FEAT_GUI
1740 && !gui.in_use
1741# endif
1742 )
1743 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1744 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001745 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001746 }
1747}
1748
Bram Moolenaar669a8282017-11-19 20:13:05 +01001749# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001750 static void
1751f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1752{
1753 if (rettv_list_alloc(rettv) == OK)
1754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001755 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001756
1757 if (msg != NULL)
1758 {
1759 pumitem_T *array;
1760 int size = split_message(msg, &array);
1761 int i;
1762
1763 /* Skip the first and last item, they are always empty. */
1764 for (i = 1; i < size - 1; ++i)
1765 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001766 while (size > 0)
1767 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001768 vim_free(array);
1769 }
1770 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001771}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001772# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001773#endif
1774
1775/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001776 * "browse(save, title, initdir, default)" function
1777 */
1778 static void
1779f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1780{
1781#ifdef FEAT_BROWSE
1782 int save;
1783 char_u *title;
1784 char_u *initdir;
1785 char_u *defname;
1786 char_u buf[NUMBUFLEN];
1787 char_u buf2[NUMBUFLEN];
1788 int error = FALSE;
1789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001790 save = (int)tv_get_number_chk(&argvars[0], &error);
1791 title = tv_get_string_chk(&argvars[1]);
1792 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1793 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001794
1795 if (error || title == NULL || initdir == NULL || defname == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string =
1799 do_browse(save ? BROWSE_SAVE : 0,
1800 title, defname, NULL, initdir, NULL, curbuf);
1801#else
1802 rettv->vval.v_string = NULL;
1803#endif
1804 rettv->v_type = VAR_STRING;
1805}
1806
1807/*
1808 * "browsedir(title, initdir)" function
1809 */
1810 static void
1811f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1812{
1813#ifdef FEAT_BROWSE
1814 char_u *title;
1815 char_u *initdir;
1816 char_u buf[NUMBUFLEN];
1817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 title = tv_get_string_chk(&argvars[0]);
1819 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001820
1821 if (title == NULL || initdir == NULL)
1822 rettv->vval.v_string = NULL;
1823 else
1824 rettv->vval.v_string = do_browse(BROWSE_DIR,
1825 title, NULL, NULL, initdir, NULL, curbuf);
1826#else
1827 rettv->vval.v_string = NULL;
1828#endif
1829 rettv->v_type = VAR_STRING;
1830}
1831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832/*
1833 * Find a buffer by number or exact name.
1834 */
1835 static buf_T *
1836find_buffer(typval_T *avar)
1837{
1838 buf_T *buf = NULL;
1839
1840 if (avar->v_type == VAR_NUMBER)
1841 buf = buflist_findnr((int)avar->vval.v_number);
1842 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1843 {
1844 buf = buflist_findname_exp(avar->vval.v_string);
1845 if (buf == NULL)
1846 {
1847 /* No full path name match, try a match with a URL or a "nofile"
1848 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001849 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001850 if (buf->b_fname != NULL
1851 && (path_with_url(buf->b_fname)
1852#ifdef FEAT_QUICKFIX
1853 || bt_nofile(buf)
1854#endif
1855 )
1856 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1857 break;
1858 }
1859 }
1860 return buf;
1861}
1862
1863/*
1864 * "bufexists(expr)" function
1865 */
1866 static void
1867f_bufexists(typval_T *argvars, typval_T *rettv)
1868{
1869 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1870}
1871
1872/*
1873 * "buflisted(expr)" function
1874 */
1875 static void
1876f_buflisted(typval_T *argvars, typval_T *rettv)
1877{
1878 buf_T *buf;
1879
1880 buf = find_buffer(&argvars[0]);
1881 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1882}
1883
1884/*
1885 * "bufloaded(expr)" function
1886 */
1887 static void
1888f_bufloaded(typval_T *argvars, typval_T *rettv)
1889{
1890 buf_T *buf;
1891
1892 buf = find_buffer(&argvars[0]);
1893 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1894}
1895
1896 buf_T *
1897buflist_find_by_name(char_u *name, int curtab_only)
1898{
1899 int save_magic;
1900 char_u *save_cpo;
1901 buf_T *buf;
1902
1903 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1904 save_magic = p_magic;
1905 p_magic = TRUE;
1906 save_cpo = p_cpo;
1907 p_cpo = (char_u *)"";
1908
1909 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1910 TRUE, FALSE, curtab_only));
1911
1912 p_magic = save_magic;
1913 p_cpo = save_cpo;
1914 return buf;
1915}
1916
1917/*
1918 * Get buffer by number or pattern.
1919 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001920 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001921tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922{
1923 char_u *name = tv->vval.v_string;
1924 buf_T *buf;
1925
1926 if (tv->v_type == VAR_NUMBER)
1927 return buflist_findnr((int)tv->vval.v_number);
1928 if (tv->v_type != VAR_STRING)
1929 return NULL;
1930 if (name == NULL || *name == NUL)
1931 return curbuf;
1932 if (name[0] == '$' && name[1] == NUL)
1933 return lastbuf;
1934
1935 buf = buflist_find_by_name(name, curtab_only);
1936
1937 /* If not found, try expanding the name, like done for bufexists(). */
1938 if (buf == NULL)
1939 buf = find_buffer(tv);
1940
1941 return buf;
1942}
1943
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001944#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001946 * Get the buffer from "arg" and give an error and return NULL if it is not
1947 * valid.
1948 */
1949 static buf_T *
1950get_buf_arg(typval_T *arg)
1951{
1952 buf_T *buf;
1953
1954 ++emsg_off;
1955 buf = tv_get_buf(arg, FALSE);
1956 --emsg_off;
1957 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001959 return buf;
1960}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001961#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001962
1963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 * "bufname(expr)" function
1965 */
1966 static void
1967f_bufname(typval_T *argvars, typval_T *rettv)
1968{
1969 buf_T *buf;
1970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001971 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001972 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001973 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 rettv->v_type = VAR_STRING;
1975 if (buf != NULL && buf->b_fname != NULL)
1976 rettv->vval.v_string = vim_strsave(buf->b_fname);
1977 else
1978 rettv->vval.v_string = NULL;
1979 --emsg_off;
1980}
1981
1982/*
1983 * "bufnr(expr)" function
1984 */
1985 static void
1986f_bufnr(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_T *buf;
1989 int error = FALSE;
1990 char_u *name;
1991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001992 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001994 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 --emsg_off;
1996
1997 /* If the buffer isn't found and the second argument is not zero create a
1998 * new buffer. */
1999 if (buf == NULL
2000 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002001 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002003 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004 && !error)
2005 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2006
2007 if (buf != NULL)
2008 rettv->vval.v_number = buf->b_fnum;
2009 else
2010 rettv->vval.v_number = -1;
2011}
2012
2013 static void
2014buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2015{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 win_T *wp;
2017 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 buf_T *buf;
2019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002020 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002022 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002023 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002024 {
2025 ++winnr;
2026 if (wp->w_buffer == buf)
2027 break;
2028 }
2029 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002030 --emsg_off;
2031}
2032
2033/*
2034 * "bufwinid(nr)" function
2035 */
2036 static void
2037f_bufwinid(typval_T *argvars, typval_T *rettv)
2038{
2039 buf_win_common(argvars, rettv, FALSE);
2040}
2041
2042/*
2043 * "bufwinnr(nr)" function
2044 */
2045 static void
2046f_bufwinnr(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_win_common(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "byte2line(byte)" function
2053 */
2054 static void
2055f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2056{
2057#ifndef FEAT_BYTEOFF
2058 rettv->vval.v_number = -1;
2059#else
2060 long boff = 0;
2061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 if (boff < 0)
2064 rettv->vval.v_number = -1;
2065 else
2066 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2067 (linenr_T)0, &boff);
2068#endif
2069}
2070
2071 static void
2072byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2073{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 char_u *str;
2076 varnumber_T idx;
2077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 str = tv_get_string_chk(&argvars[0]);
2079 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 rettv->vval.v_number = -1;
2081 if (str == NULL || idx < 0)
2082 return;
2083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 t = str;
2085 for ( ; idx > 0; idx--)
2086 {
2087 if (*t == NUL) /* EOL reached */
2088 return;
2089 if (enc_utf8 && comp)
2090 t += utf_ptr2len(t);
2091 else
2092 t += (*mb_ptr2len)(t);
2093 }
2094 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096
2097/*
2098 * "byteidx()" function
2099 */
2100 static void
2101f_byteidx(typval_T *argvars, typval_T *rettv)
2102{
2103 byteidx(argvars, rettv, FALSE);
2104}
2105
2106/*
2107 * "byteidxcomp()" function
2108 */
2109 static void
2110f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2111{
2112 byteidx(argvars, rettv, TRUE);
2113}
2114
2115/*
2116 * "call(func, arglist [, dict])" function
2117 */
2118 static void
2119f_call(typval_T *argvars, typval_T *rettv)
2120{
2121 char_u *func;
2122 partial_T *partial = NULL;
2123 dict_T *selfdict = NULL;
2124
2125 if (argvars[1].v_type != VAR_LIST)
2126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 return;
2129 }
2130 if (argvars[1].vval.v_list == NULL)
2131 return;
2132
2133 if (argvars[0].v_type == VAR_FUNC)
2134 func = argvars[0].vval.v_string;
2135 else if (argvars[0].v_type == VAR_PARTIAL)
2136 {
2137 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002138 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 }
2140 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 if (*func == NUL)
2143 return; /* type error or empty name */
2144
2145 if (argvars[2].v_type != VAR_UNKNOWN)
2146 {
2147 if (argvars[2].v_type != VAR_DICT)
2148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 return;
2151 }
2152 selfdict = argvars[2].vval.v_dict;
2153 }
2154
2155 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2156}
2157
2158#ifdef FEAT_FLOAT
2159/*
2160 * "ceil({float})" function
2161 */
2162 static void
2163f_ceil(typval_T *argvars, typval_T *rettv)
2164{
2165 float_T f = 0.0;
2166
2167 rettv->v_type = VAR_FLOAT;
2168 if (get_float_arg(argvars, &f) == OK)
2169 rettv->vval.v_float = ceil(f);
2170 else
2171 rettv->vval.v_float = 0.0;
2172}
2173#endif
2174
2175#ifdef FEAT_JOB_CHANNEL
2176/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002177 * "ch_canread()" function
2178 */
2179 static void
2180f_ch_canread(typval_T *argvars, typval_T *rettv)
2181{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002182 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002183
2184 rettv->vval.v_number = 0;
2185 if (channel != NULL)
2186 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2187 || channel_has_readahead(channel, PART_OUT)
2188 || channel_has_readahead(channel, PART_ERR);
2189}
2190
2191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 * "ch_close()" function
2193 */
2194 static void
2195f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2196{
2197 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2198
2199 if (channel != NULL)
2200 {
2201 channel_close(channel, FALSE);
2202 channel_clear(channel);
2203 }
2204}
2205
2206/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002207 * "ch_close()" function
2208 */
2209 static void
2210f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2211{
2212 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2213
2214 if (channel != NULL)
2215 channel_close_in(channel);
2216}
2217
2218/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 * "ch_getbufnr()" function
2220 */
2221 static void
2222f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2223{
2224 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2225
2226 rettv->vval.v_number = -1;
2227 if (channel != NULL)
2228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002229 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 int part;
2231
2232 if (STRCMP(what, "err") == 0)
2233 part = PART_ERR;
2234 else if (STRCMP(what, "out") == 0)
2235 part = PART_OUT;
2236 else if (STRCMP(what, "in") == 0)
2237 part = PART_IN;
2238 else
2239 part = PART_SOCK;
2240 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2241 rettv->vval.v_number =
2242 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2243 }
2244}
2245
2246/*
2247 * "ch_getjob()" function
2248 */
2249 static void
2250f_ch_getjob(typval_T *argvars, typval_T *rettv)
2251{
2252 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2253
2254 if (channel != NULL)
2255 {
2256 rettv->v_type = VAR_JOB;
2257 rettv->vval.v_job = channel->ch_job;
2258 if (channel->ch_job != NULL)
2259 ++channel->ch_job->jv_refcount;
2260 }
2261}
2262
2263/*
2264 * "ch_info()" function
2265 */
2266 static void
2267f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2268{
2269 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2270
2271 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2272 channel_info(channel, rettv->vval.v_dict);
2273}
2274
2275/*
2276 * "ch_log()" function
2277 */
2278 static void
2279f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 channel_T *channel = NULL;
2283
2284 if (argvars[1].v_type != VAR_UNKNOWN)
2285 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2286
Bram Moolenaard5359b22018-04-05 22:44:39 +02002287 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289
2290/*
2291 * "ch_logfile()" function
2292 */
2293 static void
2294f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2295{
2296 char_u *fname;
2297 char_u *opt = (char_u *)"";
2298 char_u buf[NUMBUFLEN];
2299
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002300 /* Don't open a file in restricted mode. */
2301 if (check_restricted() || check_secure())
2302 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002303 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 ch_logfile(fname, opt);
2307}
2308
2309/*
2310 * "ch_open()" function
2311 */
2312 static void
2313f_ch_open(typval_T *argvars, typval_T *rettv)
2314{
2315 rettv->v_type = VAR_CHANNEL;
2316 if (check_restricted() || check_secure())
2317 return;
2318 rettv->vval.v_channel = channel_open_func(argvars);
2319}
2320
2321/*
2322 * "ch_read()" function
2323 */
2324 static void
2325f_ch_read(typval_T *argvars, typval_T *rettv)
2326{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002327 common_channel_read(argvars, rettv, FALSE, FALSE);
2328}
2329
2330/*
2331 * "ch_readblob()" function
2332 */
2333 static void
2334f_ch_readblob(typval_T *argvars, typval_T *rettv)
2335{
2336 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337}
2338
2339/*
2340 * "ch_readraw()" function
2341 */
2342 static void
2343f_ch_readraw(typval_T *argvars, typval_T *rettv)
2344{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346}
2347
2348/*
2349 * "ch_evalexpr()" function
2350 */
2351 static void
2352f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2353{
2354 ch_expr_common(argvars, rettv, TRUE);
2355}
2356
2357/*
2358 * "ch_sendexpr()" function
2359 */
2360 static void
2361f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2362{
2363 ch_expr_common(argvars, rettv, FALSE);
2364}
2365
2366/*
2367 * "ch_evalraw()" function
2368 */
2369 static void
2370f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2371{
2372 ch_raw_common(argvars, rettv, TRUE);
2373}
2374
2375/*
2376 * "ch_sendraw()" function
2377 */
2378 static void
2379f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2380{
2381 ch_raw_common(argvars, rettv, FALSE);
2382}
2383
2384/*
2385 * "ch_setoptions()" function
2386 */
2387 static void
2388f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2389{
2390 channel_T *channel;
2391 jobopt_T opt;
2392
2393 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2394 if (channel == NULL)
2395 return;
2396 clear_job_options(&opt);
2397 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002398 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 channel_set_options(channel, &opt);
2400 free_job_options(&opt);
2401}
2402
2403/*
2404 * "ch_status()" function
2405 */
2406 static void
2407f_ch_status(typval_T *argvars, typval_T *rettv)
2408{
2409 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002410 jobopt_T opt;
2411 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412
2413 /* return an empty string by default */
2414 rettv->v_type = VAR_STRING;
2415 rettv->vval.v_string = NULL;
2416
2417 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002418
2419 if (argvars[1].v_type != VAR_UNKNOWN)
2420 {
2421 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002422 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002423 && (opt.jo_set & JO_PART))
2424 part = opt.jo_part;
2425 }
2426
2427 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428}
2429#endif
2430
2431/*
2432 * "changenr()" function
2433 */
2434 static void
2435f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2436{
2437 rettv->vval.v_number = curbuf->b_u_seq_cur;
2438}
2439
2440/*
2441 * "char2nr(string)" function
2442 */
2443 static void
2444f_char2nr(typval_T *argvars, typval_T *rettv)
2445{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 if (has_mbyte)
2447 {
2448 int utf8 = 0;
2449
2450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002451 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452
2453 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002454 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002456 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 }
2458 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002459 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460}
2461
2462/*
2463 * "cindent(lnum)" function
2464 */
2465 static void
2466f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2467{
2468#ifdef FEAT_CINDENT
2469 pos_T pos;
2470 linenr_T lnum;
2471
2472 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002473 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2475 {
2476 curwin->w_cursor.lnum = lnum;
2477 rettv->vval.v_number = get_c_indent();
2478 curwin->w_cursor = pos;
2479 }
2480 else
2481#endif
2482 rettv->vval.v_number = -1;
2483}
2484
2485/*
2486 * "clearmatches()" function
2487 */
2488 static void
2489f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2490{
2491#ifdef FEAT_SEARCH_EXTRA
2492 clear_matches(curwin);
2493#endif
2494}
2495
2496/*
2497 * "col(string)" function
2498 */
2499 static void
2500f_col(typval_T *argvars, typval_T *rettv)
2501{
2502 colnr_T col = 0;
2503 pos_T *fp;
2504 int fnum = curbuf->b_fnum;
2505
2506 fp = var2fpos(&argvars[0], FALSE, &fnum);
2507 if (fp != NULL && fnum == curbuf->b_fnum)
2508 {
2509 if (fp->col == MAXCOL)
2510 {
2511 /* '> can be MAXCOL, get the length of the line then */
2512 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2513 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2514 else
2515 col = MAXCOL;
2516 }
2517 else
2518 {
2519 col = fp->col + 1;
2520#ifdef FEAT_VIRTUALEDIT
2521 /* col(".") when the cursor is on the NUL at the end of the line
2522 * because of "coladd" can be seen as an extra column. */
2523 if (virtual_active() && fp == &curwin->w_cursor)
2524 {
2525 char_u *p = ml_get_cursor();
2526
2527 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2528 curwin->w_virtcol - curwin->w_cursor.coladd))
2529 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 int l;
2531
2532 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2533 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 }
2535 }
2536#endif
2537 }
2538 }
2539 rettv->vval.v_number = col;
2540}
2541
2542#if defined(FEAT_INS_EXPAND)
2543/*
2544 * "complete()" function
2545 */
2546 static void
2547f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2548{
2549 int startcol;
2550
2551 if ((State & INSERT) == 0)
2552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002553 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554 return;
2555 }
2556
2557 /* Check for undo allowed here, because if something was already inserted
2558 * the line was already saved for undo and this check isn't done. */
2559 if (!undo_allowed())
2560 return;
2561
2562 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 return;
2566 }
2567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002568 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 if (startcol <= 0)
2570 return;
2571
2572 set_completion(startcol - 1, argvars[1].vval.v_list);
2573}
2574
2575/*
2576 * "complete_add()" function
2577 */
2578 static void
2579f_complete_add(typval_T *argvars, typval_T *rettv)
2580{
2581 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2582}
2583
2584/*
2585 * "complete_check()" function
2586 */
2587 static void
2588f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2589{
2590 int saved = RedrawingDisabled;
2591
2592 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002593 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594 rettv->vval.v_number = compl_interrupted;
2595 RedrawingDisabled = saved;
2596}
2597#endif
2598
2599/*
2600 * "confirm(message, buttons[, default [, type]])" function
2601 */
2602 static void
2603f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2604{
2605#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2606 char_u *message;
2607 char_u *buttons = NULL;
2608 char_u buf[NUMBUFLEN];
2609 char_u buf2[NUMBUFLEN];
2610 int def = 1;
2611 int type = VIM_GENERIC;
2612 char_u *typestr;
2613 int error = FALSE;
2614
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002615 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 if (message == NULL)
2617 error = TRUE;
2618 if (argvars[1].v_type != VAR_UNKNOWN)
2619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002620 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 if (buttons == NULL)
2622 error = TRUE;
2623 if (argvars[2].v_type != VAR_UNKNOWN)
2624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002625 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002626 if (argvars[3].v_type != VAR_UNKNOWN)
2627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 if (typestr == NULL)
2630 error = TRUE;
2631 else
2632 {
2633 switch (TOUPPER_ASC(*typestr))
2634 {
2635 case 'E': type = VIM_ERROR; break;
2636 case 'Q': type = VIM_QUESTION; break;
2637 case 'I': type = VIM_INFO; break;
2638 case 'W': type = VIM_WARNING; break;
2639 case 'G': type = VIM_GENERIC; break;
2640 }
2641 }
2642 }
2643 }
2644 }
2645
2646 if (buttons == NULL || *buttons == NUL)
2647 buttons = (char_u *)_("&Ok");
2648
2649 if (!error)
2650 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2651 def, NULL, FALSE);
2652#endif
2653}
2654
2655/*
2656 * "copy()" function
2657 */
2658 static void
2659f_copy(typval_T *argvars, typval_T *rettv)
2660{
2661 item_copy(&argvars[0], rettv, FALSE, 0);
2662}
2663
2664#ifdef FEAT_FLOAT
2665/*
2666 * "cos()" function
2667 */
2668 static void
2669f_cos(typval_T *argvars, typval_T *rettv)
2670{
2671 float_T f = 0.0;
2672
2673 rettv->v_type = VAR_FLOAT;
2674 if (get_float_arg(argvars, &f) == OK)
2675 rettv->vval.v_float = cos(f);
2676 else
2677 rettv->vval.v_float = 0.0;
2678}
2679
2680/*
2681 * "cosh()" function
2682 */
2683 static void
2684f_cosh(typval_T *argvars, typval_T *rettv)
2685{
2686 float_T f = 0.0;
2687
2688 rettv->v_type = VAR_FLOAT;
2689 if (get_float_arg(argvars, &f) == OK)
2690 rettv->vval.v_float = cosh(f);
2691 else
2692 rettv->vval.v_float = 0.0;
2693}
2694#endif
2695
2696/*
2697 * "count()" function
2698 */
2699 static void
2700f_count(typval_T *argvars, typval_T *rettv)
2701{
2702 long n = 0;
2703 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002704 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705
Bram Moolenaar9966b212017-07-28 16:46:57 +02002706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002707 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002708
2709 if (argvars[0].v_type == VAR_STRING)
2710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002711 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002712 char_u *p = argvars[0].vval.v_string;
2713 char_u *next;
2714
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002715 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002716 {
2717 if (ic)
2718 {
2719 size_t len = STRLEN(expr);
2720
2721 while (*p != NUL)
2722 {
2723 if (MB_STRNICMP(p, expr, len) == 0)
2724 {
2725 ++n;
2726 p += len;
2727 }
2728 else
2729 MB_PTR_ADV(p);
2730 }
2731 }
2732 else
2733 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2734 != NULL)
2735 {
2736 ++n;
2737 p = next + STRLEN(expr);
2738 }
2739 }
2740
2741 }
2742 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 {
2744 listitem_T *li;
2745 list_T *l;
2746 long idx;
2747
2748 if ((l = argvars[0].vval.v_list) != NULL)
2749 {
2750 li = l->lv_first;
2751 if (argvars[2].v_type != VAR_UNKNOWN)
2752 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 if (argvars[3].v_type != VAR_UNKNOWN)
2754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002755 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 if (!error)
2757 {
2758 li = list_find(l, idx);
2759 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002760 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 }
2762 }
2763 if (error)
2764 li = NULL;
2765 }
2766
2767 for ( ; li != NULL; li = li->li_next)
2768 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2769 ++n;
2770 }
2771 }
2772 else if (argvars[0].v_type == VAR_DICT)
2773 {
2774 int todo;
2775 dict_T *d;
2776 hashitem_T *hi;
2777
2778 if ((d = argvars[0].vval.v_dict) != NULL)
2779 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (argvars[2].v_type != VAR_UNKNOWN)
2781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 }
2785
2786 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2787 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2788 {
2789 if (!HASHITEM_EMPTY(hi))
2790 {
2791 --todo;
2792 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2793 ++n;
2794 }
2795 }
2796 }
2797 }
2798 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002799 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 rettv->vval.v_number = n;
2801}
2802
2803/*
2804 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2805 *
2806 * Checks the existence of a cscope connection.
2807 */
2808 static void
2809f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2810{
2811#ifdef FEAT_CSCOPE
2812 int num = 0;
2813 char_u *dbpath = NULL;
2814 char_u *prepend = NULL;
2815 char_u buf[NUMBUFLEN];
2816
2817 if (argvars[0].v_type != VAR_UNKNOWN
2818 && argvars[1].v_type != VAR_UNKNOWN)
2819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002820 num = (int)tv_get_number(&argvars[0]);
2821 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002823 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002824 }
2825
2826 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2827#endif
2828}
2829
2830/*
2831 * "cursor(lnum, col)" function, or
2832 * "cursor(list)"
2833 *
2834 * Moves the cursor to the specified line and column.
2835 * Returns 0 when the position could be set, -1 otherwise.
2836 */
2837 static void
2838f_cursor(typval_T *argvars, typval_T *rettv)
2839{
2840 long line, col;
2841#ifdef FEAT_VIRTUALEDIT
2842 long coladd = 0;
2843#endif
2844 int set_curswant = TRUE;
2845
2846 rettv->vval.v_number = -1;
2847 if (argvars[1].v_type == VAR_UNKNOWN)
2848 {
2849 pos_T pos;
2850 colnr_T curswant = -1;
2851
2852 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002854 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 return;
2856 }
2857 line = pos.lnum;
2858 col = pos.col;
2859#ifdef FEAT_VIRTUALEDIT
2860 coladd = pos.coladd;
2861#endif
2862 if (curswant >= 0)
2863 {
2864 curwin->w_curswant = curswant - 1;
2865 set_curswant = FALSE;
2866 }
2867 }
2868 else
2869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002870 line = tv_get_lnum(argvars);
2871 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872#ifdef FEAT_VIRTUALEDIT
2873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002874 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875#endif
2876 }
2877 if (line < 0 || col < 0
2878#ifdef FEAT_VIRTUALEDIT
2879 || coladd < 0
2880#endif
2881 )
2882 return; /* type error; errmsg already given */
2883 if (line > 0)
2884 curwin->w_cursor.lnum = line;
2885 if (col > 0)
2886 curwin->w_cursor.col = col - 1;
2887#ifdef FEAT_VIRTUALEDIT
2888 curwin->w_cursor.coladd = coladd;
2889#endif
2890
2891 /* Make sure the cursor is in a valid position. */
2892 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893 /* Correct cursor for multi-byte character. */
2894 if (has_mbyte)
2895 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002896
2897 curwin->w_set_curswant = set_curswant;
2898 rettv->vval.v_number = 0;
2899}
2900
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002901#ifdef WIN3264
2902/*
2903 * "debugbreak()" function
2904 */
2905 static void
2906f_debugbreak(typval_T *argvars, typval_T *rettv)
2907{
2908 int pid;
2909
2910 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002911 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002912 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002913 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002914 else
2915 {
2916 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2917
2918 if (hProcess != NULL)
2919 {
2920 DebugBreakProcess(hProcess);
2921 CloseHandle(hProcess);
2922 rettv->vval.v_number = OK;
2923 }
2924 }
2925}
2926#endif
2927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002928/*
2929 * "deepcopy()" function
2930 */
2931 static void
2932f_deepcopy(typval_T *argvars, typval_T *rettv)
2933{
2934 int noref = 0;
2935 int copyID;
2936
2937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002938 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 else
2942 {
2943 copyID = get_copyID();
2944 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2945 }
2946}
2947
2948/*
2949 * "delete()" function
2950 */
2951 static void
2952f_delete(typval_T *argvars, typval_T *rettv)
2953{
2954 char_u nbuf[NUMBUFLEN];
2955 char_u *name;
2956 char_u *flags;
2957
2958 rettv->vval.v_number = -1;
2959 if (check_restricted() || check_secure())
2960 return;
2961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002962 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 if (name == NULL || *name == NUL)
2964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966 return;
2967 }
2968
2969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002970 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 else
2972 flags = (char_u *)"";
2973
2974 if (*flags == NUL)
2975 /* delete a file */
2976 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2977 else if (STRCMP(flags, "d") == 0)
2978 /* delete an empty directory */
2979 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2980 else if (STRCMP(flags, "rf") == 0)
2981 /* delete a directory recursively */
2982 rettv->vval.v_number = delete_recursive(name);
2983 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985}
2986
2987/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002988 * "deletebufline()" function
2989 */
2990 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002991f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002992{
2993 buf_T *buf;
2994 linenr_T first, last;
2995 linenr_T lnum;
2996 long count;
2997 int is_curbuf;
2998 buf_T *curbuf_save = NULL;
2999 win_T *curwin_save = NULL;
3000 tabpage_T *tp;
3001 win_T *wp;
3002
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003003 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003004 if (buf == NULL)
3005 {
3006 rettv->vval.v_number = 1; /* FAIL */
3007 return;
3008 }
3009 is_curbuf = buf == curbuf;
3010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003011 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003013 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003014 else
3015 last = first;
3016
3017 if (buf->b_ml.ml_mfp == NULL || first < 1
3018 || first > buf->b_ml.ml_line_count || last < first)
3019 {
3020 rettv->vval.v_number = 1; /* FAIL */
3021 return;
3022 }
3023
3024 if (!is_curbuf)
3025 {
3026 curbuf_save = curbuf;
3027 curwin_save = curwin;
3028 curbuf = buf;
3029 find_win_for_curbuf();
3030 }
3031 if (last > curbuf->b_ml.ml_line_count)
3032 last = curbuf->b_ml.ml_line_count;
3033 count = last - first + 1;
3034
3035 // When coming here from Insert mode, sync undo, so that this can be
3036 // undone separately from what was previously inserted.
3037 if (u_sync_once == 2)
3038 {
3039 u_sync_once = 1; // notify that u_sync() was called
3040 u_sync(TRUE);
3041 }
3042
3043 if (u_save(first - 1, last + 1) == FAIL)
3044 {
3045 rettv->vval.v_number = 1; /* FAIL */
3046 return;
3047 }
3048
3049 for (lnum = first; lnum <= last; ++lnum)
3050 ml_delete(first, TRUE);
3051
3052 FOR_ALL_TAB_WINDOWS(tp, wp)
3053 if (wp->w_buffer == buf)
3054 {
3055 if (wp->w_cursor.lnum > last)
3056 wp->w_cursor.lnum -= count;
3057 else if (wp->w_cursor.lnum> first)
3058 wp->w_cursor.lnum = first;
3059 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3060 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3061 }
3062 check_cursor_col();
3063 deleted_lines_mark(first, count);
3064
3065 if (!is_curbuf)
3066 {
3067 curbuf = curbuf_save;
3068 curwin = curwin_save;
3069 }
3070}
3071
3072/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 * "did_filetype()" function
3074 */
3075 static void
3076f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3077{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003079}
3080
3081/*
3082 * "diff_filler()" function
3083 */
3084 static void
3085f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3086{
3087#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003088 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089#endif
3090}
3091
3092/*
3093 * "diff_hlID()" function
3094 */
3095 static void
3096f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3097{
3098#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003099 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003101 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 static int fnum = 0;
3103 static int change_start = 0;
3104 static int change_end = 0;
3105 static hlf_T hlID = (hlf_T)0;
3106 int filler_lines;
3107 int col;
3108
3109 if (lnum < 0) /* ignore type error in {lnum} arg */
3110 lnum = 0;
3111 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003112 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 || fnum != curbuf->b_fnum)
3114 {
3115 /* New line, buffer, change: need to get the values. */
3116 filler_lines = diff_check(curwin, lnum);
3117 if (filler_lines < 0)
3118 {
3119 if (filler_lines == -1)
3120 {
3121 change_start = MAXCOL;
3122 change_end = -1;
3123 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3124 hlID = HLF_ADD; /* added line */
3125 else
3126 hlID = HLF_CHD; /* changed line */
3127 }
3128 else
3129 hlID = HLF_ADD; /* added line */
3130 }
3131 else
3132 hlID = (hlf_T)0;
3133 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003134 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 fnum = curbuf->b_fnum;
3136 }
3137
3138 if (hlID == HLF_CHD || hlID == HLF_TXD)
3139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003140 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 if (col >= change_start && col <= change_end)
3142 hlID = HLF_TXD; /* changed text */
3143 else
3144 hlID = HLF_CHD; /* changed line */
3145 }
3146 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3147#endif
3148}
3149
3150/*
3151 * "empty({expr})" function
3152 */
3153 static void
3154f_empty(typval_T *argvars, typval_T *rettv)
3155{
3156 int n = FALSE;
3157
3158 switch (argvars[0].v_type)
3159 {
3160 case VAR_STRING:
3161 case VAR_FUNC:
3162 n = argvars[0].vval.v_string == NULL
3163 || *argvars[0].vval.v_string == NUL;
3164 break;
3165 case VAR_PARTIAL:
3166 n = FALSE;
3167 break;
3168 case VAR_NUMBER:
3169 n = argvars[0].vval.v_number == 0;
3170 break;
3171 case VAR_FLOAT:
3172#ifdef FEAT_FLOAT
3173 n = argvars[0].vval.v_float == 0.0;
3174 break;
3175#endif
3176 case VAR_LIST:
3177 n = argvars[0].vval.v_list == NULL
3178 || argvars[0].vval.v_list->lv_first == NULL;
3179 break;
3180 case VAR_DICT:
3181 n = argvars[0].vval.v_dict == NULL
3182 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3183 break;
3184 case VAR_SPECIAL:
3185 n = argvars[0].vval.v_number != VVAL_TRUE;
3186 break;
3187
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003188 case VAR_BLOB:
3189 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003190 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3191 break;
3192
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 case VAR_JOB:
3194#ifdef FEAT_JOB_CHANNEL
3195 n = argvars[0].vval.v_job == NULL
3196 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3197 break;
3198#endif
3199 case VAR_CHANNEL:
3200#ifdef FEAT_JOB_CHANNEL
3201 n = argvars[0].vval.v_channel == NULL
3202 || !channel_is_open(argvars[0].vval.v_channel);
3203 break;
3204#endif
3205 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003206 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003207 n = TRUE;
3208 break;
3209 }
3210
3211 rettv->vval.v_number = n;
3212}
3213
3214/*
3215 * "escape({string}, {chars})" function
3216 */
3217 static void
3218f_escape(typval_T *argvars, typval_T *rettv)
3219{
3220 char_u buf[NUMBUFLEN];
3221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003222 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3223 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 rettv->v_type = VAR_STRING;
3225}
3226
3227/*
3228 * "eval()" function
3229 */
3230 static void
3231f_eval(typval_T *argvars, typval_T *rettv)
3232{
3233 char_u *s, *p;
3234
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003235 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 if (s != NULL)
3237 s = skipwhite(s);
3238
3239 p = s;
3240 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3241 {
3242 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003243 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 need_clr_eos = FALSE;
3245 rettv->v_type = VAR_NUMBER;
3246 rettv->vval.v_number = 0;
3247 }
3248 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003249 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250}
3251
3252/*
3253 * "eventhandler()" function
3254 */
3255 static void
3256f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3257{
3258 rettv->vval.v_number = vgetc_busy;
3259}
3260
3261/*
3262 * "executable()" function
3263 */
3264 static void
3265f_executable(typval_T *argvars, typval_T *rettv)
3266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003267 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268
3269 /* Check in $PATH and also check directly if there is a directory name. */
3270 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3271 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3272}
3273
3274static garray_T redir_execute_ga;
3275
3276/*
3277 * Append "value[value_len]" to the execute() output.
3278 */
3279 void
3280execute_redir_str(char_u *value, int value_len)
3281{
3282 int len;
3283
3284 if (value_len == -1)
3285 len = (int)STRLEN(value); /* Append the entire string */
3286 else
3287 len = value_len; /* Append only "value_len" characters */
3288 if (ga_grow(&redir_execute_ga, len) == OK)
3289 {
3290 mch_memmove((char *)redir_execute_ga.ga_data
3291 + redir_execute_ga.ga_len, value, len);
3292 redir_execute_ga.ga_len += len;
3293 }
3294}
3295
3296/*
3297 * Get next line from a list.
3298 * Called by do_cmdline() to get the next line.
3299 * Returns allocated string, or NULL for end of function.
3300 */
3301
3302 static char_u *
3303get_list_line(
3304 int c UNUSED,
3305 void *cookie,
3306 int indent UNUSED)
3307{
3308 listitem_T **p = (listitem_T **)cookie;
3309 listitem_T *item = *p;
3310 char_u buf[NUMBUFLEN];
3311 char_u *s;
3312
3313 if (item == NULL)
3314 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003315 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 *p = item->li_next;
3317 return s == NULL ? NULL : vim_strsave(s);
3318}
3319
3320/*
3321 * "execute()" function
3322 */
3323 static void
3324f_execute(typval_T *argvars, typval_T *rettv)
3325{
3326 char_u *cmd = NULL;
3327 list_T *list = NULL;
3328 int save_msg_silent = msg_silent;
3329 int save_emsg_silent = emsg_silent;
3330 int save_emsg_noredir = emsg_noredir;
3331 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003332 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003334 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003335 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336
3337 rettv->vval.v_string = NULL;
3338 rettv->v_type = VAR_STRING;
3339
3340 if (argvars[0].v_type == VAR_LIST)
3341 {
3342 list = argvars[0].vval.v_list;
3343 if (list == NULL || list->lv_first == NULL)
3344 /* empty list, no commands, empty output */
3345 return;
3346 ++list->lv_refcount;
3347 }
3348 else
3349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003350 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 if (cmd == NULL)
3352 return;
3353 }
3354
3355 if (argvars[1].v_type != VAR_UNKNOWN)
3356 {
3357 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
3360 if (s == NULL)
3361 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003362 if (*s == NUL)
3363 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 if (STRNCMP(s, "silent", 6) == 0)
3365 ++msg_silent;
3366 if (STRCMP(s, "silent!") == 0)
3367 {
3368 emsg_silent = TRUE;
3369 emsg_noredir = TRUE;
3370 }
3371 }
3372 else
3373 ++msg_silent;
3374
3375 if (redir_execute)
3376 save_ga = redir_execute_ga;
3377 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3378 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003379 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003380 if (!echo_output)
3381 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382
3383 if (cmd != NULL)
3384 do_cmdline_cmd(cmd);
3385 else
3386 {
3387 listitem_T *item = list->lv_first;
3388
3389 do_cmdline(NULL, get_list_line, (void *)&item,
3390 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3391 --list->lv_refcount;
3392 }
3393
Bram Moolenaard297f352017-01-29 20:31:21 +01003394 /* Need to append a NUL to the result. */
3395 if (ga_grow(&redir_execute_ga, 1) == OK)
3396 {
3397 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3398 rettv->vval.v_string = redir_execute_ga.ga_data;
3399 }
3400 else
3401 {
3402 ga_clear(&redir_execute_ga);
3403 rettv->vval.v_string = NULL;
3404 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 msg_silent = save_msg_silent;
3406 emsg_silent = save_emsg_silent;
3407 emsg_noredir = save_emsg_noredir;
3408
3409 redir_execute = save_redir_execute;
3410 if (redir_execute)
3411 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003412 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003414 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003415 if (echo_output)
3416 // When not working silently: put it in column zero. A following
3417 // "echon" will overwrite the message, unavoidably.
3418 msg_col = 0;
3419 else
3420 // When working silently: Put it back where it was, since nothing
3421 // should have been written.
3422 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423}
3424
3425/*
3426 * "exepath()" function
3427 */
3428 static void
3429f_exepath(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u *p = NULL;
3432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 rettv->v_type = VAR_STRING;
3435 rettv->vval.v_string = p;
3436}
3437
3438/*
3439 * "exists()" function
3440 */
3441 static void
3442f_exists(typval_T *argvars, typval_T *rettv)
3443{
3444 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003447 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 if (*p == '$') /* environment variable */
3449 {
3450 /* first try "normal" environment variables (fast) */
3451 if (mch_getenv(p + 1) != NULL)
3452 n = TRUE;
3453 else
3454 {
3455 /* try expanding things like $VIM and ${HOME} */
3456 p = expand_env_save(p);
3457 if (p != NULL && *p != '$')
3458 n = TRUE;
3459 vim_free(p);
3460 }
3461 }
3462 else if (*p == '&' || *p == '+') /* option */
3463 {
3464 n = (get_option_tv(&p, NULL, TRUE) == OK);
3465 if (*skipwhite(p) != NUL)
3466 n = FALSE; /* trailing garbage */
3467 }
3468 else if (*p == '*') /* internal or user defined function */
3469 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003470 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 }
3472 else if (*p == ':')
3473 {
3474 n = cmd_exists(p + 1);
3475 }
3476 else if (*p == '#')
3477 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478 if (p[1] == '#')
3479 n = autocmd_supported(p + 2);
3480 else
3481 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 }
3483 else /* internal variable */
3484 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003485 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487
3488 rettv->vval.v_number = n;
3489}
3490
3491#ifdef FEAT_FLOAT
3492/*
3493 * "exp()" function
3494 */
3495 static void
3496f_exp(typval_T *argvars, typval_T *rettv)
3497{
3498 float_T f = 0.0;
3499
3500 rettv->v_type = VAR_FLOAT;
3501 if (get_float_arg(argvars, &f) == OK)
3502 rettv->vval.v_float = exp(f);
3503 else
3504 rettv->vval.v_float = 0.0;
3505}
3506#endif
3507
3508/*
3509 * "expand()" function
3510 */
3511 static void
3512f_expand(typval_T *argvars, typval_T *rettv)
3513{
3514 char_u *s;
3515 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003516 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003517 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3518 expand_T xpc;
3519 int error = FALSE;
3520 char_u *result;
3521
3522 rettv->v_type = VAR_STRING;
3523 if (argvars[1].v_type != VAR_UNKNOWN
3524 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526 && !error)
3527 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003528 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 }
3530
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003531 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (*s == '%' || *s == '#' || *s == '<')
3533 {
3534 ++emsg_off;
3535 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3536 --emsg_off;
3537 if (rettv->v_type == VAR_LIST)
3538 {
3539 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3540 list_append_string(rettv->vval.v_list, result, -1);
3541 else
3542 vim_free(result);
3543 }
3544 else
3545 rettv->vval.v_string = result;
3546 }
3547 else
3548 {
3549 /* When the optional second argument is non-zero, don't remove matches
3550 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3551 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003552 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 options |= WILD_KEEP_ALL;
3554 if (!error)
3555 {
3556 ExpandInit(&xpc);
3557 xpc.xp_context = EXPAND_FILES;
3558 if (p_wic)
3559 options += WILD_ICASE;
3560 if (rettv->v_type == VAR_STRING)
3561 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3562 options, WILD_ALL);
3563 else if (rettv_list_alloc(rettv) != FAIL)
3564 {
3565 int i;
3566
3567 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3568 for (i = 0; i < xpc.xp_numfiles; i++)
3569 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3570 ExpandCleanup(&xpc);
3571 }
3572 }
3573 else
3574 rettv->vval.v_string = NULL;
3575 }
3576}
3577
3578/*
3579 * "extend(list, list [, idx])" function
3580 * "extend(dict, dict [, action])" function
3581 */
3582 static void
3583f_extend(typval_T *argvars, typval_T *rettv)
3584{
3585 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3586
3587 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3588 {
3589 list_T *l1, *l2;
3590 listitem_T *item;
3591 long before;
3592 int error = FALSE;
3593
3594 l1 = argvars[0].vval.v_list;
3595 l2 = argvars[1].vval.v_list;
3596 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3597 && l2 != NULL)
3598 {
3599 if (argvars[2].v_type != VAR_UNKNOWN)
3600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003601 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 if (error)
3603 return; /* type error; errmsg already given */
3604
3605 if (before == l1->lv_len)
3606 item = NULL;
3607 else
3608 {
3609 item = list_find(l1, before);
3610 if (item == NULL)
3611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003612 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 return;
3614 }
3615 }
3616 }
3617 else
3618 item = NULL;
3619 list_extend(l1, l2, item);
3620
3621 copy_tv(&argvars[0], rettv);
3622 }
3623 }
3624 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3625 {
3626 dict_T *d1, *d2;
3627 char_u *action;
3628 int i;
3629
3630 d1 = argvars[0].vval.v_dict;
3631 d2 = argvars[1].vval.v_dict;
3632 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3633 && d2 != NULL)
3634 {
3635 /* Check the third argument. */
3636 if (argvars[2].v_type != VAR_UNKNOWN)
3637 {
3638 static char *(av[]) = {"keep", "force", "error"};
3639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 if (action == NULL)
3642 return; /* type error; errmsg already given */
3643 for (i = 0; i < 3; ++i)
3644 if (STRCMP(action, av[i]) == 0)
3645 break;
3646 if (i == 3)
3647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 return;
3650 }
3651 }
3652 else
3653 action = (char_u *)"force";
3654
3655 dict_extend(d1, d2, action);
3656
3657 copy_tv(&argvars[0], rettv);
3658 }
3659 }
3660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662}
3663
3664/*
3665 * "feedkeys()" function
3666 */
3667 static void
3668f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3669{
3670 int remap = TRUE;
3671 int insert = FALSE;
3672 char_u *keys, *flags;
3673 char_u nbuf[NUMBUFLEN];
3674 int typed = FALSE;
3675 int execute = FALSE;
3676 int dangerous = FALSE;
3677 char_u *keys_esc;
3678
3679 /* This is not allowed in the sandbox. If the commands would still be
3680 * executed in the sandbox it would be OK, but it probably happens later,
3681 * when "sandbox" is no longer set. */
3682 if (check_secure())
3683 return;
3684
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003685 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003686
3687 if (argvars[1].v_type != VAR_UNKNOWN)
3688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003689 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 for ( ; *flags != NUL; ++flags)
3691 {
3692 switch (*flags)
3693 {
3694 case 'n': remap = FALSE; break;
3695 case 'm': remap = TRUE; break;
3696 case 't': typed = TRUE; break;
3697 case 'i': insert = TRUE; break;
3698 case 'x': execute = TRUE; break;
3699 case '!': dangerous = TRUE; break;
3700 }
3701 }
3702 }
3703
3704 if (*keys != NUL || execute)
3705 {
3706 /* Need to escape K_SPECIAL and CSI before putting the string in the
3707 * typeahead buffer. */
3708 keys_esc = vim_strsave_escape_csi(keys);
3709 if (keys_esc != NULL)
3710 {
3711 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3712 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3713 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003714 if (vgetc_busy
3715#ifdef FEAT_TIMERS
3716 || timer_busy
3717#endif
3718 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003719 typebuf_was_filled = TRUE;
3720 if (execute)
3721 {
3722 int save_msg_scroll = msg_scroll;
3723
3724 /* Avoid a 1 second delay when the keys start Insert mode. */
3725 msg_scroll = FALSE;
3726
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003727 if (!dangerous)
3728 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003729 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003730 if (!dangerous)
3731 --ex_normal_busy;
3732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003733 msg_scroll |= save_msg_scroll;
3734 }
3735 }
3736 }
3737}
3738
3739/*
3740 * "filereadable()" function
3741 */
3742 static void
3743f_filereadable(typval_T *argvars, typval_T *rettv)
3744{
3745 int fd;
3746 char_u *p;
3747 int n;
3748
3749#ifndef O_NONBLOCK
3750# define O_NONBLOCK 0
3751#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003752 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003753 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3754 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3755 {
3756 n = TRUE;
3757 close(fd);
3758 }
3759 else
3760 n = FALSE;
3761
3762 rettv->vval.v_number = n;
3763}
3764
3765/*
3766 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3767 * rights to write into.
3768 */
3769 static void
3770f_filewritable(typval_T *argvars, typval_T *rettv)
3771{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003772 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003773}
3774
3775 static void
3776findfilendir(
3777 typval_T *argvars UNUSED,
3778 typval_T *rettv,
3779 int find_what UNUSED)
3780{
3781#ifdef FEAT_SEARCHPATH
3782 char_u *fname;
3783 char_u *fresult = NULL;
3784 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3785 char_u *p;
3786 char_u pathbuf[NUMBUFLEN];
3787 int count = 1;
3788 int first = TRUE;
3789 int error = FALSE;
3790#endif
3791
3792 rettv->vval.v_string = NULL;
3793 rettv->v_type = VAR_STRING;
3794
3795#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797
3798 if (argvars[1].v_type != VAR_UNKNOWN)
3799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003800 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 if (p == NULL)
3802 error = TRUE;
3803 else
3804 {
3805 if (*p != NUL)
3806 path = p;
3807
3808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003809 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003810 }
3811 }
3812
3813 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3814 error = TRUE;
3815
3816 if (*fname != NUL && !error)
3817 {
3818 do
3819 {
3820 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3821 vim_free(fresult);
3822 fresult = find_file_in_path_option(first ? fname : NULL,
3823 first ? (int)STRLEN(fname) : 0,
3824 0, first, path,
3825 find_what,
3826 curbuf->b_ffname,
3827 find_what == FINDFILE_DIR
3828 ? (char_u *)"" : curbuf->b_p_sua);
3829 first = FALSE;
3830
3831 if (fresult != NULL && rettv->v_type == VAR_LIST)
3832 list_append_string(rettv->vval.v_list, fresult, -1);
3833
3834 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3835 }
3836
3837 if (rettv->v_type == VAR_STRING)
3838 rettv->vval.v_string = fresult;
3839#endif
3840}
3841
3842/*
3843 * "filter()" function
3844 */
3845 static void
3846f_filter(typval_T *argvars, typval_T *rettv)
3847{
3848 filter_map(argvars, rettv, FALSE);
3849}
3850
3851/*
3852 * "finddir({fname}[, {path}[, {count}]])" function
3853 */
3854 static void
3855f_finddir(typval_T *argvars, typval_T *rettv)
3856{
3857 findfilendir(argvars, rettv, FINDFILE_DIR);
3858}
3859
3860/*
3861 * "findfile({fname}[, {path}[, {count}]])" function
3862 */
3863 static void
3864f_findfile(typval_T *argvars, typval_T *rettv)
3865{
3866 findfilendir(argvars, rettv, FINDFILE_FILE);
3867}
3868
3869#ifdef FEAT_FLOAT
3870/*
3871 * "float2nr({float})" function
3872 */
3873 static void
3874f_float2nr(typval_T *argvars, typval_T *rettv)
3875{
3876 float_T f = 0.0;
3877
3878 if (get_float_arg(argvars, &f) == OK)
3879 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003880 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003881 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003882 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003883 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 else
3885 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 }
3887}
3888
3889/*
3890 * "floor({float})" function
3891 */
3892 static void
3893f_floor(typval_T *argvars, typval_T *rettv)
3894{
3895 float_T f = 0.0;
3896
3897 rettv->v_type = VAR_FLOAT;
3898 if (get_float_arg(argvars, &f) == OK)
3899 rettv->vval.v_float = floor(f);
3900 else
3901 rettv->vval.v_float = 0.0;
3902}
3903
3904/*
3905 * "fmod()" function
3906 */
3907 static void
3908f_fmod(typval_T *argvars, typval_T *rettv)
3909{
3910 float_T fx = 0.0, fy = 0.0;
3911
3912 rettv->v_type = VAR_FLOAT;
3913 if (get_float_arg(argvars, &fx) == OK
3914 && get_float_arg(&argvars[1], &fy) == OK)
3915 rettv->vval.v_float = fmod(fx, fy);
3916 else
3917 rettv->vval.v_float = 0.0;
3918}
3919#endif
3920
3921/*
3922 * "fnameescape({string})" function
3923 */
3924 static void
3925f_fnameescape(typval_T *argvars, typval_T *rettv)
3926{
3927 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003928 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 rettv->v_type = VAR_STRING;
3930}
3931
3932/*
3933 * "fnamemodify({fname}, {mods})" function
3934 */
3935 static void
3936f_fnamemodify(typval_T *argvars, typval_T *rettv)
3937{
3938 char_u *fname;
3939 char_u *mods;
3940 int usedlen = 0;
3941 int len;
3942 char_u *fbuf = NULL;
3943 char_u buf[NUMBUFLEN];
3944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003945 fname = tv_get_string_chk(&argvars[0]);
3946 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003947 if (fname == NULL || mods == NULL)
3948 fname = NULL;
3949 else
3950 {
3951 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003952 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 }
3954
3955 rettv->v_type = VAR_STRING;
3956 if (fname == NULL)
3957 rettv->vval.v_string = NULL;
3958 else
3959 rettv->vval.v_string = vim_strnsave(fname, len);
3960 vim_free(fbuf);
3961}
3962
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963/*
3964 * "foldclosed()" function
3965 */
3966 static void
3967foldclosed_both(
3968 typval_T *argvars UNUSED,
3969 typval_T *rettv,
3970 int end UNUSED)
3971{
3972#ifdef FEAT_FOLDING
3973 linenr_T lnum;
3974 linenr_T first, last;
3975
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003976 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3978 {
3979 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3980 {
3981 if (end)
3982 rettv->vval.v_number = (varnumber_T)last;
3983 else
3984 rettv->vval.v_number = (varnumber_T)first;
3985 return;
3986 }
3987 }
3988#endif
3989 rettv->vval.v_number = -1;
3990}
3991
3992/*
3993 * "foldclosed()" function
3994 */
3995 static void
3996f_foldclosed(typval_T *argvars, typval_T *rettv)
3997{
3998 foldclosed_both(argvars, rettv, FALSE);
3999}
4000
4001/*
4002 * "foldclosedend()" function
4003 */
4004 static void
4005f_foldclosedend(typval_T *argvars, typval_T *rettv)
4006{
4007 foldclosed_both(argvars, rettv, TRUE);
4008}
4009
4010/*
4011 * "foldlevel()" function
4012 */
4013 static void
4014f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4015{
4016#ifdef FEAT_FOLDING
4017 linenr_T lnum;
4018
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4021 rettv->vval.v_number = foldLevel(lnum);
4022#endif
4023}
4024
4025/*
4026 * "foldtext()" function
4027 */
4028 static void
4029f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4030{
4031#ifdef FEAT_FOLDING
4032 linenr_T foldstart;
4033 linenr_T foldend;
4034 char_u *dashes;
4035 linenr_T lnum;
4036 char_u *s;
4037 char_u *r;
4038 int len;
4039 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004040 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004041#endif
4042
4043 rettv->v_type = VAR_STRING;
4044 rettv->vval.v_string = NULL;
4045#ifdef FEAT_FOLDING
4046 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4047 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4048 dashes = get_vim_var_str(VV_FOLDDASHES);
4049 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4050 && dashes != NULL)
4051 {
4052 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004053 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004054 if (!linewhite(lnum))
4055 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004056
4057 /* Find interesting text in this line. */
4058 s = skipwhite(ml_get(lnum));
4059 /* skip C comment-start */
4060 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4061 {
4062 s = skipwhite(s + 2);
4063 if (*skipwhite(s) == NUL
4064 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4065 {
4066 s = skipwhite(ml_get(lnum + 1));
4067 if (*s == '*')
4068 s = skipwhite(s + 1);
4069 }
4070 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004071 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004072 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004073 r = alloc((unsigned)(STRLEN(txt)
4074 + STRLEN(dashes) /* for %s */
4075 + 20 /* for %3ld */
4076 + STRLEN(s))); /* concatenated */
4077 if (r != NULL)
4078 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004079 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004080 len = (int)STRLEN(r);
4081 STRCAT(r, s);
4082 /* remove 'foldmarker' and 'commentstring' */
4083 foldtext_cleanup(r + len);
4084 rettv->vval.v_string = r;
4085 }
4086 }
4087#endif
4088}
4089
4090/*
4091 * "foldtextresult(lnum)" function
4092 */
4093 static void
4094f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4095{
4096#ifdef FEAT_FOLDING
4097 linenr_T lnum;
4098 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004099 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100 foldinfo_T foldinfo;
4101 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004102 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004103#endif
4104
4105 rettv->v_type = VAR_STRING;
4106 rettv->vval.v_string = NULL;
4107#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004108 if (entered)
4109 return; /* reject recursive use */
4110 entered = TRUE;
4111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004112 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004113 /* treat illegal types and illegal string values for {lnum} the same */
4114 if (lnum < 0)
4115 lnum = 0;
4116 fold_count = foldedCount(curwin, lnum, &foldinfo);
4117 if (fold_count > 0)
4118 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4120 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004121 if (text == buf)
4122 text = vim_strsave(text);
4123 rettv->vval.v_string = text;
4124 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004125
4126 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004127#endif
4128}
4129
4130/*
4131 * "foreground()" function
4132 */
4133 static void
4134f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4135{
4136#ifdef FEAT_GUI
4137 if (gui.in_use)
4138 gui_mch_set_foreground();
4139#else
4140# ifdef WIN32
4141 win32_set_foreground();
4142# endif
4143#endif
4144}
4145
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004147common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148{
4149 char_u *s;
4150 char_u *name;
4151 int use_string = FALSE;
4152 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004153 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004154
4155 if (argvars[0].v_type == VAR_FUNC)
4156 {
4157 /* function(MyFunc, [arg], dict) */
4158 s = argvars[0].vval.v_string;
4159 }
4160 else if (argvars[0].v_type == VAR_PARTIAL
4161 && argvars[0].vval.v_partial != NULL)
4162 {
4163 /* function(dict.MyFunc, [arg]) */
4164 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004165 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004166 }
4167 else
4168 {
4169 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004170 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004171 use_string = TRUE;
4172 }
4173
Bram Moolenaar843b8842016-08-21 14:36:15 +02004174 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004175 {
4176 name = s;
4177 trans_name = trans_function_name(&name, FALSE,
4178 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4179 if (*name != NUL)
4180 s = NULL;
4181 }
4182
Bram Moolenaar843b8842016-08-21 14:36:15 +02004183 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4184 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004187 else if (trans_name != NULL && (is_funcref
4188 ? find_func(trans_name) == NULL
4189 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 else
4192 {
4193 int dict_idx = 0;
4194 int arg_idx = 0;
4195 list_T *list = NULL;
4196
4197 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4198 {
4199 char sid_buf[25];
4200 int off = *s == 's' ? 2 : 5;
4201
4202 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4203 * also be called from another script. Using trans_function_name()
4204 * would also work, but some plugins depend on the name being
4205 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004206 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4208 if (name != NULL)
4209 {
4210 STRCPY(name, sid_buf);
4211 STRCAT(name, s + off);
4212 }
4213 }
4214 else
4215 name = vim_strsave(s);
4216
4217 if (argvars[1].v_type != VAR_UNKNOWN)
4218 {
4219 if (argvars[2].v_type != VAR_UNKNOWN)
4220 {
4221 /* function(name, [args], dict) */
4222 arg_idx = 1;
4223 dict_idx = 2;
4224 }
4225 else if (argvars[1].v_type == VAR_DICT)
4226 /* function(name, dict) */
4227 dict_idx = 1;
4228 else
4229 /* function(name, [args]) */
4230 arg_idx = 1;
4231 if (dict_idx > 0)
4232 {
4233 if (argvars[dict_idx].v_type != VAR_DICT)
4234 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004235 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004236 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004237 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 }
4239 if (argvars[dict_idx].vval.v_dict == NULL)
4240 dict_idx = 0;
4241 }
4242 if (arg_idx > 0)
4243 {
4244 if (argvars[arg_idx].v_type != VAR_LIST)
4245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 list = argvars[arg_idx].vval.v_list;
4251 if (list == NULL || list->lv_len == 0)
4252 arg_idx = 0;
4253 }
4254 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004255 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 {
4257 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4258
4259 /* result is a VAR_PARTIAL */
4260 if (pt == NULL)
4261 vim_free(name);
4262 else
4263 {
4264 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4265 {
4266 listitem_T *li;
4267 int i = 0;
4268 int arg_len = 0;
4269 int lv_len = 0;
4270
4271 if (arg_pt != NULL)
4272 arg_len = arg_pt->pt_argc;
4273 if (list != NULL)
4274 lv_len = list->lv_len;
4275 pt->pt_argc = arg_len + lv_len;
4276 pt->pt_argv = (typval_T *)alloc(
4277 sizeof(typval_T) * pt->pt_argc);
4278 if (pt->pt_argv == NULL)
4279 {
4280 vim_free(pt);
4281 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004282 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004283 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004284 for (i = 0; i < arg_len; i++)
4285 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4286 if (lv_len > 0)
4287 for (li = list->lv_first; li != NULL;
4288 li = li->li_next)
4289 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 }
4291
4292 /* For "function(dict.func, [], dict)" and "func" is a partial
4293 * use "dict". That is backwards compatible. */
4294 if (dict_idx > 0)
4295 {
4296 /* The dict is bound explicitly, pt_auto is FALSE. */
4297 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4298 ++pt->pt_dict->dv_refcount;
4299 }
4300 else if (arg_pt != NULL)
4301 {
4302 /* If the dict was bound automatically the result is also
4303 * bound automatically. */
4304 pt->pt_dict = arg_pt->pt_dict;
4305 pt->pt_auto = arg_pt->pt_auto;
4306 if (pt->pt_dict != NULL)
4307 ++pt->pt_dict->dv_refcount;
4308 }
4309
4310 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004311 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4312 {
4313 pt->pt_func = arg_pt->pt_func;
4314 func_ptr_ref(pt->pt_func);
4315 vim_free(name);
4316 }
4317 else if (is_funcref)
4318 {
4319 pt->pt_func = find_func(trans_name);
4320 func_ptr_ref(pt->pt_func);
4321 vim_free(name);
4322 }
4323 else
4324 {
4325 pt->pt_name = name;
4326 func_ref(name);
4327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004328 }
4329 rettv->v_type = VAR_PARTIAL;
4330 rettv->vval.v_partial = pt;
4331 }
4332 else
4333 {
4334 /* result is a VAR_FUNC */
4335 rettv->v_type = VAR_FUNC;
4336 rettv->vval.v_string = name;
4337 func_ref(name);
4338 }
4339 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004340theend:
4341 vim_free(trans_name);
4342}
4343
4344/*
4345 * "funcref()" function
4346 */
4347 static void
4348f_funcref(typval_T *argvars, typval_T *rettv)
4349{
4350 common_function(argvars, rettv, TRUE);
4351}
4352
4353/*
4354 * "function()" function
4355 */
4356 static void
4357f_function(typval_T *argvars, typval_T *rettv)
4358{
4359 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360}
4361
4362/*
4363 * "garbagecollect()" function
4364 */
4365 static void
4366f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4367{
4368 /* This is postponed until we are back at the toplevel, because we may be
4369 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4370 want_garbage_collect = TRUE;
4371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 garbage_collect_at_exit = TRUE;
4374}
4375
4376/*
4377 * "get()" function
4378 */
4379 static void
4380f_get(typval_T *argvars, typval_T *rettv)
4381{
4382 listitem_T *li;
4383 list_T *l;
4384 dictitem_T *di;
4385 dict_T *d;
4386 typval_T *tv = NULL;
4387
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004388 if (argvars[0].v_type == VAR_BLOB)
4389 {
4390 int error = FALSE;
4391 int idx = tv_get_number_chk(&argvars[1], &error);
4392
4393 if (!error)
4394 {
4395 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004396 if (idx < 0)
4397 idx = blob_len(argvars[0].vval.v_blob) + idx;
4398 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4399 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004400 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004401 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004403 tv = rettv;
4404 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004405 }
4406 }
4407 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 {
4409 if ((l = argvars[0].vval.v_list) != NULL)
4410 {
4411 int error = FALSE;
4412
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004413 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414 if (!error && li != NULL)
4415 tv = &li->li_tv;
4416 }
4417 }
4418 else if (argvars[0].v_type == VAR_DICT)
4419 {
4420 if ((d = argvars[0].vval.v_dict) != NULL)
4421 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004422 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423 if (di != NULL)
4424 tv = &di->di_tv;
4425 }
4426 }
4427 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4428 {
4429 partial_T *pt;
4430 partial_T fref_pt;
4431
4432 if (argvars[0].v_type == VAR_PARTIAL)
4433 pt = argvars[0].vval.v_partial;
4434 else
4435 {
4436 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4437 fref_pt.pt_name = argvars[0].vval.v_string;
4438 pt = &fref_pt;
4439 }
4440
4441 if (pt != NULL)
4442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004443 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004444 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004445
4446 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4447 {
4448 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 n = partial_name(pt);
4450 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 rettv->vval.v_string = NULL;
4452 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004453 {
4454 rettv->vval.v_string = vim_strsave(n);
4455 if (rettv->v_type == VAR_FUNC)
4456 func_ref(rettv->vval.v_string);
4457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 }
4459 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004460 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 else if (STRCMP(what, "args") == 0)
4462 {
4463 rettv->v_type = VAR_LIST;
4464 if (rettv_list_alloc(rettv) == OK)
4465 {
4466 int i;
4467
4468 for (i = 0; i < pt->pt_argc; ++i)
4469 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4470 }
4471 }
4472 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004473 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 return;
4475 }
4476 }
4477 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004478 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004479
4480 if (tv == NULL)
4481 {
4482 if (argvars[2].v_type != VAR_UNKNOWN)
4483 copy_tv(&argvars[2], rettv);
4484 }
4485 else
4486 copy_tv(tv, rettv);
4487}
4488
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004489/*
4490 * Returns buffer options, variables and other attributes in a dictionary.
4491 */
4492 static dict_T *
4493get_buffer_info(buf_T *buf)
4494{
4495 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004496 tabpage_T *tp;
4497 win_T *wp;
4498 list_T *windows;
4499
4500 dict = dict_alloc();
4501 if (dict == NULL)
4502 return NULL;
4503
Bram Moolenaare0be1672018-07-08 16:50:37 +02004504 dict_add_number(dict, "bufnr", buf->b_fnum);
4505 dict_add_string(dict, "name", buf->b_ffname);
4506 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4507 : buflist_findlnum(buf));
4508 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4509 dict_add_number(dict, "listed", buf->b_p_bl);
4510 dict_add_number(dict, "changed", bufIsChanged(buf));
4511 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4512 dict_add_number(dict, "hidden",
4513 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004515 /* Get a reference to buffer variables */
4516 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004517
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004518 /* List of windows displaying this buffer */
4519 windows = list_alloc();
4520 if (windows != NULL)
4521 {
4522 FOR_ALL_TAB_WINDOWS(tp, wp)
4523 if (wp->w_buffer == buf)
4524 list_append_number(windows, (varnumber_T)wp->w_id);
4525 dict_add_list(dict, "windows", windows);
4526 }
4527
4528#ifdef FEAT_SIGNS
4529 if (buf->b_signlist != NULL)
4530 {
4531 /* List of signs placed in this buffer */
4532 list_T *signs = list_alloc();
4533 if (signs != NULL)
4534 {
4535 get_buffer_signs(buf, signs);
4536 dict_add_list(dict, "signs", signs);
4537 }
4538 }
4539#endif
4540
4541 return dict;
4542}
4543
4544/*
4545 * "getbufinfo()" function
4546 */
4547 static void
4548f_getbufinfo(typval_T *argvars, typval_T *rettv)
4549{
4550 buf_T *buf = NULL;
4551 buf_T *argbuf = NULL;
4552 dict_T *d;
4553 int filtered = FALSE;
4554 int sel_buflisted = FALSE;
4555 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004556 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004557
4558 if (rettv_list_alloc(rettv) != OK)
4559 return;
4560
4561 /* List of all the buffers or selected buffers */
4562 if (argvars[0].v_type == VAR_DICT)
4563 {
4564 dict_T *sel_d = argvars[0].vval.v_dict;
4565
4566 if (sel_d != NULL)
4567 {
4568 dictitem_T *di;
4569
4570 filtered = TRUE;
4571
4572 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004573 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004574 sel_buflisted = TRUE;
4575
4576 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004577 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004578 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004579
4580 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004581 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004582 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004583 }
4584 }
4585 else if (argvars[0].v_type != VAR_UNKNOWN)
4586 {
4587 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004589 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004590 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004591 --emsg_off;
4592 if (argbuf == NULL)
4593 return;
4594 }
4595
4596 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004597 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004598 {
4599 if (argbuf != NULL && argbuf != buf)
4600 continue;
4601 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004602 || (sel_buflisted && !buf->b_p_bl)
4603 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004604 continue;
4605
4606 d = get_buffer_info(buf);
4607 if (d != NULL)
4608 list_append_dict(rettv->vval.v_list, d);
4609 if (argbuf != NULL)
4610 return;
4611 }
4612}
4613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004614/*
4615 * Get line or list of lines from buffer "buf" into "rettv".
4616 * Return a range (from start to end) of lines in rettv from the specified
4617 * buffer.
4618 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4619 */
4620 static void
4621get_buffer_lines(
4622 buf_T *buf,
4623 linenr_T start,
4624 linenr_T end,
4625 int retlist,
4626 typval_T *rettv)
4627{
4628 char_u *p;
4629
4630 rettv->v_type = VAR_STRING;
4631 rettv->vval.v_string = NULL;
4632 if (retlist && rettv_list_alloc(rettv) == FAIL)
4633 return;
4634
4635 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4636 return;
4637
4638 if (!retlist)
4639 {
4640 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4641 p = ml_get_buf(buf, start, FALSE);
4642 else
4643 p = (char_u *)"";
4644 rettv->vval.v_string = vim_strsave(p);
4645 }
4646 else
4647 {
4648 if (end < start)
4649 return;
4650
4651 if (start < 1)
4652 start = 1;
4653 if (end > buf->b_ml.ml_line_count)
4654 end = buf->b_ml.ml_line_count;
4655 while (start <= end)
4656 if (list_append_string(rettv->vval.v_list,
4657 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4658 break;
4659 }
4660}
4661
4662/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004663 * "getbufline()" function
4664 */
4665 static void
4666f_getbufline(typval_T *argvars, typval_T *rettv)
4667{
4668 linenr_T lnum;
4669 linenr_T end;
4670 buf_T *buf;
4671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004672 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004674 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 --emsg_off;
4676
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004677 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 if (argvars[2].v_type == VAR_UNKNOWN)
4679 end = lnum;
4680 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004681 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682
4683 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4684}
4685
4686/*
4687 * "getbufvar()" function
4688 */
4689 static void
4690f_getbufvar(typval_T *argvars, typval_T *rettv)
4691{
4692 buf_T *buf;
4693 buf_T *save_curbuf;
4694 char_u *varname;
4695 dictitem_T *v;
4696 int done = FALSE;
4697
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004698 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4699 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004700 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004701 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004702
4703 rettv->v_type = VAR_STRING;
4704 rettv->vval.v_string = NULL;
4705
4706 if (buf != NULL && varname != NULL)
4707 {
4708 /* set curbuf to be our buf, temporarily */
4709 save_curbuf = curbuf;
4710 curbuf = buf;
4711
Bram Moolenaar30567352016-08-27 21:25:44 +02004712 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004714 if (varname[1] == NUL)
4715 {
4716 /* get all buffer-local options in a dict */
4717 dict_T *opts = get_winbuf_options(TRUE);
4718
4719 if (opts != NULL)
4720 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004721 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004722 done = TRUE;
4723 }
4724 }
4725 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4726 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 done = TRUE;
4728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 else
4730 {
4731 /* Look up the variable. */
4732 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4733 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4734 'b', varname, FALSE);
4735 if (v != NULL)
4736 {
4737 copy_tv(&v->di_tv, rettv);
4738 done = TRUE;
4739 }
4740 }
4741
4742 /* restore previous notion of curbuf */
4743 curbuf = save_curbuf;
4744 }
4745
4746 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4747 /* use the default value */
4748 copy_tv(&argvars[2], rettv);
4749
4750 --emsg_off;
4751}
4752
4753/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004754 * "getchangelist()" function
4755 */
4756 static void
4757f_getchangelist(typval_T *argvars, typval_T *rettv)
4758{
4759#ifdef FEAT_JUMPLIST
4760 buf_T *buf;
4761 int i;
4762 list_T *l;
4763 dict_T *d;
4764#endif
4765
4766 if (rettv_list_alloc(rettv) != OK)
4767 return;
4768
4769#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004770 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004771 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004772 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004773 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004774 if (buf == NULL)
4775 return;
4776
4777 l = list_alloc();
4778 if (l == NULL)
4779 return;
4780
4781 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4782 return;
4783 /*
4784 * The current window change list index tracks only the position in the
4785 * current buffer change list. For other buffers, use the change list
4786 * length as the current index.
4787 */
4788 list_append_number(rettv->vval.v_list,
4789 (varnumber_T)((buf == curwin->w_buffer)
4790 ? curwin->w_changelistidx : buf->b_changelistlen));
4791
4792 for (i = 0; i < buf->b_changelistlen; ++i)
4793 {
4794 if (buf->b_changelist[i].lnum == 0)
4795 continue;
4796 if ((d = dict_alloc()) == NULL)
4797 return;
4798 if (list_append_dict(l, d) == FAIL)
4799 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004800 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4801 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004802# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004803 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004804# endif
4805 }
4806#endif
4807}
4808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004809 * "getchar()" function
4810 */
4811 static void
4812f_getchar(typval_T *argvars, typval_T *rettv)
4813{
4814 varnumber_T n;
4815 int error = FALSE;
4816
Bram Moolenaar84d93902018-09-11 20:10:20 +02004817#ifdef MESSAGE_QUEUE
4818 // vpeekc() used to check for messages, but that caused problems, invoking
4819 // a callback where it was not expected. Some plugins use getchar(1) in a
4820 // loop to await a message, therefore make sure we check for messages here.
4821 parse_queued_messages();
4822#endif
4823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824 /* Position the cursor. Needed after a message that ends in a space. */
4825 windgoto(msg_row, msg_col);
4826
4827 ++no_mapping;
4828 ++allow_keys;
4829 for (;;)
4830 {
4831 if (argvars[0].v_type == VAR_UNKNOWN)
4832 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004833 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004834 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004835 /* getchar(1): only check if char avail */
4836 n = vpeekc_any();
4837 else if (error || vpeekc_any() == NUL)
4838 /* illegal argument or getchar(0) and no char avail: return zero */
4839 n = 0;
4840 else
4841 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004842 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004843
4844 if (n == K_IGNORE)
4845 continue;
4846 break;
4847 }
4848 --no_mapping;
4849 --allow_keys;
4850
4851 set_vim_var_nr(VV_MOUSE_WIN, 0);
4852 set_vim_var_nr(VV_MOUSE_WINID, 0);
4853 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4854 set_vim_var_nr(VV_MOUSE_COL, 0);
4855
4856 rettv->vval.v_number = n;
4857 if (IS_SPECIAL(n) || mod_mask != 0)
4858 {
4859 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4860 int i = 0;
4861
4862 /* Turn a special key into three bytes, plus modifier. */
4863 if (mod_mask != 0)
4864 {
4865 temp[i++] = K_SPECIAL;
4866 temp[i++] = KS_MODIFIER;
4867 temp[i++] = mod_mask;
4868 }
4869 if (IS_SPECIAL(n))
4870 {
4871 temp[i++] = K_SPECIAL;
4872 temp[i++] = K_SECOND(n);
4873 temp[i++] = K_THIRD(n);
4874 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875 else if (has_mbyte)
4876 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 else
4878 temp[i++] = n;
4879 temp[i++] = NUL;
4880 rettv->v_type = VAR_STRING;
4881 rettv->vval.v_string = vim_strsave(temp);
4882
4883#ifdef FEAT_MOUSE
4884 if (is_mouse_key(n))
4885 {
4886 int row = mouse_row;
4887 int col = mouse_col;
4888 win_T *win;
4889 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004890 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004891 int winnr = 1;
4892
4893 if (row >= 0 && col >= 0)
4894 {
4895 /* Find the window at the mouse coordinates and compute the
4896 * text position. */
4897 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004898 if (win == NULL)
4899 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901 for (wp = firstwin; wp != win; wp = wp->w_next)
4902 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004903 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4904 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4905 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4906 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4907 }
4908 }
4909#endif
4910 }
4911}
4912
4913/*
4914 * "getcharmod()" function
4915 */
4916 static void
4917f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4918{
4919 rettv->vval.v_number = mod_mask;
4920}
4921
4922/*
4923 * "getcharsearch()" function
4924 */
4925 static void
4926f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4927{
4928 if (rettv_dict_alloc(rettv) != FAIL)
4929 {
4930 dict_T *dict = rettv->vval.v_dict;
4931
Bram Moolenaare0be1672018-07-08 16:50:37 +02004932 dict_add_string(dict, "char", last_csearch());
4933 dict_add_number(dict, "forward", last_csearch_forward());
4934 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 }
4936}
4937
4938/*
4939 * "getcmdline()" function
4940 */
4941 static void
4942f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4943{
4944 rettv->v_type = VAR_STRING;
4945 rettv->vval.v_string = get_cmdline_str();
4946}
4947
4948/*
4949 * "getcmdpos()" function
4950 */
4951 static void
4952f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4953{
4954 rettv->vval.v_number = get_cmdline_pos() + 1;
4955}
4956
4957/*
4958 * "getcmdtype()" function
4959 */
4960 static void
4961f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4962{
4963 rettv->v_type = VAR_STRING;
4964 rettv->vval.v_string = alloc(2);
4965 if (rettv->vval.v_string != NULL)
4966 {
4967 rettv->vval.v_string[0] = get_cmdline_type();
4968 rettv->vval.v_string[1] = NUL;
4969 }
4970}
4971
4972/*
4973 * "getcmdwintype()" function
4974 */
4975 static void
4976f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4977{
4978 rettv->v_type = VAR_STRING;
4979 rettv->vval.v_string = NULL;
4980#ifdef FEAT_CMDWIN
4981 rettv->vval.v_string = alloc(2);
4982 if (rettv->vval.v_string != NULL)
4983 {
4984 rettv->vval.v_string[0] = cmdwin_type;
4985 rettv->vval.v_string[1] = NUL;
4986 }
4987#endif
4988}
4989
4990#if defined(FEAT_CMDL_COMPL)
4991/*
4992 * "getcompletion()" function
4993 */
4994 static void
4995f_getcompletion(typval_T *argvars, typval_T *rettv)
4996{
4997 char_u *pat;
4998 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004999 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005000 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5001 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005002
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005004 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005005
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005006 if (p_wic)
5007 options |= WILD_ICASE;
5008
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005009 /* For filtered results, 'wildignore' is used */
5010 if (!filtered)
5011 options |= WILD_KEEP_ALL;
5012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005014 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005015 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005016 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 if (xpc.xp_context == EXPAND_NOTHING)
5018 {
5019 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005020 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005021 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005022 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005023 return;
5024 }
5025
5026# if defined(FEAT_MENU)
5027 if (xpc.xp_context == EXPAND_MENUS)
5028 {
5029 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5030 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5031 }
5032# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005033#ifdef FEAT_CSCOPE
5034 if (xpc.xp_context == EXPAND_CSCOPE)
5035 {
5036 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5037 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5038 }
5039#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005040#ifdef FEAT_SIGNS
5041 if (xpc.xp_context == EXPAND_SIGN)
5042 {
5043 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5044 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5045 }
5046#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047
5048 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5049 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5050 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005051 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052
5053 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5054
5055 for (i = 0; i < xpc.xp_numfiles; i++)
5056 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5057 }
5058 vim_free(pat);
5059 ExpandCleanup(&xpc);
5060}
5061#endif
5062
5063/*
5064 * "getcwd()" function
5065 */
5066 static void
5067f_getcwd(typval_T *argvars, typval_T *rettv)
5068{
5069 win_T *wp = NULL;
5070 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005071 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005072
5073 rettv->v_type = VAR_STRING;
5074 rettv->vval.v_string = NULL;
5075
Bram Moolenaar54591292018-02-09 20:53:59 +01005076 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5077 global = TRUE;
5078 else
5079 wp = find_tabwin(&argvars[0], &argvars[1]);
5080
5081 if (wp != NULL && wp->w_localdir != NULL)
5082 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5083 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005085 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 rettv->vval.v_string = vim_strsave(globaldir);
5087 else
5088 {
5089 cwd = alloc(MAXPATHL);
5090 if (cwd != NULL)
5091 {
5092 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5093 rettv->vval.v_string = vim_strsave(cwd);
5094 vim_free(cwd);
5095 }
5096 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005098#ifdef BACKSLASH_IN_FILENAME
5099 if (rettv->vval.v_string != NULL)
5100 slash_adjust(rettv->vval.v_string);
5101#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102}
5103
5104/*
5105 * "getfontname()" function
5106 */
5107 static void
5108f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5109{
5110 rettv->v_type = VAR_STRING;
5111 rettv->vval.v_string = NULL;
5112#ifdef FEAT_GUI
5113 if (gui.in_use)
5114 {
5115 GuiFont font;
5116 char_u *name = NULL;
5117
5118 if (argvars[0].v_type == VAR_UNKNOWN)
5119 {
5120 /* Get the "Normal" font. Either the name saved by
5121 * hl_set_font_name() or from the font ID. */
5122 font = gui.norm_font;
5123 name = hl_get_font_name();
5124 }
5125 else
5126 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005127 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005128 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5129 return;
5130 font = gui_mch_get_font(name, FALSE);
5131 if (font == NOFONT)
5132 return; /* Invalid font name, return empty string. */
5133 }
5134 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5135 if (argvars[0].v_type != VAR_UNKNOWN)
5136 gui_mch_free_font(font);
5137 }
5138#endif
5139}
5140
5141/*
5142 * "getfperm({fname})" function
5143 */
5144 static void
5145f_getfperm(typval_T *argvars, typval_T *rettv)
5146{
5147 char_u *fname;
5148 stat_T st;
5149 char_u *perm = NULL;
5150 char_u flags[] = "rwx";
5151 int i;
5152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005153 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154
5155 rettv->v_type = VAR_STRING;
5156 if (mch_stat((char *)fname, &st) >= 0)
5157 {
5158 perm = vim_strsave((char_u *)"---------");
5159 if (perm != NULL)
5160 {
5161 for (i = 0; i < 9; i++)
5162 {
5163 if (st.st_mode & (1 << (8 - i)))
5164 perm[i] = flags[i % 3];
5165 }
5166 }
5167 }
5168 rettv->vval.v_string = perm;
5169}
5170
5171/*
5172 * "getfsize({fname})" function
5173 */
5174 static void
5175f_getfsize(typval_T *argvars, typval_T *rettv)
5176{
5177 char_u *fname;
5178 stat_T st;
5179
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005180 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005181
5182 rettv->v_type = VAR_NUMBER;
5183
5184 if (mch_stat((char *)fname, &st) >= 0)
5185 {
5186 if (mch_isdir(fname))
5187 rettv->vval.v_number = 0;
5188 else
5189 {
5190 rettv->vval.v_number = (varnumber_T)st.st_size;
5191
5192 /* non-perfect check for overflow */
5193 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5194 rettv->vval.v_number = -2;
5195 }
5196 }
5197 else
5198 rettv->vval.v_number = -1;
5199}
5200
5201/*
5202 * "getftime({fname})" function
5203 */
5204 static void
5205f_getftime(typval_T *argvars, typval_T *rettv)
5206{
5207 char_u *fname;
5208 stat_T st;
5209
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005210 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211
5212 if (mch_stat((char *)fname, &st) >= 0)
5213 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5214 else
5215 rettv->vval.v_number = -1;
5216}
5217
5218/*
5219 * "getftype({fname})" function
5220 */
5221 static void
5222f_getftype(typval_T *argvars, typval_T *rettv)
5223{
5224 char_u *fname;
5225 stat_T st;
5226 char_u *type = NULL;
5227 char *t;
5228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005229 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230
5231 rettv->v_type = VAR_STRING;
5232 if (mch_lstat((char *)fname, &st) >= 0)
5233 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005234 if (S_ISREG(st.st_mode))
5235 t = "file";
5236 else if (S_ISDIR(st.st_mode))
5237 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005238 else if (S_ISLNK(st.st_mode))
5239 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005240 else if (S_ISBLK(st.st_mode))
5241 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005242 else if (S_ISCHR(st.st_mode))
5243 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005244 else if (S_ISFIFO(st.st_mode))
5245 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005246 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005247 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248 else
5249 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005250 type = vim_strsave((char_u *)t);
5251 }
5252 rettv->vval.v_string = type;
5253}
5254
5255/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005256 * "getjumplist()" function
5257 */
5258 static void
5259f_getjumplist(typval_T *argvars, typval_T *rettv)
5260{
5261#ifdef FEAT_JUMPLIST
5262 win_T *wp;
5263 int i;
5264 list_T *l;
5265 dict_T *d;
5266#endif
5267
5268 if (rettv_list_alloc(rettv) != OK)
5269 return;
5270
5271#ifdef FEAT_JUMPLIST
5272 wp = find_tabwin(&argvars[0], &argvars[1]);
5273 if (wp == NULL)
5274 return;
5275
5276 l = list_alloc();
5277 if (l == NULL)
5278 return;
5279
5280 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5281 return;
5282 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5283
Bram Moolenaar48679742018-02-13 13:33:29 +01005284 cleanup_jumplist(wp, TRUE);
5285
Bram Moolenaar4f505882018-02-10 21:06:32 +01005286 for (i = 0; i < wp->w_jumplistlen; ++i)
5287 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005288 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5289 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005290 if ((d = dict_alloc()) == NULL)
5291 return;
5292 if (list_append_dict(l, d) == FAIL)
5293 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005294 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5295 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005296# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005297 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005298# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005299 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005300 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005301 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005302 }
5303#endif
5304}
5305
5306/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 * "getline(lnum, [end])" function
5308 */
5309 static void
5310f_getline(typval_T *argvars, typval_T *rettv)
5311{
5312 linenr_T lnum;
5313 linenr_T end;
5314 int retlist;
5315
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005316 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317 if (argvars[1].v_type == VAR_UNKNOWN)
5318 {
5319 end = 0;
5320 retlist = FALSE;
5321 }
5322 else
5323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005324 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 retlist = TRUE;
5326 }
5327
5328 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5329}
5330
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005331#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005332 static void
5333get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5334{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005335 if (what_arg->v_type == VAR_UNKNOWN)
5336 {
5337 if (rettv_list_alloc(rettv) == OK)
5338 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005339 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005340 }
5341 else
5342 {
5343 if (rettv_dict_alloc(rettv) == OK)
5344 if (is_qf || (wp != NULL))
5345 {
5346 if (what_arg->v_type == VAR_DICT)
5347 {
5348 dict_T *d = what_arg->vval.v_dict;
5349
5350 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005351 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005352 }
5353 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005354 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005355 }
5356 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005357}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005358#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005359
5360/*
5361 * "getloclist()" function
5362 */
5363 static void
5364f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5365{
5366#ifdef FEAT_QUICKFIX
5367 win_T *wp;
5368
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005369 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005370 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5371#endif
5372}
5373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005374/*
5375 * "getmatches()" function
5376 */
5377 static void
5378f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5379{
5380#ifdef FEAT_SEARCH_EXTRA
5381 dict_T *dict;
5382 matchitem_T *cur = curwin->w_match_head;
5383 int i;
5384
5385 if (rettv_list_alloc(rettv) == OK)
5386 {
5387 while (cur != NULL)
5388 {
5389 dict = dict_alloc();
5390 if (dict == NULL)
5391 return;
5392 if (cur->match.regprog == NULL)
5393 {
5394 /* match added with matchaddpos() */
5395 for (i = 0; i < MAXPOSMATCH; ++i)
5396 {
5397 llpos_T *llpos;
5398 char buf[6];
5399 list_T *l;
5400
5401 llpos = &cur->pos.pos[i];
5402 if (llpos->lnum == 0)
5403 break;
5404 l = list_alloc();
5405 if (l == NULL)
5406 break;
5407 list_append_number(l, (varnumber_T)llpos->lnum);
5408 if (llpos->col > 0)
5409 {
5410 list_append_number(l, (varnumber_T)llpos->col);
5411 list_append_number(l, (varnumber_T)llpos->len);
5412 }
5413 sprintf(buf, "pos%d", i + 1);
5414 dict_add_list(dict, buf, l);
5415 }
5416 }
5417 else
5418 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005419 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005420 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005421 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5422 dict_add_number(dict, "priority", (long)cur->priority);
5423 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar13505972019-01-24 15:04:48 +01005424# if defined(FEAT_CONCEAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005425 if (cur->conceal_char)
5426 {
5427 char_u buf[MB_MAXBYTES + 1];
5428
5429 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005430 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 }
5432# endif
5433 list_append_dict(rettv->vval.v_list, dict);
5434 cur = cur->next;
5435 }
5436 }
5437#endif
5438}
5439
5440/*
5441 * "getpid()" function
5442 */
5443 static void
5444f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5445{
5446 rettv->vval.v_number = mch_get_pid();
5447}
5448
5449 static void
5450getpos_both(
5451 typval_T *argvars,
5452 typval_T *rettv,
5453 int getcurpos)
5454{
5455 pos_T *fp;
5456 list_T *l;
5457 int fnum = -1;
5458
5459 if (rettv_list_alloc(rettv) == OK)
5460 {
5461 l = rettv->vval.v_list;
5462 if (getcurpos)
5463 fp = &curwin->w_cursor;
5464 else
5465 fp = var2fpos(&argvars[0], TRUE, &fnum);
5466 if (fnum != -1)
5467 list_append_number(l, (varnumber_T)fnum);
5468 else
5469 list_append_number(l, (varnumber_T)0);
5470 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5471 : (varnumber_T)0);
5472 list_append_number(l, (fp != NULL)
5473 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5474 : (varnumber_T)0);
5475 list_append_number(l,
5476#ifdef FEAT_VIRTUALEDIT
5477 (fp != NULL) ? (varnumber_T)fp->coladd :
5478#endif
5479 (varnumber_T)0);
5480 if (getcurpos)
5481 {
5482 update_curswant();
5483 list_append_number(l, curwin->w_curswant == MAXCOL ?
5484 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5485 }
5486 }
5487 else
5488 rettv->vval.v_number = FALSE;
5489}
5490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491/*
5492 * "getcurpos()" function
5493 */
5494 static void
5495f_getcurpos(typval_T *argvars, typval_T *rettv)
5496{
5497 getpos_both(argvars, rettv, TRUE);
5498}
5499
5500/*
5501 * "getpos(string)" function
5502 */
5503 static void
5504f_getpos(typval_T *argvars, typval_T *rettv)
5505{
5506 getpos_both(argvars, rettv, FALSE);
5507}
5508
5509/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005510 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511 */
5512 static void
5513f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5514{
5515#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005516 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517#endif
5518}
5519
5520/*
5521 * "getreg()" function
5522 */
5523 static void
5524f_getreg(typval_T *argvars, typval_T *rettv)
5525{
5526 char_u *strregname;
5527 int regname;
5528 int arg2 = FALSE;
5529 int return_list = FALSE;
5530 int error = FALSE;
5531
5532 if (argvars[0].v_type != VAR_UNKNOWN)
5533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005534 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535 error = strregname == NULL;
5536 if (argvars[1].v_type != VAR_UNKNOWN)
5537 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005538 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005540 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 }
5542 }
5543 else
5544 strregname = get_vim_var_str(VV_REG);
5545
5546 if (error)
5547 return;
5548
5549 regname = (strregname == NULL ? '"' : *strregname);
5550 if (regname == 0)
5551 regname = '"';
5552
5553 if (return_list)
5554 {
5555 rettv->v_type = VAR_LIST;
5556 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5557 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5558 if (rettv->vval.v_list == NULL)
5559 (void)rettv_list_alloc(rettv);
5560 else
5561 ++rettv->vval.v_list->lv_refcount;
5562 }
5563 else
5564 {
5565 rettv->v_type = VAR_STRING;
5566 rettv->vval.v_string = get_reg_contents(regname,
5567 arg2 ? GREG_EXPR_SRC : 0);
5568 }
5569}
5570
5571/*
5572 * "getregtype()" function
5573 */
5574 static void
5575f_getregtype(typval_T *argvars, typval_T *rettv)
5576{
5577 char_u *strregname;
5578 int regname;
5579 char_u buf[NUMBUFLEN + 2];
5580 long reglen = 0;
5581
5582 if (argvars[0].v_type != VAR_UNKNOWN)
5583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 if (strregname == NULL) /* type error; errmsg already given */
5586 {
5587 rettv->v_type = VAR_STRING;
5588 rettv->vval.v_string = NULL;
5589 return;
5590 }
5591 }
5592 else
5593 /* Default to v:register */
5594 strregname = get_vim_var_str(VV_REG);
5595
5596 regname = (strregname == NULL ? '"' : *strregname);
5597 if (regname == 0)
5598 regname = '"';
5599
5600 buf[0] = NUL;
5601 buf[1] = NUL;
5602 switch (get_reg_type(regname, &reglen))
5603 {
5604 case MLINE: buf[0] = 'V'; break;
5605 case MCHAR: buf[0] = 'v'; break;
5606 case MBLOCK:
5607 buf[0] = Ctrl_V;
5608 sprintf((char *)buf + 1, "%ld", reglen + 1);
5609 break;
5610 }
5611 rettv->v_type = VAR_STRING;
5612 rettv->vval.v_string = vim_strsave(buf);
5613}
5614
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005615/*
5616 * Returns information (variables, options, etc.) about a tab page
5617 * as a dictionary.
5618 */
5619 static dict_T *
5620get_tabpage_info(tabpage_T *tp, int tp_idx)
5621{
5622 win_T *wp;
5623 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005624 list_T *l;
5625
5626 dict = dict_alloc();
5627 if (dict == NULL)
5628 return NULL;
5629
Bram Moolenaare0be1672018-07-08 16:50:37 +02005630 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005631
5632 l = list_alloc();
5633 if (l != NULL)
5634 {
5635 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5636 wp; wp = wp->w_next)
5637 list_append_number(l, (varnumber_T)wp->w_id);
5638 dict_add_list(dict, "windows", l);
5639 }
5640
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005641 /* Make a reference to tabpage variables */
5642 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643
5644 return dict;
5645}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005646
5647/*
5648 * "gettabinfo()" function
5649 */
5650 static void
5651f_gettabinfo(typval_T *argvars, typval_T *rettv)
5652{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005653 tabpage_T *tp, *tparg = NULL;
5654 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005655 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005656
5657 if (rettv_list_alloc(rettv) != OK)
5658 return;
5659
5660 if (argvars[0].v_type != VAR_UNKNOWN)
5661 {
5662 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005663 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005664 if (tparg == NULL)
5665 return;
5666 }
5667
5668 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005669 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005670 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005671 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005672 if (tparg != NULL && tp != tparg)
5673 continue;
5674 d = get_tabpage_info(tp, tpnr);
5675 if (d != NULL)
5676 list_append_dict(rettv->vval.v_list, d);
5677 if (tparg != NULL)
5678 return;
5679 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005680}
5681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682/*
5683 * "gettabvar()" function
5684 */
5685 static void
5686f_gettabvar(typval_T *argvars, typval_T *rettv)
5687{
5688 win_T *oldcurwin;
5689 tabpage_T *tp, *oldtabpage;
5690 dictitem_T *v;
5691 char_u *varname;
5692 int done = FALSE;
5693
5694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = NULL;
5696
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005697 varname = tv_get_string_chk(&argvars[1]);
5698 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699 if (tp != NULL && varname != NULL)
5700 {
5701 /* Set tp to be our tabpage, temporarily. Also set the window to the
5702 * first window in the tabpage, otherwise the window is not valid. */
5703 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005704 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5705 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706 {
5707 /* look up the variable */
5708 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5709 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5710 if (v != NULL)
5711 {
5712 copy_tv(&v->di_tv, rettv);
5713 done = TRUE;
5714 }
5715 }
5716
5717 /* restore previous notion of curwin */
5718 restore_win(oldcurwin, oldtabpage, TRUE);
5719 }
5720
5721 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5722 copy_tv(&argvars[2], rettv);
5723}
5724
5725/*
5726 * "gettabwinvar()" function
5727 */
5728 static void
5729f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5730{
5731 getwinvar(argvars, rettv, 1);
5732}
5733
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005734/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005735 * "gettagstack()" function
5736 */
5737 static void
5738f_gettagstack(typval_T *argvars, typval_T *rettv)
5739{
5740 win_T *wp = curwin; // default is current window
5741
5742 if (rettv_dict_alloc(rettv) != OK)
5743 return;
5744
5745 if (argvars[0].v_type != VAR_UNKNOWN)
5746 {
5747 wp = find_win_by_nr_or_id(&argvars[0]);
5748 if (wp == NULL)
5749 return;
5750 }
5751
5752 get_tagstack(wp, rettv->vval.v_dict);
5753}
5754
5755/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005756 * Returns information about a window as a dictionary.
5757 */
5758 static dict_T *
5759get_win_info(win_T *wp, short tpnr, short winnr)
5760{
5761 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005762
5763 dict = dict_alloc();
5764 if (dict == NULL)
5765 return NULL;
5766
Bram Moolenaare0be1672018-07-08 16:50:37 +02005767 dict_add_number(dict, "tabnr", tpnr);
5768 dict_add_number(dict, "winnr", winnr);
5769 dict_add_number(dict, "winid", wp->w_id);
5770 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005771 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005772#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005773 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005774#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005775 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005776 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005777 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005778
Bram Moolenaar69905d12017-08-13 18:14:47 +02005779#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005780 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005781#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005782#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005783 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5784 dict_add_number(dict, "loclist",
5785 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005786#endif
5787
Bram Moolenaar30567352016-08-27 21:25:44 +02005788 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005789 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005790
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005791 return dict;
5792}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005793
5794/*
5795 * "getwininfo()" function
5796 */
5797 static void
5798f_getwininfo(typval_T *argvars, typval_T *rettv)
5799{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005800 tabpage_T *tp;
5801 win_T *wp = NULL, *wparg = NULL;
5802 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005803 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005804
5805 if (rettv_list_alloc(rettv) != OK)
5806 return;
5807
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808 if (argvars[0].v_type != VAR_UNKNOWN)
5809 {
5810 wparg = win_id2wp(argvars);
5811 if (wparg == NULL)
5812 return;
5813 }
5814
5815 /* Collect information about either all the windows across all the tab
5816 * pages or one particular window.
5817 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005818 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005819 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005820 tabnr++;
5821 winnr = 0;
5822 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005823 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005824 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005825 if (wparg != NULL && wp != wparg)
5826 continue;
5827 d = get_win_info(wp, tabnr, winnr);
5828 if (d != NULL)
5829 list_append_dict(rettv->vval.v_list, d);
5830 if (wparg != NULL)
5831 /* found information about a specific window */
5832 return;
5833 }
5834 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005835}
5836
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005837/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 * "win_findbuf()" function
5839 */
5840 static void
5841f_win_findbuf(typval_T *argvars, typval_T *rettv)
5842{
5843 if (rettv_list_alloc(rettv) != FAIL)
5844 win_findbuf(argvars, rettv->vval.v_list);
5845}
5846
5847/*
5848 * "win_getid()" function
5849 */
5850 static void
5851f_win_getid(typval_T *argvars, typval_T *rettv)
5852{
5853 rettv->vval.v_number = win_getid(argvars);
5854}
5855
5856/*
5857 * "win_gotoid()" function
5858 */
5859 static void
5860f_win_gotoid(typval_T *argvars, typval_T *rettv)
5861{
5862 rettv->vval.v_number = win_gotoid(argvars);
5863}
5864
5865/*
5866 * "win_id2tabwin()" function
5867 */
5868 static void
5869f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5870{
5871 if (rettv_list_alloc(rettv) != FAIL)
5872 win_id2tabwin(argvars, rettv->vval.v_list);
5873}
5874
5875/*
5876 * "win_id2win()" function
5877 */
5878 static void
5879f_win_id2win(typval_T *argvars, typval_T *rettv)
5880{
5881 rettv->vval.v_number = win_id2win(argvars);
5882}
5883
5884/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005885 * "win_screenpos()" function
5886 */
5887 static void
5888f_win_screenpos(typval_T *argvars, typval_T *rettv)
5889{
5890 win_T *wp;
5891
5892 if (rettv_list_alloc(rettv) == FAIL)
5893 return;
5894
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005895 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005896 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5897 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5898}
5899
5900/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005901 * "getwinpos({timeout})" function
5902 */
5903 static void
5904f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5905{
5906 int x = -1;
5907 int y = -1;
5908
5909 if (rettv_list_alloc(rettv) == FAIL)
5910 return;
5911#ifdef FEAT_GUI
5912 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005913 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005914# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5915 else
5916# endif
5917#endif
5918#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5919 {
5920 varnumber_T timeout = 100;
5921
5922 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005923 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005924 term_get_winpos(&x, &y, timeout);
5925 }
5926#endif
5927 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5928 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5929}
5930
5931
5932/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005933 * "getwinposx()" function
5934 */
5935 static void
5936f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5937{
5938 rettv->vval.v_number = -1;
5939#ifdef FEAT_GUI
5940 if (gui.in_use)
5941 {
5942 int x, y;
5943
5944 if (gui_mch_get_winpos(&x, &y) == OK)
5945 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005946 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005947 }
5948#endif
5949#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5950 {
5951 int x, y;
5952
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005953 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005954 rettv->vval.v_number = x;
5955 }
5956#endif
5957}
5958
5959/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005960 * "getwinposy()" function
5961 */
5962 static void
5963f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5964{
5965 rettv->vval.v_number = -1;
5966#ifdef FEAT_GUI
5967 if (gui.in_use)
5968 {
5969 int x, y;
5970
5971 if (gui_mch_get_winpos(&x, &y) == OK)
5972 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005973 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974 }
5975#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005976#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5977 {
5978 int x, y;
5979
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005980 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005981 rettv->vval.v_number = y;
5982 }
5983#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005984}
5985
5986/*
5987 * "getwinvar()" function
5988 */
5989 static void
5990f_getwinvar(typval_T *argvars, typval_T *rettv)
5991{
5992 getwinvar(argvars, rettv, 0);
5993}
5994
5995/*
5996 * "glob()" function
5997 */
5998 static void
5999f_glob(typval_T *argvars, typval_T *rettv)
6000{
6001 int options = WILD_SILENT|WILD_USE_NL;
6002 expand_T xpc;
6003 int error = FALSE;
6004
6005 /* When the optional second argument is non-zero, don't remove matches
6006 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6007 rettv->v_type = VAR_STRING;
6008 if (argvars[1].v_type != VAR_UNKNOWN)
6009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006010 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006011 options |= WILD_KEEP_ALL;
6012 if (argvars[2].v_type != VAR_UNKNOWN)
6013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006015 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006016 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017 }
6018 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006019 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006020 options |= WILD_ALLLINKS;
6021 }
6022 }
6023 if (!error)
6024 {
6025 ExpandInit(&xpc);
6026 xpc.xp_context = EXPAND_FILES;
6027 if (p_wic)
6028 options += WILD_ICASE;
6029 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006030 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 NULL, options, WILD_ALL);
6032 else if (rettv_list_alloc(rettv) != FAIL)
6033 {
6034 int i;
6035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006036 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006037 NULL, options, WILD_ALL_KEEP);
6038 for (i = 0; i < xpc.xp_numfiles; i++)
6039 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6040
6041 ExpandCleanup(&xpc);
6042 }
6043 }
6044 else
6045 rettv->vval.v_string = NULL;
6046}
6047
6048/*
6049 * "globpath()" function
6050 */
6051 static void
6052f_globpath(typval_T *argvars, typval_T *rettv)
6053{
6054 int flags = 0;
6055 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006056 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057 int error = FALSE;
6058 garray_T ga;
6059 int i;
6060
6061 /* When the optional second argument is non-zero, don't remove matches
6062 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6063 rettv->v_type = VAR_STRING;
6064 if (argvars[2].v_type != VAR_UNKNOWN)
6065 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006066 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006067 flags |= WILD_KEEP_ALL;
6068 if (argvars[3].v_type != VAR_UNKNOWN)
6069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006070 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006071 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006072 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 }
6074 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006075 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006076 flags |= WILD_ALLLINKS;
6077 }
6078 }
6079 if (file != NULL && !error)
6080 {
6081 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006082 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006083 if (rettv->v_type == VAR_STRING)
6084 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6085 else if (rettv_list_alloc(rettv) != FAIL)
6086 for (i = 0; i < ga.ga_len; ++i)
6087 list_append_string(rettv->vval.v_list,
6088 ((char_u **)(ga.ga_data))[i], -1);
6089 ga_clear_strings(&ga);
6090 }
6091 else
6092 rettv->vval.v_string = NULL;
6093}
6094
6095/*
6096 * "glob2regpat()" function
6097 */
6098 static void
6099f_glob2regpat(typval_T *argvars, typval_T *rettv)
6100{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006101 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006102
6103 rettv->v_type = VAR_STRING;
6104 rettv->vval.v_string = (pat == NULL)
6105 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6106}
6107
6108/* for VIM_VERSION_ defines */
6109#include "version.h"
6110
6111/*
6112 * "has()" function
6113 */
6114 static void
6115f_has(typval_T *argvars, typval_T *rettv)
6116{
6117 int i;
6118 char_u *name;
6119 int n = FALSE;
6120 static char *(has_list[]) =
6121 {
6122#ifdef AMIGA
6123 "amiga",
6124# ifdef FEAT_ARP
6125 "arp",
6126# endif
6127#endif
6128#ifdef __BEOS__
6129 "beos",
6130#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006131#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006132 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6133 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006134# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006135 "macunix", /* Mac OS X, with the darwin feature */
6136 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006137# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006138#endif
6139#ifdef __QNX__
6140 "qnx",
6141#endif
6142#ifdef UNIX
6143 "unix",
6144#endif
6145#ifdef VMS
6146 "vms",
6147#endif
6148#ifdef WIN32
6149 "win32",
6150#endif
6151#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6152 "win32unix",
6153#endif
6154#if defined(WIN64) || defined(_WIN64)
6155 "win64",
6156#endif
6157#ifdef EBCDIC
6158 "ebcdic",
6159#endif
6160#ifndef CASE_INSENSITIVE_FILENAME
6161 "fname_case",
6162#endif
6163#ifdef HAVE_ACL
6164 "acl",
6165#endif
6166#ifdef FEAT_ARABIC
6167 "arabic",
6168#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006169 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006170#ifdef FEAT_AUTOCHDIR
6171 "autochdir",
6172#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006173#ifdef FEAT_AUTOSERVERNAME
6174 "autoservername",
6175#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006176#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006177 "balloon_eval",
6178# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6179 "balloon_multiline",
6180# endif
6181#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006182#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006183 "balloon_eval_term",
6184#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006185#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6186 "builtin_terms",
6187# ifdef ALL_BUILTIN_TCAPS
6188 "all_builtin_terms",
6189# endif
6190#endif
6191#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6192 || defined(FEAT_GUI_W32) \
6193 || defined(FEAT_GUI_MOTIF))
6194 "browsefilter",
6195#endif
6196#ifdef FEAT_BYTEOFF
6197 "byte_offset",
6198#endif
6199#ifdef FEAT_JOB_CHANNEL
6200 "channel",
6201#endif
6202#ifdef FEAT_CINDENT
6203 "cindent",
6204#endif
6205#ifdef FEAT_CLIENTSERVER
6206 "clientserver",
6207#endif
6208#ifdef FEAT_CLIPBOARD
6209 "clipboard",
6210#endif
6211#ifdef FEAT_CMDL_COMPL
6212 "cmdline_compl",
6213#endif
6214#ifdef FEAT_CMDHIST
6215 "cmdline_hist",
6216#endif
6217#ifdef FEAT_COMMENTS
6218 "comments",
6219#endif
6220#ifdef FEAT_CONCEAL
6221 "conceal",
6222#endif
6223#ifdef FEAT_CRYPT
6224 "cryptv",
6225 "crypt-blowfish",
6226 "crypt-blowfish2",
6227#endif
6228#ifdef FEAT_CSCOPE
6229 "cscope",
6230#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006231 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006232#ifdef CURSOR_SHAPE
6233 "cursorshape",
6234#endif
6235#ifdef DEBUG
6236 "debug",
6237#endif
6238#ifdef FEAT_CON_DIALOG
6239 "dialog_con",
6240#endif
6241#ifdef FEAT_GUI_DIALOG
6242 "dialog_gui",
6243#endif
6244#ifdef FEAT_DIFF
6245 "diff",
6246#endif
6247#ifdef FEAT_DIGRAPHS
6248 "digraphs",
6249#endif
6250#ifdef FEAT_DIRECTX
6251 "directx",
6252#endif
6253#ifdef FEAT_DND
6254 "dnd",
6255#endif
6256#ifdef FEAT_EMACS_TAGS
6257 "emacs_tags",
6258#endif
6259 "eval", /* always present, of course! */
6260 "ex_extra", /* graduated feature */
6261#ifdef FEAT_SEARCH_EXTRA
6262 "extra_search",
6263#endif
6264#ifdef FEAT_FKMAP
6265 "farsi",
6266#endif
6267#ifdef FEAT_SEARCHPATH
6268 "file_in_path",
6269#endif
6270#ifdef FEAT_FILTERPIPE
6271 "filterpipe",
6272#endif
6273#ifdef FEAT_FIND_ID
6274 "find_in_path",
6275#endif
6276#ifdef FEAT_FLOAT
6277 "float",
6278#endif
6279#ifdef FEAT_FOLDING
6280 "folding",
6281#endif
6282#ifdef FEAT_FOOTER
6283 "footer",
6284#endif
6285#if !defined(USE_SYSTEM) && defined(UNIX)
6286 "fork",
6287#endif
6288#ifdef FEAT_GETTEXT
6289 "gettext",
6290#endif
6291#ifdef FEAT_GUI
6292 "gui",
6293#endif
6294#ifdef FEAT_GUI_ATHENA
6295# ifdef FEAT_GUI_NEXTAW
6296 "gui_neXtaw",
6297# else
6298 "gui_athena",
6299# endif
6300#endif
6301#ifdef FEAT_GUI_GTK
6302 "gui_gtk",
6303# ifdef USE_GTK3
6304 "gui_gtk3",
6305# else
6306 "gui_gtk2",
6307# endif
6308#endif
6309#ifdef FEAT_GUI_GNOME
6310 "gui_gnome",
6311#endif
6312#ifdef FEAT_GUI_MAC
6313 "gui_mac",
6314#endif
6315#ifdef FEAT_GUI_MOTIF
6316 "gui_motif",
6317#endif
6318#ifdef FEAT_GUI_PHOTON
6319 "gui_photon",
6320#endif
6321#ifdef FEAT_GUI_W32
6322 "gui_win32",
6323#endif
6324#ifdef FEAT_HANGULIN
6325 "hangul_input",
6326#endif
6327#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6328 "iconv",
6329#endif
6330#ifdef FEAT_INS_EXPAND
6331 "insert_expand",
6332#endif
6333#ifdef FEAT_JOB_CHANNEL
6334 "job",
6335#endif
6336#ifdef FEAT_JUMPLIST
6337 "jumplist",
6338#endif
6339#ifdef FEAT_KEYMAP
6340 "keymap",
6341#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006342 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343#ifdef FEAT_LANGMAP
6344 "langmap",
6345#endif
6346#ifdef FEAT_LIBCALL
6347 "libcall",
6348#endif
6349#ifdef FEAT_LINEBREAK
6350 "linebreak",
6351#endif
6352#ifdef FEAT_LISP
6353 "lispindent",
6354#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006355 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356#ifdef FEAT_LOCALMAP
6357 "localmap",
6358#endif
6359#ifdef FEAT_LUA
6360# ifndef DYNAMIC_LUA
6361 "lua",
6362# endif
6363#endif
6364#ifdef FEAT_MENU
6365 "menu",
6366#endif
6367#ifdef FEAT_SESSION
6368 "mksession",
6369#endif
6370#ifdef FEAT_MODIFY_FNAME
6371 "modify_fname",
6372#endif
6373#ifdef FEAT_MOUSE
6374 "mouse",
6375#endif
6376#ifdef FEAT_MOUSESHAPE
6377 "mouseshape",
6378#endif
6379#if defined(UNIX) || defined(VMS)
6380# ifdef FEAT_MOUSE_DEC
6381 "mouse_dec",
6382# endif
6383# ifdef FEAT_MOUSE_GPM
6384 "mouse_gpm",
6385# endif
6386# ifdef FEAT_MOUSE_JSB
6387 "mouse_jsbterm",
6388# endif
6389# ifdef FEAT_MOUSE_NET
6390 "mouse_netterm",
6391# endif
6392# ifdef FEAT_MOUSE_PTERM
6393 "mouse_pterm",
6394# endif
6395# ifdef FEAT_MOUSE_SGR
6396 "mouse_sgr",
6397# endif
6398# ifdef FEAT_SYSMOUSE
6399 "mouse_sysmouse",
6400# endif
6401# ifdef FEAT_MOUSE_URXVT
6402 "mouse_urxvt",
6403# endif
6404# ifdef FEAT_MOUSE_XTERM
6405 "mouse_xterm",
6406# endif
6407#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006409#ifdef FEAT_MBYTE_IME
6410 "multi_byte_ime",
6411#endif
6412#ifdef FEAT_MULTI_LANG
6413 "multi_lang",
6414#endif
6415#ifdef FEAT_MZSCHEME
6416#ifndef DYNAMIC_MZSCHEME
6417 "mzscheme",
6418#endif
6419#endif
6420#ifdef FEAT_NUM64
6421 "num64",
6422#endif
6423#ifdef FEAT_OLE
6424 "ole",
6425#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006426#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006428#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429#ifdef FEAT_PATH_EXTRA
6430 "path_extra",
6431#endif
6432#ifdef FEAT_PERL
6433#ifndef DYNAMIC_PERL
6434 "perl",
6435#endif
6436#endif
6437#ifdef FEAT_PERSISTENT_UNDO
6438 "persistent_undo",
6439#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006440#if defined(FEAT_PYTHON)
6441 "python_compiled",
6442# if defined(DYNAMIC_PYTHON)
6443 "python_dynamic",
6444# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006446 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006447# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006448#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006449#if defined(FEAT_PYTHON3)
6450 "python3_compiled",
6451# if defined(DYNAMIC_PYTHON3)
6452 "python3_dynamic",
6453# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006454 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006455 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006456# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457#endif
6458#ifdef FEAT_POSTSCRIPT
6459 "postscript",
6460#endif
6461#ifdef FEAT_PRINTER
6462 "printer",
6463#endif
6464#ifdef FEAT_PROFILE
6465 "profile",
6466#endif
6467#ifdef FEAT_RELTIME
6468 "reltime",
6469#endif
6470#ifdef FEAT_QUICKFIX
6471 "quickfix",
6472#endif
6473#ifdef FEAT_RIGHTLEFT
6474 "rightleft",
6475#endif
6476#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6477 "ruby",
6478#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006479 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006480#ifdef FEAT_CMDL_INFO
6481 "showcmd",
6482 "cmdline_info",
6483#endif
6484#ifdef FEAT_SIGNS
6485 "signs",
6486#endif
6487#ifdef FEAT_SMARTINDENT
6488 "smartindent",
6489#endif
6490#ifdef STARTUPTIME
6491 "startuptime",
6492#endif
6493#ifdef FEAT_STL_OPT
6494 "statusline",
6495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496#ifdef FEAT_NETBEANS_INTG
6497 "netbeans_intg",
6498#endif
6499#ifdef FEAT_SPELL
6500 "spell",
6501#endif
6502#ifdef FEAT_SYN_HL
6503 "syntax",
6504#endif
6505#if defined(USE_SYSTEM) || !defined(UNIX)
6506 "system",
6507#endif
6508#ifdef FEAT_TAG_BINS
6509 "tag_binary",
6510#endif
6511#ifdef FEAT_TAG_OLDSTATIC
6512 "tag_old_static",
6513#endif
6514#ifdef FEAT_TAG_ANYWHITE
6515 "tag_any_white",
6516#endif
6517#ifdef FEAT_TCL
6518# ifndef DYNAMIC_TCL
6519 "tcl",
6520# endif
6521#endif
6522#ifdef FEAT_TERMGUICOLORS
6523 "termguicolors",
6524#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006525#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006526 "terminal",
6527#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006528#ifdef TERMINFO
6529 "terminfo",
6530#endif
6531#ifdef FEAT_TERMRESPONSE
6532 "termresponse",
6533#endif
6534#ifdef FEAT_TEXTOBJ
6535 "textobjects",
6536#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006537#ifdef FEAT_TEXT_PROP
6538 "textprop",
6539#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006540#ifdef HAVE_TGETENT
6541 "tgetent",
6542#endif
6543#ifdef FEAT_TIMERS
6544 "timers",
6545#endif
6546#ifdef FEAT_TITLE
6547 "title",
6548#endif
6549#ifdef FEAT_TOOLBAR
6550 "toolbar",
6551#endif
6552#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6553 "unnamedplus",
6554#endif
6555#ifdef FEAT_USR_CMDS
6556 "user-commands", /* was accidentally included in 5.4 */
6557 "user_commands",
6558#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006559#ifdef FEAT_VARTABS
6560 "vartabs",
6561#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006562#ifdef FEAT_VIMINFO
6563 "viminfo",
6564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#ifdef FEAT_VIRTUALEDIT
6567 "virtualedit",
6568#endif
6569 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006572#ifdef FEAT_VTP
6573 "vtp",
6574#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006575#ifdef FEAT_WILDIGN
6576 "wildignore",
6577#endif
6578#ifdef FEAT_WILDMENU
6579 "wildmenu",
6580#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582#ifdef FEAT_WAK
6583 "winaltkeys",
6584#endif
6585#ifdef FEAT_WRITEBACKUP
6586 "writebackup",
6587#endif
6588#ifdef FEAT_XIM
6589 "xim",
6590#endif
6591#ifdef FEAT_XFONTSET
6592 "xfontset",
6593#endif
6594#ifdef FEAT_XPM_W32
6595 "xpm",
6596 "xpm_w32", /* for backward compatibility */
6597#else
6598# if defined(HAVE_XPM)
6599 "xpm",
6600# endif
6601#endif
6602#ifdef USE_XSMP
6603 "xsmp",
6604#endif
6605#ifdef USE_XSMP_INTERACT
6606 "xsmp_interact",
6607#endif
6608#ifdef FEAT_XCLIPBOARD
6609 "xterm_clipboard",
6610#endif
6611#ifdef FEAT_XTERM_SAVE
6612 "xterm_save",
6613#endif
6614#if defined(UNIX) && defined(FEAT_X11)
6615 "X11",
6616#endif
6617 NULL
6618 };
6619
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006620 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006621 for (i = 0; has_list[i] != NULL; ++i)
6622 if (STRICMP(name, has_list[i]) == 0)
6623 {
6624 n = TRUE;
6625 break;
6626 }
6627
6628 if (n == FALSE)
6629 {
6630 if (STRNICMP(name, "patch", 5) == 0)
6631 {
6632 if (name[5] == '-'
6633 && STRLEN(name) >= 11
6634 && vim_isdigit(name[6])
6635 && vim_isdigit(name[8])
6636 && vim_isdigit(name[10]))
6637 {
6638 int major = atoi((char *)name + 6);
6639 int minor = atoi((char *)name + 8);
6640
6641 /* Expect "patch-9.9.01234". */
6642 n = (major < VIM_VERSION_MAJOR
6643 || (major == VIM_VERSION_MAJOR
6644 && (minor < VIM_VERSION_MINOR
6645 || (minor == VIM_VERSION_MINOR
6646 && has_patch(atoi((char *)name + 10))))));
6647 }
6648 else
6649 n = has_patch(atoi((char *)name + 5));
6650 }
6651 else if (STRICMP(name, "vim_starting") == 0)
6652 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006653 else if (STRICMP(name, "ttyin") == 0)
6654 n = mch_input_isatty();
6655 else if (STRICMP(name, "ttyout") == 0)
6656 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657 else if (STRICMP(name, "multi_byte_encoding") == 0)
6658 n = has_mbyte;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6660 else if (STRICMP(name, "balloon_multiline") == 0)
6661 n = multiline_balloon_available();
6662#endif
6663#ifdef DYNAMIC_TCL
6664 else if (STRICMP(name, "tcl") == 0)
6665 n = tcl_enabled(FALSE);
6666#endif
6667#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6668 else if (STRICMP(name, "iconv") == 0)
6669 n = iconv_enabled(FALSE);
6670#endif
6671#ifdef DYNAMIC_LUA
6672 else if (STRICMP(name, "lua") == 0)
6673 n = lua_enabled(FALSE);
6674#endif
6675#ifdef DYNAMIC_MZSCHEME
6676 else if (STRICMP(name, "mzscheme") == 0)
6677 n = mzscheme_enabled(FALSE);
6678#endif
6679#ifdef DYNAMIC_RUBY
6680 else if (STRICMP(name, "ruby") == 0)
6681 n = ruby_enabled(FALSE);
6682#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006683#ifdef DYNAMIC_PYTHON
6684 else if (STRICMP(name, "python") == 0)
6685 n = python_enabled(FALSE);
6686#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687#ifdef DYNAMIC_PYTHON3
6688 else if (STRICMP(name, "python3") == 0)
6689 n = python3_enabled(FALSE);
6690#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006691#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6692 else if (STRICMP(name, "pythonx") == 0)
6693 {
6694# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6695 if (p_pyx == 0)
6696 n = python3_enabled(FALSE) || python_enabled(FALSE);
6697 else if (p_pyx == 3)
6698 n = python3_enabled(FALSE);
6699 else if (p_pyx == 2)
6700 n = python_enabled(FALSE);
6701# elif defined(DYNAMIC_PYTHON)
6702 n = python_enabled(FALSE);
6703# elif defined(DYNAMIC_PYTHON3)
6704 n = python3_enabled(FALSE);
6705# endif
6706 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707#endif
6708#ifdef DYNAMIC_PERL
6709 else if (STRICMP(name, "perl") == 0)
6710 n = perl_enabled(FALSE);
6711#endif
6712#ifdef FEAT_GUI
6713 else if (STRICMP(name, "gui_running") == 0)
6714 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715# ifdef FEAT_BROWSE
6716 else if (STRICMP(name, "browse") == 0)
6717 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6718# endif
6719#endif
6720#ifdef FEAT_SYN_HL
6721 else if (STRICMP(name, "syntax_items") == 0)
6722 n = syntax_present(curwin);
6723#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006724#ifdef FEAT_VTP
6725 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006726 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727#endif
6728#ifdef FEAT_NETBEANS_INTG
6729 else if (STRICMP(name, "netbeans_enabled") == 0)
6730 n = netbeans_active();
6731#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006732#if defined(FEAT_TERMINAL) && defined(WIN3264)
6733 else if (STRICMP(name, "terminal") == 0)
6734 n = terminal_enabled();
6735#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006736 }
6737
6738 rettv->vval.v_number = n;
6739}
6740
6741/*
6742 * "has_key()" function
6743 */
6744 static void
6745f_has_key(typval_T *argvars, typval_T *rettv)
6746{
6747 if (argvars[0].v_type != VAR_DICT)
6748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006749 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 return;
6751 }
6752 if (argvars[0].vval.v_dict == NULL)
6753 return;
6754
6755 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006756 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757}
6758
6759/*
6760 * "haslocaldir()" function
6761 */
6762 static void
6763f_haslocaldir(typval_T *argvars, typval_T *rettv)
6764{
6765 win_T *wp = NULL;
6766
6767 wp = find_tabwin(&argvars[0], &argvars[1]);
6768 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6769}
6770
6771/*
6772 * "hasmapto()" function
6773 */
6774 static void
6775f_hasmapto(typval_T *argvars, typval_T *rettv)
6776{
6777 char_u *name;
6778 char_u *mode;
6779 char_u buf[NUMBUFLEN];
6780 int abbr = FALSE;
6781
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006782 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783 if (argvars[1].v_type == VAR_UNKNOWN)
6784 mode = (char_u *)"nvo";
6785 else
6786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006787 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006789 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790 }
6791
6792 if (map_to_exists(name, mode, abbr))
6793 rettv->vval.v_number = TRUE;
6794 else
6795 rettv->vval.v_number = FALSE;
6796}
6797
6798/*
6799 * "histadd()" function
6800 */
6801 static void
6802f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6803{
6804#ifdef FEAT_CMDHIST
6805 int histype;
6806 char_u *str;
6807 char_u buf[NUMBUFLEN];
6808#endif
6809
6810 rettv->vval.v_number = FALSE;
6811 if (check_restricted() || check_secure())
6812 return;
6813#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006814 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 histype = str != NULL ? get_histtype(str) : -1;
6816 if (histype >= 0)
6817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006818 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819 if (*str != NUL)
6820 {
6821 init_history();
6822 add_to_history(histype, str, FALSE, NUL);
6823 rettv->vval.v_number = TRUE;
6824 return;
6825 }
6826 }
6827#endif
6828}
6829
6830/*
6831 * "histdel()" function
6832 */
6833 static void
6834f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6835{
6836#ifdef FEAT_CMDHIST
6837 int n;
6838 char_u buf[NUMBUFLEN];
6839 char_u *str;
6840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006841 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006842 if (str == NULL)
6843 n = 0;
6844 else if (argvars[1].v_type == VAR_UNKNOWN)
6845 /* only one argument: clear entire history */
6846 n = clr_history(get_histtype(str));
6847 else if (argvars[1].v_type == VAR_NUMBER)
6848 /* index given: remove that entry */
6849 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006850 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006851 else
6852 /* string given: remove all matching entries */
6853 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006854 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855 rettv->vval.v_number = n;
6856#endif
6857}
6858
6859/*
6860 * "histget()" function
6861 */
6862 static void
6863f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6864{
6865#ifdef FEAT_CMDHIST
6866 int type;
6867 int idx;
6868 char_u *str;
6869
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006870 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006871 if (str == NULL)
6872 rettv->vval.v_string = NULL;
6873 else
6874 {
6875 type = get_histtype(str);
6876 if (argvars[1].v_type == VAR_UNKNOWN)
6877 idx = get_history_idx(type);
6878 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006879 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006880 /* -1 on type error */
6881 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6882 }
6883#else
6884 rettv->vval.v_string = NULL;
6885#endif
6886 rettv->v_type = VAR_STRING;
6887}
6888
6889/*
6890 * "histnr()" function
6891 */
6892 static void
6893f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6894{
6895 int i;
6896
6897#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899
6900 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6901 if (i >= HIST_CMD && i < HIST_COUNT)
6902 i = get_history_idx(i);
6903 else
6904#endif
6905 i = -1;
6906 rettv->vval.v_number = i;
6907}
6908
6909/*
6910 * "highlightID(name)" function
6911 */
6912 static void
6913f_hlID(typval_T *argvars, typval_T *rettv)
6914{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006915 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006916}
6917
6918/*
6919 * "highlight_exists()" function
6920 */
6921 static void
6922f_hlexists(typval_T *argvars, typval_T *rettv)
6923{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925}
6926
6927/*
6928 * "hostname()" function
6929 */
6930 static void
6931f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6932{
6933 char_u hostname[256];
6934
6935 mch_get_host_name(hostname, 256);
6936 rettv->v_type = VAR_STRING;
6937 rettv->vval.v_string = vim_strsave(hostname);
6938}
6939
6940/*
6941 * iconv() function
6942 */
6943 static void
6944f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6945{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006946 char_u buf1[NUMBUFLEN];
6947 char_u buf2[NUMBUFLEN];
6948 char_u *from, *to, *str;
6949 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950
6951 rettv->v_type = VAR_STRING;
6952 rettv->vval.v_string = NULL;
6953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006954 str = tv_get_string(&argvars[0]);
6955 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6956 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 vimconv.vc_type = CONV_NONE;
6958 convert_setup(&vimconv, from, to);
6959
6960 /* If the encodings are equal, no conversion needed. */
6961 if (vimconv.vc_type == CONV_NONE)
6962 rettv->vval.v_string = vim_strsave(str);
6963 else
6964 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6965
6966 convert_setup(&vimconv, NULL, NULL);
6967 vim_free(from);
6968 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969}
6970
6971/*
6972 * "indent()" function
6973 */
6974 static void
6975f_indent(typval_T *argvars, typval_T *rettv)
6976{
6977 linenr_T lnum;
6978
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6981 rettv->vval.v_number = get_indent_lnum(lnum);
6982 else
6983 rettv->vval.v_number = -1;
6984}
6985
6986/*
6987 * "index()" function
6988 */
6989 static void
6990f_index(typval_T *argvars, typval_T *rettv)
6991{
6992 list_T *l;
6993 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006994 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006995 long idx = 0;
6996 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006997 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006998
6999 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007000 if (argvars[0].v_type == VAR_BLOB)
7001 {
7002 typval_T tv;
7003 int start = 0;
7004
7005 if (argvars[2].v_type != VAR_UNKNOWN)
7006 {
7007 start = tv_get_number_chk(&argvars[2], &error);
7008 if (error)
7009 return;
7010 }
7011 b = argvars[0].vval.v_blob;
7012 if (b == NULL)
7013 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007014 if (start < 0)
7015 {
7016 start = blob_len(b) + start;
7017 if (start < 0)
7018 start = 0;
7019 }
7020
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007021 for (idx = start; idx < blob_len(b); ++idx)
7022 {
7023 tv.v_type = VAR_NUMBER;
7024 tv.vval.v_number = blob_get(b, idx);
7025 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7026 {
7027 rettv->vval.v_number = idx;
7028 return;
7029 }
7030 }
7031 return;
7032 }
7033 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007035 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 return;
7037 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007038
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039 l = argvars[0].vval.v_list;
7040 if (l != NULL)
7041 {
7042 item = l->lv_first;
7043 if (argvars[2].v_type != VAR_UNKNOWN)
7044 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 /* Start at specified item. Use the cached index that list_find()
7046 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007047 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 idx = l->lv_idx;
7049 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007050 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007051 if (error)
7052 item = NULL;
7053 }
7054
7055 for ( ; item != NULL; item = item->li_next, ++idx)
7056 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7057 {
7058 rettv->vval.v_number = idx;
7059 break;
7060 }
7061 }
7062}
7063
7064static int inputsecret_flag = 0;
7065
7066/*
7067 * "input()" function
7068 * Also handles inputsecret() when inputsecret is set.
7069 */
7070 static void
7071f_input(typval_T *argvars, typval_T *rettv)
7072{
7073 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7074}
7075
7076/*
7077 * "inputdialog()" function
7078 */
7079 static void
7080f_inputdialog(typval_T *argvars, typval_T *rettv)
7081{
7082#if defined(FEAT_GUI_TEXTDIALOG)
7083 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7084 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7085 {
7086 char_u *message;
7087 char_u buf[NUMBUFLEN];
7088 char_u *defstr = (char_u *)"";
7089
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007090 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007092 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7094 else
7095 IObuff[0] = NUL;
7096 if (message != NULL && defstr != NULL
7097 && do_dialog(VIM_QUESTION, NULL, message,
7098 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7099 rettv->vval.v_string = vim_strsave(IObuff);
7100 else
7101 {
7102 if (message != NULL && defstr != NULL
7103 && argvars[1].v_type != VAR_UNKNOWN
7104 && argvars[2].v_type != VAR_UNKNOWN)
7105 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007106 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 else
7108 rettv->vval.v_string = NULL;
7109 }
7110 rettv->v_type = VAR_STRING;
7111 }
7112 else
7113#endif
7114 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7115}
7116
7117/*
7118 * "inputlist()" function
7119 */
7120 static void
7121f_inputlist(typval_T *argvars, typval_T *rettv)
7122{
7123 listitem_T *li;
7124 int selected;
7125 int mouse_used;
7126
7127#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007128 /* While starting up, there is no place to enter text. When running tests
7129 * with --not-a-term we assume feedkeys() will be used. */
7130 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 return;
7132#endif
7133 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007135 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 return;
7137 }
7138
7139 msg_start();
7140 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7141 lines_left = Rows; /* avoid more prompt */
7142 msg_scroll = TRUE;
7143 msg_clr_eos();
7144
7145 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7146 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007147 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148 msg_putchar('\n');
7149 }
7150
7151 /* Ask for choice. */
7152 selected = prompt_for_number(&mouse_used);
7153 if (mouse_used)
7154 selected -= lines_left;
7155
7156 rettv->vval.v_number = selected;
7157}
7158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7160
7161/*
7162 * "inputrestore()" function
7163 */
7164 static void
7165f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7166{
7167 if (ga_userinput.ga_len > 0)
7168 {
7169 --ga_userinput.ga_len;
7170 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7171 + ga_userinput.ga_len);
7172 /* default return is zero == OK */
7173 }
7174 else if (p_verbose > 1)
7175 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007176 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177 rettv->vval.v_number = 1; /* Failed */
7178 }
7179}
7180
7181/*
7182 * "inputsave()" function
7183 */
7184 static void
7185f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7186{
7187 /* Add an entry to the stack of typeahead storage. */
7188 if (ga_grow(&ga_userinput, 1) == OK)
7189 {
7190 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7191 + ga_userinput.ga_len);
7192 ++ga_userinput.ga_len;
7193 /* default return is zero == OK */
7194 }
7195 else
7196 rettv->vval.v_number = 1; /* Failed */
7197}
7198
7199/*
7200 * "inputsecret()" function
7201 */
7202 static void
7203f_inputsecret(typval_T *argvars, typval_T *rettv)
7204{
7205 ++cmdline_star;
7206 ++inputsecret_flag;
7207 f_input(argvars, rettv);
7208 --cmdline_star;
7209 --inputsecret_flag;
7210}
7211
7212/*
7213 * "insert()" function
7214 */
7215 static void
7216f_insert(typval_T *argvars, typval_T *rettv)
7217{
7218 long before = 0;
7219 listitem_T *item;
7220 list_T *l;
7221 int error = FALSE;
7222
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007223 if (argvars[0].v_type == VAR_BLOB)
7224 {
7225 int val, len;
7226 char_u *p;
7227
7228 len = blob_len(argvars[0].vval.v_blob);
7229 if (argvars[2].v_type != VAR_UNKNOWN)
7230 {
7231 before = (long)tv_get_number_chk(&argvars[2], &error);
7232 if (error)
7233 return; // type error; errmsg already given
7234 if (before < 0 || before > len)
7235 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007236 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007237 return;
7238 }
7239 }
7240 val = tv_get_number_chk(&argvars[1], &error);
7241 if (error)
7242 return;
7243 if (val < 0 || val > 255)
7244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007245 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 return;
7247 }
7248
7249 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7250 return;
7251 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7252 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7253 *(p + before) = val;
7254 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7255
7256 copy_tv(&argvars[0], rettv);
7257 }
7258 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007259 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007260 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7261 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007262 {
7263 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007264 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007265 if (error)
7266 return; /* type error; errmsg already given */
7267
7268 if (before == l->lv_len)
7269 item = NULL;
7270 else
7271 {
7272 item = list_find(l, before);
7273 if (item == NULL)
7274 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007275 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 l = NULL;
7277 }
7278 }
7279 if (l != NULL)
7280 {
7281 list_insert_tv(l, &argvars[1], item);
7282 copy_tv(&argvars[0], rettv);
7283 }
7284 }
7285}
7286
7287/*
7288 * "invert(expr)" function
7289 */
7290 static void
7291f_invert(typval_T *argvars, typval_T *rettv)
7292{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007293 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294}
7295
7296/*
7297 * "isdirectory()" function
7298 */
7299 static void
7300f_isdirectory(typval_T *argvars, typval_T *rettv)
7301{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007302 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303}
7304
7305/*
7306 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7307 * or it refers to a List or Dictionary that is locked.
7308 */
7309 static int
7310tv_islocked(typval_T *tv)
7311{
7312 return (tv->v_lock & VAR_LOCKED)
7313 || (tv->v_type == VAR_LIST
7314 && tv->vval.v_list != NULL
7315 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7316 || (tv->v_type == VAR_DICT
7317 && tv->vval.v_dict != NULL
7318 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7319}
7320
7321/*
7322 * "islocked()" function
7323 */
7324 static void
7325f_islocked(typval_T *argvars, typval_T *rettv)
7326{
7327 lval_T lv;
7328 char_u *end;
7329 dictitem_T *di;
7330
7331 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007332 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007333 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 if (end != NULL && lv.ll_name != NULL)
7335 {
7336 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007337 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 else
7339 {
7340 if (lv.ll_tv == NULL)
7341 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007342 di = find_var(lv.ll_name, NULL, TRUE);
7343 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007345 /* Consider a variable locked when:
7346 * 1. the variable itself is locked
7347 * 2. the value of the variable is locked.
7348 * 3. the List or Dict value is locked.
7349 */
7350 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7351 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 }
7353 }
7354 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007355 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007356 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 else if (lv.ll_list != NULL)
7359 /* List item. */
7360 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7361 else
7362 /* Dictionary item. */
7363 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7364 }
7365 }
7366
7367 clear_lval(&lv);
7368}
7369
7370#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7371/*
7372 * "isnan()" function
7373 */
7374 static void
7375f_isnan(typval_T *argvars, typval_T *rettv)
7376{
7377 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7378 && isnan(argvars[0].vval.v_float);
7379}
7380#endif
7381
7382/*
7383 * "items(dict)" function
7384 */
7385 static void
7386f_items(typval_T *argvars, typval_T *rettv)
7387{
7388 dict_list(argvars, rettv, 2);
7389}
7390
7391#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7392/*
7393 * Get the job from the argument.
7394 * Returns NULL if the job is invalid.
7395 */
7396 static job_T *
7397get_job_arg(typval_T *tv)
7398{
7399 job_T *job;
7400
7401 if (tv->v_type != VAR_JOB)
7402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007403 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007404 return NULL;
7405 }
7406 job = tv->vval.v_job;
7407
7408 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007409 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007410 return job;
7411}
7412
7413/*
7414 * "job_getchannel()" function
7415 */
7416 static void
7417f_job_getchannel(typval_T *argvars, typval_T *rettv)
7418{
7419 job_T *job = get_job_arg(&argvars[0]);
7420
7421 if (job != NULL)
7422 {
7423 rettv->v_type = VAR_CHANNEL;
7424 rettv->vval.v_channel = job->jv_channel;
7425 if (job->jv_channel != NULL)
7426 ++job->jv_channel->ch_refcount;
7427 }
7428}
7429
7430/*
7431 * "job_info()" function
7432 */
7433 static void
7434f_job_info(typval_T *argvars, typval_T *rettv)
7435{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007436 if (argvars[0].v_type != VAR_UNKNOWN)
7437 {
7438 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007440 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7441 job_info(job, rettv->vval.v_dict);
7442 }
7443 else if (rettv_list_alloc(rettv) == OK)
7444 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007445}
7446
7447/*
7448 * "job_setoptions()" function
7449 */
7450 static void
7451f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7452{
7453 job_T *job = get_job_arg(&argvars[0]);
7454 jobopt_T opt;
7455
7456 if (job == NULL)
7457 return;
7458 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007459 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007460 job_set_options(job, &opt);
7461 free_job_options(&opt);
7462}
7463
7464/*
7465 * "job_start()" function
7466 */
7467 static void
7468f_job_start(typval_T *argvars, typval_T *rettv)
7469{
7470 rettv->v_type = VAR_JOB;
7471 if (check_restricted() || check_secure())
7472 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007473 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474}
7475
7476/*
7477 * "job_status()" function
7478 */
7479 static void
7480f_job_status(typval_T *argvars, typval_T *rettv)
7481{
7482 job_T *job = get_job_arg(&argvars[0]);
7483
7484 if (job != NULL)
7485 {
7486 rettv->v_type = VAR_STRING;
7487 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7488 }
7489}
7490
7491/*
7492 * "job_stop()" function
7493 */
7494 static void
7495f_job_stop(typval_T *argvars, typval_T *rettv)
7496{
7497 job_T *job = get_job_arg(&argvars[0]);
7498
7499 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007500 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501}
7502#endif
7503
7504/*
7505 * "join()" function
7506 */
7507 static void
7508f_join(typval_T *argvars, typval_T *rettv)
7509{
7510 garray_T ga;
7511 char_u *sep;
7512
7513 if (argvars[0].v_type != VAR_LIST)
7514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007515 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007516 return;
7517 }
7518 if (argvars[0].vval.v_list == NULL)
7519 return;
7520 if (argvars[1].v_type == VAR_UNKNOWN)
7521 sep = (char_u *)" ";
7522 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007523 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524
7525 rettv->v_type = VAR_STRING;
7526
7527 if (sep != NULL)
7528 {
7529 ga_init2(&ga, (int)sizeof(char), 80);
7530 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7531 ga_append(&ga, NUL);
7532 rettv->vval.v_string = (char_u *)ga.ga_data;
7533 }
7534 else
7535 rettv->vval.v_string = NULL;
7536}
7537
7538/*
7539 * "js_decode()" function
7540 */
7541 static void
7542f_js_decode(typval_T *argvars, typval_T *rettv)
7543{
7544 js_read_T reader;
7545
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547 reader.js_fill = NULL;
7548 reader.js_used = 0;
7549 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007551}
7552
7553/*
7554 * "js_encode()" function
7555 */
7556 static void
7557f_js_encode(typval_T *argvars, typval_T *rettv)
7558{
7559 rettv->v_type = VAR_STRING;
7560 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7561}
7562
7563/*
7564 * "json_decode()" function
7565 */
7566 static void
7567f_json_decode(typval_T *argvars, typval_T *rettv)
7568{
7569 js_read_T reader;
7570
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007571 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 reader.js_fill = NULL;
7573 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007574 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007575}
7576
7577/*
7578 * "json_encode()" function
7579 */
7580 static void
7581f_json_encode(typval_T *argvars, typval_T *rettv)
7582{
7583 rettv->v_type = VAR_STRING;
7584 rettv->vval.v_string = json_encode(&argvars[0], 0);
7585}
7586
7587/*
7588 * "keys()" function
7589 */
7590 static void
7591f_keys(typval_T *argvars, typval_T *rettv)
7592{
7593 dict_list(argvars, rettv, 0);
7594}
7595
7596/*
7597 * "last_buffer_nr()" function.
7598 */
7599 static void
7600f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7601{
7602 int n = 0;
7603 buf_T *buf;
7604
Bram Moolenaar29323592016-07-24 22:04:11 +02007605 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 if (n < buf->b_fnum)
7607 n = buf->b_fnum;
7608
7609 rettv->vval.v_number = n;
7610}
7611
7612/*
7613 * "len()" function
7614 */
7615 static void
7616f_len(typval_T *argvars, typval_T *rettv)
7617{
7618 switch (argvars[0].v_type)
7619 {
7620 case VAR_STRING:
7621 case VAR_NUMBER:
7622 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007623 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007624 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007625 case VAR_BLOB:
7626 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7627 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007628 case VAR_LIST:
7629 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7630 break;
7631 case VAR_DICT:
7632 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7633 break;
7634 case VAR_UNKNOWN:
7635 case VAR_SPECIAL:
7636 case VAR_FLOAT:
7637 case VAR_FUNC:
7638 case VAR_PARTIAL:
7639 case VAR_JOB:
7640 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 break;
7643 }
7644}
7645
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007646 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007647libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648{
7649#ifdef FEAT_LIBCALL
7650 char_u *string_in;
7651 char_u **string_result;
7652 int nr_result;
7653#endif
7654
7655 rettv->v_type = type;
7656 if (type != VAR_NUMBER)
7657 rettv->vval.v_string = NULL;
7658
7659 if (check_restricted() || check_secure())
7660 return;
7661
7662#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007663 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007664 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7665 {
7666 string_in = NULL;
7667 if (argvars[2].v_type == VAR_STRING)
7668 string_in = argvars[2].vval.v_string;
7669 if (type == VAR_NUMBER)
7670 string_result = NULL;
7671 else
7672 string_result = &rettv->vval.v_string;
7673 if (mch_libcall(argvars[0].vval.v_string,
7674 argvars[1].vval.v_string,
7675 string_in,
7676 argvars[2].vval.v_number,
7677 string_result,
7678 &nr_result) == OK
7679 && type == VAR_NUMBER)
7680 rettv->vval.v_number = nr_result;
7681 }
7682#endif
7683}
7684
7685/*
7686 * "libcall()" function
7687 */
7688 static void
7689f_libcall(typval_T *argvars, typval_T *rettv)
7690{
7691 libcall_common(argvars, rettv, VAR_STRING);
7692}
7693
7694/*
7695 * "libcallnr()" function
7696 */
7697 static void
7698f_libcallnr(typval_T *argvars, typval_T *rettv)
7699{
7700 libcall_common(argvars, rettv, VAR_NUMBER);
7701}
7702
7703/*
7704 * "line(string)" function
7705 */
7706 static void
7707f_line(typval_T *argvars, typval_T *rettv)
7708{
7709 linenr_T lnum = 0;
7710 pos_T *fp;
7711 int fnum;
7712
7713 fp = var2fpos(&argvars[0], TRUE, &fnum);
7714 if (fp != NULL)
7715 lnum = fp->lnum;
7716 rettv->vval.v_number = lnum;
7717}
7718
7719/*
7720 * "line2byte(lnum)" function
7721 */
7722 static void
7723f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7724{
7725#ifndef FEAT_BYTEOFF
7726 rettv->vval.v_number = -1;
7727#else
7728 linenr_T lnum;
7729
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007730 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7732 rettv->vval.v_number = -1;
7733 else
7734 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7735 if (rettv->vval.v_number >= 0)
7736 ++rettv->vval.v_number;
7737#endif
7738}
7739
7740/*
7741 * "lispindent(lnum)" function
7742 */
7743 static void
7744f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7745{
7746#ifdef FEAT_LISP
7747 pos_T pos;
7748 linenr_T lnum;
7749
7750 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007751 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007752 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7753 {
7754 curwin->w_cursor.lnum = lnum;
7755 rettv->vval.v_number = get_lisp_indent();
7756 curwin->w_cursor = pos;
7757 }
7758 else
7759#endif
7760 rettv->vval.v_number = -1;
7761}
7762
7763/*
7764 * "localtime()" function
7765 */
7766 static void
7767f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7768{
7769 rettv->vval.v_number = (varnumber_T)time(NULL);
7770}
7771
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007772 static void
7773get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7774{
7775 char_u *keys;
7776 char_u *which;
7777 char_u buf[NUMBUFLEN];
7778 char_u *keys_buf = NULL;
7779 char_u *rhs;
7780 int mode;
7781 int abbr = FALSE;
7782 int get_dict = FALSE;
7783 mapblock_T *mp;
7784 int buffer_local;
7785
7786 /* return empty string for failure */
7787 rettv->v_type = VAR_STRING;
7788 rettv->vval.v_string = NULL;
7789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007790 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791 if (*keys == NUL)
7792 return;
7793
7794 if (argvars[1].v_type != VAR_UNKNOWN)
7795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007796 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007797 if (argvars[2].v_type != VAR_UNKNOWN)
7798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007799 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007800 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007801 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 }
7803 }
7804 else
7805 which = (char_u *)"";
7806 if (which == NULL)
7807 return;
7808
7809 mode = get_map_mode(&which, 0);
7810
7811 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7812 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7813 vim_free(keys_buf);
7814
7815 if (!get_dict)
7816 {
7817 /* Return a string. */
7818 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007819 {
7820 if (*rhs == NUL)
7821 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7822 else
7823 rettv->vval.v_string = str2special_save(rhs, FALSE);
7824 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007825
7826 }
7827 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7828 {
7829 /* Return a dictionary. */
7830 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7831 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7832 dict_T *dict = rettv->vval.v_dict;
7833
Bram Moolenaare0be1672018-07-08 16:50:37 +02007834 dict_add_string(dict, "lhs", lhs);
7835 dict_add_string(dict, "rhs", mp->m_orig_str);
7836 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7837 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7838 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007839 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7840 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007841 dict_add_number(dict, "buffer", (long)buffer_local);
7842 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7843 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844
7845 vim_free(lhs);
7846 vim_free(mapmode);
7847 }
7848}
7849
7850#ifdef FEAT_FLOAT
7851/*
7852 * "log()" function
7853 */
7854 static void
7855f_log(typval_T *argvars, typval_T *rettv)
7856{
7857 float_T f = 0.0;
7858
7859 rettv->v_type = VAR_FLOAT;
7860 if (get_float_arg(argvars, &f) == OK)
7861 rettv->vval.v_float = log(f);
7862 else
7863 rettv->vval.v_float = 0.0;
7864}
7865
7866/*
7867 * "log10()" function
7868 */
7869 static void
7870f_log10(typval_T *argvars, typval_T *rettv)
7871{
7872 float_T f = 0.0;
7873
7874 rettv->v_type = VAR_FLOAT;
7875 if (get_float_arg(argvars, &f) == OK)
7876 rettv->vval.v_float = log10(f);
7877 else
7878 rettv->vval.v_float = 0.0;
7879}
7880#endif
7881
7882#ifdef FEAT_LUA
7883/*
7884 * "luaeval()" function
7885 */
7886 static void
7887f_luaeval(typval_T *argvars, typval_T *rettv)
7888{
7889 char_u *str;
7890 char_u buf[NUMBUFLEN];
7891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 do_luaeval(str, argvars + 1, rettv);
7894}
7895#endif
7896
7897/*
7898 * "map()" function
7899 */
7900 static void
7901f_map(typval_T *argvars, typval_T *rettv)
7902{
7903 filter_map(argvars, rettv, TRUE);
7904}
7905
7906/*
7907 * "maparg()" function
7908 */
7909 static void
7910f_maparg(typval_T *argvars, typval_T *rettv)
7911{
7912 get_maparg(argvars, rettv, TRUE);
7913}
7914
7915/*
7916 * "mapcheck()" function
7917 */
7918 static void
7919f_mapcheck(typval_T *argvars, typval_T *rettv)
7920{
7921 get_maparg(argvars, rettv, FALSE);
7922}
7923
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007924typedef enum
7925{
7926 MATCH_END, /* matchend() */
7927 MATCH_MATCH, /* match() */
7928 MATCH_STR, /* matchstr() */
7929 MATCH_LIST, /* matchlist() */
7930 MATCH_POS /* matchstrpos() */
7931} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932
7933 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007934find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007935{
7936 char_u *str = NULL;
7937 long len = 0;
7938 char_u *expr = NULL;
7939 char_u *pat;
7940 regmatch_T regmatch;
7941 char_u patbuf[NUMBUFLEN];
7942 char_u strbuf[NUMBUFLEN];
7943 char_u *save_cpo;
7944 long start = 0;
7945 long nth = 1;
7946 colnr_T startcol = 0;
7947 int match = 0;
7948 list_T *l = NULL;
7949 listitem_T *li = NULL;
7950 long idx = 0;
7951 char_u *tofree = NULL;
7952
7953 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7954 save_cpo = p_cpo;
7955 p_cpo = (char_u *)"";
7956
7957 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007958 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007960 /* type MATCH_LIST: return empty list when there are no matches.
7961 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007962 if (rettv_list_alloc(rettv) == FAIL)
7963 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007964 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 && (list_append_string(rettv->vval.v_list,
7966 (char_u *)"", 0) == FAIL
7967 || list_append_number(rettv->vval.v_list,
7968 (varnumber_T)-1) == FAIL
7969 || list_append_number(rettv->vval.v_list,
7970 (varnumber_T)-1) == FAIL
7971 || list_append_number(rettv->vval.v_list,
7972 (varnumber_T)-1) == FAIL))
7973 {
7974 list_free(rettv->vval.v_list);
7975 rettv->vval.v_list = NULL;
7976 goto theend;
7977 }
7978 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007979 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007980 {
7981 rettv->v_type = VAR_STRING;
7982 rettv->vval.v_string = NULL;
7983 }
7984
7985 if (argvars[0].v_type == VAR_LIST)
7986 {
7987 if ((l = argvars[0].vval.v_list) == NULL)
7988 goto theend;
7989 li = l->lv_first;
7990 }
7991 else
7992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007993 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 len = (long)STRLEN(str);
7995 }
7996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007997 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007998 if (pat == NULL)
7999 goto theend;
8000
8001 if (argvars[2].v_type != VAR_UNKNOWN)
8002 {
8003 int error = FALSE;
8004
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008005 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 if (error)
8007 goto theend;
8008 if (l != NULL)
8009 {
8010 li = list_find(l, start);
8011 if (li == NULL)
8012 goto theend;
8013 idx = l->lv_idx; /* use the cached index */
8014 }
8015 else
8016 {
8017 if (start < 0)
8018 start = 0;
8019 if (start > len)
8020 goto theend;
8021 /* When "count" argument is there ignore matches before "start",
8022 * otherwise skip part of the string. Differs when pattern is "^"
8023 * or "\<". */
8024 if (argvars[3].v_type != VAR_UNKNOWN)
8025 startcol = start;
8026 else
8027 {
8028 str += start;
8029 len -= start;
8030 }
8031 }
8032
8033 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008034 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008035 if (error)
8036 goto theend;
8037 }
8038
8039 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8040 if (regmatch.regprog != NULL)
8041 {
8042 regmatch.rm_ic = p_ic;
8043
8044 for (;;)
8045 {
8046 if (l != NULL)
8047 {
8048 if (li == NULL)
8049 {
8050 match = FALSE;
8051 break;
8052 }
8053 vim_free(tofree);
8054 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8055 if (str == NULL)
8056 break;
8057 }
8058
8059 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8060
8061 if (match && --nth <= 0)
8062 break;
8063 if (l == NULL && !match)
8064 break;
8065
8066 /* Advance to just after the match. */
8067 if (l != NULL)
8068 {
8069 li = li->li_next;
8070 ++idx;
8071 }
8072 else
8073 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008074 startcol = (colnr_T)(regmatch.startp[0]
8075 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076 if (startcol > (colnr_T)len
8077 || str + startcol <= regmatch.startp[0])
8078 {
8079 match = FALSE;
8080 break;
8081 }
8082 }
8083 }
8084
8085 if (match)
8086 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008087 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 {
8089 listitem_T *li1 = rettv->vval.v_list->lv_first;
8090 listitem_T *li2 = li1->li_next;
8091 listitem_T *li3 = li2->li_next;
8092 listitem_T *li4 = li3->li_next;
8093
8094 vim_free(li1->li_tv.vval.v_string);
8095 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8096 (int)(regmatch.endp[0] - regmatch.startp[0]));
8097 li3->li_tv.vval.v_number =
8098 (varnumber_T)(regmatch.startp[0] - expr);
8099 li4->li_tv.vval.v_number =
8100 (varnumber_T)(regmatch.endp[0] - expr);
8101 if (l != NULL)
8102 li2->li_tv.vval.v_number = (varnumber_T)idx;
8103 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008104 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 {
8106 int i;
8107
8108 /* return list with matched string and submatches */
8109 for (i = 0; i < NSUBEXP; ++i)
8110 {
8111 if (regmatch.endp[i] == NULL)
8112 {
8113 if (list_append_string(rettv->vval.v_list,
8114 (char_u *)"", 0) == FAIL)
8115 break;
8116 }
8117 else if (list_append_string(rettv->vval.v_list,
8118 regmatch.startp[i],
8119 (int)(regmatch.endp[i] - regmatch.startp[i]))
8120 == FAIL)
8121 break;
8122 }
8123 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008124 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 {
8126 /* return matched string */
8127 if (l != NULL)
8128 copy_tv(&li->li_tv, rettv);
8129 else
8130 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8131 (int)(regmatch.endp[0] - regmatch.startp[0]));
8132 }
8133 else if (l != NULL)
8134 rettv->vval.v_number = idx;
8135 else
8136 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008137 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008138 rettv->vval.v_number =
8139 (varnumber_T)(regmatch.startp[0] - str);
8140 else
8141 rettv->vval.v_number =
8142 (varnumber_T)(regmatch.endp[0] - str);
8143 rettv->vval.v_number += (varnumber_T)(str - expr);
8144 }
8145 }
8146 vim_regfree(regmatch.regprog);
8147 }
8148
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008149theend:
8150 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 /* matchstrpos() without a list: drop the second item. */
8152 listitem_remove(rettv->vval.v_list,
8153 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008154 vim_free(tofree);
8155 p_cpo = save_cpo;
8156}
8157
8158/*
8159 * "match()" function
8160 */
8161 static void
8162f_match(typval_T *argvars, typval_T *rettv)
8163{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008164 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165}
8166
Bram Moolenaar95e51472018-07-28 16:55:56 +02008167#ifdef FEAT_SEARCH_EXTRA
8168 static int
8169matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8170{
8171 dictitem_T *di;
8172
8173 if (tv->v_type != VAR_DICT)
8174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008175 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008176 return FAIL;
8177 }
8178
8179 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008180 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008181 (char_u *)"conceal", FALSE);
8182
8183 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8184 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008185 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008186 if (*win == NULL)
8187 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008188 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008189 return FAIL;
8190 }
8191 }
8192
8193 return OK;
8194}
8195#endif
8196
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008197/*
8198 * "matchadd()" function
8199 */
8200 static void
8201f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8202{
8203#ifdef FEAT_SEARCH_EXTRA
8204 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008205 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8206 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008207 int prio = 10; /* default priority */
8208 int id = -1;
8209 int error = FALSE;
8210 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008211 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212
8213 rettv->vval.v_number = -1;
8214
8215 if (grp == NULL || pat == NULL)
8216 return;
8217 if (argvars[2].v_type != VAR_UNKNOWN)
8218 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 if (argvars[3].v_type != VAR_UNKNOWN)
8221 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008222 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008223 if (argvars[4].v_type != VAR_UNKNOWN
8224 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8225 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226 }
8227 }
8228 if (error == TRUE)
8229 return;
8230 if (id >= 1 && id <= 3)
8231 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008232 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233 return;
8234 }
8235
Bram Moolenaar95e51472018-07-28 16:55:56 +02008236 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 conceal_char);
8238#endif
8239}
8240
8241/*
8242 * "matchaddpos()" function
8243 */
8244 static void
8245f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8246{
8247#ifdef FEAT_SEARCH_EXTRA
8248 char_u buf[NUMBUFLEN];
8249 char_u *group;
8250 int prio = 10;
8251 int id = -1;
8252 int error = FALSE;
8253 list_T *l;
8254 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008255 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256
8257 rettv->vval.v_number = -1;
8258
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008259 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260 if (group == NULL)
8261 return;
8262
8263 if (argvars[1].v_type != VAR_LIST)
8264 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008265 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008266 return;
8267 }
8268 l = argvars[1].vval.v_list;
8269 if (l == NULL)
8270 return;
8271
8272 if (argvars[2].v_type != VAR_UNKNOWN)
8273 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008274 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008275 if (argvars[3].v_type != VAR_UNKNOWN)
8276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008277 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008278
8279 if (argvars[4].v_type != VAR_UNKNOWN
8280 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8281 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008282 }
8283 }
8284 if (error == TRUE)
8285 return;
8286
8287 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8288 if (id == 1 || id == 2)
8289 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008290 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008291 return;
8292 }
8293
Bram Moolenaar95e51472018-07-28 16:55:56 +02008294 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295 conceal_char);
8296#endif
8297}
8298
8299/*
8300 * "matcharg()" function
8301 */
8302 static void
8303f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8304{
8305 if (rettv_list_alloc(rettv) == OK)
8306 {
8307#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008308 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008309 matchitem_T *m;
8310
8311 if (id >= 1 && id <= 3)
8312 {
8313 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8314 {
8315 list_append_string(rettv->vval.v_list,
8316 syn_id2name(m->hlg_id), -1);
8317 list_append_string(rettv->vval.v_list, m->pattern, -1);
8318 }
8319 else
8320 {
8321 list_append_string(rettv->vval.v_list, NULL, -1);
8322 list_append_string(rettv->vval.v_list, NULL, -1);
8323 }
8324 }
8325#endif
8326 }
8327}
8328
8329/*
8330 * "matchdelete()" function
8331 */
8332 static void
8333f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8334{
8335#ifdef FEAT_SEARCH_EXTRA
8336 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008337 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008338#endif
8339}
8340
8341/*
8342 * "matchend()" function
8343 */
8344 static void
8345f_matchend(typval_T *argvars, typval_T *rettv)
8346{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008347 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008348}
8349
8350/*
8351 * "matchlist()" function
8352 */
8353 static void
8354f_matchlist(typval_T *argvars, typval_T *rettv)
8355{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008356 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357}
8358
8359/*
8360 * "matchstr()" function
8361 */
8362 static void
8363f_matchstr(typval_T *argvars, typval_T *rettv)
8364{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008365 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366}
8367
8368/*
8369 * "matchstrpos()" function
8370 */
8371 static void
8372f_matchstrpos(typval_T *argvars, typval_T *rettv)
8373{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008374 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008375}
8376
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 static void
8378max_min(typval_T *argvars, typval_T *rettv, int domax)
8379{
8380 varnumber_T n = 0;
8381 varnumber_T i;
8382 int error = FALSE;
8383
8384 if (argvars[0].v_type == VAR_LIST)
8385 {
8386 list_T *l;
8387 listitem_T *li;
8388
8389 l = argvars[0].vval.v_list;
8390 if (l != NULL)
8391 {
8392 li = l->lv_first;
8393 if (li != NULL)
8394 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008395 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396 for (;;)
8397 {
8398 li = li->li_next;
8399 if (li == NULL)
8400 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008401 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 if (domax ? i > n : i < n)
8403 n = i;
8404 }
8405 }
8406 }
8407 }
8408 else if (argvars[0].v_type == VAR_DICT)
8409 {
8410 dict_T *d;
8411 int first = TRUE;
8412 hashitem_T *hi;
8413 int todo;
8414
8415 d = argvars[0].vval.v_dict;
8416 if (d != NULL)
8417 {
8418 todo = (int)d->dv_hashtab.ht_used;
8419 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8420 {
8421 if (!HASHITEM_EMPTY(hi))
8422 {
8423 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008424 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008425 if (first)
8426 {
8427 n = i;
8428 first = FALSE;
8429 }
8430 else if (domax ? i > n : i < n)
8431 n = i;
8432 }
8433 }
8434 }
8435 }
8436 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008437 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 rettv->vval.v_number = error ? 0 : n;
8439}
8440
8441/*
8442 * "max()" function
8443 */
8444 static void
8445f_max(typval_T *argvars, typval_T *rettv)
8446{
8447 max_min(argvars, rettv, TRUE);
8448}
8449
8450/*
8451 * "min()" function
8452 */
8453 static void
8454f_min(typval_T *argvars, typval_T *rettv)
8455{
8456 max_min(argvars, rettv, FALSE);
8457}
8458
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459/*
8460 * Create the directory in which "dir" is located, and higher levels when
8461 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008462 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008463 */
8464 static int
8465mkdir_recurse(char_u *dir, int prot)
8466{
8467 char_u *p;
8468 char_u *updir;
8469 int r = FAIL;
8470
8471 /* Get end of directory name in "dir".
8472 * We're done when it's "/" or "c:/". */
8473 p = gettail_sep(dir);
8474 if (p <= get_past_head(dir))
8475 return OK;
8476
8477 /* If the directory exists we're done. Otherwise: create it.*/
8478 updir = vim_strnsave(dir, (int)(p - dir));
8479 if (updir == NULL)
8480 return FAIL;
8481 if (mch_isdir(updir))
8482 r = OK;
8483 else if (mkdir_recurse(updir, prot) == OK)
8484 r = vim_mkdir_emsg(updir, prot);
8485 vim_free(updir);
8486 return r;
8487}
8488
8489#ifdef vim_mkdir
8490/*
8491 * "mkdir()" function
8492 */
8493 static void
8494f_mkdir(typval_T *argvars, typval_T *rettv)
8495{
8496 char_u *dir;
8497 char_u buf[NUMBUFLEN];
8498 int prot = 0755;
8499
8500 rettv->vval.v_number = FAIL;
8501 if (check_restricted() || check_secure())
8502 return;
8503
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008504 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008506 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008508 if (*gettail(dir) == NUL)
8509 /* remove trailing slashes */
8510 *gettail_sep(dir) = NUL;
8511
8512 if (argvars[1].v_type != VAR_UNKNOWN)
8513 {
8514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008516 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008517 if (prot == -1)
8518 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008519 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008520 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008521 {
8522 if (mch_isdir(dir))
8523 {
8524 /* With the "p" flag it's OK if the dir already exists. */
8525 rettv->vval.v_number = OK;
8526 return;
8527 }
8528 mkdir_recurse(dir, prot);
8529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008531 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532}
8533#endif
8534
8535/*
8536 * "mode()" function
8537 */
8538 static void
8539f_mode(typval_T *argvars, typval_T *rettv)
8540{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008541 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542
Bram Moolenaar612cc382018-07-29 15:34:26 +02008543 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544
8545 if (time_for_testing == 93784)
8546 {
8547 /* Testing the two-character code. */
8548 buf[0] = 'x';
8549 buf[1] = '!';
8550 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008551#ifdef FEAT_TERMINAL
8552 else if (term_use_loop())
8553 buf[0] = 't';
8554#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 else if (VIsual_active)
8556 {
8557 if (VIsual_select)
8558 buf[0] = VIsual_mode + 's' - 'v';
8559 else
8560 buf[0] = VIsual_mode;
8561 }
8562 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8563 || State == CONFIRM)
8564 {
8565 buf[0] = 'r';
8566 if (State == ASKMORE)
8567 buf[1] = 'm';
8568 else if (State == CONFIRM)
8569 buf[1] = '?';
8570 }
8571 else if (State == EXTERNCMD)
8572 buf[0] = '!';
8573 else if (State & INSERT)
8574 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575 if (State & VREPLACE_FLAG)
8576 {
8577 buf[0] = 'R';
8578 buf[1] = 'v';
8579 }
8580 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008581 {
8582 if (State & REPLACE_FLAG)
8583 buf[0] = 'R';
8584 else
8585 buf[0] = 'i';
8586#ifdef FEAT_INS_EXPAND
8587 if (ins_compl_active())
8588 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008589 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008590 buf[1] = 'x';
8591#endif
8592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008594 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 {
8596 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008597 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008598 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008599 else if (exmode_active == EXMODE_NORMAL)
8600 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 }
8602 else
8603 {
8604 buf[0] = 'n';
8605 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008606 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008607 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008608 // to be able to detect force-linewise/blockwise/characterwise operations
8609 buf[2] = motion_force;
8610 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008611 else if (restart_edit == 'I' || restart_edit == 'R'
8612 || restart_edit == 'V')
8613 {
8614 buf[1] = 'i';
8615 buf[2] = restart_edit;
8616 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617 }
8618
8619 /* Clear out the minor mode when the argument is not a non-zero number or
8620 * non-empty string. */
8621 if (!non_zero_arg(&argvars[0]))
8622 buf[1] = NUL;
8623
8624 rettv->vval.v_string = vim_strsave(buf);
8625 rettv->v_type = VAR_STRING;
8626}
8627
8628#if defined(FEAT_MZSCHEME) || defined(PROTO)
8629/*
8630 * "mzeval()" function
8631 */
8632 static void
8633f_mzeval(typval_T *argvars, typval_T *rettv)
8634{
8635 char_u *str;
8636 char_u buf[NUMBUFLEN];
8637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008638 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 do_mzeval(str, rettv);
8640}
8641
8642 void
8643mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8644{
8645 typval_T argvars[3];
8646
8647 argvars[0].v_type = VAR_STRING;
8648 argvars[0].vval.v_string = name;
8649 copy_tv(args, &argvars[1]);
8650 argvars[2].v_type = VAR_UNKNOWN;
8651 f_call(argvars, rettv);
8652 clear_tv(&argvars[1]);
8653}
8654#endif
8655
8656/*
8657 * "nextnonblank()" function
8658 */
8659 static void
8660f_nextnonblank(typval_T *argvars, typval_T *rettv)
8661{
8662 linenr_T lnum;
8663
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008664 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665 {
8666 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8667 {
8668 lnum = 0;
8669 break;
8670 }
8671 if (*skipwhite(ml_get(lnum)) != NUL)
8672 break;
8673 }
8674 rettv->vval.v_number = lnum;
8675}
8676
8677/*
8678 * "nr2char()" function
8679 */
8680 static void
8681f_nr2char(typval_T *argvars, typval_T *rettv)
8682{
8683 char_u buf[NUMBUFLEN];
8684
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008685 if (has_mbyte)
8686 {
8687 int utf8 = 0;
8688
8689 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008690 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008691 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008692 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008694 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 }
8696 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008698 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 buf[1] = NUL;
8700 }
8701 rettv->v_type = VAR_STRING;
8702 rettv->vval.v_string = vim_strsave(buf);
8703}
8704
8705/*
8706 * "or(expr, expr)" function
8707 */
8708 static void
8709f_or(typval_T *argvars, typval_T *rettv)
8710{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008711 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8712 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713}
8714
8715/*
8716 * "pathshorten()" function
8717 */
8718 static void
8719f_pathshorten(typval_T *argvars, typval_T *rettv)
8720{
8721 char_u *p;
8722
8723 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008724 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725 if (p == NULL)
8726 rettv->vval.v_string = NULL;
8727 else
8728 {
8729 p = vim_strsave(p);
8730 rettv->vval.v_string = p;
8731 if (p != NULL)
8732 shorten_dir(p);
8733 }
8734}
8735
8736#ifdef FEAT_PERL
8737/*
8738 * "perleval()" function
8739 */
8740 static void
8741f_perleval(typval_T *argvars, typval_T *rettv)
8742{
8743 char_u *str;
8744 char_u buf[NUMBUFLEN];
8745
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008746 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747 do_perleval(str, rettv);
8748}
8749#endif
8750
8751#ifdef FEAT_FLOAT
8752/*
8753 * "pow()" function
8754 */
8755 static void
8756f_pow(typval_T *argvars, typval_T *rettv)
8757{
8758 float_T fx = 0.0, fy = 0.0;
8759
8760 rettv->v_type = VAR_FLOAT;
8761 if (get_float_arg(argvars, &fx) == OK
8762 && get_float_arg(&argvars[1], &fy) == OK)
8763 rettv->vval.v_float = pow(fx, fy);
8764 else
8765 rettv->vval.v_float = 0.0;
8766}
8767#endif
8768
8769/*
8770 * "prevnonblank()" function
8771 */
8772 static void
8773f_prevnonblank(typval_T *argvars, typval_T *rettv)
8774{
8775 linenr_T lnum;
8776
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008777 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008778 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8779 lnum = 0;
8780 else
8781 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8782 --lnum;
8783 rettv->vval.v_number = lnum;
8784}
8785
8786/* This dummy va_list is here because:
8787 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8788 * - locally in the function results in a "used before set" warning
8789 * - using va_start() to initialize it gives "function with fixed args" error */
8790static va_list ap;
8791
8792/*
8793 * "printf()" function
8794 */
8795 static void
8796f_printf(typval_T *argvars, typval_T *rettv)
8797{
8798 char_u buf[NUMBUFLEN];
8799 int len;
8800 char_u *s;
8801 int saved_did_emsg = did_emsg;
8802 char *fmt;
8803
8804 rettv->v_type = VAR_STRING;
8805 rettv->vval.v_string = NULL;
8806
8807 /* Get the required length, allocate the buffer and do it for real. */
8808 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008809 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008810 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 if (!did_emsg)
8812 {
8813 s = alloc(len + 1);
8814 if (s != NULL)
8815 {
8816 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008817 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8818 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 }
8820 }
8821 did_emsg |= saved_did_emsg;
8822}
8823
Bram Moolenaarf2732452018-06-03 14:47:35 +02008824#ifdef FEAT_JOB_CHANNEL
8825/*
8826 * "prompt_setcallback({buffer}, {callback})" function
8827 */
8828 static void
8829f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8830{
8831 buf_T *buf;
8832 char_u *callback;
8833 partial_T *partial;
8834
8835 if (check_secure())
8836 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008837 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008838 if (buf == NULL)
8839 return;
8840
8841 callback = get_callback(&argvars[1], &partial);
8842 if (callback == NULL)
8843 return;
8844
8845 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8846 if (partial == NULL)
8847 buf->b_prompt_callback = vim_strsave(callback);
8848 else
8849 /* pointer into the partial */
8850 buf->b_prompt_callback = callback;
8851 buf->b_prompt_partial = partial;
8852}
8853
8854/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008855 * "prompt_setinterrupt({buffer}, {callback})" function
8856 */
8857 static void
8858f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8859{
8860 buf_T *buf;
8861 char_u *callback;
8862 partial_T *partial;
8863
8864 if (check_secure())
8865 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008866 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008867 if (buf == NULL)
8868 return;
8869
8870 callback = get_callback(&argvars[1], &partial);
8871 if (callback == NULL)
8872 return;
8873
8874 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8875 if (partial == NULL)
8876 buf->b_prompt_interrupt = vim_strsave(callback);
8877 else
8878 /* pointer into the partial */
8879 buf->b_prompt_interrupt = callback;
8880 buf->b_prompt_int_partial = partial;
8881}
8882
8883/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008884 * "prompt_setprompt({buffer}, {text})" function
8885 */
8886 static void
8887f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8888{
8889 buf_T *buf;
8890 char_u *text;
8891
8892 if (check_secure())
8893 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008894 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008895 if (buf == NULL)
8896 return;
8897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008898 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008899 vim_free(buf->b_prompt_text);
8900 buf->b_prompt_text = vim_strsave(text);
8901}
8902#endif
8903
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008904/*
8905 * "pumvisible()" function
8906 */
8907 static void
8908f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8909{
8910#ifdef FEAT_INS_EXPAND
8911 if (pum_visible())
8912 rettv->vval.v_number = 1;
8913#endif
8914}
8915
8916#ifdef FEAT_PYTHON3
8917/*
8918 * "py3eval()" function
8919 */
8920 static void
8921f_py3eval(typval_T *argvars, typval_T *rettv)
8922{
8923 char_u *str;
8924 char_u buf[NUMBUFLEN];
8925
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008926 if (p_pyx == 0)
8927 p_pyx = 3;
8928
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 do_py3eval(str, rettv);
8931}
8932#endif
8933
8934#ifdef FEAT_PYTHON
8935/*
8936 * "pyeval()" function
8937 */
8938 static void
8939f_pyeval(typval_T *argvars, typval_T *rettv)
8940{
8941 char_u *str;
8942 char_u buf[NUMBUFLEN];
8943
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008944 if (p_pyx == 0)
8945 p_pyx = 2;
8946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008947 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948 do_pyeval(str, rettv);
8949}
8950#endif
8951
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008952#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8953/*
8954 * "pyxeval()" function
8955 */
8956 static void
8957f_pyxeval(typval_T *argvars, typval_T *rettv)
8958{
8959# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8960 init_pyxversion();
8961 if (p_pyx == 2)
8962 f_pyeval(argvars, rettv);
8963 else
8964 f_py3eval(argvars, rettv);
8965# elif defined(FEAT_PYTHON)
8966 f_pyeval(argvars, rettv);
8967# elif defined(FEAT_PYTHON3)
8968 f_py3eval(argvars, rettv);
8969# endif
8970}
8971#endif
8972
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973/*
8974 * "range()" function
8975 */
8976 static void
8977f_range(typval_T *argvars, typval_T *rettv)
8978{
8979 varnumber_T start;
8980 varnumber_T end;
8981 varnumber_T stride = 1;
8982 varnumber_T i;
8983 int error = FALSE;
8984
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008985 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 if (argvars[1].v_type == VAR_UNKNOWN)
8987 {
8988 end = start - 1;
8989 start = 0;
8990 }
8991 else
8992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008993 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008994 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 }
8997
8998 if (error)
8999 return; /* type error; errmsg already given */
9000 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009001 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009003 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 else
9005 {
9006 if (rettv_list_alloc(rettv) == OK)
9007 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9008 if (list_append_number(rettv->vval.v_list,
9009 (varnumber_T)i) == FAIL)
9010 break;
9011 }
9012}
9013
9014/*
9015 * "readfile()" function
9016 */
9017 static void
9018f_readfile(typval_T *argvars, typval_T *rettv)
9019{
9020 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009021 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009022 int failed = FALSE;
9023 char_u *fname;
9024 FILE *fd;
9025 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9026 int io_size = sizeof(buf);
9027 int readlen; /* size of last fread() */
9028 char_u *prev = NULL; /* previously read bytes, if any */
9029 long prevlen = 0; /* length of data in prev */
9030 long prevsize = 0; /* size of prev buffer */
9031 long maxline = MAXLNUM;
9032 long cnt = 0;
9033 char_u *p; /* position in buf */
9034 char_u *start; /* start of current line */
9035
9036 if (argvars[1].v_type != VAR_UNKNOWN)
9037 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009038 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009040 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9041 blob = TRUE;
9042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009043 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009044 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 }
9046
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009047 if (blob)
9048 {
9049 if (rettv_blob_alloc(rettv) == FAIL)
9050 return;
9051 }
9052 else
9053 {
9054 if (rettv_list_alloc(rettv) == FAIL)
9055 return;
9056 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057
9058 /* Always open the file in binary mode, library functions have a mind of
9059 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009060 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009063 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 return;
9065 }
9066
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009067 if (blob)
9068 {
9069 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9070 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009071 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009072 blob_free(rettv->vval.v_blob);
9073 }
9074 fclose(fd);
9075 return;
9076 }
9077
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009078 while (cnt < maxline || maxline < 0)
9079 {
9080 readlen = (int)fread(buf, 1, io_size, fd);
9081
9082 /* This for loop processes what was read, but is also entered at end
9083 * of file so that either:
9084 * - an incomplete line gets written
9085 * - a "binary" file gets an empty line at the end if it ends in a
9086 * newline. */
9087 for (p = buf, start = buf;
9088 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9089 ++p)
9090 {
9091 if (*p == '\n' || readlen <= 0)
9092 {
9093 listitem_T *li;
9094 char_u *s = NULL;
9095 long_u len = p - start;
9096
9097 /* Finished a line. Remove CRs before NL. */
9098 if (readlen > 0 && !binary)
9099 {
9100 while (len > 0 && start[len - 1] == '\r')
9101 --len;
9102 /* removal may cross back to the "prev" string */
9103 if (len == 0)
9104 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9105 --prevlen;
9106 }
9107 if (prevlen == 0)
9108 s = vim_strnsave(start, (int)len);
9109 else
9110 {
9111 /* Change "prev" buffer to be the right size. This way
9112 * the bytes are only copied once, and very long lines are
9113 * allocated only once. */
9114 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9115 {
9116 mch_memmove(s + prevlen, start, len);
9117 s[prevlen + len] = NUL;
9118 prev = NULL; /* the list will own the string */
9119 prevlen = prevsize = 0;
9120 }
9121 }
9122 if (s == NULL)
9123 {
9124 do_outofmem_msg((long_u) prevlen + len + 1);
9125 failed = TRUE;
9126 break;
9127 }
9128
9129 if ((li = listitem_alloc()) == NULL)
9130 {
9131 vim_free(s);
9132 failed = TRUE;
9133 break;
9134 }
9135 li->li_tv.v_type = VAR_STRING;
9136 li->li_tv.v_lock = 0;
9137 li->li_tv.vval.v_string = s;
9138 list_append(rettv->vval.v_list, li);
9139
9140 start = p + 1; /* step over newline */
9141 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9142 break;
9143 }
9144 else if (*p == NUL)
9145 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9147 * when finding the BF and check the previous two bytes. */
9148 else if (*p == 0xbf && enc_utf8 && !binary)
9149 {
9150 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9151 * + 1, these may be in the "prev" string. */
9152 char_u back1 = p >= buf + 1 ? p[-1]
9153 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9154 char_u back2 = p >= buf + 2 ? p[-2]
9155 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9156 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9157
9158 if (back2 == 0xef && back1 == 0xbb)
9159 {
9160 char_u *dest = p - 2;
9161
9162 /* Usually a BOM is at the beginning of a file, and so at
9163 * the beginning of a line; then we can just step over it.
9164 */
9165 if (start == dest)
9166 start = p + 1;
9167 else
9168 {
9169 /* have to shuffle buf to close gap */
9170 int adjust_prevlen = 0;
9171
9172 if (dest < buf)
9173 {
9174 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9175 dest = buf;
9176 }
9177 if (readlen > p - buf + 1)
9178 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9179 readlen -= 3 - adjust_prevlen;
9180 prevlen -= adjust_prevlen;
9181 p = dest - 1;
9182 }
9183 }
9184 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009185 } /* for */
9186
9187 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9188 break;
9189 if (start < p)
9190 {
9191 /* There's part of a line in buf, store it in "prev". */
9192 if (p - start + prevlen >= prevsize)
9193 {
9194 /* need bigger "prev" buffer */
9195 char_u *newprev;
9196
9197 /* A common use case is ordinary text files and "prev" gets a
9198 * fragment of a line, so the first allocation is made
9199 * small, to avoid repeatedly 'allocing' large and
9200 * 'reallocing' small. */
9201 if (prevsize == 0)
9202 prevsize = (long)(p - start);
9203 else
9204 {
9205 long grow50pc = (prevsize * 3) / 2;
9206 long growmin = (long)((p - start) * 2 + prevlen);
9207 prevsize = grow50pc > growmin ? grow50pc : growmin;
9208 }
9209 newprev = prev == NULL ? alloc(prevsize)
9210 : vim_realloc(prev, prevsize);
9211 if (newprev == NULL)
9212 {
9213 do_outofmem_msg((long_u)prevsize);
9214 failed = TRUE;
9215 break;
9216 }
9217 prev = newprev;
9218 }
9219 /* Add the line part to end of "prev". */
9220 mch_memmove(prev + prevlen, start, p - start);
9221 prevlen += (long)(p - start);
9222 }
9223 } /* while */
9224
9225 /*
9226 * For a negative line count use only the lines at the end of the file,
9227 * free the rest.
9228 */
9229 if (!failed && maxline < 0)
9230 while (cnt > -maxline)
9231 {
9232 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9233 --cnt;
9234 }
9235
9236 if (failed)
9237 {
9238 list_free(rettv->vval.v_list);
9239 /* readfile doc says an empty list is returned on error */
9240 rettv->vval.v_list = list_alloc();
9241 }
9242
9243 vim_free(prev);
9244 fclose(fd);
9245}
9246
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009247 static void
9248return_register(int regname, typval_T *rettv)
9249{
9250 char_u buf[2] = {0, 0};
9251
9252 buf[0] = (char_u)regname;
9253 rettv->v_type = VAR_STRING;
9254 rettv->vval.v_string = vim_strsave(buf);
9255}
9256
9257/*
9258 * "reg_executing()" function
9259 */
9260 static void
9261f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9262{
9263 return_register(reg_executing, rettv);
9264}
9265
9266/*
9267 * "reg_recording()" function
9268 */
9269 static void
9270f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9271{
9272 return_register(reg_recording, rettv);
9273}
9274
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009275#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009276/*
9277 * Convert a List to proftime_T.
9278 * Return FAIL when there is something wrong.
9279 */
9280 static int
9281list2proftime(typval_T *arg, proftime_T *tm)
9282{
9283 long n1, n2;
9284 int error = FALSE;
9285
9286 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9287 || arg->vval.v_list->lv_len != 2)
9288 return FAIL;
9289 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9290 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9291# ifdef WIN3264
9292 tm->HighPart = n1;
9293 tm->LowPart = n2;
9294# else
9295 tm->tv_sec = n1;
9296 tm->tv_usec = n2;
9297# endif
9298 return error ? FAIL : OK;
9299}
9300#endif /* FEAT_RELTIME */
9301
9302/*
9303 * "reltime()" function
9304 */
9305 static void
9306f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9307{
9308#ifdef FEAT_RELTIME
9309 proftime_T res;
9310 proftime_T start;
9311
9312 if (argvars[0].v_type == VAR_UNKNOWN)
9313 {
9314 /* No arguments: get current time. */
9315 profile_start(&res);
9316 }
9317 else if (argvars[1].v_type == VAR_UNKNOWN)
9318 {
9319 if (list2proftime(&argvars[0], &res) == FAIL)
9320 return;
9321 profile_end(&res);
9322 }
9323 else
9324 {
9325 /* Two arguments: compute the difference. */
9326 if (list2proftime(&argvars[0], &start) == FAIL
9327 || list2proftime(&argvars[1], &res) == FAIL)
9328 return;
9329 profile_sub(&res, &start);
9330 }
9331
9332 if (rettv_list_alloc(rettv) == OK)
9333 {
9334 long n1, n2;
9335
9336# ifdef WIN3264
9337 n1 = res.HighPart;
9338 n2 = res.LowPart;
9339# else
9340 n1 = res.tv_sec;
9341 n2 = res.tv_usec;
9342# endif
9343 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9344 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9345 }
9346#endif
9347}
9348
9349#ifdef FEAT_FLOAT
9350/*
9351 * "reltimefloat()" function
9352 */
9353 static void
9354f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9355{
9356# ifdef FEAT_RELTIME
9357 proftime_T tm;
9358# endif
9359
9360 rettv->v_type = VAR_FLOAT;
9361 rettv->vval.v_float = 0;
9362# ifdef FEAT_RELTIME
9363 if (list2proftime(&argvars[0], &tm) == OK)
9364 rettv->vval.v_float = profile_float(&tm);
9365# endif
9366}
9367#endif
9368
9369/*
9370 * "reltimestr()" function
9371 */
9372 static void
9373f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9374{
9375#ifdef FEAT_RELTIME
9376 proftime_T tm;
9377#endif
9378
9379 rettv->v_type = VAR_STRING;
9380 rettv->vval.v_string = NULL;
9381#ifdef FEAT_RELTIME
9382 if (list2proftime(&argvars[0], &tm) == OK)
9383 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9384#endif
9385}
9386
9387#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388 static void
9389make_connection(void)
9390{
9391 if (X_DISPLAY == NULL
9392# ifdef FEAT_GUI
9393 && !gui.in_use
9394# endif
9395 )
9396 {
9397 x_force_connect = TRUE;
9398 setup_term_clip();
9399 x_force_connect = FALSE;
9400 }
9401}
9402
9403 static int
9404check_connection(void)
9405{
9406 make_connection();
9407 if (X_DISPLAY == NULL)
9408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009409 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 return FAIL;
9411 }
9412 return OK;
9413}
9414#endif
9415
9416#ifdef FEAT_CLIENTSERVER
9417 static void
9418remote_common(typval_T *argvars, typval_T *rettv, int expr)
9419{
9420 char_u *server_name;
9421 char_u *keys;
9422 char_u *r = NULL;
9423 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009424 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425# ifdef WIN32
9426 HWND w;
9427# else
9428 Window w;
9429# endif
9430
9431 if (check_restricted() || check_secure())
9432 return;
9433
9434# ifdef FEAT_X11
9435 if (check_connection() == FAIL)
9436 return;
9437# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009438 if (argvars[2].v_type != VAR_UNKNOWN
9439 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009440 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009442 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443 if (server_name == NULL)
9444 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009445 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009446# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009447 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009449 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9450 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451# endif
9452 {
9453 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009455 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009456 vim_free(r);
9457 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009458 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009459 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 return;
9461 }
9462
9463 rettv->vval.v_string = r;
9464
9465 if (argvars[2].v_type != VAR_UNKNOWN)
9466 {
9467 dictitem_T v;
9468 char_u str[30];
9469 char_u *idvar;
9470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009471 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009472 if (idvar != NULL && *idvar != NUL)
9473 {
9474 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9475 v.di_tv.v_type = VAR_STRING;
9476 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009478 vim_free(v.di_tv.vval.v_string);
9479 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 }
9481}
9482#endif
9483
9484/*
9485 * "remote_expr()" function
9486 */
9487 static void
9488f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9489{
9490 rettv->v_type = VAR_STRING;
9491 rettv->vval.v_string = NULL;
9492#ifdef FEAT_CLIENTSERVER
9493 remote_common(argvars, rettv, TRUE);
9494#endif
9495}
9496
9497/*
9498 * "remote_foreground()" function
9499 */
9500 static void
9501f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9502{
9503#ifdef FEAT_CLIENTSERVER
9504# ifdef WIN32
9505 /* On Win32 it's done in this application. */
9506 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009507 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508
9509 if (server_name != NULL)
9510 serverForeground(server_name);
9511 }
9512# else
9513 /* Send a foreground() expression to the server. */
9514 argvars[1].v_type = VAR_STRING;
9515 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9516 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009517 rettv->v_type = VAR_STRING;
9518 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519 remote_common(argvars, rettv, TRUE);
9520 vim_free(argvars[1].vval.v_string);
9521# endif
9522#endif
9523}
9524
9525 static void
9526f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9527{
9528#ifdef FEAT_CLIENTSERVER
9529 dictitem_T v;
9530 char_u *s = NULL;
9531# ifdef WIN32
9532 long_u n = 0;
9533# endif
9534 char_u *serverid;
9535
9536 if (check_restricted() || check_secure())
9537 {
9538 rettv->vval.v_number = -1;
9539 return;
9540 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009541 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542 if (serverid == NULL)
9543 {
9544 rettv->vval.v_number = -1;
9545 return; /* type error; errmsg already given */
9546 }
9547# ifdef WIN32
9548 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9549 if (n == 0)
9550 rettv->vval.v_number = -1;
9551 else
9552 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009553 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 rettv->vval.v_number = (s != NULL);
9555 }
9556# else
9557 if (check_connection() == FAIL)
9558 return;
9559
9560 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9561 serverStrToWin(serverid), &s);
9562# endif
9563
9564 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9565 {
9566 char_u *retvar;
9567
9568 v.di_tv.v_type = VAR_STRING;
9569 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009570 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009571 if (retvar != NULL)
9572 set_var(retvar, &v.di_tv, FALSE);
9573 vim_free(v.di_tv.vval.v_string);
9574 }
9575#else
9576 rettv->vval.v_number = -1;
9577#endif
9578}
9579
9580 static void
9581f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9582{
9583 char_u *r = NULL;
9584
9585#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009586 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587
9588 if (serverid != NULL && !check_restricted() && !check_secure())
9589 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009590 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009591# ifdef WIN32
9592 /* The server's HWND is encoded in the 'id' parameter */
9593 long_u n = 0;
9594# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009595
9596 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009597 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009598
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009599# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009600 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9601 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009602 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009603 if (r == NULL)
9604# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009605 if (check_connection() == FAIL
9606 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9607 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009608# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009609 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610 }
9611#endif
9612 rettv->v_type = VAR_STRING;
9613 rettv->vval.v_string = r;
9614}
9615
9616/*
9617 * "remote_send()" function
9618 */
9619 static void
9620f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9621{
9622 rettv->v_type = VAR_STRING;
9623 rettv->vval.v_string = NULL;
9624#ifdef FEAT_CLIENTSERVER
9625 remote_common(argvars, rettv, FALSE);
9626#endif
9627}
9628
9629/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009630 * "remote_startserver()" function
9631 */
9632 static void
9633f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9634{
9635#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009636 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009637
9638 if (server == NULL)
9639 return; /* type error; errmsg already given */
9640 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009641 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009642 else
9643 {
9644# ifdef FEAT_X11
9645 if (check_connection() == OK)
9646 serverRegisterName(X_DISPLAY, server);
9647# else
9648 serverSetName(server);
9649# endif
9650 }
9651#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009652 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009653#endif
9654}
9655
9656/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009657 * "remove()" function
9658 */
9659 static void
9660f_remove(typval_T *argvars, typval_T *rettv)
9661{
9662 list_T *l;
9663 listitem_T *item, *item2;
9664 listitem_T *li;
9665 long idx;
9666 long end;
9667 char_u *key;
9668 dict_T *d;
9669 dictitem_T *di;
9670 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009671 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009672
9673 if (argvars[0].v_type == VAR_DICT)
9674 {
9675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009676 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677 else if ((d = argvars[0].vval.v_dict) != NULL
9678 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9679 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009680 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009681 if (key != NULL)
9682 {
9683 di = dict_find(d, key, -1);
9684 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009685 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009686 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9687 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9688 {
9689 *rettv = di->di_tv;
9690 init_tv(&di->di_tv);
9691 dictitem_remove(d, di);
9692 }
9693 }
9694 }
9695 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009696 else if (argvars[0].v_type == VAR_BLOB)
9697 {
9698 idx = (long)tv_get_number_chk(&argvars[1], &error);
9699 if (!error)
9700 {
9701 blob_T *b = argvars[0].vval.v_blob;
9702 int len = blob_len(b);
9703 char_u *p;
9704
9705 if (idx < 0)
9706 // count from the end
9707 idx = len + idx;
9708 if (idx < 0 || idx >= len)
9709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009710 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009711 return;
9712 }
9713 if (argvars[2].v_type == VAR_UNKNOWN)
9714 {
9715 // Remove one item, return its value.
9716 p = (char_u *)b->bv_ga.ga_data;
9717 rettv->vval.v_number = (varnumber_T) *(p + idx);
9718 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9719 --b->bv_ga.ga_len;
9720 }
9721 else
9722 {
9723 blob_T *blob;
9724
9725 // Remove range of items, return list with values.
9726 end = (long)tv_get_number_chk(&argvars[2], &error);
9727 if (error)
9728 return;
9729 if (end < 0)
9730 // count from the end
9731 end = len + end;
9732 if (end >= len || idx > end)
9733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009734 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009735 return;
9736 }
9737 blob = blob_alloc();
9738 if (blob == NULL)
9739 return;
9740 blob->bv_ga.ga_len = end - idx + 1;
9741 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9742 {
9743 vim_free(blob);
9744 return;
9745 }
9746 p = (char_u *)b->bv_ga.ga_data;
9747 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9748 (size_t)(end - idx + 1));
9749 ++blob->bv_refcount;
9750 rettv->v_type = VAR_BLOB;
9751 rettv->vval.v_blob = blob;
9752
9753 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9754 b->bv_ga.ga_len -= end - idx + 1;
9755 }
9756 }
9757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009759 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009761 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009763 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009765 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009766 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009767 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 else
9769 {
9770 if (argvars[2].v_type == VAR_UNKNOWN)
9771 {
9772 /* Remove one item, return its value. */
9773 vimlist_remove(l, item, item);
9774 *rettv = item->li_tv;
9775 vim_free(item);
9776 }
9777 else
9778 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009779 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009780 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009781 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009782 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009784 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 else
9786 {
9787 int cnt = 0;
9788
9789 for (li = item; li != NULL; li = li->li_next)
9790 {
9791 ++cnt;
9792 if (li == item2)
9793 break;
9794 }
9795 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009796 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009797 else
9798 {
9799 vimlist_remove(l, item, item2);
9800 if (rettv_list_alloc(rettv) == OK)
9801 {
9802 l = rettv->vval.v_list;
9803 l->lv_first = item;
9804 l->lv_last = item2;
9805 item->li_prev = NULL;
9806 item2->li_next = NULL;
9807 l->lv_len = cnt;
9808 }
9809 }
9810 }
9811 }
9812 }
9813 }
9814}
9815
9816/*
9817 * "rename({from}, {to})" function
9818 */
9819 static void
9820f_rename(typval_T *argvars, typval_T *rettv)
9821{
9822 char_u buf[NUMBUFLEN];
9823
9824 if (check_restricted() || check_secure())
9825 rettv->vval.v_number = -1;
9826 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009827 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9828 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009829}
9830
9831/*
9832 * "repeat()" function
9833 */
9834 static void
9835f_repeat(typval_T *argvars, typval_T *rettv)
9836{
9837 char_u *p;
9838 int n;
9839 int slen;
9840 int len;
9841 char_u *r;
9842 int i;
9843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009844 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 if (argvars[0].v_type == VAR_LIST)
9846 {
9847 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9848 while (n-- > 0)
9849 if (list_extend(rettv->vval.v_list,
9850 argvars[0].vval.v_list, NULL) == FAIL)
9851 break;
9852 }
9853 else
9854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009855 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 rettv->v_type = VAR_STRING;
9857 rettv->vval.v_string = NULL;
9858
9859 slen = (int)STRLEN(p);
9860 len = slen * n;
9861 if (len <= 0)
9862 return;
9863
9864 r = alloc(len + 1);
9865 if (r != NULL)
9866 {
9867 for (i = 0; i < n; i++)
9868 mch_memmove(r + i * slen, p, (size_t)slen);
9869 r[len] = NUL;
9870 }
9871
9872 rettv->vval.v_string = r;
9873 }
9874}
9875
9876/*
9877 * "resolve()" function
9878 */
9879 static void
9880f_resolve(typval_T *argvars, typval_T *rettv)
9881{
9882 char_u *p;
9883#ifdef HAVE_READLINK
9884 char_u *buf = NULL;
9885#endif
9886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009887 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888#ifdef FEAT_SHORTCUT
9889 {
9890 char_u *v = NULL;
9891
9892 v = mch_resolve_shortcut(p);
9893 if (v != NULL)
9894 rettv->vval.v_string = v;
9895 else
9896 rettv->vval.v_string = vim_strsave(p);
9897 }
9898#else
9899# ifdef HAVE_READLINK
9900 {
9901 char_u *cpy;
9902 int len;
9903 char_u *remain = NULL;
9904 char_u *q;
9905 int is_relative_to_current = FALSE;
9906 int has_trailing_pathsep = FALSE;
9907 int limit = 100;
9908
9909 p = vim_strsave(p);
9910
9911 if (p[0] == '.' && (vim_ispathsep(p[1])
9912 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9913 is_relative_to_current = TRUE;
9914
9915 len = STRLEN(p);
9916 if (len > 0 && after_pathsep(p, p + len))
9917 {
9918 has_trailing_pathsep = TRUE;
9919 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9920 }
9921
9922 q = getnextcomp(p);
9923 if (*q != NUL)
9924 {
9925 /* Separate the first path component in "p", and keep the
9926 * remainder (beginning with the path separator). */
9927 remain = vim_strsave(q - 1);
9928 q[-1] = NUL;
9929 }
9930
9931 buf = alloc(MAXPATHL + 1);
9932 if (buf == NULL)
9933 goto fail;
9934
9935 for (;;)
9936 {
9937 for (;;)
9938 {
9939 len = readlink((char *)p, (char *)buf, MAXPATHL);
9940 if (len <= 0)
9941 break;
9942 buf[len] = NUL;
9943
9944 if (limit-- == 0)
9945 {
9946 vim_free(p);
9947 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009948 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 rettv->vval.v_string = NULL;
9950 goto fail;
9951 }
9952
9953 /* Ensure that the result will have a trailing path separator
9954 * if the argument has one. */
9955 if (remain == NULL && has_trailing_pathsep)
9956 add_pathsep(buf);
9957
9958 /* Separate the first path component in the link value and
9959 * concatenate the remainders. */
9960 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9961 if (*q != NUL)
9962 {
9963 if (remain == NULL)
9964 remain = vim_strsave(q - 1);
9965 else
9966 {
9967 cpy = concat_str(q - 1, remain);
9968 if (cpy != NULL)
9969 {
9970 vim_free(remain);
9971 remain = cpy;
9972 }
9973 }
9974 q[-1] = NUL;
9975 }
9976
9977 q = gettail(p);
9978 if (q > p && *q == NUL)
9979 {
9980 /* Ignore trailing path separator. */
9981 q[-1] = NUL;
9982 q = gettail(p);
9983 }
9984 if (q > p && !mch_isFullName(buf))
9985 {
9986 /* symlink is relative to directory of argument */
9987 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9988 if (cpy != NULL)
9989 {
9990 STRCPY(cpy, p);
9991 STRCPY(gettail(cpy), buf);
9992 vim_free(p);
9993 p = cpy;
9994 }
9995 }
9996 else
9997 {
9998 vim_free(p);
9999 p = vim_strsave(buf);
10000 }
10001 }
10002
10003 if (remain == NULL)
10004 break;
10005
10006 /* Append the first path component of "remain" to "p". */
10007 q = getnextcomp(remain + 1);
10008 len = q - remain - (*q != NUL);
10009 cpy = vim_strnsave(p, STRLEN(p) + len);
10010 if (cpy != NULL)
10011 {
10012 STRNCAT(cpy, remain, len);
10013 vim_free(p);
10014 p = cpy;
10015 }
10016 /* Shorten "remain". */
10017 if (*q != NUL)
10018 STRMOVE(remain, q - 1);
10019 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010020 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 }
10022
10023 /* If the result is a relative path name, make it explicitly relative to
10024 * the current directory if and only if the argument had this form. */
10025 if (!vim_ispathsep(*p))
10026 {
10027 if (is_relative_to_current
10028 && *p != NUL
10029 && !(p[0] == '.'
10030 && (p[1] == NUL
10031 || vim_ispathsep(p[1])
10032 || (p[1] == '.'
10033 && (p[2] == NUL
10034 || vim_ispathsep(p[2]))))))
10035 {
10036 /* Prepend "./". */
10037 cpy = concat_str((char_u *)"./", p);
10038 if (cpy != NULL)
10039 {
10040 vim_free(p);
10041 p = cpy;
10042 }
10043 }
10044 else if (!is_relative_to_current)
10045 {
10046 /* Strip leading "./". */
10047 q = p;
10048 while (q[0] == '.' && vim_ispathsep(q[1]))
10049 q += 2;
10050 if (q > p)
10051 STRMOVE(p, p + 2);
10052 }
10053 }
10054
10055 /* Ensure that the result will have no trailing path separator
10056 * if the argument had none. But keep "/" or "//". */
10057 if (!has_trailing_pathsep)
10058 {
10059 q = p + STRLEN(p);
10060 if (after_pathsep(p, q))
10061 *gettail_sep(p) = NUL;
10062 }
10063
10064 rettv->vval.v_string = p;
10065 }
10066# else
10067 rettv->vval.v_string = vim_strsave(p);
10068# endif
10069#endif
10070
10071 simplify_filename(rettv->vval.v_string);
10072
10073#ifdef HAVE_READLINK
10074fail:
10075 vim_free(buf);
10076#endif
10077 rettv->v_type = VAR_STRING;
10078}
10079
10080/*
10081 * "reverse({list})" function
10082 */
10083 static void
10084f_reverse(typval_T *argvars, typval_T *rettv)
10085{
10086 list_T *l;
10087 listitem_T *li, *ni;
10088
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010089 if (argvars[0].v_type == VAR_BLOB)
10090 {
10091 blob_T *b = argvars[0].vval.v_blob;
10092 int i, len = blob_len(b);
10093
10094 for (i = 0; i < len / 2; i++)
10095 {
10096 int tmp = blob_get(b, i);
10097
10098 blob_set(b, i, blob_get(b, len - i - 1));
10099 blob_set(b, len - i - 1, tmp);
10100 }
10101 rettv_blob_set(rettv, b);
10102 return;
10103 }
10104
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010105 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010106 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 else if ((l = argvars[0].vval.v_list) != NULL
10108 && !tv_check_lock(l->lv_lock,
10109 (char_u *)N_("reverse() argument"), TRUE))
10110 {
10111 li = l->lv_last;
10112 l->lv_first = l->lv_last = NULL;
10113 l->lv_len = 0;
10114 while (li != NULL)
10115 {
10116 ni = li->li_prev;
10117 list_append(l, li);
10118 li = ni;
10119 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010120 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010121 l->lv_idx = l->lv_len - l->lv_idx - 1;
10122 }
10123}
10124
10125#define SP_NOMOVE 0x01 /* don't move cursor */
10126#define SP_REPEAT 0x02 /* repeat to find outer pair */
10127#define SP_RETCOUNT 0x04 /* return matchcount */
10128#define SP_SETPCMARK 0x08 /* set previous context mark */
10129#define SP_START 0x10 /* accept match at start position */
10130#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10131#define SP_END 0x40 /* leave cursor at end of match */
10132#define SP_COLUMN 0x80 /* start at cursor column */
10133
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010134/*
10135 * Get flags for a search function.
10136 * Possibly sets "p_ws".
10137 * Returns BACKWARD, FORWARD or zero (for an error).
10138 */
10139 static int
10140get_search_arg(typval_T *varp, int *flagsp)
10141{
10142 int dir = FORWARD;
10143 char_u *flags;
10144 char_u nbuf[NUMBUFLEN];
10145 int mask;
10146
10147 if (varp->v_type != VAR_UNKNOWN)
10148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010149 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 if (flags == NULL)
10151 return 0; /* type error; errmsg already given */
10152 while (*flags != NUL)
10153 {
10154 switch (*flags)
10155 {
10156 case 'b': dir = BACKWARD; break;
10157 case 'w': p_ws = TRUE; break;
10158 case 'W': p_ws = FALSE; break;
10159 default: mask = 0;
10160 if (flagsp != NULL)
10161 switch (*flags)
10162 {
10163 case 'c': mask = SP_START; break;
10164 case 'e': mask = SP_END; break;
10165 case 'm': mask = SP_RETCOUNT; break;
10166 case 'n': mask = SP_NOMOVE; break;
10167 case 'p': mask = SP_SUBPAT; break;
10168 case 'r': mask = SP_REPEAT; break;
10169 case 's': mask = SP_SETPCMARK; break;
10170 case 'z': mask = SP_COLUMN; break;
10171 }
10172 if (mask == 0)
10173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010174 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010175 dir = 0;
10176 }
10177 else
10178 *flagsp |= mask;
10179 }
10180 if (dir == 0)
10181 break;
10182 ++flags;
10183 }
10184 }
10185 return dir;
10186}
10187
10188/*
10189 * Shared by search() and searchpos() functions.
10190 */
10191 static int
10192search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10193{
10194 int flags;
10195 char_u *pat;
10196 pos_T pos;
10197 pos_T save_cursor;
10198 int save_p_ws = p_ws;
10199 int dir;
10200 int retval = 0; /* default: FAIL */
10201 long lnum_stop = 0;
10202 proftime_T tm;
10203#ifdef FEAT_RELTIME
10204 long time_limit = 0;
10205#endif
10206 int options = SEARCH_KEEP;
10207 int subpatnum;
10208
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010209 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010210 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10211 if (dir == 0)
10212 goto theend;
10213 flags = *flagsp;
10214 if (flags & SP_START)
10215 options |= SEARCH_START;
10216 if (flags & SP_END)
10217 options |= SEARCH_END;
10218 if (flags & SP_COLUMN)
10219 options |= SEARCH_COL;
10220
10221 /* Optional arguments: line number to stop searching and timeout. */
10222 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10223 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010224 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010225 if (lnum_stop < 0)
10226 goto theend;
10227#ifdef FEAT_RELTIME
10228 if (argvars[3].v_type != VAR_UNKNOWN)
10229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010230 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231 if (time_limit < 0)
10232 goto theend;
10233 }
10234#endif
10235 }
10236
10237#ifdef FEAT_RELTIME
10238 /* Set the time limit, if there is one. */
10239 profile_setlimit(time_limit, &tm);
10240#endif
10241
10242 /*
10243 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10244 * Check to make sure only those flags are set.
10245 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10246 * flags cannot be set. Check for that condition also.
10247 */
10248 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10249 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010251 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010252 goto theend;
10253 }
10254
10255 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010256 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010257 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 if (subpatnum != FAIL)
10259 {
10260 if (flags & SP_SUBPAT)
10261 retval = subpatnum;
10262 else
10263 retval = pos.lnum;
10264 if (flags & SP_SETPCMARK)
10265 setpcmark();
10266 curwin->w_cursor = pos;
10267 if (match_pos != NULL)
10268 {
10269 /* Store the match cursor position */
10270 match_pos->lnum = pos.lnum;
10271 match_pos->col = pos.col + 1;
10272 }
10273 /* "/$" will put the cursor after the end of the line, may need to
10274 * correct that here */
10275 check_cursor();
10276 }
10277
10278 /* If 'n' flag is used: restore cursor position. */
10279 if (flags & SP_NOMOVE)
10280 curwin->w_cursor = save_cursor;
10281 else
10282 curwin->w_set_curswant = TRUE;
10283theend:
10284 p_ws = save_p_ws;
10285
10286 return retval;
10287}
10288
10289#ifdef FEAT_FLOAT
10290
10291/*
10292 * round() is not in C90, use ceil() or floor() instead.
10293 */
10294 float_T
10295vim_round(float_T f)
10296{
10297 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10298}
10299
10300/*
10301 * "round({float})" function
10302 */
10303 static void
10304f_round(typval_T *argvars, typval_T *rettv)
10305{
10306 float_T f = 0.0;
10307
10308 rettv->v_type = VAR_FLOAT;
10309 if (get_float_arg(argvars, &f) == OK)
10310 rettv->vval.v_float = vim_round(f);
10311 else
10312 rettv->vval.v_float = 0.0;
10313}
10314#endif
10315
10316/*
10317 * "screenattr()" function
10318 */
10319 static void
10320f_screenattr(typval_T *argvars, typval_T *rettv)
10321{
10322 int row;
10323 int col;
10324 int c;
10325
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010326 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10327 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 if (row < 0 || row >= screen_Rows
10329 || col < 0 || col >= screen_Columns)
10330 c = -1;
10331 else
10332 c = ScreenAttrs[LineOffset[row] + col];
10333 rettv->vval.v_number = c;
10334}
10335
10336/*
10337 * "screenchar()" function
10338 */
10339 static void
10340f_screenchar(typval_T *argvars, typval_T *rettv)
10341{
10342 int row;
10343 int col;
10344 int off;
10345 int c;
10346
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010347 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10348 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010349 if (row < 0 || row >= screen_Rows
10350 || col < 0 || col >= screen_Columns)
10351 c = -1;
10352 else
10353 {
10354 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010355 if (enc_utf8 && ScreenLinesUC[off] != 0)
10356 c = ScreenLinesUC[off];
10357 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010358 c = ScreenLines[off];
10359 }
10360 rettv->vval.v_number = c;
10361}
10362
10363/*
10364 * "screencol()" function
10365 *
10366 * First column is 1 to be consistent with virtcol().
10367 */
10368 static void
10369f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10370{
10371 rettv->vval.v_number = screen_screencol() + 1;
10372}
10373
10374/*
10375 * "screenrow()" function
10376 */
10377 static void
10378f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10379{
10380 rettv->vval.v_number = screen_screenrow() + 1;
10381}
10382
10383/*
10384 * "search()" function
10385 */
10386 static void
10387f_search(typval_T *argvars, typval_T *rettv)
10388{
10389 int flags = 0;
10390
10391 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10392}
10393
10394/*
10395 * "searchdecl()" function
10396 */
10397 static void
10398f_searchdecl(typval_T *argvars, typval_T *rettv)
10399{
10400 int locally = 1;
10401 int thisblock = 0;
10402 int error = FALSE;
10403 char_u *name;
10404
10405 rettv->vval.v_number = 1; /* default: FAIL */
10406
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010407 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010408 if (argvars[1].v_type != VAR_UNKNOWN)
10409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010410 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010411 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010412 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413 }
10414 if (!error && name != NULL)
10415 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10416 locally, thisblock, SEARCH_KEEP) == FAIL;
10417}
10418
10419/*
10420 * Used by searchpair() and searchpairpos()
10421 */
10422 static int
10423searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10424{
10425 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010426 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010427 int save_p_ws = p_ws;
10428 int dir;
10429 int flags = 0;
10430 char_u nbuf1[NUMBUFLEN];
10431 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 int retval = 0; /* default: FAIL */
10433 long lnum_stop = 0;
10434 long time_limit = 0;
10435
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010436 /* Get the three pattern arguments: start, middle, end. Will result in an
10437 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010438 spat = tv_get_string_chk(&argvars[0]);
10439 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10440 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010441 if (spat == NULL || mpat == NULL || epat == NULL)
10442 goto theend; /* type error */
10443
10444 /* Handle the optional fourth argument: flags */
10445 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10446 if (dir == 0)
10447 goto theend;
10448
10449 /* Don't accept SP_END or SP_SUBPAT.
10450 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10451 */
10452 if ((flags & (SP_END | SP_SUBPAT)) != 0
10453 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010455 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010456 goto theend;
10457 }
10458
10459 /* Using 'r' implies 'W', otherwise it doesn't work. */
10460 if (flags & SP_REPEAT)
10461 p_ws = FALSE;
10462
10463 /* Optional fifth argument: skip expression */
10464 if (argvars[3].v_type == VAR_UNKNOWN
10465 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010466 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010467 else
10468 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010469 skip = &argvars[4];
10470 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10471 && skip->v_type != VAR_STRING)
10472 {
10473 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010474 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010475 goto theend;
10476 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 if (argvars[5].v_type != VAR_UNKNOWN)
10478 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010479 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010480 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010482 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010483 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010484 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485#ifdef FEAT_RELTIME
10486 if (argvars[6].v_type != VAR_UNKNOWN)
10487 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010488 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010489 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010491 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010492 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010493 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010494 }
10495#endif
10496 }
10497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010498
10499 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10500 match_pos, lnum_stop, time_limit);
10501
10502theend:
10503 p_ws = save_p_ws;
10504
10505 return retval;
10506}
10507
10508/*
10509 * "searchpair()" function
10510 */
10511 static void
10512f_searchpair(typval_T *argvars, typval_T *rettv)
10513{
10514 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10515}
10516
10517/*
10518 * "searchpairpos()" function
10519 */
10520 static void
10521f_searchpairpos(typval_T *argvars, typval_T *rettv)
10522{
10523 pos_T match_pos;
10524 int lnum = 0;
10525 int col = 0;
10526
10527 if (rettv_list_alloc(rettv) == FAIL)
10528 return;
10529
10530 if (searchpair_cmn(argvars, &match_pos) > 0)
10531 {
10532 lnum = match_pos.lnum;
10533 col = match_pos.col;
10534 }
10535
10536 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10537 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10538}
10539
10540/*
10541 * Search for a start/middle/end thing.
10542 * Used by searchpair(), see its documentation for the details.
10543 * Returns 0 or -1 for no match,
10544 */
10545 long
10546do_searchpair(
10547 char_u *spat, /* start pattern */
10548 char_u *mpat, /* middle pattern */
10549 char_u *epat, /* end pattern */
10550 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010551 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 int flags, /* SP_SETPCMARK and other SP_ values */
10553 pos_T *match_pos,
10554 linenr_T lnum_stop, /* stop at this line if not zero */
10555 long time_limit UNUSED) /* stop after this many msec */
10556{
10557 char_u *save_cpo;
10558 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10559 long retval = 0;
10560 pos_T pos;
10561 pos_T firstpos;
10562 pos_T foundpos;
10563 pos_T save_cursor;
10564 pos_T save_pos;
10565 int n;
10566 int r;
10567 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010568 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010569 int err;
10570 int options = SEARCH_KEEP;
10571 proftime_T tm;
10572
10573 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10574 save_cpo = p_cpo;
10575 p_cpo = empty_option;
10576
10577#ifdef FEAT_RELTIME
10578 /* Set the time limit, if there is one. */
10579 profile_setlimit(time_limit, &tm);
10580#endif
10581
10582 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10583 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010584 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10585 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 if (pat2 == NULL || pat3 == NULL)
10587 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010588 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010589 if (*mpat == NUL)
10590 STRCPY(pat3, pat2);
10591 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010592 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 spat, epat, mpat);
10594 if (flags & SP_START)
10595 options |= SEARCH_START;
10596
Bram Moolenaar48570482017-10-30 21:48:41 +010010597 if (skip != NULL)
10598 {
10599 /* Empty string means to not use the skip expression. */
10600 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10601 use_skip = skip->vval.v_string != NULL
10602 && *skip->vval.v_string != NUL;
10603 }
10604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010605 save_cursor = curwin->w_cursor;
10606 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010607 CLEAR_POS(&firstpos);
10608 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010609 pat = pat3;
10610 for (;;)
10611 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010612 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010613 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010614 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010615 /* didn't find it or found the first match again: FAIL */
10616 break;
10617
10618 if (firstpos.lnum == 0)
10619 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010620 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010621 {
10622 /* Found the same position again. Can happen with a pattern that
10623 * has "\zs" at the end and searching backwards. Advance one
10624 * character and try again. */
10625 if (dir == BACKWARD)
10626 decl(&pos);
10627 else
10628 incl(&pos);
10629 }
10630 foundpos = pos;
10631
10632 /* clear the start flag to avoid getting stuck here */
10633 options &= ~SEARCH_START;
10634
10635 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010636 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010637 {
10638 save_pos = curwin->w_cursor;
10639 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010640 err = FALSE;
10641 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 curwin->w_cursor = save_pos;
10643 if (err)
10644 {
10645 /* Evaluating {skip} caused an error, break here. */
10646 curwin->w_cursor = save_cursor;
10647 retval = -1;
10648 break;
10649 }
10650 if (r)
10651 continue;
10652 }
10653
10654 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10655 {
10656 /* Found end when searching backwards or start when searching
10657 * forward: nested pair. */
10658 ++nest;
10659 pat = pat2; /* nested, don't search for middle */
10660 }
10661 else
10662 {
10663 /* Found end when searching forward or start when searching
10664 * backward: end of (nested) pair; or found middle in outer pair. */
10665 if (--nest == 1)
10666 pat = pat3; /* outer level, search for middle */
10667 }
10668
10669 if (nest == 0)
10670 {
10671 /* Found the match: return matchcount or line number. */
10672 if (flags & SP_RETCOUNT)
10673 ++retval;
10674 else
10675 retval = pos.lnum;
10676 if (flags & SP_SETPCMARK)
10677 setpcmark();
10678 curwin->w_cursor = pos;
10679 if (!(flags & SP_REPEAT))
10680 break;
10681 nest = 1; /* search for next unmatched */
10682 }
10683 }
10684
10685 if (match_pos != NULL)
10686 {
10687 /* Store the match cursor position */
10688 match_pos->lnum = curwin->w_cursor.lnum;
10689 match_pos->col = curwin->w_cursor.col + 1;
10690 }
10691
10692 /* If 'n' flag is used or search failed: restore cursor position. */
10693 if ((flags & SP_NOMOVE) || retval == 0)
10694 curwin->w_cursor = save_cursor;
10695
10696theend:
10697 vim_free(pat2);
10698 vim_free(pat3);
10699 if (p_cpo == empty_option)
10700 p_cpo = save_cpo;
10701 else
10702 /* Darn, evaluating the {skip} expression changed the value. */
10703 free_string_option(save_cpo);
10704
10705 return retval;
10706}
10707
10708/*
10709 * "searchpos()" function
10710 */
10711 static void
10712f_searchpos(typval_T *argvars, typval_T *rettv)
10713{
10714 pos_T match_pos;
10715 int lnum = 0;
10716 int col = 0;
10717 int n;
10718 int flags = 0;
10719
10720 if (rettv_list_alloc(rettv) == FAIL)
10721 return;
10722
10723 n = search_cmn(argvars, &match_pos, &flags);
10724 if (n > 0)
10725 {
10726 lnum = match_pos.lnum;
10727 col = match_pos.col;
10728 }
10729
10730 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10731 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10732 if (flags & SP_SUBPAT)
10733 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10734}
10735
10736 static void
10737f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10738{
10739#ifdef FEAT_CLIENTSERVER
10740 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010741 char_u *server = tv_get_string_chk(&argvars[0]);
10742 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743
10744 rettv->vval.v_number = -1;
10745 if (server == NULL || reply == NULL)
10746 return;
10747 if (check_restricted() || check_secure())
10748 return;
10749# ifdef FEAT_X11
10750 if (check_connection() == FAIL)
10751 return;
10752# endif
10753
10754 if (serverSendReply(server, reply) < 0)
10755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010756 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010757 return;
10758 }
10759 rettv->vval.v_number = 0;
10760#else
10761 rettv->vval.v_number = -1;
10762#endif
10763}
10764
10765 static void
10766f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10767{
10768 char_u *r = NULL;
10769
10770#ifdef FEAT_CLIENTSERVER
10771# ifdef WIN32
10772 r = serverGetVimNames();
10773# else
10774 make_connection();
10775 if (X_DISPLAY != NULL)
10776 r = serverGetVimNames(X_DISPLAY);
10777# endif
10778#endif
10779 rettv->v_type = VAR_STRING;
10780 rettv->vval.v_string = r;
10781}
10782
10783/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010784 * "setbufline()" function
10785 */
10786 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010787f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010788{
10789 linenr_T lnum;
10790 buf_T *buf;
10791
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010792 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010793 if (buf == NULL)
10794 rettv->vval.v_number = 1; /* FAIL */
10795 else
10796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010797 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010798 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010799 }
10800}
10801
10802/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010803 * "setbufvar()" function
10804 */
10805 static void
10806f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10807{
10808 buf_T *buf;
10809 char_u *varname, *bufvarname;
10810 typval_T *varp;
10811 char_u nbuf[NUMBUFLEN];
10812
10813 if (check_restricted() || check_secure())
10814 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010815 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10816 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010817 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 varp = &argvars[2];
10819
10820 if (buf != NULL && varname != NULL && varp != NULL)
10821 {
10822 if (*varname == '&')
10823 {
10824 long numval;
10825 char_u *strval;
10826 int error = FALSE;
10827 aco_save_T aco;
10828
10829 /* set curbuf to be our buf, temporarily */
10830 aucmd_prepbuf(&aco, buf);
10831
10832 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010833 numval = (long)tv_get_number_chk(varp, &error);
10834 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010835 if (!error && strval != NULL)
10836 set_option_value(varname, numval, strval, OPT_LOCAL);
10837
10838 /* reset notion of buffer */
10839 aucmd_restbuf(&aco);
10840 }
10841 else
10842 {
10843 buf_T *save_curbuf = curbuf;
10844
10845 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10846 if (bufvarname != NULL)
10847 {
10848 curbuf = buf;
10849 STRCPY(bufvarname, "b:");
10850 STRCPY(bufvarname + 2, varname);
10851 set_var(bufvarname, varp, TRUE);
10852 vim_free(bufvarname);
10853 curbuf = save_curbuf;
10854 }
10855 }
10856 }
10857}
10858
10859 static void
10860f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10861{
10862 dict_T *d;
10863 dictitem_T *di;
10864 char_u *csearch;
10865
10866 if (argvars[0].v_type != VAR_DICT)
10867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010868 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010869 return;
10870 }
10871
10872 if ((d = argvars[0].vval.v_dict) != NULL)
10873 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010874 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 if (csearch != NULL)
10876 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877 if (enc_utf8)
10878 {
10879 int pcc[MAX_MCO];
10880 int c = utfc_ptr2char(csearch, pcc);
10881
10882 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10883 }
10884 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010885 set_last_csearch(PTR2CHAR(csearch),
10886 csearch, MB_PTR2LEN(csearch));
10887 }
10888
10889 di = dict_find(d, (char_u *)"forward", -1);
10890 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010891 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010892 ? FORWARD : BACKWARD);
10893
10894 di = dict_find(d, (char_u *)"until", -1);
10895 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010896 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 }
10898}
10899
10900/*
10901 * "setcmdpos()" function
10902 */
10903 static void
10904f_setcmdpos(typval_T *argvars, typval_T *rettv)
10905{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010906 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010907
10908 if (pos >= 0)
10909 rettv->vval.v_number = set_cmdline_pos(pos);
10910}
10911
10912/*
10913 * "setfperm({fname}, {mode})" function
10914 */
10915 static void
10916f_setfperm(typval_T *argvars, typval_T *rettv)
10917{
10918 char_u *fname;
10919 char_u modebuf[NUMBUFLEN];
10920 char_u *mode_str;
10921 int i;
10922 int mask;
10923 int mode = 0;
10924
10925 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 if (fname == NULL)
10928 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010929 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010930 if (mode_str == NULL)
10931 return;
10932 if (STRLEN(mode_str) != 9)
10933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010934 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 return;
10936 }
10937
10938 mask = 1;
10939 for (i = 8; i >= 0; --i)
10940 {
10941 if (mode_str[i] != '-')
10942 mode |= mask;
10943 mask = mask << 1;
10944 }
10945 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10946}
10947
10948/*
10949 * "setline()" function
10950 */
10951 static void
10952f_setline(typval_T *argvars, typval_T *rettv)
10953{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010954 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010955
Bram Moolenaarca851592018-06-06 21:04:07 +020010956 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957}
10958
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959/*
10960 * Used by "setqflist()" and "setloclist()" functions
10961 */
10962 static void
10963set_qf_ll_list(
10964 win_T *wp UNUSED,
10965 typval_T *list_arg UNUSED,
10966 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010967 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 typval_T *rettv)
10969{
10970#ifdef FEAT_QUICKFIX
10971 static char *e_invact = N_("E927: Invalid action: '%s'");
10972 char_u *act;
10973 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010974 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975#endif
10976
10977 rettv->vval.v_number = -1;
10978
10979#ifdef FEAT_QUICKFIX
10980 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010981 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010982 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010983 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 else
10985 {
10986 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010987 dict_T *d = NULL;
10988 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989
10990 if (action_arg->v_type == VAR_STRING)
10991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010992 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010993 if (act == NULL)
10994 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010995 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10996 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997 action = *act;
10998 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010999 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011000 }
11001 else if (action_arg->v_type == VAR_UNKNOWN)
11002 action = ' ';
11003 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011004 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005
Bram Moolenaard823fa92016-08-12 16:29:27 +020011006 if (action_arg->v_type != VAR_UNKNOWN
11007 && what_arg->v_type != VAR_UNKNOWN)
11008 {
11009 if (what_arg->v_type == VAR_DICT)
11010 d = what_arg->vval.v_dict;
11011 else
11012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011013 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011014 valid_dict = FALSE;
11015 }
11016 }
11017
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011018 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011019 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011020 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11021 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011022 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011023 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 }
11025#endif
11026}
11027
11028/*
11029 * "setloclist()" function
11030 */
11031 static void
11032f_setloclist(typval_T *argvars, typval_T *rettv)
11033{
11034 win_T *win;
11035
11036 rettv->vval.v_number = -1;
11037
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011038 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011039 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011040 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041}
11042
11043/*
11044 * "setmatches()" function
11045 */
11046 static void
11047f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11048{
11049#ifdef FEAT_SEARCH_EXTRA
11050 list_T *l;
11051 listitem_T *li;
11052 dict_T *d;
11053 list_T *s = NULL;
11054
11055 rettv->vval.v_number = -1;
11056 if (argvars[0].v_type != VAR_LIST)
11057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011058 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 return;
11060 }
11061 if ((l = argvars[0].vval.v_list) != NULL)
11062 {
11063
11064 /* To some extent make sure that we are dealing with a list from
11065 * "getmatches()". */
11066 li = l->lv_first;
11067 while (li != NULL)
11068 {
11069 if (li->li_tv.v_type != VAR_DICT
11070 || (d = li->li_tv.vval.v_dict) == NULL)
11071 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011072 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011073 return;
11074 }
11075 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11076 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11077 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11078 && dict_find(d, (char_u *)"priority", -1) != NULL
11079 && dict_find(d, (char_u *)"id", -1) != NULL))
11080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011081 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011082 return;
11083 }
11084 li = li->li_next;
11085 }
11086
11087 clear_matches(curwin);
11088 li = l->lv_first;
11089 while (li != NULL)
11090 {
11091 int i = 0;
11092 char_u buf[5];
11093 dictitem_T *di;
11094 char_u *group;
11095 int priority;
11096 int id;
11097 char_u *conceal;
11098
11099 d = li->li_tv.vval.v_dict;
11100 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11101 {
11102 if (s == NULL)
11103 {
11104 s = list_alloc();
11105 if (s == NULL)
11106 return;
11107 }
11108
11109 /* match from matchaddpos() */
11110 for (i = 1; i < 9; i++)
11111 {
11112 sprintf((char *)buf, (char *)"pos%d", i);
11113 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11114 {
11115 if (di->di_tv.v_type != VAR_LIST)
11116 return;
11117
11118 list_append_tv(s, &di->di_tv);
11119 s->lv_refcount++;
11120 }
11121 else
11122 break;
11123 }
11124 }
11125
Bram Moolenaar8f667172018-12-14 15:38:31 +010011126 group = dict_get_string(d, (char_u *)"group", TRUE);
11127 priority = (int)dict_get_number(d, (char_u *)"priority");
11128 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011130 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 : NULL;
11132 if (i == 0)
11133 {
11134 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011135 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 priority, id, NULL, conceal);
11137 }
11138 else
11139 {
11140 match_add(curwin, group, NULL, priority, id, s, conceal);
11141 list_unref(s);
11142 s = NULL;
11143 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011144 vim_free(group);
11145 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011146
11147 li = li->li_next;
11148 }
11149 rettv->vval.v_number = 0;
11150 }
11151#endif
11152}
11153
11154/*
11155 * "setpos()" function
11156 */
11157 static void
11158f_setpos(typval_T *argvars, typval_T *rettv)
11159{
11160 pos_T pos;
11161 int fnum;
11162 char_u *name;
11163 colnr_T curswant = -1;
11164
11165 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011166 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 if (name != NULL)
11168 {
11169 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11170 {
11171 if (--pos.col < 0)
11172 pos.col = 0;
11173 if (name[0] == '.' && name[1] == NUL)
11174 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011175 /* set cursor; "fnum" is ignored */
11176 curwin->w_cursor = pos;
11177 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011179 curwin->w_curswant = curswant - 1;
11180 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011181 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011182 check_cursor();
11183 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184 }
11185 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11186 {
11187 /* set mark */
11188 if (setmark_pos(name[1], &pos, fnum) == OK)
11189 rettv->vval.v_number = 0;
11190 }
11191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011192 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193 }
11194 }
11195}
11196
11197/*
11198 * "setqflist()" function
11199 */
11200 static void
11201f_setqflist(typval_T *argvars, typval_T *rettv)
11202{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011203 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204}
11205
11206/*
11207 * "setreg()" function
11208 */
11209 static void
11210f_setreg(typval_T *argvars, typval_T *rettv)
11211{
11212 int regname;
11213 char_u *strregname;
11214 char_u *stropt;
11215 char_u *strval;
11216 int append;
11217 char_u yank_type;
11218 long block_len;
11219
11220 block_len = -1;
11221 yank_type = MAUTO;
11222 append = FALSE;
11223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011224 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011225 rettv->vval.v_number = 1; /* FAIL is default */
11226
11227 if (strregname == NULL)
11228 return; /* type error; errmsg already given */
11229 regname = *strregname;
11230 if (regname == 0 || regname == '@')
11231 regname = '"';
11232
11233 if (argvars[2].v_type != VAR_UNKNOWN)
11234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011235 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 if (stropt == NULL)
11237 return; /* type error */
11238 for (; *stropt != NUL; ++stropt)
11239 switch (*stropt)
11240 {
11241 case 'a': case 'A': /* append */
11242 append = TRUE;
11243 break;
11244 case 'v': case 'c': /* character-wise selection */
11245 yank_type = MCHAR;
11246 break;
11247 case 'V': case 'l': /* line-wise selection */
11248 yank_type = MLINE;
11249 break;
11250 case 'b': case Ctrl_V: /* block-wise selection */
11251 yank_type = MBLOCK;
11252 if (VIM_ISDIGIT(stropt[1]))
11253 {
11254 ++stropt;
11255 block_len = getdigits(&stropt) - 1;
11256 --stropt;
11257 }
11258 break;
11259 }
11260 }
11261
11262 if (argvars[1].v_type == VAR_LIST)
11263 {
11264 char_u **lstval;
11265 char_u **allocval;
11266 char_u buf[NUMBUFLEN];
11267 char_u **curval;
11268 char_u **curallocval;
11269 list_T *ll = argvars[1].vval.v_list;
11270 listitem_T *li;
11271 int len;
11272
11273 /* If the list is NULL handle like an empty list. */
11274 len = ll == NULL ? 0 : ll->lv_len;
11275
11276 /* First half: use for pointers to result lines; second half: use for
11277 * pointers to allocated copies. */
11278 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11279 if (lstval == NULL)
11280 return;
11281 curval = lstval;
11282 allocval = lstval + len + 2;
11283 curallocval = allocval;
11284
11285 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11286 li = li->li_next)
11287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011288 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011289 if (strval == NULL)
11290 goto free_lstval;
11291 if (strval == buf)
11292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011293 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011294 * overwrite the string. */
11295 strval = vim_strsave(buf);
11296 if (strval == NULL)
11297 goto free_lstval;
11298 *curallocval++ = strval;
11299 }
11300 *curval++ = strval;
11301 }
11302 *curval++ = NULL;
11303
11304 write_reg_contents_lst(regname, lstval, -1,
11305 append, yank_type, block_len);
11306free_lstval:
11307 while (curallocval > allocval)
11308 vim_free(*--curallocval);
11309 vim_free(lstval);
11310 }
11311 else
11312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011313 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314 if (strval == NULL)
11315 return;
11316 write_reg_contents_ex(regname, strval, -1,
11317 append, yank_type, block_len);
11318 }
11319 rettv->vval.v_number = 0;
11320}
11321
11322/*
11323 * "settabvar()" function
11324 */
11325 static void
11326f_settabvar(typval_T *argvars, typval_T *rettv)
11327{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011328 tabpage_T *save_curtab;
11329 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 char_u *varname, *tabvarname;
11331 typval_T *varp;
11332
11333 rettv->vval.v_number = 0;
11334
11335 if (check_restricted() || check_secure())
11336 return;
11337
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011338 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11339 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011340 varp = &argvars[2];
11341
Bram Moolenaar4033c552017-09-16 20:54:51 +020011342 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011343 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011344 save_curtab = curtab;
11345 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011346
11347 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11348 if (tabvarname != NULL)
11349 {
11350 STRCPY(tabvarname, "t:");
11351 STRCPY(tabvarname + 2, varname);
11352 set_var(tabvarname, varp, TRUE);
11353 vim_free(tabvarname);
11354 }
11355
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011356 /* Restore current tabpage */
11357 if (valid_tabpage(save_curtab))
11358 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011359 }
11360}
11361
11362/*
11363 * "settabwinvar()" function
11364 */
11365 static void
11366f_settabwinvar(typval_T *argvars, typval_T *rettv)
11367{
11368 setwinvar(argvars, rettv, 1);
11369}
11370
11371/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011372 * "settagstack()" function
11373 */
11374 static void
11375f_settagstack(typval_T *argvars, typval_T *rettv)
11376{
11377 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11378 win_T *wp;
11379 dict_T *d;
11380 int action = 'r';
11381
11382 rettv->vval.v_number = -1;
11383
11384 // first argument: window number or id
11385 wp = find_win_by_nr_or_id(&argvars[0]);
11386 if (wp == NULL)
11387 return;
11388
11389 // second argument: dict with items to set in the tag stack
11390 if (argvars[1].v_type != VAR_DICT)
11391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011392 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011393 return;
11394 }
11395 d = argvars[1].vval.v_dict;
11396 if (d == NULL)
11397 return;
11398
11399 // third argument: action - 'a' for append and 'r' for replace.
11400 // default is to replace the stack.
11401 if (argvars[2].v_type == VAR_UNKNOWN)
11402 action = 'r';
11403 else if (argvars[2].v_type == VAR_STRING)
11404 {
11405 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011406 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011407 if (actstr == NULL)
11408 return;
11409 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11410 action = *actstr;
11411 else
11412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011413 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011414 return;
11415 }
11416 }
11417 else
11418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011419 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011420 return;
11421 }
11422
11423 if (set_tagstack(wp, d, action) == OK)
11424 rettv->vval.v_number = 0;
11425}
11426
11427/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011428 * "setwinvar()" function
11429 */
11430 static void
11431f_setwinvar(typval_T *argvars, typval_T *rettv)
11432{
11433 setwinvar(argvars, rettv, 0);
11434}
11435
11436#ifdef FEAT_CRYPT
11437/*
11438 * "sha256({string})" function
11439 */
11440 static void
11441f_sha256(typval_T *argvars, typval_T *rettv)
11442{
11443 char_u *p;
11444
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011445 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 rettv->vval.v_string = vim_strsave(
11447 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11448 rettv->v_type = VAR_STRING;
11449}
11450#endif /* FEAT_CRYPT */
11451
11452/*
11453 * "shellescape({string})" function
11454 */
11455 static void
11456f_shellescape(typval_T *argvars, typval_T *rettv)
11457{
Bram Moolenaar20615522017-06-05 18:46:26 +020011458 int do_special = non_zero_arg(&argvars[1]);
11459
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011461 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011462 rettv->v_type = VAR_STRING;
11463}
11464
11465/*
11466 * shiftwidth() function
11467 */
11468 static void
11469f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11470{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011471 rettv->vval.v_number = 0;
11472
11473 if (argvars[0].v_type != VAR_UNKNOWN)
11474 {
11475 long col;
11476
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011477 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011478 if (col < 0)
11479 return; // type error; errmsg already given
11480#ifdef FEAT_VARTABS
11481 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11482 return;
11483#endif
11484 }
11485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 rettv->vval.v_number = get_sw_value(curbuf);
11487}
11488
Bram Moolenaar162b7142018-12-21 15:17:36 +010011489#ifdef FEAT_SIGNS
11490/*
11491 * "sign_define()" function
11492 */
11493 static void
11494f_sign_define(typval_T *argvars, typval_T *rettv)
11495{
11496 char_u *name;
11497 dict_T *dict;
11498 char_u *icon = NULL;
11499 char_u *linehl = NULL;
11500 char_u *text = NULL;
11501 char_u *texthl = NULL;
11502
11503 rettv->vval.v_number = -1;
11504
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011505 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011506 if (name == NULL)
11507 return;
11508
11509 if (argvars[1].v_type != VAR_UNKNOWN)
11510 {
11511 if (argvars[1].v_type != VAR_DICT)
11512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011513 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011514 return;
11515 }
11516
11517 // sign attributes
11518 dict = argvars[1].vval.v_dict;
11519 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11520 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11521 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11522 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11523 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11524 text = dict_get_string(dict, (char_u *)"text", TRUE);
11525 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11526 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11527 }
11528
11529 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11530 rettv->vval.v_number = 0;
11531
11532 vim_free(icon);
11533 vim_free(linehl);
11534 vim_free(text);
11535 vim_free(texthl);
11536}
11537
11538/*
11539 * "sign_getdefined()" function
11540 */
11541 static void
11542f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11543{
11544 char_u *name = NULL;
11545
11546 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11547 return;
11548
11549 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011550 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011551
11552 sign_getlist(name, rettv->vval.v_list);
11553}
11554
11555/*
11556 * "sign_getplaced()" function
11557 */
11558 static void
11559f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11560{
11561 buf_T *buf = NULL;
11562 dict_T *dict;
11563 dictitem_T *di;
11564 linenr_T lnum = 0;
11565 int sign_id = 0;
11566 char_u *group = NULL;
11567 int notanum = FALSE;
11568
11569 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11570 return;
11571
11572 if (argvars[0].v_type != VAR_UNKNOWN)
11573 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011574 // get signs placed in the specified buffer
11575 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011576 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011577 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011578
11579 if (argvars[1].v_type != VAR_UNKNOWN)
11580 {
11581 if (argvars[1].v_type != VAR_DICT ||
11582 ((dict = argvars[1].vval.v_dict) == NULL))
11583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011584 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011585 return;
11586 }
11587 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11588 {
11589 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011590 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011591 if (notanum)
11592 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011593 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011594 }
11595 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11596 {
11597 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011598 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011599 if (notanum)
11600 return;
11601 }
11602 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011604 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011605 if (group == NULL)
11606 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011607 if (*group == '\0') // empty string means global group
11608 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011609 }
11610 }
11611 }
11612
11613 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11614}
11615
11616/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011617 * "sign_jump()" function
11618 */
11619 static void
11620f_sign_jump(typval_T *argvars, typval_T *rettv)
11621{
11622 int sign_id;
11623 char_u *sign_group = NULL;
11624 buf_T *buf;
11625 int notanum = FALSE;
11626
11627 rettv->vval.v_number = -1;
11628
11629 // Sign identifer
11630 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11631 if (notanum)
11632 return;
11633 if (sign_id <= 0)
11634 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011635 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011636 return;
11637 }
11638
11639 // Sign group
11640 sign_group = tv_get_string_chk(&argvars[1]);
11641 if (sign_group == NULL)
11642 return;
11643 if (sign_group[0] == '\0')
11644 sign_group = NULL; // global sign group
11645 else
11646 {
11647 sign_group = vim_strsave(sign_group);
11648 if (sign_group == NULL)
11649 return;
11650 }
11651
11652 // Buffer to place the sign
11653 buf = get_buf_arg(&argvars[2]);
11654 if (buf == NULL)
11655 goto cleanup;
11656
11657 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11658
11659cleanup:
11660 vim_free(sign_group);
11661}
11662
11663/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011664 * "sign_place()" function
11665 */
11666 static void
11667f_sign_place(typval_T *argvars, typval_T *rettv)
11668{
11669 int sign_id;
11670 char_u *group = NULL;
11671 char_u *sign_name;
11672 buf_T *buf;
11673 dict_T *dict;
11674 dictitem_T *di;
11675 linenr_T lnum = 0;
11676 int prio = SIGN_DEF_PRIO;
11677 int notanum = FALSE;
11678
11679 rettv->vval.v_number = -1;
11680
11681 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011682 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011683 if (notanum)
11684 return;
11685 if (sign_id < 0)
11686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011687 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011688 return;
11689 }
11690
11691 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011692 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011693 if (group == NULL)
11694 return;
11695 if (group[0] == '\0')
11696 group = NULL; // global sign group
11697 else
11698 {
11699 group = vim_strsave(group);
11700 if (group == NULL)
11701 return;
11702 }
11703
11704 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011705 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011706 if (sign_name == NULL)
11707 goto cleanup;
11708
11709 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011710 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011711 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011712 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011713
11714 if (argvars[4].v_type != VAR_UNKNOWN)
11715 {
11716 if (argvars[4].v_type != VAR_DICT ||
11717 ((dict = argvars[4].vval.v_dict) == NULL))
11718 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011719 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011720 goto cleanup;
11721 }
11722
11723 // Line number where the sign is to be placed
11724 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11725 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011726 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011727 if (notanum)
11728 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011729 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011730 }
11731 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11732 {
11733 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011734 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011735 if (notanum)
11736 goto cleanup;
11737 }
11738 }
11739
11740 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11741 rettv->vval.v_number = sign_id;
11742
11743cleanup:
11744 vim_free(group);
11745}
11746
11747/*
11748 * "sign_undefine()" function
11749 */
11750 static void
11751f_sign_undefine(typval_T *argvars, typval_T *rettv)
11752{
11753 char_u *name;
11754
11755 rettv->vval.v_number = -1;
11756
11757 if (argvars[0].v_type == VAR_UNKNOWN)
11758 {
11759 // Free all the signs
11760 free_signs();
11761 rettv->vval.v_number = 0;
11762 }
11763 else
11764 {
11765 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011766 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011767 if (name == NULL)
11768 return;
11769
11770 if (sign_undefine_by_name(name) == OK)
11771 rettv->vval.v_number = 0;
11772 }
11773}
11774
11775/*
11776 * "sign_unplace()" function
11777 */
11778 static void
11779f_sign_unplace(typval_T *argvars, typval_T *rettv)
11780{
11781 dict_T *dict;
11782 dictitem_T *di;
11783 int sign_id = 0;
11784 buf_T *buf = NULL;
11785 char_u *group = NULL;
11786
11787 rettv->vval.v_number = -1;
11788
11789 if (argvars[0].v_type != VAR_STRING)
11790 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011791 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011792 return;
11793 }
11794
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011795 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011796 if (group[0] == '\0')
11797 group = NULL; // global sign group
11798 else
11799 {
11800 group = vim_strsave(group);
11801 if (group == NULL)
11802 return;
11803 }
11804
11805 if (argvars[1].v_type != VAR_UNKNOWN)
11806 {
11807 if (argvars[1].v_type != VAR_DICT)
11808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011809 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011810 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011811 }
11812 dict = argvars[1].vval.v_dict;
11813
11814 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11815 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011816 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011817 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011818 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011819 }
11820 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11821 sign_id = dict_get_number(dict, (char_u *)"id");
11822 }
11823
11824 if (buf == NULL)
11825 {
11826 // Delete the sign in all the buffers
11827 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011828 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011829 rettv->vval.v_number = 0;
11830 }
11831 else
11832 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011833 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011834 rettv->vval.v_number = 0;
11835 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011836
11837cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011838 vim_free(group);
11839}
11840#endif
11841
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842/*
11843 * "simplify()" function
11844 */
11845 static void
11846f_simplify(typval_T *argvars, typval_T *rettv)
11847{
11848 char_u *p;
11849
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011850 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011851 rettv->vval.v_string = vim_strsave(p);
11852 simplify_filename(rettv->vval.v_string); /* simplify in place */
11853 rettv->v_type = VAR_STRING;
11854}
11855
11856#ifdef FEAT_FLOAT
11857/*
11858 * "sin()" function
11859 */
11860 static void
11861f_sin(typval_T *argvars, typval_T *rettv)
11862{
11863 float_T f = 0.0;
11864
11865 rettv->v_type = VAR_FLOAT;
11866 if (get_float_arg(argvars, &f) == OK)
11867 rettv->vval.v_float = sin(f);
11868 else
11869 rettv->vval.v_float = 0.0;
11870}
11871
11872/*
11873 * "sinh()" function
11874 */
11875 static void
11876f_sinh(typval_T *argvars, typval_T *rettv)
11877{
11878 float_T f = 0.0;
11879
11880 rettv->v_type = VAR_FLOAT;
11881 if (get_float_arg(argvars, &f) == OK)
11882 rettv->vval.v_float = sinh(f);
11883 else
11884 rettv->vval.v_float = 0.0;
11885}
11886#endif
11887
11888static int
11889#ifdef __BORLANDC__
11890 _RTLENTRYF
11891#endif
11892 item_compare(const void *s1, const void *s2);
11893static int
11894#ifdef __BORLANDC__
11895 _RTLENTRYF
11896#endif
11897 item_compare2(const void *s1, const void *s2);
11898
11899/* struct used in the array that's given to qsort() */
11900typedef struct
11901{
11902 listitem_T *item;
11903 int idx;
11904} sortItem_T;
11905
11906/* struct storing information about current sort */
11907typedef struct
11908{
11909 int item_compare_ic;
11910 int item_compare_numeric;
11911 int item_compare_numbers;
11912#ifdef FEAT_FLOAT
11913 int item_compare_float;
11914#endif
11915 char_u *item_compare_func;
11916 partial_T *item_compare_partial;
11917 dict_T *item_compare_selfdict;
11918 int item_compare_func_err;
11919 int item_compare_keep_zero;
11920} sortinfo_T;
11921static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011922#define ITEM_COMPARE_FAIL 999
11923
11924/*
11925 * Compare functions for f_sort() and f_uniq() below.
11926 */
11927 static int
11928#ifdef __BORLANDC__
11929_RTLENTRYF
11930#endif
11931item_compare(const void *s1, const void *s2)
11932{
11933 sortItem_T *si1, *si2;
11934 typval_T *tv1, *tv2;
11935 char_u *p1, *p2;
11936 char_u *tofree1 = NULL, *tofree2 = NULL;
11937 int res;
11938 char_u numbuf1[NUMBUFLEN];
11939 char_u numbuf2[NUMBUFLEN];
11940
11941 si1 = (sortItem_T *)s1;
11942 si2 = (sortItem_T *)s2;
11943 tv1 = &si1->item->li_tv;
11944 tv2 = &si2->item->li_tv;
11945
11946 if (sortinfo->item_compare_numbers)
11947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011948 varnumber_T v1 = tv_get_number(tv1);
11949 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011950
11951 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11952 }
11953
11954#ifdef FEAT_FLOAT
11955 if (sortinfo->item_compare_float)
11956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011957 float_T v1 = tv_get_float(tv1);
11958 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011959
11960 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11961 }
11962#endif
11963
11964 /* tv2string() puts quotes around a string and allocates memory. Don't do
11965 * that for string variables. Use a single quote when comparing with a
11966 * non-string to do what the docs promise. */
11967 if (tv1->v_type == VAR_STRING)
11968 {
11969 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11970 p1 = (char_u *)"'";
11971 else
11972 p1 = tv1->vval.v_string;
11973 }
11974 else
11975 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11976 if (tv2->v_type == VAR_STRING)
11977 {
11978 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11979 p2 = (char_u *)"'";
11980 else
11981 p2 = tv2->vval.v_string;
11982 }
11983 else
11984 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11985 if (p1 == NULL)
11986 p1 = (char_u *)"";
11987 if (p2 == NULL)
11988 p2 = (char_u *)"";
11989 if (!sortinfo->item_compare_numeric)
11990 {
11991 if (sortinfo->item_compare_ic)
11992 res = STRICMP(p1, p2);
11993 else
11994 res = STRCMP(p1, p2);
11995 }
11996 else
11997 {
11998 double n1, n2;
11999 n1 = strtod((char *)p1, (char **)&p1);
12000 n2 = strtod((char *)p2, (char **)&p2);
12001 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12002 }
12003
12004 /* When the result would be zero, compare the item indexes. Makes the
12005 * sort stable. */
12006 if (res == 0 && !sortinfo->item_compare_keep_zero)
12007 res = si1->idx > si2->idx ? 1 : -1;
12008
12009 vim_free(tofree1);
12010 vim_free(tofree2);
12011 return res;
12012}
12013
12014 static int
12015#ifdef __BORLANDC__
12016_RTLENTRYF
12017#endif
12018item_compare2(const void *s1, const void *s2)
12019{
12020 sortItem_T *si1, *si2;
12021 int res;
12022 typval_T rettv;
12023 typval_T argv[3];
12024 int dummy;
12025 char_u *func_name;
12026 partial_T *partial = sortinfo->item_compare_partial;
12027
12028 /* shortcut after failure in previous call; compare all items equal */
12029 if (sortinfo->item_compare_func_err)
12030 return 0;
12031
12032 si1 = (sortItem_T *)s1;
12033 si2 = (sortItem_T *)s2;
12034
12035 if (partial == NULL)
12036 func_name = sortinfo->item_compare_func;
12037 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012038 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012039
12040 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12041 * in the copy without changing the original list items. */
12042 copy_tv(&si1->item->li_tv, &argv[0]);
12043 copy_tv(&si2->item->li_tv, &argv[1]);
12044
12045 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12046 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012047 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012048 partial, sortinfo->item_compare_selfdict);
12049 clear_tv(&argv[0]);
12050 clear_tv(&argv[1]);
12051
12052 if (res == FAIL)
12053 res = ITEM_COMPARE_FAIL;
12054 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012055 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012056 if (sortinfo->item_compare_func_err)
12057 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12058 clear_tv(&rettv);
12059
12060 /* When the result would be zero, compare the pointers themselves. Makes
12061 * the sort stable. */
12062 if (res == 0 && !sortinfo->item_compare_keep_zero)
12063 res = si1->idx > si2->idx ? 1 : -1;
12064
12065 return res;
12066}
12067
12068/*
12069 * "sort({list})" function
12070 */
12071 static void
12072do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12073{
12074 list_T *l;
12075 listitem_T *li;
12076 sortItem_T *ptrs;
12077 sortinfo_T *old_sortinfo;
12078 sortinfo_T info;
12079 long len;
12080 long i;
12081
12082 /* Pointer to current info struct used in compare function. Save and
12083 * restore the current one for nested calls. */
12084 old_sortinfo = sortinfo;
12085 sortinfo = &info;
12086
12087 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012088 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012089 else
12090 {
12091 l = argvars[0].vval.v_list;
12092 if (l == NULL || tv_check_lock(l->lv_lock,
12093 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12094 TRUE))
12095 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012096 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012097
12098 len = list_len(l);
12099 if (len <= 1)
12100 goto theend; /* short list sorts pretty quickly */
12101
12102 info.item_compare_ic = FALSE;
12103 info.item_compare_numeric = FALSE;
12104 info.item_compare_numbers = FALSE;
12105#ifdef FEAT_FLOAT
12106 info.item_compare_float = FALSE;
12107#endif
12108 info.item_compare_func = NULL;
12109 info.item_compare_partial = NULL;
12110 info.item_compare_selfdict = NULL;
12111 if (argvars[1].v_type != VAR_UNKNOWN)
12112 {
12113 /* optional second argument: {func} */
12114 if (argvars[1].v_type == VAR_FUNC)
12115 info.item_compare_func = argvars[1].vval.v_string;
12116 else if (argvars[1].v_type == VAR_PARTIAL)
12117 info.item_compare_partial = argvars[1].vval.v_partial;
12118 else
12119 {
12120 int error = FALSE;
12121
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012122 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012123 if (error)
12124 goto theend; /* type error; errmsg already given */
12125 if (i == 1)
12126 info.item_compare_ic = TRUE;
12127 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012128 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012129 else if (i != 0)
12130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012131 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012132 goto theend;
12133 }
12134 if (info.item_compare_func != NULL)
12135 {
12136 if (*info.item_compare_func == NUL)
12137 {
12138 /* empty string means default sort */
12139 info.item_compare_func = NULL;
12140 }
12141 else if (STRCMP(info.item_compare_func, "n") == 0)
12142 {
12143 info.item_compare_func = NULL;
12144 info.item_compare_numeric = TRUE;
12145 }
12146 else if (STRCMP(info.item_compare_func, "N") == 0)
12147 {
12148 info.item_compare_func = NULL;
12149 info.item_compare_numbers = TRUE;
12150 }
12151#ifdef FEAT_FLOAT
12152 else if (STRCMP(info.item_compare_func, "f") == 0)
12153 {
12154 info.item_compare_func = NULL;
12155 info.item_compare_float = TRUE;
12156 }
12157#endif
12158 else if (STRCMP(info.item_compare_func, "i") == 0)
12159 {
12160 info.item_compare_func = NULL;
12161 info.item_compare_ic = TRUE;
12162 }
12163 }
12164 }
12165
12166 if (argvars[2].v_type != VAR_UNKNOWN)
12167 {
12168 /* optional third argument: {dict} */
12169 if (argvars[2].v_type != VAR_DICT)
12170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012171 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012172 goto theend;
12173 }
12174 info.item_compare_selfdict = argvars[2].vval.v_dict;
12175 }
12176 }
12177
12178 /* Make an array with each entry pointing to an item in the List. */
12179 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12180 if (ptrs == NULL)
12181 goto theend;
12182
12183 i = 0;
12184 if (sort)
12185 {
12186 /* sort(): ptrs will be the list to sort */
12187 for (li = l->lv_first; li != NULL; li = li->li_next)
12188 {
12189 ptrs[i].item = li;
12190 ptrs[i].idx = i;
12191 ++i;
12192 }
12193
12194 info.item_compare_func_err = FALSE;
12195 info.item_compare_keep_zero = FALSE;
12196 /* test the compare function */
12197 if ((info.item_compare_func != NULL
12198 || info.item_compare_partial != NULL)
12199 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12200 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012201 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012202 else
12203 {
12204 /* Sort the array with item pointers. */
12205 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12206 info.item_compare_func == NULL
12207 && info.item_compare_partial == NULL
12208 ? item_compare : item_compare2);
12209
12210 if (!info.item_compare_func_err)
12211 {
12212 /* Clear the List and append the items in sorted order. */
12213 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12214 l->lv_len = 0;
12215 for (i = 0; i < len; ++i)
12216 list_append(l, ptrs[i].item);
12217 }
12218 }
12219 }
12220 else
12221 {
12222 int (*item_compare_func_ptr)(const void *, const void *);
12223
12224 /* f_uniq(): ptrs will be a stack of items to remove */
12225 info.item_compare_func_err = FALSE;
12226 info.item_compare_keep_zero = TRUE;
12227 item_compare_func_ptr = info.item_compare_func != NULL
12228 || info.item_compare_partial != NULL
12229 ? item_compare2 : item_compare;
12230
12231 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12232 li = li->li_next)
12233 {
12234 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12235 == 0)
12236 ptrs[i++].item = li;
12237 if (info.item_compare_func_err)
12238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012239 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012240 break;
12241 }
12242 }
12243
12244 if (!info.item_compare_func_err)
12245 {
12246 while (--i >= 0)
12247 {
12248 li = ptrs[i].item->li_next;
12249 ptrs[i].item->li_next = li->li_next;
12250 if (li->li_next != NULL)
12251 li->li_next->li_prev = ptrs[i].item;
12252 else
12253 l->lv_last = ptrs[i].item;
12254 list_fix_watch(l, li);
12255 listitem_free(li);
12256 l->lv_len--;
12257 }
12258 }
12259 }
12260
12261 vim_free(ptrs);
12262 }
12263theend:
12264 sortinfo = old_sortinfo;
12265}
12266
12267/*
12268 * "sort({list})" function
12269 */
12270 static void
12271f_sort(typval_T *argvars, typval_T *rettv)
12272{
12273 do_sort_uniq(argvars, rettv, TRUE);
12274}
12275
12276/*
12277 * "uniq({list})" function
12278 */
12279 static void
12280f_uniq(typval_T *argvars, typval_T *rettv)
12281{
12282 do_sort_uniq(argvars, rettv, FALSE);
12283}
12284
12285/*
12286 * "soundfold({word})" function
12287 */
12288 static void
12289f_soundfold(typval_T *argvars, typval_T *rettv)
12290{
12291 char_u *s;
12292
12293 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012294 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012295#ifdef FEAT_SPELL
12296 rettv->vval.v_string = eval_soundfold(s);
12297#else
12298 rettv->vval.v_string = vim_strsave(s);
12299#endif
12300}
12301
12302/*
12303 * "spellbadword()" function
12304 */
12305 static void
12306f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12307{
12308 char_u *word = (char_u *)"";
12309 hlf_T attr = HLF_COUNT;
12310 int len = 0;
12311
12312 if (rettv_list_alloc(rettv) == FAIL)
12313 return;
12314
12315#ifdef FEAT_SPELL
12316 if (argvars[0].v_type == VAR_UNKNOWN)
12317 {
12318 /* Find the start and length of the badly spelled word. */
12319 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12320 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012321 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012322 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012323 curwin->w_set_curswant = TRUE;
12324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012325 }
12326 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012328 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012329 int capcol = -1;
12330
12331 if (str != NULL)
12332 {
12333 /* Check the argument for spelling. */
12334 while (*str != NUL)
12335 {
12336 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12337 if (attr != HLF_COUNT)
12338 {
12339 word = str;
12340 break;
12341 }
12342 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012343 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012344 }
12345 }
12346 }
12347#endif
12348
12349 list_append_string(rettv->vval.v_list, word, len);
12350 list_append_string(rettv->vval.v_list, (char_u *)(
12351 attr == HLF_SPB ? "bad" :
12352 attr == HLF_SPR ? "rare" :
12353 attr == HLF_SPL ? "local" :
12354 attr == HLF_SPC ? "caps" :
12355 ""), -1);
12356}
12357
12358/*
12359 * "spellsuggest()" function
12360 */
12361 static void
12362f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12363{
12364#ifdef FEAT_SPELL
12365 char_u *str;
12366 int typeerr = FALSE;
12367 int maxcount;
12368 garray_T ga;
12369 int i;
12370 listitem_T *li;
12371 int need_capital = FALSE;
12372#endif
12373
12374 if (rettv_list_alloc(rettv) == FAIL)
12375 return;
12376
12377#ifdef FEAT_SPELL
12378 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012380 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012381 if (argvars[1].v_type != VAR_UNKNOWN)
12382 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012383 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012384 if (maxcount <= 0)
12385 return;
12386 if (argvars[2].v_type != VAR_UNKNOWN)
12387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012388 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012389 if (typeerr)
12390 return;
12391 }
12392 }
12393 else
12394 maxcount = 25;
12395
12396 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12397
12398 for (i = 0; i < ga.ga_len; ++i)
12399 {
12400 str = ((char_u **)ga.ga_data)[i];
12401
12402 li = listitem_alloc();
12403 if (li == NULL)
12404 vim_free(str);
12405 else
12406 {
12407 li->li_tv.v_type = VAR_STRING;
12408 li->li_tv.v_lock = 0;
12409 li->li_tv.vval.v_string = str;
12410 list_append(rettv->vval.v_list, li);
12411 }
12412 }
12413 ga_clear(&ga);
12414 }
12415#endif
12416}
12417
12418 static void
12419f_split(typval_T *argvars, typval_T *rettv)
12420{
12421 char_u *str;
12422 char_u *end;
12423 char_u *pat = NULL;
12424 regmatch_T regmatch;
12425 char_u patbuf[NUMBUFLEN];
12426 char_u *save_cpo;
12427 int match;
12428 colnr_T col = 0;
12429 int keepempty = FALSE;
12430 int typeerr = FALSE;
12431
12432 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12433 save_cpo = p_cpo;
12434 p_cpo = (char_u *)"";
12435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012436 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437 if (argvars[1].v_type != VAR_UNKNOWN)
12438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012439 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440 if (pat == NULL)
12441 typeerr = TRUE;
12442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012443 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012444 }
12445 if (pat == NULL || *pat == NUL)
12446 pat = (char_u *)"[\\x01- ]\\+";
12447
12448 if (rettv_list_alloc(rettv) == FAIL)
12449 return;
12450 if (typeerr)
12451 return;
12452
12453 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12454 if (regmatch.regprog != NULL)
12455 {
12456 regmatch.rm_ic = FALSE;
12457 while (*str != NUL || keepempty)
12458 {
12459 if (*str == NUL)
12460 match = FALSE; /* empty item at the end */
12461 else
12462 match = vim_regexec_nl(&regmatch, str, col);
12463 if (match)
12464 end = regmatch.startp[0];
12465 else
12466 end = str + STRLEN(str);
12467 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12468 && *str != NUL && match && end < regmatch.endp[0]))
12469 {
12470 if (list_append_string(rettv->vval.v_list, str,
12471 (int)(end - str)) == FAIL)
12472 break;
12473 }
12474 if (!match)
12475 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012476 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477 if (regmatch.endp[0] > str)
12478 col = 0;
12479 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012480 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012481 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482 str = regmatch.endp[0];
12483 }
12484
12485 vim_regfree(regmatch.regprog);
12486 }
12487
12488 p_cpo = save_cpo;
12489}
12490
12491#ifdef FEAT_FLOAT
12492/*
12493 * "sqrt()" function
12494 */
12495 static void
12496f_sqrt(typval_T *argvars, typval_T *rettv)
12497{
12498 float_T f = 0.0;
12499
12500 rettv->v_type = VAR_FLOAT;
12501 if (get_float_arg(argvars, &f) == OK)
12502 rettv->vval.v_float = sqrt(f);
12503 else
12504 rettv->vval.v_float = 0.0;
12505}
12506
12507/*
12508 * "str2float()" function
12509 */
12510 static void
12511f_str2float(typval_T *argvars, typval_T *rettv)
12512{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012513 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012514 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515
Bram Moolenaar08243d22017-01-10 16:12:29 +010012516 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517 p = skipwhite(p + 1);
12518 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012519 if (isneg)
12520 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012521 rettv->v_type = VAR_FLOAT;
12522}
12523#endif
12524
12525/*
12526 * "str2nr()" function
12527 */
12528 static void
12529f_str2nr(typval_T *argvars, typval_T *rettv)
12530{
12531 int base = 10;
12532 char_u *p;
12533 varnumber_T n;
12534 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012535 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536
12537 if (argvars[1].v_type != VAR_UNKNOWN)
12538 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012539 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540 if (base != 2 && base != 8 && base != 10 && base != 16)
12541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012542 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012543 return;
12544 }
12545 }
12546
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012547 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012548 isneg = (*p == '-');
12549 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012550 p = skipwhite(p + 1);
12551 switch (base)
12552 {
12553 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12554 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12555 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12556 default: what = 0;
12557 }
12558 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012559 if (isneg)
12560 rettv->vval.v_number = -n;
12561 else
12562 rettv->vval.v_number = n;
12563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012564}
12565
12566#ifdef HAVE_STRFTIME
12567/*
12568 * "strftime({format}[, {time}])" function
12569 */
12570 static void
12571f_strftime(typval_T *argvars, typval_T *rettv)
12572{
12573 char_u result_buf[256];
12574 struct tm *curtime;
12575 time_t seconds;
12576 char_u *p;
12577
12578 rettv->v_type = VAR_STRING;
12579
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012580 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581 if (argvars[1].v_type == VAR_UNKNOWN)
12582 seconds = time(NULL);
12583 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012584 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 curtime = localtime(&seconds);
12586 /* MSVC returns NULL for an invalid value of seconds. */
12587 if (curtime == NULL)
12588 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12589 else
12590 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591 vimconv_T conv;
12592 char_u *enc;
12593
12594 conv.vc_type = CONV_NONE;
12595 enc = enc_locale();
12596 convert_setup(&conv, p_enc, enc);
12597 if (conv.vc_type != CONV_NONE)
12598 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012599 if (p != NULL)
12600 (void)strftime((char *)result_buf, sizeof(result_buf),
12601 (char *)p, curtime);
12602 else
12603 result_buf[0] = NUL;
12604
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012605 if (conv.vc_type != CONV_NONE)
12606 vim_free(p);
12607 convert_setup(&conv, enc, p_enc);
12608 if (conv.vc_type != CONV_NONE)
12609 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12610 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012611 rettv->vval.v_string = vim_strsave(result_buf);
12612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 /* Release conversion descriptors */
12614 convert_setup(&conv, NULL, NULL);
12615 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012616 }
12617}
12618#endif
12619
12620/*
12621 * "strgetchar()" function
12622 */
12623 static void
12624f_strgetchar(typval_T *argvars, typval_T *rettv)
12625{
12626 char_u *str;
12627 int len;
12628 int error = FALSE;
12629 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012630 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012631
12632 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012633 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012634 if (str == NULL)
12635 return;
12636 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012637 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012638 if (error)
12639 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012640
Bram Moolenaar13505972019-01-24 15:04:48 +010012641 while (charidx >= 0 && byteidx < len)
12642 {
12643 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012645 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12646 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012647 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012648 --charidx;
12649 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012650 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651}
12652
12653/*
12654 * "stridx()" function
12655 */
12656 static void
12657f_stridx(typval_T *argvars, typval_T *rettv)
12658{
12659 char_u buf[NUMBUFLEN];
12660 char_u *needle;
12661 char_u *haystack;
12662 char_u *save_haystack;
12663 char_u *pos;
12664 int start_idx;
12665
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012666 needle = tv_get_string_chk(&argvars[1]);
12667 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668 rettv->vval.v_number = -1;
12669 if (needle == NULL || haystack == NULL)
12670 return; /* type error; errmsg already given */
12671
12672 if (argvars[2].v_type != VAR_UNKNOWN)
12673 {
12674 int error = FALSE;
12675
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012676 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (error || start_idx >= (int)STRLEN(haystack))
12678 return;
12679 if (start_idx >= 0)
12680 haystack += start_idx;
12681 }
12682
12683 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12684 if (pos != NULL)
12685 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12686}
12687
12688/*
12689 * "string()" function
12690 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012691 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692f_string(typval_T *argvars, typval_T *rettv)
12693{
12694 char_u *tofree;
12695 char_u numbuf[NUMBUFLEN];
12696
12697 rettv->v_type = VAR_STRING;
12698 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12699 get_copyID());
12700 /* Make a copy if we have a value but it's not in allocated memory. */
12701 if (rettv->vval.v_string != NULL && tofree == NULL)
12702 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12703}
12704
12705/*
12706 * "strlen()" function
12707 */
12708 static void
12709f_strlen(typval_T *argvars, typval_T *rettv)
12710{
12711 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012712 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012713}
12714
12715/*
12716 * "strchars()" function
12717 */
12718 static void
12719f_strchars(typval_T *argvars, typval_T *rettv)
12720{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012721 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012722 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012723 varnumber_T len = 0;
12724 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012725
12726 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012727 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012728 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012729 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730 else
12731 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12733 while (*s != NUL)
12734 {
12735 func_mb_ptr2char_adv(&s);
12736 ++len;
12737 }
12738 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012739 }
12740}
12741
12742/*
12743 * "strdisplaywidth()" function
12744 */
12745 static void
12746f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12747{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012748 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749 int col = 0;
12750
12751 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012752 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012753
12754 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12755}
12756
12757/*
12758 * "strwidth()" function
12759 */
12760 static void
12761f_strwidth(typval_T *argvars, typval_T *rettv)
12762{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012763 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012764
Bram Moolenaar13505972019-01-24 15:04:48 +010012765 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766}
12767
12768/*
12769 * "strcharpart()" function
12770 */
12771 static void
12772f_strcharpart(typval_T *argvars, typval_T *rettv)
12773{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012774 char_u *p;
12775 int nchar;
12776 int nbyte = 0;
12777 int charlen;
12778 int len = 0;
12779 int slen;
12780 int error = FALSE;
12781
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012782 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012783 slen = (int)STRLEN(p);
12784
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012785 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786 if (!error)
12787 {
12788 if (nchar > 0)
12789 while (nchar > 0 && nbyte < slen)
12790 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012791 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792 --nchar;
12793 }
12794 else
12795 nbyte = nchar;
12796 if (argvars[2].v_type != VAR_UNKNOWN)
12797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012798 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012799 while (charlen > 0 && nbyte + len < slen)
12800 {
12801 int off = nbyte + len;
12802
12803 if (off < 0)
12804 len += 1;
12805 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012806 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012807 --charlen;
12808 }
12809 }
12810 else
12811 len = slen - nbyte; /* default: all bytes that are available. */
12812 }
12813
12814 /*
12815 * Only return the overlap between the specified part and the actual
12816 * string.
12817 */
12818 if (nbyte < 0)
12819 {
12820 len += nbyte;
12821 nbyte = 0;
12822 }
12823 else if (nbyte > slen)
12824 nbyte = slen;
12825 if (len < 0)
12826 len = 0;
12827 else if (nbyte + len > slen)
12828 len = slen - nbyte;
12829
12830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012832}
12833
12834/*
12835 * "strpart()" function
12836 */
12837 static void
12838f_strpart(typval_T *argvars, typval_T *rettv)
12839{
12840 char_u *p;
12841 int n;
12842 int len;
12843 int slen;
12844 int error = FALSE;
12845
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012846 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012847 slen = (int)STRLEN(p);
12848
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012849 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 if (error)
12851 len = 0;
12852 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012853 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012854 else
12855 len = slen - n; /* default len: all bytes that are available. */
12856
12857 /*
12858 * Only return the overlap between the specified part and the actual
12859 * string.
12860 */
12861 if (n < 0)
12862 {
12863 len += n;
12864 n = 0;
12865 }
12866 else if (n > slen)
12867 n = slen;
12868 if (len < 0)
12869 len = 0;
12870 else if (n + len > slen)
12871 len = slen - n;
12872
12873 rettv->v_type = VAR_STRING;
12874 rettv->vval.v_string = vim_strnsave(p + n, len);
12875}
12876
12877/*
12878 * "strridx()" function
12879 */
12880 static void
12881f_strridx(typval_T *argvars, typval_T *rettv)
12882{
12883 char_u buf[NUMBUFLEN];
12884 char_u *needle;
12885 char_u *haystack;
12886 char_u *rest;
12887 char_u *lastmatch = NULL;
12888 int haystack_len, end_idx;
12889
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012890 needle = tv_get_string_chk(&argvars[1]);
12891 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012892
12893 rettv->vval.v_number = -1;
12894 if (needle == NULL || haystack == NULL)
12895 return; /* type error; errmsg already given */
12896
12897 haystack_len = (int)STRLEN(haystack);
12898 if (argvars[2].v_type != VAR_UNKNOWN)
12899 {
12900 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012901 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902 if (end_idx < 0)
12903 return; /* can never find a match */
12904 }
12905 else
12906 end_idx = haystack_len;
12907
12908 if (*needle == NUL)
12909 {
12910 /* Empty string matches past the end. */
12911 lastmatch = haystack + end_idx;
12912 }
12913 else
12914 {
12915 for (rest = haystack; *rest != '\0'; ++rest)
12916 {
12917 rest = (char_u *)strstr((char *)rest, (char *)needle);
12918 if (rest == NULL || rest > haystack + end_idx)
12919 break;
12920 lastmatch = rest;
12921 }
12922 }
12923
12924 if (lastmatch == NULL)
12925 rettv->vval.v_number = -1;
12926 else
12927 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12928}
12929
12930/*
12931 * "strtrans()" function
12932 */
12933 static void
12934f_strtrans(typval_T *argvars, typval_T *rettv)
12935{
12936 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012937 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012938}
12939
12940/*
12941 * "submatch()" function
12942 */
12943 static void
12944f_submatch(typval_T *argvars, typval_T *rettv)
12945{
12946 int error = FALSE;
12947 int no;
12948 int retList = 0;
12949
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012950 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012951 if (error)
12952 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012953 if (no < 0 || no >= NSUBEXP)
12954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012955 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012956 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012957 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012959 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960 if (error)
12961 return;
12962
12963 if (retList == 0)
12964 {
12965 rettv->v_type = VAR_STRING;
12966 rettv->vval.v_string = reg_submatch(no);
12967 }
12968 else
12969 {
12970 rettv->v_type = VAR_LIST;
12971 rettv->vval.v_list = reg_submatch_list(no);
12972 }
12973}
12974
12975/*
12976 * "substitute()" function
12977 */
12978 static void
12979f_substitute(typval_T *argvars, typval_T *rettv)
12980{
12981 char_u patbuf[NUMBUFLEN];
12982 char_u subbuf[NUMBUFLEN];
12983 char_u flagsbuf[NUMBUFLEN];
12984
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012985 char_u *str = tv_get_string_chk(&argvars[0]);
12986 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012987 char_u *sub = NULL;
12988 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012989 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012990
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012991 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12992 expr = &argvars[2];
12993 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012994 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012996 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012997 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
12998 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999 rettv->vval.v_string = NULL;
13000 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013001 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002}
13003
13004/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013005 * "swapinfo(swap_filename)" function
13006 */
13007 static void
13008f_swapinfo(typval_T *argvars, typval_T *rettv)
13009{
13010 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013011 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013012}
13013
13014/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013015 * "swapname(expr)" function
13016 */
13017 static void
13018f_swapname(typval_T *argvars, typval_T *rettv)
13019{
13020 buf_T *buf;
13021
13022 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013023 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013024 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13025 || buf->b_ml.ml_mfp->mf_fname == NULL)
13026 rettv->vval.v_string = NULL;
13027 else
13028 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13029}
13030
13031/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013032 * "synID(lnum, col, trans)" function
13033 */
13034 static void
13035f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13036{
13037 int id = 0;
13038#ifdef FEAT_SYN_HL
13039 linenr_T lnum;
13040 colnr_T col;
13041 int trans;
13042 int transerr = FALSE;
13043
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013044 lnum = tv_get_lnum(argvars); /* -1 on type error */
13045 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13046 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047
13048 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13049 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13050 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13051#endif
13052
13053 rettv->vval.v_number = id;
13054}
13055
13056/*
13057 * "synIDattr(id, what [, mode])" function
13058 */
13059 static void
13060f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13061{
13062 char_u *p = NULL;
13063#ifdef FEAT_SYN_HL
13064 int id;
13065 char_u *what;
13066 char_u *mode;
13067 char_u modebuf[NUMBUFLEN];
13068 int modec;
13069
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013070 id = (int)tv_get_number(&argvars[0]);
13071 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 if (argvars[2].v_type != VAR_UNKNOWN)
13073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013074 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075 modec = TOLOWER_ASC(mode[0]);
13076 if (modec != 't' && modec != 'c' && modec != 'g')
13077 modec = 0; /* replace invalid with current */
13078 }
13079 else
13080 {
13081#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13082 if (USE_24BIT)
13083 modec = 'g';
13084 else
13085#endif
13086 if (t_colors > 1)
13087 modec = 'c';
13088 else
13089 modec = 't';
13090 }
13091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013092 switch (TOLOWER_ASC(what[0]))
13093 {
13094 case 'b':
13095 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13096 p = highlight_color(id, what, modec);
13097 else /* bold */
13098 p = highlight_has_attr(id, HL_BOLD, modec);
13099 break;
13100
13101 case 'f': /* fg[#] or font */
13102 p = highlight_color(id, what, modec);
13103 break;
13104
13105 case 'i':
13106 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13107 p = highlight_has_attr(id, HL_INVERSE, modec);
13108 else /* italic */
13109 p = highlight_has_attr(id, HL_ITALIC, modec);
13110 break;
13111
13112 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013113 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013114 break;
13115
13116 case 'r': /* reverse */
13117 p = highlight_has_attr(id, HL_INVERSE, modec);
13118 break;
13119
13120 case 's':
13121 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13122 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013123 /* strikeout */
13124 else if (TOLOWER_ASC(what[1]) == 't' &&
13125 TOLOWER_ASC(what[2]) == 'r')
13126 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127 else /* standout */
13128 p = highlight_has_attr(id, HL_STANDOUT, modec);
13129 break;
13130
13131 case 'u':
13132 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13133 /* underline */
13134 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13135 else
13136 /* undercurl */
13137 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13138 break;
13139 }
13140
13141 if (p != NULL)
13142 p = vim_strsave(p);
13143#endif
13144 rettv->v_type = VAR_STRING;
13145 rettv->vval.v_string = p;
13146}
13147
13148/*
13149 * "synIDtrans(id)" function
13150 */
13151 static void
13152f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13153{
13154 int id;
13155
13156#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158
13159 if (id > 0)
13160 id = syn_get_final_id(id);
13161 else
13162#endif
13163 id = 0;
13164
13165 rettv->vval.v_number = id;
13166}
13167
13168/*
13169 * "synconcealed(lnum, col)" function
13170 */
13171 static void
13172f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13173{
13174#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13175 linenr_T lnum;
13176 colnr_T col;
13177 int syntax_flags = 0;
13178 int cchar;
13179 int matchid = 0;
13180 char_u str[NUMBUFLEN];
13181#endif
13182
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013183 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184
13185#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013186 lnum = tv_get_lnum(argvars); /* -1 on type error */
13187 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188
13189 vim_memset(str, NUL, sizeof(str));
13190
13191 if (rettv_list_alloc(rettv) != FAIL)
13192 {
13193 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13194 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13195 && curwin->w_p_cole > 0)
13196 {
13197 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13198 syntax_flags = get_syntax_info(&matchid);
13199
13200 /* get the conceal character */
13201 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13202 {
13203 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013204 if (cchar == NUL && curwin->w_p_cole == 1)
13205 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013206 if (cchar != NUL)
13207 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 if (has_mbyte)
13209 (*mb_char2bytes)(cchar, str);
13210 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013211 str[0] = cchar;
13212 }
13213 }
13214 }
13215
13216 list_append_number(rettv->vval.v_list,
13217 (syntax_flags & HL_CONCEAL) != 0);
13218 /* -1 to auto-determine strlen */
13219 list_append_string(rettv->vval.v_list, str, -1);
13220 list_append_number(rettv->vval.v_list, matchid);
13221 }
13222#endif
13223}
13224
13225/*
13226 * "synstack(lnum, col)" function
13227 */
13228 static void
13229f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13230{
13231#ifdef FEAT_SYN_HL
13232 linenr_T lnum;
13233 colnr_T col;
13234 int i;
13235 int id;
13236#endif
13237
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013238 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013239
13240#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013241 lnum = tv_get_lnum(argvars); /* -1 on type error */
13242 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013243
13244 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13245 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13246 && rettv_list_alloc(rettv) != FAIL)
13247 {
13248 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13249 for (i = 0; ; ++i)
13250 {
13251 id = syn_get_stack_item(i);
13252 if (id < 0)
13253 break;
13254 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13255 break;
13256 }
13257 }
13258#endif
13259}
13260
13261 static void
13262get_cmd_output_as_rettv(
13263 typval_T *argvars,
13264 typval_T *rettv,
13265 int retlist)
13266{
13267 char_u *res = NULL;
13268 char_u *p;
13269 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 int err = FALSE;
13271 FILE *fd;
13272 list_T *list = NULL;
13273 int flags = SHELL_SILENT;
13274
13275 rettv->v_type = VAR_STRING;
13276 rettv->vval.v_string = NULL;
13277 if (check_restricted() || check_secure())
13278 goto errret;
13279
13280 if (argvars[1].v_type != VAR_UNKNOWN)
13281 {
13282 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013283 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 * command.
13285 */
13286 if ((infile = vim_tempname('i', TRUE)) == NULL)
13287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013288 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 goto errret;
13290 }
13291
13292 fd = mch_fopen((char *)infile, WRITEBIN);
13293 if (fd == NULL)
13294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013295 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013296 goto errret;
13297 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013298 if (argvars[1].v_type == VAR_NUMBER)
13299 {
13300 linenr_T lnum;
13301 buf_T *buf;
13302
13303 buf = buflist_findnr(argvars[1].vval.v_number);
13304 if (buf == NULL)
13305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013306 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013307 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013308 goto errret;
13309 }
13310
13311 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13312 {
13313 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13314 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13315 {
13316 err = TRUE;
13317 break;
13318 }
13319 if (putc(NL, fd) == EOF)
13320 {
13321 err = TRUE;
13322 break;
13323 }
13324 }
13325 }
13326 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013327 {
13328 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13329 err = TRUE;
13330 }
13331 else
13332 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013333 size_t len;
13334 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013336 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337 if (p == NULL)
13338 {
13339 fclose(fd);
13340 goto errret; /* type error; errmsg already given */
13341 }
13342 len = STRLEN(p);
13343 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13344 err = TRUE;
13345 }
13346 if (fclose(fd) != 0)
13347 err = TRUE;
13348 if (err)
13349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013350 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 goto errret;
13352 }
13353 }
13354
13355 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13356 * echoes typeahead, that messes up the display. */
13357 if (!msg_silent)
13358 flags += SHELL_COOKED;
13359
13360 if (retlist)
13361 {
13362 int len;
13363 listitem_T *li;
13364 char_u *s = NULL;
13365 char_u *start;
13366 char_u *end;
13367 int i;
13368
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013369 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370 if (res == NULL)
13371 goto errret;
13372
13373 list = list_alloc();
13374 if (list == NULL)
13375 goto errret;
13376
13377 for (i = 0; i < len; ++i)
13378 {
13379 start = res + i;
13380 while (i < len && res[i] != NL)
13381 ++i;
13382 end = res + i;
13383
13384 s = alloc((unsigned)(end - start + 1));
13385 if (s == NULL)
13386 goto errret;
13387
13388 for (p = s; start < end; ++p, ++start)
13389 *p = *start == NUL ? NL : *start;
13390 *p = NUL;
13391
13392 li = listitem_alloc();
13393 if (li == NULL)
13394 {
13395 vim_free(s);
13396 goto errret;
13397 }
13398 li->li_tv.v_type = VAR_STRING;
13399 li->li_tv.v_lock = 0;
13400 li->li_tv.vval.v_string = s;
13401 list_append(list, li);
13402 }
13403
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013404 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013405 list = NULL;
13406 }
13407 else
13408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013409 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410#ifdef USE_CR
13411 /* translate <CR> into <NL> */
13412 if (res != NULL)
13413 {
13414 char_u *s;
13415
13416 for (s = res; *s; ++s)
13417 {
13418 if (*s == CAR)
13419 *s = NL;
13420 }
13421 }
13422#else
13423# ifdef USE_CRNL
13424 /* translate <CR><NL> into <NL> */
13425 if (res != NULL)
13426 {
13427 char_u *s, *d;
13428
13429 d = res;
13430 for (s = res; *s; ++s)
13431 {
13432 if (s[0] == CAR && s[1] == NL)
13433 ++s;
13434 *d++ = *s;
13435 }
13436 *d = NUL;
13437 }
13438# endif
13439#endif
13440 rettv->vval.v_string = res;
13441 res = NULL;
13442 }
13443
13444errret:
13445 if (infile != NULL)
13446 {
13447 mch_remove(infile);
13448 vim_free(infile);
13449 }
13450 if (res != NULL)
13451 vim_free(res);
13452 if (list != NULL)
13453 list_free(list);
13454}
13455
13456/*
13457 * "system()" function
13458 */
13459 static void
13460f_system(typval_T *argvars, typval_T *rettv)
13461{
13462 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13463}
13464
13465/*
13466 * "systemlist()" function
13467 */
13468 static void
13469f_systemlist(typval_T *argvars, typval_T *rettv)
13470{
13471 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13472}
13473
13474/*
13475 * "tabpagebuflist()" function
13476 */
13477 static void
13478f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13479{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480 tabpage_T *tp;
13481 win_T *wp = NULL;
13482
13483 if (argvars[0].v_type == VAR_UNKNOWN)
13484 wp = firstwin;
13485 else
13486 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013487 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013488 if (tp != NULL)
13489 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13490 }
13491 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13492 {
13493 for (; wp != NULL; wp = wp->w_next)
13494 if (list_append_number(rettv->vval.v_list,
13495 wp->w_buffer->b_fnum) == FAIL)
13496 break;
13497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013498}
13499
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013500/*
13501 * "tabpagenr()" function
13502 */
13503 static void
13504f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13505{
13506 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013507 char_u *arg;
13508
13509 if (argvars[0].v_type != VAR_UNKNOWN)
13510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013511 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 nr = 0;
13513 if (arg != NULL)
13514 {
13515 if (STRCMP(arg, "$") == 0)
13516 nr = tabpage_index(NULL) - 1;
13517 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013518 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013519 }
13520 }
13521 else
13522 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013523 rettv->vval.v_number = nr;
13524}
13525
13526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013527/*
13528 * Common code for tabpagewinnr() and winnr().
13529 */
13530 static int
13531get_winnr(tabpage_T *tp, typval_T *argvar)
13532{
13533 win_T *twin;
13534 int nr = 1;
13535 win_T *wp;
13536 char_u *arg;
13537
13538 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13539 if (argvar->v_type != VAR_UNKNOWN)
13540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013541 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 if (arg == NULL)
13543 nr = 0; /* type error; errmsg already given */
13544 else if (STRCMP(arg, "$") == 0)
13545 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13546 else if (STRCMP(arg, "#") == 0)
13547 {
13548 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13549 if (twin == NULL)
13550 nr = 0;
13551 }
13552 else
13553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013554 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013555 nr = 0;
13556 }
13557 }
13558
13559 if (nr > 0)
13560 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13561 wp != twin; wp = wp->w_next)
13562 {
13563 if (wp == NULL)
13564 {
13565 /* didn't find it in this tabpage */
13566 nr = 0;
13567 break;
13568 }
13569 ++nr;
13570 }
13571 return nr;
13572}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013573
13574/*
13575 * "tabpagewinnr()" function
13576 */
13577 static void
13578f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13579{
13580 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 tabpage_T *tp;
13582
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013583 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584 if (tp == NULL)
13585 nr = 0;
13586 else
13587 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013588 rettv->vval.v_number = nr;
13589}
13590
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013591/*
13592 * "tagfiles()" function
13593 */
13594 static void
13595f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13596{
13597 char_u *fname;
13598 tagname_T tn;
13599 int first;
13600
13601 if (rettv_list_alloc(rettv) == FAIL)
13602 return;
13603 fname = alloc(MAXPATHL);
13604 if (fname == NULL)
13605 return;
13606
13607 for (first = TRUE; ; first = FALSE)
13608 if (get_tagfname(&tn, first, fname) == FAIL
13609 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13610 break;
13611 tagname_free(&tn);
13612 vim_free(fname);
13613}
13614
13615/*
13616 * "taglist()" function
13617 */
13618 static void
13619f_taglist(typval_T *argvars, typval_T *rettv)
13620{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013621 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622 char_u *tag_pattern;
13623
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013624 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625
13626 rettv->vval.v_number = FALSE;
13627 if (*tag_pattern == NUL)
13628 return;
13629
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013630 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013631 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013632 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013633 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013634}
13635
13636/*
13637 * "tempname()" function
13638 */
13639 static void
13640f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13641{
13642 static int x = 'A';
13643
13644 rettv->v_type = VAR_STRING;
13645 rettv->vval.v_string = vim_tempname(x, FALSE);
13646
13647 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13648 * names. Skip 'I' and 'O', they are used for shell redirection. */
13649 do
13650 {
13651 if (x == 'Z')
13652 x = '0';
13653 else if (x == '9')
13654 x = 'A';
13655 else
13656 {
13657#ifdef EBCDIC
13658 if (x == 'I')
13659 x = 'J';
13660 else if (x == 'R')
13661 x = 'S';
13662 else
13663#endif
13664 ++x;
13665 }
13666 } while (x == 'I' || x == 'O');
13667}
13668
13669#ifdef FEAT_FLOAT
13670/*
13671 * "tan()" function
13672 */
13673 static void
13674f_tan(typval_T *argvars, typval_T *rettv)
13675{
13676 float_T f = 0.0;
13677
13678 rettv->v_type = VAR_FLOAT;
13679 if (get_float_arg(argvars, &f) == OK)
13680 rettv->vval.v_float = tan(f);
13681 else
13682 rettv->vval.v_float = 0.0;
13683}
13684
13685/*
13686 * "tanh()" function
13687 */
13688 static void
13689f_tanh(typval_T *argvars, typval_T *rettv)
13690{
13691 float_T f = 0.0;
13692
13693 rettv->v_type = VAR_FLOAT;
13694 if (get_float_arg(argvars, &f) == OK)
13695 rettv->vval.v_float = tanh(f);
13696 else
13697 rettv->vval.v_float = 0.0;
13698}
13699#endif
13700
13701/*
13702 * "test_alloc_fail(id, countdown, repeat)" function
13703 */
13704 static void
13705f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13706{
13707 if (argvars[0].v_type != VAR_NUMBER
13708 || argvars[0].vval.v_number <= 0
13709 || argvars[1].v_type != VAR_NUMBER
13710 || argvars[1].vval.v_number < 0
13711 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013712 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013713 else
13714 {
13715 alloc_fail_id = argvars[0].vval.v_number;
13716 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013717 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013718 alloc_fail_countdown = argvars[1].vval.v_number;
13719 alloc_fail_repeat = argvars[2].vval.v_number;
13720 did_outofmem_msg = FALSE;
13721 }
13722}
13723
13724/*
13725 * "test_autochdir()"
13726 */
13727 static void
13728f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13729{
13730#if defined(FEAT_AUTOCHDIR)
13731 test_autochdir = TRUE;
13732#endif
13733}
13734
13735/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013736 * "test_feedinput()"
13737 */
13738 static void
13739f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13740{
13741#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013742 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013743
13744 if (val != NULL)
13745 {
13746 trash_input_buf();
13747 add_to_input_buf_csi(val, (int)STRLEN(val));
13748 }
13749#endif
13750}
13751
13752/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013753 * "test_option_not_set({name})" function
13754 */
13755 static void
13756f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13757{
13758 char_u *name = (char_u *)"";
13759
13760 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013761 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013762 else
13763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013764 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013765 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013766 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013767 }
13768}
13769
13770/*
13771 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013772 */
13773 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013774f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013776 char_u *name = (char_u *)"";
13777 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013778 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013779
13780 if (argvars[0].v_type != VAR_STRING
13781 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013782 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013783 else
13784 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013785 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013786 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013787
13788 if (STRCMP(name, (char_u *)"redraw") == 0)
13789 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013790 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13791 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013792 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13793 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013794 else if (STRCMP(name, (char_u *)"starting") == 0)
13795 {
13796 if (val)
13797 {
13798 if (save_starting < 0)
13799 save_starting = starting;
13800 starting = 0;
13801 }
13802 else
13803 {
13804 starting = save_starting;
13805 save_starting = -1;
13806 }
13807 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013808 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13809 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013810 else if (STRCMP(name, (char_u *)"ALL") == 0)
13811 {
13812 disable_char_avail_for_testing = FALSE;
13813 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013814 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013815 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013816 if (save_starting >= 0)
13817 {
13818 starting = save_starting;
13819 save_starting = -1;
13820 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013821 }
13822 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013823 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013824 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013825}
13826
13827/*
13828 * "test_garbagecollect_now()" function
13829 */
13830 static void
13831f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13832{
13833 /* This is dangerous, any Lists and Dicts used internally may be freed
13834 * while still in use. */
13835 garbage_collect(TRUE);
13836}
13837
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013838/*
13839 * "test_ignore_error()" function
13840 */
13841 static void
13842f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13843{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013844 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013845}
13846
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013847 static void
13848f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13849{
13850 rettv->v_type = VAR_BLOB;
13851 rettv->vval.v_blob = NULL;
13852}
13853
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013854#ifdef FEAT_JOB_CHANNEL
13855 static void
13856f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13857{
13858 rettv->v_type = VAR_CHANNEL;
13859 rettv->vval.v_channel = NULL;
13860}
13861#endif
13862
13863 static void
13864f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13865{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013866 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867}
13868
13869#ifdef FEAT_JOB_CHANNEL
13870 static void
13871f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13872{
13873 rettv->v_type = VAR_JOB;
13874 rettv->vval.v_job = NULL;
13875}
13876#endif
13877
13878 static void
13879f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13880{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013881 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013882}
13883
13884 static void
13885f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13886{
13887 rettv->v_type = VAR_PARTIAL;
13888 rettv->vval.v_partial = NULL;
13889}
13890
13891 static void
13892f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13893{
13894 rettv->v_type = VAR_STRING;
13895 rettv->vval.v_string = NULL;
13896}
13897
Bram Moolenaarab186732018-09-14 21:27:06 +020013898#ifdef FEAT_GUI
13899 static void
13900f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13901{
13902 char_u *which;
13903 long value;
13904 int dragging;
13905 scrollbar_T *sb = NULL;
13906
13907 if (argvars[0].v_type != VAR_STRING
13908 || (argvars[1].v_type) != VAR_NUMBER
13909 || (argvars[2].v_type) != VAR_NUMBER)
13910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013911 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013912 return;
13913 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013914 which = tv_get_string(&argvars[0]);
13915 value = tv_get_number(&argvars[1]);
13916 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013917
13918 if (STRCMP(which, "left") == 0)
13919 sb = &curwin->w_scrollbars[SBAR_LEFT];
13920 else if (STRCMP(which, "right") == 0)
13921 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13922 else if (STRCMP(which, "hor") == 0)
13923 sb = &gui.bottom_sbar;
13924 if (sb == NULL)
13925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013926 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013927 return;
13928 }
13929 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013930# ifndef USE_ON_FLY_SCROLL
13931 // need to loop through normal_cmd() to handle the scroll events
13932 exec_normal(FALSE, TRUE, FALSE);
13933# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013934}
13935#endif
13936
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937 static void
13938f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13939{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013940 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013941}
13942
13943#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13944/*
13945 * Get a callback from "arg". It can be a Funcref or a function name.
13946 * When "arg" is zero return an empty string.
13947 * Return NULL for an invalid argument.
13948 */
13949 char_u *
13950get_callback(typval_T *arg, partial_T **pp)
13951{
13952 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13953 {
13954 *pp = arg->vval.v_partial;
13955 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013956 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 }
13958 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013959 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013960 {
13961 func_ref(arg->vval.v_string);
13962 return arg->vval.v_string;
13963 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013964 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13965 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013966 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013967 return NULL;
13968}
13969
13970/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013971 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013972 */
13973 void
13974free_callback(char_u *callback, partial_T *partial)
13975{
13976 if (partial != NULL)
13977 partial_unref(partial);
13978 else if (callback != NULL)
13979 {
13980 func_unref(callback);
13981 vim_free(callback);
13982 }
13983}
13984#endif
13985
13986#ifdef FEAT_TIMERS
13987/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013988 * "timer_info([timer])" function
13989 */
13990 static void
13991f_timer_info(typval_T *argvars, typval_T *rettv)
13992{
13993 timer_T *timer = NULL;
13994
13995 if (rettv_list_alloc(rettv) != OK)
13996 return;
13997 if (argvars[0].v_type != VAR_UNKNOWN)
13998 {
13999 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014000 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014001 else
14002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014003 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014004 if (timer != NULL)
14005 add_timer_info(rettv, timer);
14006 }
14007 }
14008 else
14009 add_timer_info_all(rettv);
14010}
14011
14012/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014013 * "timer_pause(timer, paused)" function
14014 */
14015 static void
14016f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14017{
14018 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014019 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014020
14021 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014022 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014023 else
14024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014025 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014026 if (timer != NULL)
14027 timer->tr_paused = paused;
14028 }
14029}
14030
14031/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 * "timer_start(time, callback [, options])" function
14033 */
14034 static void
14035f_timer_start(typval_T *argvars, typval_T *rettv)
14036{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014037 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014038 timer_T *timer;
14039 int repeat = 0;
14040 char_u *callback;
14041 dict_T *dict;
14042 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014043
Bram Moolenaar75537a92016-09-05 22:45:28 +020014044 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 if (check_secure())
14046 return;
14047 if (argvars[2].v_type != VAR_UNKNOWN)
14048 {
14049 if (argvars[2].v_type != VAR_DICT
14050 || (dict = argvars[2].vval.v_dict) == NULL)
14051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014052 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014053 return;
14054 }
14055 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014056 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014057 }
14058
Bram Moolenaar75537a92016-09-05 22:45:28 +020014059 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014060 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014061 return;
14062
14063 timer = create_timer(msec, repeat);
14064 if (timer == NULL)
14065 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014066 else
14067 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014068 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014069 timer->tr_callback = vim_strsave(callback);
14070 else
14071 /* pointer into the partial */
14072 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014073 timer->tr_partial = partial;
14074 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 }
14076}
14077
14078/*
14079 * "timer_stop(timer)" function
14080 */
14081 static void
14082f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14083{
14084 timer_T *timer;
14085
14086 if (argvars[0].v_type != VAR_NUMBER)
14087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014088 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089 return;
14090 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014091 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014092 if (timer != NULL)
14093 stop_timer(timer);
14094}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014095
14096/*
14097 * "timer_stopall()" function
14098 */
14099 static void
14100f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14101{
14102 stop_all_timers();
14103}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104#endif
14105
14106/*
14107 * "tolower(string)" function
14108 */
14109 static void
14110f_tolower(typval_T *argvars, typval_T *rettv)
14111{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014112 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014113 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114}
14115
14116/*
14117 * "toupper(string)" function
14118 */
14119 static void
14120f_toupper(typval_T *argvars, typval_T *rettv)
14121{
14122 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014123 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124}
14125
14126/*
14127 * "tr(string, fromstr, tostr)" function
14128 */
14129 static void
14130f_tr(typval_T *argvars, typval_T *rettv)
14131{
14132 char_u *in_str;
14133 char_u *fromstr;
14134 char_u *tostr;
14135 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136 int inlen;
14137 int fromlen;
14138 int tolen;
14139 int idx;
14140 char_u *cpstr;
14141 int cplen;
14142 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143 char_u buf[NUMBUFLEN];
14144 char_u buf2[NUMBUFLEN];
14145 garray_T ga;
14146
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014147 in_str = tv_get_string(&argvars[0]);
14148 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14149 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150
14151 /* Default return value: empty string. */
14152 rettv->v_type = VAR_STRING;
14153 rettv->vval.v_string = NULL;
14154 if (fromstr == NULL || tostr == NULL)
14155 return; /* type error; errmsg already given */
14156 ga_init2(&ga, (int)sizeof(char), 80);
14157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014158 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014159 /* not multi-byte: fromstr and tostr must be the same length */
14160 if (STRLEN(fromstr) != STRLEN(tostr))
14161 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014162error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014163 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 ga_clear(&ga);
14165 return;
14166 }
14167
14168 /* fromstr and tostr have to contain the same number of chars */
14169 while (*in_str != NUL)
14170 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014171 if (has_mbyte)
14172 {
14173 inlen = (*mb_ptr2len)(in_str);
14174 cpstr = in_str;
14175 cplen = inlen;
14176 idx = 0;
14177 for (p = fromstr; *p != NUL; p += fromlen)
14178 {
14179 fromlen = (*mb_ptr2len)(p);
14180 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14181 {
14182 for (p = tostr; *p != NUL; p += tolen)
14183 {
14184 tolen = (*mb_ptr2len)(p);
14185 if (idx-- == 0)
14186 {
14187 cplen = tolen;
14188 cpstr = p;
14189 break;
14190 }
14191 }
14192 if (*p == NUL) /* tostr is shorter than fromstr */
14193 goto error;
14194 break;
14195 }
14196 ++idx;
14197 }
14198
14199 if (first && cpstr == in_str)
14200 {
14201 /* Check that fromstr and tostr have the same number of
14202 * (multi-byte) characters. Done only once when a character
14203 * of in_str doesn't appear in fromstr. */
14204 first = FALSE;
14205 for (p = tostr; *p != NUL; p += tolen)
14206 {
14207 tolen = (*mb_ptr2len)(p);
14208 --idx;
14209 }
14210 if (idx != 0)
14211 goto error;
14212 }
14213
14214 (void)ga_grow(&ga, cplen);
14215 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14216 ga.ga_len += cplen;
14217
14218 in_str += inlen;
14219 }
14220 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014221 {
14222 /* When not using multi-byte chars we can do it faster. */
14223 p = vim_strchr(fromstr, *in_str);
14224 if (p != NULL)
14225 ga_append(&ga, tostr[p - fromstr]);
14226 else
14227 ga_append(&ga, *in_str);
14228 ++in_str;
14229 }
14230 }
14231
14232 /* add a terminating NUL */
14233 (void)ga_grow(&ga, 1);
14234 ga_append(&ga, NUL);
14235
14236 rettv->vval.v_string = ga.ga_data;
14237}
14238
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014239/*
14240 * "trim({expr})" function
14241 */
14242 static void
14243f_trim(typval_T *argvars, typval_T *rettv)
14244{
14245 char_u buf1[NUMBUFLEN];
14246 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014247 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014248 char_u *mask = NULL;
14249 char_u *tail;
14250 char_u *prev;
14251 char_u *p;
14252 int c1;
14253
14254 rettv->v_type = VAR_STRING;
14255 if (head == NULL)
14256 {
14257 rettv->vval.v_string = NULL;
14258 return;
14259 }
14260
14261 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014262 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014263
14264 while (*head != NUL)
14265 {
14266 c1 = PTR2CHAR(head);
14267 if (mask == NULL)
14268 {
14269 if (c1 > ' ' && c1 != 0xa0)
14270 break;
14271 }
14272 else
14273 {
14274 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14275 if (c1 == PTR2CHAR(p))
14276 break;
14277 if (*p == NUL)
14278 break;
14279 }
14280 MB_PTR_ADV(head);
14281 }
14282
14283 for (tail = head + STRLEN(head); tail > head; tail = prev)
14284 {
14285 prev = tail;
14286 MB_PTR_BACK(head, prev);
14287 c1 = PTR2CHAR(prev);
14288 if (mask == NULL)
14289 {
14290 if (c1 > ' ' && c1 != 0xa0)
14291 break;
14292 }
14293 else
14294 {
14295 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14296 if (c1 == PTR2CHAR(p))
14297 break;
14298 if (*p == NUL)
14299 break;
14300 }
14301 }
14302 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14303}
14304
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014305#ifdef FEAT_FLOAT
14306/*
14307 * "trunc({float})" function
14308 */
14309 static void
14310f_trunc(typval_T *argvars, typval_T *rettv)
14311{
14312 float_T f = 0.0;
14313
14314 rettv->v_type = VAR_FLOAT;
14315 if (get_float_arg(argvars, &f) == OK)
14316 /* trunc() is not in C90, use floor() or ceil() instead. */
14317 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14318 else
14319 rettv->vval.v_float = 0.0;
14320}
14321#endif
14322
14323/*
14324 * "type(expr)" function
14325 */
14326 static void
14327f_type(typval_T *argvars, typval_T *rettv)
14328{
14329 int n = -1;
14330
14331 switch (argvars[0].v_type)
14332 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014333 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14334 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014335 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014336 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14337 case VAR_LIST: n = VAR_TYPE_LIST; break;
14338 case VAR_DICT: n = VAR_TYPE_DICT; break;
14339 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014340 case VAR_SPECIAL:
14341 if (argvars[0].vval.v_number == VVAL_FALSE
14342 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014343 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014344 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014345 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014346 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014347 case VAR_JOB: n = VAR_TYPE_JOB; break;
14348 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014349 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014350 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014351 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 n = -1;
14353 break;
14354 }
14355 rettv->vval.v_number = n;
14356}
14357
14358/*
14359 * "undofile(name)" function
14360 */
14361 static void
14362f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14363{
14364 rettv->v_type = VAR_STRING;
14365#ifdef FEAT_PERSISTENT_UNDO
14366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014367 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014368
14369 if (*fname == NUL)
14370 {
14371 /* If there is no file name there will be no undo file. */
14372 rettv->vval.v_string = NULL;
14373 }
14374 else
14375 {
14376 char_u *ffname = FullName_save(fname, FALSE);
14377
14378 if (ffname != NULL)
14379 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14380 vim_free(ffname);
14381 }
14382 }
14383#else
14384 rettv->vval.v_string = NULL;
14385#endif
14386}
14387
14388/*
14389 * "undotree()" function
14390 */
14391 static void
14392f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14393{
14394 if (rettv_dict_alloc(rettv) == OK)
14395 {
14396 dict_T *dict = rettv->vval.v_dict;
14397 list_T *list;
14398
Bram Moolenaare0be1672018-07-08 16:50:37 +020014399 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14400 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14401 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14402 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14403 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14404 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014405
14406 list = list_alloc();
14407 if (list != NULL)
14408 {
14409 u_eval_tree(curbuf->b_u_oldhead, list);
14410 dict_add_list(dict, "entries", list);
14411 }
14412 }
14413}
14414
14415/*
14416 * "values(dict)" function
14417 */
14418 static void
14419f_values(typval_T *argvars, typval_T *rettv)
14420{
14421 dict_list(argvars, rettv, 1);
14422}
14423
14424/*
14425 * "virtcol(string)" function
14426 */
14427 static void
14428f_virtcol(typval_T *argvars, typval_T *rettv)
14429{
14430 colnr_T vcol = 0;
14431 pos_T *fp;
14432 int fnum = curbuf->b_fnum;
14433
14434 fp = var2fpos(&argvars[0], FALSE, &fnum);
14435 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14436 && fnum == curbuf->b_fnum)
14437 {
14438 getvvcol(curwin, fp, NULL, NULL, &vcol);
14439 ++vcol;
14440 }
14441
14442 rettv->vval.v_number = vcol;
14443}
14444
14445/*
14446 * "visualmode()" function
14447 */
14448 static void
14449f_visualmode(typval_T *argvars, typval_T *rettv)
14450{
14451 char_u str[2];
14452
14453 rettv->v_type = VAR_STRING;
14454 str[0] = curbuf->b_visual_mode_eval;
14455 str[1] = NUL;
14456 rettv->vval.v_string = vim_strsave(str);
14457
14458 /* A non-zero number or non-empty string argument: reset mode. */
14459 if (non_zero_arg(&argvars[0]))
14460 curbuf->b_visual_mode_eval = NUL;
14461}
14462
14463/*
14464 * "wildmenumode()" function
14465 */
14466 static void
14467f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14468{
14469#ifdef FEAT_WILDMENU
14470 if (wild_menu_showing)
14471 rettv->vval.v_number = 1;
14472#endif
14473}
14474
14475/*
14476 * "winbufnr(nr)" function
14477 */
14478 static void
14479f_winbufnr(typval_T *argvars, typval_T *rettv)
14480{
14481 win_T *wp;
14482
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014483 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014484 if (wp == NULL)
14485 rettv->vval.v_number = -1;
14486 else
14487 rettv->vval.v_number = wp->w_buffer->b_fnum;
14488}
14489
14490/*
14491 * "wincol()" function
14492 */
14493 static void
14494f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14495{
14496 validate_cursor();
14497 rettv->vval.v_number = curwin->w_wcol + 1;
14498}
14499
14500/*
14501 * "winheight(nr)" function
14502 */
14503 static void
14504f_winheight(typval_T *argvars, typval_T *rettv)
14505{
14506 win_T *wp;
14507
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014508 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014509 if (wp == NULL)
14510 rettv->vval.v_number = -1;
14511 else
14512 rettv->vval.v_number = wp->w_height;
14513}
14514
14515/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014516 * "winlayout()" function
14517 */
14518 static void
14519f_winlayout(typval_T *argvars, typval_T *rettv)
14520{
14521 tabpage_T *tp;
14522
14523 if (rettv_list_alloc(rettv) != OK)
14524 return;
14525
14526 if (argvars[0].v_type == VAR_UNKNOWN)
14527 tp = curtab;
14528 else
14529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014530 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014531 if (tp == NULL)
14532 return;
14533 }
14534
14535 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14536}
14537
14538/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014539 * "winline()" function
14540 */
14541 static void
14542f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14543{
14544 validate_cursor();
14545 rettv->vval.v_number = curwin->w_wrow + 1;
14546}
14547
14548/*
14549 * "winnr()" function
14550 */
14551 static void
14552f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14553{
14554 int nr = 1;
14555
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014556 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014557 rettv->vval.v_number = nr;
14558}
14559
14560/*
14561 * "winrestcmd()" function
14562 */
14563 static void
14564f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14565{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014566 win_T *wp;
14567 int winnr = 1;
14568 garray_T ga;
14569 char_u buf[50];
14570
14571 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014572 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014573 {
14574 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14575 ga_concat(&ga, buf);
14576 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14577 ga_concat(&ga, buf);
14578 ++winnr;
14579 }
14580 ga_append(&ga, NUL);
14581
14582 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014583 rettv->v_type = VAR_STRING;
14584}
14585
14586/*
14587 * "winrestview()" function
14588 */
14589 static void
14590f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14591{
14592 dict_T *dict;
14593
14594 if (argvars[0].v_type != VAR_DICT
14595 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014596 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014597 else
14598 {
14599 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014600 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014601 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014602 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603#ifdef FEAT_VIRTUALEDIT
14604 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014605 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014606#endif
14607 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14608 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014609 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014610 curwin->w_set_curswant = FALSE;
14611 }
14612
14613 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014614 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014615#ifdef FEAT_DIFF
14616 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014617 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618#endif
14619 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014620 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014622 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623
14624 check_cursor();
14625 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014626 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014627 changed_window_setting();
14628
14629 if (curwin->w_topline <= 0)
14630 curwin->w_topline = 1;
14631 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14632 curwin->w_topline = curbuf->b_ml.ml_line_count;
14633#ifdef FEAT_DIFF
14634 check_topfill(curwin, TRUE);
14635#endif
14636 }
14637}
14638
14639/*
14640 * "winsaveview()" function
14641 */
14642 static void
14643f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14644{
14645 dict_T *dict;
14646
14647 if (rettv_dict_alloc(rettv) == FAIL)
14648 return;
14649 dict = rettv->vval.v_dict;
14650
Bram Moolenaare0be1672018-07-08 16:50:37 +020014651 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14652 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014654 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655#endif
14656 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014657 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658
Bram Moolenaare0be1672018-07-08 16:50:37 +020014659 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014661 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014662#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014663 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14664 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665}
14666
14667/*
14668 * "winwidth(nr)" function
14669 */
14670 static void
14671f_winwidth(typval_T *argvars, typval_T *rettv)
14672{
14673 win_T *wp;
14674
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014675 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676 if (wp == NULL)
14677 rettv->vval.v_number = -1;
14678 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680}
14681
14682/*
14683 * "wordcount()" function
14684 */
14685 static void
14686f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14687{
14688 if (rettv_dict_alloc(rettv) == FAIL)
14689 return;
14690 cursor_pos_info(rettv->vval.v_dict);
14691}
14692
14693/*
14694 * "writefile()" function
14695 */
14696 static void
14697f_writefile(typval_T *argvars, typval_T *rettv)
14698{
14699 int binary = FALSE;
14700 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014701#ifdef HAVE_FSYNC
14702 int do_fsync = p_fs;
14703#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704 char_u *fname;
14705 FILE *fd;
14706 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014707 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014708 list_T *list = NULL;
14709 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014711 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 if (check_restricted() || check_secure())
14713 return;
14714
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014715 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014717 list = argvars[0].vval.v_list;
14718 if (list == NULL)
14719 return;
14720 for (li = list->lv_first; li != NULL; li = li->li_next)
14721 if (tv_get_string_chk(&li->li_tv) == NULL)
14722 return;
14723 }
14724 else if (argvars[0].v_type == VAR_BLOB)
14725 {
14726 blob = argvars[0].vval.v_blob;
14727 if (blob == NULL)
14728 return;
14729 }
14730 else
14731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014732 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733 return;
14734 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014735
14736 if (argvars[2].v_type != VAR_UNKNOWN)
14737 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014738 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014739
14740 if (arg2 == NULL)
14741 return;
14742 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014744 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014745 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014746#ifdef HAVE_FSYNC
14747 if (vim_strchr(arg2, 's') != NULL)
14748 do_fsync = TRUE;
14749 else if (vim_strchr(arg2, 'S') != NULL)
14750 do_fsync = FALSE;
14751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014752 }
14753
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014754 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014755 if (fname == NULL)
14756 return;
14757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758 /* Always open the file in binary mode, library functions have a mind of
14759 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14761 append ? APPENDBIN : WRITEBIN)) == NULL)
14762 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014763 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014764 ret = -1;
14765 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014766 else if (blob)
14767 {
14768 if (write_blob(fd, blob) == FAIL)
14769 ret = -1;
14770#ifdef HAVE_FSYNC
14771 else if (do_fsync)
14772 // Ignore the error, the user wouldn't know what to do about it.
14773 // May happen for a device.
14774 vim_ignored = fsync(fileno(fd));
14775#endif
14776 fclose(fd);
14777 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014778 else
14779 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014780 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014781 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014782#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014783 else if (do_fsync)
14784 /* Ignore the error, the user wouldn't know what to do about it.
14785 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014786 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014787#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014788 fclose(fd);
14789 }
14790
14791 rettv->vval.v_number = ret;
14792}
14793
14794/*
14795 * "xor(expr, expr)" function
14796 */
14797 static void
14798f_xor(typval_T *argvars, typval_T *rettv)
14799{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014800 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14801 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014802}
14803
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014804#endif /* FEAT_EVAL */