blob: c7ba6f3081605d9c1191d7949ec25a1a087840c4 [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;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002520 /* col(".") when the cursor is on the NUL at the end of the line
2521 * because of "coladd" can be seen as an extra column. */
2522 if (virtual_active() && fp == &curwin->w_cursor)
2523 {
2524 char_u *p = ml_get_cursor();
2525
2526 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2527 curwin->w_virtcol - curwin->w_cursor.coladd))
2528 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529 int l;
2530
2531 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2532 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 }
2534 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 }
2536 }
2537 rettv->vval.v_number = col;
2538}
2539
2540#if defined(FEAT_INS_EXPAND)
2541/*
2542 * "complete()" function
2543 */
2544 static void
2545f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2546{
2547 int startcol;
2548
2549 if ((State & INSERT) == 0)
2550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002551 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002552 return;
2553 }
2554
2555 /* Check for undo allowed here, because if something was already inserted
2556 * the line was already saved for undo and this check isn't done. */
2557 if (!undo_allowed())
2558 return;
2559
2560 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002563 return;
2564 }
2565
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002566 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002567 if (startcol <= 0)
2568 return;
2569
2570 set_completion(startcol - 1, argvars[1].vval.v_list);
2571}
2572
2573/*
2574 * "complete_add()" function
2575 */
2576 static void
2577f_complete_add(typval_T *argvars, typval_T *rettv)
2578{
2579 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2580}
2581
2582/*
2583 * "complete_check()" function
2584 */
2585 static void
2586f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2587{
2588 int saved = RedrawingDisabled;
2589
2590 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002591 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 rettv->vval.v_number = compl_interrupted;
2593 RedrawingDisabled = saved;
2594}
2595#endif
2596
2597/*
2598 * "confirm(message, buttons[, default [, type]])" function
2599 */
2600 static void
2601f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2602{
2603#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2604 char_u *message;
2605 char_u *buttons = NULL;
2606 char_u buf[NUMBUFLEN];
2607 char_u buf2[NUMBUFLEN];
2608 int def = 1;
2609 int type = VIM_GENERIC;
2610 char_u *typestr;
2611 int error = FALSE;
2612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002613 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002614 if (message == NULL)
2615 error = TRUE;
2616 if (argvars[1].v_type != VAR_UNKNOWN)
2617 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002618 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002619 if (buttons == NULL)
2620 error = TRUE;
2621 if (argvars[2].v_type != VAR_UNKNOWN)
2622 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002623 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002624 if (argvars[3].v_type != VAR_UNKNOWN)
2625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002626 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002627 if (typestr == NULL)
2628 error = TRUE;
2629 else
2630 {
2631 switch (TOUPPER_ASC(*typestr))
2632 {
2633 case 'E': type = VIM_ERROR; break;
2634 case 'Q': type = VIM_QUESTION; break;
2635 case 'I': type = VIM_INFO; break;
2636 case 'W': type = VIM_WARNING; break;
2637 case 'G': type = VIM_GENERIC; break;
2638 }
2639 }
2640 }
2641 }
2642 }
2643
2644 if (buttons == NULL || *buttons == NUL)
2645 buttons = (char_u *)_("&Ok");
2646
2647 if (!error)
2648 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2649 def, NULL, FALSE);
2650#endif
2651}
2652
2653/*
2654 * "copy()" function
2655 */
2656 static void
2657f_copy(typval_T *argvars, typval_T *rettv)
2658{
2659 item_copy(&argvars[0], rettv, FALSE, 0);
2660}
2661
2662#ifdef FEAT_FLOAT
2663/*
2664 * "cos()" function
2665 */
2666 static void
2667f_cos(typval_T *argvars, typval_T *rettv)
2668{
2669 float_T f = 0.0;
2670
2671 rettv->v_type = VAR_FLOAT;
2672 if (get_float_arg(argvars, &f) == OK)
2673 rettv->vval.v_float = cos(f);
2674 else
2675 rettv->vval.v_float = 0.0;
2676}
2677
2678/*
2679 * "cosh()" function
2680 */
2681 static void
2682f_cosh(typval_T *argvars, typval_T *rettv)
2683{
2684 float_T f = 0.0;
2685
2686 rettv->v_type = VAR_FLOAT;
2687 if (get_float_arg(argvars, &f) == OK)
2688 rettv->vval.v_float = cosh(f);
2689 else
2690 rettv->vval.v_float = 0.0;
2691}
2692#endif
2693
2694/*
2695 * "count()" function
2696 */
2697 static void
2698f_count(typval_T *argvars, typval_T *rettv)
2699{
2700 long n = 0;
2701 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002702 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703
Bram Moolenaar9966b212017-07-28 16:46:57 +02002704 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002705 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002706
2707 if (argvars[0].v_type == VAR_STRING)
2708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002709 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002710 char_u *p = argvars[0].vval.v_string;
2711 char_u *next;
2712
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002713 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002714 {
2715 if (ic)
2716 {
2717 size_t len = STRLEN(expr);
2718
2719 while (*p != NUL)
2720 {
2721 if (MB_STRNICMP(p, expr, len) == 0)
2722 {
2723 ++n;
2724 p += len;
2725 }
2726 else
2727 MB_PTR_ADV(p);
2728 }
2729 }
2730 else
2731 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2732 != NULL)
2733 {
2734 ++n;
2735 p = next + STRLEN(expr);
2736 }
2737 }
2738
2739 }
2740 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002741 {
2742 listitem_T *li;
2743 list_T *l;
2744 long idx;
2745
2746 if ((l = argvars[0].vval.v_list) != NULL)
2747 {
2748 li = l->lv_first;
2749 if (argvars[2].v_type != VAR_UNKNOWN)
2750 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002751 if (argvars[3].v_type != VAR_UNKNOWN)
2752 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002753 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002754 if (!error)
2755 {
2756 li = list_find(l, idx);
2757 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002758 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002759 }
2760 }
2761 if (error)
2762 li = NULL;
2763 }
2764
2765 for ( ; li != NULL; li = li->li_next)
2766 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2767 ++n;
2768 }
2769 }
2770 else if (argvars[0].v_type == VAR_DICT)
2771 {
2772 int todo;
2773 dict_T *d;
2774 hashitem_T *hi;
2775
2776 if ((d = argvars[0].vval.v_dict) != NULL)
2777 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (argvars[2].v_type != VAR_UNKNOWN)
2779 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002781 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 }
2783
2784 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2785 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2786 {
2787 if (!HASHITEM_EMPTY(hi))
2788 {
2789 --todo;
2790 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2791 ++n;
2792 }
2793 }
2794 }
2795 }
2796 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002797 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 rettv->vval.v_number = n;
2799}
2800
2801/*
2802 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2803 *
2804 * Checks the existence of a cscope connection.
2805 */
2806 static void
2807f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2808{
2809#ifdef FEAT_CSCOPE
2810 int num = 0;
2811 char_u *dbpath = NULL;
2812 char_u *prepend = NULL;
2813 char_u buf[NUMBUFLEN];
2814
2815 if (argvars[0].v_type != VAR_UNKNOWN
2816 && argvars[1].v_type != VAR_UNKNOWN)
2817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002818 num = (int)tv_get_number(&argvars[0]);
2819 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002820 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002821 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 }
2823
2824 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2825#endif
2826}
2827
2828/*
2829 * "cursor(lnum, col)" function, or
2830 * "cursor(list)"
2831 *
2832 * Moves the cursor to the specified line and column.
2833 * Returns 0 when the position could be set, -1 otherwise.
2834 */
2835 static void
2836f_cursor(typval_T *argvars, typval_T *rettv)
2837{
2838 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002839 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002840 int set_curswant = TRUE;
2841
2842 rettv->vval.v_number = -1;
2843 if (argvars[1].v_type == VAR_UNKNOWN)
2844 {
2845 pos_T pos;
2846 colnr_T curswant = -1;
2847
2848 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2849 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002850 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002851 return;
2852 }
2853 line = pos.lnum;
2854 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002856 if (curswant >= 0)
2857 {
2858 curwin->w_curswant = curswant - 1;
2859 set_curswant = FALSE;
2860 }
2861 }
2862 else
2863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002864 line = tv_get_lnum(argvars);
2865 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002867 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002868 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01002869 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002870 return; /* type error; errmsg already given */
2871 if (line > 0)
2872 curwin->w_cursor.lnum = line;
2873 if (col > 0)
2874 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876
2877 /* Make sure the cursor is in a valid position. */
2878 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879 /* Correct cursor for multi-byte character. */
2880 if (has_mbyte)
2881 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002882
2883 curwin->w_set_curswant = set_curswant;
2884 rettv->vval.v_number = 0;
2885}
2886
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002887#ifdef WIN3264
2888/*
2889 * "debugbreak()" function
2890 */
2891 static void
2892f_debugbreak(typval_T *argvars, typval_T *rettv)
2893{
2894 int pid;
2895
2896 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002897 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002898 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002899 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002900 else
2901 {
2902 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2903
2904 if (hProcess != NULL)
2905 {
2906 DebugBreakProcess(hProcess);
2907 CloseHandle(hProcess);
2908 rettv->vval.v_number = OK;
2909 }
2910 }
2911}
2912#endif
2913
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914/*
2915 * "deepcopy()" function
2916 */
2917 static void
2918f_deepcopy(typval_T *argvars, typval_T *rettv)
2919{
2920 int noref = 0;
2921 int copyID;
2922
2923 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002924 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 else
2928 {
2929 copyID = get_copyID();
2930 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2931 }
2932}
2933
2934/*
2935 * "delete()" function
2936 */
2937 static void
2938f_delete(typval_T *argvars, typval_T *rettv)
2939{
2940 char_u nbuf[NUMBUFLEN];
2941 char_u *name;
2942 char_u *flags;
2943
2944 rettv->vval.v_number = -1;
2945 if (check_restricted() || check_secure())
2946 return;
2947
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002948 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 if (name == NULL || *name == NUL)
2950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002951 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 return;
2953 }
2954
2955 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002956 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 else
2958 flags = (char_u *)"";
2959
2960 if (*flags == NUL)
2961 /* delete a file */
2962 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2963 else if (STRCMP(flags, "d") == 0)
2964 /* delete an empty directory */
2965 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2966 else if (STRCMP(flags, "rf") == 0)
2967 /* delete a directory recursively */
2968 rettv->vval.v_number = delete_recursive(name);
2969 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971}
2972
2973/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002974 * "deletebufline()" function
2975 */
2976 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002977f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002978{
2979 buf_T *buf;
2980 linenr_T first, last;
2981 linenr_T lnum;
2982 long count;
2983 int is_curbuf;
2984 buf_T *curbuf_save = NULL;
2985 win_T *curwin_save = NULL;
2986 tabpage_T *tp;
2987 win_T *wp;
2988
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002989 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002990 if (buf == NULL)
2991 {
2992 rettv->vval.v_number = 1; /* FAIL */
2993 return;
2994 }
2995 is_curbuf = buf == curbuf;
2996
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02002998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002999 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003000 else
3001 last = first;
3002
3003 if (buf->b_ml.ml_mfp == NULL || first < 1
3004 || first > buf->b_ml.ml_line_count || last < first)
3005 {
3006 rettv->vval.v_number = 1; /* FAIL */
3007 return;
3008 }
3009
3010 if (!is_curbuf)
3011 {
3012 curbuf_save = curbuf;
3013 curwin_save = curwin;
3014 curbuf = buf;
3015 find_win_for_curbuf();
3016 }
3017 if (last > curbuf->b_ml.ml_line_count)
3018 last = curbuf->b_ml.ml_line_count;
3019 count = last - first + 1;
3020
3021 // When coming here from Insert mode, sync undo, so that this can be
3022 // undone separately from what was previously inserted.
3023 if (u_sync_once == 2)
3024 {
3025 u_sync_once = 1; // notify that u_sync() was called
3026 u_sync(TRUE);
3027 }
3028
3029 if (u_save(first - 1, last + 1) == FAIL)
3030 {
3031 rettv->vval.v_number = 1; /* FAIL */
3032 return;
3033 }
3034
3035 for (lnum = first; lnum <= last; ++lnum)
3036 ml_delete(first, TRUE);
3037
3038 FOR_ALL_TAB_WINDOWS(tp, wp)
3039 if (wp->w_buffer == buf)
3040 {
3041 if (wp->w_cursor.lnum > last)
3042 wp->w_cursor.lnum -= count;
3043 else if (wp->w_cursor.lnum> first)
3044 wp->w_cursor.lnum = first;
3045 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3046 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3047 }
3048 check_cursor_col();
3049 deleted_lines_mark(first, count);
3050
3051 if (!is_curbuf)
3052 {
3053 curbuf = curbuf_save;
3054 curwin = curwin_save;
3055 }
3056}
3057
3058/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003059 * "did_filetype()" function
3060 */
3061 static void
3062f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3063{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003064 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065}
3066
3067/*
3068 * "diff_filler()" function
3069 */
3070 static void
3071f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3072{
3073#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075#endif
3076}
3077
3078/*
3079 * "diff_hlID()" function
3080 */
3081 static void
3082f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3083{
3084#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003087 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 static int fnum = 0;
3089 static int change_start = 0;
3090 static int change_end = 0;
3091 static hlf_T hlID = (hlf_T)0;
3092 int filler_lines;
3093 int col;
3094
3095 if (lnum < 0) /* ignore type error in {lnum} arg */
3096 lnum = 0;
3097 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003098 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 || fnum != curbuf->b_fnum)
3100 {
3101 /* New line, buffer, change: need to get the values. */
3102 filler_lines = diff_check(curwin, lnum);
3103 if (filler_lines < 0)
3104 {
3105 if (filler_lines == -1)
3106 {
3107 change_start = MAXCOL;
3108 change_end = -1;
3109 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3110 hlID = HLF_ADD; /* added line */
3111 else
3112 hlID = HLF_CHD; /* changed line */
3113 }
3114 else
3115 hlID = HLF_ADD; /* added line */
3116 }
3117 else
3118 hlID = (hlf_T)0;
3119 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003120 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 fnum = curbuf->b_fnum;
3122 }
3123
3124 if (hlID == HLF_CHD || hlID == HLF_TXD)
3125 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 if (col >= change_start && col <= change_end)
3128 hlID = HLF_TXD; /* changed text */
3129 else
3130 hlID = HLF_CHD; /* changed line */
3131 }
3132 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3133#endif
3134}
3135
3136/*
3137 * "empty({expr})" function
3138 */
3139 static void
3140f_empty(typval_T *argvars, typval_T *rettv)
3141{
3142 int n = FALSE;
3143
3144 switch (argvars[0].v_type)
3145 {
3146 case VAR_STRING:
3147 case VAR_FUNC:
3148 n = argvars[0].vval.v_string == NULL
3149 || *argvars[0].vval.v_string == NUL;
3150 break;
3151 case VAR_PARTIAL:
3152 n = FALSE;
3153 break;
3154 case VAR_NUMBER:
3155 n = argvars[0].vval.v_number == 0;
3156 break;
3157 case VAR_FLOAT:
3158#ifdef FEAT_FLOAT
3159 n = argvars[0].vval.v_float == 0.0;
3160 break;
3161#endif
3162 case VAR_LIST:
3163 n = argvars[0].vval.v_list == NULL
3164 || argvars[0].vval.v_list->lv_first == NULL;
3165 break;
3166 case VAR_DICT:
3167 n = argvars[0].vval.v_dict == NULL
3168 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3169 break;
3170 case VAR_SPECIAL:
3171 n = argvars[0].vval.v_number != VVAL_TRUE;
3172 break;
3173
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003174 case VAR_BLOB:
3175 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003176 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3177 break;
3178
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003179 case VAR_JOB:
3180#ifdef FEAT_JOB_CHANNEL
3181 n = argvars[0].vval.v_job == NULL
3182 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3183 break;
3184#endif
3185 case VAR_CHANNEL:
3186#ifdef FEAT_JOB_CHANNEL
3187 n = argvars[0].vval.v_channel == NULL
3188 || !channel_is_open(argvars[0].vval.v_channel);
3189 break;
3190#endif
3191 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003192 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 n = TRUE;
3194 break;
3195 }
3196
3197 rettv->vval.v_number = n;
3198}
3199
3200/*
3201 * "escape({string}, {chars})" function
3202 */
3203 static void
3204f_escape(typval_T *argvars, typval_T *rettv)
3205{
3206 char_u buf[NUMBUFLEN];
3207
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003208 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3209 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003210 rettv->v_type = VAR_STRING;
3211}
3212
3213/*
3214 * "eval()" function
3215 */
3216 static void
3217f_eval(typval_T *argvars, typval_T *rettv)
3218{
3219 char_u *s, *p;
3220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003221 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 if (s != NULL)
3223 s = skipwhite(s);
3224
3225 p = s;
3226 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3227 {
3228 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003229 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 need_clr_eos = FALSE;
3231 rettv->v_type = VAR_NUMBER;
3232 rettv->vval.v_number = 0;
3233 }
3234 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003235 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236}
3237
3238/*
3239 * "eventhandler()" function
3240 */
3241 static void
3242f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3243{
3244 rettv->vval.v_number = vgetc_busy;
3245}
3246
3247/*
3248 * "executable()" function
3249 */
3250 static void
3251f_executable(typval_T *argvars, typval_T *rettv)
3252{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003253 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003254
3255 /* Check in $PATH and also check directly if there is a directory name. */
3256 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3257 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3258}
3259
3260static garray_T redir_execute_ga;
3261
3262/*
3263 * Append "value[value_len]" to the execute() output.
3264 */
3265 void
3266execute_redir_str(char_u *value, int value_len)
3267{
3268 int len;
3269
3270 if (value_len == -1)
3271 len = (int)STRLEN(value); /* Append the entire string */
3272 else
3273 len = value_len; /* Append only "value_len" characters */
3274 if (ga_grow(&redir_execute_ga, len) == OK)
3275 {
3276 mch_memmove((char *)redir_execute_ga.ga_data
3277 + redir_execute_ga.ga_len, value, len);
3278 redir_execute_ga.ga_len += len;
3279 }
3280}
3281
3282/*
3283 * Get next line from a list.
3284 * Called by do_cmdline() to get the next line.
3285 * Returns allocated string, or NULL for end of function.
3286 */
3287
3288 static char_u *
3289get_list_line(
3290 int c UNUSED,
3291 void *cookie,
3292 int indent UNUSED)
3293{
3294 listitem_T **p = (listitem_T **)cookie;
3295 listitem_T *item = *p;
3296 char_u buf[NUMBUFLEN];
3297 char_u *s;
3298
3299 if (item == NULL)
3300 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003301 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003302 *p = item->li_next;
3303 return s == NULL ? NULL : vim_strsave(s);
3304}
3305
3306/*
3307 * "execute()" function
3308 */
3309 static void
3310f_execute(typval_T *argvars, typval_T *rettv)
3311{
3312 char_u *cmd = NULL;
3313 list_T *list = NULL;
3314 int save_msg_silent = msg_silent;
3315 int save_emsg_silent = emsg_silent;
3316 int save_emsg_noredir = emsg_noredir;
3317 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003318 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003319 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003320 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003321 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003322
3323 rettv->vval.v_string = NULL;
3324 rettv->v_type = VAR_STRING;
3325
3326 if (argvars[0].v_type == VAR_LIST)
3327 {
3328 list = argvars[0].vval.v_list;
3329 if (list == NULL || list->lv_first == NULL)
3330 /* empty list, no commands, empty output */
3331 return;
3332 ++list->lv_refcount;
3333 }
3334 else
3335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003336 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 if (cmd == NULL)
3338 return;
3339 }
3340
3341 if (argvars[1].v_type != VAR_UNKNOWN)
3342 {
3343 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003344 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003345
3346 if (s == NULL)
3347 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003348 if (*s == NUL)
3349 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 if (STRNCMP(s, "silent", 6) == 0)
3351 ++msg_silent;
3352 if (STRCMP(s, "silent!") == 0)
3353 {
3354 emsg_silent = TRUE;
3355 emsg_noredir = TRUE;
3356 }
3357 }
3358 else
3359 ++msg_silent;
3360
3361 if (redir_execute)
3362 save_ga = redir_execute_ga;
3363 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3364 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003365 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003366 if (!echo_output)
3367 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003368
3369 if (cmd != NULL)
3370 do_cmdline_cmd(cmd);
3371 else
3372 {
3373 listitem_T *item = list->lv_first;
3374
3375 do_cmdline(NULL, get_list_line, (void *)&item,
3376 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3377 --list->lv_refcount;
3378 }
3379
Bram Moolenaard297f352017-01-29 20:31:21 +01003380 /* Need to append a NUL to the result. */
3381 if (ga_grow(&redir_execute_ga, 1) == OK)
3382 {
3383 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3384 rettv->vval.v_string = redir_execute_ga.ga_data;
3385 }
3386 else
3387 {
3388 ga_clear(&redir_execute_ga);
3389 rettv->vval.v_string = NULL;
3390 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003391 msg_silent = save_msg_silent;
3392 emsg_silent = save_emsg_silent;
3393 emsg_noredir = save_emsg_noredir;
3394
3395 redir_execute = save_redir_execute;
3396 if (redir_execute)
3397 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003398 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003399
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003400 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003401 if (echo_output)
3402 // When not working silently: put it in column zero. A following
3403 // "echon" will overwrite the message, unavoidably.
3404 msg_col = 0;
3405 else
3406 // When working silently: Put it back where it was, since nothing
3407 // should have been written.
3408 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003409}
3410
3411/*
3412 * "exepath()" function
3413 */
3414 static void
3415f_exepath(typval_T *argvars, typval_T *rettv)
3416{
3417 char_u *p = NULL;
3418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003420 rettv->v_type = VAR_STRING;
3421 rettv->vval.v_string = p;
3422}
3423
3424/*
3425 * "exists()" function
3426 */
3427 static void
3428f_exists(typval_T *argvars, typval_T *rettv)
3429{
3430 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003431 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 if (*p == '$') /* environment variable */
3435 {
3436 /* first try "normal" environment variables (fast) */
3437 if (mch_getenv(p + 1) != NULL)
3438 n = TRUE;
3439 else
3440 {
3441 /* try expanding things like $VIM and ${HOME} */
3442 p = expand_env_save(p);
3443 if (p != NULL && *p != '$')
3444 n = TRUE;
3445 vim_free(p);
3446 }
3447 }
3448 else if (*p == '&' || *p == '+') /* option */
3449 {
3450 n = (get_option_tv(&p, NULL, TRUE) == OK);
3451 if (*skipwhite(p) != NUL)
3452 n = FALSE; /* trailing garbage */
3453 }
3454 else if (*p == '*') /* internal or user defined function */
3455 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003456 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003457 }
3458 else if (*p == ':')
3459 {
3460 n = cmd_exists(p + 1);
3461 }
3462 else if (*p == '#')
3463 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 if (p[1] == '#')
3465 n = autocmd_supported(p + 2);
3466 else
3467 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468 }
3469 else /* internal variable */
3470 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003471 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003472 }
3473
3474 rettv->vval.v_number = n;
3475}
3476
3477#ifdef FEAT_FLOAT
3478/*
3479 * "exp()" function
3480 */
3481 static void
3482f_exp(typval_T *argvars, typval_T *rettv)
3483{
3484 float_T f = 0.0;
3485
3486 rettv->v_type = VAR_FLOAT;
3487 if (get_float_arg(argvars, &f) == OK)
3488 rettv->vval.v_float = exp(f);
3489 else
3490 rettv->vval.v_float = 0.0;
3491}
3492#endif
3493
3494/*
3495 * "expand()" function
3496 */
3497 static void
3498f_expand(typval_T *argvars, typval_T *rettv)
3499{
3500 char_u *s;
3501 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003502 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003503 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3504 expand_T xpc;
3505 int error = FALSE;
3506 char_u *result;
3507
3508 rettv->v_type = VAR_STRING;
3509 if (argvars[1].v_type != VAR_UNKNOWN
3510 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003511 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003512 && !error)
3513 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003514 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003515 }
3516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003517 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 if (*s == '%' || *s == '#' || *s == '<')
3519 {
3520 ++emsg_off;
3521 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3522 --emsg_off;
3523 if (rettv->v_type == VAR_LIST)
3524 {
3525 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3526 list_append_string(rettv->vval.v_list, result, -1);
3527 else
3528 vim_free(result);
3529 }
3530 else
3531 rettv->vval.v_string = result;
3532 }
3533 else
3534 {
3535 /* When the optional second argument is non-zero, don't remove matches
3536 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3537 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003538 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003539 options |= WILD_KEEP_ALL;
3540 if (!error)
3541 {
3542 ExpandInit(&xpc);
3543 xpc.xp_context = EXPAND_FILES;
3544 if (p_wic)
3545 options += WILD_ICASE;
3546 if (rettv->v_type == VAR_STRING)
3547 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3548 options, WILD_ALL);
3549 else if (rettv_list_alloc(rettv) != FAIL)
3550 {
3551 int i;
3552
3553 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3554 for (i = 0; i < xpc.xp_numfiles; i++)
3555 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3556 ExpandCleanup(&xpc);
3557 }
3558 }
3559 else
3560 rettv->vval.v_string = NULL;
3561 }
3562}
3563
3564/*
3565 * "extend(list, list [, idx])" function
3566 * "extend(dict, dict [, action])" function
3567 */
3568 static void
3569f_extend(typval_T *argvars, typval_T *rettv)
3570{
3571 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3572
3573 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3574 {
3575 list_T *l1, *l2;
3576 listitem_T *item;
3577 long before;
3578 int error = FALSE;
3579
3580 l1 = argvars[0].vval.v_list;
3581 l2 = argvars[1].vval.v_list;
3582 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3583 && l2 != NULL)
3584 {
3585 if (argvars[2].v_type != VAR_UNKNOWN)
3586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003587 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003588 if (error)
3589 return; /* type error; errmsg already given */
3590
3591 if (before == l1->lv_len)
3592 item = NULL;
3593 else
3594 {
3595 item = list_find(l1, before);
3596 if (item == NULL)
3597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003598 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599 return;
3600 }
3601 }
3602 }
3603 else
3604 item = NULL;
3605 list_extend(l1, l2, item);
3606
3607 copy_tv(&argvars[0], rettv);
3608 }
3609 }
3610 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3611 {
3612 dict_T *d1, *d2;
3613 char_u *action;
3614 int i;
3615
3616 d1 = argvars[0].vval.v_dict;
3617 d2 = argvars[1].vval.v_dict;
3618 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3619 && d2 != NULL)
3620 {
3621 /* Check the third argument. */
3622 if (argvars[2].v_type != VAR_UNKNOWN)
3623 {
3624 static char *(av[]) = {"keep", "force", "error"};
3625
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003626 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003627 if (action == NULL)
3628 return; /* type error; errmsg already given */
3629 for (i = 0; i < 3; ++i)
3630 if (STRCMP(action, av[i]) == 0)
3631 break;
3632 if (i == 3)
3633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003634 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 return;
3636 }
3637 }
3638 else
3639 action = (char_u *)"force";
3640
3641 dict_extend(d1, d2, action);
3642
3643 copy_tv(&argvars[0], rettv);
3644 }
3645 }
3646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003647 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003648}
3649
3650/*
3651 * "feedkeys()" function
3652 */
3653 static void
3654f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3655{
3656 int remap = TRUE;
3657 int insert = FALSE;
3658 char_u *keys, *flags;
3659 char_u nbuf[NUMBUFLEN];
3660 int typed = FALSE;
3661 int execute = FALSE;
3662 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003663 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 char_u *keys_esc;
3665
3666 /* This is not allowed in the sandbox. If the commands would still be
3667 * executed in the sandbox it would be OK, but it probably happens later,
3668 * when "sandbox" is no longer set. */
3669 if (check_secure())
3670 return;
3671
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003672 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003673
3674 if (argvars[1].v_type != VAR_UNKNOWN)
3675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003676 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003677 for ( ; *flags != NUL; ++flags)
3678 {
3679 switch (*flags)
3680 {
3681 case 'n': remap = FALSE; break;
3682 case 'm': remap = TRUE; break;
3683 case 't': typed = TRUE; break;
3684 case 'i': insert = TRUE; break;
3685 case 'x': execute = TRUE; break;
3686 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003687 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003688 }
3689 }
3690 }
3691
3692 if (*keys != NUL || execute)
3693 {
3694 /* Need to escape K_SPECIAL and CSI before putting the string in the
3695 * typeahead buffer. */
3696 keys_esc = vim_strsave_escape_csi(keys);
3697 if (keys_esc != NULL)
3698 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003699 if (lowlevel)
3700 {
3701#ifdef USE_INPUT_BUF
3702 add_to_input_buf(keys, (int)STRLEN(keys));
3703#else
3704 emsg(_("E980: lowlevel input not supported"));
3705#endif
3706 }
3707 else
3708 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003709 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3710 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003711 if (vgetc_busy
3712#ifdef FEAT_TIMERS
3713 || timer_busy
3714#endif
3715 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 typebuf_was_filled = TRUE;
3717 if (execute)
3718 {
3719 int save_msg_scroll = msg_scroll;
3720
3721 /* Avoid a 1 second delay when the keys start Insert mode. */
3722 msg_scroll = FALSE;
3723
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003724 if (!dangerous)
3725 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003726 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003727 if (!dangerous)
3728 --ex_normal_busy;
3729
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 msg_scroll |= save_msg_scroll;
3731 }
3732 }
3733 }
3734}
3735
3736/*
3737 * "filereadable()" function
3738 */
3739 static void
3740f_filereadable(typval_T *argvars, typval_T *rettv)
3741{
3742 int fd;
3743 char_u *p;
3744 int n;
3745
3746#ifndef O_NONBLOCK
3747# define O_NONBLOCK 0
3748#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003749 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3751 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3752 {
3753 n = TRUE;
3754 close(fd);
3755 }
3756 else
3757 n = FALSE;
3758
3759 rettv->vval.v_number = n;
3760}
3761
3762/*
3763 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3764 * rights to write into.
3765 */
3766 static void
3767f_filewritable(typval_T *argvars, typval_T *rettv)
3768{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003769 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770}
3771
3772 static void
3773findfilendir(
3774 typval_T *argvars UNUSED,
3775 typval_T *rettv,
3776 int find_what UNUSED)
3777{
3778#ifdef FEAT_SEARCHPATH
3779 char_u *fname;
3780 char_u *fresult = NULL;
3781 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3782 char_u *p;
3783 char_u pathbuf[NUMBUFLEN];
3784 int count = 1;
3785 int first = TRUE;
3786 int error = FALSE;
3787#endif
3788
3789 rettv->vval.v_string = NULL;
3790 rettv->v_type = VAR_STRING;
3791
3792#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003793 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003794
3795 if (argvars[1].v_type != VAR_UNKNOWN)
3796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003797 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798 if (p == NULL)
3799 error = TRUE;
3800 else
3801 {
3802 if (*p != NUL)
3803 path = p;
3804
3805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003806 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003807 }
3808 }
3809
3810 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3811 error = TRUE;
3812
3813 if (*fname != NUL && !error)
3814 {
3815 do
3816 {
3817 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3818 vim_free(fresult);
3819 fresult = find_file_in_path_option(first ? fname : NULL,
3820 first ? (int)STRLEN(fname) : 0,
3821 0, first, path,
3822 find_what,
3823 curbuf->b_ffname,
3824 find_what == FINDFILE_DIR
3825 ? (char_u *)"" : curbuf->b_p_sua);
3826 first = FALSE;
3827
3828 if (fresult != NULL && rettv->v_type == VAR_LIST)
3829 list_append_string(rettv->vval.v_list, fresult, -1);
3830
3831 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3832 }
3833
3834 if (rettv->v_type == VAR_STRING)
3835 rettv->vval.v_string = fresult;
3836#endif
3837}
3838
3839/*
3840 * "filter()" function
3841 */
3842 static void
3843f_filter(typval_T *argvars, typval_T *rettv)
3844{
3845 filter_map(argvars, rettv, FALSE);
3846}
3847
3848/*
3849 * "finddir({fname}[, {path}[, {count}]])" function
3850 */
3851 static void
3852f_finddir(typval_T *argvars, typval_T *rettv)
3853{
3854 findfilendir(argvars, rettv, FINDFILE_DIR);
3855}
3856
3857/*
3858 * "findfile({fname}[, {path}[, {count}]])" function
3859 */
3860 static void
3861f_findfile(typval_T *argvars, typval_T *rettv)
3862{
3863 findfilendir(argvars, rettv, FINDFILE_FILE);
3864}
3865
3866#ifdef FEAT_FLOAT
3867/*
3868 * "float2nr({float})" function
3869 */
3870 static void
3871f_float2nr(typval_T *argvars, typval_T *rettv)
3872{
3873 float_T f = 0.0;
3874
3875 if (get_float_arg(argvars, &f) == OK)
3876 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003877 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003878 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003879 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003880 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003881 else
3882 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883 }
3884}
3885
3886/*
3887 * "floor({float})" function
3888 */
3889 static void
3890f_floor(typval_T *argvars, typval_T *rettv)
3891{
3892 float_T f = 0.0;
3893
3894 rettv->v_type = VAR_FLOAT;
3895 if (get_float_arg(argvars, &f) == OK)
3896 rettv->vval.v_float = floor(f);
3897 else
3898 rettv->vval.v_float = 0.0;
3899}
3900
3901/*
3902 * "fmod()" function
3903 */
3904 static void
3905f_fmod(typval_T *argvars, typval_T *rettv)
3906{
3907 float_T fx = 0.0, fy = 0.0;
3908
3909 rettv->v_type = VAR_FLOAT;
3910 if (get_float_arg(argvars, &fx) == OK
3911 && get_float_arg(&argvars[1], &fy) == OK)
3912 rettv->vval.v_float = fmod(fx, fy);
3913 else
3914 rettv->vval.v_float = 0.0;
3915}
3916#endif
3917
3918/*
3919 * "fnameescape({string})" function
3920 */
3921 static void
3922f_fnameescape(typval_T *argvars, typval_T *rettv)
3923{
3924 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003925 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 rettv->v_type = VAR_STRING;
3927}
3928
3929/*
3930 * "fnamemodify({fname}, {mods})" function
3931 */
3932 static void
3933f_fnamemodify(typval_T *argvars, typval_T *rettv)
3934{
3935 char_u *fname;
3936 char_u *mods;
3937 int usedlen = 0;
3938 int len;
3939 char_u *fbuf = NULL;
3940 char_u buf[NUMBUFLEN];
3941
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003942 fname = tv_get_string_chk(&argvars[0]);
3943 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 if (fname == NULL || mods == NULL)
3945 fname = NULL;
3946 else
3947 {
3948 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003949 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 }
3951
3952 rettv->v_type = VAR_STRING;
3953 if (fname == NULL)
3954 rettv->vval.v_string = NULL;
3955 else
3956 rettv->vval.v_string = vim_strnsave(fname, len);
3957 vim_free(fbuf);
3958}
3959
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003960/*
3961 * "foldclosed()" function
3962 */
3963 static void
3964foldclosed_both(
3965 typval_T *argvars UNUSED,
3966 typval_T *rettv,
3967 int end UNUSED)
3968{
3969#ifdef FEAT_FOLDING
3970 linenr_T lnum;
3971 linenr_T first, last;
3972
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003973 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3975 {
3976 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3977 {
3978 if (end)
3979 rettv->vval.v_number = (varnumber_T)last;
3980 else
3981 rettv->vval.v_number = (varnumber_T)first;
3982 return;
3983 }
3984 }
3985#endif
3986 rettv->vval.v_number = -1;
3987}
3988
3989/*
3990 * "foldclosed()" function
3991 */
3992 static void
3993f_foldclosed(typval_T *argvars, typval_T *rettv)
3994{
3995 foldclosed_both(argvars, rettv, FALSE);
3996}
3997
3998/*
3999 * "foldclosedend()" function
4000 */
4001 static void
4002f_foldclosedend(typval_T *argvars, typval_T *rettv)
4003{
4004 foldclosed_both(argvars, rettv, TRUE);
4005}
4006
4007/*
4008 * "foldlevel()" function
4009 */
4010 static void
4011f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4012{
4013#ifdef FEAT_FOLDING
4014 linenr_T lnum;
4015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004016 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004017 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4018 rettv->vval.v_number = foldLevel(lnum);
4019#endif
4020}
4021
4022/*
4023 * "foldtext()" function
4024 */
4025 static void
4026f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4027{
4028#ifdef FEAT_FOLDING
4029 linenr_T foldstart;
4030 linenr_T foldend;
4031 char_u *dashes;
4032 linenr_T lnum;
4033 char_u *s;
4034 char_u *r;
4035 int len;
4036 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004037 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004038#endif
4039
4040 rettv->v_type = VAR_STRING;
4041 rettv->vval.v_string = NULL;
4042#ifdef FEAT_FOLDING
4043 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4044 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4045 dashes = get_vim_var_str(VV_FOLDDASHES);
4046 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4047 && dashes != NULL)
4048 {
4049 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004050 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004051 if (!linewhite(lnum))
4052 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004053
4054 /* Find interesting text in this line. */
4055 s = skipwhite(ml_get(lnum));
4056 /* skip C comment-start */
4057 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4058 {
4059 s = skipwhite(s + 2);
4060 if (*skipwhite(s) == NUL
4061 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4062 {
4063 s = skipwhite(ml_get(lnum + 1));
4064 if (*s == '*')
4065 s = skipwhite(s + 1);
4066 }
4067 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004068 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004069 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 r = alloc((unsigned)(STRLEN(txt)
4071 + STRLEN(dashes) /* for %s */
4072 + 20 /* for %3ld */
4073 + STRLEN(s))); /* concatenated */
4074 if (r != NULL)
4075 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004076 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004077 len = (int)STRLEN(r);
4078 STRCAT(r, s);
4079 /* remove 'foldmarker' and 'commentstring' */
4080 foldtext_cleanup(r + len);
4081 rettv->vval.v_string = r;
4082 }
4083 }
4084#endif
4085}
4086
4087/*
4088 * "foldtextresult(lnum)" function
4089 */
4090 static void
4091f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4092{
4093#ifdef FEAT_FOLDING
4094 linenr_T lnum;
4095 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004096 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004097 foldinfo_T foldinfo;
4098 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004099 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004100#endif
4101
4102 rettv->v_type = VAR_STRING;
4103 rettv->vval.v_string = NULL;
4104#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004105 if (entered)
4106 return; /* reject recursive use */
4107 entered = TRUE;
4108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004109 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004110 /* treat illegal types and illegal string values for {lnum} the same */
4111 if (lnum < 0)
4112 lnum = 0;
4113 fold_count = foldedCount(curwin, lnum, &foldinfo);
4114 if (fold_count > 0)
4115 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004116 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4117 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004118 if (text == buf)
4119 text = vim_strsave(text);
4120 rettv->vval.v_string = text;
4121 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004122
4123 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004124#endif
4125}
4126
4127/*
4128 * "foreground()" function
4129 */
4130 static void
4131f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4132{
4133#ifdef FEAT_GUI
4134 if (gui.in_use)
4135 gui_mch_set_foreground();
4136#else
4137# ifdef WIN32
4138 win32_set_foreground();
4139# endif
4140#endif
4141}
4142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004144common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145{
4146 char_u *s;
4147 char_u *name;
4148 int use_string = FALSE;
4149 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004150 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151
4152 if (argvars[0].v_type == VAR_FUNC)
4153 {
4154 /* function(MyFunc, [arg], dict) */
4155 s = argvars[0].vval.v_string;
4156 }
4157 else if (argvars[0].v_type == VAR_PARTIAL
4158 && argvars[0].vval.v_partial != NULL)
4159 {
4160 /* function(dict.MyFunc, [arg]) */
4161 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004162 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004163 }
4164 else
4165 {
4166 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004167 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004168 use_string = TRUE;
4169 }
4170
Bram Moolenaar843b8842016-08-21 14:36:15 +02004171 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004172 {
4173 name = s;
4174 trans_name = trans_function_name(&name, FALSE,
4175 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4176 if (*name != NUL)
4177 s = NULL;
4178 }
4179
Bram Moolenaar843b8842016-08-21 14:36:15 +02004180 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4181 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004183 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004184 else if (trans_name != NULL && (is_funcref
4185 ? find_func(trans_name) == NULL
4186 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004187 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004188 else
4189 {
4190 int dict_idx = 0;
4191 int arg_idx = 0;
4192 list_T *list = NULL;
4193
4194 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4195 {
4196 char sid_buf[25];
4197 int off = *s == 's' ? 2 : 5;
4198
4199 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4200 * also be called from another script. Using trans_function_name()
4201 * would also work, but some plugins depend on the name being
4202 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004203 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004204 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4205 if (name != NULL)
4206 {
4207 STRCPY(name, sid_buf);
4208 STRCAT(name, s + off);
4209 }
4210 }
4211 else
4212 name = vim_strsave(s);
4213
4214 if (argvars[1].v_type != VAR_UNKNOWN)
4215 {
4216 if (argvars[2].v_type != VAR_UNKNOWN)
4217 {
4218 /* function(name, [args], dict) */
4219 arg_idx = 1;
4220 dict_idx = 2;
4221 }
4222 else if (argvars[1].v_type == VAR_DICT)
4223 /* function(name, dict) */
4224 dict_idx = 1;
4225 else
4226 /* function(name, [args]) */
4227 arg_idx = 1;
4228 if (dict_idx > 0)
4229 {
4230 if (argvars[dict_idx].v_type != VAR_DICT)
4231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004232 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004233 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004234 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004235 }
4236 if (argvars[dict_idx].vval.v_dict == NULL)
4237 dict_idx = 0;
4238 }
4239 if (arg_idx > 0)
4240 {
4241 if (argvars[arg_idx].v_type != VAR_LIST)
4242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004243 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004245 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004246 }
4247 list = argvars[arg_idx].vval.v_list;
4248 if (list == NULL || list->lv_len == 0)
4249 arg_idx = 0;
4250 }
4251 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004252 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004253 {
4254 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4255
4256 /* result is a VAR_PARTIAL */
4257 if (pt == NULL)
4258 vim_free(name);
4259 else
4260 {
4261 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4262 {
4263 listitem_T *li;
4264 int i = 0;
4265 int arg_len = 0;
4266 int lv_len = 0;
4267
4268 if (arg_pt != NULL)
4269 arg_len = arg_pt->pt_argc;
4270 if (list != NULL)
4271 lv_len = list->lv_len;
4272 pt->pt_argc = arg_len + lv_len;
4273 pt->pt_argv = (typval_T *)alloc(
4274 sizeof(typval_T) * pt->pt_argc);
4275 if (pt->pt_argv == NULL)
4276 {
4277 vim_free(pt);
4278 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004279 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004281 for (i = 0; i < arg_len; i++)
4282 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4283 if (lv_len > 0)
4284 for (li = list->lv_first; li != NULL;
4285 li = li->li_next)
4286 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 }
4288
4289 /* For "function(dict.func, [], dict)" and "func" is a partial
4290 * use "dict". That is backwards compatible. */
4291 if (dict_idx > 0)
4292 {
4293 /* The dict is bound explicitly, pt_auto is FALSE. */
4294 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4295 ++pt->pt_dict->dv_refcount;
4296 }
4297 else if (arg_pt != NULL)
4298 {
4299 /* If the dict was bound automatically the result is also
4300 * bound automatically. */
4301 pt->pt_dict = arg_pt->pt_dict;
4302 pt->pt_auto = arg_pt->pt_auto;
4303 if (pt->pt_dict != NULL)
4304 ++pt->pt_dict->dv_refcount;
4305 }
4306
4307 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004308 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4309 {
4310 pt->pt_func = arg_pt->pt_func;
4311 func_ptr_ref(pt->pt_func);
4312 vim_free(name);
4313 }
4314 else if (is_funcref)
4315 {
4316 pt->pt_func = find_func(trans_name);
4317 func_ptr_ref(pt->pt_func);
4318 vim_free(name);
4319 }
4320 else
4321 {
4322 pt->pt_name = name;
4323 func_ref(name);
4324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004325 }
4326 rettv->v_type = VAR_PARTIAL;
4327 rettv->vval.v_partial = pt;
4328 }
4329 else
4330 {
4331 /* result is a VAR_FUNC */
4332 rettv->v_type = VAR_FUNC;
4333 rettv->vval.v_string = name;
4334 func_ref(name);
4335 }
4336 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004337theend:
4338 vim_free(trans_name);
4339}
4340
4341/*
4342 * "funcref()" function
4343 */
4344 static void
4345f_funcref(typval_T *argvars, typval_T *rettv)
4346{
4347 common_function(argvars, rettv, TRUE);
4348}
4349
4350/*
4351 * "function()" function
4352 */
4353 static void
4354f_function(typval_T *argvars, typval_T *rettv)
4355{
4356 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357}
4358
4359/*
4360 * "garbagecollect()" function
4361 */
4362 static void
4363f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4364{
4365 /* This is postponed until we are back at the toplevel, because we may be
4366 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4367 want_garbage_collect = TRUE;
4368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004369 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 garbage_collect_at_exit = TRUE;
4371}
4372
4373/*
4374 * "get()" function
4375 */
4376 static void
4377f_get(typval_T *argvars, typval_T *rettv)
4378{
4379 listitem_T *li;
4380 list_T *l;
4381 dictitem_T *di;
4382 dict_T *d;
4383 typval_T *tv = NULL;
4384
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004385 if (argvars[0].v_type == VAR_BLOB)
4386 {
4387 int error = FALSE;
4388 int idx = tv_get_number_chk(&argvars[1], &error);
4389
4390 if (!error)
4391 {
4392 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004393 if (idx < 0)
4394 idx = blob_len(argvars[0].vval.v_blob) + idx;
4395 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4396 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004397 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004398 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004399 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004400 tv = rettv;
4401 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004402 }
4403 }
4404 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 {
4406 if ((l = argvars[0].vval.v_list) != NULL)
4407 {
4408 int error = FALSE;
4409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004410 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 if (!error && li != NULL)
4412 tv = &li->li_tv;
4413 }
4414 }
4415 else if (argvars[0].v_type == VAR_DICT)
4416 {
4417 if ((d = argvars[0].vval.v_dict) != NULL)
4418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004419 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 if (di != NULL)
4421 tv = &di->di_tv;
4422 }
4423 }
4424 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4425 {
4426 partial_T *pt;
4427 partial_T fref_pt;
4428
4429 if (argvars[0].v_type == VAR_PARTIAL)
4430 pt = argvars[0].vval.v_partial;
4431 else
4432 {
4433 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4434 fref_pt.pt_name = argvars[0].vval.v_string;
4435 pt = &fref_pt;
4436 }
4437
4438 if (pt != NULL)
4439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004440 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442
4443 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4444 {
4445 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004446 n = partial_name(pt);
4447 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 rettv->vval.v_string = NULL;
4449 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 {
4451 rettv->vval.v_string = vim_strsave(n);
4452 if (rettv->v_type == VAR_FUNC)
4453 func_ref(rettv->vval.v_string);
4454 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 }
4456 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004457 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 else if (STRCMP(what, "args") == 0)
4459 {
4460 rettv->v_type = VAR_LIST;
4461 if (rettv_list_alloc(rettv) == OK)
4462 {
4463 int i;
4464
4465 for (i = 0; i < pt->pt_argc; ++i)
4466 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4467 }
4468 }
4469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004470 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 return;
4472 }
4473 }
4474 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004475 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004476
4477 if (tv == NULL)
4478 {
4479 if (argvars[2].v_type != VAR_UNKNOWN)
4480 copy_tv(&argvars[2], rettv);
4481 }
4482 else
4483 copy_tv(tv, rettv);
4484}
4485
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004486/*
4487 * Returns buffer options, variables and other attributes in a dictionary.
4488 */
4489 static dict_T *
4490get_buffer_info(buf_T *buf)
4491{
4492 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004493 tabpage_T *tp;
4494 win_T *wp;
4495 list_T *windows;
4496
4497 dict = dict_alloc();
4498 if (dict == NULL)
4499 return NULL;
4500
Bram Moolenaare0be1672018-07-08 16:50:37 +02004501 dict_add_number(dict, "bufnr", buf->b_fnum);
4502 dict_add_string(dict, "name", buf->b_ffname);
4503 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4504 : buflist_findlnum(buf));
4505 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4506 dict_add_number(dict, "listed", buf->b_p_bl);
4507 dict_add_number(dict, "changed", bufIsChanged(buf));
4508 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4509 dict_add_number(dict, "hidden",
4510 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004511
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004512 /* Get a reference to buffer variables */
4513 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004514
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004515 /* List of windows displaying this buffer */
4516 windows = list_alloc();
4517 if (windows != NULL)
4518 {
4519 FOR_ALL_TAB_WINDOWS(tp, wp)
4520 if (wp->w_buffer == buf)
4521 list_append_number(windows, (varnumber_T)wp->w_id);
4522 dict_add_list(dict, "windows", windows);
4523 }
4524
4525#ifdef FEAT_SIGNS
4526 if (buf->b_signlist != NULL)
4527 {
4528 /* List of signs placed in this buffer */
4529 list_T *signs = list_alloc();
4530 if (signs != NULL)
4531 {
4532 get_buffer_signs(buf, signs);
4533 dict_add_list(dict, "signs", signs);
4534 }
4535 }
4536#endif
4537
4538 return dict;
4539}
4540
4541/*
4542 * "getbufinfo()" function
4543 */
4544 static void
4545f_getbufinfo(typval_T *argvars, typval_T *rettv)
4546{
4547 buf_T *buf = NULL;
4548 buf_T *argbuf = NULL;
4549 dict_T *d;
4550 int filtered = FALSE;
4551 int sel_buflisted = FALSE;
4552 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004553 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004554
4555 if (rettv_list_alloc(rettv) != OK)
4556 return;
4557
4558 /* List of all the buffers or selected buffers */
4559 if (argvars[0].v_type == VAR_DICT)
4560 {
4561 dict_T *sel_d = argvars[0].vval.v_dict;
4562
4563 if (sel_d != NULL)
4564 {
4565 dictitem_T *di;
4566
4567 filtered = TRUE;
4568
4569 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004570 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004571 sel_buflisted = TRUE;
4572
4573 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004574 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004575 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004576
4577 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004578 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004579 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004580 }
4581 }
4582 else if (argvars[0].v_type != VAR_UNKNOWN)
4583 {
4584 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004585 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004586 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004587 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004588 --emsg_off;
4589 if (argbuf == NULL)
4590 return;
4591 }
4592
4593 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004594 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004595 {
4596 if (argbuf != NULL && argbuf != buf)
4597 continue;
4598 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004599 || (sel_buflisted && !buf->b_p_bl)
4600 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004601 continue;
4602
4603 d = get_buffer_info(buf);
4604 if (d != NULL)
4605 list_append_dict(rettv->vval.v_list, d);
4606 if (argbuf != NULL)
4607 return;
4608 }
4609}
4610
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004611/*
4612 * Get line or list of lines from buffer "buf" into "rettv".
4613 * Return a range (from start to end) of lines in rettv from the specified
4614 * buffer.
4615 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4616 */
4617 static void
4618get_buffer_lines(
4619 buf_T *buf,
4620 linenr_T start,
4621 linenr_T end,
4622 int retlist,
4623 typval_T *rettv)
4624{
4625 char_u *p;
4626
4627 rettv->v_type = VAR_STRING;
4628 rettv->vval.v_string = NULL;
4629 if (retlist && rettv_list_alloc(rettv) == FAIL)
4630 return;
4631
4632 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4633 return;
4634
4635 if (!retlist)
4636 {
4637 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4638 p = ml_get_buf(buf, start, FALSE);
4639 else
4640 p = (char_u *)"";
4641 rettv->vval.v_string = vim_strsave(p);
4642 }
4643 else
4644 {
4645 if (end < start)
4646 return;
4647
4648 if (start < 1)
4649 start = 1;
4650 if (end > buf->b_ml.ml_line_count)
4651 end = buf->b_ml.ml_line_count;
4652 while (start <= end)
4653 if (list_append_string(rettv->vval.v_list,
4654 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4655 break;
4656 }
4657}
4658
4659/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004660 * "getbufline()" function
4661 */
4662 static void
4663f_getbufline(typval_T *argvars, typval_T *rettv)
4664{
4665 linenr_T lnum;
4666 linenr_T end;
4667 buf_T *buf;
4668
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004669 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004671 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004672 --emsg_off;
4673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004674 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 if (argvars[2].v_type == VAR_UNKNOWN)
4676 end = lnum;
4677 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004678 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679
4680 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4681}
4682
4683/*
4684 * "getbufvar()" function
4685 */
4686 static void
4687f_getbufvar(typval_T *argvars, typval_T *rettv)
4688{
4689 buf_T *buf;
4690 buf_T *save_curbuf;
4691 char_u *varname;
4692 dictitem_T *v;
4693 int done = FALSE;
4694
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4696 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004697 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004698 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004699
4700 rettv->v_type = VAR_STRING;
4701 rettv->vval.v_string = NULL;
4702
4703 if (buf != NULL && varname != NULL)
4704 {
4705 /* set curbuf to be our buf, temporarily */
4706 save_curbuf = curbuf;
4707 curbuf = buf;
4708
Bram Moolenaar30567352016-08-27 21:25:44 +02004709 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004710 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004711 if (varname[1] == NUL)
4712 {
4713 /* get all buffer-local options in a dict */
4714 dict_T *opts = get_winbuf_options(TRUE);
4715
4716 if (opts != NULL)
4717 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004718 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004719 done = TRUE;
4720 }
4721 }
4722 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4723 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 done = TRUE;
4725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 else
4727 {
4728 /* Look up the variable. */
4729 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4730 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4731 'b', varname, FALSE);
4732 if (v != NULL)
4733 {
4734 copy_tv(&v->di_tv, rettv);
4735 done = TRUE;
4736 }
4737 }
4738
4739 /* restore previous notion of curbuf */
4740 curbuf = save_curbuf;
4741 }
4742
4743 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4744 /* use the default value */
4745 copy_tv(&argvars[2], rettv);
4746
4747 --emsg_off;
4748}
4749
4750/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004751 * "getchangelist()" function
4752 */
4753 static void
4754f_getchangelist(typval_T *argvars, typval_T *rettv)
4755{
4756#ifdef FEAT_JUMPLIST
4757 buf_T *buf;
4758 int i;
4759 list_T *l;
4760 dict_T *d;
4761#endif
4762
4763 if (rettv_list_alloc(rettv) != OK)
4764 return;
4765
4766#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004767 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004768 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004769 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004770 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004771 if (buf == NULL)
4772 return;
4773
4774 l = list_alloc();
4775 if (l == NULL)
4776 return;
4777
4778 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4779 return;
4780 /*
4781 * The current window change list index tracks only the position in the
4782 * current buffer change list. For other buffers, use the change list
4783 * length as the current index.
4784 */
4785 list_append_number(rettv->vval.v_list,
4786 (varnumber_T)((buf == curwin->w_buffer)
4787 ? curwin->w_changelistidx : buf->b_changelistlen));
4788
4789 for (i = 0; i < buf->b_changelistlen; ++i)
4790 {
4791 if (buf->b_changelist[i].lnum == 0)
4792 continue;
4793 if ((d = dict_alloc()) == NULL)
4794 return;
4795 if (list_append_dict(l, d) == FAIL)
4796 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004797 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4798 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02004799 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004800 }
4801#endif
4802}
4803/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004804 * "getchar()" function
4805 */
4806 static void
4807f_getchar(typval_T *argvars, typval_T *rettv)
4808{
4809 varnumber_T n;
4810 int error = FALSE;
4811
Bram Moolenaar84d93902018-09-11 20:10:20 +02004812#ifdef MESSAGE_QUEUE
4813 // vpeekc() used to check for messages, but that caused problems, invoking
4814 // a callback where it was not expected. Some plugins use getchar(1) in a
4815 // loop to await a message, therefore make sure we check for messages here.
4816 parse_queued_messages();
4817#endif
4818
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004819 /* Position the cursor. Needed after a message that ends in a space. */
4820 windgoto(msg_row, msg_col);
4821
4822 ++no_mapping;
4823 ++allow_keys;
4824 for (;;)
4825 {
4826 if (argvars[0].v_type == VAR_UNKNOWN)
4827 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004828 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004829 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004830 /* getchar(1): only check if char avail */
4831 n = vpeekc_any();
4832 else if (error || vpeekc_any() == NUL)
4833 /* illegal argument or getchar(0) and no char avail: return zero */
4834 n = 0;
4835 else
4836 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004837 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838
4839 if (n == K_IGNORE)
4840 continue;
4841 break;
4842 }
4843 --no_mapping;
4844 --allow_keys;
4845
4846 set_vim_var_nr(VV_MOUSE_WIN, 0);
4847 set_vim_var_nr(VV_MOUSE_WINID, 0);
4848 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4849 set_vim_var_nr(VV_MOUSE_COL, 0);
4850
4851 rettv->vval.v_number = n;
4852 if (IS_SPECIAL(n) || mod_mask != 0)
4853 {
4854 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4855 int i = 0;
4856
4857 /* Turn a special key into three bytes, plus modifier. */
4858 if (mod_mask != 0)
4859 {
4860 temp[i++] = K_SPECIAL;
4861 temp[i++] = KS_MODIFIER;
4862 temp[i++] = mod_mask;
4863 }
4864 if (IS_SPECIAL(n))
4865 {
4866 temp[i++] = K_SPECIAL;
4867 temp[i++] = K_SECOND(n);
4868 temp[i++] = K_THIRD(n);
4869 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004870 else if (has_mbyte)
4871 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004872 else
4873 temp[i++] = n;
4874 temp[i++] = NUL;
4875 rettv->v_type = VAR_STRING;
4876 rettv->vval.v_string = vim_strsave(temp);
4877
4878#ifdef FEAT_MOUSE
4879 if (is_mouse_key(n))
4880 {
4881 int row = mouse_row;
4882 int col = mouse_col;
4883 win_T *win;
4884 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886 int winnr = 1;
4887
4888 if (row >= 0 && col >= 0)
4889 {
4890 /* Find the window at the mouse coordinates and compute the
4891 * text position. */
4892 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004893 if (win == NULL)
4894 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004896 for (wp = firstwin; wp != win; wp = wp->w_next)
4897 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004898 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4899 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4900 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4901 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4902 }
4903 }
4904#endif
4905 }
4906}
4907
4908/*
4909 * "getcharmod()" function
4910 */
4911 static void
4912f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4913{
4914 rettv->vval.v_number = mod_mask;
4915}
4916
4917/*
4918 * "getcharsearch()" function
4919 */
4920 static void
4921f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4922{
4923 if (rettv_dict_alloc(rettv) != FAIL)
4924 {
4925 dict_T *dict = rettv->vval.v_dict;
4926
Bram Moolenaare0be1672018-07-08 16:50:37 +02004927 dict_add_string(dict, "char", last_csearch());
4928 dict_add_number(dict, "forward", last_csearch_forward());
4929 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004930 }
4931}
4932
4933/*
4934 * "getcmdline()" function
4935 */
4936 static void
4937f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4938{
4939 rettv->v_type = VAR_STRING;
4940 rettv->vval.v_string = get_cmdline_str();
4941}
4942
4943/*
4944 * "getcmdpos()" function
4945 */
4946 static void
4947f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4948{
4949 rettv->vval.v_number = get_cmdline_pos() + 1;
4950}
4951
4952/*
4953 * "getcmdtype()" function
4954 */
4955 static void
4956f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4957{
4958 rettv->v_type = VAR_STRING;
4959 rettv->vval.v_string = alloc(2);
4960 if (rettv->vval.v_string != NULL)
4961 {
4962 rettv->vval.v_string[0] = get_cmdline_type();
4963 rettv->vval.v_string[1] = NUL;
4964 }
4965}
4966
4967/*
4968 * "getcmdwintype()" function
4969 */
4970 static void
4971f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4972{
4973 rettv->v_type = VAR_STRING;
4974 rettv->vval.v_string = NULL;
4975#ifdef FEAT_CMDWIN
4976 rettv->vval.v_string = alloc(2);
4977 if (rettv->vval.v_string != NULL)
4978 {
4979 rettv->vval.v_string[0] = cmdwin_type;
4980 rettv->vval.v_string[1] = NUL;
4981 }
4982#endif
4983}
4984
4985#if defined(FEAT_CMDL_COMPL)
4986/*
4987 * "getcompletion()" function
4988 */
4989 static void
4990f_getcompletion(typval_T *argvars, typval_T *rettv)
4991{
4992 char_u *pat;
4993 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004994 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02004995 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
4996 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004997
Bram Moolenaare9d58a62016-08-13 15:07:41 +02004998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004999 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005000
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005001 if (p_wic)
5002 options |= WILD_ICASE;
5003
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005004 /* For filtered results, 'wildignore' is used */
5005 if (!filtered)
5006 options |= WILD_KEEP_ALL;
5007
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005008 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005009 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005010 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005011 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005012 if (xpc.xp_context == EXPAND_NOTHING)
5013 {
5014 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005015 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005016 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005017 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 return;
5019 }
5020
5021# if defined(FEAT_MENU)
5022 if (xpc.xp_context == EXPAND_MENUS)
5023 {
5024 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5025 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5026 }
5027# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005028#ifdef FEAT_CSCOPE
5029 if (xpc.xp_context == EXPAND_CSCOPE)
5030 {
5031 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5032 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5033 }
5034#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005035#ifdef FEAT_SIGNS
5036 if (xpc.xp_context == EXPAND_SIGN)
5037 {
5038 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5039 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5040 }
5041#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005042
5043 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5044 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5045 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005046 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005047
5048 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5049
5050 for (i = 0; i < xpc.xp_numfiles; i++)
5051 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5052 }
5053 vim_free(pat);
5054 ExpandCleanup(&xpc);
5055}
5056#endif
5057
5058/*
5059 * "getcwd()" function
5060 */
5061 static void
5062f_getcwd(typval_T *argvars, typval_T *rettv)
5063{
5064 win_T *wp = NULL;
5065 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005066 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005067
5068 rettv->v_type = VAR_STRING;
5069 rettv->vval.v_string = NULL;
5070
Bram Moolenaar54591292018-02-09 20:53:59 +01005071 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5072 global = TRUE;
5073 else
5074 wp = find_tabwin(&argvars[0], &argvars[1]);
5075
5076 if (wp != NULL && wp->w_localdir != NULL)
5077 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5078 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005080 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005081 rettv->vval.v_string = vim_strsave(globaldir);
5082 else
5083 {
5084 cwd = alloc(MAXPATHL);
5085 if (cwd != NULL)
5086 {
5087 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5088 rettv->vval.v_string = vim_strsave(cwd);
5089 vim_free(cwd);
5090 }
5091 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005093#ifdef BACKSLASH_IN_FILENAME
5094 if (rettv->vval.v_string != NULL)
5095 slash_adjust(rettv->vval.v_string);
5096#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097}
5098
5099/*
5100 * "getfontname()" function
5101 */
5102 static void
5103f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5104{
5105 rettv->v_type = VAR_STRING;
5106 rettv->vval.v_string = NULL;
5107#ifdef FEAT_GUI
5108 if (gui.in_use)
5109 {
5110 GuiFont font;
5111 char_u *name = NULL;
5112
5113 if (argvars[0].v_type == VAR_UNKNOWN)
5114 {
5115 /* Get the "Normal" font. Either the name saved by
5116 * hl_set_font_name() or from the font ID. */
5117 font = gui.norm_font;
5118 name = hl_get_font_name();
5119 }
5120 else
5121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005122 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5124 return;
5125 font = gui_mch_get_font(name, FALSE);
5126 if (font == NOFONT)
5127 return; /* Invalid font name, return empty string. */
5128 }
5129 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5130 if (argvars[0].v_type != VAR_UNKNOWN)
5131 gui_mch_free_font(font);
5132 }
5133#endif
5134}
5135
5136/*
5137 * "getfperm({fname})" function
5138 */
5139 static void
5140f_getfperm(typval_T *argvars, typval_T *rettv)
5141{
5142 char_u *fname;
5143 stat_T st;
5144 char_u *perm = NULL;
5145 char_u flags[] = "rwx";
5146 int i;
5147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005148 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149
5150 rettv->v_type = VAR_STRING;
5151 if (mch_stat((char *)fname, &st) >= 0)
5152 {
5153 perm = vim_strsave((char_u *)"---------");
5154 if (perm != NULL)
5155 {
5156 for (i = 0; i < 9; i++)
5157 {
5158 if (st.st_mode & (1 << (8 - i)))
5159 perm[i] = flags[i % 3];
5160 }
5161 }
5162 }
5163 rettv->vval.v_string = perm;
5164}
5165
5166/*
5167 * "getfsize({fname})" function
5168 */
5169 static void
5170f_getfsize(typval_T *argvars, typval_T *rettv)
5171{
5172 char_u *fname;
5173 stat_T st;
5174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005175 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005176
5177 rettv->v_type = VAR_NUMBER;
5178
5179 if (mch_stat((char *)fname, &st) >= 0)
5180 {
5181 if (mch_isdir(fname))
5182 rettv->vval.v_number = 0;
5183 else
5184 {
5185 rettv->vval.v_number = (varnumber_T)st.st_size;
5186
5187 /* non-perfect check for overflow */
5188 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5189 rettv->vval.v_number = -2;
5190 }
5191 }
5192 else
5193 rettv->vval.v_number = -1;
5194}
5195
5196/*
5197 * "getftime({fname})" function
5198 */
5199 static void
5200f_getftime(typval_T *argvars, typval_T *rettv)
5201{
5202 char_u *fname;
5203 stat_T st;
5204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005205 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005206
5207 if (mch_stat((char *)fname, &st) >= 0)
5208 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5209 else
5210 rettv->vval.v_number = -1;
5211}
5212
5213/*
5214 * "getftype({fname})" function
5215 */
5216 static void
5217f_getftype(typval_T *argvars, typval_T *rettv)
5218{
5219 char_u *fname;
5220 stat_T st;
5221 char_u *type = NULL;
5222 char *t;
5223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005224 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225
5226 rettv->v_type = VAR_STRING;
5227 if (mch_lstat((char *)fname, &st) >= 0)
5228 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229 if (S_ISREG(st.st_mode))
5230 t = "file";
5231 else if (S_ISDIR(st.st_mode))
5232 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 else if (S_ISLNK(st.st_mode))
5234 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005235 else if (S_ISBLK(st.st_mode))
5236 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 else if (S_ISCHR(st.st_mode))
5238 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239 else if (S_ISFIFO(st.st_mode))
5240 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005242 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243 else
5244 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245 type = vim_strsave((char_u *)t);
5246 }
5247 rettv->vval.v_string = type;
5248}
5249
5250/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005251 * "getjumplist()" function
5252 */
5253 static void
5254f_getjumplist(typval_T *argvars, typval_T *rettv)
5255{
5256#ifdef FEAT_JUMPLIST
5257 win_T *wp;
5258 int i;
5259 list_T *l;
5260 dict_T *d;
5261#endif
5262
5263 if (rettv_list_alloc(rettv) != OK)
5264 return;
5265
5266#ifdef FEAT_JUMPLIST
5267 wp = find_tabwin(&argvars[0], &argvars[1]);
5268 if (wp == NULL)
5269 return;
5270
5271 l = list_alloc();
5272 if (l == NULL)
5273 return;
5274
5275 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5276 return;
5277 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5278
Bram Moolenaar48679742018-02-13 13:33:29 +01005279 cleanup_jumplist(wp, TRUE);
5280
Bram Moolenaar4f505882018-02-10 21:06:32 +01005281 for (i = 0; i < wp->w_jumplistlen; ++i)
5282 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005283 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5284 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005285 if ((d = dict_alloc()) == NULL)
5286 return;
5287 if (list_append_dict(l, d) == FAIL)
5288 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005289 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5290 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005291 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005292 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005293 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005294 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005295 }
5296#endif
5297}
5298
5299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005300 * "getline(lnum, [end])" function
5301 */
5302 static void
5303f_getline(typval_T *argvars, typval_T *rettv)
5304{
5305 linenr_T lnum;
5306 linenr_T end;
5307 int retlist;
5308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005309 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005310 if (argvars[1].v_type == VAR_UNKNOWN)
5311 {
5312 end = 0;
5313 retlist = FALSE;
5314 }
5315 else
5316 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005317 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 retlist = TRUE;
5319 }
5320
5321 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5322}
5323
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005324#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005325 static void
5326get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5327{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005328 if (what_arg->v_type == VAR_UNKNOWN)
5329 {
5330 if (rettv_list_alloc(rettv) == OK)
5331 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005332 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005333 }
5334 else
5335 {
5336 if (rettv_dict_alloc(rettv) == OK)
5337 if (is_qf || (wp != NULL))
5338 {
5339 if (what_arg->v_type == VAR_DICT)
5340 {
5341 dict_T *d = what_arg->vval.v_dict;
5342
5343 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005344 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005345 }
5346 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005347 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005348 }
5349 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005350}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005351#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005352
5353/*
5354 * "getloclist()" function
5355 */
5356 static void
5357f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5358{
5359#ifdef FEAT_QUICKFIX
5360 win_T *wp;
5361
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005362 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005363 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5364#endif
5365}
5366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367/*
5368 * "getmatches()" function
5369 */
5370 static void
5371f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5372{
5373#ifdef FEAT_SEARCH_EXTRA
5374 dict_T *dict;
5375 matchitem_T *cur = curwin->w_match_head;
5376 int i;
5377
5378 if (rettv_list_alloc(rettv) == OK)
5379 {
5380 while (cur != NULL)
5381 {
5382 dict = dict_alloc();
5383 if (dict == NULL)
5384 return;
5385 if (cur->match.regprog == NULL)
5386 {
5387 /* match added with matchaddpos() */
5388 for (i = 0; i < MAXPOSMATCH; ++i)
5389 {
5390 llpos_T *llpos;
5391 char buf[6];
5392 list_T *l;
5393
5394 llpos = &cur->pos.pos[i];
5395 if (llpos->lnum == 0)
5396 break;
5397 l = list_alloc();
5398 if (l == NULL)
5399 break;
5400 list_append_number(l, (varnumber_T)llpos->lnum);
5401 if (llpos->col > 0)
5402 {
5403 list_append_number(l, (varnumber_T)llpos->col);
5404 list_append_number(l, (varnumber_T)llpos->len);
5405 }
5406 sprintf(buf, "pos%d", i + 1);
5407 dict_add_list(dict, buf, l);
5408 }
5409 }
5410 else
5411 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005412 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005413 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005414 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5415 dict_add_number(dict, "priority", (long)cur->priority);
5416 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar13505972019-01-24 15:04:48 +01005417# if defined(FEAT_CONCEAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005418 if (cur->conceal_char)
5419 {
5420 char_u buf[MB_MAXBYTES + 1];
5421
5422 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005423 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005424 }
5425# endif
5426 list_append_dict(rettv->vval.v_list, dict);
5427 cur = cur->next;
5428 }
5429 }
5430#endif
5431}
5432
5433/*
5434 * "getpid()" function
5435 */
5436 static void
5437f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5438{
5439 rettv->vval.v_number = mch_get_pid();
5440}
5441
5442 static void
5443getpos_both(
5444 typval_T *argvars,
5445 typval_T *rettv,
5446 int getcurpos)
5447{
5448 pos_T *fp;
5449 list_T *l;
5450 int fnum = -1;
5451
5452 if (rettv_list_alloc(rettv) == OK)
5453 {
5454 l = rettv->vval.v_list;
5455 if (getcurpos)
5456 fp = &curwin->w_cursor;
5457 else
5458 fp = var2fpos(&argvars[0], TRUE, &fnum);
5459 if (fnum != -1)
5460 list_append_number(l, (varnumber_T)fnum);
5461 else
5462 list_append_number(l, (varnumber_T)0);
5463 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5464 : (varnumber_T)0);
5465 list_append_number(l, (fp != NULL)
5466 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5467 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005468 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005469 (varnumber_T)0);
5470 if (getcurpos)
5471 {
5472 update_curswant();
5473 list_append_number(l, curwin->w_curswant == MAXCOL ?
5474 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5475 }
5476 }
5477 else
5478 rettv->vval.v_number = FALSE;
5479}
5480
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481/*
5482 * "getcurpos()" function
5483 */
5484 static void
5485f_getcurpos(typval_T *argvars, typval_T *rettv)
5486{
5487 getpos_both(argvars, rettv, TRUE);
5488}
5489
5490/*
5491 * "getpos(string)" function
5492 */
5493 static void
5494f_getpos(typval_T *argvars, typval_T *rettv)
5495{
5496 getpos_both(argvars, rettv, FALSE);
5497}
5498
5499/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005500 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005501 */
5502 static void
5503f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5504{
5505#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005506 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005507#endif
5508}
5509
5510/*
5511 * "getreg()" function
5512 */
5513 static void
5514f_getreg(typval_T *argvars, typval_T *rettv)
5515{
5516 char_u *strregname;
5517 int regname;
5518 int arg2 = FALSE;
5519 int return_list = FALSE;
5520 int error = FALSE;
5521
5522 if (argvars[0].v_type != VAR_UNKNOWN)
5523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005524 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005525 error = strregname == NULL;
5526 if (argvars[1].v_type != VAR_UNKNOWN)
5527 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005528 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005530 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005531 }
5532 }
5533 else
5534 strregname = get_vim_var_str(VV_REG);
5535
5536 if (error)
5537 return;
5538
5539 regname = (strregname == NULL ? '"' : *strregname);
5540 if (regname == 0)
5541 regname = '"';
5542
5543 if (return_list)
5544 {
5545 rettv->v_type = VAR_LIST;
5546 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5547 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5548 if (rettv->vval.v_list == NULL)
5549 (void)rettv_list_alloc(rettv);
5550 else
5551 ++rettv->vval.v_list->lv_refcount;
5552 }
5553 else
5554 {
5555 rettv->v_type = VAR_STRING;
5556 rettv->vval.v_string = get_reg_contents(regname,
5557 arg2 ? GREG_EXPR_SRC : 0);
5558 }
5559}
5560
5561/*
5562 * "getregtype()" function
5563 */
5564 static void
5565f_getregtype(typval_T *argvars, typval_T *rettv)
5566{
5567 char_u *strregname;
5568 int regname;
5569 char_u buf[NUMBUFLEN + 2];
5570 long reglen = 0;
5571
5572 if (argvars[0].v_type != VAR_UNKNOWN)
5573 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005574 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005575 if (strregname == NULL) /* type error; errmsg already given */
5576 {
5577 rettv->v_type = VAR_STRING;
5578 rettv->vval.v_string = NULL;
5579 return;
5580 }
5581 }
5582 else
5583 /* Default to v:register */
5584 strregname = get_vim_var_str(VV_REG);
5585
5586 regname = (strregname == NULL ? '"' : *strregname);
5587 if (regname == 0)
5588 regname = '"';
5589
5590 buf[0] = NUL;
5591 buf[1] = NUL;
5592 switch (get_reg_type(regname, &reglen))
5593 {
5594 case MLINE: buf[0] = 'V'; break;
5595 case MCHAR: buf[0] = 'v'; break;
5596 case MBLOCK:
5597 buf[0] = Ctrl_V;
5598 sprintf((char *)buf + 1, "%ld", reglen + 1);
5599 break;
5600 }
5601 rettv->v_type = VAR_STRING;
5602 rettv->vval.v_string = vim_strsave(buf);
5603}
5604
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005605/*
5606 * Returns information (variables, options, etc.) about a tab page
5607 * as a dictionary.
5608 */
5609 static dict_T *
5610get_tabpage_info(tabpage_T *tp, int tp_idx)
5611{
5612 win_T *wp;
5613 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005614 list_T *l;
5615
5616 dict = dict_alloc();
5617 if (dict == NULL)
5618 return NULL;
5619
Bram Moolenaare0be1672018-07-08 16:50:37 +02005620 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005621
5622 l = list_alloc();
5623 if (l != NULL)
5624 {
5625 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5626 wp; wp = wp->w_next)
5627 list_append_number(l, (varnumber_T)wp->w_id);
5628 dict_add_list(dict, "windows", l);
5629 }
5630
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005631 /* Make a reference to tabpage variables */
5632 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005633
5634 return dict;
5635}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005636
5637/*
5638 * "gettabinfo()" function
5639 */
5640 static void
5641f_gettabinfo(typval_T *argvars, typval_T *rettv)
5642{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005643 tabpage_T *tp, *tparg = NULL;
5644 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005645 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005646
5647 if (rettv_list_alloc(rettv) != OK)
5648 return;
5649
5650 if (argvars[0].v_type != VAR_UNKNOWN)
5651 {
5652 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005653 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654 if (tparg == NULL)
5655 return;
5656 }
5657
5658 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005659 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005660 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005661 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005662 if (tparg != NULL && tp != tparg)
5663 continue;
5664 d = get_tabpage_info(tp, tpnr);
5665 if (d != NULL)
5666 list_append_dict(rettv->vval.v_list, d);
5667 if (tparg != NULL)
5668 return;
5669 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005670}
5671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672/*
5673 * "gettabvar()" function
5674 */
5675 static void
5676f_gettabvar(typval_T *argvars, typval_T *rettv)
5677{
5678 win_T *oldcurwin;
5679 tabpage_T *tp, *oldtabpage;
5680 dictitem_T *v;
5681 char_u *varname;
5682 int done = FALSE;
5683
5684 rettv->v_type = VAR_STRING;
5685 rettv->vval.v_string = NULL;
5686
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005687 varname = tv_get_string_chk(&argvars[1]);
5688 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689 if (tp != NULL && varname != NULL)
5690 {
5691 /* Set tp to be our tabpage, temporarily. Also set the window to the
5692 * first window in the tabpage, otherwise the window is not valid. */
5693 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005694 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5695 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 {
5697 /* look up the variable */
5698 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5699 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5700 if (v != NULL)
5701 {
5702 copy_tv(&v->di_tv, rettv);
5703 done = TRUE;
5704 }
5705 }
5706
5707 /* restore previous notion of curwin */
5708 restore_win(oldcurwin, oldtabpage, TRUE);
5709 }
5710
5711 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5712 copy_tv(&argvars[2], rettv);
5713}
5714
5715/*
5716 * "gettabwinvar()" function
5717 */
5718 static void
5719f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5720{
5721 getwinvar(argvars, rettv, 1);
5722}
5723
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005724/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005725 * "gettagstack()" function
5726 */
5727 static void
5728f_gettagstack(typval_T *argvars, typval_T *rettv)
5729{
5730 win_T *wp = curwin; // default is current window
5731
5732 if (rettv_dict_alloc(rettv) != OK)
5733 return;
5734
5735 if (argvars[0].v_type != VAR_UNKNOWN)
5736 {
5737 wp = find_win_by_nr_or_id(&argvars[0]);
5738 if (wp == NULL)
5739 return;
5740 }
5741
5742 get_tagstack(wp, rettv->vval.v_dict);
5743}
5744
5745/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005746 * Returns information about a window as a dictionary.
5747 */
5748 static dict_T *
5749get_win_info(win_T *wp, short tpnr, short winnr)
5750{
5751 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752
5753 dict = dict_alloc();
5754 if (dict == NULL)
5755 return NULL;
5756
Bram Moolenaare0be1672018-07-08 16:50:37 +02005757 dict_add_number(dict, "tabnr", tpnr);
5758 dict_add_number(dict, "winnr", winnr);
5759 dict_add_number(dict, "winid", wp->w_id);
5760 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005761 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005762#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005763 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005764#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005765 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005766 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005767 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005768
Bram Moolenaar69905d12017-08-13 18:14:47 +02005769#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005770 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005771#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005772#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005773 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5774 dict_add_number(dict, "loclist",
5775 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005776#endif
5777
Bram Moolenaar30567352016-08-27 21:25:44 +02005778 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005779 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005780
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005781 return dict;
5782}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005783
5784/*
5785 * "getwininfo()" function
5786 */
5787 static void
5788f_getwininfo(typval_T *argvars, typval_T *rettv)
5789{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005790 tabpage_T *tp;
5791 win_T *wp = NULL, *wparg = NULL;
5792 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005793 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005794
5795 if (rettv_list_alloc(rettv) != OK)
5796 return;
5797
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005798 if (argvars[0].v_type != VAR_UNKNOWN)
5799 {
5800 wparg = win_id2wp(argvars);
5801 if (wparg == NULL)
5802 return;
5803 }
5804
5805 /* Collect information about either all the windows across all the tab
5806 * pages or one particular window.
5807 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005808 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005809 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005810 tabnr++;
5811 winnr = 0;
5812 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005813 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005814 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005815 if (wparg != NULL && wp != wparg)
5816 continue;
5817 d = get_win_info(wp, tabnr, winnr);
5818 if (d != NULL)
5819 list_append_dict(rettv->vval.v_list, d);
5820 if (wparg != NULL)
5821 /* found information about a specific window */
5822 return;
5823 }
5824 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005825}
5826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828 * "win_findbuf()" function
5829 */
5830 static void
5831f_win_findbuf(typval_T *argvars, typval_T *rettv)
5832{
5833 if (rettv_list_alloc(rettv) != FAIL)
5834 win_findbuf(argvars, rettv->vval.v_list);
5835}
5836
5837/*
5838 * "win_getid()" function
5839 */
5840 static void
5841f_win_getid(typval_T *argvars, typval_T *rettv)
5842{
5843 rettv->vval.v_number = win_getid(argvars);
5844}
5845
5846/*
5847 * "win_gotoid()" function
5848 */
5849 static void
5850f_win_gotoid(typval_T *argvars, typval_T *rettv)
5851{
5852 rettv->vval.v_number = win_gotoid(argvars);
5853}
5854
5855/*
5856 * "win_id2tabwin()" function
5857 */
5858 static void
5859f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5860{
5861 if (rettv_list_alloc(rettv) != FAIL)
5862 win_id2tabwin(argvars, rettv->vval.v_list);
5863}
5864
5865/*
5866 * "win_id2win()" function
5867 */
5868 static void
5869f_win_id2win(typval_T *argvars, typval_T *rettv)
5870{
5871 rettv->vval.v_number = win_id2win(argvars);
5872}
5873
5874/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005875 * "win_screenpos()" function
5876 */
5877 static void
5878f_win_screenpos(typval_T *argvars, typval_T *rettv)
5879{
5880 win_T *wp;
5881
5882 if (rettv_list_alloc(rettv) == FAIL)
5883 return;
5884
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005885 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005886 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5887 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5888}
5889
5890/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005891 * "getwinpos({timeout})" function
5892 */
5893 static void
5894f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5895{
5896 int x = -1;
5897 int y = -1;
5898
5899 if (rettv_list_alloc(rettv) == FAIL)
5900 return;
5901#ifdef FEAT_GUI
5902 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005903 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005904# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5905 else
5906# endif
5907#endif
5908#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5909 {
5910 varnumber_T timeout = 100;
5911
5912 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005913 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005914 term_get_winpos(&x, &y, timeout);
5915 }
5916#endif
5917 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5918 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5919}
5920
5921
5922/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005923 * "getwinposx()" function
5924 */
5925 static void
5926f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5927{
5928 rettv->vval.v_number = -1;
5929#ifdef FEAT_GUI
5930 if (gui.in_use)
5931 {
5932 int x, y;
5933
5934 if (gui_mch_get_winpos(&x, &y) == OK)
5935 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005936 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005937 }
5938#endif
5939#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5940 {
5941 int x, y;
5942
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005943 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005944 rettv->vval.v_number = x;
5945 }
5946#endif
5947}
5948
5949/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005950 * "getwinposy()" function
5951 */
5952 static void
5953f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5954{
5955 rettv->vval.v_number = -1;
5956#ifdef FEAT_GUI
5957 if (gui.in_use)
5958 {
5959 int x, y;
5960
5961 if (gui_mch_get_winpos(&x, &y) == OK)
5962 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005963 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005964 }
5965#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005966#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5967 {
5968 int x, y;
5969
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005970 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005971 rettv->vval.v_number = y;
5972 }
5973#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005974}
5975
5976/*
5977 * "getwinvar()" function
5978 */
5979 static void
5980f_getwinvar(typval_T *argvars, typval_T *rettv)
5981{
5982 getwinvar(argvars, rettv, 0);
5983}
5984
5985/*
5986 * "glob()" function
5987 */
5988 static void
5989f_glob(typval_T *argvars, typval_T *rettv)
5990{
5991 int options = WILD_SILENT|WILD_USE_NL;
5992 expand_T xpc;
5993 int error = FALSE;
5994
5995 /* When the optional second argument is non-zero, don't remove matches
5996 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
5997 rettv->v_type = VAR_STRING;
5998 if (argvars[1].v_type != VAR_UNKNOWN)
5999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006000 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006001 options |= WILD_KEEP_ALL;
6002 if (argvars[2].v_type != VAR_UNKNOWN)
6003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006004 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006006 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006007 }
6008 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006009 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 options |= WILD_ALLLINKS;
6011 }
6012 }
6013 if (!error)
6014 {
6015 ExpandInit(&xpc);
6016 xpc.xp_context = EXPAND_FILES;
6017 if (p_wic)
6018 options += WILD_ICASE;
6019 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006020 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006021 NULL, options, WILD_ALL);
6022 else if (rettv_list_alloc(rettv) != FAIL)
6023 {
6024 int i;
6025
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006026 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 NULL, options, WILD_ALL_KEEP);
6028 for (i = 0; i < xpc.xp_numfiles; i++)
6029 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6030
6031 ExpandCleanup(&xpc);
6032 }
6033 }
6034 else
6035 rettv->vval.v_string = NULL;
6036}
6037
6038/*
6039 * "globpath()" function
6040 */
6041 static void
6042f_globpath(typval_T *argvars, typval_T *rettv)
6043{
6044 int flags = 0;
6045 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006046 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006047 int error = FALSE;
6048 garray_T ga;
6049 int i;
6050
6051 /* When the optional second argument is non-zero, don't remove matches
6052 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6053 rettv->v_type = VAR_STRING;
6054 if (argvars[2].v_type != VAR_UNKNOWN)
6055 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006056 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006057 flags |= WILD_KEEP_ALL;
6058 if (argvars[3].v_type != VAR_UNKNOWN)
6059 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006060 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006061 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006062 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006063 }
6064 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006065 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006066 flags |= WILD_ALLLINKS;
6067 }
6068 }
6069 if (file != NULL && !error)
6070 {
6071 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006072 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006073 if (rettv->v_type == VAR_STRING)
6074 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6075 else if (rettv_list_alloc(rettv) != FAIL)
6076 for (i = 0; i < ga.ga_len; ++i)
6077 list_append_string(rettv->vval.v_list,
6078 ((char_u **)(ga.ga_data))[i], -1);
6079 ga_clear_strings(&ga);
6080 }
6081 else
6082 rettv->vval.v_string = NULL;
6083}
6084
6085/*
6086 * "glob2regpat()" function
6087 */
6088 static void
6089f_glob2regpat(typval_T *argvars, typval_T *rettv)
6090{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006091 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092
6093 rettv->v_type = VAR_STRING;
6094 rettv->vval.v_string = (pat == NULL)
6095 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6096}
6097
6098/* for VIM_VERSION_ defines */
6099#include "version.h"
6100
6101/*
6102 * "has()" function
6103 */
6104 static void
6105f_has(typval_T *argvars, typval_T *rettv)
6106{
6107 int i;
6108 char_u *name;
6109 int n = FALSE;
6110 static char *(has_list[]) =
6111 {
6112#ifdef AMIGA
6113 "amiga",
6114# ifdef FEAT_ARP
6115 "arp",
6116# endif
6117#endif
6118#ifdef __BEOS__
6119 "beos",
6120#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006121#if defined(BSD) && !defined(MACOS_X)
6122 "bsd",
6123#endif
6124#ifdef hpux
6125 "hpux",
6126#endif
6127#ifdef __linux__
6128 "linux",
6129#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006130#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006131 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6132 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006133# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006134 "macunix", /* Mac OS X, with the darwin feature */
6135 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006136# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006137#endif
6138#ifdef __QNX__
6139 "qnx",
6140#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006141#ifdef SUN_SYSTEM
6142 "sun",
6143#else
6144 "moon",
6145#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006146#ifdef UNIX
6147 "unix",
6148#endif
6149#ifdef VMS
6150 "vms",
6151#endif
6152#ifdef WIN32
6153 "win32",
6154#endif
6155#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6156 "win32unix",
6157#endif
6158#if defined(WIN64) || defined(_WIN64)
6159 "win64",
6160#endif
6161#ifdef EBCDIC
6162 "ebcdic",
6163#endif
6164#ifndef CASE_INSENSITIVE_FILENAME
6165 "fname_case",
6166#endif
6167#ifdef HAVE_ACL
6168 "acl",
6169#endif
6170#ifdef FEAT_ARABIC
6171 "arabic",
6172#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006174#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006175 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006176#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006177#ifdef FEAT_AUTOSERVERNAME
6178 "autoservername",
6179#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006180#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 "balloon_eval",
6182# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6183 "balloon_multiline",
6184# endif
6185#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006186#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006187 "balloon_eval_term",
6188#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006189#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6190 "builtin_terms",
6191# ifdef ALL_BUILTIN_TCAPS
6192 "all_builtin_terms",
6193# endif
6194#endif
6195#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6196 || defined(FEAT_GUI_W32) \
6197 || defined(FEAT_GUI_MOTIF))
6198 "browsefilter",
6199#endif
6200#ifdef FEAT_BYTEOFF
6201 "byte_offset",
6202#endif
6203#ifdef FEAT_JOB_CHANNEL
6204 "channel",
6205#endif
6206#ifdef FEAT_CINDENT
6207 "cindent",
6208#endif
6209#ifdef FEAT_CLIENTSERVER
6210 "clientserver",
6211#endif
6212#ifdef FEAT_CLIPBOARD
6213 "clipboard",
6214#endif
6215#ifdef FEAT_CMDL_COMPL
6216 "cmdline_compl",
6217#endif
6218#ifdef FEAT_CMDHIST
6219 "cmdline_hist",
6220#endif
6221#ifdef FEAT_COMMENTS
6222 "comments",
6223#endif
6224#ifdef FEAT_CONCEAL
6225 "conceal",
6226#endif
6227#ifdef FEAT_CRYPT
6228 "cryptv",
6229 "crypt-blowfish",
6230 "crypt-blowfish2",
6231#endif
6232#ifdef FEAT_CSCOPE
6233 "cscope",
6234#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006236#ifdef CURSOR_SHAPE
6237 "cursorshape",
6238#endif
6239#ifdef DEBUG
6240 "debug",
6241#endif
6242#ifdef FEAT_CON_DIALOG
6243 "dialog_con",
6244#endif
6245#ifdef FEAT_GUI_DIALOG
6246 "dialog_gui",
6247#endif
6248#ifdef FEAT_DIFF
6249 "diff",
6250#endif
6251#ifdef FEAT_DIGRAPHS
6252 "digraphs",
6253#endif
6254#ifdef FEAT_DIRECTX
6255 "directx",
6256#endif
6257#ifdef FEAT_DND
6258 "dnd",
6259#endif
6260#ifdef FEAT_EMACS_TAGS
6261 "emacs_tags",
6262#endif
6263 "eval", /* always present, of course! */
6264 "ex_extra", /* graduated feature */
6265#ifdef FEAT_SEARCH_EXTRA
6266 "extra_search",
6267#endif
6268#ifdef FEAT_FKMAP
6269 "farsi",
6270#endif
6271#ifdef FEAT_SEARCHPATH
6272 "file_in_path",
6273#endif
6274#ifdef FEAT_FILTERPIPE
6275 "filterpipe",
6276#endif
6277#ifdef FEAT_FIND_ID
6278 "find_in_path",
6279#endif
6280#ifdef FEAT_FLOAT
6281 "float",
6282#endif
6283#ifdef FEAT_FOLDING
6284 "folding",
6285#endif
6286#ifdef FEAT_FOOTER
6287 "footer",
6288#endif
6289#if !defined(USE_SYSTEM) && defined(UNIX)
6290 "fork",
6291#endif
6292#ifdef FEAT_GETTEXT
6293 "gettext",
6294#endif
6295#ifdef FEAT_GUI
6296 "gui",
6297#endif
6298#ifdef FEAT_GUI_ATHENA
6299# ifdef FEAT_GUI_NEXTAW
6300 "gui_neXtaw",
6301# else
6302 "gui_athena",
6303# endif
6304#endif
6305#ifdef FEAT_GUI_GTK
6306 "gui_gtk",
6307# ifdef USE_GTK3
6308 "gui_gtk3",
6309# else
6310 "gui_gtk2",
6311# endif
6312#endif
6313#ifdef FEAT_GUI_GNOME
6314 "gui_gnome",
6315#endif
6316#ifdef FEAT_GUI_MAC
6317 "gui_mac",
6318#endif
6319#ifdef FEAT_GUI_MOTIF
6320 "gui_motif",
6321#endif
6322#ifdef FEAT_GUI_PHOTON
6323 "gui_photon",
6324#endif
6325#ifdef FEAT_GUI_W32
6326 "gui_win32",
6327#endif
6328#ifdef FEAT_HANGULIN
6329 "hangul_input",
6330#endif
6331#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6332 "iconv",
6333#endif
6334#ifdef FEAT_INS_EXPAND
6335 "insert_expand",
6336#endif
6337#ifdef FEAT_JOB_CHANNEL
6338 "job",
6339#endif
6340#ifdef FEAT_JUMPLIST
6341 "jumplist",
6342#endif
6343#ifdef FEAT_KEYMAP
6344 "keymap",
6345#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006346 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347#ifdef FEAT_LANGMAP
6348 "langmap",
6349#endif
6350#ifdef FEAT_LIBCALL
6351 "libcall",
6352#endif
6353#ifdef FEAT_LINEBREAK
6354 "linebreak",
6355#endif
6356#ifdef FEAT_LISP
6357 "lispindent",
6358#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006360#ifdef FEAT_LOCALMAP
6361 "localmap",
6362#endif
6363#ifdef FEAT_LUA
6364# ifndef DYNAMIC_LUA
6365 "lua",
6366# endif
6367#endif
6368#ifdef FEAT_MENU
6369 "menu",
6370#endif
6371#ifdef FEAT_SESSION
6372 "mksession",
6373#endif
6374#ifdef FEAT_MODIFY_FNAME
6375 "modify_fname",
6376#endif
6377#ifdef FEAT_MOUSE
6378 "mouse",
6379#endif
6380#ifdef FEAT_MOUSESHAPE
6381 "mouseshape",
6382#endif
6383#if defined(UNIX) || defined(VMS)
6384# ifdef FEAT_MOUSE_DEC
6385 "mouse_dec",
6386# endif
6387# ifdef FEAT_MOUSE_GPM
6388 "mouse_gpm",
6389# endif
6390# ifdef FEAT_MOUSE_JSB
6391 "mouse_jsbterm",
6392# endif
6393# ifdef FEAT_MOUSE_NET
6394 "mouse_netterm",
6395# endif
6396# ifdef FEAT_MOUSE_PTERM
6397 "mouse_pterm",
6398# endif
6399# ifdef FEAT_MOUSE_SGR
6400 "mouse_sgr",
6401# endif
6402# ifdef FEAT_SYSMOUSE
6403 "mouse_sysmouse",
6404# endif
6405# ifdef FEAT_MOUSE_URXVT
6406 "mouse_urxvt",
6407# endif
6408# ifdef FEAT_MOUSE_XTERM
6409 "mouse_xterm",
6410# endif
6411#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006412 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006413#ifdef FEAT_MBYTE_IME
6414 "multi_byte_ime",
6415#endif
6416#ifdef FEAT_MULTI_LANG
6417 "multi_lang",
6418#endif
6419#ifdef FEAT_MZSCHEME
6420#ifndef DYNAMIC_MZSCHEME
6421 "mzscheme",
6422#endif
6423#endif
6424#ifdef FEAT_NUM64
6425 "num64",
6426#endif
6427#ifdef FEAT_OLE
6428 "ole",
6429#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006430#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006433#ifdef FEAT_PATH_EXTRA
6434 "path_extra",
6435#endif
6436#ifdef FEAT_PERL
6437#ifndef DYNAMIC_PERL
6438 "perl",
6439#endif
6440#endif
6441#ifdef FEAT_PERSISTENT_UNDO
6442 "persistent_undo",
6443#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006444#if defined(FEAT_PYTHON)
6445 "python_compiled",
6446# if defined(DYNAMIC_PYTHON)
6447 "python_dynamic",
6448# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006450 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006451# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006452#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006453#if defined(FEAT_PYTHON3)
6454 "python3_compiled",
6455# if defined(DYNAMIC_PYTHON3)
6456 "python3_dynamic",
6457# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006459 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006460# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006461#endif
6462#ifdef FEAT_POSTSCRIPT
6463 "postscript",
6464#endif
6465#ifdef FEAT_PRINTER
6466 "printer",
6467#endif
6468#ifdef FEAT_PROFILE
6469 "profile",
6470#endif
6471#ifdef FEAT_RELTIME
6472 "reltime",
6473#endif
6474#ifdef FEAT_QUICKFIX
6475 "quickfix",
6476#endif
6477#ifdef FEAT_RIGHTLEFT
6478 "rightleft",
6479#endif
6480#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6481 "ruby",
6482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484#ifdef FEAT_CMDL_INFO
6485 "showcmd",
6486 "cmdline_info",
6487#endif
6488#ifdef FEAT_SIGNS
6489 "signs",
6490#endif
6491#ifdef FEAT_SMARTINDENT
6492 "smartindent",
6493#endif
6494#ifdef STARTUPTIME
6495 "startuptime",
6496#endif
6497#ifdef FEAT_STL_OPT
6498 "statusline",
6499#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500#ifdef FEAT_NETBEANS_INTG
6501 "netbeans_intg",
6502#endif
6503#ifdef FEAT_SPELL
6504 "spell",
6505#endif
6506#ifdef FEAT_SYN_HL
6507 "syntax",
6508#endif
6509#if defined(USE_SYSTEM) || !defined(UNIX)
6510 "system",
6511#endif
6512#ifdef FEAT_TAG_BINS
6513 "tag_binary",
6514#endif
6515#ifdef FEAT_TAG_OLDSTATIC
6516 "tag_old_static",
6517#endif
6518#ifdef FEAT_TAG_ANYWHITE
6519 "tag_any_white",
6520#endif
6521#ifdef FEAT_TCL
6522# ifndef DYNAMIC_TCL
6523 "tcl",
6524# endif
6525#endif
6526#ifdef FEAT_TERMGUICOLORS
6527 "termguicolors",
6528#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006529#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006530 "terminal",
6531#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532#ifdef TERMINFO
6533 "terminfo",
6534#endif
6535#ifdef FEAT_TERMRESPONSE
6536 "termresponse",
6537#endif
6538#ifdef FEAT_TEXTOBJ
6539 "textobjects",
6540#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006541#ifdef FEAT_TEXT_PROP
6542 "textprop",
6543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006544#ifdef HAVE_TGETENT
6545 "tgetent",
6546#endif
6547#ifdef FEAT_TIMERS
6548 "timers",
6549#endif
6550#ifdef FEAT_TITLE
6551 "title",
6552#endif
6553#ifdef FEAT_TOOLBAR
6554 "toolbar",
6555#endif
6556#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6557 "unnamedplus",
6558#endif
6559#ifdef FEAT_USR_CMDS
6560 "user-commands", /* was accidentally included in 5.4 */
6561 "user_commands",
6562#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006563#ifdef FEAT_VARTABS
6564 "vartabs",
6565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#ifdef FEAT_VIMINFO
6567 "viminfo",
6568#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006574#ifdef FEAT_VTP
6575 "vtp",
6576#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577#ifdef FEAT_WILDIGN
6578 "wildignore",
6579#endif
6580#ifdef FEAT_WILDMENU
6581 "wildmenu",
6582#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006583 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006584#ifdef FEAT_WAK
6585 "winaltkeys",
6586#endif
6587#ifdef FEAT_WRITEBACKUP
6588 "writebackup",
6589#endif
6590#ifdef FEAT_XIM
6591 "xim",
6592#endif
6593#ifdef FEAT_XFONTSET
6594 "xfontset",
6595#endif
6596#ifdef FEAT_XPM_W32
6597 "xpm",
6598 "xpm_w32", /* for backward compatibility */
6599#else
6600# if defined(HAVE_XPM)
6601 "xpm",
6602# endif
6603#endif
6604#ifdef USE_XSMP
6605 "xsmp",
6606#endif
6607#ifdef USE_XSMP_INTERACT
6608 "xsmp_interact",
6609#endif
6610#ifdef FEAT_XCLIPBOARD
6611 "xterm_clipboard",
6612#endif
6613#ifdef FEAT_XTERM_SAVE
6614 "xterm_save",
6615#endif
6616#if defined(UNIX) && defined(FEAT_X11)
6617 "X11",
6618#endif
6619 NULL
6620 };
6621
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006622 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006623 for (i = 0; has_list[i] != NULL; ++i)
6624 if (STRICMP(name, has_list[i]) == 0)
6625 {
6626 n = TRUE;
6627 break;
6628 }
6629
6630 if (n == FALSE)
6631 {
6632 if (STRNICMP(name, "patch", 5) == 0)
6633 {
6634 if (name[5] == '-'
6635 && STRLEN(name) >= 11
6636 && vim_isdigit(name[6])
6637 && vim_isdigit(name[8])
6638 && vim_isdigit(name[10]))
6639 {
6640 int major = atoi((char *)name + 6);
6641 int minor = atoi((char *)name + 8);
6642
6643 /* Expect "patch-9.9.01234". */
6644 n = (major < VIM_VERSION_MAJOR
6645 || (major == VIM_VERSION_MAJOR
6646 && (minor < VIM_VERSION_MINOR
6647 || (minor == VIM_VERSION_MINOR
6648 && has_patch(atoi((char *)name + 10))))));
6649 }
6650 else
6651 n = has_patch(atoi((char *)name + 5));
6652 }
6653 else if (STRICMP(name, "vim_starting") == 0)
6654 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006655 else if (STRICMP(name, "ttyin") == 0)
6656 n = mch_input_isatty();
6657 else if (STRICMP(name, "ttyout") == 0)
6658 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659 else if (STRICMP(name, "multi_byte_encoding") == 0)
6660 n = has_mbyte;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006661#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6662 else if (STRICMP(name, "balloon_multiline") == 0)
6663 n = multiline_balloon_available();
6664#endif
6665#ifdef DYNAMIC_TCL
6666 else if (STRICMP(name, "tcl") == 0)
6667 n = tcl_enabled(FALSE);
6668#endif
6669#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6670 else if (STRICMP(name, "iconv") == 0)
6671 n = iconv_enabled(FALSE);
6672#endif
6673#ifdef DYNAMIC_LUA
6674 else if (STRICMP(name, "lua") == 0)
6675 n = lua_enabled(FALSE);
6676#endif
6677#ifdef DYNAMIC_MZSCHEME
6678 else if (STRICMP(name, "mzscheme") == 0)
6679 n = mzscheme_enabled(FALSE);
6680#endif
6681#ifdef DYNAMIC_RUBY
6682 else if (STRICMP(name, "ruby") == 0)
6683 n = ruby_enabled(FALSE);
6684#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685#ifdef DYNAMIC_PYTHON
6686 else if (STRICMP(name, "python") == 0)
6687 n = python_enabled(FALSE);
6688#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689#ifdef DYNAMIC_PYTHON3
6690 else if (STRICMP(name, "python3") == 0)
6691 n = python3_enabled(FALSE);
6692#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006693#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6694 else if (STRICMP(name, "pythonx") == 0)
6695 {
6696# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6697 if (p_pyx == 0)
6698 n = python3_enabled(FALSE) || python_enabled(FALSE);
6699 else if (p_pyx == 3)
6700 n = python3_enabled(FALSE);
6701 else if (p_pyx == 2)
6702 n = python_enabled(FALSE);
6703# elif defined(DYNAMIC_PYTHON)
6704 n = python_enabled(FALSE);
6705# elif defined(DYNAMIC_PYTHON3)
6706 n = python3_enabled(FALSE);
6707# endif
6708 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709#endif
6710#ifdef DYNAMIC_PERL
6711 else if (STRICMP(name, "perl") == 0)
6712 n = perl_enabled(FALSE);
6713#endif
6714#ifdef FEAT_GUI
6715 else if (STRICMP(name, "gui_running") == 0)
6716 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006717# ifdef FEAT_BROWSE
6718 else if (STRICMP(name, "browse") == 0)
6719 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6720# endif
6721#endif
6722#ifdef FEAT_SYN_HL
6723 else if (STRICMP(name, "syntax_items") == 0)
6724 n = syntax_present(curwin);
6725#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006726#ifdef FEAT_VTP
6727 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006728 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729#endif
6730#ifdef FEAT_NETBEANS_INTG
6731 else if (STRICMP(name, "netbeans_enabled") == 0)
6732 n = netbeans_active();
6733#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006734#if defined(FEAT_TERMINAL) && defined(WIN3264)
6735 else if (STRICMP(name, "terminal") == 0)
6736 n = terminal_enabled();
6737#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 }
6739
6740 rettv->vval.v_number = n;
6741}
6742
6743/*
6744 * "has_key()" function
6745 */
6746 static void
6747f_has_key(typval_T *argvars, typval_T *rettv)
6748{
6749 if (argvars[0].v_type != VAR_DICT)
6750 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006751 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752 return;
6753 }
6754 if (argvars[0].vval.v_dict == NULL)
6755 return;
6756
6757 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006758 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759}
6760
6761/*
6762 * "haslocaldir()" function
6763 */
6764 static void
6765f_haslocaldir(typval_T *argvars, typval_T *rettv)
6766{
6767 win_T *wp = NULL;
6768
6769 wp = find_tabwin(&argvars[0], &argvars[1]);
6770 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6771}
6772
6773/*
6774 * "hasmapto()" function
6775 */
6776 static void
6777f_hasmapto(typval_T *argvars, typval_T *rettv)
6778{
6779 char_u *name;
6780 char_u *mode;
6781 char_u buf[NUMBUFLEN];
6782 int abbr = FALSE;
6783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006784 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785 if (argvars[1].v_type == VAR_UNKNOWN)
6786 mode = (char_u *)"nvo";
6787 else
6788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006789 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006791 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 }
6793
6794 if (map_to_exists(name, mode, abbr))
6795 rettv->vval.v_number = TRUE;
6796 else
6797 rettv->vval.v_number = FALSE;
6798}
6799
6800/*
6801 * "histadd()" function
6802 */
6803 static void
6804f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6805{
6806#ifdef FEAT_CMDHIST
6807 int histype;
6808 char_u *str;
6809 char_u buf[NUMBUFLEN];
6810#endif
6811
6812 rettv->vval.v_number = FALSE;
6813 if (check_restricted() || check_secure())
6814 return;
6815#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006816 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817 histype = str != NULL ? get_histtype(str) : -1;
6818 if (histype >= 0)
6819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006820 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006821 if (*str != NUL)
6822 {
6823 init_history();
6824 add_to_history(histype, str, FALSE, NUL);
6825 rettv->vval.v_number = TRUE;
6826 return;
6827 }
6828 }
6829#endif
6830}
6831
6832/*
6833 * "histdel()" function
6834 */
6835 static void
6836f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6837{
6838#ifdef FEAT_CMDHIST
6839 int n;
6840 char_u buf[NUMBUFLEN];
6841 char_u *str;
6842
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006843 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844 if (str == NULL)
6845 n = 0;
6846 else if (argvars[1].v_type == VAR_UNKNOWN)
6847 /* only one argument: clear entire history */
6848 n = clr_history(get_histtype(str));
6849 else if (argvars[1].v_type == VAR_NUMBER)
6850 /* index given: remove that entry */
6851 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006852 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853 else
6854 /* string given: remove all matching entries */
6855 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006856 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 rettv->vval.v_number = n;
6858#endif
6859}
6860
6861/*
6862 * "histget()" function
6863 */
6864 static void
6865f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6866{
6867#ifdef FEAT_CMDHIST
6868 int type;
6869 int idx;
6870 char_u *str;
6871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 if (str == NULL)
6874 rettv->vval.v_string = NULL;
6875 else
6876 {
6877 type = get_histtype(str);
6878 if (argvars[1].v_type == VAR_UNKNOWN)
6879 idx = get_history_idx(type);
6880 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 /* -1 on type error */
6883 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6884 }
6885#else
6886 rettv->vval.v_string = NULL;
6887#endif
6888 rettv->v_type = VAR_STRING;
6889}
6890
6891/*
6892 * "histnr()" function
6893 */
6894 static void
6895f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6896{
6897 int i;
6898
6899#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901
6902 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6903 if (i >= HIST_CMD && i < HIST_COUNT)
6904 i = get_history_idx(i);
6905 else
6906#endif
6907 i = -1;
6908 rettv->vval.v_number = i;
6909}
6910
6911/*
6912 * "highlightID(name)" function
6913 */
6914 static void
6915f_hlID(typval_T *argvars, typval_T *rettv)
6916{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006917 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006918}
6919
6920/*
6921 * "highlight_exists()" function
6922 */
6923 static void
6924f_hlexists(typval_T *argvars, typval_T *rettv)
6925{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006926 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927}
6928
6929/*
6930 * "hostname()" function
6931 */
6932 static void
6933f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6934{
6935 char_u hostname[256];
6936
6937 mch_get_host_name(hostname, 256);
6938 rettv->v_type = VAR_STRING;
6939 rettv->vval.v_string = vim_strsave(hostname);
6940}
6941
6942/*
6943 * iconv() function
6944 */
6945 static void
6946f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6947{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 char_u buf1[NUMBUFLEN];
6949 char_u buf2[NUMBUFLEN];
6950 char_u *from, *to, *str;
6951 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006952
6953 rettv->v_type = VAR_STRING;
6954 rettv->vval.v_string = NULL;
6955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006956 str = tv_get_string(&argvars[0]);
6957 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6958 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959 vimconv.vc_type = CONV_NONE;
6960 convert_setup(&vimconv, from, to);
6961
6962 /* If the encodings are equal, no conversion needed. */
6963 if (vimconv.vc_type == CONV_NONE)
6964 rettv->vval.v_string = vim_strsave(str);
6965 else
6966 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6967
6968 convert_setup(&vimconv, NULL, NULL);
6969 vim_free(from);
6970 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006971}
6972
6973/*
6974 * "indent()" function
6975 */
6976 static void
6977f_indent(typval_T *argvars, typval_T *rettv)
6978{
6979 linenr_T lnum;
6980
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006981 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006982 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6983 rettv->vval.v_number = get_indent_lnum(lnum);
6984 else
6985 rettv->vval.v_number = -1;
6986}
6987
6988/*
6989 * "index()" function
6990 */
6991 static void
6992f_index(typval_T *argvars, typval_T *rettv)
6993{
6994 list_T *l;
6995 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006996 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006997 long idx = 0;
6998 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006999 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000
7001 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007002 if (argvars[0].v_type == VAR_BLOB)
7003 {
7004 typval_T tv;
7005 int start = 0;
7006
7007 if (argvars[2].v_type != VAR_UNKNOWN)
7008 {
7009 start = tv_get_number_chk(&argvars[2], &error);
7010 if (error)
7011 return;
7012 }
7013 b = argvars[0].vval.v_blob;
7014 if (b == NULL)
7015 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007016 if (start < 0)
7017 {
7018 start = blob_len(b) + start;
7019 if (start < 0)
7020 start = 0;
7021 }
7022
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007023 for (idx = start; idx < blob_len(b); ++idx)
7024 {
7025 tv.v_type = VAR_NUMBER;
7026 tv.vval.v_number = blob_get(b, idx);
7027 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7028 {
7029 rettv->vval.v_number = idx;
7030 return;
7031 }
7032 }
7033 return;
7034 }
7035 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007037 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007038 return;
7039 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007040
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 l = argvars[0].vval.v_list;
7042 if (l != NULL)
7043 {
7044 item = l->lv_first;
7045 if (argvars[2].v_type != VAR_UNKNOWN)
7046 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 /* Start at specified item. Use the cached index that list_find()
7048 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007049 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 idx = l->lv_idx;
7051 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007052 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 if (error)
7054 item = NULL;
7055 }
7056
7057 for ( ; item != NULL; item = item->li_next, ++idx)
7058 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7059 {
7060 rettv->vval.v_number = idx;
7061 break;
7062 }
7063 }
7064}
7065
7066static int inputsecret_flag = 0;
7067
7068/*
7069 * "input()" function
7070 * Also handles inputsecret() when inputsecret is set.
7071 */
7072 static void
7073f_input(typval_T *argvars, typval_T *rettv)
7074{
7075 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7076}
7077
7078/*
7079 * "inputdialog()" function
7080 */
7081 static void
7082f_inputdialog(typval_T *argvars, typval_T *rettv)
7083{
7084#if defined(FEAT_GUI_TEXTDIALOG)
7085 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7086 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7087 {
7088 char_u *message;
7089 char_u buf[NUMBUFLEN];
7090 char_u *defstr = (char_u *)"";
7091
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007092 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007094 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7096 else
7097 IObuff[0] = NUL;
7098 if (message != NULL && defstr != NULL
7099 && do_dialog(VIM_QUESTION, NULL, message,
7100 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7101 rettv->vval.v_string = vim_strsave(IObuff);
7102 else
7103 {
7104 if (message != NULL && defstr != NULL
7105 && argvars[1].v_type != VAR_UNKNOWN
7106 && argvars[2].v_type != VAR_UNKNOWN)
7107 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007108 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 else
7110 rettv->vval.v_string = NULL;
7111 }
7112 rettv->v_type = VAR_STRING;
7113 }
7114 else
7115#endif
7116 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7117}
7118
7119/*
7120 * "inputlist()" function
7121 */
7122 static void
7123f_inputlist(typval_T *argvars, typval_T *rettv)
7124{
7125 listitem_T *li;
7126 int selected;
7127 int mouse_used;
7128
7129#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007130 /* While starting up, there is no place to enter text. When running tests
7131 * with --not-a-term we assume feedkeys() will be used. */
7132 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 return;
7134#endif
7135 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007137 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 return;
7139 }
7140
7141 msg_start();
7142 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7143 lines_left = Rows; /* avoid more prompt */
7144 msg_scroll = TRUE;
7145 msg_clr_eos();
7146
7147 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7148 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007149 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 msg_putchar('\n');
7151 }
7152
7153 /* Ask for choice. */
7154 selected = prompt_for_number(&mouse_used);
7155 if (mouse_used)
7156 selected -= lines_left;
7157
7158 rettv->vval.v_number = selected;
7159}
7160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7162
7163/*
7164 * "inputrestore()" function
7165 */
7166 static void
7167f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7168{
7169 if (ga_userinput.ga_len > 0)
7170 {
7171 --ga_userinput.ga_len;
7172 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7173 + ga_userinput.ga_len);
7174 /* default return is zero == OK */
7175 }
7176 else if (p_verbose > 1)
7177 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007178 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007179 rettv->vval.v_number = 1; /* Failed */
7180 }
7181}
7182
7183/*
7184 * "inputsave()" function
7185 */
7186 static void
7187f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7188{
7189 /* Add an entry to the stack of typeahead storage. */
7190 if (ga_grow(&ga_userinput, 1) == OK)
7191 {
7192 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7193 + ga_userinput.ga_len);
7194 ++ga_userinput.ga_len;
7195 /* default return is zero == OK */
7196 }
7197 else
7198 rettv->vval.v_number = 1; /* Failed */
7199}
7200
7201/*
7202 * "inputsecret()" function
7203 */
7204 static void
7205f_inputsecret(typval_T *argvars, typval_T *rettv)
7206{
7207 ++cmdline_star;
7208 ++inputsecret_flag;
7209 f_input(argvars, rettv);
7210 --cmdline_star;
7211 --inputsecret_flag;
7212}
7213
7214/*
7215 * "insert()" function
7216 */
7217 static void
7218f_insert(typval_T *argvars, typval_T *rettv)
7219{
7220 long before = 0;
7221 listitem_T *item;
7222 list_T *l;
7223 int error = FALSE;
7224
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007225 if (argvars[0].v_type == VAR_BLOB)
7226 {
7227 int val, len;
7228 char_u *p;
7229
7230 len = blob_len(argvars[0].vval.v_blob);
7231 if (argvars[2].v_type != VAR_UNKNOWN)
7232 {
7233 before = (long)tv_get_number_chk(&argvars[2], &error);
7234 if (error)
7235 return; // type error; errmsg already given
7236 if (before < 0 || before > len)
7237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007238 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007239 return;
7240 }
7241 }
7242 val = tv_get_number_chk(&argvars[1], &error);
7243 if (error)
7244 return;
7245 if (val < 0 || val > 255)
7246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007247 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007248 return;
7249 }
7250
7251 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7252 return;
7253 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7254 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7255 *(p + before) = val;
7256 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7257
7258 copy_tv(&argvars[0], rettv);
7259 }
7260 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007261 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007262 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7263 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264 {
7265 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267 if (error)
7268 return; /* type error; errmsg already given */
7269
7270 if (before == l->lv_len)
7271 item = NULL;
7272 else
7273 {
7274 item = list_find(l, before);
7275 if (item == NULL)
7276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007277 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007278 l = NULL;
7279 }
7280 }
7281 if (l != NULL)
7282 {
7283 list_insert_tv(l, &argvars[1], item);
7284 copy_tv(&argvars[0], rettv);
7285 }
7286 }
7287}
7288
7289/*
7290 * "invert(expr)" function
7291 */
7292 static void
7293f_invert(typval_T *argvars, typval_T *rettv)
7294{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007295 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296}
7297
7298/*
7299 * "isdirectory()" function
7300 */
7301 static void
7302f_isdirectory(typval_T *argvars, typval_T *rettv)
7303{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007304 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305}
7306
7307/*
7308 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7309 * or it refers to a List or Dictionary that is locked.
7310 */
7311 static int
7312tv_islocked(typval_T *tv)
7313{
7314 return (tv->v_lock & VAR_LOCKED)
7315 || (tv->v_type == VAR_LIST
7316 && tv->vval.v_list != NULL
7317 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7318 || (tv->v_type == VAR_DICT
7319 && tv->vval.v_dict != NULL
7320 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7321}
7322
7323/*
7324 * "islocked()" function
7325 */
7326 static void
7327f_islocked(typval_T *argvars, typval_T *rettv)
7328{
7329 lval_T lv;
7330 char_u *end;
7331 dictitem_T *di;
7332
7333 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007334 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007335 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007336 if (end != NULL && lv.ll_name != NULL)
7337 {
7338 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007339 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 else
7341 {
7342 if (lv.ll_tv == NULL)
7343 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007344 di = find_var(lv.ll_name, NULL, TRUE);
7345 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007347 /* Consider a variable locked when:
7348 * 1. the variable itself is locked
7349 * 2. the value of the variable is locked.
7350 * 3. the List or Dict value is locked.
7351 */
7352 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7353 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007354 }
7355 }
7356 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007357 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007358 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007359 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 else if (lv.ll_list != NULL)
7361 /* List item. */
7362 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7363 else
7364 /* Dictionary item. */
7365 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7366 }
7367 }
7368
7369 clear_lval(&lv);
7370}
7371
7372#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7373/*
7374 * "isnan()" function
7375 */
7376 static void
7377f_isnan(typval_T *argvars, typval_T *rettv)
7378{
7379 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7380 && isnan(argvars[0].vval.v_float);
7381}
7382#endif
7383
7384/*
7385 * "items(dict)" function
7386 */
7387 static void
7388f_items(typval_T *argvars, typval_T *rettv)
7389{
7390 dict_list(argvars, rettv, 2);
7391}
7392
7393#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7394/*
7395 * Get the job from the argument.
7396 * Returns NULL if the job is invalid.
7397 */
7398 static job_T *
7399get_job_arg(typval_T *tv)
7400{
7401 job_T *job;
7402
7403 if (tv->v_type != VAR_JOB)
7404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007405 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406 return NULL;
7407 }
7408 job = tv->vval.v_job;
7409
7410 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007411 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007412 return job;
7413}
7414
7415/*
7416 * "job_getchannel()" function
7417 */
7418 static void
7419f_job_getchannel(typval_T *argvars, typval_T *rettv)
7420{
7421 job_T *job = get_job_arg(&argvars[0]);
7422
7423 if (job != NULL)
7424 {
7425 rettv->v_type = VAR_CHANNEL;
7426 rettv->vval.v_channel = job->jv_channel;
7427 if (job->jv_channel != NULL)
7428 ++job->jv_channel->ch_refcount;
7429 }
7430}
7431
7432/*
7433 * "job_info()" function
7434 */
7435 static void
7436f_job_info(typval_T *argvars, typval_T *rettv)
7437{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007438 if (argvars[0].v_type != VAR_UNKNOWN)
7439 {
7440 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007442 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7443 job_info(job, rettv->vval.v_dict);
7444 }
7445 else if (rettv_list_alloc(rettv) == OK)
7446 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007447}
7448
7449/*
7450 * "job_setoptions()" function
7451 */
7452 static void
7453f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7454{
7455 job_T *job = get_job_arg(&argvars[0]);
7456 jobopt_T opt;
7457
7458 if (job == NULL)
7459 return;
7460 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007461 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007462 job_set_options(job, &opt);
7463 free_job_options(&opt);
7464}
7465
7466/*
7467 * "job_start()" function
7468 */
7469 static void
7470f_job_start(typval_T *argvars, typval_T *rettv)
7471{
7472 rettv->v_type = VAR_JOB;
7473 if (check_restricted() || check_secure())
7474 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007475 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007476}
7477
7478/*
7479 * "job_status()" function
7480 */
7481 static void
7482f_job_status(typval_T *argvars, typval_T *rettv)
7483{
7484 job_T *job = get_job_arg(&argvars[0]);
7485
7486 if (job != NULL)
7487 {
7488 rettv->v_type = VAR_STRING;
7489 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7490 }
7491}
7492
7493/*
7494 * "job_stop()" function
7495 */
7496 static void
7497f_job_stop(typval_T *argvars, typval_T *rettv)
7498{
7499 job_T *job = get_job_arg(&argvars[0]);
7500
7501 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007502 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007503}
7504#endif
7505
7506/*
7507 * "join()" function
7508 */
7509 static void
7510f_join(typval_T *argvars, typval_T *rettv)
7511{
7512 garray_T ga;
7513 char_u *sep;
7514
7515 if (argvars[0].v_type != VAR_LIST)
7516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007517 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518 return;
7519 }
7520 if (argvars[0].vval.v_list == NULL)
7521 return;
7522 if (argvars[1].v_type == VAR_UNKNOWN)
7523 sep = (char_u *)" ";
7524 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007525 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007526
7527 rettv->v_type = VAR_STRING;
7528
7529 if (sep != NULL)
7530 {
7531 ga_init2(&ga, (int)sizeof(char), 80);
7532 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7533 ga_append(&ga, NUL);
7534 rettv->vval.v_string = (char_u *)ga.ga_data;
7535 }
7536 else
7537 rettv->vval.v_string = NULL;
7538}
7539
7540/*
7541 * "js_decode()" function
7542 */
7543 static void
7544f_js_decode(typval_T *argvars, typval_T *rettv)
7545{
7546 js_read_T reader;
7547
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007548 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007549 reader.js_fill = NULL;
7550 reader.js_used = 0;
7551 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007552 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553}
7554
7555/*
7556 * "js_encode()" function
7557 */
7558 static void
7559f_js_encode(typval_T *argvars, typval_T *rettv)
7560{
7561 rettv->v_type = VAR_STRING;
7562 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7563}
7564
7565/*
7566 * "json_decode()" function
7567 */
7568 static void
7569f_json_decode(typval_T *argvars, typval_T *rettv)
7570{
7571 js_read_T reader;
7572
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007573 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007574 reader.js_fill = NULL;
7575 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007576 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007577}
7578
7579/*
7580 * "json_encode()" function
7581 */
7582 static void
7583f_json_encode(typval_T *argvars, typval_T *rettv)
7584{
7585 rettv->v_type = VAR_STRING;
7586 rettv->vval.v_string = json_encode(&argvars[0], 0);
7587}
7588
7589/*
7590 * "keys()" function
7591 */
7592 static void
7593f_keys(typval_T *argvars, typval_T *rettv)
7594{
7595 dict_list(argvars, rettv, 0);
7596}
7597
7598/*
7599 * "last_buffer_nr()" function.
7600 */
7601 static void
7602f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7603{
7604 int n = 0;
7605 buf_T *buf;
7606
Bram Moolenaar29323592016-07-24 22:04:11 +02007607 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 if (n < buf->b_fnum)
7609 n = buf->b_fnum;
7610
7611 rettv->vval.v_number = n;
7612}
7613
7614/*
7615 * "len()" function
7616 */
7617 static void
7618f_len(typval_T *argvars, typval_T *rettv)
7619{
7620 switch (argvars[0].v_type)
7621 {
7622 case VAR_STRING:
7623 case VAR_NUMBER:
7624 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007625 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007626 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007627 case VAR_BLOB:
7628 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7629 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 case VAR_LIST:
7631 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7632 break;
7633 case VAR_DICT:
7634 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7635 break;
7636 case VAR_UNKNOWN:
7637 case VAR_SPECIAL:
7638 case VAR_FLOAT:
7639 case VAR_FUNC:
7640 case VAR_PARTIAL:
7641 case VAR_JOB:
7642 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007643 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007644 break;
7645 }
7646}
7647
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007649libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650{
7651#ifdef FEAT_LIBCALL
7652 char_u *string_in;
7653 char_u **string_result;
7654 int nr_result;
7655#endif
7656
7657 rettv->v_type = type;
7658 if (type != VAR_NUMBER)
7659 rettv->vval.v_string = NULL;
7660
7661 if (check_restricted() || check_secure())
7662 return;
7663
7664#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007665 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007666 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7667 {
7668 string_in = NULL;
7669 if (argvars[2].v_type == VAR_STRING)
7670 string_in = argvars[2].vval.v_string;
7671 if (type == VAR_NUMBER)
7672 string_result = NULL;
7673 else
7674 string_result = &rettv->vval.v_string;
7675 if (mch_libcall(argvars[0].vval.v_string,
7676 argvars[1].vval.v_string,
7677 string_in,
7678 argvars[2].vval.v_number,
7679 string_result,
7680 &nr_result) == OK
7681 && type == VAR_NUMBER)
7682 rettv->vval.v_number = nr_result;
7683 }
7684#endif
7685}
7686
7687/*
7688 * "libcall()" function
7689 */
7690 static void
7691f_libcall(typval_T *argvars, typval_T *rettv)
7692{
7693 libcall_common(argvars, rettv, VAR_STRING);
7694}
7695
7696/*
7697 * "libcallnr()" function
7698 */
7699 static void
7700f_libcallnr(typval_T *argvars, typval_T *rettv)
7701{
7702 libcall_common(argvars, rettv, VAR_NUMBER);
7703}
7704
7705/*
7706 * "line(string)" function
7707 */
7708 static void
7709f_line(typval_T *argvars, typval_T *rettv)
7710{
7711 linenr_T lnum = 0;
7712 pos_T *fp;
7713 int fnum;
7714
7715 fp = var2fpos(&argvars[0], TRUE, &fnum);
7716 if (fp != NULL)
7717 lnum = fp->lnum;
7718 rettv->vval.v_number = lnum;
7719}
7720
7721/*
7722 * "line2byte(lnum)" function
7723 */
7724 static void
7725f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7726{
7727#ifndef FEAT_BYTEOFF
7728 rettv->vval.v_number = -1;
7729#else
7730 linenr_T lnum;
7731
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007732 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007733 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7734 rettv->vval.v_number = -1;
7735 else
7736 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7737 if (rettv->vval.v_number >= 0)
7738 ++rettv->vval.v_number;
7739#endif
7740}
7741
7742/*
7743 * "lispindent(lnum)" function
7744 */
7745 static void
7746f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7747{
7748#ifdef FEAT_LISP
7749 pos_T pos;
7750 linenr_T lnum;
7751
7752 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007753 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007754 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7755 {
7756 curwin->w_cursor.lnum = lnum;
7757 rettv->vval.v_number = get_lisp_indent();
7758 curwin->w_cursor = pos;
7759 }
7760 else
7761#endif
7762 rettv->vval.v_number = -1;
7763}
7764
7765/*
7766 * "localtime()" function
7767 */
7768 static void
7769f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7770{
7771 rettv->vval.v_number = (varnumber_T)time(NULL);
7772}
7773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007774 static void
7775get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7776{
7777 char_u *keys;
7778 char_u *which;
7779 char_u buf[NUMBUFLEN];
7780 char_u *keys_buf = NULL;
7781 char_u *rhs;
7782 int mode;
7783 int abbr = FALSE;
7784 int get_dict = FALSE;
7785 mapblock_T *mp;
7786 int buffer_local;
7787
7788 /* return empty string for failure */
7789 rettv->v_type = VAR_STRING;
7790 rettv->vval.v_string = NULL;
7791
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007792 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793 if (*keys == NUL)
7794 return;
7795
7796 if (argvars[1].v_type != VAR_UNKNOWN)
7797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007798 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799 if (argvars[2].v_type != VAR_UNKNOWN)
7800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007801 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007803 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804 }
7805 }
7806 else
7807 which = (char_u *)"";
7808 if (which == NULL)
7809 return;
7810
7811 mode = get_map_mode(&which, 0);
7812
7813 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7814 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7815 vim_free(keys_buf);
7816
7817 if (!get_dict)
7818 {
7819 /* Return a string. */
7820 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007821 {
7822 if (*rhs == NUL)
7823 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7824 else
7825 rettv->vval.v_string = str2special_save(rhs, FALSE);
7826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827
7828 }
7829 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7830 {
7831 /* Return a dictionary. */
7832 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7833 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7834 dict_T *dict = rettv->vval.v_dict;
7835
Bram Moolenaare0be1672018-07-08 16:50:37 +02007836 dict_add_string(dict, "lhs", lhs);
7837 dict_add_string(dict, "rhs", mp->m_orig_str);
7838 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7839 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7840 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007841 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7842 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007843 dict_add_number(dict, "buffer", (long)buffer_local);
7844 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7845 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007846
7847 vim_free(lhs);
7848 vim_free(mapmode);
7849 }
7850}
7851
7852#ifdef FEAT_FLOAT
7853/*
7854 * "log()" function
7855 */
7856 static void
7857f_log(typval_T *argvars, typval_T *rettv)
7858{
7859 float_T f = 0.0;
7860
7861 rettv->v_type = VAR_FLOAT;
7862 if (get_float_arg(argvars, &f) == OK)
7863 rettv->vval.v_float = log(f);
7864 else
7865 rettv->vval.v_float = 0.0;
7866}
7867
7868/*
7869 * "log10()" function
7870 */
7871 static void
7872f_log10(typval_T *argvars, typval_T *rettv)
7873{
7874 float_T f = 0.0;
7875
7876 rettv->v_type = VAR_FLOAT;
7877 if (get_float_arg(argvars, &f) == OK)
7878 rettv->vval.v_float = log10(f);
7879 else
7880 rettv->vval.v_float = 0.0;
7881}
7882#endif
7883
7884#ifdef FEAT_LUA
7885/*
7886 * "luaeval()" function
7887 */
7888 static void
7889f_luaeval(typval_T *argvars, typval_T *rettv)
7890{
7891 char_u *str;
7892 char_u buf[NUMBUFLEN];
7893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007894 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007895 do_luaeval(str, argvars + 1, rettv);
7896}
7897#endif
7898
7899/*
7900 * "map()" function
7901 */
7902 static void
7903f_map(typval_T *argvars, typval_T *rettv)
7904{
7905 filter_map(argvars, rettv, TRUE);
7906}
7907
7908/*
7909 * "maparg()" function
7910 */
7911 static void
7912f_maparg(typval_T *argvars, typval_T *rettv)
7913{
7914 get_maparg(argvars, rettv, TRUE);
7915}
7916
7917/*
7918 * "mapcheck()" function
7919 */
7920 static void
7921f_mapcheck(typval_T *argvars, typval_T *rettv)
7922{
7923 get_maparg(argvars, rettv, FALSE);
7924}
7925
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007926typedef enum
7927{
7928 MATCH_END, /* matchend() */
7929 MATCH_MATCH, /* match() */
7930 MATCH_STR, /* matchstr() */
7931 MATCH_LIST, /* matchlist() */
7932 MATCH_POS /* matchstrpos() */
7933} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934
7935 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007936find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007937{
7938 char_u *str = NULL;
7939 long len = 0;
7940 char_u *expr = NULL;
7941 char_u *pat;
7942 regmatch_T regmatch;
7943 char_u patbuf[NUMBUFLEN];
7944 char_u strbuf[NUMBUFLEN];
7945 char_u *save_cpo;
7946 long start = 0;
7947 long nth = 1;
7948 colnr_T startcol = 0;
7949 int match = 0;
7950 list_T *l = NULL;
7951 listitem_T *li = NULL;
7952 long idx = 0;
7953 char_u *tofree = NULL;
7954
7955 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7956 save_cpo = p_cpo;
7957 p_cpo = (char_u *)"";
7958
7959 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007960 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007962 /* type MATCH_LIST: return empty list when there are no matches.
7963 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007964 if (rettv_list_alloc(rettv) == FAIL)
7965 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007966 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007967 && (list_append_string(rettv->vval.v_list,
7968 (char_u *)"", 0) == 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 || list_append_number(rettv->vval.v_list,
7974 (varnumber_T)-1) == FAIL))
7975 {
7976 list_free(rettv->vval.v_list);
7977 rettv->vval.v_list = NULL;
7978 goto theend;
7979 }
7980 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007981 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 {
7983 rettv->v_type = VAR_STRING;
7984 rettv->vval.v_string = NULL;
7985 }
7986
7987 if (argvars[0].v_type == VAR_LIST)
7988 {
7989 if ((l = argvars[0].vval.v_list) == NULL)
7990 goto theend;
7991 li = l->lv_first;
7992 }
7993 else
7994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007995 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 len = (long)STRLEN(str);
7997 }
7998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007999 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 if (pat == NULL)
8001 goto theend;
8002
8003 if (argvars[2].v_type != VAR_UNKNOWN)
8004 {
8005 int error = FALSE;
8006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008007 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008008 if (error)
8009 goto theend;
8010 if (l != NULL)
8011 {
8012 li = list_find(l, start);
8013 if (li == NULL)
8014 goto theend;
8015 idx = l->lv_idx; /* use the cached index */
8016 }
8017 else
8018 {
8019 if (start < 0)
8020 start = 0;
8021 if (start > len)
8022 goto theend;
8023 /* When "count" argument is there ignore matches before "start",
8024 * otherwise skip part of the string. Differs when pattern is "^"
8025 * or "\<". */
8026 if (argvars[3].v_type != VAR_UNKNOWN)
8027 startcol = start;
8028 else
8029 {
8030 str += start;
8031 len -= start;
8032 }
8033 }
8034
8035 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008036 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 if (error)
8038 goto theend;
8039 }
8040
8041 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8042 if (regmatch.regprog != NULL)
8043 {
8044 regmatch.rm_ic = p_ic;
8045
8046 for (;;)
8047 {
8048 if (l != NULL)
8049 {
8050 if (li == NULL)
8051 {
8052 match = FALSE;
8053 break;
8054 }
8055 vim_free(tofree);
8056 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8057 if (str == NULL)
8058 break;
8059 }
8060
8061 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8062
8063 if (match && --nth <= 0)
8064 break;
8065 if (l == NULL && !match)
8066 break;
8067
8068 /* Advance to just after the match. */
8069 if (l != NULL)
8070 {
8071 li = li->li_next;
8072 ++idx;
8073 }
8074 else
8075 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008076 startcol = (colnr_T)(regmatch.startp[0]
8077 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078 if (startcol > (colnr_T)len
8079 || str + startcol <= regmatch.startp[0])
8080 {
8081 match = FALSE;
8082 break;
8083 }
8084 }
8085 }
8086
8087 if (match)
8088 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008089 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 {
8091 listitem_T *li1 = rettv->vval.v_list->lv_first;
8092 listitem_T *li2 = li1->li_next;
8093 listitem_T *li3 = li2->li_next;
8094 listitem_T *li4 = li3->li_next;
8095
8096 vim_free(li1->li_tv.vval.v_string);
8097 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8098 (int)(regmatch.endp[0] - regmatch.startp[0]));
8099 li3->li_tv.vval.v_number =
8100 (varnumber_T)(regmatch.startp[0] - expr);
8101 li4->li_tv.vval.v_number =
8102 (varnumber_T)(regmatch.endp[0] - expr);
8103 if (l != NULL)
8104 li2->li_tv.vval.v_number = (varnumber_T)idx;
8105 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008106 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008107 {
8108 int i;
8109
8110 /* return list with matched string and submatches */
8111 for (i = 0; i < NSUBEXP; ++i)
8112 {
8113 if (regmatch.endp[i] == NULL)
8114 {
8115 if (list_append_string(rettv->vval.v_list,
8116 (char_u *)"", 0) == FAIL)
8117 break;
8118 }
8119 else if (list_append_string(rettv->vval.v_list,
8120 regmatch.startp[i],
8121 (int)(regmatch.endp[i] - regmatch.startp[i]))
8122 == FAIL)
8123 break;
8124 }
8125 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008126 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 {
8128 /* return matched string */
8129 if (l != NULL)
8130 copy_tv(&li->li_tv, rettv);
8131 else
8132 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8133 (int)(regmatch.endp[0] - regmatch.startp[0]));
8134 }
8135 else if (l != NULL)
8136 rettv->vval.v_number = idx;
8137 else
8138 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008139 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008140 rettv->vval.v_number =
8141 (varnumber_T)(regmatch.startp[0] - str);
8142 else
8143 rettv->vval.v_number =
8144 (varnumber_T)(regmatch.endp[0] - str);
8145 rettv->vval.v_number += (varnumber_T)(str - expr);
8146 }
8147 }
8148 vim_regfree(regmatch.regprog);
8149 }
8150
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008151theend:
8152 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008153 /* matchstrpos() without a list: drop the second item. */
8154 listitem_remove(rettv->vval.v_list,
8155 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008156 vim_free(tofree);
8157 p_cpo = save_cpo;
8158}
8159
8160/*
8161 * "match()" function
8162 */
8163 static void
8164f_match(typval_T *argvars, typval_T *rettv)
8165{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008166 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008167}
8168
Bram Moolenaar95e51472018-07-28 16:55:56 +02008169#ifdef FEAT_SEARCH_EXTRA
8170 static int
8171matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8172{
8173 dictitem_T *di;
8174
8175 if (tv->v_type != VAR_DICT)
8176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008177 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008178 return FAIL;
8179 }
8180
8181 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008182 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008183 (char_u *)"conceal", FALSE);
8184
8185 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8186 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008187 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008188 if (*win == NULL)
8189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008190 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008191 return FAIL;
8192 }
8193 }
8194
8195 return OK;
8196}
8197#endif
8198
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199/*
8200 * "matchadd()" function
8201 */
8202 static void
8203f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8204{
8205#ifdef FEAT_SEARCH_EXTRA
8206 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008207 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8208 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 int prio = 10; /* default priority */
8210 int id = -1;
8211 int error = FALSE;
8212 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008213 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214
8215 rettv->vval.v_number = -1;
8216
8217 if (grp == NULL || pat == NULL)
8218 return;
8219 if (argvars[2].v_type != VAR_UNKNOWN)
8220 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008221 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008222 if (argvars[3].v_type != VAR_UNKNOWN)
8223 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008224 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008225 if (argvars[4].v_type != VAR_UNKNOWN
8226 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8227 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008228 }
8229 }
8230 if (error == TRUE)
8231 return;
8232 if (id >= 1 && id <= 3)
8233 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008234 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008235 return;
8236 }
8237
Bram Moolenaar95e51472018-07-28 16:55:56 +02008238 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239 conceal_char);
8240#endif
8241}
8242
8243/*
8244 * "matchaddpos()" function
8245 */
8246 static void
8247f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8248{
8249#ifdef FEAT_SEARCH_EXTRA
8250 char_u buf[NUMBUFLEN];
8251 char_u *group;
8252 int prio = 10;
8253 int id = -1;
8254 int error = FALSE;
8255 list_T *l;
8256 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008257 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258
8259 rettv->vval.v_number = -1;
8260
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008261 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262 if (group == NULL)
8263 return;
8264
8265 if (argvars[1].v_type != VAR_LIST)
8266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008267 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008268 return;
8269 }
8270 l = argvars[1].vval.v_list;
8271 if (l == NULL)
8272 return;
8273
8274 if (argvars[2].v_type != VAR_UNKNOWN)
8275 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008276 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 if (argvars[3].v_type != VAR_UNKNOWN)
8278 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008279 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008280
8281 if (argvars[4].v_type != VAR_UNKNOWN
8282 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8283 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008284 }
8285 }
8286 if (error == TRUE)
8287 return;
8288
8289 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8290 if (id == 1 || id == 2)
8291 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008292 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 return;
8294 }
8295
Bram Moolenaar95e51472018-07-28 16:55:56 +02008296 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 conceal_char);
8298#endif
8299}
8300
8301/*
8302 * "matcharg()" function
8303 */
8304 static void
8305f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8306{
8307 if (rettv_list_alloc(rettv) == OK)
8308 {
8309#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008310 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 matchitem_T *m;
8312
8313 if (id >= 1 && id <= 3)
8314 {
8315 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8316 {
8317 list_append_string(rettv->vval.v_list,
8318 syn_id2name(m->hlg_id), -1);
8319 list_append_string(rettv->vval.v_list, m->pattern, -1);
8320 }
8321 else
8322 {
8323 list_append_string(rettv->vval.v_list, NULL, -1);
8324 list_append_string(rettv->vval.v_list, NULL, -1);
8325 }
8326 }
8327#endif
8328 }
8329}
8330
8331/*
8332 * "matchdelete()" function
8333 */
8334 static void
8335f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8336{
8337#ifdef FEAT_SEARCH_EXTRA
8338 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008339 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340#endif
8341}
8342
8343/*
8344 * "matchend()" function
8345 */
8346 static void
8347f_matchend(typval_T *argvars, typval_T *rettv)
8348{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008349 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350}
8351
8352/*
8353 * "matchlist()" function
8354 */
8355 static void
8356f_matchlist(typval_T *argvars, typval_T *rettv)
8357{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008358 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359}
8360
8361/*
8362 * "matchstr()" function
8363 */
8364 static void
8365f_matchstr(typval_T *argvars, typval_T *rettv)
8366{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008367 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368}
8369
8370/*
8371 * "matchstrpos()" function
8372 */
8373 static void
8374f_matchstrpos(typval_T *argvars, typval_T *rettv)
8375{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008376 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377}
8378
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 static void
8380max_min(typval_T *argvars, typval_T *rettv, int domax)
8381{
8382 varnumber_T n = 0;
8383 varnumber_T i;
8384 int error = FALSE;
8385
8386 if (argvars[0].v_type == VAR_LIST)
8387 {
8388 list_T *l;
8389 listitem_T *li;
8390
8391 l = argvars[0].vval.v_list;
8392 if (l != NULL)
8393 {
8394 li = l->lv_first;
8395 if (li != NULL)
8396 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 for (;;)
8399 {
8400 li = li->li_next;
8401 if (li == NULL)
8402 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008403 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 if (domax ? i > n : i < n)
8405 n = i;
8406 }
8407 }
8408 }
8409 }
8410 else if (argvars[0].v_type == VAR_DICT)
8411 {
8412 dict_T *d;
8413 int first = TRUE;
8414 hashitem_T *hi;
8415 int todo;
8416
8417 d = argvars[0].vval.v_dict;
8418 if (d != NULL)
8419 {
8420 todo = (int)d->dv_hashtab.ht_used;
8421 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8422 {
8423 if (!HASHITEM_EMPTY(hi))
8424 {
8425 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008426 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008427 if (first)
8428 {
8429 n = i;
8430 first = FALSE;
8431 }
8432 else if (domax ? i > n : i < n)
8433 n = i;
8434 }
8435 }
8436 }
8437 }
8438 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008439 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 rettv->vval.v_number = error ? 0 : n;
8441}
8442
8443/*
8444 * "max()" function
8445 */
8446 static void
8447f_max(typval_T *argvars, typval_T *rettv)
8448{
8449 max_min(argvars, rettv, TRUE);
8450}
8451
8452/*
8453 * "min()" function
8454 */
8455 static void
8456f_min(typval_T *argvars, typval_T *rettv)
8457{
8458 max_min(argvars, rettv, FALSE);
8459}
8460
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008461/*
8462 * Create the directory in which "dir" is located, and higher levels when
8463 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008464 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 */
8466 static int
8467mkdir_recurse(char_u *dir, int prot)
8468{
8469 char_u *p;
8470 char_u *updir;
8471 int r = FAIL;
8472
8473 /* Get end of directory name in "dir".
8474 * We're done when it's "/" or "c:/". */
8475 p = gettail_sep(dir);
8476 if (p <= get_past_head(dir))
8477 return OK;
8478
8479 /* If the directory exists we're done. Otherwise: create it.*/
8480 updir = vim_strnsave(dir, (int)(p - dir));
8481 if (updir == NULL)
8482 return FAIL;
8483 if (mch_isdir(updir))
8484 r = OK;
8485 else if (mkdir_recurse(updir, prot) == OK)
8486 r = vim_mkdir_emsg(updir, prot);
8487 vim_free(updir);
8488 return r;
8489}
8490
8491#ifdef vim_mkdir
8492/*
8493 * "mkdir()" function
8494 */
8495 static void
8496f_mkdir(typval_T *argvars, typval_T *rettv)
8497{
8498 char_u *dir;
8499 char_u buf[NUMBUFLEN];
8500 int prot = 0755;
8501
8502 rettv->vval.v_number = FAIL;
8503 if (check_restricted() || check_secure())
8504 return;
8505
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008506 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008507 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008508 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008510 if (*gettail(dir) == NUL)
8511 /* remove trailing slashes */
8512 *gettail_sep(dir) = NUL;
8513
8514 if (argvars[1].v_type != VAR_UNKNOWN)
8515 {
8516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008518 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008519 if (prot == -1)
8520 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008521 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008522 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008523 {
8524 if (mch_isdir(dir))
8525 {
8526 /* With the "p" flag it's OK if the dir already exists. */
8527 rettv->vval.v_number = OK;
8528 return;
8529 }
8530 mkdir_recurse(dir, prot);
8531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008532 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008533 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534}
8535#endif
8536
8537/*
8538 * "mode()" function
8539 */
8540 static void
8541f_mode(typval_T *argvars, typval_T *rettv)
8542{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008543 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008544
Bram Moolenaar612cc382018-07-29 15:34:26 +02008545 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008546
8547 if (time_for_testing == 93784)
8548 {
8549 /* Testing the two-character code. */
8550 buf[0] = 'x';
8551 buf[1] = '!';
8552 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008553#ifdef FEAT_TERMINAL
8554 else if (term_use_loop())
8555 buf[0] = 't';
8556#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008557 else if (VIsual_active)
8558 {
8559 if (VIsual_select)
8560 buf[0] = VIsual_mode + 's' - 'v';
8561 else
8562 buf[0] = VIsual_mode;
8563 }
8564 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8565 || State == CONFIRM)
8566 {
8567 buf[0] = 'r';
8568 if (State == ASKMORE)
8569 buf[1] = 'm';
8570 else if (State == CONFIRM)
8571 buf[1] = '?';
8572 }
8573 else if (State == EXTERNCMD)
8574 buf[0] = '!';
8575 else if (State & INSERT)
8576 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577 if (State & VREPLACE_FLAG)
8578 {
8579 buf[0] = 'R';
8580 buf[1] = 'v';
8581 }
8582 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008583 {
8584 if (State & REPLACE_FLAG)
8585 buf[0] = 'R';
8586 else
8587 buf[0] = 'i';
8588#ifdef FEAT_INS_EXPAND
8589 if (ins_compl_active())
8590 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008591 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008592 buf[1] = 'x';
8593#endif
8594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008595 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008596 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 {
8598 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008599 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008601 else if (exmode_active == EXMODE_NORMAL)
8602 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 }
8604 else
8605 {
8606 buf[0] = 'n';
8607 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008608 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008610 // to be able to detect force-linewise/blockwise/characterwise operations
8611 buf[2] = motion_force;
8612 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008613 else if (restart_edit == 'I' || restart_edit == 'R'
8614 || restart_edit == 'V')
8615 {
8616 buf[1] = 'i';
8617 buf[2] = restart_edit;
8618 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 }
8620
8621 /* Clear out the minor mode when the argument is not a non-zero number or
8622 * non-empty string. */
8623 if (!non_zero_arg(&argvars[0]))
8624 buf[1] = NUL;
8625
8626 rettv->vval.v_string = vim_strsave(buf);
8627 rettv->v_type = VAR_STRING;
8628}
8629
8630#if defined(FEAT_MZSCHEME) || defined(PROTO)
8631/*
8632 * "mzeval()" function
8633 */
8634 static void
8635f_mzeval(typval_T *argvars, typval_T *rettv)
8636{
8637 char_u *str;
8638 char_u buf[NUMBUFLEN];
8639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 do_mzeval(str, rettv);
8642}
8643
8644 void
8645mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8646{
8647 typval_T argvars[3];
8648
8649 argvars[0].v_type = VAR_STRING;
8650 argvars[0].vval.v_string = name;
8651 copy_tv(args, &argvars[1]);
8652 argvars[2].v_type = VAR_UNKNOWN;
8653 f_call(argvars, rettv);
8654 clear_tv(&argvars[1]);
8655}
8656#endif
8657
8658/*
8659 * "nextnonblank()" function
8660 */
8661 static void
8662f_nextnonblank(typval_T *argvars, typval_T *rettv)
8663{
8664 linenr_T lnum;
8665
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008666 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667 {
8668 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8669 {
8670 lnum = 0;
8671 break;
8672 }
8673 if (*skipwhite(ml_get(lnum)) != NUL)
8674 break;
8675 }
8676 rettv->vval.v_number = lnum;
8677}
8678
8679/*
8680 * "nr2char()" function
8681 */
8682 static void
8683f_nr2char(typval_T *argvars, typval_T *rettv)
8684{
8685 char_u buf[NUMBUFLEN];
8686
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 if (has_mbyte)
8688 {
8689 int utf8 = 0;
8690
8691 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008692 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008694 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008696 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 }
8698 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008700 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701 buf[1] = NUL;
8702 }
8703 rettv->v_type = VAR_STRING;
8704 rettv->vval.v_string = vim_strsave(buf);
8705}
8706
8707/*
8708 * "or(expr, expr)" function
8709 */
8710 static void
8711f_or(typval_T *argvars, typval_T *rettv)
8712{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008713 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8714 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715}
8716
8717/*
8718 * "pathshorten()" function
8719 */
8720 static void
8721f_pathshorten(typval_T *argvars, typval_T *rettv)
8722{
8723 char_u *p;
8724
8725 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008726 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008727 if (p == NULL)
8728 rettv->vval.v_string = NULL;
8729 else
8730 {
8731 p = vim_strsave(p);
8732 rettv->vval.v_string = p;
8733 if (p != NULL)
8734 shorten_dir(p);
8735 }
8736}
8737
8738#ifdef FEAT_PERL
8739/*
8740 * "perleval()" function
8741 */
8742 static void
8743f_perleval(typval_T *argvars, typval_T *rettv)
8744{
8745 char_u *str;
8746 char_u buf[NUMBUFLEN];
8747
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008748 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008749 do_perleval(str, rettv);
8750}
8751#endif
8752
8753#ifdef FEAT_FLOAT
8754/*
8755 * "pow()" function
8756 */
8757 static void
8758f_pow(typval_T *argvars, typval_T *rettv)
8759{
8760 float_T fx = 0.0, fy = 0.0;
8761
8762 rettv->v_type = VAR_FLOAT;
8763 if (get_float_arg(argvars, &fx) == OK
8764 && get_float_arg(&argvars[1], &fy) == OK)
8765 rettv->vval.v_float = pow(fx, fy);
8766 else
8767 rettv->vval.v_float = 0.0;
8768}
8769#endif
8770
8771/*
8772 * "prevnonblank()" function
8773 */
8774 static void
8775f_prevnonblank(typval_T *argvars, typval_T *rettv)
8776{
8777 linenr_T lnum;
8778
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008779 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008780 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8781 lnum = 0;
8782 else
8783 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8784 --lnum;
8785 rettv->vval.v_number = lnum;
8786}
8787
8788/* This dummy va_list is here because:
8789 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8790 * - locally in the function results in a "used before set" warning
8791 * - using va_start() to initialize it gives "function with fixed args" error */
8792static va_list ap;
8793
8794/*
8795 * "printf()" function
8796 */
8797 static void
8798f_printf(typval_T *argvars, typval_T *rettv)
8799{
8800 char_u buf[NUMBUFLEN];
8801 int len;
8802 char_u *s;
8803 int saved_did_emsg = did_emsg;
8804 char *fmt;
8805
8806 rettv->v_type = VAR_STRING;
8807 rettv->vval.v_string = NULL;
8808
8809 /* Get the required length, allocate the buffer and do it for real. */
8810 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008811 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008812 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813 if (!did_emsg)
8814 {
8815 s = alloc(len + 1);
8816 if (s != NULL)
8817 {
8818 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008819 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8820 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008821 }
8822 }
8823 did_emsg |= saved_did_emsg;
8824}
8825
Bram Moolenaarf2732452018-06-03 14:47:35 +02008826#ifdef FEAT_JOB_CHANNEL
8827/*
8828 * "prompt_setcallback({buffer}, {callback})" function
8829 */
8830 static void
8831f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8832{
8833 buf_T *buf;
8834 char_u *callback;
8835 partial_T *partial;
8836
8837 if (check_secure())
8838 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008839 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008840 if (buf == NULL)
8841 return;
8842
8843 callback = get_callback(&argvars[1], &partial);
8844 if (callback == NULL)
8845 return;
8846
8847 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8848 if (partial == NULL)
8849 buf->b_prompt_callback = vim_strsave(callback);
8850 else
8851 /* pointer into the partial */
8852 buf->b_prompt_callback = callback;
8853 buf->b_prompt_partial = partial;
8854}
8855
8856/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008857 * "prompt_setinterrupt({buffer}, {callback})" function
8858 */
8859 static void
8860f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8861{
8862 buf_T *buf;
8863 char_u *callback;
8864 partial_T *partial;
8865
8866 if (check_secure())
8867 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008868 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008869 if (buf == NULL)
8870 return;
8871
8872 callback = get_callback(&argvars[1], &partial);
8873 if (callback == NULL)
8874 return;
8875
8876 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8877 if (partial == NULL)
8878 buf->b_prompt_interrupt = vim_strsave(callback);
8879 else
8880 /* pointer into the partial */
8881 buf->b_prompt_interrupt = callback;
8882 buf->b_prompt_int_partial = partial;
8883}
8884
8885/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008886 * "prompt_setprompt({buffer}, {text})" function
8887 */
8888 static void
8889f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8890{
8891 buf_T *buf;
8892 char_u *text;
8893
8894 if (check_secure())
8895 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008896 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008897 if (buf == NULL)
8898 return;
8899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008900 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008901 vim_free(buf->b_prompt_text);
8902 buf->b_prompt_text = vim_strsave(text);
8903}
8904#endif
8905
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008906/*
8907 * "pumvisible()" function
8908 */
8909 static void
8910f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8911{
8912#ifdef FEAT_INS_EXPAND
8913 if (pum_visible())
8914 rettv->vval.v_number = 1;
8915#endif
8916}
8917
8918#ifdef FEAT_PYTHON3
8919/*
8920 * "py3eval()" function
8921 */
8922 static void
8923f_py3eval(typval_T *argvars, typval_T *rettv)
8924{
8925 char_u *str;
8926 char_u buf[NUMBUFLEN];
8927
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008928 if (p_pyx == 0)
8929 p_pyx = 3;
8930
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008931 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 do_py3eval(str, rettv);
8933}
8934#endif
8935
8936#ifdef FEAT_PYTHON
8937/*
8938 * "pyeval()" function
8939 */
8940 static void
8941f_pyeval(typval_T *argvars, typval_T *rettv)
8942{
8943 char_u *str;
8944 char_u buf[NUMBUFLEN];
8945
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008946 if (p_pyx == 0)
8947 p_pyx = 2;
8948
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008949 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 do_pyeval(str, rettv);
8951}
8952#endif
8953
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008954#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8955/*
8956 * "pyxeval()" function
8957 */
8958 static void
8959f_pyxeval(typval_T *argvars, typval_T *rettv)
8960{
8961# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8962 init_pyxversion();
8963 if (p_pyx == 2)
8964 f_pyeval(argvars, rettv);
8965 else
8966 f_py3eval(argvars, rettv);
8967# elif defined(FEAT_PYTHON)
8968 f_pyeval(argvars, rettv);
8969# elif defined(FEAT_PYTHON3)
8970 f_py3eval(argvars, rettv);
8971# endif
8972}
8973#endif
8974
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975/*
8976 * "range()" function
8977 */
8978 static void
8979f_range(typval_T *argvars, typval_T *rettv)
8980{
8981 varnumber_T start;
8982 varnumber_T end;
8983 varnumber_T stride = 1;
8984 varnumber_T i;
8985 int error = FALSE;
8986
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008987 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008988 if (argvars[1].v_type == VAR_UNKNOWN)
8989 {
8990 end = start - 1;
8991 start = 0;
8992 }
8993 else
8994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008995 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008997 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008998 }
8999
9000 if (error)
9001 return; /* type error; errmsg already given */
9002 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009003 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009005 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009006 else
9007 {
9008 if (rettv_list_alloc(rettv) == OK)
9009 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9010 if (list_append_number(rettv->vval.v_list,
9011 (varnumber_T)i) == FAIL)
9012 break;
9013 }
9014}
9015
9016/*
9017 * "readfile()" function
9018 */
9019 static void
9020f_readfile(typval_T *argvars, typval_T *rettv)
9021{
9022 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009023 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 int failed = FALSE;
9025 char_u *fname;
9026 FILE *fd;
9027 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9028 int io_size = sizeof(buf);
9029 int readlen; /* size of last fread() */
9030 char_u *prev = NULL; /* previously read bytes, if any */
9031 long prevlen = 0; /* length of data in prev */
9032 long prevsize = 0; /* size of prev buffer */
9033 long maxline = MAXLNUM;
9034 long cnt = 0;
9035 char_u *p; /* position in buf */
9036 char_u *start; /* start of current line */
9037
9038 if (argvars[1].v_type != VAR_UNKNOWN)
9039 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009040 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009042 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9043 blob = TRUE;
9044
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009045 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047 }
9048
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009049 if (blob)
9050 {
9051 if (rettv_blob_alloc(rettv) == FAIL)
9052 return;
9053 }
9054 else
9055 {
9056 if (rettv_list_alloc(rettv) == FAIL)
9057 return;
9058 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059
9060 /* Always open the file in binary mode, library functions have a mind of
9061 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009062 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9064 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009065 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 return;
9067 }
9068
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009069 if (blob)
9070 {
9071 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009073 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009074 blob_free(rettv->vval.v_blob);
9075 }
9076 fclose(fd);
9077 return;
9078 }
9079
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009080 while (cnt < maxline || maxline < 0)
9081 {
9082 readlen = (int)fread(buf, 1, io_size, fd);
9083
9084 /* This for loop processes what was read, but is also entered at end
9085 * of file so that either:
9086 * - an incomplete line gets written
9087 * - a "binary" file gets an empty line at the end if it ends in a
9088 * newline. */
9089 for (p = buf, start = buf;
9090 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9091 ++p)
9092 {
9093 if (*p == '\n' || readlen <= 0)
9094 {
9095 listitem_T *li;
9096 char_u *s = NULL;
9097 long_u len = p - start;
9098
9099 /* Finished a line. Remove CRs before NL. */
9100 if (readlen > 0 && !binary)
9101 {
9102 while (len > 0 && start[len - 1] == '\r')
9103 --len;
9104 /* removal may cross back to the "prev" string */
9105 if (len == 0)
9106 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9107 --prevlen;
9108 }
9109 if (prevlen == 0)
9110 s = vim_strnsave(start, (int)len);
9111 else
9112 {
9113 /* Change "prev" buffer to be the right size. This way
9114 * the bytes are only copied once, and very long lines are
9115 * allocated only once. */
9116 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9117 {
9118 mch_memmove(s + prevlen, start, len);
9119 s[prevlen + len] = NUL;
9120 prev = NULL; /* the list will own the string */
9121 prevlen = prevsize = 0;
9122 }
9123 }
9124 if (s == NULL)
9125 {
9126 do_outofmem_msg((long_u) prevlen + len + 1);
9127 failed = TRUE;
9128 break;
9129 }
9130
9131 if ((li = listitem_alloc()) == NULL)
9132 {
9133 vim_free(s);
9134 failed = TRUE;
9135 break;
9136 }
9137 li->li_tv.v_type = VAR_STRING;
9138 li->li_tv.v_lock = 0;
9139 li->li_tv.vval.v_string = s;
9140 list_append(rettv->vval.v_list, li);
9141
9142 start = p + 1; /* step over newline */
9143 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9144 break;
9145 }
9146 else if (*p == NUL)
9147 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9149 * when finding the BF and check the previous two bytes. */
9150 else if (*p == 0xbf && enc_utf8 && !binary)
9151 {
9152 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9153 * + 1, these may be in the "prev" string. */
9154 char_u back1 = p >= buf + 1 ? p[-1]
9155 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9156 char_u back2 = p >= buf + 2 ? p[-2]
9157 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9158 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9159
9160 if (back2 == 0xef && back1 == 0xbb)
9161 {
9162 char_u *dest = p - 2;
9163
9164 /* Usually a BOM is at the beginning of a file, and so at
9165 * the beginning of a line; then we can just step over it.
9166 */
9167 if (start == dest)
9168 start = p + 1;
9169 else
9170 {
9171 /* have to shuffle buf to close gap */
9172 int adjust_prevlen = 0;
9173
9174 if (dest < buf)
9175 {
9176 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9177 dest = buf;
9178 }
9179 if (readlen > p - buf + 1)
9180 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9181 readlen -= 3 - adjust_prevlen;
9182 prevlen -= adjust_prevlen;
9183 p = dest - 1;
9184 }
9185 }
9186 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009187 } /* for */
9188
9189 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9190 break;
9191 if (start < p)
9192 {
9193 /* There's part of a line in buf, store it in "prev". */
9194 if (p - start + prevlen >= prevsize)
9195 {
9196 /* need bigger "prev" buffer */
9197 char_u *newprev;
9198
9199 /* A common use case is ordinary text files and "prev" gets a
9200 * fragment of a line, so the first allocation is made
9201 * small, to avoid repeatedly 'allocing' large and
9202 * 'reallocing' small. */
9203 if (prevsize == 0)
9204 prevsize = (long)(p - start);
9205 else
9206 {
9207 long grow50pc = (prevsize * 3) / 2;
9208 long growmin = (long)((p - start) * 2 + prevlen);
9209 prevsize = grow50pc > growmin ? grow50pc : growmin;
9210 }
9211 newprev = prev == NULL ? alloc(prevsize)
9212 : vim_realloc(prev, prevsize);
9213 if (newprev == NULL)
9214 {
9215 do_outofmem_msg((long_u)prevsize);
9216 failed = TRUE;
9217 break;
9218 }
9219 prev = newprev;
9220 }
9221 /* Add the line part to end of "prev". */
9222 mch_memmove(prev + prevlen, start, p - start);
9223 prevlen += (long)(p - start);
9224 }
9225 } /* while */
9226
9227 /*
9228 * For a negative line count use only the lines at the end of the file,
9229 * free the rest.
9230 */
9231 if (!failed && maxline < 0)
9232 while (cnt > -maxline)
9233 {
9234 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9235 --cnt;
9236 }
9237
9238 if (failed)
9239 {
9240 list_free(rettv->vval.v_list);
9241 /* readfile doc says an empty list is returned on error */
9242 rettv->vval.v_list = list_alloc();
9243 }
9244
9245 vim_free(prev);
9246 fclose(fd);
9247}
9248
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009249 static void
9250return_register(int regname, typval_T *rettv)
9251{
9252 char_u buf[2] = {0, 0};
9253
9254 buf[0] = (char_u)regname;
9255 rettv->v_type = VAR_STRING;
9256 rettv->vval.v_string = vim_strsave(buf);
9257}
9258
9259/*
9260 * "reg_executing()" function
9261 */
9262 static void
9263f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9264{
9265 return_register(reg_executing, rettv);
9266}
9267
9268/*
9269 * "reg_recording()" function
9270 */
9271 static void
9272f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9273{
9274 return_register(reg_recording, rettv);
9275}
9276
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009277#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009278/*
9279 * Convert a List to proftime_T.
9280 * Return FAIL when there is something wrong.
9281 */
9282 static int
9283list2proftime(typval_T *arg, proftime_T *tm)
9284{
9285 long n1, n2;
9286 int error = FALSE;
9287
9288 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9289 || arg->vval.v_list->lv_len != 2)
9290 return FAIL;
9291 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9292 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9293# ifdef WIN3264
9294 tm->HighPart = n1;
9295 tm->LowPart = n2;
9296# else
9297 tm->tv_sec = n1;
9298 tm->tv_usec = n2;
9299# endif
9300 return error ? FAIL : OK;
9301}
9302#endif /* FEAT_RELTIME */
9303
9304/*
9305 * "reltime()" function
9306 */
9307 static void
9308f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9309{
9310#ifdef FEAT_RELTIME
9311 proftime_T res;
9312 proftime_T start;
9313
9314 if (argvars[0].v_type == VAR_UNKNOWN)
9315 {
9316 /* No arguments: get current time. */
9317 profile_start(&res);
9318 }
9319 else if (argvars[1].v_type == VAR_UNKNOWN)
9320 {
9321 if (list2proftime(&argvars[0], &res) == FAIL)
9322 return;
9323 profile_end(&res);
9324 }
9325 else
9326 {
9327 /* Two arguments: compute the difference. */
9328 if (list2proftime(&argvars[0], &start) == FAIL
9329 || list2proftime(&argvars[1], &res) == FAIL)
9330 return;
9331 profile_sub(&res, &start);
9332 }
9333
9334 if (rettv_list_alloc(rettv) == OK)
9335 {
9336 long n1, n2;
9337
9338# ifdef WIN3264
9339 n1 = res.HighPart;
9340 n2 = res.LowPart;
9341# else
9342 n1 = res.tv_sec;
9343 n2 = res.tv_usec;
9344# endif
9345 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9346 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9347 }
9348#endif
9349}
9350
9351#ifdef FEAT_FLOAT
9352/*
9353 * "reltimefloat()" function
9354 */
9355 static void
9356f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9357{
9358# ifdef FEAT_RELTIME
9359 proftime_T tm;
9360# endif
9361
9362 rettv->v_type = VAR_FLOAT;
9363 rettv->vval.v_float = 0;
9364# ifdef FEAT_RELTIME
9365 if (list2proftime(&argvars[0], &tm) == OK)
9366 rettv->vval.v_float = profile_float(&tm);
9367# endif
9368}
9369#endif
9370
9371/*
9372 * "reltimestr()" function
9373 */
9374 static void
9375f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9376{
9377#ifdef FEAT_RELTIME
9378 proftime_T tm;
9379#endif
9380
9381 rettv->v_type = VAR_STRING;
9382 rettv->vval.v_string = NULL;
9383#ifdef FEAT_RELTIME
9384 if (list2proftime(&argvars[0], &tm) == OK)
9385 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9386#endif
9387}
9388
9389#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009390 static void
9391make_connection(void)
9392{
9393 if (X_DISPLAY == NULL
9394# ifdef FEAT_GUI
9395 && !gui.in_use
9396# endif
9397 )
9398 {
9399 x_force_connect = TRUE;
9400 setup_term_clip();
9401 x_force_connect = FALSE;
9402 }
9403}
9404
9405 static int
9406check_connection(void)
9407{
9408 make_connection();
9409 if (X_DISPLAY == NULL)
9410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009411 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 return FAIL;
9413 }
9414 return OK;
9415}
9416#endif
9417
9418#ifdef FEAT_CLIENTSERVER
9419 static void
9420remote_common(typval_T *argvars, typval_T *rettv, int expr)
9421{
9422 char_u *server_name;
9423 char_u *keys;
9424 char_u *r = NULL;
9425 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009426 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009427# ifdef WIN32
9428 HWND w;
9429# else
9430 Window w;
9431# endif
9432
9433 if (check_restricted() || check_secure())
9434 return;
9435
9436# ifdef FEAT_X11
9437 if (check_connection() == FAIL)
9438 return;
9439# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009440 if (argvars[2].v_type != VAR_UNKNOWN
9441 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009442 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009443
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009444 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445 if (server_name == NULL)
9446 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009447 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009449 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009450# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009451 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9452 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453# endif
9454 {
9455 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009457 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009458 vim_free(r);
9459 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009460 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009461 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462 return;
9463 }
9464
9465 rettv->vval.v_string = r;
9466
9467 if (argvars[2].v_type != VAR_UNKNOWN)
9468 {
9469 dictitem_T v;
9470 char_u str[30];
9471 char_u *idvar;
9472
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009473 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009474 if (idvar != NULL && *idvar != NUL)
9475 {
9476 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9477 v.di_tv.v_type = VAR_STRING;
9478 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009479 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009480 vim_free(v.di_tv.vval.v_string);
9481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482 }
9483}
9484#endif
9485
9486/*
9487 * "remote_expr()" function
9488 */
9489 static void
9490f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9491{
9492 rettv->v_type = VAR_STRING;
9493 rettv->vval.v_string = NULL;
9494#ifdef FEAT_CLIENTSERVER
9495 remote_common(argvars, rettv, TRUE);
9496#endif
9497}
9498
9499/*
9500 * "remote_foreground()" function
9501 */
9502 static void
9503f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9504{
9505#ifdef FEAT_CLIENTSERVER
9506# ifdef WIN32
9507 /* On Win32 it's done in this application. */
9508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009509 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009510
9511 if (server_name != NULL)
9512 serverForeground(server_name);
9513 }
9514# else
9515 /* Send a foreground() expression to the server. */
9516 argvars[1].v_type = VAR_STRING;
9517 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9518 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009519 rettv->v_type = VAR_STRING;
9520 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009521 remote_common(argvars, rettv, TRUE);
9522 vim_free(argvars[1].vval.v_string);
9523# endif
9524#endif
9525}
9526
9527 static void
9528f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9529{
9530#ifdef FEAT_CLIENTSERVER
9531 dictitem_T v;
9532 char_u *s = NULL;
9533# ifdef WIN32
9534 long_u n = 0;
9535# endif
9536 char_u *serverid;
9537
9538 if (check_restricted() || check_secure())
9539 {
9540 rettv->vval.v_number = -1;
9541 return;
9542 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009543 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 if (serverid == NULL)
9545 {
9546 rettv->vval.v_number = -1;
9547 return; /* type error; errmsg already given */
9548 }
9549# ifdef WIN32
9550 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9551 if (n == 0)
9552 rettv->vval.v_number = -1;
9553 else
9554 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009555 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009556 rettv->vval.v_number = (s != NULL);
9557 }
9558# else
9559 if (check_connection() == FAIL)
9560 return;
9561
9562 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9563 serverStrToWin(serverid), &s);
9564# endif
9565
9566 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9567 {
9568 char_u *retvar;
9569
9570 v.di_tv.v_type = VAR_STRING;
9571 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009572 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009573 if (retvar != NULL)
9574 set_var(retvar, &v.di_tv, FALSE);
9575 vim_free(v.di_tv.vval.v_string);
9576 }
9577#else
9578 rettv->vval.v_number = -1;
9579#endif
9580}
9581
9582 static void
9583f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9584{
9585 char_u *r = NULL;
9586
9587#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009588 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009589
9590 if (serverid != NULL && !check_restricted() && !check_secure())
9591 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009592 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009593# ifdef WIN32
9594 /* The server's HWND is encoded in the 'id' parameter */
9595 long_u n = 0;
9596# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009597
9598 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009599 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009601# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009602 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9603 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009604 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009605 if (r == NULL)
9606# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009607 if (check_connection() == FAIL
9608 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9609 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009611 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009612 }
9613#endif
9614 rettv->v_type = VAR_STRING;
9615 rettv->vval.v_string = r;
9616}
9617
9618/*
9619 * "remote_send()" function
9620 */
9621 static void
9622f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9623{
9624 rettv->v_type = VAR_STRING;
9625 rettv->vval.v_string = NULL;
9626#ifdef FEAT_CLIENTSERVER
9627 remote_common(argvars, rettv, FALSE);
9628#endif
9629}
9630
9631/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009632 * "remote_startserver()" function
9633 */
9634 static void
9635f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9636{
9637#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009638 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009639
9640 if (server == NULL)
9641 return; /* type error; errmsg already given */
9642 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009643 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009644 else
9645 {
9646# ifdef FEAT_X11
9647 if (check_connection() == OK)
9648 serverRegisterName(X_DISPLAY, server);
9649# else
9650 serverSetName(server);
9651# endif
9652 }
9653#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009654 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009655#endif
9656}
9657
9658/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 * "remove()" function
9660 */
9661 static void
9662f_remove(typval_T *argvars, typval_T *rettv)
9663{
9664 list_T *l;
9665 listitem_T *item, *item2;
9666 listitem_T *li;
9667 long idx;
9668 long end;
9669 char_u *key;
9670 dict_T *d;
9671 dictitem_T *di;
9672 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009673 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009674
9675 if (argvars[0].v_type == VAR_DICT)
9676 {
9677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009678 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009679 else if ((d = argvars[0].vval.v_dict) != NULL
9680 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009682 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 if (key != NULL)
9684 {
9685 di = dict_find(d, key, -1);
9686 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009687 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9689 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9690 {
9691 *rettv = di->di_tv;
9692 init_tv(&di->di_tv);
9693 dictitem_remove(d, di);
9694 }
9695 }
9696 }
9697 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009698 else if (argvars[0].v_type == VAR_BLOB)
9699 {
9700 idx = (long)tv_get_number_chk(&argvars[1], &error);
9701 if (!error)
9702 {
9703 blob_T *b = argvars[0].vval.v_blob;
9704 int len = blob_len(b);
9705 char_u *p;
9706
9707 if (idx < 0)
9708 // count from the end
9709 idx = len + idx;
9710 if (idx < 0 || idx >= len)
9711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009712 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009713 return;
9714 }
9715 if (argvars[2].v_type == VAR_UNKNOWN)
9716 {
9717 // Remove one item, return its value.
9718 p = (char_u *)b->bv_ga.ga_data;
9719 rettv->vval.v_number = (varnumber_T) *(p + idx);
9720 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9721 --b->bv_ga.ga_len;
9722 }
9723 else
9724 {
9725 blob_T *blob;
9726
9727 // Remove range of items, return list with values.
9728 end = (long)tv_get_number_chk(&argvars[2], &error);
9729 if (error)
9730 return;
9731 if (end < 0)
9732 // count from the end
9733 end = len + end;
9734 if (end >= len || idx > end)
9735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009736 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009737 return;
9738 }
9739 blob = blob_alloc();
9740 if (blob == NULL)
9741 return;
9742 blob->bv_ga.ga_len = end - idx + 1;
9743 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9744 {
9745 vim_free(blob);
9746 return;
9747 }
9748 p = (char_u *)b->bv_ga.ga_data;
9749 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9750 (size_t)(end - idx + 1));
9751 ++blob->bv_refcount;
9752 rettv->v_type = VAR_BLOB;
9753 rettv->vval.v_blob = blob;
9754
9755 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9756 b->bv_ga.ga_len -= end - idx + 1;
9757 }
9758 }
9759 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009761 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009762 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009763 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009765 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009766 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009767 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009768 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009769 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009770 else
9771 {
9772 if (argvars[2].v_type == VAR_UNKNOWN)
9773 {
9774 /* Remove one item, return its value. */
9775 vimlist_remove(l, item, item);
9776 *rettv = item->li_tv;
9777 vim_free(item);
9778 }
9779 else
9780 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009781 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009782 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009783 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009784 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009785 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009786 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009787 else
9788 {
9789 int cnt = 0;
9790
9791 for (li = item; li != NULL; li = li->li_next)
9792 {
9793 ++cnt;
9794 if (li == item2)
9795 break;
9796 }
9797 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009798 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009799 else
9800 {
9801 vimlist_remove(l, item, item2);
9802 if (rettv_list_alloc(rettv) == OK)
9803 {
9804 l = rettv->vval.v_list;
9805 l->lv_first = item;
9806 l->lv_last = item2;
9807 item->li_prev = NULL;
9808 item2->li_next = NULL;
9809 l->lv_len = cnt;
9810 }
9811 }
9812 }
9813 }
9814 }
9815 }
9816}
9817
9818/*
9819 * "rename({from}, {to})" function
9820 */
9821 static void
9822f_rename(typval_T *argvars, typval_T *rettv)
9823{
9824 char_u buf[NUMBUFLEN];
9825
9826 if (check_restricted() || check_secure())
9827 rettv->vval.v_number = -1;
9828 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009829 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9830 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831}
9832
9833/*
9834 * "repeat()" function
9835 */
9836 static void
9837f_repeat(typval_T *argvars, typval_T *rettv)
9838{
9839 char_u *p;
9840 int n;
9841 int slen;
9842 int len;
9843 char_u *r;
9844 int i;
9845
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009846 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009847 if (argvars[0].v_type == VAR_LIST)
9848 {
9849 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9850 while (n-- > 0)
9851 if (list_extend(rettv->vval.v_list,
9852 argvars[0].vval.v_list, NULL) == FAIL)
9853 break;
9854 }
9855 else
9856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009857 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 rettv->v_type = VAR_STRING;
9859 rettv->vval.v_string = NULL;
9860
9861 slen = (int)STRLEN(p);
9862 len = slen * n;
9863 if (len <= 0)
9864 return;
9865
9866 r = alloc(len + 1);
9867 if (r != NULL)
9868 {
9869 for (i = 0; i < n; i++)
9870 mch_memmove(r + i * slen, p, (size_t)slen);
9871 r[len] = NUL;
9872 }
9873
9874 rettv->vval.v_string = r;
9875 }
9876}
9877
9878/*
9879 * "resolve()" function
9880 */
9881 static void
9882f_resolve(typval_T *argvars, typval_T *rettv)
9883{
9884 char_u *p;
9885#ifdef HAVE_READLINK
9886 char_u *buf = NULL;
9887#endif
9888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009889 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890#ifdef FEAT_SHORTCUT
9891 {
9892 char_u *v = NULL;
9893
9894 v = mch_resolve_shortcut(p);
9895 if (v != NULL)
9896 rettv->vval.v_string = v;
9897 else
9898 rettv->vval.v_string = vim_strsave(p);
9899 }
9900#else
9901# ifdef HAVE_READLINK
9902 {
9903 char_u *cpy;
9904 int len;
9905 char_u *remain = NULL;
9906 char_u *q;
9907 int is_relative_to_current = FALSE;
9908 int has_trailing_pathsep = FALSE;
9909 int limit = 100;
9910
9911 p = vim_strsave(p);
9912
9913 if (p[0] == '.' && (vim_ispathsep(p[1])
9914 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9915 is_relative_to_current = TRUE;
9916
9917 len = STRLEN(p);
9918 if (len > 0 && after_pathsep(p, p + len))
9919 {
9920 has_trailing_pathsep = TRUE;
9921 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9922 }
9923
9924 q = getnextcomp(p);
9925 if (*q != NUL)
9926 {
9927 /* Separate the first path component in "p", and keep the
9928 * remainder (beginning with the path separator). */
9929 remain = vim_strsave(q - 1);
9930 q[-1] = NUL;
9931 }
9932
9933 buf = alloc(MAXPATHL + 1);
9934 if (buf == NULL)
9935 goto fail;
9936
9937 for (;;)
9938 {
9939 for (;;)
9940 {
9941 len = readlink((char *)p, (char *)buf, MAXPATHL);
9942 if (len <= 0)
9943 break;
9944 buf[len] = NUL;
9945
9946 if (limit-- == 0)
9947 {
9948 vim_free(p);
9949 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009950 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 rettv->vval.v_string = NULL;
9952 goto fail;
9953 }
9954
9955 /* Ensure that the result will have a trailing path separator
9956 * if the argument has one. */
9957 if (remain == NULL && has_trailing_pathsep)
9958 add_pathsep(buf);
9959
9960 /* Separate the first path component in the link value and
9961 * concatenate the remainders. */
9962 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9963 if (*q != NUL)
9964 {
9965 if (remain == NULL)
9966 remain = vim_strsave(q - 1);
9967 else
9968 {
9969 cpy = concat_str(q - 1, remain);
9970 if (cpy != NULL)
9971 {
9972 vim_free(remain);
9973 remain = cpy;
9974 }
9975 }
9976 q[-1] = NUL;
9977 }
9978
9979 q = gettail(p);
9980 if (q > p && *q == NUL)
9981 {
9982 /* Ignore trailing path separator. */
9983 q[-1] = NUL;
9984 q = gettail(p);
9985 }
9986 if (q > p && !mch_isFullName(buf))
9987 {
9988 /* symlink is relative to directory of argument */
9989 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9990 if (cpy != NULL)
9991 {
9992 STRCPY(cpy, p);
9993 STRCPY(gettail(cpy), buf);
9994 vim_free(p);
9995 p = cpy;
9996 }
9997 }
9998 else
9999 {
10000 vim_free(p);
10001 p = vim_strsave(buf);
10002 }
10003 }
10004
10005 if (remain == NULL)
10006 break;
10007
10008 /* Append the first path component of "remain" to "p". */
10009 q = getnextcomp(remain + 1);
10010 len = q - remain - (*q != NUL);
10011 cpy = vim_strnsave(p, STRLEN(p) + len);
10012 if (cpy != NULL)
10013 {
10014 STRNCAT(cpy, remain, len);
10015 vim_free(p);
10016 p = cpy;
10017 }
10018 /* Shorten "remain". */
10019 if (*q != NUL)
10020 STRMOVE(remain, q - 1);
10021 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010022 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010023 }
10024
10025 /* If the result is a relative path name, make it explicitly relative to
10026 * the current directory if and only if the argument had this form. */
10027 if (!vim_ispathsep(*p))
10028 {
10029 if (is_relative_to_current
10030 && *p != NUL
10031 && !(p[0] == '.'
10032 && (p[1] == NUL
10033 || vim_ispathsep(p[1])
10034 || (p[1] == '.'
10035 && (p[2] == NUL
10036 || vim_ispathsep(p[2]))))))
10037 {
10038 /* Prepend "./". */
10039 cpy = concat_str((char_u *)"./", p);
10040 if (cpy != NULL)
10041 {
10042 vim_free(p);
10043 p = cpy;
10044 }
10045 }
10046 else if (!is_relative_to_current)
10047 {
10048 /* Strip leading "./". */
10049 q = p;
10050 while (q[0] == '.' && vim_ispathsep(q[1]))
10051 q += 2;
10052 if (q > p)
10053 STRMOVE(p, p + 2);
10054 }
10055 }
10056
10057 /* Ensure that the result will have no trailing path separator
10058 * if the argument had none. But keep "/" or "//". */
10059 if (!has_trailing_pathsep)
10060 {
10061 q = p + STRLEN(p);
10062 if (after_pathsep(p, q))
10063 *gettail_sep(p) = NUL;
10064 }
10065
10066 rettv->vval.v_string = p;
10067 }
10068# else
10069 rettv->vval.v_string = vim_strsave(p);
10070# endif
10071#endif
10072
10073 simplify_filename(rettv->vval.v_string);
10074
10075#ifdef HAVE_READLINK
10076fail:
10077 vim_free(buf);
10078#endif
10079 rettv->v_type = VAR_STRING;
10080}
10081
10082/*
10083 * "reverse({list})" function
10084 */
10085 static void
10086f_reverse(typval_T *argvars, typval_T *rettv)
10087{
10088 list_T *l;
10089 listitem_T *li, *ni;
10090
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010091 if (argvars[0].v_type == VAR_BLOB)
10092 {
10093 blob_T *b = argvars[0].vval.v_blob;
10094 int i, len = blob_len(b);
10095
10096 for (i = 0; i < len / 2; i++)
10097 {
10098 int tmp = blob_get(b, i);
10099
10100 blob_set(b, i, blob_get(b, len - i - 1));
10101 blob_set(b, len - i - 1, tmp);
10102 }
10103 rettv_blob_set(rettv, b);
10104 return;
10105 }
10106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010107 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010108 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 else if ((l = argvars[0].vval.v_list) != NULL
10110 && !tv_check_lock(l->lv_lock,
10111 (char_u *)N_("reverse() argument"), TRUE))
10112 {
10113 li = l->lv_last;
10114 l->lv_first = l->lv_last = NULL;
10115 l->lv_len = 0;
10116 while (li != NULL)
10117 {
10118 ni = li->li_prev;
10119 list_append(l, li);
10120 li = ni;
10121 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010122 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010123 l->lv_idx = l->lv_len - l->lv_idx - 1;
10124 }
10125}
10126
10127#define SP_NOMOVE 0x01 /* don't move cursor */
10128#define SP_REPEAT 0x02 /* repeat to find outer pair */
10129#define SP_RETCOUNT 0x04 /* return matchcount */
10130#define SP_SETPCMARK 0x08 /* set previous context mark */
10131#define SP_START 0x10 /* accept match at start position */
10132#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10133#define SP_END 0x40 /* leave cursor at end of match */
10134#define SP_COLUMN 0x80 /* start at cursor column */
10135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136/*
10137 * Get flags for a search function.
10138 * Possibly sets "p_ws".
10139 * Returns BACKWARD, FORWARD or zero (for an error).
10140 */
10141 static int
10142get_search_arg(typval_T *varp, int *flagsp)
10143{
10144 int dir = FORWARD;
10145 char_u *flags;
10146 char_u nbuf[NUMBUFLEN];
10147 int mask;
10148
10149 if (varp->v_type != VAR_UNKNOWN)
10150 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010151 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 if (flags == NULL)
10153 return 0; /* type error; errmsg already given */
10154 while (*flags != NUL)
10155 {
10156 switch (*flags)
10157 {
10158 case 'b': dir = BACKWARD; break;
10159 case 'w': p_ws = TRUE; break;
10160 case 'W': p_ws = FALSE; break;
10161 default: mask = 0;
10162 if (flagsp != NULL)
10163 switch (*flags)
10164 {
10165 case 'c': mask = SP_START; break;
10166 case 'e': mask = SP_END; break;
10167 case 'm': mask = SP_RETCOUNT; break;
10168 case 'n': mask = SP_NOMOVE; break;
10169 case 'p': mask = SP_SUBPAT; break;
10170 case 'r': mask = SP_REPEAT; break;
10171 case 's': mask = SP_SETPCMARK; break;
10172 case 'z': mask = SP_COLUMN; break;
10173 }
10174 if (mask == 0)
10175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010176 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010177 dir = 0;
10178 }
10179 else
10180 *flagsp |= mask;
10181 }
10182 if (dir == 0)
10183 break;
10184 ++flags;
10185 }
10186 }
10187 return dir;
10188}
10189
10190/*
10191 * Shared by search() and searchpos() functions.
10192 */
10193 static int
10194search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10195{
10196 int flags;
10197 char_u *pat;
10198 pos_T pos;
10199 pos_T save_cursor;
10200 int save_p_ws = p_ws;
10201 int dir;
10202 int retval = 0; /* default: FAIL */
10203 long lnum_stop = 0;
10204 proftime_T tm;
10205#ifdef FEAT_RELTIME
10206 long time_limit = 0;
10207#endif
10208 int options = SEARCH_KEEP;
10209 int subpatnum;
10210
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010211 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010212 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10213 if (dir == 0)
10214 goto theend;
10215 flags = *flagsp;
10216 if (flags & SP_START)
10217 options |= SEARCH_START;
10218 if (flags & SP_END)
10219 options |= SEARCH_END;
10220 if (flags & SP_COLUMN)
10221 options |= SEARCH_COL;
10222
10223 /* Optional arguments: line number to stop searching and timeout. */
10224 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10225 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010226 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010227 if (lnum_stop < 0)
10228 goto theend;
10229#ifdef FEAT_RELTIME
10230 if (argvars[3].v_type != VAR_UNKNOWN)
10231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010232 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233 if (time_limit < 0)
10234 goto theend;
10235 }
10236#endif
10237 }
10238
10239#ifdef FEAT_RELTIME
10240 /* Set the time limit, if there is one. */
10241 profile_setlimit(time_limit, &tm);
10242#endif
10243
10244 /*
10245 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10246 * Check to make sure only those flags are set.
10247 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10248 * flags cannot be set. Check for that condition also.
10249 */
10250 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10251 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010253 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254 goto theend;
10255 }
10256
10257 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010258 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010259 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 if (subpatnum != FAIL)
10261 {
10262 if (flags & SP_SUBPAT)
10263 retval = subpatnum;
10264 else
10265 retval = pos.lnum;
10266 if (flags & SP_SETPCMARK)
10267 setpcmark();
10268 curwin->w_cursor = pos;
10269 if (match_pos != NULL)
10270 {
10271 /* Store the match cursor position */
10272 match_pos->lnum = pos.lnum;
10273 match_pos->col = pos.col + 1;
10274 }
10275 /* "/$" will put the cursor after the end of the line, may need to
10276 * correct that here */
10277 check_cursor();
10278 }
10279
10280 /* If 'n' flag is used: restore cursor position. */
10281 if (flags & SP_NOMOVE)
10282 curwin->w_cursor = save_cursor;
10283 else
10284 curwin->w_set_curswant = TRUE;
10285theend:
10286 p_ws = save_p_ws;
10287
10288 return retval;
10289}
10290
10291#ifdef FEAT_FLOAT
10292
10293/*
10294 * round() is not in C90, use ceil() or floor() instead.
10295 */
10296 float_T
10297vim_round(float_T f)
10298{
10299 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10300}
10301
10302/*
10303 * "round({float})" function
10304 */
10305 static void
10306f_round(typval_T *argvars, typval_T *rettv)
10307{
10308 float_T f = 0.0;
10309
10310 rettv->v_type = VAR_FLOAT;
10311 if (get_float_arg(argvars, &f) == OK)
10312 rettv->vval.v_float = vim_round(f);
10313 else
10314 rettv->vval.v_float = 0.0;
10315}
10316#endif
10317
10318/*
10319 * "screenattr()" function
10320 */
10321 static void
10322f_screenattr(typval_T *argvars, typval_T *rettv)
10323{
10324 int row;
10325 int col;
10326 int c;
10327
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010328 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10329 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010330 if (row < 0 || row >= screen_Rows
10331 || col < 0 || col >= screen_Columns)
10332 c = -1;
10333 else
10334 c = ScreenAttrs[LineOffset[row] + col];
10335 rettv->vval.v_number = c;
10336}
10337
10338/*
10339 * "screenchar()" function
10340 */
10341 static void
10342f_screenchar(typval_T *argvars, typval_T *rettv)
10343{
10344 int row;
10345 int col;
10346 int off;
10347 int c;
10348
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010349 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10350 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351 if (row < 0 || row >= screen_Rows
10352 || col < 0 || col >= screen_Columns)
10353 c = -1;
10354 else
10355 {
10356 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 if (enc_utf8 && ScreenLinesUC[off] != 0)
10358 c = ScreenLinesUC[off];
10359 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 c = ScreenLines[off];
10361 }
10362 rettv->vval.v_number = c;
10363}
10364
10365/*
10366 * "screencol()" function
10367 *
10368 * First column is 1 to be consistent with virtcol().
10369 */
10370 static void
10371f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10372{
10373 rettv->vval.v_number = screen_screencol() + 1;
10374}
10375
10376/*
10377 * "screenrow()" function
10378 */
10379 static void
10380f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10381{
10382 rettv->vval.v_number = screen_screenrow() + 1;
10383}
10384
10385/*
10386 * "search()" function
10387 */
10388 static void
10389f_search(typval_T *argvars, typval_T *rettv)
10390{
10391 int flags = 0;
10392
10393 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10394}
10395
10396/*
10397 * "searchdecl()" function
10398 */
10399 static void
10400f_searchdecl(typval_T *argvars, typval_T *rettv)
10401{
10402 int locally = 1;
10403 int thisblock = 0;
10404 int error = FALSE;
10405 char_u *name;
10406
10407 rettv->vval.v_number = 1; /* default: FAIL */
10408
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010409 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010410 if (argvars[1].v_type != VAR_UNKNOWN)
10411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010412 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010413 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010414 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010415 }
10416 if (!error && name != NULL)
10417 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10418 locally, thisblock, SEARCH_KEEP) == FAIL;
10419}
10420
10421/*
10422 * Used by searchpair() and searchpairpos()
10423 */
10424 static int
10425searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10426{
10427 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010428 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 int save_p_ws = p_ws;
10430 int dir;
10431 int flags = 0;
10432 char_u nbuf1[NUMBUFLEN];
10433 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010434 int retval = 0; /* default: FAIL */
10435 long lnum_stop = 0;
10436 long time_limit = 0;
10437
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010438 /* Get the three pattern arguments: start, middle, end. Will result in an
10439 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010440 spat = tv_get_string_chk(&argvars[0]);
10441 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10442 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010443 if (spat == NULL || mpat == NULL || epat == NULL)
10444 goto theend; /* type error */
10445
10446 /* Handle the optional fourth argument: flags */
10447 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10448 if (dir == 0)
10449 goto theend;
10450
10451 /* Don't accept SP_END or SP_SUBPAT.
10452 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10453 */
10454 if ((flags & (SP_END | SP_SUBPAT)) != 0
10455 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010457 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010458 goto theend;
10459 }
10460
10461 /* Using 'r' implies 'W', otherwise it doesn't work. */
10462 if (flags & SP_REPEAT)
10463 p_ws = FALSE;
10464
10465 /* Optional fifth argument: skip expression */
10466 if (argvars[3].v_type == VAR_UNKNOWN
10467 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010468 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010469 else
10470 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010471 skip = &argvars[4];
10472 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10473 && skip->v_type != VAR_STRING)
10474 {
10475 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010476 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010477 goto theend;
10478 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010479 if (argvars[5].v_type != VAR_UNKNOWN)
10480 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010481 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010482 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010484 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010485 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010487#ifdef FEAT_RELTIME
10488 if (argvars[6].v_type != VAR_UNKNOWN)
10489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010490 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010491 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010493 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010494 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010495 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010496 }
10497#endif
10498 }
10499 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500
10501 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10502 match_pos, lnum_stop, time_limit);
10503
10504theend:
10505 p_ws = save_p_ws;
10506
10507 return retval;
10508}
10509
10510/*
10511 * "searchpair()" function
10512 */
10513 static void
10514f_searchpair(typval_T *argvars, typval_T *rettv)
10515{
10516 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10517}
10518
10519/*
10520 * "searchpairpos()" function
10521 */
10522 static void
10523f_searchpairpos(typval_T *argvars, typval_T *rettv)
10524{
10525 pos_T match_pos;
10526 int lnum = 0;
10527 int col = 0;
10528
10529 if (rettv_list_alloc(rettv) == FAIL)
10530 return;
10531
10532 if (searchpair_cmn(argvars, &match_pos) > 0)
10533 {
10534 lnum = match_pos.lnum;
10535 col = match_pos.col;
10536 }
10537
10538 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10539 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10540}
10541
10542/*
10543 * Search for a start/middle/end thing.
10544 * Used by searchpair(), see its documentation for the details.
10545 * Returns 0 or -1 for no match,
10546 */
10547 long
10548do_searchpair(
10549 char_u *spat, /* start pattern */
10550 char_u *mpat, /* middle pattern */
10551 char_u *epat, /* end pattern */
10552 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010553 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010554 int flags, /* SP_SETPCMARK and other SP_ values */
10555 pos_T *match_pos,
10556 linenr_T lnum_stop, /* stop at this line if not zero */
10557 long time_limit UNUSED) /* stop after this many msec */
10558{
10559 char_u *save_cpo;
10560 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10561 long retval = 0;
10562 pos_T pos;
10563 pos_T firstpos;
10564 pos_T foundpos;
10565 pos_T save_cursor;
10566 pos_T save_pos;
10567 int n;
10568 int r;
10569 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010570 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010571 int err;
10572 int options = SEARCH_KEEP;
10573 proftime_T tm;
10574
10575 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10576 save_cpo = p_cpo;
10577 p_cpo = empty_option;
10578
10579#ifdef FEAT_RELTIME
10580 /* Set the time limit, if there is one. */
10581 profile_setlimit(time_limit, &tm);
10582#endif
10583
10584 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10585 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010586 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10587 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 if (pat2 == NULL || pat3 == NULL)
10589 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010590 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010591 if (*mpat == NUL)
10592 STRCPY(pat3, pat2);
10593 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010594 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010595 spat, epat, mpat);
10596 if (flags & SP_START)
10597 options |= SEARCH_START;
10598
Bram Moolenaar48570482017-10-30 21:48:41 +010010599 if (skip != NULL)
10600 {
10601 /* Empty string means to not use the skip expression. */
10602 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10603 use_skip = skip->vval.v_string != NULL
10604 && *skip->vval.v_string != NUL;
10605 }
10606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607 save_cursor = curwin->w_cursor;
10608 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010609 CLEAR_POS(&firstpos);
10610 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010611 pat = pat3;
10612 for (;;)
10613 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010614 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010615 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010616 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 /* didn't find it or found the first match again: FAIL */
10618 break;
10619
10620 if (firstpos.lnum == 0)
10621 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010622 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 {
10624 /* Found the same position again. Can happen with a pattern that
10625 * has "\zs" at the end and searching backwards. Advance one
10626 * character and try again. */
10627 if (dir == BACKWARD)
10628 decl(&pos);
10629 else
10630 incl(&pos);
10631 }
10632 foundpos = pos;
10633
10634 /* clear the start flag to avoid getting stuck here */
10635 options &= ~SEARCH_START;
10636
10637 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010638 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639 {
10640 save_pos = curwin->w_cursor;
10641 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010642 err = FALSE;
10643 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010644 curwin->w_cursor = save_pos;
10645 if (err)
10646 {
10647 /* Evaluating {skip} caused an error, break here. */
10648 curwin->w_cursor = save_cursor;
10649 retval = -1;
10650 break;
10651 }
10652 if (r)
10653 continue;
10654 }
10655
10656 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10657 {
10658 /* Found end when searching backwards or start when searching
10659 * forward: nested pair. */
10660 ++nest;
10661 pat = pat2; /* nested, don't search for middle */
10662 }
10663 else
10664 {
10665 /* Found end when searching forward or start when searching
10666 * backward: end of (nested) pair; or found middle in outer pair. */
10667 if (--nest == 1)
10668 pat = pat3; /* outer level, search for middle */
10669 }
10670
10671 if (nest == 0)
10672 {
10673 /* Found the match: return matchcount or line number. */
10674 if (flags & SP_RETCOUNT)
10675 ++retval;
10676 else
10677 retval = pos.lnum;
10678 if (flags & SP_SETPCMARK)
10679 setpcmark();
10680 curwin->w_cursor = pos;
10681 if (!(flags & SP_REPEAT))
10682 break;
10683 nest = 1; /* search for next unmatched */
10684 }
10685 }
10686
10687 if (match_pos != NULL)
10688 {
10689 /* Store the match cursor position */
10690 match_pos->lnum = curwin->w_cursor.lnum;
10691 match_pos->col = curwin->w_cursor.col + 1;
10692 }
10693
10694 /* If 'n' flag is used or search failed: restore cursor position. */
10695 if ((flags & SP_NOMOVE) || retval == 0)
10696 curwin->w_cursor = save_cursor;
10697
10698theend:
10699 vim_free(pat2);
10700 vim_free(pat3);
10701 if (p_cpo == empty_option)
10702 p_cpo = save_cpo;
10703 else
10704 /* Darn, evaluating the {skip} expression changed the value. */
10705 free_string_option(save_cpo);
10706
10707 return retval;
10708}
10709
10710/*
10711 * "searchpos()" function
10712 */
10713 static void
10714f_searchpos(typval_T *argvars, typval_T *rettv)
10715{
10716 pos_T match_pos;
10717 int lnum = 0;
10718 int col = 0;
10719 int n;
10720 int flags = 0;
10721
10722 if (rettv_list_alloc(rettv) == FAIL)
10723 return;
10724
10725 n = search_cmn(argvars, &match_pos, &flags);
10726 if (n > 0)
10727 {
10728 lnum = match_pos.lnum;
10729 col = match_pos.col;
10730 }
10731
10732 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10733 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10734 if (flags & SP_SUBPAT)
10735 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10736}
10737
10738 static void
10739f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10740{
10741#ifdef FEAT_CLIENTSERVER
10742 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010743 char_u *server = tv_get_string_chk(&argvars[0]);
10744 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745
10746 rettv->vval.v_number = -1;
10747 if (server == NULL || reply == NULL)
10748 return;
10749 if (check_restricted() || check_secure())
10750 return;
10751# ifdef FEAT_X11
10752 if (check_connection() == FAIL)
10753 return;
10754# endif
10755
10756 if (serverSendReply(server, reply) < 0)
10757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010758 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010759 return;
10760 }
10761 rettv->vval.v_number = 0;
10762#else
10763 rettv->vval.v_number = -1;
10764#endif
10765}
10766
10767 static void
10768f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10769{
10770 char_u *r = NULL;
10771
10772#ifdef FEAT_CLIENTSERVER
10773# ifdef WIN32
10774 r = serverGetVimNames();
10775# else
10776 make_connection();
10777 if (X_DISPLAY != NULL)
10778 r = serverGetVimNames(X_DISPLAY);
10779# endif
10780#endif
10781 rettv->v_type = VAR_STRING;
10782 rettv->vval.v_string = r;
10783}
10784
10785/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010786 * "setbufline()" function
10787 */
10788 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010789f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010790{
10791 linenr_T lnum;
10792 buf_T *buf;
10793
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010794 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010795 if (buf == NULL)
10796 rettv->vval.v_number = 1; /* FAIL */
10797 else
10798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010799 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010800 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010801 }
10802}
10803
10804/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010805 * "setbufvar()" function
10806 */
10807 static void
10808f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10809{
10810 buf_T *buf;
10811 char_u *varname, *bufvarname;
10812 typval_T *varp;
10813 char_u nbuf[NUMBUFLEN];
10814
10815 if (check_restricted() || check_secure())
10816 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010817 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10818 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010819 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010820 varp = &argvars[2];
10821
10822 if (buf != NULL && varname != NULL && varp != NULL)
10823 {
10824 if (*varname == '&')
10825 {
10826 long numval;
10827 char_u *strval;
10828 int error = FALSE;
10829 aco_save_T aco;
10830
10831 /* set curbuf to be our buf, temporarily */
10832 aucmd_prepbuf(&aco, buf);
10833
10834 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010835 numval = (long)tv_get_number_chk(varp, &error);
10836 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837 if (!error && strval != NULL)
10838 set_option_value(varname, numval, strval, OPT_LOCAL);
10839
10840 /* reset notion of buffer */
10841 aucmd_restbuf(&aco);
10842 }
10843 else
10844 {
10845 buf_T *save_curbuf = curbuf;
10846
10847 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10848 if (bufvarname != NULL)
10849 {
10850 curbuf = buf;
10851 STRCPY(bufvarname, "b:");
10852 STRCPY(bufvarname + 2, varname);
10853 set_var(bufvarname, varp, TRUE);
10854 vim_free(bufvarname);
10855 curbuf = save_curbuf;
10856 }
10857 }
10858 }
10859}
10860
10861 static void
10862f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10863{
10864 dict_T *d;
10865 dictitem_T *di;
10866 char_u *csearch;
10867
10868 if (argvars[0].v_type != VAR_DICT)
10869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010870 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010871 return;
10872 }
10873
10874 if ((d = argvars[0].vval.v_dict) != NULL)
10875 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010876 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010877 if (csearch != NULL)
10878 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010879 if (enc_utf8)
10880 {
10881 int pcc[MAX_MCO];
10882 int c = utfc_ptr2char(csearch, pcc);
10883
10884 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10885 }
10886 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010887 set_last_csearch(PTR2CHAR(csearch),
10888 csearch, MB_PTR2LEN(csearch));
10889 }
10890
10891 di = dict_find(d, (char_u *)"forward", -1);
10892 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010893 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894 ? FORWARD : BACKWARD);
10895
10896 di = dict_find(d, (char_u *)"until", -1);
10897 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 }
10900}
10901
10902/*
10903 * "setcmdpos()" function
10904 */
10905 static void
10906f_setcmdpos(typval_T *argvars, typval_T *rettv)
10907{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010908 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010909
10910 if (pos >= 0)
10911 rettv->vval.v_number = set_cmdline_pos(pos);
10912}
10913
10914/*
10915 * "setfperm({fname}, {mode})" function
10916 */
10917 static void
10918f_setfperm(typval_T *argvars, typval_T *rettv)
10919{
10920 char_u *fname;
10921 char_u modebuf[NUMBUFLEN];
10922 char_u *mode_str;
10923 int i;
10924 int mask;
10925 int mode = 0;
10926
10927 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010928 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 if (fname == NULL)
10930 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010931 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 if (mode_str == NULL)
10933 return;
10934 if (STRLEN(mode_str) != 9)
10935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010936 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010937 return;
10938 }
10939
10940 mask = 1;
10941 for (i = 8; i >= 0; --i)
10942 {
10943 if (mode_str[i] != '-')
10944 mode |= mask;
10945 mask = mask << 1;
10946 }
10947 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10948}
10949
10950/*
10951 * "setline()" function
10952 */
10953 static void
10954f_setline(typval_T *argvars, typval_T *rettv)
10955{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010956 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957
Bram Moolenaarca851592018-06-06 21:04:07 +020010958 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959}
10960
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010961/*
10962 * Used by "setqflist()" and "setloclist()" functions
10963 */
10964 static void
10965set_qf_ll_list(
10966 win_T *wp UNUSED,
10967 typval_T *list_arg UNUSED,
10968 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010969 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970 typval_T *rettv)
10971{
10972#ifdef FEAT_QUICKFIX
10973 static char *e_invact = N_("E927: Invalid action: '%s'");
10974 char_u *act;
10975 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010976 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977#endif
10978
10979 rettv->vval.v_number = -1;
10980
10981#ifdef FEAT_QUICKFIX
10982 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010983 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010984 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010985 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 else
10987 {
10988 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010989 dict_T *d = NULL;
10990 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991
10992 if (action_arg->v_type == VAR_STRING)
10993 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010994 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 if (act == NULL)
10996 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020010997 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
10998 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 action = *act;
11000 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011002 }
11003 else if (action_arg->v_type == VAR_UNKNOWN)
11004 action = ' ';
11005 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011006 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007
Bram Moolenaard823fa92016-08-12 16:29:27 +020011008 if (action_arg->v_type != VAR_UNKNOWN
11009 && what_arg->v_type != VAR_UNKNOWN)
11010 {
11011 if (what_arg->v_type == VAR_DICT)
11012 d = what_arg->vval.v_dict;
11013 else
11014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011015 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011016 valid_dict = FALSE;
11017 }
11018 }
11019
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011020 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011021 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011022 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11023 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011025 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011026 }
11027#endif
11028}
11029
11030/*
11031 * "setloclist()" function
11032 */
11033 static void
11034f_setloclist(typval_T *argvars, typval_T *rettv)
11035{
11036 win_T *win;
11037
11038 rettv->vval.v_number = -1;
11039
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011040 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011042 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043}
11044
11045/*
11046 * "setmatches()" function
11047 */
11048 static void
11049f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11050{
11051#ifdef FEAT_SEARCH_EXTRA
11052 list_T *l;
11053 listitem_T *li;
11054 dict_T *d;
11055 list_T *s = NULL;
11056
11057 rettv->vval.v_number = -1;
11058 if (argvars[0].v_type != VAR_LIST)
11059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011060 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 return;
11062 }
11063 if ((l = argvars[0].vval.v_list) != NULL)
11064 {
11065
11066 /* To some extent make sure that we are dealing with a list from
11067 * "getmatches()". */
11068 li = l->lv_first;
11069 while (li != NULL)
11070 {
11071 if (li->li_tv.v_type != VAR_DICT
11072 || (d = li->li_tv.vval.v_dict) == NULL)
11073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011074 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011075 return;
11076 }
11077 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11078 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11079 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11080 && dict_find(d, (char_u *)"priority", -1) != NULL
11081 && dict_find(d, (char_u *)"id", -1) != NULL))
11082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011083 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 return;
11085 }
11086 li = li->li_next;
11087 }
11088
11089 clear_matches(curwin);
11090 li = l->lv_first;
11091 while (li != NULL)
11092 {
11093 int i = 0;
11094 char_u buf[5];
11095 dictitem_T *di;
11096 char_u *group;
11097 int priority;
11098 int id;
11099 char_u *conceal;
11100
11101 d = li->li_tv.vval.v_dict;
11102 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11103 {
11104 if (s == NULL)
11105 {
11106 s = list_alloc();
11107 if (s == NULL)
11108 return;
11109 }
11110
11111 /* match from matchaddpos() */
11112 for (i = 1; i < 9; i++)
11113 {
11114 sprintf((char *)buf, (char *)"pos%d", i);
11115 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11116 {
11117 if (di->di_tv.v_type != VAR_LIST)
11118 return;
11119
11120 list_append_tv(s, &di->di_tv);
11121 s->lv_refcount++;
11122 }
11123 else
11124 break;
11125 }
11126 }
11127
Bram Moolenaar8f667172018-12-14 15:38:31 +010011128 group = dict_get_string(d, (char_u *)"group", TRUE);
11129 priority = (int)dict_get_number(d, (char_u *)"priority");
11130 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011132 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133 : NULL;
11134 if (i == 0)
11135 {
11136 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011137 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 priority, id, NULL, conceal);
11139 }
11140 else
11141 {
11142 match_add(curwin, group, NULL, priority, id, s, conceal);
11143 list_unref(s);
11144 s = NULL;
11145 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011146 vim_free(group);
11147 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148
11149 li = li->li_next;
11150 }
11151 rettv->vval.v_number = 0;
11152 }
11153#endif
11154}
11155
11156/*
11157 * "setpos()" function
11158 */
11159 static void
11160f_setpos(typval_T *argvars, typval_T *rettv)
11161{
11162 pos_T pos;
11163 int fnum;
11164 char_u *name;
11165 colnr_T curswant = -1;
11166
11167 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011168 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011169 if (name != NULL)
11170 {
11171 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11172 {
11173 if (--pos.col < 0)
11174 pos.col = 0;
11175 if (name[0] == '.' && name[1] == NUL)
11176 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011177 /* set cursor; "fnum" is ignored */
11178 curwin->w_cursor = pos;
11179 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011180 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011181 curwin->w_curswant = curswant - 1;
11182 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011183 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011184 check_cursor();
11185 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011186 }
11187 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11188 {
11189 /* set mark */
11190 if (setmark_pos(name[1], &pos, fnum) == OK)
11191 rettv->vval.v_number = 0;
11192 }
11193 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011194 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 }
11196 }
11197}
11198
11199/*
11200 * "setqflist()" function
11201 */
11202 static void
11203f_setqflist(typval_T *argvars, typval_T *rettv)
11204{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011205 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206}
11207
11208/*
11209 * "setreg()" function
11210 */
11211 static void
11212f_setreg(typval_T *argvars, typval_T *rettv)
11213{
11214 int regname;
11215 char_u *strregname;
11216 char_u *stropt;
11217 char_u *strval;
11218 int append;
11219 char_u yank_type;
11220 long block_len;
11221
11222 block_len = -1;
11223 yank_type = MAUTO;
11224 append = FALSE;
11225
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011226 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011227 rettv->vval.v_number = 1; /* FAIL is default */
11228
11229 if (strregname == NULL)
11230 return; /* type error; errmsg already given */
11231 regname = *strregname;
11232 if (regname == 0 || regname == '@')
11233 regname = '"';
11234
11235 if (argvars[2].v_type != VAR_UNKNOWN)
11236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011237 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011238 if (stropt == NULL)
11239 return; /* type error */
11240 for (; *stropt != NUL; ++stropt)
11241 switch (*stropt)
11242 {
11243 case 'a': case 'A': /* append */
11244 append = TRUE;
11245 break;
11246 case 'v': case 'c': /* character-wise selection */
11247 yank_type = MCHAR;
11248 break;
11249 case 'V': case 'l': /* line-wise selection */
11250 yank_type = MLINE;
11251 break;
11252 case 'b': case Ctrl_V: /* block-wise selection */
11253 yank_type = MBLOCK;
11254 if (VIM_ISDIGIT(stropt[1]))
11255 {
11256 ++stropt;
11257 block_len = getdigits(&stropt) - 1;
11258 --stropt;
11259 }
11260 break;
11261 }
11262 }
11263
11264 if (argvars[1].v_type == VAR_LIST)
11265 {
11266 char_u **lstval;
11267 char_u **allocval;
11268 char_u buf[NUMBUFLEN];
11269 char_u **curval;
11270 char_u **curallocval;
11271 list_T *ll = argvars[1].vval.v_list;
11272 listitem_T *li;
11273 int len;
11274
11275 /* If the list is NULL handle like an empty list. */
11276 len = ll == NULL ? 0 : ll->lv_len;
11277
11278 /* First half: use for pointers to result lines; second half: use for
11279 * pointers to allocated copies. */
11280 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11281 if (lstval == NULL)
11282 return;
11283 curval = lstval;
11284 allocval = lstval + len + 2;
11285 curallocval = allocval;
11286
11287 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11288 li = li->li_next)
11289 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011290 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011291 if (strval == NULL)
11292 goto free_lstval;
11293 if (strval == buf)
11294 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011295 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011296 * overwrite the string. */
11297 strval = vim_strsave(buf);
11298 if (strval == NULL)
11299 goto free_lstval;
11300 *curallocval++ = strval;
11301 }
11302 *curval++ = strval;
11303 }
11304 *curval++ = NULL;
11305
11306 write_reg_contents_lst(regname, lstval, -1,
11307 append, yank_type, block_len);
11308free_lstval:
11309 while (curallocval > allocval)
11310 vim_free(*--curallocval);
11311 vim_free(lstval);
11312 }
11313 else
11314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011315 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011316 if (strval == NULL)
11317 return;
11318 write_reg_contents_ex(regname, strval, -1,
11319 append, yank_type, block_len);
11320 }
11321 rettv->vval.v_number = 0;
11322}
11323
11324/*
11325 * "settabvar()" function
11326 */
11327 static void
11328f_settabvar(typval_T *argvars, typval_T *rettv)
11329{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 tabpage_T *save_curtab;
11331 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 char_u *varname, *tabvarname;
11333 typval_T *varp;
11334
11335 rettv->vval.v_number = 0;
11336
11337 if (check_restricted() || check_secure())
11338 return;
11339
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011340 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11341 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011342 varp = &argvars[2];
11343
Bram Moolenaar4033c552017-09-16 20:54:51 +020011344 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011345 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011346 save_curtab = curtab;
11347 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011348
11349 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11350 if (tabvarname != NULL)
11351 {
11352 STRCPY(tabvarname, "t:");
11353 STRCPY(tabvarname + 2, varname);
11354 set_var(tabvarname, varp, TRUE);
11355 vim_free(tabvarname);
11356 }
11357
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011358 /* Restore current tabpage */
11359 if (valid_tabpage(save_curtab))
11360 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 }
11362}
11363
11364/*
11365 * "settabwinvar()" function
11366 */
11367 static void
11368f_settabwinvar(typval_T *argvars, typval_T *rettv)
11369{
11370 setwinvar(argvars, rettv, 1);
11371}
11372
11373/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011374 * "settagstack()" function
11375 */
11376 static void
11377f_settagstack(typval_T *argvars, typval_T *rettv)
11378{
11379 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11380 win_T *wp;
11381 dict_T *d;
11382 int action = 'r';
11383
11384 rettv->vval.v_number = -1;
11385
11386 // first argument: window number or id
11387 wp = find_win_by_nr_or_id(&argvars[0]);
11388 if (wp == NULL)
11389 return;
11390
11391 // second argument: dict with items to set in the tag stack
11392 if (argvars[1].v_type != VAR_DICT)
11393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011394 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011395 return;
11396 }
11397 d = argvars[1].vval.v_dict;
11398 if (d == NULL)
11399 return;
11400
11401 // third argument: action - 'a' for append and 'r' for replace.
11402 // default is to replace the stack.
11403 if (argvars[2].v_type == VAR_UNKNOWN)
11404 action = 'r';
11405 else if (argvars[2].v_type == VAR_STRING)
11406 {
11407 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011408 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011409 if (actstr == NULL)
11410 return;
11411 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11412 action = *actstr;
11413 else
11414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011415 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011416 return;
11417 }
11418 }
11419 else
11420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011421 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011422 return;
11423 }
11424
11425 if (set_tagstack(wp, d, action) == OK)
11426 rettv->vval.v_number = 0;
11427}
11428
11429/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 * "setwinvar()" function
11431 */
11432 static void
11433f_setwinvar(typval_T *argvars, typval_T *rettv)
11434{
11435 setwinvar(argvars, rettv, 0);
11436}
11437
11438#ifdef FEAT_CRYPT
11439/*
11440 * "sha256({string})" function
11441 */
11442 static void
11443f_sha256(typval_T *argvars, typval_T *rettv)
11444{
11445 char_u *p;
11446
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011447 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 rettv->vval.v_string = vim_strsave(
11449 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11450 rettv->v_type = VAR_STRING;
11451}
11452#endif /* FEAT_CRYPT */
11453
11454/*
11455 * "shellescape({string})" function
11456 */
11457 static void
11458f_shellescape(typval_T *argvars, typval_T *rettv)
11459{
Bram Moolenaar20615522017-06-05 18:46:26 +020011460 int do_special = non_zero_arg(&argvars[1]);
11461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011462 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011463 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011464 rettv->v_type = VAR_STRING;
11465}
11466
11467/*
11468 * shiftwidth() function
11469 */
11470 static void
11471f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11472{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011473 rettv->vval.v_number = 0;
11474
11475 if (argvars[0].v_type != VAR_UNKNOWN)
11476 {
11477 long col;
11478
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011479 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011480 if (col < 0)
11481 return; // type error; errmsg already given
11482#ifdef FEAT_VARTABS
11483 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11484 return;
11485#endif
11486 }
11487
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488 rettv->vval.v_number = get_sw_value(curbuf);
11489}
11490
Bram Moolenaar162b7142018-12-21 15:17:36 +010011491#ifdef FEAT_SIGNS
11492/*
11493 * "sign_define()" function
11494 */
11495 static void
11496f_sign_define(typval_T *argvars, typval_T *rettv)
11497{
11498 char_u *name;
11499 dict_T *dict;
11500 char_u *icon = NULL;
11501 char_u *linehl = NULL;
11502 char_u *text = NULL;
11503 char_u *texthl = NULL;
11504
11505 rettv->vval.v_number = -1;
11506
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011507 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011508 if (name == NULL)
11509 return;
11510
11511 if (argvars[1].v_type != VAR_UNKNOWN)
11512 {
11513 if (argvars[1].v_type != VAR_DICT)
11514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011515 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011516 return;
11517 }
11518
11519 // sign attributes
11520 dict = argvars[1].vval.v_dict;
11521 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11522 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11523 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11524 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11525 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11526 text = dict_get_string(dict, (char_u *)"text", TRUE);
11527 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11528 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11529 }
11530
11531 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11532 rettv->vval.v_number = 0;
11533
11534 vim_free(icon);
11535 vim_free(linehl);
11536 vim_free(text);
11537 vim_free(texthl);
11538}
11539
11540/*
11541 * "sign_getdefined()" function
11542 */
11543 static void
11544f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11545{
11546 char_u *name = NULL;
11547
11548 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11549 return;
11550
11551 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011552 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011553
11554 sign_getlist(name, rettv->vval.v_list);
11555}
11556
11557/*
11558 * "sign_getplaced()" function
11559 */
11560 static void
11561f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11562{
11563 buf_T *buf = NULL;
11564 dict_T *dict;
11565 dictitem_T *di;
11566 linenr_T lnum = 0;
11567 int sign_id = 0;
11568 char_u *group = NULL;
11569 int notanum = FALSE;
11570
11571 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11572 return;
11573
11574 if (argvars[0].v_type != VAR_UNKNOWN)
11575 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011576 // get signs placed in the specified buffer
11577 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011578 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011579 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011580
11581 if (argvars[1].v_type != VAR_UNKNOWN)
11582 {
11583 if (argvars[1].v_type != VAR_DICT ||
11584 ((dict = argvars[1].vval.v_dict) == NULL))
11585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011586 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011587 return;
11588 }
11589 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11590 {
11591 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011592 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011593 if (notanum)
11594 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011595 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011596 }
11597 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11598 {
11599 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011600 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011601 if (notanum)
11602 return;
11603 }
11604 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11605 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011606 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011607 if (group == NULL)
11608 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011609 if (*group == '\0') // empty string means global group
11610 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011611 }
11612 }
11613 }
11614
11615 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11616}
11617
11618/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011619 * "sign_jump()" function
11620 */
11621 static void
11622f_sign_jump(typval_T *argvars, typval_T *rettv)
11623{
11624 int sign_id;
11625 char_u *sign_group = NULL;
11626 buf_T *buf;
11627 int notanum = FALSE;
11628
11629 rettv->vval.v_number = -1;
11630
11631 // Sign identifer
11632 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11633 if (notanum)
11634 return;
11635 if (sign_id <= 0)
11636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011637 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011638 return;
11639 }
11640
11641 // Sign group
11642 sign_group = tv_get_string_chk(&argvars[1]);
11643 if (sign_group == NULL)
11644 return;
11645 if (sign_group[0] == '\0')
11646 sign_group = NULL; // global sign group
11647 else
11648 {
11649 sign_group = vim_strsave(sign_group);
11650 if (sign_group == NULL)
11651 return;
11652 }
11653
11654 // Buffer to place the sign
11655 buf = get_buf_arg(&argvars[2]);
11656 if (buf == NULL)
11657 goto cleanup;
11658
11659 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11660
11661cleanup:
11662 vim_free(sign_group);
11663}
11664
11665/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011666 * "sign_place()" function
11667 */
11668 static void
11669f_sign_place(typval_T *argvars, typval_T *rettv)
11670{
11671 int sign_id;
11672 char_u *group = NULL;
11673 char_u *sign_name;
11674 buf_T *buf;
11675 dict_T *dict;
11676 dictitem_T *di;
11677 linenr_T lnum = 0;
11678 int prio = SIGN_DEF_PRIO;
11679 int notanum = FALSE;
11680
11681 rettv->vval.v_number = -1;
11682
11683 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011684 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011685 if (notanum)
11686 return;
11687 if (sign_id < 0)
11688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011689 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011690 return;
11691 }
11692
11693 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011694 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011695 if (group == NULL)
11696 return;
11697 if (group[0] == '\0')
11698 group = NULL; // global sign group
11699 else
11700 {
11701 group = vim_strsave(group);
11702 if (group == NULL)
11703 return;
11704 }
11705
11706 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011707 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011708 if (sign_name == NULL)
11709 goto cleanup;
11710
11711 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011712 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011713 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011714 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011715
11716 if (argvars[4].v_type != VAR_UNKNOWN)
11717 {
11718 if (argvars[4].v_type != VAR_DICT ||
11719 ((dict = argvars[4].vval.v_dict) == NULL))
11720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011721 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011722 goto cleanup;
11723 }
11724
11725 // Line number where the sign is to be placed
11726 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011728 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011729 if (notanum)
11730 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011731 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011732 }
11733 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11734 {
11735 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011736 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011737 if (notanum)
11738 goto cleanup;
11739 }
11740 }
11741
11742 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11743 rettv->vval.v_number = sign_id;
11744
11745cleanup:
11746 vim_free(group);
11747}
11748
11749/*
11750 * "sign_undefine()" function
11751 */
11752 static void
11753f_sign_undefine(typval_T *argvars, typval_T *rettv)
11754{
11755 char_u *name;
11756
11757 rettv->vval.v_number = -1;
11758
11759 if (argvars[0].v_type == VAR_UNKNOWN)
11760 {
11761 // Free all the signs
11762 free_signs();
11763 rettv->vval.v_number = 0;
11764 }
11765 else
11766 {
11767 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011768 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011769 if (name == NULL)
11770 return;
11771
11772 if (sign_undefine_by_name(name) == OK)
11773 rettv->vval.v_number = 0;
11774 }
11775}
11776
11777/*
11778 * "sign_unplace()" function
11779 */
11780 static void
11781f_sign_unplace(typval_T *argvars, typval_T *rettv)
11782{
11783 dict_T *dict;
11784 dictitem_T *di;
11785 int sign_id = 0;
11786 buf_T *buf = NULL;
11787 char_u *group = NULL;
11788
11789 rettv->vval.v_number = -1;
11790
11791 if (argvars[0].v_type != VAR_STRING)
11792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011793 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011794 return;
11795 }
11796
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011797 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011798 if (group[0] == '\0')
11799 group = NULL; // global sign group
11800 else
11801 {
11802 group = vim_strsave(group);
11803 if (group == NULL)
11804 return;
11805 }
11806
11807 if (argvars[1].v_type != VAR_UNKNOWN)
11808 {
11809 if (argvars[1].v_type != VAR_DICT)
11810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011811 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011812 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011813 }
11814 dict = argvars[1].vval.v_dict;
11815
11816 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11817 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011818 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011819 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011820 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011821 }
11822 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11823 sign_id = dict_get_number(dict, (char_u *)"id");
11824 }
11825
11826 if (buf == NULL)
11827 {
11828 // Delete the sign in all the buffers
11829 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011830 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011831 rettv->vval.v_number = 0;
11832 }
11833 else
11834 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011835 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011836 rettv->vval.v_number = 0;
11837 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011838
11839cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011840 vim_free(group);
11841}
11842#endif
11843
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011844/*
11845 * "simplify()" function
11846 */
11847 static void
11848f_simplify(typval_T *argvars, typval_T *rettv)
11849{
11850 char_u *p;
11851
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011852 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853 rettv->vval.v_string = vim_strsave(p);
11854 simplify_filename(rettv->vval.v_string); /* simplify in place */
11855 rettv->v_type = VAR_STRING;
11856}
11857
11858#ifdef FEAT_FLOAT
11859/*
11860 * "sin()" function
11861 */
11862 static void
11863f_sin(typval_T *argvars, typval_T *rettv)
11864{
11865 float_T f = 0.0;
11866
11867 rettv->v_type = VAR_FLOAT;
11868 if (get_float_arg(argvars, &f) == OK)
11869 rettv->vval.v_float = sin(f);
11870 else
11871 rettv->vval.v_float = 0.0;
11872}
11873
11874/*
11875 * "sinh()" function
11876 */
11877 static void
11878f_sinh(typval_T *argvars, typval_T *rettv)
11879{
11880 float_T f = 0.0;
11881
11882 rettv->v_type = VAR_FLOAT;
11883 if (get_float_arg(argvars, &f) == OK)
11884 rettv->vval.v_float = sinh(f);
11885 else
11886 rettv->vval.v_float = 0.0;
11887}
11888#endif
11889
11890static int
11891#ifdef __BORLANDC__
11892 _RTLENTRYF
11893#endif
11894 item_compare(const void *s1, const void *s2);
11895static int
11896#ifdef __BORLANDC__
11897 _RTLENTRYF
11898#endif
11899 item_compare2(const void *s1, const void *s2);
11900
11901/* struct used in the array that's given to qsort() */
11902typedef struct
11903{
11904 listitem_T *item;
11905 int idx;
11906} sortItem_T;
11907
11908/* struct storing information about current sort */
11909typedef struct
11910{
11911 int item_compare_ic;
11912 int item_compare_numeric;
11913 int item_compare_numbers;
11914#ifdef FEAT_FLOAT
11915 int item_compare_float;
11916#endif
11917 char_u *item_compare_func;
11918 partial_T *item_compare_partial;
11919 dict_T *item_compare_selfdict;
11920 int item_compare_func_err;
11921 int item_compare_keep_zero;
11922} sortinfo_T;
11923static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011924#define ITEM_COMPARE_FAIL 999
11925
11926/*
11927 * Compare functions for f_sort() and f_uniq() below.
11928 */
11929 static int
11930#ifdef __BORLANDC__
11931_RTLENTRYF
11932#endif
11933item_compare(const void *s1, const void *s2)
11934{
11935 sortItem_T *si1, *si2;
11936 typval_T *tv1, *tv2;
11937 char_u *p1, *p2;
11938 char_u *tofree1 = NULL, *tofree2 = NULL;
11939 int res;
11940 char_u numbuf1[NUMBUFLEN];
11941 char_u numbuf2[NUMBUFLEN];
11942
11943 si1 = (sortItem_T *)s1;
11944 si2 = (sortItem_T *)s2;
11945 tv1 = &si1->item->li_tv;
11946 tv2 = &si2->item->li_tv;
11947
11948 if (sortinfo->item_compare_numbers)
11949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011950 varnumber_T v1 = tv_get_number(tv1);
11951 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011952
11953 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11954 }
11955
11956#ifdef FEAT_FLOAT
11957 if (sortinfo->item_compare_float)
11958 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011959 float_T v1 = tv_get_float(tv1);
11960 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961
11962 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11963 }
11964#endif
11965
11966 /* tv2string() puts quotes around a string and allocates memory. Don't do
11967 * that for string variables. Use a single quote when comparing with a
11968 * non-string to do what the docs promise. */
11969 if (tv1->v_type == VAR_STRING)
11970 {
11971 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11972 p1 = (char_u *)"'";
11973 else
11974 p1 = tv1->vval.v_string;
11975 }
11976 else
11977 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11978 if (tv2->v_type == VAR_STRING)
11979 {
11980 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11981 p2 = (char_u *)"'";
11982 else
11983 p2 = tv2->vval.v_string;
11984 }
11985 else
11986 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11987 if (p1 == NULL)
11988 p1 = (char_u *)"";
11989 if (p2 == NULL)
11990 p2 = (char_u *)"";
11991 if (!sortinfo->item_compare_numeric)
11992 {
11993 if (sortinfo->item_compare_ic)
11994 res = STRICMP(p1, p2);
11995 else
11996 res = STRCMP(p1, p2);
11997 }
11998 else
11999 {
12000 double n1, n2;
12001 n1 = strtod((char *)p1, (char **)&p1);
12002 n2 = strtod((char *)p2, (char **)&p2);
12003 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12004 }
12005
12006 /* When the result would be zero, compare the item indexes. Makes the
12007 * sort stable. */
12008 if (res == 0 && !sortinfo->item_compare_keep_zero)
12009 res = si1->idx > si2->idx ? 1 : -1;
12010
12011 vim_free(tofree1);
12012 vim_free(tofree2);
12013 return res;
12014}
12015
12016 static int
12017#ifdef __BORLANDC__
12018_RTLENTRYF
12019#endif
12020item_compare2(const void *s1, const void *s2)
12021{
12022 sortItem_T *si1, *si2;
12023 int res;
12024 typval_T rettv;
12025 typval_T argv[3];
12026 int dummy;
12027 char_u *func_name;
12028 partial_T *partial = sortinfo->item_compare_partial;
12029
12030 /* shortcut after failure in previous call; compare all items equal */
12031 if (sortinfo->item_compare_func_err)
12032 return 0;
12033
12034 si1 = (sortItem_T *)s1;
12035 si2 = (sortItem_T *)s2;
12036
12037 if (partial == NULL)
12038 func_name = sortinfo->item_compare_func;
12039 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012040 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012041
12042 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12043 * in the copy without changing the original list items. */
12044 copy_tv(&si1->item->li_tv, &argv[0]);
12045 copy_tv(&si2->item->li_tv, &argv[1]);
12046
12047 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12048 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012049 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050 partial, sortinfo->item_compare_selfdict);
12051 clear_tv(&argv[0]);
12052 clear_tv(&argv[1]);
12053
12054 if (res == FAIL)
12055 res = ITEM_COMPARE_FAIL;
12056 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012057 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012058 if (sortinfo->item_compare_func_err)
12059 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12060 clear_tv(&rettv);
12061
12062 /* When the result would be zero, compare the pointers themselves. Makes
12063 * the sort stable. */
12064 if (res == 0 && !sortinfo->item_compare_keep_zero)
12065 res = si1->idx > si2->idx ? 1 : -1;
12066
12067 return res;
12068}
12069
12070/*
12071 * "sort({list})" function
12072 */
12073 static void
12074do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12075{
12076 list_T *l;
12077 listitem_T *li;
12078 sortItem_T *ptrs;
12079 sortinfo_T *old_sortinfo;
12080 sortinfo_T info;
12081 long len;
12082 long i;
12083
12084 /* Pointer to current info struct used in compare function. Save and
12085 * restore the current one for nested calls. */
12086 old_sortinfo = sortinfo;
12087 sortinfo = &info;
12088
12089 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012090 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012091 else
12092 {
12093 l = argvars[0].vval.v_list;
12094 if (l == NULL || tv_check_lock(l->lv_lock,
12095 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12096 TRUE))
12097 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012098 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012099
12100 len = list_len(l);
12101 if (len <= 1)
12102 goto theend; /* short list sorts pretty quickly */
12103
12104 info.item_compare_ic = FALSE;
12105 info.item_compare_numeric = FALSE;
12106 info.item_compare_numbers = FALSE;
12107#ifdef FEAT_FLOAT
12108 info.item_compare_float = FALSE;
12109#endif
12110 info.item_compare_func = NULL;
12111 info.item_compare_partial = NULL;
12112 info.item_compare_selfdict = NULL;
12113 if (argvars[1].v_type != VAR_UNKNOWN)
12114 {
12115 /* optional second argument: {func} */
12116 if (argvars[1].v_type == VAR_FUNC)
12117 info.item_compare_func = argvars[1].vval.v_string;
12118 else if (argvars[1].v_type == VAR_PARTIAL)
12119 info.item_compare_partial = argvars[1].vval.v_partial;
12120 else
12121 {
12122 int error = FALSE;
12123
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012124 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012125 if (error)
12126 goto theend; /* type error; errmsg already given */
12127 if (i == 1)
12128 info.item_compare_ic = TRUE;
12129 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012130 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012131 else if (i != 0)
12132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012133 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012134 goto theend;
12135 }
12136 if (info.item_compare_func != NULL)
12137 {
12138 if (*info.item_compare_func == NUL)
12139 {
12140 /* empty string means default sort */
12141 info.item_compare_func = NULL;
12142 }
12143 else if (STRCMP(info.item_compare_func, "n") == 0)
12144 {
12145 info.item_compare_func = NULL;
12146 info.item_compare_numeric = TRUE;
12147 }
12148 else if (STRCMP(info.item_compare_func, "N") == 0)
12149 {
12150 info.item_compare_func = NULL;
12151 info.item_compare_numbers = TRUE;
12152 }
12153#ifdef FEAT_FLOAT
12154 else if (STRCMP(info.item_compare_func, "f") == 0)
12155 {
12156 info.item_compare_func = NULL;
12157 info.item_compare_float = TRUE;
12158 }
12159#endif
12160 else if (STRCMP(info.item_compare_func, "i") == 0)
12161 {
12162 info.item_compare_func = NULL;
12163 info.item_compare_ic = TRUE;
12164 }
12165 }
12166 }
12167
12168 if (argvars[2].v_type != VAR_UNKNOWN)
12169 {
12170 /* optional third argument: {dict} */
12171 if (argvars[2].v_type != VAR_DICT)
12172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012173 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012174 goto theend;
12175 }
12176 info.item_compare_selfdict = argvars[2].vval.v_dict;
12177 }
12178 }
12179
12180 /* Make an array with each entry pointing to an item in the List. */
12181 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12182 if (ptrs == NULL)
12183 goto theend;
12184
12185 i = 0;
12186 if (sort)
12187 {
12188 /* sort(): ptrs will be the list to sort */
12189 for (li = l->lv_first; li != NULL; li = li->li_next)
12190 {
12191 ptrs[i].item = li;
12192 ptrs[i].idx = i;
12193 ++i;
12194 }
12195
12196 info.item_compare_func_err = FALSE;
12197 info.item_compare_keep_zero = FALSE;
12198 /* test the compare function */
12199 if ((info.item_compare_func != NULL
12200 || info.item_compare_partial != NULL)
12201 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12202 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012203 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012204 else
12205 {
12206 /* Sort the array with item pointers. */
12207 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12208 info.item_compare_func == NULL
12209 && info.item_compare_partial == NULL
12210 ? item_compare : item_compare2);
12211
12212 if (!info.item_compare_func_err)
12213 {
12214 /* Clear the List and append the items in sorted order. */
12215 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12216 l->lv_len = 0;
12217 for (i = 0; i < len; ++i)
12218 list_append(l, ptrs[i].item);
12219 }
12220 }
12221 }
12222 else
12223 {
12224 int (*item_compare_func_ptr)(const void *, const void *);
12225
12226 /* f_uniq(): ptrs will be a stack of items to remove */
12227 info.item_compare_func_err = FALSE;
12228 info.item_compare_keep_zero = TRUE;
12229 item_compare_func_ptr = info.item_compare_func != NULL
12230 || info.item_compare_partial != NULL
12231 ? item_compare2 : item_compare;
12232
12233 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12234 li = li->li_next)
12235 {
12236 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12237 == 0)
12238 ptrs[i++].item = li;
12239 if (info.item_compare_func_err)
12240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012241 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012242 break;
12243 }
12244 }
12245
12246 if (!info.item_compare_func_err)
12247 {
12248 while (--i >= 0)
12249 {
12250 li = ptrs[i].item->li_next;
12251 ptrs[i].item->li_next = li->li_next;
12252 if (li->li_next != NULL)
12253 li->li_next->li_prev = ptrs[i].item;
12254 else
12255 l->lv_last = ptrs[i].item;
12256 list_fix_watch(l, li);
12257 listitem_free(li);
12258 l->lv_len--;
12259 }
12260 }
12261 }
12262
12263 vim_free(ptrs);
12264 }
12265theend:
12266 sortinfo = old_sortinfo;
12267}
12268
12269/*
12270 * "sort({list})" function
12271 */
12272 static void
12273f_sort(typval_T *argvars, typval_T *rettv)
12274{
12275 do_sort_uniq(argvars, rettv, TRUE);
12276}
12277
12278/*
12279 * "uniq({list})" function
12280 */
12281 static void
12282f_uniq(typval_T *argvars, typval_T *rettv)
12283{
12284 do_sort_uniq(argvars, rettv, FALSE);
12285}
12286
12287/*
12288 * "soundfold({word})" function
12289 */
12290 static void
12291f_soundfold(typval_T *argvars, typval_T *rettv)
12292{
12293 char_u *s;
12294
12295 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012296 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012297#ifdef FEAT_SPELL
12298 rettv->vval.v_string = eval_soundfold(s);
12299#else
12300 rettv->vval.v_string = vim_strsave(s);
12301#endif
12302}
12303
12304/*
12305 * "spellbadword()" function
12306 */
12307 static void
12308f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12309{
12310 char_u *word = (char_u *)"";
12311 hlf_T attr = HLF_COUNT;
12312 int len = 0;
12313
12314 if (rettv_list_alloc(rettv) == FAIL)
12315 return;
12316
12317#ifdef FEAT_SPELL
12318 if (argvars[0].v_type == VAR_UNKNOWN)
12319 {
12320 /* Find the start and length of the badly spelled word. */
12321 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12322 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012323 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012324 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012325 curwin->w_set_curswant = TRUE;
12326 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012327 }
12328 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012330 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012331 int capcol = -1;
12332
12333 if (str != NULL)
12334 {
12335 /* Check the argument for spelling. */
12336 while (*str != NUL)
12337 {
12338 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12339 if (attr != HLF_COUNT)
12340 {
12341 word = str;
12342 break;
12343 }
12344 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012345 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012346 }
12347 }
12348 }
12349#endif
12350
12351 list_append_string(rettv->vval.v_list, word, len);
12352 list_append_string(rettv->vval.v_list, (char_u *)(
12353 attr == HLF_SPB ? "bad" :
12354 attr == HLF_SPR ? "rare" :
12355 attr == HLF_SPL ? "local" :
12356 attr == HLF_SPC ? "caps" :
12357 ""), -1);
12358}
12359
12360/*
12361 * "spellsuggest()" function
12362 */
12363 static void
12364f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12365{
12366#ifdef FEAT_SPELL
12367 char_u *str;
12368 int typeerr = FALSE;
12369 int maxcount;
12370 garray_T ga;
12371 int i;
12372 listitem_T *li;
12373 int need_capital = FALSE;
12374#endif
12375
12376 if (rettv_list_alloc(rettv) == FAIL)
12377 return;
12378
12379#ifdef FEAT_SPELL
12380 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12381 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012382 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012383 if (argvars[1].v_type != VAR_UNKNOWN)
12384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012385 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012386 if (maxcount <= 0)
12387 return;
12388 if (argvars[2].v_type != VAR_UNKNOWN)
12389 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012390 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012391 if (typeerr)
12392 return;
12393 }
12394 }
12395 else
12396 maxcount = 25;
12397
12398 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12399
12400 for (i = 0; i < ga.ga_len; ++i)
12401 {
12402 str = ((char_u **)ga.ga_data)[i];
12403
12404 li = listitem_alloc();
12405 if (li == NULL)
12406 vim_free(str);
12407 else
12408 {
12409 li->li_tv.v_type = VAR_STRING;
12410 li->li_tv.v_lock = 0;
12411 li->li_tv.vval.v_string = str;
12412 list_append(rettv->vval.v_list, li);
12413 }
12414 }
12415 ga_clear(&ga);
12416 }
12417#endif
12418}
12419
12420 static void
12421f_split(typval_T *argvars, typval_T *rettv)
12422{
12423 char_u *str;
12424 char_u *end;
12425 char_u *pat = NULL;
12426 regmatch_T regmatch;
12427 char_u patbuf[NUMBUFLEN];
12428 char_u *save_cpo;
12429 int match;
12430 colnr_T col = 0;
12431 int keepempty = FALSE;
12432 int typeerr = FALSE;
12433
12434 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12435 save_cpo = p_cpo;
12436 p_cpo = (char_u *)"";
12437
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012438 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012439 if (argvars[1].v_type != VAR_UNKNOWN)
12440 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012441 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012442 if (pat == NULL)
12443 typeerr = TRUE;
12444 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012445 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012446 }
12447 if (pat == NULL || *pat == NUL)
12448 pat = (char_u *)"[\\x01- ]\\+";
12449
12450 if (rettv_list_alloc(rettv) == FAIL)
12451 return;
12452 if (typeerr)
12453 return;
12454
12455 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12456 if (regmatch.regprog != NULL)
12457 {
12458 regmatch.rm_ic = FALSE;
12459 while (*str != NUL || keepempty)
12460 {
12461 if (*str == NUL)
12462 match = FALSE; /* empty item at the end */
12463 else
12464 match = vim_regexec_nl(&regmatch, str, col);
12465 if (match)
12466 end = regmatch.startp[0];
12467 else
12468 end = str + STRLEN(str);
12469 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12470 && *str != NUL && match && end < regmatch.endp[0]))
12471 {
12472 if (list_append_string(rettv->vval.v_list, str,
12473 (int)(end - str)) == FAIL)
12474 break;
12475 }
12476 if (!match)
12477 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012478 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479 if (regmatch.endp[0] > str)
12480 col = 0;
12481 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012482 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012483 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484 str = regmatch.endp[0];
12485 }
12486
12487 vim_regfree(regmatch.regprog);
12488 }
12489
12490 p_cpo = save_cpo;
12491}
12492
12493#ifdef FEAT_FLOAT
12494/*
12495 * "sqrt()" function
12496 */
12497 static void
12498f_sqrt(typval_T *argvars, typval_T *rettv)
12499{
12500 float_T f = 0.0;
12501
12502 rettv->v_type = VAR_FLOAT;
12503 if (get_float_arg(argvars, &f) == OK)
12504 rettv->vval.v_float = sqrt(f);
12505 else
12506 rettv->vval.v_float = 0.0;
12507}
12508
12509/*
12510 * "str2float()" function
12511 */
12512 static void
12513f_str2float(typval_T *argvars, typval_T *rettv)
12514{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012515 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012516 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012517
Bram Moolenaar08243d22017-01-10 16:12:29 +010012518 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012519 p = skipwhite(p + 1);
12520 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012521 if (isneg)
12522 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523 rettv->v_type = VAR_FLOAT;
12524}
12525#endif
12526
12527/*
12528 * "str2nr()" function
12529 */
12530 static void
12531f_str2nr(typval_T *argvars, typval_T *rettv)
12532{
12533 int base = 10;
12534 char_u *p;
12535 varnumber_T n;
12536 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012537 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012538
12539 if (argvars[1].v_type != VAR_UNKNOWN)
12540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012541 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012542 if (base != 2 && base != 8 && base != 10 && base != 16)
12543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012544 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 return;
12546 }
12547 }
12548
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012549 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012550 isneg = (*p == '-');
12551 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 p = skipwhite(p + 1);
12553 switch (base)
12554 {
12555 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12556 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12557 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12558 default: what = 0;
12559 }
12560 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012561 if (isneg)
12562 rettv->vval.v_number = -n;
12563 else
12564 rettv->vval.v_number = n;
12565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012566}
12567
12568#ifdef HAVE_STRFTIME
12569/*
12570 * "strftime({format}[, {time}])" function
12571 */
12572 static void
12573f_strftime(typval_T *argvars, typval_T *rettv)
12574{
12575 char_u result_buf[256];
12576 struct tm *curtime;
12577 time_t seconds;
12578 char_u *p;
12579
12580 rettv->v_type = VAR_STRING;
12581
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012582 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012583 if (argvars[1].v_type == VAR_UNKNOWN)
12584 seconds = time(NULL);
12585 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012586 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 curtime = localtime(&seconds);
12588 /* MSVC returns NULL for an invalid value of seconds. */
12589 if (curtime == NULL)
12590 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12591 else
12592 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012593 vimconv_T conv;
12594 char_u *enc;
12595
12596 conv.vc_type = CONV_NONE;
12597 enc = enc_locale();
12598 convert_setup(&conv, p_enc, enc);
12599 if (conv.vc_type != CONV_NONE)
12600 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012601 if (p != NULL)
12602 (void)strftime((char *)result_buf, sizeof(result_buf),
12603 (char *)p, curtime);
12604 else
12605 result_buf[0] = NUL;
12606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012607 if (conv.vc_type != CONV_NONE)
12608 vim_free(p);
12609 convert_setup(&conv, enc, p_enc);
12610 if (conv.vc_type != CONV_NONE)
12611 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12612 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012613 rettv->vval.v_string = vim_strsave(result_buf);
12614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012615 /* Release conversion descriptors */
12616 convert_setup(&conv, NULL, NULL);
12617 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 }
12619}
12620#endif
12621
12622/*
12623 * "strgetchar()" function
12624 */
12625 static void
12626f_strgetchar(typval_T *argvars, typval_T *rettv)
12627{
12628 char_u *str;
12629 int len;
12630 int error = FALSE;
12631 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012632 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012633
12634 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012635 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012636 if (str == NULL)
12637 return;
12638 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012639 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012640 if (error)
12641 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012642
Bram Moolenaar13505972019-01-24 15:04:48 +010012643 while (charidx >= 0 && byteidx < len)
12644 {
12645 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012646 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012647 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12648 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012650 --charidx;
12651 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012652 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653}
12654
12655/*
12656 * "stridx()" function
12657 */
12658 static void
12659f_stridx(typval_T *argvars, typval_T *rettv)
12660{
12661 char_u buf[NUMBUFLEN];
12662 char_u *needle;
12663 char_u *haystack;
12664 char_u *save_haystack;
12665 char_u *pos;
12666 int start_idx;
12667
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012668 needle = tv_get_string_chk(&argvars[1]);
12669 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012670 rettv->vval.v_number = -1;
12671 if (needle == NULL || haystack == NULL)
12672 return; /* type error; errmsg already given */
12673
12674 if (argvars[2].v_type != VAR_UNKNOWN)
12675 {
12676 int error = FALSE;
12677
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012678 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679 if (error || start_idx >= (int)STRLEN(haystack))
12680 return;
12681 if (start_idx >= 0)
12682 haystack += start_idx;
12683 }
12684
12685 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12686 if (pos != NULL)
12687 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12688}
12689
12690/*
12691 * "string()" function
12692 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012693 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694f_string(typval_T *argvars, typval_T *rettv)
12695{
12696 char_u *tofree;
12697 char_u numbuf[NUMBUFLEN];
12698
12699 rettv->v_type = VAR_STRING;
12700 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12701 get_copyID());
12702 /* Make a copy if we have a value but it's not in allocated memory. */
12703 if (rettv->vval.v_string != NULL && tofree == NULL)
12704 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12705}
12706
12707/*
12708 * "strlen()" function
12709 */
12710 static void
12711f_strlen(typval_T *argvars, typval_T *rettv)
12712{
12713 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012714 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012715}
12716
12717/*
12718 * "strchars()" function
12719 */
12720 static void
12721f_strchars(typval_T *argvars, typval_T *rettv)
12722{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012723 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012724 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012725 varnumber_T len = 0;
12726 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012727
12728 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012729 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012730 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012731 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012732 else
12733 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12735 while (*s != NUL)
12736 {
12737 func_mb_ptr2char_adv(&s);
12738 ++len;
12739 }
12740 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 }
12742}
12743
12744/*
12745 * "strdisplaywidth()" function
12746 */
12747 static void
12748f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12749{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012750 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012751 int col = 0;
12752
12753 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012754 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012755
12756 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12757}
12758
12759/*
12760 * "strwidth()" function
12761 */
12762 static void
12763f_strwidth(typval_T *argvars, typval_T *rettv)
12764{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012765 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012766
Bram Moolenaar13505972019-01-24 15:04:48 +010012767 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012768}
12769
12770/*
12771 * "strcharpart()" function
12772 */
12773 static void
12774f_strcharpart(typval_T *argvars, typval_T *rettv)
12775{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 char_u *p;
12777 int nchar;
12778 int nbyte = 0;
12779 int charlen;
12780 int len = 0;
12781 int slen;
12782 int error = FALSE;
12783
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012784 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012785 slen = (int)STRLEN(p);
12786
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012787 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012788 if (!error)
12789 {
12790 if (nchar > 0)
12791 while (nchar > 0 && nbyte < slen)
12792 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012793 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 --nchar;
12795 }
12796 else
12797 nbyte = nchar;
12798 if (argvars[2].v_type != VAR_UNKNOWN)
12799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012800 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801 while (charlen > 0 && nbyte + len < slen)
12802 {
12803 int off = nbyte + len;
12804
12805 if (off < 0)
12806 len += 1;
12807 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012808 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809 --charlen;
12810 }
12811 }
12812 else
12813 len = slen - nbyte; /* default: all bytes that are available. */
12814 }
12815
12816 /*
12817 * Only return the overlap between the specified part and the actual
12818 * string.
12819 */
12820 if (nbyte < 0)
12821 {
12822 len += nbyte;
12823 nbyte = 0;
12824 }
12825 else if (nbyte > slen)
12826 nbyte = slen;
12827 if (len < 0)
12828 len = 0;
12829 else if (nbyte + len > slen)
12830 len = slen - nbyte;
12831
12832 rettv->v_type = VAR_STRING;
12833 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012834}
12835
12836/*
12837 * "strpart()" function
12838 */
12839 static void
12840f_strpart(typval_T *argvars, typval_T *rettv)
12841{
12842 char_u *p;
12843 int n;
12844 int len;
12845 int slen;
12846 int error = FALSE;
12847
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012848 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012849 slen = (int)STRLEN(p);
12850
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012851 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012852 if (error)
12853 len = 0;
12854 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012855 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856 else
12857 len = slen - n; /* default len: all bytes that are available. */
12858
12859 /*
12860 * Only return the overlap between the specified part and the actual
12861 * string.
12862 */
12863 if (n < 0)
12864 {
12865 len += n;
12866 n = 0;
12867 }
12868 else if (n > slen)
12869 n = slen;
12870 if (len < 0)
12871 len = 0;
12872 else if (n + len > slen)
12873 len = slen - n;
12874
12875 rettv->v_type = VAR_STRING;
12876 rettv->vval.v_string = vim_strnsave(p + n, len);
12877}
12878
12879/*
12880 * "strridx()" function
12881 */
12882 static void
12883f_strridx(typval_T *argvars, typval_T *rettv)
12884{
12885 char_u buf[NUMBUFLEN];
12886 char_u *needle;
12887 char_u *haystack;
12888 char_u *rest;
12889 char_u *lastmatch = NULL;
12890 int haystack_len, end_idx;
12891
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012892 needle = tv_get_string_chk(&argvars[1]);
12893 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894
12895 rettv->vval.v_number = -1;
12896 if (needle == NULL || haystack == NULL)
12897 return; /* type error; errmsg already given */
12898
12899 haystack_len = (int)STRLEN(haystack);
12900 if (argvars[2].v_type != VAR_UNKNOWN)
12901 {
12902 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012903 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012904 if (end_idx < 0)
12905 return; /* can never find a match */
12906 }
12907 else
12908 end_idx = haystack_len;
12909
12910 if (*needle == NUL)
12911 {
12912 /* Empty string matches past the end. */
12913 lastmatch = haystack + end_idx;
12914 }
12915 else
12916 {
12917 for (rest = haystack; *rest != '\0'; ++rest)
12918 {
12919 rest = (char_u *)strstr((char *)rest, (char *)needle);
12920 if (rest == NULL || rest > haystack + end_idx)
12921 break;
12922 lastmatch = rest;
12923 }
12924 }
12925
12926 if (lastmatch == NULL)
12927 rettv->vval.v_number = -1;
12928 else
12929 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12930}
12931
12932/*
12933 * "strtrans()" function
12934 */
12935 static void
12936f_strtrans(typval_T *argvars, typval_T *rettv)
12937{
12938 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012939 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012940}
12941
12942/*
12943 * "submatch()" function
12944 */
12945 static void
12946f_submatch(typval_T *argvars, typval_T *rettv)
12947{
12948 int error = FALSE;
12949 int no;
12950 int retList = 0;
12951
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012952 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012953 if (error)
12954 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012955 if (no < 0 || no >= NSUBEXP)
12956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012957 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012958 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012959 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012960 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012961 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012962 if (error)
12963 return;
12964
12965 if (retList == 0)
12966 {
12967 rettv->v_type = VAR_STRING;
12968 rettv->vval.v_string = reg_submatch(no);
12969 }
12970 else
12971 {
12972 rettv->v_type = VAR_LIST;
12973 rettv->vval.v_list = reg_submatch_list(no);
12974 }
12975}
12976
12977/*
12978 * "substitute()" function
12979 */
12980 static void
12981f_substitute(typval_T *argvars, typval_T *rettv)
12982{
12983 char_u patbuf[NUMBUFLEN];
12984 char_u subbuf[NUMBUFLEN];
12985 char_u flagsbuf[NUMBUFLEN];
12986
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012987 char_u *str = tv_get_string_chk(&argvars[0]);
12988 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012989 char_u *sub = NULL;
12990 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012991 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012992
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012993 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
12994 expr = &argvars[2];
12995 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012996 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012998 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012999 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13000 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013001 rettv->vval.v_string = NULL;
13002 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013003 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013004}
13005
13006/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013007 * "swapinfo(swap_filename)" function
13008 */
13009 static void
13010f_swapinfo(typval_T *argvars, typval_T *rettv)
13011{
13012 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013013 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013014}
13015
13016/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013017 * "swapname(expr)" function
13018 */
13019 static void
13020f_swapname(typval_T *argvars, typval_T *rettv)
13021{
13022 buf_T *buf;
13023
13024 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013025 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013026 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13027 || buf->b_ml.ml_mfp->mf_fname == NULL)
13028 rettv->vval.v_string = NULL;
13029 else
13030 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13031}
13032
13033/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013034 * "synID(lnum, col, trans)" function
13035 */
13036 static void
13037f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13038{
13039 int id = 0;
13040#ifdef FEAT_SYN_HL
13041 linenr_T lnum;
13042 colnr_T col;
13043 int trans;
13044 int transerr = FALSE;
13045
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013046 lnum = tv_get_lnum(argvars); /* -1 on type error */
13047 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13048 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049
13050 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13051 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13052 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13053#endif
13054
13055 rettv->vval.v_number = id;
13056}
13057
13058/*
13059 * "synIDattr(id, what [, mode])" function
13060 */
13061 static void
13062f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13063{
13064 char_u *p = NULL;
13065#ifdef FEAT_SYN_HL
13066 int id;
13067 char_u *what;
13068 char_u *mode;
13069 char_u modebuf[NUMBUFLEN];
13070 int modec;
13071
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013072 id = (int)tv_get_number(&argvars[0]);
13073 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074 if (argvars[2].v_type != VAR_UNKNOWN)
13075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013076 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077 modec = TOLOWER_ASC(mode[0]);
13078 if (modec != 't' && modec != 'c' && modec != 'g')
13079 modec = 0; /* replace invalid with current */
13080 }
13081 else
13082 {
13083#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13084 if (USE_24BIT)
13085 modec = 'g';
13086 else
13087#endif
13088 if (t_colors > 1)
13089 modec = 'c';
13090 else
13091 modec = 't';
13092 }
13093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013094 switch (TOLOWER_ASC(what[0]))
13095 {
13096 case 'b':
13097 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13098 p = highlight_color(id, what, modec);
13099 else /* bold */
13100 p = highlight_has_attr(id, HL_BOLD, modec);
13101 break;
13102
13103 case 'f': /* fg[#] or font */
13104 p = highlight_color(id, what, modec);
13105 break;
13106
13107 case 'i':
13108 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13109 p = highlight_has_attr(id, HL_INVERSE, modec);
13110 else /* italic */
13111 p = highlight_has_attr(id, HL_ITALIC, modec);
13112 break;
13113
13114 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013115 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 break;
13117
13118 case 'r': /* reverse */
13119 p = highlight_has_attr(id, HL_INVERSE, modec);
13120 break;
13121
13122 case 's':
13123 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13124 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013125 /* strikeout */
13126 else if (TOLOWER_ASC(what[1]) == 't' &&
13127 TOLOWER_ASC(what[2]) == 'r')
13128 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 else /* standout */
13130 p = highlight_has_attr(id, HL_STANDOUT, modec);
13131 break;
13132
13133 case 'u':
13134 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13135 /* underline */
13136 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13137 else
13138 /* undercurl */
13139 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13140 break;
13141 }
13142
13143 if (p != NULL)
13144 p = vim_strsave(p);
13145#endif
13146 rettv->v_type = VAR_STRING;
13147 rettv->vval.v_string = p;
13148}
13149
13150/*
13151 * "synIDtrans(id)" function
13152 */
13153 static void
13154f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13155{
13156 int id;
13157
13158#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013159 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013160
13161 if (id > 0)
13162 id = syn_get_final_id(id);
13163 else
13164#endif
13165 id = 0;
13166
13167 rettv->vval.v_number = id;
13168}
13169
13170/*
13171 * "synconcealed(lnum, col)" function
13172 */
13173 static void
13174f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13175{
13176#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13177 linenr_T lnum;
13178 colnr_T col;
13179 int syntax_flags = 0;
13180 int cchar;
13181 int matchid = 0;
13182 char_u str[NUMBUFLEN];
13183#endif
13184
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013185 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186
13187#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013188 lnum = tv_get_lnum(argvars); /* -1 on type error */
13189 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190
13191 vim_memset(str, NUL, sizeof(str));
13192
13193 if (rettv_list_alloc(rettv) != FAIL)
13194 {
13195 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13196 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13197 && curwin->w_p_cole > 0)
13198 {
13199 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13200 syntax_flags = get_syntax_info(&matchid);
13201
13202 /* get the conceal character */
13203 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13204 {
13205 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013206 if (cchar == NUL && curwin->w_p_cole == 1)
13207 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 if (cchar != NUL)
13209 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 if (has_mbyte)
13211 (*mb_char2bytes)(cchar, str);
13212 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213 str[0] = cchar;
13214 }
13215 }
13216 }
13217
13218 list_append_number(rettv->vval.v_list,
13219 (syntax_flags & HL_CONCEAL) != 0);
13220 /* -1 to auto-determine strlen */
13221 list_append_string(rettv->vval.v_list, str, -1);
13222 list_append_number(rettv->vval.v_list, matchid);
13223 }
13224#endif
13225}
13226
13227/*
13228 * "synstack(lnum, col)" function
13229 */
13230 static void
13231f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13232{
13233#ifdef FEAT_SYN_HL
13234 linenr_T lnum;
13235 colnr_T col;
13236 int i;
13237 int id;
13238#endif
13239
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013240 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241
13242#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013243 lnum = tv_get_lnum(argvars); /* -1 on type error */
13244 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245
13246 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13247 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13248 && rettv_list_alloc(rettv) != FAIL)
13249 {
13250 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13251 for (i = 0; ; ++i)
13252 {
13253 id = syn_get_stack_item(i);
13254 if (id < 0)
13255 break;
13256 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13257 break;
13258 }
13259 }
13260#endif
13261}
13262
13263 static void
13264get_cmd_output_as_rettv(
13265 typval_T *argvars,
13266 typval_T *rettv,
13267 int retlist)
13268{
13269 char_u *res = NULL;
13270 char_u *p;
13271 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272 int err = FALSE;
13273 FILE *fd;
13274 list_T *list = NULL;
13275 int flags = SHELL_SILENT;
13276
13277 rettv->v_type = VAR_STRING;
13278 rettv->vval.v_string = NULL;
13279 if (check_restricted() || check_secure())
13280 goto errret;
13281
13282 if (argvars[1].v_type != VAR_UNKNOWN)
13283 {
13284 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013285 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013286 * command.
13287 */
13288 if ((infile = vim_tempname('i', TRUE)) == NULL)
13289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013290 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291 goto errret;
13292 }
13293
13294 fd = mch_fopen((char *)infile, WRITEBIN);
13295 if (fd == NULL)
13296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013297 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 goto errret;
13299 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013300 if (argvars[1].v_type == VAR_NUMBER)
13301 {
13302 linenr_T lnum;
13303 buf_T *buf;
13304
13305 buf = buflist_findnr(argvars[1].vval.v_number);
13306 if (buf == NULL)
13307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013308 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013309 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013310 goto errret;
13311 }
13312
13313 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13314 {
13315 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13316 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13317 {
13318 err = TRUE;
13319 break;
13320 }
13321 if (putc(NL, fd) == EOF)
13322 {
13323 err = TRUE;
13324 break;
13325 }
13326 }
13327 }
13328 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 {
13330 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13331 err = TRUE;
13332 }
13333 else
13334 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013335 size_t len;
13336 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013338 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 if (p == NULL)
13340 {
13341 fclose(fd);
13342 goto errret; /* type error; errmsg already given */
13343 }
13344 len = STRLEN(p);
13345 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13346 err = TRUE;
13347 }
13348 if (fclose(fd) != 0)
13349 err = TRUE;
13350 if (err)
13351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013352 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 goto errret;
13354 }
13355 }
13356
13357 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13358 * echoes typeahead, that messes up the display. */
13359 if (!msg_silent)
13360 flags += SHELL_COOKED;
13361
13362 if (retlist)
13363 {
13364 int len;
13365 listitem_T *li;
13366 char_u *s = NULL;
13367 char_u *start;
13368 char_u *end;
13369 int i;
13370
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013371 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372 if (res == NULL)
13373 goto errret;
13374
13375 list = list_alloc();
13376 if (list == NULL)
13377 goto errret;
13378
13379 for (i = 0; i < len; ++i)
13380 {
13381 start = res + i;
13382 while (i < len && res[i] != NL)
13383 ++i;
13384 end = res + i;
13385
13386 s = alloc((unsigned)(end - start + 1));
13387 if (s == NULL)
13388 goto errret;
13389
13390 for (p = s; start < end; ++p, ++start)
13391 *p = *start == NUL ? NL : *start;
13392 *p = NUL;
13393
13394 li = listitem_alloc();
13395 if (li == NULL)
13396 {
13397 vim_free(s);
13398 goto errret;
13399 }
13400 li->li_tv.v_type = VAR_STRING;
13401 li->li_tv.v_lock = 0;
13402 li->li_tv.vval.v_string = s;
13403 list_append(list, li);
13404 }
13405
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013406 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013407 list = NULL;
13408 }
13409 else
13410 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013411 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412#ifdef USE_CR
13413 /* translate <CR> into <NL> */
13414 if (res != NULL)
13415 {
13416 char_u *s;
13417
13418 for (s = res; *s; ++s)
13419 {
13420 if (*s == CAR)
13421 *s = NL;
13422 }
13423 }
13424#else
13425# ifdef USE_CRNL
13426 /* translate <CR><NL> into <NL> */
13427 if (res != NULL)
13428 {
13429 char_u *s, *d;
13430
13431 d = res;
13432 for (s = res; *s; ++s)
13433 {
13434 if (s[0] == CAR && s[1] == NL)
13435 ++s;
13436 *d++ = *s;
13437 }
13438 *d = NUL;
13439 }
13440# endif
13441#endif
13442 rettv->vval.v_string = res;
13443 res = NULL;
13444 }
13445
13446errret:
13447 if (infile != NULL)
13448 {
13449 mch_remove(infile);
13450 vim_free(infile);
13451 }
13452 if (res != NULL)
13453 vim_free(res);
13454 if (list != NULL)
13455 list_free(list);
13456}
13457
13458/*
13459 * "system()" function
13460 */
13461 static void
13462f_system(typval_T *argvars, typval_T *rettv)
13463{
13464 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13465}
13466
13467/*
13468 * "systemlist()" function
13469 */
13470 static void
13471f_systemlist(typval_T *argvars, typval_T *rettv)
13472{
13473 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13474}
13475
13476/*
13477 * "tabpagebuflist()" function
13478 */
13479 static void
13480f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13481{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013482 tabpage_T *tp;
13483 win_T *wp = NULL;
13484
13485 if (argvars[0].v_type == VAR_UNKNOWN)
13486 wp = firstwin;
13487 else
13488 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013489 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013490 if (tp != NULL)
13491 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13492 }
13493 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13494 {
13495 for (; wp != NULL; wp = wp->w_next)
13496 if (list_append_number(rettv->vval.v_list,
13497 wp->w_buffer->b_fnum) == FAIL)
13498 break;
13499 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013500}
13501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502/*
13503 * "tabpagenr()" function
13504 */
13505 static void
13506f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13507{
13508 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013509 char_u *arg;
13510
13511 if (argvars[0].v_type != VAR_UNKNOWN)
13512 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013513 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013514 nr = 0;
13515 if (arg != NULL)
13516 {
13517 if (STRCMP(arg, "$") == 0)
13518 nr = tabpage_index(NULL) - 1;
13519 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013520 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 }
13522 }
13523 else
13524 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013525 rettv->vval.v_number = nr;
13526}
13527
13528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013529/*
13530 * Common code for tabpagewinnr() and winnr().
13531 */
13532 static int
13533get_winnr(tabpage_T *tp, typval_T *argvar)
13534{
13535 win_T *twin;
13536 int nr = 1;
13537 win_T *wp;
13538 char_u *arg;
13539
13540 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13541 if (argvar->v_type != VAR_UNKNOWN)
13542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013543 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 if (arg == NULL)
13545 nr = 0; /* type error; errmsg already given */
13546 else if (STRCMP(arg, "$") == 0)
13547 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13548 else if (STRCMP(arg, "#") == 0)
13549 {
13550 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13551 if (twin == NULL)
13552 nr = 0;
13553 }
13554 else
13555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013556 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013557 nr = 0;
13558 }
13559 }
13560
13561 if (nr > 0)
13562 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13563 wp != twin; wp = wp->w_next)
13564 {
13565 if (wp == NULL)
13566 {
13567 /* didn't find it in this tabpage */
13568 nr = 0;
13569 break;
13570 }
13571 ++nr;
13572 }
13573 return nr;
13574}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013575
13576/*
13577 * "tabpagewinnr()" function
13578 */
13579 static void
13580f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13581{
13582 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583 tabpage_T *tp;
13584
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013585 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013586 if (tp == NULL)
13587 nr = 0;
13588 else
13589 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013590 rettv->vval.v_number = nr;
13591}
13592
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013593/*
13594 * "tagfiles()" function
13595 */
13596 static void
13597f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13598{
13599 char_u *fname;
13600 tagname_T tn;
13601 int first;
13602
13603 if (rettv_list_alloc(rettv) == FAIL)
13604 return;
13605 fname = alloc(MAXPATHL);
13606 if (fname == NULL)
13607 return;
13608
13609 for (first = TRUE; ; first = FALSE)
13610 if (get_tagfname(&tn, first, fname) == FAIL
13611 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13612 break;
13613 tagname_free(&tn);
13614 vim_free(fname);
13615}
13616
13617/*
13618 * "taglist()" function
13619 */
13620 static void
13621f_taglist(typval_T *argvars, typval_T *rettv)
13622{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013623 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013624 char_u *tag_pattern;
13625
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013626 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013627
13628 rettv->vval.v_number = FALSE;
13629 if (*tag_pattern == NUL)
13630 return;
13631
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013632 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013633 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013634 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013635 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636}
13637
13638/*
13639 * "tempname()" function
13640 */
13641 static void
13642f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13643{
13644 static int x = 'A';
13645
13646 rettv->v_type = VAR_STRING;
13647 rettv->vval.v_string = vim_tempname(x, FALSE);
13648
13649 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13650 * names. Skip 'I' and 'O', they are used for shell redirection. */
13651 do
13652 {
13653 if (x == 'Z')
13654 x = '0';
13655 else if (x == '9')
13656 x = 'A';
13657 else
13658 {
13659#ifdef EBCDIC
13660 if (x == 'I')
13661 x = 'J';
13662 else if (x == 'R')
13663 x = 'S';
13664 else
13665#endif
13666 ++x;
13667 }
13668 } while (x == 'I' || x == 'O');
13669}
13670
13671#ifdef FEAT_FLOAT
13672/*
13673 * "tan()" function
13674 */
13675 static void
13676f_tan(typval_T *argvars, typval_T *rettv)
13677{
13678 float_T f = 0.0;
13679
13680 rettv->v_type = VAR_FLOAT;
13681 if (get_float_arg(argvars, &f) == OK)
13682 rettv->vval.v_float = tan(f);
13683 else
13684 rettv->vval.v_float = 0.0;
13685}
13686
13687/*
13688 * "tanh()" function
13689 */
13690 static void
13691f_tanh(typval_T *argvars, typval_T *rettv)
13692{
13693 float_T f = 0.0;
13694
13695 rettv->v_type = VAR_FLOAT;
13696 if (get_float_arg(argvars, &f) == OK)
13697 rettv->vval.v_float = tanh(f);
13698 else
13699 rettv->vval.v_float = 0.0;
13700}
13701#endif
13702
13703/*
13704 * "test_alloc_fail(id, countdown, repeat)" function
13705 */
13706 static void
13707f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13708{
13709 if (argvars[0].v_type != VAR_NUMBER
13710 || argvars[0].vval.v_number <= 0
13711 || argvars[1].v_type != VAR_NUMBER
13712 || argvars[1].vval.v_number < 0
13713 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013714 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013715 else
13716 {
13717 alloc_fail_id = argvars[0].vval.v_number;
13718 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013719 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013720 alloc_fail_countdown = argvars[1].vval.v_number;
13721 alloc_fail_repeat = argvars[2].vval.v_number;
13722 did_outofmem_msg = FALSE;
13723 }
13724}
13725
13726/*
13727 * "test_autochdir()"
13728 */
13729 static void
13730f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13731{
13732#if defined(FEAT_AUTOCHDIR)
13733 test_autochdir = TRUE;
13734#endif
13735}
13736
13737/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013738 * "test_feedinput()"
13739 */
13740 static void
13741f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13742{
13743#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013744 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013745
13746 if (val != NULL)
13747 {
13748 trash_input_buf();
13749 add_to_input_buf_csi(val, (int)STRLEN(val));
13750 }
13751#endif
13752}
13753
13754/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013755 * "test_option_not_set({name})" function
13756 */
13757 static void
13758f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13759{
13760 char_u *name = (char_u *)"";
13761
13762 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013763 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013764 else
13765 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013766 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013767 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013768 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013769 }
13770}
13771
13772/*
13773 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013774 */
13775 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013776f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013777{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013778 char_u *name = (char_u *)"";
13779 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013780 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013781
13782 if (argvars[0].v_type != VAR_STRING
13783 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013784 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013785 else
13786 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013787 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013788 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013789
13790 if (STRCMP(name, (char_u *)"redraw") == 0)
13791 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013792 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13793 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013794 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13795 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013796 else if (STRCMP(name, (char_u *)"starting") == 0)
13797 {
13798 if (val)
13799 {
13800 if (save_starting < 0)
13801 save_starting = starting;
13802 starting = 0;
13803 }
13804 else
13805 {
13806 starting = save_starting;
13807 save_starting = -1;
13808 }
13809 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013810 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13811 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013812 else if (STRCMP(name, (char_u *)"ALL") == 0)
13813 {
13814 disable_char_avail_for_testing = FALSE;
13815 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013816 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013817 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013818 if (save_starting >= 0)
13819 {
13820 starting = save_starting;
13821 save_starting = -1;
13822 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013823 }
13824 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013825 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013826 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013827}
13828
13829/*
13830 * "test_garbagecollect_now()" function
13831 */
13832 static void
13833f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13834{
13835 /* This is dangerous, any Lists and Dicts used internally may be freed
13836 * while still in use. */
13837 garbage_collect(TRUE);
13838}
13839
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013840/*
13841 * "test_ignore_error()" function
13842 */
13843 static void
13844f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13845{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013846 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013847}
13848
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013849 static void
13850f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13851{
13852 rettv->v_type = VAR_BLOB;
13853 rettv->vval.v_blob = NULL;
13854}
13855
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013856#ifdef FEAT_JOB_CHANNEL
13857 static void
13858f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13859{
13860 rettv->v_type = VAR_CHANNEL;
13861 rettv->vval.v_channel = NULL;
13862}
13863#endif
13864
13865 static void
13866f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13867{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013868 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013869}
13870
13871#ifdef FEAT_JOB_CHANNEL
13872 static void
13873f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13874{
13875 rettv->v_type = VAR_JOB;
13876 rettv->vval.v_job = NULL;
13877}
13878#endif
13879
13880 static void
13881f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13882{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013883 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013884}
13885
13886 static void
13887f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13888{
13889 rettv->v_type = VAR_PARTIAL;
13890 rettv->vval.v_partial = NULL;
13891}
13892
13893 static void
13894f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13895{
13896 rettv->v_type = VAR_STRING;
13897 rettv->vval.v_string = NULL;
13898}
13899
Bram Moolenaarab186732018-09-14 21:27:06 +020013900#ifdef FEAT_GUI
13901 static void
13902f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13903{
13904 char_u *which;
13905 long value;
13906 int dragging;
13907 scrollbar_T *sb = NULL;
13908
13909 if (argvars[0].v_type != VAR_STRING
13910 || (argvars[1].v_type) != VAR_NUMBER
13911 || (argvars[2].v_type) != VAR_NUMBER)
13912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013913 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013914 return;
13915 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013916 which = tv_get_string(&argvars[0]);
13917 value = tv_get_number(&argvars[1]);
13918 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013919
13920 if (STRCMP(which, "left") == 0)
13921 sb = &curwin->w_scrollbars[SBAR_LEFT];
13922 else if (STRCMP(which, "right") == 0)
13923 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13924 else if (STRCMP(which, "hor") == 0)
13925 sb = &gui.bottom_sbar;
13926 if (sb == NULL)
13927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013928 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013929 return;
13930 }
13931 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013932# ifndef USE_ON_FLY_SCROLL
13933 // need to loop through normal_cmd() to handle the scroll events
13934 exec_normal(FALSE, TRUE, FALSE);
13935# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013936}
13937#endif
13938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013939 static void
13940f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13941{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013942 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013943}
13944
13945#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13946/*
13947 * Get a callback from "arg". It can be a Funcref or a function name.
13948 * When "arg" is zero return an empty string.
13949 * Return NULL for an invalid argument.
13950 */
13951 char_u *
13952get_callback(typval_T *arg, partial_T **pp)
13953{
13954 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13955 {
13956 *pp = arg->vval.v_partial;
13957 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013958 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013959 }
13960 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013961 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013962 {
13963 func_ref(arg->vval.v_string);
13964 return arg->vval.v_string;
13965 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13967 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013968 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013969 return NULL;
13970}
13971
13972/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013973 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974 */
13975 void
13976free_callback(char_u *callback, partial_T *partial)
13977{
13978 if (partial != NULL)
13979 partial_unref(partial);
13980 else if (callback != NULL)
13981 {
13982 func_unref(callback);
13983 vim_free(callback);
13984 }
13985}
13986#endif
13987
13988#ifdef FEAT_TIMERS
13989/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013990 * "timer_info([timer])" function
13991 */
13992 static void
13993f_timer_info(typval_T *argvars, typval_T *rettv)
13994{
13995 timer_T *timer = NULL;
13996
13997 if (rettv_list_alloc(rettv) != OK)
13998 return;
13999 if (argvars[0].v_type != VAR_UNKNOWN)
14000 {
14001 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014002 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014003 else
14004 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014005 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014006 if (timer != NULL)
14007 add_timer_info(rettv, timer);
14008 }
14009 }
14010 else
14011 add_timer_info_all(rettv);
14012}
14013
14014/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014015 * "timer_pause(timer, paused)" function
14016 */
14017 static void
14018f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14019{
14020 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014021 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014022
14023 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014024 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014025 else
14026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014027 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014028 if (timer != NULL)
14029 timer->tr_paused = paused;
14030 }
14031}
14032
14033/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 * "timer_start(time, callback [, options])" function
14035 */
14036 static void
14037f_timer_start(typval_T *argvars, typval_T *rettv)
14038{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014039 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014040 timer_T *timer;
14041 int repeat = 0;
14042 char_u *callback;
14043 dict_T *dict;
14044 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045
Bram Moolenaar75537a92016-09-05 22:45:28 +020014046 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014047 if (check_secure())
14048 return;
14049 if (argvars[2].v_type != VAR_UNKNOWN)
14050 {
14051 if (argvars[2].v_type != VAR_DICT
14052 || (dict = argvars[2].vval.v_dict) == NULL)
14053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014054 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 return;
14056 }
14057 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014058 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014059 }
14060
Bram Moolenaar75537a92016-09-05 22:45:28 +020014061 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014062 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014063 return;
14064
14065 timer = create_timer(msec, repeat);
14066 if (timer == NULL)
14067 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068 else
14069 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014070 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014071 timer->tr_callback = vim_strsave(callback);
14072 else
14073 /* pointer into the partial */
14074 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014075 timer->tr_partial = partial;
14076 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 }
14078}
14079
14080/*
14081 * "timer_stop(timer)" function
14082 */
14083 static void
14084f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14085{
14086 timer_T *timer;
14087
14088 if (argvars[0].v_type != VAR_NUMBER)
14089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014090 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091 return;
14092 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014093 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094 if (timer != NULL)
14095 stop_timer(timer);
14096}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014097
14098/*
14099 * "timer_stopall()" function
14100 */
14101 static void
14102f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14103{
14104 stop_all_timers();
14105}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014106#endif
14107
14108/*
14109 * "tolower(string)" function
14110 */
14111 static void
14112f_tolower(typval_T *argvars, typval_T *rettv)
14113{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014115 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116}
14117
14118/*
14119 * "toupper(string)" function
14120 */
14121 static void
14122f_toupper(typval_T *argvars, typval_T *rettv)
14123{
14124 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014125 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014126}
14127
14128/*
14129 * "tr(string, fromstr, tostr)" function
14130 */
14131 static void
14132f_tr(typval_T *argvars, typval_T *rettv)
14133{
14134 char_u *in_str;
14135 char_u *fromstr;
14136 char_u *tostr;
14137 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138 int inlen;
14139 int fromlen;
14140 int tolen;
14141 int idx;
14142 char_u *cpstr;
14143 int cplen;
14144 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145 char_u buf[NUMBUFLEN];
14146 char_u buf2[NUMBUFLEN];
14147 garray_T ga;
14148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014149 in_str = tv_get_string(&argvars[0]);
14150 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14151 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014152
14153 /* Default return value: empty string. */
14154 rettv->v_type = VAR_STRING;
14155 rettv->vval.v_string = NULL;
14156 if (fromstr == NULL || tostr == NULL)
14157 return; /* type error; errmsg already given */
14158 ga_init2(&ga, (int)sizeof(char), 80);
14159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014160 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161 /* not multi-byte: fromstr and tostr must be the same length */
14162 if (STRLEN(fromstr) != STRLEN(tostr))
14163 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014165 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014166 ga_clear(&ga);
14167 return;
14168 }
14169
14170 /* fromstr and tostr have to contain the same number of chars */
14171 while (*in_str != NUL)
14172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014173 if (has_mbyte)
14174 {
14175 inlen = (*mb_ptr2len)(in_str);
14176 cpstr = in_str;
14177 cplen = inlen;
14178 idx = 0;
14179 for (p = fromstr; *p != NUL; p += fromlen)
14180 {
14181 fromlen = (*mb_ptr2len)(p);
14182 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14183 {
14184 for (p = tostr; *p != NUL; p += tolen)
14185 {
14186 tolen = (*mb_ptr2len)(p);
14187 if (idx-- == 0)
14188 {
14189 cplen = tolen;
14190 cpstr = p;
14191 break;
14192 }
14193 }
14194 if (*p == NUL) /* tostr is shorter than fromstr */
14195 goto error;
14196 break;
14197 }
14198 ++idx;
14199 }
14200
14201 if (first && cpstr == in_str)
14202 {
14203 /* Check that fromstr and tostr have the same number of
14204 * (multi-byte) characters. Done only once when a character
14205 * of in_str doesn't appear in fromstr. */
14206 first = FALSE;
14207 for (p = tostr; *p != NUL; p += tolen)
14208 {
14209 tolen = (*mb_ptr2len)(p);
14210 --idx;
14211 }
14212 if (idx != 0)
14213 goto error;
14214 }
14215
14216 (void)ga_grow(&ga, cplen);
14217 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14218 ga.ga_len += cplen;
14219
14220 in_str += inlen;
14221 }
14222 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223 {
14224 /* When not using multi-byte chars we can do it faster. */
14225 p = vim_strchr(fromstr, *in_str);
14226 if (p != NULL)
14227 ga_append(&ga, tostr[p - fromstr]);
14228 else
14229 ga_append(&ga, *in_str);
14230 ++in_str;
14231 }
14232 }
14233
14234 /* add a terminating NUL */
14235 (void)ga_grow(&ga, 1);
14236 ga_append(&ga, NUL);
14237
14238 rettv->vval.v_string = ga.ga_data;
14239}
14240
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014241/*
14242 * "trim({expr})" function
14243 */
14244 static void
14245f_trim(typval_T *argvars, typval_T *rettv)
14246{
14247 char_u buf1[NUMBUFLEN];
14248 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014249 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014250 char_u *mask = NULL;
14251 char_u *tail;
14252 char_u *prev;
14253 char_u *p;
14254 int c1;
14255
14256 rettv->v_type = VAR_STRING;
14257 if (head == NULL)
14258 {
14259 rettv->vval.v_string = NULL;
14260 return;
14261 }
14262
14263 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014264 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014265
14266 while (*head != NUL)
14267 {
14268 c1 = PTR2CHAR(head);
14269 if (mask == NULL)
14270 {
14271 if (c1 > ' ' && c1 != 0xa0)
14272 break;
14273 }
14274 else
14275 {
14276 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14277 if (c1 == PTR2CHAR(p))
14278 break;
14279 if (*p == NUL)
14280 break;
14281 }
14282 MB_PTR_ADV(head);
14283 }
14284
14285 for (tail = head + STRLEN(head); tail > head; tail = prev)
14286 {
14287 prev = tail;
14288 MB_PTR_BACK(head, prev);
14289 c1 = PTR2CHAR(prev);
14290 if (mask == NULL)
14291 {
14292 if (c1 > ' ' && c1 != 0xa0)
14293 break;
14294 }
14295 else
14296 {
14297 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14298 if (c1 == PTR2CHAR(p))
14299 break;
14300 if (*p == NUL)
14301 break;
14302 }
14303 }
14304 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14305}
14306
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014307#ifdef FEAT_FLOAT
14308/*
14309 * "trunc({float})" function
14310 */
14311 static void
14312f_trunc(typval_T *argvars, typval_T *rettv)
14313{
14314 float_T f = 0.0;
14315
14316 rettv->v_type = VAR_FLOAT;
14317 if (get_float_arg(argvars, &f) == OK)
14318 /* trunc() is not in C90, use floor() or ceil() instead. */
14319 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14320 else
14321 rettv->vval.v_float = 0.0;
14322}
14323#endif
14324
14325/*
14326 * "type(expr)" function
14327 */
14328 static void
14329f_type(typval_T *argvars, typval_T *rettv)
14330{
14331 int n = -1;
14332
14333 switch (argvars[0].v_type)
14334 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014335 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14336 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014337 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014338 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14339 case VAR_LIST: n = VAR_TYPE_LIST; break;
14340 case VAR_DICT: n = VAR_TYPE_DICT; break;
14341 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014342 case VAR_SPECIAL:
14343 if (argvars[0].vval.v_number == VVAL_FALSE
14344 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014345 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014346 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014347 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014348 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014349 case VAR_JOB: n = VAR_TYPE_JOB; break;
14350 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014351 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014352 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014353 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014354 n = -1;
14355 break;
14356 }
14357 rettv->vval.v_number = n;
14358}
14359
14360/*
14361 * "undofile(name)" function
14362 */
14363 static void
14364f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14365{
14366 rettv->v_type = VAR_STRING;
14367#ifdef FEAT_PERSISTENT_UNDO
14368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014369 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014370
14371 if (*fname == NUL)
14372 {
14373 /* If there is no file name there will be no undo file. */
14374 rettv->vval.v_string = NULL;
14375 }
14376 else
14377 {
14378 char_u *ffname = FullName_save(fname, FALSE);
14379
14380 if (ffname != NULL)
14381 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14382 vim_free(ffname);
14383 }
14384 }
14385#else
14386 rettv->vval.v_string = NULL;
14387#endif
14388}
14389
14390/*
14391 * "undotree()" function
14392 */
14393 static void
14394f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14395{
14396 if (rettv_dict_alloc(rettv) == OK)
14397 {
14398 dict_T *dict = rettv->vval.v_dict;
14399 list_T *list;
14400
Bram Moolenaare0be1672018-07-08 16:50:37 +020014401 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14402 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14403 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14404 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14405 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14406 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407
14408 list = list_alloc();
14409 if (list != NULL)
14410 {
14411 u_eval_tree(curbuf->b_u_oldhead, list);
14412 dict_add_list(dict, "entries", list);
14413 }
14414 }
14415}
14416
14417/*
14418 * "values(dict)" function
14419 */
14420 static void
14421f_values(typval_T *argvars, typval_T *rettv)
14422{
14423 dict_list(argvars, rettv, 1);
14424}
14425
14426/*
14427 * "virtcol(string)" function
14428 */
14429 static void
14430f_virtcol(typval_T *argvars, typval_T *rettv)
14431{
14432 colnr_T vcol = 0;
14433 pos_T *fp;
14434 int fnum = curbuf->b_fnum;
14435
14436 fp = var2fpos(&argvars[0], FALSE, &fnum);
14437 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14438 && fnum == curbuf->b_fnum)
14439 {
14440 getvvcol(curwin, fp, NULL, NULL, &vcol);
14441 ++vcol;
14442 }
14443
14444 rettv->vval.v_number = vcol;
14445}
14446
14447/*
14448 * "visualmode()" function
14449 */
14450 static void
14451f_visualmode(typval_T *argvars, typval_T *rettv)
14452{
14453 char_u str[2];
14454
14455 rettv->v_type = VAR_STRING;
14456 str[0] = curbuf->b_visual_mode_eval;
14457 str[1] = NUL;
14458 rettv->vval.v_string = vim_strsave(str);
14459
14460 /* A non-zero number or non-empty string argument: reset mode. */
14461 if (non_zero_arg(&argvars[0]))
14462 curbuf->b_visual_mode_eval = NUL;
14463}
14464
14465/*
14466 * "wildmenumode()" function
14467 */
14468 static void
14469f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14470{
14471#ifdef FEAT_WILDMENU
14472 if (wild_menu_showing)
14473 rettv->vval.v_number = 1;
14474#endif
14475}
14476
14477/*
14478 * "winbufnr(nr)" function
14479 */
14480 static void
14481f_winbufnr(typval_T *argvars, typval_T *rettv)
14482{
14483 win_T *wp;
14484
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014485 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014486 if (wp == NULL)
14487 rettv->vval.v_number = -1;
14488 else
14489 rettv->vval.v_number = wp->w_buffer->b_fnum;
14490}
14491
14492/*
14493 * "wincol()" function
14494 */
14495 static void
14496f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14497{
14498 validate_cursor();
14499 rettv->vval.v_number = curwin->w_wcol + 1;
14500}
14501
14502/*
14503 * "winheight(nr)" function
14504 */
14505 static void
14506f_winheight(typval_T *argvars, typval_T *rettv)
14507{
14508 win_T *wp;
14509
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014510 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014511 if (wp == NULL)
14512 rettv->vval.v_number = -1;
14513 else
14514 rettv->vval.v_number = wp->w_height;
14515}
14516
14517/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014518 * "winlayout()" function
14519 */
14520 static void
14521f_winlayout(typval_T *argvars, typval_T *rettv)
14522{
14523 tabpage_T *tp;
14524
14525 if (rettv_list_alloc(rettv) != OK)
14526 return;
14527
14528 if (argvars[0].v_type == VAR_UNKNOWN)
14529 tp = curtab;
14530 else
14531 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014532 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014533 if (tp == NULL)
14534 return;
14535 }
14536
14537 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14538}
14539
14540/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014541 * "winline()" function
14542 */
14543 static void
14544f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14545{
14546 validate_cursor();
14547 rettv->vval.v_number = curwin->w_wrow + 1;
14548}
14549
14550/*
14551 * "winnr()" function
14552 */
14553 static void
14554f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14555{
14556 int nr = 1;
14557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014558 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014559 rettv->vval.v_number = nr;
14560}
14561
14562/*
14563 * "winrestcmd()" function
14564 */
14565 static void
14566f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14567{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568 win_T *wp;
14569 int winnr = 1;
14570 garray_T ga;
14571 char_u buf[50];
14572
14573 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014574 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575 {
14576 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14577 ga_concat(&ga, buf);
14578 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14579 ga_concat(&ga, buf);
14580 ++winnr;
14581 }
14582 ga_append(&ga, NUL);
14583
14584 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014585 rettv->v_type = VAR_STRING;
14586}
14587
14588/*
14589 * "winrestview()" function
14590 */
14591 static void
14592f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14593{
14594 dict_T *dict;
14595
14596 if (argvars[0].v_type != VAR_DICT
14597 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014598 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014599 else
14600 {
14601 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014602 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014603 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014604 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014606 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014607 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 Moolenaare0be1672018-07-08 16:50:37 +020014653 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014654 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014655 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014656
Bram Moolenaare0be1672018-07-08 16:50:37 +020014657 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014659 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014661 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14662 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014663}
14664
14665/*
14666 * "winwidth(nr)" function
14667 */
14668 static void
14669f_winwidth(typval_T *argvars, typval_T *rettv)
14670{
14671 win_T *wp;
14672
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014673 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014674 if (wp == NULL)
14675 rettv->vval.v_number = -1;
14676 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678}
14679
14680/*
14681 * "wordcount()" function
14682 */
14683 static void
14684f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14685{
14686 if (rettv_dict_alloc(rettv) == FAIL)
14687 return;
14688 cursor_pos_info(rettv->vval.v_dict);
14689}
14690
14691/*
14692 * "writefile()" function
14693 */
14694 static void
14695f_writefile(typval_T *argvars, typval_T *rettv)
14696{
14697 int binary = FALSE;
14698 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014699#ifdef HAVE_FSYNC
14700 int do_fsync = p_fs;
14701#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 char_u *fname;
14703 FILE *fd;
14704 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014705 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014706 list_T *list = NULL;
14707 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014709 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 if (check_restricted() || check_secure())
14711 return;
14712
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014713 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014715 list = argvars[0].vval.v_list;
14716 if (list == NULL)
14717 return;
14718 for (li = list->lv_first; li != NULL; li = li->li_next)
14719 if (tv_get_string_chk(&li->li_tv) == NULL)
14720 return;
14721 }
14722 else if (argvars[0].v_type == VAR_BLOB)
14723 {
14724 blob = argvars[0].vval.v_blob;
14725 if (blob == NULL)
14726 return;
14727 }
14728 else
14729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014730 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014731 return;
14732 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733
14734 if (argvars[2].v_type != VAR_UNKNOWN)
14735 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014736 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014737
14738 if (arg2 == NULL)
14739 return;
14740 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014742 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014744#ifdef HAVE_FSYNC
14745 if (vim_strchr(arg2, 's') != NULL)
14746 do_fsync = TRUE;
14747 else if (vim_strchr(arg2, 'S') != NULL)
14748 do_fsync = FALSE;
14749#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014750 }
14751
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014752 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014753 if (fname == NULL)
14754 return;
14755
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 /* Always open the file in binary mode, library functions have a mind of
14757 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14759 append ? APPENDBIN : WRITEBIN)) == NULL)
14760 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014761 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 ret = -1;
14763 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014764 else if (blob)
14765 {
14766 if (write_blob(fd, blob) == FAIL)
14767 ret = -1;
14768#ifdef HAVE_FSYNC
14769 else if (do_fsync)
14770 // Ignore the error, the user wouldn't know what to do about it.
14771 // May happen for a device.
14772 vim_ignored = fsync(fileno(fd));
14773#endif
14774 fclose(fd);
14775 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014776 else
14777 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014778 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014779 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014780#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014781 else if (do_fsync)
14782 /* Ignore the error, the user wouldn't know what to do about it.
14783 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014784 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014785#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014786 fclose(fd);
14787 }
14788
14789 rettv->vval.v_number = ret;
14790}
14791
14792/*
14793 * "xor(expr, expr)" function
14794 */
14795 static void
14796f_xor(typval_T *argvars, typval_T *rettv)
14797{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014798 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14799 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800}
14801
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014802#endif /* FEAT_EVAL */