blob: 269423f9291a35a9fd3c2e9899e9a17ec8c83fba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020034
35#ifdef FEAT_FLOAT
36static void f_abs(typval_T *argvars, typval_T *rettv);
37static void f_acos(typval_T *argvars, typval_T *rettv);
38#endif
39static void f_add(typval_T *argvars, typval_T *rettv);
40static void f_and(typval_T *argvars, typval_T *rettv);
41static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020042static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020043static void f_argc(typval_T *argvars, typval_T *rettv);
44static void f_argidx(typval_T *argvars, typval_T *rettv);
45static void f_arglistid(typval_T *argvars, typval_T *rettv);
46static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010047static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020048static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010049static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020050static void f_assert_exception(typval_T *argvars, typval_T *rettv);
51static void f_assert_fails(typval_T *argvars, typval_T *rettv);
52static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020053static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020054static void f_assert_match(typval_T *argvars, typval_T *rettv);
55static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
56static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010057static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020058static void f_assert_true(typval_T *argvars, typval_T *rettv);
59#ifdef FEAT_FLOAT
60static void f_asin(typval_T *argvars, typval_T *rettv);
61static void f_atan(typval_T *argvars, typval_T *rettv);
62static void f_atan2(typval_T *argvars, typval_T *rettv);
63#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010064#ifdef FEAT_BEVAL
65static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010066# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010067static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020070static void f_browse(typval_T *argvars, typval_T *rettv);
71static void f_browsedir(typval_T *argvars, typval_T *rettv);
72static void f_bufexists(typval_T *argvars, typval_T *rettv);
73static void f_buflisted(typval_T *argvars, typval_T *rettv);
74static void f_bufloaded(typval_T *argvars, typval_T *rettv);
75static void f_bufname(typval_T *argvars, typval_T *rettv);
76static void f_bufnr(typval_T *argvars, typval_T *rettv);
77static void f_bufwinid(typval_T *argvars, typval_T *rettv);
78static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
79static void f_byte2line(typval_T *argvars, typval_T *rettv);
80static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
81static void f_byteidx(typval_T *argvars, typval_T *rettv);
82static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
83static void f_call(typval_T *argvars, typval_T *rettv);
84#ifdef FEAT_FLOAT
85static void f_ceil(typval_T *argvars, typval_T *rettv);
86#endif
87#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010088static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020089static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020090static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
92static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
93static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
94static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
95static void f_ch_info(typval_T *argvars, typval_T *rettv);
96static void f_ch_log(typval_T *argvars, typval_T *rettv);
97static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
98static void f_ch_open(typval_T *argvars, typval_T *rettv);
99static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100100static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200101static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
102static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
103static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
105static void f_ch_status(typval_T *argvars, typval_T *rettv);
106#endif
107static void f_changenr(typval_T *argvars, typval_T *rettv);
108static void f_char2nr(typval_T *argvars, typval_T *rettv);
109static void f_cindent(typval_T *argvars, typval_T *rettv);
110static void f_clearmatches(typval_T *argvars, typval_T *rettv);
111static void f_col(typval_T *argvars, typval_T *rettv);
112#if defined(FEAT_INS_EXPAND)
113static void f_complete(typval_T *argvars, typval_T *rettv);
114static void f_complete_add(typval_T *argvars, typval_T *rettv);
115static void f_complete_check(typval_T *argvars, typval_T *rettv);
116#endif
117static void f_confirm(typval_T *argvars, typval_T *rettv);
118static void f_copy(typval_T *argvars, typval_T *rettv);
119#ifdef FEAT_FLOAT
120static void f_cos(typval_T *argvars, typval_T *rettv);
121static void f_cosh(typval_T *argvars, typval_T *rettv);
122#endif
123static void f_count(typval_T *argvars, typval_T *rettv);
124static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
125static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200126#ifdef WIN3264
127static void f_debugbreak(typval_T *argvars, typval_T *rettv);
128#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200129static void f_deepcopy(typval_T *argvars, typval_T *rettv);
130static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200131static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200132static void f_did_filetype(typval_T *argvars, typval_T *rettv);
133static void f_diff_filler(typval_T *argvars, typval_T *rettv);
134static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
135static void f_empty(typval_T *argvars, typval_T *rettv);
136static void f_escape(typval_T *argvars, typval_T *rettv);
137static void f_eval(typval_T *argvars, typval_T *rettv);
138static void f_eventhandler(typval_T *argvars, typval_T *rettv);
139static void f_executable(typval_T *argvars, typval_T *rettv);
140static void f_execute(typval_T *argvars, typval_T *rettv);
141static void f_exepath(typval_T *argvars, typval_T *rettv);
142static void f_exists(typval_T *argvars, typval_T *rettv);
143#ifdef FEAT_FLOAT
144static void f_exp(typval_T *argvars, typval_T *rettv);
145#endif
146static void f_expand(typval_T *argvars, typval_T *rettv);
147static void f_extend(typval_T *argvars, typval_T *rettv);
148static void f_feedkeys(typval_T *argvars, typval_T *rettv);
149static void f_filereadable(typval_T *argvars, typval_T *rettv);
150static void f_filewritable(typval_T *argvars, typval_T *rettv);
151static void f_filter(typval_T *argvars, typval_T *rettv);
152static void f_finddir(typval_T *argvars, typval_T *rettv);
153static void f_findfile(typval_T *argvars, typval_T *rettv);
154#ifdef FEAT_FLOAT
155static void f_float2nr(typval_T *argvars, typval_T *rettv);
156static void f_floor(typval_T *argvars, typval_T *rettv);
157static void f_fmod(typval_T *argvars, typval_T *rettv);
158#endif
159static void f_fnameescape(typval_T *argvars, typval_T *rettv);
160static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
161static void f_foldclosed(typval_T *argvars, typval_T *rettv);
162static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
163static void f_foldlevel(typval_T *argvars, typval_T *rettv);
164static void f_foldtext(typval_T *argvars, typval_T *rettv);
165static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
166static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200167static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200168static void f_function(typval_T *argvars, typval_T *rettv);
169static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
170static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200171static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200172static void f_getbufline(typval_T *argvars, typval_T *rettv);
173static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100174static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200175static void f_getchar(typval_T *argvars, typval_T *rettv);
176static void f_getcharmod(typval_T *argvars, typval_T *rettv);
177static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
178static void f_getcmdline(typval_T *argvars, typval_T *rettv);
179#if defined(FEAT_CMDL_COMPL)
180static void f_getcompletion(typval_T *argvars, typval_T *rettv);
181#endif
182static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
183static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
184static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
185static void f_getcwd(typval_T *argvars, typval_T *rettv);
186static void f_getfontname(typval_T *argvars, typval_T *rettv);
187static void f_getfperm(typval_T *argvars, typval_T *rettv);
188static void f_getfsize(typval_T *argvars, typval_T *rettv);
189static void f_getftime(typval_T *argvars, typval_T *rettv);
190static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100191static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200193static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200194static void f_getmatches(typval_T *argvars, typval_T *rettv);
195static void f_getpid(typval_T *argvars, typval_T *rettv);
196static void f_getcurpos(typval_T *argvars, typval_T *rettv);
197static void f_getpos(typval_T *argvars, typval_T *rettv);
198static void f_getqflist(typval_T *argvars, typval_T *rettv);
199static void f_getreg(typval_T *argvars, typval_T *rettv);
200static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200201static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200202static void f_gettabvar(typval_T *argvars, typval_T *rettv);
203static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100204static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200205static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100206static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200207static void f_getwinposx(typval_T *argvars, typval_T *rettv);
208static void f_getwinposy(typval_T *argvars, typval_T *rettv);
209static void f_getwinvar(typval_T *argvars, typval_T *rettv);
210static void f_glob(typval_T *argvars, typval_T *rettv);
211static void f_globpath(typval_T *argvars, typval_T *rettv);
212static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
213static void f_has(typval_T *argvars, typval_T *rettv);
214static void f_has_key(typval_T *argvars, typval_T *rettv);
215static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
216static void f_hasmapto(typval_T *argvars, typval_T *rettv);
217static void f_histadd(typval_T *argvars, typval_T *rettv);
218static void f_histdel(typval_T *argvars, typval_T *rettv);
219static void f_histget(typval_T *argvars, typval_T *rettv);
220static void f_histnr(typval_T *argvars, typval_T *rettv);
221static void f_hlID(typval_T *argvars, typval_T *rettv);
222static void f_hlexists(typval_T *argvars, typval_T *rettv);
223static void f_hostname(typval_T *argvars, typval_T *rettv);
224static void f_iconv(typval_T *argvars, typval_T *rettv);
225static void f_indent(typval_T *argvars, typval_T *rettv);
226static void f_index(typval_T *argvars, typval_T *rettv);
227static void f_input(typval_T *argvars, typval_T *rettv);
228static void f_inputdialog(typval_T *argvars, typval_T *rettv);
229static void f_inputlist(typval_T *argvars, typval_T *rettv);
230static void f_inputrestore(typval_T *argvars, typval_T *rettv);
231static void f_inputsave(typval_T *argvars, typval_T *rettv);
232static void f_inputsecret(typval_T *argvars, typval_T *rettv);
233static void f_insert(typval_T *argvars, typval_T *rettv);
234static void f_invert(typval_T *argvars, typval_T *rettv);
235static void f_isdirectory(typval_T *argvars, typval_T *rettv);
236static void f_islocked(typval_T *argvars, typval_T *rettv);
237#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
238static void f_isnan(typval_T *argvars, typval_T *rettv);
239#endif
240static void f_items(typval_T *argvars, typval_T *rettv);
241#ifdef FEAT_JOB_CHANNEL
242static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
243static void f_job_info(typval_T *argvars, typval_T *rettv);
244static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
245static void f_job_start(typval_T *argvars, typval_T *rettv);
246static void f_job_stop(typval_T *argvars, typval_T *rettv);
247static void f_job_status(typval_T *argvars, typval_T *rettv);
248#endif
249static void f_join(typval_T *argvars, typval_T *rettv);
250static void f_js_decode(typval_T *argvars, typval_T *rettv);
251static void f_js_encode(typval_T *argvars, typval_T *rettv);
252static void f_json_decode(typval_T *argvars, typval_T *rettv);
253static void f_json_encode(typval_T *argvars, typval_T *rettv);
254static void f_keys(typval_T *argvars, typval_T *rettv);
255static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
256static void f_len(typval_T *argvars, typval_T *rettv);
257static void f_libcall(typval_T *argvars, typval_T *rettv);
258static void f_libcallnr(typval_T *argvars, typval_T *rettv);
259static void f_line(typval_T *argvars, typval_T *rettv);
260static void f_line2byte(typval_T *argvars, typval_T *rettv);
261static void f_lispindent(typval_T *argvars, typval_T *rettv);
262static void f_localtime(typval_T *argvars, typval_T *rettv);
263#ifdef FEAT_FLOAT
264static void f_log(typval_T *argvars, typval_T *rettv);
265static void f_log10(typval_T *argvars, typval_T *rettv);
266#endif
267#ifdef FEAT_LUA
268static void f_luaeval(typval_T *argvars, typval_T *rettv);
269#endif
270static void f_map(typval_T *argvars, typval_T *rettv);
271static void f_maparg(typval_T *argvars, typval_T *rettv);
272static void f_mapcheck(typval_T *argvars, typval_T *rettv);
273static void f_match(typval_T *argvars, typval_T *rettv);
274static void f_matchadd(typval_T *argvars, typval_T *rettv);
275static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
276static void f_matcharg(typval_T *argvars, typval_T *rettv);
277static void f_matchdelete(typval_T *argvars, typval_T *rettv);
278static void f_matchend(typval_T *argvars, typval_T *rettv);
279static void f_matchlist(typval_T *argvars, typval_T *rettv);
280static void f_matchstr(typval_T *argvars, typval_T *rettv);
281static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
282static void f_max(typval_T *argvars, typval_T *rettv);
283static void f_min(typval_T *argvars, typval_T *rettv);
284#ifdef vim_mkdir
285static void f_mkdir(typval_T *argvars, typval_T *rettv);
286#endif
287static void f_mode(typval_T *argvars, typval_T *rettv);
288#ifdef FEAT_MZSCHEME
289static void f_mzeval(typval_T *argvars, typval_T *rettv);
290#endif
291static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
292static void f_nr2char(typval_T *argvars, typval_T *rettv);
293static void f_or(typval_T *argvars, typval_T *rettv);
294static void f_pathshorten(typval_T *argvars, typval_T *rettv);
295#ifdef FEAT_PERL
296static void f_perleval(typval_T *argvars, typval_T *rettv);
297#endif
298#ifdef FEAT_FLOAT
299static void f_pow(typval_T *argvars, typval_T *rettv);
300#endif
301static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
302static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200303#ifdef FEAT_JOB_CHANNEL
304static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200305static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200306static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200308static void f_pumvisible(typval_T *argvars, typval_T *rettv);
309#ifdef FEAT_PYTHON3
310static void f_py3eval(typval_T *argvars, typval_T *rettv);
311#endif
312#ifdef FEAT_PYTHON
313static void f_pyeval(typval_T *argvars, typval_T *rettv);
314#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100315#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
316static void f_pyxeval(typval_T *argvars, typval_T *rettv);
317#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200318static void f_range(typval_T *argvars, typval_T *rettv);
319static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200320static void f_reg_executing(typval_T *argvars, typval_T *rettv);
321static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200322static void f_reltime(typval_T *argvars, typval_T *rettv);
323#ifdef FEAT_FLOAT
324static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
325#endif
326static void f_reltimestr(typval_T *argvars, typval_T *rettv);
327static void f_remote_expr(typval_T *argvars, typval_T *rettv);
328static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
329static void f_remote_peek(typval_T *argvars, typval_T *rettv);
330static void f_remote_read(typval_T *argvars, typval_T *rettv);
331static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100332static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200333static void f_remove(typval_T *argvars, typval_T *rettv);
334static void f_rename(typval_T *argvars, typval_T *rettv);
335static void f_repeat(typval_T *argvars, typval_T *rettv);
336static void f_resolve(typval_T *argvars, typval_T *rettv);
337static void f_reverse(typval_T *argvars, typval_T *rettv);
338#ifdef FEAT_FLOAT
339static void f_round(typval_T *argvars, typval_T *rettv);
340#endif
341static void f_screenattr(typval_T *argvars, typval_T *rettv);
342static void f_screenchar(typval_T *argvars, typval_T *rettv);
343static void f_screencol(typval_T *argvars, typval_T *rettv);
344static void f_screenrow(typval_T *argvars, typval_T *rettv);
345static void f_search(typval_T *argvars, typval_T *rettv);
346static void f_searchdecl(typval_T *argvars, typval_T *rettv);
347static void f_searchpair(typval_T *argvars, typval_T *rettv);
348static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
349static void f_searchpos(typval_T *argvars, typval_T *rettv);
350static void f_server2client(typval_T *argvars, typval_T *rettv);
351static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200352static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_setbufvar(typval_T *argvars, typval_T *rettv);
354static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
355static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
356static void f_setfperm(typval_T *argvars, typval_T *rettv);
357static void f_setline(typval_T *argvars, typval_T *rettv);
358static void f_setloclist(typval_T *argvars, typval_T *rettv);
359static void f_setmatches(typval_T *argvars, typval_T *rettv);
360static void f_setpos(typval_T *argvars, typval_T *rettv);
361static void f_setqflist(typval_T *argvars, typval_T *rettv);
362static void f_setreg(typval_T *argvars, typval_T *rettv);
363static void f_settabvar(typval_T *argvars, typval_T *rettv);
364static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100365static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200366static void f_setwinvar(typval_T *argvars, typval_T *rettv);
367#ifdef FEAT_CRYPT
368static void f_sha256(typval_T *argvars, typval_T *rettv);
369#endif /* FEAT_CRYPT */
370static void f_shellescape(typval_T *argvars, typval_T *rettv);
371static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100372#ifdef FEAT_SIGNS
373static void f_sign_define(typval_T *argvars, typval_T *rettv);
374static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
375static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100376static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100377static void f_sign_place(typval_T *argvars, typval_T *rettv);
378static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
379static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
380#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_simplify(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_FLOAT
383static void f_sin(typval_T *argvars, typval_T *rettv);
384static void f_sinh(typval_T *argvars, typval_T *rettv);
385#endif
386static void f_sort(typval_T *argvars, typval_T *rettv);
387static void f_soundfold(typval_T *argvars, typval_T *rettv);
388static void f_spellbadword(typval_T *argvars, typval_T *rettv);
389static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
390static void f_split(typval_T *argvars, typval_T *rettv);
391#ifdef FEAT_FLOAT
392static void f_sqrt(typval_T *argvars, typval_T *rettv);
393static void f_str2float(typval_T *argvars, typval_T *rettv);
394#endif
395static void f_str2nr(typval_T *argvars, typval_T *rettv);
396static void f_strchars(typval_T *argvars, typval_T *rettv);
397#ifdef HAVE_STRFTIME
398static void f_strftime(typval_T *argvars, typval_T *rettv);
399#endif
400static void f_strgetchar(typval_T *argvars, typval_T *rettv);
401static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200402static void f_strlen(typval_T *argvars, typval_T *rettv);
403static void f_strcharpart(typval_T *argvars, typval_T *rettv);
404static void f_strpart(typval_T *argvars, typval_T *rettv);
405static void f_strridx(typval_T *argvars, typval_T *rettv);
406static void f_strtrans(typval_T *argvars, typval_T *rettv);
407static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
408static void f_strwidth(typval_T *argvars, typval_T *rettv);
409static void f_submatch(typval_T *argvars, typval_T *rettv);
410static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200411static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200412static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200413static void f_synID(typval_T *argvars, typval_T *rettv);
414static void f_synIDattr(typval_T *argvars, typval_T *rettv);
415static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
416static void f_synstack(typval_T *argvars, typval_T *rettv);
417static void f_synconcealed(typval_T *argvars, typval_T *rettv);
418static void f_system(typval_T *argvars, typval_T *rettv);
419static void f_systemlist(typval_T *argvars, typval_T *rettv);
420static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
421static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
422static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
423static void f_taglist(typval_T *argvars, typval_T *rettv);
424static void f_tagfiles(typval_T *argvars, typval_T *rettv);
425static void f_tempname(typval_T *argvars, typval_T *rettv);
426static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
427static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200428static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200429static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100430static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200431static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100432static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100433static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200434#ifdef FEAT_JOB_CHANNEL
435static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
436#endif
437static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
438#ifdef FEAT_JOB_CHANNEL
439static void f_test_null_job(typval_T *argvars, typval_T *rettv);
440#endif
441static void f_test_null_list(typval_T *argvars, typval_T *rettv);
442static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
443static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200444#ifdef FEAT_GUI
445static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
446#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200447static void f_test_settime(typval_T *argvars, typval_T *rettv);
448#ifdef FEAT_FLOAT
449static void f_tan(typval_T *argvars, typval_T *rettv);
450static void f_tanh(typval_T *argvars, typval_T *rettv);
451#endif
452#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200453static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200454static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200455static void f_timer_start(typval_T *argvars, typval_T *rettv);
456static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200457static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200458#endif
459static void f_tolower(typval_T *argvars, typval_T *rettv);
460static void f_toupper(typval_T *argvars, typval_T *rettv);
461static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100462static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200463#ifdef FEAT_FLOAT
464static void f_trunc(typval_T *argvars, typval_T *rettv);
465#endif
466static void f_type(typval_T *argvars, typval_T *rettv);
467static void f_undofile(typval_T *argvars, typval_T *rettv);
468static void f_undotree(typval_T *argvars, typval_T *rettv);
469static void f_uniq(typval_T *argvars, typval_T *rettv);
470static void f_values(typval_T *argvars, typval_T *rettv);
471static void f_virtcol(typval_T *argvars, typval_T *rettv);
472static void f_visualmode(typval_T *argvars, typval_T *rettv);
473static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
474static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
475static void f_win_getid(typval_T *argvars, typval_T *rettv);
476static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
477static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
478static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100479static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480static void f_winbufnr(typval_T *argvars, typval_T *rettv);
481static void f_wincol(typval_T *argvars, typval_T *rettv);
482static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200483static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484static void f_winline(typval_T *argvars, typval_T *rettv);
485static void f_winnr(typval_T *argvars, typval_T *rettv);
486static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
487static void f_winrestview(typval_T *argvars, typval_T *rettv);
488static void f_winsaveview(typval_T *argvars, typval_T *rettv);
489static void f_winwidth(typval_T *argvars, typval_T *rettv);
490static void f_writefile(typval_T *argvars, typval_T *rettv);
491static void f_wordcount(typval_T *argvars, typval_T *rettv);
492static void f_xor(typval_T *argvars, typval_T *rettv);
493
494/*
495 * Array with names and number of arguments of all internal functions
496 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
497 */
498static struct fst
499{
500 char *f_name; /* function name */
501 char f_min_argc; /* minimal number of arguments */
502 char f_max_argc; /* maximal number of arguments */
503 void (*f_func)(typval_T *args, typval_T *rvar);
504 /* implementation of function */
505} functions[] =
506{
507#ifdef FEAT_FLOAT
508 {"abs", 1, 1, f_abs},
509 {"acos", 1, 1, f_acos}, /* WJMc */
510#endif
511 {"add", 2, 2, f_add},
512 {"and", 2, 2, f_and},
513 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200514 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200515 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200516 {"argidx", 0, 0, f_argidx},
517 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200518 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200519#ifdef FEAT_FLOAT
520 {"asin", 1, 1, f_asin}, /* WJMc */
521#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100522 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200523 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100524 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200525 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200526 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200527 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100528 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200529 {"assert_match", 2, 3, f_assert_match},
530 {"assert_notequal", 2, 3, f_assert_notequal},
531 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100532 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200533 {"assert_true", 1, 2, f_assert_true},
534#ifdef FEAT_FLOAT
535 {"atan", 1, 1, f_atan},
536 {"atan2", 2, 2, f_atan2},
537#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100538#ifdef FEAT_BEVAL
539 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100540# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100541 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100542# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100543#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"browse", 4, 4, f_browse},
545 {"browsedir", 2, 2, f_browsedir},
546 {"bufexists", 1, 1, f_bufexists},
547 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
548 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
549 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
550 {"buflisted", 1, 1, f_buflisted},
551 {"bufloaded", 1, 1, f_bufloaded},
552 {"bufname", 1, 1, f_bufname},
553 {"bufnr", 1, 2, f_bufnr},
554 {"bufwinid", 1, 1, f_bufwinid},
555 {"bufwinnr", 1, 1, f_bufwinnr},
556 {"byte2line", 1, 1, f_byte2line},
557 {"byteidx", 2, 2, f_byteidx},
558 {"byteidxcomp", 2, 2, f_byteidxcomp},
559 {"call", 2, 3, f_call},
560#ifdef FEAT_FLOAT
561 {"ceil", 1, 1, f_ceil},
562#endif
563#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100564 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200565 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200566 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
568 {"ch_evalraw", 2, 3, f_ch_evalraw},
569 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
570 {"ch_getjob", 1, 1, f_ch_getjob},
571 {"ch_info", 1, 1, f_ch_info},
572 {"ch_log", 1, 2, f_ch_log},
573 {"ch_logfile", 1, 2, f_ch_logfile},
574 {"ch_open", 1, 2, f_ch_open},
575 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100576 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200577 {"ch_readraw", 1, 2, f_ch_readraw},
578 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
579 {"ch_sendraw", 2, 3, f_ch_sendraw},
580 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200581 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200582#endif
583 {"changenr", 0, 0, f_changenr},
584 {"char2nr", 1, 2, f_char2nr},
585 {"cindent", 1, 1, f_cindent},
586 {"clearmatches", 0, 0, f_clearmatches},
587 {"col", 1, 1, f_col},
588#if defined(FEAT_INS_EXPAND)
589 {"complete", 2, 2, f_complete},
590 {"complete_add", 1, 1, f_complete_add},
591 {"complete_check", 0, 0, f_complete_check},
592#endif
593 {"confirm", 1, 4, f_confirm},
594 {"copy", 1, 1, f_copy},
595#ifdef FEAT_FLOAT
596 {"cos", 1, 1, f_cos},
597 {"cosh", 1, 1, f_cosh},
598#endif
599 {"count", 2, 4, f_count},
600 {"cscope_connection",0,3, f_cscope_connection},
601 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200602#ifdef WIN3264
603 {"debugbreak", 1, 1, f_debugbreak},
604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605 {"deepcopy", 1, 2, f_deepcopy},
606 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200607 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"did_filetype", 0, 0, f_did_filetype},
609 {"diff_filler", 1, 1, f_diff_filler},
610 {"diff_hlID", 2, 2, f_diff_hlID},
611 {"empty", 1, 1, f_empty},
612 {"escape", 2, 2, f_escape},
613 {"eval", 1, 1, f_eval},
614 {"eventhandler", 0, 0, f_eventhandler},
615 {"executable", 1, 1, f_executable},
616 {"execute", 1, 2, f_execute},
617 {"exepath", 1, 1, f_exepath},
618 {"exists", 1, 1, f_exists},
619#ifdef FEAT_FLOAT
620 {"exp", 1, 1, f_exp},
621#endif
622 {"expand", 1, 3, f_expand},
623 {"extend", 2, 3, f_extend},
624 {"feedkeys", 1, 2, f_feedkeys},
625 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
626 {"filereadable", 1, 1, f_filereadable},
627 {"filewritable", 1, 1, f_filewritable},
628 {"filter", 2, 2, f_filter},
629 {"finddir", 1, 3, f_finddir},
630 {"findfile", 1, 3, f_findfile},
631#ifdef FEAT_FLOAT
632 {"float2nr", 1, 1, f_float2nr},
633 {"floor", 1, 1, f_floor},
634 {"fmod", 2, 2, f_fmod},
635#endif
636 {"fnameescape", 1, 1, f_fnameescape},
637 {"fnamemodify", 2, 2, f_fnamemodify},
638 {"foldclosed", 1, 1, f_foldclosed},
639 {"foldclosedend", 1, 1, f_foldclosedend},
640 {"foldlevel", 1, 1, f_foldlevel},
641 {"foldtext", 0, 0, f_foldtext},
642 {"foldtextresult", 1, 1, f_foldtextresult},
643 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200644 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200645 {"function", 1, 3, f_function},
646 {"garbagecollect", 0, 1, f_garbagecollect},
647 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200648 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200649 {"getbufline", 2, 3, f_getbufline},
650 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100651 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200652 {"getchar", 0, 1, f_getchar},
653 {"getcharmod", 0, 0, f_getcharmod},
654 {"getcharsearch", 0, 0, f_getcharsearch},
655 {"getcmdline", 0, 0, f_getcmdline},
656 {"getcmdpos", 0, 0, f_getcmdpos},
657 {"getcmdtype", 0, 0, f_getcmdtype},
658 {"getcmdwintype", 0, 0, f_getcmdwintype},
659#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200660 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200661#endif
662 {"getcurpos", 0, 0, f_getcurpos},
663 {"getcwd", 0, 2, f_getcwd},
664 {"getfontname", 0, 1, f_getfontname},
665 {"getfperm", 1, 1, f_getfperm},
666 {"getfsize", 1, 1, f_getfsize},
667 {"getftime", 1, 1, f_getftime},
668 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100669 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200671 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200672 {"getmatches", 0, 0, f_getmatches},
673 {"getpid", 0, 0, f_getpid},
674 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200675 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200676 {"getreg", 0, 3, f_getreg},
677 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200678 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200679 {"gettabvar", 2, 3, f_gettabvar},
680 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100681 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200682 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100683 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200684 {"getwinposx", 0, 0, f_getwinposx},
685 {"getwinposy", 0, 0, f_getwinposy},
686 {"getwinvar", 2, 3, f_getwinvar},
687 {"glob", 1, 4, f_glob},
688 {"glob2regpat", 1, 1, f_glob2regpat},
689 {"globpath", 2, 5, f_globpath},
690 {"has", 1, 1, f_has},
691 {"has_key", 2, 2, f_has_key},
692 {"haslocaldir", 0, 2, f_haslocaldir},
693 {"hasmapto", 1, 3, f_hasmapto},
694 {"highlightID", 1, 1, f_hlID}, /* obsolete */
695 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
696 {"histadd", 2, 2, f_histadd},
697 {"histdel", 1, 2, f_histdel},
698 {"histget", 1, 2, f_histget},
699 {"histnr", 1, 1, f_histnr},
700 {"hlID", 1, 1, f_hlID},
701 {"hlexists", 1, 1, f_hlexists},
702 {"hostname", 0, 0, f_hostname},
703 {"iconv", 3, 3, f_iconv},
704 {"indent", 1, 1, f_indent},
705 {"index", 2, 4, f_index},
706 {"input", 1, 3, f_input},
707 {"inputdialog", 1, 3, f_inputdialog},
708 {"inputlist", 1, 1, f_inputlist},
709 {"inputrestore", 0, 0, f_inputrestore},
710 {"inputsave", 0, 0, f_inputsave},
711 {"inputsecret", 1, 2, f_inputsecret},
712 {"insert", 2, 3, f_insert},
713 {"invert", 1, 1, f_invert},
714 {"isdirectory", 1, 1, f_isdirectory},
715 {"islocked", 1, 1, f_islocked},
716#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
717 {"isnan", 1, 1, f_isnan},
718#endif
719 {"items", 1, 1, f_items},
720#ifdef FEAT_JOB_CHANNEL
721 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200722 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200723 {"job_setoptions", 2, 2, f_job_setoptions},
724 {"job_start", 1, 2, f_job_start},
725 {"job_status", 1, 1, f_job_status},
726 {"job_stop", 1, 2, f_job_stop},
727#endif
728 {"join", 1, 2, f_join},
729 {"js_decode", 1, 1, f_js_decode},
730 {"js_encode", 1, 1, f_js_encode},
731 {"json_decode", 1, 1, f_json_decode},
732 {"json_encode", 1, 1, f_json_encode},
733 {"keys", 1, 1, f_keys},
734 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
735 {"len", 1, 1, f_len},
736 {"libcall", 3, 3, f_libcall},
737 {"libcallnr", 3, 3, f_libcallnr},
738 {"line", 1, 1, f_line},
739 {"line2byte", 1, 1, f_line2byte},
740 {"lispindent", 1, 1, f_lispindent},
741 {"localtime", 0, 0, f_localtime},
742#ifdef FEAT_FLOAT
743 {"log", 1, 1, f_log},
744 {"log10", 1, 1, f_log10},
745#endif
746#ifdef FEAT_LUA
747 {"luaeval", 1, 2, f_luaeval},
748#endif
749 {"map", 2, 2, f_map},
750 {"maparg", 1, 4, f_maparg},
751 {"mapcheck", 1, 3, f_mapcheck},
752 {"match", 2, 4, f_match},
753 {"matchadd", 2, 5, f_matchadd},
754 {"matchaddpos", 2, 5, f_matchaddpos},
755 {"matcharg", 1, 1, f_matcharg},
756 {"matchdelete", 1, 1, f_matchdelete},
757 {"matchend", 2, 4, f_matchend},
758 {"matchlist", 2, 4, f_matchlist},
759 {"matchstr", 2, 4, f_matchstr},
760 {"matchstrpos", 2, 4, f_matchstrpos},
761 {"max", 1, 1, f_max},
762 {"min", 1, 1, f_min},
763#ifdef vim_mkdir
764 {"mkdir", 1, 3, f_mkdir},
765#endif
766 {"mode", 0, 1, f_mode},
767#ifdef FEAT_MZSCHEME
768 {"mzeval", 1, 1, f_mzeval},
769#endif
770 {"nextnonblank", 1, 1, f_nextnonblank},
771 {"nr2char", 1, 2, f_nr2char},
772 {"or", 2, 2, f_or},
773 {"pathshorten", 1, 1, f_pathshorten},
774#ifdef FEAT_PERL
775 {"perleval", 1, 1, f_perleval},
776#endif
777#ifdef FEAT_FLOAT
778 {"pow", 2, 2, f_pow},
779#endif
780 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100781 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200782#ifdef FEAT_JOB_CHANNEL
783 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200784 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200785 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
786#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100787#ifdef FEAT_TEXT_PROP
788 {"prop_add", 3, 3, f_prop_add},
789 {"prop_clear", 1, 3, f_prop_clear},
790 {"prop_list", 1, 2, f_prop_list},
791 {"prop_remove", 2, 3, f_prop_remove},
792 {"prop_type_add", 2, 2, f_prop_type_add},
793 {"prop_type_change", 2, 2, f_prop_type_change},
794 {"prop_type_delete", 1, 2, f_prop_type_delete},
795 {"prop_type_get", 1, 2, f_prop_type_get},
796 {"prop_type_list", 0, 1, f_prop_type_list},
797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200798 {"pumvisible", 0, 0, f_pumvisible},
799#ifdef FEAT_PYTHON3
800 {"py3eval", 1, 1, f_py3eval},
801#endif
802#ifdef FEAT_PYTHON
803 {"pyeval", 1, 1, f_pyeval},
804#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100805#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
806 {"pyxeval", 1, 1, f_pyxeval},
807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200808 {"range", 1, 3, f_range},
809 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200810 {"reg_executing", 0, 0, f_reg_executing},
811 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200812 {"reltime", 0, 2, f_reltime},
813#ifdef FEAT_FLOAT
814 {"reltimefloat", 1, 1, f_reltimefloat},
815#endif
816 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100817 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818 {"remote_foreground", 1, 1, f_remote_foreground},
819 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100820 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100822 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823 {"remove", 2, 3, f_remove},
824 {"rename", 2, 2, f_rename},
825 {"repeat", 2, 2, f_repeat},
826 {"resolve", 1, 1, f_resolve},
827 {"reverse", 1, 1, f_reverse},
828#ifdef FEAT_FLOAT
829 {"round", 1, 1, f_round},
830#endif
831 {"screenattr", 2, 2, f_screenattr},
832 {"screenchar", 2, 2, f_screenchar},
833 {"screencol", 0, 0, f_screencol},
834 {"screenrow", 0, 0, f_screenrow},
835 {"search", 1, 4, f_search},
836 {"searchdecl", 1, 3, f_searchdecl},
837 {"searchpair", 3, 7, f_searchpair},
838 {"searchpairpos", 3, 7, f_searchpairpos},
839 {"searchpos", 1, 4, f_searchpos},
840 {"server2client", 2, 2, f_server2client},
841 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200842 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"setbufvar", 3, 3, f_setbufvar},
844 {"setcharsearch", 1, 1, f_setcharsearch},
845 {"setcmdpos", 1, 1, f_setcmdpos},
846 {"setfperm", 2, 2, f_setfperm},
847 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200848 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"setmatches", 1, 1, f_setmatches},
850 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200851 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"setreg", 2, 3, f_setreg},
853 {"settabvar", 3, 3, f_settabvar},
854 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100855 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"setwinvar", 3, 3, f_setwinvar},
857#ifdef FEAT_CRYPT
858 {"sha256", 1, 1, f_sha256},
859#endif
860 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100861 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100862#ifdef FEAT_SIGNS
863 {"sign_define", 1, 2, f_sign_define},
864 {"sign_getdefined", 0, 1, f_sign_getdefined},
865 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100866 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100867 {"sign_place", 4, 5, f_sign_place},
868 {"sign_undefine", 0, 1, f_sign_undefine},
869 {"sign_unplace", 1, 2, f_sign_unplace},
870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"simplify", 1, 1, f_simplify},
872#ifdef FEAT_FLOAT
873 {"sin", 1, 1, f_sin},
874 {"sinh", 1, 1, f_sinh},
875#endif
876 {"sort", 1, 3, f_sort},
877 {"soundfold", 1, 1, f_soundfold},
878 {"spellbadword", 0, 1, f_spellbadword},
879 {"spellsuggest", 1, 3, f_spellsuggest},
880 {"split", 1, 3, f_split},
881#ifdef FEAT_FLOAT
882 {"sqrt", 1, 1, f_sqrt},
883 {"str2float", 1, 1, f_str2float},
884#endif
885 {"str2nr", 1, 2, f_str2nr},
886 {"strcharpart", 2, 3, f_strcharpart},
887 {"strchars", 1, 2, f_strchars},
888 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
889#ifdef HAVE_STRFTIME
890 {"strftime", 1, 2, f_strftime},
891#endif
892 {"strgetchar", 2, 2, f_strgetchar},
893 {"stridx", 2, 3, f_stridx},
894 {"string", 1, 1, f_string},
895 {"strlen", 1, 1, f_strlen},
896 {"strpart", 2, 3, f_strpart},
897 {"strridx", 2, 3, f_strridx},
898 {"strtrans", 1, 1, f_strtrans},
899 {"strwidth", 1, 1, f_strwidth},
900 {"submatch", 1, 2, f_submatch},
901 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200902 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200903 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"synID", 3, 3, f_synID},
905 {"synIDattr", 2, 3, f_synIDattr},
906 {"synIDtrans", 1, 1, f_synIDtrans},
907 {"synconcealed", 2, 2, f_synconcealed},
908 {"synstack", 2, 2, f_synstack},
909 {"system", 1, 2, f_system},
910 {"systemlist", 1, 2, f_systemlist},
911 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
912 {"tabpagenr", 0, 1, f_tabpagenr},
913 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
914 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100915 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916#ifdef FEAT_FLOAT
917 {"tan", 1, 1, f_tan},
918 {"tanh", 1, 1, f_tanh},
919#endif
920 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200921#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100922 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
923 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100924 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200925 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200926# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
927 {"term_getansicolors", 1, 1, f_term_getansicolors},
928# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200929 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200930 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200931 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200932 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200933 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200934 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200935 {"term_getstatus", 1, 1, f_term_getstatus},
936 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200937 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200938 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200939 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200940 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200941# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
942 {"term_setansicolors", 2, 2, f_term_setansicolors},
943# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100944 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100945 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200946 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200947 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200948 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200949#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
951 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200952 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100954 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100955 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956#ifdef FEAT_JOB_CHANNEL
957 {"test_null_channel", 0, 0, f_test_null_channel},
958#endif
959 {"test_null_dict", 0, 0, f_test_null_dict},
960#ifdef FEAT_JOB_CHANNEL
961 {"test_null_job", 0, 0, f_test_null_job},
962#endif
963 {"test_null_list", 0, 0, f_test_null_list},
964 {"test_null_partial", 0, 0, f_test_null_partial},
965 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200966 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100967 {"test_override", 2, 2, f_test_override},
Bram Moolenaarab186732018-09-14 21:27:06 +0200968#ifdef FEAT_GUI
969 {"test_scrollbar", 3, 3, f_test_scrollbar},
970#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"test_settime", 1, 1, f_test_settime},
972#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200973 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200974 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200975 {"timer_start", 2, 3, f_timer_start},
976 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200977 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978#endif
979 {"tolower", 1, 1, f_tolower},
980 {"toupper", 1, 1, f_toupper},
981 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100982 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983#ifdef FEAT_FLOAT
984 {"trunc", 1, 1, f_trunc},
985#endif
986 {"type", 1, 1, f_type},
987 {"undofile", 1, 1, f_undofile},
988 {"undotree", 0, 0, f_undotree},
989 {"uniq", 1, 3, f_uniq},
990 {"values", 1, 1, f_values},
991 {"virtcol", 1, 1, f_virtcol},
992 {"visualmode", 0, 1, f_visualmode},
993 {"wildmenumode", 0, 0, f_wildmenumode},
994 {"win_findbuf", 1, 1, f_win_findbuf},
995 {"win_getid", 0, 2, f_win_getid},
996 {"win_gotoid", 1, 1, f_win_gotoid},
997 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
998 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100999 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"winbufnr", 1, 1, f_winbufnr},
1001 {"wincol", 0, 0, f_wincol},
1002 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001003 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001004 {"winline", 0, 0, f_winline},
1005 {"winnr", 0, 1, f_winnr},
1006 {"winrestcmd", 0, 0, f_winrestcmd},
1007 {"winrestview", 1, 1, f_winrestview},
1008 {"winsaveview", 0, 0, f_winsaveview},
1009 {"winwidth", 1, 1, f_winwidth},
1010 {"wordcount", 0, 0, f_wordcount},
1011 {"writefile", 2, 3, f_writefile},
1012 {"xor", 2, 2, f_xor},
1013};
1014
1015#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1016
1017/*
1018 * Function given to ExpandGeneric() to obtain the list of internal
1019 * or user defined function names.
1020 */
1021 char_u *
1022get_function_name(expand_T *xp, int idx)
1023{
1024 static int intidx = -1;
1025 char_u *name;
1026
1027 if (idx == 0)
1028 intidx = -1;
1029 if (intidx < 0)
1030 {
1031 name = get_user_func_name(xp, idx);
1032 if (name != NULL)
1033 return name;
1034 }
1035 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1036 {
1037 STRCPY(IObuff, functions[intidx].f_name);
1038 STRCAT(IObuff, "(");
1039 if (functions[intidx].f_max_argc == 0)
1040 STRCAT(IObuff, ")");
1041 return IObuff;
1042 }
1043
1044 return NULL;
1045}
1046
1047/*
1048 * Function given to ExpandGeneric() to obtain the list of internal or
1049 * user defined variable or function names.
1050 */
1051 char_u *
1052get_expr_name(expand_T *xp, int idx)
1053{
1054 static int intidx = -1;
1055 char_u *name;
1056
1057 if (idx == 0)
1058 intidx = -1;
1059 if (intidx < 0)
1060 {
1061 name = get_function_name(xp, idx);
1062 if (name != NULL)
1063 return name;
1064 }
1065 return get_user_var_name(xp, ++intidx);
1066}
1067
1068#endif /* FEAT_CMDL_COMPL */
1069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001070/*
1071 * Find internal function in table above.
1072 * Return index, or -1 if not found
1073 */
1074 int
1075find_internal_func(
1076 char_u *name) /* name of the function */
1077{
1078 int first = 0;
1079 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1080 int cmp;
1081 int x;
1082
1083 /*
1084 * Find the function name in the table. Binary search.
1085 */
1086 while (first <= last)
1087 {
1088 x = first + ((unsigned)(last - first) >> 1);
1089 cmp = STRCMP(name, functions[x].f_name);
1090 if (cmp < 0)
1091 last = x - 1;
1092 else if (cmp > 0)
1093 first = x + 1;
1094 else
1095 return x;
1096 }
1097 return -1;
1098}
1099
1100 int
1101call_internal_func(
1102 char_u *name,
1103 int argcount,
1104 typval_T *argvars,
1105 typval_T *rettv)
1106{
1107 int i;
1108
1109 i = find_internal_func(name);
1110 if (i < 0)
1111 return ERROR_UNKNOWN;
1112 if (argcount < functions[i].f_min_argc)
1113 return ERROR_TOOFEW;
1114 if (argcount > functions[i].f_max_argc)
1115 return ERROR_TOOMANY;
1116 argvars[argcount].v_type = VAR_UNKNOWN;
1117 functions[i].f_func(argvars, rettv);
1118 return ERROR_NONE;
1119}
1120
1121/*
1122 * Return TRUE for a non-zero Number and a non-empty String.
1123 */
1124 static int
1125non_zero_arg(typval_T *argvars)
1126{
1127 return ((argvars[0].v_type == VAR_NUMBER
1128 && argvars[0].vval.v_number != 0)
1129 || (argvars[0].v_type == VAR_SPECIAL
1130 && argvars[0].vval.v_number == VVAL_TRUE)
1131 || (argvars[0].v_type == VAR_STRING
1132 && argvars[0].vval.v_string != NULL
1133 && *argvars[0].vval.v_string != NUL));
1134}
1135
1136/*
1137 * Get the lnum from the first argument.
1138 * Also accepts ".", "$", etc., but that only works for the current buffer.
1139 * Returns -1 on error.
1140 */
1141 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001142tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001143{
1144 typval_T rettv;
1145 linenr_T lnum;
1146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001147 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001148 if (lnum == 0) /* no valid number, try using line() */
1149 {
1150 rettv.v_type = VAR_NUMBER;
1151 f_line(argvars, &rettv);
1152 lnum = (linenr_T)rettv.vval.v_number;
1153 clear_tv(&rettv);
1154 }
1155 return lnum;
1156}
1157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001158/*
1159 * Get the lnum from the first argument.
1160 * Also accepts "$", then "buf" is used.
1161 * Returns 0 on error.
1162 */
1163 static linenr_T
1164tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1165{
1166 if (argvars[0].v_type == VAR_STRING
1167 && argvars[0].vval.v_string != NULL
1168 && argvars[0].vval.v_string[0] == '$'
1169 && buf != NULL)
1170 return buf->b_ml.ml_line_count;
1171 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1172}
1173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001174#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001175/*
1176 * Get the float value of "argvars[0]" into "f".
1177 * Returns FAIL when the argument is not a Number or Float.
1178 */
1179 static int
1180get_float_arg(typval_T *argvars, float_T *f)
1181{
1182 if (argvars[0].v_type == VAR_FLOAT)
1183 {
1184 *f = argvars[0].vval.v_float;
1185 return OK;
1186 }
1187 if (argvars[0].v_type == VAR_NUMBER)
1188 {
1189 *f = (float_T)argvars[0].vval.v_number;
1190 return OK;
1191 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001193 return FAIL;
1194}
1195
1196/*
1197 * "abs(expr)" function
1198 */
1199 static void
1200f_abs(typval_T *argvars, typval_T *rettv)
1201{
1202 if (argvars[0].v_type == VAR_FLOAT)
1203 {
1204 rettv->v_type = VAR_FLOAT;
1205 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1206 }
1207 else
1208 {
1209 varnumber_T n;
1210 int error = FALSE;
1211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001213 if (error)
1214 rettv->vval.v_number = -1;
1215 else if (n > 0)
1216 rettv->vval.v_number = n;
1217 else
1218 rettv->vval.v_number = -n;
1219 }
1220}
1221
1222/*
1223 * "acos()" function
1224 */
1225 static void
1226f_acos(typval_T *argvars, typval_T *rettv)
1227{
1228 float_T f = 0.0;
1229
1230 rettv->v_type = VAR_FLOAT;
1231 if (get_float_arg(argvars, &f) == OK)
1232 rettv->vval.v_float = acos(f);
1233 else
1234 rettv->vval.v_float = 0.0;
1235}
1236#endif
1237
1238/*
1239 * "add(list, item)" function
1240 */
1241 static void
1242f_add(typval_T *argvars, typval_T *rettv)
1243{
1244 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001245 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246
1247 rettv->vval.v_number = 1; /* Default: Failed */
1248 if (argvars[0].v_type == VAR_LIST)
1249 {
1250 if ((l = argvars[0].vval.v_list) != NULL
1251 && !tv_check_lock(l->lv_lock,
1252 (char_u *)N_("add() argument"), TRUE)
1253 && list_append_tv(l, &argvars[1]) == OK)
1254 copy_tv(&argvars[0], rettv);
1255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001256 else if (argvars[0].v_type == VAR_BLOB)
1257 {
1258 if ((b = argvars[0].vval.v_blob) != NULL
1259 && !tv_check_lock(b->bv_lock,
1260 (char_u *)N_("add() argument"), TRUE))
1261 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001262 int error = FALSE;
1263 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1264
1265 if (!error)
1266 {
1267 ga_append(&b->bv_ga, (int)n);
1268 copy_tv(&argvars[0], rettv);
1269 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001270 }
1271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001272 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001273 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001274}
1275
1276/*
1277 * "and(expr, expr)" function
1278 */
1279 static void
1280f_and(typval_T *argvars, typval_T *rettv)
1281{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001282 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1283 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001284}
1285
1286/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001287 * If there is a window for "curbuf", make it the current window.
1288 */
1289 static void
1290find_win_for_curbuf(void)
1291{
1292 wininfo_T *wip;
1293
1294 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1295 {
1296 if (wip->wi_win != NULL)
1297 {
1298 curwin = wip->wi_win;
1299 break;
1300 }
1301 }
1302}
1303
1304/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001305 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 */
1307 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001308set_buffer_lines(
1309 buf_T *buf,
1310 linenr_T lnum_arg,
1311 int append,
1312 typval_T *lines,
1313 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314{
Bram Moolenaarca851592018-06-06 21:04:07 +02001315 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1316 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317 list_T *l = NULL;
1318 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001320 linenr_T append_lnum;
1321 buf_T *curbuf_save = NULL;
1322 win_T *curwin_save = NULL;
1323 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001324
Bram Moolenaarca851592018-06-06 21:04:07 +02001325 /* When using the current buffer ml_mfp will be set if needed. Useful when
1326 * setline() is used on startup. For other buffers the buffer must be
1327 * loaded. */
1328 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001329 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001330 rettv->vval.v_number = 1; /* FAIL */
1331 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332 }
1333
Bram Moolenaarca851592018-06-06 21:04:07 +02001334 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001335 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001336 curbuf_save = curbuf;
1337 curwin_save = curwin;
1338 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001339 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001340 }
1341
1342 if (append)
1343 // appendbufline() uses the line number below which we insert
1344 append_lnum = lnum - 1;
1345 else
1346 // setbufline() uses the line number above which we insert, we only
1347 // append if it's below the last line
1348 append_lnum = curbuf->b_ml.ml_line_count;
1349
1350 if (lines->v_type == VAR_LIST)
1351 {
1352 l = lines->vval.v_list;
1353 li = l->lv_first;
1354 }
1355 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001356 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001357
1358 /* default result is zero == OK */
1359 for (;;)
1360 {
1361 if (l != NULL)
1362 {
1363 /* list argument, get next string */
1364 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001366 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367 li = li->li_next;
1368 }
1369
Bram Moolenaarca851592018-06-06 21:04:07 +02001370 rettv->vval.v_number = 1; /* FAIL */
1371 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1372 break;
1373
1374 /* When coming here from Insert mode, sync undo, so that this can be
1375 * undone separately from what was previously inserted. */
1376 if (u_sync_once == 2)
1377 {
1378 u_sync_once = 1; /* notify that u_sync() was called */
1379 u_sync(TRUE);
1380 }
1381
1382 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1383 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001384 // Existing line, replace it.
1385 // Removes any existing text properties.
1386 if (u_savesub(lnum) == OK && ml_replace_len(
1387 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 {
1389 changed_bytes(lnum, 0);
1390 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1391 check_cursor_col();
1392 rettv->vval.v_number = 0; /* OK */
1393 }
1394 }
1395 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1396 {
1397 /* append the line */
1398 ++added;
1399 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1400 rettv->vval.v_number = 0; /* OK */
1401 }
1402
1403 if (l == NULL) /* only one string argument */
1404 break;
1405 ++lnum;
1406 }
1407
1408 if (added > 0)
1409 {
1410 win_T *wp;
1411 tabpage_T *tp;
1412
1413 appended_lines_mark(append_lnum, added);
1414 FOR_ALL_TAB_WINDOWS(tp, wp)
1415 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1416 wp->w_cursor.lnum += added;
1417 check_cursor_col();
1418
Bram Moolenaarf2732452018-06-03 14:47:35 +02001419#ifdef FEAT_JOB_CHANNEL
1420 if (bt_prompt(curbuf) && (State & INSERT))
1421 // show the line with the prompt
1422 update_topline();
1423#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001424 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001425
1426 if (!is_curbuf)
1427 {
1428 curbuf = curbuf_save;
1429 curwin = curwin_save;
1430 }
1431}
1432
1433/*
1434 * "append(lnum, string/list)" function
1435 */
1436 static void
1437f_append(typval_T *argvars, typval_T *rettv)
1438{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001440
1441 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1442}
1443
1444/*
1445 * "appendbufline(buf, lnum, string/list)" function
1446 */
1447 static void
1448f_appendbufline(typval_T *argvars, typval_T *rettv)
1449{
1450 linenr_T lnum;
1451 buf_T *buf;
1452
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001453 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001454 if (buf == NULL)
1455 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001456 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001458 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001459 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001461}
1462
1463/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001464 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001465 */
1466 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001467f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001468{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001469 win_T *wp;
1470
1471 if (argvars[0].v_type == VAR_UNKNOWN)
1472 // use the current window
1473 rettv->vval.v_number = ARGCOUNT;
1474 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001475 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001476 // use the global argument list
1477 rettv->vval.v_number = GARGCOUNT;
1478 else
1479 {
1480 // use the argument list of the specified window
1481 wp = find_win_by_nr_or_id(&argvars[0]);
1482 if (wp != NULL)
1483 rettv->vval.v_number = WARGCOUNT(wp);
1484 else
1485 rettv->vval.v_number = -1;
1486 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001487}
1488
1489/*
1490 * "argidx()" function
1491 */
1492 static void
1493f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1494{
1495 rettv->vval.v_number = curwin->w_arg_idx;
1496}
1497
1498/*
1499 * "arglistid()" function
1500 */
1501 static void
1502f_arglistid(typval_T *argvars, typval_T *rettv)
1503{
1504 win_T *wp;
1505
1506 rettv->vval.v_number = -1;
1507 wp = find_tabwin(&argvars[0], &argvars[1]);
1508 if (wp != NULL)
1509 rettv->vval.v_number = wp->w_alist->id;
1510}
1511
1512/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513 * Get the argument list for a given window
1514 */
1515 static void
1516get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1517{
1518 int idx;
1519
1520 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1521 for (idx = 0; idx < argcount; ++idx)
1522 list_append_string(rettv->vval.v_list,
1523 alist_name(&arglist[idx]), -1);
1524}
1525
1526/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527 * "argv(nr)" function
1528 */
1529 static void
1530f_argv(typval_T *argvars, typval_T *rettv)
1531{
1532 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001533 aentry_T *arglist = NULL;
1534 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535
1536 if (argvars[0].v_type != VAR_UNKNOWN)
1537 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538 if (argvars[1].v_type == VAR_UNKNOWN)
1539 {
1540 arglist = ARGLIST;
1541 argcount = ARGCOUNT;
1542 }
1543 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001544 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001545 {
1546 arglist = GARGLIST;
1547 argcount = GARGCOUNT;
1548 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001549 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001550 {
1551 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1552
1553 if (wp != NULL)
1554 {
1555 /* Use the argument list of the specified window */
1556 arglist = WARGLIST(wp);
1557 argcount = WARGCOUNT(wp);
1558 }
1559 }
1560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001562 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001563 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001564 if (arglist != NULL && idx >= 0 && idx < argcount)
1565 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1566 else if (idx == -1)
1567 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001568 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 else
1570 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001571}
1572
1573/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001574 * "assert_beeps(cmd [, error])" function
1575 */
1576 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001577f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001578{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001579 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001580}
1581
1582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 * "assert_equal(expected, actual[, msg])" function
1584 */
1585 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001586f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001587{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001588 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589}
1590
1591/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001592 * "assert_equalfile(fname-one, fname-two)" function
1593 */
1594 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001595f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001596{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001597 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001598}
1599
1600/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601 * "assert_notequal(expected, actual[, msg])" function
1602 */
1603 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001604f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001606 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607}
1608
1609/*
1610 * "assert_exception(string[, msg])" function
1611 */
1612 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001613f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001615 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616}
1617
1618/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001619 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 */
1621 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001622f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001623{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001624 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625}
1626
1627/*
1628 * "assert_false(actual[, msg])" function
1629 */
1630 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634}
1635
1636/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001637 * "assert_inrange(lower, upper[, msg])" function
1638 */
1639 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001641{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001643}
1644
1645/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001646 * "assert_match(pattern, actual[, msg])" function
1647 */
1648 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001652}
1653
1654/*
1655 * "assert_notmatch(pattern, actual[, msg])" function
1656 */
1657 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661}
1662
1663/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001664 * "assert_report(msg)" function
1665 */
1666 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001668{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001670}
1671
1672/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 * "assert_true(actual[, msg])" function
1674 */
1675 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679}
1680
1681#ifdef FEAT_FLOAT
1682/*
1683 * "asin()" function
1684 */
1685 static void
1686f_asin(typval_T *argvars, typval_T *rettv)
1687{
1688 float_T f = 0.0;
1689
1690 rettv->v_type = VAR_FLOAT;
1691 if (get_float_arg(argvars, &f) == OK)
1692 rettv->vval.v_float = asin(f);
1693 else
1694 rettv->vval.v_float = 0.0;
1695}
1696
1697/*
1698 * "atan()" function
1699 */
1700 static void
1701f_atan(typval_T *argvars, typval_T *rettv)
1702{
1703 float_T f = 0.0;
1704
1705 rettv->v_type = VAR_FLOAT;
1706 if (get_float_arg(argvars, &f) == OK)
1707 rettv->vval.v_float = atan(f);
1708 else
1709 rettv->vval.v_float = 0.0;
1710}
1711
1712/*
1713 * "atan2()" function
1714 */
1715 static void
1716f_atan2(typval_T *argvars, typval_T *rettv)
1717{
1718 float_T fx = 0.0, fy = 0.0;
1719
1720 rettv->v_type = VAR_FLOAT;
1721 if (get_float_arg(argvars, &fx) == OK
1722 && get_float_arg(&argvars[1], &fy) == OK)
1723 rettv->vval.v_float = atan2(fx, fy);
1724 else
1725 rettv->vval.v_float = 0.0;
1726}
1727#endif
1728
1729/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001730 * "balloon_show()" function
1731 */
1732#ifdef FEAT_BEVAL
1733 static void
1734f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1735{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001736 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001737 {
1738 if (argvars[0].v_type == VAR_LIST
1739# ifdef FEAT_GUI
1740 && !gui.in_use
1741# endif
1742 )
1743 post_balloon(balloonEval, NULL, argvars[0].vval.v_list);
1744 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001745 post_balloon(balloonEval, tv_get_string_chk(&argvars[0]), NULL);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001746 }
1747}
1748
Bram Moolenaar669a8282017-11-19 20:13:05 +01001749# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001750 static void
1751f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1752{
1753 if (rettv_list_alloc(rettv) == OK)
1754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001755 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001756
1757 if (msg != NULL)
1758 {
1759 pumitem_T *array;
1760 int size = split_message(msg, &array);
1761 int i;
1762
1763 /* Skip the first and last item, they are always empty. */
1764 for (i = 1; i < size - 1; ++i)
1765 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001766 while (size > 0)
1767 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001768 vim_free(array);
1769 }
1770 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001771}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001772# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001773#endif
1774
1775/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001776 * "browse(save, title, initdir, default)" function
1777 */
1778 static void
1779f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1780{
1781#ifdef FEAT_BROWSE
1782 int save;
1783 char_u *title;
1784 char_u *initdir;
1785 char_u *defname;
1786 char_u buf[NUMBUFLEN];
1787 char_u buf2[NUMBUFLEN];
1788 int error = FALSE;
1789
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001790 save = (int)tv_get_number_chk(&argvars[0], &error);
1791 title = tv_get_string_chk(&argvars[1]);
1792 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1793 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001794
1795 if (error || title == NULL || initdir == NULL || defname == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string =
1799 do_browse(save ? BROWSE_SAVE : 0,
1800 title, defname, NULL, initdir, NULL, curbuf);
1801#else
1802 rettv->vval.v_string = NULL;
1803#endif
1804 rettv->v_type = VAR_STRING;
1805}
1806
1807/*
1808 * "browsedir(title, initdir)" function
1809 */
1810 static void
1811f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1812{
1813#ifdef FEAT_BROWSE
1814 char_u *title;
1815 char_u *initdir;
1816 char_u buf[NUMBUFLEN];
1817
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001818 title = tv_get_string_chk(&argvars[0]);
1819 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001820
1821 if (title == NULL || initdir == NULL)
1822 rettv->vval.v_string = NULL;
1823 else
1824 rettv->vval.v_string = do_browse(BROWSE_DIR,
1825 title, NULL, NULL, initdir, NULL, curbuf);
1826#else
1827 rettv->vval.v_string = NULL;
1828#endif
1829 rettv->v_type = VAR_STRING;
1830}
1831
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001832/*
1833 * Find a buffer by number or exact name.
1834 */
1835 static buf_T *
1836find_buffer(typval_T *avar)
1837{
1838 buf_T *buf = NULL;
1839
1840 if (avar->v_type == VAR_NUMBER)
1841 buf = buflist_findnr((int)avar->vval.v_number);
1842 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1843 {
1844 buf = buflist_findname_exp(avar->vval.v_string);
1845 if (buf == NULL)
1846 {
1847 /* No full path name match, try a match with a URL or a "nofile"
1848 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001849 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001850 if (buf->b_fname != NULL
1851 && (path_with_url(buf->b_fname)
1852#ifdef FEAT_QUICKFIX
1853 || bt_nofile(buf)
1854#endif
1855 )
1856 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1857 break;
1858 }
1859 }
1860 return buf;
1861}
1862
1863/*
1864 * "bufexists(expr)" function
1865 */
1866 static void
1867f_bufexists(typval_T *argvars, typval_T *rettv)
1868{
1869 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1870}
1871
1872/*
1873 * "buflisted(expr)" function
1874 */
1875 static void
1876f_buflisted(typval_T *argvars, typval_T *rettv)
1877{
1878 buf_T *buf;
1879
1880 buf = find_buffer(&argvars[0]);
1881 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1882}
1883
1884/*
1885 * "bufloaded(expr)" function
1886 */
1887 static void
1888f_bufloaded(typval_T *argvars, typval_T *rettv)
1889{
1890 buf_T *buf;
1891
1892 buf = find_buffer(&argvars[0]);
1893 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1894}
1895
1896 buf_T *
1897buflist_find_by_name(char_u *name, int curtab_only)
1898{
1899 int save_magic;
1900 char_u *save_cpo;
1901 buf_T *buf;
1902
1903 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1904 save_magic = p_magic;
1905 p_magic = TRUE;
1906 save_cpo = p_cpo;
1907 p_cpo = (char_u *)"";
1908
1909 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1910 TRUE, FALSE, curtab_only));
1911
1912 p_magic = save_magic;
1913 p_cpo = save_cpo;
1914 return buf;
1915}
1916
1917/*
1918 * Get buffer by number or pattern.
1919 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001920 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001921tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001922{
1923 char_u *name = tv->vval.v_string;
1924 buf_T *buf;
1925
1926 if (tv->v_type == VAR_NUMBER)
1927 return buflist_findnr((int)tv->vval.v_number);
1928 if (tv->v_type != VAR_STRING)
1929 return NULL;
1930 if (name == NULL || *name == NUL)
1931 return curbuf;
1932 if (name[0] == '$' && name[1] == NUL)
1933 return lastbuf;
1934
1935 buf = buflist_find_by_name(name, curtab_only);
1936
1937 /* If not found, try expanding the name, like done for bufexists(). */
1938 if (buf == NULL)
1939 buf = find_buffer(tv);
1940
1941 return buf;
1942}
1943
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001944#ifdef FEAT_SIGNS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001945/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001946 * Get the buffer from "arg" and give an error and return NULL if it is not
1947 * valid.
1948 */
1949 static buf_T *
1950get_buf_arg(typval_T *arg)
1951{
1952 buf_T *buf;
1953
1954 ++emsg_off;
1955 buf = tv_get_buf(arg, FALSE);
1956 --emsg_off;
1957 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001959 return buf;
1960}
Bram Moolenaarec9d3002019-01-12 13:50:31 +01001961#endif
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01001962
1963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001964 * "bufname(expr)" function
1965 */
1966 static void
1967f_bufname(typval_T *argvars, typval_T *rettv)
1968{
1969 buf_T *buf;
1970
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001971 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001972 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001973 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001974 rettv->v_type = VAR_STRING;
1975 if (buf != NULL && buf->b_fname != NULL)
1976 rettv->vval.v_string = vim_strsave(buf->b_fname);
1977 else
1978 rettv->vval.v_string = NULL;
1979 --emsg_off;
1980}
1981
1982/*
1983 * "bufnr(expr)" function
1984 */
1985 static void
1986f_bufnr(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_T *buf;
1989 int error = FALSE;
1990 char_u *name;
1991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001992 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001994 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001995 --emsg_off;
1996
1997 /* If the buffer isn't found and the second argument is not zero create a
1998 * new buffer. */
1999 if (buf == NULL
2000 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002001 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002002 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002003 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002004 && !error)
2005 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2006
2007 if (buf != NULL)
2008 rettv->vval.v_number = buf->b_fnum;
2009 else
2010 rettv->vval.v_number = -1;
2011}
2012
2013 static void
2014buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2015{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002016 win_T *wp;
2017 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018 buf_T *buf;
2019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002020 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002021 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002022 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002023 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002024 {
2025 ++winnr;
2026 if (wp->w_buffer == buf)
2027 break;
2028 }
2029 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002030 --emsg_off;
2031}
2032
2033/*
2034 * "bufwinid(nr)" function
2035 */
2036 static void
2037f_bufwinid(typval_T *argvars, typval_T *rettv)
2038{
2039 buf_win_common(argvars, rettv, FALSE);
2040}
2041
2042/*
2043 * "bufwinnr(nr)" function
2044 */
2045 static void
2046f_bufwinnr(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_win_common(argvars, rettv, TRUE);
2049}
2050
2051/*
2052 * "byte2line(byte)" function
2053 */
2054 static void
2055f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2056{
2057#ifndef FEAT_BYTEOFF
2058 rettv->vval.v_number = -1;
2059#else
2060 long boff = 0;
2061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 if (boff < 0)
2064 rettv->vval.v_number = -1;
2065 else
2066 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2067 (linenr_T)0, &boff);
2068#endif
2069}
2070
2071 static void
2072byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2073{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002075 char_u *str;
2076 varnumber_T idx;
2077
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 str = tv_get_string_chk(&argvars[0]);
2079 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 rettv->vval.v_number = -1;
2081 if (str == NULL || idx < 0)
2082 return;
2083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 t = str;
2085 for ( ; idx > 0; idx--)
2086 {
2087 if (*t == NUL) /* EOL reached */
2088 return;
2089 if (enc_utf8 && comp)
2090 t += utf_ptr2len(t);
2091 else
2092 t += (*mb_ptr2len)(t);
2093 }
2094 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095}
2096
2097/*
2098 * "byteidx()" function
2099 */
2100 static void
2101f_byteidx(typval_T *argvars, typval_T *rettv)
2102{
2103 byteidx(argvars, rettv, FALSE);
2104}
2105
2106/*
2107 * "byteidxcomp()" function
2108 */
2109 static void
2110f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2111{
2112 byteidx(argvars, rettv, TRUE);
2113}
2114
2115/*
2116 * "call(func, arglist [, dict])" function
2117 */
2118 static void
2119f_call(typval_T *argvars, typval_T *rettv)
2120{
2121 char_u *func;
2122 partial_T *partial = NULL;
2123 dict_T *selfdict = NULL;
2124
2125 if (argvars[1].v_type != VAR_LIST)
2126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002127 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002128 return;
2129 }
2130 if (argvars[1].vval.v_list == NULL)
2131 return;
2132
2133 if (argvars[0].v_type == VAR_FUNC)
2134 func = argvars[0].vval.v_string;
2135 else if (argvars[0].v_type == VAR_PARTIAL)
2136 {
2137 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002138 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 }
2140 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 if (*func == NUL)
2143 return; /* type error or empty name */
2144
2145 if (argvars[2].v_type != VAR_UNKNOWN)
2146 {
2147 if (argvars[2].v_type != VAR_DICT)
2148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 return;
2151 }
2152 selfdict = argvars[2].vval.v_dict;
2153 }
2154
2155 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2156}
2157
2158#ifdef FEAT_FLOAT
2159/*
2160 * "ceil({float})" function
2161 */
2162 static void
2163f_ceil(typval_T *argvars, typval_T *rettv)
2164{
2165 float_T f = 0.0;
2166
2167 rettv->v_type = VAR_FLOAT;
2168 if (get_float_arg(argvars, &f) == OK)
2169 rettv->vval.v_float = ceil(f);
2170 else
2171 rettv->vval.v_float = 0.0;
2172}
2173#endif
2174
2175#ifdef FEAT_JOB_CHANNEL
2176/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002177 * "ch_canread()" function
2178 */
2179 static void
2180f_ch_canread(typval_T *argvars, typval_T *rettv)
2181{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002182 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002183
2184 rettv->vval.v_number = 0;
2185 if (channel != NULL)
2186 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2187 || channel_has_readahead(channel, PART_OUT)
2188 || channel_has_readahead(channel, PART_ERR);
2189}
2190
2191/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002192 * "ch_close()" function
2193 */
2194 static void
2195f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2196{
2197 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2198
2199 if (channel != NULL)
2200 {
2201 channel_close(channel, FALSE);
2202 channel_clear(channel);
2203 }
2204}
2205
2206/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002207 * "ch_close()" function
2208 */
2209 static void
2210f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2211{
2212 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2213
2214 if (channel != NULL)
2215 channel_close_in(channel);
2216}
2217
2218/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 * "ch_getbufnr()" function
2220 */
2221 static void
2222f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2223{
2224 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2225
2226 rettv->vval.v_number = -1;
2227 if (channel != NULL)
2228 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002229 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 int part;
2231
2232 if (STRCMP(what, "err") == 0)
2233 part = PART_ERR;
2234 else if (STRCMP(what, "out") == 0)
2235 part = PART_OUT;
2236 else if (STRCMP(what, "in") == 0)
2237 part = PART_IN;
2238 else
2239 part = PART_SOCK;
2240 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2241 rettv->vval.v_number =
2242 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2243 }
2244}
2245
2246/*
2247 * "ch_getjob()" function
2248 */
2249 static void
2250f_ch_getjob(typval_T *argvars, typval_T *rettv)
2251{
2252 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2253
2254 if (channel != NULL)
2255 {
2256 rettv->v_type = VAR_JOB;
2257 rettv->vval.v_job = channel->ch_job;
2258 if (channel->ch_job != NULL)
2259 ++channel->ch_job->jv_refcount;
2260 }
2261}
2262
2263/*
2264 * "ch_info()" function
2265 */
2266 static void
2267f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2268{
2269 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2270
2271 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2272 channel_info(channel, rettv->vval.v_dict);
2273}
2274
2275/*
2276 * "ch_log()" function
2277 */
2278 static void
2279f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2280{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 channel_T *channel = NULL;
2283
2284 if (argvars[1].v_type != VAR_UNKNOWN)
2285 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2286
Bram Moolenaard5359b22018-04-05 22:44:39 +02002287 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288}
2289
2290/*
2291 * "ch_logfile()" function
2292 */
2293 static void
2294f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2295{
2296 char_u *fname;
2297 char_u *opt = (char_u *)"";
2298 char_u buf[NUMBUFLEN];
2299
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002300 /* Don't open a file in restricted mode. */
2301 if (check_restricted() || check_secure())
2302 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002303 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002304 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 ch_logfile(fname, opt);
2307}
2308
2309/*
2310 * "ch_open()" function
2311 */
2312 static void
2313f_ch_open(typval_T *argvars, typval_T *rettv)
2314{
2315 rettv->v_type = VAR_CHANNEL;
2316 if (check_restricted() || check_secure())
2317 return;
2318 rettv->vval.v_channel = channel_open_func(argvars);
2319}
2320
2321/*
2322 * "ch_read()" function
2323 */
2324 static void
2325f_ch_read(typval_T *argvars, typval_T *rettv)
2326{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002327 common_channel_read(argvars, rettv, FALSE, FALSE);
2328}
2329
2330/*
2331 * "ch_readblob()" function
2332 */
2333 static void
2334f_ch_readblob(typval_T *argvars, typval_T *rettv)
2335{
2336 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002337}
2338
2339/*
2340 * "ch_readraw()" function
2341 */
2342 static void
2343f_ch_readraw(typval_T *argvars, typval_T *rettv)
2344{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002345 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002346}
2347
2348/*
2349 * "ch_evalexpr()" function
2350 */
2351 static void
2352f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2353{
2354 ch_expr_common(argvars, rettv, TRUE);
2355}
2356
2357/*
2358 * "ch_sendexpr()" function
2359 */
2360 static void
2361f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2362{
2363 ch_expr_common(argvars, rettv, FALSE);
2364}
2365
2366/*
2367 * "ch_evalraw()" function
2368 */
2369 static void
2370f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2371{
2372 ch_raw_common(argvars, rettv, TRUE);
2373}
2374
2375/*
2376 * "ch_sendraw()" function
2377 */
2378 static void
2379f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2380{
2381 ch_raw_common(argvars, rettv, FALSE);
2382}
2383
2384/*
2385 * "ch_setoptions()" function
2386 */
2387 static void
2388f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2389{
2390 channel_T *channel;
2391 jobopt_T opt;
2392
2393 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2394 if (channel == NULL)
2395 return;
2396 clear_job_options(&opt);
2397 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002398 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002399 channel_set_options(channel, &opt);
2400 free_job_options(&opt);
2401}
2402
2403/*
2404 * "ch_status()" function
2405 */
2406 static void
2407f_ch_status(typval_T *argvars, typval_T *rettv)
2408{
2409 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002410 jobopt_T opt;
2411 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002412
2413 /* return an empty string by default */
2414 rettv->v_type = VAR_STRING;
2415 rettv->vval.v_string = NULL;
2416
2417 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002418
2419 if (argvars[1].v_type != VAR_UNKNOWN)
2420 {
2421 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002422 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002423 && (opt.jo_set & JO_PART))
2424 part = opt.jo_part;
2425 }
2426
2427 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428}
2429#endif
2430
2431/*
2432 * "changenr()" function
2433 */
2434 static void
2435f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2436{
2437 rettv->vval.v_number = curbuf->b_u_seq_cur;
2438}
2439
2440/*
2441 * "char2nr(string)" function
2442 */
2443 static void
2444f_char2nr(typval_T *argvars, typval_T *rettv)
2445{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002446 if (has_mbyte)
2447 {
2448 int utf8 = 0;
2449
2450 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002451 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002452
2453 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002454 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002455 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002456 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002457 }
2458 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002459 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460}
2461
2462/*
2463 * "cindent(lnum)" function
2464 */
2465 static void
2466f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2467{
2468#ifdef FEAT_CINDENT
2469 pos_T pos;
2470 linenr_T lnum;
2471
2472 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002473 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002474 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2475 {
2476 curwin->w_cursor.lnum = lnum;
2477 rettv->vval.v_number = get_c_indent();
2478 curwin->w_cursor = pos;
2479 }
2480 else
2481#endif
2482 rettv->vval.v_number = -1;
2483}
2484
2485/*
2486 * "clearmatches()" function
2487 */
2488 static void
2489f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2490{
2491#ifdef FEAT_SEARCH_EXTRA
2492 clear_matches(curwin);
2493#endif
2494}
2495
2496/*
2497 * "col(string)" function
2498 */
2499 static void
2500f_col(typval_T *argvars, typval_T *rettv)
2501{
2502 colnr_T col = 0;
2503 pos_T *fp;
2504 int fnum = curbuf->b_fnum;
2505
2506 fp = var2fpos(&argvars[0], FALSE, &fnum);
2507 if (fp != NULL && fnum == curbuf->b_fnum)
2508 {
2509 if (fp->col == MAXCOL)
2510 {
2511 /* '> can be MAXCOL, get the length of the line then */
2512 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2513 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2514 else
2515 col = MAXCOL;
2516 }
2517 else
2518 {
2519 col = fp->col + 1;
2520#ifdef FEAT_VIRTUALEDIT
2521 /* col(".") when the cursor is on the NUL at the end of the line
2522 * because of "coladd" can be seen as an extra column. */
2523 if (virtual_active() && fp == &curwin->w_cursor)
2524 {
2525 char_u *p = ml_get_cursor();
2526
2527 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2528 curwin->w_virtcol - curwin->w_cursor.coladd))
2529 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002530 int l;
2531
2532 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2533 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 }
2535 }
2536#endif
2537 }
2538 }
2539 rettv->vval.v_number = col;
2540}
2541
2542#if defined(FEAT_INS_EXPAND)
2543/*
2544 * "complete()" function
2545 */
2546 static void
2547f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2548{
2549 int startcol;
2550
2551 if ((State & INSERT) == 0)
2552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002553 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554 return;
2555 }
2556
2557 /* Check for undo allowed here, because if something was already inserted
2558 * the line was already saved for undo and this check isn't done. */
2559 if (!undo_allowed())
2560 return;
2561
2562 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002565 return;
2566 }
2567
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002568 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002569 if (startcol <= 0)
2570 return;
2571
2572 set_completion(startcol - 1, argvars[1].vval.v_list);
2573}
2574
2575/*
2576 * "complete_add()" function
2577 */
2578 static void
2579f_complete_add(typval_T *argvars, typval_T *rettv)
2580{
2581 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2582}
2583
2584/*
2585 * "complete_check()" function
2586 */
2587 static void
2588f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2589{
2590 int saved = RedrawingDisabled;
2591
2592 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002593 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594 rettv->vval.v_number = compl_interrupted;
2595 RedrawingDisabled = saved;
2596}
2597#endif
2598
2599/*
2600 * "confirm(message, buttons[, default [, type]])" function
2601 */
2602 static void
2603f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2604{
2605#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2606 char_u *message;
2607 char_u *buttons = NULL;
2608 char_u buf[NUMBUFLEN];
2609 char_u buf2[NUMBUFLEN];
2610 int def = 1;
2611 int type = VIM_GENERIC;
2612 char_u *typestr;
2613 int error = FALSE;
2614
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002615 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002616 if (message == NULL)
2617 error = TRUE;
2618 if (argvars[1].v_type != VAR_UNKNOWN)
2619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002620 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621 if (buttons == NULL)
2622 error = TRUE;
2623 if (argvars[2].v_type != VAR_UNKNOWN)
2624 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002625 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002626 if (argvars[3].v_type != VAR_UNKNOWN)
2627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002628 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629 if (typestr == NULL)
2630 error = TRUE;
2631 else
2632 {
2633 switch (TOUPPER_ASC(*typestr))
2634 {
2635 case 'E': type = VIM_ERROR; break;
2636 case 'Q': type = VIM_QUESTION; break;
2637 case 'I': type = VIM_INFO; break;
2638 case 'W': type = VIM_WARNING; break;
2639 case 'G': type = VIM_GENERIC; break;
2640 }
2641 }
2642 }
2643 }
2644 }
2645
2646 if (buttons == NULL || *buttons == NUL)
2647 buttons = (char_u *)_("&Ok");
2648
2649 if (!error)
2650 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2651 def, NULL, FALSE);
2652#endif
2653}
2654
2655/*
2656 * "copy()" function
2657 */
2658 static void
2659f_copy(typval_T *argvars, typval_T *rettv)
2660{
2661 item_copy(&argvars[0], rettv, FALSE, 0);
2662}
2663
2664#ifdef FEAT_FLOAT
2665/*
2666 * "cos()" function
2667 */
2668 static void
2669f_cos(typval_T *argvars, typval_T *rettv)
2670{
2671 float_T f = 0.0;
2672
2673 rettv->v_type = VAR_FLOAT;
2674 if (get_float_arg(argvars, &f) == OK)
2675 rettv->vval.v_float = cos(f);
2676 else
2677 rettv->vval.v_float = 0.0;
2678}
2679
2680/*
2681 * "cosh()" function
2682 */
2683 static void
2684f_cosh(typval_T *argvars, typval_T *rettv)
2685{
2686 float_T f = 0.0;
2687
2688 rettv->v_type = VAR_FLOAT;
2689 if (get_float_arg(argvars, &f) == OK)
2690 rettv->vval.v_float = cosh(f);
2691 else
2692 rettv->vval.v_float = 0.0;
2693}
2694#endif
2695
2696/*
2697 * "count()" function
2698 */
2699 static void
2700f_count(typval_T *argvars, typval_T *rettv)
2701{
2702 long n = 0;
2703 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002704 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705
Bram Moolenaar9966b212017-07-28 16:46:57 +02002706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002707 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002708
2709 if (argvars[0].v_type == VAR_STRING)
2710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002711 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002712 char_u *p = argvars[0].vval.v_string;
2713 char_u *next;
2714
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002715 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002716 {
2717 if (ic)
2718 {
2719 size_t len = STRLEN(expr);
2720
2721 while (*p != NUL)
2722 {
2723 if (MB_STRNICMP(p, expr, len) == 0)
2724 {
2725 ++n;
2726 p += len;
2727 }
2728 else
2729 MB_PTR_ADV(p);
2730 }
2731 }
2732 else
2733 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2734 != NULL)
2735 {
2736 ++n;
2737 p = next + STRLEN(expr);
2738 }
2739 }
2740
2741 }
2742 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 {
2744 listitem_T *li;
2745 list_T *l;
2746 long idx;
2747
2748 if ((l = argvars[0].vval.v_list) != NULL)
2749 {
2750 li = l->lv_first;
2751 if (argvars[2].v_type != VAR_UNKNOWN)
2752 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753 if (argvars[3].v_type != VAR_UNKNOWN)
2754 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002755 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756 if (!error)
2757 {
2758 li = list_find(l, idx);
2759 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002760 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002761 }
2762 }
2763 if (error)
2764 li = NULL;
2765 }
2766
2767 for ( ; li != NULL; li = li->li_next)
2768 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2769 ++n;
2770 }
2771 }
2772 else if (argvars[0].v_type == VAR_DICT)
2773 {
2774 int todo;
2775 dict_T *d;
2776 hashitem_T *hi;
2777
2778 if ((d = argvars[0].vval.v_dict) != NULL)
2779 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (argvars[2].v_type != VAR_UNKNOWN)
2781 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002783 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002784 }
2785
2786 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2787 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2788 {
2789 if (!HASHITEM_EMPTY(hi))
2790 {
2791 --todo;
2792 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2793 ++n;
2794 }
2795 }
2796 }
2797 }
2798 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002799 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 rettv->vval.v_number = n;
2801}
2802
2803/*
2804 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2805 *
2806 * Checks the existence of a cscope connection.
2807 */
2808 static void
2809f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2810{
2811#ifdef FEAT_CSCOPE
2812 int num = 0;
2813 char_u *dbpath = NULL;
2814 char_u *prepend = NULL;
2815 char_u buf[NUMBUFLEN];
2816
2817 if (argvars[0].v_type != VAR_UNKNOWN
2818 && argvars[1].v_type != VAR_UNKNOWN)
2819 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002820 num = (int)tv_get_number(&argvars[0]);
2821 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002822 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002823 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002824 }
2825
2826 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2827#endif
2828}
2829
2830/*
2831 * "cursor(lnum, col)" function, or
2832 * "cursor(list)"
2833 *
2834 * Moves the cursor to the specified line and column.
2835 * Returns 0 when the position could be set, -1 otherwise.
2836 */
2837 static void
2838f_cursor(typval_T *argvars, typval_T *rettv)
2839{
2840 long line, col;
2841#ifdef FEAT_VIRTUALEDIT
2842 long coladd = 0;
2843#endif
2844 int set_curswant = TRUE;
2845
2846 rettv->vval.v_number = -1;
2847 if (argvars[1].v_type == VAR_UNKNOWN)
2848 {
2849 pos_T pos;
2850 colnr_T curswant = -1;
2851
2852 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2853 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002854 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002855 return;
2856 }
2857 line = pos.lnum;
2858 col = pos.col;
2859#ifdef FEAT_VIRTUALEDIT
2860 coladd = pos.coladd;
2861#endif
2862 if (curswant >= 0)
2863 {
2864 curwin->w_curswant = curswant - 1;
2865 set_curswant = FALSE;
2866 }
2867 }
2868 else
2869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002870 line = tv_get_lnum(argvars);
2871 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002872#ifdef FEAT_VIRTUALEDIT
2873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002874 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875#endif
2876 }
2877 if (line < 0 || col < 0
2878#ifdef FEAT_VIRTUALEDIT
2879 || coladd < 0
2880#endif
2881 )
2882 return; /* type error; errmsg already given */
2883 if (line > 0)
2884 curwin->w_cursor.lnum = line;
2885 if (col > 0)
2886 curwin->w_cursor.col = col - 1;
2887#ifdef FEAT_VIRTUALEDIT
2888 curwin->w_cursor.coladd = coladd;
2889#endif
2890
2891 /* Make sure the cursor is in a valid position. */
2892 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002893 /* Correct cursor for multi-byte character. */
2894 if (has_mbyte)
2895 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002896
2897 curwin->w_set_curswant = set_curswant;
2898 rettv->vval.v_number = 0;
2899}
2900
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002901#ifdef WIN3264
2902/*
2903 * "debugbreak()" function
2904 */
2905 static void
2906f_debugbreak(typval_T *argvars, typval_T *rettv)
2907{
2908 int pid;
2909
2910 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002911 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002912 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002913 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002914 else
2915 {
2916 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2917
2918 if (hProcess != NULL)
2919 {
2920 DebugBreakProcess(hProcess);
2921 CloseHandle(hProcess);
2922 rettv->vval.v_number = OK;
2923 }
2924 }
2925}
2926#endif
2927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002928/*
2929 * "deepcopy()" function
2930 */
2931 static void
2932f_deepcopy(typval_T *argvars, typval_T *rettv)
2933{
2934 int noref = 0;
2935 int copyID;
2936
2937 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002938 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 else
2942 {
2943 copyID = get_copyID();
2944 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2945 }
2946}
2947
2948/*
2949 * "delete()" function
2950 */
2951 static void
2952f_delete(typval_T *argvars, typval_T *rettv)
2953{
2954 char_u nbuf[NUMBUFLEN];
2955 char_u *name;
2956 char_u *flags;
2957
2958 rettv->vval.v_number = -1;
2959 if (check_restricted() || check_secure())
2960 return;
2961
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002962 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002963 if (name == NULL || *name == NUL)
2964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002966 return;
2967 }
2968
2969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002970 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 else
2972 flags = (char_u *)"";
2973
2974 if (*flags == NUL)
2975 /* delete a file */
2976 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2977 else if (STRCMP(flags, "d") == 0)
2978 /* delete an empty directory */
2979 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2980 else if (STRCMP(flags, "rf") == 0)
2981 /* delete a directory recursively */
2982 rettv->vval.v_number = delete_recursive(name);
2983 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985}
2986
2987/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02002988 * "deletebufline()" function
2989 */
2990 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02002991f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02002992{
2993 buf_T *buf;
2994 linenr_T first, last;
2995 linenr_T lnum;
2996 long count;
2997 int is_curbuf;
2998 buf_T *curbuf_save = NULL;
2999 win_T *curwin_save = NULL;
3000 tabpage_T *tp;
3001 win_T *wp;
3002
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003003 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003004 if (buf == NULL)
3005 {
3006 rettv->vval.v_number = 1; /* FAIL */
3007 return;
3008 }
3009 is_curbuf = buf == curbuf;
3010
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003011 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003012 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003013 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003014 else
3015 last = first;
3016
3017 if (buf->b_ml.ml_mfp == NULL || first < 1
3018 || first > buf->b_ml.ml_line_count || last < first)
3019 {
3020 rettv->vval.v_number = 1; /* FAIL */
3021 return;
3022 }
3023
3024 if (!is_curbuf)
3025 {
3026 curbuf_save = curbuf;
3027 curwin_save = curwin;
3028 curbuf = buf;
3029 find_win_for_curbuf();
3030 }
3031 if (last > curbuf->b_ml.ml_line_count)
3032 last = curbuf->b_ml.ml_line_count;
3033 count = last - first + 1;
3034
3035 // When coming here from Insert mode, sync undo, so that this can be
3036 // undone separately from what was previously inserted.
3037 if (u_sync_once == 2)
3038 {
3039 u_sync_once = 1; // notify that u_sync() was called
3040 u_sync(TRUE);
3041 }
3042
3043 if (u_save(first - 1, last + 1) == FAIL)
3044 {
3045 rettv->vval.v_number = 1; /* FAIL */
3046 return;
3047 }
3048
3049 for (lnum = first; lnum <= last; ++lnum)
3050 ml_delete(first, TRUE);
3051
3052 FOR_ALL_TAB_WINDOWS(tp, wp)
3053 if (wp->w_buffer == buf)
3054 {
3055 if (wp->w_cursor.lnum > last)
3056 wp->w_cursor.lnum -= count;
3057 else if (wp->w_cursor.lnum> first)
3058 wp->w_cursor.lnum = first;
3059 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3060 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3061 }
3062 check_cursor_col();
3063 deleted_lines_mark(first, count);
3064
3065 if (!is_curbuf)
3066 {
3067 curbuf = curbuf_save;
3068 curwin = curwin_save;
3069 }
3070}
3071
3072/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073 * "did_filetype()" function
3074 */
3075 static void
3076f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3077{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003079}
3080
3081/*
3082 * "diff_filler()" function
3083 */
3084 static void
3085f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3086{
3087#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003088 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089#endif
3090}
3091
3092/*
3093 * "diff_hlID()" function
3094 */
3095 static void
3096f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3097{
3098#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003099 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003101 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003102 static int fnum = 0;
3103 static int change_start = 0;
3104 static int change_end = 0;
3105 static hlf_T hlID = (hlf_T)0;
3106 int filler_lines;
3107 int col;
3108
3109 if (lnum < 0) /* ignore type error in {lnum} arg */
3110 lnum = 0;
3111 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003112 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 || fnum != curbuf->b_fnum)
3114 {
3115 /* New line, buffer, change: need to get the values. */
3116 filler_lines = diff_check(curwin, lnum);
3117 if (filler_lines < 0)
3118 {
3119 if (filler_lines == -1)
3120 {
3121 change_start = MAXCOL;
3122 change_end = -1;
3123 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3124 hlID = HLF_ADD; /* added line */
3125 else
3126 hlID = HLF_CHD; /* changed line */
3127 }
3128 else
3129 hlID = HLF_ADD; /* added line */
3130 }
3131 else
3132 hlID = (hlf_T)0;
3133 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003134 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 fnum = curbuf->b_fnum;
3136 }
3137
3138 if (hlID == HLF_CHD || hlID == HLF_TXD)
3139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003140 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003141 if (col >= change_start && col <= change_end)
3142 hlID = HLF_TXD; /* changed text */
3143 else
3144 hlID = HLF_CHD; /* changed line */
3145 }
3146 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3147#endif
3148}
3149
3150/*
3151 * "empty({expr})" function
3152 */
3153 static void
3154f_empty(typval_T *argvars, typval_T *rettv)
3155{
3156 int n = FALSE;
3157
3158 switch (argvars[0].v_type)
3159 {
3160 case VAR_STRING:
3161 case VAR_FUNC:
3162 n = argvars[0].vval.v_string == NULL
3163 || *argvars[0].vval.v_string == NUL;
3164 break;
3165 case VAR_PARTIAL:
3166 n = FALSE;
3167 break;
3168 case VAR_NUMBER:
3169 n = argvars[0].vval.v_number == 0;
3170 break;
3171 case VAR_FLOAT:
3172#ifdef FEAT_FLOAT
3173 n = argvars[0].vval.v_float == 0.0;
3174 break;
3175#endif
3176 case VAR_LIST:
3177 n = argvars[0].vval.v_list == NULL
3178 || argvars[0].vval.v_list->lv_first == NULL;
3179 break;
3180 case VAR_DICT:
3181 n = argvars[0].vval.v_dict == NULL
3182 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3183 break;
3184 case VAR_SPECIAL:
3185 n = argvars[0].vval.v_number != VVAL_TRUE;
3186 break;
3187
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003188 case VAR_BLOB:
3189 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003190 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3191 break;
3192
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003193 case VAR_JOB:
3194#ifdef FEAT_JOB_CHANNEL
3195 n = argvars[0].vval.v_job == NULL
3196 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3197 break;
3198#endif
3199 case VAR_CHANNEL:
3200#ifdef FEAT_JOB_CHANNEL
3201 n = argvars[0].vval.v_channel == NULL
3202 || !channel_is_open(argvars[0].vval.v_channel);
3203 break;
3204#endif
3205 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003206 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003207 n = TRUE;
3208 break;
3209 }
3210
3211 rettv->vval.v_number = n;
3212}
3213
3214/*
3215 * "escape({string}, {chars})" function
3216 */
3217 static void
3218f_escape(typval_T *argvars, typval_T *rettv)
3219{
3220 char_u buf[NUMBUFLEN];
3221
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003222 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3223 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224 rettv->v_type = VAR_STRING;
3225}
3226
3227/*
3228 * "eval()" function
3229 */
3230 static void
3231f_eval(typval_T *argvars, typval_T *rettv)
3232{
3233 char_u *s, *p;
3234
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003235 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 if (s != NULL)
3237 s = skipwhite(s);
3238
3239 p = s;
3240 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3241 {
3242 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003243 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 need_clr_eos = FALSE;
3245 rettv->v_type = VAR_NUMBER;
3246 rettv->vval.v_number = 0;
3247 }
3248 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003249 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250}
3251
3252/*
3253 * "eventhandler()" function
3254 */
3255 static void
3256f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3257{
3258 rettv->vval.v_number = vgetc_busy;
3259}
3260
3261/*
3262 * "executable()" function
3263 */
3264 static void
3265f_executable(typval_T *argvars, typval_T *rettv)
3266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003267 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003268
3269 /* Check in $PATH and also check directly if there is a directory name. */
3270 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3271 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3272}
3273
3274static garray_T redir_execute_ga;
3275
3276/*
3277 * Append "value[value_len]" to the execute() output.
3278 */
3279 void
3280execute_redir_str(char_u *value, int value_len)
3281{
3282 int len;
3283
3284 if (value_len == -1)
3285 len = (int)STRLEN(value); /* Append the entire string */
3286 else
3287 len = value_len; /* Append only "value_len" characters */
3288 if (ga_grow(&redir_execute_ga, len) == OK)
3289 {
3290 mch_memmove((char *)redir_execute_ga.ga_data
3291 + redir_execute_ga.ga_len, value, len);
3292 redir_execute_ga.ga_len += len;
3293 }
3294}
3295
3296/*
3297 * Get next line from a list.
3298 * Called by do_cmdline() to get the next line.
3299 * Returns allocated string, or NULL for end of function.
3300 */
3301
3302 static char_u *
3303get_list_line(
3304 int c UNUSED,
3305 void *cookie,
3306 int indent UNUSED)
3307{
3308 listitem_T **p = (listitem_T **)cookie;
3309 listitem_T *item = *p;
3310 char_u buf[NUMBUFLEN];
3311 char_u *s;
3312
3313 if (item == NULL)
3314 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003315 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003316 *p = item->li_next;
3317 return s == NULL ? NULL : vim_strsave(s);
3318}
3319
3320/*
3321 * "execute()" function
3322 */
3323 static void
3324f_execute(typval_T *argvars, typval_T *rettv)
3325{
3326 char_u *cmd = NULL;
3327 list_T *list = NULL;
3328 int save_msg_silent = msg_silent;
3329 int save_emsg_silent = emsg_silent;
3330 int save_emsg_noredir = emsg_noredir;
3331 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003332 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003333 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003334 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003335 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003336
3337 rettv->vval.v_string = NULL;
3338 rettv->v_type = VAR_STRING;
3339
3340 if (argvars[0].v_type == VAR_LIST)
3341 {
3342 list = argvars[0].vval.v_list;
3343 if (list == NULL || list->lv_first == NULL)
3344 /* empty list, no commands, empty output */
3345 return;
3346 ++list->lv_refcount;
3347 }
3348 else
3349 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003350 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 if (cmd == NULL)
3352 return;
3353 }
3354
3355 if (argvars[1].v_type != VAR_UNKNOWN)
3356 {
3357 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003358 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003359
3360 if (s == NULL)
3361 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003362 if (*s == NUL)
3363 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 if (STRNCMP(s, "silent", 6) == 0)
3365 ++msg_silent;
3366 if (STRCMP(s, "silent!") == 0)
3367 {
3368 emsg_silent = TRUE;
3369 emsg_noredir = TRUE;
3370 }
3371 }
3372 else
3373 ++msg_silent;
3374
3375 if (redir_execute)
3376 save_ga = redir_execute_ga;
3377 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3378 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003379 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003380 if (!echo_output)
3381 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003382
3383 if (cmd != NULL)
3384 do_cmdline_cmd(cmd);
3385 else
3386 {
3387 listitem_T *item = list->lv_first;
3388
3389 do_cmdline(NULL, get_list_line, (void *)&item,
3390 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3391 --list->lv_refcount;
3392 }
3393
Bram Moolenaard297f352017-01-29 20:31:21 +01003394 /* Need to append a NUL to the result. */
3395 if (ga_grow(&redir_execute_ga, 1) == OK)
3396 {
3397 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3398 rettv->vval.v_string = redir_execute_ga.ga_data;
3399 }
3400 else
3401 {
3402 ga_clear(&redir_execute_ga);
3403 rettv->vval.v_string = NULL;
3404 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 msg_silent = save_msg_silent;
3406 emsg_silent = save_emsg_silent;
3407 emsg_noredir = save_emsg_noredir;
3408
3409 redir_execute = save_redir_execute;
3410 if (redir_execute)
3411 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003412 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003414 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003415 if (echo_output)
3416 // When not working silently: put it in column zero. A following
3417 // "echon" will overwrite the message, unavoidably.
3418 msg_col = 0;
3419 else
3420 // When working silently: Put it back where it was, since nothing
3421 // should have been written.
3422 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003423}
3424
3425/*
3426 * "exepath()" function
3427 */
3428 static void
3429f_exepath(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u *p = NULL;
3432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 rettv->v_type = VAR_STRING;
3435 rettv->vval.v_string = p;
3436}
3437
3438/*
3439 * "exists()" function
3440 */
3441 static void
3442f_exists(typval_T *argvars, typval_T *rettv)
3443{
3444 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003445 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003447 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 if (*p == '$') /* environment variable */
3449 {
3450 /* first try "normal" environment variables (fast) */
3451 if (mch_getenv(p + 1) != NULL)
3452 n = TRUE;
3453 else
3454 {
3455 /* try expanding things like $VIM and ${HOME} */
3456 p = expand_env_save(p);
3457 if (p != NULL && *p != '$')
3458 n = TRUE;
3459 vim_free(p);
3460 }
3461 }
3462 else if (*p == '&' || *p == '+') /* option */
3463 {
3464 n = (get_option_tv(&p, NULL, TRUE) == OK);
3465 if (*skipwhite(p) != NUL)
3466 n = FALSE; /* trailing garbage */
3467 }
3468 else if (*p == '*') /* internal or user defined function */
3469 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003470 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003471 }
3472 else if (*p == ':')
3473 {
3474 n = cmd_exists(p + 1);
3475 }
3476 else if (*p == '#')
3477 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478 if (p[1] == '#')
3479 n = autocmd_supported(p + 2);
3480 else
3481 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003482 }
3483 else /* internal variable */
3484 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003485 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003486 }
3487
3488 rettv->vval.v_number = n;
3489}
3490
3491#ifdef FEAT_FLOAT
3492/*
3493 * "exp()" function
3494 */
3495 static void
3496f_exp(typval_T *argvars, typval_T *rettv)
3497{
3498 float_T f = 0.0;
3499
3500 rettv->v_type = VAR_FLOAT;
3501 if (get_float_arg(argvars, &f) == OK)
3502 rettv->vval.v_float = exp(f);
3503 else
3504 rettv->vval.v_float = 0.0;
3505}
3506#endif
3507
3508/*
3509 * "expand()" function
3510 */
3511 static void
3512f_expand(typval_T *argvars, typval_T *rettv)
3513{
3514 char_u *s;
3515 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003516 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003517 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3518 expand_T xpc;
3519 int error = FALSE;
3520 char_u *result;
3521
3522 rettv->v_type = VAR_STRING;
3523 if (argvars[1].v_type != VAR_UNKNOWN
3524 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526 && !error)
3527 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003528 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003529 }
3530
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003531 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003532 if (*s == '%' || *s == '#' || *s == '<')
3533 {
3534 ++emsg_off;
3535 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3536 --emsg_off;
3537 if (rettv->v_type == VAR_LIST)
3538 {
3539 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3540 list_append_string(rettv->vval.v_list, result, -1);
3541 else
3542 vim_free(result);
3543 }
3544 else
3545 rettv->vval.v_string = result;
3546 }
3547 else
3548 {
3549 /* When the optional second argument is non-zero, don't remove matches
3550 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3551 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003552 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 options |= WILD_KEEP_ALL;
3554 if (!error)
3555 {
3556 ExpandInit(&xpc);
3557 xpc.xp_context = EXPAND_FILES;
3558 if (p_wic)
3559 options += WILD_ICASE;
3560 if (rettv->v_type == VAR_STRING)
3561 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3562 options, WILD_ALL);
3563 else if (rettv_list_alloc(rettv) != FAIL)
3564 {
3565 int i;
3566
3567 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3568 for (i = 0; i < xpc.xp_numfiles; i++)
3569 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3570 ExpandCleanup(&xpc);
3571 }
3572 }
3573 else
3574 rettv->vval.v_string = NULL;
3575 }
3576}
3577
3578/*
3579 * "extend(list, list [, idx])" function
3580 * "extend(dict, dict [, action])" function
3581 */
3582 static void
3583f_extend(typval_T *argvars, typval_T *rettv)
3584{
3585 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3586
3587 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3588 {
3589 list_T *l1, *l2;
3590 listitem_T *item;
3591 long before;
3592 int error = FALSE;
3593
3594 l1 = argvars[0].vval.v_list;
3595 l2 = argvars[1].vval.v_list;
3596 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3597 && l2 != NULL)
3598 {
3599 if (argvars[2].v_type != VAR_UNKNOWN)
3600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003601 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 if (error)
3603 return; /* type error; errmsg already given */
3604
3605 if (before == l1->lv_len)
3606 item = NULL;
3607 else
3608 {
3609 item = list_find(l1, before);
3610 if (item == NULL)
3611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003612 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613 return;
3614 }
3615 }
3616 }
3617 else
3618 item = NULL;
3619 list_extend(l1, l2, item);
3620
3621 copy_tv(&argvars[0], rettv);
3622 }
3623 }
3624 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3625 {
3626 dict_T *d1, *d2;
3627 char_u *action;
3628 int i;
3629
3630 d1 = argvars[0].vval.v_dict;
3631 d2 = argvars[1].vval.v_dict;
3632 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3633 && d2 != NULL)
3634 {
3635 /* Check the third argument. */
3636 if (argvars[2].v_type != VAR_UNKNOWN)
3637 {
3638 static char *(av[]) = {"keep", "force", "error"};
3639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003641 if (action == NULL)
3642 return; /* type error; errmsg already given */
3643 for (i = 0; i < 3; ++i)
3644 if (STRCMP(action, av[i]) == 0)
3645 break;
3646 if (i == 3)
3647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003648 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003649 return;
3650 }
3651 }
3652 else
3653 action = (char_u *)"force";
3654
3655 dict_extend(d1, d2, action);
3656
3657 copy_tv(&argvars[0], rettv);
3658 }
3659 }
3660 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003661 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003662}
3663
3664/*
3665 * "feedkeys()" function
3666 */
3667 static void
3668f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3669{
3670 int remap = TRUE;
3671 int insert = FALSE;
3672 char_u *keys, *flags;
3673 char_u nbuf[NUMBUFLEN];
3674 int typed = FALSE;
3675 int execute = FALSE;
3676 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003677 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678 char_u *keys_esc;
3679
3680 /* This is not allowed in the sandbox. If the commands would still be
3681 * executed in the sandbox it would be OK, but it probably happens later,
3682 * when "sandbox" is no longer set. */
3683 if (check_secure())
3684 return;
3685
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003686 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003687
3688 if (argvars[1].v_type != VAR_UNKNOWN)
3689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003690 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 for ( ; *flags != NUL; ++flags)
3692 {
3693 switch (*flags)
3694 {
3695 case 'n': remap = FALSE; break;
3696 case 'm': remap = TRUE; break;
3697 case 't': typed = TRUE; break;
3698 case 'i': insert = TRUE; break;
3699 case 'x': execute = TRUE; break;
3700 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003701 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 }
3703 }
3704 }
3705
3706 if (*keys != NUL || execute)
3707 {
3708 /* Need to escape K_SPECIAL and CSI before putting the string in the
3709 * typeahead buffer. */
3710 keys_esc = vim_strsave_escape_csi(keys);
3711 if (keys_esc != NULL)
3712 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003713 if (lowlevel)
3714 {
3715#ifdef USE_INPUT_BUF
3716 add_to_input_buf(keys, (int)STRLEN(keys));
3717#else
3718 emsg(_("E980: lowlevel input not supported"));
3719#endif
3720 }
3721 else
3722 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3724 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003725 if (vgetc_busy
3726#ifdef FEAT_TIMERS
3727 || timer_busy
3728#endif
3729 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003730 typebuf_was_filled = TRUE;
3731 if (execute)
3732 {
3733 int save_msg_scroll = msg_scroll;
3734
3735 /* Avoid a 1 second delay when the keys start Insert mode. */
3736 msg_scroll = FALSE;
3737
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003738 if (!dangerous)
3739 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003740 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003741 if (!dangerous)
3742 --ex_normal_busy;
3743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003744 msg_scroll |= save_msg_scroll;
3745 }
3746 }
3747 }
3748}
3749
3750/*
3751 * "filereadable()" function
3752 */
3753 static void
3754f_filereadable(typval_T *argvars, typval_T *rettv)
3755{
3756 int fd;
3757 char_u *p;
3758 int n;
3759
3760#ifndef O_NONBLOCK
3761# define O_NONBLOCK 0
3762#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003763 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003764 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3765 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3766 {
3767 n = TRUE;
3768 close(fd);
3769 }
3770 else
3771 n = FALSE;
3772
3773 rettv->vval.v_number = n;
3774}
3775
3776/*
3777 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3778 * rights to write into.
3779 */
3780 static void
3781f_filewritable(typval_T *argvars, typval_T *rettv)
3782{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003783 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003784}
3785
3786 static void
3787findfilendir(
3788 typval_T *argvars UNUSED,
3789 typval_T *rettv,
3790 int find_what UNUSED)
3791{
3792#ifdef FEAT_SEARCHPATH
3793 char_u *fname;
3794 char_u *fresult = NULL;
3795 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3796 char_u *p;
3797 char_u pathbuf[NUMBUFLEN];
3798 int count = 1;
3799 int first = TRUE;
3800 int error = FALSE;
3801#endif
3802
3803 rettv->vval.v_string = NULL;
3804 rettv->v_type = VAR_STRING;
3805
3806#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003807 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808
3809 if (argvars[1].v_type != VAR_UNKNOWN)
3810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003811 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003812 if (p == NULL)
3813 error = TRUE;
3814 else
3815 {
3816 if (*p != NUL)
3817 path = p;
3818
3819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003820 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 }
3822 }
3823
3824 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3825 error = TRUE;
3826
3827 if (*fname != NUL && !error)
3828 {
3829 do
3830 {
3831 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3832 vim_free(fresult);
3833 fresult = find_file_in_path_option(first ? fname : NULL,
3834 first ? (int)STRLEN(fname) : 0,
3835 0, first, path,
3836 find_what,
3837 curbuf->b_ffname,
3838 find_what == FINDFILE_DIR
3839 ? (char_u *)"" : curbuf->b_p_sua);
3840 first = FALSE;
3841
3842 if (fresult != NULL && rettv->v_type == VAR_LIST)
3843 list_append_string(rettv->vval.v_list, fresult, -1);
3844
3845 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3846 }
3847
3848 if (rettv->v_type == VAR_STRING)
3849 rettv->vval.v_string = fresult;
3850#endif
3851}
3852
3853/*
3854 * "filter()" function
3855 */
3856 static void
3857f_filter(typval_T *argvars, typval_T *rettv)
3858{
3859 filter_map(argvars, rettv, FALSE);
3860}
3861
3862/*
3863 * "finddir({fname}[, {path}[, {count}]])" function
3864 */
3865 static void
3866f_finddir(typval_T *argvars, typval_T *rettv)
3867{
3868 findfilendir(argvars, rettv, FINDFILE_DIR);
3869}
3870
3871/*
3872 * "findfile({fname}[, {path}[, {count}]])" function
3873 */
3874 static void
3875f_findfile(typval_T *argvars, typval_T *rettv)
3876{
3877 findfilendir(argvars, rettv, FINDFILE_FILE);
3878}
3879
3880#ifdef FEAT_FLOAT
3881/*
3882 * "float2nr({float})" function
3883 */
3884 static void
3885f_float2nr(typval_T *argvars, typval_T *rettv)
3886{
3887 float_T f = 0.0;
3888
3889 if (get_float_arg(argvars, &f) == OK)
3890 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003891 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003892 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003893 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003894 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 else
3896 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 }
3898}
3899
3900/*
3901 * "floor({float})" function
3902 */
3903 static void
3904f_floor(typval_T *argvars, typval_T *rettv)
3905{
3906 float_T f = 0.0;
3907
3908 rettv->v_type = VAR_FLOAT;
3909 if (get_float_arg(argvars, &f) == OK)
3910 rettv->vval.v_float = floor(f);
3911 else
3912 rettv->vval.v_float = 0.0;
3913}
3914
3915/*
3916 * "fmod()" function
3917 */
3918 static void
3919f_fmod(typval_T *argvars, typval_T *rettv)
3920{
3921 float_T fx = 0.0, fy = 0.0;
3922
3923 rettv->v_type = VAR_FLOAT;
3924 if (get_float_arg(argvars, &fx) == OK
3925 && get_float_arg(&argvars[1], &fy) == OK)
3926 rettv->vval.v_float = fmod(fx, fy);
3927 else
3928 rettv->vval.v_float = 0.0;
3929}
3930#endif
3931
3932/*
3933 * "fnameescape({string})" function
3934 */
3935 static void
3936f_fnameescape(typval_T *argvars, typval_T *rettv)
3937{
3938 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003939 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940 rettv->v_type = VAR_STRING;
3941}
3942
3943/*
3944 * "fnamemodify({fname}, {mods})" function
3945 */
3946 static void
3947f_fnamemodify(typval_T *argvars, typval_T *rettv)
3948{
3949 char_u *fname;
3950 char_u *mods;
3951 int usedlen = 0;
3952 int len;
3953 char_u *fbuf = NULL;
3954 char_u buf[NUMBUFLEN];
3955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003956 fname = tv_get_string_chk(&argvars[0]);
3957 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003958 if (fname == NULL || mods == NULL)
3959 fname = NULL;
3960 else
3961 {
3962 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003963 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003964 }
3965
3966 rettv->v_type = VAR_STRING;
3967 if (fname == NULL)
3968 rettv->vval.v_string = NULL;
3969 else
3970 rettv->vval.v_string = vim_strnsave(fname, len);
3971 vim_free(fbuf);
3972}
3973
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974/*
3975 * "foldclosed()" function
3976 */
3977 static void
3978foldclosed_both(
3979 typval_T *argvars UNUSED,
3980 typval_T *rettv,
3981 int end UNUSED)
3982{
3983#ifdef FEAT_FOLDING
3984 linenr_T lnum;
3985 linenr_T first, last;
3986
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003987 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003988 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3989 {
3990 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3991 {
3992 if (end)
3993 rettv->vval.v_number = (varnumber_T)last;
3994 else
3995 rettv->vval.v_number = (varnumber_T)first;
3996 return;
3997 }
3998 }
3999#endif
4000 rettv->vval.v_number = -1;
4001}
4002
4003/*
4004 * "foldclosed()" function
4005 */
4006 static void
4007f_foldclosed(typval_T *argvars, typval_T *rettv)
4008{
4009 foldclosed_both(argvars, rettv, FALSE);
4010}
4011
4012/*
4013 * "foldclosedend()" function
4014 */
4015 static void
4016f_foldclosedend(typval_T *argvars, typval_T *rettv)
4017{
4018 foldclosed_both(argvars, rettv, TRUE);
4019}
4020
4021/*
4022 * "foldlevel()" function
4023 */
4024 static void
4025f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4026{
4027#ifdef FEAT_FOLDING
4028 linenr_T lnum;
4029
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004030 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4032 rettv->vval.v_number = foldLevel(lnum);
4033#endif
4034}
4035
4036/*
4037 * "foldtext()" function
4038 */
4039 static void
4040f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4041{
4042#ifdef FEAT_FOLDING
4043 linenr_T foldstart;
4044 linenr_T foldend;
4045 char_u *dashes;
4046 linenr_T lnum;
4047 char_u *s;
4048 char_u *r;
4049 int len;
4050 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004051 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004052#endif
4053
4054 rettv->v_type = VAR_STRING;
4055 rettv->vval.v_string = NULL;
4056#ifdef FEAT_FOLDING
4057 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4058 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4059 dashes = get_vim_var_str(VV_FOLDDASHES);
4060 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4061 && dashes != NULL)
4062 {
4063 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004064 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004065 if (!linewhite(lnum))
4066 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004067
4068 /* Find interesting text in this line. */
4069 s = skipwhite(ml_get(lnum));
4070 /* skip C comment-start */
4071 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4072 {
4073 s = skipwhite(s + 2);
4074 if (*skipwhite(s) == NUL
4075 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4076 {
4077 s = skipwhite(ml_get(lnum + 1));
4078 if (*s == '*')
4079 s = skipwhite(s + 1);
4080 }
4081 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004082 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004083 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004084 r = alloc((unsigned)(STRLEN(txt)
4085 + STRLEN(dashes) /* for %s */
4086 + 20 /* for %3ld */
4087 + STRLEN(s))); /* concatenated */
4088 if (r != NULL)
4089 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004090 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004091 len = (int)STRLEN(r);
4092 STRCAT(r, s);
4093 /* remove 'foldmarker' and 'commentstring' */
4094 foldtext_cleanup(r + len);
4095 rettv->vval.v_string = r;
4096 }
4097 }
4098#endif
4099}
4100
4101/*
4102 * "foldtextresult(lnum)" function
4103 */
4104 static void
4105f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4106{
4107#ifdef FEAT_FOLDING
4108 linenr_T lnum;
4109 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004110 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004111 foldinfo_T foldinfo;
4112 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004113 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004114#endif
4115
4116 rettv->v_type = VAR_STRING;
4117 rettv->vval.v_string = NULL;
4118#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004119 if (entered)
4120 return; /* reject recursive use */
4121 entered = TRUE;
4122
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004123 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004124 /* treat illegal types and illegal string values for {lnum} the same */
4125 if (lnum < 0)
4126 lnum = 0;
4127 fold_count = foldedCount(curwin, lnum, &foldinfo);
4128 if (fold_count > 0)
4129 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004130 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4131 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004132 if (text == buf)
4133 text = vim_strsave(text);
4134 rettv->vval.v_string = text;
4135 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004136
4137 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138#endif
4139}
4140
4141/*
4142 * "foreground()" function
4143 */
4144 static void
4145f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4146{
4147#ifdef FEAT_GUI
4148 if (gui.in_use)
4149 gui_mch_set_foreground();
4150#else
4151# ifdef WIN32
4152 win32_set_foreground();
4153# endif
4154#endif
4155}
4156
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004158common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004159{
4160 char_u *s;
4161 char_u *name;
4162 int use_string = FALSE;
4163 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004164 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165
4166 if (argvars[0].v_type == VAR_FUNC)
4167 {
4168 /* function(MyFunc, [arg], dict) */
4169 s = argvars[0].vval.v_string;
4170 }
4171 else if (argvars[0].v_type == VAR_PARTIAL
4172 && argvars[0].vval.v_partial != NULL)
4173 {
4174 /* function(dict.MyFunc, [arg]) */
4175 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004176 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004177 }
4178 else
4179 {
4180 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004181 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 use_string = TRUE;
4183 }
4184
Bram Moolenaar843b8842016-08-21 14:36:15 +02004185 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004186 {
4187 name = s;
4188 trans_name = trans_function_name(&name, FALSE,
4189 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4190 if (*name != NUL)
4191 s = NULL;
4192 }
4193
Bram Moolenaar843b8842016-08-21 14:36:15 +02004194 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4195 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004196 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004197 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004198 else if (trans_name != NULL && (is_funcref
4199 ? find_func(trans_name) == NULL
4200 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004201 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 else
4203 {
4204 int dict_idx = 0;
4205 int arg_idx = 0;
4206 list_T *list = NULL;
4207
4208 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4209 {
4210 char sid_buf[25];
4211 int off = *s == 's' ? 2 : 5;
4212
4213 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4214 * also be called from another script. Using trans_function_name()
4215 * would also work, but some plugins depend on the name being
4216 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004217 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004218 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4219 if (name != NULL)
4220 {
4221 STRCPY(name, sid_buf);
4222 STRCAT(name, s + off);
4223 }
4224 }
4225 else
4226 name = vim_strsave(s);
4227
4228 if (argvars[1].v_type != VAR_UNKNOWN)
4229 {
4230 if (argvars[2].v_type != VAR_UNKNOWN)
4231 {
4232 /* function(name, [args], dict) */
4233 arg_idx = 1;
4234 dict_idx = 2;
4235 }
4236 else if (argvars[1].v_type == VAR_DICT)
4237 /* function(name, dict) */
4238 dict_idx = 1;
4239 else
4240 /* function(name, [args]) */
4241 arg_idx = 1;
4242 if (dict_idx > 0)
4243 {
4244 if (argvars[dict_idx].v_type != VAR_DICT)
4245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004246 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004247 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004248 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004249 }
4250 if (argvars[dict_idx].vval.v_dict == NULL)
4251 dict_idx = 0;
4252 }
4253 if (arg_idx > 0)
4254 {
4255 if (argvars[arg_idx].v_type != VAR_LIST)
4256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004259 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004260 }
4261 list = argvars[arg_idx].vval.v_list;
4262 if (list == NULL || list->lv_len == 0)
4263 arg_idx = 0;
4264 }
4265 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004266 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004267 {
4268 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4269
4270 /* result is a VAR_PARTIAL */
4271 if (pt == NULL)
4272 vim_free(name);
4273 else
4274 {
4275 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4276 {
4277 listitem_T *li;
4278 int i = 0;
4279 int arg_len = 0;
4280 int lv_len = 0;
4281
4282 if (arg_pt != NULL)
4283 arg_len = arg_pt->pt_argc;
4284 if (list != NULL)
4285 lv_len = list->lv_len;
4286 pt->pt_argc = arg_len + lv_len;
4287 pt->pt_argv = (typval_T *)alloc(
4288 sizeof(typval_T) * pt->pt_argc);
4289 if (pt->pt_argv == NULL)
4290 {
4291 vim_free(pt);
4292 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004293 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004294 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004295 for (i = 0; i < arg_len; i++)
4296 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4297 if (lv_len > 0)
4298 for (li = list->lv_first; li != NULL;
4299 li = li->li_next)
4300 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 }
4302
4303 /* For "function(dict.func, [], dict)" and "func" is a partial
4304 * use "dict". That is backwards compatible. */
4305 if (dict_idx > 0)
4306 {
4307 /* The dict is bound explicitly, pt_auto is FALSE. */
4308 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4309 ++pt->pt_dict->dv_refcount;
4310 }
4311 else if (arg_pt != NULL)
4312 {
4313 /* If the dict was bound automatically the result is also
4314 * bound automatically. */
4315 pt->pt_dict = arg_pt->pt_dict;
4316 pt->pt_auto = arg_pt->pt_auto;
4317 if (pt->pt_dict != NULL)
4318 ++pt->pt_dict->dv_refcount;
4319 }
4320
4321 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004322 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4323 {
4324 pt->pt_func = arg_pt->pt_func;
4325 func_ptr_ref(pt->pt_func);
4326 vim_free(name);
4327 }
4328 else if (is_funcref)
4329 {
4330 pt->pt_func = find_func(trans_name);
4331 func_ptr_ref(pt->pt_func);
4332 vim_free(name);
4333 }
4334 else
4335 {
4336 pt->pt_name = name;
4337 func_ref(name);
4338 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 }
4340 rettv->v_type = VAR_PARTIAL;
4341 rettv->vval.v_partial = pt;
4342 }
4343 else
4344 {
4345 /* result is a VAR_FUNC */
4346 rettv->v_type = VAR_FUNC;
4347 rettv->vval.v_string = name;
4348 func_ref(name);
4349 }
4350 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351theend:
4352 vim_free(trans_name);
4353}
4354
4355/*
4356 * "funcref()" function
4357 */
4358 static void
4359f_funcref(typval_T *argvars, typval_T *rettv)
4360{
4361 common_function(argvars, rettv, TRUE);
4362}
4363
4364/*
4365 * "function()" function
4366 */
4367 static void
4368f_function(typval_T *argvars, typval_T *rettv)
4369{
4370 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371}
4372
4373/*
4374 * "garbagecollect()" function
4375 */
4376 static void
4377f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4378{
4379 /* This is postponed until we are back at the toplevel, because we may be
4380 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4381 want_garbage_collect = TRUE;
4382
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004383 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004384 garbage_collect_at_exit = TRUE;
4385}
4386
4387/*
4388 * "get()" function
4389 */
4390 static void
4391f_get(typval_T *argvars, typval_T *rettv)
4392{
4393 listitem_T *li;
4394 list_T *l;
4395 dictitem_T *di;
4396 dict_T *d;
4397 typval_T *tv = NULL;
4398
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004399 if (argvars[0].v_type == VAR_BLOB)
4400 {
4401 int error = FALSE;
4402 int idx = tv_get_number_chk(&argvars[1], &error);
4403
4404 if (!error)
4405 {
4406 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004407 if (idx < 0)
4408 idx = blob_len(argvars[0].vval.v_blob) + idx;
4409 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4410 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004411 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004412 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004413 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004414 tv = rettv;
4415 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004416 }
4417 }
4418 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419 {
4420 if ((l = argvars[0].vval.v_list) != NULL)
4421 {
4422 int error = FALSE;
4423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004424 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004425 if (!error && li != NULL)
4426 tv = &li->li_tv;
4427 }
4428 }
4429 else if (argvars[0].v_type == VAR_DICT)
4430 {
4431 if ((d = argvars[0].vval.v_dict) != NULL)
4432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004433 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434 if (di != NULL)
4435 tv = &di->di_tv;
4436 }
4437 }
4438 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4439 {
4440 partial_T *pt;
4441 partial_T fref_pt;
4442
4443 if (argvars[0].v_type == VAR_PARTIAL)
4444 pt = argvars[0].vval.v_partial;
4445 else
4446 {
4447 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4448 fref_pt.pt_name = argvars[0].vval.v_string;
4449 pt = &fref_pt;
4450 }
4451
4452 if (pt != NULL)
4453 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004454 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004455 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004456
4457 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4458 {
4459 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004460 n = partial_name(pt);
4461 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004462 rettv->vval.v_string = NULL;
4463 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004464 {
4465 rettv->vval.v_string = vim_strsave(n);
4466 if (rettv->v_type == VAR_FUNC)
4467 func_ref(rettv->vval.v_string);
4468 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004469 }
4470 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004471 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472 else if (STRCMP(what, "args") == 0)
4473 {
4474 rettv->v_type = VAR_LIST;
4475 if (rettv_list_alloc(rettv) == OK)
4476 {
4477 int i;
4478
4479 for (i = 0; i < pt->pt_argc; ++i)
4480 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4481 }
4482 }
4483 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004484 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004485 return;
4486 }
4487 }
4488 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004489 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004490
4491 if (tv == NULL)
4492 {
4493 if (argvars[2].v_type != VAR_UNKNOWN)
4494 copy_tv(&argvars[2], rettv);
4495 }
4496 else
4497 copy_tv(tv, rettv);
4498}
4499
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004500/*
4501 * Returns buffer options, variables and other attributes in a dictionary.
4502 */
4503 static dict_T *
4504get_buffer_info(buf_T *buf)
4505{
4506 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004507 tabpage_T *tp;
4508 win_T *wp;
4509 list_T *windows;
4510
4511 dict = dict_alloc();
4512 if (dict == NULL)
4513 return NULL;
4514
Bram Moolenaare0be1672018-07-08 16:50:37 +02004515 dict_add_number(dict, "bufnr", buf->b_fnum);
4516 dict_add_string(dict, "name", buf->b_ffname);
4517 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4518 : buflist_findlnum(buf));
4519 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4520 dict_add_number(dict, "listed", buf->b_p_bl);
4521 dict_add_number(dict, "changed", bufIsChanged(buf));
4522 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4523 dict_add_number(dict, "hidden",
4524 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004525
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004526 /* Get a reference to buffer variables */
4527 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004528
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004529 /* List of windows displaying this buffer */
4530 windows = list_alloc();
4531 if (windows != NULL)
4532 {
4533 FOR_ALL_TAB_WINDOWS(tp, wp)
4534 if (wp->w_buffer == buf)
4535 list_append_number(windows, (varnumber_T)wp->w_id);
4536 dict_add_list(dict, "windows", windows);
4537 }
4538
4539#ifdef FEAT_SIGNS
4540 if (buf->b_signlist != NULL)
4541 {
4542 /* List of signs placed in this buffer */
4543 list_T *signs = list_alloc();
4544 if (signs != NULL)
4545 {
4546 get_buffer_signs(buf, signs);
4547 dict_add_list(dict, "signs", signs);
4548 }
4549 }
4550#endif
4551
4552 return dict;
4553}
4554
4555/*
4556 * "getbufinfo()" function
4557 */
4558 static void
4559f_getbufinfo(typval_T *argvars, typval_T *rettv)
4560{
4561 buf_T *buf = NULL;
4562 buf_T *argbuf = NULL;
4563 dict_T *d;
4564 int filtered = FALSE;
4565 int sel_buflisted = FALSE;
4566 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004567 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004568
4569 if (rettv_list_alloc(rettv) != OK)
4570 return;
4571
4572 /* List of all the buffers or selected buffers */
4573 if (argvars[0].v_type == VAR_DICT)
4574 {
4575 dict_T *sel_d = argvars[0].vval.v_dict;
4576
4577 if (sel_d != NULL)
4578 {
4579 dictitem_T *di;
4580
4581 filtered = TRUE;
4582
4583 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004584 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004585 sel_buflisted = TRUE;
4586
4587 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004588 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004589 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004590
4591 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004592 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004593 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004594 }
4595 }
4596 else if (argvars[0].v_type != VAR_UNKNOWN)
4597 {
4598 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004599 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004600 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004601 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004602 --emsg_off;
4603 if (argbuf == NULL)
4604 return;
4605 }
4606
4607 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004608 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004609 {
4610 if (argbuf != NULL && argbuf != buf)
4611 continue;
4612 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004613 || (sel_buflisted && !buf->b_p_bl)
4614 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004615 continue;
4616
4617 d = get_buffer_info(buf);
4618 if (d != NULL)
4619 list_append_dict(rettv->vval.v_list, d);
4620 if (argbuf != NULL)
4621 return;
4622 }
4623}
4624
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625/*
4626 * Get line or list of lines from buffer "buf" into "rettv".
4627 * Return a range (from start to end) of lines in rettv from the specified
4628 * buffer.
4629 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4630 */
4631 static void
4632get_buffer_lines(
4633 buf_T *buf,
4634 linenr_T start,
4635 linenr_T end,
4636 int retlist,
4637 typval_T *rettv)
4638{
4639 char_u *p;
4640
4641 rettv->v_type = VAR_STRING;
4642 rettv->vval.v_string = NULL;
4643 if (retlist && rettv_list_alloc(rettv) == FAIL)
4644 return;
4645
4646 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4647 return;
4648
4649 if (!retlist)
4650 {
4651 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4652 p = ml_get_buf(buf, start, FALSE);
4653 else
4654 p = (char_u *)"";
4655 rettv->vval.v_string = vim_strsave(p);
4656 }
4657 else
4658 {
4659 if (end < start)
4660 return;
4661
4662 if (start < 1)
4663 start = 1;
4664 if (end > buf->b_ml.ml_line_count)
4665 end = buf->b_ml.ml_line_count;
4666 while (start <= end)
4667 if (list_append_string(rettv->vval.v_list,
4668 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4669 break;
4670 }
4671}
4672
4673/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674 * "getbufline()" function
4675 */
4676 static void
4677f_getbufline(typval_T *argvars, typval_T *rettv)
4678{
4679 linenr_T lnum;
4680 linenr_T end;
4681 buf_T *buf;
4682
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004683 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004685 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004686 --emsg_off;
4687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004688 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 if (argvars[2].v_type == VAR_UNKNOWN)
4690 end = lnum;
4691 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004692 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004693
4694 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4695}
4696
4697/*
4698 * "getbufvar()" function
4699 */
4700 static void
4701f_getbufvar(typval_T *argvars, typval_T *rettv)
4702{
4703 buf_T *buf;
4704 buf_T *save_curbuf;
4705 char_u *varname;
4706 dictitem_T *v;
4707 int done = FALSE;
4708
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004709 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4710 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004712 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713
4714 rettv->v_type = VAR_STRING;
4715 rettv->vval.v_string = NULL;
4716
4717 if (buf != NULL && varname != NULL)
4718 {
4719 /* set curbuf to be our buf, temporarily */
4720 save_curbuf = curbuf;
4721 curbuf = buf;
4722
Bram Moolenaar30567352016-08-27 21:25:44 +02004723 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004725 if (varname[1] == NUL)
4726 {
4727 /* get all buffer-local options in a dict */
4728 dict_T *opts = get_winbuf_options(TRUE);
4729
4730 if (opts != NULL)
4731 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004732 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004733 done = TRUE;
4734 }
4735 }
4736 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4737 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004738 done = TRUE;
4739 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004740 else
4741 {
4742 /* Look up the variable. */
4743 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4744 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4745 'b', varname, FALSE);
4746 if (v != NULL)
4747 {
4748 copy_tv(&v->di_tv, rettv);
4749 done = TRUE;
4750 }
4751 }
4752
4753 /* restore previous notion of curbuf */
4754 curbuf = save_curbuf;
4755 }
4756
4757 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4758 /* use the default value */
4759 copy_tv(&argvars[2], rettv);
4760
4761 --emsg_off;
4762}
4763
4764/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004765 * "getchangelist()" function
4766 */
4767 static void
4768f_getchangelist(typval_T *argvars, typval_T *rettv)
4769{
4770#ifdef FEAT_JUMPLIST
4771 buf_T *buf;
4772 int i;
4773 list_T *l;
4774 dict_T *d;
4775#endif
4776
4777 if (rettv_list_alloc(rettv) != OK)
4778 return;
4779
4780#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004782 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004783 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004784 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004785 if (buf == NULL)
4786 return;
4787
4788 l = list_alloc();
4789 if (l == NULL)
4790 return;
4791
4792 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4793 return;
4794 /*
4795 * The current window change list index tracks only the position in the
4796 * current buffer change list. For other buffers, use the change list
4797 * length as the current index.
4798 */
4799 list_append_number(rettv->vval.v_list,
4800 (varnumber_T)((buf == curwin->w_buffer)
4801 ? curwin->w_changelistidx : buf->b_changelistlen));
4802
4803 for (i = 0; i < buf->b_changelistlen; ++i)
4804 {
4805 if (buf->b_changelist[i].lnum == 0)
4806 continue;
4807 if ((d = dict_alloc()) == NULL)
4808 return;
4809 if (list_append_dict(l, d) == FAIL)
4810 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004811 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4812 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004813# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004814 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004815# endif
4816 }
4817#endif
4818}
4819/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004820 * "getchar()" function
4821 */
4822 static void
4823f_getchar(typval_T *argvars, typval_T *rettv)
4824{
4825 varnumber_T n;
4826 int error = FALSE;
4827
Bram Moolenaar84d93902018-09-11 20:10:20 +02004828#ifdef MESSAGE_QUEUE
4829 // vpeekc() used to check for messages, but that caused problems, invoking
4830 // a callback where it was not expected. Some plugins use getchar(1) in a
4831 // loop to await a message, therefore make sure we check for messages here.
4832 parse_queued_messages();
4833#endif
4834
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004835 /* Position the cursor. Needed after a message that ends in a space. */
4836 windgoto(msg_row, msg_col);
4837
4838 ++no_mapping;
4839 ++allow_keys;
4840 for (;;)
4841 {
4842 if (argvars[0].v_type == VAR_UNKNOWN)
4843 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004844 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004846 /* getchar(1): only check if char avail */
4847 n = vpeekc_any();
4848 else if (error || vpeekc_any() == NUL)
4849 /* illegal argument or getchar(0) and no char avail: return zero */
4850 n = 0;
4851 else
4852 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004853 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004854
4855 if (n == K_IGNORE)
4856 continue;
4857 break;
4858 }
4859 --no_mapping;
4860 --allow_keys;
4861
4862 set_vim_var_nr(VV_MOUSE_WIN, 0);
4863 set_vim_var_nr(VV_MOUSE_WINID, 0);
4864 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4865 set_vim_var_nr(VV_MOUSE_COL, 0);
4866
4867 rettv->vval.v_number = n;
4868 if (IS_SPECIAL(n) || mod_mask != 0)
4869 {
4870 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4871 int i = 0;
4872
4873 /* Turn a special key into three bytes, plus modifier. */
4874 if (mod_mask != 0)
4875 {
4876 temp[i++] = K_SPECIAL;
4877 temp[i++] = KS_MODIFIER;
4878 temp[i++] = mod_mask;
4879 }
4880 if (IS_SPECIAL(n))
4881 {
4882 temp[i++] = K_SPECIAL;
4883 temp[i++] = K_SECOND(n);
4884 temp[i++] = K_THIRD(n);
4885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886 else if (has_mbyte)
4887 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 else
4889 temp[i++] = n;
4890 temp[i++] = NUL;
4891 rettv->v_type = VAR_STRING;
4892 rettv->vval.v_string = vim_strsave(temp);
4893
4894#ifdef FEAT_MOUSE
4895 if (is_mouse_key(n))
4896 {
4897 int row = mouse_row;
4898 int col = mouse_col;
4899 win_T *win;
4900 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004901 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 int winnr = 1;
4903
4904 if (row >= 0 && col >= 0)
4905 {
4906 /* Find the window at the mouse coordinates and compute the
4907 * text position. */
4908 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004909 if (win == NULL)
4910 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912 for (wp = firstwin; wp != win; wp = wp->w_next)
4913 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004914 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4915 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4916 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4917 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4918 }
4919 }
4920#endif
4921 }
4922}
4923
4924/*
4925 * "getcharmod()" function
4926 */
4927 static void
4928f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4929{
4930 rettv->vval.v_number = mod_mask;
4931}
4932
4933/*
4934 * "getcharsearch()" function
4935 */
4936 static void
4937f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4938{
4939 if (rettv_dict_alloc(rettv) != FAIL)
4940 {
4941 dict_T *dict = rettv->vval.v_dict;
4942
Bram Moolenaare0be1672018-07-08 16:50:37 +02004943 dict_add_string(dict, "char", last_csearch());
4944 dict_add_number(dict, "forward", last_csearch_forward());
4945 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004946 }
4947}
4948
4949/*
4950 * "getcmdline()" function
4951 */
4952 static void
4953f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4954{
4955 rettv->v_type = VAR_STRING;
4956 rettv->vval.v_string = get_cmdline_str();
4957}
4958
4959/*
4960 * "getcmdpos()" function
4961 */
4962 static void
4963f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4964{
4965 rettv->vval.v_number = get_cmdline_pos() + 1;
4966}
4967
4968/*
4969 * "getcmdtype()" function
4970 */
4971 static void
4972f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4973{
4974 rettv->v_type = VAR_STRING;
4975 rettv->vval.v_string = alloc(2);
4976 if (rettv->vval.v_string != NULL)
4977 {
4978 rettv->vval.v_string[0] = get_cmdline_type();
4979 rettv->vval.v_string[1] = NUL;
4980 }
4981}
4982
4983/*
4984 * "getcmdwintype()" function
4985 */
4986 static void
4987f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4988{
4989 rettv->v_type = VAR_STRING;
4990 rettv->vval.v_string = NULL;
4991#ifdef FEAT_CMDWIN
4992 rettv->vval.v_string = alloc(2);
4993 if (rettv->vval.v_string != NULL)
4994 {
4995 rettv->vval.v_string[0] = cmdwin_type;
4996 rettv->vval.v_string[1] = NUL;
4997 }
4998#endif
4999}
5000
5001#if defined(FEAT_CMDL_COMPL)
5002/*
5003 * "getcompletion()" function
5004 */
5005 static void
5006f_getcompletion(typval_T *argvars, typval_T *rettv)
5007{
5008 char_u *pat;
5009 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005010 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005011 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5012 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005013
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005015 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005016
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 if (p_wic)
5018 options |= WILD_ICASE;
5019
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005020 /* For filtered results, 'wildignore' is used */
5021 if (!filtered)
5022 options |= WILD_KEEP_ALL;
5023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005025 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005027 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005028 if (xpc.xp_context == EXPAND_NOTHING)
5029 {
5030 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005031 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005033 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005034 return;
5035 }
5036
5037# if defined(FEAT_MENU)
5038 if (xpc.xp_context == EXPAND_MENUS)
5039 {
5040 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5041 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5042 }
5043# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005044#ifdef FEAT_CSCOPE
5045 if (xpc.xp_context == EXPAND_CSCOPE)
5046 {
5047 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5048 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5049 }
5050#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005051#ifdef FEAT_SIGNS
5052 if (xpc.xp_context == EXPAND_SIGN)
5053 {
5054 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5055 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5056 }
5057#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005058
5059 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5060 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5061 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005062 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005063
5064 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5065
5066 for (i = 0; i < xpc.xp_numfiles; i++)
5067 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5068 }
5069 vim_free(pat);
5070 ExpandCleanup(&xpc);
5071}
5072#endif
5073
5074/*
5075 * "getcwd()" function
5076 */
5077 static void
5078f_getcwd(typval_T *argvars, typval_T *rettv)
5079{
5080 win_T *wp = NULL;
5081 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005082 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083
5084 rettv->v_type = VAR_STRING;
5085 rettv->vval.v_string = NULL;
5086
Bram Moolenaar54591292018-02-09 20:53:59 +01005087 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5088 global = TRUE;
5089 else
5090 wp = find_tabwin(&argvars[0], &argvars[1]);
5091
5092 if (wp != NULL && wp->w_localdir != NULL)
5093 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5094 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005096 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 rettv->vval.v_string = vim_strsave(globaldir);
5098 else
5099 {
5100 cwd = alloc(MAXPATHL);
5101 if (cwd != NULL)
5102 {
5103 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5104 rettv->vval.v_string = vim_strsave(cwd);
5105 vim_free(cwd);
5106 }
5107 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005109#ifdef BACKSLASH_IN_FILENAME
5110 if (rettv->vval.v_string != NULL)
5111 slash_adjust(rettv->vval.v_string);
5112#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113}
5114
5115/*
5116 * "getfontname()" function
5117 */
5118 static void
5119f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5120{
5121 rettv->v_type = VAR_STRING;
5122 rettv->vval.v_string = NULL;
5123#ifdef FEAT_GUI
5124 if (gui.in_use)
5125 {
5126 GuiFont font;
5127 char_u *name = NULL;
5128
5129 if (argvars[0].v_type == VAR_UNKNOWN)
5130 {
5131 /* Get the "Normal" font. Either the name saved by
5132 * hl_set_font_name() or from the font ID. */
5133 font = gui.norm_font;
5134 name = hl_get_font_name();
5135 }
5136 else
5137 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005138 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5140 return;
5141 font = gui_mch_get_font(name, FALSE);
5142 if (font == NOFONT)
5143 return; /* Invalid font name, return empty string. */
5144 }
5145 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5146 if (argvars[0].v_type != VAR_UNKNOWN)
5147 gui_mch_free_font(font);
5148 }
5149#endif
5150}
5151
5152/*
5153 * "getfperm({fname})" function
5154 */
5155 static void
5156f_getfperm(typval_T *argvars, typval_T *rettv)
5157{
5158 char_u *fname;
5159 stat_T st;
5160 char_u *perm = NULL;
5161 char_u flags[] = "rwx";
5162 int i;
5163
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005164 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005165
5166 rettv->v_type = VAR_STRING;
5167 if (mch_stat((char *)fname, &st) >= 0)
5168 {
5169 perm = vim_strsave((char_u *)"---------");
5170 if (perm != NULL)
5171 {
5172 for (i = 0; i < 9; i++)
5173 {
5174 if (st.st_mode & (1 << (8 - i)))
5175 perm[i] = flags[i % 3];
5176 }
5177 }
5178 }
5179 rettv->vval.v_string = perm;
5180}
5181
5182/*
5183 * "getfsize({fname})" function
5184 */
5185 static void
5186f_getfsize(typval_T *argvars, typval_T *rettv)
5187{
5188 char_u *fname;
5189 stat_T st;
5190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005191 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005192
5193 rettv->v_type = VAR_NUMBER;
5194
5195 if (mch_stat((char *)fname, &st) >= 0)
5196 {
5197 if (mch_isdir(fname))
5198 rettv->vval.v_number = 0;
5199 else
5200 {
5201 rettv->vval.v_number = (varnumber_T)st.st_size;
5202
5203 /* non-perfect check for overflow */
5204 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5205 rettv->vval.v_number = -2;
5206 }
5207 }
5208 else
5209 rettv->vval.v_number = -1;
5210}
5211
5212/*
5213 * "getftime({fname})" function
5214 */
5215 static void
5216f_getftime(typval_T *argvars, typval_T *rettv)
5217{
5218 char_u *fname;
5219 stat_T st;
5220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005221 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222
5223 if (mch_stat((char *)fname, &st) >= 0)
5224 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5225 else
5226 rettv->vval.v_number = -1;
5227}
5228
5229/*
5230 * "getftype({fname})" function
5231 */
5232 static void
5233f_getftype(typval_T *argvars, typval_T *rettv)
5234{
5235 char_u *fname;
5236 stat_T st;
5237 char_u *type = NULL;
5238 char *t;
5239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005240 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241
5242 rettv->v_type = VAR_STRING;
5243 if (mch_lstat((char *)fname, &st) >= 0)
5244 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245 if (S_ISREG(st.st_mode))
5246 t = "file";
5247 else if (S_ISDIR(st.st_mode))
5248 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249 else if (S_ISLNK(st.st_mode))
5250 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005251 else if (S_ISBLK(st.st_mode))
5252 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005253 else if (S_ISCHR(st.st_mode))
5254 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255 else if (S_ISFIFO(st.st_mode))
5256 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005257 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005258 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005259 else
5260 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261 type = vim_strsave((char_u *)t);
5262 }
5263 rettv->vval.v_string = type;
5264}
5265
5266/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005267 * "getjumplist()" function
5268 */
5269 static void
5270f_getjumplist(typval_T *argvars, typval_T *rettv)
5271{
5272#ifdef FEAT_JUMPLIST
5273 win_T *wp;
5274 int i;
5275 list_T *l;
5276 dict_T *d;
5277#endif
5278
5279 if (rettv_list_alloc(rettv) != OK)
5280 return;
5281
5282#ifdef FEAT_JUMPLIST
5283 wp = find_tabwin(&argvars[0], &argvars[1]);
5284 if (wp == NULL)
5285 return;
5286
5287 l = list_alloc();
5288 if (l == NULL)
5289 return;
5290
5291 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5292 return;
5293 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5294
Bram Moolenaar48679742018-02-13 13:33:29 +01005295 cleanup_jumplist(wp, TRUE);
5296
Bram Moolenaar4f505882018-02-10 21:06:32 +01005297 for (i = 0; i < wp->w_jumplistlen; ++i)
5298 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005299 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5300 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005301 if ((d = dict_alloc()) == NULL)
5302 return;
5303 if (list_append_dict(l, d) == FAIL)
5304 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005305 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5306 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005307# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005308 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005309# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005310 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005311 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005312 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005313 }
5314#endif
5315}
5316
5317/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 * "getline(lnum, [end])" function
5319 */
5320 static void
5321f_getline(typval_T *argvars, typval_T *rettv)
5322{
5323 linenr_T lnum;
5324 linenr_T end;
5325 int retlist;
5326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005327 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005328 if (argvars[1].v_type == VAR_UNKNOWN)
5329 {
5330 end = 0;
5331 retlist = FALSE;
5332 }
5333 else
5334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005335 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336 retlist = TRUE;
5337 }
5338
5339 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5340}
5341
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005342#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005343 static void
5344get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5345{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005346 if (what_arg->v_type == VAR_UNKNOWN)
5347 {
5348 if (rettv_list_alloc(rettv) == OK)
5349 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005350 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005351 }
5352 else
5353 {
5354 if (rettv_dict_alloc(rettv) == OK)
5355 if (is_qf || (wp != NULL))
5356 {
5357 if (what_arg->v_type == VAR_DICT)
5358 {
5359 dict_T *d = what_arg->vval.v_dict;
5360
5361 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005362 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005363 }
5364 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005365 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005366 }
5367 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005368}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005369#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005370
5371/*
5372 * "getloclist()" function
5373 */
5374 static void
5375f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5376{
5377#ifdef FEAT_QUICKFIX
5378 win_T *wp;
5379
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005380 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005381 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5382#endif
5383}
5384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005385/*
5386 * "getmatches()" function
5387 */
5388 static void
5389f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5390{
5391#ifdef FEAT_SEARCH_EXTRA
5392 dict_T *dict;
5393 matchitem_T *cur = curwin->w_match_head;
5394 int i;
5395
5396 if (rettv_list_alloc(rettv) == OK)
5397 {
5398 while (cur != NULL)
5399 {
5400 dict = dict_alloc();
5401 if (dict == NULL)
5402 return;
5403 if (cur->match.regprog == NULL)
5404 {
5405 /* match added with matchaddpos() */
5406 for (i = 0; i < MAXPOSMATCH; ++i)
5407 {
5408 llpos_T *llpos;
5409 char buf[6];
5410 list_T *l;
5411
5412 llpos = &cur->pos.pos[i];
5413 if (llpos->lnum == 0)
5414 break;
5415 l = list_alloc();
5416 if (l == NULL)
5417 break;
5418 list_append_number(l, (varnumber_T)llpos->lnum);
5419 if (llpos->col > 0)
5420 {
5421 list_append_number(l, (varnumber_T)llpos->col);
5422 list_append_number(l, (varnumber_T)llpos->len);
5423 }
5424 sprintf(buf, "pos%d", i + 1);
5425 dict_add_list(dict, buf, l);
5426 }
5427 }
5428 else
5429 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005430 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005431 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005432 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5433 dict_add_number(dict, "priority", (long)cur->priority);
5434 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar13505972019-01-24 15:04:48 +01005435# if defined(FEAT_CONCEAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005436 if (cur->conceal_char)
5437 {
5438 char_u buf[MB_MAXBYTES + 1];
5439
5440 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005441 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005442 }
5443# endif
5444 list_append_dict(rettv->vval.v_list, dict);
5445 cur = cur->next;
5446 }
5447 }
5448#endif
5449}
5450
5451/*
5452 * "getpid()" function
5453 */
5454 static void
5455f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5456{
5457 rettv->vval.v_number = mch_get_pid();
5458}
5459
5460 static void
5461getpos_both(
5462 typval_T *argvars,
5463 typval_T *rettv,
5464 int getcurpos)
5465{
5466 pos_T *fp;
5467 list_T *l;
5468 int fnum = -1;
5469
5470 if (rettv_list_alloc(rettv) == OK)
5471 {
5472 l = rettv->vval.v_list;
5473 if (getcurpos)
5474 fp = &curwin->w_cursor;
5475 else
5476 fp = var2fpos(&argvars[0], TRUE, &fnum);
5477 if (fnum != -1)
5478 list_append_number(l, (varnumber_T)fnum);
5479 else
5480 list_append_number(l, (varnumber_T)0);
5481 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5482 : (varnumber_T)0);
5483 list_append_number(l, (fp != NULL)
5484 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5485 : (varnumber_T)0);
5486 list_append_number(l,
5487#ifdef FEAT_VIRTUALEDIT
5488 (fp != NULL) ? (varnumber_T)fp->coladd :
5489#endif
5490 (varnumber_T)0);
5491 if (getcurpos)
5492 {
5493 update_curswant();
5494 list_append_number(l, curwin->w_curswant == MAXCOL ?
5495 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5496 }
5497 }
5498 else
5499 rettv->vval.v_number = FALSE;
5500}
5501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502/*
5503 * "getcurpos()" function
5504 */
5505 static void
5506f_getcurpos(typval_T *argvars, typval_T *rettv)
5507{
5508 getpos_both(argvars, rettv, TRUE);
5509}
5510
5511/*
5512 * "getpos(string)" function
5513 */
5514 static void
5515f_getpos(typval_T *argvars, typval_T *rettv)
5516{
5517 getpos_both(argvars, rettv, FALSE);
5518}
5519
5520/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005521 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005522 */
5523 static void
5524f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5525{
5526#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005527 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005528#endif
5529}
5530
5531/*
5532 * "getreg()" function
5533 */
5534 static void
5535f_getreg(typval_T *argvars, typval_T *rettv)
5536{
5537 char_u *strregname;
5538 int regname;
5539 int arg2 = FALSE;
5540 int return_list = FALSE;
5541 int error = FALSE;
5542
5543 if (argvars[0].v_type != VAR_UNKNOWN)
5544 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005545 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 error = strregname == NULL;
5547 if (argvars[1].v_type != VAR_UNKNOWN)
5548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005549 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005551 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 }
5553 }
5554 else
5555 strregname = get_vim_var_str(VV_REG);
5556
5557 if (error)
5558 return;
5559
5560 regname = (strregname == NULL ? '"' : *strregname);
5561 if (regname == 0)
5562 regname = '"';
5563
5564 if (return_list)
5565 {
5566 rettv->v_type = VAR_LIST;
5567 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5568 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5569 if (rettv->vval.v_list == NULL)
5570 (void)rettv_list_alloc(rettv);
5571 else
5572 ++rettv->vval.v_list->lv_refcount;
5573 }
5574 else
5575 {
5576 rettv->v_type = VAR_STRING;
5577 rettv->vval.v_string = get_reg_contents(regname,
5578 arg2 ? GREG_EXPR_SRC : 0);
5579 }
5580}
5581
5582/*
5583 * "getregtype()" function
5584 */
5585 static void
5586f_getregtype(typval_T *argvars, typval_T *rettv)
5587{
5588 char_u *strregname;
5589 int regname;
5590 char_u buf[NUMBUFLEN + 2];
5591 long reglen = 0;
5592
5593 if (argvars[0].v_type != VAR_UNKNOWN)
5594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005595 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005596 if (strregname == NULL) /* type error; errmsg already given */
5597 {
5598 rettv->v_type = VAR_STRING;
5599 rettv->vval.v_string = NULL;
5600 return;
5601 }
5602 }
5603 else
5604 /* Default to v:register */
5605 strregname = get_vim_var_str(VV_REG);
5606
5607 regname = (strregname == NULL ? '"' : *strregname);
5608 if (regname == 0)
5609 regname = '"';
5610
5611 buf[0] = NUL;
5612 buf[1] = NUL;
5613 switch (get_reg_type(regname, &reglen))
5614 {
5615 case MLINE: buf[0] = 'V'; break;
5616 case MCHAR: buf[0] = 'v'; break;
5617 case MBLOCK:
5618 buf[0] = Ctrl_V;
5619 sprintf((char *)buf + 1, "%ld", reglen + 1);
5620 break;
5621 }
5622 rettv->v_type = VAR_STRING;
5623 rettv->vval.v_string = vim_strsave(buf);
5624}
5625
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005626/*
5627 * Returns information (variables, options, etc.) about a tab page
5628 * as a dictionary.
5629 */
5630 static dict_T *
5631get_tabpage_info(tabpage_T *tp, int tp_idx)
5632{
5633 win_T *wp;
5634 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005635 list_T *l;
5636
5637 dict = dict_alloc();
5638 if (dict == NULL)
5639 return NULL;
5640
Bram Moolenaare0be1672018-07-08 16:50:37 +02005641 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005642
5643 l = list_alloc();
5644 if (l != NULL)
5645 {
5646 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5647 wp; wp = wp->w_next)
5648 list_append_number(l, (varnumber_T)wp->w_id);
5649 dict_add_list(dict, "windows", l);
5650 }
5651
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005652 /* Make a reference to tabpage variables */
5653 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005654
5655 return dict;
5656}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005657
5658/*
5659 * "gettabinfo()" function
5660 */
5661 static void
5662f_gettabinfo(typval_T *argvars, typval_T *rettv)
5663{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005664 tabpage_T *tp, *tparg = NULL;
5665 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005666 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005667
5668 if (rettv_list_alloc(rettv) != OK)
5669 return;
5670
5671 if (argvars[0].v_type != VAR_UNKNOWN)
5672 {
5673 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005674 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005675 if (tparg == NULL)
5676 return;
5677 }
5678
5679 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005680 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005681 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005682 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005683 if (tparg != NULL && tp != tparg)
5684 continue;
5685 d = get_tabpage_info(tp, tpnr);
5686 if (d != NULL)
5687 list_append_dict(rettv->vval.v_list, d);
5688 if (tparg != NULL)
5689 return;
5690 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005691}
5692
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693/*
5694 * "gettabvar()" function
5695 */
5696 static void
5697f_gettabvar(typval_T *argvars, typval_T *rettv)
5698{
5699 win_T *oldcurwin;
5700 tabpage_T *tp, *oldtabpage;
5701 dictitem_T *v;
5702 char_u *varname;
5703 int done = FALSE;
5704
5705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = NULL;
5707
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005708 varname = tv_get_string_chk(&argvars[1]);
5709 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710 if (tp != NULL && varname != NULL)
5711 {
5712 /* Set tp to be our tabpage, temporarily. Also set the window to the
5713 * first window in the tabpage, otherwise the window is not valid. */
5714 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005715 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5716 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 {
5718 /* look up the variable */
5719 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5720 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5721 if (v != NULL)
5722 {
5723 copy_tv(&v->di_tv, rettv);
5724 done = TRUE;
5725 }
5726 }
5727
5728 /* restore previous notion of curwin */
5729 restore_win(oldcurwin, oldtabpage, TRUE);
5730 }
5731
5732 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5733 copy_tv(&argvars[2], rettv);
5734}
5735
5736/*
5737 * "gettabwinvar()" function
5738 */
5739 static void
5740f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5741{
5742 getwinvar(argvars, rettv, 1);
5743}
5744
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005745/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005746 * "gettagstack()" function
5747 */
5748 static void
5749f_gettagstack(typval_T *argvars, typval_T *rettv)
5750{
5751 win_T *wp = curwin; // default is current window
5752
5753 if (rettv_dict_alloc(rettv) != OK)
5754 return;
5755
5756 if (argvars[0].v_type != VAR_UNKNOWN)
5757 {
5758 wp = find_win_by_nr_or_id(&argvars[0]);
5759 if (wp == NULL)
5760 return;
5761 }
5762
5763 get_tagstack(wp, rettv->vval.v_dict);
5764}
5765
5766/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005767 * Returns information about a window as a dictionary.
5768 */
5769 static dict_T *
5770get_win_info(win_T *wp, short tpnr, short winnr)
5771{
5772 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005773
5774 dict = dict_alloc();
5775 if (dict == NULL)
5776 return NULL;
5777
Bram Moolenaare0be1672018-07-08 16:50:37 +02005778 dict_add_number(dict, "tabnr", tpnr);
5779 dict_add_number(dict, "winnr", winnr);
5780 dict_add_number(dict, "winid", wp->w_id);
5781 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005782 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005783#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005784 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005785#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005786 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005787 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005788 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005789
Bram Moolenaar69905d12017-08-13 18:14:47 +02005790#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005791 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005792#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005793#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005794 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5795 dict_add_number(dict, "loclist",
5796 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005797#endif
5798
Bram Moolenaar30567352016-08-27 21:25:44 +02005799 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005800 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005801
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005802 return dict;
5803}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005804
5805/*
5806 * "getwininfo()" function
5807 */
5808 static void
5809f_getwininfo(typval_T *argvars, typval_T *rettv)
5810{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005811 tabpage_T *tp;
5812 win_T *wp = NULL, *wparg = NULL;
5813 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005814 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005815
5816 if (rettv_list_alloc(rettv) != OK)
5817 return;
5818
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005819 if (argvars[0].v_type != VAR_UNKNOWN)
5820 {
5821 wparg = win_id2wp(argvars);
5822 if (wparg == NULL)
5823 return;
5824 }
5825
5826 /* Collect information about either all the windows across all the tab
5827 * pages or one particular window.
5828 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005829 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005830 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005831 tabnr++;
5832 winnr = 0;
5833 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005834 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005835 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005836 if (wparg != NULL && wp != wparg)
5837 continue;
5838 d = get_win_info(wp, tabnr, winnr);
5839 if (d != NULL)
5840 list_append_dict(rettv->vval.v_list, d);
5841 if (wparg != NULL)
5842 /* found information about a specific window */
5843 return;
5844 }
5845 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005846}
5847
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005848/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 * "win_findbuf()" function
5850 */
5851 static void
5852f_win_findbuf(typval_T *argvars, typval_T *rettv)
5853{
5854 if (rettv_list_alloc(rettv) != FAIL)
5855 win_findbuf(argvars, rettv->vval.v_list);
5856}
5857
5858/*
5859 * "win_getid()" function
5860 */
5861 static void
5862f_win_getid(typval_T *argvars, typval_T *rettv)
5863{
5864 rettv->vval.v_number = win_getid(argvars);
5865}
5866
5867/*
5868 * "win_gotoid()" function
5869 */
5870 static void
5871f_win_gotoid(typval_T *argvars, typval_T *rettv)
5872{
5873 rettv->vval.v_number = win_gotoid(argvars);
5874}
5875
5876/*
5877 * "win_id2tabwin()" function
5878 */
5879 static void
5880f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5881{
5882 if (rettv_list_alloc(rettv) != FAIL)
5883 win_id2tabwin(argvars, rettv->vval.v_list);
5884}
5885
5886/*
5887 * "win_id2win()" function
5888 */
5889 static void
5890f_win_id2win(typval_T *argvars, typval_T *rettv)
5891{
5892 rettv->vval.v_number = win_id2win(argvars);
5893}
5894
5895/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005896 * "win_screenpos()" function
5897 */
5898 static void
5899f_win_screenpos(typval_T *argvars, typval_T *rettv)
5900{
5901 win_T *wp;
5902
5903 if (rettv_list_alloc(rettv) == FAIL)
5904 return;
5905
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005906 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005907 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5908 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5909}
5910
5911/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005912 * "getwinpos({timeout})" function
5913 */
5914 static void
5915f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5916{
5917 int x = -1;
5918 int y = -1;
5919
5920 if (rettv_list_alloc(rettv) == FAIL)
5921 return;
5922#ifdef FEAT_GUI
5923 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005924 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005925# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5926 else
5927# endif
5928#endif
5929#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5930 {
5931 varnumber_T timeout = 100;
5932
5933 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005934 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005935 term_get_winpos(&x, &y, timeout);
5936 }
5937#endif
5938 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5939 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5940}
5941
5942
5943/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005944 * "getwinposx()" function
5945 */
5946 static void
5947f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5948{
5949 rettv->vval.v_number = -1;
5950#ifdef FEAT_GUI
5951 if (gui.in_use)
5952 {
5953 int x, y;
5954
5955 if (gui_mch_get_winpos(&x, &y) == OK)
5956 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005957 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005958 }
5959#endif
5960#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5961 {
5962 int x, y;
5963
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005964 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005965 rettv->vval.v_number = x;
5966 }
5967#endif
5968}
5969
5970/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005971 * "getwinposy()" function
5972 */
5973 static void
5974f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5975{
5976 rettv->vval.v_number = -1;
5977#ifdef FEAT_GUI
5978 if (gui.in_use)
5979 {
5980 int x, y;
5981
5982 if (gui_mch_get_winpos(&x, &y) == OK)
5983 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005984 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005985 }
5986#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005987#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5988 {
5989 int x, y;
5990
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005991 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005992 rettv->vval.v_number = y;
5993 }
5994#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005995}
5996
5997/*
5998 * "getwinvar()" function
5999 */
6000 static void
6001f_getwinvar(typval_T *argvars, typval_T *rettv)
6002{
6003 getwinvar(argvars, rettv, 0);
6004}
6005
6006/*
6007 * "glob()" function
6008 */
6009 static void
6010f_glob(typval_T *argvars, typval_T *rettv)
6011{
6012 int options = WILD_SILENT|WILD_USE_NL;
6013 expand_T xpc;
6014 int error = FALSE;
6015
6016 /* When the optional second argument is non-zero, don't remove matches
6017 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6018 rettv->v_type = VAR_STRING;
6019 if (argvars[1].v_type != VAR_UNKNOWN)
6020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006021 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 options |= WILD_KEEP_ALL;
6023 if (argvars[2].v_type != VAR_UNKNOWN)
6024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006025 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006026 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006027 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006028 }
6029 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006030 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006031 options |= WILD_ALLLINKS;
6032 }
6033 }
6034 if (!error)
6035 {
6036 ExpandInit(&xpc);
6037 xpc.xp_context = EXPAND_FILES;
6038 if (p_wic)
6039 options += WILD_ICASE;
6040 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006041 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006042 NULL, options, WILD_ALL);
6043 else if (rettv_list_alloc(rettv) != FAIL)
6044 {
6045 int i;
6046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006047 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006048 NULL, options, WILD_ALL_KEEP);
6049 for (i = 0; i < xpc.xp_numfiles; i++)
6050 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6051
6052 ExpandCleanup(&xpc);
6053 }
6054 }
6055 else
6056 rettv->vval.v_string = NULL;
6057}
6058
6059/*
6060 * "globpath()" function
6061 */
6062 static void
6063f_globpath(typval_T *argvars, typval_T *rettv)
6064{
6065 int flags = 0;
6066 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006067 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006068 int error = FALSE;
6069 garray_T ga;
6070 int i;
6071
6072 /* When the optional second argument is non-zero, don't remove matches
6073 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6074 rettv->v_type = VAR_STRING;
6075 if (argvars[2].v_type != VAR_UNKNOWN)
6076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006077 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006078 flags |= WILD_KEEP_ALL;
6079 if (argvars[3].v_type != VAR_UNKNOWN)
6080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006081 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006082 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006083 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006084 }
6085 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006086 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006087 flags |= WILD_ALLLINKS;
6088 }
6089 }
6090 if (file != NULL && !error)
6091 {
6092 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006093 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 if (rettv->v_type == VAR_STRING)
6095 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6096 else if (rettv_list_alloc(rettv) != FAIL)
6097 for (i = 0; i < ga.ga_len; ++i)
6098 list_append_string(rettv->vval.v_list,
6099 ((char_u **)(ga.ga_data))[i], -1);
6100 ga_clear_strings(&ga);
6101 }
6102 else
6103 rettv->vval.v_string = NULL;
6104}
6105
6106/*
6107 * "glob2regpat()" function
6108 */
6109 static void
6110f_glob2regpat(typval_T *argvars, typval_T *rettv)
6111{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006112 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006113
6114 rettv->v_type = VAR_STRING;
6115 rettv->vval.v_string = (pat == NULL)
6116 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6117}
6118
6119/* for VIM_VERSION_ defines */
6120#include "version.h"
6121
6122/*
6123 * "has()" function
6124 */
6125 static void
6126f_has(typval_T *argvars, typval_T *rettv)
6127{
6128 int i;
6129 char_u *name;
6130 int n = FALSE;
6131 static char *(has_list[]) =
6132 {
6133#ifdef AMIGA
6134 "amiga",
6135# ifdef FEAT_ARP
6136 "arp",
6137# endif
6138#endif
6139#ifdef __BEOS__
6140 "beos",
6141#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006142#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006143 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6144 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006145# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006146 "macunix", /* Mac OS X, with the darwin feature */
6147 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006148# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149#endif
6150#ifdef __QNX__
6151 "qnx",
6152#endif
6153#ifdef UNIX
6154 "unix",
6155#endif
6156#ifdef VMS
6157 "vms",
6158#endif
6159#ifdef WIN32
6160 "win32",
6161#endif
6162#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6163 "win32unix",
6164#endif
6165#if defined(WIN64) || defined(_WIN64)
6166 "win64",
6167#endif
6168#ifdef EBCDIC
6169 "ebcdic",
6170#endif
6171#ifndef CASE_INSENSITIVE_FILENAME
6172 "fname_case",
6173#endif
6174#ifdef HAVE_ACL
6175 "acl",
6176#endif
6177#ifdef FEAT_ARABIC
6178 "arabic",
6179#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006180 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006181#ifdef FEAT_AUTOCHDIR
6182 "autochdir",
6183#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006184#ifdef FEAT_AUTOSERVERNAME
6185 "autoservername",
6186#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006187#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006188 "balloon_eval",
6189# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6190 "balloon_multiline",
6191# endif
6192#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006193#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006194 "balloon_eval_term",
6195#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006196#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6197 "builtin_terms",
6198# ifdef ALL_BUILTIN_TCAPS
6199 "all_builtin_terms",
6200# endif
6201#endif
6202#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6203 || defined(FEAT_GUI_W32) \
6204 || defined(FEAT_GUI_MOTIF))
6205 "browsefilter",
6206#endif
6207#ifdef FEAT_BYTEOFF
6208 "byte_offset",
6209#endif
6210#ifdef FEAT_JOB_CHANNEL
6211 "channel",
6212#endif
6213#ifdef FEAT_CINDENT
6214 "cindent",
6215#endif
6216#ifdef FEAT_CLIENTSERVER
6217 "clientserver",
6218#endif
6219#ifdef FEAT_CLIPBOARD
6220 "clipboard",
6221#endif
6222#ifdef FEAT_CMDL_COMPL
6223 "cmdline_compl",
6224#endif
6225#ifdef FEAT_CMDHIST
6226 "cmdline_hist",
6227#endif
6228#ifdef FEAT_COMMENTS
6229 "comments",
6230#endif
6231#ifdef FEAT_CONCEAL
6232 "conceal",
6233#endif
6234#ifdef FEAT_CRYPT
6235 "cryptv",
6236 "crypt-blowfish",
6237 "crypt-blowfish2",
6238#endif
6239#ifdef FEAT_CSCOPE
6240 "cscope",
6241#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006242 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006243#ifdef CURSOR_SHAPE
6244 "cursorshape",
6245#endif
6246#ifdef DEBUG
6247 "debug",
6248#endif
6249#ifdef FEAT_CON_DIALOG
6250 "dialog_con",
6251#endif
6252#ifdef FEAT_GUI_DIALOG
6253 "dialog_gui",
6254#endif
6255#ifdef FEAT_DIFF
6256 "diff",
6257#endif
6258#ifdef FEAT_DIGRAPHS
6259 "digraphs",
6260#endif
6261#ifdef FEAT_DIRECTX
6262 "directx",
6263#endif
6264#ifdef FEAT_DND
6265 "dnd",
6266#endif
6267#ifdef FEAT_EMACS_TAGS
6268 "emacs_tags",
6269#endif
6270 "eval", /* always present, of course! */
6271 "ex_extra", /* graduated feature */
6272#ifdef FEAT_SEARCH_EXTRA
6273 "extra_search",
6274#endif
6275#ifdef FEAT_FKMAP
6276 "farsi",
6277#endif
6278#ifdef FEAT_SEARCHPATH
6279 "file_in_path",
6280#endif
6281#ifdef FEAT_FILTERPIPE
6282 "filterpipe",
6283#endif
6284#ifdef FEAT_FIND_ID
6285 "find_in_path",
6286#endif
6287#ifdef FEAT_FLOAT
6288 "float",
6289#endif
6290#ifdef FEAT_FOLDING
6291 "folding",
6292#endif
6293#ifdef FEAT_FOOTER
6294 "footer",
6295#endif
6296#if !defined(USE_SYSTEM) && defined(UNIX)
6297 "fork",
6298#endif
6299#ifdef FEAT_GETTEXT
6300 "gettext",
6301#endif
6302#ifdef FEAT_GUI
6303 "gui",
6304#endif
6305#ifdef FEAT_GUI_ATHENA
6306# ifdef FEAT_GUI_NEXTAW
6307 "gui_neXtaw",
6308# else
6309 "gui_athena",
6310# endif
6311#endif
6312#ifdef FEAT_GUI_GTK
6313 "gui_gtk",
6314# ifdef USE_GTK3
6315 "gui_gtk3",
6316# else
6317 "gui_gtk2",
6318# endif
6319#endif
6320#ifdef FEAT_GUI_GNOME
6321 "gui_gnome",
6322#endif
6323#ifdef FEAT_GUI_MAC
6324 "gui_mac",
6325#endif
6326#ifdef FEAT_GUI_MOTIF
6327 "gui_motif",
6328#endif
6329#ifdef FEAT_GUI_PHOTON
6330 "gui_photon",
6331#endif
6332#ifdef FEAT_GUI_W32
6333 "gui_win32",
6334#endif
6335#ifdef FEAT_HANGULIN
6336 "hangul_input",
6337#endif
6338#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6339 "iconv",
6340#endif
6341#ifdef FEAT_INS_EXPAND
6342 "insert_expand",
6343#endif
6344#ifdef FEAT_JOB_CHANNEL
6345 "job",
6346#endif
6347#ifdef FEAT_JUMPLIST
6348 "jumplist",
6349#endif
6350#ifdef FEAT_KEYMAP
6351 "keymap",
6352#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006353 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006354#ifdef FEAT_LANGMAP
6355 "langmap",
6356#endif
6357#ifdef FEAT_LIBCALL
6358 "libcall",
6359#endif
6360#ifdef FEAT_LINEBREAK
6361 "linebreak",
6362#endif
6363#ifdef FEAT_LISP
6364 "lispindent",
6365#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006366 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006367#ifdef FEAT_LOCALMAP
6368 "localmap",
6369#endif
6370#ifdef FEAT_LUA
6371# ifndef DYNAMIC_LUA
6372 "lua",
6373# endif
6374#endif
6375#ifdef FEAT_MENU
6376 "menu",
6377#endif
6378#ifdef FEAT_SESSION
6379 "mksession",
6380#endif
6381#ifdef FEAT_MODIFY_FNAME
6382 "modify_fname",
6383#endif
6384#ifdef FEAT_MOUSE
6385 "mouse",
6386#endif
6387#ifdef FEAT_MOUSESHAPE
6388 "mouseshape",
6389#endif
6390#if defined(UNIX) || defined(VMS)
6391# ifdef FEAT_MOUSE_DEC
6392 "mouse_dec",
6393# endif
6394# ifdef FEAT_MOUSE_GPM
6395 "mouse_gpm",
6396# endif
6397# ifdef FEAT_MOUSE_JSB
6398 "mouse_jsbterm",
6399# endif
6400# ifdef FEAT_MOUSE_NET
6401 "mouse_netterm",
6402# endif
6403# ifdef FEAT_MOUSE_PTERM
6404 "mouse_pterm",
6405# endif
6406# ifdef FEAT_MOUSE_SGR
6407 "mouse_sgr",
6408# endif
6409# ifdef FEAT_SYSMOUSE
6410 "mouse_sysmouse",
6411# endif
6412# ifdef FEAT_MOUSE_URXVT
6413 "mouse_urxvt",
6414# endif
6415# ifdef FEAT_MOUSE_XTERM
6416 "mouse_xterm",
6417# endif
6418#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006419 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006420#ifdef FEAT_MBYTE_IME
6421 "multi_byte_ime",
6422#endif
6423#ifdef FEAT_MULTI_LANG
6424 "multi_lang",
6425#endif
6426#ifdef FEAT_MZSCHEME
6427#ifndef DYNAMIC_MZSCHEME
6428 "mzscheme",
6429#endif
6430#endif
6431#ifdef FEAT_NUM64
6432 "num64",
6433#endif
6434#ifdef FEAT_OLE
6435 "ole",
6436#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006437#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006439#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440#ifdef FEAT_PATH_EXTRA
6441 "path_extra",
6442#endif
6443#ifdef FEAT_PERL
6444#ifndef DYNAMIC_PERL
6445 "perl",
6446#endif
6447#endif
6448#ifdef FEAT_PERSISTENT_UNDO
6449 "persistent_undo",
6450#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006451#if defined(FEAT_PYTHON)
6452 "python_compiled",
6453# if defined(DYNAMIC_PYTHON)
6454 "python_dynamic",
6455# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006456 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006457 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006458# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006460#if defined(FEAT_PYTHON3)
6461 "python3_compiled",
6462# if defined(DYNAMIC_PYTHON3)
6463 "python3_dynamic",
6464# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006466 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006467# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468#endif
6469#ifdef FEAT_POSTSCRIPT
6470 "postscript",
6471#endif
6472#ifdef FEAT_PRINTER
6473 "printer",
6474#endif
6475#ifdef FEAT_PROFILE
6476 "profile",
6477#endif
6478#ifdef FEAT_RELTIME
6479 "reltime",
6480#endif
6481#ifdef FEAT_QUICKFIX
6482 "quickfix",
6483#endif
6484#ifdef FEAT_RIGHTLEFT
6485 "rightleft",
6486#endif
6487#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6488 "ruby",
6489#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006490 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491#ifdef FEAT_CMDL_INFO
6492 "showcmd",
6493 "cmdline_info",
6494#endif
6495#ifdef FEAT_SIGNS
6496 "signs",
6497#endif
6498#ifdef FEAT_SMARTINDENT
6499 "smartindent",
6500#endif
6501#ifdef STARTUPTIME
6502 "startuptime",
6503#endif
6504#ifdef FEAT_STL_OPT
6505 "statusline",
6506#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006507#ifdef FEAT_NETBEANS_INTG
6508 "netbeans_intg",
6509#endif
6510#ifdef FEAT_SPELL
6511 "spell",
6512#endif
6513#ifdef FEAT_SYN_HL
6514 "syntax",
6515#endif
6516#if defined(USE_SYSTEM) || !defined(UNIX)
6517 "system",
6518#endif
6519#ifdef FEAT_TAG_BINS
6520 "tag_binary",
6521#endif
6522#ifdef FEAT_TAG_OLDSTATIC
6523 "tag_old_static",
6524#endif
6525#ifdef FEAT_TAG_ANYWHITE
6526 "tag_any_white",
6527#endif
6528#ifdef FEAT_TCL
6529# ifndef DYNAMIC_TCL
6530 "tcl",
6531# endif
6532#endif
6533#ifdef FEAT_TERMGUICOLORS
6534 "termguicolors",
6535#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006536#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006537 "terminal",
6538#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006539#ifdef TERMINFO
6540 "terminfo",
6541#endif
6542#ifdef FEAT_TERMRESPONSE
6543 "termresponse",
6544#endif
6545#ifdef FEAT_TEXTOBJ
6546 "textobjects",
6547#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006548#ifdef FEAT_TEXT_PROP
6549 "textprop",
6550#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006551#ifdef HAVE_TGETENT
6552 "tgetent",
6553#endif
6554#ifdef FEAT_TIMERS
6555 "timers",
6556#endif
6557#ifdef FEAT_TITLE
6558 "title",
6559#endif
6560#ifdef FEAT_TOOLBAR
6561 "toolbar",
6562#endif
6563#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6564 "unnamedplus",
6565#endif
6566#ifdef FEAT_USR_CMDS
6567 "user-commands", /* was accidentally included in 5.4 */
6568 "user_commands",
6569#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006570#ifdef FEAT_VARTABS
6571 "vartabs",
6572#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006573#ifdef FEAT_VIMINFO
6574 "viminfo",
6575#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006576 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006577#ifdef FEAT_VIRTUALEDIT
6578 "virtualedit",
6579#endif
6580 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006581 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006583#ifdef FEAT_VTP
6584 "vtp",
6585#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#ifdef FEAT_WILDIGN
6587 "wildignore",
6588#endif
6589#ifdef FEAT_WILDMENU
6590 "wildmenu",
6591#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593#ifdef FEAT_WAK
6594 "winaltkeys",
6595#endif
6596#ifdef FEAT_WRITEBACKUP
6597 "writebackup",
6598#endif
6599#ifdef FEAT_XIM
6600 "xim",
6601#endif
6602#ifdef FEAT_XFONTSET
6603 "xfontset",
6604#endif
6605#ifdef FEAT_XPM_W32
6606 "xpm",
6607 "xpm_w32", /* for backward compatibility */
6608#else
6609# if defined(HAVE_XPM)
6610 "xpm",
6611# endif
6612#endif
6613#ifdef USE_XSMP
6614 "xsmp",
6615#endif
6616#ifdef USE_XSMP_INTERACT
6617 "xsmp_interact",
6618#endif
6619#ifdef FEAT_XCLIPBOARD
6620 "xterm_clipboard",
6621#endif
6622#ifdef FEAT_XTERM_SAVE
6623 "xterm_save",
6624#endif
6625#if defined(UNIX) && defined(FEAT_X11)
6626 "X11",
6627#endif
6628 NULL
6629 };
6630
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006631 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006632 for (i = 0; has_list[i] != NULL; ++i)
6633 if (STRICMP(name, has_list[i]) == 0)
6634 {
6635 n = TRUE;
6636 break;
6637 }
6638
6639 if (n == FALSE)
6640 {
6641 if (STRNICMP(name, "patch", 5) == 0)
6642 {
6643 if (name[5] == '-'
6644 && STRLEN(name) >= 11
6645 && vim_isdigit(name[6])
6646 && vim_isdigit(name[8])
6647 && vim_isdigit(name[10]))
6648 {
6649 int major = atoi((char *)name + 6);
6650 int minor = atoi((char *)name + 8);
6651
6652 /* Expect "patch-9.9.01234". */
6653 n = (major < VIM_VERSION_MAJOR
6654 || (major == VIM_VERSION_MAJOR
6655 && (minor < VIM_VERSION_MINOR
6656 || (minor == VIM_VERSION_MINOR
6657 && has_patch(atoi((char *)name + 10))))));
6658 }
6659 else
6660 n = has_patch(atoi((char *)name + 5));
6661 }
6662 else if (STRICMP(name, "vim_starting") == 0)
6663 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006664 else if (STRICMP(name, "ttyin") == 0)
6665 n = mch_input_isatty();
6666 else if (STRICMP(name, "ttyout") == 0)
6667 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006668 else if (STRICMP(name, "multi_byte_encoding") == 0)
6669 n = has_mbyte;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6671 else if (STRICMP(name, "balloon_multiline") == 0)
6672 n = multiline_balloon_available();
6673#endif
6674#ifdef DYNAMIC_TCL
6675 else if (STRICMP(name, "tcl") == 0)
6676 n = tcl_enabled(FALSE);
6677#endif
6678#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6679 else if (STRICMP(name, "iconv") == 0)
6680 n = iconv_enabled(FALSE);
6681#endif
6682#ifdef DYNAMIC_LUA
6683 else if (STRICMP(name, "lua") == 0)
6684 n = lua_enabled(FALSE);
6685#endif
6686#ifdef DYNAMIC_MZSCHEME
6687 else if (STRICMP(name, "mzscheme") == 0)
6688 n = mzscheme_enabled(FALSE);
6689#endif
6690#ifdef DYNAMIC_RUBY
6691 else if (STRICMP(name, "ruby") == 0)
6692 n = ruby_enabled(FALSE);
6693#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694#ifdef DYNAMIC_PYTHON
6695 else if (STRICMP(name, "python") == 0)
6696 n = python_enabled(FALSE);
6697#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698#ifdef DYNAMIC_PYTHON3
6699 else if (STRICMP(name, "python3") == 0)
6700 n = python3_enabled(FALSE);
6701#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006702#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6703 else if (STRICMP(name, "pythonx") == 0)
6704 {
6705# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6706 if (p_pyx == 0)
6707 n = python3_enabled(FALSE) || python_enabled(FALSE);
6708 else if (p_pyx == 3)
6709 n = python3_enabled(FALSE);
6710 else if (p_pyx == 2)
6711 n = python_enabled(FALSE);
6712# elif defined(DYNAMIC_PYTHON)
6713 n = python_enabled(FALSE);
6714# elif defined(DYNAMIC_PYTHON3)
6715 n = python3_enabled(FALSE);
6716# endif
6717 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006718#endif
6719#ifdef DYNAMIC_PERL
6720 else if (STRICMP(name, "perl") == 0)
6721 n = perl_enabled(FALSE);
6722#endif
6723#ifdef FEAT_GUI
6724 else if (STRICMP(name, "gui_running") == 0)
6725 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726# ifdef FEAT_BROWSE
6727 else if (STRICMP(name, "browse") == 0)
6728 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6729# endif
6730#endif
6731#ifdef FEAT_SYN_HL
6732 else if (STRICMP(name, "syntax_items") == 0)
6733 n = syntax_present(curwin);
6734#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006735#ifdef FEAT_VTP
6736 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006737 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738#endif
6739#ifdef FEAT_NETBEANS_INTG
6740 else if (STRICMP(name, "netbeans_enabled") == 0)
6741 n = netbeans_active();
6742#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006743#if defined(FEAT_TERMINAL) && defined(WIN3264)
6744 else if (STRICMP(name, "terminal") == 0)
6745 n = terminal_enabled();
6746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006747 }
6748
6749 rettv->vval.v_number = n;
6750}
6751
6752/*
6753 * "has_key()" function
6754 */
6755 static void
6756f_has_key(typval_T *argvars, typval_T *rettv)
6757{
6758 if (argvars[0].v_type != VAR_DICT)
6759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006760 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761 return;
6762 }
6763 if (argvars[0].vval.v_dict == NULL)
6764 return;
6765
6766 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006767 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768}
6769
6770/*
6771 * "haslocaldir()" function
6772 */
6773 static void
6774f_haslocaldir(typval_T *argvars, typval_T *rettv)
6775{
6776 win_T *wp = NULL;
6777
6778 wp = find_tabwin(&argvars[0], &argvars[1]);
6779 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6780}
6781
6782/*
6783 * "hasmapto()" function
6784 */
6785 static void
6786f_hasmapto(typval_T *argvars, typval_T *rettv)
6787{
6788 char_u *name;
6789 char_u *mode;
6790 char_u buf[NUMBUFLEN];
6791 int abbr = FALSE;
6792
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006793 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006794 if (argvars[1].v_type == VAR_UNKNOWN)
6795 mode = (char_u *)"nvo";
6796 else
6797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006798 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006800 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006801 }
6802
6803 if (map_to_exists(name, mode, abbr))
6804 rettv->vval.v_number = TRUE;
6805 else
6806 rettv->vval.v_number = FALSE;
6807}
6808
6809/*
6810 * "histadd()" function
6811 */
6812 static void
6813f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6814{
6815#ifdef FEAT_CMDHIST
6816 int histype;
6817 char_u *str;
6818 char_u buf[NUMBUFLEN];
6819#endif
6820
6821 rettv->vval.v_number = FALSE;
6822 if (check_restricted() || check_secure())
6823 return;
6824#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006825 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826 histype = str != NULL ? get_histtype(str) : -1;
6827 if (histype >= 0)
6828 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006829 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830 if (*str != NUL)
6831 {
6832 init_history();
6833 add_to_history(histype, str, FALSE, NUL);
6834 rettv->vval.v_number = TRUE;
6835 return;
6836 }
6837 }
6838#endif
6839}
6840
6841/*
6842 * "histdel()" function
6843 */
6844 static void
6845f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6846{
6847#ifdef FEAT_CMDHIST
6848 int n;
6849 char_u buf[NUMBUFLEN];
6850 char_u *str;
6851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006852 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853 if (str == NULL)
6854 n = 0;
6855 else if (argvars[1].v_type == VAR_UNKNOWN)
6856 /* only one argument: clear entire history */
6857 n = clr_history(get_histtype(str));
6858 else if (argvars[1].v_type == VAR_NUMBER)
6859 /* index given: remove that entry */
6860 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006861 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006862 else
6863 /* string given: remove all matching entries */
6864 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006865 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006866 rettv->vval.v_number = n;
6867#endif
6868}
6869
6870/*
6871 * "histget()" function
6872 */
6873 static void
6874f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6875{
6876#ifdef FEAT_CMDHIST
6877 int type;
6878 int idx;
6879 char_u *str;
6880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006881 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 if (str == NULL)
6883 rettv->vval.v_string = NULL;
6884 else
6885 {
6886 type = get_histtype(str);
6887 if (argvars[1].v_type == VAR_UNKNOWN)
6888 idx = get_history_idx(type);
6889 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006890 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 /* -1 on type error */
6892 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6893 }
6894#else
6895 rettv->vval.v_string = NULL;
6896#endif
6897 rettv->v_type = VAR_STRING;
6898}
6899
6900/*
6901 * "histnr()" function
6902 */
6903 static void
6904f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6905{
6906 int i;
6907
6908#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006909 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006910
6911 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6912 if (i >= HIST_CMD && i < HIST_COUNT)
6913 i = get_history_idx(i);
6914 else
6915#endif
6916 i = -1;
6917 rettv->vval.v_number = i;
6918}
6919
6920/*
6921 * "highlightID(name)" function
6922 */
6923 static void
6924f_hlID(typval_T *argvars, typval_T *rettv)
6925{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006926 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006927}
6928
6929/*
6930 * "highlight_exists()" function
6931 */
6932 static void
6933f_hlexists(typval_T *argvars, typval_T *rettv)
6934{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006935 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936}
6937
6938/*
6939 * "hostname()" function
6940 */
6941 static void
6942f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6943{
6944 char_u hostname[256];
6945
6946 mch_get_host_name(hostname, 256);
6947 rettv->v_type = VAR_STRING;
6948 rettv->vval.v_string = vim_strsave(hostname);
6949}
6950
6951/*
6952 * iconv() function
6953 */
6954 static void
6955f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6956{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957 char_u buf1[NUMBUFLEN];
6958 char_u buf2[NUMBUFLEN];
6959 char_u *from, *to, *str;
6960 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961
6962 rettv->v_type = VAR_STRING;
6963 rettv->vval.v_string = NULL;
6964
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006965 str = tv_get_string(&argvars[0]);
6966 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6967 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006968 vimconv.vc_type = CONV_NONE;
6969 convert_setup(&vimconv, from, to);
6970
6971 /* If the encodings are equal, no conversion needed. */
6972 if (vimconv.vc_type == CONV_NONE)
6973 rettv->vval.v_string = vim_strsave(str);
6974 else
6975 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6976
6977 convert_setup(&vimconv, NULL, NULL);
6978 vim_free(from);
6979 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980}
6981
6982/*
6983 * "indent()" function
6984 */
6985 static void
6986f_indent(typval_T *argvars, typval_T *rettv)
6987{
6988 linenr_T lnum;
6989
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006990 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006991 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6992 rettv->vval.v_number = get_indent_lnum(lnum);
6993 else
6994 rettv->vval.v_number = -1;
6995}
6996
6997/*
6998 * "index()" function
6999 */
7000 static void
7001f_index(typval_T *argvars, typval_T *rettv)
7002{
7003 list_T *l;
7004 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007005 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 long idx = 0;
7007 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007008 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009
7010 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007011 if (argvars[0].v_type == VAR_BLOB)
7012 {
7013 typval_T tv;
7014 int start = 0;
7015
7016 if (argvars[2].v_type != VAR_UNKNOWN)
7017 {
7018 start = tv_get_number_chk(&argvars[2], &error);
7019 if (error)
7020 return;
7021 }
7022 b = argvars[0].vval.v_blob;
7023 if (b == NULL)
7024 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007025 if (start < 0)
7026 {
7027 start = blob_len(b) + start;
7028 if (start < 0)
7029 start = 0;
7030 }
7031
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007032 for (idx = start; idx < blob_len(b); ++idx)
7033 {
7034 tv.v_type = VAR_NUMBER;
7035 tv.vval.v_number = blob_get(b, idx);
7036 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7037 {
7038 rettv->vval.v_number = idx;
7039 return;
7040 }
7041 }
7042 return;
7043 }
7044 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007046 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 return;
7048 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007049
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007050 l = argvars[0].vval.v_list;
7051 if (l != NULL)
7052 {
7053 item = l->lv_first;
7054 if (argvars[2].v_type != VAR_UNKNOWN)
7055 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007056 /* Start at specified item. Use the cached index that list_find()
7057 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007058 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059 idx = l->lv_idx;
7060 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007061 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 if (error)
7063 item = NULL;
7064 }
7065
7066 for ( ; item != NULL; item = item->li_next, ++idx)
7067 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7068 {
7069 rettv->vval.v_number = idx;
7070 break;
7071 }
7072 }
7073}
7074
7075static int inputsecret_flag = 0;
7076
7077/*
7078 * "input()" function
7079 * Also handles inputsecret() when inputsecret is set.
7080 */
7081 static void
7082f_input(typval_T *argvars, typval_T *rettv)
7083{
7084 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7085}
7086
7087/*
7088 * "inputdialog()" function
7089 */
7090 static void
7091f_inputdialog(typval_T *argvars, typval_T *rettv)
7092{
7093#if defined(FEAT_GUI_TEXTDIALOG)
7094 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7095 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7096 {
7097 char_u *message;
7098 char_u buf[NUMBUFLEN];
7099 char_u *defstr = (char_u *)"";
7100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007101 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007102 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007103 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7105 else
7106 IObuff[0] = NUL;
7107 if (message != NULL && defstr != NULL
7108 && do_dialog(VIM_QUESTION, NULL, message,
7109 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7110 rettv->vval.v_string = vim_strsave(IObuff);
7111 else
7112 {
7113 if (message != NULL && defstr != NULL
7114 && argvars[1].v_type != VAR_UNKNOWN
7115 && argvars[2].v_type != VAR_UNKNOWN)
7116 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007117 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 else
7119 rettv->vval.v_string = NULL;
7120 }
7121 rettv->v_type = VAR_STRING;
7122 }
7123 else
7124#endif
7125 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7126}
7127
7128/*
7129 * "inputlist()" function
7130 */
7131 static void
7132f_inputlist(typval_T *argvars, typval_T *rettv)
7133{
7134 listitem_T *li;
7135 int selected;
7136 int mouse_used;
7137
7138#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007139 /* While starting up, there is no place to enter text. When running tests
7140 * with --not-a-term we assume feedkeys() will be used. */
7141 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007142 return;
7143#endif
7144 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007146 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 return;
7148 }
7149
7150 msg_start();
7151 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7152 lines_left = Rows; /* avoid more prompt */
7153 msg_scroll = TRUE;
7154 msg_clr_eos();
7155
7156 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7157 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007158 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007159 msg_putchar('\n');
7160 }
7161
7162 /* Ask for choice. */
7163 selected = prompt_for_number(&mouse_used);
7164 if (mouse_used)
7165 selected -= lines_left;
7166
7167 rettv->vval.v_number = selected;
7168}
7169
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007170static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7171
7172/*
7173 * "inputrestore()" function
7174 */
7175 static void
7176f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7177{
7178 if (ga_userinput.ga_len > 0)
7179 {
7180 --ga_userinput.ga_len;
7181 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7182 + ga_userinput.ga_len);
7183 /* default return is zero == OK */
7184 }
7185 else if (p_verbose > 1)
7186 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007187 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007188 rettv->vval.v_number = 1; /* Failed */
7189 }
7190}
7191
7192/*
7193 * "inputsave()" function
7194 */
7195 static void
7196f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7197{
7198 /* Add an entry to the stack of typeahead storage. */
7199 if (ga_grow(&ga_userinput, 1) == OK)
7200 {
7201 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7202 + ga_userinput.ga_len);
7203 ++ga_userinput.ga_len;
7204 /* default return is zero == OK */
7205 }
7206 else
7207 rettv->vval.v_number = 1; /* Failed */
7208}
7209
7210/*
7211 * "inputsecret()" function
7212 */
7213 static void
7214f_inputsecret(typval_T *argvars, typval_T *rettv)
7215{
7216 ++cmdline_star;
7217 ++inputsecret_flag;
7218 f_input(argvars, rettv);
7219 --cmdline_star;
7220 --inputsecret_flag;
7221}
7222
7223/*
7224 * "insert()" function
7225 */
7226 static void
7227f_insert(typval_T *argvars, typval_T *rettv)
7228{
7229 long before = 0;
7230 listitem_T *item;
7231 list_T *l;
7232 int error = FALSE;
7233
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007234 if (argvars[0].v_type == VAR_BLOB)
7235 {
7236 int val, len;
7237 char_u *p;
7238
7239 len = blob_len(argvars[0].vval.v_blob);
7240 if (argvars[2].v_type != VAR_UNKNOWN)
7241 {
7242 before = (long)tv_get_number_chk(&argvars[2], &error);
7243 if (error)
7244 return; // type error; errmsg already given
7245 if (before < 0 || before > len)
7246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007247 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007248 return;
7249 }
7250 }
7251 val = tv_get_number_chk(&argvars[1], &error);
7252 if (error)
7253 return;
7254 if (val < 0 || val > 255)
7255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007256 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007257 return;
7258 }
7259
7260 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7261 return;
7262 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7263 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7264 *(p + before) = val;
7265 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7266
7267 copy_tv(&argvars[0], rettv);
7268 }
7269 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007270 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007271 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7272 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273 {
7274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007275 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007276 if (error)
7277 return; /* type error; errmsg already given */
7278
7279 if (before == l->lv_len)
7280 item = NULL;
7281 else
7282 {
7283 item = list_find(l, before);
7284 if (item == NULL)
7285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007286 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007287 l = NULL;
7288 }
7289 }
7290 if (l != NULL)
7291 {
7292 list_insert_tv(l, &argvars[1], item);
7293 copy_tv(&argvars[0], rettv);
7294 }
7295 }
7296}
7297
7298/*
7299 * "invert(expr)" function
7300 */
7301 static void
7302f_invert(typval_T *argvars, typval_T *rettv)
7303{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007304 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305}
7306
7307/*
7308 * "isdirectory()" function
7309 */
7310 static void
7311f_isdirectory(typval_T *argvars, typval_T *rettv)
7312{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007313 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007314}
7315
7316/*
7317 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7318 * or it refers to a List or Dictionary that is locked.
7319 */
7320 static int
7321tv_islocked(typval_T *tv)
7322{
7323 return (tv->v_lock & VAR_LOCKED)
7324 || (tv->v_type == VAR_LIST
7325 && tv->vval.v_list != NULL
7326 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7327 || (tv->v_type == VAR_DICT
7328 && tv->vval.v_dict != NULL
7329 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7330}
7331
7332/*
7333 * "islocked()" function
7334 */
7335 static void
7336f_islocked(typval_T *argvars, typval_T *rettv)
7337{
7338 lval_T lv;
7339 char_u *end;
7340 dictitem_T *di;
7341
7342 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007343 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007344 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 if (end != NULL && lv.ll_name != NULL)
7346 {
7347 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007348 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 else
7350 {
7351 if (lv.ll_tv == NULL)
7352 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007353 di = find_var(lv.ll_name, NULL, TRUE);
7354 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007355 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007356 /* Consider a variable locked when:
7357 * 1. the variable itself is locked
7358 * 2. the value of the variable is locked.
7359 * 3. the List or Dict value is locked.
7360 */
7361 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7362 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007363 }
7364 }
7365 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007366 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007367 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007368 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 else if (lv.ll_list != NULL)
7370 /* List item. */
7371 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7372 else
7373 /* Dictionary item. */
7374 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7375 }
7376 }
7377
7378 clear_lval(&lv);
7379}
7380
7381#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7382/*
7383 * "isnan()" function
7384 */
7385 static void
7386f_isnan(typval_T *argvars, typval_T *rettv)
7387{
7388 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7389 && isnan(argvars[0].vval.v_float);
7390}
7391#endif
7392
7393/*
7394 * "items(dict)" function
7395 */
7396 static void
7397f_items(typval_T *argvars, typval_T *rettv)
7398{
7399 dict_list(argvars, rettv, 2);
7400}
7401
7402#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7403/*
7404 * Get the job from the argument.
7405 * Returns NULL if the job is invalid.
7406 */
7407 static job_T *
7408get_job_arg(typval_T *tv)
7409{
7410 job_T *job;
7411
7412 if (tv->v_type != VAR_JOB)
7413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007415 return NULL;
7416 }
7417 job = tv->vval.v_job;
7418
7419 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007420 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007421 return job;
7422}
7423
7424/*
7425 * "job_getchannel()" function
7426 */
7427 static void
7428f_job_getchannel(typval_T *argvars, typval_T *rettv)
7429{
7430 job_T *job = get_job_arg(&argvars[0]);
7431
7432 if (job != NULL)
7433 {
7434 rettv->v_type = VAR_CHANNEL;
7435 rettv->vval.v_channel = job->jv_channel;
7436 if (job->jv_channel != NULL)
7437 ++job->jv_channel->ch_refcount;
7438 }
7439}
7440
7441/*
7442 * "job_info()" function
7443 */
7444 static void
7445f_job_info(typval_T *argvars, typval_T *rettv)
7446{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007447 if (argvars[0].v_type != VAR_UNKNOWN)
7448 {
7449 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007451 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7452 job_info(job, rettv->vval.v_dict);
7453 }
7454 else if (rettv_list_alloc(rettv) == OK)
7455 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007456}
7457
7458/*
7459 * "job_setoptions()" function
7460 */
7461 static void
7462f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7463{
7464 job_T *job = get_job_arg(&argvars[0]);
7465 jobopt_T opt;
7466
7467 if (job == NULL)
7468 return;
7469 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007470 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 job_set_options(job, &opt);
7472 free_job_options(&opt);
7473}
7474
7475/*
7476 * "job_start()" function
7477 */
7478 static void
7479f_job_start(typval_T *argvars, typval_T *rettv)
7480{
7481 rettv->v_type = VAR_JOB;
7482 if (check_restricted() || check_secure())
7483 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007484 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007485}
7486
7487/*
7488 * "job_status()" function
7489 */
7490 static void
7491f_job_status(typval_T *argvars, typval_T *rettv)
7492{
7493 job_T *job = get_job_arg(&argvars[0]);
7494
7495 if (job != NULL)
7496 {
7497 rettv->v_type = VAR_STRING;
7498 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7499 }
7500}
7501
7502/*
7503 * "job_stop()" function
7504 */
7505 static void
7506f_job_stop(typval_T *argvars, typval_T *rettv)
7507{
7508 job_T *job = get_job_arg(&argvars[0]);
7509
7510 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007511 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512}
7513#endif
7514
7515/*
7516 * "join()" function
7517 */
7518 static void
7519f_join(typval_T *argvars, typval_T *rettv)
7520{
7521 garray_T ga;
7522 char_u *sep;
7523
7524 if (argvars[0].v_type != VAR_LIST)
7525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527 return;
7528 }
7529 if (argvars[0].vval.v_list == NULL)
7530 return;
7531 if (argvars[1].v_type == VAR_UNKNOWN)
7532 sep = (char_u *)" ";
7533 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007534 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535
7536 rettv->v_type = VAR_STRING;
7537
7538 if (sep != NULL)
7539 {
7540 ga_init2(&ga, (int)sizeof(char), 80);
7541 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7542 ga_append(&ga, NUL);
7543 rettv->vval.v_string = (char_u *)ga.ga_data;
7544 }
7545 else
7546 rettv->vval.v_string = NULL;
7547}
7548
7549/*
7550 * "js_decode()" function
7551 */
7552 static void
7553f_js_decode(typval_T *argvars, typval_T *rettv)
7554{
7555 js_read_T reader;
7556
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007557 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 reader.js_fill = NULL;
7559 reader.js_used = 0;
7560 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007561 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562}
7563
7564/*
7565 * "js_encode()" function
7566 */
7567 static void
7568f_js_encode(typval_T *argvars, typval_T *rettv)
7569{
7570 rettv->v_type = VAR_STRING;
7571 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7572}
7573
7574/*
7575 * "json_decode()" function
7576 */
7577 static void
7578f_json_decode(typval_T *argvars, typval_T *rettv)
7579{
7580 js_read_T reader;
7581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007582 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007583 reader.js_fill = NULL;
7584 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007585 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007586}
7587
7588/*
7589 * "json_encode()" function
7590 */
7591 static void
7592f_json_encode(typval_T *argvars, typval_T *rettv)
7593{
7594 rettv->v_type = VAR_STRING;
7595 rettv->vval.v_string = json_encode(&argvars[0], 0);
7596}
7597
7598/*
7599 * "keys()" function
7600 */
7601 static void
7602f_keys(typval_T *argvars, typval_T *rettv)
7603{
7604 dict_list(argvars, rettv, 0);
7605}
7606
7607/*
7608 * "last_buffer_nr()" function.
7609 */
7610 static void
7611f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7612{
7613 int n = 0;
7614 buf_T *buf;
7615
Bram Moolenaar29323592016-07-24 22:04:11 +02007616 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617 if (n < buf->b_fnum)
7618 n = buf->b_fnum;
7619
7620 rettv->vval.v_number = n;
7621}
7622
7623/*
7624 * "len()" function
7625 */
7626 static void
7627f_len(typval_T *argvars, typval_T *rettv)
7628{
7629 switch (argvars[0].v_type)
7630 {
7631 case VAR_STRING:
7632 case VAR_NUMBER:
7633 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007634 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007636 case VAR_BLOB:
7637 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7638 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 case VAR_LIST:
7640 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7641 break;
7642 case VAR_DICT:
7643 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7644 break;
7645 case VAR_UNKNOWN:
7646 case VAR_SPECIAL:
7647 case VAR_FLOAT:
7648 case VAR_FUNC:
7649 case VAR_PARTIAL:
7650 case VAR_JOB:
7651 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 break;
7654 }
7655}
7656
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007657 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007658libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007659{
7660#ifdef FEAT_LIBCALL
7661 char_u *string_in;
7662 char_u **string_result;
7663 int nr_result;
7664#endif
7665
7666 rettv->v_type = type;
7667 if (type != VAR_NUMBER)
7668 rettv->vval.v_string = NULL;
7669
7670 if (check_restricted() || check_secure())
7671 return;
7672
7673#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007674 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7676 {
7677 string_in = NULL;
7678 if (argvars[2].v_type == VAR_STRING)
7679 string_in = argvars[2].vval.v_string;
7680 if (type == VAR_NUMBER)
7681 string_result = NULL;
7682 else
7683 string_result = &rettv->vval.v_string;
7684 if (mch_libcall(argvars[0].vval.v_string,
7685 argvars[1].vval.v_string,
7686 string_in,
7687 argvars[2].vval.v_number,
7688 string_result,
7689 &nr_result) == OK
7690 && type == VAR_NUMBER)
7691 rettv->vval.v_number = nr_result;
7692 }
7693#endif
7694}
7695
7696/*
7697 * "libcall()" function
7698 */
7699 static void
7700f_libcall(typval_T *argvars, typval_T *rettv)
7701{
7702 libcall_common(argvars, rettv, VAR_STRING);
7703}
7704
7705/*
7706 * "libcallnr()" function
7707 */
7708 static void
7709f_libcallnr(typval_T *argvars, typval_T *rettv)
7710{
7711 libcall_common(argvars, rettv, VAR_NUMBER);
7712}
7713
7714/*
7715 * "line(string)" function
7716 */
7717 static void
7718f_line(typval_T *argvars, typval_T *rettv)
7719{
7720 linenr_T lnum = 0;
7721 pos_T *fp;
7722 int fnum;
7723
7724 fp = var2fpos(&argvars[0], TRUE, &fnum);
7725 if (fp != NULL)
7726 lnum = fp->lnum;
7727 rettv->vval.v_number = lnum;
7728}
7729
7730/*
7731 * "line2byte(lnum)" function
7732 */
7733 static void
7734f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7735{
7736#ifndef FEAT_BYTEOFF
7737 rettv->vval.v_number = -1;
7738#else
7739 linenr_T lnum;
7740
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007741 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7743 rettv->vval.v_number = -1;
7744 else
7745 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7746 if (rettv->vval.v_number >= 0)
7747 ++rettv->vval.v_number;
7748#endif
7749}
7750
7751/*
7752 * "lispindent(lnum)" function
7753 */
7754 static void
7755f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7756{
7757#ifdef FEAT_LISP
7758 pos_T pos;
7759 linenr_T lnum;
7760
7761 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007762 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7764 {
7765 curwin->w_cursor.lnum = lnum;
7766 rettv->vval.v_number = get_lisp_indent();
7767 curwin->w_cursor = pos;
7768 }
7769 else
7770#endif
7771 rettv->vval.v_number = -1;
7772}
7773
7774/*
7775 * "localtime()" function
7776 */
7777 static void
7778f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7779{
7780 rettv->vval.v_number = (varnumber_T)time(NULL);
7781}
7782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007783 static void
7784get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7785{
7786 char_u *keys;
7787 char_u *which;
7788 char_u buf[NUMBUFLEN];
7789 char_u *keys_buf = NULL;
7790 char_u *rhs;
7791 int mode;
7792 int abbr = FALSE;
7793 int get_dict = FALSE;
7794 mapblock_T *mp;
7795 int buffer_local;
7796
7797 /* return empty string for failure */
7798 rettv->v_type = VAR_STRING;
7799 rettv->vval.v_string = NULL;
7800
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007801 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802 if (*keys == NUL)
7803 return;
7804
7805 if (argvars[1].v_type != VAR_UNKNOWN)
7806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007807 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007808 if (argvars[2].v_type != VAR_UNKNOWN)
7809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007810 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007811 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007812 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 }
7814 }
7815 else
7816 which = (char_u *)"";
7817 if (which == NULL)
7818 return;
7819
7820 mode = get_map_mode(&which, 0);
7821
7822 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7823 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7824 vim_free(keys_buf);
7825
7826 if (!get_dict)
7827 {
7828 /* Return a string. */
7829 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007830 {
7831 if (*rhs == NUL)
7832 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7833 else
7834 rettv->vval.v_string = str2special_save(rhs, FALSE);
7835 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007836
7837 }
7838 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7839 {
7840 /* Return a dictionary. */
7841 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7842 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7843 dict_T *dict = rettv->vval.v_dict;
7844
Bram Moolenaare0be1672018-07-08 16:50:37 +02007845 dict_add_string(dict, "lhs", lhs);
7846 dict_add_string(dict, "rhs", mp->m_orig_str);
7847 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7848 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7849 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007850 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7851 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007852 dict_add_number(dict, "buffer", (long)buffer_local);
7853 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7854 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855
7856 vim_free(lhs);
7857 vim_free(mapmode);
7858 }
7859}
7860
7861#ifdef FEAT_FLOAT
7862/*
7863 * "log()" function
7864 */
7865 static void
7866f_log(typval_T *argvars, typval_T *rettv)
7867{
7868 float_T f = 0.0;
7869
7870 rettv->v_type = VAR_FLOAT;
7871 if (get_float_arg(argvars, &f) == OK)
7872 rettv->vval.v_float = log(f);
7873 else
7874 rettv->vval.v_float = 0.0;
7875}
7876
7877/*
7878 * "log10()" function
7879 */
7880 static void
7881f_log10(typval_T *argvars, typval_T *rettv)
7882{
7883 float_T f = 0.0;
7884
7885 rettv->v_type = VAR_FLOAT;
7886 if (get_float_arg(argvars, &f) == OK)
7887 rettv->vval.v_float = log10(f);
7888 else
7889 rettv->vval.v_float = 0.0;
7890}
7891#endif
7892
7893#ifdef FEAT_LUA
7894/*
7895 * "luaeval()" function
7896 */
7897 static void
7898f_luaeval(typval_T *argvars, typval_T *rettv)
7899{
7900 char_u *str;
7901 char_u buf[NUMBUFLEN];
7902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007903 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 do_luaeval(str, argvars + 1, rettv);
7905}
7906#endif
7907
7908/*
7909 * "map()" function
7910 */
7911 static void
7912f_map(typval_T *argvars, typval_T *rettv)
7913{
7914 filter_map(argvars, rettv, TRUE);
7915}
7916
7917/*
7918 * "maparg()" function
7919 */
7920 static void
7921f_maparg(typval_T *argvars, typval_T *rettv)
7922{
7923 get_maparg(argvars, rettv, TRUE);
7924}
7925
7926/*
7927 * "mapcheck()" function
7928 */
7929 static void
7930f_mapcheck(typval_T *argvars, typval_T *rettv)
7931{
7932 get_maparg(argvars, rettv, FALSE);
7933}
7934
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007935typedef enum
7936{
7937 MATCH_END, /* matchend() */
7938 MATCH_MATCH, /* match() */
7939 MATCH_STR, /* matchstr() */
7940 MATCH_LIST, /* matchlist() */
7941 MATCH_POS /* matchstrpos() */
7942} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007943
7944 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007945find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007946{
7947 char_u *str = NULL;
7948 long len = 0;
7949 char_u *expr = NULL;
7950 char_u *pat;
7951 regmatch_T regmatch;
7952 char_u patbuf[NUMBUFLEN];
7953 char_u strbuf[NUMBUFLEN];
7954 char_u *save_cpo;
7955 long start = 0;
7956 long nth = 1;
7957 colnr_T startcol = 0;
7958 int match = 0;
7959 list_T *l = NULL;
7960 listitem_T *li = NULL;
7961 long idx = 0;
7962 char_u *tofree = NULL;
7963
7964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7965 save_cpo = p_cpo;
7966 p_cpo = (char_u *)"";
7967
7968 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007969 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007971 /* type MATCH_LIST: return empty list when there are no matches.
7972 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007973 if (rettv_list_alloc(rettv) == FAIL)
7974 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007975 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007976 && (list_append_string(rettv->vval.v_list,
7977 (char_u *)"", 0) == FAIL
7978 || list_append_number(rettv->vval.v_list,
7979 (varnumber_T)-1) == FAIL
7980 || list_append_number(rettv->vval.v_list,
7981 (varnumber_T)-1) == FAIL
7982 || list_append_number(rettv->vval.v_list,
7983 (varnumber_T)-1) == FAIL))
7984 {
7985 list_free(rettv->vval.v_list);
7986 rettv->vval.v_list = NULL;
7987 goto theend;
7988 }
7989 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007990 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 {
7992 rettv->v_type = VAR_STRING;
7993 rettv->vval.v_string = NULL;
7994 }
7995
7996 if (argvars[0].v_type == VAR_LIST)
7997 {
7998 if ((l = argvars[0].vval.v_list) == NULL)
7999 goto theend;
8000 li = l->lv_first;
8001 }
8002 else
8003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008004 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 len = (long)STRLEN(str);
8006 }
8007
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008008 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008009 if (pat == NULL)
8010 goto theend;
8011
8012 if (argvars[2].v_type != VAR_UNKNOWN)
8013 {
8014 int error = FALSE;
8015
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008016 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 if (error)
8018 goto theend;
8019 if (l != NULL)
8020 {
8021 li = list_find(l, start);
8022 if (li == NULL)
8023 goto theend;
8024 idx = l->lv_idx; /* use the cached index */
8025 }
8026 else
8027 {
8028 if (start < 0)
8029 start = 0;
8030 if (start > len)
8031 goto theend;
8032 /* When "count" argument is there ignore matches before "start",
8033 * otherwise skip part of the string. Differs when pattern is "^"
8034 * or "\<". */
8035 if (argvars[3].v_type != VAR_UNKNOWN)
8036 startcol = start;
8037 else
8038 {
8039 str += start;
8040 len -= start;
8041 }
8042 }
8043
8044 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008045 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008046 if (error)
8047 goto theend;
8048 }
8049
8050 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8051 if (regmatch.regprog != NULL)
8052 {
8053 regmatch.rm_ic = p_ic;
8054
8055 for (;;)
8056 {
8057 if (l != NULL)
8058 {
8059 if (li == NULL)
8060 {
8061 match = FALSE;
8062 break;
8063 }
8064 vim_free(tofree);
8065 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8066 if (str == NULL)
8067 break;
8068 }
8069
8070 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8071
8072 if (match && --nth <= 0)
8073 break;
8074 if (l == NULL && !match)
8075 break;
8076
8077 /* Advance to just after the match. */
8078 if (l != NULL)
8079 {
8080 li = li->li_next;
8081 ++idx;
8082 }
8083 else
8084 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 startcol = (colnr_T)(regmatch.startp[0]
8086 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008087 if (startcol > (colnr_T)len
8088 || str + startcol <= regmatch.startp[0])
8089 {
8090 match = FALSE;
8091 break;
8092 }
8093 }
8094 }
8095
8096 if (match)
8097 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008098 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099 {
8100 listitem_T *li1 = rettv->vval.v_list->lv_first;
8101 listitem_T *li2 = li1->li_next;
8102 listitem_T *li3 = li2->li_next;
8103 listitem_T *li4 = li3->li_next;
8104
8105 vim_free(li1->li_tv.vval.v_string);
8106 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8107 (int)(regmatch.endp[0] - regmatch.startp[0]));
8108 li3->li_tv.vval.v_number =
8109 (varnumber_T)(regmatch.startp[0] - expr);
8110 li4->li_tv.vval.v_number =
8111 (varnumber_T)(regmatch.endp[0] - expr);
8112 if (l != NULL)
8113 li2->li_tv.vval.v_number = (varnumber_T)idx;
8114 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008115 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 {
8117 int i;
8118
8119 /* return list with matched string and submatches */
8120 for (i = 0; i < NSUBEXP; ++i)
8121 {
8122 if (regmatch.endp[i] == NULL)
8123 {
8124 if (list_append_string(rettv->vval.v_list,
8125 (char_u *)"", 0) == FAIL)
8126 break;
8127 }
8128 else if (list_append_string(rettv->vval.v_list,
8129 regmatch.startp[i],
8130 (int)(regmatch.endp[i] - regmatch.startp[i]))
8131 == FAIL)
8132 break;
8133 }
8134 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008135 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008136 {
8137 /* return matched string */
8138 if (l != NULL)
8139 copy_tv(&li->li_tv, rettv);
8140 else
8141 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8142 (int)(regmatch.endp[0] - regmatch.startp[0]));
8143 }
8144 else if (l != NULL)
8145 rettv->vval.v_number = idx;
8146 else
8147 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008148 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149 rettv->vval.v_number =
8150 (varnumber_T)(regmatch.startp[0] - str);
8151 else
8152 rettv->vval.v_number =
8153 (varnumber_T)(regmatch.endp[0] - str);
8154 rettv->vval.v_number += (varnumber_T)(str - expr);
8155 }
8156 }
8157 vim_regfree(regmatch.regprog);
8158 }
8159
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008160theend:
8161 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 /* matchstrpos() without a list: drop the second item. */
8163 listitem_remove(rettv->vval.v_list,
8164 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 vim_free(tofree);
8166 p_cpo = save_cpo;
8167}
8168
8169/*
8170 * "match()" function
8171 */
8172 static void
8173f_match(typval_T *argvars, typval_T *rettv)
8174{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008175 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176}
8177
Bram Moolenaar95e51472018-07-28 16:55:56 +02008178#ifdef FEAT_SEARCH_EXTRA
8179 static int
8180matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8181{
8182 dictitem_T *di;
8183
8184 if (tv->v_type != VAR_DICT)
8185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008187 return FAIL;
8188 }
8189
8190 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008191 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008192 (char_u *)"conceal", FALSE);
8193
8194 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8195 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008196 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008197 if (*win == NULL)
8198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008199 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008200 return FAIL;
8201 }
8202 }
8203
8204 return OK;
8205}
8206#endif
8207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208/*
8209 * "matchadd()" function
8210 */
8211 static void
8212f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8213{
8214#ifdef FEAT_SEARCH_EXTRA
8215 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008216 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8217 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008218 int prio = 10; /* default priority */
8219 int id = -1;
8220 int error = FALSE;
8221 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008222 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008223
8224 rettv->vval.v_number = -1;
8225
8226 if (grp == NULL || pat == NULL)
8227 return;
8228 if (argvars[2].v_type != VAR_UNKNOWN)
8229 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008230 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008231 if (argvars[3].v_type != VAR_UNKNOWN)
8232 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008233 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008234 if (argvars[4].v_type != VAR_UNKNOWN
8235 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8236 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 }
8238 }
8239 if (error == TRUE)
8240 return;
8241 if (id >= 1 && id <= 3)
8242 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008243 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008244 return;
8245 }
8246
Bram Moolenaar95e51472018-07-28 16:55:56 +02008247 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008248 conceal_char);
8249#endif
8250}
8251
8252/*
8253 * "matchaddpos()" function
8254 */
8255 static void
8256f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8257{
8258#ifdef FEAT_SEARCH_EXTRA
8259 char_u buf[NUMBUFLEN];
8260 char_u *group;
8261 int prio = 10;
8262 int id = -1;
8263 int error = FALSE;
8264 list_T *l;
8265 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008266 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267
8268 rettv->vval.v_number = -1;
8269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008270 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008271 if (group == NULL)
8272 return;
8273
8274 if (argvars[1].v_type != VAR_LIST)
8275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008276 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008277 return;
8278 }
8279 l = argvars[1].vval.v_list;
8280 if (l == NULL)
8281 return;
8282
8283 if (argvars[2].v_type != VAR_UNKNOWN)
8284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008285 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 if (argvars[3].v_type != VAR_UNKNOWN)
8287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008288 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008289
8290 if (argvars[4].v_type != VAR_UNKNOWN
8291 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8292 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 }
8294 }
8295 if (error == TRUE)
8296 return;
8297
8298 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8299 if (id == 1 || id == 2)
8300 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008301 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008302 return;
8303 }
8304
Bram Moolenaar95e51472018-07-28 16:55:56 +02008305 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008306 conceal_char);
8307#endif
8308}
8309
8310/*
8311 * "matcharg()" function
8312 */
8313 static void
8314f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8315{
8316 if (rettv_list_alloc(rettv) == OK)
8317 {
8318#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008319 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008320 matchitem_T *m;
8321
8322 if (id >= 1 && id <= 3)
8323 {
8324 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8325 {
8326 list_append_string(rettv->vval.v_list,
8327 syn_id2name(m->hlg_id), -1);
8328 list_append_string(rettv->vval.v_list, m->pattern, -1);
8329 }
8330 else
8331 {
8332 list_append_string(rettv->vval.v_list, NULL, -1);
8333 list_append_string(rettv->vval.v_list, NULL, -1);
8334 }
8335 }
8336#endif
8337 }
8338}
8339
8340/*
8341 * "matchdelete()" function
8342 */
8343 static void
8344f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8345{
8346#ifdef FEAT_SEARCH_EXTRA
8347 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008348 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349#endif
8350}
8351
8352/*
8353 * "matchend()" function
8354 */
8355 static void
8356f_matchend(typval_T *argvars, typval_T *rettv)
8357{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008358 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008359}
8360
8361/*
8362 * "matchlist()" function
8363 */
8364 static void
8365f_matchlist(typval_T *argvars, typval_T *rettv)
8366{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008367 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368}
8369
8370/*
8371 * "matchstr()" function
8372 */
8373 static void
8374f_matchstr(typval_T *argvars, typval_T *rettv)
8375{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008376 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377}
8378
8379/*
8380 * "matchstrpos()" function
8381 */
8382 static void
8383f_matchstrpos(typval_T *argvars, typval_T *rettv)
8384{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008385 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008386}
8387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008388 static void
8389max_min(typval_T *argvars, typval_T *rettv, int domax)
8390{
8391 varnumber_T n = 0;
8392 varnumber_T i;
8393 int error = FALSE;
8394
8395 if (argvars[0].v_type == VAR_LIST)
8396 {
8397 list_T *l;
8398 listitem_T *li;
8399
8400 l = argvars[0].vval.v_list;
8401 if (l != NULL)
8402 {
8403 li = l->lv_first;
8404 if (li != NULL)
8405 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008406 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 for (;;)
8408 {
8409 li = li->li_next;
8410 if (li == NULL)
8411 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 if (domax ? i > n : i < n)
8414 n = i;
8415 }
8416 }
8417 }
8418 }
8419 else if (argvars[0].v_type == VAR_DICT)
8420 {
8421 dict_T *d;
8422 int first = TRUE;
8423 hashitem_T *hi;
8424 int todo;
8425
8426 d = argvars[0].vval.v_dict;
8427 if (d != NULL)
8428 {
8429 todo = (int)d->dv_hashtab.ht_used;
8430 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8431 {
8432 if (!HASHITEM_EMPTY(hi))
8433 {
8434 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008435 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008436 if (first)
8437 {
8438 n = i;
8439 first = FALSE;
8440 }
8441 else if (domax ? i > n : i < n)
8442 n = i;
8443 }
8444 }
8445 }
8446 }
8447 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008448 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008449 rettv->vval.v_number = error ? 0 : n;
8450}
8451
8452/*
8453 * "max()" function
8454 */
8455 static void
8456f_max(typval_T *argvars, typval_T *rettv)
8457{
8458 max_min(argvars, rettv, TRUE);
8459}
8460
8461/*
8462 * "min()" function
8463 */
8464 static void
8465f_min(typval_T *argvars, typval_T *rettv)
8466{
8467 max_min(argvars, rettv, FALSE);
8468}
8469
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008470/*
8471 * Create the directory in which "dir" is located, and higher levels when
8472 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008473 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008474 */
8475 static int
8476mkdir_recurse(char_u *dir, int prot)
8477{
8478 char_u *p;
8479 char_u *updir;
8480 int r = FAIL;
8481
8482 /* Get end of directory name in "dir".
8483 * We're done when it's "/" or "c:/". */
8484 p = gettail_sep(dir);
8485 if (p <= get_past_head(dir))
8486 return OK;
8487
8488 /* If the directory exists we're done. Otherwise: create it.*/
8489 updir = vim_strnsave(dir, (int)(p - dir));
8490 if (updir == NULL)
8491 return FAIL;
8492 if (mch_isdir(updir))
8493 r = OK;
8494 else if (mkdir_recurse(updir, prot) == OK)
8495 r = vim_mkdir_emsg(updir, prot);
8496 vim_free(updir);
8497 return r;
8498}
8499
8500#ifdef vim_mkdir
8501/*
8502 * "mkdir()" function
8503 */
8504 static void
8505f_mkdir(typval_T *argvars, typval_T *rettv)
8506{
8507 char_u *dir;
8508 char_u buf[NUMBUFLEN];
8509 int prot = 0755;
8510
8511 rettv->vval.v_number = FAIL;
8512 if (check_restricted() || check_secure())
8513 return;
8514
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008515 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008517 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008519 if (*gettail(dir) == NUL)
8520 /* remove trailing slashes */
8521 *gettail_sep(dir) = NUL;
8522
8523 if (argvars[1].v_type != VAR_UNKNOWN)
8524 {
8525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008526 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008527 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008528 if (prot == -1)
8529 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008531 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008532 {
8533 if (mch_isdir(dir))
8534 {
8535 /* With the "p" flag it's OK if the dir already exists. */
8536 rettv->vval.v_number = OK;
8537 return;
8538 }
8539 mkdir_recurse(dir, prot);
8540 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008542 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543}
8544#endif
8545
8546/*
8547 * "mode()" function
8548 */
8549 static void
8550f_mode(typval_T *argvars, typval_T *rettv)
8551{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008552 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553
Bram Moolenaar612cc382018-07-29 15:34:26 +02008554 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555
8556 if (time_for_testing == 93784)
8557 {
8558 /* Testing the two-character code. */
8559 buf[0] = 'x';
8560 buf[1] = '!';
8561 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008562#ifdef FEAT_TERMINAL
8563 else if (term_use_loop())
8564 buf[0] = 't';
8565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 else if (VIsual_active)
8567 {
8568 if (VIsual_select)
8569 buf[0] = VIsual_mode + 's' - 'v';
8570 else
8571 buf[0] = VIsual_mode;
8572 }
8573 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8574 || State == CONFIRM)
8575 {
8576 buf[0] = 'r';
8577 if (State == ASKMORE)
8578 buf[1] = 'm';
8579 else if (State == CONFIRM)
8580 buf[1] = '?';
8581 }
8582 else if (State == EXTERNCMD)
8583 buf[0] = '!';
8584 else if (State & INSERT)
8585 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008586 if (State & VREPLACE_FLAG)
8587 {
8588 buf[0] = 'R';
8589 buf[1] = 'v';
8590 }
8591 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008592 {
8593 if (State & REPLACE_FLAG)
8594 buf[0] = 'R';
8595 else
8596 buf[0] = 'i';
8597#ifdef FEAT_INS_EXPAND
8598 if (ins_compl_active())
8599 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008600 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008601 buf[1] = 'x';
8602#endif
8603 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008604 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008605 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008606 {
8607 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008608 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008610 else if (exmode_active == EXMODE_NORMAL)
8611 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 }
8613 else
8614 {
8615 buf[0] = 'n';
8616 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008617 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008619 // to be able to detect force-linewise/blockwise/characterwise operations
8620 buf[2] = motion_force;
8621 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008622 else if (restart_edit == 'I' || restart_edit == 'R'
8623 || restart_edit == 'V')
8624 {
8625 buf[1] = 'i';
8626 buf[2] = restart_edit;
8627 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 }
8629
8630 /* Clear out the minor mode when the argument is not a non-zero number or
8631 * non-empty string. */
8632 if (!non_zero_arg(&argvars[0]))
8633 buf[1] = NUL;
8634
8635 rettv->vval.v_string = vim_strsave(buf);
8636 rettv->v_type = VAR_STRING;
8637}
8638
8639#if defined(FEAT_MZSCHEME) || defined(PROTO)
8640/*
8641 * "mzeval()" function
8642 */
8643 static void
8644f_mzeval(typval_T *argvars, typval_T *rettv)
8645{
8646 char_u *str;
8647 char_u buf[NUMBUFLEN];
8648
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008649 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 do_mzeval(str, rettv);
8651}
8652
8653 void
8654mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8655{
8656 typval_T argvars[3];
8657
8658 argvars[0].v_type = VAR_STRING;
8659 argvars[0].vval.v_string = name;
8660 copy_tv(args, &argvars[1]);
8661 argvars[2].v_type = VAR_UNKNOWN;
8662 f_call(argvars, rettv);
8663 clear_tv(&argvars[1]);
8664}
8665#endif
8666
8667/*
8668 * "nextnonblank()" function
8669 */
8670 static void
8671f_nextnonblank(typval_T *argvars, typval_T *rettv)
8672{
8673 linenr_T lnum;
8674
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008675 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008676 {
8677 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8678 {
8679 lnum = 0;
8680 break;
8681 }
8682 if (*skipwhite(ml_get(lnum)) != NUL)
8683 break;
8684 }
8685 rettv->vval.v_number = lnum;
8686}
8687
8688/*
8689 * "nr2char()" function
8690 */
8691 static void
8692f_nr2char(typval_T *argvars, typval_T *rettv)
8693{
8694 char_u buf[NUMBUFLEN];
8695
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 if (has_mbyte)
8697 {
8698 int utf8 = 0;
8699
8700 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008701 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008703 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008704 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008705 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 }
8707 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008709 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 buf[1] = NUL;
8711 }
8712 rettv->v_type = VAR_STRING;
8713 rettv->vval.v_string = vim_strsave(buf);
8714}
8715
8716/*
8717 * "or(expr, expr)" function
8718 */
8719 static void
8720f_or(typval_T *argvars, typval_T *rettv)
8721{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008722 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8723 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724}
8725
8726/*
8727 * "pathshorten()" function
8728 */
8729 static void
8730f_pathshorten(typval_T *argvars, typval_T *rettv)
8731{
8732 char_u *p;
8733
8734 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008735 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 if (p == NULL)
8737 rettv->vval.v_string = NULL;
8738 else
8739 {
8740 p = vim_strsave(p);
8741 rettv->vval.v_string = p;
8742 if (p != NULL)
8743 shorten_dir(p);
8744 }
8745}
8746
8747#ifdef FEAT_PERL
8748/*
8749 * "perleval()" function
8750 */
8751 static void
8752f_perleval(typval_T *argvars, typval_T *rettv)
8753{
8754 char_u *str;
8755 char_u buf[NUMBUFLEN];
8756
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 do_perleval(str, rettv);
8759}
8760#endif
8761
8762#ifdef FEAT_FLOAT
8763/*
8764 * "pow()" function
8765 */
8766 static void
8767f_pow(typval_T *argvars, typval_T *rettv)
8768{
8769 float_T fx = 0.0, fy = 0.0;
8770
8771 rettv->v_type = VAR_FLOAT;
8772 if (get_float_arg(argvars, &fx) == OK
8773 && get_float_arg(&argvars[1], &fy) == OK)
8774 rettv->vval.v_float = pow(fx, fy);
8775 else
8776 rettv->vval.v_float = 0.0;
8777}
8778#endif
8779
8780/*
8781 * "prevnonblank()" function
8782 */
8783 static void
8784f_prevnonblank(typval_T *argvars, typval_T *rettv)
8785{
8786 linenr_T lnum;
8787
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008788 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8790 lnum = 0;
8791 else
8792 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8793 --lnum;
8794 rettv->vval.v_number = lnum;
8795}
8796
8797/* This dummy va_list is here because:
8798 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8799 * - locally in the function results in a "used before set" warning
8800 * - using va_start() to initialize it gives "function with fixed args" error */
8801static va_list ap;
8802
8803/*
8804 * "printf()" function
8805 */
8806 static void
8807f_printf(typval_T *argvars, typval_T *rettv)
8808{
8809 char_u buf[NUMBUFLEN];
8810 int len;
8811 char_u *s;
8812 int saved_did_emsg = did_emsg;
8813 char *fmt;
8814
8815 rettv->v_type = VAR_STRING;
8816 rettv->vval.v_string = NULL;
8817
8818 /* Get the required length, allocate the buffer and do it for real. */
8819 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008820 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008821 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 if (!did_emsg)
8823 {
8824 s = alloc(len + 1);
8825 if (s != NULL)
8826 {
8827 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008828 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8829 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008830 }
8831 }
8832 did_emsg |= saved_did_emsg;
8833}
8834
Bram Moolenaarf2732452018-06-03 14:47:35 +02008835#ifdef FEAT_JOB_CHANNEL
8836/*
8837 * "prompt_setcallback({buffer}, {callback})" function
8838 */
8839 static void
8840f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8841{
8842 buf_T *buf;
8843 char_u *callback;
8844 partial_T *partial;
8845
8846 if (check_secure())
8847 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008848 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008849 if (buf == NULL)
8850 return;
8851
8852 callback = get_callback(&argvars[1], &partial);
8853 if (callback == NULL)
8854 return;
8855
8856 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8857 if (partial == NULL)
8858 buf->b_prompt_callback = vim_strsave(callback);
8859 else
8860 /* pointer into the partial */
8861 buf->b_prompt_callback = callback;
8862 buf->b_prompt_partial = partial;
8863}
8864
8865/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008866 * "prompt_setinterrupt({buffer}, {callback})" function
8867 */
8868 static void
8869f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8870{
8871 buf_T *buf;
8872 char_u *callback;
8873 partial_T *partial;
8874
8875 if (check_secure())
8876 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008877 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008878 if (buf == NULL)
8879 return;
8880
8881 callback = get_callback(&argvars[1], &partial);
8882 if (callback == NULL)
8883 return;
8884
8885 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8886 if (partial == NULL)
8887 buf->b_prompt_interrupt = vim_strsave(callback);
8888 else
8889 /* pointer into the partial */
8890 buf->b_prompt_interrupt = callback;
8891 buf->b_prompt_int_partial = partial;
8892}
8893
8894/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008895 * "prompt_setprompt({buffer}, {text})" function
8896 */
8897 static void
8898f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8899{
8900 buf_T *buf;
8901 char_u *text;
8902
8903 if (check_secure())
8904 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008905 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008906 if (buf == NULL)
8907 return;
8908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008909 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008910 vim_free(buf->b_prompt_text);
8911 buf->b_prompt_text = vim_strsave(text);
8912}
8913#endif
8914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915/*
8916 * "pumvisible()" function
8917 */
8918 static void
8919f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8920{
8921#ifdef FEAT_INS_EXPAND
8922 if (pum_visible())
8923 rettv->vval.v_number = 1;
8924#endif
8925}
8926
8927#ifdef FEAT_PYTHON3
8928/*
8929 * "py3eval()" function
8930 */
8931 static void
8932f_py3eval(typval_T *argvars, typval_T *rettv)
8933{
8934 char_u *str;
8935 char_u buf[NUMBUFLEN];
8936
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008937 if (p_pyx == 0)
8938 p_pyx = 3;
8939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008940 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941 do_py3eval(str, rettv);
8942}
8943#endif
8944
8945#ifdef FEAT_PYTHON
8946/*
8947 * "pyeval()" function
8948 */
8949 static void
8950f_pyeval(typval_T *argvars, typval_T *rettv)
8951{
8952 char_u *str;
8953 char_u buf[NUMBUFLEN];
8954
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008955 if (p_pyx == 0)
8956 p_pyx = 2;
8957
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008958 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959 do_pyeval(str, rettv);
8960}
8961#endif
8962
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008963#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8964/*
8965 * "pyxeval()" function
8966 */
8967 static void
8968f_pyxeval(typval_T *argvars, typval_T *rettv)
8969{
8970# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8971 init_pyxversion();
8972 if (p_pyx == 2)
8973 f_pyeval(argvars, rettv);
8974 else
8975 f_py3eval(argvars, rettv);
8976# elif defined(FEAT_PYTHON)
8977 f_pyeval(argvars, rettv);
8978# elif defined(FEAT_PYTHON3)
8979 f_py3eval(argvars, rettv);
8980# endif
8981}
8982#endif
8983
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984/*
8985 * "range()" function
8986 */
8987 static void
8988f_range(typval_T *argvars, typval_T *rettv)
8989{
8990 varnumber_T start;
8991 varnumber_T end;
8992 varnumber_T stride = 1;
8993 varnumber_T i;
8994 int error = FALSE;
8995
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008996 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 if (argvars[1].v_type == VAR_UNKNOWN)
8998 {
8999 end = start - 1;
9000 start = 0;
9001 }
9002 else
9003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009004 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009006 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 }
9008
9009 if (error)
9010 return; /* type error; errmsg already given */
9011 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009012 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009014 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 else
9016 {
9017 if (rettv_list_alloc(rettv) == OK)
9018 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9019 if (list_append_number(rettv->vval.v_list,
9020 (varnumber_T)i) == FAIL)
9021 break;
9022 }
9023}
9024
9025/*
9026 * "readfile()" function
9027 */
9028 static void
9029f_readfile(typval_T *argvars, typval_T *rettv)
9030{
9031 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009032 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 int failed = FALSE;
9034 char_u *fname;
9035 FILE *fd;
9036 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9037 int io_size = sizeof(buf);
9038 int readlen; /* size of last fread() */
9039 char_u *prev = NULL; /* previously read bytes, if any */
9040 long prevlen = 0; /* length of data in prev */
9041 long prevsize = 0; /* size of prev buffer */
9042 long maxline = MAXLNUM;
9043 long cnt = 0;
9044 char_u *p; /* position in buf */
9045 char_u *start; /* start of current line */
9046
9047 if (argvars[1].v_type != VAR_UNKNOWN)
9048 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009049 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009051 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9052 blob = TRUE;
9053
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009055 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 }
9057
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009058 if (blob)
9059 {
9060 if (rettv_blob_alloc(rettv) == FAIL)
9061 return;
9062 }
9063 else
9064 {
9065 if (rettv_list_alloc(rettv) == FAIL)
9066 return;
9067 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068
9069 /* Always open the file in binary mode, library functions have a mind of
9070 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009071 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009072 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009074 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 return;
9076 }
9077
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009078 if (blob)
9079 {
9080 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009082 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009083 blob_free(rettv->vval.v_blob);
9084 }
9085 fclose(fd);
9086 return;
9087 }
9088
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 while (cnt < maxline || maxline < 0)
9090 {
9091 readlen = (int)fread(buf, 1, io_size, fd);
9092
9093 /* This for loop processes what was read, but is also entered at end
9094 * of file so that either:
9095 * - an incomplete line gets written
9096 * - a "binary" file gets an empty line at the end if it ends in a
9097 * newline. */
9098 for (p = buf, start = buf;
9099 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9100 ++p)
9101 {
9102 if (*p == '\n' || readlen <= 0)
9103 {
9104 listitem_T *li;
9105 char_u *s = NULL;
9106 long_u len = p - start;
9107
9108 /* Finished a line. Remove CRs before NL. */
9109 if (readlen > 0 && !binary)
9110 {
9111 while (len > 0 && start[len - 1] == '\r')
9112 --len;
9113 /* removal may cross back to the "prev" string */
9114 if (len == 0)
9115 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9116 --prevlen;
9117 }
9118 if (prevlen == 0)
9119 s = vim_strnsave(start, (int)len);
9120 else
9121 {
9122 /* Change "prev" buffer to be the right size. This way
9123 * the bytes are only copied once, and very long lines are
9124 * allocated only once. */
9125 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9126 {
9127 mch_memmove(s + prevlen, start, len);
9128 s[prevlen + len] = NUL;
9129 prev = NULL; /* the list will own the string */
9130 prevlen = prevsize = 0;
9131 }
9132 }
9133 if (s == NULL)
9134 {
9135 do_outofmem_msg((long_u) prevlen + len + 1);
9136 failed = TRUE;
9137 break;
9138 }
9139
9140 if ((li = listitem_alloc()) == NULL)
9141 {
9142 vim_free(s);
9143 failed = TRUE;
9144 break;
9145 }
9146 li->li_tv.v_type = VAR_STRING;
9147 li->li_tv.v_lock = 0;
9148 li->li_tv.vval.v_string = s;
9149 list_append(rettv->vval.v_list, li);
9150
9151 start = p + 1; /* step over newline */
9152 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9153 break;
9154 }
9155 else if (*p == NUL)
9156 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009157 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9158 * when finding the BF and check the previous two bytes. */
9159 else if (*p == 0xbf && enc_utf8 && !binary)
9160 {
9161 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9162 * + 1, these may be in the "prev" string. */
9163 char_u back1 = p >= buf + 1 ? p[-1]
9164 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9165 char_u back2 = p >= buf + 2 ? p[-2]
9166 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9167 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9168
9169 if (back2 == 0xef && back1 == 0xbb)
9170 {
9171 char_u *dest = p - 2;
9172
9173 /* Usually a BOM is at the beginning of a file, and so at
9174 * the beginning of a line; then we can just step over it.
9175 */
9176 if (start == dest)
9177 start = p + 1;
9178 else
9179 {
9180 /* have to shuffle buf to close gap */
9181 int adjust_prevlen = 0;
9182
9183 if (dest < buf)
9184 {
9185 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9186 dest = buf;
9187 }
9188 if (readlen > p - buf + 1)
9189 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9190 readlen -= 3 - adjust_prevlen;
9191 prevlen -= adjust_prevlen;
9192 p = dest - 1;
9193 }
9194 }
9195 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 } /* for */
9197
9198 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9199 break;
9200 if (start < p)
9201 {
9202 /* There's part of a line in buf, store it in "prev". */
9203 if (p - start + prevlen >= prevsize)
9204 {
9205 /* need bigger "prev" buffer */
9206 char_u *newprev;
9207
9208 /* A common use case is ordinary text files and "prev" gets a
9209 * fragment of a line, so the first allocation is made
9210 * small, to avoid repeatedly 'allocing' large and
9211 * 'reallocing' small. */
9212 if (prevsize == 0)
9213 prevsize = (long)(p - start);
9214 else
9215 {
9216 long grow50pc = (prevsize * 3) / 2;
9217 long growmin = (long)((p - start) * 2 + prevlen);
9218 prevsize = grow50pc > growmin ? grow50pc : growmin;
9219 }
9220 newprev = prev == NULL ? alloc(prevsize)
9221 : vim_realloc(prev, prevsize);
9222 if (newprev == NULL)
9223 {
9224 do_outofmem_msg((long_u)prevsize);
9225 failed = TRUE;
9226 break;
9227 }
9228 prev = newprev;
9229 }
9230 /* Add the line part to end of "prev". */
9231 mch_memmove(prev + prevlen, start, p - start);
9232 prevlen += (long)(p - start);
9233 }
9234 } /* while */
9235
9236 /*
9237 * For a negative line count use only the lines at the end of the file,
9238 * free the rest.
9239 */
9240 if (!failed && maxline < 0)
9241 while (cnt > -maxline)
9242 {
9243 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9244 --cnt;
9245 }
9246
9247 if (failed)
9248 {
9249 list_free(rettv->vval.v_list);
9250 /* readfile doc says an empty list is returned on error */
9251 rettv->vval.v_list = list_alloc();
9252 }
9253
9254 vim_free(prev);
9255 fclose(fd);
9256}
9257
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009258 static void
9259return_register(int regname, typval_T *rettv)
9260{
9261 char_u buf[2] = {0, 0};
9262
9263 buf[0] = (char_u)regname;
9264 rettv->v_type = VAR_STRING;
9265 rettv->vval.v_string = vim_strsave(buf);
9266}
9267
9268/*
9269 * "reg_executing()" function
9270 */
9271 static void
9272f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9273{
9274 return_register(reg_executing, rettv);
9275}
9276
9277/*
9278 * "reg_recording()" function
9279 */
9280 static void
9281f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9282{
9283 return_register(reg_recording, rettv);
9284}
9285
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009286#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287/*
9288 * Convert a List to proftime_T.
9289 * Return FAIL when there is something wrong.
9290 */
9291 static int
9292list2proftime(typval_T *arg, proftime_T *tm)
9293{
9294 long n1, n2;
9295 int error = FALSE;
9296
9297 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9298 || arg->vval.v_list->lv_len != 2)
9299 return FAIL;
9300 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9301 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9302# ifdef WIN3264
9303 tm->HighPart = n1;
9304 tm->LowPart = n2;
9305# else
9306 tm->tv_sec = n1;
9307 tm->tv_usec = n2;
9308# endif
9309 return error ? FAIL : OK;
9310}
9311#endif /* FEAT_RELTIME */
9312
9313/*
9314 * "reltime()" function
9315 */
9316 static void
9317f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9318{
9319#ifdef FEAT_RELTIME
9320 proftime_T res;
9321 proftime_T start;
9322
9323 if (argvars[0].v_type == VAR_UNKNOWN)
9324 {
9325 /* No arguments: get current time. */
9326 profile_start(&res);
9327 }
9328 else if (argvars[1].v_type == VAR_UNKNOWN)
9329 {
9330 if (list2proftime(&argvars[0], &res) == FAIL)
9331 return;
9332 profile_end(&res);
9333 }
9334 else
9335 {
9336 /* Two arguments: compute the difference. */
9337 if (list2proftime(&argvars[0], &start) == FAIL
9338 || list2proftime(&argvars[1], &res) == FAIL)
9339 return;
9340 profile_sub(&res, &start);
9341 }
9342
9343 if (rettv_list_alloc(rettv) == OK)
9344 {
9345 long n1, n2;
9346
9347# ifdef WIN3264
9348 n1 = res.HighPart;
9349 n2 = res.LowPart;
9350# else
9351 n1 = res.tv_sec;
9352 n2 = res.tv_usec;
9353# endif
9354 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9355 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9356 }
9357#endif
9358}
9359
9360#ifdef FEAT_FLOAT
9361/*
9362 * "reltimefloat()" function
9363 */
9364 static void
9365f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9366{
9367# ifdef FEAT_RELTIME
9368 proftime_T tm;
9369# endif
9370
9371 rettv->v_type = VAR_FLOAT;
9372 rettv->vval.v_float = 0;
9373# ifdef FEAT_RELTIME
9374 if (list2proftime(&argvars[0], &tm) == OK)
9375 rettv->vval.v_float = profile_float(&tm);
9376# endif
9377}
9378#endif
9379
9380/*
9381 * "reltimestr()" function
9382 */
9383 static void
9384f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9385{
9386#ifdef FEAT_RELTIME
9387 proftime_T tm;
9388#endif
9389
9390 rettv->v_type = VAR_STRING;
9391 rettv->vval.v_string = NULL;
9392#ifdef FEAT_RELTIME
9393 if (list2proftime(&argvars[0], &tm) == OK)
9394 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9395#endif
9396}
9397
9398#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009399 static void
9400make_connection(void)
9401{
9402 if (X_DISPLAY == NULL
9403# ifdef FEAT_GUI
9404 && !gui.in_use
9405# endif
9406 )
9407 {
9408 x_force_connect = TRUE;
9409 setup_term_clip();
9410 x_force_connect = FALSE;
9411 }
9412}
9413
9414 static int
9415check_connection(void)
9416{
9417 make_connection();
9418 if (X_DISPLAY == NULL)
9419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009420 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 return FAIL;
9422 }
9423 return OK;
9424}
9425#endif
9426
9427#ifdef FEAT_CLIENTSERVER
9428 static void
9429remote_common(typval_T *argvars, typval_T *rettv, int expr)
9430{
9431 char_u *server_name;
9432 char_u *keys;
9433 char_u *r = NULL;
9434 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009435 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009436# ifdef WIN32
9437 HWND w;
9438# else
9439 Window w;
9440# endif
9441
9442 if (check_restricted() || check_secure())
9443 return;
9444
9445# ifdef FEAT_X11
9446 if (check_connection() == FAIL)
9447 return;
9448# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009449 if (argvars[2].v_type != VAR_UNKNOWN
9450 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009451 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009453 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454 if (server_name == NULL)
9455 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009456 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009457# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009458 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009460 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9461 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009462# endif
9463 {
9464 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009466 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009467 vim_free(r);
9468 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009470 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009471 return;
9472 }
9473
9474 rettv->vval.v_string = r;
9475
9476 if (argvars[2].v_type != VAR_UNKNOWN)
9477 {
9478 dictitem_T v;
9479 char_u str[30];
9480 char_u *idvar;
9481
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009483 if (idvar != NULL && *idvar != NUL)
9484 {
9485 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9486 v.di_tv.v_type = VAR_STRING;
9487 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009489 vim_free(v.di_tv.vval.v_string);
9490 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009491 }
9492}
9493#endif
9494
9495/*
9496 * "remote_expr()" function
9497 */
9498 static void
9499f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9500{
9501 rettv->v_type = VAR_STRING;
9502 rettv->vval.v_string = NULL;
9503#ifdef FEAT_CLIENTSERVER
9504 remote_common(argvars, rettv, TRUE);
9505#endif
9506}
9507
9508/*
9509 * "remote_foreground()" function
9510 */
9511 static void
9512f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9513{
9514#ifdef FEAT_CLIENTSERVER
9515# ifdef WIN32
9516 /* On Win32 it's done in this application. */
9517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519
9520 if (server_name != NULL)
9521 serverForeground(server_name);
9522 }
9523# else
9524 /* Send a foreground() expression to the server. */
9525 argvars[1].v_type = VAR_STRING;
9526 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9527 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009528 rettv->v_type = VAR_STRING;
9529 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530 remote_common(argvars, rettv, TRUE);
9531 vim_free(argvars[1].vval.v_string);
9532# endif
9533#endif
9534}
9535
9536 static void
9537f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9538{
9539#ifdef FEAT_CLIENTSERVER
9540 dictitem_T v;
9541 char_u *s = NULL;
9542# ifdef WIN32
9543 long_u n = 0;
9544# endif
9545 char_u *serverid;
9546
9547 if (check_restricted() || check_secure())
9548 {
9549 rettv->vval.v_number = -1;
9550 return;
9551 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009552 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 if (serverid == NULL)
9554 {
9555 rettv->vval.v_number = -1;
9556 return; /* type error; errmsg already given */
9557 }
9558# ifdef WIN32
9559 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9560 if (n == 0)
9561 rettv->vval.v_number = -1;
9562 else
9563 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009564 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009565 rettv->vval.v_number = (s != NULL);
9566 }
9567# else
9568 if (check_connection() == FAIL)
9569 return;
9570
9571 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9572 serverStrToWin(serverid), &s);
9573# endif
9574
9575 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9576 {
9577 char_u *retvar;
9578
9579 v.di_tv.v_type = VAR_STRING;
9580 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009581 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009582 if (retvar != NULL)
9583 set_var(retvar, &v.di_tv, FALSE);
9584 vim_free(v.di_tv.vval.v_string);
9585 }
9586#else
9587 rettv->vval.v_number = -1;
9588#endif
9589}
9590
9591 static void
9592f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9593{
9594 char_u *r = NULL;
9595
9596#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009597 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009598
9599 if (serverid != NULL && !check_restricted() && !check_secure())
9600 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009601 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009602# ifdef WIN32
9603 /* The server's HWND is encoded in the 'id' parameter */
9604 long_u n = 0;
9605# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009606
9607 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009608 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009610# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009611 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9612 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009613 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009614 if (r == NULL)
9615# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009616 if (check_connection() == FAIL
9617 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9618 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009620 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621 }
9622#endif
9623 rettv->v_type = VAR_STRING;
9624 rettv->vval.v_string = r;
9625}
9626
9627/*
9628 * "remote_send()" function
9629 */
9630 static void
9631f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9632{
9633 rettv->v_type = VAR_STRING;
9634 rettv->vval.v_string = NULL;
9635#ifdef FEAT_CLIENTSERVER
9636 remote_common(argvars, rettv, FALSE);
9637#endif
9638}
9639
9640/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009641 * "remote_startserver()" function
9642 */
9643 static void
9644f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9645{
9646#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009647 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009648
9649 if (server == NULL)
9650 return; /* type error; errmsg already given */
9651 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009652 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009653 else
9654 {
9655# ifdef FEAT_X11
9656 if (check_connection() == OK)
9657 serverRegisterName(X_DISPLAY, server);
9658# else
9659 serverSetName(server);
9660# endif
9661 }
9662#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009663 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009664#endif
9665}
9666
9667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009668 * "remove()" function
9669 */
9670 static void
9671f_remove(typval_T *argvars, typval_T *rettv)
9672{
9673 list_T *l;
9674 listitem_T *item, *item2;
9675 listitem_T *li;
9676 long idx;
9677 long end;
9678 char_u *key;
9679 dict_T *d;
9680 dictitem_T *di;
9681 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009682 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683
9684 if (argvars[0].v_type == VAR_DICT)
9685 {
9686 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009687 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009688 else if ((d = argvars[0].vval.v_dict) != NULL
9689 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009691 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009692 if (key != NULL)
9693 {
9694 di = dict_find(d, key, -1);
9695 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009696 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009697 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9698 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9699 {
9700 *rettv = di->di_tv;
9701 init_tv(&di->di_tv);
9702 dictitem_remove(d, di);
9703 }
9704 }
9705 }
9706 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009707 else if (argvars[0].v_type == VAR_BLOB)
9708 {
9709 idx = (long)tv_get_number_chk(&argvars[1], &error);
9710 if (!error)
9711 {
9712 blob_T *b = argvars[0].vval.v_blob;
9713 int len = blob_len(b);
9714 char_u *p;
9715
9716 if (idx < 0)
9717 // count from the end
9718 idx = len + idx;
9719 if (idx < 0 || idx >= len)
9720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009721 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009722 return;
9723 }
9724 if (argvars[2].v_type == VAR_UNKNOWN)
9725 {
9726 // Remove one item, return its value.
9727 p = (char_u *)b->bv_ga.ga_data;
9728 rettv->vval.v_number = (varnumber_T) *(p + idx);
9729 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9730 --b->bv_ga.ga_len;
9731 }
9732 else
9733 {
9734 blob_T *blob;
9735
9736 // Remove range of items, return list with values.
9737 end = (long)tv_get_number_chk(&argvars[2], &error);
9738 if (error)
9739 return;
9740 if (end < 0)
9741 // count from the end
9742 end = len + end;
9743 if (end >= len || idx > end)
9744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009745 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009746 return;
9747 }
9748 blob = blob_alloc();
9749 if (blob == NULL)
9750 return;
9751 blob->bv_ga.ga_len = end - idx + 1;
9752 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9753 {
9754 vim_free(blob);
9755 return;
9756 }
9757 p = (char_u *)b->bv_ga.ga_data;
9758 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9759 (size_t)(end - idx + 1));
9760 ++blob->bv_refcount;
9761 rettv->v_type = VAR_BLOB;
9762 rettv->vval.v_blob = blob;
9763
9764 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9765 b->bv_ga.ga_len -= end - idx + 1;
9766 }
9767 }
9768 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009770 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009772 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009774 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009776 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009778 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009779 else
9780 {
9781 if (argvars[2].v_type == VAR_UNKNOWN)
9782 {
9783 /* Remove one item, return its value. */
9784 vimlist_remove(l, item, item);
9785 *rettv = item->li_tv;
9786 vim_free(item);
9787 }
9788 else
9789 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009790 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009791 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009793 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009795 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 else
9797 {
9798 int cnt = 0;
9799
9800 for (li = item; li != NULL; li = li->li_next)
9801 {
9802 ++cnt;
9803 if (li == item2)
9804 break;
9805 }
9806 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009807 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009808 else
9809 {
9810 vimlist_remove(l, item, item2);
9811 if (rettv_list_alloc(rettv) == OK)
9812 {
9813 l = rettv->vval.v_list;
9814 l->lv_first = item;
9815 l->lv_last = item2;
9816 item->li_prev = NULL;
9817 item2->li_next = NULL;
9818 l->lv_len = cnt;
9819 }
9820 }
9821 }
9822 }
9823 }
9824 }
9825}
9826
9827/*
9828 * "rename({from}, {to})" function
9829 */
9830 static void
9831f_rename(typval_T *argvars, typval_T *rettv)
9832{
9833 char_u buf[NUMBUFLEN];
9834
9835 if (check_restricted() || check_secure())
9836 rettv->vval.v_number = -1;
9837 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009838 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9839 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009840}
9841
9842/*
9843 * "repeat()" function
9844 */
9845 static void
9846f_repeat(typval_T *argvars, typval_T *rettv)
9847{
9848 char_u *p;
9849 int n;
9850 int slen;
9851 int len;
9852 char_u *r;
9853 int i;
9854
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009855 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856 if (argvars[0].v_type == VAR_LIST)
9857 {
9858 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9859 while (n-- > 0)
9860 if (list_extend(rettv->vval.v_list,
9861 argvars[0].vval.v_list, NULL) == FAIL)
9862 break;
9863 }
9864 else
9865 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009866 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 rettv->v_type = VAR_STRING;
9868 rettv->vval.v_string = NULL;
9869
9870 slen = (int)STRLEN(p);
9871 len = slen * n;
9872 if (len <= 0)
9873 return;
9874
9875 r = alloc(len + 1);
9876 if (r != NULL)
9877 {
9878 for (i = 0; i < n; i++)
9879 mch_memmove(r + i * slen, p, (size_t)slen);
9880 r[len] = NUL;
9881 }
9882
9883 rettv->vval.v_string = r;
9884 }
9885}
9886
9887/*
9888 * "resolve()" function
9889 */
9890 static void
9891f_resolve(typval_T *argvars, typval_T *rettv)
9892{
9893 char_u *p;
9894#ifdef HAVE_READLINK
9895 char_u *buf = NULL;
9896#endif
9897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009898 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899#ifdef FEAT_SHORTCUT
9900 {
9901 char_u *v = NULL;
9902
9903 v = mch_resolve_shortcut(p);
9904 if (v != NULL)
9905 rettv->vval.v_string = v;
9906 else
9907 rettv->vval.v_string = vim_strsave(p);
9908 }
9909#else
9910# ifdef HAVE_READLINK
9911 {
9912 char_u *cpy;
9913 int len;
9914 char_u *remain = NULL;
9915 char_u *q;
9916 int is_relative_to_current = FALSE;
9917 int has_trailing_pathsep = FALSE;
9918 int limit = 100;
9919
9920 p = vim_strsave(p);
9921
9922 if (p[0] == '.' && (vim_ispathsep(p[1])
9923 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9924 is_relative_to_current = TRUE;
9925
9926 len = STRLEN(p);
9927 if (len > 0 && after_pathsep(p, p + len))
9928 {
9929 has_trailing_pathsep = TRUE;
9930 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9931 }
9932
9933 q = getnextcomp(p);
9934 if (*q != NUL)
9935 {
9936 /* Separate the first path component in "p", and keep the
9937 * remainder (beginning with the path separator). */
9938 remain = vim_strsave(q - 1);
9939 q[-1] = NUL;
9940 }
9941
9942 buf = alloc(MAXPATHL + 1);
9943 if (buf == NULL)
9944 goto fail;
9945
9946 for (;;)
9947 {
9948 for (;;)
9949 {
9950 len = readlink((char *)p, (char *)buf, MAXPATHL);
9951 if (len <= 0)
9952 break;
9953 buf[len] = NUL;
9954
9955 if (limit-- == 0)
9956 {
9957 vim_free(p);
9958 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009959 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 rettv->vval.v_string = NULL;
9961 goto fail;
9962 }
9963
9964 /* Ensure that the result will have a trailing path separator
9965 * if the argument has one. */
9966 if (remain == NULL && has_trailing_pathsep)
9967 add_pathsep(buf);
9968
9969 /* Separate the first path component in the link value and
9970 * concatenate the remainders. */
9971 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9972 if (*q != NUL)
9973 {
9974 if (remain == NULL)
9975 remain = vim_strsave(q - 1);
9976 else
9977 {
9978 cpy = concat_str(q - 1, remain);
9979 if (cpy != NULL)
9980 {
9981 vim_free(remain);
9982 remain = cpy;
9983 }
9984 }
9985 q[-1] = NUL;
9986 }
9987
9988 q = gettail(p);
9989 if (q > p && *q == NUL)
9990 {
9991 /* Ignore trailing path separator. */
9992 q[-1] = NUL;
9993 q = gettail(p);
9994 }
9995 if (q > p && !mch_isFullName(buf))
9996 {
9997 /* symlink is relative to directory of argument */
9998 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9999 if (cpy != NULL)
10000 {
10001 STRCPY(cpy, p);
10002 STRCPY(gettail(cpy), buf);
10003 vim_free(p);
10004 p = cpy;
10005 }
10006 }
10007 else
10008 {
10009 vim_free(p);
10010 p = vim_strsave(buf);
10011 }
10012 }
10013
10014 if (remain == NULL)
10015 break;
10016
10017 /* Append the first path component of "remain" to "p". */
10018 q = getnextcomp(remain + 1);
10019 len = q - remain - (*q != NUL);
10020 cpy = vim_strnsave(p, STRLEN(p) + len);
10021 if (cpy != NULL)
10022 {
10023 STRNCAT(cpy, remain, len);
10024 vim_free(p);
10025 p = cpy;
10026 }
10027 /* Shorten "remain". */
10028 if (*q != NUL)
10029 STRMOVE(remain, q - 1);
10030 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010031 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 }
10033
10034 /* If the result is a relative path name, make it explicitly relative to
10035 * the current directory if and only if the argument had this form. */
10036 if (!vim_ispathsep(*p))
10037 {
10038 if (is_relative_to_current
10039 && *p != NUL
10040 && !(p[0] == '.'
10041 && (p[1] == NUL
10042 || vim_ispathsep(p[1])
10043 || (p[1] == '.'
10044 && (p[2] == NUL
10045 || vim_ispathsep(p[2]))))))
10046 {
10047 /* Prepend "./". */
10048 cpy = concat_str((char_u *)"./", p);
10049 if (cpy != NULL)
10050 {
10051 vim_free(p);
10052 p = cpy;
10053 }
10054 }
10055 else if (!is_relative_to_current)
10056 {
10057 /* Strip leading "./". */
10058 q = p;
10059 while (q[0] == '.' && vim_ispathsep(q[1]))
10060 q += 2;
10061 if (q > p)
10062 STRMOVE(p, p + 2);
10063 }
10064 }
10065
10066 /* Ensure that the result will have no trailing path separator
10067 * if the argument had none. But keep "/" or "//". */
10068 if (!has_trailing_pathsep)
10069 {
10070 q = p + STRLEN(p);
10071 if (after_pathsep(p, q))
10072 *gettail_sep(p) = NUL;
10073 }
10074
10075 rettv->vval.v_string = p;
10076 }
10077# else
10078 rettv->vval.v_string = vim_strsave(p);
10079# endif
10080#endif
10081
10082 simplify_filename(rettv->vval.v_string);
10083
10084#ifdef HAVE_READLINK
10085fail:
10086 vim_free(buf);
10087#endif
10088 rettv->v_type = VAR_STRING;
10089}
10090
10091/*
10092 * "reverse({list})" function
10093 */
10094 static void
10095f_reverse(typval_T *argvars, typval_T *rettv)
10096{
10097 list_T *l;
10098 listitem_T *li, *ni;
10099
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010100 if (argvars[0].v_type == VAR_BLOB)
10101 {
10102 blob_T *b = argvars[0].vval.v_blob;
10103 int i, len = blob_len(b);
10104
10105 for (i = 0; i < len / 2; i++)
10106 {
10107 int tmp = blob_get(b, i);
10108
10109 blob_set(b, i, blob_get(b, len - i - 1));
10110 blob_set(b, len - i - 1, tmp);
10111 }
10112 rettv_blob_set(rettv, b);
10113 return;
10114 }
10115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010116 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010117 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118 else if ((l = argvars[0].vval.v_list) != NULL
10119 && !tv_check_lock(l->lv_lock,
10120 (char_u *)N_("reverse() argument"), TRUE))
10121 {
10122 li = l->lv_last;
10123 l->lv_first = l->lv_last = NULL;
10124 l->lv_len = 0;
10125 while (li != NULL)
10126 {
10127 ni = li->li_prev;
10128 list_append(l, li);
10129 li = ni;
10130 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010131 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010132 l->lv_idx = l->lv_len - l->lv_idx - 1;
10133 }
10134}
10135
10136#define SP_NOMOVE 0x01 /* don't move cursor */
10137#define SP_REPEAT 0x02 /* repeat to find outer pair */
10138#define SP_RETCOUNT 0x04 /* return matchcount */
10139#define SP_SETPCMARK 0x08 /* set previous context mark */
10140#define SP_START 0x10 /* accept match at start position */
10141#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10142#define SP_END 0x40 /* leave cursor at end of match */
10143#define SP_COLUMN 0x80 /* start at cursor column */
10144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145/*
10146 * Get flags for a search function.
10147 * Possibly sets "p_ws".
10148 * Returns BACKWARD, FORWARD or zero (for an error).
10149 */
10150 static int
10151get_search_arg(typval_T *varp, int *flagsp)
10152{
10153 int dir = FORWARD;
10154 char_u *flags;
10155 char_u nbuf[NUMBUFLEN];
10156 int mask;
10157
10158 if (varp->v_type != VAR_UNKNOWN)
10159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010160 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 if (flags == NULL)
10162 return 0; /* type error; errmsg already given */
10163 while (*flags != NUL)
10164 {
10165 switch (*flags)
10166 {
10167 case 'b': dir = BACKWARD; break;
10168 case 'w': p_ws = TRUE; break;
10169 case 'W': p_ws = FALSE; break;
10170 default: mask = 0;
10171 if (flagsp != NULL)
10172 switch (*flags)
10173 {
10174 case 'c': mask = SP_START; break;
10175 case 'e': mask = SP_END; break;
10176 case 'm': mask = SP_RETCOUNT; break;
10177 case 'n': mask = SP_NOMOVE; break;
10178 case 'p': mask = SP_SUBPAT; break;
10179 case 'r': mask = SP_REPEAT; break;
10180 case 's': mask = SP_SETPCMARK; break;
10181 case 'z': mask = SP_COLUMN; break;
10182 }
10183 if (mask == 0)
10184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010185 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010186 dir = 0;
10187 }
10188 else
10189 *flagsp |= mask;
10190 }
10191 if (dir == 0)
10192 break;
10193 ++flags;
10194 }
10195 }
10196 return dir;
10197}
10198
10199/*
10200 * Shared by search() and searchpos() functions.
10201 */
10202 static int
10203search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10204{
10205 int flags;
10206 char_u *pat;
10207 pos_T pos;
10208 pos_T save_cursor;
10209 int save_p_ws = p_ws;
10210 int dir;
10211 int retval = 0; /* default: FAIL */
10212 long lnum_stop = 0;
10213 proftime_T tm;
10214#ifdef FEAT_RELTIME
10215 long time_limit = 0;
10216#endif
10217 int options = SEARCH_KEEP;
10218 int subpatnum;
10219
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010220 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010221 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10222 if (dir == 0)
10223 goto theend;
10224 flags = *flagsp;
10225 if (flags & SP_START)
10226 options |= SEARCH_START;
10227 if (flags & SP_END)
10228 options |= SEARCH_END;
10229 if (flags & SP_COLUMN)
10230 options |= SEARCH_COL;
10231
10232 /* Optional arguments: line number to stop searching and timeout. */
10233 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010235 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236 if (lnum_stop < 0)
10237 goto theend;
10238#ifdef FEAT_RELTIME
10239 if (argvars[3].v_type != VAR_UNKNOWN)
10240 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010241 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010242 if (time_limit < 0)
10243 goto theend;
10244 }
10245#endif
10246 }
10247
10248#ifdef FEAT_RELTIME
10249 /* Set the time limit, if there is one. */
10250 profile_setlimit(time_limit, &tm);
10251#endif
10252
10253 /*
10254 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10255 * Check to make sure only those flags are set.
10256 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10257 * flags cannot be set. Check for that condition also.
10258 */
10259 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10260 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010262 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 goto theend;
10264 }
10265
10266 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010267 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010268 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269 if (subpatnum != FAIL)
10270 {
10271 if (flags & SP_SUBPAT)
10272 retval = subpatnum;
10273 else
10274 retval = pos.lnum;
10275 if (flags & SP_SETPCMARK)
10276 setpcmark();
10277 curwin->w_cursor = pos;
10278 if (match_pos != NULL)
10279 {
10280 /* Store the match cursor position */
10281 match_pos->lnum = pos.lnum;
10282 match_pos->col = pos.col + 1;
10283 }
10284 /* "/$" will put the cursor after the end of the line, may need to
10285 * correct that here */
10286 check_cursor();
10287 }
10288
10289 /* If 'n' flag is used: restore cursor position. */
10290 if (flags & SP_NOMOVE)
10291 curwin->w_cursor = save_cursor;
10292 else
10293 curwin->w_set_curswant = TRUE;
10294theend:
10295 p_ws = save_p_ws;
10296
10297 return retval;
10298}
10299
10300#ifdef FEAT_FLOAT
10301
10302/*
10303 * round() is not in C90, use ceil() or floor() instead.
10304 */
10305 float_T
10306vim_round(float_T f)
10307{
10308 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10309}
10310
10311/*
10312 * "round({float})" function
10313 */
10314 static void
10315f_round(typval_T *argvars, typval_T *rettv)
10316{
10317 float_T f = 0.0;
10318
10319 rettv->v_type = VAR_FLOAT;
10320 if (get_float_arg(argvars, &f) == OK)
10321 rettv->vval.v_float = vim_round(f);
10322 else
10323 rettv->vval.v_float = 0.0;
10324}
10325#endif
10326
10327/*
10328 * "screenattr()" function
10329 */
10330 static void
10331f_screenattr(typval_T *argvars, typval_T *rettv)
10332{
10333 int row;
10334 int col;
10335 int c;
10336
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010337 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10338 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 if (row < 0 || row >= screen_Rows
10340 || col < 0 || col >= screen_Columns)
10341 c = -1;
10342 else
10343 c = ScreenAttrs[LineOffset[row] + col];
10344 rettv->vval.v_number = c;
10345}
10346
10347/*
10348 * "screenchar()" function
10349 */
10350 static void
10351f_screenchar(typval_T *argvars, typval_T *rettv)
10352{
10353 int row;
10354 int col;
10355 int off;
10356 int c;
10357
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010358 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10359 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360 if (row < 0 || row >= screen_Rows
10361 || col < 0 || col >= screen_Columns)
10362 c = -1;
10363 else
10364 {
10365 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 if (enc_utf8 && ScreenLinesUC[off] != 0)
10367 c = ScreenLinesUC[off];
10368 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010369 c = ScreenLines[off];
10370 }
10371 rettv->vval.v_number = c;
10372}
10373
10374/*
10375 * "screencol()" function
10376 *
10377 * First column is 1 to be consistent with virtcol().
10378 */
10379 static void
10380f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10381{
10382 rettv->vval.v_number = screen_screencol() + 1;
10383}
10384
10385/*
10386 * "screenrow()" function
10387 */
10388 static void
10389f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10390{
10391 rettv->vval.v_number = screen_screenrow() + 1;
10392}
10393
10394/*
10395 * "search()" function
10396 */
10397 static void
10398f_search(typval_T *argvars, typval_T *rettv)
10399{
10400 int flags = 0;
10401
10402 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10403}
10404
10405/*
10406 * "searchdecl()" function
10407 */
10408 static void
10409f_searchdecl(typval_T *argvars, typval_T *rettv)
10410{
10411 int locally = 1;
10412 int thisblock = 0;
10413 int error = FALSE;
10414 char_u *name;
10415
10416 rettv->vval.v_number = 1; /* default: FAIL */
10417
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010418 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010419 if (argvars[1].v_type != VAR_UNKNOWN)
10420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010421 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010422 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010423 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010424 }
10425 if (!error && name != NULL)
10426 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10427 locally, thisblock, SEARCH_KEEP) == FAIL;
10428}
10429
10430/*
10431 * Used by searchpair() and searchpairpos()
10432 */
10433 static int
10434searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10435{
10436 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010437 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010438 int save_p_ws = p_ws;
10439 int dir;
10440 int flags = 0;
10441 char_u nbuf1[NUMBUFLEN];
10442 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010443 int retval = 0; /* default: FAIL */
10444 long lnum_stop = 0;
10445 long time_limit = 0;
10446
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010447 /* Get the three pattern arguments: start, middle, end. Will result in an
10448 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010449 spat = tv_get_string_chk(&argvars[0]);
10450 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10451 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010452 if (spat == NULL || mpat == NULL || epat == NULL)
10453 goto theend; /* type error */
10454
10455 /* Handle the optional fourth argument: flags */
10456 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10457 if (dir == 0)
10458 goto theend;
10459
10460 /* Don't accept SP_END or SP_SUBPAT.
10461 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10462 */
10463 if ((flags & (SP_END | SP_SUBPAT)) != 0
10464 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010466 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010467 goto theend;
10468 }
10469
10470 /* Using 'r' implies 'W', otherwise it doesn't work. */
10471 if (flags & SP_REPEAT)
10472 p_ws = FALSE;
10473
10474 /* Optional fifth argument: skip expression */
10475 if (argvars[3].v_type == VAR_UNKNOWN
10476 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010477 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010478 else
10479 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010480 skip = &argvars[4];
10481 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10482 && skip->v_type != VAR_STRING)
10483 {
10484 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010485 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010486 goto theend;
10487 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010488 if (argvars[5].v_type != VAR_UNKNOWN)
10489 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010490 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010491 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010493 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
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#ifdef FEAT_RELTIME
10497 if (argvars[6].v_type != VAR_UNKNOWN)
10498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010499 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010502 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010503 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010504 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010505 }
10506#endif
10507 }
10508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010509
10510 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10511 match_pos, lnum_stop, time_limit);
10512
10513theend:
10514 p_ws = save_p_ws;
10515
10516 return retval;
10517}
10518
10519/*
10520 * "searchpair()" function
10521 */
10522 static void
10523f_searchpair(typval_T *argvars, typval_T *rettv)
10524{
10525 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10526}
10527
10528/*
10529 * "searchpairpos()" function
10530 */
10531 static void
10532f_searchpairpos(typval_T *argvars, typval_T *rettv)
10533{
10534 pos_T match_pos;
10535 int lnum = 0;
10536 int col = 0;
10537
10538 if (rettv_list_alloc(rettv) == FAIL)
10539 return;
10540
10541 if (searchpair_cmn(argvars, &match_pos) > 0)
10542 {
10543 lnum = match_pos.lnum;
10544 col = match_pos.col;
10545 }
10546
10547 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10548 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10549}
10550
10551/*
10552 * Search for a start/middle/end thing.
10553 * Used by searchpair(), see its documentation for the details.
10554 * Returns 0 or -1 for no match,
10555 */
10556 long
10557do_searchpair(
10558 char_u *spat, /* start pattern */
10559 char_u *mpat, /* middle pattern */
10560 char_u *epat, /* end pattern */
10561 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010562 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010563 int flags, /* SP_SETPCMARK and other SP_ values */
10564 pos_T *match_pos,
10565 linenr_T lnum_stop, /* stop at this line if not zero */
10566 long time_limit UNUSED) /* stop after this many msec */
10567{
10568 char_u *save_cpo;
10569 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10570 long retval = 0;
10571 pos_T pos;
10572 pos_T firstpos;
10573 pos_T foundpos;
10574 pos_T save_cursor;
10575 pos_T save_pos;
10576 int n;
10577 int r;
10578 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010579 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 int err;
10581 int options = SEARCH_KEEP;
10582 proftime_T tm;
10583
10584 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10585 save_cpo = p_cpo;
10586 p_cpo = empty_option;
10587
10588#ifdef FEAT_RELTIME
10589 /* Set the time limit, if there is one. */
10590 profile_setlimit(time_limit, &tm);
10591#endif
10592
10593 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10594 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010595 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10596 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010597 if (pat2 == NULL || pat3 == NULL)
10598 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010599 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 if (*mpat == NUL)
10601 STRCPY(pat3, pat2);
10602 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010603 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 spat, epat, mpat);
10605 if (flags & SP_START)
10606 options |= SEARCH_START;
10607
Bram Moolenaar48570482017-10-30 21:48:41 +010010608 if (skip != NULL)
10609 {
10610 /* Empty string means to not use the skip expression. */
10611 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10612 use_skip = skip->vval.v_string != NULL
10613 && *skip->vval.v_string != NUL;
10614 }
10615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010616 save_cursor = curwin->w_cursor;
10617 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010618 CLEAR_POS(&firstpos);
10619 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010620 pat = pat3;
10621 for (;;)
10622 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010623 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010624 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010625 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010626 /* didn't find it or found the first match again: FAIL */
10627 break;
10628
10629 if (firstpos.lnum == 0)
10630 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010631 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632 {
10633 /* Found the same position again. Can happen with a pattern that
10634 * has "\zs" at the end and searching backwards. Advance one
10635 * character and try again. */
10636 if (dir == BACKWARD)
10637 decl(&pos);
10638 else
10639 incl(&pos);
10640 }
10641 foundpos = pos;
10642
10643 /* clear the start flag to avoid getting stuck here */
10644 options &= ~SEARCH_START;
10645
10646 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010647 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648 {
10649 save_pos = curwin->w_cursor;
10650 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010651 err = FALSE;
10652 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010653 curwin->w_cursor = save_pos;
10654 if (err)
10655 {
10656 /* Evaluating {skip} caused an error, break here. */
10657 curwin->w_cursor = save_cursor;
10658 retval = -1;
10659 break;
10660 }
10661 if (r)
10662 continue;
10663 }
10664
10665 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10666 {
10667 /* Found end when searching backwards or start when searching
10668 * forward: nested pair. */
10669 ++nest;
10670 pat = pat2; /* nested, don't search for middle */
10671 }
10672 else
10673 {
10674 /* Found end when searching forward or start when searching
10675 * backward: end of (nested) pair; or found middle in outer pair. */
10676 if (--nest == 1)
10677 pat = pat3; /* outer level, search for middle */
10678 }
10679
10680 if (nest == 0)
10681 {
10682 /* Found the match: return matchcount or line number. */
10683 if (flags & SP_RETCOUNT)
10684 ++retval;
10685 else
10686 retval = pos.lnum;
10687 if (flags & SP_SETPCMARK)
10688 setpcmark();
10689 curwin->w_cursor = pos;
10690 if (!(flags & SP_REPEAT))
10691 break;
10692 nest = 1; /* search for next unmatched */
10693 }
10694 }
10695
10696 if (match_pos != NULL)
10697 {
10698 /* Store the match cursor position */
10699 match_pos->lnum = curwin->w_cursor.lnum;
10700 match_pos->col = curwin->w_cursor.col + 1;
10701 }
10702
10703 /* If 'n' flag is used or search failed: restore cursor position. */
10704 if ((flags & SP_NOMOVE) || retval == 0)
10705 curwin->w_cursor = save_cursor;
10706
10707theend:
10708 vim_free(pat2);
10709 vim_free(pat3);
10710 if (p_cpo == empty_option)
10711 p_cpo = save_cpo;
10712 else
10713 /* Darn, evaluating the {skip} expression changed the value. */
10714 free_string_option(save_cpo);
10715
10716 return retval;
10717}
10718
10719/*
10720 * "searchpos()" function
10721 */
10722 static void
10723f_searchpos(typval_T *argvars, typval_T *rettv)
10724{
10725 pos_T match_pos;
10726 int lnum = 0;
10727 int col = 0;
10728 int n;
10729 int flags = 0;
10730
10731 if (rettv_list_alloc(rettv) == FAIL)
10732 return;
10733
10734 n = search_cmn(argvars, &match_pos, &flags);
10735 if (n > 0)
10736 {
10737 lnum = match_pos.lnum;
10738 col = match_pos.col;
10739 }
10740
10741 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10742 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10743 if (flags & SP_SUBPAT)
10744 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10745}
10746
10747 static void
10748f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10749{
10750#ifdef FEAT_CLIENTSERVER
10751 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010752 char_u *server = tv_get_string_chk(&argvars[0]);
10753 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010754
10755 rettv->vval.v_number = -1;
10756 if (server == NULL || reply == NULL)
10757 return;
10758 if (check_restricted() || check_secure())
10759 return;
10760# ifdef FEAT_X11
10761 if (check_connection() == FAIL)
10762 return;
10763# endif
10764
10765 if (serverSendReply(server, reply) < 0)
10766 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010767 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010768 return;
10769 }
10770 rettv->vval.v_number = 0;
10771#else
10772 rettv->vval.v_number = -1;
10773#endif
10774}
10775
10776 static void
10777f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10778{
10779 char_u *r = NULL;
10780
10781#ifdef FEAT_CLIENTSERVER
10782# ifdef WIN32
10783 r = serverGetVimNames();
10784# else
10785 make_connection();
10786 if (X_DISPLAY != NULL)
10787 r = serverGetVimNames(X_DISPLAY);
10788# endif
10789#endif
10790 rettv->v_type = VAR_STRING;
10791 rettv->vval.v_string = r;
10792}
10793
10794/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010795 * "setbufline()" function
10796 */
10797 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010798f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010799{
10800 linenr_T lnum;
10801 buf_T *buf;
10802
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010803 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010804 if (buf == NULL)
10805 rettv->vval.v_number = 1; /* FAIL */
10806 else
10807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010808 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010809 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010810 }
10811}
10812
10813/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814 * "setbufvar()" function
10815 */
10816 static void
10817f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10818{
10819 buf_T *buf;
10820 char_u *varname, *bufvarname;
10821 typval_T *varp;
10822 char_u nbuf[NUMBUFLEN];
10823
10824 if (check_restricted() || check_secure())
10825 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010826 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10827 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010828 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010829 varp = &argvars[2];
10830
10831 if (buf != NULL && varname != NULL && varp != NULL)
10832 {
10833 if (*varname == '&')
10834 {
10835 long numval;
10836 char_u *strval;
10837 int error = FALSE;
10838 aco_save_T aco;
10839
10840 /* set curbuf to be our buf, temporarily */
10841 aucmd_prepbuf(&aco, buf);
10842
10843 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010844 numval = (long)tv_get_number_chk(varp, &error);
10845 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010846 if (!error && strval != NULL)
10847 set_option_value(varname, numval, strval, OPT_LOCAL);
10848
10849 /* reset notion of buffer */
10850 aucmd_restbuf(&aco);
10851 }
10852 else
10853 {
10854 buf_T *save_curbuf = curbuf;
10855
10856 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10857 if (bufvarname != NULL)
10858 {
10859 curbuf = buf;
10860 STRCPY(bufvarname, "b:");
10861 STRCPY(bufvarname + 2, varname);
10862 set_var(bufvarname, varp, TRUE);
10863 vim_free(bufvarname);
10864 curbuf = save_curbuf;
10865 }
10866 }
10867 }
10868}
10869
10870 static void
10871f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10872{
10873 dict_T *d;
10874 dictitem_T *di;
10875 char_u *csearch;
10876
10877 if (argvars[0].v_type != VAR_DICT)
10878 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010879 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010880 return;
10881 }
10882
10883 if ((d = argvars[0].vval.v_dict) != NULL)
10884 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010885 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010886 if (csearch != NULL)
10887 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010888 if (enc_utf8)
10889 {
10890 int pcc[MAX_MCO];
10891 int c = utfc_ptr2char(csearch, pcc);
10892
10893 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10894 }
10895 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 set_last_csearch(PTR2CHAR(csearch),
10897 csearch, MB_PTR2LEN(csearch));
10898 }
10899
10900 di = dict_find(d, (char_u *)"forward", -1);
10901 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010902 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 ? FORWARD : BACKWARD);
10904
10905 di = dict_find(d, (char_u *)"until", -1);
10906 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010907 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010908 }
10909}
10910
10911/*
10912 * "setcmdpos()" function
10913 */
10914 static void
10915f_setcmdpos(typval_T *argvars, typval_T *rettv)
10916{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010917 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010918
10919 if (pos >= 0)
10920 rettv->vval.v_number = set_cmdline_pos(pos);
10921}
10922
10923/*
10924 * "setfperm({fname}, {mode})" function
10925 */
10926 static void
10927f_setfperm(typval_T *argvars, typval_T *rettv)
10928{
10929 char_u *fname;
10930 char_u modebuf[NUMBUFLEN];
10931 char_u *mode_str;
10932 int i;
10933 int mask;
10934 int mode = 0;
10935
10936 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010937 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010938 if (fname == NULL)
10939 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010940 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941 if (mode_str == NULL)
10942 return;
10943 if (STRLEN(mode_str) != 9)
10944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010945 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010946 return;
10947 }
10948
10949 mask = 1;
10950 for (i = 8; i >= 0; --i)
10951 {
10952 if (mode_str[i] != '-')
10953 mode |= mask;
10954 mask = mask << 1;
10955 }
10956 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10957}
10958
10959/*
10960 * "setline()" function
10961 */
10962 static void
10963f_setline(typval_T *argvars, typval_T *rettv)
10964{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010965 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010966
Bram Moolenaarca851592018-06-06 21:04:07 +020010967 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968}
10969
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970/*
10971 * Used by "setqflist()" and "setloclist()" functions
10972 */
10973 static void
10974set_qf_ll_list(
10975 win_T *wp UNUSED,
10976 typval_T *list_arg UNUSED,
10977 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020010978 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010979 typval_T *rettv)
10980{
10981#ifdef FEAT_QUICKFIX
10982 static char *e_invact = N_("E927: Invalid action: '%s'");
10983 char_u *act;
10984 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010985 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986#endif
10987
10988 rettv->vval.v_number = -1;
10989
10990#ifdef FEAT_QUICKFIX
10991 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010992 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020010993 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010994 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 else
10996 {
10997 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020010998 dict_T *d = NULL;
10999 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011000
11001 if (action_arg->v_type == VAR_STRING)
11002 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011003 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 if (act == NULL)
11005 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011006 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11007 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 action = *act;
11009 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011010 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011011 }
11012 else if (action_arg->v_type == VAR_UNKNOWN)
11013 action = ' ';
11014 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011015 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011016
Bram Moolenaard823fa92016-08-12 16:29:27 +020011017 if (action_arg->v_type != VAR_UNKNOWN
11018 && what_arg->v_type != VAR_UNKNOWN)
11019 {
11020 if (what_arg->v_type == VAR_DICT)
11021 d = what_arg->vval.v_dict;
11022 else
11023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011024 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011025 valid_dict = FALSE;
11026 }
11027 }
11028
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011029 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011030 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011031 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11032 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011034 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 }
11036#endif
11037}
11038
11039/*
11040 * "setloclist()" function
11041 */
11042 static void
11043f_setloclist(typval_T *argvars, typval_T *rettv)
11044{
11045 win_T *win;
11046
11047 rettv->vval.v_number = -1;
11048
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011049 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011051 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052}
11053
11054/*
11055 * "setmatches()" function
11056 */
11057 static void
11058f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11059{
11060#ifdef FEAT_SEARCH_EXTRA
11061 list_T *l;
11062 listitem_T *li;
11063 dict_T *d;
11064 list_T *s = NULL;
11065
11066 rettv->vval.v_number = -1;
11067 if (argvars[0].v_type != VAR_LIST)
11068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011069 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011070 return;
11071 }
11072 if ((l = argvars[0].vval.v_list) != NULL)
11073 {
11074
11075 /* To some extent make sure that we are dealing with a list from
11076 * "getmatches()". */
11077 li = l->lv_first;
11078 while (li != NULL)
11079 {
11080 if (li->li_tv.v_type != VAR_DICT
11081 || (d = li->li_tv.vval.v_dict) == NULL)
11082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011083 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 return;
11085 }
11086 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11087 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11088 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11089 && dict_find(d, (char_u *)"priority", -1) != NULL
11090 && dict_find(d, (char_u *)"id", -1) != NULL))
11091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011092 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011093 return;
11094 }
11095 li = li->li_next;
11096 }
11097
11098 clear_matches(curwin);
11099 li = l->lv_first;
11100 while (li != NULL)
11101 {
11102 int i = 0;
11103 char_u buf[5];
11104 dictitem_T *di;
11105 char_u *group;
11106 int priority;
11107 int id;
11108 char_u *conceal;
11109
11110 d = li->li_tv.vval.v_dict;
11111 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11112 {
11113 if (s == NULL)
11114 {
11115 s = list_alloc();
11116 if (s == NULL)
11117 return;
11118 }
11119
11120 /* match from matchaddpos() */
11121 for (i = 1; i < 9; i++)
11122 {
11123 sprintf((char *)buf, (char *)"pos%d", i);
11124 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11125 {
11126 if (di->di_tv.v_type != VAR_LIST)
11127 return;
11128
11129 list_append_tv(s, &di->di_tv);
11130 s->lv_refcount++;
11131 }
11132 else
11133 break;
11134 }
11135 }
11136
Bram Moolenaar8f667172018-12-14 15:38:31 +010011137 group = dict_get_string(d, (char_u *)"group", TRUE);
11138 priority = (int)dict_get_number(d, (char_u *)"priority");
11139 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011140 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011141 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142 : NULL;
11143 if (i == 0)
11144 {
11145 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011146 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011147 priority, id, NULL, conceal);
11148 }
11149 else
11150 {
11151 match_add(curwin, group, NULL, priority, id, s, conceal);
11152 list_unref(s);
11153 s = NULL;
11154 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011155 vim_free(group);
11156 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157
11158 li = li->li_next;
11159 }
11160 rettv->vval.v_number = 0;
11161 }
11162#endif
11163}
11164
11165/*
11166 * "setpos()" function
11167 */
11168 static void
11169f_setpos(typval_T *argvars, typval_T *rettv)
11170{
11171 pos_T pos;
11172 int fnum;
11173 char_u *name;
11174 colnr_T curswant = -1;
11175
11176 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011177 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 if (name != NULL)
11179 {
11180 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11181 {
11182 if (--pos.col < 0)
11183 pos.col = 0;
11184 if (name[0] == '.' && name[1] == NUL)
11185 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011186 /* set cursor; "fnum" is ignored */
11187 curwin->w_cursor = pos;
11188 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011189 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011190 curwin->w_curswant = curswant - 1;
11191 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011192 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011193 check_cursor();
11194 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011195 }
11196 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11197 {
11198 /* set mark */
11199 if (setmark_pos(name[1], &pos, fnum) == OK)
11200 rettv->vval.v_number = 0;
11201 }
11202 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011203 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 }
11205 }
11206}
11207
11208/*
11209 * "setqflist()" function
11210 */
11211 static void
11212f_setqflist(typval_T *argvars, typval_T *rettv)
11213{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011214 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011215}
11216
11217/*
11218 * "setreg()" function
11219 */
11220 static void
11221f_setreg(typval_T *argvars, typval_T *rettv)
11222{
11223 int regname;
11224 char_u *strregname;
11225 char_u *stropt;
11226 char_u *strval;
11227 int append;
11228 char_u yank_type;
11229 long block_len;
11230
11231 block_len = -1;
11232 yank_type = MAUTO;
11233 append = FALSE;
11234
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011235 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011236 rettv->vval.v_number = 1; /* FAIL is default */
11237
11238 if (strregname == NULL)
11239 return; /* type error; errmsg already given */
11240 regname = *strregname;
11241 if (regname == 0 || regname == '@')
11242 regname = '"';
11243
11244 if (argvars[2].v_type != VAR_UNKNOWN)
11245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011246 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011247 if (stropt == NULL)
11248 return; /* type error */
11249 for (; *stropt != NUL; ++stropt)
11250 switch (*stropt)
11251 {
11252 case 'a': case 'A': /* append */
11253 append = TRUE;
11254 break;
11255 case 'v': case 'c': /* character-wise selection */
11256 yank_type = MCHAR;
11257 break;
11258 case 'V': case 'l': /* line-wise selection */
11259 yank_type = MLINE;
11260 break;
11261 case 'b': case Ctrl_V: /* block-wise selection */
11262 yank_type = MBLOCK;
11263 if (VIM_ISDIGIT(stropt[1]))
11264 {
11265 ++stropt;
11266 block_len = getdigits(&stropt) - 1;
11267 --stropt;
11268 }
11269 break;
11270 }
11271 }
11272
11273 if (argvars[1].v_type == VAR_LIST)
11274 {
11275 char_u **lstval;
11276 char_u **allocval;
11277 char_u buf[NUMBUFLEN];
11278 char_u **curval;
11279 char_u **curallocval;
11280 list_T *ll = argvars[1].vval.v_list;
11281 listitem_T *li;
11282 int len;
11283
11284 /* If the list is NULL handle like an empty list. */
11285 len = ll == NULL ? 0 : ll->lv_len;
11286
11287 /* First half: use for pointers to result lines; second half: use for
11288 * pointers to allocated copies. */
11289 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11290 if (lstval == NULL)
11291 return;
11292 curval = lstval;
11293 allocval = lstval + len + 2;
11294 curallocval = allocval;
11295
11296 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11297 li = li->li_next)
11298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011299 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 if (strval == NULL)
11301 goto free_lstval;
11302 if (strval == buf)
11303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 * overwrite the string. */
11306 strval = vim_strsave(buf);
11307 if (strval == NULL)
11308 goto free_lstval;
11309 *curallocval++ = strval;
11310 }
11311 *curval++ = strval;
11312 }
11313 *curval++ = NULL;
11314
11315 write_reg_contents_lst(regname, lstval, -1,
11316 append, yank_type, block_len);
11317free_lstval:
11318 while (curallocval > allocval)
11319 vim_free(*--curallocval);
11320 vim_free(lstval);
11321 }
11322 else
11323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011324 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011325 if (strval == NULL)
11326 return;
11327 write_reg_contents_ex(regname, strval, -1,
11328 append, yank_type, block_len);
11329 }
11330 rettv->vval.v_number = 0;
11331}
11332
11333/*
11334 * "settabvar()" function
11335 */
11336 static void
11337f_settabvar(typval_T *argvars, typval_T *rettv)
11338{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 tabpage_T *save_curtab;
11340 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 char_u *varname, *tabvarname;
11342 typval_T *varp;
11343
11344 rettv->vval.v_number = 0;
11345
11346 if (check_restricted() || check_secure())
11347 return;
11348
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011349 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11350 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011351 varp = &argvars[2];
11352
Bram Moolenaar4033c552017-09-16 20:54:51 +020011353 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011354 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355 save_curtab = curtab;
11356 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011357
11358 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11359 if (tabvarname != NULL)
11360 {
11361 STRCPY(tabvarname, "t:");
11362 STRCPY(tabvarname + 2, varname);
11363 set_var(tabvarname, varp, TRUE);
11364 vim_free(tabvarname);
11365 }
11366
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 /* Restore current tabpage */
11368 if (valid_tabpage(save_curtab))
11369 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 }
11371}
11372
11373/*
11374 * "settabwinvar()" function
11375 */
11376 static void
11377f_settabwinvar(typval_T *argvars, typval_T *rettv)
11378{
11379 setwinvar(argvars, rettv, 1);
11380}
11381
11382/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011383 * "settagstack()" function
11384 */
11385 static void
11386f_settagstack(typval_T *argvars, typval_T *rettv)
11387{
11388 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11389 win_T *wp;
11390 dict_T *d;
11391 int action = 'r';
11392
11393 rettv->vval.v_number = -1;
11394
11395 // first argument: window number or id
11396 wp = find_win_by_nr_or_id(&argvars[0]);
11397 if (wp == NULL)
11398 return;
11399
11400 // second argument: dict with items to set in the tag stack
11401 if (argvars[1].v_type != VAR_DICT)
11402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011403 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011404 return;
11405 }
11406 d = argvars[1].vval.v_dict;
11407 if (d == NULL)
11408 return;
11409
11410 // third argument: action - 'a' for append and 'r' for replace.
11411 // default is to replace the stack.
11412 if (argvars[2].v_type == VAR_UNKNOWN)
11413 action = 'r';
11414 else if (argvars[2].v_type == VAR_STRING)
11415 {
11416 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011417 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011418 if (actstr == NULL)
11419 return;
11420 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11421 action = *actstr;
11422 else
11423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011424 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011425 return;
11426 }
11427 }
11428 else
11429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011430 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011431 return;
11432 }
11433
11434 if (set_tagstack(wp, d, action) == OK)
11435 rettv->vval.v_number = 0;
11436}
11437
11438/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439 * "setwinvar()" function
11440 */
11441 static void
11442f_setwinvar(typval_T *argvars, typval_T *rettv)
11443{
11444 setwinvar(argvars, rettv, 0);
11445}
11446
11447#ifdef FEAT_CRYPT
11448/*
11449 * "sha256({string})" function
11450 */
11451 static void
11452f_sha256(typval_T *argvars, typval_T *rettv)
11453{
11454 char_u *p;
11455
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011456 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457 rettv->vval.v_string = vim_strsave(
11458 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11459 rettv->v_type = VAR_STRING;
11460}
11461#endif /* FEAT_CRYPT */
11462
11463/*
11464 * "shellescape({string})" function
11465 */
11466 static void
11467f_shellescape(typval_T *argvars, typval_T *rettv)
11468{
Bram Moolenaar20615522017-06-05 18:46:26 +020011469 int do_special = non_zero_arg(&argvars[1]);
11470
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011472 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011473 rettv->v_type = VAR_STRING;
11474}
11475
11476/*
11477 * shiftwidth() function
11478 */
11479 static void
11480f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11481{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011482 rettv->vval.v_number = 0;
11483
11484 if (argvars[0].v_type != VAR_UNKNOWN)
11485 {
11486 long col;
11487
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011488 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011489 if (col < 0)
11490 return; // type error; errmsg already given
11491#ifdef FEAT_VARTABS
11492 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11493 return;
11494#endif
11495 }
11496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 rettv->vval.v_number = get_sw_value(curbuf);
11498}
11499
Bram Moolenaar162b7142018-12-21 15:17:36 +010011500#ifdef FEAT_SIGNS
11501/*
11502 * "sign_define()" function
11503 */
11504 static void
11505f_sign_define(typval_T *argvars, typval_T *rettv)
11506{
11507 char_u *name;
11508 dict_T *dict;
11509 char_u *icon = NULL;
11510 char_u *linehl = NULL;
11511 char_u *text = NULL;
11512 char_u *texthl = NULL;
11513
11514 rettv->vval.v_number = -1;
11515
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011516 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011517 if (name == NULL)
11518 return;
11519
11520 if (argvars[1].v_type != VAR_UNKNOWN)
11521 {
11522 if (argvars[1].v_type != VAR_DICT)
11523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011524 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011525 return;
11526 }
11527
11528 // sign attributes
11529 dict = argvars[1].vval.v_dict;
11530 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11531 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11532 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11533 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11534 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11535 text = dict_get_string(dict, (char_u *)"text", TRUE);
11536 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11537 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11538 }
11539
11540 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11541 rettv->vval.v_number = 0;
11542
11543 vim_free(icon);
11544 vim_free(linehl);
11545 vim_free(text);
11546 vim_free(texthl);
11547}
11548
11549/*
11550 * "sign_getdefined()" function
11551 */
11552 static void
11553f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11554{
11555 char_u *name = NULL;
11556
11557 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11558 return;
11559
11560 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011561 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011562
11563 sign_getlist(name, rettv->vval.v_list);
11564}
11565
11566/*
11567 * "sign_getplaced()" function
11568 */
11569 static void
11570f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11571{
11572 buf_T *buf = NULL;
11573 dict_T *dict;
11574 dictitem_T *di;
11575 linenr_T lnum = 0;
11576 int sign_id = 0;
11577 char_u *group = NULL;
11578 int notanum = FALSE;
11579
11580 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11581 return;
11582
11583 if (argvars[0].v_type != VAR_UNKNOWN)
11584 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011585 // get signs placed in the specified buffer
11586 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011587 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011588 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011589
11590 if (argvars[1].v_type != VAR_UNKNOWN)
11591 {
11592 if (argvars[1].v_type != VAR_DICT ||
11593 ((dict = argvars[1].vval.v_dict) == NULL))
11594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011595 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011596 return;
11597 }
11598 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11599 {
11600 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011601 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011602 if (notanum)
11603 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011604 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011605 }
11606 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11607 {
11608 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011609 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011610 if (notanum)
11611 return;
11612 }
11613 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11614 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011615 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011616 if (group == NULL)
11617 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011618 if (*group == '\0') // empty string means global group
11619 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011620 }
11621 }
11622 }
11623
11624 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11625}
11626
11627/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011628 * "sign_jump()" function
11629 */
11630 static void
11631f_sign_jump(typval_T *argvars, typval_T *rettv)
11632{
11633 int sign_id;
11634 char_u *sign_group = NULL;
11635 buf_T *buf;
11636 int notanum = FALSE;
11637
11638 rettv->vval.v_number = -1;
11639
11640 // Sign identifer
11641 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11642 if (notanum)
11643 return;
11644 if (sign_id <= 0)
11645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011646 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011647 return;
11648 }
11649
11650 // Sign group
11651 sign_group = tv_get_string_chk(&argvars[1]);
11652 if (sign_group == NULL)
11653 return;
11654 if (sign_group[0] == '\0')
11655 sign_group = NULL; // global sign group
11656 else
11657 {
11658 sign_group = vim_strsave(sign_group);
11659 if (sign_group == NULL)
11660 return;
11661 }
11662
11663 // Buffer to place the sign
11664 buf = get_buf_arg(&argvars[2]);
11665 if (buf == NULL)
11666 goto cleanup;
11667
11668 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11669
11670cleanup:
11671 vim_free(sign_group);
11672}
11673
11674/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011675 * "sign_place()" function
11676 */
11677 static void
11678f_sign_place(typval_T *argvars, typval_T *rettv)
11679{
11680 int sign_id;
11681 char_u *group = NULL;
11682 char_u *sign_name;
11683 buf_T *buf;
11684 dict_T *dict;
11685 dictitem_T *di;
11686 linenr_T lnum = 0;
11687 int prio = SIGN_DEF_PRIO;
11688 int notanum = FALSE;
11689
11690 rettv->vval.v_number = -1;
11691
11692 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011693 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011694 if (notanum)
11695 return;
11696 if (sign_id < 0)
11697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011698 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011699 return;
11700 }
11701
11702 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011703 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011704 if (group == NULL)
11705 return;
11706 if (group[0] == '\0')
11707 group = NULL; // global sign group
11708 else
11709 {
11710 group = vim_strsave(group);
11711 if (group == NULL)
11712 return;
11713 }
11714
11715 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011716 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011717 if (sign_name == NULL)
11718 goto cleanup;
11719
11720 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011721 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011722 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011723 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011724
11725 if (argvars[4].v_type != VAR_UNKNOWN)
11726 {
11727 if (argvars[4].v_type != VAR_DICT ||
11728 ((dict = argvars[4].vval.v_dict) == NULL))
11729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011730 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011731 goto cleanup;
11732 }
11733
11734 // Line number where the sign is to be placed
11735 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11736 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011737 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011738 if (notanum)
11739 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011740 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011741 }
11742 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11743 {
11744 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011745 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011746 if (notanum)
11747 goto cleanup;
11748 }
11749 }
11750
11751 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11752 rettv->vval.v_number = sign_id;
11753
11754cleanup:
11755 vim_free(group);
11756}
11757
11758/*
11759 * "sign_undefine()" function
11760 */
11761 static void
11762f_sign_undefine(typval_T *argvars, typval_T *rettv)
11763{
11764 char_u *name;
11765
11766 rettv->vval.v_number = -1;
11767
11768 if (argvars[0].v_type == VAR_UNKNOWN)
11769 {
11770 // Free all the signs
11771 free_signs();
11772 rettv->vval.v_number = 0;
11773 }
11774 else
11775 {
11776 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011777 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011778 if (name == NULL)
11779 return;
11780
11781 if (sign_undefine_by_name(name) == OK)
11782 rettv->vval.v_number = 0;
11783 }
11784}
11785
11786/*
11787 * "sign_unplace()" function
11788 */
11789 static void
11790f_sign_unplace(typval_T *argvars, typval_T *rettv)
11791{
11792 dict_T *dict;
11793 dictitem_T *di;
11794 int sign_id = 0;
11795 buf_T *buf = NULL;
11796 char_u *group = NULL;
11797
11798 rettv->vval.v_number = -1;
11799
11800 if (argvars[0].v_type != VAR_STRING)
11801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011802 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011803 return;
11804 }
11805
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011806 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011807 if (group[0] == '\0')
11808 group = NULL; // global sign group
11809 else
11810 {
11811 group = vim_strsave(group);
11812 if (group == NULL)
11813 return;
11814 }
11815
11816 if (argvars[1].v_type != VAR_UNKNOWN)
11817 {
11818 if (argvars[1].v_type != VAR_DICT)
11819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011820 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011821 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011822 }
11823 dict = argvars[1].vval.v_dict;
11824
11825 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11826 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011827 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011828 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011829 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011830 }
11831 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11832 sign_id = dict_get_number(dict, (char_u *)"id");
11833 }
11834
11835 if (buf == NULL)
11836 {
11837 // Delete the sign in all the buffers
11838 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011839 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011840 rettv->vval.v_number = 0;
11841 }
11842 else
11843 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011844 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011845 rettv->vval.v_number = 0;
11846 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011847
11848cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011849 vim_free(group);
11850}
11851#endif
11852
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011853/*
11854 * "simplify()" function
11855 */
11856 static void
11857f_simplify(typval_T *argvars, typval_T *rettv)
11858{
11859 char_u *p;
11860
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011861 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 rettv->vval.v_string = vim_strsave(p);
11863 simplify_filename(rettv->vval.v_string); /* simplify in place */
11864 rettv->v_type = VAR_STRING;
11865}
11866
11867#ifdef FEAT_FLOAT
11868/*
11869 * "sin()" function
11870 */
11871 static void
11872f_sin(typval_T *argvars, typval_T *rettv)
11873{
11874 float_T f = 0.0;
11875
11876 rettv->v_type = VAR_FLOAT;
11877 if (get_float_arg(argvars, &f) == OK)
11878 rettv->vval.v_float = sin(f);
11879 else
11880 rettv->vval.v_float = 0.0;
11881}
11882
11883/*
11884 * "sinh()" function
11885 */
11886 static void
11887f_sinh(typval_T *argvars, typval_T *rettv)
11888{
11889 float_T f = 0.0;
11890
11891 rettv->v_type = VAR_FLOAT;
11892 if (get_float_arg(argvars, &f) == OK)
11893 rettv->vval.v_float = sinh(f);
11894 else
11895 rettv->vval.v_float = 0.0;
11896}
11897#endif
11898
11899static int
11900#ifdef __BORLANDC__
11901 _RTLENTRYF
11902#endif
11903 item_compare(const void *s1, const void *s2);
11904static int
11905#ifdef __BORLANDC__
11906 _RTLENTRYF
11907#endif
11908 item_compare2(const void *s1, const void *s2);
11909
11910/* struct used in the array that's given to qsort() */
11911typedef struct
11912{
11913 listitem_T *item;
11914 int idx;
11915} sortItem_T;
11916
11917/* struct storing information about current sort */
11918typedef struct
11919{
11920 int item_compare_ic;
11921 int item_compare_numeric;
11922 int item_compare_numbers;
11923#ifdef FEAT_FLOAT
11924 int item_compare_float;
11925#endif
11926 char_u *item_compare_func;
11927 partial_T *item_compare_partial;
11928 dict_T *item_compare_selfdict;
11929 int item_compare_func_err;
11930 int item_compare_keep_zero;
11931} sortinfo_T;
11932static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011933#define ITEM_COMPARE_FAIL 999
11934
11935/*
11936 * Compare functions for f_sort() and f_uniq() below.
11937 */
11938 static int
11939#ifdef __BORLANDC__
11940_RTLENTRYF
11941#endif
11942item_compare(const void *s1, const void *s2)
11943{
11944 sortItem_T *si1, *si2;
11945 typval_T *tv1, *tv2;
11946 char_u *p1, *p2;
11947 char_u *tofree1 = NULL, *tofree2 = NULL;
11948 int res;
11949 char_u numbuf1[NUMBUFLEN];
11950 char_u numbuf2[NUMBUFLEN];
11951
11952 si1 = (sortItem_T *)s1;
11953 si2 = (sortItem_T *)s2;
11954 tv1 = &si1->item->li_tv;
11955 tv2 = &si2->item->li_tv;
11956
11957 if (sortinfo->item_compare_numbers)
11958 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011959 varnumber_T v1 = tv_get_number(tv1);
11960 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011961
11962 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11963 }
11964
11965#ifdef FEAT_FLOAT
11966 if (sortinfo->item_compare_float)
11967 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011968 float_T v1 = tv_get_float(tv1);
11969 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011970
11971 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11972 }
11973#endif
11974
11975 /* tv2string() puts quotes around a string and allocates memory. Don't do
11976 * that for string variables. Use a single quote when comparing with a
11977 * non-string to do what the docs promise. */
11978 if (tv1->v_type == VAR_STRING)
11979 {
11980 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11981 p1 = (char_u *)"'";
11982 else
11983 p1 = tv1->vval.v_string;
11984 }
11985 else
11986 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
11987 if (tv2->v_type == VAR_STRING)
11988 {
11989 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
11990 p2 = (char_u *)"'";
11991 else
11992 p2 = tv2->vval.v_string;
11993 }
11994 else
11995 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
11996 if (p1 == NULL)
11997 p1 = (char_u *)"";
11998 if (p2 == NULL)
11999 p2 = (char_u *)"";
12000 if (!sortinfo->item_compare_numeric)
12001 {
12002 if (sortinfo->item_compare_ic)
12003 res = STRICMP(p1, p2);
12004 else
12005 res = STRCMP(p1, p2);
12006 }
12007 else
12008 {
12009 double n1, n2;
12010 n1 = strtod((char *)p1, (char **)&p1);
12011 n2 = strtod((char *)p2, (char **)&p2);
12012 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12013 }
12014
12015 /* When the result would be zero, compare the item indexes. Makes the
12016 * sort stable. */
12017 if (res == 0 && !sortinfo->item_compare_keep_zero)
12018 res = si1->idx > si2->idx ? 1 : -1;
12019
12020 vim_free(tofree1);
12021 vim_free(tofree2);
12022 return res;
12023}
12024
12025 static int
12026#ifdef __BORLANDC__
12027_RTLENTRYF
12028#endif
12029item_compare2(const void *s1, const void *s2)
12030{
12031 sortItem_T *si1, *si2;
12032 int res;
12033 typval_T rettv;
12034 typval_T argv[3];
12035 int dummy;
12036 char_u *func_name;
12037 partial_T *partial = sortinfo->item_compare_partial;
12038
12039 /* shortcut after failure in previous call; compare all items equal */
12040 if (sortinfo->item_compare_func_err)
12041 return 0;
12042
12043 si1 = (sortItem_T *)s1;
12044 si2 = (sortItem_T *)s2;
12045
12046 if (partial == NULL)
12047 func_name = sortinfo->item_compare_func;
12048 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012049 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012050
12051 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12052 * in the copy without changing the original list items. */
12053 copy_tv(&si1->item->li_tv, &argv[0]);
12054 copy_tv(&si2->item->li_tv, &argv[1]);
12055
12056 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12057 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012058 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012059 partial, sortinfo->item_compare_selfdict);
12060 clear_tv(&argv[0]);
12061 clear_tv(&argv[1]);
12062
12063 if (res == FAIL)
12064 res = ITEM_COMPARE_FAIL;
12065 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012066 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012067 if (sortinfo->item_compare_func_err)
12068 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12069 clear_tv(&rettv);
12070
12071 /* When the result would be zero, compare the pointers themselves. Makes
12072 * the sort stable. */
12073 if (res == 0 && !sortinfo->item_compare_keep_zero)
12074 res = si1->idx > si2->idx ? 1 : -1;
12075
12076 return res;
12077}
12078
12079/*
12080 * "sort({list})" function
12081 */
12082 static void
12083do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12084{
12085 list_T *l;
12086 listitem_T *li;
12087 sortItem_T *ptrs;
12088 sortinfo_T *old_sortinfo;
12089 sortinfo_T info;
12090 long len;
12091 long i;
12092
12093 /* Pointer to current info struct used in compare function. Save and
12094 * restore the current one for nested calls. */
12095 old_sortinfo = sortinfo;
12096 sortinfo = &info;
12097
12098 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012099 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012100 else
12101 {
12102 l = argvars[0].vval.v_list;
12103 if (l == NULL || tv_check_lock(l->lv_lock,
12104 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12105 TRUE))
12106 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012107 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012108
12109 len = list_len(l);
12110 if (len <= 1)
12111 goto theend; /* short list sorts pretty quickly */
12112
12113 info.item_compare_ic = FALSE;
12114 info.item_compare_numeric = FALSE;
12115 info.item_compare_numbers = FALSE;
12116#ifdef FEAT_FLOAT
12117 info.item_compare_float = FALSE;
12118#endif
12119 info.item_compare_func = NULL;
12120 info.item_compare_partial = NULL;
12121 info.item_compare_selfdict = NULL;
12122 if (argvars[1].v_type != VAR_UNKNOWN)
12123 {
12124 /* optional second argument: {func} */
12125 if (argvars[1].v_type == VAR_FUNC)
12126 info.item_compare_func = argvars[1].vval.v_string;
12127 else if (argvars[1].v_type == VAR_PARTIAL)
12128 info.item_compare_partial = argvars[1].vval.v_partial;
12129 else
12130 {
12131 int error = FALSE;
12132
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012133 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012134 if (error)
12135 goto theend; /* type error; errmsg already given */
12136 if (i == 1)
12137 info.item_compare_ic = TRUE;
12138 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012139 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012140 else if (i != 0)
12141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012142 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012143 goto theend;
12144 }
12145 if (info.item_compare_func != NULL)
12146 {
12147 if (*info.item_compare_func == NUL)
12148 {
12149 /* empty string means default sort */
12150 info.item_compare_func = NULL;
12151 }
12152 else if (STRCMP(info.item_compare_func, "n") == 0)
12153 {
12154 info.item_compare_func = NULL;
12155 info.item_compare_numeric = TRUE;
12156 }
12157 else if (STRCMP(info.item_compare_func, "N") == 0)
12158 {
12159 info.item_compare_func = NULL;
12160 info.item_compare_numbers = TRUE;
12161 }
12162#ifdef FEAT_FLOAT
12163 else if (STRCMP(info.item_compare_func, "f") == 0)
12164 {
12165 info.item_compare_func = NULL;
12166 info.item_compare_float = TRUE;
12167 }
12168#endif
12169 else if (STRCMP(info.item_compare_func, "i") == 0)
12170 {
12171 info.item_compare_func = NULL;
12172 info.item_compare_ic = TRUE;
12173 }
12174 }
12175 }
12176
12177 if (argvars[2].v_type != VAR_UNKNOWN)
12178 {
12179 /* optional third argument: {dict} */
12180 if (argvars[2].v_type != VAR_DICT)
12181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012182 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012183 goto theend;
12184 }
12185 info.item_compare_selfdict = argvars[2].vval.v_dict;
12186 }
12187 }
12188
12189 /* Make an array with each entry pointing to an item in the List. */
12190 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12191 if (ptrs == NULL)
12192 goto theend;
12193
12194 i = 0;
12195 if (sort)
12196 {
12197 /* sort(): ptrs will be the list to sort */
12198 for (li = l->lv_first; li != NULL; li = li->li_next)
12199 {
12200 ptrs[i].item = li;
12201 ptrs[i].idx = i;
12202 ++i;
12203 }
12204
12205 info.item_compare_func_err = FALSE;
12206 info.item_compare_keep_zero = FALSE;
12207 /* test the compare function */
12208 if ((info.item_compare_func != NULL
12209 || info.item_compare_partial != NULL)
12210 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12211 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012212 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012213 else
12214 {
12215 /* Sort the array with item pointers. */
12216 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12217 info.item_compare_func == NULL
12218 && info.item_compare_partial == NULL
12219 ? item_compare : item_compare2);
12220
12221 if (!info.item_compare_func_err)
12222 {
12223 /* Clear the List and append the items in sorted order. */
12224 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12225 l->lv_len = 0;
12226 for (i = 0; i < len; ++i)
12227 list_append(l, ptrs[i].item);
12228 }
12229 }
12230 }
12231 else
12232 {
12233 int (*item_compare_func_ptr)(const void *, const void *);
12234
12235 /* f_uniq(): ptrs will be a stack of items to remove */
12236 info.item_compare_func_err = FALSE;
12237 info.item_compare_keep_zero = TRUE;
12238 item_compare_func_ptr = info.item_compare_func != NULL
12239 || info.item_compare_partial != NULL
12240 ? item_compare2 : item_compare;
12241
12242 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12243 li = li->li_next)
12244 {
12245 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12246 == 0)
12247 ptrs[i++].item = li;
12248 if (info.item_compare_func_err)
12249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012250 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012251 break;
12252 }
12253 }
12254
12255 if (!info.item_compare_func_err)
12256 {
12257 while (--i >= 0)
12258 {
12259 li = ptrs[i].item->li_next;
12260 ptrs[i].item->li_next = li->li_next;
12261 if (li->li_next != NULL)
12262 li->li_next->li_prev = ptrs[i].item;
12263 else
12264 l->lv_last = ptrs[i].item;
12265 list_fix_watch(l, li);
12266 listitem_free(li);
12267 l->lv_len--;
12268 }
12269 }
12270 }
12271
12272 vim_free(ptrs);
12273 }
12274theend:
12275 sortinfo = old_sortinfo;
12276}
12277
12278/*
12279 * "sort({list})" function
12280 */
12281 static void
12282f_sort(typval_T *argvars, typval_T *rettv)
12283{
12284 do_sort_uniq(argvars, rettv, TRUE);
12285}
12286
12287/*
12288 * "uniq({list})" function
12289 */
12290 static void
12291f_uniq(typval_T *argvars, typval_T *rettv)
12292{
12293 do_sort_uniq(argvars, rettv, FALSE);
12294}
12295
12296/*
12297 * "soundfold({word})" function
12298 */
12299 static void
12300f_soundfold(typval_T *argvars, typval_T *rettv)
12301{
12302 char_u *s;
12303
12304 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012305 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012306#ifdef FEAT_SPELL
12307 rettv->vval.v_string = eval_soundfold(s);
12308#else
12309 rettv->vval.v_string = vim_strsave(s);
12310#endif
12311}
12312
12313/*
12314 * "spellbadword()" function
12315 */
12316 static void
12317f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12318{
12319 char_u *word = (char_u *)"";
12320 hlf_T attr = HLF_COUNT;
12321 int len = 0;
12322
12323 if (rettv_list_alloc(rettv) == FAIL)
12324 return;
12325
12326#ifdef FEAT_SPELL
12327 if (argvars[0].v_type == VAR_UNKNOWN)
12328 {
12329 /* Find the start and length of the badly spelled word. */
12330 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12331 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012332 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012334 curwin->w_set_curswant = TRUE;
12335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012336 }
12337 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012339 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012340 int capcol = -1;
12341
12342 if (str != NULL)
12343 {
12344 /* Check the argument for spelling. */
12345 while (*str != NUL)
12346 {
12347 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12348 if (attr != HLF_COUNT)
12349 {
12350 word = str;
12351 break;
12352 }
12353 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012354 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012355 }
12356 }
12357 }
12358#endif
12359
12360 list_append_string(rettv->vval.v_list, word, len);
12361 list_append_string(rettv->vval.v_list, (char_u *)(
12362 attr == HLF_SPB ? "bad" :
12363 attr == HLF_SPR ? "rare" :
12364 attr == HLF_SPL ? "local" :
12365 attr == HLF_SPC ? "caps" :
12366 ""), -1);
12367}
12368
12369/*
12370 * "spellsuggest()" function
12371 */
12372 static void
12373f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12374{
12375#ifdef FEAT_SPELL
12376 char_u *str;
12377 int typeerr = FALSE;
12378 int maxcount;
12379 garray_T ga;
12380 int i;
12381 listitem_T *li;
12382 int need_capital = FALSE;
12383#endif
12384
12385 if (rettv_list_alloc(rettv) == FAIL)
12386 return;
12387
12388#ifdef FEAT_SPELL
12389 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012391 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012392 if (argvars[1].v_type != VAR_UNKNOWN)
12393 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012394 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012395 if (maxcount <= 0)
12396 return;
12397 if (argvars[2].v_type != VAR_UNKNOWN)
12398 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012399 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012400 if (typeerr)
12401 return;
12402 }
12403 }
12404 else
12405 maxcount = 25;
12406
12407 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12408
12409 for (i = 0; i < ga.ga_len; ++i)
12410 {
12411 str = ((char_u **)ga.ga_data)[i];
12412
12413 li = listitem_alloc();
12414 if (li == NULL)
12415 vim_free(str);
12416 else
12417 {
12418 li->li_tv.v_type = VAR_STRING;
12419 li->li_tv.v_lock = 0;
12420 li->li_tv.vval.v_string = str;
12421 list_append(rettv->vval.v_list, li);
12422 }
12423 }
12424 ga_clear(&ga);
12425 }
12426#endif
12427}
12428
12429 static void
12430f_split(typval_T *argvars, typval_T *rettv)
12431{
12432 char_u *str;
12433 char_u *end;
12434 char_u *pat = NULL;
12435 regmatch_T regmatch;
12436 char_u patbuf[NUMBUFLEN];
12437 char_u *save_cpo;
12438 int match;
12439 colnr_T col = 0;
12440 int keepempty = FALSE;
12441 int typeerr = FALSE;
12442
12443 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12444 save_cpo = p_cpo;
12445 p_cpo = (char_u *)"";
12446
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012447 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012448 if (argvars[1].v_type != VAR_UNKNOWN)
12449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012450 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451 if (pat == NULL)
12452 typeerr = TRUE;
12453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012454 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012455 }
12456 if (pat == NULL || *pat == NUL)
12457 pat = (char_u *)"[\\x01- ]\\+";
12458
12459 if (rettv_list_alloc(rettv) == FAIL)
12460 return;
12461 if (typeerr)
12462 return;
12463
12464 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12465 if (regmatch.regprog != NULL)
12466 {
12467 regmatch.rm_ic = FALSE;
12468 while (*str != NUL || keepempty)
12469 {
12470 if (*str == NUL)
12471 match = FALSE; /* empty item at the end */
12472 else
12473 match = vim_regexec_nl(&regmatch, str, col);
12474 if (match)
12475 end = regmatch.startp[0];
12476 else
12477 end = str + STRLEN(str);
12478 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12479 && *str != NUL && match && end < regmatch.endp[0]))
12480 {
12481 if (list_append_string(rettv->vval.v_list, str,
12482 (int)(end - str)) == FAIL)
12483 break;
12484 }
12485 if (!match)
12486 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012487 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012488 if (regmatch.endp[0] > str)
12489 col = 0;
12490 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012491 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012492 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012493 str = regmatch.endp[0];
12494 }
12495
12496 vim_regfree(regmatch.regprog);
12497 }
12498
12499 p_cpo = save_cpo;
12500}
12501
12502#ifdef FEAT_FLOAT
12503/*
12504 * "sqrt()" function
12505 */
12506 static void
12507f_sqrt(typval_T *argvars, typval_T *rettv)
12508{
12509 float_T f = 0.0;
12510
12511 rettv->v_type = VAR_FLOAT;
12512 if (get_float_arg(argvars, &f) == OK)
12513 rettv->vval.v_float = sqrt(f);
12514 else
12515 rettv->vval.v_float = 0.0;
12516}
12517
12518/*
12519 * "str2float()" function
12520 */
12521 static void
12522f_str2float(typval_T *argvars, typval_T *rettv)
12523{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012524 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012525 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526
Bram Moolenaar08243d22017-01-10 16:12:29 +010012527 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012528 p = skipwhite(p + 1);
12529 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012530 if (isneg)
12531 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012532 rettv->v_type = VAR_FLOAT;
12533}
12534#endif
12535
12536/*
12537 * "str2nr()" function
12538 */
12539 static void
12540f_str2nr(typval_T *argvars, typval_T *rettv)
12541{
12542 int base = 10;
12543 char_u *p;
12544 varnumber_T n;
12545 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012546 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547
12548 if (argvars[1].v_type != VAR_UNKNOWN)
12549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012550 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551 if (base != 2 && base != 8 && base != 10 && base != 16)
12552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012553 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012554 return;
12555 }
12556 }
12557
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012558 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012559 isneg = (*p == '-');
12560 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 p = skipwhite(p + 1);
12562 switch (base)
12563 {
12564 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12565 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12566 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12567 default: what = 0;
12568 }
12569 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012570 if (isneg)
12571 rettv->vval.v_number = -n;
12572 else
12573 rettv->vval.v_number = n;
12574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012575}
12576
12577#ifdef HAVE_STRFTIME
12578/*
12579 * "strftime({format}[, {time}])" function
12580 */
12581 static void
12582f_strftime(typval_T *argvars, typval_T *rettv)
12583{
12584 char_u result_buf[256];
12585 struct tm *curtime;
12586 time_t seconds;
12587 char_u *p;
12588
12589 rettv->v_type = VAR_STRING;
12590
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012591 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012592 if (argvars[1].v_type == VAR_UNKNOWN)
12593 seconds = time(NULL);
12594 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012595 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012596 curtime = localtime(&seconds);
12597 /* MSVC returns NULL for an invalid value of seconds. */
12598 if (curtime == NULL)
12599 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12600 else
12601 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012602 vimconv_T conv;
12603 char_u *enc;
12604
12605 conv.vc_type = CONV_NONE;
12606 enc = enc_locale();
12607 convert_setup(&conv, p_enc, enc);
12608 if (conv.vc_type != CONV_NONE)
12609 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012610 if (p != NULL)
12611 (void)strftime((char *)result_buf, sizeof(result_buf),
12612 (char *)p, curtime);
12613 else
12614 result_buf[0] = NUL;
12615
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012616 if (conv.vc_type != CONV_NONE)
12617 vim_free(p);
12618 convert_setup(&conv, enc, p_enc);
12619 if (conv.vc_type != CONV_NONE)
12620 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12621 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 rettv->vval.v_string = vim_strsave(result_buf);
12623
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012624 /* Release conversion descriptors */
12625 convert_setup(&conv, NULL, NULL);
12626 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012627 }
12628}
12629#endif
12630
12631/*
12632 * "strgetchar()" function
12633 */
12634 static void
12635f_strgetchar(typval_T *argvars, typval_T *rettv)
12636{
12637 char_u *str;
12638 int len;
12639 int error = FALSE;
12640 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010012641 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012642
12643 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012644 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012645 if (str == NULL)
12646 return;
12647 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012648 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012649 if (error)
12650 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651
Bram Moolenaar13505972019-01-24 15:04:48 +010012652 while (charidx >= 0 && byteidx < len)
12653 {
12654 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655 {
Bram Moolenaar13505972019-01-24 15:04:48 +010012656 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12657 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 }
Bram Moolenaar13505972019-01-24 15:04:48 +010012659 --charidx;
12660 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662}
12663
12664/*
12665 * "stridx()" function
12666 */
12667 static void
12668f_stridx(typval_T *argvars, typval_T *rettv)
12669{
12670 char_u buf[NUMBUFLEN];
12671 char_u *needle;
12672 char_u *haystack;
12673 char_u *save_haystack;
12674 char_u *pos;
12675 int start_idx;
12676
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012677 needle = tv_get_string_chk(&argvars[1]);
12678 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012679 rettv->vval.v_number = -1;
12680 if (needle == NULL || haystack == NULL)
12681 return; /* type error; errmsg already given */
12682
12683 if (argvars[2].v_type != VAR_UNKNOWN)
12684 {
12685 int error = FALSE;
12686
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012687 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012688 if (error || start_idx >= (int)STRLEN(haystack))
12689 return;
12690 if (start_idx >= 0)
12691 haystack += start_idx;
12692 }
12693
12694 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12695 if (pos != NULL)
12696 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12697}
12698
12699/*
12700 * "string()" function
12701 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012702 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012703f_string(typval_T *argvars, typval_T *rettv)
12704{
12705 char_u *tofree;
12706 char_u numbuf[NUMBUFLEN];
12707
12708 rettv->v_type = VAR_STRING;
12709 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12710 get_copyID());
12711 /* Make a copy if we have a value but it's not in allocated memory. */
12712 if (rettv->vval.v_string != NULL && tofree == NULL)
12713 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12714}
12715
12716/*
12717 * "strlen()" function
12718 */
12719 static void
12720f_strlen(typval_T *argvars, typval_T *rettv)
12721{
12722 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012723 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012724}
12725
12726/*
12727 * "strchars()" function
12728 */
12729 static void
12730f_strchars(typval_T *argvars, typval_T *rettv)
12731{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012732 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 varnumber_T len = 0;
12735 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736
12737 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012738 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012739 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012740 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 else
12742 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012743 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12744 while (*s != NUL)
12745 {
12746 func_mb_ptr2char_adv(&s);
12747 ++len;
12748 }
12749 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012750 }
12751}
12752
12753/*
12754 * "strdisplaywidth()" function
12755 */
12756 static void
12757f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12758{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012759 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012760 int col = 0;
12761
12762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012763 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012764
12765 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12766}
12767
12768/*
12769 * "strwidth()" function
12770 */
12771 static void
12772f_strwidth(typval_T *argvars, typval_T *rettv)
12773{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012774 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012775
Bram Moolenaar13505972019-01-24 15:04:48 +010012776 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012777}
12778
12779/*
12780 * "strcharpart()" function
12781 */
12782 static void
12783f_strcharpart(typval_T *argvars, typval_T *rettv)
12784{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012785 char_u *p;
12786 int nchar;
12787 int nbyte = 0;
12788 int charlen;
12789 int len = 0;
12790 int slen;
12791 int error = FALSE;
12792
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012793 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012794 slen = (int)STRLEN(p);
12795
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012796 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012797 if (!error)
12798 {
12799 if (nchar > 0)
12800 while (nchar > 0 && nbyte < slen)
12801 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012802 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012803 --nchar;
12804 }
12805 else
12806 nbyte = nchar;
12807 if (argvars[2].v_type != VAR_UNKNOWN)
12808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012809 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012810 while (charlen > 0 && nbyte + len < slen)
12811 {
12812 int off = nbyte + len;
12813
12814 if (off < 0)
12815 len += 1;
12816 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012817 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 --charlen;
12819 }
12820 }
12821 else
12822 len = slen - nbyte; /* default: all bytes that are available. */
12823 }
12824
12825 /*
12826 * Only return the overlap between the specified part and the actual
12827 * string.
12828 */
12829 if (nbyte < 0)
12830 {
12831 len += nbyte;
12832 nbyte = 0;
12833 }
12834 else if (nbyte > slen)
12835 nbyte = slen;
12836 if (len < 0)
12837 len = 0;
12838 else if (nbyte + len > slen)
12839 len = slen - nbyte;
12840
12841 rettv->v_type = VAR_STRING;
12842 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012843}
12844
12845/*
12846 * "strpart()" function
12847 */
12848 static void
12849f_strpart(typval_T *argvars, typval_T *rettv)
12850{
12851 char_u *p;
12852 int n;
12853 int len;
12854 int slen;
12855 int error = FALSE;
12856
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012857 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012858 slen = (int)STRLEN(p);
12859
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012860 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861 if (error)
12862 len = 0;
12863 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012864 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865 else
12866 len = slen - n; /* default len: all bytes that are available. */
12867
12868 /*
12869 * Only return the overlap between the specified part and the actual
12870 * string.
12871 */
12872 if (n < 0)
12873 {
12874 len += n;
12875 n = 0;
12876 }
12877 else if (n > slen)
12878 n = slen;
12879 if (len < 0)
12880 len = 0;
12881 else if (n + len > slen)
12882 len = slen - n;
12883
12884 rettv->v_type = VAR_STRING;
12885 rettv->vval.v_string = vim_strnsave(p + n, len);
12886}
12887
12888/*
12889 * "strridx()" function
12890 */
12891 static void
12892f_strridx(typval_T *argvars, typval_T *rettv)
12893{
12894 char_u buf[NUMBUFLEN];
12895 char_u *needle;
12896 char_u *haystack;
12897 char_u *rest;
12898 char_u *lastmatch = NULL;
12899 int haystack_len, end_idx;
12900
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012901 needle = tv_get_string_chk(&argvars[1]);
12902 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903
12904 rettv->vval.v_number = -1;
12905 if (needle == NULL || haystack == NULL)
12906 return; /* type error; errmsg already given */
12907
12908 haystack_len = (int)STRLEN(haystack);
12909 if (argvars[2].v_type != VAR_UNKNOWN)
12910 {
12911 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012912 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 if (end_idx < 0)
12914 return; /* can never find a match */
12915 }
12916 else
12917 end_idx = haystack_len;
12918
12919 if (*needle == NUL)
12920 {
12921 /* Empty string matches past the end. */
12922 lastmatch = haystack + end_idx;
12923 }
12924 else
12925 {
12926 for (rest = haystack; *rest != '\0'; ++rest)
12927 {
12928 rest = (char_u *)strstr((char *)rest, (char *)needle);
12929 if (rest == NULL || rest > haystack + end_idx)
12930 break;
12931 lastmatch = rest;
12932 }
12933 }
12934
12935 if (lastmatch == NULL)
12936 rettv->vval.v_number = -1;
12937 else
12938 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12939}
12940
12941/*
12942 * "strtrans()" function
12943 */
12944 static void
12945f_strtrans(typval_T *argvars, typval_T *rettv)
12946{
12947 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012948 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949}
12950
12951/*
12952 * "submatch()" function
12953 */
12954 static void
12955f_submatch(typval_T *argvars, typval_T *rettv)
12956{
12957 int error = FALSE;
12958 int no;
12959 int retList = 0;
12960
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012961 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012962 if (error)
12963 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012964 if (no < 0 || no >= NSUBEXP)
12965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012966 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010012967 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020012968 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012970 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012971 if (error)
12972 return;
12973
12974 if (retList == 0)
12975 {
12976 rettv->v_type = VAR_STRING;
12977 rettv->vval.v_string = reg_submatch(no);
12978 }
12979 else
12980 {
12981 rettv->v_type = VAR_LIST;
12982 rettv->vval.v_list = reg_submatch_list(no);
12983 }
12984}
12985
12986/*
12987 * "substitute()" function
12988 */
12989 static void
12990f_substitute(typval_T *argvars, typval_T *rettv)
12991{
12992 char_u patbuf[NUMBUFLEN];
12993 char_u subbuf[NUMBUFLEN];
12994 char_u flagsbuf[NUMBUFLEN];
12995
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012996 char_u *str = tv_get_string_chk(&argvars[0]);
12997 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020012998 char_u *sub = NULL;
12999 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013000 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013001
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013002 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13003 expr = &argvars[2];
13004 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013005 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013006
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013007 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013008 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13009 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013010 rettv->vval.v_string = NULL;
13011 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013012 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013}
13014
13015/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013016 * "swapinfo(swap_filename)" function
13017 */
13018 static void
13019f_swapinfo(typval_T *argvars, typval_T *rettv)
13020{
13021 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013022 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013023}
13024
13025/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013026 * "swapname(expr)" function
13027 */
13028 static void
13029f_swapname(typval_T *argvars, typval_T *rettv)
13030{
13031 buf_T *buf;
13032
13033 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013034 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013035 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13036 || buf->b_ml.ml_mfp->mf_fname == NULL)
13037 rettv->vval.v_string = NULL;
13038 else
13039 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13040}
13041
13042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 * "synID(lnum, col, trans)" function
13044 */
13045 static void
13046f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13047{
13048 int id = 0;
13049#ifdef FEAT_SYN_HL
13050 linenr_T lnum;
13051 colnr_T col;
13052 int trans;
13053 int transerr = FALSE;
13054
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013055 lnum = tv_get_lnum(argvars); /* -1 on type error */
13056 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13057 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013058
13059 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13060 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13061 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13062#endif
13063
13064 rettv->vval.v_number = id;
13065}
13066
13067/*
13068 * "synIDattr(id, what [, mode])" function
13069 */
13070 static void
13071f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13072{
13073 char_u *p = NULL;
13074#ifdef FEAT_SYN_HL
13075 int id;
13076 char_u *what;
13077 char_u *mode;
13078 char_u modebuf[NUMBUFLEN];
13079 int modec;
13080
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013081 id = (int)tv_get_number(&argvars[0]);
13082 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 if (argvars[2].v_type != VAR_UNKNOWN)
13084 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 modec = TOLOWER_ASC(mode[0]);
13087 if (modec != 't' && modec != 'c' && modec != 'g')
13088 modec = 0; /* replace invalid with current */
13089 }
13090 else
13091 {
13092#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13093 if (USE_24BIT)
13094 modec = 'g';
13095 else
13096#endif
13097 if (t_colors > 1)
13098 modec = 'c';
13099 else
13100 modec = 't';
13101 }
13102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103 switch (TOLOWER_ASC(what[0]))
13104 {
13105 case 'b':
13106 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13107 p = highlight_color(id, what, modec);
13108 else /* bold */
13109 p = highlight_has_attr(id, HL_BOLD, modec);
13110 break;
13111
13112 case 'f': /* fg[#] or font */
13113 p = highlight_color(id, what, modec);
13114 break;
13115
13116 case 'i':
13117 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13118 p = highlight_has_attr(id, HL_INVERSE, modec);
13119 else /* italic */
13120 p = highlight_has_attr(id, HL_ITALIC, modec);
13121 break;
13122
13123 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013124 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013125 break;
13126
13127 case 'r': /* reverse */
13128 p = highlight_has_attr(id, HL_INVERSE, modec);
13129 break;
13130
13131 case 's':
13132 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13133 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013134 /* strikeout */
13135 else if (TOLOWER_ASC(what[1]) == 't' &&
13136 TOLOWER_ASC(what[2]) == 'r')
13137 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138 else /* standout */
13139 p = highlight_has_attr(id, HL_STANDOUT, modec);
13140 break;
13141
13142 case 'u':
13143 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13144 /* underline */
13145 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13146 else
13147 /* undercurl */
13148 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13149 break;
13150 }
13151
13152 if (p != NULL)
13153 p = vim_strsave(p);
13154#endif
13155 rettv->v_type = VAR_STRING;
13156 rettv->vval.v_string = p;
13157}
13158
13159/*
13160 * "synIDtrans(id)" function
13161 */
13162 static void
13163f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13164{
13165 int id;
13166
13167#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013168 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013169
13170 if (id > 0)
13171 id = syn_get_final_id(id);
13172 else
13173#endif
13174 id = 0;
13175
13176 rettv->vval.v_number = id;
13177}
13178
13179/*
13180 * "synconcealed(lnum, col)" function
13181 */
13182 static void
13183f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13184{
13185#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13186 linenr_T lnum;
13187 colnr_T col;
13188 int syntax_flags = 0;
13189 int cchar;
13190 int matchid = 0;
13191 char_u str[NUMBUFLEN];
13192#endif
13193
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013194 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195
13196#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013197 lnum = tv_get_lnum(argvars); /* -1 on type error */
13198 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013199
13200 vim_memset(str, NUL, sizeof(str));
13201
13202 if (rettv_list_alloc(rettv) != FAIL)
13203 {
13204 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13205 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13206 && curwin->w_p_cole > 0)
13207 {
13208 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13209 syntax_flags = get_syntax_info(&matchid);
13210
13211 /* get the conceal character */
13212 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13213 {
13214 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013215 if (cchar == NUL && curwin->w_p_cole == 1)
13216 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 if (cchar != NUL)
13218 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (has_mbyte)
13220 (*mb_char2bytes)(cchar, str);
13221 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222 str[0] = cchar;
13223 }
13224 }
13225 }
13226
13227 list_append_number(rettv->vval.v_list,
13228 (syntax_flags & HL_CONCEAL) != 0);
13229 /* -1 to auto-determine strlen */
13230 list_append_string(rettv->vval.v_list, str, -1);
13231 list_append_number(rettv->vval.v_list, matchid);
13232 }
13233#endif
13234}
13235
13236/*
13237 * "synstack(lnum, col)" function
13238 */
13239 static void
13240f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13241{
13242#ifdef FEAT_SYN_HL
13243 linenr_T lnum;
13244 colnr_T col;
13245 int i;
13246 int id;
13247#endif
13248
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013249 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013250
13251#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013252 lnum = tv_get_lnum(argvars); /* -1 on type error */
13253 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254
13255 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13256 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13257 && rettv_list_alloc(rettv) != FAIL)
13258 {
13259 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13260 for (i = 0; ; ++i)
13261 {
13262 id = syn_get_stack_item(i);
13263 if (id < 0)
13264 break;
13265 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13266 break;
13267 }
13268 }
13269#endif
13270}
13271
13272 static void
13273get_cmd_output_as_rettv(
13274 typval_T *argvars,
13275 typval_T *rettv,
13276 int retlist)
13277{
13278 char_u *res = NULL;
13279 char_u *p;
13280 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013281 int err = FALSE;
13282 FILE *fd;
13283 list_T *list = NULL;
13284 int flags = SHELL_SILENT;
13285
13286 rettv->v_type = VAR_STRING;
13287 rettv->vval.v_string = NULL;
13288 if (check_restricted() || check_secure())
13289 goto errret;
13290
13291 if (argvars[1].v_type != VAR_UNKNOWN)
13292 {
13293 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013294 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 * command.
13296 */
13297 if ((infile = vim_tempname('i', TRUE)) == NULL)
13298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013299 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 goto errret;
13301 }
13302
13303 fd = mch_fopen((char *)infile, WRITEBIN);
13304 if (fd == NULL)
13305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013306 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013307 goto errret;
13308 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013309 if (argvars[1].v_type == VAR_NUMBER)
13310 {
13311 linenr_T lnum;
13312 buf_T *buf;
13313
13314 buf = buflist_findnr(argvars[1].vval.v_number);
13315 if (buf == NULL)
13316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013317 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013318 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013319 goto errret;
13320 }
13321
13322 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13323 {
13324 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13325 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13326 {
13327 err = TRUE;
13328 break;
13329 }
13330 if (putc(NL, fd) == EOF)
13331 {
13332 err = TRUE;
13333 break;
13334 }
13335 }
13336 }
13337 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 {
13339 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13340 err = TRUE;
13341 }
13342 else
13343 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013344 size_t len;
13345 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013347 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348 if (p == NULL)
13349 {
13350 fclose(fd);
13351 goto errret; /* type error; errmsg already given */
13352 }
13353 len = STRLEN(p);
13354 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13355 err = TRUE;
13356 }
13357 if (fclose(fd) != 0)
13358 err = TRUE;
13359 if (err)
13360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013361 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362 goto errret;
13363 }
13364 }
13365
13366 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13367 * echoes typeahead, that messes up the display. */
13368 if (!msg_silent)
13369 flags += SHELL_COOKED;
13370
13371 if (retlist)
13372 {
13373 int len;
13374 listitem_T *li;
13375 char_u *s = NULL;
13376 char_u *start;
13377 char_u *end;
13378 int i;
13379
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013380 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381 if (res == NULL)
13382 goto errret;
13383
13384 list = list_alloc();
13385 if (list == NULL)
13386 goto errret;
13387
13388 for (i = 0; i < len; ++i)
13389 {
13390 start = res + i;
13391 while (i < len && res[i] != NL)
13392 ++i;
13393 end = res + i;
13394
13395 s = alloc((unsigned)(end - start + 1));
13396 if (s == NULL)
13397 goto errret;
13398
13399 for (p = s; start < end; ++p, ++start)
13400 *p = *start == NUL ? NL : *start;
13401 *p = NUL;
13402
13403 li = listitem_alloc();
13404 if (li == NULL)
13405 {
13406 vim_free(s);
13407 goto errret;
13408 }
13409 li->li_tv.v_type = VAR_STRING;
13410 li->li_tv.v_lock = 0;
13411 li->li_tv.vval.v_string = s;
13412 list_append(list, li);
13413 }
13414
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013415 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416 list = NULL;
13417 }
13418 else
13419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013420 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013421#ifdef USE_CR
13422 /* translate <CR> into <NL> */
13423 if (res != NULL)
13424 {
13425 char_u *s;
13426
13427 for (s = res; *s; ++s)
13428 {
13429 if (*s == CAR)
13430 *s = NL;
13431 }
13432 }
13433#else
13434# ifdef USE_CRNL
13435 /* translate <CR><NL> into <NL> */
13436 if (res != NULL)
13437 {
13438 char_u *s, *d;
13439
13440 d = res;
13441 for (s = res; *s; ++s)
13442 {
13443 if (s[0] == CAR && s[1] == NL)
13444 ++s;
13445 *d++ = *s;
13446 }
13447 *d = NUL;
13448 }
13449# endif
13450#endif
13451 rettv->vval.v_string = res;
13452 res = NULL;
13453 }
13454
13455errret:
13456 if (infile != NULL)
13457 {
13458 mch_remove(infile);
13459 vim_free(infile);
13460 }
13461 if (res != NULL)
13462 vim_free(res);
13463 if (list != NULL)
13464 list_free(list);
13465}
13466
13467/*
13468 * "system()" function
13469 */
13470 static void
13471f_system(typval_T *argvars, typval_T *rettv)
13472{
13473 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13474}
13475
13476/*
13477 * "systemlist()" function
13478 */
13479 static void
13480f_systemlist(typval_T *argvars, typval_T *rettv)
13481{
13482 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13483}
13484
13485/*
13486 * "tabpagebuflist()" function
13487 */
13488 static void
13489f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13490{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013491 tabpage_T *tp;
13492 win_T *wp = NULL;
13493
13494 if (argvars[0].v_type == VAR_UNKNOWN)
13495 wp = firstwin;
13496 else
13497 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013498 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013499 if (tp != NULL)
13500 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13501 }
13502 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13503 {
13504 for (; wp != NULL; wp = wp->w_next)
13505 if (list_append_number(rettv->vval.v_list,
13506 wp->w_buffer->b_fnum) == FAIL)
13507 break;
13508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013509}
13510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013511/*
13512 * "tabpagenr()" function
13513 */
13514 static void
13515f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13516{
13517 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013518 char_u *arg;
13519
13520 if (argvars[0].v_type != VAR_UNKNOWN)
13521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013522 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013523 nr = 0;
13524 if (arg != NULL)
13525 {
13526 if (STRCMP(arg, "$") == 0)
13527 nr = tabpage_index(NULL) - 1;
13528 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013529 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013530 }
13531 }
13532 else
13533 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013534 rettv->vval.v_number = nr;
13535}
13536
13537
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013538/*
13539 * Common code for tabpagewinnr() and winnr().
13540 */
13541 static int
13542get_winnr(tabpage_T *tp, typval_T *argvar)
13543{
13544 win_T *twin;
13545 int nr = 1;
13546 win_T *wp;
13547 char_u *arg;
13548
13549 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13550 if (argvar->v_type != VAR_UNKNOWN)
13551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013552 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013553 if (arg == NULL)
13554 nr = 0; /* type error; errmsg already given */
13555 else if (STRCMP(arg, "$") == 0)
13556 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13557 else if (STRCMP(arg, "#") == 0)
13558 {
13559 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13560 if (twin == NULL)
13561 nr = 0;
13562 }
13563 else
13564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013565 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 nr = 0;
13567 }
13568 }
13569
13570 if (nr > 0)
13571 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13572 wp != twin; wp = wp->w_next)
13573 {
13574 if (wp == NULL)
13575 {
13576 /* didn't find it in this tabpage */
13577 nr = 0;
13578 break;
13579 }
13580 ++nr;
13581 }
13582 return nr;
13583}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013584
13585/*
13586 * "tabpagewinnr()" function
13587 */
13588 static void
13589f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13590{
13591 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592 tabpage_T *tp;
13593
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013594 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013595 if (tp == NULL)
13596 nr = 0;
13597 else
13598 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013599 rettv->vval.v_number = nr;
13600}
13601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013602/*
13603 * "tagfiles()" function
13604 */
13605 static void
13606f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13607{
13608 char_u *fname;
13609 tagname_T tn;
13610 int first;
13611
13612 if (rettv_list_alloc(rettv) == FAIL)
13613 return;
13614 fname = alloc(MAXPATHL);
13615 if (fname == NULL)
13616 return;
13617
13618 for (first = TRUE; ; first = FALSE)
13619 if (get_tagfname(&tn, first, fname) == FAIL
13620 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13621 break;
13622 tagname_free(&tn);
13623 vim_free(fname);
13624}
13625
13626/*
13627 * "taglist()" function
13628 */
13629 static void
13630f_taglist(typval_T *argvars, typval_T *rettv)
13631{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013632 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013633 char_u *tag_pattern;
13634
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013635 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013636
13637 rettv->vval.v_number = FALSE;
13638 if (*tag_pattern == NUL)
13639 return;
13640
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013641 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013642 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013643 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013644 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013645}
13646
13647/*
13648 * "tempname()" function
13649 */
13650 static void
13651f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13652{
13653 static int x = 'A';
13654
13655 rettv->v_type = VAR_STRING;
13656 rettv->vval.v_string = vim_tempname(x, FALSE);
13657
13658 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13659 * names. Skip 'I' and 'O', they are used for shell redirection. */
13660 do
13661 {
13662 if (x == 'Z')
13663 x = '0';
13664 else if (x == '9')
13665 x = 'A';
13666 else
13667 {
13668#ifdef EBCDIC
13669 if (x == 'I')
13670 x = 'J';
13671 else if (x == 'R')
13672 x = 'S';
13673 else
13674#endif
13675 ++x;
13676 }
13677 } while (x == 'I' || x == 'O');
13678}
13679
13680#ifdef FEAT_FLOAT
13681/*
13682 * "tan()" function
13683 */
13684 static void
13685f_tan(typval_T *argvars, typval_T *rettv)
13686{
13687 float_T f = 0.0;
13688
13689 rettv->v_type = VAR_FLOAT;
13690 if (get_float_arg(argvars, &f) == OK)
13691 rettv->vval.v_float = tan(f);
13692 else
13693 rettv->vval.v_float = 0.0;
13694}
13695
13696/*
13697 * "tanh()" function
13698 */
13699 static void
13700f_tanh(typval_T *argvars, typval_T *rettv)
13701{
13702 float_T f = 0.0;
13703
13704 rettv->v_type = VAR_FLOAT;
13705 if (get_float_arg(argvars, &f) == OK)
13706 rettv->vval.v_float = tanh(f);
13707 else
13708 rettv->vval.v_float = 0.0;
13709}
13710#endif
13711
13712/*
13713 * "test_alloc_fail(id, countdown, repeat)" function
13714 */
13715 static void
13716f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13717{
13718 if (argvars[0].v_type != VAR_NUMBER
13719 || argvars[0].vval.v_number <= 0
13720 || argvars[1].v_type != VAR_NUMBER
13721 || argvars[1].vval.v_number < 0
13722 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013723 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 else
13725 {
13726 alloc_fail_id = argvars[0].vval.v_number;
13727 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013728 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013729 alloc_fail_countdown = argvars[1].vval.v_number;
13730 alloc_fail_repeat = argvars[2].vval.v_number;
13731 did_outofmem_msg = FALSE;
13732 }
13733}
13734
13735/*
13736 * "test_autochdir()"
13737 */
13738 static void
13739f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13740{
13741#if defined(FEAT_AUTOCHDIR)
13742 test_autochdir = TRUE;
13743#endif
13744}
13745
13746/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013747 * "test_feedinput()"
13748 */
13749 static void
13750f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13751{
13752#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013753 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013754
13755 if (val != NULL)
13756 {
13757 trash_input_buf();
13758 add_to_input_buf_csi(val, (int)STRLEN(val));
13759 }
13760#endif
13761}
13762
13763/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013764 * "test_option_not_set({name})" function
13765 */
13766 static void
13767f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13768{
13769 char_u *name = (char_u *)"";
13770
13771 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013772 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013773 else
13774 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013775 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013776 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013777 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013778 }
13779}
13780
13781/*
13782 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013783 */
13784 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013785f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013786{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013787 char_u *name = (char_u *)"";
13788 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013789 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013790
13791 if (argvars[0].v_type != VAR_STRING
13792 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013793 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013794 else
13795 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013796 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013797 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013798
13799 if (STRCMP(name, (char_u *)"redraw") == 0)
13800 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013801 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13802 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013803 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13804 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013805 else if (STRCMP(name, (char_u *)"starting") == 0)
13806 {
13807 if (val)
13808 {
13809 if (save_starting < 0)
13810 save_starting = starting;
13811 starting = 0;
13812 }
13813 else
13814 {
13815 starting = save_starting;
13816 save_starting = -1;
13817 }
13818 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013819 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13820 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013821 else if (STRCMP(name, (char_u *)"ALL") == 0)
13822 {
13823 disable_char_avail_for_testing = FALSE;
13824 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013825 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013826 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013827 if (save_starting >= 0)
13828 {
13829 starting = save_starting;
13830 save_starting = -1;
13831 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013832 }
13833 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013834 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013835 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013836}
13837
13838/*
13839 * "test_garbagecollect_now()" function
13840 */
13841 static void
13842f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13843{
13844 /* This is dangerous, any Lists and Dicts used internally may be freed
13845 * while still in use. */
13846 garbage_collect(TRUE);
13847}
13848
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013849/*
13850 * "test_ignore_error()" function
13851 */
13852 static void
13853f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13854{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013855 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013856}
13857
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013858 static void
13859f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13860{
13861 rettv->v_type = VAR_BLOB;
13862 rettv->vval.v_blob = NULL;
13863}
13864
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013865#ifdef FEAT_JOB_CHANNEL
13866 static void
13867f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13868{
13869 rettv->v_type = VAR_CHANNEL;
13870 rettv->vval.v_channel = NULL;
13871}
13872#endif
13873
13874 static void
13875f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13876{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013877 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013878}
13879
13880#ifdef FEAT_JOB_CHANNEL
13881 static void
13882f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13883{
13884 rettv->v_type = VAR_JOB;
13885 rettv->vval.v_job = NULL;
13886}
13887#endif
13888
13889 static void
13890f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13891{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013892 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893}
13894
13895 static void
13896f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13897{
13898 rettv->v_type = VAR_PARTIAL;
13899 rettv->vval.v_partial = NULL;
13900}
13901
13902 static void
13903f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13904{
13905 rettv->v_type = VAR_STRING;
13906 rettv->vval.v_string = NULL;
13907}
13908
Bram Moolenaarab186732018-09-14 21:27:06 +020013909#ifdef FEAT_GUI
13910 static void
13911f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13912{
13913 char_u *which;
13914 long value;
13915 int dragging;
13916 scrollbar_T *sb = NULL;
13917
13918 if (argvars[0].v_type != VAR_STRING
13919 || (argvars[1].v_type) != VAR_NUMBER
13920 || (argvars[2].v_type) != VAR_NUMBER)
13921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013922 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013923 return;
13924 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013925 which = tv_get_string(&argvars[0]);
13926 value = tv_get_number(&argvars[1]);
13927 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013928
13929 if (STRCMP(which, "left") == 0)
13930 sb = &curwin->w_scrollbars[SBAR_LEFT];
13931 else if (STRCMP(which, "right") == 0)
13932 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13933 else if (STRCMP(which, "hor") == 0)
13934 sb = &gui.bottom_sbar;
13935 if (sb == NULL)
13936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013937 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020013938 return;
13939 }
13940 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020013941# ifndef USE_ON_FLY_SCROLL
13942 // need to loop through normal_cmd() to handle the scroll events
13943 exec_normal(FALSE, TRUE, FALSE);
13944# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020013945}
13946#endif
13947
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013948 static void
13949f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
13950{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013951 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952}
13953
13954#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
13955/*
13956 * Get a callback from "arg". It can be a Funcref or a function name.
13957 * When "arg" is zero return an empty string.
13958 * Return NULL for an invalid argument.
13959 */
13960 char_u *
13961get_callback(typval_T *arg, partial_T **pp)
13962{
13963 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
13964 {
13965 *pp = arg->vval.v_partial;
13966 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013967 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968 }
13969 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020013970 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971 {
13972 func_ref(arg->vval.v_string);
13973 return arg->vval.v_string;
13974 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013975 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
13976 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013977 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013978 return NULL;
13979}
13980
13981/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020013982 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013983 */
13984 void
13985free_callback(char_u *callback, partial_T *partial)
13986{
13987 if (partial != NULL)
13988 partial_unref(partial);
13989 else if (callback != NULL)
13990 {
13991 func_unref(callback);
13992 vim_free(callback);
13993 }
13994}
13995#endif
13996
13997#ifdef FEAT_TIMERS
13998/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020013999 * "timer_info([timer])" function
14000 */
14001 static void
14002f_timer_info(typval_T *argvars, typval_T *rettv)
14003{
14004 timer_T *timer = NULL;
14005
14006 if (rettv_list_alloc(rettv) != OK)
14007 return;
14008 if (argvars[0].v_type != VAR_UNKNOWN)
14009 {
14010 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014011 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014012 else
14013 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014014 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014015 if (timer != NULL)
14016 add_timer_info(rettv, timer);
14017 }
14018 }
14019 else
14020 add_timer_info_all(rettv);
14021}
14022
14023/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014024 * "timer_pause(timer, paused)" function
14025 */
14026 static void
14027f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14028{
14029 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014030 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014031
14032 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014033 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014034 else
14035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014036 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014037 if (timer != NULL)
14038 timer->tr_paused = paused;
14039 }
14040}
14041
14042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014043 * "timer_start(time, callback [, options])" function
14044 */
14045 static void
14046f_timer_start(typval_T *argvars, typval_T *rettv)
14047{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014048 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014049 timer_T *timer;
14050 int repeat = 0;
14051 char_u *callback;
14052 dict_T *dict;
14053 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054
Bram Moolenaar75537a92016-09-05 22:45:28 +020014055 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056 if (check_secure())
14057 return;
14058 if (argvars[2].v_type != VAR_UNKNOWN)
14059 {
14060 if (argvars[2].v_type != VAR_DICT
14061 || (dict = argvars[2].vval.v_dict) == NULL)
14062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014063 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014064 return;
14065 }
14066 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014067 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068 }
14069
Bram Moolenaar75537a92016-09-05 22:45:28 +020014070 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014071 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014072 return;
14073
14074 timer = create_timer(msec, repeat);
14075 if (timer == NULL)
14076 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 else
14078 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014079 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014080 timer->tr_callback = vim_strsave(callback);
14081 else
14082 /* pointer into the partial */
14083 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014084 timer->tr_partial = partial;
14085 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 }
14087}
14088
14089/*
14090 * "timer_stop(timer)" function
14091 */
14092 static void
14093f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14094{
14095 timer_T *timer;
14096
14097 if (argvars[0].v_type != VAR_NUMBER)
14098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014099 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014100 return;
14101 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014102 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014103 if (timer != NULL)
14104 stop_timer(timer);
14105}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014106
14107/*
14108 * "timer_stopall()" function
14109 */
14110 static void
14111f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14112{
14113 stop_all_timers();
14114}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115#endif
14116
14117/*
14118 * "tolower(string)" function
14119 */
14120 static void
14121f_tolower(typval_T *argvars, typval_T *rettv)
14122{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014124 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014125}
14126
14127/*
14128 * "toupper(string)" function
14129 */
14130 static void
14131f_toupper(typval_T *argvars, typval_T *rettv)
14132{
14133 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014134 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135}
14136
14137/*
14138 * "tr(string, fromstr, tostr)" function
14139 */
14140 static void
14141f_tr(typval_T *argvars, typval_T *rettv)
14142{
14143 char_u *in_str;
14144 char_u *fromstr;
14145 char_u *tostr;
14146 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 int inlen;
14148 int fromlen;
14149 int tolen;
14150 int idx;
14151 char_u *cpstr;
14152 int cplen;
14153 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014154 char_u buf[NUMBUFLEN];
14155 char_u buf2[NUMBUFLEN];
14156 garray_T ga;
14157
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014158 in_str = tv_get_string(&argvars[0]);
14159 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14160 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014161
14162 /* Default return value: empty string. */
14163 rettv->v_type = VAR_STRING;
14164 rettv->vval.v_string = NULL;
14165 if (fromstr == NULL || tostr == NULL)
14166 return; /* type error; errmsg already given */
14167 ga_init2(&ga, (int)sizeof(char), 80);
14168
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014169 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014170 /* not multi-byte: fromstr and tostr must be the same length */
14171 if (STRLEN(fromstr) != STRLEN(tostr))
14172 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014173error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014174 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014175 ga_clear(&ga);
14176 return;
14177 }
14178
14179 /* fromstr and tostr have to contain the same number of chars */
14180 while (*in_str != NUL)
14181 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182 if (has_mbyte)
14183 {
14184 inlen = (*mb_ptr2len)(in_str);
14185 cpstr = in_str;
14186 cplen = inlen;
14187 idx = 0;
14188 for (p = fromstr; *p != NUL; p += fromlen)
14189 {
14190 fromlen = (*mb_ptr2len)(p);
14191 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14192 {
14193 for (p = tostr; *p != NUL; p += tolen)
14194 {
14195 tolen = (*mb_ptr2len)(p);
14196 if (idx-- == 0)
14197 {
14198 cplen = tolen;
14199 cpstr = p;
14200 break;
14201 }
14202 }
14203 if (*p == NUL) /* tostr is shorter than fromstr */
14204 goto error;
14205 break;
14206 }
14207 ++idx;
14208 }
14209
14210 if (first && cpstr == in_str)
14211 {
14212 /* Check that fromstr and tostr have the same number of
14213 * (multi-byte) characters. Done only once when a character
14214 * of in_str doesn't appear in fromstr. */
14215 first = FALSE;
14216 for (p = tostr; *p != NUL; p += tolen)
14217 {
14218 tolen = (*mb_ptr2len)(p);
14219 --idx;
14220 }
14221 if (idx != 0)
14222 goto error;
14223 }
14224
14225 (void)ga_grow(&ga, cplen);
14226 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14227 ga.ga_len += cplen;
14228
14229 in_str += inlen;
14230 }
14231 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014232 {
14233 /* When not using multi-byte chars we can do it faster. */
14234 p = vim_strchr(fromstr, *in_str);
14235 if (p != NULL)
14236 ga_append(&ga, tostr[p - fromstr]);
14237 else
14238 ga_append(&ga, *in_str);
14239 ++in_str;
14240 }
14241 }
14242
14243 /* add a terminating NUL */
14244 (void)ga_grow(&ga, 1);
14245 ga_append(&ga, NUL);
14246
14247 rettv->vval.v_string = ga.ga_data;
14248}
14249
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014250/*
14251 * "trim({expr})" function
14252 */
14253 static void
14254f_trim(typval_T *argvars, typval_T *rettv)
14255{
14256 char_u buf1[NUMBUFLEN];
14257 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014258 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014259 char_u *mask = NULL;
14260 char_u *tail;
14261 char_u *prev;
14262 char_u *p;
14263 int c1;
14264
14265 rettv->v_type = VAR_STRING;
14266 if (head == NULL)
14267 {
14268 rettv->vval.v_string = NULL;
14269 return;
14270 }
14271
14272 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014273 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014274
14275 while (*head != NUL)
14276 {
14277 c1 = PTR2CHAR(head);
14278 if (mask == NULL)
14279 {
14280 if (c1 > ' ' && c1 != 0xa0)
14281 break;
14282 }
14283 else
14284 {
14285 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14286 if (c1 == PTR2CHAR(p))
14287 break;
14288 if (*p == NUL)
14289 break;
14290 }
14291 MB_PTR_ADV(head);
14292 }
14293
14294 for (tail = head + STRLEN(head); tail > head; tail = prev)
14295 {
14296 prev = tail;
14297 MB_PTR_BACK(head, prev);
14298 c1 = PTR2CHAR(prev);
14299 if (mask == NULL)
14300 {
14301 if (c1 > ' ' && c1 != 0xa0)
14302 break;
14303 }
14304 else
14305 {
14306 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14307 if (c1 == PTR2CHAR(p))
14308 break;
14309 if (*p == NUL)
14310 break;
14311 }
14312 }
14313 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14314}
14315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014316#ifdef FEAT_FLOAT
14317/*
14318 * "trunc({float})" function
14319 */
14320 static void
14321f_trunc(typval_T *argvars, typval_T *rettv)
14322{
14323 float_T f = 0.0;
14324
14325 rettv->v_type = VAR_FLOAT;
14326 if (get_float_arg(argvars, &f) == OK)
14327 /* trunc() is not in C90, use floor() or ceil() instead. */
14328 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14329 else
14330 rettv->vval.v_float = 0.0;
14331}
14332#endif
14333
14334/*
14335 * "type(expr)" function
14336 */
14337 static void
14338f_type(typval_T *argvars, typval_T *rettv)
14339{
14340 int n = -1;
14341
14342 switch (argvars[0].v_type)
14343 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014344 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14345 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014346 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014347 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14348 case VAR_LIST: n = VAR_TYPE_LIST; break;
14349 case VAR_DICT: n = VAR_TYPE_DICT; break;
14350 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014351 case VAR_SPECIAL:
14352 if (argvars[0].vval.v_number == VVAL_FALSE
14353 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014354 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014356 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014357 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014358 case VAR_JOB: n = VAR_TYPE_JOB; break;
14359 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014360 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014361 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014362 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014363 n = -1;
14364 break;
14365 }
14366 rettv->vval.v_number = n;
14367}
14368
14369/*
14370 * "undofile(name)" function
14371 */
14372 static void
14373f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14374{
14375 rettv->v_type = VAR_STRING;
14376#ifdef FEAT_PERSISTENT_UNDO
14377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014378 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014379
14380 if (*fname == NUL)
14381 {
14382 /* If there is no file name there will be no undo file. */
14383 rettv->vval.v_string = NULL;
14384 }
14385 else
14386 {
14387 char_u *ffname = FullName_save(fname, FALSE);
14388
14389 if (ffname != NULL)
14390 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14391 vim_free(ffname);
14392 }
14393 }
14394#else
14395 rettv->vval.v_string = NULL;
14396#endif
14397}
14398
14399/*
14400 * "undotree()" function
14401 */
14402 static void
14403f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14404{
14405 if (rettv_dict_alloc(rettv) == OK)
14406 {
14407 dict_T *dict = rettv->vval.v_dict;
14408 list_T *list;
14409
Bram Moolenaare0be1672018-07-08 16:50:37 +020014410 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14411 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14412 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14413 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14414 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14415 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014416
14417 list = list_alloc();
14418 if (list != NULL)
14419 {
14420 u_eval_tree(curbuf->b_u_oldhead, list);
14421 dict_add_list(dict, "entries", list);
14422 }
14423 }
14424}
14425
14426/*
14427 * "values(dict)" function
14428 */
14429 static void
14430f_values(typval_T *argvars, typval_T *rettv)
14431{
14432 dict_list(argvars, rettv, 1);
14433}
14434
14435/*
14436 * "virtcol(string)" function
14437 */
14438 static void
14439f_virtcol(typval_T *argvars, typval_T *rettv)
14440{
14441 colnr_T vcol = 0;
14442 pos_T *fp;
14443 int fnum = curbuf->b_fnum;
14444
14445 fp = var2fpos(&argvars[0], FALSE, &fnum);
14446 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14447 && fnum == curbuf->b_fnum)
14448 {
14449 getvvcol(curwin, fp, NULL, NULL, &vcol);
14450 ++vcol;
14451 }
14452
14453 rettv->vval.v_number = vcol;
14454}
14455
14456/*
14457 * "visualmode()" function
14458 */
14459 static void
14460f_visualmode(typval_T *argvars, typval_T *rettv)
14461{
14462 char_u str[2];
14463
14464 rettv->v_type = VAR_STRING;
14465 str[0] = curbuf->b_visual_mode_eval;
14466 str[1] = NUL;
14467 rettv->vval.v_string = vim_strsave(str);
14468
14469 /* A non-zero number or non-empty string argument: reset mode. */
14470 if (non_zero_arg(&argvars[0]))
14471 curbuf->b_visual_mode_eval = NUL;
14472}
14473
14474/*
14475 * "wildmenumode()" function
14476 */
14477 static void
14478f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14479{
14480#ifdef FEAT_WILDMENU
14481 if (wild_menu_showing)
14482 rettv->vval.v_number = 1;
14483#endif
14484}
14485
14486/*
14487 * "winbufnr(nr)" function
14488 */
14489 static void
14490f_winbufnr(typval_T *argvars, typval_T *rettv)
14491{
14492 win_T *wp;
14493
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014494 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014495 if (wp == NULL)
14496 rettv->vval.v_number = -1;
14497 else
14498 rettv->vval.v_number = wp->w_buffer->b_fnum;
14499}
14500
14501/*
14502 * "wincol()" function
14503 */
14504 static void
14505f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14506{
14507 validate_cursor();
14508 rettv->vval.v_number = curwin->w_wcol + 1;
14509}
14510
14511/*
14512 * "winheight(nr)" function
14513 */
14514 static void
14515f_winheight(typval_T *argvars, typval_T *rettv)
14516{
14517 win_T *wp;
14518
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014519 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014520 if (wp == NULL)
14521 rettv->vval.v_number = -1;
14522 else
14523 rettv->vval.v_number = wp->w_height;
14524}
14525
14526/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014527 * "winlayout()" function
14528 */
14529 static void
14530f_winlayout(typval_T *argvars, typval_T *rettv)
14531{
14532 tabpage_T *tp;
14533
14534 if (rettv_list_alloc(rettv) != OK)
14535 return;
14536
14537 if (argvars[0].v_type == VAR_UNKNOWN)
14538 tp = curtab;
14539 else
14540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014541 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014542 if (tp == NULL)
14543 return;
14544 }
14545
14546 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14547}
14548
14549/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014550 * "winline()" function
14551 */
14552 static void
14553f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14554{
14555 validate_cursor();
14556 rettv->vval.v_number = curwin->w_wrow + 1;
14557}
14558
14559/*
14560 * "winnr()" function
14561 */
14562 static void
14563f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14564{
14565 int nr = 1;
14566
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014567 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568 rettv->vval.v_number = nr;
14569}
14570
14571/*
14572 * "winrestcmd()" function
14573 */
14574 static void
14575f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14576{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014577 win_T *wp;
14578 int winnr = 1;
14579 garray_T ga;
14580 char_u buf[50];
14581
14582 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014583 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584 {
14585 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14586 ga_concat(&ga, buf);
14587 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14588 ga_concat(&ga, buf);
14589 ++winnr;
14590 }
14591 ga_append(&ga, NUL);
14592
14593 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014594 rettv->v_type = VAR_STRING;
14595}
14596
14597/*
14598 * "winrestview()" function
14599 */
14600 static void
14601f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14602{
14603 dict_T *dict;
14604
14605 if (argvars[0].v_type != VAR_DICT
14606 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014607 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 else
14609 {
14610 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014611 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014612 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014613 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014614#ifdef FEAT_VIRTUALEDIT
14615 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014616 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617#endif
14618 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14619 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014620 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014621 curwin->w_set_curswant = FALSE;
14622 }
14623
14624 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014625 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014626#ifdef FEAT_DIFF
14627 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014628 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014629#endif
14630 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014631 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014632 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014633 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014634
14635 check_cursor();
14636 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014637 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014638 changed_window_setting();
14639
14640 if (curwin->w_topline <= 0)
14641 curwin->w_topline = 1;
14642 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14643 curwin->w_topline = curbuf->b_ml.ml_line_count;
14644#ifdef FEAT_DIFF
14645 check_topfill(curwin, TRUE);
14646#endif
14647 }
14648}
14649
14650/*
14651 * "winsaveview()" function
14652 */
14653 static void
14654f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14655{
14656 dict_T *dict;
14657
14658 if (rettv_dict_alloc(rettv) == FAIL)
14659 return;
14660 dict = rettv->vval.v_dict;
14661
Bram Moolenaare0be1672018-07-08 16:50:37 +020014662 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14663 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014665 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666#endif
14667 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014668 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014669
Bram Moolenaare0be1672018-07-08 16:50:37 +020014670 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014671#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014672 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014673#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014674 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14675 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676}
14677
14678/*
14679 * "winwidth(nr)" function
14680 */
14681 static void
14682f_winwidth(typval_T *argvars, typval_T *rettv)
14683{
14684 win_T *wp;
14685
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014686 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687 if (wp == NULL)
14688 rettv->vval.v_number = -1;
14689 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014690 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691}
14692
14693/*
14694 * "wordcount()" function
14695 */
14696 static void
14697f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14698{
14699 if (rettv_dict_alloc(rettv) == FAIL)
14700 return;
14701 cursor_pos_info(rettv->vval.v_dict);
14702}
14703
14704/*
14705 * "writefile()" function
14706 */
14707 static void
14708f_writefile(typval_T *argvars, typval_T *rettv)
14709{
14710 int binary = FALSE;
14711 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014712#ifdef HAVE_FSYNC
14713 int do_fsync = p_fs;
14714#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014715 char_u *fname;
14716 FILE *fd;
14717 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014718 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014719 list_T *list = NULL;
14720 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014721
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014722 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014723 if (check_restricted() || check_secure())
14724 return;
14725
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014726 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014727 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014728 list = argvars[0].vval.v_list;
14729 if (list == NULL)
14730 return;
14731 for (li = list->lv_first; li != NULL; li = li->li_next)
14732 if (tv_get_string_chk(&li->li_tv) == NULL)
14733 return;
14734 }
14735 else if (argvars[0].v_type == VAR_BLOB)
14736 {
14737 blob = argvars[0].vval.v_blob;
14738 if (blob == NULL)
14739 return;
14740 }
14741 else
14742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014743 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014744 return;
14745 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014746
14747 if (argvars[2].v_type != VAR_UNKNOWN)
14748 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014749 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014750
14751 if (arg2 == NULL)
14752 return;
14753 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014754 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014755 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014757#ifdef HAVE_FSYNC
14758 if (vim_strchr(arg2, 's') != NULL)
14759 do_fsync = TRUE;
14760 else if (vim_strchr(arg2, 'S') != NULL)
14761 do_fsync = FALSE;
14762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763 }
14764
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014765 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014766 if (fname == NULL)
14767 return;
14768
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014769 /* Always open the file in binary mode, library functions have a mind of
14770 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014771 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14772 append ? APPENDBIN : WRITEBIN)) == NULL)
14773 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014774 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014775 ret = -1;
14776 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014777 else if (blob)
14778 {
14779 if (write_blob(fd, blob) == FAIL)
14780 ret = -1;
14781#ifdef HAVE_FSYNC
14782 else if (do_fsync)
14783 // Ignore the error, the user wouldn't know what to do about it.
14784 // May happen for a device.
14785 vim_ignored = fsync(fileno(fd));
14786#endif
14787 fclose(fd);
14788 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014789 else
14790 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014791 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014793#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014794 else if (do_fsync)
14795 /* Ignore the error, the user wouldn't know what to do about it.
14796 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014797 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014799 fclose(fd);
14800 }
14801
14802 rettv->vval.v_number = ret;
14803}
14804
14805/*
14806 * "xor(expr, expr)" function
14807 */
14808 static void
14809f_xor(typval_T *argvars, typval_T *rettv)
14810{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014811 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14812 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014813}
14814
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815#endif /* FEAT_EVAL */