blob: e0f5d6e7311317bd9ac18cada1ed86e28ab7ac06 [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{
2074#ifdef FEAT_MBYTE
2075 char_u *t;
2076#endif
2077 char_u *str;
2078 varnumber_T idx;
2079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002080 str = tv_get_string_chk(&argvars[0]);
2081 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 rettv->vval.v_number = -1;
2083 if (str == NULL || idx < 0)
2084 return;
2085
2086#ifdef FEAT_MBYTE
2087 t = str;
2088 for ( ; idx > 0; idx--)
2089 {
2090 if (*t == NUL) /* EOL reached */
2091 return;
2092 if (enc_utf8 && comp)
2093 t += utf_ptr2len(t);
2094 else
2095 t += (*mb_ptr2len)(t);
2096 }
2097 rettv->vval.v_number = (varnumber_T)(t - str);
2098#else
2099 if ((size_t)idx <= STRLEN(str))
2100 rettv->vval.v_number = idx;
2101#endif
2102}
2103
2104/*
2105 * "byteidx()" function
2106 */
2107 static void
2108f_byteidx(typval_T *argvars, typval_T *rettv)
2109{
2110 byteidx(argvars, rettv, FALSE);
2111}
2112
2113/*
2114 * "byteidxcomp()" function
2115 */
2116 static void
2117f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2118{
2119 byteidx(argvars, rettv, TRUE);
2120}
2121
2122/*
2123 * "call(func, arglist [, dict])" function
2124 */
2125 static void
2126f_call(typval_T *argvars, typval_T *rettv)
2127{
2128 char_u *func;
2129 partial_T *partial = NULL;
2130 dict_T *selfdict = NULL;
2131
2132 if (argvars[1].v_type != VAR_LIST)
2133 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002134 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002135 return;
2136 }
2137 if (argvars[1].vval.v_list == NULL)
2138 return;
2139
2140 if (argvars[0].v_type == VAR_FUNC)
2141 func = argvars[0].vval.v_string;
2142 else if (argvars[0].v_type == VAR_PARTIAL)
2143 {
2144 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002145 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002146 }
2147 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002148 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 if (*func == NUL)
2150 return; /* type error or empty name */
2151
2152 if (argvars[2].v_type != VAR_UNKNOWN)
2153 {
2154 if (argvars[2].v_type != VAR_DICT)
2155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 return;
2158 }
2159 selfdict = argvars[2].vval.v_dict;
2160 }
2161
2162 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2163}
2164
2165#ifdef FEAT_FLOAT
2166/*
2167 * "ceil({float})" function
2168 */
2169 static void
2170f_ceil(typval_T *argvars, typval_T *rettv)
2171{
2172 float_T f = 0.0;
2173
2174 rettv->v_type = VAR_FLOAT;
2175 if (get_float_arg(argvars, &f) == OK)
2176 rettv->vval.v_float = ceil(f);
2177 else
2178 rettv->vval.v_float = 0.0;
2179}
2180#endif
2181
2182#ifdef FEAT_JOB_CHANNEL
2183/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002184 * "ch_canread()" function
2185 */
2186 static void
2187f_ch_canread(typval_T *argvars, typval_T *rettv)
2188{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002189 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002190
2191 rettv->vval.v_number = 0;
2192 if (channel != NULL)
2193 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2194 || channel_has_readahead(channel, PART_OUT)
2195 || channel_has_readahead(channel, PART_ERR);
2196}
2197
2198/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002199 * "ch_close()" function
2200 */
2201 static void
2202f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2203{
2204 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2205
2206 if (channel != NULL)
2207 {
2208 channel_close(channel, FALSE);
2209 channel_clear(channel);
2210 }
2211}
2212
2213/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002214 * "ch_close()" function
2215 */
2216 static void
2217f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2218{
2219 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2220
2221 if (channel != NULL)
2222 channel_close_in(channel);
2223}
2224
2225/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 * "ch_getbufnr()" function
2227 */
2228 static void
2229f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2230{
2231 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2232
2233 rettv->vval.v_number = -1;
2234 if (channel != NULL)
2235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002236 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002237 int part;
2238
2239 if (STRCMP(what, "err") == 0)
2240 part = PART_ERR;
2241 else if (STRCMP(what, "out") == 0)
2242 part = PART_OUT;
2243 else if (STRCMP(what, "in") == 0)
2244 part = PART_IN;
2245 else
2246 part = PART_SOCK;
2247 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2248 rettv->vval.v_number =
2249 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2250 }
2251}
2252
2253/*
2254 * "ch_getjob()" function
2255 */
2256 static void
2257f_ch_getjob(typval_T *argvars, typval_T *rettv)
2258{
2259 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2260
2261 if (channel != NULL)
2262 {
2263 rettv->v_type = VAR_JOB;
2264 rettv->vval.v_job = channel->ch_job;
2265 if (channel->ch_job != NULL)
2266 ++channel->ch_job->jv_refcount;
2267 }
2268}
2269
2270/*
2271 * "ch_info()" function
2272 */
2273 static void
2274f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2275{
2276 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2277
2278 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2279 channel_info(channel, rettv->vval.v_dict);
2280}
2281
2282/*
2283 * "ch_log()" function
2284 */
2285 static void
2286f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2287{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002288 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002289 channel_T *channel = NULL;
2290
2291 if (argvars[1].v_type != VAR_UNKNOWN)
2292 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2293
Bram Moolenaard5359b22018-04-05 22:44:39 +02002294 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295}
2296
2297/*
2298 * "ch_logfile()" function
2299 */
2300 static void
2301f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2302{
2303 char_u *fname;
2304 char_u *opt = (char_u *)"";
2305 char_u buf[NUMBUFLEN];
2306
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002307 /* Don't open a file in restricted mode. */
2308 if (check_restricted() || check_secure())
2309 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002310 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002311 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002312 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 ch_logfile(fname, opt);
2314}
2315
2316/*
2317 * "ch_open()" function
2318 */
2319 static void
2320f_ch_open(typval_T *argvars, typval_T *rettv)
2321{
2322 rettv->v_type = VAR_CHANNEL;
2323 if (check_restricted() || check_secure())
2324 return;
2325 rettv->vval.v_channel = channel_open_func(argvars);
2326}
2327
2328/*
2329 * "ch_read()" function
2330 */
2331 static void
2332f_ch_read(typval_T *argvars, typval_T *rettv)
2333{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002334 common_channel_read(argvars, rettv, FALSE, FALSE);
2335}
2336
2337/*
2338 * "ch_readblob()" function
2339 */
2340 static void
2341f_ch_readblob(typval_T *argvars, typval_T *rettv)
2342{
2343 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002344}
2345
2346/*
2347 * "ch_readraw()" function
2348 */
2349 static void
2350f_ch_readraw(typval_T *argvars, typval_T *rettv)
2351{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002352 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002353}
2354
2355/*
2356 * "ch_evalexpr()" function
2357 */
2358 static void
2359f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2360{
2361 ch_expr_common(argvars, rettv, TRUE);
2362}
2363
2364/*
2365 * "ch_sendexpr()" function
2366 */
2367 static void
2368f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2369{
2370 ch_expr_common(argvars, rettv, FALSE);
2371}
2372
2373/*
2374 * "ch_evalraw()" function
2375 */
2376 static void
2377f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2378{
2379 ch_raw_common(argvars, rettv, TRUE);
2380}
2381
2382/*
2383 * "ch_sendraw()" function
2384 */
2385 static void
2386f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2387{
2388 ch_raw_common(argvars, rettv, FALSE);
2389}
2390
2391/*
2392 * "ch_setoptions()" function
2393 */
2394 static void
2395f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2396{
2397 channel_T *channel;
2398 jobopt_T opt;
2399
2400 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2401 if (channel == NULL)
2402 return;
2403 clear_job_options(&opt);
2404 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002405 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406 channel_set_options(channel, &opt);
2407 free_job_options(&opt);
2408}
2409
2410/*
2411 * "ch_status()" function
2412 */
2413 static void
2414f_ch_status(typval_T *argvars, typval_T *rettv)
2415{
2416 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002417 jobopt_T opt;
2418 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002419
2420 /* return an empty string by default */
2421 rettv->v_type = VAR_STRING;
2422 rettv->vval.v_string = NULL;
2423
2424 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002425
2426 if (argvars[1].v_type != VAR_UNKNOWN)
2427 {
2428 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002429 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002430 && (opt.jo_set & JO_PART))
2431 part = opt.jo_part;
2432 }
2433
2434 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435}
2436#endif
2437
2438/*
2439 * "changenr()" function
2440 */
2441 static void
2442f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2443{
2444 rettv->vval.v_number = curbuf->b_u_seq_cur;
2445}
2446
2447/*
2448 * "char2nr(string)" function
2449 */
2450 static void
2451f_char2nr(typval_T *argvars, typval_T *rettv)
2452{
2453#ifdef FEAT_MBYTE
2454 if (has_mbyte)
2455 {
2456 int utf8 = 0;
2457
2458 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002459 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002460
2461 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002462 rettv->vval.v_number = (*utf_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002463 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002464 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002465 }
2466 else
2467#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002468 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002469}
2470
2471/*
2472 * "cindent(lnum)" function
2473 */
2474 static void
2475f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2476{
2477#ifdef FEAT_CINDENT
2478 pos_T pos;
2479 linenr_T lnum;
2480
2481 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002482 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002483 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2484 {
2485 curwin->w_cursor.lnum = lnum;
2486 rettv->vval.v_number = get_c_indent();
2487 curwin->w_cursor = pos;
2488 }
2489 else
2490#endif
2491 rettv->vval.v_number = -1;
2492}
2493
2494/*
2495 * "clearmatches()" function
2496 */
2497 static void
2498f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2499{
2500#ifdef FEAT_SEARCH_EXTRA
2501 clear_matches(curwin);
2502#endif
2503}
2504
2505/*
2506 * "col(string)" function
2507 */
2508 static void
2509f_col(typval_T *argvars, typval_T *rettv)
2510{
2511 colnr_T col = 0;
2512 pos_T *fp;
2513 int fnum = curbuf->b_fnum;
2514
2515 fp = var2fpos(&argvars[0], FALSE, &fnum);
2516 if (fp != NULL && fnum == curbuf->b_fnum)
2517 {
2518 if (fp->col == MAXCOL)
2519 {
2520 /* '> can be MAXCOL, get the length of the line then */
2521 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2522 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2523 else
2524 col = MAXCOL;
2525 }
2526 else
2527 {
2528 col = fp->col + 1;
2529#ifdef FEAT_VIRTUALEDIT
2530 /* col(".") when the cursor is on the NUL at the end of the line
2531 * because of "coladd" can be seen as an extra column. */
2532 if (virtual_active() && fp == &curwin->w_cursor)
2533 {
2534 char_u *p = ml_get_cursor();
2535
2536 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2537 curwin->w_virtcol - curwin->w_cursor.coladd))
2538 {
2539# ifdef FEAT_MBYTE
2540 int l;
2541
2542 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2543 col += l;
2544# else
2545 if (*p != NUL && p[1] == NUL)
2546 ++col;
2547# endif
2548 }
2549 }
2550#endif
2551 }
2552 }
2553 rettv->vval.v_number = col;
2554}
2555
2556#if defined(FEAT_INS_EXPAND)
2557/*
2558 * "complete()" function
2559 */
2560 static void
2561f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2562{
2563 int startcol;
2564
2565 if ((State & INSERT) == 0)
2566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002567 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002568 return;
2569 }
2570
2571 /* Check for undo allowed here, because if something was already inserted
2572 * the line was already saved for undo and this check isn't done. */
2573 if (!undo_allowed())
2574 return;
2575
2576 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002578 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002579 return;
2580 }
2581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002582 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 if (startcol <= 0)
2584 return;
2585
2586 set_completion(startcol - 1, argvars[1].vval.v_list);
2587}
2588
2589/*
2590 * "complete_add()" function
2591 */
2592 static void
2593f_complete_add(typval_T *argvars, typval_T *rettv)
2594{
2595 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2596}
2597
2598/*
2599 * "complete_check()" function
2600 */
2601 static void
2602f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2603{
2604 int saved = RedrawingDisabled;
2605
2606 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002607 ins_compl_check_keys(0, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002608 rettv->vval.v_number = compl_interrupted;
2609 RedrawingDisabled = saved;
2610}
2611#endif
2612
2613/*
2614 * "confirm(message, buttons[, default [, type]])" function
2615 */
2616 static void
2617f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2618{
2619#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2620 char_u *message;
2621 char_u *buttons = NULL;
2622 char_u buf[NUMBUFLEN];
2623 char_u buf2[NUMBUFLEN];
2624 int def = 1;
2625 int type = VIM_GENERIC;
2626 char_u *typestr;
2627 int error = FALSE;
2628
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002629 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630 if (message == NULL)
2631 error = TRUE;
2632 if (argvars[1].v_type != VAR_UNKNOWN)
2633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002634 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002635 if (buttons == NULL)
2636 error = TRUE;
2637 if (argvars[2].v_type != VAR_UNKNOWN)
2638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002639 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002640 if (argvars[3].v_type != VAR_UNKNOWN)
2641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002642 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002643 if (typestr == NULL)
2644 error = TRUE;
2645 else
2646 {
2647 switch (TOUPPER_ASC(*typestr))
2648 {
2649 case 'E': type = VIM_ERROR; break;
2650 case 'Q': type = VIM_QUESTION; break;
2651 case 'I': type = VIM_INFO; break;
2652 case 'W': type = VIM_WARNING; break;
2653 case 'G': type = VIM_GENERIC; break;
2654 }
2655 }
2656 }
2657 }
2658 }
2659
2660 if (buttons == NULL || *buttons == NUL)
2661 buttons = (char_u *)_("&Ok");
2662
2663 if (!error)
2664 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2665 def, NULL, FALSE);
2666#endif
2667}
2668
2669/*
2670 * "copy()" function
2671 */
2672 static void
2673f_copy(typval_T *argvars, typval_T *rettv)
2674{
2675 item_copy(&argvars[0], rettv, FALSE, 0);
2676}
2677
2678#ifdef FEAT_FLOAT
2679/*
2680 * "cos()" function
2681 */
2682 static void
2683f_cos(typval_T *argvars, typval_T *rettv)
2684{
2685 float_T f = 0.0;
2686
2687 rettv->v_type = VAR_FLOAT;
2688 if (get_float_arg(argvars, &f) == OK)
2689 rettv->vval.v_float = cos(f);
2690 else
2691 rettv->vval.v_float = 0.0;
2692}
2693
2694/*
2695 * "cosh()" function
2696 */
2697 static void
2698f_cosh(typval_T *argvars, typval_T *rettv)
2699{
2700 float_T f = 0.0;
2701
2702 rettv->v_type = VAR_FLOAT;
2703 if (get_float_arg(argvars, &f) == OK)
2704 rettv->vval.v_float = cosh(f);
2705 else
2706 rettv->vval.v_float = 0.0;
2707}
2708#endif
2709
2710/*
2711 * "count()" function
2712 */
2713 static void
2714f_count(typval_T *argvars, typval_T *rettv)
2715{
2716 long n = 0;
2717 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002718 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002719
Bram Moolenaar9966b212017-07-28 16:46:57 +02002720 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002721 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002722
2723 if (argvars[0].v_type == VAR_STRING)
2724 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002725 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002726 char_u *p = argvars[0].vval.v_string;
2727 char_u *next;
2728
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002729 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002730 {
2731 if (ic)
2732 {
2733 size_t len = STRLEN(expr);
2734
2735 while (*p != NUL)
2736 {
2737 if (MB_STRNICMP(p, expr, len) == 0)
2738 {
2739 ++n;
2740 p += len;
2741 }
2742 else
2743 MB_PTR_ADV(p);
2744 }
2745 }
2746 else
2747 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2748 != NULL)
2749 {
2750 ++n;
2751 p = next + STRLEN(expr);
2752 }
2753 }
2754
2755 }
2756 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002757 {
2758 listitem_T *li;
2759 list_T *l;
2760 long idx;
2761
2762 if ((l = argvars[0].vval.v_list) != NULL)
2763 {
2764 li = l->lv_first;
2765 if (argvars[2].v_type != VAR_UNKNOWN)
2766 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767 if (argvars[3].v_type != VAR_UNKNOWN)
2768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002769 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 if (!error)
2771 {
2772 li = list_find(l, idx);
2773 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002774 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 }
2776 }
2777 if (error)
2778 li = NULL;
2779 }
2780
2781 for ( ; li != NULL; li = li->li_next)
2782 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2783 ++n;
2784 }
2785 }
2786 else if (argvars[0].v_type == VAR_DICT)
2787 {
2788 int todo;
2789 dict_T *d;
2790 hashitem_T *hi;
2791
2792 if ((d = argvars[0].vval.v_dict) != NULL)
2793 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002794 if (argvars[2].v_type != VAR_UNKNOWN)
2795 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002797 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 }
2799
2800 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2801 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2802 {
2803 if (!HASHITEM_EMPTY(hi))
2804 {
2805 --todo;
2806 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2807 ++n;
2808 }
2809 }
2810 }
2811 }
2812 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002813 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002814 rettv->vval.v_number = n;
2815}
2816
2817/*
2818 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2819 *
2820 * Checks the existence of a cscope connection.
2821 */
2822 static void
2823f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2824{
2825#ifdef FEAT_CSCOPE
2826 int num = 0;
2827 char_u *dbpath = NULL;
2828 char_u *prepend = NULL;
2829 char_u buf[NUMBUFLEN];
2830
2831 if (argvars[0].v_type != VAR_UNKNOWN
2832 && argvars[1].v_type != VAR_UNKNOWN)
2833 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002834 num = (int)tv_get_number(&argvars[0]);
2835 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002837 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002838 }
2839
2840 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2841#endif
2842}
2843
2844/*
2845 * "cursor(lnum, col)" function, or
2846 * "cursor(list)"
2847 *
2848 * Moves the cursor to the specified line and column.
2849 * Returns 0 when the position could be set, -1 otherwise.
2850 */
2851 static void
2852f_cursor(typval_T *argvars, typval_T *rettv)
2853{
2854 long line, col;
2855#ifdef FEAT_VIRTUALEDIT
2856 long coladd = 0;
2857#endif
2858 int set_curswant = TRUE;
2859
2860 rettv->vval.v_number = -1;
2861 if (argvars[1].v_type == VAR_UNKNOWN)
2862 {
2863 pos_T pos;
2864 colnr_T curswant = -1;
2865
2866 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
2867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002868 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002869 return;
2870 }
2871 line = pos.lnum;
2872 col = pos.col;
2873#ifdef FEAT_VIRTUALEDIT
2874 coladd = pos.coladd;
2875#endif
2876 if (curswant >= 0)
2877 {
2878 curwin->w_curswant = curswant - 1;
2879 set_curswant = FALSE;
2880 }
2881 }
2882 else
2883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002884 line = tv_get_lnum(argvars);
2885 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002886#ifdef FEAT_VIRTUALEDIT
2887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002888 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002889#endif
2890 }
2891 if (line < 0 || col < 0
2892#ifdef FEAT_VIRTUALEDIT
2893 || coladd < 0
2894#endif
2895 )
2896 return; /* type error; errmsg already given */
2897 if (line > 0)
2898 curwin->w_cursor.lnum = line;
2899 if (col > 0)
2900 curwin->w_cursor.col = col - 1;
2901#ifdef FEAT_VIRTUALEDIT
2902 curwin->w_cursor.coladd = coladd;
2903#endif
2904
2905 /* Make sure the cursor is in a valid position. */
2906 check_cursor();
2907#ifdef FEAT_MBYTE
2908 /* Correct cursor for multi-byte character. */
2909 if (has_mbyte)
2910 mb_adjust_cursor();
2911#endif
2912
2913 curwin->w_set_curswant = set_curswant;
2914 rettv->vval.v_number = 0;
2915}
2916
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002917#ifdef WIN3264
2918/*
2919 * "debugbreak()" function
2920 */
2921 static void
2922f_debugbreak(typval_T *argvars, typval_T *rettv)
2923{
2924 int pid;
2925
2926 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002927 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002928 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002929 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02002930 else
2931 {
2932 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
2933
2934 if (hProcess != NULL)
2935 {
2936 DebugBreakProcess(hProcess);
2937 CloseHandle(hProcess);
2938 rettv->vval.v_number = OK;
2939 }
2940 }
2941}
2942#endif
2943
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002944/*
2945 * "deepcopy()" function
2946 */
2947 static void
2948f_deepcopy(typval_T *argvars, typval_T *rettv)
2949{
2950 int noref = 0;
2951 int copyID;
2952
2953 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002954 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 else
2958 {
2959 copyID = get_copyID();
2960 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
2961 }
2962}
2963
2964/*
2965 * "delete()" function
2966 */
2967 static void
2968f_delete(typval_T *argvars, typval_T *rettv)
2969{
2970 char_u nbuf[NUMBUFLEN];
2971 char_u *name;
2972 char_u *flags;
2973
2974 rettv->vval.v_number = -1;
2975 if (check_restricted() || check_secure())
2976 return;
2977
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002978 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 if (name == NULL || *name == NUL)
2980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002981 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002982 return;
2983 }
2984
2985 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002986 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002987 else
2988 flags = (char_u *)"";
2989
2990 if (*flags == NUL)
2991 /* delete a file */
2992 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
2993 else if (STRCMP(flags, "d") == 0)
2994 /* delete an empty directory */
2995 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
2996 else if (STRCMP(flags, "rf") == 0)
2997 /* delete a directory recursively */
2998 rettv->vval.v_number = delete_recursive(name);
2999 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003000 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001}
3002
3003/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003004 * "deletebufline()" function
3005 */
3006 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003007f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003008{
3009 buf_T *buf;
3010 linenr_T first, last;
3011 linenr_T lnum;
3012 long count;
3013 int is_curbuf;
3014 buf_T *curbuf_save = NULL;
3015 win_T *curwin_save = NULL;
3016 tabpage_T *tp;
3017 win_T *wp;
3018
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003019 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003020 if (buf == NULL)
3021 {
3022 rettv->vval.v_number = 1; /* FAIL */
3023 return;
3024 }
3025 is_curbuf = buf == curbuf;
3026
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003027 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003028 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003029 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003030 else
3031 last = first;
3032
3033 if (buf->b_ml.ml_mfp == NULL || first < 1
3034 || first > buf->b_ml.ml_line_count || last < first)
3035 {
3036 rettv->vval.v_number = 1; /* FAIL */
3037 return;
3038 }
3039
3040 if (!is_curbuf)
3041 {
3042 curbuf_save = curbuf;
3043 curwin_save = curwin;
3044 curbuf = buf;
3045 find_win_for_curbuf();
3046 }
3047 if (last > curbuf->b_ml.ml_line_count)
3048 last = curbuf->b_ml.ml_line_count;
3049 count = last - first + 1;
3050
3051 // When coming here from Insert mode, sync undo, so that this can be
3052 // undone separately from what was previously inserted.
3053 if (u_sync_once == 2)
3054 {
3055 u_sync_once = 1; // notify that u_sync() was called
3056 u_sync(TRUE);
3057 }
3058
3059 if (u_save(first - 1, last + 1) == FAIL)
3060 {
3061 rettv->vval.v_number = 1; /* FAIL */
3062 return;
3063 }
3064
3065 for (lnum = first; lnum <= last; ++lnum)
3066 ml_delete(first, TRUE);
3067
3068 FOR_ALL_TAB_WINDOWS(tp, wp)
3069 if (wp->w_buffer == buf)
3070 {
3071 if (wp->w_cursor.lnum > last)
3072 wp->w_cursor.lnum -= count;
3073 else if (wp->w_cursor.lnum> first)
3074 wp->w_cursor.lnum = first;
3075 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3076 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3077 }
3078 check_cursor_col();
3079 deleted_lines_mark(first, count);
3080
3081 if (!is_curbuf)
3082 {
3083 curbuf = curbuf_save;
3084 curwin = curwin_save;
3085 }
3086}
3087
3088/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003089 * "did_filetype()" function
3090 */
3091 static void
3092f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3093{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003094 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003095}
3096
3097/*
3098 * "diff_filler()" function
3099 */
3100 static void
3101f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3102{
3103#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003104 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003105#endif
3106}
3107
3108/*
3109 * "diff_hlID()" function
3110 */
3111 static void
3112f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3113{
3114#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003117 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118 static int fnum = 0;
3119 static int change_start = 0;
3120 static int change_end = 0;
3121 static hlf_T hlID = (hlf_T)0;
3122 int filler_lines;
3123 int col;
3124
3125 if (lnum < 0) /* ignore type error in {lnum} arg */
3126 lnum = 0;
3127 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003128 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129 || fnum != curbuf->b_fnum)
3130 {
3131 /* New line, buffer, change: need to get the values. */
3132 filler_lines = diff_check(curwin, lnum);
3133 if (filler_lines < 0)
3134 {
3135 if (filler_lines == -1)
3136 {
3137 change_start = MAXCOL;
3138 change_end = -1;
3139 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3140 hlID = HLF_ADD; /* added line */
3141 else
3142 hlID = HLF_CHD; /* changed line */
3143 }
3144 else
3145 hlID = HLF_ADD; /* added line */
3146 }
3147 else
3148 hlID = (hlf_T)0;
3149 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003150 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003151 fnum = curbuf->b_fnum;
3152 }
3153
3154 if (hlID == HLF_CHD || hlID == HLF_TXD)
3155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003157 if (col >= change_start && col <= change_end)
3158 hlID = HLF_TXD; /* changed text */
3159 else
3160 hlID = HLF_CHD; /* changed line */
3161 }
3162 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3163#endif
3164}
3165
3166/*
3167 * "empty({expr})" function
3168 */
3169 static void
3170f_empty(typval_T *argvars, typval_T *rettv)
3171{
3172 int n = FALSE;
3173
3174 switch (argvars[0].v_type)
3175 {
3176 case VAR_STRING:
3177 case VAR_FUNC:
3178 n = argvars[0].vval.v_string == NULL
3179 || *argvars[0].vval.v_string == NUL;
3180 break;
3181 case VAR_PARTIAL:
3182 n = FALSE;
3183 break;
3184 case VAR_NUMBER:
3185 n = argvars[0].vval.v_number == 0;
3186 break;
3187 case VAR_FLOAT:
3188#ifdef FEAT_FLOAT
3189 n = argvars[0].vval.v_float == 0.0;
3190 break;
3191#endif
3192 case VAR_LIST:
3193 n = argvars[0].vval.v_list == NULL
3194 || argvars[0].vval.v_list->lv_first == NULL;
3195 break;
3196 case VAR_DICT:
3197 n = argvars[0].vval.v_dict == NULL
3198 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3199 break;
3200 case VAR_SPECIAL:
3201 n = argvars[0].vval.v_number != VVAL_TRUE;
3202 break;
3203
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003204 case VAR_BLOB:
3205 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003206 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3207 break;
3208
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003209 case VAR_JOB:
3210#ifdef FEAT_JOB_CHANNEL
3211 n = argvars[0].vval.v_job == NULL
3212 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3213 break;
3214#endif
3215 case VAR_CHANNEL:
3216#ifdef FEAT_JOB_CHANNEL
3217 n = argvars[0].vval.v_channel == NULL
3218 || !channel_is_open(argvars[0].vval.v_channel);
3219 break;
3220#endif
3221 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003222 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003223 n = TRUE;
3224 break;
3225 }
3226
3227 rettv->vval.v_number = n;
3228}
3229
3230/*
3231 * "escape({string}, {chars})" function
3232 */
3233 static void
3234f_escape(typval_T *argvars, typval_T *rettv)
3235{
3236 char_u buf[NUMBUFLEN];
3237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003238 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3239 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240 rettv->v_type = VAR_STRING;
3241}
3242
3243/*
3244 * "eval()" function
3245 */
3246 static void
3247f_eval(typval_T *argvars, typval_T *rettv)
3248{
3249 char_u *s, *p;
3250
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003251 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003252 if (s != NULL)
3253 s = skipwhite(s);
3254
3255 p = s;
3256 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3257 {
3258 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003259 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 need_clr_eos = FALSE;
3261 rettv->v_type = VAR_NUMBER;
3262 rettv->vval.v_number = 0;
3263 }
3264 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266}
3267
3268/*
3269 * "eventhandler()" function
3270 */
3271 static void
3272f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3273{
3274 rettv->vval.v_number = vgetc_busy;
3275}
3276
3277/*
3278 * "executable()" function
3279 */
3280 static void
3281f_executable(typval_T *argvars, typval_T *rettv)
3282{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003283 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284
3285 /* Check in $PATH and also check directly if there is a directory name. */
3286 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3287 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3288}
3289
3290static garray_T redir_execute_ga;
3291
3292/*
3293 * Append "value[value_len]" to the execute() output.
3294 */
3295 void
3296execute_redir_str(char_u *value, int value_len)
3297{
3298 int len;
3299
3300 if (value_len == -1)
3301 len = (int)STRLEN(value); /* Append the entire string */
3302 else
3303 len = value_len; /* Append only "value_len" characters */
3304 if (ga_grow(&redir_execute_ga, len) == OK)
3305 {
3306 mch_memmove((char *)redir_execute_ga.ga_data
3307 + redir_execute_ga.ga_len, value, len);
3308 redir_execute_ga.ga_len += len;
3309 }
3310}
3311
3312/*
3313 * Get next line from a list.
3314 * Called by do_cmdline() to get the next line.
3315 * Returns allocated string, or NULL for end of function.
3316 */
3317
3318 static char_u *
3319get_list_line(
3320 int c UNUSED,
3321 void *cookie,
3322 int indent UNUSED)
3323{
3324 listitem_T **p = (listitem_T **)cookie;
3325 listitem_T *item = *p;
3326 char_u buf[NUMBUFLEN];
3327 char_u *s;
3328
3329 if (item == NULL)
3330 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003331 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003332 *p = item->li_next;
3333 return s == NULL ? NULL : vim_strsave(s);
3334}
3335
3336/*
3337 * "execute()" function
3338 */
3339 static void
3340f_execute(typval_T *argvars, typval_T *rettv)
3341{
3342 char_u *cmd = NULL;
3343 list_T *list = NULL;
3344 int save_msg_silent = msg_silent;
3345 int save_emsg_silent = emsg_silent;
3346 int save_emsg_noredir = emsg_noredir;
3347 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003348 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003349 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003350 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003351 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352
3353 rettv->vval.v_string = NULL;
3354 rettv->v_type = VAR_STRING;
3355
3356 if (argvars[0].v_type == VAR_LIST)
3357 {
3358 list = argvars[0].vval.v_list;
3359 if (list == NULL || list->lv_first == NULL)
3360 /* empty list, no commands, empty output */
3361 return;
3362 ++list->lv_refcount;
3363 }
3364 else
3365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003366 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003367 if (cmd == NULL)
3368 return;
3369 }
3370
3371 if (argvars[1].v_type != VAR_UNKNOWN)
3372 {
3373 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003374 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003375
3376 if (s == NULL)
3377 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003378 if (*s == NUL)
3379 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003380 if (STRNCMP(s, "silent", 6) == 0)
3381 ++msg_silent;
3382 if (STRCMP(s, "silent!") == 0)
3383 {
3384 emsg_silent = TRUE;
3385 emsg_noredir = TRUE;
3386 }
3387 }
3388 else
3389 ++msg_silent;
3390
3391 if (redir_execute)
3392 save_ga = redir_execute_ga;
3393 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3394 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003395 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003396 if (!echo_output)
3397 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003398
3399 if (cmd != NULL)
3400 do_cmdline_cmd(cmd);
3401 else
3402 {
3403 listitem_T *item = list->lv_first;
3404
3405 do_cmdline(NULL, get_list_line, (void *)&item,
3406 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3407 --list->lv_refcount;
3408 }
3409
Bram Moolenaard297f352017-01-29 20:31:21 +01003410 /* Need to append a NUL to the result. */
3411 if (ga_grow(&redir_execute_ga, 1) == OK)
3412 {
3413 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3414 rettv->vval.v_string = redir_execute_ga.ga_data;
3415 }
3416 else
3417 {
3418 ga_clear(&redir_execute_ga);
3419 rettv->vval.v_string = NULL;
3420 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 msg_silent = save_msg_silent;
3422 emsg_silent = save_emsg_silent;
3423 emsg_noredir = save_emsg_noredir;
3424
3425 redir_execute = save_redir_execute;
3426 if (redir_execute)
3427 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003428 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003429
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003430 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003431 if (echo_output)
3432 // When not working silently: put it in column zero. A following
3433 // "echon" will overwrite the message, unavoidably.
3434 msg_col = 0;
3435 else
3436 // When working silently: Put it back where it was, since nothing
3437 // should have been written.
3438 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439}
3440
3441/*
3442 * "exepath()" function
3443 */
3444 static void
3445f_exepath(typval_T *argvars, typval_T *rettv)
3446{
3447 char_u *p = NULL;
3448
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003449 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450 rettv->v_type = VAR_STRING;
3451 rettv->vval.v_string = p;
3452}
3453
3454/*
3455 * "exists()" function
3456 */
3457 static void
3458f_exists(typval_T *argvars, typval_T *rettv)
3459{
3460 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003463 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003464 if (*p == '$') /* environment variable */
3465 {
3466 /* first try "normal" environment variables (fast) */
3467 if (mch_getenv(p + 1) != NULL)
3468 n = TRUE;
3469 else
3470 {
3471 /* try expanding things like $VIM and ${HOME} */
3472 p = expand_env_save(p);
3473 if (p != NULL && *p != '$')
3474 n = TRUE;
3475 vim_free(p);
3476 }
3477 }
3478 else if (*p == '&' || *p == '+') /* option */
3479 {
3480 n = (get_option_tv(&p, NULL, TRUE) == OK);
3481 if (*skipwhite(p) != NUL)
3482 n = FALSE; /* trailing garbage */
3483 }
3484 else if (*p == '*') /* internal or user defined function */
3485 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003486 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003487 }
3488 else if (*p == ':')
3489 {
3490 n = cmd_exists(p + 1);
3491 }
3492 else if (*p == '#')
3493 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003494 if (p[1] == '#')
3495 n = autocmd_supported(p + 2);
3496 else
3497 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003498 }
3499 else /* internal variable */
3500 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003501 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003502 }
3503
3504 rettv->vval.v_number = n;
3505}
3506
3507#ifdef FEAT_FLOAT
3508/*
3509 * "exp()" function
3510 */
3511 static void
3512f_exp(typval_T *argvars, typval_T *rettv)
3513{
3514 float_T f = 0.0;
3515
3516 rettv->v_type = VAR_FLOAT;
3517 if (get_float_arg(argvars, &f) == OK)
3518 rettv->vval.v_float = exp(f);
3519 else
3520 rettv->vval.v_float = 0.0;
3521}
3522#endif
3523
3524/*
3525 * "expand()" function
3526 */
3527 static void
3528f_expand(typval_T *argvars, typval_T *rettv)
3529{
3530 char_u *s;
3531 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003532 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3534 expand_T xpc;
3535 int error = FALSE;
3536 char_u *result;
3537
3538 rettv->v_type = VAR_STRING;
3539 if (argvars[1].v_type != VAR_UNKNOWN
3540 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003541 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542 && !error)
3543 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003544 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 }
3546
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003547 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 if (*s == '%' || *s == '#' || *s == '<')
3549 {
3550 ++emsg_off;
3551 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3552 --emsg_off;
3553 if (rettv->v_type == VAR_LIST)
3554 {
3555 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3556 list_append_string(rettv->vval.v_list, result, -1);
3557 else
3558 vim_free(result);
3559 }
3560 else
3561 rettv->vval.v_string = result;
3562 }
3563 else
3564 {
3565 /* When the optional second argument is non-zero, don't remove matches
3566 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3567 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003568 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569 options |= WILD_KEEP_ALL;
3570 if (!error)
3571 {
3572 ExpandInit(&xpc);
3573 xpc.xp_context = EXPAND_FILES;
3574 if (p_wic)
3575 options += WILD_ICASE;
3576 if (rettv->v_type == VAR_STRING)
3577 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3578 options, WILD_ALL);
3579 else if (rettv_list_alloc(rettv) != FAIL)
3580 {
3581 int i;
3582
3583 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3584 for (i = 0; i < xpc.xp_numfiles; i++)
3585 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3586 ExpandCleanup(&xpc);
3587 }
3588 }
3589 else
3590 rettv->vval.v_string = NULL;
3591 }
3592}
3593
3594/*
3595 * "extend(list, list [, idx])" function
3596 * "extend(dict, dict [, action])" function
3597 */
3598 static void
3599f_extend(typval_T *argvars, typval_T *rettv)
3600{
3601 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3602
3603 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3604 {
3605 list_T *l1, *l2;
3606 listitem_T *item;
3607 long before;
3608 int error = FALSE;
3609
3610 l1 = argvars[0].vval.v_list;
3611 l2 = argvars[1].vval.v_list;
3612 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
3613 && l2 != NULL)
3614 {
3615 if (argvars[2].v_type != VAR_UNKNOWN)
3616 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003617 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003618 if (error)
3619 return; /* type error; errmsg already given */
3620
3621 if (before == l1->lv_len)
3622 item = NULL;
3623 else
3624 {
3625 item = list_find(l1, before);
3626 if (item == NULL)
3627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003628 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003629 return;
3630 }
3631 }
3632 }
3633 else
3634 item = NULL;
3635 list_extend(l1, l2, item);
3636
3637 copy_tv(&argvars[0], rettv);
3638 }
3639 }
3640 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3641 {
3642 dict_T *d1, *d2;
3643 char_u *action;
3644 int i;
3645
3646 d1 = argvars[0].vval.v_dict;
3647 d2 = argvars[1].vval.v_dict;
3648 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
3649 && d2 != NULL)
3650 {
3651 /* Check the third argument. */
3652 if (argvars[2].v_type != VAR_UNKNOWN)
3653 {
3654 static char *(av[]) = {"keep", "force", "error"};
3655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003656 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 if (action == NULL)
3658 return; /* type error; errmsg already given */
3659 for (i = 0; i < 3; ++i)
3660 if (STRCMP(action, av[i]) == 0)
3661 break;
3662 if (i == 3)
3663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003664 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 return;
3666 }
3667 }
3668 else
3669 action = (char_u *)"force";
3670
3671 dict_extend(d1, d2, action);
3672
3673 copy_tv(&argvars[0], rettv);
3674 }
3675 }
3676 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003677 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003678}
3679
3680/*
3681 * "feedkeys()" function
3682 */
3683 static void
3684f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3685{
3686 int remap = TRUE;
3687 int insert = FALSE;
3688 char_u *keys, *flags;
3689 char_u nbuf[NUMBUFLEN];
3690 int typed = FALSE;
3691 int execute = FALSE;
3692 int dangerous = FALSE;
3693 char_u *keys_esc;
3694
3695 /* This is not allowed in the sandbox. If the commands would still be
3696 * executed in the sandbox it would be OK, but it probably happens later,
3697 * when "sandbox" is no longer set. */
3698 if (check_secure())
3699 return;
3700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003701 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702
3703 if (argvars[1].v_type != VAR_UNKNOWN)
3704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003705 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 for ( ; *flags != NUL; ++flags)
3707 {
3708 switch (*flags)
3709 {
3710 case 'n': remap = FALSE; break;
3711 case 'm': remap = TRUE; break;
3712 case 't': typed = TRUE; break;
3713 case 'i': insert = TRUE; break;
3714 case 'x': execute = TRUE; break;
3715 case '!': dangerous = TRUE; break;
3716 }
3717 }
3718 }
3719
3720 if (*keys != NUL || execute)
3721 {
3722 /* Need to escape K_SPECIAL and CSI before putting the string in the
3723 * typeahead buffer. */
3724 keys_esc = vim_strsave_escape_csi(keys);
3725 if (keys_esc != NULL)
3726 {
3727 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
3728 insert ? 0 : typebuf.tb_len, !typed, FALSE);
3729 vim_free(keys_esc);
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003730 if (vgetc_busy
3731#ifdef FEAT_TIMERS
3732 || timer_busy
3733#endif
3734 )
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 typebuf_was_filled = TRUE;
3736 if (execute)
3737 {
3738 int save_msg_scroll = msg_scroll;
3739
3740 /* Avoid a 1 second delay when the keys start Insert mode. */
3741 msg_scroll = FALSE;
3742
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003743 if (!dangerous)
3744 ++ex_normal_busy;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02003745 exec_normal(TRUE, FALSE, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003746 if (!dangerous)
3747 --ex_normal_busy;
3748
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 msg_scroll |= save_msg_scroll;
3750 }
3751 }
3752 }
3753}
3754
3755/*
3756 * "filereadable()" function
3757 */
3758 static void
3759f_filereadable(typval_T *argvars, typval_T *rettv)
3760{
3761 int fd;
3762 char_u *p;
3763 int n;
3764
3765#ifndef O_NONBLOCK
3766# define O_NONBLOCK 0
3767#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003768 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003769 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3770 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3771 {
3772 n = TRUE;
3773 close(fd);
3774 }
3775 else
3776 n = FALSE;
3777
3778 rettv->vval.v_number = n;
3779}
3780
3781/*
3782 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3783 * rights to write into.
3784 */
3785 static void
3786f_filewritable(typval_T *argvars, typval_T *rettv)
3787{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003788 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003789}
3790
3791 static void
3792findfilendir(
3793 typval_T *argvars UNUSED,
3794 typval_T *rettv,
3795 int find_what UNUSED)
3796{
3797#ifdef FEAT_SEARCHPATH
3798 char_u *fname;
3799 char_u *fresult = NULL;
3800 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3801 char_u *p;
3802 char_u pathbuf[NUMBUFLEN];
3803 int count = 1;
3804 int first = TRUE;
3805 int error = FALSE;
3806#endif
3807
3808 rettv->vval.v_string = NULL;
3809 rettv->v_type = VAR_STRING;
3810
3811#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003812 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003813
3814 if (argvars[1].v_type != VAR_UNKNOWN)
3815 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003816 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003817 if (p == NULL)
3818 error = TRUE;
3819 else
3820 {
3821 if (*p != NUL)
3822 path = p;
3823
3824 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003825 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 }
3827 }
3828
3829 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
3830 error = TRUE;
3831
3832 if (*fname != NUL && !error)
3833 {
3834 do
3835 {
3836 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
3837 vim_free(fresult);
3838 fresult = find_file_in_path_option(first ? fname : NULL,
3839 first ? (int)STRLEN(fname) : 0,
3840 0, first, path,
3841 find_what,
3842 curbuf->b_ffname,
3843 find_what == FINDFILE_DIR
3844 ? (char_u *)"" : curbuf->b_p_sua);
3845 first = FALSE;
3846
3847 if (fresult != NULL && rettv->v_type == VAR_LIST)
3848 list_append_string(rettv->vval.v_list, fresult, -1);
3849
3850 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
3851 }
3852
3853 if (rettv->v_type == VAR_STRING)
3854 rettv->vval.v_string = fresult;
3855#endif
3856}
3857
3858/*
3859 * "filter()" function
3860 */
3861 static void
3862f_filter(typval_T *argvars, typval_T *rettv)
3863{
3864 filter_map(argvars, rettv, FALSE);
3865}
3866
3867/*
3868 * "finddir({fname}[, {path}[, {count}]])" function
3869 */
3870 static void
3871f_finddir(typval_T *argvars, typval_T *rettv)
3872{
3873 findfilendir(argvars, rettv, FINDFILE_DIR);
3874}
3875
3876/*
3877 * "findfile({fname}[, {path}[, {count}]])" function
3878 */
3879 static void
3880f_findfile(typval_T *argvars, typval_T *rettv)
3881{
3882 findfilendir(argvars, rettv, FINDFILE_FILE);
3883}
3884
3885#ifdef FEAT_FLOAT
3886/*
3887 * "float2nr({float})" function
3888 */
3889 static void
3890f_float2nr(typval_T *argvars, typval_T *rettv)
3891{
3892 float_T f = 0.0;
3893
3894 if (get_float_arg(argvars, &f) == OK)
3895 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003896 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003897 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02003898 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01003899 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 else
3901 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 }
3903}
3904
3905/*
3906 * "floor({float})" function
3907 */
3908 static void
3909f_floor(typval_T *argvars, typval_T *rettv)
3910{
3911 float_T f = 0.0;
3912
3913 rettv->v_type = VAR_FLOAT;
3914 if (get_float_arg(argvars, &f) == OK)
3915 rettv->vval.v_float = floor(f);
3916 else
3917 rettv->vval.v_float = 0.0;
3918}
3919
3920/*
3921 * "fmod()" function
3922 */
3923 static void
3924f_fmod(typval_T *argvars, typval_T *rettv)
3925{
3926 float_T fx = 0.0, fy = 0.0;
3927
3928 rettv->v_type = VAR_FLOAT;
3929 if (get_float_arg(argvars, &fx) == OK
3930 && get_float_arg(&argvars[1], &fy) == OK)
3931 rettv->vval.v_float = fmod(fx, fy);
3932 else
3933 rettv->vval.v_float = 0.0;
3934}
3935#endif
3936
3937/*
3938 * "fnameescape({string})" function
3939 */
3940 static void
3941f_fnameescape(typval_T *argvars, typval_T *rettv)
3942{
3943 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003944 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003945 rettv->v_type = VAR_STRING;
3946}
3947
3948/*
3949 * "fnamemodify({fname}, {mods})" function
3950 */
3951 static void
3952f_fnamemodify(typval_T *argvars, typval_T *rettv)
3953{
3954 char_u *fname;
3955 char_u *mods;
3956 int usedlen = 0;
3957 int len;
3958 char_u *fbuf = NULL;
3959 char_u buf[NUMBUFLEN];
3960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003961 fname = tv_get_string_chk(&argvars[0]);
3962 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963 if (fname == NULL || mods == NULL)
3964 fname = NULL;
3965 else
3966 {
3967 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02003968 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003969 }
3970
3971 rettv->v_type = VAR_STRING;
3972 if (fname == NULL)
3973 rettv->vval.v_string = NULL;
3974 else
3975 rettv->vval.v_string = vim_strnsave(fname, len);
3976 vim_free(fbuf);
3977}
3978
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003979/*
3980 * "foldclosed()" function
3981 */
3982 static void
3983foldclosed_both(
3984 typval_T *argvars UNUSED,
3985 typval_T *rettv,
3986 int end UNUSED)
3987{
3988#ifdef FEAT_FOLDING
3989 linenr_T lnum;
3990 linenr_T first, last;
3991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003992 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
3994 {
3995 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
3996 {
3997 if (end)
3998 rettv->vval.v_number = (varnumber_T)last;
3999 else
4000 rettv->vval.v_number = (varnumber_T)first;
4001 return;
4002 }
4003 }
4004#endif
4005 rettv->vval.v_number = -1;
4006}
4007
4008/*
4009 * "foldclosed()" function
4010 */
4011 static void
4012f_foldclosed(typval_T *argvars, typval_T *rettv)
4013{
4014 foldclosed_both(argvars, rettv, FALSE);
4015}
4016
4017/*
4018 * "foldclosedend()" function
4019 */
4020 static void
4021f_foldclosedend(typval_T *argvars, typval_T *rettv)
4022{
4023 foldclosed_both(argvars, rettv, TRUE);
4024}
4025
4026/*
4027 * "foldlevel()" function
4028 */
4029 static void
4030f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4031{
4032#ifdef FEAT_FOLDING
4033 linenr_T lnum;
4034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004035 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004036 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4037 rettv->vval.v_number = foldLevel(lnum);
4038#endif
4039}
4040
4041/*
4042 * "foldtext()" function
4043 */
4044 static void
4045f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4046{
4047#ifdef FEAT_FOLDING
4048 linenr_T foldstart;
4049 linenr_T foldend;
4050 char_u *dashes;
4051 linenr_T lnum;
4052 char_u *s;
4053 char_u *r;
4054 int len;
4055 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004056 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057#endif
4058
4059 rettv->v_type = VAR_STRING;
4060 rettv->vval.v_string = NULL;
4061#ifdef FEAT_FOLDING
4062 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4063 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4064 dashes = get_vim_var_str(VV_FOLDDASHES);
4065 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4066 && dashes != NULL)
4067 {
4068 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004069 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 if (!linewhite(lnum))
4071 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072
4073 /* Find interesting text in this line. */
4074 s = skipwhite(ml_get(lnum));
4075 /* skip C comment-start */
4076 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4077 {
4078 s = skipwhite(s + 2);
4079 if (*skipwhite(s) == NUL
4080 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4081 {
4082 s = skipwhite(ml_get(lnum + 1));
4083 if (*s == '*')
4084 s = skipwhite(s + 1);
4085 }
4086 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004087 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004088 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004089 r = alloc((unsigned)(STRLEN(txt)
4090 + STRLEN(dashes) /* for %s */
4091 + 20 /* for %3ld */
4092 + STRLEN(s))); /* concatenated */
4093 if (r != NULL)
4094 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004095 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 len = (int)STRLEN(r);
4097 STRCAT(r, s);
4098 /* remove 'foldmarker' and 'commentstring' */
4099 foldtext_cleanup(r + len);
4100 rettv->vval.v_string = r;
4101 }
4102 }
4103#endif
4104}
4105
4106/*
4107 * "foldtextresult(lnum)" function
4108 */
4109 static void
4110f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4111{
4112#ifdef FEAT_FOLDING
4113 linenr_T lnum;
4114 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004115 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004116 foldinfo_T foldinfo;
4117 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004118 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004119#endif
4120
4121 rettv->v_type = VAR_STRING;
4122 rettv->vval.v_string = NULL;
4123#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004124 if (entered)
4125 return; /* reject recursive use */
4126 entered = TRUE;
4127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004128 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004129 /* treat illegal types and illegal string values for {lnum} the same */
4130 if (lnum < 0)
4131 lnum = 0;
4132 fold_count = foldedCount(curwin, lnum, &foldinfo);
4133 if (fold_count > 0)
4134 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004135 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4136 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004137 if (text == buf)
4138 text = vim_strsave(text);
4139 rettv->vval.v_string = text;
4140 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004141
4142 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004143#endif
4144}
4145
4146/*
4147 * "foreground()" function
4148 */
4149 static void
4150f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4151{
4152#ifdef FEAT_GUI
4153 if (gui.in_use)
4154 gui_mch_set_foreground();
4155#else
4156# ifdef WIN32
4157 win32_set_foreground();
4158# endif
4159#endif
4160}
4161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004162 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004163common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004164{
4165 char_u *s;
4166 char_u *name;
4167 int use_string = FALSE;
4168 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004169 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170
4171 if (argvars[0].v_type == VAR_FUNC)
4172 {
4173 /* function(MyFunc, [arg], dict) */
4174 s = argvars[0].vval.v_string;
4175 }
4176 else if (argvars[0].v_type == VAR_PARTIAL
4177 && argvars[0].vval.v_partial != NULL)
4178 {
4179 /* function(dict.MyFunc, [arg]) */
4180 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004181 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004182 }
4183 else
4184 {
4185 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 use_string = TRUE;
4188 }
4189
Bram Moolenaar843b8842016-08-21 14:36:15 +02004190 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004191 {
4192 name = s;
4193 trans_name = trans_function_name(&name, FALSE,
4194 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4195 if (*name != NUL)
4196 s = NULL;
4197 }
4198
Bram Moolenaar843b8842016-08-21 14:36:15 +02004199 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4200 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004201 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004202 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004203 else if (trans_name != NULL && (is_funcref
4204 ? find_func(trans_name) == NULL
4205 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004206 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 else
4208 {
4209 int dict_idx = 0;
4210 int arg_idx = 0;
4211 list_T *list = NULL;
4212
4213 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4214 {
4215 char sid_buf[25];
4216 int off = *s == 's' ? 2 : 5;
4217
4218 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4219 * also be called from another script. Using trans_function_name()
4220 * would also work, but some plugins depend on the name being
4221 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004222 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4224 if (name != NULL)
4225 {
4226 STRCPY(name, sid_buf);
4227 STRCAT(name, s + off);
4228 }
4229 }
4230 else
4231 name = vim_strsave(s);
4232
4233 if (argvars[1].v_type != VAR_UNKNOWN)
4234 {
4235 if (argvars[2].v_type != VAR_UNKNOWN)
4236 {
4237 /* function(name, [args], dict) */
4238 arg_idx = 1;
4239 dict_idx = 2;
4240 }
4241 else if (argvars[1].v_type == VAR_DICT)
4242 /* function(name, dict) */
4243 dict_idx = 1;
4244 else
4245 /* function(name, [args]) */
4246 arg_idx = 1;
4247 if (dict_idx > 0)
4248 {
4249 if (argvars[dict_idx].v_type != VAR_DICT)
4250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004251 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004252 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004253 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004254 }
4255 if (argvars[dict_idx].vval.v_dict == NULL)
4256 dict_idx = 0;
4257 }
4258 if (arg_idx > 0)
4259 {
4260 if (argvars[arg_idx].v_type != VAR_LIST)
4261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004262 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004264 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265 }
4266 list = argvars[arg_idx].vval.v_list;
4267 if (list == NULL || list->lv_len == 0)
4268 arg_idx = 0;
4269 }
4270 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004271 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004272 {
4273 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4274
4275 /* result is a VAR_PARTIAL */
4276 if (pt == NULL)
4277 vim_free(name);
4278 else
4279 {
4280 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4281 {
4282 listitem_T *li;
4283 int i = 0;
4284 int arg_len = 0;
4285 int lv_len = 0;
4286
4287 if (arg_pt != NULL)
4288 arg_len = arg_pt->pt_argc;
4289 if (list != NULL)
4290 lv_len = list->lv_len;
4291 pt->pt_argc = arg_len + lv_len;
4292 pt->pt_argv = (typval_T *)alloc(
4293 sizeof(typval_T) * pt->pt_argc);
4294 if (pt->pt_argv == NULL)
4295 {
4296 vim_free(pt);
4297 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004298 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004299 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004300 for (i = 0; i < arg_len; i++)
4301 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4302 if (lv_len > 0)
4303 for (li = list->lv_first; li != NULL;
4304 li = li->li_next)
4305 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004306 }
4307
4308 /* For "function(dict.func, [], dict)" and "func" is a partial
4309 * use "dict". That is backwards compatible. */
4310 if (dict_idx > 0)
4311 {
4312 /* The dict is bound explicitly, pt_auto is FALSE. */
4313 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4314 ++pt->pt_dict->dv_refcount;
4315 }
4316 else if (arg_pt != NULL)
4317 {
4318 /* If the dict was bound automatically the result is also
4319 * bound automatically. */
4320 pt->pt_dict = arg_pt->pt_dict;
4321 pt->pt_auto = arg_pt->pt_auto;
4322 if (pt->pt_dict != NULL)
4323 ++pt->pt_dict->dv_refcount;
4324 }
4325
4326 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004327 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4328 {
4329 pt->pt_func = arg_pt->pt_func;
4330 func_ptr_ref(pt->pt_func);
4331 vim_free(name);
4332 }
4333 else if (is_funcref)
4334 {
4335 pt->pt_func = find_func(trans_name);
4336 func_ptr_ref(pt->pt_func);
4337 vim_free(name);
4338 }
4339 else
4340 {
4341 pt->pt_name = name;
4342 func_ref(name);
4343 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 }
4345 rettv->v_type = VAR_PARTIAL;
4346 rettv->vval.v_partial = pt;
4347 }
4348 else
4349 {
4350 /* result is a VAR_FUNC */
4351 rettv->v_type = VAR_FUNC;
4352 rettv->vval.v_string = name;
4353 func_ref(name);
4354 }
4355 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004356theend:
4357 vim_free(trans_name);
4358}
4359
4360/*
4361 * "funcref()" function
4362 */
4363 static void
4364f_funcref(typval_T *argvars, typval_T *rettv)
4365{
4366 common_function(argvars, rettv, TRUE);
4367}
4368
4369/*
4370 * "function()" function
4371 */
4372 static void
4373f_function(typval_T *argvars, typval_T *rettv)
4374{
4375 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004376}
4377
4378/*
4379 * "garbagecollect()" function
4380 */
4381 static void
4382f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4383{
4384 /* This is postponed until we are back at the toplevel, because we may be
4385 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4386 want_garbage_collect = TRUE;
4387
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004388 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389 garbage_collect_at_exit = TRUE;
4390}
4391
4392/*
4393 * "get()" function
4394 */
4395 static void
4396f_get(typval_T *argvars, typval_T *rettv)
4397{
4398 listitem_T *li;
4399 list_T *l;
4400 dictitem_T *di;
4401 dict_T *d;
4402 typval_T *tv = NULL;
4403
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004404 if (argvars[0].v_type == VAR_BLOB)
4405 {
4406 int error = FALSE;
4407 int idx = tv_get_number_chk(&argvars[1], &error);
4408
4409 if (!error)
4410 {
4411 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004412 if (idx < 0)
4413 idx = blob_len(argvars[0].vval.v_blob) + idx;
4414 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4415 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004416 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004417 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004418 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004419 tv = rettv;
4420 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004421 }
4422 }
4423 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004424 {
4425 if ((l = argvars[0].vval.v_list) != NULL)
4426 {
4427 int error = FALSE;
4428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004429 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004430 if (!error && li != NULL)
4431 tv = &li->li_tv;
4432 }
4433 }
4434 else if (argvars[0].v_type == VAR_DICT)
4435 {
4436 if ((d = argvars[0].vval.v_dict) != NULL)
4437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004438 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004439 if (di != NULL)
4440 tv = &di->di_tv;
4441 }
4442 }
4443 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4444 {
4445 partial_T *pt;
4446 partial_T fref_pt;
4447
4448 if (argvars[0].v_type == VAR_PARTIAL)
4449 pt = argvars[0].vval.v_partial;
4450 else
4451 {
4452 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4453 fref_pt.pt_name = argvars[0].vval.v_string;
4454 pt = &fref_pt;
4455 }
4456
4457 if (pt != NULL)
4458 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004459 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004460 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461
4462 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4463 {
4464 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004465 n = partial_name(pt);
4466 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467 rettv->vval.v_string = NULL;
4468 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004469 {
4470 rettv->vval.v_string = vim_strsave(n);
4471 if (rettv->v_type == VAR_FUNC)
4472 func_ref(rettv->vval.v_string);
4473 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 }
4475 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004476 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004477 else if (STRCMP(what, "args") == 0)
4478 {
4479 rettv->v_type = VAR_LIST;
4480 if (rettv_list_alloc(rettv) == OK)
4481 {
4482 int i;
4483
4484 for (i = 0; i < pt->pt_argc; ++i)
4485 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4486 }
4487 }
4488 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004489 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004490 return;
4491 }
4492 }
4493 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004494 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004495
4496 if (tv == NULL)
4497 {
4498 if (argvars[2].v_type != VAR_UNKNOWN)
4499 copy_tv(&argvars[2], rettv);
4500 }
4501 else
4502 copy_tv(tv, rettv);
4503}
4504
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004505/*
4506 * Returns buffer options, variables and other attributes in a dictionary.
4507 */
4508 static dict_T *
4509get_buffer_info(buf_T *buf)
4510{
4511 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004512 tabpage_T *tp;
4513 win_T *wp;
4514 list_T *windows;
4515
4516 dict = dict_alloc();
4517 if (dict == NULL)
4518 return NULL;
4519
Bram Moolenaare0be1672018-07-08 16:50:37 +02004520 dict_add_number(dict, "bufnr", buf->b_fnum);
4521 dict_add_string(dict, "name", buf->b_ffname);
4522 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4523 : buflist_findlnum(buf));
4524 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4525 dict_add_number(dict, "listed", buf->b_p_bl);
4526 dict_add_number(dict, "changed", bufIsChanged(buf));
4527 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4528 dict_add_number(dict, "hidden",
4529 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004530
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004531 /* Get a reference to buffer variables */
4532 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004533
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004534 /* List of windows displaying this buffer */
4535 windows = list_alloc();
4536 if (windows != NULL)
4537 {
4538 FOR_ALL_TAB_WINDOWS(tp, wp)
4539 if (wp->w_buffer == buf)
4540 list_append_number(windows, (varnumber_T)wp->w_id);
4541 dict_add_list(dict, "windows", windows);
4542 }
4543
4544#ifdef FEAT_SIGNS
4545 if (buf->b_signlist != NULL)
4546 {
4547 /* List of signs placed in this buffer */
4548 list_T *signs = list_alloc();
4549 if (signs != NULL)
4550 {
4551 get_buffer_signs(buf, signs);
4552 dict_add_list(dict, "signs", signs);
4553 }
4554 }
4555#endif
4556
4557 return dict;
4558}
4559
4560/*
4561 * "getbufinfo()" function
4562 */
4563 static void
4564f_getbufinfo(typval_T *argvars, typval_T *rettv)
4565{
4566 buf_T *buf = NULL;
4567 buf_T *argbuf = NULL;
4568 dict_T *d;
4569 int filtered = FALSE;
4570 int sel_buflisted = FALSE;
4571 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004572 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004573
4574 if (rettv_list_alloc(rettv) != OK)
4575 return;
4576
4577 /* List of all the buffers or selected buffers */
4578 if (argvars[0].v_type == VAR_DICT)
4579 {
4580 dict_T *sel_d = argvars[0].vval.v_dict;
4581
4582 if (sel_d != NULL)
4583 {
4584 dictitem_T *di;
4585
4586 filtered = TRUE;
4587
4588 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004589 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004590 sel_buflisted = TRUE;
4591
4592 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004593 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004594 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004595
4596 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004597 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004598 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004599 }
4600 }
4601 else if (argvars[0].v_type != VAR_UNKNOWN)
4602 {
4603 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004604 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004605 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004606 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004607 --emsg_off;
4608 if (argbuf == NULL)
4609 return;
4610 }
4611
4612 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004613 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004614 {
4615 if (argbuf != NULL && argbuf != buf)
4616 continue;
4617 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004618 || (sel_buflisted && !buf->b_p_bl)
4619 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004620 continue;
4621
4622 d = get_buffer_info(buf);
4623 if (d != NULL)
4624 list_append_dict(rettv->vval.v_list, d);
4625 if (argbuf != NULL)
4626 return;
4627 }
4628}
4629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630/*
4631 * Get line or list of lines from buffer "buf" into "rettv".
4632 * Return a range (from start to end) of lines in rettv from the specified
4633 * buffer.
4634 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4635 */
4636 static void
4637get_buffer_lines(
4638 buf_T *buf,
4639 linenr_T start,
4640 linenr_T end,
4641 int retlist,
4642 typval_T *rettv)
4643{
4644 char_u *p;
4645
4646 rettv->v_type = VAR_STRING;
4647 rettv->vval.v_string = NULL;
4648 if (retlist && rettv_list_alloc(rettv) == FAIL)
4649 return;
4650
4651 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4652 return;
4653
4654 if (!retlist)
4655 {
4656 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4657 p = ml_get_buf(buf, start, FALSE);
4658 else
4659 p = (char_u *)"";
4660 rettv->vval.v_string = vim_strsave(p);
4661 }
4662 else
4663 {
4664 if (end < start)
4665 return;
4666
4667 if (start < 1)
4668 start = 1;
4669 if (end > buf->b_ml.ml_line_count)
4670 end = buf->b_ml.ml_line_count;
4671 while (start <= end)
4672 if (list_append_string(rettv->vval.v_list,
4673 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4674 break;
4675 }
4676}
4677
4678/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004679 * "getbufline()" function
4680 */
4681 static void
4682f_getbufline(typval_T *argvars, typval_T *rettv)
4683{
4684 linenr_T lnum;
4685 linenr_T end;
4686 buf_T *buf;
4687
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004688 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004690 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 --emsg_off;
4692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004693 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004694 if (argvars[2].v_type == VAR_UNKNOWN)
4695 end = lnum;
4696 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004697 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698
4699 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4700}
4701
4702/*
4703 * "getbufvar()" function
4704 */
4705 static void
4706f_getbufvar(typval_T *argvars, typval_T *rettv)
4707{
4708 buf_T *buf;
4709 buf_T *save_curbuf;
4710 char_u *varname;
4711 dictitem_T *v;
4712 int done = FALSE;
4713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004714 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4715 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004716 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004717 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004718
4719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = NULL;
4721
4722 if (buf != NULL && varname != NULL)
4723 {
4724 /* set curbuf to be our buf, temporarily */
4725 save_curbuf = curbuf;
4726 curbuf = buf;
4727
Bram Moolenaar30567352016-08-27 21:25:44 +02004728 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004730 if (varname[1] == NUL)
4731 {
4732 /* get all buffer-local options in a dict */
4733 dict_T *opts = get_winbuf_options(TRUE);
4734
4735 if (opts != NULL)
4736 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004737 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004738 done = TRUE;
4739 }
4740 }
4741 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4742 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004743 done = TRUE;
4744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745 else
4746 {
4747 /* Look up the variable. */
4748 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4749 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4750 'b', varname, FALSE);
4751 if (v != NULL)
4752 {
4753 copy_tv(&v->di_tv, rettv);
4754 done = TRUE;
4755 }
4756 }
4757
4758 /* restore previous notion of curbuf */
4759 curbuf = save_curbuf;
4760 }
4761
4762 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4763 /* use the default value */
4764 copy_tv(&argvars[2], rettv);
4765
4766 --emsg_off;
4767}
4768
4769/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004770 * "getchangelist()" function
4771 */
4772 static void
4773f_getchangelist(typval_T *argvars, typval_T *rettv)
4774{
4775#ifdef FEAT_JUMPLIST
4776 buf_T *buf;
4777 int i;
4778 list_T *l;
4779 dict_T *d;
4780#endif
4781
4782 if (rettv_list_alloc(rettv) != OK)
4783 return;
4784
4785#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004786 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004787 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004788 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004789 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004790 if (buf == NULL)
4791 return;
4792
4793 l = list_alloc();
4794 if (l == NULL)
4795 return;
4796
4797 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4798 return;
4799 /*
4800 * The current window change list index tracks only the position in the
4801 * current buffer change list. For other buffers, use the change list
4802 * length as the current index.
4803 */
4804 list_append_number(rettv->vval.v_list,
4805 (varnumber_T)((buf == curwin->w_buffer)
4806 ? curwin->w_changelistidx : buf->b_changelistlen));
4807
4808 for (i = 0; i < buf->b_changelistlen; ++i)
4809 {
4810 if (buf->b_changelist[i].lnum == 0)
4811 continue;
4812 if ((d = dict_alloc()) == NULL)
4813 return;
4814 if (list_append_dict(l, d) == FAIL)
4815 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02004816 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
4817 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004818# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02004819 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004820# endif
4821 }
4822#endif
4823}
4824/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004825 * "getchar()" function
4826 */
4827 static void
4828f_getchar(typval_T *argvars, typval_T *rettv)
4829{
4830 varnumber_T n;
4831 int error = FALSE;
4832
Bram Moolenaar84d93902018-09-11 20:10:20 +02004833#ifdef MESSAGE_QUEUE
4834 // vpeekc() used to check for messages, but that caused problems, invoking
4835 // a callback where it was not expected. Some plugins use getchar(1) in a
4836 // loop to await a message, therefore make sure we check for messages here.
4837 parse_queued_messages();
4838#endif
4839
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004840 /* Position the cursor. Needed after a message that ends in a space. */
4841 windgoto(msg_row, msg_col);
4842
4843 ++no_mapping;
4844 ++allow_keys;
4845 for (;;)
4846 {
4847 if (argvars[0].v_type == VAR_UNKNOWN)
4848 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004849 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004851 /* getchar(1): only check if char avail */
4852 n = vpeekc_any();
4853 else if (error || vpeekc_any() == NUL)
4854 /* illegal argument or getchar(0) and no char avail: return zero */
4855 n = 0;
4856 else
4857 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01004858 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004859
4860 if (n == K_IGNORE)
4861 continue;
4862 break;
4863 }
4864 --no_mapping;
4865 --allow_keys;
4866
4867 set_vim_var_nr(VV_MOUSE_WIN, 0);
4868 set_vim_var_nr(VV_MOUSE_WINID, 0);
4869 set_vim_var_nr(VV_MOUSE_LNUM, 0);
4870 set_vim_var_nr(VV_MOUSE_COL, 0);
4871
4872 rettv->vval.v_number = n;
4873 if (IS_SPECIAL(n) || mod_mask != 0)
4874 {
4875 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
4876 int i = 0;
4877
4878 /* Turn a special key into three bytes, plus modifier. */
4879 if (mod_mask != 0)
4880 {
4881 temp[i++] = K_SPECIAL;
4882 temp[i++] = KS_MODIFIER;
4883 temp[i++] = mod_mask;
4884 }
4885 if (IS_SPECIAL(n))
4886 {
4887 temp[i++] = K_SPECIAL;
4888 temp[i++] = K_SECOND(n);
4889 temp[i++] = K_THIRD(n);
4890 }
4891#ifdef FEAT_MBYTE
4892 else if (has_mbyte)
4893 i += (*mb_char2bytes)(n, temp + i);
4894#endif
4895 else
4896 temp[i++] = n;
4897 temp[i++] = NUL;
4898 rettv->v_type = VAR_STRING;
4899 rettv->vval.v_string = vim_strsave(temp);
4900
4901#ifdef FEAT_MOUSE
4902 if (is_mouse_key(n))
4903 {
4904 int row = mouse_row;
4905 int col = mouse_col;
4906 win_T *win;
4907 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004908 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004909 int winnr = 1;
4910
4911 if (row >= 0 && col >= 0)
4912 {
4913 /* Find the window at the mouse coordinates and compute the
4914 * text position. */
4915 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02004916 if (win == NULL)
4917 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004918 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004919 for (wp = firstwin; wp != win; wp = wp->w_next)
4920 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004921 set_vim_var_nr(VV_MOUSE_WIN, winnr);
4922 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
4923 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
4924 set_vim_var_nr(VV_MOUSE_COL, col + 1);
4925 }
4926 }
4927#endif
4928 }
4929}
4930
4931/*
4932 * "getcharmod()" function
4933 */
4934 static void
4935f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
4936{
4937 rettv->vval.v_number = mod_mask;
4938}
4939
4940/*
4941 * "getcharsearch()" function
4942 */
4943 static void
4944f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
4945{
4946 if (rettv_dict_alloc(rettv) != FAIL)
4947 {
4948 dict_T *dict = rettv->vval.v_dict;
4949
Bram Moolenaare0be1672018-07-08 16:50:37 +02004950 dict_add_string(dict, "char", last_csearch());
4951 dict_add_number(dict, "forward", last_csearch_forward());
4952 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 }
4954}
4955
4956/*
4957 * "getcmdline()" function
4958 */
4959 static void
4960f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
4961{
4962 rettv->v_type = VAR_STRING;
4963 rettv->vval.v_string = get_cmdline_str();
4964}
4965
4966/*
4967 * "getcmdpos()" function
4968 */
4969 static void
4970f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
4971{
4972 rettv->vval.v_number = get_cmdline_pos() + 1;
4973}
4974
4975/*
4976 * "getcmdtype()" function
4977 */
4978 static void
4979f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
4980{
4981 rettv->v_type = VAR_STRING;
4982 rettv->vval.v_string = alloc(2);
4983 if (rettv->vval.v_string != NULL)
4984 {
4985 rettv->vval.v_string[0] = get_cmdline_type();
4986 rettv->vval.v_string[1] = NUL;
4987 }
4988}
4989
4990/*
4991 * "getcmdwintype()" function
4992 */
4993 static void
4994f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
4995{
4996 rettv->v_type = VAR_STRING;
4997 rettv->vval.v_string = NULL;
4998#ifdef FEAT_CMDWIN
4999 rettv->vval.v_string = alloc(2);
5000 if (rettv->vval.v_string != NULL)
5001 {
5002 rettv->vval.v_string[0] = cmdwin_type;
5003 rettv->vval.v_string[1] = NUL;
5004 }
5005#endif
5006}
5007
5008#if defined(FEAT_CMDL_COMPL)
5009/*
5010 * "getcompletion()" function
5011 */
5012 static void
5013f_getcompletion(typval_T *argvars, typval_T *rettv)
5014{
5015 char_u *pat;
5016 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005017 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005018 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5019 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005020
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005021 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005022 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005023
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005024 if (p_wic)
5025 options |= WILD_ICASE;
5026
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005027 /* For filtered results, 'wildignore' is used */
5028 if (!filtered)
5029 options |= WILD_KEEP_ALL;
5030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005032 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005034 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005035 if (xpc.xp_context == EXPAND_NOTHING)
5036 {
5037 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005038 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005039 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005040 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005041 return;
5042 }
5043
5044# if defined(FEAT_MENU)
5045 if (xpc.xp_context == EXPAND_MENUS)
5046 {
5047 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5048 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5049 }
5050# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005051#ifdef FEAT_CSCOPE
5052 if (xpc.xp_context == EXPAND_CSCOPE)
5053 {
5054 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5055 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5056 }
5057#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005058#ifdef FEAT_SIGNS
5059 if (xpc.xp_context == EXPAND_SIGN)
5060 {
5061 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5062 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5063 }
5064#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5067 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5068 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005069 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070
5071 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5072
5073 for (i = 0; i < xpc.xp_numfiles; i++)
5074 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5075 }
5076 vim_free(pat);
5077 ExpandCleanup(&xpc);
5078}
5079#endif
5080
5081/*
5082 * "getcwd()" function
5083 */
5084 static void
5085f_getcwd(typval_T *argvars, typval_T *rettv)
5086{
5087 win_T *wp = NULL;
5088 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005089 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090
5091 rettv->v_type = VAR_STRING;
5092 rettv->vval.v_string = NULL;
5093
Bram Moolenaar54591292018-02-09 20:53:59 +01005094 if (argvars[0].v_type == VAR_NUMBER && argvars[0].vval.v_number == -1)
5095 global = TRUE;
5096 else
5097 wp = find_tabwin(&argvars[0], &argvars[1]);
5098
5099 if (wp != NULL && wp->w_localdir != NULL)
5100 rettv->vval.v_string = vim_strsave(wp->w_localdir);
5101 else if (wp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005103 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104 rettv->vval.v_string = vim_strsave(globaldir);
5105 else
5106 {
5107 cwd = alloc(MAXPATHL);
5108 if (cwd != NULL)
5109 {
5110 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5111 rettv->vval.v_string = vim_strsave(cwd);
5112 vim_free(cwd);
5113 }
5114 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005115 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005116#ifdef BACKSLASH_IN_FILENAME
5117 if (rettv->vval.v_string != NULL)
5118 slash_adjust(rettv->vval.v_string);
5119#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120}
5121
5122/*
5123 * "getfontname()" function
5124 */
5125 static void
5126f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5127{
5128 rettv->v_type = VAR_STRING;
5129 rettv->vval.v_string = NULL;
5130#ifdef FEAT_GUI
5131 if (gui.in_use)
5132 {
5133 GuiFont font;
5134 char_u *name = NULL;
5135
5136 if (argvars[0].v_type == VAR_UNKNOWN)
5137 {
5138 /* Get the "Normal" font. Either the name saved by
5139 * hl_set_font_name() or from the font ID. */
5140 font = gui.norm_font;
5141 name = hl_get_font_name();
5142 }
5143 else
5144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005145 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005146 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5147 return;
5148 font = gui_mch_get_font(name, FALSE);
5149 if (font == NOFONT)
5150 return; /* Invalid font name, return empty string. */
5151 }
5152 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5153 if (argvars[0].v_type != VAR_UNKNOWN)
5154 gui_mch_free_font(font);
5155 }
5156#endif
5157}
5158
5159/*
5160 * "getfperm({fname})" function
5161 */
5162 static void
5163f_getfperm(typval_T *argvars, typval_T *rettv)
5164{
5165 char_u *fname;
5166 stat_T st;
5167 char_u *perm = NULL;
5168 char_u flags[] = "rwx";
5169 int i;
5170
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005171 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005172
5173 rettv->v_type = VAR_STRING;
5174 if (mch_stat((char *)fname, &st) >= 0)
5175 {
5176 perm = vim_strsave((char_u *)"---------");
5177 if (perm != NULL)
5178 {
5179 for (i = 0; i < 9; i++)
5180 {
5181 if (st.st_mode & (1 << (8 - i)))
5182 perm[i] = flags[i % 3];
5183 }
5184 }
5185 }
5186 rettv->vval.v_string = perm;
5187}
5188
5189/*
5190 * "getfsize({fname})" function
5191 */
5192 static void
5193f_getfsize(typval_T *argvars, typval_T *rettv)
5194{
5195 char_u *fname;
5196 stat_T st;
5197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005198 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005199
5200 rettv->v_type = VAR_NUMBER;
5201
5202 if (mch_stat((char *)fname, &st) >= 0)
5203 {
5204 if (mch_isdir(fname))
5205 rettv->vval.v_number = 0;
5206 else
5207 {
5208 rettv->vval.v_number = (varnumber_T)st.st_size;
5209
5210 /* non-perfect check for overflow */
5211 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5212 rettv->vval.v_number = -2;
5213 }
5214 }
5215 else
5216 rettv->vval.v_number = -1;
5217}
5218
5219/*
5220 * "getftime({fname})" function
5221 */
5222 static void
5223f_getftime(typval_T *argvars, typval_T *rettv)
5224{
5225 char_u *fname;
5226 stat_T st;
5227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005228 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229
5230 if (mch_stat((char *)fname, &st) >= 0)
5231 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5232 else
5233 rettv->vval.v_number = -1;
5234}
5235
5236/*
5237 * "getftype({fname})" function
5238 */
5239 static void
5240f_getftype(typval_T *argvars, typval_T *rettv)
5241{
5242 char_u *fname;
5243 stat_T st;
5244 char_u *type = NULL;
5245 char *t;
5246
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005247 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005248
5249 rettv->v_type = VAR_STRING;
5250 if (mch_lstat((char *)fname, &st) >= 0)
5251 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005252 if (S_ISREG(st.st_mode))
5253 t = "file";
5254 else if (S_ISDIR(st.st_mode))
5255 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256 else if (S_ISLNK(st.st_mode))
5257 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005258 else if (S_ISBLK(st.st_mode))
5259 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260 else if (S_ISCHR(st.st_mode))
5261 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262 else if (S_ISFIFO(st.st_mode))
5263 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005264 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005265 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005266 else
5267 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005268 type = vim_strsave((char_u *)t);
5269 }
5270 rettv->vval.v_string = type;
5271}
5272
5273/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005274 * "getjumplist()" function
5275 */
5276 static void
5277f_getjumplist(typval_T *argvars, typval_T *rettv)
5278{
5279#ifdef FEAT_JUMPLIST
5280 win_T *wp;
5281 int i;
5282 list_T *l;
5283 dict_T *d;
5284#endif
5285
5286 if (rettv_list_alloc(rettv) != OK)
5287 return;
5288
5289#ifdef FEAT_JUMPLIST
5290 wp = find_tabwin(&argvars[0], &argvars[1]);
5291 if (wp == NULL)
5292 return;
5293
5294 l = list_alloc();
5295 if (l == NULL)
5296 return;
5297
5298 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5299 return;
5300 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5301
Bram Moolenaar48679742018-02-13 13:33:29 +01005302 cleanup_jumplist(wp, TRUE);
5303
Bram Moolenaar4f505882018-02-10 21:06:32 +01005304 for (i = 0; i < wp->w_jumplistlen; ++i)
5305 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005306 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5307 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005308 if ((d = dict_alloc()) == NULL)
5309 return;
5310 if (list_append_dict(l, d) == FAIL)
5311 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005312 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5313 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005314# ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +02005315 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005316# endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005317 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005318 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005319 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005320 }
5321#endif
5322}
5323
5324/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 * "getline(lnum, [end])" function
5326 */
5327 static void
5328f_getline(typval_T *argvars, typval_T *rettv)
5329{
5330 linenr_T lnum;
5331 linenr_T end;
5332 int retlist;
5333
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005334 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005335 if (argvars[1].v_type == VAR_UNKNOWN)
5336 {
5337 end = 0;
5338 retlist = FALSE;
5339 }
5340 else
5341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005342 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343 retlist = TRUE;
5344 }
5345
5346 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5347}
5348
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005349#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005350 static void
5351get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5352{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005353 if (what_arg->v_type == VAR_UNKNOWN)
5354 {
5355 if (rettv_list_alloc(rettv) == OK)
5356 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005357 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005358 }
5359 else
5360 {
5361 if (rettv_dict_alloc(rettv) == OK)
5362 if (is_qf || (wp != NULL))
5363 {
5364 if (what_arg->v_type == VAR_DICT)
5365 {
5366 dict_T *d = what_arg->vval.v_dict;
5367
5368 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005369 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005370 }
5371 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005372 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005373 }
5374 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005375}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005376#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005377
5378/*
5379 * "getloclist()" function
5380 */
5381 static void
5382f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5383{
5384#ifdef FEAT_QUICKFIX
5385 win_T *wp;
5386
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005387 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005388 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5389#endif
5390}
5391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005392/*
5393 * "getmatches()" function
5394 */
5395 static void
5396f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5397{
5398#ifdef FEAT_SEARCH_EXTRA
5399 dict_T *dict;
5400 matchitem_T *cur = curwin->w_match_head;
5401 int i;
5402
5403 if (rettv_list_alloc(rettv) == OK)
5404 {
5405 while (cur != NULL)
5406 {
5407 dict = dict_alloc();
5408 if (dict == NULL)
5409 return;
5410 if (cur->match.regprog == NULL)
5411 {
5412 /* match added with matchaddpos() */
5413 for (i = 0; i < MAXPOSMATCH; ++i)
5414 {
5415 llpos_T *llpos;
5416 char buf[6];
5417 list_T *l;
5418
5419 llpos = &cur->pos.pos[i];
5420 if (llpos->lnum == 0)
5421 break;
5422 l = list_alloc();
5423 if (l == NULL)
5424 break;
5425 list_append_number(l, (varnumber_T)llpos->lnum);
5426 if (llpos->col > 0)
5427 {
5428 list_append_number(l, (varnumber_T)llpos->col);
5429 list_append_number(l, (varnumber_T)llpos->len);
5430 }
5431 sprintf(buf, "pos%d", i + 1);
5432 dict_add_list(dict, buf, l);
5433 }
5434 }
5435 else
5436 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005437 dict_add_string(dict, "pattern", cur->pattern);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438 }
Bram Moolenaare0be1672018-07-08 16:50:37 +02005439 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5440 dict_add_number(dict, "priority", (long)cur->priority);
5441 dict_add_number(dict, "id", (long)cur->id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005442# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
5443 if (cur->conceal_char)
5444 {
5445 char_u buf[MB_MAXBYTES + 1];
5446
5447 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005448 dict_add_string(dict, "conceal", (char_u *)&buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005449 }
5450# endif
5451 list_append_dict(rettv->vval.v_list, dict);
5452 cur = cur->next;
5453 }
5454 }
5455#endif
5456}
5457
5458/*
5459 * "getpid()" function
5460 */
5461 static void
5462f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5463{
5464 rettv->vval.v_number = mch_get_pid();
5465}
5466
5467 static void
5468getpos_both(
5469 typval_T *argvars,
5470 typval_T *rettv,
5471 int getcurpos)
5472{
5473 pos_T *fp;
5474 list_T *l;
5475 int fnum = -1;
5476
5477 if (rettv_list_alloc(rettv) == OK)
5478 {
5479 l = rettv->vval.v_list;
5480 if (getcurpos)
5481 fp = &curwin->w_cursor;
5482 else
5483 fp = var2fpos(&argvars[0], TRUE, &fnum);
5484 if (fnum != -1)
5485 list_append_number(l, (varnumber_T)fnum);
5486 else
5487 list_append_number(l, (varnumber_T)0);
5488 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5489 : (varnumber_T)0);
5490 list_append_number(l, (fp != NULL)
5491 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5492 : (varnumber_T)0);
5493 list_append_number(l,
5494#ifdef FEAT_VIRTUALEDIT
5495 (fp != NULL) ? (varnumber_T)fp->coladd :
5496#endif
5497 (varnumber_T)0);
5498 if (getcurpos)
5499 {
5500 update_curswant();
5501 list_append_number(l, curwin->w_curswant == MAXCOL ?
5502 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
5503 }
5504 }
5505 else
5506 rettv->vval.v_number = FALSE;
5507}
5508
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005509/*
5510 * "getcurpos()" function
5511 */
5512 static void
5513f_getcurpos(typval_T *argvars, typval_T *rettv)
5514{
5515 getpos_both(argvars, rettv, TRUE);
5516}
5517
5518/*
5519 * "getpos(string)" function
5520 */
5521 static void
5522f_getpos(typval_T *argvars, typval_T *rettv)
5523{
5524 getpos_both(argvars, rettv, FALSE);
5525}
5526
5527/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005528 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529 */
5530 static void
5531f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5532{
5533#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005534 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005535#endif
5536}
5537
5538/*
5539 * "getreg()" function
5540 */
5541 static void
5542f_getreg(typval_T *argvars, typval_T *rettv)
5543{
5544 char_u *strregname;
5545 int regname;
5546 int arg2 = FALSE;
5547 int return_list = FALSE;
5548 int error = FALSE;
5549
5550 if (argvars[0].v_type != VAR_UNKNOWN)
5551 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005552 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 error = strregname == NULL;
5554 if (argvars[1].v_type != VAR_UNKNOWN)
5555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005558 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005559 }
5560 }
5561 else
5562 strregname = get_vim_var_str(VV_REG);
5563
5564 if (error)
5565 return;
5566
5567 regname = (strregname == NULL ? '"' : *strregname);
5568 if (regname == 0)
5569 regname = '"';
5570
5571 if (return_list)
5572 {
5573 rettv->v_type = VAR_LIST;
5574 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5575 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5576 if (rettv->vval.v_list == NULL)
5577 (void)rettv_list_alloc(rettv);
5578 else
5579 ++rettv->vval.v_list->lv_refcount;
5580 }
5581 else
5582 {
5583 rettv->v_type = VAR_STRING;
5584 rettv->vval.v_string = get_reg_contents(regname,
5585 arg2 ? GREG_EXPR_SRC : 0);
5586 }
5587}
5588
5589/*
5590 * "getregtype()" function
5591 */
5592 static void
5593f_getregtype(typval_T *argvars, typval_T *rettv)
5594{
5595 char_u *strregname;
5596 int regname;
5597 char_u buf[NUMBUFLEN + 2];
5598 long reglen = 0;
5599
5600 if (argvars[0].v_type != VAR_UNKNOWN)
5601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005602 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005603 if (strregname == NULL) /* type error; errmsg already given */
5604 {
5605 rettv->v_type = VAR_STRING;
5606 rettv->vval.v_string = NULL;
5607 return;
5608 }
5609 }
5610 else
5611 /* Default to v:register */
5612 strregname = get_vim_var_str(VV_REG);
5613
5614 regname = (strregname == NULL ? '"' : *strregname);
5615 if (regname == 0)
5616 regname = '"';
5617
5618 buf[0] = NUL;
5619 buf[1] = NUL;
5620 switch (get_reg_type(regname, &reglen))
5621 {
5622 case MLINE: buf[0] = 'V'; break;
5623 case MCHAR: buf[0] = 'v'; break;
5624 case MBLOCK:
5625 buf[0] = Ctrl_V;
5626 sprintf((char *)buf + 1, "%ld", reglen + 1);
5627 break;
5628 }
5629 rettv->v_type = VAR_STRING;
5630 rettv->vval.v_string = vim_strsave(buf);
5631}
5632
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005633/*
5634 * Returns information (variables, options, etc.) about a tab page
5635 * as a dictionary.
5636 */
5637 static dict_T *
5638get_tabpage_info(tabpage_T *tp, int tp_idx)
5639{
5640 win_T *wp;
5641 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005642 list_T *l;
5643
5644 dict = dict_alloc();
5645 if (dict == NULL)
5646 return NULL;
5647
Bram Moolenaare0be1672018-07-08 16:50:37 +02005648 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005649
5650 l = list_alloc();
5651 if (l != NULL)
5652 {
5653 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5654 wp; wp = wp->w_next)
5655 list_append_number(l, (varnumber_T)wp->w_id);
5656 dict_add_list(dict, "windows", l);
5657 }
5658
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005659 /* Make a reference to tabpage variables */
5660 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005661
5662 return dict;
5663}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005664
5665/*
5666 * "gettabinfo()" function
5667 */
5668 static void
5669f_gettabinfo(typval_T *argvars, typval_T *rettv)
5670{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005671 tabpage_T *tp, *tparg = NULL;
5672 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005673 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005674
5675 if (rettv_list_alloc(rettv) != OK)
5676 return;
5677
5678 if (argvars[0].v_type != VAR_UNKNOWN)
5679 {
5680 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005681 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005682 if (tparg == NULL)
5683 return;
5684 }
5685
5686 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005687 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005688 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005689 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005690 if (tparg != NULL && tp != tparg)
5691 continue;
5692 d = get_tabpage_info(tp, tpnr);
5693 if (d != NULL)
5694 list_append_dict(rettv->vval.v_list, d);
5695 if (tparg != NULL)
5696 return;
5697 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005698}
5699
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700/*
5701 * "gettabvar()" function
5702 */
5703 static void
5704f_gettabvar(typval_T *argvars, typval_T *rettv)
5705{
5706 win_T *oldcurwin;
5707 tabpage_T *tp, *oldtabpage;
5708 dictitem_T *v;
5709 char_u *varname;
5710 int done = FALSE;
5711
5712 rettv->v_type = VAR_STRING;
5713 rettv->vval.v_string = NULL;
5714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005715 varname = tv_get_string_chk(&argvars[1]);
5716 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 if (tp != NULL && varname != NULL)
5718 {
5719 /* Set tp to be our tabpage, temporarily. Also set the window to the
5720 * first window in the tabpage, otherwise the window is not valid. */
5721 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005722 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5723 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 {
5725 /* look up the variable */
5726 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5727 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5728 if (v != NULL)
5729 {
5730 copy_tv(&v->di_tv, rettv);
5731 done = TRUE;
5732 }
5733 }
5734
5735 /* restore previous notion of curwin */
5736 restore_win(oldcurwin, oldtabpage, TRUE);
5737 }
5738
5739 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5740 copy_tv(&argvars[2], rettv);
5741}
5742
5743/*
5744 * "gettabwinvar()" function
5745 */
5746 static void
5747f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5748{
5749 getwinvar(argvars, rettv, 1);
5750}
5751
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005752/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005753 * "gettagstack()" function
5754 */
5755 static void
5756f_gettagstack(typval_T *argvars, typval_T *rettv)
5757{
5758 win_T *wp = curwin; // default is current window
5759
5760 if (rettv_dict_alloc(rettv) != OK)
5761 return;
5762
5763 if (argvars[0].v_type != VAR_UNKNOWN)
5764 {
5765 wp = find_win_by_nr_or_id(&argvars[0]);
5766 if (wp == NULL)
5767 return;
5768 }
5769
5770 get_tagstack(wp, rettv->vval.v_dict);
5771}
5772
5773/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005774 * Returns information about a window as a dictionary.
5775 */
5776 static dict_T *
5777get_win_info(win_T *wp, short tpnr, short winnr)
5778{
5779 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005780
5781 dict = dict_alloc();
5782 if (dict == NULL)
5783 return NULL;
5784
Bram Moolenaare0be1672018-07-08 16:50:37 +02005785 dict_add_number(dict, "tabnr", tpnr);
5786 dict_add_number(dict, "winnr", winnr);
5787 dict_add_number(dict, "winid", wp->w_id);
5788 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005789 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005790#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02005791 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005792#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02005793 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02005794 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005795 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005796
Bram Moolenaar69905d12017-08-13 18:14:47 +02005797#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02005798 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02005799#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02005800#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02005801 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
5802 dict_add_number(dict, "loclist",
5803 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02005804#endif
5805
Bram Moolenaar30567352016-08-27 21:25:44 +02005806 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005807 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005808
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005809 return dict;
5810}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005811
5812/*
5813 * "getwininfo()" function
5814 */
5815 static void
5816f_getwininfo(typval_T *argvars, typval_T *rettv)
5817{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005818 tabpage_T *tp;
5819 win_T *wp = NULL, *wparg = NULL;
5820 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005821 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005822
5823 if (rettv_list_alloc(rettv) != OK)
5824 return;
5825
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005826 if (argvars[0].v_type != VAR_UNKNOWN)
5827 {
5828 wparg = win_id2wp(argvars);
5829 if (wparg == NULL)
5830 return;
5831 }
5832
5833 /* Collect information about either all the windows across all the tab
5834 * pages or one particular window.
5835 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005836 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005837 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005838 tabnr++;
5839 winnr = 0;
5840 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005841 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005842 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005843 if (wparg != NULL && wp != wparg)
5844 continue;
5845 d = get_win_info(wp, tabnr, winnr);
5846 if (d != NULL)
5847 list_append_dict(rettv->vval.v_list, d);
5848 if (wparg != NULL)
5849 /* found information about a specific window */
5850 return;
5851 }
5852 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005853}
5854
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005855/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 * "win_findbuf()" function
5857 */
5858 static void
5859f_win_findbuf(typval_T *argvars, typval_T *rettv)
5860{
5861 if (rettv_list_alloc(rettv) != FAIL)
5862 win_findbuf(argvars, rettv->vval.v_list);
5863}
5864
5865/*
5866 * "win_getid()" function
5867 */
5868 static void
5869f_win_getid(typval_T *argvars, typval_T *rettv)
5870{
5871 rettv->vval.v_number = win_getid(argvars);
5872}
5873
5874/*
5875 * "win_gotoid()" function
5876 */
5877 static void
5878f_win_gotoid(typval_T *argvars, typval_T *rettv)
5879{
5880 rettv->vval.v_number = win_gotoid(argvars);
5881}
5882
5883/*
5884 * "win_id2tabwin()" function
5885 */
5886 static void
5887f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
5888{
5889 if (rettv_list_alloc(rettv) != FAIL)
5890 win_id2tabwin(argvars, rettv->vval.v_list);
5891}
5892
5893/*
5894 * "win_id2win()" function
5895 */
5896 static void
5897f_win_id2win(typval_T *argvars, typval_T *rettv)
5898{
5899 rettv->vval.v_number = win_id2win(argvars);
5900}
5901
5902/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005903 * "win_screenpos()" function
5904 */
5905 static void
5906f_win_screenpos(typval_T *argvars, typval_T *rettv)
5907{
5908 win_T *wp;
5909
5910 if (rettv_list_alloc(rettv) == FAIL)
5911 return;
5912
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005913 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01005914 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
5915 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
5916}
5917
5918/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005919 * "getwinpos({timeout})" function
5920 */
5921 static void
5922f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
5923{
5924 int x = -1;
5925 int y = -1;
5926
5927 if (rettv_list_alloc(rettv) == FAIL)
5928 return;
5929#ifdef FEAT_GUI
5930 if (gui.in_use)
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01005931 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005932# if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5933 else
5934# endif
5935#endif
5936#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5937 {
5938 varnumber_T timeout = 100;
5939
5940 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005941 timeout = tv_get_number(&argvars[0]);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005942 term_get_winpos(&x, &y, timeout);
5943 }
5944#endif
5945 list_append_number(rettv->vval.v_list, (varnumber_T)x);
5946 list_append_number(rettv->vval.v_list, (varnumber_T)y);
5947}
5948
5949
5950/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005951 * "getwinposx()" function
5952 */
5953 static void
5954f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
5955{
5956 rettv->vval.v_number = -1;
5957#ifdef FEAT_GUI
5958 if (gui.in_use)
5959 {
5960 int x, y;
5961
5962 if (gui_mch_get_winpos(&x, &y) == OK)
5963 rettv->vval.v_number = x;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005964 return;
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005965 }
5966#endif
5967#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5968 {
5969 int x, y;
5970
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005971 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005972 rettv->vval.v_number = x;
5973 }
5974#endif
5975}
5976
5977/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005978 * "getwinposy()" function
5979 */
5980 static void
5981f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
5982{
5983 rettv->vval.v_number = -1;
5984#ifdef FEAT_GUI
5985 if (gui.in_use)
5986 {
5987 int x, y;
5988
5989 if (gui_mch_get_winpos(&x, &y) == OK)
5990 rettv->vval.v_number = y;
Bram Moolenaar7860bac2017-04-09 15:03:15 +02005991 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005992 }
5993#endif
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005994#if defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)
5995 {
5996 int x, y;
5997
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01005998 if (term_get_winpos(&x, &y, (varnumber_T)100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02005999 rettv->vval.v_number = y;
6000 }
6001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006002}
6003
6004/*
6005 * "getwinvar()" function
6006 */
6007 static void
6008f_getwinvar(typval_T *argvars, typval_T *rettv)
6009{
6010 getwinvar(argvars, rettv, 0);
6011}
6012
6013/*
6014 * "glob()" function
6015 */
6016 static void
6017f_glob(typval_T *argvars, typval_T *rettv)
6018{
6019 int options = WILD_SILENT|WILD_USE_NL;
6020 expand_T xpc;
6021 int error = FALSE;
6022
6023 /* When the optional second argument is non-zero, don't remove matches
6024 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6025 rettv->v_type = VAR_STRING;
6026 if (argvars[1].v_type != VAR_UNKNOWN)
6027 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006028 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 options |= WILD_KEEP_ALL;
6030 if (argvars[2].v_type != VAR_UNKNOWN)
6031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006032 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006033 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006034 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006035 }
6036 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006037 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006038 options |= WILD_ALLLINKS;
6039 }
6040 }
6041 if (!error)
6042 {
6043 ExpandInit(&xpc);
6044 xpc.xp_context = EXPAND_FILES;
6045 if (p_wic)
6046 options += WILD_ICASE;
6047 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006048 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006049 NULL, options, WILD_ALL);
6050 else if (rettv_list_alloc(rettv) != FAIL)
6051 {
6052 int i;
6053
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006054 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006055 NULL, options, WILD_ALL_KEEP);
6056 for (i = 0; i < xpc.xp_numfiles; i++)
6057 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6058
6059 ExpandCleanup(&xpc);
6060 }
6061 }
6062 else
6063 rettv->vval.v_string = NULL;
6064}
6065
6066/*
6067 * "globpath()" function
6068 */
6069 static void
6070f_globpath(typval_T *argvars, typval_T *rettv)
6071{
6072 int flags = 0;
6073 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006074 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006075 int error = FALSE;
6076 garray_T ga;
6077 int i;
6078
6079 /* When the optional second argument is non-zero, don't remove matches
6080 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6081 rettv->v_type = VAR_STRING;
6082 if (argvars[2].v_type != VAR_UNKNOWN)
6083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006084 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006085 flags |= WILD_KEEP_ALL;
6086 if (argvars[3].v_type != VAR_UNKNOWN)
6087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006088 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006089 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006090 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006091 }
6092 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006093 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006094 flags |= WILD_ALLLINKS;
6095 }
6096 }
6097 if (file != NULL && !error)
6098 {
6099 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006100 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006101 if (rettv->v_type == VAR_STRING)
6102 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6103 else if (rettv_list_alloc(rettv) != FAIL)
6104 for (i = 0; i < ga.ga_len; ++i)
6105 list_append_string(rettv->vval.v_list,
6106 ((char_u **)(ga.ga_data))[i], -1);
6107 ga_clear_strings(&ga);
6108 }
6109 else
6110 rettv->vval.v_string = NULL;
6111}
6112
6113/*
6114 * "glob2regpat()" function
6115 */
6116 static void
6117f_glob2regpat(typval_T *argvars, typval_T *rettv)
6118{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006119 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006120
6121 rettv->v_type = VAR_STRING;
6122 rettv->vval.v_string = (pat == NULL)
6123 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6124}
6125
6126/* for VIM_VERSION_ defines */
6127#include "version.h"
6128
6129/*
6130 * "has()" function
6131 */
6132 static void
6133f_has(typval_T *argvars, typval_T *rettv)
6134{
6135 int i;
6136 char_u *name;
6137 int n = FALSE;
6138 static char *(has_list[]) =
6139 {
6140#ifdef AMIGA
6141 "amiga",
6142# ifdef FEAT_ARP
6143 "arp",
6144# endif
6145#endif
6146#ifdef __BEOS__
6147 "beos",
6148#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006149#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006150 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6151 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006152# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006153 "macunix", /* Mac OS X, with the darwin feature */
6154 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006155# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156#endif
6157#ifdef __QNX__
6158 "qnx",
6159#endif
6160#ifdef UNIX
6161 "unix",
6162#endif
6163#ifdef VMS
6164 "vms",
6165#endif
6166#ifdef WIN32
6167 "win32",
6168#endif
6169#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6170 "win32unix",
6171#endif
6172#if defined(WIN64) || defined(_WIN64)
6173 "win64",
6174#endif
6175#ifdef EBCDIC
6176 "ebcdic",
6177#endif
6178#ifndef CASE_INSENSITIVE_FILENAME
6179 "fname_case",
6180#endif
6181#ifdef HAVE_ACL
6182 "acl",
6183#endif
6184#ifdef FEAT_ARABIC
6185 "arabic",
6186#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006187 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006188#ifdef FEAT_AUTOCHDIR
6189 "autochdir",
6190#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006191#ifdef FEAT_AUTOSERVERNAME
6192 "autoservername",
6193#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006194#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006195 "balloon_eval",
6196# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
6197 "balloon_multiline",
6198# endif
6199#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006200#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006201 "balloon_eval_term",
6202#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006203#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6204 "builtin_terms",
6205# ifdef ALL_BUILTIN_TCAPS
6206 "all_builtin_terms",
6207# endif
6208#endif
6209#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
6210 || defined(FEAT_GUI_W32) \
6211 || defined(FEAT_GUI_MOTIF))
6212 "browsefilter",
6213#endif
6214#ifdef FEAT_BYTEOFF
6215 "byte_offset",
6216#endif
6217#ifdef FEAT_JOB_CHANNEL
6218 "channel",
6219#endif
6220#ifdef FEAT_CINDENT
6221 "cindent",
6222#endif
6223#ifdef FEAT_CLIENTSERVER
6224 "clientserver",
6225#endif
6226#ifdef FEAT_CLIPBOARD
6227 "clipboard",
6228#endif
6229#ifdef FEAT_CMDL_COMPL
6230 "cmdline_compl",
6231#endif
6232#ifdef FEAT_CMDHIST
6233 "cmdline_hist",
6234#endif
6235#ifdef FEAT_COMMENTS
6236 "comments",
6237#endif
6238#ifdef FEAT_CONCEAL
6239 "conceal",
6240#endif
6241#ifdef FEAT_CRYPT
6242 "cryptv",
6243 "crypt-blowfish",
6244 "crypt-blowfish2",
6245#endif
6246#ifdef FEAT_CSCOPE
6247 "cscope",
6248#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006249 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006250#ifdef CURSOR_SHAPE
6251 "cursorshape",
6252#endif
6253#ifdef DEBUG
6254 "debug",
6255#endif
6256#ifdef FEAT_CON_DIALOG
6257 "dialog_con",
6258#endif
6259#ifdef FEAT_GUI_DIALOG
6260 "dialog_gui",
6261#endif
6262#ifdef FEAT_DIFF
6263 "diff",
6264#endif
6265#ifdef FEAT_DIGRAPHS
6266 "digraphs",
6267#endif
6268#ifdef FEAT_DIRECTX
6269 "directx",
6270#endif
6271#ifdef FEAT_DND
6272 "dnd",
6273#endif
6274#ifdef FEAT_EMACS_TAGS
6275 "emacs_tags",
6276#endif
6277 "eval", /* always present, of course! */
6278 "ex_extra", /* graduated feature */
6279#ifdef FEAT_SEARCH_EXTRA
6280 "extra_search",
6281#endif
6282#ifdef FEAT_FKMAP
6283 "farsi",
6284#endif
6285#ifdef FEAT_SEARCHPATH
6286 "file_in_path",
6287#endif
6288#ifdef FEAT_FILTERPIPE
6289 "filterpipe",
6290#endif
6291#ifdef FEAT_FIND_ID
6292 "find_in_path",
6293#endif
6294#ifdef FEAT_FLOAT
6295 "float",
6296#endif
6297#ifdef FEAT_FOLDING
6298 "folding",
6299#endif
6300#ifdef FEAT_FOOTER
6301 "footer",
6302#endif
6303#if !defined(USE_SYSTEM) && defined(UNIX)
6304 "fork",
6305#endif
6306#ifdef FEAT_GETTEXT
6307 "gettext",
6308#endif
6309#ifdef FEAT_GUI
6310 "gui",
6311#endif
6312#ifdef FEAT_GUI_ATHENA
6313# ifdef FEAT_GUI_NEXTAW
6314 "gui_neXtaw",
6315# else
6316 "gui_athena",
6317# endif
6318#endif
6319#ifdef FEAT_GUI_GTK
6320 "gui_gtk",
6321# ifdef USE_GTK3
6322 "gui_gtk3",
6323# else
6324 "gui_gtk2",
6325# endif
6326#endif
6327#ifdef FEAT_GUI_GNOME
6328 "gui_gnome",
6329#endif
6330#ifdef FEAT_GUI_MAC
6331 "gui_mac",
6332#endif
6333#ifdef FEAT_GUI_MOTIF
6334 "gui_motif",
6335#endif
6336#ifdef FEAT_GUI_PHOTON
6337 "gui_photon",
6338#endif
6339#ifdef FEAT_GUI_W32
6340 "gui_win32",
6341#endif
6342#ifdef FEAT_HANGULIN
6343 "hangul_input",
6344#endif
6345#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6346 "iconv",
6347#endif
6348#ifdef FEAT_INS_EXPAND
6349 "insert_expand",
6350#endif
6351#ifdef FEAT_JOB_CHANNEL
6352 "job",
6353#endif
6354#ifdef FEAT_JUMPLIST
6355 "jumplist",
6356#endif
6357#ifdef FEAT_KEYMAP
6358 "keymap",
6359#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006360 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006361#ifdef FEAT_LANGMAP
6362 "langmap",
6363#endif
6364#ifdef FEAT_LIBCALL
6365 "libcall",
6366#endif
6367#ifdef FEAT_LINEBREAK
6368 "linebreak",
6369#endif
6370#ifdef FEAT_LISP
6371 "lispindent",
6372#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006373 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006374#ifdef FEAT_LOCALMAP
6375 "localmap",
6376#endif
6377#ifdef FEAT_LUA
6378# ifndef DYNAMIC_LUA
6379 "lua",
6380# endif
6381#endif
6382#ifdef FEAT_MENU
6383 "menu",
6384#endif
6385#ifdef FEAT_SESSION
6386 "mksession",
6387#endif
6388#ifdef FEAT_MODIFY_FNAME
6389 "modify_fname",
6390#endif
6391#ifdef FEAT_MOUSE
6392 "mouse",
6393#endif
6394#ifdef FEAT_MOUSESHAPE
6395 "mouseshape",
6396#endif
6397#if defined(UNIX) || defined(VMS)
6398# ifdef FEAT_MOUSE_DEC
6399 "mouse_dec",
6400# endif
6401# ifdef FEAT_MOUSE_GPM
6402 "mouse_gpm",
6403# endif
6404# ifdef FEAT_MOUSE_JSB
6405 "mouse_jsbterm",
6406# endif
6407# ifdef FEAT_MOUSE_NET
6408 "mouse_netterm",
6409# endif
6410# ifdef FEAT_MOUSE_PTERM
6411 "mouse_pterm",
6412# endif
6413# ifdef FEAT_MOUSE_SGR
6414 "mouse_sgr",
6415# endif
6416# ifdef FEAT_SYSMOUSE
6417 "mouse_sysmouse",
6418# endif
6419# ifdef FEAT_MOUSE_URXVT
6420 "mouse_urxvt",
6421# endif
6422# ifdef FEAT_MOUSE_XTERM
6423 "mouse_xterm",
6424# endif
6425#endif
6426#ifdef FEAT_MBYTE
6427 "multi_byte",
6428#endif
6429#ifdef FEAT_MBYTE_IME
6430 "multi_byte_ime",
6431#endif
6432#ifdef FEAT_MULTI_LANG
6433 "multi_lang",
6434#endif
6435#ifdef FEAT_MZSCHEME
6436#ifndef DYNAMIC_MZSCHEME
6437 "mzscheme",
6438#endif
6439#endif
6440#ifdef FEAT_NUM64
6441 "num64",
6442#endif
6443#ifdef FEAT_OLE
6444 "ole",
6445#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006446#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006447 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006448#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006449#ifdef FEAT_PATH_EXTRA
6450 "path_extra",
6451#endif
6452#ifdef FEAT_PERL
6453#ifndef DYNAMIC_PERL
6454 "perl",
6455#endif
6456#endif
6457#ifdef FEAT_PERSISTENT_UNDO
6458 "persistent_undo",
6459#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006460#if defined(FEAT_PYTHON)
6461 "python_compiled",
6462# if defined(DYNAMIC_PYTHON)
6463 "python_dynamic",
6464# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "python",
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
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006469#if defined(FEAT_PYTHON3)
6470 "python3_compiled",
6471# if defined(DYNAMIC_PYTHON3)
6472 "python3_dynamic",
6473# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006475 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006476# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477#endif
6478#ifdef FEAT_POSTSCRIPT
6479 "postscript",
6480#endif
6481#ifdef FEAT_PRINTER
6482 "printer",
6483#endif
6484#ifdef FEAT_PROFILE
6485 "profile",
6486#endif
6487#ifdef FEAT_RELTIME
6488 "reltime",
6489#endif
6490#ifdef FEAT_QUICKFIX
6491 "quickfix",
6492#endif
6493#ifdef FEAT_RIGHTLEFT
6494 "rightleft",
6495#endif
6496#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6497 "ruby",
6498#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006499 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006500#ifdef FEAT_CMDL_INFO
6501 "showcmd",
6502 "cmdline_info",
6503#endif
6504#ifdef FEAT_SIGNS
6505 "signs",
6506#endif
6507#ifdef FEAT_SMARTINDENT
6508 "smartindent",
6509#endif
6510#ifdef STARTUPTIME
6511 "startuptime",
6512#endif
6513#ifdef FEAT_STL_OPT
6514 "statusline",
6515#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516#ifdef FEAT_NETBEANS_INTG
6517 "netbeans_intg",
6518#endif
6519#ifdef FEAT_SPELL
6520 "spell",
6521#endif
6522#ifdef FEAT_SYN_HL
6523 "syntax",
6524#endif
6525#if defined(USE_SYSTEM) || !defined(UNIX)
6526 "system",
6527#endif
6528#ifdef FEAT_TAG_BINS
6529 "tag_binary",
6530#endif
6531#ifdef FEAT_TAG_OLDSTATIC
6532 "tag_old_static",
6533#endif
6534#ifdef FEAT_TAG_ANYWHITE
6535 "tag_any_white",
6536#endif
6537#ifdef FEAT_TCL
6538# ifndef DYNAMIC_TCL
6539 "tcl",
6540# endif
6541#endif
6542#ifdef FEAT_TERMGUICOLORS
6543 "termguicolors",
6544#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006545#if defined(FEAT_TERMINAL) && !defined(WIN3264)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006546 "terminal",
6547#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006548#ifdef TERMINFO
6549 "terminfo",
6550#endif
6551#ifdef FEAT_TERMRESPONSE
6552 "termresponse",
6553#endif
6554#ifdef FEAT_TEXTOBJ
6555 "textobjects",
6556#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006557#ifdef FEAT_TEXT_PROP
6558 "textprop",
6559#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006560#ifdef HAVE_TGETENT
6561 "tgetent",
6562#endif
6563#ifdef FEAT_TIMERS
6564 "timers",
6565#endif
6566#ifdef FEAT_TITLE
6567 "title",
6568#endif
6569#ifdef FEAT_TOOLBAR
6570 "toolbar",
6571#endif
6572#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6573 "unnamedplus",
6574#endif
6575#ifdef FEAT_USR_CMDS
6576 "user-commands", /* was accidentally included in 5.4 */
6577 "user_commands",
6578#endif
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006579#ifdef FEAT_VARTABS
6580 "vartabs",
6581#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006582#ifdef FEAT_VIMINFO
6583 "viminfo",
6584#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006585 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#ifdef FEAT_VIRTUALEDIT
6587 "virtualedit",
6588#endif
6589 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006592#ifdef FEAT_VTP
6593 "vtp",
6594#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006595#ifdef FEAT_WILDIGN
6596 "wildignore",
6597#endif
6598#ifdef FEAT_WILDMENU
6599 "wildmenu",
6600#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006602#ifdef FEAT_WAK
6603 "winaltkeys",
6604#endif
6605#ifdef FEAT_WRITEBACKUP
6606 "writebackup",
6607#endif
6608#ifdef FEAT_XIM
6609 "xim",
6610#endif
6611#ifdef FEAT_XFONTSET
6612 "xfontset",
6613#endif
6614#ifdef FEAT_XPM_W32
6615 "xpm",
6616 "xpm_w32", /* for backward compatibility */
6617#else
6618# if defined(HAVE_XPM)
6619 "xpm",
6620# endif
6621#endif
6622#ifdef USE_XSMP
6623 "xsmp",
6624#endif
6625#ifdef USE_XSMP_INTERACT
6626 "xsmp_interact",
6627#endif
6628#ifdef FEAT_XCLIPBOARD
6629 "xterm_clipboard",
6630#endif
6631#ifdef FEAT_XTERM_SAVE
6632 "xterm_save",
6633#endif
6634#if defined(UNIX) && defined(FEAT_X11)
6635 "X11",
6636#endif
6637 NULL
6638 };
6639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006640 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 for (i = 0; has_list[i] != NULL; ++i)
6642 if (STRICMP(name, has_list[i]) == 0)
6643 {
6644 n = TRUE;
6645 break;
6646 }
6647
6648 if (n == FALSE)
6649 {
6650 if (STRNICMP(name, "patch", 5) == 0)
6651 {
6652 if (name[5] == '-'
6653 && STRLEN(name) >= 11
6654 && vim_isdigit(name[6])
6655 && vim_isdigit(name[8])
6656 && vim_isdigit(name[10]))
6657 {
6658 int major = atoi((char *)name + 6);
6659 int minor = atoi((char *)name + 8);
6660
6661 /* Expect "patch-9.9.01234". */
6662 n = (major < VIM_VERSION_MAJOR
6663 || (major == VIM_VERSION_MAJOR
6664 && (minor < VIM_VERSION_MINOR
6665 || (minor == VIM_VERSION_MINOR
6666 && has_patch(atoi((char *)name + 10))))));
6667 }
6668 else
6669 n = has_patch(atoi((char *)name + 5));
6670 }
6671 else if (STRICMP(name, "vim_starting") == 0)
6672 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006673 else if (STRICMP(name, "ttyin") == 0)
6674 n = mch_input_isatty();
6675 else if (STRICMP(name, "ttyout") == 0)
6676 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677#ifdef FEAT_MBYTE
6678 else if (STRICMP(name, "multi_byte_encoding") == 0)
6679 n = has_mbyte;
6680#endif
6681#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
6682 else if (STRICMP(name, "balloon_multiline") == 0)
6683 n = multiline_balloon_available();
6684#endif
6685#ifdef DYNAMIC_TCL
6686 else if (STRICMP(name, "tcl") == 0)
6687 n = tcl_enabled(FALSE);
6688#endif
6689#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6690 else if (STRICMP(name, "iconv") == 0)
6691 n = iconv_enabled(FALSE);
6692#endif
6693#ifdef DYNAMIC_LUA
6694 else if (STRICMP(name, "lua") == 0)
6695 n = lua_enabled(FALSE);
6696#endif
6697#ifdef DYNAMIC_MZSCHEME
6698 else if (STRICMP(name, "mzscheme") == 0)
6699 n = mzscheme_enabled(FALSE);
6700#endif
6701#ifdef DYNAMIC_RUBY
6702 else if (STRICMP(name, "ruby") == 0)
6703 n = ruby_enabled(FALSE);
6704#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006705#ifdef DYNAMIC_PYTHON
6706 else if (STRICMP(name, "python") == 0)
6707 n = python_enabled(FALSE);
6708#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006709#ifdef DYNAMIC_PYTHON3
6710 else if (STRICMP(name, "python3") == 0)
6711 n = python3_enabled(FALSE);
6712#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006713#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6714 else if (STRICMP(name, "pythonx") == 0)
6715 {
6716# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6717 if (p_pyx == 0)
6718 n = python3_enabled(FALSE) || python_enabled(FALSE);
6719 else if (p_pyx == 3)
6720 n = python3_enabled(FALSE);
6721 else if (p_pyx == 2)
6722 n = python_enabled(FALSE);
6723# elif defined(DYNAMIC_PYTHON)
6724 n = python_enabled(FALSE);
6725# elif defined(DYNAMIC_PYTHON3)
6726 n = python3_enabled(FALSE);
6727# endif
6728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729#endif
6730#ifdef DYNAMIC_PERL
6731 else if (STRICMP(name, "perl") == 0)
6732 n = perl_enabled(FALSE);
6733#endif
6734#ifdef FEAT_GUI
6735 else if (STRICMP(name, "gui_running") == 0)
6736 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006737# ifdef FEAT_BROWSE
6738 else if (STRICMP(name, "browse") == 0)
6739 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6740# endif
6741#endif
6742#ifdef FEAT_SYN_HL
6743 else if (STRICMP(name, "syntax_items") == 0)
6744 n = syntax_present(curwin);
6745#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006746#ifdef FEAT_VTP
6747 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006748 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006749#endif
6750#ifdef FEAT_NETBEANS_INTG
6751 else if (STRICMP(name, "netbeans_enabled") == 0)
6752 n = netbeans_active();
6753#endif
Bram Moolenaara83e3962017-08-17 14:39:07 +02006754#if defined(FEAT_TERMINAL) && defined(WIN3264)
6755 else if (STRICMP(name, "terminal") == 0)
6756 n = terminal_enabled();
6757#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006758 }
6759
6760 rettv->vval.v_number = n;
6761}
6762
6763/*
6764 * "has_key()" function
6765 */
6766 static void
6767f_has_key(typval_T *argvars, typval_T *rettv)
6768{
6769 if (argvars[0].v_type != VAR_DICT)
6770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006771 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772 return;
6773 }
6774 if (argvars[0].vval.v_dict == NULL)
6775 return;
6776
6777 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006778 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779}
6780
6781/*
6782 * "haslocaldir()" function
6783 */
6784 static void
6785f_haslocaldir(typval_T *argvars, typval_T *rettv)
6786{
6787 win_T *wp = NULL;
6788
6789 wp = find_tabwin(&argvars[0], &argvars[1]);
6790 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
6791}
6792
6793/*
6794 * "hasmapto()" function
6795 */
6796 static void
6797f_hasmapto(typval_T *argvars, typval_T *rettv)
6798{
6799 char_u *name;
6800 char_u *mode;
6801 char_u buf[NUMBUFLEN];
6802 int abbr = FALSE;
6803
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006804 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 if (argvars[1].v_type == VAR_UNKNOWN)
6806 mode = (char_u *)"nvo";
6807 else
6808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006809 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006811 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 }
6813
6814 if (map_to_exists(name, mode, abbr))
6815 rettv->vval.v_number = TRUE;
6816 else
6817 rettv->vval.v_number = FALSE;
6818}
6819
6820/*
6821 * "histadd()" function
6822 */
6823 static void
6824f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
6825{
6826#ifdef FEAT_CMDHIST
6827 int histype;
6828 char_u *str;
6829 char_u buf[NUMBUFLEN];
6830#endif
6831
6832 rettv->vval.v_number = FALSE;
6833 if (check_restricted() || check_secure())
6834 return;
6835#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006836 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837 histype = str != NULL ? get_histtype(str) : -1;
6838 if (histype >= 0)
6839 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006840 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841 if (*str != NUL)
6842 {
6843 init_history();
6844 add_to_history(histype, str, FALSE, NUL);
6845 rettv->vval.v_number = TRUE;
6846 return;
6847 }
6848 }
6849#endif
6850}
6851
6852/*
6853 * "histdel()" function
6854 */
6855 static void
6856f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
6857{
6858#ifdef FEAT_CMDHIST
6859 int n;
6860 char_u buf[NUMBUFLEN];
6861 char_u *str;
6862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006863 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 if (str == NULL)
6865 n = 0;
6866 else if (argvars[1].v_type == VAR_UNKNOWN)
6867 /* only one argument: clear entire history */
6868 n = clr_history(get_histtype(str));
6869 else if (argvars[1].v_type == VAR_NUMBER)
6870 /* index given: remove that entry */
6871 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873 else
6874 /* string given: remove all matching entries */
6875 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006876 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006877 rettv->vval.v_number = n;
6878#endif
6879}
6880
6881/*
6882 * "histget()" function
6883 */
6884 static void
6885f_histget(typval_T *argvars UNUSED, typval_T *rettv)
6886{
6887#ifdef FEAT_CMDHIST
6888 int type;
6889 int idx;
6890 char_u *str;
6891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 if (str == NULL)
6894 rettv->vval.v_string = NULL;
6895 else
6896 {
6897 type = get_histtype(str);
6898 if (argvars[1].v_type == VAR_UNKNOWN)
6899 idx = get_history_idx(type);
6900 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006901 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 /* -1 on type error */
6903 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
6904 }
6905#else
6906 rettv->vval.v_string = NULL;
6907#endif
6908 rettv->v_type = VAR_STRING;
6909}
6910
6911/*
6912 * "histnr()" function
6913 */
6914 static void
6915f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
6916{
6917 int i;
6918
6919#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006920 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006921
6922 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
6923 if (i >= HIST_CMD && i < HIST_COUNT)
6924 i = get_history_idx(i);
6925 else
6926#endif
6927 i = -1;
6928 rettv->vval.v_number = i;
6929}
6930
6931/*
6932 * "highlightID(name)" function
6933 */
6934 static void
6935f_hlID(typval_T *argvars, typval_T *rettv)
6936{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006937 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938}
6939
6940/*
6941 * "highlight_exists()" function
6942 */
6943 static void
6944f_hlexists(typval_T *argvars, typval_T *rettv)
6945{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006946 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947}
6948
6949/*
6950 * "hostname()" function
6951 */
6952 static void
6953f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
6954{
6955 char_u hostname[256];
6956
6957 mch_get_host_name(hostname, 256);
6958 rettv->v_type = VAR_STRING;
6959 rettv->vval.v_string = vim_strsave(hostname);
6960}
6961
6962/*
6963 * iconv() function
6964 */
6965 static void
6966f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
6967{
6968#ifdef FEAT_MBYTE
6969 char_u buf1[NUMBUFLEN];
6970 char_u buf2[NUMBUFLEN];
6971 char_u *from, *to, *str;
6972 vimconv_T vimconv;
6973#endif
6974
6975 rettv->v_type = VAR_STRING;
6976 rettv->vval.v_string = NULL;
6977
6978#ifdef FEAT_MBYTE
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 str = tv_get_string(&argvars[0]);
6980 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
6981 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006982 vimconv.vc_type = CONV_NONE;
6983 convert_setup(&vimconv, from, to);
6984
6985 /* If the encodings are equal, no conversion needed. */
6986 if (vimconv.vc_type == CONV_NONE)
6987 rettv->vval.v_string = vim_strsave(str);
6988 else
6989 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
6990
6991 convert_setup(&vimconv, NULL, NULL);
6992 vim_free(from);
6993 vim_free(to);
6994#endif
6995}
6996
6997/*
6998 * "indent()" function
6999 */
7000 static void
7001f_indent(typval_T *argvars, typval_T *rettv)
7002{
7003 linenr_T lnum;
7004
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007005 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7007 rettv->vval.v_number = get_indent_lnum(lnum);
7008 else
7009 rettv->vval.v_number = -1;
7010}
7011
7012/*
7013 * "index()" function
7014 */
7015 static void
7016f_index(typval_T *argvars, typval_T *rettv)
7017{
7018 list_T *l;
7019 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007020 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021 long idx = 0;
7022 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007023 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007024
7025 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007026 if (argvars[0].v_type == VAR_BLOB)
7027 {
7028 typval_T tv;
7029 int start = 0;
7030
7031 if (argvars[2].v_type != VAR_UNKNOWN)
7032 {
7033 start = tv_get_number_chk(&argvars[2], &error);
7034 if (error)
7035 return;
7036 }
7037 b = argvars[0].vval.v_blob;
7038 if (b == NULL)
7039 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007040 if (start < 0)
7041 {
7042 start = blob_len(b) + start;
7043 if (start < 0)
7044 start = 0;
7045 }
7046
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007047 for (idx = start; idx < blob_len(b); ++idx)
7048 {
7049 tv.v_type = VAR_NUMBER;
7050 tv.vval.v_number = blob_get(b, idx);
7051 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7052 {
7053 rettv->vval.v_number = idx;
7054 return;
7055 }
7056 }
7057 return;
7058 }
7059 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007060 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007061 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007062 return;
7063 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007065 l = argvars[0].vval.v_list;
7066 if (l != NULL)
7067 {
7068 item = l->lv_first;
7069 if (argvars[2].v_type != VAR_UNKNOWN)
7070 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 /* Start at specified item. Use the cached index that list_find()
7072 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 idx = l->lv_idx;
7075 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007076 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (error)
7078 item = NULL;
7079 }
7080
7081 for ( ; item != NULL; item = item->li_next, ++idx)
7082 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7083 {
7084 rettv->vval.v_number = idx;
7085 break;
7086 }
7087 }
7088}
7089
7090static int inputsecret_flag = 0;
7091
7092/*
7093 * "input()" function
7094 * Also handles inputsecret() when inputsecret is set.
7095 */
7096 static void
7097f_input(typval_T *argvars, typval_T *rettv)
7098{
7099 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7100}
7101
7102/*
7103 * "inputdialog()" function
7104 */
7105 static void
7106f_inputdialog(typval_T *argvars, typval_T *rettv)
7107{
7108#if defined(FEAT_GUI_TEXTDIALOG)
7109 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7110 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7111 {
7112 char_u *message;
7113 char_u buf[NUMBUFLEN];
7114 char_u *defstr = (char_u *)"";
7115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007116 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7120 else
7121 IObuff[0] = NUL;
7122 if (message != NULL && defstr != NULL
7123 && do_dialog(VIM_QUESTION, NULL, message,
7124 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7125 rettv->vval.v_string = vim_strsave(IObuff);
7126 else
7127 {
7128 if (message != NULL && defstr != NULL
7129 && argvars[1].v_type != VAR_UNKNOWN
7130 && argvars[2].v_type != VAR_UNKNOWN)
7131 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007132 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 else
7134 rettv->vval.v_string = NULL;
7135 }
7136 rettv->v_type = VAR_STRING;
7137 }
7138 else
7139#endif
7140 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7141}
7142
7143/*
7144 * "inputlist()" function
7145 */
7146 static void
7147f_inputlist(typval_T *argvars, typval_T *rettv)
7148{
7149 listitem_T *li;
7150 int selected;
7151 int mouse_used;
7152
7153#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007154 /* While starting up, there is no place to enter text. When running tests
7155 * with --not-a-term we assume feedkeys() will be used. */
7156 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 return;
7158#endif
7159 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007161 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007162 return;
7163 }
7164
7165 msg_start();
7166 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7167 lines_left = Rows; /* avoid more prompt */
7168 msg_scroll = TRUE;
7169 msg_clr_eos();
7170
7171 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7172 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007173 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 msg_putchar('\n');
7175 }
7176
7177 /* Ask for choice. */
7178 selected = prompt_for_number(&mouse_used);
7179 if (mouse_used)
7180 selected -= lines_left;
7181
7182 rettv->vval.v_number = selected;
7183}
7184
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7186
7187/*
7188 * "inputrestore()" function
7189 */
7190 static void
7191f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7192{
7193 if (ga_userinput.ga_len > 0)
7194 {
7195 --ga_userinput.ga_len;
7196 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7197 + ga_userinput.ga_len);
7198 /* default return is zero == OK */
7199 }
7200 else if (p_verbose > 1)
7201 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007202 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203 rettv->vval.v_number = 1; /* Failed */
7204 }
7205}
7206
7207/*
7208 * "inputsave()" function
7209 */
7210 static void
7211f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7212{
7213 /* Add an entry to the stack of typeahead storage. */
7214 if (ga_grow(&ga_userinput, 1) == OK)
7215 {
7216 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7217 + ga_userinput.ga_len);
7218 ++ga_userinput.ga_len;
7219 /* default return is zero == OK */
7220 }
7221 else
7222 rettv->vval.v_number = 1; /* Failed */
7223}
7224
7225/*
7226 * "inputsecret()" function
7227 */
7228 static void
7229f_inputsecret(typval_T *argvars, typval_T *rettv)
7230{
7231 ++cmdline_star;
7232 ++inputsecret_flag;
7233 f_input(argvars, rettv);
7234 --cmdline_star;
7235 --inputsecret_flag;
7236}
7237
7238/*
7239 * "insert()" function
7240 */
7241 static void
7242f_insert(typval_T *argvars, typval_T *rettv)
7243{
7244 long before = 0;
7245 listitem_T *item;
7246 list_T *l;
7247 int error = FALSE;
7248
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007249 if (argvars[0].v_type == VAR_BLOB)
7250 {
7251 int val, len;
7252 char_u *p;
7253
7254 len = blob_len(argvars[0].vval.v_blob);
7255 if (argvars[2].v_type != VAR_UNKNOWN)
7256 {
7257 before = (long)tv_get_number_chk(&argvars[2], &error);
7258 if (error)
7259 return; // type error; errmsg already given
7260 if (before < 0 || before > len)
7261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007262 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007263 return;
7264 }
7265 }
7266 val = tv_get_number_chk(&argvars[1], &error);
7267 if (error)
7268 return;
7269 if (val < 0 || val > 255)
7270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007271 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007272 return;
7273 }
7274
7275 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7276 return;
7277 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7278 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7279 *(p + before) = val;
7280 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7281
7282 copy_tv(&argvars[0], rettv);
7283 }
7284 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007285 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007286 else if ((l = argvars[0].vval.v_list) != NULL && !tv_check_lock(l->lv_lock,
7287 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 {
7289 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007290 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 if (error)
7292 return; /* type error; errmsg already given */
7293
7294 if (before == l->lv_len)
7295 item = NULL;
7296 else
7297 {
7298 item = list_find(l, before);
7299 if (item == NULL)
7300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007301 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007302 l = NULL;
7303 }
7304 }
7305 if (l != NULL)
7306 {
7307 list_insert_tv(l, &argvars[1], item);
7308 copy_tv(&argvars[0], rettv);
7309 }
7310 }
7311}
7312
7313/*
7314 * "invert(expr)" function
7315 */
7316 static void
7317f_invert(typval_T *argvars, typval_T *rettv)
7318{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007319 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007320}
7321
7322/*
7323 * "isdirectory()" function
7324 */
7325 static void
7326f_isdirectory(typval_T *argvars, typval_T *rettv)
7327{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007328 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329}
7330
7331/*
7332 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7333 * or it refers to a List or Dictionary that is locked.
7334 */
7335 static int
7336tv_islocked(typval_T *tv)
7337{
7338 return (tv->v_lock & VAR_LOCKED)
7339 || (tv->v_type == VAR_LIST
7340 && tv->vval.v_list != NULL
7341 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7342 || (tv->v_type == VAR_DICT
7343 && tv->vval.v_dict != NULL
7344 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7345}
7346
7347/*
7348 * "islocked()" function
7349 */
7350 static void
7351f_islocked(typval_T *argvars, typval_T *rettv)
7352{
7353 lval_T lv;
7354 char_u *end;
7355 dictitem_T *di;
7356
7357 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007358 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007359 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007360 if (end != NULL && lv.ll_name != NULL)
7361 {
7362 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007363 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007364 else
7365 {
7366 if (lv.ll_tv == NULL)
7367 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007368 di = find_var(lv.ll_name, NULL, TRUE);
7369 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007370 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007371 /* Consider a variable locked when:
7372 * 1. the variable itself is locked
7373 * 2. the value of the variable is locked.
7374 * 3. the List or Dict value is locked.
7375 */
7376 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7377 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007378 }
7379 }
7380 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007381 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007383 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007384 else if (lv.ll_list != NULL)
7385 /* List item. */
7386 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7387 else
7388 /* Dictionary item. */
7389 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7390 }
7391 }
7392
7393 clear_lval(&lv);
7394}
7395
7396#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7397/*
7398 * "isnan()" function
7399 */
7400 static void
7401f_isnan(typval_T *argvars, typval_T *rettv)
7402{
7403 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7404 && isnan(argvars[0].vval.v_float);
7405}
7406#endif
7407
7408/*
7409 * "items(dict)" function
7410 */
7411 static void
7412f_items(typval_T *argvars, typval_T *rettv)
7413{
7414 dict_list(argvars, rettv, 2);
7415}
7416
7417#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7418/*
7419 * Get the job from the argument.
7420 * Returns NULL if the job is invalid.
7421 */
7422 static job_T *
7423get_job_arg(typval_T *tv)
7424{
7425 job_T *job;
7426
7427 if (tv->v_type != VAR_JOB)
7428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 return NULL;
7431 }
7432 job = tv->vval.v_job;
7433
7434 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007435 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 return job;
7437}
7438
7439/*
7440 * "job_getchannel()" function
7441 */
7442 static void
7443f_job_getchannel(typval_T *argvars, typval_T *rettv)
7444{
7445 job_T *job = get_job_arg(&argvars[0]);
7446
7447 if (job != NULL)
7448 {
7449 rettv->v_type = VAR_CHANNEL;
7450 rettv->vval.v_channel = job->jv_channel;
7451 if (job->jv_channel != NULL)
7452 ++job->jv_channel->ch_refcount;
7453 }
7454}
7455
7456/*
7457 * "job_info()" function
7458 */
7459 static void
7460f_job_info(typval_T *argvars, typval_T *rettv)
7461{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007462 if (argvars[0].v_type != VAR_UNKNOWN)
7463 {
7464 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007465
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007466 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7467 job_info(job, rettv->vval.v_dict);
7468 }
7469 else if (rettv_list_alloc(rettv) == OK)
7470 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471}
7472
7473/*
7474 * "job_setoptions()" function
7475 */
7476 static void
7477f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7478{
7479 job_T *job = get_job_arg(&argvars[0]);
7480 jobopt_T opt;
7481
7482 if (job == NULL)
7483 return;
7484 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007485 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007486 job_set_options(job, &opt);
7487 free_job_options(&opt);
7488}
7489
7490/*
7491 * "job_start()" function
7492 */
7493 static void
7494f_job_start(typval_T *argvars, typval_T *rettv)
7495{
7496 rettv->v_type = VAR_JOB;
7497 if (check_restricted() || check_secure())
7498 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007499 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007500}
7501
7502/*
7503 * "job_status()" function
7504 */
7505 static void
7506f_job_status(typval_T *argvars, typval_T *rettv)
7507{
7508 job_T *job = get_job_arg(&argvars[0]);
7509
7510 if (job != NULL)
7511 {
7512 rettv->v_type = VAR_STRING;
7513 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7514 }
7515}
7516
7517/*
7518 * "job_stop()" function
7519 */
7520 static void
7521f_job_stop(typval_T *argvars, typval_T *rettv)
7522{
7523 job_T *job = get_job_arg(&argvars[0]);
7524
7525 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007526 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007527}
7528#endif
7529
7530/*
7531 * "join()" function
7532 */
7533 static void
7534f_join(typval_T *argvars, typval_T *rettv)
7535{
7536 garray_T ga;
7537 char_u *sep;
7538
7539 if (argvars[0].v_type != VAR_LIST)
7540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007541 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007542 return;
7543 }
7544 if (argvars[0].vval.v_list == NULL)
7545 return;
7546 if (argvars[1].v_type == VAR_UNKNOWN)
7547 sep = (char_u *)" ";
7548 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007549 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007550
7551 rettv->v_type = VAR_STRING;
7552
7553 if (sep != NULL)
7554 {
7555 ga_init2(&ga, (int)sizeof(char), 80);
7556 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7557 ga_append(&ga, NUL);
7558 rettv->vval.v_string = (char_u *)ga.ga_data;
7559 }
7560 else
7561 rettv->vval.v_string = NULL;
7562}
7563
7564/*
7565 * "js_decode()" function
7566 */
7567 static void
7568f_js_decode(typval_T *argvars, typval_T *rettv)
7569{
7570 js_read_T reader;
7571
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007572 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007573 reader.js_fill = NULL;
7574 reader.js_used = 0;
7575 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007576 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007577}
7578
7579/*
7580 * "js_encode()" function
7581 */
7582 static void
7583f_js_encode(typval_T *argvars, typval_T *rettv)
7584{
7585 rettv->v_type = VAR_STRING;
7586 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7587}
7588
7589/*
7590 * "json_decode()" function
7591 */
7592 static void
7593f_json_decode(typval_T *argvars, typval_T *rettv)
7594{
7595 js_read_T reader;
7596
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 reader.js_fill = NULL;
7599 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007600 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601}
7602
7603/*
7604 * "json_encode()" function
7605 */
7606 static void
7607f_json_encode(typval_T *argvars, typval_T *rettv)
7608{
7609 rettv->v_type = VAR_STRING;
7610 rettv->vval.v_string = json_encode(&argvars[0], 0);
7611}
7612
7613/*
7614 * "keys()" function
7615 */
7616 static void
7617f_keys(typval_T *argvars, typval_T *rettv)
7618{
7619 dict_list(argvars, rettv, 0);
7620}
7621
7622/*
7623 * "last_buffer_nr()" function.
7624 */
7625 static void
7626f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7627{
7628 int n = 0;
7629 buf_T *buf;
7630
Bram Moolenaar29323592016-07-24 22:04:11 +02007631 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007632 if (n < buf->b_fnum)
7633 n = buf->b_fnum;
7634
7635 rettv->vval.v_number = n;
7636}
7637
7638/*
7639 * "len()" function
7640 */
7641 static void
7642f_len(typval_T *argvars, typval_T *rettv)
7643{
7644 switch (argvars[0].v_type)
7645 {
7646 case VAR_STRING:
7647 case VAR_NUMBER:
7648 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007649 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007651 case VAR_BLOB:
7652 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7653 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 case VAR_LIST:
7655 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7656 break;
7657 case VAR_DICT:
7658 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7659 break;
7660 case VAR_UNKNOWN:
7661 case VAR_SPECIAL:
7662 case VAR_FLOAT:
7663 case VAR_FUNC:
7664 case VAR_PARTIAL:
7665 case VAR_JOB:
7666 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007667 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007668 break;
7669 }
7670}
7671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007672 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007673libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674{
7675#ifdef FEAT_LIBCALL
7676 char_u *string_in;
7677 char_u **string_result;
7678 int nr_result;
7679#endif
7680
7681 rettv->v_type = type;
7682 if (type != VAR_NUMBER)
7683 rettv->vval.v_string = NULL;
7684
7685 if (check_restricted() || check_secure())
7686 return;
7687
7688#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007689 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007690 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7691 {
7692 string_in = NULL;
7693 if (argvars[2].v_type == VAR_STRING)
7694 string_in = argvars[2].vval.v_string;
7695 if (type == VAR_NUMBER)
7696 string_result = NULL;
7697 else
7698 string_result = &rettv->vval.v_string;
7699 if (mch_libcall(argvars[0].vval.v_string,
7700 argvars[1].vval.v_string,
7701 string_in,
7702 argvars[2].vval.v_number,
7703 string_result,
7704 &nr_result) == OK
7705 && type == VAR_NUMBER)
7706 rettv->vval.v_number = nr_result;
7707 }
7708#endif
7709}
7710
7711/*
7712 * "libcall()" function
7713 */
7714 static void
7715f_libcall(typval_T *argvars, typval_T *rettv)
7716{
7717 libcall_common(argvars, rettv, VAR_STRING);
7718}
7719
7720/*
7721 * "libcallnr()" function
7722 */
7723 static void
7724f_libcallnr(typval_T *argvars, typval_T *rettv)
7725{
7726 libcall_common(argvars, rettv, VAR_NUMBER);
7727}
7728
7729/*
7730 * "line(string)" function
7731 */
7732 static void
7733f_line(typval_T *argvars, typval_T *rettv)
7734{
7735 linenr_T lnum = 0;
7736 pos_T *fp;
7737 int fnum;
7738
7739 fp = var2fpos(&argvars[0], TRUE, &fnum);
7740 if (fp != NULL)
7741 lnum = fp->lnum;
7742 rettv->vval.v_number = lnum;
7743}
7744
7745/*
7746 * "line2byte(lnum)" function
7747 */
7748 static void
7749f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7750{
7751#ifndef FEAT_BYTEOFF
7752 rettv->vval.v_number = -1;
7753#else
7754 linenr_T lnum;
7755
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007756 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7758 rettv->vval.v_number = -1;
7759 else
7760 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7761 if (rettv->vval.v_number >= 0)
7762 ++rettv->vval.v_number;
7763#endif
7764}
7765
7766/*
7767 * "lispindent(lnum)" function
7768 */
7769 static void
7770f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
7771{
7772#ifdef FEAT_LISP
7773 pos_T pos;
7774 linenr_T lnum;
7775
7776 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007777 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007778 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7779 {
7780 curwin->w_cursor.lnum = lnum;
7781 rettv->vval.v_number = get_lisp_indent();
7782 curwin->w_cursor = pos;
7783 }
7784 else
7785#endif
7786 rettv->vval.v_number = -1;
7787}
7788
7789/*
7790 * "localtime()" function
7791 */
7792 static void
7793f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
7794{
7795 rettv->vval.v_number = (varnumber_T)time(NULL);
7796}
7797
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798 static void
7799get_maparg(typval_T *argvars, typval_T *rettv, int exact)
7800{
7801 char_u *keys;
7802 char_u *which;
7803 char_u buf[NUMBUFLEN];
7804 char_u *keys_buf = NULL;
7805 char_u *rhs;
7806 int mode;
7807 int abbr = FALSE;
7808 int get_dict = FALSE;
7809 mapblock_T *mp;
7810 int buffer_local;
7811
7812 /* return empty string for failure */
7813 rettv->v_type = VAR_STRING;
7814 rettv->vval.v_string = NULL;
7815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007816 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 if (*keys == NUL)
7818 return;
7819
7820 if (argvars[1].v_type != VAR_UNKNOWN)
7821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007822 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007823 if (argvars[2].v_type != VAR_UNKNOWN)
7824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007825 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007826 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007827 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828 }
7829 }
7830 else
7831 which = (char_u *)"";
7832 if (which == NULL)
7833 return;
7834
7835 mode = get_map_mode(&which, 0);
7836
7837 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
7838 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
7839 vim_free(keys_buf);
7840
7841 if (!get_dict)
7842 {
7843 /* Return a string. */
7844 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02007845 {
7846 if (*rhs == NUL)
7847 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
7848 else
7849 rettv->vval.v_string = str2special_save(rhs, FALSE);
7850 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007851
7852 }
7853 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
7854 {
7855 /* Return a dictionary. */
7856 char_u *lhs = str2special_save(mp->m_keys, TRUE);
7857 char_u *mapmode = map_mode_to_chars(mp->m_mode);
7858 dict_T *dict = rettv->vval.v_dict;
7859
Bram Moolenaare0be1672018-07-08 16:50:37 +02007860 dict_add_string(dict, "lhs", lhs);
7861 dict_add_string(dict, "rhs", mp->m_orig_str);
7862 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
7863 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
7864 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007865 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
7866 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02007867 dict_add_number(dict, "buffer", (long)buffer_local);
7868 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
7869 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870
7871 vim_free(lhs);
7872 vim_free(mapmode);
7873 }
7874}
7875
7876#ifdef FEAT_FLOAT
7877/*
7878 * "log()" function
7879 */
7880 static void
7881f_log(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 = log(f);
7888 else
7889 rettv->vval.v_float = 0.0;
7890}
7891
7892/*
7893 * "log10()" function
7894 */
7895 static void
7896f_log10(typval_T *argvars, typval_T *rettv)
7897{
7898 float_T f = 0.0;
7899
7900 rettv->v_type = VAR_FLOAT;
7901 if (get_float_arg(argvars, &f) == OK)
7902 rettv->vval.v_float = log10(f);
7903 else
7904 rettv->vval.v_float = 0.0;
7905}
7906#endif
7907
7908#ifdef FEAT_LUA
7909/*
7910 * "luaeval()" function
7911 */
7912 static void
7913f_luaeval(typval_T *argvars, typval_T *rettv)
7914{
7915 char_u *str;
7916 char_u buf[NUMBUFLEN];
7917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007918 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 do_luaeval(str, argvars + 1, rettv);
7920}
7921#endif
7922
7923/*
7924 * "map()" function
7925 */
7926 static void
7927f_map(typval_T *argvars, typval_T *rettv)
7928{
7929 filter_map(argvars, rettv, TRUE);
7930}
7931
7932/*
7933 * "maparg()" function
7934 */
7935 static void
7936f_maparg(typval_T *argvars, typval_T *rettv)
7937{
7938 get_maparg(argvars, rettv, TRUE);
7939}
7940
7941/*
7942 * "mapcheck()" function
7943 */
7944 static void
7945f_mapcheck(typval_T *argvars, typval_T *rettv)
7946{
7947 get_maparg(argvars, rettv, FALSE);
7948}
7949
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007950typedef enum
7951{
7952 MATCH_END, /* matchend() */
7953 MATCH_MATCH, /* match() */
7954 MATCH_STR, /* matchstr() */
7955 MATCH_LIST, /* matchlist() */
7956 MATCH_POS /* matchstrpos() */
7957} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007958
7959 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007960find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007961{
7962 char_u *str = NULL;
7963 long len = 0;
7964 char_u *expr = NULL;
7965 char_u *pat;
7966 regmatch_T regmatch;
7967 char_u patbuf[NUMBUFLEN];
7968 char_u strbuf[NUMBUFLEN];
7969 char_u *save_cpo;
7970 long start = 0;
7971 long nth = 1;
7972 colnr_T startcol = 0;
7973 int match = 0;
7974 list_T *l = NULL;
7975 listitem_T *li = NULL;
7976 long idx = 0;
7977 char_u *tofree = NULL;
7978
7979 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7980 save_cpo = p_cpo;
7981 p_cpo = (char_u *)"";
7982
7983 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007984 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007986 /* type MATCH_LIST: return empty list when there are no matches.
7987 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 if (rettv_list_alloc(rettv) == FAIL)
7989 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02007990 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007991 && (list_append_string(rettv->vval.v_list,
7992 (char_u *)"", 0) == FAIL
7993 || list_append_number(rettv->vval.v_list,
7994 (varnumber_T)-1) == FAIL
7995 || list_append_number(rettv->vval.v_list,
7996 (varnumber_T)-1) == FAIL
7997 || list_append_number(rettv->vval.v_list,
7998 (varnumber_T)-1) == FAIL))
7999 {
8000 list_free(rettv->vval.v_list);
8001 rettv->vval.v_list = NULL;
8002 goto theend;
8003 }
8004 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008005 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008006 {
8007 rettv->v_type = VAR_STRING;
8008 rettv->vval.v_string = NULL;
8009 }
8010
8011 if (argvars[0].v_type == VAR_LIST)
8012 {
8013 if ((l = argvars[0].vval.v_list) == NULL)
8014 goto theend;
8015 li = l->lv_first;
8016 }
8017 else
8018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008019 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008020 len = (long)STRLEN(str);
8021 }
8022
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008023 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008024 if (pat == NULL)
8025 goto theend;
8026
8027 if (argvars[2].v_type != VAR_UNKNOWN)
8028 {
8029 int error = FALSE;
8030
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008031 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008032 if (error)
8033 goto theend;
8034 if (l != NULL)
8035 {
8036 li = list_find(l, start);
8037 if (li == NULL)
8038 goto theend;
8039 idx = l->lv_idx; /* use the cached index */
8040 }
8041 else
8042 {
8043 if (start < 0)
8044 start = 0;
8045 if (start > len)
8046 goto theend;
8047 /* When "count" argument is there ignore matches before "start",
8048 * otherwise skip part of the string. Differs when pattern is "^"
8049 * or "\<". */
8050 if (argvars[3].v_type != VAR_UNKNOWN)
8051 startcol = start;
8052 else
8053 {
8054 str += start;
8055 len -= start;
8056 }
8057 }
8058
8059 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008060 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008061 if (error)
8062 goto theend;
8063 }
8064
8065 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8066 if (regmatch.regprog != NULL)
8067 {
8068 regmatch.rm_ic = p_ic;
8069
8070 for (;;)
8071 {
8072 if (l != NULL)
8073 {
8074 if (li == NULL)
8075 {
8076 match = FALSE;
8077 break;
8078 }
8079 vim_free(tofree);
8080 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8081 if (str == NULL)
8082 break;
8083 }
8084
8085 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8086
8087 if (match && --nth <= 0)
8088 break;
8089 if (l == NULL && !match)
8090 break;
8091
8092 /* Advance to just after the match. */
8093 if (l != NULL)
8094 {
8095 li = li->li_next;
8096 ++idx;
8097 }
8098 else
8099 {
8100#ifdef FEAT_MBYTE
8101 startcol = (colnr_T)(regmatch.startp[0]
8102 + (*mb_ptr2len)(regmatch.startp[0]) - str);
8103#else
8104 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
8105#endif
8106 if (startcol > (colnr_T)len
8107 || str + startcol <= regmatch.startp[0])
8108 {
8109 match = FALSE;
8110 break;
8111 }
8112 }
8113 }
8114
8115 if (match)
8116 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008117 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 {
8119 listitem_T *li1 = rettv->vval.v_list->lv_first;
8120 listitem_T *li2 = li1->li_next;
8121 listitem_T *li3 = li2->li_next;
8122 listitem_T *li4 = li3->li_next;
8123
8124 vim_free(li1->li_tv.vval.v_string);
8125 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8126 (int)(regmatch.endp[0] - regmatch.startp[0]));
8127 li3->li_tv.vval.v_number =
8128 (varnumber_T)(regmatch.startp[0] - expr);
8129 li4->li_tv.vval.v_number =
8130 (varnumber_T)(regmatch.endp[0] - expr);
8131 if (l != NULL)
8132 li2->li_tv.vval.v_number = (varnumber_T)idx;
8133 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008134 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008135 {
8136 int i;
8137
8138 /* return list with matched string and submatches */
8139 for (i = 0; i < NSUBEXP; ++i)
8140 {
8141 if (regmatch.endp[i] == NULL)
8142 {
8143 if (list_append_string(rettv->vval.v_list,
8144 (char_u *)"", 0) == FAIL)
8145 break;
8146 }
8147 else if (list_append_string(rettv->vval.v_list,
8148 regmatch.startp[i],
8149 (int)(regmatch.endp[i] - regmatch.startp[i]))
8150 == FAIL)
8151 break;
8152 }
8153 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008154 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008155 {
8156 /* return matched string */
8157 if (l != NULL)
8158 copy_tv(&li->li_tv, rettv);
8159 else
8160 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8161 (int)(regmatch.endp[0] - regmatch.startp[0]));
8162 }
8163 else if (l != NULL)
8164 rettv->vval.v_number = idx;
8165 else
8166 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008167 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168 rettv->vval.v_number =
8169 (varnumber_T)(regmatch.startp[0] - str);
8170 else
8171 rettv->vval.v_number =
8172 (varnumber_T)(regmatch.endp[0] - str);
8173 rettv->vval.v_number += (varnumber_T)(str - expr);
8174 }
8175 }
8176 vim_regfree(regmatch.regprog);
8177 }
8178
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008179theend:
8180 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 /* matchstrpos() without a list: drop the second item. */
8182 listitem_remove(rettv->vval.v_list,
8183 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184 vim_free(tofree);
8185 p_cpo = save_cpo;
8186}
8187
8188/*
8189 * "match()" function
8190 */
8191 static void
8192f_match(typval_T *argvars, typval_T *rettv)
8193{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008194 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008195}
8196
Bram Moolenaar95e51472018-07-28 16:55:56 +02008197#ifdef FEAT_SEARCH_EXTRA
8198 static int
8199matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8200{
8201 dictitem_T *di;
8202
8203 if (tv->v_type != VAR_DICT)
8204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008205 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008206 return FAIL;
8207 }
8208
8209 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008210 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008211 (char_u *)"conceal", FALSE);
8212
8213 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8214 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008215 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008216 if (*win == NULL)
8217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008218 emsg(_("E957: Invalid window number"));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008219 return FAIL;
8220 }
8221 }
8222
8223 return OK;
8224}
8225#endif
8226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227/*
8228 * "matchadd()" function
8229 */
8230 static void
8231f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8232{
8233#ifdef FEAT_SEARCH_EXTRA
8234 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008235 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8236 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237 int prio = 10; /* default priority */
8238 int id = -1;
8239 int error = FALSE;
8240 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008241 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008242
8243 rettv->vval.v_number = -1;
8244
8245 if (grp == NULL || pat == NULL)
8246 return;
8247 if (argvars[2].v_type != VAR_UNKNOWN)
8248 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008249 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008250 if (argvars[3].v_type != VAR_UNKNOWN)
8251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008252 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008253 if (argvars[4].v_type != VAR_UNKNOWN
8254 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8255 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 }
8257 }
8258 if (error == TRUE)
8259 return;
8260 if (id >= 1 && id <= 3)
8261 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008262 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263 return;
8264 }
8265
Bram Moolenaar95e51472018-07-28 16:55:56 +02008266 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008267 conceal_char);
8268#endif
8269}
8270
8271/*
8272 * "matchaddpos()" function
8273 */
8274 static void
8275f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8276{
8277#ifdef FEAT_SEARCH_EXTRA
8278 char_u buf[NUMBUFLEN];
8279 char_u *group;
8280 int prio = 10;
8281 int id = -1;
8282 int error = FALSE;
8283 list_T *l;
8284 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008285 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286
8287 rettv->vval.v_number = -1;
8288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008289 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 if (group == NULL)
8291 return;
8292
8293 if (argvars[1].v_type != VAR_LIST)
8294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008295 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296 return;
8297 }
8298 l = argvars[1].vval.v_list;
8299 if (l == NULL)
8300 return;
8301
8302 if (argvars[2].v_type != VAR_UNKNOWN)
8303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 if (argvars[3].v_type != VAR_UNKNOWN)
8306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008308
8309 if (argvars[4].v_type != VAR_UNKNOWN
8310 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8311 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008312 }
8313 }
8314 if (error == TRUE)
8315 return;
8316
8317 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8318 if (id == 1 || id == 2)
8319 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008320 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 return;
8322 }
8323
Bram Moolenaar95e51472018-07-28 16:55:56 +02008324 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 conceal_char);
8326#endif
8327}
8328
8329/*
8330 * "matcharg()" function
8331 */
8332 static void
8333f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8334{
8335 if (rettv_list_alloc(rettv) == OK)
8336 {
8337#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008338 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 matchitem_T *m;
8340
8341 if (id >= 1 && id <= 3)
8342 {
8343 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8344 {
8345 list_append_string(rettv->vval.v_list,
8346 syn_id2name(m->hlg_id), -1);
8347 list_append_string(rettv->vval.v_list, m->pattern, -1);
8348 }
8349 else
8350 {
8351 list_append_string(rettv->vval.v_list, NULL, -1);
8352 list_append_string(rettv->vval.v_list, NULL, -1);
8353 }
8354 }
8355#endif
8356 }
8357}
8358
8359/*
8360 * "matchdelete()" function
8361 */
8362 static void
8363f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8364{
8365#ifdef FEAT_SEARCH_EXTRA
8366 rettv->vval.v_number = match_delete(curwin,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008367 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008368#endif
8369}
8370
8371/*
8372 * "matchend()" function
8373 */
8374 static void
8375f_matchend(typval_T *argvars, typval_T *rettv)
8376{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008377 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008378}
8379
8380/*
8381 * "matchlist()" function
8382 */
8383 static void
8384f_matchlist(typval_T *argvars, typval_T *rettv)
8385{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008386 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008387}
8388
8389/*
8390 * "matchstr()" function
8391 */
8392 static void
8393f_matchstr(typval_T *argvars, typval_T *rettv)
8394{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008395 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008396}
8397
8398/*
8399 * "matchstrpos()" function
8400 */
8401 static void
8402f_matchstrpos(typval_T *argvars, typval_T *rettv)
8403{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008404 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405}
8406
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008407 static void
8408max_min(typval_T *argvars, typval_T *rettv, int domax)
8409{
8410 varnumber_T n = 0;
8411 varnumber_T i;
8412 int error = FALSE;
8413
8414 if (argvars[0].v_type == VAR_LIST)
8415 {
8416 list_T *l;
8417 listitem_T *li;
8418
8419 l = argvars[0].vval.v_list;
8420 if (l != NULL)
8421 {
8422 li = l->lv_first;
8423 if (li != NULL)
8424 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008425 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 for (;;)
8427 {
8428 li = li->li_next;
8429 if (li == NULL)
8430 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008431 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432 if (domax ? i > n : i < n)
8433 n = i;
8434 }
8435 }
8436 }
8437 }
8438 else if (argvars[0].v_type == VAR_DICT)
8439 {
8440 dict_T *d;
8441 int first = TRUE;
8442 hashitem_T *hi;
8443 int todo;
8444
8445 d = argvars[0].vval.v_dict;
8446 if (d != NULL)
8447 {
8448 todo = (int)d->dv_hashtab.ht_used;
8449 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8450 {
8451 if (!HASHITEM_EMPTY(hi))
8452 {
8453 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008454 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008455 if (first)
8456 {
8457 n = i;
8458 first = FALSE;
8459 }
8460 else if (domax ? i > n : i < n)
8461 n = i;
8462 }
8463 }
8464 }
8465 }
8466 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008467 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468 rettv->vval.v_number = error ? 0 : n;
8469}
8470
8471/*
8472 * "max()" function
8473 */
8474 static void
8475f_max(typval_T *argvars, typval_T *rettv)
8476{
8477 max_min(argvars, rettv, TRUE);
8478}
8479
8480/*
8481 * "min()" function
8482 */
8483 static void
8484f_min(typval_T *argvars, typval_T *rettv)
8485{
8486 max_min(argvars, rettv, FALSE);
8487}
8488
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489/*
8490 * Create the directory in which "dir" is located, and higher levels when
8491 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008492 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 */
8494 static int
8495mkdir_recurse(char_u *dir, int prot)
8496{
8497 char_u *p;
8498 char_u *updir;
8499 int r = FAIL;
8500
8501 /* Get end of directory name in "dir".
8502 * We're done when it's "/" or "c:/". */
8503 p = gettail_sep(dir);
8504 if (p <= get_past_head(dir))
8505 return OK;
8506
8507 /* If the directory exists we're done. Otherwise: create it.*/
8508 updir = vim_strnsave(dir, (int)(p - dir));
8509 if (updir == NULL)
8510 return FAIL;
8511 if (mch_isdir(updir))
8512 r = OK;
8513 else if (mkdir_recurse(updir, prot) == OK)
8514 r = vim_mkdir_emsg(updir, prot);
8515 vim_free(updir);
8516 return r;
8517}
8518
8519#ifdef vim_mkdir
8520/*
8521 * "mkdir()" function
8522 */
8523 static void
8524f_mkdir(typval_T *argvars, typval_T *rettv)
8525{
8526 char_u *dir;
8527 char_u buf[NUMBUFLEN];
8528 int prot = 0755;
8529
8530 rettv->vval.v_number = FAIL;
8531 if (check_restricted() || check_secure())
8532 return;
8533
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008534 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008536 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008538 if (*gettail(dir) == NUL)
8539 /* remove trailing slashes */
8540 *gettail_sep(dir) = NUL;
8541
8542 if (argvars[1].v_type != VAR_UNKNOWN)
8543 {
8544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008547 if (prot == -1)
8548 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008550 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008551 {
8552 if (mch_isdir(dir))
8553 {
8554 /* With the "p" flag it's OK if the dir already exists. */
8555 rettv->vval.v_number = OK;
8556 return;
8557 }
8558 mkdir_recurse(dir, prot);
8559 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008561 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562}
8563#endif
8564
8565/*
8566 * "mode()" function
8567 */
8568 static void
8569f_mode(typval_T *argvars, typval_T *rettv)
8570{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008571 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008572
Bram Moolenaar612cc382018-07-29 15:34:26 +02008573 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008574
8575 if (time_for_testing == 93784)
8576 {
8577 /* Testing the two-character code. */
8578 buf[0] = 'x';
8579 buf[1] = '!';
8580 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008581#ifdef FEAT_TERMINAL
8582 else if (term_use_loop())
8583 buf[0] = 't';
8584#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008585 else if (VIsual_active)
8586 {
8587 if (VIsual_select)
8588 buf[0] = VIsual_mode + 's' - 'v';
8589 else
8590 buf[0] = VIsual_mode;
8591 }
8592 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8593 || State == CONFIRM)
8594 {
8595 buf[0] = 'r';
8596 if (State == ASKMORE)
8597 buf[1] = 'm';
8598 else if (State == CONFIRM)
8599 buf[1] = '?';
8600 }
8601 else if (State == EXTERNCMD)
8602 buf[0] = '!';
8603 else if (State & INSERT)
8604 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008605 if (State & VREPLACE_FLAG)
8606 {
8607 buf[0] = 'R';
8608 buf[1] = 'v';
8609 }
8610 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008611 {
8612 if (State & REPLACE_FLAG)
8613 buf[0] = 'R';
8614 else
8615 buf[0] = 'i';
8616#ifdef FEAT_INS_EXPAND
8617 if (ins_compl_active())
8618 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008619 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008620 buf[1] = 'x';
8621#endif
8622 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008624 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 {
8626 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008627 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008629 else if (exmode_active == EXMODE_NORMAL)
8630 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 }
8632 else
8633 {
8634 buf[0] = 'n';
8635 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008636 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008638 // to be able to detect force-linewise/blockwise/characterwise operations
8639 buf[2] = motion_force;
8640 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008641 else if (restart_edit == 'I' || restart_edit == 'R'
8642 || restart_edit == 'V')
8643 {
8644 buf[1] = 'i';
8645 buf[2] = restart_edit;
8646 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008647 }
8648
8649 /* Clear out the minor mode when the argument is not a non-zero number or
8650 * non-empty string. */
8651 if (!non_zero_arg(&argvars[0]))
8652 buf[1] = NUL;
8653
8654 rettv->vval.v_string = vim_strsave(buf);
8655 rettv->v_type = VAR_STRING;
8656}
8657
8658#if defined(FEAT_MZSCHEME) || defined(PROTO)
8659/*
8660 * "mzeval()" function
8661 */
8662 static void
8663f_mzeval(typval_T *argvars, typval_T *rettv)
8664{
8665 char_u *str;
8666 char_u buf[NUMBUFLEN];
8667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008668 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008669 do_mzeval(str, rettv);
8670}
8671
8672 void
8673mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8674{
8675 typval_T argvars[3];
8676
8677 argvars[0].v_type = VAR_STRING;
8678 argvars[0].vval.v_string = name;
8679 copy_tv(args, &argvars[1]);
8680 argvars[2].v_type = VAR_UNKNOWN;
8681 f_call(argvars, rettv);
8682 clear_tv(&argvars[1]);
8683}
8684#endif
8685
8686/*
8687 * "nextnonblank()" function
8688 */
8689 static void
8690f_nextnonblank(typval_T *argvars, typval_T *rettv)
8691{
8692 linenr_T lnum;
8693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008694 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 {
8696 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8697 {
8698 lnum = 0;
8699 break;
8700 }
8701 if (*skipwhite(ml_get(lnum)) != NUL)
8702 break;
8703 }
8704 rettv->vval.v_number = lnum;
8705}
8706
8707/*
8708 * "nr2char()" function
8709 */
8710 static void
8711f_nr2char(typval_T *argvars, typval_T *rettv)
8712{
8713 char_u buf[NUMBUFLEN];
8714
8715#ifdef FEAT_MBYTE
8716 if (has_mbyte)
8717 {
8718 int utf8 = 0;
8719
8720 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 if (utf8)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 buf[(*utf_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 }
8727 else
8728#endif
8729 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008730 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008731 buf[1] = NUL;
8732 }
8733 rettv->v_type = VAR_STRING;
8734 rettv->vval.v_string = vim_strsave(buf);
8735}
8736
8737/*
8738 * "or(expr, expr)" function
8739 */
8740 static void
8741f_or(typval_T *argvars, typval_T *rettv)
8742{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008743 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
8744 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745}
8746
8747/*
8748 * "pathshorten()" function
8749 */
8750 static void
8751f_pathshorten(typval_T *argvars, typval_T *rettv)
8752{
8753 char_u *p;
8754
8755 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 if (p == NULL)
8758 rettv->vval.v_string = NULL;
8759 else
8760 {
8761 p = vim_strsave(p);
8762 rettv->vval.v_string = p;
8763 if (p != NULL)
8764 shorten_dir(p);
8765 }
8766}
8767
8768#ifdef FEAT_PERL
8769/*
8770 * "perleval()" function
8771 */
8772 static void
8773f_perleval(typval_T *argvars, typval_T *rettv)
8774{
8775 char_u *str;
8776 char_u buf[NUMBUFLEN];
8777
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008778 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779 do_perleval(str, rettv);
8780}
8781#endif
8782
8783#ifdef FEAT_FLOAT
8784/*
8785 * "pow()" function
8786 */
8787 static void
8788f_pow(typval_T *argvars, typval_T *rettv)
8789{
8790 float_T fx = 0.0, fy = 0.0;
8791
8792 rettv->v_type = VAR_FLOAT;
8793 if (get_float_arg(argvars, &fx) == OK
8794 && get_float_arg(&argvars[1], &fy) == OK)
8795 rettv->vval.v_float = pow(fx, fy);
8796 else
8797 rettv->vval.v_float = 0.0;
8798}
8799#endif
8800
8801/*
8802 * "prevnonblank()" function
8803 */
8804 static void
8805f_prevnonblank(typval_T *argvars, typval_T *rettv)
8806{
8807 linenr_T lnum;
8808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008809 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008810 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8811 lnum = 0;
8812 else
8813 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8814 --lnum;
8815 rettv->vval.v_number = lnum;
8816}
8817
8818/* This dummy va_list is here because:
8819 * - passing a NULL pointer doesn't work when va_list isn't a pointer
8820 * - locally in the function results in a "used before set" warning
8821 * - using va_start() to initialize it gives "function with fixed args" error */
8822static va_list ap;
8823
8824/*
8825 * "printf()" function
8826 */
8827 static void
8828f_printf(typval_T *argvars, typval_T *rettv)
8829{
8830 char_u buf[NUMBUFLEN];
8831 int len;
8832 char_u *s;
8833 int saved_did_emsg = did_emsg;
8834 char *fmt;
8835
8836 rettv->v_type = VAR_STRING;
8837 rettv->vval.v_string = NULL;
8838
8839 /* Get the required length, allocate the buffer and do it for real. */
8840 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008842 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008843 if (!did_emsg)
8844 {
8845 s = alloc(len + 1);
8846 if (s != NULL)
8847 {
8848 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02008849 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
8850 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 }
8852 }
8853 did_emsg |= saved_did_emsg;
8854}
8855
Bram Moolenaarf2732452018-06-03 14:47:35 +02008856#ifdef FEAT_JOB_CHANNEL
8857/*
8858 * "prompt_setcallback({buffer}, {callback})" function
8859 */
8860 static void
8861f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
8862{
8863 buf_T *buf;
8864 char_u *callback;
8865 partial_T *partial;
8866
8867 if (check_secure())
8868 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008869 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008870 if (buf == NULL)
8871 return;
8872
8873 callback = get_callback(&argvars[1], &partial);
8874 if (callback == NULL)
8875 return;
8876
8877 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
8878 if (partial == NULL)
8879 buf->b_prompt_callback = vim_strsave(callback);
8880 else
8881 /* pointer into the partial */
8882 buf->b_prompt_callback = callback;
8883 buf->b_prompt_partial = partial;
8884}
8885
8886/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008887 * "prompt_setinterrupt({buffer}, {callback})" function
8888 */
8889 static void
8890f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
8891{
8892 buf_T *buf;
8893 char_u *callback;
8894 partial_T *partial;
8895
8896 if (check_secure())
8897 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008898 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02008899 if (buf == NULL)
8900 return;
8901
8902 callback = get_callback(&argvars[1], &partial);
8903 if (callback == NULL)
8904 return;
8905
8906 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
8907 if (partial == NULL)
8908 buf->b_prompt_interrupt = vim_strsave(callback);
8909 else
8910 /* pointer into the partial */
8911 buf->b_prompt_interrupt = callback;
8912 buf->b_prompt_int_partial = partial;
8913}
8914
8915/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02008916 * "prompt_setprompt({buffer}, {text})" function
8917 */
8918 static void
8919f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
8920{
8921 buf_T *buf;
8922 char_u *text;
8923
8924 if (check_secure())
8925 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01008926 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008927 if (buf == NULL)
8928 return;
8929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008930 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02008931 vim_free(buf->b_prompt_text);
8932 buf->b_prompt_text = vim_strsave(text);
8933}
8934#endif
8935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936/*
8937 * "pumvisible()" function
8938 */
8939 static void
8940f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8941{
8942#ifdef FEAT_INS_EXPAND
8943 if (pum_visible())
8944 rettv->vval.v_number = 1;
8945#endif
8946}
8947
8948#ifdef FEAT_PYTHON3
8949/*
8950 * "py3eval()" function
8951 */
8952 static void
8953f_py3eval(typval_T *argvars, typval_T *rettv)
8954{
8955 char_u *str;
8956 char_u buf[NUMBUFLEN];
8957
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008958 if (p_pyx == 0)
8959 p_pyx = 3;
8960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008961 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962 do_py3eval(str, rettv);
8963}
8964#endif
8965
8966#ifdef FEAT_PYTHON
8967/*
8968 * "pyeval()" function
8969 */
8970 static void
8971f_pyeval(typval_T *argvars, typval_T *rettv)
8972{
8973 char_u *str;
8974 char_u buf[NUMBUFLEN];
8975
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008976 if (p_pyx == 0)
8977 p_pyx = 2;
8978
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008979 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008980 do_pyeval(str, rettv);
8981}
8982#endif
8983
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01008984#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
8985/*
8986 * "pyxeval()" function
8987 */
8988 static void
8989f_pyxeval(typval_T *argvars, typval_T *rettv)
8990{
8991# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
8992 init_pyxversion();
8993 if (p_pyx == 2)
8994 f_pyeval(argvars, rettv);
8995 else
8996 f_py3eval(argvars, rettv);
8997# elif defined(FEAT_PYTHON)
8998 f_pyeval(argvars, rettv);
8999# elif defined(FEAT_PYTHON3)
9000 f_py3eval(argvars, rettv);
9001# endif
9002}
9003#endif
9004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005/*
9006 * "range()" function
9007 */
9008 static void
9009f_range(typval_T *argvars, typval_T *rettv)
9010{
9011 varnumber_T start;
9012 varnumber_T end;
9013 varnumber_T stride = 1;
9014 varnumber_T i;
9015 int error = FALSE;
9016
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009017 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 if (argvars[1].v_type == VAR_UNKNOWN)
9019 {
9020 end = start - 1;
9021 start = 0;
9022 }
9023 else
9024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009025 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009027 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 }
9029
9030 if (error)
9031 return; /* type error; errmsg already given */
9032 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009033 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009035 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 else
9037 {
9038 if (rettv_list_alloc(rettv) == OK)
9039 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9040 if (list_append_number(rettv->vval.v_list,
9041 (varnumber_T)i) == FAIL)
9042 break;
9043 }
9044}
9045
9046/*
9047 * "readfile()" function
9048 */
9049 static void
9050f_readfile(typval_T *argvars, typval_T *rettv)
9051{
9052 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009053 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009054 int failed = FALSE;
9055 char_u *fname;
9056 FILE *fd;
9057 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9058 int io_size = sizeof(buf);
9059 int readlen; /* size of last fread() */
9060 char_u *prev = NULL; /* previously read bytes, if any */
9061 long prevlen = 0; /* length of data in prev */
9062 long prevsize = 0; /* size of prev buffer */
9063 long maxline = MAXLNUM;
9064 long cnt = 0;
9065 char_u *p; /* position in buf */
9066 char_u *start; /* start of current line */
9067
9068 if (argvars[1].v_type != VAR_UNKNOWN)
9069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009070 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009071 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009072 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9073 blob = TRUE;
9074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009076 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 }
9078
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009079 if (blob)
9080 {
9081 if (rettv_blob_alloc(rettv) == FAIL)
9082 return;
9083 }
9084 else
9085 {
9086 if (rettv_list_alloc(rettv) == FAIL)
9087 return;
9088 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089
9090 /* Always open the file in binary mode, library functions have a mind of
9091 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009092 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009093 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009095 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 return;
9097 }
9098
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009099 if (blob)
9100 {
9101 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009103 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009104 blob_free(rettv->vval.v_blob);
9105 }
9106 fclose(fd);
9107 return;
9108 }
9109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 while (cnt < maxline || maxline < 0)
9111 {
9112 readlen = (int)fread(buf, 1, io_size, fd);
9113
9114 /* This for loop processes what was read, but is also entered at end
9115 * of file so that either:
9116 * - an incomplete line gets written
9117 * - a "binary" file gets an empty line at the end if it ends in a
9118 * newline. */
9119 for (p = buf, start = buf;
9120 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9121 ++p)
9122 {
9123 if (*p == '\n' || readlen <= 0)
9124 {
9125 listitem_T *li;
9126 char_u *s = NULL;
9127 long_u len = p - start;
9128
9129 /* Finished a line. Remove CRs before NL. */
9130 if (readlen > 0 && !binary)
9131 {
9132 while (len > 0 && start[len - 1] == '\r')
9133 --len;
9134 /* removal may cross back to the "prev" string */
9135 if (len == 0)
9136 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9137 --prevlen;
9138 }
9139 if (prevlen == 0)
9140 s = vim_strnsave(start, (int)len);
9141 else
9142 {
9143 /* Change "prev" buffer to be the right size. This way
9144 * the bytes are only copied once, and very long lines are
9145 * allocated only once. */
9146 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9147 {
9148 mch_memmove(s + prevlen, start, len);
9149 s[prevlen + len] = NUL;
9150 prev = NULL; /* the list will own the string */
9151 prevlen = prevsize = 0;
9152 }
9153 }
9154 if (s == NULL)
9155 {
9156 do_outofmem_msg((long_u) prevlen + len + 1);
9157 failed = TRUE;
9158 break;
9159 }
9160
9161 if ((li = listitem_alloc()) == NULL)
9162 {
9163 vim_free(s);
9164 failed = TRUE;
9165 break;
9166 }
9167 li->li_tv.v_type = VAR_STRING;
9168 li->li_tv.v_lock = 0;
9169 li->li_tv.vval.v_string = s;
9170 list_append(rettv->vval.v_list, li);
9171
9172 start = p + 1; /* step over newline */
9173 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9174 break;
9175 }
9176 else if (*p == NUL)
9177 *p = '\n';
9178#ifdef FEAT_MBYTE
9179 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9180 * when finding the BF and check the previous two bytes. */
9181 else if (*p == 0xbf && enc_utf8 && !binary)
9182 {
9183 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9184 * + 1, these may be in the "prev" string. */
9185 char_u back1 = p >= buf + 1 ? p[-1]
9186 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9187 char_u back2 = p >= buf + 2 ? p[-2]
9188 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9189 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9190
9191 if (back2 == 0xef && back1 == 0xbb)
9192 {
9193 char_u *dest = p - 2;
9194
9195 /* Usually a BOM is at the beginning of a file, and so at
9196 * the beginning of a line; then we can just step over it.
9197 */
9198 if (start == dest)
9199 start = p + 1;
9200 else
9201 {
9202 /* have to shuffle buf to close gap */
9203 int adjust_prevlen = 0;
9204
9205 if (dest < buf)
9206 {
9207 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9208 dest = buf;
9209 }
9210 if (readlen > p - buf + 1)
9211 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9212 readlen -= 3 - adjust_prevlen;
9213 prevlen -= adjust_prevlen;
9214 p = dest - 1;
9215 }
9216 }
9217 }
9218#endif
9219 } /* for */
9220
9221 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9222 break;
9223 if (start < p)
9224 {
9225 /* There's part of a line in buf, store it in "prev". */
9226 if (p - start + prevlen >= prevsize)
9227 {
9228 /* need bigger "prev" buffer */
9229 char_u *newprev;
9230
9231 /* A common use case is ordinary text files and "prev" gets a
9232 * fragment of a line, so the first allocation is made
9233 * small, to avoid repeatedly 'allocing' large and
9234 * 'reallocing' small. */
9235 if (prevsize == 0)
9236 prevsize = (long)(p - start);
9237 else
9238 {
9239 long grow50pc = (prevsize * 3) / 2;
9240 long growmin = (long)((p - start) * 2 + prevlen);
9241 prevsize = grow50pc > growmin ? grow50pc : growmin;
9242 }
9243 newprev = prev == NULL ? alloc(prevsize)
9244 : vim_realloc(prev, prevsize);
9245 if (newprev == NULL)
9246 {
9247 do_outofmem_msg((long_u)prevsize);
9248 failed = TRUE;
9249 break;
9250 }
9251 prev = newprev;
9252 }
9253 /* Add the line part to end of "prev". */
9254 mch_memmove(prev + prevlen, start, p - start);
9255 prevlen += (long)(p - start);
9256 }
9257 } /* while */
9258
9259 /*
9260 * For a negative line count use only the lines at the end of the file,
9261 * free the rest.
9262 */
9263 if (!failed && maxline < 0)
9264 while (cnt > -maxline)
9265 {
9266 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9267 --cnt;
9268 }
9269
9270 if (failed)
9271 {
9272 list_free(rettv->vval.v_list);
9273 /* readfile doc says an empty list is returned on error */
9274 rettv->vval.v_list = list_alloc();
9275 }
9276
9277 vim_free(prev);
9278 fclose(fd);
9279}
9280
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009281 static void
9282return_register(int regname, typval_T *rettv)
9283{
9284 char_u buf[2] = {0, 0};
9285
9286 buf[0] = (char_u)regname;
9287 rettv->v_type = VAR_STRING;
9288 rettv->vval.v_string = vim_strsave(buf);
9289}
9290
9291/*
9292 * "reg_executing()" function
9293 */
9294 static void
9295f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9296{
9297 return_register(reg_executing, rettv);
9298}
9299
9300/*
9301 * "reg_recording()" function
9302 */
9303 static void
9304f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9305{
9306 return_register(reg_recording, rettv);
9307}
9308
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009309#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310/*
9311 * Convert a List to proftime_T.
9312 * Return FAIL when there is something wrong.
9313 */
9314 static int
9315list2proftime(typval_T *arg, proftime_T *tm)
9316{
9317 long n1, n2;
9318 int error = FALSE;
9319
9320 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9321 || arg->vval.v_list->lv_len != 2)
9322 return FAIL;
9323 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9324 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
9325# ifdef WIN3264
9326 tm->HighPart = n1;
9327 tm->LowPart = n2;
9328# else
9329 tm->tv_sec = n1;
9330 tm->tv_usec = n2;
9331# endif
9332 return error ? FAIL : OK;
9333}
9334#endif /* FEAT_RELTIME */
9335
9336/*
9337 * "reltime()" function
9338 */
9339 static void
9340f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9341{
9342#ifdef FEAT_RELTIME
9343 proftime_T res;
9344 proftime_T start;
9345
9346 if (argvars[0].v_type == VAR_UNKNOWN)
9347 {
9348 /* No arguments: get current time. */
9349 profile_start(&res);
9350 }
9351 else if (argvars[1].v_type == VAR_UNKNOWN)
9352 {
9353 if (list2proftime(&argvars[0], &res) == FAIL)
9354 return;
9355 profile_end(&res);
9356 }
9357 else
9358 {
9359 /* Two arguments: compute the difference. */
9360 if (list2proftime(&argvars[0], &start) == FAIL
9361 || list2proftime(&argvars[1], &res) == FAIL)
9362 return;
9363 profile_sub(&res, &start);
9364 }
9365
9366 if (rettv_list_alloc(rettv) == OK)
9367 {
9368 long n1, n2;
9369
9370# ifdef WIN3264
9371 n1 = res.HighPart;
9372 n2 = res.LowPart;
9373# else
9374 n1 = res.tv_sec;
9375 n2 = res.tv_usec;
9376# endif
9377 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9378 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9379 }
9380#endif
9381}
9382
9383#ifdef FEAT_FLOAT
9384/*
9385 * "reltimefloat()" function
9386 */
9387 static void
9388f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9389{
9390# ifdef FEAT_RELTIME
9391 proftime_T tm;
9392# endif
9393
9394 rettv->v_type = VAR_FLOAT;
9395 rettv->vval.v_float = 0;
9396# ifdef FEAT_RELTIME
9397 if (list2proftime(&argvars[0], &tm) == OK)
9398 rettv->vval.v_float = profile_float(&tm);
9399# endif
9400}
9401#endif
9402
9403/*
9404 * "reltimestr()" function
9405 */
9406 static void
9407f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9408{
9409#ifdef FEAT_RELTIME
9410 proftime_T tm;
9411#endif
9412
9413 rettv->v_type = VAR_STRING;
9414 rettv->vval.v_string = NULL;
9415#ifdef FEAT_RELTIME
9416 if (list2proftime(&argvars[0], &tm) == OK)
9417 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9418#endif
9419}
9420
9421#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009422 static void
9423make_connection(void)
9424{
9425 if (X_DISPLAY == NULL
9426# ifdef FEAT_GUI
9427 && !gui.in_use
9428# endif
9429 )
9430 {
9431 x_force_connect = TRUE;
9432 setup_term_clip();
9433 x_force_connect = FALSE;
9434 }
9435}
9436
9437 static int
9438check_connection(void)
9439{
9440 make_connection();
9441 if (X_DISPLAY == NULL)
9442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009443 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009444 return FAIL;
9445 }
9446 return OK;
9447}
9448#endif
9449
9450#ifdef FEAT_CLIENTSERVER
9451 static void
9452remote_common(typval_T *argvars, typval_T *rettv, int expr)
9453{
9454 char_u *server_name;
9455 char_u *keys;
9456 char_u *r = NULL;
9457 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009458 int timeout = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459# ifdef WIN32
9460 HWND w;
9461# else
9462 Window w;
9463# endif
9464
9465 if (check_restricted() || check_secure())
9466 return;
9467
9468# ifdef FEAT_X11
9469 if (check_connection() == FAIL)
9470 return;
9471# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009472 if (argvars[2].v_type != VAR_UNKNOWN
9473 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009474 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009476 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477 if (server_name == NULL)
9478 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009479 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009481 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009482# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009483 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9484 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009485# endif
9486 {
9487 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009489 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009490 vim_free(r);
9491 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009493 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 return;
9495 }
9496
9497 rettv->vval.v_string = r;
9498
9499 if (argvars[2].v_type != VAR_UNKNOWN)
9500 {
9501 dictitem_T v;
9502 char_u str[30];
9503 char_u *idvar;
9504
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009505 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009506 if (idvar != NULL && *idvar != NUL)
9507 {
9508 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9509 v.di_tv.v_type = VAR_STRING;
9510 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009512 vim_free(v.di_tv.vval.v_string);
9513 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514 }
9515}
9516#endif
9517
9518/*
9519 * "remote_expr()" function
9520 */
9521 static void
9522f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9523{
9524 rettv->v_type = VAR_STRING;
9525 rettv->vval.v_string = NULL;
9526#ifdef FEAT_CLIENTSERVER
9527 remote_common(argvars, rettv, TRUE);
9528#endif
9529}
9530
9531/*
9532 * "remote_foreground()" function
9533 */
9534 static void
9535f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9536{
9537#ifdef FEAT_CLIENTSERVER
9538# ifdef WIN32
9539 /* On Win32 it's done in this application. */
9540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009541 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542
9543 if (server_name != NULL)
9544 serverForeground(server_name);
9545 }
9546# else
9547 /* Send a foreground() expression to the server. */
9548 argvars[1].v_type = VAR_STRING;
9549 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9550 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009551 rettv->v_type = VAR_STRING;
9552 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009553 remote_common(argvars, rettv, TRUE);
9554 vim_free(argvars[1].vval.v_string);
9555# endif
9556#endif
9557}
9558
9559 static void
9560f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9561{
9562#ifdef FEAT_CLIENTSERVER
9563 dictitem_T v;
9564 char_u *s = NULL;
9565# ifdef WIN32
9566 long_u n = 0;
9567# endif
9568 char_u *serverid;
9569
9570 if (check_restricted() || check_secure())
9571 {
9572 rettv->vval.v_number = -1;
9573 return;
9574 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009575 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009576 if (serverid == NULL)
9577 {
9578 rettv->vval.v_number = -1;
9579 return; /* type error; errmsg already given */
9580 }
9581# ifdef WIN32
9582 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9583 if (n == 0)
9584 rettv->vval.v_number = -1;
9585 else
9586 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009587 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009588 rettv->vval.v_number = (s != NULL);
9589 }
9590# else
9591 if (check_connection() == FAIL)
9592 return;
9593
9594 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9595 serverStrToWin(serverid), &s);
9596# endif
9597
9598 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9599 {
9600 char_u *retvar;
9601
9602 v.di_tv.v_type = VAR_STRING;
9603 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009604 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009605 if (retvar != NULL)
9606 set_var(retvar, &v.di_tv, FALSE);
9607 vim_free(v.di_tv.vval.v_string);
9608 }
9609#else
9610 rettv->vval.v_number = -1;
9611#endif
9612}
9613
9614 static void
9615f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9616{
9617 char_u *r = NULL;
9618
9619#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009620 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009621
9622 if (serverid != NULL && !check_restricted() && !check_secure())
9623 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009624 int timeout = 0;
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009625# ifdef WIN32
9626 /* The server's HWND is encoded in the 'id' parameter */
9627 long_u n = 0;
9628# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009629
9630 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009631 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009632
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009633# ifdef WIN32
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009634 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
9635 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009636 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009637 if (r == NULL)
9638# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009639 if (check_connection() == FAIL
9640 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
9641 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009642# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009643 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009644 }
9645#endif
9646 rettv->v_type = VAR_STRING;
9647 rettv->vval.v_string = r;
9648}
9649
9650/*
9651 * "remote_send()" function
9652 */
9653 static void
9654f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
9655{
9656 rettv->v_type = VAR_STRING;
9657 rettv->vval.v_string = NULL;
9658#ifdef FEAT_CLIENTSERVER
9659 remote_common(argvars, rettv, FALSE);
9660#endif
9661}
9662
9663/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009664 * "remote_startserver()" function
9665 */
9666 static void
9667f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9668{
9669#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009670 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009671
9672 if (server == NULL)
9673 return; /* type error; errmsg already given */
9674 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009675 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009676 else
9677 {
9678# ifdef FEAT_X11
9679 if (check_connection() == OK)
9680 serverRegisterName(X_DISPLAY, server);
9681# else
9682 serverSetName(server);
9683# endif
9684 }
9685#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009686 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01009687#endif
9688}
9689
9690/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 * "remove()" function
9692 */
9693 static void
9694f_remove(typval_T *argvars, typval_T *rettv)
9695{
9696 list_T *l;
9697 listitem_T *item, *item2;
9698 listitem_T *li;
9699 long idx;
9700 long end;
9701 char_u *key;
9702 dict_T *d;
9703 dictitem_T *di;
9704 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009705 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009706
9707 if (argvars[0].v_type == VAR_DICT)
9708 {
9709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009710 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 else if ((d = argvars[0].vval.v_dict) != NULL
9712 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
9713 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009714 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009715 if (key != NULL)
9716 {
9717 di = dict_find(d, key, -1);
9718 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009719 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
9721 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
9722 {
9723 *rettv = di->di_tv;
9724 init_tv(&di->di_tv);
9725 dictitem_remove(d, di);
9726 }
9727 }
9728 }
9729 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009730 else if (argvars[0].v_type == VAR_BLOB)
9731 {
9732 idx = (long)tv_get_number_chk(&argvars[1], &error);
9733 if (!error)
9734 {
9735 blob_T *b = argvars[0].vval.v_blob;
9736 int len = blob_len(b);
9737 char_u *p;
9738
9739 if (idx < 0)
9740 // count from the end
9741 idx = len + idx;
9742 if (idx < 0 || idx >= len)
9743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009744 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009745 return;
9746 }
9747 if (argvars[2].v_type == VAR_UNKNOWN)
9748 {
9749 // Remove one item, return its value.
9750 p = (char_u *)b->bv_ga.ga_data;
9751 rettv->vval.v_number = (varnumber_T) *(p + idx);
9752 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
9753 --b->bv_ga.ga_len;
9754 }
9755 else
9756 {
9757 blob_T *blob;
9758
9759 // Remove range of items, return list with values.
9760 end = (long)tv_get_number_chk(&argvars[2], &error);
9761 if (error)
9762 return;
9763 if (end < 0)
9764 // count from the end
9765 end = len + end;
9766 if (end >= len || idx > end)
9767 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009768 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009769 return;
9770 }
9771 blob = blob_alloc();
9772 if (blob == NULL)
9773 return;
9774 blob->bv_ga.ga_len = end - idx + 1;
9775 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
9776 {
9777 vim_free(blob);
9778 return;
9779 }
9780 p = (char_u *)b->bv_ga.ga_data;
9781 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
9782 (size_t)(end - idx + 1));
9783 ++blob->bv_refcount;
9784 rettv->v_type = VAR_BLOB;
9785 rettv->vval.v_blob = blob;
9786
9787 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
9788 b->bv_ga.ga_len -= end - idx + 1;
9789 }
9790 }
9791 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009792 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01009793 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009794 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009795 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009797 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009798 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009799 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009800 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009801 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009802 else
9803 {
9804 if (argvars[2].v_type == VAR_UNKNOWN)
9805 {
9806 /* Remove one item, return its value. */
9807 vimlist_remove(l, item, item);
9808 *rettv = item->li_tv;
9809 vim_free(item);
9810 }
9811 else
9812 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009813 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009814 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009815 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009816 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009818 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009819 else
9820 {
9821 int cnt = 0;
9822
9823 for (li = item; li != NULL; li = li->li_next)
9824 {
9825 ++cnt;
9826 if (li == item2)
9827 break;
9828 }
9829 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009830 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 else
9832 {
9833 vimlist_remove(l, item, item2);
9834 if (rettv_list_alloc(rettv) == OK)
9835 {
9836 l = rettv->vval.v_list;
9837 l->lv_first = item;
9838 l->lv_last = item2;
9839 item->li_prev = NULL;
9840 item2->li_next = NULL;
9841 l->lv_len = cnt;
9842 }
9843 }
9844 }
9845 }
9846 }
9847 }
9848}
9849
9850/*
9851 * "rename({from}, {to})" function
9852 */
9853 static void
9854f_rename(typval_T *argvars, typval_T *rettv)
9855{
9856 char_u buf[NUMBUFLEN];
9857
9858 if (check_restricted() || check_secure())
9859 rettv->vval.v_number = -1;
9860 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009861 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
9862 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009863}
9864
9865/*
9866 * "repeat()" function
9867 */
9868 static void
9869f_repeat(typval_T *argvars, typval_T *rettv)
9870{
9871 char_u *p;
9872 int n;
9873 int slen;
9874 int len;
9875 char_u *r;
9876 int i;
9877
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009878 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 if (argvars[0].v_type == VAR_LIST)
9880 {
9881 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
9882 while (n-- > 0)
9883 if (list_extend(rettv->vval.v_list,
9884 argvars[0].vval.v_list, NULL) == FAIL)
9885 break;
9886 }
9887 else
9888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009889 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 rettv->v_type = VAR_STRING;
9891 rettv->vval.v_string = NULL;
9892
9893 slen = (int)STRLEN(p);
9894 len = slen * n;
9895 if (len <= 0)
9896 return;
9897
9898 r = alloc(len + 1);
9899 if (r != NULL)
9900 {
9901 for (i = 0; i < n; i++)
9902 mch_memmove(r + i * slen, p, (size_t)slen);
9903 r[len] = NUL;
9904 }
9905
9906 rettv->vval.v_string = r;
9907 }
9908}
9909
9910/*
9911 * "resolve()" function
9912 */
9913 static void
9914f_resolve(typval_T *argvars, typval_T *rettv)
9915{
9916 char_u *p;
9917#ifdef HAVE_READLINK
9918 char_u *buf = NULL;
9919#endif
9920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009921 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922#ifdef FEAT_SHORTCUT
9923 {
9924 char_u *v = NULL;
9925
9926 v = mch_resolve_shortcut(p);
9927 if (v != NULL)
9928 rettv->vval.v_string = v;
9929 else
9930 rettv->vval.v_string = vim_strsave(p);
9931 }
9932#else
9933# ifdef HAVE_READLINK
9934 {
9935 char_u *cpy;
9936 int len;
9937 char_u *remain = NULL;
9938 char_u *q;
9939 int is_relative_to_current = FALSE;
9940 int has_trailing_pathsep = FALSE;
9941 int limit = 100;
9942
9943 p = vim_strsave(p);
9944
9945 if (p[0] == '.' && (vim_ispathsep(p[1])
9946 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9947 is_relative_to_current = TRUE;
9948
9949 len = STRLEN(p);
9950 if (len > 0 && after_pathsep(p, p + len))
9951 {
9952 has_trailing_pathsep = TRUE;
9953 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
9954 }
9955
9956 q = getnextcomp(p);
9957 if (*q != NUL)
9958 {
9959 /* Separate the first path component in "p", and keep the
9960 * remainder (beginning with the path separator). */
9961 remain = vim_strsave(q - 1);
9962 q[-1] = NUL;
9963 }
9964
9965 buf = alloc(MAXPATHL + 1);
9966 if (buf == NULL)
9967 goto fail;
9968
9969 for (;;)
9970 {
9971 for (;;)
9972 {
9973 len = readlink((char *)p, (char *)buf, MAXPATHL);
9974 if (len <= 0)
9975 break;
9976 buf[len] = NUL;
9977
9978 if (limit-- == 0)
9979 {
9980 vim_free(p);
9981 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009982 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009983 rettv->vval.v_string = NULL;
9984 goto fail;
9985 }
9986
9987 /* Ensure that the result will have a trailing path separator
9988 * if the argument has one. */
9989 if (remain == NULL && has_trailing_pathsep)
9990 add_pathsep(buf);
9991
9992 /* Separate the first path component in the link value and
9993 * concatenate the remainders. */
9994 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9995 if (*q != NUL)
9996 {
9997 if (remain == NULL)
9998 remain = vim_strsave(q - 1);
9999 else
10000 {
10001 cpy = concat_str(q - 1, remain);
10002 if (cpy != NULL)
10003 {
10004 vim_free(remain);
10005 remain = cpy;
10006 }
10007 }
10008 q[-1] = NUL;
10009 }
10010
10011 q = gettail(p);
10012 if (q > p && *q == NUL)
10013 {
10014 /* Ignore trailing path separator. */
10015 q[-1] = NUL;
10016 q = gettail(p);
10017 }
10018 if (q > p && !mch_isFullName(buf))
10019 {
10020 /* symlink is relative to directory of argument */
10021 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
10022 if (cpy != NULL)
10023 {
10024 STRCPY(cpy, p);
10025 STRCPY(gettail(cpy), buf);
10026 vim_free(p);
10027 p = cpy;
10028 }
10029 }
10030 else
10031 {
10032 vim_free(p);
10033 p = vim_strsave(buf);
10034 }
10035 }
10036
10037 if (remain == NULL)
10038 break;
10039
10040 /* Append the first path component of "remain" to "p". */
10041 q = getnextcomp(remain + 1);
10042 len = q - remain - (*q != NUL);
10043 cpy = vim_strnsave(p, STRLEN(p) + len);
10044 if (cpy != NULL)
10045 {
10046 STRNCAT(cpy, remain, len);
10047 vim_free(p);
10048 p = cpy;
10049 }
10050 /* Shorten "remain". */
10051 if (*q != NUL)
10052 STRMOVE(remain, q - 1);
10053 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010054 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010055 }
10056
10057 /* If the result is a relative path name, make it explicitly relative to
10058 * the current directory if and only if the argument had this form. */
10059 if (!vim_ispathsep(*p))
10060 {
10061 if (is_relative_to_current
10062 && *p != NUL
10063 && !(p[0] == '.'
10064 && (p[1] == NUL
10065 || vim_ispathsep(p[1])
10066 || (p[1] == '.'
10067 && (p[2] == NUL
10068 || vim_ispathsep(p[2]))))))
10069 {
10070 /* Prepend "./". */
10071 cpy = concat_str((char_u *)"./", p);
10072 if (cpy != NULL)
10073 {
10074 vim_free(p);
10075 p = cpy;
10076 }
10077 }
10078 else if (!is_relative_to_current)
10079 {
10080 /* Strip leading "./". */
10081 q = p;
10082 while (q[0] == '.' && vim_ispathsep(q[1]))
10083 q += 2;
10084 if (q > p)
10085 STRMOVE(p, p + 2);
10086 }
10087 }
10088
10089 /* Ensure that the result will have no trailing path separator
10090 * if the argument had none. But keep "/" or "//". */
10091 if (!has_trailing_pathsep)
10092 {
10093 q = p + STRLEN(p);
10094 if (after_pathsep(p, q))
10095 *gettail_sep(p) = NUL;
10096 }
10097
10098 rettv->vval.v_string = p;
10099 }
10100# else
10101 rettv->vval.v_string = vim_strsave(p);
10102# endif
10103#endif
10104
10105 simplify_filename(rettv->vval.v_string);
10106
10107#ifdef HAVE_READLINK
10108fail:
10109 vim_free(buf);
10110#endif
10111 rettv->v_type = VAR_STRING;
10112}
10113
10114/*
10115 * "reverse({list})" function
10116 */
10117 static void
10118f_reverse(typval_T *argvars, typval_T *rettv)
10119{
10120 list_T *l;
10121 listitem_T *li, *ni;
10122
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010123 if (argvars[0].v_type == VAR_BLOB)
10124 {
10125 blob_T *b = argvars[0].vval.v_blob;
10126 int i, len = blob_len(b);
10127
10128 for (i = 0; i < len / 2; i++)
10129 {
10130 int tmp = blob_get(b, i);
10131
10132 blob_set(b, i, blob_get(b, len - i - 1));
10133 blob_set(b, len - i - 1, tmp);
10134 }
10135 rettv_blob_set(rettv, b);
10136 return;
10137 }
10138
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010139 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010140 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010141 else if ((l = argvars[0].vval.v_list) != NULL
10142 && !tv_check_lock(l->lv_lock,
10143 (char_u *)N_("reverse() argument"), TRUE))
10144 {
10145 li = l->lv_last;
10146 l->lv_first = l->lv_last = NULL;
10147 l->lv_len = 0;
10148 while (li != NULL)
10149 {
10150 ni = li->li_prev;
10151 list_append(l, li);
10152 li = ni;
10153 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010154 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155 l->lv_idx = l->lv_len - l->lv_idx - 1;
10156 }
10157}
10158
10159#define SP_NOMOVE 0x01 /* don't move cursor */
10160#define SP_REPEAT 0x02 /* repeat to find outer pair */
10161#define SP_RETCOUNT 0x04 /* return matchcount */
10162#define SP_SETPCMARK 0x08 /* set previous context mark */
10163#define SP_START 0x10 /* accept match at start position */
10164#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10165#define SP_END 0x40 /* leave cursor at end of match */
10166#define SP_COLUMN 0x80 /* start at cursor column */
10167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168/*
10169 * Get flags for a search function.
10170 * Possibly sets "p_ws".
10171 * Returns BACKWARD, FORWARD or zero (for an error).
10172 */
10173 static int
10174get_search_arg(typval_T *varp, int *flagsp)
10175{
10176 int dir = FORWARD;
10177 char_u *flags;
10178 char_u nbuf[NUMBUFLEN];
10179 int mask;
10180
10181 if (varp->v_type != VAR_UNKNOWN)
10182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010183 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010184 if (flags == NULL)
10185 return 0; /* type error; errmsg already given */
10186 while (*flags != NUL)
10187 {
10188 switch (*flags)
10189 {
10190 case 'b': dir = BACKWARD; break;
10191 case 'w': p_ws = TRUE; break;
10192 case 'W': p_ws = FALSE; break;
10193 default: mask = 0;
10194 if (flagsp != NULL)
10195 switch (*flags)
10196 {
10197 case 'c': mask = SP_START; break;
10198 case 'e': mask = SP_END; break;
10199 case 'm': mask = SP_RETCOUNT; break;
10200 case 'n': mask = SP_NOMOVE; break;
10201 case 'p': mask = SP_SUBPAT; break;
10202 case 'r': mask = SP_REPEAT; break;
10203 case 's': mask = SP_SETPCMARK; break;
10204 case 'z': mask = SP_COLUMN; break;
10205 }
10206 if (mask == 0)
10207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010208 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 dir = 0;
10210 }
10211 else
10212 *flagsp |= mask;
10213 }
10214 if (dir == 0)
10215 break;
10216 ++flags;
10217 }
10218 }
10219 return dir;
10220}
10221
10222/*
10223 * Shared by search() and searchpos() functions.
10224 */
10225 static int
10226search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10227{
10228 int flags;
10229 char_u *pat;
10230 pos_T pos;
10231 pos_T save_cursor;
10232 int save_p_ws = p_ws;
10233 int dir;
10234 int retval = 0; /* default: FAIL */
10235 long lnum_stop = 0;
10236 proftime_T tm;
10237#ifdef FEAT_RELTIME
10238 long time_limit = 0;
10239#endif
10240 int options = SEARCH_KEEP;
10241 int subpatnum;
10242
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010243 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10245 if (dir == 0)
10246 goto theend;
10247 flags = *flagsp;
10248 if (flags & SP_START)
10249 options |= SEARCH_START;
10250 if (flags & SP_END)
10251 options |= SEARCH_END;
10252 if (flags & SP_COLUMN)
10253 options |= SEARCH_COL;
10254
10255 /* Optional arguments: line number to stop searching and timeout. */
10256 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010258 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010259 if (lnum_stop < 0)
10260 goto theend;
10261#ifdef FEAT_RELTIME
10262 if (argvars[3].v_type != VAR_UNKNOWN)
10263 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010264 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 if (time_limit < 0)
10266 goto theend;
10267 }
10268#endif
10269 }
10270
10271#ifdef FEAT_RELTIME
10272 /* Set the time limit, if there is one. */
10273 profile_setlimit(time_limit, &tm);
10274#endif
10275
10276 /*
10277 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10278 * Check to make sure only those flags are set.
10279 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10280 * flags cannot be set. Check for that condition also.
10281 */
10282 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10283 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010285 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010286 goto theend;
10287 }
10288
10289 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010290 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010291 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010292 if (subpatnum != FAIL)
10293 {
10294 if (flags & SP_SUBPAT)
10295 retval = subpatnum;
10296 else
10297 retval = pos.lnum;
10298 if (flags & SP_SETPCMARK)
10299 setpcmark();
10300 curwin->w_cursor = pos;
10301 if (match_pos != NULL)
10302 {
10303 /* Store the match cursor position */
10304 match_pos->lnum = pos.lnum;
10305 match_pos->col = pos.col + 1;
10306 }
10307 /* "/$" will put the cursor after the end of the line, may need to
10308 * correct that here */
10309 check_cursor();
10310 }
10311
10312 /* If 'n' flag is used: restore cursor position. */
10313 if (flags & SP_NOMOVE)
10314 curwin->w_cursor = save_cursor;
10315 else
10316 curwin->w_set_curswant = TRUE;
10317theend:
10318 p_ws = save_p_ws;
10319
10320 return retval;
10321}
10322
10323#ifdef FEAT_FLOAT
10324
10325/*
10326 * round() is not in C90, use ceil() or floor() instead.
10327 */
10328 float_T
10329vim_round(float_T f)
10330{
10331 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10332}
10333
10334/*
10335 * "round({float})" function
10336 */
10337 static void
10338f_round(typval_T *argvars, typval_T *rettv)
10339{
10340 float_T f = 0.0;
10341
10342 rettv->v_type = VAR_FLOAT;
10343 if (get_float_arg(argvars, &f) == OK)
10344 rettv->vval.v_float = vim_round(f);
10345 else
10346 rettv->vval.v_float = 0.0;
10347}
10348#endif
10349
10350/*
10351 * "screenattr()" function
10352 */
10353 static void
10354f_screenattr(typval_T *argvars, typval_T *rettv)
10355{
10356 int row;
10357 int col;
10358 int c;
10359
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010360 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10361 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010362 if (row < 0 || row >= screen_Rows
10363 || col < 0 || col >= screen_Columns)
10364 c = -1;
10365 else
10366 c = ScreenAttrs[LineOffset[row] + col];
10367 rettv->vval.v_number = c;
10368}
10369
10370/*
10371 * "screenchar()" function
10372 */
10373 static void
10374f_screenchar(typval_T *argvars, typval_T *rettv)
10375{
10376 int row;
10377 int col;
10378 int off;
10379 int c;
10380
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010381 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10382 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010383 if (row < 0 || row >= screen_Rows
10384 || col < 0 || col >= screen_Columns)
10385 c = -1;
10386 else
10387 {
10388 off = LineOffset[row] + col;
10389#ifdef FEAT_MBYTE
10390 if (enc_utf8 && ScreenLinesUC[off] != 0)
10391 c = ScreenLinesUC[off];
10392 else
10393#endif
10394 c = ScreenLines[off];
10395 }
10396 rettv->vval.v_number = c;
10397}
10398
10399/*
10400 * "screencol()" function
10401 *
10402 * First column is 1 to be consistent with virtcol().
10403 */
10404 static void
10405f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10406{
10407 rettv->vval.v_number = screen_screencol() + 1;
10408}
10409
10410/*
10411 * "screenrow()" function
10412 */
10413 static void
10414f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10415{
10416 rettv->vval.v_number = screen_screenrow() + 1;
10417}
10418
10419/*
10420 * "search()" function
10421 */
10422 static void
10423f_search(typval_T *argvars, typval_T *rettv)
10424{
10425 int flags = 0;
10426
10427 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10428}
10429
10430/*
10431 * "searchdecl()" function
10432 */
10433 static void
10434f_searchdecl(typval_T *argvars, typval_T *rettv)
10435{
10436 int locally = 1;
10437 int thisblock = 0;
10438 int error = FALSE;
10439 char_u *name;
10440
10441 rettv->vval.v_number = 1; /* default: FAIL */
10442
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010443 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010444 if (argvars[1].v_type != VAR_UNKNOWN)
10445 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010446 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010447 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010448 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010449 }
10450 if (!error && name != NULL)
10451 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10452 locally, thisblock, SEARCH_KEEP) == FAIL;
10453}
10454
10455/*
10456 * Used by searchpair() and searchpairpos()
10457 */
10458 static int
10459searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10460{
10461 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010462 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010463 int save_p_ws = p_ws;
10464 int dir;
10465 int flags = 0;
10466 char_u nbuf1[NUMBUFLEN];
10467 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 int retval = 0; /* default: FAIL */
10469 long lnum_stop = 0;
10470 long time_limit = 0;
10471
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010472 /* Get the three pattern arguments: start, middle, end. Will result in an
10473 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010474 spat = tv_get_string_chk(&argvars[0]);
10475 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10476 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010477 if (spat == NULL || mpat == NULL || epat == NULL)
10478 goto theend; /* type error */
10479
10480 /* Handle the optional fourth argument: flags */
10481 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10482 if (dir == 0)
10483 goto theend;
10484
10485 /* Don't accept SP_END or SP_SUBPAT.
10486 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10487 */
10488 if ((flags & (SP_END | SP_SUBPAT)) != 0
10489 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010491 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010492 goto theend;
10493 }
10494
10495 /* Using 'r' implies 'W', otherwise it doesn't work. */
10496 if (flags & SP_REPEAT)
10497 p_ws = FALSE;
10498
10499 /* Optional fifth argument: skip expression */
10500 if (argvars[3].v_type == VAR_UNKNOWN
10501 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010502 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010503 else
10504 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010505 skip = &argvars[4];
10506 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10507 && skip->v_type != VAR_STRING)
10508 {
10509 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010510 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010511 goto theend;
10512 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (argvars[5].v_type != VAR_UNKNOWN)
10514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010515 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010518 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010519 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010520 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010521#ifdef FEAT_RELTIME
10522 if (argvars[6].v_type != VAR_UNKNOWN)
10523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010524 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010525 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010527 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010530 }
10531#endif
10532 }
10533 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010534
10535 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10536 match_pos, lnum_stop, time_limit);
10537
10538theend:
10539 p_ws = save_p_ws;
10540
10541 return retval;
10542}
10543
10544/*
10545 * "searchpair()" function
10546 */
10547 static void
10548f_searchpair(typval_T *argvars, typval_T *rettv)
10549{
10550 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
10551}
10552
10553/*
10554 * "searchpairpos()" function
10555 */
10556 static void
10557f_searchpairpos(typval_T *argvars, typval_T *rettv)
10558{
10559 pos_T match_pos;
10560 int lnum = 0;
10561 int col = 0;
10562
10563 if (rettv_list_alloc(rettv) == FAIL)
10564 return;
10565
10566 if (searchpair_cmn(argvars, &match_pos) > 0)
10567 {
10568 lnum = match_pos.lnum;
10569 col = match_pos.col;
10570 }
10571
10572 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10573 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10574}
10575
10576/*
10577 * Search for a start/middle/end thing.
10578 * Used by searchpair(), see its documentation for the details.
10579 * Returns 0 or -1 for no match,
10580 */
10581 long
10582do_searchpair(
10583 char_u *spat, /* start pattern */
10584 char_u *mpat, /* middle pattern */
10585 char_u *epat, /* end pattern */
10586 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010010587 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 int flags, /* SP_SETPCMARK and other SP_ values */
10589 pos_T *match_pos,
10590 linenr_T lnum_stop, /* stop at this line if not zero */
10591 long time_limit UNUSED) /* stop after this many msec */
10592{
10593 char_u *save_cpo;
10594 char_u *pat, *pat2 = NULL, *pat3 = NULL;
10595 long retval = 0;
10596 pos_T pos;
10597 pos_T firstpos;
10598 pos_T foundpos;
10599 pos_T save_cursor;
10600 pos_T save_pos;
10601 int n;
10602 int r;
10603 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010010604 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010605 int err;
10606 int options = SEARCH_KEEP;
10607 proftime_T tm;
10608
10609 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10610 save_cpo = p_cpo;
10611 p_cpo = empty_option;
10612
10613#ifdef FEAT_RELTIME
10614 /* Set the time limit, if there is one. */
10615 profile_setlimit(time_limit, &tm);
10616#endif
10617
10618 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10619 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010620 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 17));
10621 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 if (pat2 == NULL || pat3 == NULL)
10623 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010624 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010625 if (*mpat == NUL)
10626 STRCPY(pat3, pat2);
10627 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010010628 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 spat, epat, mpat);
10630 if (flags & SP_START)
10631 options |= SEARCH_START;
10632
Bram Moolenaar48570482017-10-30 21:48:41 +010010633 if (skip != NULL)
10634 {
10635 /* Empty string means to not use the skip expression. */
10636 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
10637 use_skip = skip->vval.v_string != NULL
10638 && *skip->vval.v_string != NUL;
10639 }
10640
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010641 save_cursor = curwin->w_cursor;
10642 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010643 CLEAR_POS(&firstpos);
10644 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010645 pat = pat3;
10646 for (;;)
10647 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010648 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010649 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010650 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010651 /* didn't find it or found the first match again: FAIL */
10652 break;
10653
10654 if (firstpos.lnum == 0)
10655 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010010656 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010657 {
10658 /* Found the same position again. Can happen with a pattern that
10659 * has "\zs" at the end and searching backwards. Advance one
10660 * character and try again. */
10661 if (dir == BACKWARD)
10662 decl(&pos);
10663 else
10664 incl(&pos);
10665 }
10666 foundpos = pos;
10667
10668 /* clear the start flag to avoid getting stuck here */
10669 options &= ~SEARCH_START;
10670
10671 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010010672 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010673 {
10674 save_pos = curwin->w_cursor;
10675 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010010676 err = FALSE;
10677 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010678 curwin->w_cursor = save_pos;
10679 if (err)
10680 {
10681 /* Evaluating {skip} caused an error, break here. */
10682 curwin->w_cursor = save_cursor;
10683 retval = -1;
10684 break;
10685 }
10686 if (r)
10687 continue;
10688 }
10689
10690 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10691 {
10692 /* Found end when searching backwards or start when searching
10693 * forward: nested pair. */
10694 ++nest;
10695 pat = pat2; /* nested, don't search for middle */
10696 }
10697 else
10698 {
10699 /* Found end when searching forward or start when searching
10700 * backward: end of (nested) pair; or found middle in outer pair. */
10701 if (--nest == 1)
10702 pat = pat3; /* outer level, search for middle */
10703 }
10704
10705 if (nest == 0)
10706 {
10707 /* Found the match: return matchcount or line number. */
10708 if (flags & SP_RETCOUNT)
10709 ++retval;
10710 else
10711 retval = pos.lnum;
10712 if (flags & SP_SETPCMARK)
10713 setpcmark();
10714 curwin->w_cursor = pos;
10715 if (!(flags & SP_REPEAT))
10716 break;
10717 nest = 1; /* search for next unmatched */
10718 }
10719 }
10720
10721 if (match_pos != NULL)
10722 {
10723 /* Store the match cursor position */
10724 match_pos->lnum = curwin->w_cursor.lnum;
10725 match_pos->col = curwin->w_cursor.col + 1;
10726 }
10727
10728 /* If 'n' flag is used or search failed: restore cursor position. */
10729 if ((flags & SP_NOMOVE) || retval == 0)
10730 curwin->w_cursor = save_cursor;
10731
10732theend:
10733 vim_free(pat2);
10734 vim_free(pat3);
10735 if (p_cpo == empty_option)
10736 p_cpo = save_cpo;
10737 else
10738 /* Darn, evaluating the {skip} expression changed the value. */
10739 free_string_option(save_cpo);
10740
10741 return retval;
10742}
10743
10744/*
10745 * "searchpos()" function
10746 */
10747 static void
10748f_searchpos(typval_T *argvars, typval_T *rettv)
10749{
10750 pos_T match_pos;
10751 int lnum = 0;
10752 int col = 0;
10753 int n;
10754 int flags = 0;
10755
10756 if (rettv_list_alloc(rettv) == FAIL)
10757 return;
10758
10759 n = search_cmn(argvars, &match_pos, &flags);
10760 if (n > 0)
10761 {
10762 lnum = match_pos.lnum;
10763 col = match_pos.col;
10764 }
10765
10766 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
10767 list_append_number(rettv->vval.v_list, (varnumber_T)col);
10768 if (flags & SP_SUBPAT)
10769 list_append_number(rettv->vval.v_list, (varnumber_T)n);
10770}
10771
10772 static void
10773f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
10774{
10775#ifdef FEAT_CLIENTSERVER
10776 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010777 char_u *server = tv_get_string_chk(&argvars[0]);
10778 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779
10780 rettv->vval.v_number = -1;
10781 if (server == NULL || reply == NULL)
10782 return;
10783 if (check_restricted() || check_secure())
10784 return;
10785# ifdef FEAT_X11
10786 if (check_connection() == FAIL)
10787 return;
10788# endif
10789
10790 if (serverSendReply(server, reply) < 0)
10791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010792 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010793 return;
10794 }
10795 rettv->vval.v_number = 0;
10796#else
10797 rettv->vval.v_number = -1;
10798#endif
10799}
10800
10801 static void
10802f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
10803{
10804 char_u *r = NULL;
10805
10806#ifdef FEAT_CLIENTSERVER
10807# ifdef WIN32
10808 r = serverGetVimNames();
10809# else
10810 make_connection();
10811 if (X_DISPLAY != NULL)
10812 r = serverGetVimNames(X_DISPLAY);
10813# endif
10814#endif
10815 rettv->v_type = VAR_STRING;
10816 rettv->vval.v_string = r;
10817}
10818
10819/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010820 * "setbufline()" function
10821 */
10822 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020010823f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010824{
10825 linenr_T lnum;
10826 buf_T *buf;
10827
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010828 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010829 if (buf == NULL)
10830 rettv->vval.v_number = 1; /* FAIL */
10831 else
10832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010833 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020010834 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020010835 }
10836}
10837
10838/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010839 * "setbufvar()" function
10840 */
10841 static void
10842f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
10843{
10844 buf_T *buf;
10845 char_u *varname, *bufvarname;
10846 typval_T *varp;
10847 char_u nbuf[NUMBUFLEN];
10848
10849 if (check_restricted() || check_secure())
10850 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010851 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
10852 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010010853 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 varp = &argvars[2];
10855
10856 if (buf != NULL && varname != NULL && varp != NULL)
10857 {
10858 if (*varname == '&')
10859 {
10860 long numval;
10861 char_u *strval;
10862 int error = FALSE;
10863 aco_save_T aco;
10864
10865 /* set curbuf to be our buf, temporarily */
10866 aucmd_prepbuf(&aco, buf);
10867
10868 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010869 numval = (long)tv_get_number_chk(varp, &error);
10870 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010871 if (!error && strval != NULL)
10872 set_option_value(varname, numval, strval, OPT_LOCAL);
10873
10874 /* reset notion of buffer */
10875 aucmd_restbuf(&aco);
10876 }
10877 else
10878 {
10879 buf_T *save_curbuf = curbuf;
10880
10881 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10882 if (bufvarname != NULL)
10883 {
10884 curbuf = buf;
10885 STRCPY(bufvarname, "b:");
10886 STRCPY(bufvarname + 2, varname);
10887 set_var(bufvarname, varp, TRUE);
10888 vim_free(bufvarname);
10889 curbuf = save_curbuf;
10890 }
10891 }
10892 }
10893}
10894
10895 static void
10896f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
10897{
10898 dict_T *d;
10899 dictitem_T *di;
10900 char_u *csearch;
10901
10902 if (argvars[0].v_type != VAR_DICT)
10903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010904 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905 return;
10906 }
10907
10908 if ((d = argvars[0].vval.v_dict) != NULL)
10909 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010010910 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 if (csearch != NULL)
10912 {
10913#ifdef FEAT_MBYTE
10914 if (enc_utf8)
10915 {
10916 int pcc[MAX_MCO];
10917 int c = utfc_ptr2char(csearch, pcc);
10918
10919 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
10920 }
10921 else
10922#endif
10923 set_last_csearch(PTR2CHAR(csearch),
10924 csearch, MB_PTR2LEN(csearch));
10925 }
10926
10927 di = dict_find(d, (char_u *)"forward", -1);
10928 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010929 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010930 ? FORWARD : BACKWARD);
10931
10932 di = dict_find(d, (char_u *)"until", -1);
10933 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010934 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 }
10936}
10937
10938/*
10939 * "setcmdpos()" function
10940 */
10941 static void
10942f_setcmdpos(typval_T *argvars, typval_T *rettv)
10943{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010944 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010945
10946 if (pos >= 0)
10947 rettv->vval.v_number = set_cmdline_pos(pos);
10948}
10949
10950/*
10951 * "setfperm({fname}, {mode})" function
10952 */
10953 static void
10954f_setfperm(typval_T *argvars, typval_T *rettv)
10955{
10956 char_u *fname;
10957 char_u modebuf[NUMBUFLEN];
10958 char_u *mode_str;
10959 int i;
10960 int mask;
10961 int mode = 0;
10962
10963 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010964 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965 if (fname == NULL)
10966 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010967 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (mode_str == NULL)
10969 return;
10970 if (STRLEN(mode_str) != 9)
10971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010972 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010973 return;
10974 }
10975
10976 mask = 1;
10977 for (i = 8; i >= 0; --i)
10978 {
10979 if (mode_str[i] != '-')
10980 mode |= mask;
10981 mask = mask << 1;
10982 }
10983 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
10984}
10985
10986/*
10987 * "setline()" function
10988 */
10989 static void
10990f_setline(typval_T *argvars, typval_T *rettv)
10991{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010992 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010993
Bram Moolenaarca851592018-06-06 21:04:07 +020010994 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995}
10996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997/*
10998 * Used by "setqflist()" and "setloclist()" functions
10999 */
11000 static void
11001set_qf_ll_list(
11002 win_T *wp UNUSED,
11003 typval_T *list_arg UNUSED,
11004 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011005 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011006 typval_T *rettv)
11007{
11008#ifdef FEAT_QUICKFIX
11009 static char *e_invact = N_("E927: Invalid action: '%s'");
11010 char_u *act;
11011 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011012 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013#endif
11014
11015 rettv->vval.v_number = -1;
11016
11017#ifdef FEAT_QUICKFIX
11018 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011019 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011020 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011021 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011022 else
11023 {
11024 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011025 dict_T *d = NULL;
11026 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011027
11028 if (action_arg->v_type == VAR_STRING)
11029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011030 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 if (act == NULL)
11032 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011033 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11034 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011035 action = *act;
11036 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011037 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011038 }
11039 else if (action_arg->v_type == VAR_UNKNOWN)
11040 action = ' ';
11041 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011042 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043
Bram Moolenaard823fa92016-08-12 16:29:27 +020011044 if (action_arg->v_type != VAR_UNKNOWN
11045 && what_arg->v_type != VAR_UNKNOWN)
11046 {
11047 if (what_arg->v_type == VAR_DICT)
11048 d = what_arg->vval.v_dict;
11049 else
11050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011051 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011052 valid_dict = FALSE;
11053 }
11054 }
11055
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011056 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011057 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011058 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11059 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011061 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062 }
11063#endif
11064}
11065
11066/*
11067 * "setloclist()" function
11068 */
11069 static void
11070f_setloclist(typval_T *argvars, typval_T *rettv)
11071{
11072 win_T *win;
11073
11074 rettv->vval.v_number = -1;
11075
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011076 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011077 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011078 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079}
11080
11081/*
11082 * "setmatches()" function
11083 */
11084 static void
11085f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11086{
11087#ifdef FEAT_SEARCH_EXTRA
11088 list_T *l;
11089 listitem_T *li;
11090 dict_T *d;
11091 list_T *s = NULL;
11092
11093 rettv->vval.v_number = -1;
11094 if (argvars[0].v_type != VAR_LIST)
11095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011096 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 return;
11098 }
11099 if ((l = argvars[0].vval.v_list) != NULL)
11100 {
11101
11102 /* To some extent make sure that we are dealing with a list from
11103 * "getmatches()". */
11104 li = l->lv_first;
11105 while (li != NULL)
11106 {
11107 if (li->li_tv.v_type != VAR_DICT
11108 || (d = li->li_tv.vval.v_dict) == NULL)
11109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011110 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011111 return;
11112 }
11113 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11114 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11115 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11116 && dict_find(d, (char_u *)"priority", -1) != NULL
11117 && dict_find(d, (char_u *)"id", -1) != NULL))
11118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011119 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120 return;
11121 }
11122 li = li->li_next;
11123 }
11124
11125 clear_matches(curwin);
11126 li = l->lv_first;
11127 while (li != NULL)
11128 {
11129 int i = 0;
11130 char_u buf[5];
11131 dictitem_T *di;
11132 char_u *group;
11133 int priority;
11134 int id;
11135 char_u *conceal;
11136
11137 d = li->li_tv.vval.v_dict;
11138 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11139 {
11140 if (s == NULL)
11141 {
11142 s = list_alloc();
11143 if (s == NULL)
11144 return;
11145 }
11146
11147 /* match from matchaddpos() */
11148 for (i = 1; i < 9; i++)
11149 {
11150 sprintf((char *)buf, (char *)"pos%d", i);
11151 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11152 {
11153 if (di->di_tv.v_type != VAR_LIST)
11154 return;
11155
11156 list_append_tv(s, &di->di_tv);
11157 s->lv_refcount++;
11158 }
11159 else
11160 break;
11161 }
11162 }
11163
Bram Moolenaar8f667172018-12-14 15:38:31 +010011164 group = dict_get_string(d, (char_u *)"group", TRUE);
11165 priority = (int)dict_get_number(d, (char_u *)"priority");
11166 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011167 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011168 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011169 : NULL;
11170 if (i == 0)
11171 {
11172 match_add(curwin, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011173 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011174 priority, id, NULL, conceal);
11175 }
11176 else
11177 {
11178 match_add(curwin, group, NULL, priority, id, s, conceal);
11179 list_unref(s);
11180 s = NULL;
11181 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011182 vim_free(group);
11183 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184
11185 li = li->li_next;
11186 }
11187 rettv->vval.v_number = 0;
11188 }
11189#endif
11190}
11191
11192/*
11193 * "setpos()" function
11194 */
11195 static void
11196f_setpos(typval_T *argvars, typval_T *rettv)
11197{
11198 pos_T pos;
11199 int fnum;
11200 char_u *name;
11201 colnr_T curswant = -1;
11202
11203 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011204 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205 if (name != NULL)
11206 {
11207 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11208 {
11209 if (--pos.col < 0)
11210 pos.col = 0;
11211 if (name[0] == '.' && name[1] == NUL)
11212 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011213 /* set cursor; "fnum" is ignored */
11214 curwin->w_cursor = pos;
11215 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011216 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011217 curwin->w_curswant = curswant - 1;
11218 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011219 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011220 check_cursor();
11221 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011222 }
11223 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11224 {
11225 /* set mark */
11226 if (setmark_pos(name[1], &pos, fnum) == OK)
11227 rettv->vval.v_number = 0;
11228 }
11229 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011230 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011231 }
11232 }
11233}
11234
11235/*
11236 * "setqflist()" function
11237 */
11238 static void
11239f_setqflist(typval_T *argvars, typval_T *rettv)
11240{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011241 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011242}
11243
11244/*
11245 * "setreg()" function
11246 */
11247 static void
11248f_setreg(typval_T *argvars, typval_T *rettv)
11249{
11250 int regname;
11251 char_u *strregname;
11252 char_u *stropt;
11253 char_u *strval;
11254 int append;
11255 char_u yank_type;
11256 long block_len;
11257
11258 block_len = -1;
11259 yank_type = MAUTO;
11260 append = FALSE;
11261
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011262 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011263 rettv->vval.v_number = 1; /* FAIL is default */
11264
11265 if (strregname == NULL)
11266 return; /* type error; errmsg already given */
11267 regname = *strregname;
11268 if (regname == 0 || regname == '@')
11269 regname = '"';
11270
11271 if (argvars[2].v_type != VAR_UNKNOWN)
11272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011273 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011274 if (stropt == NULL)
11275 return; /* type error */
11276 for (; *stropt != NUL; ++stropt)
11277 switch (*stropt)
11278 {
11279 case 'a': case 'A': /* append */
11280 append = TRUE;
11281 break;
11282 case 'v': case 'c': /* character-wise selection */
11283 yank_type = MCHAR;
11284 break;
11285 case 'V': case 'l': /* line-wise selection */
11286 yank_type = MLINE;
11287 break;
11288 case 'b': case Ctrl_V: /* block-wise selection */
11289 yank_type = MBLOCK;
11290 if (VIM_ISDIGIT(stropt[1]))
11291 {
11292 ++stropt;
11293 block_len = getdigits(&stropt) - 1;
11294 --stropt;
11295 }
11296 break;
11297 }
11298 }
11299
11300 if (argvars[1].v_type == VAR_LIST)
11301 {
11302 char_u **lstval;
11303 char_u **allocval;
11304 char_u buf[NUMBUFLEN];
11305 char_u **curval;
11306 char_u **curallocval;
11307 list_T *ll = argvars[1].vval.v_list;
11308 listitem_T *li;
11309 int len;
11310
11311 /* If the list is NULL handle like an empty list. */
11312 len = ll == NULL ? 0 : ll->lv_len;
11313
11314 /* First half: use for pointers to result lines; second half: use for
11315 * pointers to allocated copies. */
11316 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11317 if (lstval == NULL)
11318 return;
11319 curval = lstval;
11320 allocval = lstval + len + 2;
11321 curallocval = allocval;
11322
11323 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11324 li = li->li_next)
11325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011326 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 if (strval == NULL)
11328 goto free_lstval;
11329 if (strval == buf)
11330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011331 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 * overwrite the string. */
11333 strval = vim_strsave(buf);
11334 if (strval == NULL)
11335 goto free_lstval;
11336 *curallocval++ = strval;
11337 }
11338 *curval++ = strval;
11339 }
11340 *curval++ = NULL;
11341
11342 write_reg_contents_lst(regname, lstval, -1,
11343 append, yank_type, block_len);
11344free_lstval:
11345 while (curallocval > allocval)
11346 vim_free(*--curallocval);
11347 vim_free(lstval);
11348 }
11349 else
11350 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011351 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011352 if (strval == NULL)
11353 return;
11354 write_reg_contents_ex(regname, strval, -1,
11355 append, yank_type, block_len);
11356 }
11357 rettv->vval.v_number = 0;
11358}
11359
11360/*
11361 * "settabvar()" function
11362 */
11363 static void
11364f_settabvar(typval_T *argvars, typval_T *rettv)
11365{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 tabpage_T *save_curtab;
11367 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 char_u *varname, *tabvarname;
11369 typval_T *varp;
11370
11371 rettv->vval.v_number = 0;
11372
11373 if (check_restricted() || check_secure())
11374 return;
11375
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011376 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11377 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 varp = &argvars[2];
11379
Bram Moolenaar4033c552017-09-16 20:54:51 +020011380 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 save_curtab = curtab;
11383 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384
11385 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
11386 if (tabvarname != NULL)
11387 {
11388 STRCPY(tabvarname, "t:");
11389 STRCPY(tabvarname + 2, varname);
11390 set_var(tabvarname, varp, TRUE);
11391 vim_free(tabvarname);
11392 }
11393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011394 /* Restore current tabpage */
11395 if (valid_tabpage(save_curtab))
11396 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011397 }
11398}
11399
11400/*
11401 * "settabwinvar()" function
11402 */
11403 static void
11404f_settabwinvar(typval_T *argvars, typval_T *rettv)
11405{
11406 setwinvar(argvars, rettv, 1);
11407}
11408
11409/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011410 * "settagstack()" function
11411 */
11412 static void
11413f_settagstack(typval_T *argvars, typval_T *rettv)
11414{
11415 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11416 win_T *wp;
11417 dict_T *d;
11418 int action = 'r';
11419
11420 rettv->vval.v_number = -1;
11421
11422 // first argument: window number or id
11423 wp = find_win_by_nr_or_id(&argvars[0]);
11424 if (wp == NULL)
11425 return;
11426
11427 // second argument: dict with items to set in the tag stack
11428 if (argvars[1].v_type != VAR_DICT)
11429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011430 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011431 return;
11432 }
11433 d = argvars[1].vval.v_dict;
11434 if (d == NULL)
11435 return;
11436
11437 // third argument: action - 'a' for append and 'r' for replace.
11438 // default is to replace the stack.
11439 if (argvars[2].v_type == VAR_UNKNOWN)
11440 action = 'r';
11441 else if (argvars[2].v_type == VAR_STRING)
11442 {
11443 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011444 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011445 if (actstr == NULL)
11446 return;
11447 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11448 action = *actstr;
11449 else
11450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011451 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011452 return;
11453 }
11454 }
11455 else
11456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011457 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011458 return;
11459 }
11460
11461 if (set_tagstack(wp, d, action) == OK)
11462 rettv->vval.v_number = 0;
11463}
11464
11465/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466 * "setwinvar()" function
11467 */
11468 static void
11469f_setwinvar(typval_T *argvars, typval_T *rettv)
11470{
11471 setwinvar(argvars, rettv, 0);
11472}
11473
11474#ifdef FEAT_CRYPT
11475/*
11476 * "sha256({string})" function
11477 */
11478 static void
11479f_sha256(typval_T *argvars, typval_T *rettv)
11480{
11481 char_u *p;
11482
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011483 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484 rettv->vval.v_string = vim_strsave(
11485 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11486 rettv->v_type = VAR_STRING;
11487}
11488#endif /* FEAT_CRYPT */
11489
11490/*
11491 * "shellescape({string})" function
11492 */
11493 static void
11494f_shellescape(typval_T *argvars, typval_T *rettv)
11495{
Bram Moolenaar20615522017-06-05 18:46:26 +020011496 int do_special = non_zero_arg(&argvars[1]);
11497
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011498 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 rettv->v_type = VAR_STRING;
11501}
11502
11503/*
11504 * shiftwidth() function
11505 */
11506 static void
11507f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11508{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011509 rettv->vval.v_number = 0;
11510
11511 if (argvars[0].v_type != VAR_UNKNOWN)
11512 {
11513 long col;
11514
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011515 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011516 if (col < 0)
11517 return; // type error; errmsg already given
11518#ifdef FEAT_VARTABS
11519 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11520 return;
11521#endif
11522 }
11523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011524 rettv->vval.v_number = get_sw_value(curbuf);
11525}
11526
Bram Moolenaar162b7142018-12-21 15:17:36 +010011527#ifdef FEAT_SIGNS
11528/*
11529 * "sign_define()" function
11530 */
11531 static void
11532f_sign_define(typval_T *argvars, typval_T *rettv)
11533{
11534 char_u *name;
11535 dict_T *dict;
11536 char_u *icon = NULL;
11537 char_u *linehl = NULL;
11538 char_u *text = NULL;
11539 char_u *texthl = NULL;
11540
11541 rettv->vval.v_number = -1;
11542
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011543 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011544 if (name == NULL)
11545 return;
11546
11547 if (argvars[1].v_type != VAR_UNKNOWN)
11548 {
11549 if (argvars[1].v_type != VAR_DICT)
11550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011551 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011552 return;
11553 }
11554
11555 // sign attributes
11556 dict = argvars[1].vval.v_dict;
11557 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
11558 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
11559 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
11560 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
11561 if (dict_find(dict, (char_u *)"text", -1) != NULL)
11562 text = dict_get_string(dict, (char_u *)"text", TRUE);
11563 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
11564 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
11565 }
11566
11567 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
11568 rettv->vval.v_number = 0;
11569
11570 vim_free(icon);
11571 vim_free(linehl);
11572 vim_free(text);
11573 vim_free(texthl);
11574}
11575
11576/*
11577 * "sign_getdefined()" function
11578 */
11579 static void
11580f_sign_getdefined(typval_T *argvars, typval_T *rettv)
11581{
11582 char_u *name = NULL;
11583
11584 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
11585 return;
11586
11587 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011588 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011589
11590 sign_getlist(name, rettv->vval.v_list);
11591}
11592
11593/*
11594 * "sign_getplaced()" function
11595 */
11596 static void
11597f_sign_getplaced(typval_T *argvars, typval_T *rettv)
11598{
11599 buf_T *buf = NULL;
11600 dict_T *dict;
11601 dictitem_T *di;
11602 linenr_T lnum = 0;
11603 int sign_id = 0;
11604 char_u *group = NULL;
11605 int notanum = FALSE;
11606
11607 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
11608 return;
11609
11610 if (argvars[0].v_type != VAR_UNKNOWN)
11611 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011612 // get signs placed in the specified buffer
11613 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011614 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011615 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011616
11617 if (argvars[1].v_type != VAR_UNKNOWN)
11618 {
11619 if (argvars[1].v_type != VAR_DICT ||
11620 ((dict = argvars[1].vval.v_dict) == NULL))
11621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011622 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011623 return;
11624 }
11625 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11626 {
11627 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011628 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011629 if (notanum)
11630 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011631 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011632 }
11633 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
11634 {
11635 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011636 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011637 if (notanum)
11638 return;
11639 }
11640 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
11641 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011642 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011643 if (group == NULL)
11644 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010011645 if (*group == '\0') // empty string means global group
11646 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011647 }
11648 }
11649 }
11650
11651 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
11652}
11653
11654/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011655 * "sign_jump()" function
11656 */
11657 static void
11658f_sign_jump(typval_T *argvars, typval_T *rettv)
11659{
11660 int sign_id;
11661 char_u *sign_group = NULL;
11662 buf_T *buf;
11663 int notanum = FALSE;
11664
11665 rettv->vval.v_number = -1;
11666
11667 // Sign identifer
11668 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
11669 if (notanum)
11670 return;
11671 if (sign_id <= 0)
11672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011673 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011674 return;
11675 }
11676
11677 // Sign group
11678 sign_group = tv_get_string_chk(&argvars[1]);
11679 if (sign_group == NULL)
11680 return;
11681 if (sign_group[0] == '\0')
11682 sign_group = NULL; // global sign group
11683 else
11684 {
11685 sign_group = vim_strsave(sign_group);
11686 if (sign_group == NULL)
11687 return;
11688 }
11689
11690 // Buffer to place the sign
11691 buf = get_buf_arg(&argvars[2]);
11692 if (buf == NULL)
11693 goto cleanup;
11694
11695 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
11696
11697cleanup:
11698 vim_free(sign_group);
11699}
11700
11701/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010011702 * "sign_place()" function
11703 */
11704 static void
11705f_sign_place(typval_T *argvars, typval_T *rettv)
11706{
11707 int sign_id;
11708 char_u *group = NULL;
11709 char_u *sign_name;
11710 buf_T *buf;
11711 dict_T *dict;
11712 dictitem_T *di;
11713 linenr_T lnum = 0;
11714 int prio = SIGN_DEF_PRIO;
11715 int notanum = FALSE;
11716
11717 rettv->vval.v_number = -1;
11718
11719 // Sign identifer
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011720 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011721 if (notanum)
11722 return;
11723 if (sign_id < 0)
11724 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011725 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011726 return;
11727 }
11728
11729 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011731 if (group == NULL)
11732 return;
11733 if (group[0] == '\0')
11734 group = NULL; // global sign group
11735 else
11736 {
11737 group = vim_strsave(group);
11738 if (group == NULL)
11739 return;
11740 }
11741
11742 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011743 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011744 if (sign_name == NULL)
11745 goto cleanup;
11746
11747 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011748 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011749 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011750 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011751
11752 if (argvars[4].v_type != VAR_UNKNOWN)
11753 {
11754 if (argvars[4].v_type != VAR_DICT ||
11755 ((dict = argvars[4].vval.v_dict) == NULL))
11756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011757 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011758 goto cleanup;
11759 }
11760
11761 // Line number where the sign is to be placed
11762 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
11763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011764 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011765 if (notanum)
11766 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011767 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011768 }
11769 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
11770 {
11771 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011772 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011773 if (notanum)
11774 goto cleanup;
11775 }
11776 }
11777
11778 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
11779 rettv->vval.v_number = sign_id;
11780
11781cleanup:
11782 vim_free(group);
11783}
11784
11785/*
11786 * "sign_undefine()" function
11787 */
11788 static void
11789f_sign_undefine(typval_T *argvars, typval_T *rettv)
11790{
11791 char_u *name;
11792
11793 rettv->vval.v_number = -1;
11794
11795 if (argvars[0].v_type == VAR_UNKNOWN)
11796 {
11797 // Free all the signs
11798 free_signs();
11799 rettv->vval.v_number = 0;
11800 }
11801 else
11802 {
11803 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011804 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011805 if (name == NULL)
11806 return;
11807
11808 if (sign_undefine_by_name(name) == OK)
11809 rettv->vval.v_number = 0;
11810 }
11811}
11812
11813/*
11814 * "sign_unplace()" function
11815 */
11816 static void
11817f_sign_unplace(typval_T *argvars, typval_T *rettv)
11818{
11819 dict_T *dict;
11820 dictitem_T *di;
11821 int sign_id = 0;
11822 buf_T *buf = NULL;
11823 char_u *group = NULL;
11824
11825 rettv->vval.v_number = -1;
11826
11827 if (argvars[0].v_type != VAR_STRING)
11828 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011829 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010011830 return;
11831 }
11832
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011833 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011834 if (group[0] == '\0')
11835 group = NULL; // global sign group
11836 else
11837 {
11838 group = vim_strsave(group);
11839 if (group == NULL)
11840 return;
11841 }
11842
11843 if (argvars[1].v_type != VAR_UNKNOWN)
11844 {
11845 if (argvars[1].v_type != VAR_DICT)
11846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011847 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011848 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011849 }
11850 dict = argvars[1].vval.v_dict;
11851
11852 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
11853 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010011854 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010011855 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011856 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010011857 }
11858 if (dict_find(dict, (char_u *)"id", -1) != NULL)
11859 sign_id = dict_get_number(dict, (char_u *)"id");
11860 }
11861
11862 if (buf == NULL)
11863 {
11864 // Delete the sign in all the buffers
11865 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011866 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011867 rettv->vval.v_number = 0;
11868 }
11869 else
11870 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010011871 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010011872 rettv->vval.v_number = 0;
11873 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010011874
11875cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010011876 vim_free(group);
11877}
11878#endif
11879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011880/*
11881 * "simplify()" function
11882 */
11883 static void
11884f_simplify(typval_T *argvars, typval_T *rettv)
11885{
11886 char_u *p;
11887
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011888 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011889 rettv->vval.v_string = vim_strsave(p);
11890 simplify_filename(rettv->vval.v_string); /* simplify in place */
11891 rettv->v_type = VAR_STRING;
11892}
11893
11894#ifdef FEAT_FLOAT
11895/*
11896 * "sin()" function
11897 */
11898 static void
11899f_sin(typval_T *argvars, typval_T *rettv)
11900{
11901 float_T f = 0.0;
11902
11903 rettv->v_type = VAR_FLOAT;
11904 if (get_float_arg(argvars, &f) == OK)
11905 rettv->vval.v_float = sin(f);
11906 else
11907 rettv->vval.v_float = 0.0;
11908}
11909
11910/*
11911 * "sinh()" function
11912 */
11913 static void
11914f_sinh(typval_T *argvars, typval_T *rettv)
11915{
11916 float_T f = 0.0;
11917
11918 rettv->v_type = VAR_FLOAT;
11919 if (get_float_arg(argvars, &f) == OK)
11920 rettv->vval.v_float = sinh(f);
11921 else
11922 rettv->vval.v_float = 0.0;
11923}
11924#endif
11925
11926static int
11927#ifdef __BORLANDC__
11928 _RTLENTRYF
11929#endif
11930 item_compare(const void *s1, const void *s2);
11931static int
11932#ifdef __BORLANDC__
11933 _RTLENTRYF
11934#endif
11935 item_compare2(const void *s1, const void *s2);
11936
11937/* struct used in the array that's given to qsort() */
11938typedef struct
11939{
11940 listitem_T *item;
11941 int idx;
11942} sortItem_T;
11943
11944/* struct storing information about current sort */
11945typedef struct
11946{
11947 int item_compare_ic;
11948 int item_compare_numeric;
11949 int item_compare_numbers;
11950#ifdef FEAT_FLOAT
11951 int item_compare_float;
11952#endif
11953 char_u *item_compare_func;
11954 partial_T *item_compare_partial;
11955 dict_T *item_compare_selfdict;
11956 int item_compare_func_err;
11957 int item_compare_keep_zero;
11958} sortinfo_T;
11959static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011960#define ITEM_COMPARE_FAIL 999
11961
11962/*
11963 * Compare functions for f_sort() and f_uniq() below.
11964 */
11965 static int
11966#ifdef __BORLANDC__
11967_RTLENTRYF
11968#endif
11969item_compare(const void *s1, const void *s2)
11970{
11971 sortItem_T *si1, *si2;
11972 typval_T *tv1, *tv2;
11973 char_u *p1, *p2;
11974 char_u *tofree1 = NULL, *tofree2 = NULL;
11975 int res;
11976 char_u numbuf1[NUMBUFLEN];
11977 char_u numbuf2[NUMBUFLEN];
11978
11979 si1 = (sortItem_T *)s1;
11980 si2 = (sortItem_T *)s2;
11981 tv1 = &si1->item->li_tv;
11982 tv2 = &si2->item->li_tv;
11983
11984 if (sortinfo->item_compare_numbers)
11985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011986 varnumber_T v1 = tv_get_number(tv1);
11987 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011988
11989 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11990 }
11991
11992#ifdef FEAT_FLOAT
11993 if (sortinfo->item_compare_float)
11994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011995 float_T v1 = tv_get_float(tv1);
11996 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011997
11998 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
11999 }
12000#endif
12001
12002 /* tv2string() puts quotes around a string and allocates memory. Don't do
12003 * that for string variables. Use a single quote when comparing with a
12004 * non-string to do what the docs promise. */
12005 if (tv1->v_type == VAR_STRING)
12006 {
12007 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12008 p1 = (char_u *)"'";
12009 else
12010 p1 = tv1->vval.v_string;
12011 }
12012 else
12013 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12014 if (tv2->v_type == VAR_STRING)
12015 {
12016 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12017 p2 = (char_u *)"'";
12018 else
12019 p2 = tv2->vval.v_string;
12020 }
12021 else
12022 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12023 if (p1 == NULL)
12024 p1 = (char_u *)"";
12025 if (p2 == NULL)
12026 p2 = (char_u *)"";
12027 if (!sortinfo->item_compare_numeric)
12028 {
12029 if (sortinfo->item_compare_ic)
12030 res = STRICMP(p1, p2);
12031 else
12032 res = STRCMP(p1, p2);
12033 }
12034 else
12035 {
12036 double n1, n2;
12037 n1 = strtod((char *)p1, (char **)&p1);
12038 n2 = strtod((char *)p2, (char **)&p2);
12039 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12040 }
12041
12042 /* When the result would be zero, compare the item indexes. Makes the
12043 * sort stable. */
12044 if (res == 0 && !sortinfo->item_compare_keep_zero)
12045 res = si1->idx > si2->idx ? 1 : -1;
12046
12047 vim_free(tofree1);
12048 vim_free(tofree2);
12049 return res;
12050}
12051
12052 static int
12053#ifdef __BORLANDC__
12054_RTLENTRYF
12055#endif
12056item_compare2(const void *s1, const void *s2)
12057{
12058 sortItem_T *si1, *si2;
12059 int res;
12060 typval_T rettv;
12061 typval_T argv[3];
12062 int dummy;
12063 char_u *func_name;
12064 partial_T *partial = sortinfo->item_compare_partial;
12065
12066 /* shortcut after failure in previous call; compare all items equal */
12067 if (sortinfo->item_compare_func_err)
12068 return 0;
12069
12070 si1 = (sortItem_T *)s1;
12071 si2 = (sortItem_T *)s2;
12072
12073 if (partial == NULL)
12074 func_name = sortinfo->item_compare_func;
12075 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012076 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012077
12078 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12079 * in the copy without changing the original list items. */
12080 copy_tv(&si1->item->li_tv, &argv[0]);
12081 copy_tv(&si2->item->li_tv, &argv[1]);
12082
12083 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12084 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaardf48fb42016-07-22 21:50:18 +020012085 &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012086 partial, sortinfo->item_compare_selfdict);
12087 clear_tv(&argv[0]);
12088 clear_tv(&argv[1]);
12089
12090 if (res == FAIL)
12091 res = ITEM_COMPARE_FAIL;
12092 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012093 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012094 if (sortinfo->item_compare_func_err)
12095 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12096 clear_tv(&rettv);
12097
12098 /* When the result would be zero, compare the pointers themselves. Makes
12099 * the sort stable. */
12100 if (res == 0 && !sortinfo->item_compare_keep_zero)
12101 res = si1->idx > si2->idx ? 1 : -1;
12102
12103 return res;
12104}
12105
12106/*
12107 * "sort({list})" function
12108 */
12109 static void
12110do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12111{
12112 list_T *l;
12113 listitem_T *li;
12114 sortItem_T *ptrs;
12115 sortinfo_T *old_sortinfo;
12116 sortinfo_T info;
12117 long len;
12118 long i;
12119
12120 /* Pointer to current info struct used in compare function. Save and
12121 * restore the current one for nested calls. */
12122 old_sortinfo = sortinfo;
12123 sortinfo = &info;
12124
12125 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012126 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012127 else
12128 {
12129 l = argvars[0].vval.v_list;
12130 if (l == NULL || tv_check_lock(l->lv_lock,
12131 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12132 TRUE))
12133 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012134 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012135
12136 len = list_len(l);
12137 if (len <= 1)
12138 goto theend; /* short list sorts pretty quickly */
12139
12140 info.item_compare_ic = FALSE;
12141 info.item_compare_numeric = FALSE;
12142 info.item_compare_numbers = FALSE;
12143#ifdef FEAT_FLOAT
12144 info.item_compare_float = FALSE;
12145#endif
12146 info.item_compare_func = NULL;
12147 info.item_compare_partial = NULL;
12148 info.item_compare_selfdict = NULL;
12149 if (argvars[1].v_type != VAR_UNKNOWN)
12150 {
12151 /* optional second argument: {func} */
12152 if (argvars[1].v_type == VAR_FUNC)
12153 info.item_compare_func = argvars[1].vval.v_string;
12154 else if (argvars[1].v_type == VAR_PARTIAL)
12155 info.item_compare_partial = argvars[1].vval.v_partial;
12156 else
12157 {
12158 int error = FALSE;
12159
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012160 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012161 if (error)
12162 goto theend; /* type error; errmsg already given */
12163 if (i == 1)
12164 info.item_compare_ic = TRUE;
12165 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012167 else if (i != 0)
12168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012169 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012170 goto theend;
12171 }
12172 if (info.item_compare_func != NULL)
12173 {
12174 if (*info.item_compare_func == NUL)
12175 {
12176 /* empty string means default sort */
12177 info.item_compare_func = NULL;
12178 }
12179 else if (STRCMP(info.item_compare_func, "n") == 0)
12180 {
12181 info.item_compare_func = NULL;
12182 info.item_compare_numeric = TRUE;
12183 }
12184 else if (STRCMP(info.item_compare_func, "N") == 0)
12185 {
12186 info.item_compare_func = NULL;
12187 info.item_compare_numbers = TRUE;
12188 }
12189#ifdef FEAT_FLOAT
12190 else if (STRCMP(info.item_compare_func, "f") == 0)
12191 {
12192 info.item_compare_func = NULL;
12193 info.item_compare_float = TRUE;
12194 }
12195#endif
12196 else if (STRCMP(info.item_compare_func, "i") == 0)
12197 {
12198 info.item_compare_func = NULL;
12199 info.item_compare_ic = TRUE;
12200 }
12201 }
12202 }
12203
12204 if (argvars[2].v_type != VAR_UNKNOWN)
12205 {
12206 /* optional third argument: {dict} */
12207 if (argvars[2].v_type != VAR_DICT)
12208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012209 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012210 goto theend;
12211 }
12212 info.item_compare_selfdict = argvars[2].vval.v_dict;
12213 }
12214 }
12215
12216 /* Make an array with each entry pointing to an item in the List. */
12217 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12218 if (ptrs == NULL)
12219 goto theend;
12220
12221 i = 0;
12222 if (sort)
12223 {
12224 /* sort(): ptrs will be the list to sort */
12225 for (li = l->lv_first; li != NULL; li = li->li_next)
12226 {
12227 ptrs[i].item = li;
12228 ptrs[i].idx = i;
12229 ++i;
12230 }
12231
12232 info.item_compare_func_err = FALSE;
12233 info.item_compare_keep_zero = FALSE;
12234 /* test the compare function */
12235 if ((info.item_compare_func != NULL
12236 || info.item_compare_partial != NULL)
12237 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12238 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012239 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012240 else
12241 {
12242 /* Sort the array with item pointers. */
12243 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12244 info.item_compare_func == NULL
12245 && info.item_compare_partial == NULL
12246 ? item_compare : item_compare2);
12247
12248 if (!info.item_compare_func_err)
12249 {
12250 /* Clear the List and append the items in sorted order. */
12251 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12252 l->lv_len = 0;
12253 for (i = 0; i < len; ++i)
12254 list_append(l, ptrs[i].item);
12255 }
12256 }
12257 }
12258 else
12259 {
12260 int (*item_compare_func_ptr)(const void *, const void *);
12261
12262 /* f_uniq(): ptrs will be a stack of items to remove */
12263 info.item_compare_func_err = FALSE;
12264 info.item_compare_keep_zero = TRUE;
12265 item_compare_func_ptr = info.item_compare_func != NULL
12266 || info.item_compare_partial != NULL
12267 ? item_compare2 : item_compare;
12268
12269 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12270 li = li->li_next)
12271 {
12272 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12273 == 0)
12274 ptrs[i++].item = li;
12275 if (info.item_compare_func_err)
12276 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012277 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012278 break;
12279 }
12280 }
12281
12282 if (!info.item_compare_func_err)
12283 {
12284 while (--i >= 0)
12285 {
12286 li = ptrs[i].item->li_next;
12287 ptrs[i].item->li_next = li->li_next;
12288 if (li->li_next != NULL)
12289 li->li_next->li_prev = ptrs[i].item;
12290 else
12291 l->lv_last = ptrs[i].item;
12292 list_fix_watch(l, li);
12293 listitem_free(li);
12294 l->lv_len--;
12295 }
12296 }
12297 }
12298
12299 vim_free(ptrs);
12300 }
12301theend:
12302 sortinfo = old_sortinfo;
12303}
12304
12305/*
12306 * "sort({list})" function
12307 */
12308 static void
12309f_sort(typval_T *argvars, typval_T *rettv)
12310{
12311 do_sort_uniq(argvars, rettv, TRUE);
12312}
12313
12314/*
12315 * "uniq({list})" function
12316 */
12317 static void
12318f_uniq(typval_T *argvars, typval_T *rettv)
12319{
12320 do_sort_uniq(argvars, rettv, FALSE);
12321}
12322
12323/*
12324 * "soundfold({word})" function
12325 */
12326 static void
12327f_soundfold(typval_T *argvars, typval_T *rettv)
12328{
12329 char_u *s;
12330
12331 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012332 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012333#ifdef FEAT_SPELL
12334 rettv->vval.v_string = eval_soundfold(s);
12335#else
12336 rettv->vval.v_string = vim_strsave(s);
12337#endif
12338}
12339
12340/*
12341 * "spellbadword()" function
12342 */
12343 static void
12344f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12345{
12346 char_u *word = (char_u *)"";
12347 hlf_T attr = HLF_COUNT;
12348 int len = 0;
12349
12350 if (rettv_list_alloc(rettv) == FAIL)
12351 return;
12352
12353#ifdef FEAT_SPELL
12354 if (argvars[0].v_type == VAR_UNKNOWN)
12355 {
12356 /* Find the start and length of the badly spelled word. */
12357 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12358 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012359 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012360 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012361 curwin->w_set_curswant = TRUE;
12362 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 }
12364 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012366 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012367 int capcol = -1;
12368
12369 if (str != NULL)
12370 {
12371 /* Check the argument for spelling. */
12372 while (*str != NUL)
12373 {
12374 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12375 if (attr != HLF_COUNT)
12376 {
12377 word = str;
12378 break;
12379 }
12380 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012381 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012382 }
12383 }
12384 }
12385#endif
12386
12387 list_append_string(rettv->vval.v_list, word, len);
12388 list_append_string(rettv->vval.v_list, (char_u *)(
12389 attr == HLF_SPB ? "bad" :
12390 attr == HLF_SPR ? "rare" :
12391 attr == HLF_SPL ? "local" :
12392 attr == HLF_SPC ? "caps" :
12393 ""), -1);
12394}
12395
12396/*
12397 * "spellsuggest()" function
12398 */
12399 static void
12400f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12401{
12402#ifdef FEAT_SPELL
12403 char_u *str;
12404 int typeerr = FALSE;
12405 int maxcount;
12406 garray_T ga;
12407 int i;
12408 listitem_T *li;
12409 int need_capital = FALSE;
12410#endif
12411
12412 if (rettv_list_alloc(rettv) == FAIL)
12413 return;
12414
12415#ifdef FEAT_SPELL
12416 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12417 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012418 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012419 if (argvars[1].v_type != VAR_UNKNOWN)
12420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012421 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012422 if (maxcount <= 0)
12423 return;
12424 if (argvars[2].v_type != VAR_UNKNOWN)
12425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012426 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427 if (typeerr)
12428 return;
12429 }
12430 }
12431 else
12432 maxcount = 25;
12433
12434 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12435
12436 for (i = 0; i < ga.ga_len; ++i)
12437 {
12438 str = ((char_u **)ga.ga_data)[i];
12439
12440 li = listitem_alloc();
12441 if (li == NULL)
12442 vim_free(str);
12443 else
12444 {
12445 li->li_tv.v_type = VAR_STRING;
12446 li->li_tv.v_lock = 0;
12447 li->li_tv.vval.v_string = str;
12448 list_append(rettv->vval.v_list, li);
12449 }
12450 }
12451 ga_clear(&ga);
12452 }
12453#endif
12454}
12455
12456 static void
12457f_split(typval_T *argvars, typval_T *rettv)
12458{
12459 char_u *str;
12460 char_u *end;
12461 char_u *pat = NULL;
12462 regmatch_T regmatch;
12463 char_u patbuf[NUMBUFLEN];
12464 char_u *save_cpo;
12465 int match;
12466 colnr_T col = 0;
12467 int keepempty = FALSE;
12468 int typeerr = FALSE;
12469
12470 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12471 save_cpo = p_cpo;
12472 p_cpo = (char_u *)"";
12473
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012474 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475 if (argvars[1].v_type != VAR_UNKNOWN)
12476 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012477 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012478 if (pat == NULL)
12479 typeerr = TRUE;
12480 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012481 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012482 }
12483 if (pat == NULL || *pat == NUL)
12484 pat = (char_u *)"[\\x01- ]\\+";
12485
12486 if (rettv_list_alloc(rettv) == FAIL)
12487 return;
12488 if (typeerr)
12489 return;
12490
12491 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12492 if (regmatch.regprog != NULL)
12493 {
12494 regmatch.rm_ic = FALSE;
12495 while (*str != NUL || keepempty)
12496 {
12497 if (*str == NUL)
12498 match = FALSE; /* empty item at the end */
12499 else
12500 match = vim_regexec_nl(&regmatch, str, col);
12501 if (match)
12502 end = regmatch.startp[0];
12503 else
12504 end = str + STRLEN(str);
12505 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12506 && *str != NUL && match && end < regmatch.endp[0]))
12507 {
12508 if (list_append_string(rettv->vval.v_list, str,
12509 (int)(end - str)) == FAIL)
12510 break;
12511 }
12512 if (!match)
12513 break;
12514 /* Advance to just after the match. */
12515 if (regmatch.endp[0] > str)
12516 col = 0;
12517 else
12518 {
12519 /* Don't get stuck at the same match. */
12520#ifdef FEAT_MBYTE
12521 col = (*mb_ptr2len)(regmatch.endp[0]);
12522#else
12523 col = 1;
12524#endif
12525 }
12526 str = regmatch.endp[0];
12527 }
12528
12529 vim_regfree(regmatch.regprog);
12530 }
12531
12532 p_cpo = save_cpo;
12533}
12534
12535#ifdef FEAT_FLOAT
12536/*
12537 * "sqrt()" function
12538 */
12539 static void
12540f_sqrt(typval_T *argvars, typval_T *rettv)
12541{
12542 float_T f = 0.0;
12543
12544 rettv->v_type = VAR_FLOAT;
12545 if (get_float_arg(argvars, &f) == OK)
12546 rettv->vval.v_float = sqrt(f);
12547 else
12548 rettv->vval.v_float = 0.0;
12549}
12550
12551/*
12552 * "str2float()" function
12553 */
12554 static void
12555f_str2float(typval_T *argvars, typval_T *rettv)
12556{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012557 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012558 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012559
Bram Moolenaar08243d22017-01-10 16:12:29 +010012560 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561 p = skipwhite(p + 1);
12562 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012563 if (isneg)
12564 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012565 rettv->v_type = VAR_FLOAT;
12566}
12567#endif
12568
12569/*
12570 * "str2nr()" function
12571 */
12572 static void
12573f_str2nr(typval_T *argvars, typval_T *rettv)
12574{
12575 int base = 10;
12576 char_u *p;
12577 varnumber_T n;
12578 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010012579 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580
12581 if (argvars[1].v_type != VAR_UNKNOWN)
12582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012583 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584 if (base != 2 && base != 8 && base != 10 && base != 16)
12585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012586 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587 return;
12588 }
12589 }
12590
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012591 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010012592 isneg = (*p == '-');
12593 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594 p = skipwhite(p + 1);
12595 switch (base)
12596 {
12597 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
12598 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
12599 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
12600 default: what = 0;
12601 }
12602 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar08243d22017-01-10 16:12:29 +010012603 if (isneg)
12604 rettv->vval.v_number = -n;
12605 else
12606 rettv->vval.v_number = n;
12607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012608}
12609
12610#ifdef HAVE_STRFTIME
12611/*
12612 * "strftime({format}[, {time}])" function
12613 */
12614 static void
12615f_strftime(typval_T *argvars, typval_T *rettv)
12616{
12617 char_u result_buf[256];
12618 struct tm *curtime;
12619 time_t seconds;
12620 char_u *p;
12621
12622 rettv->v_type = VAR_STRING;
12623
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012624 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 if (argvars[1].v_type == VAR_UNKNOWN)
12626 seconds = time(NULL);
12627 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012628 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012629 curtime = localtime(&seconds);
12630 /* MSVC returns NULL for an invalid value of seconds. */
12631 if (curtime == NULL)
12632 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
12633 else
12634 {
12635# ifdef FEAT_MBYTE
12636 vimconv_T conv;
12637 char_u *enc;
12638
12639 conv.vc_type = CONV_NONE;
12640 enc = enc_locale();
12641 convert_setup(&conv, p_enc, enc);
12642 if (conv.vc_type != CONV_NONE)
12643 p = string_convert(&conv, p, NULL);
12644# endif
12645 if (p != NULL)
12646 (void)strftime((char *)result_buf, sizeof(result_buf),
12647 (char *)p, curtime);
12648 else
12649 result_buf[0] = NUL;
12650
12651# ifdef FEAT_MBYTE
12652 if (conv.vc_type != CONV_NONE)
12653 vim_free(p);
12654 convert_setup(&conv, enc, p_enc);
12655 if (conv.vc_type != CONV_NONE)
12656 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
12657 else
12658# endif
12659 rettv->vval.v_string = vim_strsave(result_buf);
12660
12661# ifdef FEAT_MBYTE
12662 /* Release conversion descriptors */
12663 convert_setup(&conv, NULL, NULL);
12664 vim_free(enc);
12665# endif
12666 }
12667}
12668#endif
12669
12670/*
12671 * "strgetchar()" function
12672 */
12673 static void
12674f_strgetchar(typval_T *argvars, typval_T *rettv)
12675{
12676 char_u *str;
12677 int len;
12678 int error = FALSE;
12679 int charidx;
12680
12681 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012682 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012683 if (str == NULL)
12684 return;
12685 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012686 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012687 if (error)
12688 return;
12689#ifdef FEAT_MBYTE
12690 {
12691 int byteidx = 0;
12692
12693 while (charidx >= 0 && byteidx < len)
12694 {
12695 if (charidx == 0)
12696 {
12697 rettv->vval.v_number = mb_ptr2char(str + byteidx);
12698 break;
12699 }
12700 --charidx;
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012701 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012702 }
12703 }
12704#else
12705 if (charidx < len)
12706 rettv->vval.v_number = str[charidx];
12707#endif
12708}
12709
12710/*
12711 * "stridx()" function
12712 */
12713 static void
12714f_stridx(typval_T *argvars, typval_T *rettv)
12715{
12716 char_u buf[NUMBUFLEN];
12717 char_u *needle;
12718 char_u *haystack;
12719 char_u *save_haystack;
12720 char_u *pos;
12721 int start_idx;
12722
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012723 needle = tv_get_string_chk(&argvars[1]);
12724 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012725 rettv->vval.v_number = -1;
12726 if (needle == NULL || haystack == NULL)
12727 return; /* type error; errmsg already given */
12728
12729 if (argvars[2].v_type != VAR_UNKNOWN)
12730 {
12731 int error = FALSE;
12732
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012733 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 if (error || start_idx >= (int)STRLEN(haystack))
12735 return;
12736 if (start_idx >= 0)
12737 haystack += start_idx;
12738 }
12739
12740 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12741 if (pos != NULL)
12742 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
12743}
12744
12745/*
12746 * "string()" function
12747 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010012748 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749f_string(typval_T *argvars, typval_T *rettv)
12750{
12751 char_u *tofree;
12752 char_u numbuf[NUMBUFLEN];
12753
12754 rettv->v_type = VAR_STRING;
12755 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
12756 get_copyID());
12757 /* Make a copy if we have a value but it's not in allocated memory. */
12758 if (rettv->vval.v_string != NULL && tofree == NULL)
12759 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
12760}
12761
12762/*
12763 * "strlen()" function
12764 */
12765 static void
12766f_strlen(typval_T *argvars, typval_T *rettv)
12767{
12768 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012769 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770}
12771
12772/*
12773 * "strchars()" function
12774 */
12775 static void
12776f_strchars(typval_T *argvars, typval_T *rettv)
12777{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012778 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012779 int skipcc = 0;
12780#ifdef FEAT_MBYTE
12781 varnumber_T len = 0;
12782 int (*func_mb_ptr2char_adv)(char_u **pp);
12783#endif
12784
12785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012786 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012787 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012788 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012789 else
12790 {
12791#ifdef FEAT_MBYTE
12792 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
12793 while (*s != NUL)
12794 {
12795 func_mb_ptr2char_adv(&s);
12796 ++len;
12797 }
12798 rettv->vval.v_number = len;
12799#else
12800 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
12801#endif
12802 }
12803}
12804
12805/*
12806 * "strdisplaywidth()" function
12807 */
12808 static void
12809f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
12810{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012811 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012812 int col = 0;
12813
12814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012815 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816
12817 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
12818}
12819
12820/*
12821 * "strwidth()" function
12822 */
12823 static void
12824f_strwidth(typval_T *argvars, typval_T *rettv)
12825{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012826 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012827
12828 rettv->vval.v_number = (varnumber_T)(
12829#ifdef FEAT_MBYTE
12830 mb_string2cells(s, -1)
12831#else
12832 STRLEN(s)
12833#endif
12834 );
12835}
12836
12837/*
12838 * "strcharpart()" function
12839 */
12840 static void
12841f_strcharpart(typval_T *argvars, typval_T *rettv)
12842{
12843#ifdef FEAT_MBYTE
12844 char_u *p;
12845 int nchar;
12846 int nbyte = 0;
12847 int charlen;
12848 int len = 0;
12849 int slen;
12850 int error = FALSE;
12851
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012852 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012853 slen = (int)STRLEN(p);
12854
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012855 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856 if (!error)
12857 {
12858 if (nchar > 0)
12859 while (nchar > 0 && nbyte < slen)
12860 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012861 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012862 --nchar;
12863 }
12864 else
12865 nbyte = nchar;
12866 if (argvars[2].v_type != VAR_UNKNOWN)
12867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012868 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012869 while (charlen > 0 && nbyte + len < slen)
12870 {
12871 int off = nbyte + len;
12872
12873 if (off < 0)
12874 len += 1;
12875 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020012876 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 --charlen;
12878 }
12879 }
12880 else
12881 len = slen - nbyte; /* default: all bytes that are available. */
12882 }
12883
12884 /*
12885 * Only return the overlap between the specified part and the actual
12886 * string.
12887 */
12888 if (nbyte < 0)
12889 {
12890 len += nbyte;
12891 nbyte = 0;
12892 }
12893 else if (nbyte > slen)
12894 nbyte = slen;
12895 if (len < 0)
12896 len = 0;
12897 else if (nbyte + len > slen)
12898 len = slen - nbyte;
12899
12900 rettv->v_type = VAR_STRING;
12901 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
12902#else
12903 f_strpart(argvars, rettv);
12904#endif
12905}
12906
12907/*
12908 * "strpart()" function
12909 */
12910 static void
12911f_strpart(typval_T *argvars, typval_T *rettv)
12912{
12913 char_u *p;
12914 int n;
12915 int len;
12916 int slen;
12917 int error = FALSE;
12918
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012919 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012920 slen = (int)STRLEN(p);
12921
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012922 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012923 if (error)
12924 len = 0;
12925 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012926 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012927 else
12928 len = slen - n; /* default len: all bytes that are available. */
12929
12930 /*
12931 * Only return the overlap between the specified part and the actual
12932 * string.
12933 */
12934 if (n < 0)
12935 {
12936 len += n;
12937 n = 0;
12938 }
12939 else if (n > slen)
12940 n = slen;
12941 if (len < 0)
12942 len = 0;
12943 else if (n + len > slen)
12944 len = slen - n;
12945
12946 rettv->v_type = VAR_STRING;
12947 rettv->vval.v_string = vim_strnsave(p + n, len);
12948}
12949
12950/*
12951 * "strridx()" function
12952 */
12953 static void
12954f_strridx(typval_T *argvars, typval_T *rettv)
12955{
12956 char_u buf[NUMBUFLEN];
12957 char_u *needle;
12958 char_u *haystack;
12959 char_u *rest;
12960 char_u *lastmatch = NULL;
12961 int haystack_len, end_idx;
12962
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012963 needle = tv_get_string_chk(&argvars[1]);
12964 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012965
12966 rettv->vval.v_number = -1;
12967 if (needle == NULL || haystack == NULL)
12968 return; /* type error; errmsg already given */
12969
12970 haystack_len = (int)STRLEN(haystack);
12971 if (argvars[2].v_type != VAR_UNKNOWN)
12972 {
12973 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012974 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012975 if (end_idx < 0)
12976 return; /* can never find a match */
12977 }
12978 else
12979 end_idx = haystack_len;
12980
12981 if (*needle == NUL)
12982 {
12983 /* Empty string matches past the end. */
12984 lastmatch = haystack + end_idx;
12985 }
12986 else
12987 {
12988 for (rest = haystack; *rest != '\0'; ++rest)
12989 {
12990 rest = (char_u *)strstr((char *)rest, (char *)needle);
12991 if (rest == NULL || rest > haystack + end_idx)
12992 break;
12993 lastmatch = rest;
12994 }
12995 }
12996
12997 if (lastmatch == NULL)
12998 rettv->vval.v_number = -1;
12999 else
13000 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13001}
13002
13003/*
13004 * "strtrans()" function
13005 */
13006 static void
13007f_strtrans(typval_T *argvars, typval_T *rettv)
13008{
13009 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011}
13012
13013/*
13014 * "submatch()" function
13015 */
13016 static void
13017f_submatch(typval_T *argvars, typval_T *rettv)
13018{
13019 int error = FALSE;
13020 int no;
13021 int retList = 0;
13022
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013023 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013024 if (error)
13025 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013026 if (no < 0 || no >= NSUBEXP)
13027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013028 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013029 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013030 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013031 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013032 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013033 if (error)
13034 return;
13035
13036 if (retList == 0)
13037 {
13038 rettv->v_type = VAR_STRING;
13039 rettv->vval.v_string = reg_submatch(no);
13040 }
13041 else
13042 {
13043 rettv->v_type = VAR_LIST;
13044 rettv->vval.v_list = reg_submatch_list(no);
13045 }
13046}
13047
13048/*
13049 * "substitute()" function
13050 */
13051 static void
13052f_substitute(typval_T *argvars, typval_T *rettv)
13053{
13054 char_u patbuf[NUMBUFLEN];
13055 char_u subbuf[NUMBUFLEN];
13056 char_u flagsbuf[NUMBUFLEN];
13057
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013058 char_u *str = tv_get_string_chk(&argvars[0]);
13059 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013060 char_u *sub = NULL;
13061 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013062 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013064 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13065 expr = &argvars[2];
13066 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013067 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013069 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013070 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13071 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 rettv->vval.v_string = NULL;
13073 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013074 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075}
13076
13077/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013078 * "swapinfo(swap_filename)" function
13079 */
13080 static void
13081f_swapinfo(typval_T *argvars, typval_T *rettv)
13082{
13083 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013084 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013085}
13086
13087/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013088 * "swapname(expr)" function
13089 */
13090 static void
13091f_swapname(typval_T *argvars, typval_T *rettv)
13092{
13093 buf_T *buf;
13094
13095 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013096 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013097 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13098 || buf->b_ml.ml_mfp->mf_fname == NULL)
13099 rettv->vval.v_string = NULL;
13100 else
13101 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13102}
13103
13104/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 * "synID(lnum, col, trans)" function
13106 */
13107 static void
13108f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13109{
13110 int id = 0;
13111#ifdef FEAT_SYN_HL
13112 linenr_T lnum;
13113 colnr_T col;
13114 int trans;
13115 int transerr = FALSE;
13116
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013117 lnum = tv_get_lnum(argvars); /* -1 on type error */
13118 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13119 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013120
13121 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13122 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13123 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13124#endif
13125
13126 rettv->vval.v_number = id;
13127}
13128
13129/*
13130 * "synIDattr(id, what [, mode])" function
13131 */
13132 static void
13133f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13134{
13135 char_u *p = NULL;
13136#ifdef FEAT_SYN_HL
13137 int id;
13138 char_u *what;
13139 char_u *mode;
13140 char_u modebuf[NUMBUFLEN];
13141 int modec;
13142
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013143 id = (int)tv_get_number(&argvars[0]);
13144 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 if (argvars[2].v_type != VAR_UNKNOWN)
13146 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013147 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013148 modec = TOLOWER_ASC(mode[0]);
13149 if (modec != 't' && modec != 'c' && modec != 'g')
13150 modec = 0; /* replace invalid with current */
13151 }
13152 else
13153 {
13154#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13155 if (USE_24BIT)
13156 modec = 'g';
13157 else
13158#endif
13159 if (t_colors > 1)
13160 modec = 'c';
13161 else
13162 modec = 't';
13163 }
13164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165 switch (TOLOWER_ASC(what[0]))
13166 {
13167 case 'b':
13168 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13169 p = highlight_color(id, what, modec);
13170 else /* bold */
13171 p = highlight_has_attr(id, HL_BOLD, modec);
13172 break;
13173
13174 case 'f': /* fg[#] or font */
13175 p = highlight_color(id, what, modec);
13176 break;
13177
13178 case 'i':
13179 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13180 p = highlight_has_attr(id, HL_INVERSE, modec);
13181 else /* italic */
13182 p = highlight_has_attr(id, HL_ITALIC, modec);
13183 break;
13184
13185 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013186 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 break;
13188
13189 case 'r': /* reverse */
13190 p = highlight_has_attr(id, HL_INVERSE, modec);
13191 break;
13192
13193 case 's':
13194 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13195 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013196 /* strikeout */
13197 else if (TOLOWER_ASC(what[1]) == 't' &&
13198 TOLOWER_ASC(what[2]) == 'r')
13199 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 else /* standout */
13201 p = highlight_has_attr(id, HL_STANDOUT, modec);
13202 break;
13203
13204 case 'u':
13205 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13206 /* underline */
13207 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13208 else
13209 /* undercurl */
13210 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13211 break;
13212 }
13213
13214 if (p != NULL)
13215 p = vim_strsave(p);
13216#endif
13217 rettv->v_type = VAR_STRING;
13218 rettv->vval.v_string = p;
13219}
13220
13221/*
13222 * "synIDtrans(id)" function
13223 */
13224 static void
13225f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13226{
13227 int id;
13228
13229#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013230 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231
13232 if (id > 0)
13233 id = syn_get_final_id(id);
13234 else
13235#endif
13236 id = 0;
13237
13238 rettv->vval.v_number = id;
13239}
13240
13241/*
13242 * "synconcealed(lnum, col)" function
13243 */
13244 static void
13245f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13246{
13247#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13248 linenr_T lnum;
13249 colnr_T col;
13250 int syntax_flags = 0;
13251 int cchar;
13252 int matchid = 0;
13253 char_u str[NUMBUFLEN];
13254#endif
13255
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013256 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257
13258#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013259 lnum = tv_get_lnum(argvars); /* -1 on type error */
13260 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261
13262 vim_memset(str, NUL, sizeof(str));
13263
13264 if (rettv_list_alloc(rettv) != FAIL)
13265 {
13266 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13267 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13268 && curwin->w_p_cole > 0)
13269 {
13270 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13271 syntax_flags = get_syntax_info(&matchid);
13272
13273 /* get the conceal character */
13274 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13275 {
13276 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013277 if (cchar == NUL && curwin->w_p_cole == 1)
13278 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013279 if (cchar != NUL)
13280 {
13281# ifdef FEAT_MBYTE
13282 if (has_mbyte)
13283 (*mb_char2bytes)(cchar, str);
13284 else
13285# endif
13286 str[0] = cchar;
13287 }
13288 }
13289 }
13290
13291 list_append_number(rettv->vval.v_list,
13292 (syntax_flags & HL_CONCEAL) != 0);
13293 /* -1 to auto-determine strlen */
13294 list_append_string(rettv->vval.v_list, str, -1);
13295 list_append_number(rettv->vval.v_list, matchid);
13296 }
13297#endif
13298}
13299
13300/*
13301 * "synstack(lnum, col)" function
13302 */
13303 static void
13304f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13305{
13306#ifdef FEAT_SYN_HL
13307 linenr_T lnum;
13308 colnr_T col;
13309 int i;
13310 int id;
13311#endif
13312
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013313 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314
13315#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013316 lnum = tv_get_lnum(argvars); /* -1 on type error */
13317 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013318
13319 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13320 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13321 && rettv_list_alloc(rettv) != FAIL)
13322 {
13323 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13324 for (i = 0; ; ++i)
13325 {
13326 id = syn_get_stack_item(i);
13327 if (id < 0)
13328 break;
13329 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13330 break;
13331 }
13332 }
13333#endif
13334}
13335
13336 static void
13337get_cmd_output_as_rettv(
13338 typval_T *argvars,
13339 typval_T *rettv,
13340 int retlist)
13341{
13342 char_u *res = NULL;
13343 char_u *p;
13344 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 int err = FALSE;
13346 FILE *fd;
13347 list_T *list = NULL;
13348 int flags = SHELL_SILENT;
13349
13350 rettv->v_type = VAR_STRING;
13351 rettv->vval.v_string = NULL;
13352 if (check_restricted() || check_secure())
13353 goto errret;
13354
13355 if (argvars[1].v_type != VAR_UNKNOWN)
13356 {
13357 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013358 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013359 * command.
13360 */
13361 if ((infile = vim_tempname('i', TRUE)) == NULL)
13362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013363 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364 goto errret;
13365 }
13366
13367 fd = mch_fopen((char *)infile, WRITEBIN);
13368 if (fd == NULL)
13369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013370 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013371 goto errret;
13372 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013373 if (argvars[1].v_type == VAR_NUMBER)
13374 {
13375 linenr_T lnum;
13376 buf_T *buf;
13377
13378 buf = buflist_findnr(argvars[1].vval.v_number);
13379 if (buf == NULL)
13380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013381 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013382 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013383 goto errret;
13384 }
13385
13386 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13387 {
13388 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13389 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13390 {
13391 err = TRUE;
13392 break;
13393 }
13394 if (putc(NL, fd) == EOF)
13395 {
13396 err = TRUE;
13397 break;
13398 }
13399 }
13400 }
13401 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013402 {
13403 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13404 err = TRUE;
13405 }
13406 else
13407 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013408 size_t len;
13409 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013411 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013412 if (p == NULL)
13413 {
13414 fclose(fd);
13415 goto errret; /* type error; errmsg already given */
13416 }
13417 len = STRLEN(p);
13418 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13419 err = TRUE;
13420 }
13421 if (fclose(fd) != 0)
13422 err = TRUE;
13423 if (err)
13424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013425 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013426 goto errret;
13427 }
13428 }
13429
13430 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13431 * echoes typeahead, that messes up the display. */
13432 if (!msg_silent)
13433 flags += SHELL_COOKED;
13434
13435 if (retlist)
13436 {
13437 int len;
13438 listitem_T *li;
13439 char_u *s = NULL;
13440 char_u *start;
13441 char_u *end;
13442 int i;
13443
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013444 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013445 if (res == NULL)
13446 goto errret;
13447
13448 list = list_alloc();
13449 if (list == NULL)
13450 goto errret;
13451
13452 for (i = 0; i < len; ++i)
13453 {
13454 start = res + i;
13455 while (i < len && res[i] != NL)
13456 ++i;
13457 end = res + i;
13458
13459 s = alloc((unsigned)(end - start + 1));
13460 if (s == NULL)
13461 goto errret;
13462
13463 for (p = s; start < end; ++p, ++start)
13464 *p = *start == NUL ? NL : *start;
13465 *p = NUL;
13466
13467 li = listitem_alloc();
13468 if (li == NULL)
13469 {
13470 vim_free(s);
13471 goto errret;
13472 }
13473 li->li_tv.v_type = VAR_STRING;
13474 li->li_tv.v_lock = 0;
13475 li->li_tv.vval.v_string = s;
13476 list_append(list, li);
13477 }
13478
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013479 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013480 list = NULL;
13481 }
13482 else
13483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013484 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013485#ifdef USE_CR
13486 /* translate <CR> into <NL> */
13487 if (res != NULL)
13488 {
13489 char_u *s;
13490
13491 for (s = res; *s; ++s)
13492 {
13493 if (*s == CAR)
13494 *s = NL;
13495 }
13496 }
13497#else
13498# ifdef USE_CRNL
13499 /* translate <CR><NL> into <NL> */
13500 if (res != NULL)
13501 {
13502 char_u *s, *d;
13503
13504 d = res;
13505 for (s = res; *s; ++s)
13506 {
13507 if (s[0] == CAR && s[1] == NL)
13508 ++s;
13509 *d++ = *s;
13510 }
13511 *d = NUL;
13512 }
13513# endif
13514#endif
13515 rettv->vval.v_string = res;
13516 res = NULL;
13517 }
13518
13519errret:
13520 if (infile != NULL)
13521 {
13522 mch_remove(infile);
13523 vim_free(infile);
13524 }
13525 if (res != NULL)
13526 vim_free(res);
13527 if (list != NULL)
13528 list_free(list);
13529}
13530
13531/*
13532 * "system()" function
13533 */
13534 static void
13535f_system(typval_T *argvars, typval_T *rettv)
13536{
13537 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13538}
13539
13540/*
13541 * "systemlist()" function
13542 */
13543 static void
13544f_systemlist(typval_T *argvars, typval_T *rettv)
13545{
13546 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13547}
13548
13549/*
13550 * "tabpagebuflist()" function
13551 */
13552 static void
13553f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13554{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013555 tabpage_T *tp;
13556 win_T *wp = NULL;
13557
13558 if (argvars[0].v_type == VAR_UNKNOWN)
13559 wp = firstwin;
13560 else
13561 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013562 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013563 if (tp != NULL)
13564 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13565 }
13566 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
13567 {
13568 for (; wp != NULL; wp = wp->w_next)
13569 if (list_append_number(rettv->vval.v_list,
13570 wp->w_buffer->b_fnum) == FAIL)
13571 break;
13572 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013573}
13574
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013575/*
13576 * "tabpagenr()" function
13577 */
13578 static void
13579f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
13580{
13581 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013582 char_u *arg;
13583
13584 if (argvars[0].v_type != VAR_UNKNOWN)
13585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013586 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587 nr = 0;
13588 if (arg != NULL)
13589 {
13590 if (STRCMP(arg, "$") == 0)
13591 nr = tabpage_index(NULL) - 1;
13592 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013593 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013594 }
13595 }
13596 else
13597 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013598 rettv->vval.v_number = nr;
13599}
13600
13601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013602/*
13603 * Common code for tabpagewinnr() and winnr().
13604 */
13605 static int
13606get_winnr(tabpage_T *tp, typval_T *argvar)
13607{
13608 win_T *twin;
13609 int nr = 1;
13610 win_T *wp;
13611 char_u *arg;
13612
13613 twin = (tp == curtab) ? curwin : tp->tp_curwin;
13614 if (argvar->v_type != VAR_UNKNOWN)
13615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013616 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 if (arg == NULL)
13618 nr = 0; /* type error; errmsg already given */
13619 else if (STRCMP(arg, "$") == 0)
13620 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
13621 else if (STRCMP(arg, "#") == 0)
13622 {
13623 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
13624 if (twin == NULL)
13625 nr = 0;
13626 }
13627 else
13628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013629 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013630 nr = 0;
13631 }
13632 }
13633
13634 if (nr > 0)
13635 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
13636 wp != twin; wp = wp->w_next)
13637 {
13638 if (wp == NULL)
13639 {
13640 /* didn't find it in this tabpage */
13641 nr = 0;
13642 break;
13643 }
13644 ++nr;
13645 }
13646 return nr;
13647}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013648
13649/*
13650 * "tabpagewinnr()" function
13651 */
13652 static void
13653f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
13654{
13655 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013656 tabpage_T *tp;
13657
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013658 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013659 if (tp == NULL)
13660 nr = 0;
13661 else
13662 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013663 rettv->vval.v_number = nr;
13664}
13665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013666/*
13667 * "tagfiles()" function
13668 */
13669 static void
13670f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
13671{
13672 char_u *fname;
13673 tagname_T tn;
13674 int first;
13675
13676 if (rettv_list_alloc(rettv) == FAIL)
13677 return;
13678 fname = alloc(MAXPATHL);
13679 if (fname == NULL)
13680 return;
13681
13682 for (first = TRUE; ; first = FALSE)
13683 if (get_tagfname(&tn, first, fname) == FAIL
13684 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
13685 break;
13686 tagname_free(&tn);
13687 vim_free(fname);
13688}
13689
13690/*
13691 * "taglist()" function
13692 */
13693 static void
13694f_taglist(typval_T *argvars, typval_T *rettv)
13695{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013696 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697 char_u *tag_pattern;
13698
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013699 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013700
13701 rettv->vval.v_number = FALSE;
13702 if (*tag_pattern == NUL)
13703 return;
13704
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013705 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013706 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013707 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010013708 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013709}
13710
13711/*
13712 * "tempname()" function
13713 */
13714 static void
13715f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
13716{
13717 static int x = 'A';
13718
13719 rettv->v_type = VAR_STRING;
13720 rettv->vval.v_string = vim_tempname(x, FALSE);
13721
13722 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
13723 * names. Skip 'I' and 'O', they are used for shell redirection. */
13724 do
13725 {
13726 if (x == 'Z')
13727 x = '0';
13728 else if (x == '9')
13729 x = 'A';
13730 else
13731 {
13732#ifdef EBCDIC
13733 if (x == 'I')
13734 x = 'J';
13735 else if (x == 'R')
13736 x = 'S';
13737 else
13738#endif
13739 ++x;
13740 }
13741 } while (x == 'I' || x == 'O');
13742}
13743
13744#ifdef FEAT_FLOAT
13745/*
13746 * "tan()" function
13747 */
13748 static void
13749f_tan(typval_T *argvars, typval_T *rettv)
13750{
13751 float_T f = 0.0;
13752
13753 rettv->v_type = VAR_FLOAT;
13754 if (get_float_arg(argvars, &f) == OK)
13755 rettv->vval.v_float = tan(f);
13756 else
13757 rettv->vval.v_float = 0.0;
13758}
13759
13760/*
13761 * "tanh()" function
13762 */
13763 static void
13764f_tanh(typval_T *argvars, typval_T *rettv)
13765{
13766 float_T f = 0.0;
13767
13768 rettv->v_type = VAR_FLOAT;
13769 if (get_float_arg(argvars, &f) == OK)
13770 rettv->vval.v_float = tanh(f);
13771 else
13772 rettv->vval.v_float = 0.0;
13773}
13774#endif
13775
13776/*
13777 * "test_alloc_fail(id, countdown, repeat)" function
13778 */
13779 static void
13780f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
13781{
13782 if (argvars[0].v_type != VAR_NUMBER
13783 || argvars[0].vval.v_number <= 0
13784 || argvars[1].v_type != VAR_NUMBER
13785 || argvars[1].vval.v_number < 0
13786 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013787 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013788 else
13789 {
13790 alloc_fail_id = argvars[0].vval.v_number;
13791 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013792 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013793 alloc_fail_countdown = argvars[1].vval.v_number;
13794 alloc_fail_repeat = argvars[2].vval.v_number;
13795 did_outofmem_msg = FALSE;
13796 }
13797}
13798
13799/*
13800 * "test_autochdir()"
13801 */
13802 static void
13803f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13804{
13805#if defined(FEAT_AUTOCHDIR)
13806 test_autochdir = TRUE;
13807#endif
13808}
13809
13810/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013811 * "test_feedinput()"
13812 */
13813 static void
13814f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
13815{
13816#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013817 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020013818
13819 if (val != NULL)
13820 {
13821 trash_input_buf();
13822 add_to_input_buf_csi(val, (int)STRLEN(val));
13823 }
13824#endif
13825}
13826
13827/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013828 * "test_option_not_set({name})" function
13829 */
13830 static void
13831f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
13832{
13833 char_u *name = (char_u *)"";
13834
13835 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013836 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013837 else
13838 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013839 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013840 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013841 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020013842 }
13843}
13844
13845/*
13846 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013847 */
13848 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013849f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013850{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013851 char_u *name = (char_u *)"";
13852 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013853 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013854
13855 if (argvars[0].v_type != VAR_STRING
13856 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013857 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013858 else
13859 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010013860 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013861 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013862
13863 if (STRCMP(name, (char_u *)"redraw") == 0)
13864 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013865 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
13866 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013867 else if (STRCMP(name, (char_u *)"char_avail") == 0)
13868 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013869 else if (STRCMP(name, (char_u *)"starting") == 0)
13870 {
13871 if (val)
13872 {
13873 if (save_starting < 0)
13874 save_starting = starting;
13875 starting = 0;
13876 }
13877 else
13878 {
13879 starting = save_starting;
13880 save_starting = -1;
13881 }
13882 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013883 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
13884 nfa_fail_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013885 else if (STRCMP(name, (char_u *)"ALL") == 0)
13886 {
13887 disable_char_avail_for_testing = FALSE;
13888 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020013889 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020013890 nfa_fail_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020013891 if (save_starting >= 0)
13892 {
13893 starting = save_starting;
13894 save_starting = -1;
13895 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013896 }
13897 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013898 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010013899 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013900}
13901
13902/*
13903 * "test_garbagecollect_now()" function
13904 */
13905 static void
13906f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
13907{
13908 /* This is dangerous, any Lists and Dicts used internally may be freed
13909 * while still in use. */
13910 garbage_collect(TRUE);
13911}
13912
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013913/*
13914 * "test_ignore_error()" function
13915 */
13916 static void
13917f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
13918{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013919 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010013920}
13921
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010013922 static void
13923f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
13924{
13925 rettv->v_type = VAR_BLOB;
13926 rettv->vval.v_blob = NULL;
13927}
13928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013929#ifdef FEAT_JOB_CHANNEL
13930 static void
13931f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
13932{
13933 rettv->v_type = VAR_CHANNEL;
13934 rettv->vval.v_channel = NULL;
13935}
13936#endif
13937
13938 static void
13939f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
13940{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013941 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013942}
13943
13944#ifdef FEAT_JOB_CHANNEL
13945 static void
13946f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
13947{
13948 rettv->v_type = VAR_JOB;
13949 rettv->vval.v_job = NULL;
13950}
13951#endif
13952
13953 static void
13954f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
13955{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013956 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957}
13958
13959 static void
13960f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
13961{
13962 rettv->v_type = VAR_PARTIAL;
13963 rettv->vval.v_partial = NULL;
13964}
13965
13966 static void
13967f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
13968{
13969 rettv->v_type = VAR_STRING;
13970 rettv->vval.v_string = NULL;
13971}
13972
Bram Moolenaarab186732018-09-14 21:27:06 +020013973#ifdef FEAT_GUI
13974 static void
13975f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
13976{
13977 char_u *which;
13978 long value;
13979 int dragging;
13980 scrollbar_T *sb = NULL;
13981
13982 if (argvars[0].v_type != VAR_STRING
13983 || (argvars[1].v_type) != VAR_NUMBER
13984 || (argvars[2].v_type) != VAR_NUMBER)
13985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013986 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020013987 return;
13988 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013989 which = tv_get_string(&argvars[0]);
13990 value = tv_get_number(&argvars[1]);
13991 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020013992
13993 if (STRCMP(which, "left") == 0)
13994 sb = &curwin->w_scrollbars[SBAR_LEFT];
13995 else if (STRCMP(which, "right") == 0)
13996 sb = &curwin->w_scrollbars[SBAR_RIGHT];
13997 else if (STRCMP(which, "hor") == 0)
13998 sb = &gui.bottom_sbar;
13999 if (sb == NULL)
14000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014001 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014002 return;
14003 }
14004 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014005# ifndef USE_ON_FLY_SCROLL
14006 // need to loop through normal_cmd() to handle the scroll events
14007 exec_normal(FALSE, TRUE, FALSE);
14008# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014009}
14010#endif
14011
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014012 static void
14013f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14014{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014015 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014016}
14017
14018#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
14019/*
14020 * Get a callback from "arg". It can be a Funcref or a function name.
14021 * When "arg" is zero return an empty string.
14022 * Return NULL for an invalid argument.
14023 */
14024 char_u *
14025get_callback(typval_T *arg, partial_T **pp)
14026{
14027 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14028 {
14029 *pp = arg->vval.v_partial;
14030 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014031 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 }
14033 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014034 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014035 {
14036 func_ref(arg->vval.v_string);
14037 return arg->vval.v_string;
14038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14040 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014041 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014042 return NULL;
14043}
14044
14045/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014046 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014047 */
14048 void
14049free_callback(char_u *callback, partial_T *partial)
14050{
14051 if (partial != NULL)
14052 partial_unref(partial);
14053 else if (callback != NULL)
14054 {
14055 func_unref(callback);
14056 vim_free(callback);
14057 }
14058}
14059#endif
14060
14061#ifdef FEAT_TIMERS
14062/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014063 * "timer_info([timer])" function
14064 */
14065 static void
14066f_timer_info(typval_T *argvars, typval_T *rettv)
14067{
14068 timer_T *timer = NULL;
14069
14070 if (rettv_list_alloc(rettv) != OK)
14071 return;
14072 if (argvars[0].v_type != VAR_UNKNOWN)
14073 {
14074 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014075 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014076 else
14077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014078 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014079 if (timer != NULL)
14080 add_timer_info(rettv, timer);
14081 }
14082 }
14083 else
14084 add_timer_info_all(rettv);
14085}
14086
14087/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014088 * "timer_pause(timer, paused)" function
14089 */
14090 static void
14091f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14092{
14093 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014094 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014095
14096 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014097 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014098 else
14099 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014100 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014101 if (timer != NULL)
14102 timer->tr_paused = paused;
14103 }
14104}
14105
14106/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014107 * "timer_start(time, callback [, options])" function
14108 */
14109 static void
14110f_timer_start(typval_T *argvars, typval_T *rettv)
14111{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014112 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014113 timer_T *timer;
14114 int repeat = 0;
14115 char_u *callback;
14116 dict_T *dict;
14117 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014118
Bram Moolenaar75537a92016-09-05 22:45:28 +020014119 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120 if (check_secure())
14121 return;
14122 if (argvars[2].v_type != VAR_UNKNOWN)
14123 {
14124 if (argvars[2].v_type != VAR_DICT
14125 || (dict = argvars[2].vval.v_dict) == NULL)
14126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014127 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014128 return;
14129 }
14130 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014131 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014132 }
14133
Bram Moolenaar75537a92016-09-05 22:45:28 +020014134 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014136 return;
14137
14138 timer = create_timer(msec, repeat);
14139 if (timer == NULL)
14140 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 else
14142 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014143 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014144 timer->tr_callback = vim_strsave(callback);
14145 else
14146 /* pointer into the partial */
14147 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014148 timer->tr_partial = partial;
14149 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150 }
14151}
14152
14153/*
14154 * "timer_stop(timer)" function
14155 */
14156 static void
14157f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14158{
14159 timer_T *timer;
14160
14161 if (argvars[0].v_type != VAR_NUMBER)
14162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014163 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014164 return;
14165 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014166 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014167 if (timer != NULL)
14168 stop_timer(timer);
14169}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014170
14171/*
14172 * "timer_stopall()" function
14173 */
14174 static void
14175f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14176{
14177 stop_all_timers();
14178}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179#endif
14180
14181/*
14182 * "tolower(string)" function
14183 */
14184 static void
14185f_tolower(typval_T *argvars, typval_T *rettv)
14186{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014187 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014188 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014189}
14190
14191/*
14192 * "toupper(string)" function
14193 */
14194 static void
14195f_toupper(typval_T *argvars, typval_T *rettv)
14196{
14197 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014198 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014199}
14200
14201/*
14202 * "tr(string, fromstr, tostr)" function
14203 */
14204 static void
14205f_tr(typval_T *argvars, typval_T *rettv)
14206{
14207 char_u *in_str;
14208 char_u *fromstr;
14209 char_u *tostr;
14210 char_u *p;
14211#ifdef FEAT_MBYTE
14212 int inlen;
14213 int fromlen;
14214 int tolen;
14215 int idx;
14216 char_u *cpstr;
14217 int cplen;
14218 int first = TRUE;
14219#endif
14220 char_u buf[NUMBUFLEN];
14221 char_u buf2[NUMBUFLEN];
14222 garray_T ga;
14223
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014224 in_str = tv_get_string(&argvars[0]);
14225 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14226 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014227
14228 /* Default return value: empty string. */
14229 rettv->v_type = VAR_STRING;
14230 rettv->vval.v_string = NULL;
14231 if (fromstr == NULL || tostr == NULL)
14232 return; /* type error; errmsg already given */
14233 ga_init2(&ga, (int)sizeof(char), 80);
14234
14235#ifdef FEAT_MBYTE
14236 if (!has_mbyte)
14237#endif
14238 /* not multi-byte: fromstr and tostr must be the same length */
14239 if (STRLEN(fromstr) != STRLEN(tostr))
14240 {
14241#ifdef FEAT_MBYTE
14242error:
14243#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014244 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014245 ga_clear(&ga);
14246 return;
14247 }
14248
14249 /* fromstr and tostr have to contain the same number of chars */
14250 while (*in_str != NUL)
14251 {
14252#ifdef FEAT_MBYTE
14253 if (has_mbyte)
14254 {
14255 inlen = (*mb_ptr2len)(in_str);
14256 cpstr = in_str;
14257 cplen = inlen;
14258 idx = 0;
14259 for (p = fromstr; *p != NUL; p += fromlen)
14260 {
14261 fromlen = (*mb_ptr2len)(p);
14262 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14263 {
14264 for (p = tostr; *p != NUL; p += tolen)
14265 {
14266 tolen = (*mb_ptr2len)(p);
14267 if (idx-- == 0)
14268 {
14269 cplen = tolen;
14270 cpstr = p;
14271 break;
14272 }
14273 }
14274 if (*p == NUL) /* tostr is shorter than fromstr */
14275 goto error;
14276 break;
14277 }
14278 ++idx;
14279 }
14280
14281 if (first && cpstr == in_str)
14282 {
14283 /* Check that fromstr and tostr have the same number of
14284 * (multi-byte) characters. Done only once when a character
14285 * of in_str doesn't appear in fromstr. */
14286 first = FALSE;
14287 for (p = tostr; *p != NUL; p += tolen)
14288 {
14289 tolen = (*mb_ptr2len)(p);
14290 --idx;
14291 }
14292 if (idx != 0)
14293 goto error;
14294 }
14295
14296 (void)ga_grow(&ga, cplen);
14297 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14298 ga.ga_len += cplen;
14299
14300 in_str += inlen;
14301 }
14302 else
14303#endif
14304 {
14305 /* When not using multi-byte chars we can do it faster. */
14306 p = vim_strchr(fromstr, *in_str);
14307 if (p != NULL)
14308 ga_append(&ga, tostr[p - fromstr]);
14309 else
14310 ga_append(&ga, *in_str);
14311 ++in_str;
14312 }
14313 }
14314
14315 /* add a terminating NUL */
14316 (void)ga_grow(&ga, 1);
14317 ga_append(&ga, NUL);
14318
14319 rettv->vval.v_string = ga.ga_data;
14320}
14321
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014322/*
14323 * "trim({expr})" function
14324 */
14325 static void
14326f_trim(typval_T *argvars, typval_T *rettv)
14327{
14328 char_u buf1[NUMBUFLEN];
14329 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014330 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014331 char_u *mask = NULL;
14332 char_u *tail;
14333 char_u *prev;
14334 char_u *p;
14335 int c1;
14336
14337 rettv->v_type = VAR_STRING;
14338 if (head == NULL)
14339 {
14340 rettv->vval.v_string = NULL;
14341 return;
14342 }
14343
14344 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014345 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014346
14347 while (*head != NUL)
14348 {
14349 c1 = PTR2CHAR(head);
14350 if (mask == NULL)
14351 {
14352 if (c1 > ' ' && c1 != 0xa0)
14353 break;
14354 }
14355 else
14356 {
14357 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14358 if (c1 == PTR2CHAR(p))
14359 break;
14360 if (*p == NUL)
14361 break;
14362 }
14363 MB_PTR_ADV(head);
14364 }
14365
14366 for (tail = head + STRLEN(head); tail > head; tail = prev)
14367 {
14368 prev = tail;
14369 MB_PTR_BACK(head, prev);
14370 c1 = PTR2CHAR(prev);
14371 if (mask == NULL)
14372 {
14373 if (c1 > ' ' && c1 != 0xa0)
14374 break;
14375 }
14376 else
14377 {
14378 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14379 if (c1 == PTR2CHAR(p))
14380 break;
14381 if (*p == NUL)
14382 break;
14383 }
14384 }
14385 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14386}
14387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014388#ifdef FEAT_FLOAT
14389/*
14390 * "trunc({float})" function
14391 */
14392 static void
14393f_trunc(typval_T *argvars, typval_T *rettv)
14394{
14395 float_T f = 0.0;
14396
14397 rettv->v_type = VAR_FLOAT;
14398 if (get_float_arg(argvars, &f) == OK)
14399 /* trunc() is not in C90, use floor() or ceil() instead. */
14400 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14401 else
14402 rettv->vval.v_float = 0.0;
14403}
14404#endif
14405
14406/*
14407 * "type(expr)" function
14408 */
14409 static void
14410f_type(typval_T *argvars, typval_T *rettv)
14411{
14412 int n = -1;
14413
14414 switch (argvars[0].v_type)
14415 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014416 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14417 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014418 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014419 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14420 case VAR_LIST: n = VAR_TYPE_LIST; break;
14421 case VAR_DICT: n = VAR_TYPE_DICT; break;
14422 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014423 case VAR_SPECIAL:
14424 if (argvars[0].vval.v_number == VVAL_FALSE
14425 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014426 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014427 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014428 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014429 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014430 case VAR_JOB: n = VAR_TYPE_JOB; break;
14431 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014432 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014433 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014434 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014435 n = -1;
14436 break;
14437 }
14438 rettv->vval.v_number = n;
14439}
14440
14441/*
14442 * "undofile(name)" function
14443 */
14444 static void
14445f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
14446{
14447 rettv->v_type = VAR_STRING;
14448#ifdef FEAT_PERSISTENT_UNDO
14449 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014450 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014451
14452 if (*fname == NUL)
14453 {
14454 /* If there is no file name there will be no undo file. */
14455 rettv->vval.v_string = NULL;
14456 }
14457 else
14458 {
14459 char_u *ffname = FullName_save(fname, FALSE);
14460
14461 if (ffname != NULL)
14462 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
14463 vim_free(ffname);
14464 }
14465 }
14466#else
14467 rettv->vval.v_string = NULL;
14468#endif
14469}
14470
14471/*
14472 * "undotree()" function
14473 */
14474 static void
14475f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
14476{
14477 if (rettv_dict_alloc(rettv) == OK)
14478 {
14479 dict_T *dict = rettv->vval.v_dict;
14480 list_T *list;
14481
Bram Moolenaare0be1672018-07-08 16:50:37 +020014482 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
14483 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
14484 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
14485 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
14486 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
14487 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014488
14489 list = list_alloc();
14490 if (list != NULL)
14491 {
14492 u_eval_tree(curbuf->b_u_oldhead, list);
14493 dict_add_list(dict, "entries", list);
14494 }
14495 }
14496}
14497
14498/*
14499 * "values(dict)" function
14500 */
14501 static void
14502f_values(typval_T *argvars, typval_T *rettv)
14503{
14504 dict_list(argvars, rettv, 1);
14505}
14506
14507/*
14508 * "virtcol(string)" function
14509 */
14510 static void
14511f_virtcol(typval_T *argvars, typval_T *rettv)
14512{
14513 colnr_T vcol = 0;
14514 pos_T *fp;
14515 int fnum = curbuf->b_fnum;
14516
14517 fp = var2fpos(&argvars[0], FALSE, &fnum);
14518 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
14519 && fnum == curbuf->b_fnum)
14520 {
14521 getvvcol(curwin, fp, NULL, NULL, &vcol);
14522 ++vcol;
14523 }
14524
14525 rettv->vval.v_number = vcol;
14526}
14527
14528/*
14529 * "visualmode()" function
14530 */
14531 static void
14532f_visualmode(typval_T *argvars, typval_T *rettv)
14533{
14534 char_u str[2];
14535
14536 rettv->v_type = VAR_STRING;
14537 str[0] = curbuf->b_visual_mode_eval;
14538 str[1] = NUL;
14539 rettv->vval.v_string = vim_strsave(str);
14540
14541 /* A non-zero number or non-empty string argument: reset mode. */
14542 if (non_zero_arg(&argvars[0]))
14543 curbuf->b_visual_mode_eval = NUL;
14544}
14545
14546/*
14547 * "wildmenumode()" function
14548 */
14549 static void
14550f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14551{
14552#ifdef FEAT_WILDMENU
14553 if (wild_menu_showing)
14554 rettv->vval.v_number = 1;
14555#endif
14556}
14557
14558/*
14559 * "winbufnr(nr)" function
14560 */
14561 static void
14562f_winbufnr(typval_T *argvars, typval_T *rettv)
14563{
14564 win_T *wp;
14565
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014566 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014567 if (wp == NULL)
14568 rettv->vval.v_number = -1;
14569 else
14570 rettv->vval.v_number = wp->w_buffer->b_fnum;
14571}
14572
14573/*
14574 * "wincol()" function
14575 */
14576 static void
14577f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
14578{
14579 validate_cursor();
14580 rettv->vval.v_number = curwin->w_wcol + 1;
14581}
14582
14583/*
14584 * "winheight(nr)" function
14585 */
14586 static void
14587f_winheight(typval_T *argvars, typval_T *rettv)
14588{
14589 win_T *wp;
14590
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014591 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014592 if (wp == NULL)
14593 rettv->vval.v_number = -1;
14594 else
14595 rettv->vval.v_number = wp->w_height;
14596}
14597
14598/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014599 * "winlayout()" function
14600 */
14601 static void
14602f_winlayout(typval_T *argvars, typval_T *rettv)
14603{
14604 tabpage_T *tp;
14605
14606 if (rettv_list_alloc(rettv) != OK)
14607 return;
14608
14609 if (argvars[0].v_type == VAR_UNKNOWN)
14610 tp = curtab;
14611 else
14612 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014613 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020014614 if (tp == NULL)
14615 return;
14616 }
14617
14618 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
14619}
14620
14621/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014622 * "winline()" function
14623 */
14624 static void
14625f_winline(typval_T *argvars UNUSED, typval_T *rettv)
14626{
14627 validate_cursor();
14628 rettv->vval.v_number = curwin->w_wrow + 1;
14629}
14630
14631/*
14632 * "winnr()" function
14633 */
14634 static void
14635f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
14636{
14637 int nr = 1;
14638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014639 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014640 rettv->vval.v_number = nr;
14641}
14642
14643/*
14644 * "winrestcmd()" function
14645 */
14646 static void
14647f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
14648{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014649 win_T *wp;
14650 int winnr = 1;
14651 garray_T ga;
14652 char_u buf[50];
14653
14654 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020014655 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014656 {
14657 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14658 ga_concat(&ga, buf);
14659 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14660 ga_concat(&ga, buf);
14661 ++winnr;
14662 }
14663 ga_append(&ga, NUL);
14664
14665 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666 rettv->v_type = VAR_STRING;
14667}
14668
14669/*
14670 * "winrestview()" function
14671 */
14672 static void
14673f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
14674{
14675 dict_T *dict;
14676
14677 if (argvars[0].v_type != VAR_DICT
14678 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014679 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680 else
14681 {
14682 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014683 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014684 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014685 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686#ifdef FEAT_VIRTUALEDIT
14687 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014688 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689#endif
14690 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
14691 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010014692 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014693 curwin->w_set_curswant = FALSE;
14694 }
14695
14696 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014697 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698#ifdef FEAT_DIFF
14699 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014700 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701#endif
14702 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014703 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014705 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706
14707 check_cursor();
14708 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020014709 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 changed_window_setting();
14711
14712 if (curwin->w_topline <= 0)
14713 curwin->w_topline = 1;
14714 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
14715 curwin->w_topline = curbuf->b_ml.ml_line_count;
14716#ifdef FEAT_DIFF
14717 check_topfill(curwin, TRUE);
14718#endif
14719 }
14720}
14721
14722/*
14723 * "winsaveview()" function
14724 */
14725 static void
14726f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
14727{
14728 dict_T *dict;
14729
14730 if (rettv_dict_alloc(rettv) == FAIL)
14731 return;
14732 dict = rettv->vval.v_dict;
14733
Bram Moolenaare0be1672018-07-08 16:50:37 +020014734 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
14735 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014736#ifdef FEAT_VIRTUALEDIT
Bram Moolenaare0be1672018-07-08 16:50:37 +020014737 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014738#endif
14739 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020014740 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741
Bram Moolenaare0be1672018-07-08 16:50:37 +020014742 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014743#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020014744 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014745#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020014746 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
14747 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014748}
14749
14750/*
14751 * "winwidth(nr)" function
14752 */
14753 static void
14754f_winwidth(typval_T *argvars, typval_T *rettv)
14755{
14756 win_T *wp;
14757
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020014758 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014759 if (wp == NULL)
14760 rettv->vval.v_number = -1;
14761 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763}
14764
14765/*
14766 * "wordcount()" function
14767 */
14768 static void
14769f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
14770{
14771 if (rettv_dict_alloc(rettv) == FAIL)
14772 return;
14773 cursor_pos_info(rettv->vval.v_dict);
14774}
14775
14776/*
14777 * "writefile()" function
14778 */
14779 static void
14780f_writefile(typval_T *argvars, typval_T *rettv)
14781{
14782 int binary = FALSE;
14783 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014784#ifdef HAVE_FSYNC
14785 int do_fsync = p_fs;
14786#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 char_u *fname;
14788 FILE *fd;
14789 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014790 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014791 list_T *list = NULL;
14792 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014793
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014794 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014795 if (check_restricted() || check_secure())
14796 return;
14797
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014798 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014799 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014800 list = argvars[0].vval.v_list;
14801 if (list == NULL)
14802 return;
14803 for (li = list->lv_first; li != NULL; li = li->li_next)
14804 if (tv_get_string_chk(&li->li_tv) == NULL)
14805 return;
14806 }
14807 else if (argvars[0].v_type == VAR_BLOB)
14808 {
14809 blob = argvars[0].vval.v_blob;
14810 if (blob == NULL)
14811 return;
14812 }
14813 else
14814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014815 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014816 return;
14817 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014818
14819 if (argvars[2].v_type != VAR_UNKNOWN)
14820 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014821 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014822
14823 if (arg2 == NULL)
14824 return;
14825 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014826 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014827 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014829#ifdef HAVE_FSYNC
14830 if (vim_strchr(arg2, 's') != NULL)
14831 do_fsync = TRUE;
14832 else if (vim_strchr(arg2, 'S') != NULL)
14833 do_fsync = FALSE;
14834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014835 }
14836
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014837 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014838 if (fname == NULL)
14839 return;
14840
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014841 /* Always open the file in binary mode, library functions have a mind of
14842 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014843 if (*fname == NUL || (fd = mch_fopen((char *)fname,
14844 append ? APPENDBIN : WRITEBIN)) == NULL)
14845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014846 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014847 ret = -1;
14848 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014849 else if (blob)
14850 {
14851 if (write_blob(fd, blob) == FAIL)
14852 ret = -1;
14853#ifdef HAVE_FSYNC
14854 else if (do_fsync)
14855 // Ignore the error, the user wouldn't know what to do about it.
14856 // May happen for a device.
14857 vim_ignored = fsync(fileno(fd));
14858#endif
14859 fclose(fd);
14860 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014861 else
14862 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020014863 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014864 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014865#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010014866 else if (do_fsync)
14867 /* Ignore the error, the user wouldn't know what to do about it.
14868 * May happen for a device. */
Bram Moolenaar42335f52018-09-13 15:33:43 +020014869 vim_ignored = fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010014870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014871 fclose(fd);
14872 }
14873
14874 rettv->vval.v_number = ret;
14875}
14876
14877/*
14878 * "xor(expr, expr)" function
14879 */
14880 static void
14881f_xor(typval_T *argvars, typval_T *rettv)
14882{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014883 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
14884 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014885}
14886
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014887#endif /* FEAT_EVAL */