blob: 2d8bf5275064a93339832232afa549211a74ca10 [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 Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200152static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_extend(typval_T *argvars, typval_T *rettv);
154static void f_feedkeys(typval_T *argvars, typval_T *rettv);
155static void f_filereadable(typval_T *argvars, typval_T *rettv);
156static void f_filewritable(typval_T *argvars, typval_T *rettv);
157static void f_filter(typval_T *argvars, typval_T *rettv);
158static void f_finddir(typval_T *argvars, typval_T *rettv);
159static void f_findfile(typval_T *argvars, typval_T *rettv);
160#ifdef FEAT_FLOAT
161static void f_float2nr(typval_T *argvars, typval_T *rettv);
162static void f_floor(typval_T *argvars, typval_T *rettv);
163static void f_fmod(typval_T *argvars, typval_T *rettv);
164#endif
165static void f_fnameescape(typval_T *argvars, typval_T *rettv);
166static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
167static void f_foldclosed(typval_T *argvars, typval_T *rettv);
168static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
169static void f_foldlevel(typval_T *argvars, typval_T *rettv);
170static void f_foldtext(typval_T *argvars, typval_T *rettv);
171static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
172static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200173static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_function(typval_T *argvars, typval_T *rettv);
175static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
176static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200177static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getbufline(typval_T *argvars, typval_T *rettv);
179static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100180static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getchar(typval_T *argvars, typval_T *rettv);
182static void f_getcharmod(typval_T *argvars, typval_T *rettv);
183static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
184static void f_getcmdline(typval_T *argvars, typval_T *rettv);
185#if defined(FEAT_CMDL_COMPL)
186static void f_getcompletion(typval_T *argvars, typval_T *rettv);
187#endif
188static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
189static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
190static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
191static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getfontname(typval_T *argvars, typval_T *rettv);
194static void f_getfperm(typval_T *argvars, typval_T *rettv);
195static void f_getfsize(typval_T *argvars, typval_T *rettv);
196static void f_getftime(typval_T *argvars, typval_T *rettv);
197static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100198static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200200static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getmatches(typval_T *argvars, typval_T *rettv);
202static void f_getpid(typval_T *argvars, typval_T *rettv);
203static void f_getcurpos(typval_T *argvars, typval_T *rettv);
204static void f_getpos(typval_T *argvars, typval_T *rettv);
205static void f_getqflist(typval_T *argvars, typval_T *rettv);
206static void f_getreg(typval_T *argvars, typval_T *rettv);
207static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_gettabvar(typval_T *argvars, typval_T *rettv);
210static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100211static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200212static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100213static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_getwinposx(typval_T *argvars, typval_T *rettv);
215static void f_getwinposy(typval_T *argvars, typval_T *rettv);
216static void f_getwinvar(typval_T *argvars, typval_T *rettv);
217static void f_glob(typval_T *argvars, typval_T *rettv);
218static void f_globpath(typval_T *argvars, typval_T *rettv);
219static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
220static void f_has(typval_T *argvars, typval_T *rettv);
221static void f_has_key(typval_T *argvars, typval_T *rettv);
222static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
223static void f_hasmapto(typval_T *argvars, typval_T *rettv);
224static void f_histadd(typval_T *argvars, typval_T *rettv);
225static void f_histdel(typval_T *argvars, typval_T *rettv);
226static void f_histget(typval_T *argvars, typval_T *rettv);
227static void f_histnr(typval_T *argvars, typval_T *rettv);
228static void f_hlID(typval_T *argvars, typval_T *rettv);
229static void f_hlexists(typval_T *argvars, typval_T *rettv);
230static void f_hostname(typval_T *argvars, typval_T *rettv);
231static void f_iconv(typval_T *argvars, typval_T *rettv);
232static void f_indent(typval_T *argvars, typval_T *rettv);
233static void f_index(typval_T *argvars, typval_T *rettv);
234static void f_input(typval_T *argvars, typval_T *rettv);
235static void f_inputdialog(typval_T *argvars, typval_T *rettv);
236static void f_inputlist(typval_T *argvars, typval_T *rettv);
237static void f_inputrestore(typval_T *argvars, typval_T *rettv);
238static void f_inputsave(typval_T *argvars, typval_T *rettv);
239static void f_inputsecret(typval_T *argvars, typval_T *rettv);
240static void f_insert(typval_T *argvars, typval_T *rettv);
241static void f_invert(typval_T *argvars, typval_T *rettv);
242static void f_isdirectory(typval_T *argvars, typval_T *rettv);
243static void f_islocked(typval_T *argvars, typval_T *rettv);
244#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200245static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_isnan(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_items(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_JOB_CHANNEL
250static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
251static void f_job_info(typval_T *argvars, typval_T *rettv);
252static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
253static void f_job_start(typval_T *argvars, typval_T *rettv);
254static void f_job_stop(typval_T *argvars, typval_T *rettv);
255static void f_job_status(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_join(typval_T *argvars, typval_T *rettv);
258static void f_js_decode(typval_T *argvars, typval_T *rettv);
259static void f_js_encode(typval_T *argvars, typval_T *rettv);
260static void f_json_decode(typval_T *argvars, typval_T *rettv);
261static void f_json_encode(typval_T *argvars, typval_T *rettv);
262static void f_keys(typval_T *argvars, typval_T *rettv);
263static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
264static void f_len(typval_T *argvars, typval_T *rettv);
265static void f_libcall(typval_T *argvars, typval_T *rettv);
266static void f_libcallnr(typval_T *argvars, typval_T *rettv);
267static void f_line(typval_T *argvars, typval_T *rettv);
268static void f_line2byte(typval_T *argvars, typval_T *rettv);
269static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200270static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_localtime(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_log(typval_T *argvars, typval_T *rettv);
274static void f_log10(typval_T *argvars, typval_T *rettv);
275#endif
276#ifdef FEAT_LUA
277static void f_luaeval(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_map(typval_T *argvars, typval_T *rettv);
280static void f_maparg(typval_T *argvars, typval_T *rettv);
281static void f_mapcheck(typval_T *argvars, typval_T *rettv);
282static void f_match(typval_T *argvars, typval_T *rettv);
283static void f_matchadd(typval_T *argvars, typval_T *rettv);
284static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
285static void f_matcharg(typval_T *argvars, typval_T *rettv);
286static void f_matchdelete(typval_T *argvars, typval_T *rettv);
287static void f_matchend(typval_T *argvars, typval_T *rettv);
288static void f_matchlist(typval_T *argvars, typval_T *rettv);
289static void f_matchstr(typval_T *argvars, typval_T *rettv);
290static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
291static void f_max(typval_T *argvars, typval_T *rettv);
292static void f_min(typval_T *argvars, typval_T *rettv);
293#ifdef vim_mkdir
294static void f_mkdir(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_mode(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_MZSCHEME
298static void f_mzeval(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
301static void f_nr2char(typval_T *argvars, typval_T *rettv);
302static void f_or(typval_T *argvars, typval_T *rettv);
303static void f_pathshorten(typval_T *argvars, typval_T *rettv);
304#ifdef FEAT_PERL
305static void f_perleval(typval_T *argvars, typval_T *rettv);
306#endif
307#ifdef FEAT_FLOAT
308static void f_pow(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
311static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200312#ifdef FEAT_JOB_CHANNEL
313static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200314static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200315static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_pumvisible(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_PYTHON3
319static void f_py3eval(typval_T *argvars, typval_T *rettv);
320#endif
321#ifdef FEAT_PYTHON
322static void f_pyeval(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100324#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
325static void f_pyxeval(typval_T *argvars, typval_T *rettv);
326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200328static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200330static void f_reg_executing(typval_T *argvars, typval_T *rettv);
331static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_reltime(typval_T *argvars, typval_T *rettv);
333#ifdef FEAT_FLOAT
334static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
335#endif
336static void f_reltimestr(typval_T *argvars, typval_T *rettv);
337static void f_remote_expr(typval_T *argvars, typval_T *rettv);
338static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
339static void f_remote_peek(typval_T *argvars, typval_T *rettv);
340static void f_remote_read(typval_T *argvars, typval_T *rettv);
341static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100342static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_remove(typval_T *argvars, typval_T *rettv);
344static void f_rename(typval_T *argvars, typval_T *rettv);
345static void f_repeat(typval_T *argvars, typval_T *rettv);
346static void f_resolve(typval_T *argvars, typval_T *rettv);
347static void f_reverse(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_round(typval_T *argvars, typval_T *rettv);
350#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100351#ifdef FEAT_RUBY
352static void f_rubyeval(typval_T *argvars, typval_T *rettv);
353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_screenattr(typval_T *argvars, typval_T *rettv);
355static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100356static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_screencol(typval_T *argvars, typval_T *rettv);
358static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100359static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360static void f_search(typval_T *argvars, typval_T *rettv);
361static void f_searchdecl(typval_T *argvars, typval_T *rettv);
362static void f_searchpair(typval_T *argvars, typval_T *rettv);
363static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
364static void f_searchpos(typval_T *argvars, typval_T *rettv);
365static void f_server2client(typval_T *argvars, typval_T *rettv);
366static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200367static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_setbufvar(typval_T *argvars, typval_T *rettv);
369static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
370static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200371static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setfperm(typval_T *argvars, typval_T *rettv);
373static void f_setline(typval_T *argvars, typval_T *rettv);
374static void f_setloclist(typval_T *argvars, typval_T *rettv);
375static void f_setmatches(typval_T *argvars, typval_T *rettv);
376static void f_setpos(typval_T *argvars, typval_T *rettv);
377static void f_setqflist(typval_T *argvars, typval_T *rettv);
378static void f_setreg(typval_T *argvars, typval_T *rettv);
379static void f_settabvar(typval_T *argvars, typval_T *rettv);
380static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100381static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200382static void f_setwinvar(typval_T *argvars, typval_T *rettv);
383#ifdef FEAT_CRYPT
384static void f_sha256(typval_T *argvars, typval_T *rettv);
385#endif /* FEAT_CRYPT */
386static void f_shellescape(typval_T *argvars, typval_T *rettv);
387static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388#ifdef FEAT_SIGNS
389static void f_sign_define(typval_T *argvars, typval_T *rettv);
390static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
391static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100392static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100393static void f_sign_place(typval_T *argvars, typval_T *rettv);
394static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
395static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_simplify(typval_T *argvars, typval_T *rettv);
398#ifdef FEAT_FLOAT
399static void f_sin(typval_T *argvars, typval_T *rettv);
400static void f_sinh(typval_T *argvars, typval_T *rettv);
401#endif
402static void f_sort(typval_T *argvars, typval_T *rettv);
403static void f_soundfold(typval_T *argvars, typval_T *rettv);
404static void f_spellbadword(typval_T *argvars, typval_T *rettv);
405static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
406static void f_split(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_sqrt(typval_T *argvars, typval_T *rettv);
409static void f_str2float(typval_T *argvars, typval_T *rettv);
410#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200411static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_str2nr(typval_T *argvars, typval_T *rettv);
413static void f_strchars(typval_T *argvars, typval_T *rettv);
414#ifdef HAVE_STRFTIME
415static void f_strftime(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_strgetchar(typval_T *argvars, typval_T *rettv);
418static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_strlen(typval_T *argvars, typval_T *rettv);
420static void f_strcharpart(typval_T *argvars, typval_T *rettv);
421static void f_strpart(typval_T *argvars, typval_T *rettv);
422static void f_strridx(typval_T *argvars, typval_T *rettv);
423static void f_strtrans(typval_T *argvars, typval_T *rettv);
424static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
425static void f_strwidth(typval_T *argvars, typval_T *rettv);
426static void f_submatch(typval_T *argvars, typval_T *rettv);
427static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200428static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200429static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_synID(typval_T *argvars, typval_T *rettv);
431static void f_synIDattr(typval_T *argvars, typval_T *rettv);
432static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
433static void f_synstack(typval_T *argvars, typval_T *rettv);
434static void f_synconcealed(typval_T *argvars, typval_T *rettv);
435static void f_system(typval_T *argvars, typval_T *rettv);
436static void f_systemlist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
438static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
439static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
440static void f_taglist(typval_T *argvars, typval_T *rettv);
441static void f_tagfiles(typval_T *argvars, typval_T *rettv);
442static void f_tempname(typval_T *argvars, typval_T *rettv);
443static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
444static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200445static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200446static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200447static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100448static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100449static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100451static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100452static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200453#ifdef FEAT_JOB_CHANNEL
454static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
455#endif
456static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
457#ifdef FEAT_JOB_CHANNEL
458static void f_test_null_job(typval_T *argvars, typval_T *rettv);
459#endif
460static void f_test_null_list(typval_T *argvars, typval_T *rettv);
461static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
462static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200463#ifdef FEAT_GUI
464static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
465#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200467static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_test_settime(typval_T *argvars, typval_T *rettv);
470#ifdef FEAT_FLOAT
471static void f_tan(typval_T *argvars, typval_T *rettv);
472static void f_tanh(typval_T *argvars, typval_T *rettv);
473#endif
474#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200475static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200476static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477static void f_timer_start(typval_T *argvars, typval_T *rettv);
478static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200479static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481static void f_tolower(typval_T *argvars, typval_T *rettv);
482static void f_toupper(typval_T *argvars, typval_T *rettv);
483static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100484static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485#ifdef FEAT_FLOAT
486static void f_trunc(typval_T *argvars, typval_T *rettv);
487#endif
488static void f_type(typval_T *argvars, typval_T *rettv);
489static void f_undofile(typval_T *argvars, typval_T *rettv);
490static void f_undotree(typval_T *argvars, typval_T *rettv);
491static void f_uniq(typval_T *argvars, typval_T *rettv);
492static void f_values(typval_T *argvars, typval_T *rettv);
493static void f_virtcol(typval_T *argvars, typval_T *rettv);
494static void f_visualmode(typval_T *argvars, typval_T *rettv);
495static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200496static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
498static void f_win_getid(typval_T *argvars, typval_T *rettv);
499static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
500static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
501static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100502static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503static void f_winbufnr(typval_T *argvars, typval_T *rettv);
504static void f_wincol(typval_T *argvars, typval_T *rettv);
505static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200506static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507static void f_winline(typval_T *argvars, typval_T *rettv);
508static void f_winnr(typval_T *argvars, typval_T *rettv);
509static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
510static void f_winrestview(typval_T *argvars, typval_T *rettv);
511static void f_winsaveview(typval_T *argvars, typval_T *rettv);
512static void f_winwidth(typval_T *argvars, typval_T *rettv);
513static void f_writefile(typval_T *argvars, typval_T *rettv);
514static void f_wordcount(typval_T *argvars, typval_T *rettv);
515static void f_xor(typval_T *argvars, typval_T *rettv);
516
517/*
518 * Array with names and number of arguments of all internal functions
519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
520 */
521static struct fst
522{
523 char *f_name; /* function name */
524 char f_min_argc; /* minimal number of arguments */
525 char f_max_argc; /* maximal number of arguments */
526 void (*f_func)(typval_T *args, typval_T *rvar);
527 /* implementation of function */
528} functions[] =
529{
530#ifdef FEAT_FLOAT
531 {"abs", 1, 1, f_abs},
532 {"acos", 1, 1, f_acos}, /* WJMc */
533#endif
534 {"add", 2, 2, f_add},
535 {"and", 2, 2, f_and},
536 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200537 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"argidx", 0, 0, f_argidx},
540 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200541 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#ifdef FEAT_FLOAT
543 {"asin", 1, 1, f_asin}, /* WJMc */
544#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100545 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100547 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200549 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100551 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"assert_match", 2, 3, f_assert_match},
553 {"assert_notequal", 2, 3, f_assert_notequal},
554 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100555 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"assert_true", 1, 2, f_assert_true},
557#ifdef FEAT_FLOAT
558 {"atan", 1, 1, f_atan},
559 {"atan2", 2, 2, f_atan2},
560#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200562 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100563 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100565 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100566# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200568 {"browse", 4, 4, f_browse},
569 {"browsedir", 2, 2, f_browsedir},
570 {"bufexists", 1, 1, f_bufexists},
571 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
572 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
573 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
574 {"buflisted", 1, 1, f_buflisted},
575 {"bufloaded", 1, 1, f_bufloaded},
576 {"bufname", 1, 1, f_bufname},
577 {"bufnr", 1, 2, f_bufnr},
578 {"bufwinid", 1, 1, f_bufwinid},
579 {"bufwinnr", 1, 1, f_bufwinnr},
580 {"byte2line", 1, 1, f_byte2line},
581 {"byteidx", 2, 2, f_byteidx},
582 {"byteidxcomp", 2, 2, f_byteidxcomp},
583 {"call", 2, 3, f_call},
584#ifdef FEAT_FLOAT
585 {"ceil", 1, 1, f_ceil},
586#endif
587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200590 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
592 {"ch_evalraw", 2, 3, f_ch_evalraw},
593 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
594 {"ch_getjob", 1, 1, f_ch_getjob},
595 {"ch_info", 1, 1, f_ch_info},
596 {"ch_log", 1, 2, f_ch_log},
597 {"ch_logfile", 1, 2, f_ch_logfile},
598 {"ch_open", 1, 2, f_ch_open},
599 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"ch_readraw", 1, 2, f_ch_readraw},
602 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
603 {"ch_sendraw", 2, 3, f_ch_sendraw},
604 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200605 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606#endif
607 {"changenr", 0, 0, f_changenr},
608 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200609 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100611 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"col", 1, 1, f_col},
613#if defined(FEAT_INS_EXPAND)
614 {"complete", 2, 2, f_complete},
615 {"complete_add", 1, 1, f_complete_add},
616 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100617 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618#endif
619 {"confirm", 1, 4, f_confirm},
620 {"copy", 1, 1, f_copy},
621#ifdef FEAT_FLOAT
622 {"cos", 1, 1, f_cos},
623 {"cosh", 1, 1, f_cosh},
624#endif
625 {"count", 2, 4, f_count},
626 {"cscope_connection",0,3, f_cscope_connection},
627 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200629 {"debugbreak", 1, 1, f_debugbreak},
630#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"deepcopy", 1, 2, f_deepcopy},
632 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200633 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"did_filetype", 0, 0, f_did_filetype},
635 {"diff_filler", 1, 1, f_diff_filler},
636 {"diff_hlID", 2, 2, f_diff_hlID},
637 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200638 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"escape", 2, 2, f_escape},
640 {"eval", 1, 1, f_eval},
641 {"eventhandler", 0, 0, f_eventhandler},
642 {"executable", 1, 1, f_executable},
643 {"execute", 1, 2, f_execute},
644 {"exepath", 1, 1, f_exepath},
645 {"exists", 1, 1, f_exists},
646#ifdef FEAT_FLOAT
647 {"exp", 1, 1, f_exp},
648#endif
649 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200650 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"extend", 2, 3, f_extend},
652 {"feedkeys", 1, 2, f_feedkeys},
653 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
654 {"filereadable", 1, 1, f_filereadable},
655 {"filewritable", 1, 1, f_filewritable},
656 {"filter", 2, 2, f_filter},
657 {"finddir", 1, 3, f_finddir},
658 {"findfile", 1, 3, f_findfile},
659#ifdef FEAT_FLOAT
660 {"float2nr", 1, 1, f_float2nr},
661 {"floor", 1, 1, f_floor},
662 {"fmod", 2, 2, f_fmod},
663#endif
664 {"fnameescape", 1, 1, f_fnameescape},
665 {"fnamemodify", 2, 2, f_fnamemodify},
666 {"foldclosed", 1, 1, f_foldclosed},
667 {"foldclosedend", 1, 1, f_foldclosedend},
668 {"foldlevel", 1, 1, f_foldlevel},
669 {"foldtext", 0, 0, f_foldtext},
670 {"foldtextresult", 1, 1, f_foldtextresult},
671 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200672 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"function", 1, 3, f_function},
674 {"garbagecollect", 0, 1, f_garbagecollect},
675 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200676 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getbufline", 2, 3, f_getbufline},
678 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100679 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getchar", 0, 1, f_getchar},
681 {"getcharmod", 0, 0, f_getcharmod},
682 {"getcharsearch", 0, 0, f_getcharsearch},
683 {"getcmdline", 0, 0, f_getcmdline},
684 {"getcmdpos", 0, 0, f_getcmdpos},
685 {"getcmdtype", 0, 0, f_getcmdtype},
686 {"getcmdwintype", 0, 0, f_getcmdwintype},
687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200688 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689#endif
690 {"getcurpos", 0, 0, f_getcurpos},
691 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200692 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200693 {"getfontname", 0, 1, f_getfontname},
694 {"getfperm", 1, 1, f_getfperm},
695 {"getfsize", 1, 1, f_getfsize},
696 {"getftime", 1, 1, f_getftime},
697 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100698 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100701 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getpid", 0, 0, f_getpid},
703 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200704 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"getreg", 0, 3, f_getreg},
706 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200708 {"gettabvar", 2, 3, f_gettabvar},
709 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100710 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200711 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100712 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"getwinposx", 0, 0, f_getwinposx},
714 {"getwinposy", 0, 0, f_getwinposy},
715 {"getwinvar", 2, 3, f_getwinvar},
716 {"glob", 1, 4, f_glob},
717 {"glob2regpat", 1, 1, f_glob2regpat},
718 {"globpath", 2, 5, f_globpath},
719 {"has", 1, 1, f_has},
720 {"has_key", 2, 2, f_has_key},
721 {"haslocaldir", 0, 2, f_haslocaldir},
722 {"hasmapto", 1, 3, f_hasmapto},
723 {"highlightID", 1, 1, f_hlID}, /* obsolete */
724 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
725 {"histadd", 2, 2, f_histadd},
726 {"histdel", 1, 2, f_histdel},
727 {"histget", 1, 2, f_histget},
728 {"histnr", 1, 1, f_histnr},
729 {"hlID", 1, 1, f_hlID},
730 {"hlexists", 1, 1, f_hlexists},
731 {"hostname", 0, 0, f_hostname},
732 {"iconv", 3, 3, f_iconv},
733 {"indent", 1, 1, f_indent},
734 {"index", 2, 4, f_index},
735 {"input", 1, 3, f_input},
736 {"inputdialog", 1, 3, f_inputdialog},
737 {"inputlist", 1, 1, f_inputlist},
738 {"inputrestore", 0, 0, f_inputrestore},
739 {"inputsave", 0, 0, f_inputsave},
740 {"inputsecret", 1, 2, f_inputsecret},
741 {"insert", 2, 3, f_insert},
742 {"invert", 1, 1, f_invert},
743 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isinf", 1, 1, f_isinf},
746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747 {"islocked", 1, 1, f_islocked},
748#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
749 {"isnan", 1, 1, f_isnan},
750#endif
751 {"items", 1, 1, f_items},
752#ifdef FEAT_JOB_CHANNEL
753 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200754 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"job_setoptions", 2, 2, f_job_setoptions},
756 {"job_start", 1, 2, f_job_start},
757 {"job_status", 1, 1, f_job_status},
758 {"job_stop", 1, 2, f_job_stop},
759#endif
760 {"join", 1, 2, f_join},
761 {"js_decode", 1, 1, f_js_decode},
762 {"js_encode", 1, 1, f_js_encode},
763 {"json_decode", 1, 1, f_json_decode},
764 {"json_encode", 1, 1, f_json_encode},
765 {"keys", 1, 1, f_keys},
766 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
767 {"len", 1, 1, f_len},
768 {"libcall", 3, 3, f_libcall},
769 {"libcallnr", 3, 3, f_libcallnr},
770 {"line", 1, 1, f_line},
771 {"line2byte", 1, 1, f_line2byte},
772 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200773 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200775 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"localtime", 0, 0, f_localtime},
778#ifdef FEAT_FLOAT
779 {"log", 1, 1, f_log},
780 {"log10", 1, 1, f_log10},
781#endif
782#ifdef FEAT_LUA
783 {"luaeval", 1, 2, f_luaeval},
784#endif
785 {"map", 2, 2, f_map},
786 {"maparg", 1, 4, f_maparg},
787 {"mapcheck", 1, 3, f_mapcheck},
788 {"match", 2, 4, f_match},
789 {"matchadd", 2, 5, f_matchadd},
790 {"matchaddpos", 2, 5, f_matchaddpos},
791 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100792 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"matchend", 2, 4, f_matchend},
794 {"matchlist", 2, 4, f_matchlist},
795 {"matchstr", 2, 4, f_matchstr},
796 {"matchstrpos", 2, 4, f_matchstrpos},
797 {"max", 1, 1, f_max},
798 {"min", 1, 1, f_min},
799#ifdef vim_mkdir
800 {"mkdir", 1, 3, f_mkdir},
801#endif
802 {"mode", 0, 1, f_mode},
803#ifdef FEAT_MZSCHEME
804 {"mzeval", 1, 1, f_mzeval},
805#endif
806 {"nextnonblank", 1, 1, f_nextnonblank},
807 {"nr2char", 1, 2, f_nr2char},
808 {"or", 2, 2, f_or},
809 {"pathshorten", 1, 1, f_pathshorten},
810#ifdef FEAT_PERL
811 {"perleval", 1, 1, f_perleval},
812#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200814 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200815 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200817 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200818 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200819 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200820 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200821 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200822#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200823#ifdef FEAT_FLOAT
824 {"pow", 2, 2, f_pow},
825#endif
826 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100827 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200828#ifdef FEAT_JOB_CHANNEL
829 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200830 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200831 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
832#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100833#ifdef FEAT_TEXT_PROP
834 {"prop_add", 3, 3, f_prop_add},
835 {"prop_clear", 1, 3, f_prop_clear},
836 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100837 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838 {"prop_type_add", 2, 2, f_prop_type_add},
839 {"prop_type_change", 2, 2, f_prop_type_change},
840 {"prop_type_delete", 1, 2, f_prop_type_delete},
841 {"prop_type_get", 1, 2, f_prop_type_get},
842 {"prop_type_list", 0, 1, f_prop_type_list},
843#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200844 {"pumvisible", 0, 0, f_pumvisible},
845#ifdef FEAT_PYTHON3
846 {"py3eval", 1, 1, f_py3eval},
847#endif
848#ifdef FEAT_PYTHON
849 {"pyeval", 1, 1, f_pyeval},
850#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100851#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
852 {"pyxeval", 1, 1, f_pyxeval},
853#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200855 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200857 {"reg_executing", 0, 0, f_reg_executing},
858 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200859 {"reltime", 0, 2, f_reltime},
860#ifdef FEAT_FLOAT
861 {"reltimefloat", 1, 1, f_reltimefloat},
862#endif
863 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100864 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"remote_foreground", 1, 1, f_remote_foreground},
866 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100867 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100869 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"remove", 2, 3, f_remove},
871 {"rename", 2, 2, f_rename},
872 {"repeat", 2, 2, f_repeat},
873 {"resolve", 1, 1, f_resolve},
874 {"reverse", 1, 1, f_reverse},
875#ifdef FEAT_FLOAT
876 {"round", 1, 1, f_round},
877#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100878#ifdef FEAT_RUBY
879 {"rubyeval", 1, 1, f_rubyeval},
880#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881 {"screenattr", 2, 2, f_screenattr},
882 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100883 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200884 {"screencol", 0, 0, f_screencol},
885 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100886 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200887 {"search", 1, 4, f_search},
888 {"searchdecl", 1, 3, f_searchdecl},
889 {"searchpair", 3, 7, f_searchpair},
890 {"searchpairpos", 3, 7, f_searchpairpos},
891 {"searchpos", 1, 4, f_searchpos},
892 {"server2client", 2, 2, f_server2client},
893 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200894 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"setbufvar", 3, 3, f_setbufvar},
896 {"setcharsearch", 1, 1, f_setcharsearch},
897 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200898 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"setfperm", 2, 2, f_setfperm},
900 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200901 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100902 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200904 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200905 {"setreg", 2, 3, f_setreg},
906 {"settabvar", 3, 3, f_settabvar},
907 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100908 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200909 {"setwinvar", 3, 3, f_setwinvar},
910#ifdef FEAT_CRYPT
911 {"sha256", 1, 1, f_sha256},
912#endif
913 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100914 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100915#ifdef FEAT_SIGNS
916 {"sign_define", 1, 2, f_sign_define},
917 {"sign_getdefined", 0, 1, f_sign_getdefined},
918 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100919 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100920 {"sign_place", 4, 5, f_sign_place},
921 {"sign_undefine", 0, 1, f_sign_undefine},
922 {"sign_unplace", 1, 2, f_sign_unplace},
923#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200924 {"simplify", 1, 1, f_simplify},
925#ifdef FEAT_FLOAT
926 {"sin", 1, 1, f_sin},
927 {"sinh", 1, 1, f_sinh},
928#endif
929 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200930#ifdef FEAT_SOUND
931 {"sound_playevent", 1, 2, f_sound_playevent},
932 {"sound_playfile", 1, 2, f_sound_playfile},
933 {"sound_stop", 1, 1, f_sound_stop},
934 {"sound_stopall", 0, 0, f_sound_stopall},
935#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200936 {"soundfold", 1, 1, f_soundfold},
937 {"spellbadword", 0, 1, f_spellbadword},
938 {"spellsuggest", 1, 3, f_spellsuggest},
939 {"split", 1, 3, f_split},
940#ifdef FEAT_FLOAT
941 {"sqrt", 1, 1, f_sqrt},
942 {"str2float", 1, 1, f_str2float},
943#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200944 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945 {"str2nr", 1, 2, f_str2nr},
946 {"strcharpart", 2, 3, f_strcharpart},
947 {"strchars", 1, 2, f_strchars},
948 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
949#ifdef HAVE_STRFTIME
950 {"strftime", 1, 2, f_strftime},
951#endif
952 {"strgetchar", 2, 2, f_strgetchar},
953 {"stridx", 2, 3, f_stridx},
954 {"string", 1, 1, f_string},
955 {"strlen", 1, 1, f_strlen},
956 {"strpart", 2, 3, f_strpart},
957 {"strridx", 2, 3, f_strridx},
958 {"strtrans", 1, 1, f_strtrans},
959 {"strwidth", 1, 1, f_strwidth},
960 {"submatch", 1, 2, f_submatch},
961 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200962 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200963 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964 {"synID", 3, 3, f_synID},
965 {"synIDattr", 2, 3, f_synIDattr},
966 {"synIDtrans", 1, 1, f_synIDtrans},
967 {"synconcealed", 2, 2, f_synconcealed},
968 {"synstack", 2, 2, f_synstack},
969 {"system", 1, 2, f_system},
970 {"systemlist", 1, 2, f_systemlist},
971 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
972 {"tabpagenr", 0, 1, f_tabpagenr},
973 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
974 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100975 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200976#ifdef FEAT_FLOAT
977 {"tan", 1, 1, f_tan},
978 {"tanh", 1, 1, f_tanh},
979#endif
980 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100982 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
983 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100984 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200985 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200986# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
987 {"term_getansicolors", 1, 1, f_term_getansicolors},
988# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200989 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200990 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200991 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200992 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200993 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200994 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200995 {"term_getstatus", 1, 1, f_term_getstatus},
996 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200997 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200998 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200999 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001000 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001001# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1002 {"term_setansicolors", 2, 2, f_term_setansicolors},
1003# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001004 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001005 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001006 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001007 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001008 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001009#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001010 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1011 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001012 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001013 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001014 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001015 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001016 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001017#ifdef FEAT_JOB_CHANNEL
1018 {"test_null_channel", 0, 0, f_test_null_channel},
1019#endif
1020 {"test_null_dict", 0, 0, f_test_null_dict},
1021#ifdef FEAT_JOB_CHANNEL
1022 {"test_null_job", 0, 0, f_test_null_job},
1023#endif
1024 {"test_null_list", 0, 0, f_test_null_list},
1025 {"test_null_partial", 0, 0, f_test_null_partial},
1026 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001027 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001028 {"test_override", 2, 2, f_test_override},
1029 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001030#ifdef FEAT_GUI
1031 {"test_scrollbar", 3, 3, f_test_scrollbar},
1032#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001033#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001034 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001035#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001036 {"test_settime", 1, 1, f_test_settime},
1037#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001038 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001039 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040 {"timer_start", 2, 3, f_timer_start},
1041 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001042 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001043#endif
1044 {"tolower", 1, 1, f_tolower},
1045 {"toupper", 1, 1, f_toupper},
1046 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001047 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048#ifdef FEAT_FLOAT
1049 {"trunc", 1, 1, f_trunc},
1050#endif
1051 {"type", 1, 1, f_type},
1052 {"undofile", 1, 1, f_undofile},
1053 {"undotree", 0, 0, f_undotree},
1054 {"uniq", 1, 3, f_uniq},
1055 {"values", 1, 1, f_values},
1056 {"virtcol", 1, 1, f_virtcol},
1057 {"visualmode", 0, 1, f_visualmode},
1058 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001059 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001060 {"win_findbuf", 1, 1, f_win_findbuf},
1061 {"win_getid", 0, 2, f_win_getid},
1062 {"win_gotoid", 1, 1, f_win_gotoid},
1063 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1064 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001065 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001066 {"winbufnr", 1, 1, f_winbufnr},
1067 {"wincol", 0, 0, f_wincol},
1068 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001069 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001070 {"winline", 0, 0, f_winline},
1071 {"winnr", 0, 1, f_winnr},
1072 {"winrestcmd", 0, 0, f_winrestcmd},
1073 {"winrestview", 1, 1, f_winrestview},
1074 {"winsaveview", 0, 0, f_winsaveview},
1075 {"winwidth", 1, 1, f_winwidth},
1076 {"wordcount", 0, 0, f_wordcount},
1077 {"writefile", 2, 3, f_writefile},
1078 {"xor", 2, 2, f_xor},
1079};
1080
1081#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1082
1083/*
1084 * Function given to ExpandGeneric() to obtain the list of internal
1085 * or user defined function names.
1086 */
1087 char_u *
1088get_function_name(expand_T *xp, int idx)
1089{
1090 static int intidx = -1;
1091 char_u *name;
1092
1093 if (idx == 0)
1094 intidx = -1;
1095 if (intidx < 0)
1096 {
1097 name = get_user_func_name(xp, idx);
1098 if (name != NULL)
1099 return name;
1100 }
1101 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1102 {
1103 STRCPY(IObuff, functions[intidx].f_name);
1104 STRCAT(IObuff, "(");
1105 if (functions[intidx].f_max_argc == 0)
1106 STRCAT(IObuff, ")");
1107 return IObuff;
1108 }
1109
1110 return NULL;
1111}
1112
1113/*
1114 * Function given to ExpandGeneric() to obtain the list of internal or
1115 * user defined variable or function names.
1116 */
1117 char_u *
1118get_expr_name(expand_T *xp, int idx)
1119{
1120 static int intidx = -1;
1121 char_u *name;
1122
1123 if (idx == 0)
1124 intidx = -1;
1125 if (intidx < 0)
1126 {
1127 name = get_function_name(xp, idx);
1128 if (name != NULL)
1129 return name;
1130 }
1131 return get_user_var_name(xp, ++intidx);
1132}
1133
1134#endif /* FEAT_CMDL_COMPL */
1135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001136/*
1137 * Find internal function in table above.
1138 * Return index, or -1 if not found
1139 */
1140 int
1141find_internal_func(
1142 char_u *name) /* name of the function */
1143{
1144 int first = 0;
1145 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1146 int cmp;
1147 int x;
1148
1149 /*
1150 * Find the function name in the table. Binary search.
1151 */
1152 while (first <= last)
1153 {
1154 x = first + ((unsigned)(last - first) >> 1);
1155 cmp = STRCMP(name, functions[x].f_name);
1156 if (cmp < 0)
1157 last = x - 1;
1158 else if (cmp > 0)
1159 first = x + 1;
1160 else
1161 return x;
1162 }
1163 return -1;
1164}
1165
1166 int
1167call_internal_func(
1168 char_u *name,
1169 int argcount,
1170 typval_T *argvars,
1171 typval_T *rettv)
1172{
1173 int i;
1174
1175 i = find_internal_func(name);
1176 if (i < 0)
1177 return ERROR_UNKNOWN;
1178 if (argcount < functions[i].f_min_argc)
1179 return ERROR_TOOFEW;
1180 if (argcount > functions[i].f_max_argc)
1181 return ERROR_TOOMANY;
1182 argvars[argcount].v_type = VAR_UNKNOWN;
1183 functions[i].f_func(argvars, rettv);
1184 return ERROR_NONE;
1185}
1186
1187/*
1188 * Return TRUE for a non-zero Number and a non-empty String.
1189 */
1190 static int
1191non_zero_arg(typval_T *argvars)
1192{
1193 return ((argvars[0].v_type == VAR_NUMBER
1194 && argvars[0].vval.v_number != 0)
1195 || (argvars[0].v_type == VAR_SPECIAL
1196 && argvars[0].vval.v_number == VVAL_TRUE)
1197 || (argvars[0].v_type == VAR_STRING
1198 && argvars[0].vval.v_string != NULL
1199 && *argvars[0].vval.v_string != NUL));
1200}
1201
1202/*
1203 * Get the lnum from the first argument.
1204 * Also accepts ".", "$", etc., but that only works for the current buffer.
1205 * Returns -1 on error.
1206 */
1207 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001208tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001209{
1210 typval_T rettv;
1211 linenr_T lnum;
1212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001213 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001214 if (lnum == 0) /* no valid number, try using line() */
1215 {
1216 rettv.v_type = VAR_NUMBER;
1217 f_line(argvars, &rettv);
1218 lnum = (linenr_T)rettv.vval.v_number;
1219 clear_tv(&rettv);
1220 }
1221 return lnum;
1222}
1223
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001224/*
1225 * Get the lnum from the first argument.
1226 * Also accepts "$", then "buf" is used.
1227 * Returns 0 on error.
1228 */
1229 static linenr_T
1230tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1231{
1232 if (argvars[0].v_type == VAR_STRING
1233 && argvars[0].vval.v_string != NULL
1234 && argvars[0].vval.v_string[0] == '$'
1235 && buf != NULL)
1236 return buf->b_ml.ml_line_count;
1237 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1238}
1239
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001240#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001241/*
1242 * Get the float value of "argvars[0]" into "f".
1243 * Returns FAIL when the argument is not a Number or Float.
1244 */
1245 static int
1246get_float_arg(typval_T *argvars, float_T *f)
1247{
1248 if (argvars[0].v_type == VAR_FLOAT)
1249 {
1250 *f = argvars[0].vval.v_float;
1251 return OK;
1252 }
1253 if (argvars[0].v_type == VAR_NUMBER)
1254 {
1255 *f = (float_T)argvars[0].vval.v_number;
1256 return OK;
1257 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001258 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 return FAIL;
1260}
1261
1262/*
1263 * "abs(expr)" function
1264 */
1265 static void
1266f_abs(typval_T *argvars, typval_T *rettv)
1267{
1268 if (argvars[0].v_type == VAR_FLOAT)
1269 {
1270 rettv->v_type = VAR_FLOAT;
1271 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1272 }
1273 else
1274 {
1275 varnumber_T n;
1276 int error = FALSE;
1277
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001278 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001279 if (error)
1280 rettv->vval.v_number = -1;
1281 else if (n > 0)
1282 rettv->vval.v_number = n;
1283 else
1284 rettv->vval.v_number = -n;
1285 }
1286}
1287
1288/*
1289 * "acos()" function
1290 */
1291 static void
1292f_acos(typval_T *argvars, typval_T *rettv)
1293{
1294 float_T f = 0.0;
1295
1296 rettv->v_type = VAR_FLOAT;
1297 if (get_float_arg(argvars, &f) == OK)
1298 rettv->vval.v_float = acos(f);
1299 else
1300 rettv->vval.v_float = 0.0;
1301}
1302#endif
1303
1304/*
1305 * "add(list, item)" function
1306 */
1307 static void
1308f_add(typval_T *argvars, typval_T *rettv)
1309{
1310 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001311 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001312
1313 rettv->vval.v_number = 1; /* Default: Failed */
1314 if (argvars[0].v_type == VAR_LIST)
1315 {
1316 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001317 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 (char_u *)N_("add() argument"), TRUE)
1319 && list_append_tv(l, &argvars[1]) == OK)
1320 copy_tv(&argvars[0], rettv);
1321 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001322 else if (argvars[0].v_type == VAR_BLOB)
1323 {
1324 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001325 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 (char_u *)N_("add() argument"), TRUE))
1327 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001328 int error = FALSE;
1329 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1330
1331 if (!error)
1332 {
1333 ga_append(&b->bv_ga, (int)n);
1334 copy_tv(&argvars[0], rettv);
1335 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001336 }
1337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001338 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001339 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340}
1341
1342/*
1343 * "and(expr, expr)" function
1344 */
1345 static void
1346f_and(typval_T *argvars, typval_T *rettv)
1347{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001348 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1349 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001350}
1351
1352/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001353 * If there is a window for "curbuf", make it the current window.
1354 */
1355 static void
1356find_win_for_curbuf(void)
1357{
1358 wininfo_T *wip;
1359
1360 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1361 {
1362 if (wip->wi_win != NULL)
1363 {
1364 curwin = wip->wi_win;
1365 break;
1366 }
1367 }
1368}
1369
1370/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 */
1373 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001374set_buffer_lines(
1375 buf_T *buf,
1376 linenr_T lnum_arg,
1377 int append,
1378 typval_T *lines,
1379 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001380{
Bram Moolenaarca851592018-06-06 21:04:07 +02001381 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1382 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 list_T *l = NULL;
1384 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 linenr_T append_lnum;
1387 buf_T *curbuf_save = NULL;
1388 win_T *curwin_save = NULL;
1389 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 /* When using the current buffer ml_mfp will be set if needed. Useful when
1392 * setline() is used on startup. For other buffers the buffer must be
1393 * loaded. */
1394 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001396 rettv->vval.v_number = 1; /* FAIL */
1397 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001398 }
1399
Bram Moolenaarca851592018-06-06 21:04:07 +02001400 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001401 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001402 curbuf_save = curbuf;
1403 curwin_save = curwin;
1404 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001405 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001406 }
1407
1408 if (append)
1409 // appendbufline() uses the line number below which we insert
1410 append_lnum = lnum - 1;
1411 else
1412 // setbufline() uses the line number above which we insert, we only
1413 // append if it's below the last line
1414 append_lnum = curbuf->b_ml.ml_line_count;
1415
1416 if (lines->v_type == VAR_LIST)
1417 {
1418 l = lines->vval.v_list;
1419 li = l->lv_first;
1420 }
1421 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001422 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001423
1424 /* default result is zero == OK */
1425 for (;;)
1426 {
1427 if (l != NULL)
1428 {
1429 /* list argument, get next string */
1430 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001431 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001432 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001433 li = li->li_next;
1434 }
1435
Bram Moolenaarca851592018-06-06 21:04:07 +02001436 rettv->vval.v_number = 1; /* FAIL */
1437 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1438 break;
1439
1440 /* When coming here from Insert mode, sync undo, so that this can be
1441 * undone separately from what was previously inserted. */
1442 if (u_sync_once == 2)
1443 {
1444 u_sync_once = 1; /* notify that u_sync() was called */
1445 u_sync(TRUE);
1446 }
1447
1448 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1449 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001450 // Existing line, replace it.
1451 // Removes any existing text properties.
1452 if (u_savesub(lnum) == OK && ml_replace_len(
1453 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001454 {
1455 changed_bytes(lnum, 0);
1456 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1457 check_cursor_col();
1458 rettv->vval.v_number = 0; /* OK */
1459 }
1460 }
1461 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1462 {
1463 /* append the line */
1464 ++added;
1465 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1466 rettv->vval.v_number = 0; /* OK */
1467 }
1468
1469 if (l == NULL) /* only one string argument */
1470 break;
1471 ++lnum;
1472 }
1473
1474 if (added > 0)
1475 {
1476 win_T *wp;
1477 tabpage_T *tp;
1478
1479 appended_lines_mark(append_lnum, added);
1480 FOR_ALL_TAB_WINDOWS(tp, wp)
1481 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1482 wp->w_cursor.lnum += added;
1483 check_cursor_col();
1484
Bram Moolenaarf2732452018-06-03 14:47:35 +02001485#ifdef FEAT_JOB_CHANNEL
1486 if (bt_prompt(curbuf) && (State & INSERT))
1487 // show the line with the prompt
1488 update_topline();
1489#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001490 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001491
1492 if (!is_curbuf)
1493 {
1494 curbuf = curbuf_save;
1495 curwin = curwin_save;
1496 }
1497}
1498
1499/*
1500 * "append(lnum, string/list)" function
1501 */
1502 static void
1503f_append(typval_T *argvars, typval_T *rettv)
1504{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001505 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001506
1507 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1508}
1509
1510/*
1511 * "appendbufline(buf, lnum, string/list)" function
1512 */
1513 static void
1514f_appendbufline(typval_T *argvars, typval_T *rettv)
1515{
1516 linenr_T lnum;
1517 buf_T *buf;
1518
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001519 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001520 if (buf == NULL)
1521 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001522 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001524 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001525 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1526 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527}
1528
1529/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001530 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001531 */
1532 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001533f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001535 win_T *wp;
1536
1537 if (argvars[0].v_type == VAR_UNKNOWN)
1538 // use the current window
1539 rettv->vval.v_number = ARGCOUNT;
1540 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001541 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001542 // use the global argument list
1543 rettv->vval.v_number = GARGCOUNT;
1544 else
1545 {
1546 // use the argument list of the specified window
1547 wp = find_win_by_nr_or_id(&argvars[0]);
1548 if (wp != NULL)
1549 rettv->vval.v_number = WARGCOUNT(wp);
1550 else
1551 rettv->vval.v_number = -1;
1552 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001553}
1554
1555/*
1556 * "argidx()" function
1557 */
1558 static void
1559f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1560{
1561 rettv->vval.v_number = curwin->w_arg_idx;
1562}
1563
1564/*
1565 * "arglistid()" function
1566 */
1567 static void
1568f_arglistid(typval_T *argvars, typval_T *rettv)
1569{
1570 win_T *wp;
1571
1572 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001573 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001574 if (wp != NULL)
1575 rettv->vval.v_number = wp->w_alist->id;
1576}
1577
1578/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 * Get the argument list for a given window
1580 */
1581 static void
1582get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1583{
1584 int idx;
1585
1586 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1587 for (idx = 0; idx < argcount; ++idx)
1588 list_append_string(rettv->vval.v_list,
1589 alist_name(&arglist[idx]), -1);
1590}
1591
1592/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593 * "argv(nr)" function
1594 */
1595 static void
1596f_argv(typval_T *argvars, typval_T *rettv)
1597{
1598 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001599 aentry_T *arglist = NULL;
1600 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001601
1602 if (argvars[0].v_type != VAR_UNKNOWN)
1603 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001604 if (argvars[1].v_type == VAR_UNKNOWN)
1605 {
1606 arglist = ARGLIST;
1607 argcount = ARGCOUNT;
1608 }
1609 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001610 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001611 {
1612 arglist = GARGLIST;
1613 argcount = GARGCOUNT;
1614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001616 {
1617 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1618
1619 if (wp != NULL)
1620 {
1621 /* Use the argument list of the specified window */
1622 arglist = WARGLIST(wp);
1623 argcount = WARGCOUNT(wp);
1624 }
1625 }
1626
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001628 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001629 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001630 if (arglist != NULL && idx >= 0 && idx < argcount)
1631 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1632 else if (idx == -1)
1633 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001635 else
1636 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637}
1638
1639/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001640 * "assert_beeps(cmd [, error])" function
1641 */
1642 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001644{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001646}
1647
1648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001649 * "assert_equal(expected, actual[, msg])" function
1650 */
1651 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655}
1656
1657/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001658 * "assert_equalfile(fname-one, fname-two)" function
1659 */
1660 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001662{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001663 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001664}
1665
1666/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667 * "assert_notequal(expected, actual[, msg])" function
1668 */
1669 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001672 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673}
1674
1675/*
1676 * "assert_exception(string[, msg])" function
1677 */
1678 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001681 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001682}
1683
1684/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001685 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686 */
1687 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001690 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691}
1692
1693/*
1694 * "assert_false(actual[, msg])" function
1695 */
1696 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001699 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700}
1701
1702/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001703 * "assert_inrange(lower, upper[, msg])" function
1704 */
1705 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001707{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001708 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001709}
1710
1711/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712 * "assert_match(pattern, actual[, msg])" function
1713 */
1714 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001716{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001717 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718}
1719
1720/*
1721 * "assert_notmatch(pattern, actual[, msg])" function
1722 */
1723 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001724f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001726 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727}
1728
1729/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001730 * "assert_report(msg)" function
1731 */
1732 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001733f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001734{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001735 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001736}
1737
1738/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001739 * "assert_true(actual[, msg])" function
1740 */
1741 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001742f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001743{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001744 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001745}
1746
1747#ifdef FEAT_FLOAT
1748/*
1749 * "asin()" function
1750 */
1751 static void
1752f_asin(typval_T *argvars, typval_T *rettv)
1753{
1754 float_T f = 0.0;
1755
1756 rettv->v_type = VAR_FLOAT;
1757 if (get_float_arg(argvars, &f) == OK)
1758 rettv->vval.v_float = asin(f);
1759 else
1760 rettv->vval.v_float = 0.0;
1761}
1762
1763/*
1764 * "atan()" function
1765 */
1766 static void
1767f_atan(typval_T *argvars, typval_T *rettv)
1768{
1769 float_T f = 0.0;
1770
1771 rettv->v_type = VAR_FLOAT;
1772 if (get_float_arg(argvars, &f) == OK)
1773 rettv->vval.v_float = atan(f);
1774 else
1775 rettv->vval.v_float = 0.0;
1776}
1777
1778/*
1779 * "atan2()" function
1780 */
1781 static void
1782f_atan2(typval_T *argvars, typval_T *rettv)
1783{
1784 float_T fx = 0.0, fy = 0.0;
1785
1786 rettv->v_type = VAR_FLOAT;
1787 if (get_float_arg(argvars, &fx) == OK
1788 && get_float_arg(&argvars[1], &fy) == OK)
1789 rettv->vval.v_float = atan2(fx, fy);
1790 else
1791 rettv->vval.v_float = 0.0;
1792}
1793#endif
1794
1795/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001796 * "balloon_show()" function
1797 */
1798#ifdef FEAT_BEVAL
1799 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001800f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1801{
1802 rettv->v_type = VAR_STRING;
1803 if (balloonEval != NULL)
1804 {
1805 if (balloonEval->msg == NULL)
1806 rettv->vval.v_string = NULL;
1807 else
1808 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1809 }
1810}
1811
1812 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001813f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1814{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001815 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001816 {
1817 if (argvars[0].v_type == VAR_LIST
1818# ifdef FEAT_GUI
1819 && !gui.in_use
1820# endif
1821 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001822 {
1823 list_T *l = argvars[0].vval.v_list;
1824
1825 // empty list removes the balloon
1826 post_balloon(balloonEval, NULL,
1827 l == NULL || l->lv_len == 0 ? NULL : l);
1828 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001829 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001830 {
1831 char_u *mesg = tv_get_string_chk(&argvars[0]);
1832
1833 if (mesg != NULL)
1834 // empty string removes the balloon
1835 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1836 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001837 }
1838}
1839
Bram Moolenaar669a8282017-11-19 20:13:05 +01001840# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001841 static void
1842f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1843{
1844 if (rettv_list_alloc(rettv) == OK)
1845 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001846 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001847
1848 if (msg != NULL)
1849 {
1850 pumitem_T *array;
1851 int size = split_message(msg, &array);
1852 int i;
1853
1854 /* Skip the first and last item, they are always empty. */
1855 for (i = 1; i < size - 1; ++i)
1856 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001857 while (size > 0)
1858 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001859 vim_free(array);
1860 }
1861 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001862}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001863# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001864#endif
1865
1866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001867 * "browse(save, title, initdir, default)" function
1868 */
1869 static void
1870f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1871{
1872#ifdef FEAT_BROWSE
1873 int save;
1874 char_u *title;
1875 char_u *initdir;
1876 char_u *defname;
1877 char_u buf[NUMBUFLEN];
1878 char_u buf2[NUMBUFLEN];
1879 int error = FALSE;
1880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001881 save = (int)tv_get_number_chk(&argvars[0], &error);
1882 title = tv_get_string_chk(&argvars[1]);
1883 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1884 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001885
1886 if (error || title == NULL || initdir == NULL || defname == NULL)
1887 rettv->vval.v_string = NULL;
1888 else
1889 rettv->vval.v_string =
1890 do_browse(save ? BROWSE_SAVE : 0,
1891 title, defname, NULL, initdir, NULL, curbuf);
1892#else
1893 rettv->vval.v_string = NULL;
1894#endif
1895 rettv->v_type = VAR_STRING;
1896}
1897
1898/*
1899 * "browsedir(title, initdir)" function
1900 */
1901 static void
1902f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1903{
1904#ifdef FEAT_BROWSE
1905 char_u *title;
1906 char_u *initdir;
1907 char_u buf[NUMBUFLEN];
1908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001909 title = tv_get_string_chk(&argvars[0]);
1910 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911
1912 if (title == NULL || initdir == NULL)
1913 rettv->vval.v_string = NULL;
1914 else
1915 rettv->vval.v_string = do_browse(BROWSE_DIR,
1916 title, NULL, NULL, initdir, NULL, curbuf);
1917#else
1918 rettv->vval.v_string = NULL;
1919#endif
1920 rettv->v_type = VAR_STRING;
1921}
1922
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001923/*
1924 * Find a buffer by number or exact name.
1925 */
1926 static buf_T *
1927find_buffer(typval_T *avar)
1928{
1929 buf_T *buf = NULL;
1930
1931 if (avar->v_type == VAR_NUMBER)
1932 buf = buflist_findnr((int)avar->vval.v_number);
1933 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1934 {
1935 buf = buflist_findname_exp(avar->vval.v_string);
1936 if (buf == NULL)
1937 {
1938 /* No full path name match, try a match with a URL or a "nofile"
1939 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001940 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001941 if (buf->b_fname != NULL
1942 && (path_with_url(buf->b_fname)
1943#ifdef FEAT_QUICKFIX
1944 || bt_nofile(buf)
1945#endif
1946 )
1947 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1948 break;
1949 }
1950 }
1951 return buf;
1952}
1953
1954/*
1955 * "bufexists(expr)" function
1956 */
1957 static void
1958f_bufexists(typval_T *argvars, typval_T *rettv)
1959{
1960 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1961}
1962
1963/*
1964 * "buflisted(expr)" function
1965 */
1966 static void
1967f_buflisted(typval_T *argvars, typval_T *rettv)
1968{
1969 buf_T *buf;
1970
1971 buf = find_buffer(&argvars[0]);
1972 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1973}
1974
1975/*
1976 * "bufloaded(expr)" function
1977 */
1978 static void
1979f_bufloaded(typval_T *argvars, typval_T *rettv)
1980{
1981 buf_T *buf;
1982
1983 buf = find_buffer(&argvars[0]);
1984 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1985}
1986
1987 buf_T *
1988buflist_find_by_name(char_u *name, int curtab_only)
1989{
1990 int save_magic;
1991 char_u *save_cpo;
1992 buf_T *buf;
1993
1994 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1995 save_magic = p_magic;
1996 p_magic = TRUE;
1997 save_cpo = p_cpo;
1998 p_cpo = (char_u *)"";
1999
2000 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2001 TRUE, FALSE, curtab_only));
2002
2003 p_magic = save_magic;
2004 p_cpo = save_cpo;
2005 return buf;
2006}
2007
2008/*
2009 * Get buffer by number or pattern.
2010 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002011 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002012tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002013{
2014 char_u *name = tv->vval.v_string;
2015 buf_T *buf;
2016
2017 if (tv->v_type == VAR_NUMBER)
2018 return buflist_findnr((int)tv->vval.v_number);
2019 if (tv->v_type != VAR_STRING)
2020 return NULL;
2021 if (name == NULL || *name == NUL)
2022 return curbuf;
2023 if (name[0] == '$' && name[1] == NUL)
2024 return lastbuf;
2025
2026 buf = buflist_find_by_name(name, curtab_only);
2027
2028 /* If not found, try expanding the name, like done for bufexists(). */
2029 if (buf == NULL)
2030 buf = find_buffer(tv);
2031
2032 return buf;
2033}
2034
2035/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002036 * Get the buffer from "arg" and give an error and return NULL if it is not
2037 * valid.
2038 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002039 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002040get_buf_arg(typval_T *arg)
2041{
2042 buf_T *buf;
2043
2044 ++emsg_off;
2045 buf = tv_get_buf(arg, FALSE);
2046 --emsg_off;
2047 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002049 return buf;
2050}
2051
2052/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 * "bufname(expr)" function
2054 */
2055 static void
2056f_bufname(typval_T *argvars, typval_T *rettv)
2057{
2058 buf_T *buf;
2059
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002060 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002061 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002062 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 rettv->v_type = VAR_STRING;
2064 if (buf != NULL && buf->b_fname != NULL)
2065 rettv->vval.v_string = vim_strsave(buf->b_fname);
2066 else
2067 rettv->vval.v_string = NULL;
2068 --emsg_off;
2069}
2070
2071/*
2072 * "bufnr(expr)" function
2073 */
2074 static void
2075f_bufnr(typval_T *argvars, typval_T *rettv)
2076{
2077 buf_T *buf;
2078 int error = FALSE;
2079 char_u *name;
2080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002081 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002082 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002083 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 --emsg_off;
2085
2086 /* If the buffer isn't found and the second argument is not zero create a
2087 * new buffer. */
2088 if (buf == NULL
2089 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002090 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002092 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 && !error)
2094 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2095
2096 if (buf != NULL)
2097 rettv->vval.v_number = buf->b_fnum;
2098 else
2099 rettv->vval.v_number = -1;
2100}
2101
2102 static void
2103buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2104{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 win_T *wp;
2106 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 buf_T *buf;
2108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002109 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002111 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002112 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002113 {
2114 ++winnr;
2115 if (wp->w_buffer == buf)
2116 break;
2117 }
2118 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002119 --emsg_off;
2120}
2121
2122/*
2123 * "bufwinid(nr)" function
2124 */
2125 static void
2126f_bufwinid(typval_T *argvars, typval_T *rettv)
2127{
2128 buf_win_common(argvars, rettv, FALSE);
2129}
2130
2131/*
2132 * "bufwinnr(nr)" function
2133 */
2134 static void
2135f_bufwinnr(typval_T *argvars, typval_T *rettv)
2136{
2137 buf_win_common(argvars, rettv, TRUE);
2138}
2139
2140/*
2141 * "byte2line(byte)" function
2142 */
2143 static void
2144f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2145{
2146#ifndef FEAT_BYTEOFF
2147 rettv->vval.v_number = -1;
2148#else
2149 long boff = 0;
2150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002151 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152 if (boff < 0)
2153 rettv->vval.v_number = -1;
2154 else
2155 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2156 (linenr_T)0, &boff);
2157#endif
2158}
2159
2160 static void
2161byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2162{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 char_u *str;
2165 varnumber_T idx;
2166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002167 str = tv_get_string_chk(&argvars[0]);
2168 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 rettv->vval.v_number = -1;
2170 if (str == NULL || idx < 0)
2171 return;
2172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002173 t = str;
2174 for ( ; idx > 0; idx--)
2175 {
2176 if (*t == NUL) /* EOL reached */
2177 return;
2178 if (enc_utf8 && comp)
2179 t += utf_ptr2len(t);
2180 else
2181 t += (*mb_ptr2len)(t);
2182 }
2183 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002184}
2185
2186/*
2187 * "byteidx()" function
2188 */
2189 static void
2190f_byteidx(typval_T *argvars, typval_T *rettv)
2191{
2192 byteidx(argvars, rettv, FALSE);
2193}
2194
2195/*
2196 * "byteidxcomp()" function
2197 */
2198 static void
2199f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2200{
2201 byteidx(argvars, rettv, TRUE);
2202}
2203
2204/*
2205 * "call(func, arglist [, dict])" function
2206 */
2207 static void
2208f_call(typval_T *argvars, typval_T *rettv)
2209{
2210 char_u *func;
2211 partial_T *partial = NULL;
2212 dict_T *selfdict = NULL;
2213
2214 if (argvars[1].v_type != VAR_LIST)
2215 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002216 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002217 return;
2218 }
2219 if (argvars[1].vval.v_list == NULL)
2220 return;
2221
2222 if (argvars[0].v_type == VAR_FUNC)
2223 func = argvars[0].vval.v_string;
2224 else if (argvars[0].v_type == VAR_PARTIAL)
2225 {
2226 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002227 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002228 }
2229 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002230 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002231 if (*func == NUL)
2232 return; /* type error or empty name */
2233
2234 if (argvars[2].v_type != VAR_UNKNOWN)
2235 {
2236 if (argvars[2].v_type != VAR_DICT)
2237 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002238 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002239 return;
2240 }
2241 selfdict = argvars[2].vval.v_dict;
2242 }
2243
2244 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2245}
2246
2247#ifdef FEAT_FLOAT
2248/*
2249 * "ceil({float})" function
2250 */
2251 static void
2252f_ceil(typval_T *argvars, typval_T *rettv)
2253{
2254 float_T f = 0.0;
2255
2256 rettv->v_type = VAR_FLOAT;
2257 if (get_float_arg(argvars, &f) == OK)
2258 rettv->vval.v_float = ceil(f);
2259 else
2260 rettv->vval.v_float = 0.0;
2261}
2262#endif
2263
2264#ifdef FEAT_JOB_CHANNEL
2265/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002266 * "ch_canread()" function
2267 */
2268 static void
2269f_ch_canread(typval_T *argvars, typval_T *rettv)
2270{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002271 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002272
2273 rettv->vval.v_number = 0;
2274 if (channel != NULL)
2275 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2276 || channel_has_readahead(channel, PART_OUT)
2277 || channel_has_readahead(channel, PART_ERR);
2278}
2279
2280/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002281 * "ch_close()" function
2282 */
2283 static void
2284f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2285{
2286 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2287
2288 if (channel != NULL)
2289 {
2290 channel_close(channel, FALSE);
2291 channel_clear(channel);
2292 }
2293}
2294
2295/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002296 * "ch_close()" function
2297 */
2298 static void
2299f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2300{
2301 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2302
2303 if (channel != NULL)
2304 channel_close_in(channel);
2305}
2306
2307/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002308 * "ch_getbufnr()" function
2309 */
2310 static void
2311f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2312{
2313 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2314
2315 rettv->vval.v_number = -1;
2316 if (channel != NULL)
2317 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002318 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002319 int part;
2320
2321 if (STRCMP(what, "err") == 0)
2322 part = PART_ERR;
2323 else if (STRCMP(what, "out") == 0)
2324 part = PART_OUT;
2325 else if (STRCMP(what, "in") == 0)
2326 part = PART_IN;
2327 else
2328 part = PART_SOCK;
2329 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2330 rettv->vval.v_number =
2331 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2332 }
2333}
2334
2335/*
2336 * "ch_getjob()" function
2337 */
2338 static void
2339f_ch_getjob(typval_T *argvars, typval_T *rettv)
2340{
2341 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2342
2343 if (channel != NULL)
2344 {
2345 rettv->v_type = VAR_JOB;
2346 rettv->vval.v_job = channel->ch_job;
2347 if (channel->ch_job != NULL)
2348 ++channel->ch_job->jv_refcount;
2349 }
2350}
2351
2352/*
2353 * "ch_info()" function
2354 */
2355 static void
2356f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2357{
2358 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2359
2360 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2361 channel_info(channel, rettv->vval.v_dict);
2362}
2363
2364/*
2365 * "ch_log()" function
2366 */
2367 static void
2368f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2369{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002370 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002371 channel_T *channel = NULL;
2372
2373 if (argvars[1].v_type != VAR_UNKNOWN)
2374 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2375
Bram Moolenaard5359b22018-04-05 22:44:39 +02002376 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002377}
2378
2379/*
2380 * "ch_logfile()" function
2381 */
2382 static void
2383f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2384{
2385 char_u *fname;
2386 char_u *opt = (char_u *)"";
2387 char_u buf[NUMBUFLEN];
2388
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002389 /* Don't open a file in restricted mode. */
2390 if (check_restricted() || check_secure())
2391 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002392 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002393 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002394 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 ch_logfile(fname, opt);
2396}
2397
2398/*
2399 * "ch_open()" function
2400 */
2401 static void
2402f_ch_open(typval_T *argvars, typval_T *rettv)
2403{
2404 rettv->v_type = VAR_CHANNEL;
2405 if (check_restricted() || check_secure())
2406 return;
2407 rettv->vval.v_channel = channel_open_func(argvars);
2408}
2409
2410/*
2411 * "ch_read()" function
2412 */
2413 static void
2414f_ch_read(typval_T *argvars, typval_T *rettv)
2415{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002416 common_channel_read(argvars, rettv, FALSE, FALSE);
2417}
2418
2419/*
2420 * "ch_readblob()" function
2421 */
2422 static void
2423f_ch_readblob(typval_T *argvars, typval_T *rettv)
2424{
2425 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002426}
2427
2428/*
2429 * "ch_readraw()" function
2430 */
2431 static void
2432f_ch_readraw(typval_T *argvars, typval_T *rettv)
2433{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002434 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002435}
2436
2437/*
2438 * "ch_evalexpr()" function
2439 */
2440 static void
2441f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2442{
2443 ch_expr_common(argvars, rettv, TRUE);
2444}
2445
2446/*
2447 * "ch_sendexpr()" function
2448 */
2449 static void
2450f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2451{
2452 ch_expr_common(argvars, rettv, FALSE);
2453}
2454
2455/*
2456 * "ch_evalraw()" function
2457 */
2458 static void
2459f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2460{
2461 ch_raw_common(argvars, rettv, TRUE);
2462}
2463
2464/*
2465 * "ch_sendraw()" function
2466 */
2467 static void
2468f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2469{
2470 ch_raw_common(argvars, rettv, FALSE);
2471}
2472
2473/*
2474 * "ch_setoptions()" function
2475 */
2476 static void
2477f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2478{
2479 channel_T *channel;
2480 jobopt_T opt;
2481
2482 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2483 if (channel == NULL)
2484 return;
2485 clear_job_options(&opt);
2486 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002487 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488 channel_set_options(channel, &opt);
2489 free_job_options(&opt);
2490}
2491
2492/*
2493 * "ch_status()" function
2494 */
2495 static void
2496f_ch_status(typval_T *argvars, typval_T *rettv)
2497{
2498 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002499 jobopt_T opt;
2500 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002501
2502 /* return an empty string by default */
2503 rettv->v_type = VAR_STRING;
2504 rettv->vval.v_string = NULL;
2505
2506 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002507
2508 if (argvars[1].v_type != VAR_UNKNOWN)
2509 {
2510 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002511 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002512 && (opt.jo_set & JO_PART))
2513 part = opt.jo_part;
2514 }
2515
2516 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002517}
2518#endif
2519
2520/*
2521 * "changenr()" function
2522 */
2523 static void
2524f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2525{
2526 rettv->vval.v_number = curbuf->b_u_seq_cur;
2527}
2528
2529/*
2530 * "char2nr(string)" function
2531 */
2532 static void
2533f_char2nr(typval_T *argvars, typval_T *rettv)
2534{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002535 if (has_mbyte)
2536 {
2537 int utf8 = 0;
2538
2539 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002540 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541
2542 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002543 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002544 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002545 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546 }
2547 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002548 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549}
2550
2551/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002552 * "chdir(dir)" function
2553 */
2554 static void
2555f_chdir(typval_T *argvars, typval_T *rettv)
2556{
2557 char_u *cwd;
2558 cdscope_T scope = CDSCOPE_GLOBAL;
2559
2560 rettv->v_type = VAR_STRING;
2561 rettv->vval.v_string = NULL;
2562
2563 if (argvars[0].v_type != VAR_STRING)
2564 return;
2565
2566 // Return the current directory
2567 cwd = alloc(MAXPATHL);
2568 if (cwd != NULL)
2569 {
2570 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2571 {
2572#ifdef BACKSLASH_IN_FILENAME
2573 slash_adjust(cwd);
2574#endif
2575 rettv->vval.v_string = vim_strsave(cwd);
2576 }
2577 vim_free(cwd);
2578 }
2579
2580 if (curwin->w_localdir != NULL)
2581 scope = CDSCOPE_WINDOW;
2582 else if (curtab->tp_localdir != NULL)
2583 scope = CDSCOPE_TABPAGE;
2584
2585 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2586 // Directory change failed
2587 VIM_CLEAR(rettv->vval.v_string);
2588}
2589
2590/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002591 * "cindent(lnum)" function
2592 */
2593 static void
2594f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2595{
2596#ifdef FEAT_CINDENT
2597 pos_T pos;
2598 linenr_T lnum;
2599
2600 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002601 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002602 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2603 {
2604 curwin->w_cursor.lnum = lnum;
2605 rettv->vval.v_number = get_c_indent();
2606 curwin->w_cursor = pos;
2607 }
2608 else
2609#endif
2610 rettv->vval.v_number = -1;
2611}
2612
Bram Moolenaaraff74912019-03-30 18:11:49 +01002613 static win_T *
2614get_optional_window(typval_T *argvars, int idx)
2615{
2616 win_T *win = curwin;
2617
2618 if (argvars[idx].v_type != VAR_UNKNOWN)
2619 {
2620 win = find_win_by_nr_or_id(&argvars[idx]);
2621 if (win == NULL)
2622 {
2623 emsg(_(e_invalwindow));
2624 return NULL;
2625 }
2626 }
2627 return win;
2628}
2629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002630/*
2631 * "clearmatches()" function
2632 */
2633 static void
2634f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2635{
2636#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002637 win_T *win = get_optional_window(argvars, 0);
2638
2639 if (win != NULL)
2640 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002641#endif
2642}
2643
2644/*
2645 * "col(string)" function
2646 */
2647 static void
2648f_col(typval_T *argvars, typval_T *rettv)
2649{
2650 colnr_T col = 0;
2651 pos_T *fp;
2652 int fnum = curbuf->b_fnum;
2653
2654 fp = var2fpos(&argvars[0], FALSE, &fnum);
2655 if (fp != NULL && fnum == curbuf->b_fnum)
2656 {
2657 if (fp->col == MAXCOL)
2658 {
2659 /* '> can be MAXCOL, get the length of the line then */
2660 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2661 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2662 else
2663 col = MAXCOL;
2664 }
2665 else
2666 {
2667 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 /* col(".") when the cursor is on the NUL at the end of the line
2669 * because of "coladd" can be seen as an extra column. */
2670 if (virtual_active() && fp == &curwin->w_cursor)
2671 {
2672 char_u *p = ml_get_cursor();
2673
2674 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2675 curwin->w_virtcol - curwin->w_cursor.coladd))
2676 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002677 int l;
2678
2679 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2680 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002681 }
2682 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 }
2684 }
2685 rettv->vval.v_number = col;
2686}
2687
2688#if defined(FEAT_INS_EXPAND)
2689/*
2690 * "complete()" function
2691 */
2692 static void
2693f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2694{
2695 int startcol;
2696
2697 if ((State & INSERT) == 0)
2698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002699 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002700 return;
2701 }
2702
2703 /* Check for undo allowed here, because if something was already inserted
2704 * the line was already saved for undo and this check isn't done. */
2705 if (!undo_allowed())
2706 return;
2707
2708 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002710 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002711 return;
2712 }
2713
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002714 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002715 if (startcol <= 0)
2716 return;
2717
2718 set_completion(startcol - 1, argvars[1].vval.v_list);
2719}
2720
2721/*
2722 * "complete_add()" function
2723 */
2724 static void
2725f_complete_add(typval_T *argvars, typval_T *rettv)
2726{
2727 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2728}
2729
2730/*
2731 * "complete_check()" function
2732 */
2733 static void
2734f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2735{
2736 int saved = RedrawingDisabled;
2737
2738 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002739 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002740 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002741 RedrawingDisabled = saved;
2742}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002743
2744/*
2745 * "complete_info()" function
2746 */
2747 static void
2748f_complete_info(typval_T *argvars, typval_T *rettv)
2749{
2750 list_T *what_list = NULL;
2751
2752 if (rettv_dict_alloc(rettv) != OK)
2753 return;
2754
2755 if (argvars[0].v_type != VAR_UNKNOWN)
2756 {
2757 if (argvars[0].v_type != VAR_LIST)
2758 {
2759 emsg(_(e_listreq));
2760 return;
2761 }
2762 what_list = argvars[0].vval.v_list;
2763 }
2764 get_complete_info(what_list, rettv->vval.v_dict);
2765}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002766#endif
2767
2768/*
2769 * "confirm(message, buttons[, default [, type]])" function
2770 */
2771 static void
2772f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2773{
2774#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2775 char_u *message;
2776 char_u *buttons = NULL;
2777 char_u buf[NUMBUFLEN];
2778 char_u buf2[NUMBUFLEN];
2779 int def = 1;
2780 int type = VIM_GENERIC;
2781 char_u *typestr;
2782 int error = FALSE;
2783
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002784 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (message == NULL)
2786 error = TRUE;
2787 if (argvars[1].v_type != VAR_UNKNOWN)
2788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002789 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 if (buttons == NULL)
2791 error = TRUE;
2792 if (argvars[2].v_type != VAR_UNKNOWN)
2793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 if (argvars[3].v_type != VAR_UNKNOWN)
2796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002797 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002798 if (typestr == NULL)
2799 error = TRUE;
2800 else
2801 {
2802 switch (TOUPPER_ASC(*typestr))
2803 {
2804 case 'E': type = VIM_ERROR; break;
2805 case 'Q': type = VIM_QUESTION; break;
2806 case 'I': type = VIM_INFO; break;
2807 case 'W': type = VIM_WARNING; break;
2808 case 'G': type = VIM_GENERIC; break;
2809 }
2810 }
2811 }
2812 }
2813 }
2814
2815 if (buttons == NULL || *buttons == NUL)
2816 buttons = (char_u *)_("&Ok");
2817
2818 if (!error)
2819 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2820 def, NULL, FALSE);
2821#endif
2822}
2823
2824/*
2825 * "copy()" function
2826 */
2827 static void
2828f_copy(typval_T *argvars, typval_T *rettv)
2829{
2830 item_copy(&argvars[0], rettv, FALSE, 0);
2831}
2832
2833#ifdef FEAT_FLOAT
2834/*
2835 * "cos()" function
2836 */
2837 static void
2838f_cos(typval_T *argvars, typval_T *rettv)
2839{
2840 float_T f = 0.0;
2841
2842 rettv->v_type = VAR_FLOAT;
2843 if (get_float_arg(argvars, &f) == OK)
2844 rettv->vval.v_float = cos(f);
2845 else
2846 rettv->vval.v_float = 0.0;
2847}
2848
2849/*
2850 * "cosh()" function
2851 */
2852 static void
2853f_cosh(typval_T *argvars, typval_T *rettv)
2854{
2855 float_T f = 0.0;
2856
2857 rettv->v_type = VAR_FLOAT;
2858 if (get_float_arg(argvars, &f) == OK)
2859 rettv->vval.v_float = cosh(f);
2860 else
2861 rettv->vval.v_float = 0.0;
2862}
2863#endif
2864
2865/*
2866 * "count()" function
2867 */
2868 static void
2869f_count(typval_T *argvars, typval_T *rettv)
2870{
2871 long n = 0;
2872 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002873 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002874
Bram Moolenaar9966b212017-07-28 16:46:57 +02002875 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002876 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002877
2878 if (argvars[0].v_type == VAR_STRING)
2879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002880 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002881 char_u *p = argvars[0].vval.v_string;
2882 char_u *next;
2883
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002884 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002885 {
2886 if (ic)
2887 {
2888 size_t len = STRLEN(expr);
2889
2890 while (*p != NUL)
2891 {
2892 if (MB_STRNICMP(p, expr, len) == 0)
2893 {
2894 ++n;
2895 p += len;
2896 }
2897 else
2898 MB_PTR_ADV(p);
2899 }
2900 }
2901 else
2902 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2903 != NULL)
2904 {
2905 ++n;
2906 p = next + STRLEN(expr);
2907 }
2908 }
2909
2910 }
2911 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912 {
2913 listitem_T *li;
2914 list_T *l;
2915 long idx;
2916
2917 if ((l = argvars[0].vval.v_list) != NULL)
2918 {
2919 li = l->lv_first;
2920 if (argvars[2].v_type != VAR_UNKNOWN)
2921 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922 if (argvars[3].v_type != VAR_UNKNOWN)
2923 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002924 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002925 if (!error)
2926 {
2927 li = list_find(l, idx);
2928 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002929 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002930 }
2931 }
2932 if (error)
2933 li = NULL;
2934 }
2935
2936 for ( ; li != NULL; li = li->li_next)
2937 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2938 ++n;
2939 }
2940 }
2941 else if (argvars[0].v_type == VAR_DICT)
2942 {
2943 int todo;
2944 dict_T *d;
2945 hashitem_T *hi;
2946
2947 if ((d = argvars[0].vval.v_dict) != NULL)
2948 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 if (argvars[2].v_type != VAR_UNKNOWN)
2950 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002952 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002953 }
2954
2955 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2956 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2957 {
2958 if (!HASHITEM_EMPTY(hi))
2959 {
2960 --todo;
2961 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2962 ++n;
2963 }
2964 }
2965 }
2966 }
2967 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002968 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002969 rettv->vval.v_number = n;
2970}
2971
2972/*
2973 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2974 *
2975 * Checks the existence of a cscope connection.
2976 */
2977 static void
2978f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2979{
2980#ifdef FEAT_CSCOPE
2981 int num = 0;
2982 char_u *dbpath = NULL;
2983 char_u *prepend = NULL;
2984 char_u buf[NUMBUFLEN];
2985
2986 if (argvars[0].v_type != VAR_UNKNOWN
2987 && argvars[1].v_type != VAR_UNKNOWN)
2988 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002989 num = (int)tv_get_number(&argvars[0]);
2990 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002992 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 }
2994
2995 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2996#endif
2997}
2998
2999/*
3000 * "cursor(lnum, col)" function, or
3001 * "cursor(list)"
3002 *
3003 * Moves the cursor to the specified line and column.
3004 * Returns 0 when the position could be set, -1 otherwise.
3005 */
3006 static void
3007f_cursor(typval_T *argvars, typval_T *rettv)
3008{
3009 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 int set_curswant = TRUE;
3012
3013 rettv->vval.v_number = -1;
3014 if (argvars[1].v_type == VAR_UNKNOWN)
3015 {
3016 pos_T pos;
3017 colnr_T curswant = -1;
3018
3019 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003022 return;
3023 }
3024 line = pos.lnum;
3025 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 if (curswant >= 0)
3028 {
3029 curwin->w_curswant = curswant - 1;
3030 set_curswant = FALSE;
3031 }
3032 }
3033 else
3034 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003035 line = tv_get_lnum(argvars);
3036 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003038 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003040 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041 return; /* type error; errmsg already given */
3042 if (line > 0)
3043 curwin->w_cursor.lnum = line;
3044 if (col > 0)
3045 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047
3048 /* Make sure the cursor is in a valid position. */
3049 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003050 /* Correct cursor for multi-byte character. */
3051 if (has_mbyte)
3052 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053
3054 curwin->w_set_curswant = set_curswant;
3055 rettv->vval.v_number = 0;
3056}
3057
Bram Moolenaar4f974752019-02-17 17:44:42 +01003058#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003059/*
3060 * "debugbreak()" function
3061 */
3062 static void
3063f_debugbreak(typval_T *argvars, typval_T *rettv)
3064{
3065 int pid;
3066
3067 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003068 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003069 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003070 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003071 else
3072 {
3073 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3074
3075 if (hProcess != NULL)
3076 {
3077 DebugBreakProcess(hProcess);
3078 CloseHandle(hProcess);
3079 rettv->vval.v_number = OK;
3080 }
3081 }
3082}
3083#endif
3084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085/*
3086 * "deepcopy()" function
3087 */
3088 static void
3089f_deepcopy(typval_T *argvars, typval_T *rettv)
3090{
3091 int noref = 0;
3092 int copyID;
3093
3094 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003095 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003096 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003097 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098 else
3099 {
3100 copyID = get_copyID();
3101 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3102 }
3103}
3104
3105/*
3106 * "delete()" function
3107 */
3108 static void
3109f_delete(typval_T *argvars, typval_T *rettv)
3110{
3111 char_u nbuf[NUMBUFLEN];
3112 char_u *name;
3113 char_u *flags;
3114
3115 rettv->vval.v_number = -1;
3116 if (check_restricted() || check_secure())
3117 return;
3118
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003119 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120 if (name == NULL || *name == NUL)
3121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003122 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003123 return;
3124 }
3125
3126 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003127 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 else
3129 flags = (char_u *)"";
3130
3131 if (*flags == NUL)
3132 /* delete a file */
3133 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3134 else if (STRCMP(flags, "d") == 0)
3135 /* delete an empty directory */
3136 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3137 else if (STRCMP(flags, "rf") == 0)
3138 /* delete a directory recursively */
3139 rettv->vval.v_number = delete_recursive(name);
3140 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003141 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003142}
3143
3144/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003145 * "deletebufline()" function
3146 */
3147 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003148f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149{
3150 buf_T *buf;
3151 linenr_T first, last;
3152 linenr_T lnum;
3153 long count;
3154 int is_curbuf;
3155 buf_T *curbuf_save = NULL;
3156 win_T *curwin_save = NULL;
3157 tabpage_T *tp;
3158 win_T *wp;
3159
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003160 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003161 if (buf == NULL)
3162 {
3163 rettv->vval.v_number = 1; /* FAIL */
3164 return;
3165 }
3166 is_curbuf = buf == curbuf;
3167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003168 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003169 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003170 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003171 else
3172 last = first;
3173
3174 if (buf->b_ml.ml_mfp == NULL || first < 1
3175 || first > buf->b_ml.ml_line_count || last < first)
3176 {
3177 rettv->vval.v_number = 1; /* FAIL */
3178 return;
3179 }
3180
3181 if (!is_curbuf)
3182 {
3183 curbuf_save = curbuf;
3184 curwin_save = curwin;
3185 curbuf = buf;
3186 find_win_for_curbuf();
3187 }
3188 if (last > curbuf->b_ml.ml_line_count)
3189 last = curbuf->b_ml.ml_line_count;
3190 count = last - first + 1;
3191
3192 // When coming here from Insert mode, sync undo, so that this can be
3193 // undone separately from what was previously inserted.
3194 if (u_sync_once == 2)
3195 {
3196 u_sync_once = 1; // notify that u_sync() was called
3197 u_sync(TRUE);
3198 }
3199
3200 if (u_save(first - 1, last + 1) == FAIL)
3201 {
3202 rettv->vval.v_number = 1; /* FAIL */
3203 return;
3204 }
3205
3206 for (lnum = first; lnum <= last; ++lnum)
3207 ml_delete(first, TRUE);
3208
3209 FOR_ALL_TAB_WINDOWS(tp, wp)
3210 if (wp->w_buffer == buf)
3211 {
3212 if (wp->w_cursor.lnum > last)
3213 wp->w_cursor.lnum -= count;
3214 else if (wp->w_cursor.lnum> first)
3215 wp->w_cursor.lnum = first;
3216 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3217 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3218 }
3219 check_cursor_col();
3220 deleted_lines_mark(first, count);
3221
3222 if (!is_curbuf)
3223 {
3224 curbuf = curbuf_save;
3225 curwin = curwin_save;
3226 }
3227}
3228
3229/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003230 * "did_filetype()" function
3231 */
3232 static void
3233f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3234{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236}
3237
3238/*
3239 * "diff_filler()" function
3240 */
3241 static void
3242f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3243{
3244#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003245 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246#endif
3247}
3248
3249/*
3250 * "diff_hlID()" function
3251 */
3252 static void
3253f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3254{
3255#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003256 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003258 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 static int fnum = 0;
3260 static int change_start = 0;
3261 static int change_end = 0;
3262 static hlf_T hlID = (hlf_T)0;
3263 int filler_lines;
3264 int col;
3265
3266 if (lnum < 0) /* ignore type error in {lnum} arg */
3267 lnum = 0;
3268 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003269 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003270 || fnum != curbuf->b_fnum)
3271 {
3272 /* New line, buffer, change: need to get the values. */
3273 filler_lines = diff_check(curwin, lnum);
3274 if (filler_lines < 0)
3275 {
3276 if (filler_lines == -1)
3277 {
3278 change_start = MAXCOL;
3279 change_end = -1;
3280 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3281 hlID = HLF_ADD; /* added line */
3282 else
3283 hlID = HLF_CHD; /* changed line */
3284 }
3285 else
3286 hlID = HLF_ADD; /* added line */
3287 }
3288 else
3289 hlID = (hlf_T)0;
3290 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003291 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 fnum = curbuf->b_fnum;
3293 }
3294
3295 if (hlID == HLF_CHD || hlID == HLF_TXD)
3296 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003297 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003298 if (col >= change_start && col <= change_end)
3299 hlID = HLF_TXD; /* changed text */
3300 else
3301 hlID = HLF_CHD; /* changed line */
3302 }
3303 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3304#endif
3305}
3306
3307/*
3308 * "empty({expr})" function
3309 */
3310 static void
3311f_empty(typval_T *argvars, typval_T *rettv)
3312{
3313 int n = FALSE;
3314
3315 switch (argvars[0].v_type)
3316 {
3317 case VAR_STRING:
3318 case VAR_FUNC:
3319 n = argvars[0].vval.v_string == NULL
3320 || *argvars[0].vval.v_string == NUL;
3321 break;
3322 case VAR_PARTIAL:
3323 n = FALSE;
3324 break;
3325 case VAR_NUMBER:
3326 n = argvars[0].vval.v_number == 0;
3327 break;
3328 case VAR_FLOAT:
3329#ifdef FEAT_FLOAT
3330 n = argvars[0].vval.v_float == 0.0;
3331 break;
3332#endif
3333 case VAR_LIST:
3334 n = argvars[0].vval.v_list == NULL
3335 || argvars[0].vval.v_list->lv_first == NULL;
3336 break;
3337 case VAR_DICT:
3338 n = argvars[0].vval.v_dict == NULL
3339 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3340 break;
3341 case VAR_SPECIAL:
3342 n = argvars[0].vval.v_number != VVAL_TRUE;
3343 break;
3344
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003345 case VAR_BLOB:
3346 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003347 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3348 break;
3349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003350 case VAR_JOB:
3351#ifdef FEAT_JOB_CHANNEL
3352 n = argvars[0].vval.v_job == NULL
3353 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3354 break;
3355#endif
3356 case VAR_CHANNEL:
3357#ifdef FEAT_JOB_CHANNEL
3358 n = argvars[0].vval.v_channel == NULL
3359 || !channel_is_open(argvars[0].vval.v_channel);
3360 break;
3361#endif
3362 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003363 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003364 n = TRUE;
3365 break;
3366 }
3367
3368 rettv->vval.v_number = n;
3369}
3370
3371/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003372 * "environ()" function
3373 */
3374 static void
3375f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3376{
3377#if !defined(AMIGA)
3378 int i = 0;
3379 char_u *entry, *value;
3380# ifdef MSWIN
3381 extern wchar_t **_wenviron;
3382# else
3383 extern char **environ;
3384# endif
3385
3386 if (rettv_dict_alloc(rettv) != OK)
3387 return;
3388
3389# ifdef MSWIN
3390 if (*_wenviron == NULL)
3391 return;
3392# else
3393 if (*environ == NULL)
3394 return;
3395# endif
3396
3397 for (i = 0; ; ++i)
3398 {
3399# ifdef MSWIN
3400 short_u *p;
3401
3402 if ((p = (short_u *)_wenviron[i]) == NULL)
3403 return;
3404 entry = utf16_to_enc(p, NULL);
3405# else
3406 if ((entry = (char_u *)environ[i]) == NULL)
3407 return;
3408 entry = vim_strsave(entry);
3409# endif
3410 if (entry == NULL) // out of memory
3411 return;
3412 if ((value = vim_strchr(entry, '=')) == NULL)
3413 {
3414 vim_free(entry);
3415 continue;
3416 }
3417 *value++ = NUL;
3418 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3419 vim_free(entry);
3420 }
3421#endif
3422}
3423
3424/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003425 * "escape({string}, {chars})" function
3426 */
3427 static void
3428f_escape(typval_T *argvars, typval_T *rettv)
3429{
3430 char_u buf[NUMBUFLEN];
3431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003432 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3433 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 rettv->v_type = VAR_STRING;
3435}
3436
3437/*
3438 * "eval()" function
3439 */
3440 static void
3441f_eval(typval_T *argvars, typval_T *rettv)
3442{
3443 char_u *s, *p;
3444
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003445 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 if (s != NULL)
3447 s = skipwhite(s);
3448
3449 p = s;
3450 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3451 {
3452 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003453 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003454 need_clr_eos = FALSE;
3455 rettv->v_type = VAR_NUMBER;
3456 rettv->vval.v_number = 0;
3457 }
3458 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003459 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003460}
3461
3462/*
3463 * "eventhandler()" function
3464 */
3465 static void
3466f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3467{
3468 rettv->vval.v_number = vgetc_busy;
3469}
3470
3471/*
3472 * "executable()" function
3473 */
3474 static void
3475f_executable(typval_T *argvars, typval_T *rettv)
3476{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003477 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003478
3479 /* Check in $PATH and also check directly if there is a directory name. */
3480 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3481 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3482}
3483
3484static garray_T redir_execute_ga;
3485
3486/*
3487 * Append "value[value_len]" to the execute() output.
3488 */
3489 void
3490execute_redir_str(char_u *value, int value_len)
3491{
3492 int len;
3493
3494 if (value_len == -1)
3495 len = (int)STRLEN(value); /* Append the entire string */
3496 else
3497 len = value_len; /* Append only "value_len" characters */
3498 if (ga_grow(&redir_execute_ga, len) == OK)
3499 {
3500 mch_memmove((char *)redir_execute_ga.ga_data
3501 + redir_execute_ga.ga_len, value, len);
3502 redir_execute_ga.ga_len += len;
3503 }
3504}
3505
3506/*
3507 * Get next line from a list.
3508 * Called by do_cmdline() to get the next line.
3509 * Returns allocated string, or NULL for end of function.
3510 */
3511
3512 static char_u *
3513get_list_line(
3514 int c UNUSED,
3515 void *cookie,
3516 int indent UNUSED)
3517{
3518 listitem_T **p = (listitem_T **)cookie;
3519 listitem_T *item = *p;
3520 char_u buf[NUMBUFLEN];
3521 char_u *s;
3522
3523 if (item == NULL)
3524 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003525 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526 *p = item->li_next;
3527 return s == NULL ? NULL : vim_strsave(s);
3528}
3529
3530/*
3531 * "execute()" function
3532 */
3533 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003534execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535{
3536 char_u *cmd = NULL;
3537 list_T *list = NULL;
3538 int save_msg_silent = msg_silent;
3539 int save_emsg_silent = emsg_silent;
3540 int save_emsg_noredir = emsg_noredir;
3541 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003542 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003544 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003545 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003546
3547 rettv->vval.v_string = NULL;
3548 rettv->v_type = VAR_STRING;
3549
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003550 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003552 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 if (list == NULL || list->lv_first == NULL)
3554 /* empty list, no commands, empty output */
3555 return;
3556 ++list->lv_refcount;
3557 }
3558 else
3559 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003560 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (cmd == NULL)
3562 return;
3563 }
3564
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003565 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 {
3567 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003568 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003569
3570 if (s == NULL)
3571 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003572 if (*s == NUL)
3573 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574 if (STRNCMP(s, "silent", 6) == 0)
3575 ++msg_silent;
3576 if (STRCMP(s, "silent!") == 0)
3577 {
3578 emsg_silent = TRUE;
3579 emsg_noredir = TRUE;
3580 }
3581 }
3582 else
3583 ++msg_silent;
3584
3585 if (redir_execute)
3586 save_ga = redir_execute_ga;
3587 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3588 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003589 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003590 if (!echo_output)
3591 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003592
3593 if (cmd != NULL)
3594 do_cmdline_cmd(cmd);
3595 else
3596 {
3597 listitem_T *item = list->lv_first;
3598
3599 do_cmdline(NULL, get_list_line, (void *)&item,
3600 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3601 --list->lv_refcount;
3602 }
3603
Bram Moolenaard297f352017-01-29 20:31:21 +01003604 /* Need to append a NUL to the result. */
3605 if (ga_grow(&redir_execute_ga, 1) == OK)
3606 {
3607 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3608 rettv->vval.v_string = redir_execute_ga.ga_data;
3609 }
3610 else
3611 {
3612 ga_clear(&redir_execute_ga);
3613 rettv->vval.v_string = NULL;
3614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615 msg_silent = save_msg_silent;
3616 emsg_silent = save_emsg_silent;
3617 emsg_noredir = save_emsg_noredir;
3618
3619 redir_execute = save_redir_execute;
3620 if (redir_execute)
3621 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003622 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003624 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003625 if (echo_output)
3626 // When not working silently: put it in column zero. A following
3627 // "echon" will overwrite the message, unavoidably.
3628 msg_col = 0;
3629 else
3630 // When working silently: Put it back where it was, since nothing
3631 // should have been written.
3632 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003633}
3634
3635/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003636 * "execute()" function
3637 */
3638 static void
3639f_execute(typval_T *argvars, typval_T *rettv)
3640{
3641 execute_common(argvars, rettv, 0);
3642}
3643
3644/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 * "exepath()" function
3646 */
3647 static void
3648f_exepath(typval_T *argvars, typval_T *rettv)
3649{
3650 char_u *p = NULL;
3651
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003652 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653 rettv->v_type = VAR_STRING;
3654 rettv->vval.v_string = p;
3655}
3656
3657/*
3658 * "exists()" function
3659 */
3660 static void
3661f_exists(typval_T *argvars, typval_T *rettv)
3662{
3663 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003664 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003666 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667 if (*p == '$') /* environment variable */
3668 {
3669 /* first try "normal" environment variables (fast) */
3670 if (mch_getenv(p + 1) != NULL)
3671 n = TRUE;
3672 else
3673 {
3674 /* try expanding things like $VIM and ${HOME} */
3675 p = expand_env_save(p);
3676 if (p != NULL && *p != '$')
3677 n = TRUE;
3678 vim_free(p);
3679 }
3680 }
3681 else if (*p == '&' || *p == '+') /* option */
3682 {
3683 n = (get_option_tv(&p, NULL, TRUE) == OK);
3684 if (*skipwhite(p) != NUL)
3685 n = FALSE; /* trailing garbage */
3686 }
3687 else if (*p == '*') /* internal or user defined function */
3688 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003689 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003690 }
3691 else if (*p == ':')
3692 {
3693 n = cmd_exists(p + 1);
3694 }
3695 else if (*p == '#')
3696 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 if (p[1] == '#')
3698 n = autocmd_supported(p + 2);
3699 else
3700 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003701 }
3702 else /* internal variable */
3703 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003704 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003705 }
3706
3707 rettv->vval.v_number = n;
3708}
3709
3710#ifdef FEAT_FLOAT
3711/*
3712 * "exp()" function
3713 */
3714 static void
3715f_exp(typval_T *argvars, typval_T *rettv)
3716{
3717 float_T f = 0.0;
3718
3719 rettv->v_type = VAR_FLOAT;
3720 if (get_float_arg(argvars, &f) == OK)
3721 rettv->vval.v_float = exp(f);
3722 else
3723 rettv->vval.v_float = 0.0;
3724}
3725#endif
3726
3727/*
3728 * "expand()" function
3729 */
3730 static void
3731f_expand(typval_T *argvars, typval_T *rettv)
3732{
3733 char_u *s;
3734 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003735 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003736 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3737 expand_T xpc;
3738 int error = FALSE;
3739 char_u *result;
3740
3741 rettv->v_type = VAR_STRING;
3742 if (argvars[1].v_type != VAR_UNKNOWN
3743 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003744 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003745 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003746 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 if (*s == '%' || *s == '#' || *s == '<')
3750 {
3751 ++emsg_off;
3752 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3753 --emsg_off;
3754 if (rettv->v_type == VAR_LIST)
3755 {
3756 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3757 list_append_string(rettv->vval.v_list, result, -1);
3758 else
3759 vim_free(result);
3760 }
3761 else
3762 rettv->vval.v_string = result;
3763 }
3764 else
3765 {
3766 /* When the optional second argument is non-zero, don't remove matches
3767 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3768 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003769 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003770 options |= WILD_KEEP_ALL;
3771 if (!error)
3772 {
3773 ExpandInit(&xpc);
3774 xpc.xp_context = EXPAND_FILES;
3775 if (p_wic)
3776 options += WILD_ICASE;
3777 if (rettv->v_type == VAR_STRING)
3778 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3779 options, WILD_ALL);
3780 else if (rettv_list_alloc(rettv) != FAIL)
3781 {
3782 int i;
3783
3784 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3785 for (i = 0; i < xpc.xp_numfiles; i++)
3786 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3787 ExpandCleanup(&xpc);
3788 }
3789 }
3790 else
3791 rettv->vval.v_string = NULL;
3792 }
3793}
3794
3795/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003796 * "expandcmd()" function
3797 * Expand all the special characters in a command string.
3798 */
3799 static void
3800f_expandcmd(typval_T *argvars, typval_T *rettv)
3801{
3802 exarg_T eap;
3803 char_u *cmdstr;
3804 char *errormsg = NULL;
3805
3806 rettv->v_type = VAR_STRING;
3807 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3808
3809 memset(&eap, 0, sizeof(eap));
3810 eap.cmd = cmdstr;
3811 eap.arg = cmdstr;
3812 eap.argt |= NOSPC;
3813 eap.usefilter = FALSE;
3814 eap.nextcmd = NULL;
3815 eap.cmdidx = CMD_USER;
3816
3817 expand_filename(&eap, &cmdstr, &errormsg);
3818 if (errormsg != NULL && *errormsg != NUL)
3819 emsg(errormsg);
3820
3821 rettv->vval.v_string = cmdstr;
3822}
3823
3824/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003825 * "extend(list, list [, idx])" function
3826 * "extend(dict, dict [, action])" function
3827 */
3828 static void
3829f_extend(typval_T *argvars, typval_T *rettv)
3830{
3831 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3832
3833 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3834 {
3835 list_T *l1, *l2;
3836 listitem_T *item;
3837 long before;
3838 int error = FALSE;
3839
3840 l1 = argvars[0].vval.v_list;
3841 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003842 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003843 && l2 != NULL)
3844 {
3845 if (argvars[2].v_type != VAR_UNKNOWN)
3846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003847 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848 if (error)
3849 return; /* type error; errmsg already given */
3850
3851 if (before == l1->lv_len)
3852 item = NULL;
3853 else
3854 {
3855 item = list_find(l1, before);
3856 if (item == NULL)
3857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003858 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003859 return;
3860 }
3861 }
3862 }
3863 else
3864 item = NULL;
3865 list_extend(l1, l2, item);
3866
3867 copy_tv(&argvars[0], rettv);
3868 }
3869 }
3870 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3871 {
3872 dict_T *d1, *d2;
3873 char_u *action;
3874 int i;
3875
3876 d1 = argvars[0].vval.v_dict;
3877 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003878 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 && d2 != NULL)
3880 {
3881 /* Check the third argument. */
3882 if (argvars[2].v_type != VAR_UNKNOWN)
3883 {
3884 static char *(av[]) = {"keep", "force", "error"};
3885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003886 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 if (action == NULL)
3888 return; /* type error; errmsg already given */
3889 for (i = 0; i < 3; ++i)
3890 if (STRCMP(action, av[i]) == 0)
3891 break;
3892 if (i == 3)
3893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003894 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003895 return;
3896 }
3897 }
3898 else
3899 action = (char_u *)"force";
3900
3901 dict_extend(d1, d2, action);
3902
3903 copy_tv(&argvars[0], rettv);
3904 }
3905 }
3906 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003907 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003908}
3909
3910/*
3911 * "feedkeys()" function
3912 */
3913 static void
3914f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3915{
3916 int remap = TRUE;
3917 int insert = FALSE;
3918 char_u *keys, *flags;
3919 char_u nbuf[NUMBUFLEN];
3920 int typed = FALSE;
3921 int execute = FALSE;
3922 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003923 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003924 char_u *keys_esc;
3925
3926 /* This is not allowed in the sandbox. If the commands would still be
3927 * executed in the sandbox it would be OK, but it probably happens later,
3928 * when "sandbox" is no longer set. */
3929 if (check_secure())
3930 return;
3931
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003932 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933
3934 if (argvars[1].v_type != VAR_UNKNOWN)
3935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003936 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003937 for ( ; *flags != NUL; ++flags)
3938 {
3939 switch (*flags)
3940 {
3941 case 'n': remap = FALSE; break;
3942 case 'm': remap = TRUE; break;
3943 case 't': typed = TRUE; break;
3944 case 'i': insert = TRUE; break;
3945 case 'x': execute = TRUE; break;
3946 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003947 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003948 }
3949 }
3950 }
3951
3952 if (*keys != NUL || execute)
3953 {
3954 /* Need to escape K_SPECIAL and CSI before putting the string in the
3955 * typeahead buffer. */
3956 keys_esc = vim_strsave_escape_csi(keys);
3957 if (keys_esc != NULL)
3958 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003959 if (lowlevel)
3960 {
3961#ifdef USE_INPUT_BUF
3962 add_to_input_buf(keys, (int)STRLEN(keys));
3963#else
3964 emsg(_("E980: lowlevel input not supported"));
3965#endif
3966 }
3967 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003968 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003969 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003970 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003971 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003972#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003973 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003974#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003975 )
3976 typebuf_was_filled = TRUE;
3977 }
3978 vim_free(keys_esc);
3979
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003980 if (execute)
3981 {
3982 int save_msg_scroll = msg_scroll;
3983
3984 /* Avoid a 1 second delay when the keys start Insert mode. */
3985 msg_scroll = FALSE;
3986
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003987 if (!dangerous)
3988 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003989 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003990 if (!dangerous)
3991 --ex_normal_busy;
3992
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003993 msg_scroll |= save_msg_scroll;
3994 }
3995 }
3996 }
3997}
3998
3999/*
4000 * "filereadable()" function
4001 */
4002 static void
4003f_filereadable(typval_T *argvars, typval_T *rettv)
4004{
4005 int fd;
4006 char_u *p;
4007 int n;
4008
4009#ifndef O_NONBLOCK
4010# define O_NONBLOCK 0
4011#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004012 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004013 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4014 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4015 {
4016 n = TRUE;
4017 close(fd);
4018 }
4019 else
4020 n = FALSE;
4021
4022 rettv->vval.v_number = n;
4023}
4024
4025/*
4026 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4027 * rights to write into.
4028 */
4029 static void
4030f_filewritable(typval_T *argvars, typval_T *rettv)
4031{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004032 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033}
4034
4035 static void
4036findfilendir(
4037 typval_T *argvars UNUSED,
4038 typval_T *rettv,
4039 int find_what UNUSED)
4040{
4041#ifdef FEAT_SEARCHPATH
4042 char_u *fname;
4043 char_u *fresult = NULL;
4044 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4045 char_u *p;
4046 char_u pathbuf[NUMBUFLEN];
4047 int count = 1;
4048 int first = TRUE;
4049 int error = FALSE;
4050#endif
4051
4052 rettv->vval.v_string = NULL;
4053 rettv->v_type = VAR_STRING;
4054
4055#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004056 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004057
4058 if (argvars[1].v_type != VAR_UNKNOWN)
4059 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004060 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004061 if (p == NULL)
4062 error = TRUE;
4063 else
4064 {
4065 if (*p != NUL)
4066 path = p;
4067
4068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004069 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004070 }
4071 }
4072
4073 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4074 error = TRUE;
4075
4076 if (*fname != NUL && !error)
4077 {
4078 do
4079 {
4080 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4081 vim_free(fresult);
4082 fresult = find_file_in_path_option(first ? fname : NULL,
4083 first ? (int)STRLEN(fname) : 0,
4084 0, first, path,
4085 find_what,
4086 curbuf->b_ffname,
4087 find_what == FINDFILE_DIR
4088 ? (char_u *)"" : curbuf->b_p_sua);
4089 first = FALSE;
4090
4091 if (fresult != NULL && rettv->v_type == VAR_LIST)
4092 list_append_string(rettv->vval.v_list, fresult, -1);
4093
4094 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4095 }
4096
4097 if (rettv->v_type == VAR_STRING)
4098 rettv->vval.v_string = fresult;
4099#endif
4100}
4101
4102/*
4103 * "filter()" function
4104 */
4105 static void
4106f_filter(typval_T *argvars, typval_T *rettv)
4107{
4108 filter_map(argvars, rettv, FALSE);
4109}
4110
4111/*
4112 * "finddir({fname}[, {path}[, {count}]])" function
4113 */
4114 static void
4115f_finddir(typval_T *argvars, typval_T *rettv)
4116{
4117 findfilendir(argvars, rettv, FINDFILE_DIR);
4118}
4119
4120/*
4121 * "findfile({fname}[, {path}[, {count}]])" function
4122 */
4123 static void
4124f_findfile(typval_T *argvars, typval_T *rettv)
4125{
4126 findfilendir(argvars, rettv, FINDFILE_FILE);
4127}
4128
4129#ifdef FEAT_FLOAT
4130/*
4131 * "float2nr({float})" function
4132 */
4133 static void
4134f_float2nr(typval_T *argvars, typval_T *rettv)
4135{
4136 float_T f = 0.0;
4137
4138 if (get_float_arg(argvars, &f) == OK)
4139 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004140 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004141 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004142 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004143 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004144 else
4145 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 }
4147}
4148
4149/*
4150 * "floor({float})" function
4151 */
4152 static void
4153f_floor(typval_T *argvars, typval_T *rettv)
4154{
4155 float_T f = 0.0;
4156
4157 rettv->v_type = VAR_FLOAT;
4158 if (get_float_arg(argvars, &f) == OK)
4159 rettv->vval.v_float = floor(f);
4160 else
4161 rettv->vval.v_float = 0.0;
4162}
4163
4164/*
4165 * "fmod()" function
4166 */
4167 static void
4168f_fmod(typval_T *argvars, typval_T *rettv)
4169{
4170 float_T fx = 0.0, fy = 0.0;
4171
4172 rettv->v_type = VAR_FLOAT;
4173 if (get_float_arg(argvars, &fx) == OK
4174 && get_float_arg(&argvars[1], &fy) == OK)
4175 rettv->vval.v_float = fmod(fx, fy);
4176 else
4177 rettv->vval.v_float = 0.0;
4178}
4179#endif
4180
4181/*
4182 * "fnameescape({string})" function
4183 */
4184 static void
4185f_fnameescape(typval_T *argvars, typval_T *rettv)
4186{
4187 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004188 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004189 rettv->v_type = VAR_STRING;
4190}
4191
4192/*
4193 * "fnamemodify({fname}, {mods})" function
4194 */
4195 static void
4196f_fnamemodify(typval_T *argvars, typval_T *rettv)
4197{
4198 char_u *fname;
4199 char_u *mods;
4200 int usedlen = 0;
4201 int len;
4202 char_u *fbuf = NULL;
4203 char_u buf[NUMBUFLEN];
4204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004205 fname = tv_get_string_chk(&argvars[0]);
4206 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004207 if (fname == NULL || mods == NULL)
4208 fname = NULL;
4209 else
4210 {
4211 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004212 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004213 }
4214
4215 rettv->v_type = VAR_STRING;
4216 if (fname == NULL)
4217 rettv->vval.v_string = NULL;
4218 else
4219 rettv->vval.v_string = vim_strnsave(fname, len);
4220 vim_free(fbuf);
4221}
4222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004223/*
4224 * "foldclosed()" function
4225 */
4226 static void
4227foldclosed_both(
4228 typval_T *argvars UNUSED,
4229 typval_T *rettv,
4230 int end UNUSED)
4231{
4232#ifdef FEAT_FOLDING
4233 linenr_T lnum;
4234 linenr_T first, last;
4235
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004236 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004237 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4238 {
4239 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4240 {
4241 if (end)
4242 rettv->vval.v_number = (varnumber_T)last;
4243 else
4244 rettv->vval.v_number = (varnumber_T)first;
4245 return;
4246 }
4247 }
4248#endif
4249 rettv->vval.v_number = -1;
4250}
4251
4252/*
4253 * "foldclosed()" function
4254 */
4255 static void
4256f_foldclosed(typval_T *argvars, typval_T *rettv)
4257{
4258 foldclosed_both(argvars, rettv, FALSE);
4259}
4260
4261/*
4262 * "foldclosedend()" function
4263 */
4264 static void
4265f_foldclosedend(typval_T *argvars, typval_T *rettv)
4266{
4267 foldclosed_both(argvars, rettv, TRUE);
4268}
4269
4270/*
4271 * "foldlevel()" function
4272 */
4273 static void
4274f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4275{
4276#ifdef FEAT_FOLDING
4277 linenr_T lnum;
4278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004279 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004280 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4281 rettv->vval.v_number = foldLevel(lnum);
4282#endif
4283}
4284
4285/*
4286 * "foldtext()" function
4287 */
4288 static void
4289f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4290{
4291#ifdef FEAT_FOLDING
4292 linenr_T foldstart;
4293 linenr_T foldend;
4294 char_u *dashes;
4295 linenr_T lnum;
4296 char_u *s;
4297 char_u *r;
4298 int len;
4299 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004300 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301#endif
4302
4303 rettv->v_type = VAR_STRING;
4304 rettv->vval.v_string = NULL;
4305#ifdef FEAT_FOLDING
4306 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4307 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4308 dashes = get_vim_var_str(VV_FOLDDASHES);
4309 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4310 && dashes != NULL)
4311 {
4312 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004313 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004314 if (!linewhite(lnum))
4315 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004316
4317 /* Find interesting text in this line. */
4318 s = skipwhite(ml_get(lnum));
4319 /* skip C comment-start */
4320 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4321 {
4322 s = skipwhite(s + 2);
4323 if (*skipwhite(s) == NUL
4324 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4325 {
4326 s = skipwhite(ml_get(lnum + 1));
4327 if (*s == '*')
4328 s = skipwhite(s + 1);
4329 }
4330 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004331 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004332 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004333 r = alloc(STRLEN(txt)
4334 + STRLEN(dashes) // for %s
4335 + 20 // for %3ld
4336 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004337 if (r != NULL)
4338 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004339 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004340 len = (int)STRLEN(r);
4341 STRCAT(r, s);
4342 /* remove 'foldmarker' and 'commentstring' */
4343 foldtext_cleanup(r + len);
4344 rettv->vval.v_string = r;
4345 }
4346 }
4347#endif
4348}
4349
4350/*
4351 * "foldtextresult(lnum)" function
4352 */
4353 static void
4354f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4355{
4356#ifdef FEAT_FOLDING
4357 linenr_T lnum;
4358 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004359 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360 foldinfo_T foldinfo;
4361 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004362 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004363#endif
4364
4365 rettv->v_type = VAR_STRING;
4366 rettv->vval.v_string = NULL;
4367#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004368 if (entered)
4369 return; /* reject recursive use */
4370 entered = TRUE;
4371
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004372 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373 /* treat illegal types and illegal string values for {lnum} the same */
4374 if (lnum < 0)
4375 lnum = 0;
4376 fold_count = foldedCount(curwin, lnum, &foldinfo);
4377 if (fold_count > 0)
4378 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004379 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4380 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004381 if (text == buf)
4382 text = vim_strsave(text);
4383 rettv->vval.v_string = text;
4384 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004385
4386 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004387#endif
4388}
4389
4390/*
4391 * "foreground()" function
4392 */
4393 static void
4394f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4395{
4396#ifdef FEAT_GUI
4397 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004398 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004399 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004400 return;
4401 }
4402#endif
4403#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405#endif
4406}
4407
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004408 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004409common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410{
4411 char_u *s;
4412 char_u *name;
4413 int use_string = FALSE;
4414 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004415 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416
4417 if (argvars[0].v_type == VAR_FUNC)
4418 {
4419 /* function(MyFunc, [arg], dict) */
4420 s = argvars[0].vval.v_string;
4421 }
4422 else if (argvars[0].v_type == VAR_PARTIAL
4423 && argvars[0].vval.v_partial != NULL)
4424 {
4425 /* function(dict.MyFunc, [arg]) */
4426 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004427 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004428 }
4429 else
4430 {
4431 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004432 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 use_string = TRUE;
4434 }
4435
Bram Moolenaar843b8842016-08-21 14:36:15 +02004436 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004437 {
4438 name = s;
4439 trans_name = trans_function_name(&name, FALSE,
4440 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4441 if (*name != NUL)
4442 s = NULL;
4443 }
4444
Bram Moolenaar843b8842016-08-21 14:36:15 +02004445 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4446 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004447 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 else if (trans_name != NULL && (is_funcref
4450 ? find_func(trans_name) == NULL
4451 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004452 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453 else
4454 {
4455 int dict_idx = 0;
4456 int arg_idx = 0;
4457 list_T *list = NULL;
4458
4459 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4460 {
4461 char sid_buf[25];
4462 int off = *s == 's' ? 2 : 5;
4463
4464 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4465 * also be called from another script. Using trans_function_name()
4466 * would also work, but some plugins depend on the name being
4467 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004468 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004469 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004470 if (name != NULL)
4471 {
4472 STRCPY(name, sid_buf);
4473 STRCAT(name, s + off);
4474 }
4475 }
4476 else
4477 name = vim_strsave(s);
4478
4479 if (argvars[1].v_type != VAR_UNKNOWN)
4480 {
4481 if (argvars[2].v_type != VAR_UNKNOWN)
4482 {
4483 /* function(name, [args], dict) */
4484 arg_idx = 1;
4485 dict_idx = 2;
4486 }
4487 else if (argvars[1].v_type == VAR_DICT)
4488 /* function(name, dict) */
4489 dict_idx = 1;
4490 else
4491 /* function(name, [args]) */
4492 arg_idx = 1;
4493 if (dict_idx > 0)
4494 {
4495 if (argvars[dict_idx].v_type != VAR_DICT)
4496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004497 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004498 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004499 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 }
4501 if (argvars[dict_idx].vval.v_dict == NULL)
4502 dict_idx = 0;
4503 }
4504 if (arg_idx > 0)
4505 {
4506 if (argvars[arg_idx].v_type != VAR_LIST)
4507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004508 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004509 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004510 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 }
4512 list = argvars[arg_idx].vval.v_list;
4513 if (list == NULL || list->lv_len == 0)
4514 arg_idx = 0;
4515 }
4516 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004517 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004519 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520
4521 /* result is a VAR_PARTIAL */
4522 if (pt == NULL)
4523 vim_free(name);
4524 else
4525 {
4526 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4527 {
4528 listitem_T *li;
4529 int i = 0;
4530 int arg_len = 0;
4531 int lv_len = 0;
4532
4533 if (arg_pt != NULL)
4534 arg_len = arg_pt->pt_argc;
4535 if (list != NULL)
4536 lv_len = list->lv_len;
4537 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004538 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539 if (pt->pt_argv == NULL)
4540 {
4541 vim_free(pt);
4542 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004543 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004545 for (i = 0; i < arg_len; i++)
4546 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4547 if (lv_len > 0)
4548 for (li = list->lv_first; li != NULL;
4549 li = li->li_next)
4550 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551 }
4552
4553 /* For "function(dict.func, [], dict)" and "func" is a partial
4554 * use "dict". That is backwards compatible. */
4555 if (dict_idx > 0)
4556 {
4557 /* The dict is bound explicitly, pt_auto is FALSE. */
4558 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4559 ++pt->pt_dict->dv_refcount;
4560 }
4561 else if (arg_pt != NULL)
4562 {
4563 /* If the dict was bound automatically the result is also
4564 * bound automatically. */
4565 pt->pt_dict = arg_pt->pt_dict;
4566 pt->pt_auto = arg_pt->pt_auto;
4567 if (pt->pt_dict != NULL)
4568 ++pt->pt_dict->dv_refcount;
4569 }
4570
4571 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004572 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4573 {
4574 pt->pt_func = arg_pt->pt_func;
4575 func_ptr_ref(pt->pt_func);
4576 vim_free(name);
4577 }
4578 else if (is_funcref)
4579 {
4580 pt->pt_func = find_func(trans_name);
4581 func_ptr_ref(pt->pt_func);
4582 vim_free(name);
4583 }
4584 else
4585 {
4586 pt->pt_name = name;
4587 func_ref(name);
4588 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004589 }
4590 rettv->v_type = VAR_PARTIAL;
4591 rettv->vval.v_partial = pt;
4592 }
4593 else
4594 {
4595 /* result is a VAR_FUNC */
4596 rettv->v_type = VAR_FUNC;
4597 rettv->vval.v_string = name;
4598 func_ref(name);
4599 }
4600 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004601theend:
4602 vim_free(trans_name);
4603}
4604
4605/*
4606 * "funcref()" function
4607 */
4608 static void
4609f_funcref(typval_T *argvars, typval_T *rettv)
4610{
4611 common_function(argvars, rettv, TRUE);
4612}
4613
4614/*
4615 * "function()" function
4616 */
4617 static void
4618f_function(typval_T *argvars, typval_T *rettv)
4619{
4620 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004621}
4622
4623/*
4624 * "garbagecollect()" function
4625 */
4626 static void
4627f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4628{
4629 /* This is postponed until we are back at the toplevel, because we may be
4630 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4631 want_garbage_collect = TRUE;
4632
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004633 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634 garbage_collect_at_exit = TRUE;
4635}
4636
4637/*
4638 * "get()" function
4639 */
4640 static void
4641f_get(typval_T *argvars, typval_T *rettv)
4642{
4643 listitem_T *li;
4644 list_T *l;
4645 dictitem_T *di;
4646 dict_T *d;
4647 typval_T *tv = NULL;
4648
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004649 if (argvars[0].v_type == VAR_BLOB)
4650 {
4651 int error = FALSE;
4652 int idx = tv_get_number_chk(&argvars[1], &error);
4653
4654 if (!error)
4655 {
4656 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004657 if (idx < 0)
4658 idx = blob_len(argvars[0].vval.v_blob) + idx;
4659 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4660 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004661 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004662 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004663 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004664 tv = rettv;
4665 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004666 }
4667 }
4668 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 {
4670 if ((l = argvars[0].vval.v_list) != NULL)
4671 {
4672 int error = FALSE;
4673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004674 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 if (!error && li != NULL)
4676 tv = &li->li_tv;
4677 }
4678 }
4679 else if (argvars[0].v_type == VAR_DICT)
4680 {
4681 if ((d = argvars[0].vval.v_dict) != NULL)
4682 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004683 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 if (di != NULL)
4685 tv = &di->di_tv;
4686 }
4687 }
4688 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4689 {
4690 partial_T *pt;
4691 partial_T fref_pt;
4692
4693 if (argvars[0].v_type == VAR_PARTIAL)
4694 pt = argvars[0].vval.v_partial;
4695 else
4696 {
4697 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4698 fref_pt.pt_name = argvars[0].vval.v_string;
4699 pt = &fref_pt;
4700 }
4701
4702 if (pt != NULL)
4703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004704 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004705 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004706
4707 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4708 {
4709 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004710 n = partial_name(pt);
4711 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004712 rettv->vval.v_string = NULL;
4713 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004714 {
4715 rettv->vval.v_string = vim_strsave(n);
4716 if (rettv->v_type == VAR_FUNC)
4717 func_ref(rettv->vval.v_string);
4718 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 }
4720 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004721 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004722 else if (STRCMP(what, "args") == 0)
4723 {
4724 rettv->v_type = VAR_LIST;
4725 if (rettv_list_alloc(rettv) == OK)
4726 {
4727 int i;
4728
4729 for (i = 0; i < pt->pt_argc; ++i)
4730 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4731 }
4732 }
4733 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004734 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004735 return;
4736 }
4737 }
4738 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004739 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004740
4741 if (tv == NULL)
4742 {
4743 if (argvars[2].v_type != VAR_UNKNOWN)
4744 copy_tv(&argvars[2], rettv);
4745 }
4746 else
4747 copy_tv(tv, rettv);
4748}
4749
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004750/*
4751 * Returns buffer options, variables and other attributes in a dictionary.
4752 */
4753 static dict_T *
4754get_buffer_info(buf_T *buf)
4755{
4756 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004757 tabpage_T *tp;
4758 win_T *wp;
4759 list_T *windows;
4760
4761 dict = dict_alloc();
4762 if (dict == NULL)
4763 return NULL;
4764
Bram Moolenaare0be1672018-07-08 16:50:37 +02004765 dict_add_number(dict, "bufnr", buf->b_fnum);
4766 dict_add_string(dict, "name", buf->b_ffname);
4767 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4768 : buflist_findlnum(buf));
4769 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4770 dict_add_number(dict, "listed", buf->b_p_bl);
4771 dict_add_number(dict, "changed", bufIsChanged(buf));
4772 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4773 dict_add_number(dict, "hidden",
4774 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004775
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004776 /* Get a reference to buffer variables */
4777 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004778
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004779 /* List of windows displaying this buffer */
4780 windows = list_alloc();
4781 if (windows != NULL)
4782 {
4783 FOR_ALL_TAB_WINDOWS(tp, wp)
4784 if (wp->w_buffer == buf)
4785 list_append_number(windows, (varnumber_T)wp->w_id);
4786 dict_add_list(dict, "windows", windows);
4787 }
4788
4789#ifdef FEAT_SIGNS
4790 if (buf->b_signlist != NULL)
4791 {
4792 /* List of signs placed in this buffer */
4793 list_T *signs = list_alloc();
4794 if (signs != NULL)
4795 {
4796 get_buffer_signs(buf, signs);
4797 dict_add_list(dict, "signs", signs);
4798 }
4799 }
4800#endif
4801
4802 return dict;
4803}
4804
4805/*
4806 * "getbufinfo()" function
4807 */
4808 static void
4809f_getbufinfo(typval_T *argvars, typval_T *rettv)
4810{
4811 buf_T *buf = NULL;
4812 buf_T *argbuf = NULL;
4813 dict_T *d;
4814 int filtered = FALSE;
4815 int sel_buflisted = FALSE;
4816 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004817 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004818
4819 if (rettv_list_alloc(rettv) != OK)
4820 return;
4821
4822 /* List of all the buffers or selected buffers */
4823 if (argvars[0].v_type == VAR_DICT)
4824 {
4825 dict_T *sel_d = argvars[0].vval.v_dict;
4826
4827 if (sel_d != NULL)
4828 {
4829 dictitem_T *di;
4830
4831 filtered = TRUE;
4832
4833 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004834 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004835 sel_buflisted = TRUE;
4836
4837 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004838 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004839 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004840
4841 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004842 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004843 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004844 }
4845 }
4846 else if (argvars[0].v_type != VAR_UNKNOWN)
4847 {
4848 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004850 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004851 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004852 --emsg_off;
4853 if (argbuf == NULL)
4854 return;
4855 }
4856
4857 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004858 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004859 {
4860 if (argbuf != NULL && argbuf != buf)
4861 continue;
4862 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004863 || (sel_buflisted && !buf->b_p_bl)
4864 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004865 continue;
4866
4867 d = get_buffer_info(buf);
4868 if (d != NULL)
4869 list_append_dict(rettv->vval.v_list, d);
4870 if (argbuf != NULL)
4871 return;
4872 }
4873}
4874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004875/*
4876 * Get line or list of lines from buffer "buf" into "rettv".
4877 * Return a range (from start to end) of lines in rettv from the specified
4878 * buffer.
4879 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4880 */
4881 static void
4882get_buffer_lines(
4883 buf_T *buf,
4884 linenr_T start,
4885 linenr_T end,
4886 int retlist,
4887 typval_T *rettv)
4888{
4889 char_u *p;
4890
4891 rettv->v_type = VAR_STRING;
4892 rettv->vval.v_string = NULL;
4893 if (retlist && rettv_list_alloc(rettv) == FAIL)
4894 return;
4895
4896 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4897 return;
4898
4899 if (!retlist)
4900 {
4901 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4902 p = ml_get_buf(buf, start, FALSE);
4903 else
4904 p = (char_u *)"";
4905 rettv->vval.v_string = vim_strsave(p);
4906 }
4907 else
4908 {
4909 if (end < start)
4910 return;
4911
4912 if (start < 1)
4913 start = 1;
4914 if (end > buf->b_ml.ml_line_count)
4915 end = buf->b_ml.ml_line_count;
4916 while (start <= end)
4917 if (list_append_string(rettv->vval.v_list,
4918 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4919 break;
4920 }
4921}
4922
4923/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 * "getbufline()" function
4925 */
4926 static void
4927f_getbufline(typval_T *argvars, typval_T *rettv)
4928{
4929 linenr_T lnum;
4930 linenr_T end;
4931 buf_T *buf;
4932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004933 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004934 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004935 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 --emsg_off;
4937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004938 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 if (argvars[2].v_type == VAR_UNKNOWN)
4940 end = lnum;
4941 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004942 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943
4944 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4945}
4946
4947/*
4948 * "getbufvar()" function
4949 */
4950 static void
4951f_getbufvar(typval_T *argvars, typval_T *rettv)
4952{
4953 buf_T *buf;
4954 buf_T *save_curbuf;
4955 char_u *varname;
4956 dictitem_T *v;
4957 int done = FALSE;
4958
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004959 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4960 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004961 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004962 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963
4964 rettv->v_type = VAR_STRING;
4965 rettv->vval.v_string = NULL;
4966
4967 if (buf != NULL && varname != NULL)
4968 {
4969 /* set curbuf to be our buf, temporarily */
4970 save_curbuf = curbuf;
4971 curbuf = buf;
4972
Bram Moolenaar30567352016-08-27 21:25:44 +02004973 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004974 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004975 if (varname[1] == NUL)
4976 {
4977 /* get all buffer-local options in a dict */
4978 dict_T *opts = get_winbuf_options(TRUE);
4979
4980 if (opts != NULL)
4981 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004982 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004983 done = TRUE;
4984 }
4985 }
4986 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4987 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004988 done = TRUE;
4989 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004990 else
4991 {
4992 /* Look up the variable. */
4993 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4994 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4995 'b', varname, FALSE);
4996 if (v != NULL)
4997 {
4998 copy_tv(&v->di_tv, rettv);
4999 done = TRUE;
5000 }
5001 }
5002
5003 /* restore previous notion of curbuf */
5004 curbuf = save_curbuf;
5005 }
5006
5007 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5008 /* use the default value */
5009 copy_tv(&argvars[2], rettv);
5010
5011 --emsg_off;
5012}
5013
5014/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005015 * "getchangelist()" function
5016 */
5017 static void
5018f_getchangelist(typval_T *argvars, typval_T *rettv)
5019{
5020#ifdef FEAT_JUMPLIST
5021 buf_T *buf;
5022 int i;
5023 list_T *l;
5024 dict_T *d;
5025#endif
5026
5027 if (rettv_list_alloc(rettv) != OK)
5028 return;
5029
5030#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005031 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005032 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005033 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005034 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005035 if (buf == NULL)
5036 return;
5037
5038 l = list_alloc();
5039 if (l == NULL)
5040 return;
5041
5042 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5043 return;
5044 /*
5045 * The current window change list index tracks only the position in the
5046 * current buffer change list. For other buffers, use the change list
5047 * length as the current index.
5048 */
5049 list_append_number(rettv->vval.v_list,
5050 (varnumber_T)((buf == curwin->w_buffer)
5051 ? curwin->w_changelistidx : buf->b_changelistlen));
5052
5053 for (i = 0; i < buf->b_changelistlen; ++i)
5054 {
5055 if (buf->b_changelist[i].lnum == 0)
5056 continue;
5057 if ((d = dict_alloc()) == NULL)
5058 return;
5059 if (list_append_dict(l, d) == FAIL)
5060 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005061 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5062 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005063 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005064 }
5065#endif
5066}
5067/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005068 * "getchar()" function
5069 */
5070 static void
5071f_getchar(typval_T *argvars, typval_T *rettv)
5072{
5073 varnumber_T n;
5074 int error = FALSE;
5075
Bram Moolenaar84d93902018-09-11 20:10:20 +02005076#ifdef MESSAGE_QUEUE
5077 // vpeekc() used to check for messages, but that caused problems, invoking
5078 // a callback where it was not expected. Some plugins use getchar(1) in a
5079 // loop to await a message, therefore make sure we check for messages here.
5080 parse_queued_messages();
5081#endif
5082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 /* Position the cursor. Needed after a message that ends in a space. */
5084 windgoto(msg_row, msg_col);
5085
5086 ++no_mapping;
5087 ++allow_keys;
5088 for (;;)
5089 {
5090 if (argvars[0].v_type == VAR_UNKNOWN)
5091 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005092 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005093 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005094 /* getchar(1): only check if char avail */
5095 n = vpeekc_any();
5096 else if (error || vpeekc_any() == NUL)
5097 /* illegal argument or getchar(0) and no char avail: return zero */
5098 n = 0;
5099 else
5100 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005101 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102
5103 if (n == K_IGNORE)
5104 continue;
5105 break;
5106 }
5107 --no_mapping;
5108 --allow_keys;
5109
5110 set_vim_var_nr(VV_MOUSE_WIN, 0);
5111 set_vim_var_nr(VV_MOUSE_WINID, 0);
5112 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5113 set_vim_var_nr(VV_MOUSE_COL, 0);
5114
5115 rettv->vval.v_number = n;
5116 if (IS_SPECIAL(n) || mod_mask != 0)
5117 {
5118 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5119 int i = 0;
5120
5121 /* Turn a special key into three bytes, plus modifier. */
5122 if (mod_mask != 0)
5123 {
5124 temp[i++] = K_SPECIAL;
5125 temp[i++] = KS_MODIFIER;
5126 temp[i++] = mod_mask;
5127 }
5128 if (IS_SPECIAL(n))
5129 {
5130 temp[i++] = K_SPECIAL;
5131 temp[i++] = K_SECOND(n);
5132 temp[i++] = K_THIRD(n);
5133 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005134 else if (has_mbyte)
5135 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 else
5137 temp[i++] = n;
5138 temp[i++] = NUL;
5139 rettv->v_type = VAR_STRING;
5140 rettv->vval.v_string = vim_strsave(temp);
5141
5142#ifdef FEAT_MOUSE
5143 if (is_mouse_key(n))
5144 {
5145 int row = mouse_row;
5146 int col = mouse_col;
5147 win_T *win;
5148 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005149 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 int winnr = 1;
5151
5152 if (row >= 0 && col >= 0)
5153 {
5154 /* Find the window at the mouse coordinates and compute the
5155 * text position. */
5156 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005157 if (win == NULL)
5158 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005159 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 for (wp = firstwin; wp != win; wp = wp->w_next)
5161 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005162 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5163 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5164 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5165 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5166 }
5167 }
5168#endif
5169 }
5170}
5171
5172/*
5173 * "getcharmod()" function
5174 */
5175 static void
5176f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5177{
5178 rettv->vval.v_number = mod_mask;
5179}
5180
5181/*
5182 * "getcharsearch()" function
5183 */
5184 static void
5185f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5186{
5187 if (rettv_dict_alloc(rettv) != FAIL)
5188 {
5189 dict_T *dict = rettv->vval.v_dict;
5190
Bram Moolenaare0be1672018-07-08 16:50:37 +02005191 dict_add_string(dict, "char", last_csearch());
5192 dict_add_number(dict, "forward", last_csearch_forward());
5193 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005194 }
5195}
5196
5197/*
5198 * "getcmdline()" function
5199 */
5200 static void
5201f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5202{
5203 rettv->v_type = VAR_STRING;
5204 rettv->vval.v_string = get_cmdline_str();
5205}
5206
5207/*
5208 * "getcmdpos()" function
5209 */
5210 static void
5211f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5212{
5213 rettv->vval.v_number = get_cmdline_pos() + 1;
5214}
5215
5216/*
5217 * "getcmdtype()" function
5218 */
5219 static void
5220f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5221{
5222 rettv->v_type = VAR_STRING;
5223 rettv->vval.v_string = alloc(2);
5224 if (rettv->vval.v_string != NULL)
5225 {
5226 rettv->vval.v_string[0] = get_cmdline_type();
5227 rettv->vval.v_string[1] = NUL;
5228 }
5229}
5230
5231/*
5232 * "getcmdwintype()" function
5233 */
5234 static void
5235f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5236{
5237 rettv->v_type = VAR_STRING;
5238 rettv->vval.v_string = NULL;
5239#ifdef FEAT_CMDWIN
5240 rettv->vval.v_string = alloc(2);
5241 if (rettv->vval.v_string != NULL)
5242 {
5243 rettv->vval.v_string[0] = cmdwin_type;
5244 rettv->vval.v_string[1] = NUL;
5245 }
5246#endif
5247}
5248
5249#if defined(FEAT_CMDL_COMPL)
5250/*
5251 * "getcompletion()" function
5252 */
5253 static void
5254f_getcompletion(typval_T *argvars, typval_T *rettv)
5255{
5256 char_u *pat;
5257 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005258 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005259 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5260 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005262 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005263 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005264
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005265 if (p_wic)
5266 options |= WILD_ICASE;
5267
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005268 /* For filtered results, 'wildignore' is used */
5269 if (!filtered)
5270 options |= WILD_KEEP_ALL;
5271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005273 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005275 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005276 if (xpc.xp_context == EXPAND_NOTHING)
5277 {
5278 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005279 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005281 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 return;
5283 }
5284
5285# if defined(FEAT_MENU)
5286 if (xpc.xp_context == EXPAND_MENUS)
5287 {
5288 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5289 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5290 }
5291# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005292#ifdef FEAT_CSCOPE
5293 if (xpc.xp_context == EXPAND_CSCOPE)
5294 {
5295 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5296 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5297 }
5298#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005299#ifdef FEAT_SIGNS
5300 if (xpc.xp_context == EXPAND_SIGN)
5301 {
5302 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5303 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5304 }
5305#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005306
5307 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5308 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5309 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005310 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311
5312 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5313
5314 for (i = 0; i < xpc.xp_numfiles; i++)
5315 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5316 }
5317 vim_free(pat);
5318 ExpandCleanup(&xpc);
5319}
5320#endif
5321
5322/*
5323 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005324 *
5325 * Return the current working directory of a window in a tab page.
5326 * First optional argument 'winnr' is the window number or -1 and the second
5327 * optional argument 'tabnr' is the tab page number.
5328 *
5329 * If no arguments are supplied, then return the directory of the current
5330 * window.
5331 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5332 * the specified window.
5333 * If 'winnr' is 0 then return the directory of the current window.
5334 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5335 * directory of the specified tab page. Otherwise return the directory of the
5336 * specified window in the specified tab page.
5337 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338 */
5339 static void
5340f_getcwd(typval_T *argvars, typval_T *rettv)
5341{
5342 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005343 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005344 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005345 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005346
5347 rettv->v_type = VAR_STRING;
5348 rettv->vval.v_string = NULL;
5349
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005350 if (argvars[0].v_type == VAR_NUMBER
5351 && argvars[0].vval.v_number == -1
5352 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005353 global = TRUE;
5354 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005355 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005356
5357 if (wp != NULL && wp->w_localdir != NULL)
5358 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005359 else if (tp != NULL && tp->tp_localdir != NULL)
5360 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5361 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005362 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005363 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005364 rettv->vval.v_string = vim_strsave(globaldir);
5365 else
5366 {
5367 cwd = alloc(MAXPATHL);
5368 if (cwd != NULL)
5369 {
5370 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5371 rettv->vval.v_string = vim_strsave(cwd);
5372 vim_free(cwd);
5373 }
5374 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005376#ifdef BACKSLASH_IN_FILENAME
5377 if (rettv->vval.v_string != NULL)
5378 slash_adjust(rettv->vval.v_string);
5379#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005380}
5381
5382/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005383 * "getenv()" function
5384 */
5385 static void
5386f_getenv(typval_T *argvars, typval_T *rettv)
5387{
5388 int mustfree = FALSE;
5389 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5390
5391 if (p == NULL)
5392 {
5393 rettv->v_type = VAR_SPECIAL;
5394 rettv->vval.v_number = VVAL_NULL;
5395 return;
5396 }
5397 if (!mustfree)
5398 p = vim_strsave(p);
5399 rettv->vval.v_string = p;
5400 rettv->v_type = VAR_STRING;
5401}
5402
5403/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005404 * "getfontname()" function
5405 */
5406 static void
5407f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5408{
5409 rettv->v_type = VAR_STRING;
5410 rettv->vval.v_string = NULL;
5411#ifdef FEAT_GUI
5412 if (gui.in_use)
5413 {
5414 GuiFont font;
5415 char_u *name = NULL;
5416
5417 if (argvars[0].v_type == VAR_UNKNOWN)
5418 {
5419 /* Get the "Normal" font. Either the name saved by
5420 * hl_set_font_name() or from the font ID. */
5421 font = gui.norm_font;
5422 name = hl_get_font_name();
5423 }
5424 else
5425 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005426 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005427 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5428 return;
5429 font = gui_mch_get_font(name, FALSE);
5430 if (font == NOFONT)
5431 return; /* Invalid font name, return empty string. */
5432 }
5433 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5434 if (argvars[0].v_type != VAR_UNKNOWN)
5435 gui_mch_free_font(font);
5436 }
5437#endif
5438}
5439
5440/*
5441 * "getfperm({fname})" function
5442 */
5443 static void
5444f_getfperm(typval_T *argvars, typval_T *rettv)
5445{
5446 char_u *fname;
5447 stat_T st;
5448 char_u *perm = NULL;
5449 char_u flags[] = "rwx";
5450 int i;
5451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005452 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453
5454 rettv->v_type = VAR_STRING;
5455 if (mch_stat((char *)fname, &st) >= 0)
5456 {
5457 perm = vim_strsave((char_u *)"---------");
5458 if (perm != NULL)
5459 {
5460 for (i = 0; i < 9; i++)
5461 {
5462 if (st.st_mode & (1 << (8 - i)))
5463 perm[i] = flags[i % 3];
5464 }
5465 }
5466 }
5467 rettv->vval.v_string = perm;
5468}
5469
5470/*
5471 * "getfsize({fname})" function
5472 */
5473 static void
5474f_getfsize(typval_T *argvars, typval_T *rettv)
5475{
5476 char_u *fname;
5477 stat_T st;
5478
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005479 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480
5481 rettv->v_type = VAR_NUMBER;
5482
5483 if (mch_stat((char *)fname, &st) >= 0)
5484 {
5485 if (mch_isdir(fname))
5486 rettv->vval.v_number = 0;
5487 else
5488 {
5489 rettv->vval.v_number = (varnumber_T)st.st_size;
5490
5491 /* non-perfect check for overflow */
5492 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5493 rettv->vval.v_number = -2;
5494 }
5495 }
5496 else
5497 rettv->vval.v_number = -1;
5498}
5499
5500/*
5501 * "getftime({fname})" function
5502 */
5503 static void
5504f_getftime(typval_T *argvars, typval_T *rettv)
5505{
5506 char_u *fname;
5507 stat_T st;
5508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005509 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510
5511 if (mch_stat((char *)fname, &st) >= 0)
5512 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5513 else
5514 rettv->vval.v_number = -1;
5515}
5516
5517/*
5518 * "getftype({fname})" function
5519 */
5520 static void
5521f_getftype(typval_T *argvars, typval_T *rettv)
5522{
5523 char_u *fname;
5524 stat_T st;
5525 char_u *type = NULL;
5526 char *t;
5527
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005528 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005529
5530 rettv->v_type = VAR_STRING;
5531 if (mch_lstat((char *)fname, &st) >= 0)
5532 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005533 if (S_ISREG(st.st_mode))
5534 t = "file";
5535 else if (S_ISDIR(st.st_mode))
5536 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537 else if (S_ISLNK(st.st_mode))
5538 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005539 else if (S_ISBLK(st.st_mode))
5540 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 else if (S_ISCHR(st.st_mode))
5542 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005543 else if (S_ISFIFO(st.st_mode))
5544 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005546 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 else
5548 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 type = vim_strsave((char_u *)t);
5550 }
5551 rettv->vval.v_string = type;
5552}
5553
5554/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005555 * "getjumplist()" function
5556 */
5557 static void
5558f_getjumplist(typval_T *argvars, typval_T *rettv)
5559{
5560#ifdef FEAT_JUMPLIST
5561 win_T *wp;
5562 int i;
5563 list_T *l;
5564 dict_T *d;
5565#endif
5566
5567 if (rettv_list_alloc(rettv) != OK)
5568 return;
5569
5570#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005571 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005572 if (wp == NULL)
5573 return;
5574
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005575 cleanup_jumplist(wp, TRUE);
5576
Bram Moolenaar4f505882018-02-10 21:06:32 +01005577 l = list_alloc();
5578 if (l == NULL)
5579 return;
5580
5581 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5582 return;
5583 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5584
5585 for (i = 0; i < wp->w_jumplistlen; ++i)
5586 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005587 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5588 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005589 if ((d = dict_alloc()) == NULL)
5590 return;
5591 if (list_append_dict(l, d) == FAIL)
5592 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005593 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5594 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005595 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005596 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005597 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005598 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005599 }
5600#endif
5601}
5602
5603/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005604 * "getline(lnum, [end])" function
5605 */
5606 static void
5607f_getline(typval_T *argvars, typval_T *rettv)
5608{
5609 linenr_T lnum;
5610 linenr_T end;
5611 int retlist;
5612
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005613 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614 if (argvars[1].v_type == VAR_UNKNOWN)
5615 {
5616 end = 0;
5617 retlist = FALSE;
5618 }
5619 else
5620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 retlist = TRUE;
5623 }
5624
5625 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5626}
5627
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005628#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005629 static void
5630get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5631{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005632 if (what_arg->v_type == VAR_UNKNOWN)
5633 {
5634 if (rettv_list_alloc(rettv) == OK)
5635 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005636 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005637 }
5638 else
5639 {
5640 if (rettv_dict_alloc(rettv) == OK)
5641 if (is_qf || (wp != NULL))
5642 {
5643 if (what_arg->v_type == VAR_DICT)
5644 {
5645 dict_T *d = what_arg->vval.v_dict;
5646
5647 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005648 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005649 }
5650 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005651 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005652 }
5653 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005654}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005655#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005656
5657/*
5658 * "getloclist()" function
5659 */
5660 static void
5661f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5662{
5663#ifdef FEAT_QUICKFIX
5664 win_T *wp;
5665
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005666 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005667 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5668#endif
5669}
5670
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005671/*
5672 * "getmatches()" function
5673 */
5674 static void
5675f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5676{
5677#ifdef FEAT_SEARCH_EXTRA
5678 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005679 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005680 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005681 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682
Bram Moolenaaraff74912019-03-30 18:11:49 +01005683 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5684 return;
5685
5686 cur = win->w_match_head;
5687 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005689 dict = dict_alloc();
5690 if (dict == NULL)
5691 return;
5692 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005694 /* match added with matchaddpos() */
5695 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005697 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005698 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005699 list_T *l;
5700
5701 llpos = &cur->pos.pos[i];
5702 if (llpos->lnum == 0)
5703 break;
5704 l = list_alloc();
5705 if (l == NULL)
5706 break;
5707 list_append_number(l, (varnumber_T)llpos->lnum);
5708 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005709 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005710 list_append_number(l, (varnumber_T)llpos->col);
5711 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005712 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005713 sprintf(buf, "pos%d", i + 1);
5714 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005715 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005717 else
5718 {
5719 dict_add_string(dict, "pattern", cur->pattern);
5720 }
5721 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5722 dict_add_number(dict, "priority", (long)cur->priority);
5723 dict_add_number(dict, "id", (long)cur->id);
5724# if defined(FEAT_CONCEAL)
5725 if (cur->conceal_char)
5726 {
5727 char_u buf[MB_MAXBYTES + 1];
5728
5729 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5730 dict_add_string(dict, "conceal", (char_u *)&buf);
5731 }
5732# endif
5733 list_append_dict(rettv->vval.v_list, dict);
5734 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005735 }
5736#endif
5737}
5738
5739/*
5740 * "getpid()" function
5741 */
5742 static void
5743f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5744{
5745 rettv->vval.v_number = mch_get_pid();
5746}
5747
5748 static void
5749getpos_both(
5750 typval_T *argvars,
5751 typval_T *rettv,
5752 int getcurpos)
5753{
5754 pos_T *fp;
5755 list_T *l;
5756 int fnum = -1;
5757
5758 if (rettv_list_alloc(rettv) == OK)
5759 {
5760 l = rettv->vval.v_list;
5761 if (getcurpos)
5762 fp = &curwin->w_cursor;
5763 else
5764 fp = var2fpos(&argvars[0], TRUE, &fnum);
5765 if (fnum != -1)
5766 list_append_number(l, (varnumber_T)fnum);
5767 else
5768 list_append_number(l, (varnumber_T)0);
5769 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5770 : (varnumber_T)0);
5771 list_append_number(l, (fp != NULL)
5772 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5773 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005774 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005775 (varnumber_T)0);
5776 if (getcurpos)
5777 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005778 int save_set_curswant = curwin->w_set_curswant;
5779 colnr_T save_curswant = curwin->w_curswant;
5780 colnr_T save_virtcol = curwin->w_virtcol;
5781
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 update_curswant();
5783 list_append_number(l, curwin->w_curswant == MAXCOL ?
5784 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005785
5786 // Do not change "curswant", as it is unexpected that a get
5787 // function has a side effect.
5788 if (save_set_curswant)
5789 {
5790 curwin->w_set_curswant = save_set_curswant;
5791 curwin->w_curswant = save_curswant;
5792 curwin->w_virtcol = save_virtcol;
5793 curwin->w_valid &= ~VALID_VIRTCOL;
5794 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 }
5796 }
5797 else
5798 rettv->vval.v_number = FALSE;
5799}
5800
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801/*
5802 * "getcurpos()" function
5803 */
5804 static void
5805f_getcurpos(typval_T *argvars, typval_T *rettv)
5806{
5807 getpos_both(argvars, rettv, TRUE);
5808}
5809
5810/*
5811 * "getpos(string)" function
5812 */
5813 static void
5814f_getpos(typval_T *argvars, typval_T *rettv)
5815{
5816 getpos_both(argvars, rettv, FALSE);
5817}
5818
5819/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005820 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005821 */
5822 static void
5823f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5824{
5825#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005826 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005827#endif
5828}
5829
5830/*
5831 * "getreg()" function
5832 */
5833 static void
5834f_getreg(typval_T *argvars, typval_T *rettv)
5835{
5836 char_u *strregname;
5837 int regname;
5838 int arg2 = FALSE;
5839 int return_list = FALSE;
5840 int error = FALSE;
5841
5842 if (argvars[0].v_type != VAR_UNKNOWN)
5843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 error = strregname == NULL;
5846 if (argvars[1].v_type != VAR_UNKNOWN)
5847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005848 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005849 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005850 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005851 }
5852 }
5853 else
5854 strregname = get_vim_var_str(VV_REG);
5855
5856 if (error)
5857 return;
5858
5859 regname = (strregname == NULL ? '"' : *strregname);
5860 if (regname == 0)
5861 regname = '"';
5862
5863 if (return_list)
5864 {
5865 rettv->v_type = VAR_LIST;
5866 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5867 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5868 if (rettv->vval.v_list == NULL)
5869 (void)rettv_list_alloc(rettv);
5870 else
5871 ++rettv->vval.v_list->lv_refcount;
5872 }
5873 else
5874 {
5875 rettv->v_type = VAR_STRING;
5876 rettv->vval.v_string = get_reg_contents(regname,
5877 arg2 ? GREG_EXPR_SRC : 0);
5878 }
5879}
5880
5881/*
5882 * "getregtype()" function
5883 */
5884 static void
5885f_getregtype(typval_T *argvars, typval_T *rettv)
5886{
5887 char_u *strregname;
5888 int regname;
5889 char_u buf[NUMBUFLEN + 2];
5890 long reglen = 0;
5891
5892 if (argvars[0].v_type != VAR_UNKNOWN)
5893 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005894 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005895 if (strregname == NULL) /* type error; errmsg already given */
5896 {
5897 rettv->v_type = VAR_STRING;
5898 rettv->vval.v_string = NULL;
5899 return;
5900 }
5901 }
5902 else
5903 /* Default to v:register */
5904 strregname = get_vim_var_str(VV_REG);
5905
5906 regname = (strregname == NULL ? '"' : *strregname);
5907 if (regname == 0)
5908 regname = '"';
5909
5910 buf[0] = NUL;
5911 buf[1] = NUL;
5912 switch (get_reg_type(regname, &reglen))
5913 {
5914 case MLINE: buf[0] = 'V'; break;
5915 case MCHAR: buf[0] = 'v'; break;
5916 case MBLOCK:
5917 buf[0] = Ctrl_V;
5918 sprintf((char *)buf + 1, "%ld", reglen + 1);
5919 break;
5920 }
5921 rettv->v_type = VAR_STRING;
5922 rettv->vval.v_string = vim_strsave(buf);
5923}
5924
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005925/*
5926 * Returns information (variables, options, etc.) about a tab page
5927 * as a dictionary.
5928 */
5929 static dict_T *
5930get_tabpage_info(tabpage_T *tp, int tp_idx)
5931{
5932 win_T *wp;
5933 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005934 list_T *l;
5935
5936 dict = dict_alloc();
5937 if (dict == NULL)
5938 return NULL;
5939
Bram Moolenaare0be1672018-07-08 16:50:37 +02005940 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005941
5942 l = list_alloc();
5943 if (l != NULL)
5944 {
5945 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5946 wp; wp = wp->w_next)
5947 list_append_number(l, (varnumber_T)wp->w_id);
5948 dict_add_list(dict, "windows", l);
5949 }
5950
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005951 /* Make a reference to tabpage variables */
5952 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005953
5954 return dict;
5955}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005956
5957/*
5958 * "gettabinfo()" function
5959 */
5960 static void
5961f_gettabinfo(typval_T *argvars, typval_T *rettv)
5962{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005963 tabpage_T *tp, *tparg = NULL;
5964 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005965 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005966
5967 if (rettv_list_alloc(rettv) != OK)
5968 return;
5969
5970 if (argvars[0].v_type != VAR_UNKNOWN)
5971 {
5972 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005973 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005974 if (tparg == NULL)
5975 return;
5976 }
5977
5978 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005979 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005980 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005981 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005982 if (tparg != NULL && tp != tparg)
5983 continue;
5984 d = get_tabpage_info(tp, tpnr);
5985 if (d != NULL)
5986 list_append_dict(rettv->vval.v_list, d);
5987 if (tparg != NULL)
5988 return;
5989 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005990}
5991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005992/*
5993 * "gettabvar()" function
5994 */
5995 static void
5996f_gettabvar(typval_T *argvars, typval_T *rettv)
5997{
5998 win_T *oldcurwin;
5999 tabpage_T *tp, *oldtabpage;
6000 dictitem_T *v;
6001 char_u *varname;
6002 int done = FALSE;
6003
6004 rettv->v_type = VAR_STRING;
6005 rettv->vval.v_string = NULL;
6006
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 varname = tv_get_string_chk(&argvars[1]);
6008 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006009 if (tp != NULL && varname != NULL)
6010 {
6011 /* Set tp to be our tabpage, temporarily. Also set the window to the
6012 * first window in the tabpage, otherwise the window is not valid. */
6013 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006014 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6015 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 {
6017 /* look up the variable */
6018 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6019 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6020 if (v != NULL)
6021 {
6022 copy_tv(&v->di_tv, rettv);
6023 done = TRUE;
6024 }
6025 }
6026
6027 /* restore previous notion of curwin */
6028 restore_win(oldcurwin, oldtabpage, TRUE);
6029 }
6030
6031 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6032 copy_tv(&argvars[2], rettv);
6033}
6034
6035/*
6036 * "gettabwinvar()" function
6037 */
6038 static void
6039f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6040{
6041 getwinvar(argvars, rettv, 1);
6042}
6043
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006044/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006045 * "gettagstack()" function
6046 */
6047 static void
6048f_gettagstack(typval_T *argvars, typval_T *rettv)
6049{
6050 win_T *wp = curwin; // default is current window
6051
6052 if (rettv_dict_alloc(rettv) != OK)
6053 return;
6054
6055 if (argvars[0].v_type != VAR_UNKNOWN)
6056 {
6057 wp = find_win_by_nr_or_id(&argvars[0]);
6058 if (wp == NULL)
6059 return;
6060 }
6061
6062 get_tagstack(wp, rettv->vval.v_dict);
6063}
6064
6065/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066 * Returns information about a window as a dictionary.
6067 */
6068 static dict_T *
6069get_win_info(win_T *wp, short tpnr, short winnr)
6070{
6071 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006072
6073 dict = dict_alloc();
6074 if (dict == NULL)
6075 return NULL;
6076
Bram Moolenaare0be1672018-07-08 16:50:37 +02006077 dict_add_number(dict, "tabnr", tpnr);
6078 dict_add_number(dict, "winnr", winnr);
6079 dict_add_number(dict, "winid", wp->w_id);
6080 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006081 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006082 dict_add_number(dict, "topline", wp->w_topline);
6083 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006084#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006085 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006086#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006087 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006088 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006089 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006090
Bram Moolenaar69905d12017-08-13 18:14:47 +02006091#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006092 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006093#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006094#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006095 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6096 dict_add_number(dict, "loclist",
6097 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006098#endif
6099
Bram Moolenaar30567352016-08-27 21:25:44 +02006100 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006101 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006102
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006103 return dict;
6104}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006105
6106/*
6107 * "getwininfo()" function
6108 */
6109 static void
6110f_getwininfo(typval_T *argvars, typval_T *rettv)
6111{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006112 tabpage_T *tp;
6113 win_T *wp = NULL, *wparg = NULL;
6114 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006115 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006116
6117 if (rettv_list_alloc(rettv) != OK)
6118 return;
6119
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006120 if (argvars[0].v_type != VAR_UNKNOWN)
6121 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006122 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006123 if (wparg == NULL)
6124 return;
6125 }
6126
6127 /* Collect information about either all the windows across all the tab
6128 * pages or one particular window.
6129 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006130 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006131 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006132 tabnr++;
6133 winnr = 0;
6134 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006135 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006136 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006137 if (wparg != NULL && wp != wparg)
6138 continue;
6139 d = get_win_info(wp, tabnr, winnr);
6140 if (d != NULL)
6141 list_append_dict(rettv->vval.v_list, d);
6142 if (wparg != NULL)
6143 /* found information about a specific window */
6144 return;
6145 }
6146 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006147}
6148
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006149/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006150 * "win_execute()" function
6151 */
6152 static void
6153f_win_execute(typval_T *argvars, typval_T *rettv)
6154{
6155 int id = (int)tv_get_number(argvars);
6156 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006157 win_T *save_curwin;
6158 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006159
6160 if (wp != NULL)
6161 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006162 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6163 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006164 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006165 check_cursor();
6166 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006167 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006168 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006169 }
6170}
6171
6172/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006173 * "win_findbuf()" function
6174 */
6175 static void
6176f_win_findbuf(typval_T *argvars, typval_T *rettv)
6177{
6178 if (rettv_list_alloc(rettv) != FAIL)
6179 win_findbuf(argvars, rettv->vval.v_list);
6180}
6181
6182/*
6183 * "win_getid()" function
6184 */
6185 static void
6186f_win_getid(typval_T *argvars, typval_T *rettv)
6187{
6188 rettv->vval.v_number = win_getid(argvars);
6189}
6190
6191/*
6192 * "win_gotoid()" function
6193 */
6194 static void
6195f_win_gotoid(typval_T *argvars, typval_T *rettv)
6196{
6197 rettv->vval.v_number = win_gotoid(argvars);
6198}
6199
6200/*
6201 * "win_id2tabwin()" function
6202 */
6203 static void
6204f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6205{
6206 if (rettv_list_alloc(rettv) != FAIL)
6207 win_id2tabwin(argvars, rettv->vval.v_list);
6208}
6209
6210/*
6211 * "win_id2win()" function
6212 */
6213 static void
6214f_win_id2win(typval_T *argvars, typval_T *rettv)
6215{
6216 rettv->vval.v_number = win_id2win(argvars);
6217}
6218
6219/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006220 * "win_screenpos()" function
6221 */
6222 static void
6223f_win_screenpos(typval_T *argvars, typval_T *rettv)
6224{
6225 win_T *wp;
6226
6227 if (rettv_list_alloc(rettv) == FAIL)
6228 return;
6229
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006230 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006231 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6232 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6233}
6234
6235/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006236 * "getwinpos({timeout})" function
6237 */
6238 static void
6239f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6240{
6241 int x = -1;
6242 int y = -1;
6243
6244 if (rettv_list_alloc(rettv) == FAIL)
6245 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006246#if defined(FEAT_GUI) \
6247 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6248 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006249 {
6250 varnumber_T timeout = 100;
6251
6252 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006254
6255 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006256 }
6257#endif
6258 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6259 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6260}
6261
6262
6263/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006264 * "getwinposx()" function
6265 */
6266 static void
6267f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6268{
6269 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006270#if defined(FEAT_GUI) \
6271 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6272 || defined(MSWIN)
6273
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006274 {
6275 int x, y;
6276
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006277 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006278 rettv->vval.v_number = x;
6279 }
6280#endif
6281}
6282
6283/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006284 * "getwinposy()" function
6285 */
6286 static void
6287f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6288{
6289 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006290#if defined(FEAT_GUI) \
6291 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6292 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006293 {
6294 int x, y;
6295
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006296 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006297 rettv->vval.v_number = y;
6298 }
6299#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300}
6301
6302/*
6303 * "getwinvar()" function
6304 */
6305 static void
6306f_getwinvar(typval_T *argvars, typval_T *rettv)
6307{
6308 getwinvar(argvars, rettv, 0);
6309}
6310
6311/*
6312 * "glob()" function
6313 */
6314 static void
6315f_glob(typval_T *argvars, typval_T *rettv)
6316{
6317 int options = WILD_SILENT|WILD_USE_NL;
6318 expand_T xpc;
6319 int error = FALSE;
6320
6321 /* When the optional second argument is non-zero, don't remove matches
6322 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6323 rettv->v_type = VAR_STRING;
6324 if (argvars[1].v_type != VAR_UNKNOWN)
6325 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006326 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006327 options |= WILD_KEEP_ALL;
6328 if (argvars[2].v_type != VAR_UNKNOWN)
6329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006330 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006331 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006332 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 options |= WILD_ALLLINKS;
6335 }
6336 }
6337 if (!error)
6338 {
6339 ExpandInit(&xpc);
6340 xpc.xp_context = EXPAND_FILES;
6341 if (p_wic)
6342 options += WILD_ICASE;
6343 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006344 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006345 NULL, options, WILD_ALL);
6346 else if (rettv_list_alloc(rettv) != FAIL)
6347 {
6348 int i;
6349
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006350 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351 NULL, options, WILD_ALL_KEEP);
6352 for (i = 0; i < xpc.xp_numfiles; i++)
6353 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6354
6355 ExpandCleanup(&xpc);
6356 }
6357 }
6358 else
6359 rettv->vval.v_string = NULL;
6360}
6361
6362/*
6363 * "globpath()" function
6364 */
6365 static void
6366f_globpath(typval_T *argvars, typval_T *rettv)
6367{
6368 int flags = 0;
6369 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006370 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006371 int error = FALSE;
6372 garray_T ga;
6373 int i;
6374
6375 /* When the optional second argument is non-zero, don't remove matches
6376 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6377 rettv->v_type = VAR_STRING;
6378 if (argvars[2].v_type != VAR_UNKNOWN)
6379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006380 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006381 flags |= WILD_KEEP_ALL;
6382 if (argvars[3].v_type != VAR_UNKNOWN)
6383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006384 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006385 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006387 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388 flags |= WILD_ALLLINKS;
6389 }
6390 }
6391 if (file != NULL && !error)
6392 {
6393 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006394 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 if (rettv->v_type == VAR_STRING)
6396 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6397 else if (rettv_list_alloc(rettv) != FAIL)
6398 for (i = 0; i < ga.ga_len; ++i)
6399 list_append_string(rettv->vval.v_list,
6400 ((char_u **)(ga.ga_data))[i], -1);
6401 ga_clear_strings(&ga);
6402 }
6403 else
6404 rettv->vval.v_string = NULL;
6405}
6406
6407/*
6408 * "glob2regpat()" function
6409 */
6410 static void
6411f_glob2regpat(typval_T *argvars, typval_T *rettv)
6412{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006413 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006414
6415 rettv->v_type = VAR_STRING;
6416 rettv->vval.v_string = (pat == NULL)
6417 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6418}
6419
6420/* for VIM_VERSION_ defines */
6421#include "version.h"
6422
6423/*
6424 * "has()" function
6425 */
6426 static void
6427f_has(typval_T *argvars, typval_T *rettv)
6428{
6429 int i;
6430 char_u *name;
6431 int n = FALSE;
6432 static char *(has_list[]) =
6433 {
6434#ifdef AMIGA
6435 "amiga",
6436# ifdef FEAT_ARP
6437 "arp",
6438# endif
6439#endif
6440#ifdef __BEOS__
6441 "beos",
6442#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006443#if defined(BSD) && !defined(MACOS_X)
6444 "bsd",
6445#endif
6446#ifdef hpux
6447 "hpux",
6448#endif
6449#ifdef __linux__
6450 "linux",
6451#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006452#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006453 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6454 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006455# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006456 "macunix", /* Mac OS X, with the darwin feature */
6457 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006458# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006459#endif
6460#ifdef __QNX__
6461 "qnx",
6462#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006463#ifdef SUN_SYSTEM
6464 "sun",
6465#else
6466 "moon",
6467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468#ifdef UNIX
6469 "unix",
6470#endif
6471#ifdef VMS
6472 "vms",
6473#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006474#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475 "win32",
6476#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006477#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478 "win32unix",
6479#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006480#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 "win64",
6482#endif
6483#ifdef EBCDIC
6484 "ebcdic",
6485#endif
6486#ifndef CASE_INSENSITIVE_FILENAME
6487 "fname_case",
6488#endif
6489#ifdef HAVE_ACL
6490 "acl",
6491#endif
6492#ifdef FEAT_ARABIC
6493 "arabic",
6494#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006495 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006496#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006497 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006498#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006499#ifdef FEAT_AUTOSERVERNAME
6500 "autoservername",
6501#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006502#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006504# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006505 "balloon_multiline",
6506# endif
6507#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006508#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006509 "balloon_eval_term",
6510#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6512 "builtin_terms",
6513# ifdef ALL_BUILTIN_TCAPS
6514 "all_builtin_terms",
6515# endif
6516#endif
6517#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006518 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 || defined(FEAT_GUI_MOTIF))
6520 "browsefilter",
6521#endif
6522#ifdef FEAT_BYTEOFF
6523 "byte_offset",
6524#endif
6525#ifdef FEAT_JOB_CHANNEL
6526 "channel",
6527#endif
6528#ifdef FEAT_CINDENT
6529 "cindent",
6530#endif
6531#ifdef FEAT_CLIENTSERVER
6532 "clientserver",
6533#endif
6534#ifdef FEAT_CLIPBOARD
6535 "clipboard",
6536#endif
6537#ifdef FEAT_CMDL_COMPL
6538 "cmdline_compl",
6539#endif
6540#ifdef FEAT_CMDHIST
6541 "cmdline_hist",
6542#endif
6543#ifdef FEAT_COMMENTS
6544 "comments",
6545#endif
6546#ifdef FEAT_CONCEAL
6547 "conceal",
6548#endif
6549#ifdef FEAT_CRYPT
6550 "cryptv",
6551 "crypt-blowfish",
6552 "crypt-blowfish2",
6553#endif
6554#ifdef FEAT_CSCOPE
6555 "cscope",
6556#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006557 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558#ifdef CURSOR_SHAPE
6559 "cursorshape",
6560#endif
6561#ifdef DEBUG
6562 "debug",
6563#endif
6564#ifdef FEAT_CON_DIALOG
6565 "dialog_con",
6566#endif
6567#ifdef FEAT_GUI_DIALOG
6568 "dialog_gui",
6569#endif
6570#ifdef FEAT_DIFF
6571 "diff",
6572#endif
6573#ifdef FEAT_DIGRAPHS
6574 "digraphs",
6575#endif
6576#ifdef FEAT_DIRECTX
6577 "directx",
6578#endif
6579#ifdef FEAT_DND
6580 "dnd",
6581#endif
6582#ifdef FEAT_EMACS_TAGS
6583 "emacs_tags",
6584#endif
6585 "eval", /* always present, of course! */
6586 "ex_extra", /* graduated feature */
6587#ifdef FEAT_SEARCH_EXTRA
6588 "extra_search",
6589#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006590#ifdef FEAT_SEARCHPATH
6591 "file_in_path",
6592#endif
6593#ifdef FEAT_FILTERPIPE
6594 "filterpipe",
6595#endif
6596#ifdef FEAT_FIND_ID
6597 "find_in_path",
6598#endif
6599#ifdef FEAT_FLOAT
6600 "float",
6601#endif
6602#ifdef FEAT_FOLDING
6603 "folding",
6604#endif
6605#ifdef FEAT_FOOTER
6606 "footer",
6607#endif
6608#if !defined(USE_SYSTEM) && defined(UNIX)
6609 "fork",
6610#endif
6611#ifdef FEAT_GETTEXT
6612 "gettext",
6613#endif
6614#ifdef FEAT_GUI
6615 "gui",
6616#endif
6617#ifdef FEAT_GUI_ATHENA
6618# ifdef FEAT_GUI_NEXTAW
6619 "gui_neXtaw",
6620# else
6621 "gui_athena",
6622# endif
6623#endif
6624#ifdef FEAT_GUI_GTK
6625 "gui_gtk",
6626# ifdef USE_GTK3
6627 "gui_gtk3",
6628# else
6629 "gui_gtk2",
6630# endif
6631#endif
6632#ifdef FEAT_GUI_GNOME
6633 "gui_gnome",
6634#endif
6635#ifdef FEAT_GUI_MAC
6636 "gui_mac",
6637#endif
6638#ifdef FEAT_GUI_MOTIF
6639 "gui_motif",
6640#endif
6641#ifdef FEAT_GUI_PHOTON
6642 "gui_photon",
6643#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006644#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 "gui_win32",
6646#endif
6647#ifdef FEAT_HANGULIN
6648 "hangul_input",
6649#endif
6650#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6651 "iconv",
6652#endif
6653#ifdef FEAT_INS_EXPAND
6654 "insert_expand",
6655#endif
6656#ifdef FEAT_JOB_CHANNEL
6657 "job",
6658#endif
6659#ifdef FEAT_JUMPLIST
6660 "jumplist",
6661#endif
6662#ifdef FEAT_KEYMAP
6663 "keymap",
6664#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006665 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006666#ifdef FEAT_LANGMAP
6667 "langmap",
6668#endif
6669#ifdef FEAT_LIBCALL
6670 "libcall",
6671#endif
6672#ifdef FEAT_LINEBREAK
6673 "linebreak",
6674#endif
6675#ifdef FEAT_LISP
6676 "lispindent",
6677#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679#ifdef FEAT_LOCALMAP
6680 "localmap",
6681#endif
6682#ifdef FEAT_LUA
6683# ifndef DYNAMIC_LUA
6684 "lua",
6685# endif
6686#endif
6687#ifdef FEAT_MENU
6688 "menu",
6689#endif
6690#ifdef FEAT_SESSION
6691 "mksession",
6692#endif
6693#ifdef FEAT_MODIFY_FNAME
6694 "modify_fname",
6695#endif
6696#ifdef FEAT_MOUSE
6697 "mouse",
6698#endif
6699#ifdef FEAT_MOUSESHAPE
6700 "mouseshape",
6701#endif
6702#if defined(UNIX) || defined(VMS)
6703# ifdef FEAT_MOUSE_DEC
6704 "mouse_dec",
6705# endif
6706# ifdef FEAT_MOUSE_GPM
6707 "mouse_gpm",
6708# endif
6709# ifdef FEAT_MOUSE_JSB
6710 "mouse_jsbterm",
6711# endif
6712# ifdef FEAT_MOUSE_NET
6713 "mouse_netterm",
6714# endif
6715# ifdef FEAT_MOUSE_PTERM
6716 "mouse_pterm",
6717# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006718# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006719 "mouse_sgr",
6720# endif
6721# ifdef FEAT_SYSMOUSE
6722 "mouse_sysmouse",
6723# endif
6724# ifdef FEAT_MOUSE_URXVT
6725 "mouse_urxvt",
6726# endif
6727# ifdef FEAT_MOUSE_XTERM
6728 "mouse_xterm",
6729# endif
6730#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732#ifdef FEAT_MBYTE_IME
6733 "multi_byte_ime",
6734#endif
6735#ifdef FEAT_MULTI_LANG
6736 "multi_lang",
6737#endif
6738#ifdef FEAT_MZSCHEME
6739#ifndef DYNAMIC_MZSCHEME
6740 "mzscheme",
6741#endif
6742#endif
6743#ifdef FEAT_NUM64
6744 "num64",
6745#endif
6746#ifdef FEAT_OLE
6747 "ole",
6748#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006749#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006750 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006751#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006752#ifdef FEAT_PATH_EXTRA
6753 "path_extra",
6754#endif
6755#ifdef FEAT_PERL
6756#ifndef DYNAMIC_PERL
6757 "perl",
6758#endif
6759#endif
6760#ifdef FEAT_PERSISTENT_UNDO
6761 "persistent_undo",
6762#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006763#if defined(FEAT_PYTHON)
6764 "python_compiled",
6765# if defined(DYNAMIC_PYTHON)
6766 "python_dynamic",
6767# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006768 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006769 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006770# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006772#if defined(FEAT_PYTHON3)
6773 "python3_compiled",
6774# if defined(DYNAMIC_PYTHON3)
6775 "python3_dynamic",
6776# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006778 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006779# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006780#endif
6781#ifdef FEAT_POSTSCRIPT
6782 "postscript",
6783#endif
6784#ifdef FEAT_PRINTER
6785 "printer",
6786#endif
6787#ifdef FEAT_PROFILE
6788 "profile",
6789#endif
6790#ifdef FEAT_RELTIME
6791 "reltime",
6792#endif
6793#ifdef FEAT_QUICKFIX
6794 "quickfix",
6795#endif
6796#ifdef FEAT_RIGHTLEFT
6797 "rightleft",
6798#endif
6799#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6800 "ruby",
6801#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006802 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006803#ifdef FEAT_CMDL_INFO
6804 "showcmd",
6805 "cmdline_info",
6806#endif
6807#ifdef FEAT_SIGNS
6808 "signs",
6809#endif
6810#ifdef FEAT_SMARTINDENT
6811 "smartindent",
6812#endif
6813#ifdef STARTUPTIME
6814 "startuptime",
6815#endif
6816#ifdef FEAT_STL_OPT
6817 "statusline",
6818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819#ifdef FEAT_NETBEANS_INTG
6820 "netbeans_intg",
6821#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006822#ifdef FEAT_SOUND
6823 "sound",
6824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825#ifdef FEAT_SPELL
6826 "spell",
6827#endif
6828#ifdef FEAT_SYN_HL
6829 "syntax",
6830#endif
6831#if defined(USE_SYSTEM) || !defined(UNIX)
6832 "system",
6833#endif
6834#ifdef FEAT_TAG_BINS
6835 "tag_binary",
6836#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006837#ifdef FEAT_TCL
6838# ifndef DYNAMIC_TCL
6839 "tcl",
6840# endif
6841#endif
6842#ifdef FEAT_TERMGUICOLORS
6843 "termguicolors",
6844#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006845#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006846 "terminal",
6847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848#ifdef TERMINFO
6849 "terminfo",
6850#endif
6851#ifdef FEAT_TERMRESPONSE
6852 "termresponse",
6853#endif
6854#ifdef FEAT_TEXTOBJ
6855 "textobjects",
6856#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006857#ifdef FEAT_TEXT_PROP
6858 "textprop",
6859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860#ifdef HAVE_TGETENT
6861 "tgetent",
6862#endif
6863#ifdef FEAT_TIMERS
6864 "timers",
6865#endif
6866#ifdef FEAT_TITLE
6867 "title",
6868#endif
6869#ifdef FEAT_TOOLBAR
6870 "toolbar",
6871#endif
6872#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6873 "unnamedplus",
6874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006875 "user-commands", /* was accidentally included in 5.4 */
6876 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006877#ifdef FEAT_VARTABS
6878 "vartabs",
6879#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006880 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006881#ifdef FEAT_VIMINFO
6882 "viminfo",
6883#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006884 "vimscript-1",
6885 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006886 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006887 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006891#ifdef FEAT_VTP
6892 "vtp",
6893#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894#ifdef FEAT_WILDIGN
6895 "wildignore",
6896#endif
6897#ifdef FEAT_WILDMENU
6898 "wildmenu",
6899#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901#ifdef FEAT_WAK
6902 "winaltkeys",
6903#endif
6904#ifdef FEAT_WRITEBACKUP
6905 "writebackup",
6906#endif
6907#ifdef FEAT_XIM
6908 "xim",
6909#endif
6910#ifdef FEAT_XFONTSET
6911 "xfontset",
6912#endif
6913#ifdef FEAT_XPM_W32
6914 "xpm",
6915 "xpm_w32", /* for backward compatibility */
6916#else
6917# if defined(HAVE_XPM)
6918 "xpm",
6919# endif
6920#endif
6921#ifdef USE_XSMP
6922 "xsmp",
6923#endif
6924#ifdef USE_XSMP_INTERACT
6925 "xsmp_interact",
6926#endif
6927#ifdef FEAT_XCLIPBOARD
6928 "xterm_clipboard",
6929#endif
6930#ifdef FEAT_XTERM_SAVE
6931 "xterm_save",
6932#endif
6933#if defined(UNIX) && defined(FEAT_X11)
6934 "X11",
6935#endif
6936 NULL
6937 };
6938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006939 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006940 for (i = 0; has_list[i] != NULL; ++i)
6941 if (STRICMP(name, has_list[i]) == 0)
6942 {
6943 n = TRUE;
6944 break;
6945 }
6946
6947 if (n == FALSE)
6948 {
6949 if (STRNICMP(name, "patch", 5) == 0)
6950 {
6951 if (name[5] == '-'
6952 && STRLEN(name) >= 11
6953 && vim_isdigit(name[6])
6954 && vim_isdigit(name[8])
6955 && vim_isdigit(name[10]))
6956 {
6957 int major = atoi((char *)name + 6);
6958 int minor = atoi((char *)name + 8);
6959
6960 /* Expect "patch-9.9.01234". */
6961 n = (major < VIM_VERSION_MAJOR
6962 || (major == VIM_VERSION_MAJOR
6963 && (minor < VIM_VERSION_MINOR
6964 || (minor == VIM_VERSION_MINOR
6965 && has_patch(atoi((char *)name + 10))))));
6966 }
6967 else
6968 n = has_patch(atoi((char *)name + 5));
6969 }
6970 else if (STRICMP(name, "vim_starting") == 0)
6971 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006972 else if (STRICMP(name, "ttyin") == 0)
6973 n = mch_input_isatty();
6974 else if (STRICMP(name, "ttyout") == 0)
6975 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006976 else if (STRICMP(name, "multi_byte_encoding") == 0)
6977 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006978#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006979 else if (STRICMP(name, "balloon_multiline") == 0)
6980 n = multiline_balloon_available();
6981#endif
6982#ifdef DYNAMIC_TCL
6983 else if (STRICMP(name, "tcl") == 0)
6984 n = tcl_enabled(FALSE);
6985#endif
6986#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6987 else if (STRICMP(name, "iconv") == 0)
6988 n = iconv_enabled(FALSE);
6989#endif
6990#ifdef DYNAMIC_LUA
6991 else if (STRICMP(name, "lua") == 0)
6992 n = lua_enabled(FALSE);
6993#endif
6994#ifdef DYNAMIC_MZSCHEME
6995 else if (STRICMP(name, "mzscheme") == 0)
6996 n = mzscheme_enabled(FALSE);
6997#endif
6998#ifdef DYNAMIC_RUBY
6999 else if (STRICMP(name, "ruby") == 0)
7000 n = ruby_enabled(FALSE);
7001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007002#ifdef DYNAMIC_PYTHON
7003 else if (STRICMP(name, "python") == 0)
7004 n = python_enabled(FALSE);
7005#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006#ifdef DYNAMIC_PYTHON3
7007 else if (STRICMP(name, "python3") == 0)
7008 n = python3_enabled(FALSE);
7009#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007010#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7011 else if (STRICMP(name, "pythonx") == 0)
7012 {
7013# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7014 if (p_pyx == 0)
7015 n = python3_enabled(FALSE) || python_enabled(FALSE);
7016 else if (p_pyx == 3)
7017 n = python3_enabled(FALSE);
7018 else if (p_pyx == 2)
7019 n = python_enabled(FALSE);
7020# elif defined(DYNAMIC_PYTHON)
7021 n = python_enabled(FALSE);
7022# elif defined(DYNAMIC_PYTHON3)
7023 n = python3_enabled(FALSE);
7024# endif
7025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007026#endif
7027#ifdef DYNAMIC_PERL
7028 else if (STRICMP(name, "perl") == 0)
7029 n = perl_enabled(FALSE);
7030#endif
7031#ifdef FEAT_GUI
7032 else if (STRICMP(name, "gui_running") == 0)
7033 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034# ifdef FEAT_BROWSE
7035 else if (STRICMP(name, "browse") == 0)
7036 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7037# endif
7038#endif
7039#ifdef FEAT_SYN_HL
7040 else if (STRICMP(name, "syntax_items") == 0)
7041 n = syntax_present(curwin);
7042#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007043#ifdef FEAT_VTP
7044 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007045 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046#endif
7047#ifdef FEAT_NETBEANS_INTG
7048 else if (STRICMP(name, "netbeans_enabled") == 0)
7049 n = netbeans_active();
7050#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007051#ifdef FEAT_MOUSE_GPM
7052 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7053 n = gpm_enabled();
7054#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007055#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007056 else if (STRICMP(name, "terminal") == 0)
7057 n = terminal_enabled();
7058#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007059#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007060 else if (STRICMP(name, "conpty") == 0)
7061 n = use_conpty();
7062#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063 }
7064
7065 rettv->vval.v_number = n;
7066}
7067
7068/*
7069 * "has_key()" function
7070 */
7071 static void
7072f_has_key(typval_T *argvars, typval_T *rettv)
7073{
7074 if (argvars[0].v_type != VAR_DICT)
7075 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007076 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 return;
7078 }
7079 if (argvars[0].vval.v_dict == NULL)
7080 return;
7081
7082 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007083 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084}
7085
7086/*
7087 * "haslocaldir()" function
7088 */
7089 static void
7090f_haslocaldir(typval_T *argvars, typval_T *rettv)
7091{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007092 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007093 win_T *wp = NULL;
7094
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007095 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7096
7097 // Check for window-local and tab-local directories
7098 if (wp != NULL && wp->w_localdir != NULL)
7099 rettv->vval.v_number = 1;
7100 else if (tp != NULL && tp->tp_localdir != NULL)
7101 rettv->vval.v_number = 2;
7102 else
7103 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104}
7105
7106/*
7107 * "hasmapto()" function
7108 */
7109 static void
7110f_hasmapto(typval_T *argvars, typval_T *rettv)
7111{
7112 char_u *name;
7113 char_u *mode;
7114 char_u buf[NUMBUFLEN];
7115 int abbr = FALSE;
7116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007117 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007118 if (argvars[1].v_type == VAR_UNKNOWN)
7119 mode = (char_u *)"nvo";
7120 else
7121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007122 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007124 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 }
7126
7127 if (map_to_exists(name, mode, abbr))
7128 rettv->vval.v_number = TRUE;
7129 else
7130 rettv->vval.v_number = FALSE;
7131}
7132
7133/*
7134 * "histadd()" function
7135 */
7136 static void
7137f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7138{
7139#ifdef FEAT_CMDHIST
7140 int histype;
7141 char_u *str;
7142 char_u buf[NUMBUFLEN];
7143#endif
7144
7145 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007146 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007147 return;
7148#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007149 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 histype = str != NULL ? get_histtype(str) : -1;
7151 if (histype >= 0)
7152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007153 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 if (*str != NUL)
7155 {
7156 init_history();
7157 add_to_history(histype, str, FALSE, NUL);
7158 rettv->vval.v_number = TRUE;
7159 return;
7160 }
7161 }
7162#endif
7163}
7164
7165/*
7166 * "histdel()" function
7167 */
7168 static void
7169f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7170{
7171#ifdef FEAT_CMDHIST
7172 int n;
7173 char_u buf[NUMBUFLEN];
7174 char_u *str;
7175
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007176 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177 if (str == NULL)
7178 n = 0;
7179 else if (argvars[1].v_type == VAR_UNKNOWN)
7180 /* only one argument: clear entire history */
7181 n = clr_history(get_histtype(str));
7182 else if (argvars[1].v_type == VAR_NUMBER)
7183 /* index given: remove that entry */
7184 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007185 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007186 else
7187 /* string given: remove all matching entries */
7188 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007189 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 rettv->vval.v_number = n;
7191#endif
7192}
7193
7194/*
7195 * "histget()" function
7196 */
7197 static void
7198f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7199{
7200#ifdef FEAT_CMDHIST
7201 int type;
7202 int idx;
7203 char_u *str;
7204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007205 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007206 if (str == NULL)
7207 rettv->vval.v_string = NULL;
7208 else
7209 {
7210 type = get_histtype(str);
7211 if (argvars[1].v_type == VAR_UNKNOWN)
7212 idx = get_history_idx(type);
7213 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007214 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215 /* -1 on type error */
7216 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7217 }
7218#else
7219 rettv->vval.v_string = NULL;
7220#endif
7221 rettv->v_type = VAR_STRING;
7222}
7223
7224/*
7225 * "histnr()" function
7226 */
7227 static void
7228f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7229{
7230 int i;
7231
7232#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007233 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007234
7235 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7236 if (i >= HIST_CMD && i < HIST_COUNT)
7237 i = get_history_idx(i);
7238 else
7239#endif
7240 i = -1;
7241 rettv->vval.v_number = i;
7242}
7243
7244/*
7245 * "highlightID(name)" function
7246 */
7247 static void
7248f_hlID(typval_T *argvars, typval_T *rettv)
7249{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007250 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251}
7252
7253/*
7254 * "highlight_exists()" function
7255 */
7256 static void
7257f_hlexists(typval_T *argvars, typval_T *rettv)
7258{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007259 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007260}
7261
7262/*
7263 * "hostname()" function
7264 */
7265 static void
7266f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7267{
7268 char_u hostname[256];
7269
7270 mch_get_host_name(hostname, 256);
7271 rettv->v_type = VAR_STRING;
7272 rettv->vval.v_string = vim_strsave(hostname);
7273}
7274
7275/*
7276 * iconv() function
7277 */
7278 static void
7279f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7280{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007281 char_u buf1[NUMBUFLEN];
7282 char_u buf2[NUMBUFLEN];
7283 char_u *from, *to, *str;
7284 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007285
7286 rettv->v_type = VAR_STRING;
7287 rettv->vval.v_string = NULL;
7288
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007289 str = tv_get_string(&argvars[0]);
7290 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7291 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 vimconv.vc_type = CONV_NONE;
7293 convert_setup(&vimconv, from, to);
7294
7295 /* If the encodings are equal, no conversion needed. */
7296 if (vimconv.vc_type == CONV_NONE)
7297 rettv->vval.v_string = vim_strsave(str);
7298 else
7299 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7300
7301 convert_setup(&vimconv, NULL, NULL);
7302 vim_free(from);
7303 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304}
7305
7306/*
7307 * "indent()" function
7308 */
7309 static void
7310f_indent(typval_T *argvars, typval_T *rettv)
7311{
7312 linenr_T lnum;
7313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007314 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7316 rettv->vval.v_number = get_indent_lnum(lnum);
7317 else
7318 rettv->vval.v_number = -1;
7319}
7320
7321/*
7322 * "index()" function
7323 */
7324 static void
7325f_index(typval_T *argvars, typval_T *rettv)
7326{
7327 list_T *l;
7328 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007329 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 long idx = 0;
7331 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007332 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333
7334 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007335 if (argvars[0].v_type == VAR_BLOB)
7336 {
7337 typval_T tv;
7338 int start = 0;
7339
7340 if (argvars[2].v_type != VAR_UNKNOWN)
7341 {
7342 start = tv_get_number_chk(&argvars[2], &error);
7343 if (error)
7344 return;
7345 }
7346 b = argvars[0].vval.v_blob;
7347 if (b == NULL)
7348 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007349 if (start < 0)
7350 {
7351 start = blob_len(b) + start;
7352 if (start < 0)
7353 start = 0;
7354 }
7355
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007356 for (idx = start; idx < blob_len(b); ++idx)
7357 {
7358 tv.v_type = VAR_NUMBER;
7359 tv.vval.v_number = blob_get(b, idx);
7360 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7361 {
7362 rettv->vval.v_number = idx;
7363 return;
7364 }
7365 }
7366 return;
7367 }
7368 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007369 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007370 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007371 return;
7372 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007373
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007374 l = argvars[0].vval.v_list;
7375 if (l != NULL)
7376 {
7377 item = l->lv_first;
7378 if (argvars[2].v_type != VAR_UNKNOWN)
7379 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 /* Start at specified item. Use the cached index that list_find()
7381 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007382 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383 idx = l->lv_idx;
7384 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 if (error)
7387 item = NULL;
7388 }
7389
7390 for ( ; item != NULL; item = item->li_next, ++idx)
7391 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7392 {
7393 rettv->vval.v_number = idx;
7394 break;
7395 }
7396 }
7397}
7398
7399static int inputsecret_flag = 0;
7400
7401/*
7402 * "input()" function
7403 * Also handles inputsecret() when inputsecret is set.
7404 */
7405 static void
7406f_input(typval_T *argvars, typval_T *rettv)
7407{
7408 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7409}
7410
7411/*
7412 * "inputdialog()" function
7413 */
7414 static void
7415f_inputdialog(typval_T *argvars, typval_T *rettv)
7416{
7417#if defined(FEAT_GUI_TEXTDIALOG)
7418 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7419 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7420 {
7421 char_u *message;
7422 char_u buf[NUMBUFLEN];
7423 char_u *defstr = (char_u *)"";
7424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007425 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007427 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007428 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7429 else
7430 IObuff[0] = NUL;
7431 if (message != NULL && defstr != NULL
7432 && do_dialog(VIM_QUESTION, NULL, message,
7433 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7434 rettv->vval.v_string = vim_strsave(IObuff);
7435 else
7436 {
7437 if (message != NULL && defstr != NULL
7438 && argvars[1].v_type != VAR_UNKNOWN
7439 && argvars[2].v_type != VAR_UNKNOWN)
7440 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007441 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 else
7443 rettv->vval.v_string = NULL;
7444 }
7445 rettv->v_type = VAR_STRING;
7446 }
7447 else
7448#endif
7449 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7450}
7451
7452/*
7453 * "inputlist()" function
7454 */
7455 static void
7456f_inputlist(typval_T *argvars, typval_T *rettv)
7457{
7458 listitem_T *li;
7459 int selected;
7460 int mouse_used;
7461
7462#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007463 /* While starting up, there is no place to enter text. When running tests
7464 * with --not-a-term we assume feedkeys() will be used. */
7465 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007466 return;
7467#endif
7468 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007470 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 return;
7472 }
7473
7474 msg_start();
7475 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7476 lines_left = Rows; /* avoid more prompt */
7477 msg_scroll = TRUE;
7478 msg_clr_eos();
7479
7480 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7481 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007482 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007483 msg_putchar('\n');
7484 }
7485
7486 /* Ask for choice. */
7487 selected = prompt_for_number(&mouse_used);
7488 if (mouse_used)
7489 selected -= lines_left;
7490
7491 rettv->vval.v_number = selected;
7492}
7493
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007494static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7495
7496/*
7497 * "inputrestore()" function
7498 */
7499 static void
7500f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7501{
7502 if (ga_userinput.ga_len > 0)
7503 {
7504 --ga_userinput.ga_len;
7505 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7506 + ga_userinput.ga_len);
7507 /* default return is zero == OK */
7508 }
7509 else if (p_verbose > 1)
7510 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007511 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007512 rettv->vval.v_number = 1; /* Failed */
7513 }
7514}
7515
7516/*
7517 * "inputsave()" function
7518 */
7519 static void
7520f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7521{
7522 /* Add an entry to the stack of typeahead storage. */
7523 if (ga_grow(&ga_userinput, 1) == OK)
7524 {
7525 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7526 + ga_userinput.ga_len);
7527 ++ga_userinput.ga_len;
7528 /* default return is zero == OK */
7529 }
7530 else
7531 rettv->vval.v_number = 1; /* Failed */
7532}
7533
7534/*
7535 * "inputsecret()" function
7536 */
7537 static void
7538f_inputsecret(typval_T *argvars, typval_T *rettv)
7539{
7540 ++cmdline_star;
7541 ++inputsecret_flag;
7542 f_input(argvars, rettv);
7543 --cmdline_star;
7544 --inputsecret_flag;
7545}
7546
7547/*
7548 * "insert()" function
7549 */
7550 static void
7551f_insert(typval_T *argvars, typval_T *rettv)
7552{
7553 long before = 0;
7554 listitem_T *item;
7555 list_T *l;
7556 int error = FALSE;
7557
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007558 if (argvars[0].v_type == VAR_BLOB)
7559 {
7560 int val, len;
7561 char_u *p;
7562
7563 len = blob_len(argvars[0].vval.v_blob);
7564 if (argvars[2].v_type != VAR_UNKNOWN)
7565 {
7566 before = (long)tv_get_number_chk(&argvars[2], &error);
7567 if (error)
7568 return; // type error; errmsg already given
7569 if (before < 0 || before > len)
7570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007571 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007572 return;
7573 }
7574 }
7575 val = tv_get_number_chk(&argvars[1], &error);
7576 if (error)
7577 return;
7578 if (val < 0 || val > 255)
7579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007580 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007581 return;
7582 }
7583
7584 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7585 return;
7586 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7587 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7588 *(p + before) = val;
7589 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7590
7591 copy_tv(&argvars[0], rettv);
7592 }
7593 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007594 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007595 else if ((l = argvars[0].vval.v_list) != NULL
7596 && !var_check_lock(l->lv_lock,
7597 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 {
7599 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007600 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007601 if (error)
7602 return; /* type error; errmsg already given */
7603
7604 if (before == l->lv_len)
7605 item = NULL;
7606 else
7607 {
7608 item = list_find(l, before);
7609 if (item == NULL)
7610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007611 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 l = NULL;
7613 }
7614 }
7615 if (l != NULL)
7616 {
7617 list_insert_tv(l, &argvars[1], item);
7618 copy_tv(&argvars[0], rettv);
7619 }
7620 }
7621}
7622
7623/*
7624 * "invert(expr)" function
7625 */
7626 static void
7627f_invert(typval_T *argvars, typval_T *rettv)
7628{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007629 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630}
7631
7632/*
7633 * "isdirectory()" function
7634 */
7635 static void
7636f_isdirectory(typval_T *argvars, typval_T *rettv)
7637{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007638 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639}
7640
7641/*
7642 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7643 * or it refers to a List or Dictionary that is locked.
7644 */
7645 static int
7646tv_islocked(typval_T *tv)
7647{
7648 return (tv->v_lock & VAR_LOCKED)
7649 || (tv->v_type == VAR_LIST
7650 && tv->vval.v_list != NULL
7651 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7652 || (tv->v_type == VAR_DICT
7653 && tv->vval.v_dict != NULL
7654 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7655}
7656
7657/*
7658 * "islocked()" function
7659 */
7660 static void
7661f_islocked(typval_T *argvars, typval_T *rettv)
7662{
7663 lval_T lv;
7664 char_u *end;
7665 dictitem_T *di;
7666
7667 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007668 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007669 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 if (end != NULL && lv.ll_name != NULL)
7671 {
7672 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 else
7675 {
7676 if (lv.ll_tv == NULL)
7677 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007678 di = find_var(lv.ll_name, NULL, TRUE);
7679 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007681 /* Consider a variable locked when:
7682 * 1. the variable itself is locked
7683 * 2. the value of the variable is locked.
7684 * 3. the List or Dict value is locked.
7685 */
7686 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7687 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 }
7689 }
7690 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007691 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007692 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007693 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007694 else if (lv.ll_list != NULL)
7695 /* List item. */
7696 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7697 else
7698 /* Dictionary item. */
7699 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7700 }
7701 }
7702
7703 clear_lval(&lv);
7704}
7705
7706#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7707/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007708 * "isinf()" function
7709 */
7710 static void
7711f_isinf(typval_T *argvars, typval_T *rettv)
7712{
7713 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7714 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7715}
7716
7717/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 * "isnan()" function
7719 */
7720 static void
7721f_isnan(typval_T *argvars, typval_T *rettv)
7722{
7723 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7724 && isnan(argvars[0].vval.v_float);
7725}
7726#endif
7727
7728/*
7729 * "items(dict)" function
7730 */
7731 static void
7732f_items(typval_T *argvars, typval_T *rettv)
7733{
7734 dict_list(argvars, rettv, 2);
7735}
7736
7737#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7738/*
7739 * Get the job from the argument.
7740 * Returns NULL if the job is invalid.
7741 */
7742 static job_T *
7743get_job_arg(typval_T *tv)
7744{
7745 job_T *job;
7746
7747 if (tv->v_type != VAR_JOB)
7748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007749 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750 return NULL;
7751 }
7752 job = tv->vval.v_job;
7753
7754 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007755 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007756 return job;
7757}
7758
7759/*
7760 * "job_getchannel()" function
7761 */
7762 static void
7763f_job_getchannel(typval_T *argvars, typval_T *rettv)
7764{
7765 job_T *job = get_job_arg(&argvars[0]);
7766
7767 if (job != NULL)
7768 {
7769 rettv->v_type = VAR_CHANNEL;
7770 rettv->vval.v_channel = job->jv_channel;
7771 if (job->jv_channel != NULL)
7772 ++job->jv_channel->ch_refcount;
7773 }
7774}
7775
7776/*
7777 * "job_info()" function
7778 */
7779 static void
7780f_job_info(typval_T *argvars, typval_T *rettv)
7781{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007782 if (argvars[0].v_type != VAR_UNKNOWN)
7783 {
7784 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007786 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7787 job_info(job, rettv->vval.v_dict);
7788 }
7789 else if (rettv_list_alloc(rettv) == OK)
7790 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007791}
7792
7793/*
7794 * "job_setoptions()" function
7795 */
7796 static void
7797f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7798{
7799 job_T *job = get_job_arg(&argvars[0]);
7800 jobopt_T opt;
7801
7802 if (job == NULL)
7803 return;
7804 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007805 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806 job_set_options(job, &opt);
7807 free_job_options(&opt);
7808}
7809
7810/*
7811 * "job_start()" function
7812 */
7813 static void
7814f_job_start(typval_T *argvars, typval_T *rettv)
7815{
7816 rettv->v_type = VAR_JOB;
7817 if (check_restricted() || check_secure())
7818 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007819 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820}
7821
7822/*
7823 * "job_status()" function
7824 */
7825 static void
7826f_job_status(typval_T *argvars, typval_T *rettv)
7827{
7828 job_T *job = get_job_arg(&argvars[0]);
7829
7830 if (job != NULL)
7831 {
7832 rettv->v_type = VAR_STRING;
7833 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7834 }
7835}
7836
7837/*
7838 * "job_stop()" function
7839 */
7840 static void
7841f_job_stop(typval_T *argvars, typval_T *rettv)
7842{
7843 job_T *job = get_job_arg(&argvars[0]);
7844
7845 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007846 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007847}
7848#endif
7849
7850/*
7851 * "join()" function
7852 */
7853 static void
7854f_join(typval_T *argvars, typval_T *rettv)
7855{
7856 garray_T ga;
7857 char_u *sep;
7858
7859 if (argvars[0].v_type != VAR_LIST)
7860 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007861 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007862 return;
7863 }
7864 if (argvars[0].vval.v_list == NULL)
7865 return;
7866 if (argvars[1].v_type == VAR_UNKNOWN)
7867 sep = (char_u *)" ";
7868 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007869 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870
7871 rettv->v_type = VAR_STRING;
7872
7873 if (sep != NULL)
7874 {
7875 ga_init2(&ga, (int)sizeof(char), 80);
7876 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7877 ga_append(&ga, NUL);
7878 rettv->vval.v_string = (char_u *)ga.ga_data;
7879 }
7880 else
7881 rettv->vval.v_string = NULL;
7882}
7883
7884/*
7885 * "js_decode()" function
7886 */
7887 static void
7888f_js_decode(typval_T *argvars, typval_T *rettv)
7889{
7890 js_read_T reader;
7891
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 reader.js_fill = NULL;
7894 reader.js_used = 0;
7895 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007896 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897}
7898
7899/*
7900 * "js_encode()" function
7901 */
7902 static void
7903f_js_encode(typval_T *argvars, typval_T *rettv)
7904{
7905 rettv->v_type = VAR_STRING;
7906 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7907}
7908
7909/*
7910 * "json_decode()" function
7911 */
7912 static void
7913f_json_decode(typval_T *argvars, typval_T *rettv)
7914{
7915 js_read_T reader;
7916
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007917 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918 reader.js_fill = NULL;
7919 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007920 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007921}
7922
7923/*
7924 * "json_encode()" function
7925 */
7926 static void
7927f_json_encode(typval_T *argvars, typval_T *rettv)
7928{
7929 rettv->v_type = VAR_STRING;
7930 rettv->vval.v_string = json_encode(&argvars[0], 0);
7931}
7932
7933/*
7934 * "keys()" function
7935 */
7936 static void
7937f_keys(typval_T *argvars, typval_T *rettv)
7938{
7939 dict_list(argvars, rettv, 0);
7940}
7941
7942/*
7943 * "last_buffer_nr()" function.
7944 */
7945 static void
7946f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7947{
7948 int n = 0;
7949 buf_T *buf;
7950
Bram Moolenaar29323592016-07-24 22:04:11 +02007951 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 if (n < buf->b_fnum)
7953 n = buf->b_fnum;
7954
7955 rettv->vval.v_number = n;
7956}
7957
7958/*
7959 * "len()" function
7960 */
7961 static void
7962f_len(typval_T *argvars, typval_T *rettv)
7963{
7964 switch (argvars[0].v_type)
7965 {
7966 case VAR_STRING:
7967 case VAR_NUMBER:
7968 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007969 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007971 case VAR_BLOB:
7972 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7973 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007974 case VAR_LIST:
7975 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7976 break;
7977 case VAR_DICT:
7978 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7979 break;
7980 case VAR_UNKNOWN:
7981 case VAR_SPECIAL:
7982 case VAR_FLOAT:
7983 case VAR_FUNC:
7984 case VAR_PARTIAL:
7985 case VAR_JOB:
7986 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007987 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007988 break;
7989 }
7990}
7991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007992 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007993libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994{
7995#ifdef FEAT_LIBCALL
7996 char_u *string_in;
7997 char_u **string_result;
7998 int nr_result;
7999#endif
8000
8001 rettv->v_type = type;
8002 if (type != VAR_NUMBER)
8003 rettv->vval.v_string = NULL;
8004
8005 if (check_restricted() || check_secure())
8006 return;
8007
8008#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008009 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008010 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8011 {
8012 string_in = NULL;
8013 if (argvars[2].v_type == VAR_STRING)
8014 string_in = argvars[2].vval.v_string;
8015 if (type == VAR_NUMBER)
8016 string_result = NULL;
8017 else
8018 string_result = &rettv->vval.v_string;
8019 if (mch_libcall(argvars[0].vval.v_string,
8020 argvars[1].vval.v_string,
8021 string_in,
8022 argvars[2].vval.v_number,
8023 string_result,
8024 &nr_result) == OK
8025 && type == VAR_NUMBER)
8026 rettv->vval.v_number = nr_result;
8027 }
8028#endif
8029}
8030
8031/*
8032 * "libcall()" function
8033 */
8034 static void
8035f_libcall(typval_T *argvars, typval_T *rettv)
8036{
8037 libcall_common(argvars, rettv, VAR_STRING);
8038}
8039
8040/*
8041 * "libcallnr()" function
8042 */
8043 static void
8044f_libcallnr(typval_T *argvars, typval_T *rettv)
8045{
8046 libcall_common(argvars, rettv, VAR_NUMBER);
8047}
8048
8049/*
8050 * "line(string)" function
8051 */
8052 static void
8053f_line(typval_T *argvars, typval_T *rettv)
8054{
8055 linenr_T lnum = 0;
8056 pos_T *fp;
8057 int fnum;
8058
8059 fp = var2fpos(&argvars[0], TRUE, &fnum);
8060 if (fp != NULL)
8061 lnum = fp->lnum;
8062 rettv->vval.v_number = lnum;
8063}
8064
8065/*
8066 * "line2byte(lnum)" function
8067 */
8068 static void
8069f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8070{
8071#ifndef FEAT_BYTEOFF
8072 rettv->vval.v_number = -1;
8073#else
8074 linenr_T lnum;
8075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008076 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008077 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8078 rettv->vval.v_number = -1;
8079 else
8080 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8081 if (rettv->vval.v_number >= 0)
8082 ++rettv->vval.v_number;
8083#endif
8084}
8085
8086/*
8087 * "lispindent(lnum)" function
8088 */
8089 static void
8090f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8091{
8092#ifdef FEAT_LISP
8093 pos_T pos;
8094 linenr_T lnum;
8095
8096 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008097 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008098 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8099 {
8100 curwin->w_cursor.lnum = lnum;
8101 rettv->vval.v_number = get_lisp_indent();
8102 curwin->w_cursor = pos;
8103 }
8104 else
8105#endif
8106 rettv->vval.v_number = -1;
8107}
8108
8109/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008110 * "list2str()" function
8111 */
8112 static void
8113f_list2str(typval_T *argvars, typval_T *rettv)
8114{
8115 list_T *l;
8116 listitem_T *li;
8117 garray_T ga;
8118 int utf8 = FALSE;
8119
8120 rettv->v_type = VAR_STRING;
8121 rettv->vval.v_string = NULL;
8122 if (argvars[0].v_type != VAR_LIST)
8123 {
8124 emsg(_(e_invarg));
8125 return;
8126 }
8127
8128 l = argvars[0].vval.v_list;
8129 if (l == NULL)
8130 return; // empty list results in empty string
8131
8132 if (argvars[1].v_type != VAR_UNKNOWN)
8133 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8134
8135 ga_init2(&ga, 1, 80);
8136 if (has_mbyte || utf8)
8137 {
8138 char_u buf[MB_MAXBYTES + 1];
8139 int (*char2bytes)(int, char_u *);
8140
8141 if (utf8 || enc_utf8)
8142 char2bytes = utf_char2bytes;
8143 else
8144 char2bytes = mb_char2bytes;
8145
8146 for (li = l->lv_first; li != NULL; li = li->li_next)
8147 {
8148 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8149 ga_concat(&ga, buf);
8150 }
8151 ga_append(&ga, NUL);
8152 }
8153 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8154 {
8155 for (li = l->lv_first; li != NULL; li = li->li_next)
8156 ga_append(&ga, tv_get_number(&li->li_tv));
8157 ga_append(&ga, NUL);
8158 }
8159
8160 rettv->v_type = VAR_STRING;
8161 rettv->vval.v_string = ga.ga_data;
8162}
8163
8164/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008165 * "localtime()" function
8166 */
8167 static void
8168f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8169{
8170 rettv->vval.v_number = (varnumber_T)time(NULL);
8171}
8172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 static void
8174get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8175{
8176 char_u *keys;
8177 char_u *which;
8178 char_u buf[NUMBUFLEN];
8179 char_u *keys_buf = NULL;
8180 char_u *rhs;
8181 int mode;
8182 int abbr = FALSE;
8183 int get_dict = FALSE;
8184 mapblock_T *mp;
8185 int buffer_local;
8186
8187 /* return empty string for failure */
8188 rettv->v_type = VAR_STRING;
8189 rettv->vval.v_string = NULL;
8190
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008191 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008192 if (*keys == NUL)
8193 return;
8194
8195 if (argvars[1].v_type != VAR_UNKNOWN)
8196 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008197 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008198 if (argvars[2].v_type != VAR_UNKNOWN)
8199 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008200 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008201 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008202 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 }
8204 }
8205 else
8206 which = (char_u *)"";
8207 if (which == NULL)
8208 return;
8209
8210 mode = get_map_mode(&which, 0);
8211
8212 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8213 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8214 vim_free(keys_buf);
8215
8216 if (!get_dict)
8217 {
8218 /* Return a string. */
8219 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008220 {
8221 if (*rhs == NUL)
8222 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8223 else
8224 rettv->vval.v_string = str2special_save(rhs, FALSE);
8225 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008226
8227 }
8228 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8229 {
8230 /* Return a dictionary. */
8231 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8232 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8233 dict_T *dict = rettv->vval.v_dict;
8234
Bram Moolenaare0be1672018-07-08 16:50:37 +02008235 dict_add_string(dict, "lhs", lhs);
8236 dict_add_string(dict, "rhs", mp->m_orig_str);
8237 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8238 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8239 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008240 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8241 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008242 dict_add_number(dict, "buffer", (long)buffer_local);
8243 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8244 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008245
8246 vim_free(lhs);
8247 vim_free(mapmode);
8248 }
8249}
8250
8251#ifdef FEAT_FLOAT
8252/*
8253 * "log()" function
8254 */
8255 static void
8256f_log(typval_T *argvars, typval_T *rettv)
8257{
8258 float_T f = 0.0;
8259
8260 rettv->v_type = VAR_FLOAT;
8261 if (get_float_arg(argvars, &f) == OK)
8262 rettv->vval.v_float = log(f);
8263 else
8264 rettv->vval.v_float = 0.0;
8265}
8266
8267/*
8268 * "log10()" function
8269 */
8270 static void
8271f_log10(typval_T *argvars, typval_T *rettv)
8272{
8273 float_T f = 0.0;
8274
8275 rettv->v_type = VAR_FLOAT;
8276 if (get_float_arg(argvars, &f) == OK)
8277 rettv->vval.v_float = log10(f);
8278 else
8279 rettv->vval.v_float = 0.0;
8280}
8281#endif
8282
8283#ifdef FEAT_LUA
8284/*
8285 * "luaeval()" function
8286 */
8287 static void
8288f_luaeval(typval_T *argvars, typval_T *rettv)
8289{
8290 char_u *str;
8291 char_u buf[NUMBUFLEN];
8292
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008293 if (check_restricted() || check_secure())
8294 return;
8295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008296 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008297 do_luaeval(str, argvars + 1, rettv);
8298}
8299#endif
8300
8301/*
8302 * "map()" function
8303 */
8304 static void
8305f_map(typval_T *argvars, typval_T *rettv)
8306{
8307 filter_map(argvars, rettv, TRUE);
8308}
8309
8310/*
8311 * "maparg()" function
8312 */
8313 static void
8314f_maparg(typval_T *argvars, typval_T *rettv)
8315{
8316 get_maparg(argvars, rettv, TRUE);
8317}
8318
8319/*
8320 * "mapcheck()" function
8321 */
8322 static void
8323f_mapcheck(typval_T *argvars, typval_T *rettv)
8324{
8325 get_maparg(argvars, rettv, FALSE);
8326}
8327
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008328typedef enum
8329{
8330 MATCH_END, /* matchend() */
8331 MATCH_MATCH, /* match() */
8332 MATCH_STR, /* matchstr() */
8333 MATCH_LIST, /* matchlist() */
8334 MATCH_POS /* matchstrpos() */
8335} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008336
8337 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008338find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339{
8340 char_u *str = NULL;
8341 long len = 0;
8342 char_u *expr = NULL;
8343 char_u *pat;
8344 regmatch_T regmatch;
8345 char_u patbuf[NUMBUFLEN];
8346 char_u strbuf[NUMBUFLEN];
8347 char_u *save_cpo;
8348 long start = 0;
8349 long nth = 1;
8350 colnr_T startcol = 0;
8351 int match = 0;
8352 list_T *l = NULL;
8353 listitem_T *li = NULL;
8354 long idx = 0;
8355 char_u *tofree = NULL;
8356
8357 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8358 save_cpo = p_cpo;
8359 p_cpo = (char_u *)"";
8360
8361 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008362 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008364 /* type MATCH_LIST: return empty list when there are no matches.
8365 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008366 if (rettv_list_alloc(rettv) == FAIL)
8367 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008368 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 && (list_append_string(rettv->vval.v_list,
8370 (char_u *)"", 0) == FAIL
8371 || list_append_number(rettv->vval.v_list,
8372 (varnumber_T)-1) == FAIL
8373 || list_append_number(rettv->vval.v_list,
8374 (varnumber_T)-1) == FAIL
8375 || list_append_number(rettv->vval.v_list,
8376 (varnumber_T)-1) == FAIL))
8377 {
8378 list_free(rettv->vval.v_list);
8379 rettv->vval.v_list = NULL;
8380 goto theend;
8381 }
8382 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008383 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008384 {
8385 rettv->v_type = VAR_STRING;
8386 rettv->vval.v_string = NULL;
8387 }
8388
8389 if (argvars[0].v_type == VAR_LIST)
8390 {
8391 if ((l = argvars[0].vval.v_list) == NULL)
8392 goto theend;
8393 li = l->lv_first;
8394 }
8395 else
8396 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 len = (long)STRLEN(str);
8399 }
8400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008401 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 if (pat == NULL)
8403 goto theend;
8404
8405 if (argvars[2].v_type != VAR_UNKNOWN)
8406 {
8407 int error = FALSE;
8408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008409 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 if (error)
8411 goto theend;
8412 if (l != NULL)
8413 {
8414 li = list_find(l, start);
8415 if (li == NULL)
8416 goto theend;
8417 idx = l->lv_idx; /* use the cached index */
8418 }
8419 else
8420 {
8421 if (start < 0)
8422 start = 0;
8423 if (start > len)
8424 goto theend;
8425 /* When "count" argument is there ignore matches before "start",
8426 * otherwise skip part of the string. Differs when pattern is "^"
8427 * or "\<". */
8428 if (argvars[3].v_type != VAR_UNKNOWN)
8429 startcol = start;
8430 else
8431 {
8432 str += start;
8433 len -= start;
8434 }
8435 }
8436
8437 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008438 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 if (error)
8440 goto theend;
8441 }
8442
8443 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8444 if (regmatch.regprog != NULL)
8445 {
8446 regmatch.rm_ic = p_ic;
8447
8448 for (;;)
8449 {
8450 if (l != NULL)
8451 {
8452 if (li == NULL)
8453 {
8454 match = FALSE;
8455 break;
8456 }
8457 vim_free(tofree);
8458 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8459 if (str == NULL)
8460 break;
8461 }
8462
8463 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8464
8465 if (match && --nth <= 0)
8466 break;
8467 if (l == NULL && !match)
8468 break;
8469
8470 /* Advance to just after the match. */
8471 if (l != NULL)
8472 {
8473 li = li->li_next;
8474 ++idx;
8475 }
8476 else
8477 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 startcol = (colnr_T)(regmatch.startp[0]
8479 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008480 if (startcol > (colnr_T)len
8481 || str + startcol <= regmatch.startp[0])
8482 {
8483 match = FALSE;
8484 break;
8485 }
8486 }
8487 }
8488
8489 if (match)
8490 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008491 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492 {
8493 listitem_T *li1 = rettv->vval.v_list->lv_first;
8494 listitem_T *li2 = li1->li_next;
8495 listitem_T *li3 = li2->li_next;
8496 listitem_T *li4 = li3->li_next;
8497
8498 vim_free(li1->li_tv.vval.v_string);
8499 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8500 (int)(regmatch.endp[0] - regmatch.startp[0]));
8501 li3->li_tv.vval.v_number =
8502 (varnumber_T)(regmatch.startp[0] - expr);
8503 li4->li_tv.vval.v_number =
8504 (varnumber_T)(regmatch.endp[0] - expr);
8505 if (l != NULL)
8506 li2->li_tv.vval.v_number = (varnumber_T)idx;
8507 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008508 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008509 {
8510 int i;
8511
8512 /* return list with matched string and submatches */
8513 for (i = 0; i < NSUBEXP; ++i)
8514 {
8515 if (regmatch.endp[i] == NULL)
8516 {
8517 if (list_append_string(rettv->vval.v_list,
8518 (char_u *)"", 0) == FAIL)
8519 break;
8520 }
8521 else if (list_append_string(rettv->vval.v_list,
8522 regmatch.startp[i],
8523 (int)(regmatch.endp[i] - regmatch.startp[i]))
8524 == FAIL)
8525 break;
8526 }
8527 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008528 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529 {
8530 /* return matched string */
8531 if (l != NULL)
8532 copy_tv(&li->li_tv, rettv);
8533 else
8534 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8535 (int)(regmatch.endp[0] - regmatch.startp[0]));
8536 }
8537 else if (l != NULL)
8538 rettv->vval.v_number = idx;
8539 else
8540 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008541 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 rettv->vval.v_number =
8543 (varnumber_T)(regmatch.startp[0] - str);
8544 else
8545 rettv->vval.v_number =
8546 (varnumber_T)(regmatch.endp[0] - str);
8547 rettv->vval.v_number += (varnumber_T)(str - expr);
8548 }
8549 }
8550 vim_regfree(regmatch.regprog);
8551 }
8552
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008553theend:
8554 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 /* matchstrpos() without a list: drop the second item. */
8556 listitem_remove(rettv->vval.v_list,
8557 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 vim_free(tofree);
8559 p_cpo = save_cpo;
8560}
8561
8562/*
8563 * "match()" function
8564 */
8565 static void
8566f_match(typval_T *argvars, typval_T *rettv)
8567{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008568 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569}
8570
Bram Moolenaar95e51472018-07-28 16:55:56 +02008571#ifdef FEAT_SEARCH_EXTRA
8572 static int
8573matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8574{
8575 dictitem_T *di;
8576
8577 if (tv->v_type != VAR_DICT)
8578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008579 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008580 return FAIL;
8581 }
8582
8583 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008584 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008585 (char_u *)"conceal", FALSE);
8586
8587 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8588 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008589 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008590 if (*win == NULL)
8591 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008592 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008593 return FAIL;
8594 }
8595 }
8596
8597 return OK;
8598}
8599#endif
8600
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601/*
8602 * "matchadd()" function
8603 */
8604 static void
8605f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8606{
8607#ifdef FEAT_SEARCH_EXTRA
8608 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008609 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8610 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008611 int prio = 10; /* default priority */
8612 int id = -1;
8613 int error = FALSE;
8614 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008615 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616
8617 rettv->vval.v_number = -1;
8618
8619 if (grp == NULL || pat == NULL)
8620 return;
8621 if (argvars[2].v_type != VAR_UNKNOWN)
8622 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if (argvars[3].v_type != VAR_UNKNOWN)
8625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008626 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008627 if (argvars[4].v_type != VAR_UNKNOWN
8628 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8629 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 }
8631 }
8632 if (error == TRUE)
8633 return;
8634 if (id >= 1 && id <= 3)
8635 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008636 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 return;
8638 }
8639
Bram Moolenaar95e51472018-07-28 16:55:56 +02008640 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 conceal_char);
8642#endif
8643}
8644
8645/*
8646 * "matchaddpos()" function
8647 */
8648 static void
8649f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8650{
8651#ifdef FEAT_SEARCH_EXTRA
8652 char_u buf[NUMBUFLEN];
8653 char_u *group;
8654 int prio = 10;
8655 int id = -1;
8656 int error = FALSE;
8657 list_T *l;
8658 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008659 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008660
8661 rettv->vval.v_number = -1;
8662
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008663 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664 if (group == NULL)
8665 return;
8666
8667 if (argvars[1].v_type != VAR_LIST)
8668 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008669 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670 return;
8671 }
8672 l = argvars[1].vval.v_list;
8673 if (l == NULL)
8674 return;
8675
8676 if (argvars[2].v_type != VAR_UNKNOWN)
8677 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008678 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008679 if (argvars[3].v_type != VAR_UNKNOWN)
8680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008681 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008682
8683 if (argvars[4].v_type != VAR_UNKNOWN
8684 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8685 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 }
8687 }
8688 if (error == TRUE)
8689 return;
8690
8691 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8692 if (id == 1 || id == 2)
8693 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008694 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008695 return;
8696 }
8697
Bram Moolenaar95e51472018-07-28 16:55:56 +02008698 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 conceal_char);
8700#endif
8701}
8702
8703/*
8704 * "matcharg()" function
8705 */
8706 static void
8707f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8708{
8709 if (rettv_list_alloc(rettv) == OK)
8710 {
8711#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008712 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008713 matchitem_T *m;
8714
8715 if (id >= 1 && id <= 3)
8716 {
8717 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8718 {
8719 list_append_string(rettv->vval.v_list,
8720 syn_id2name(m->hlg_id), -1);
8721 list_append_string(rettv->vval.v_list, m->pattern, -1);
8722 }
8723 else
8724 {
8725 list_append_string(rettv->vval.v_list, NULL, -1);
8726 list_append_string(rettv->vval.v_list, NULL, -1);
8727 }
8728 }
8729#endif
8730 }
8731}
8732
8733/*
8734 * "matchdelete()" function
8735 */
8736 static void
8737f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8738{
8739#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008740 win_T *win = get_optional_window(argvars, 1);
8741
8742 if (win == NULL)
8743 rettv->vval.v_number = -1;
8744 else
8745 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008746 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008747#endif
8748}
8749
8750/*
8751 * "matchend()" function
8752 */
8753 static void
8754f_matchend(typval_T *argvars, typval_T *rettv)
8755{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008756 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757}
8758
8759/*
8760 * "matchlist()" function
8761 */
8762 static void
8763f_matchlist(typval_T *argvars, typval_T *rettv)
8764{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008765 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766}
8767
8768/*
8769 * "matchstr()" function
8770 */
8771 static void
8772f_matchstr(typval_T *argvars, typval_T *rettv)
8773{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008774 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008775}
8776
8777/*
8778 * "matchstrpos()" function
8779 */
8780 static void
8781f_matchstrpos(typval_T *argvars, typval_T *rettv)
8782{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008783 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008784}
8785
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786 static void
8787max_min(typval_T *argvars, typval_T *rettv, int domax)
8788{
8789 varnumber_T n = 0;
8790 varnumber_T i;
8791 int error = FALSE;
8792
8793 if (argvars[0].v_type == VAR_LIST)
8794 {
8795 list_T *l;
8796 listitem_T *li;
8797
8798 l = argvars[0].vval.v_list;
8799 if (l != NULL)
8800 {
8801 li = l->lv_first;
8802 if (li != NULL)
8803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008804 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008805 for (;;)
8806 {
8807 li = li->li_next;
8808 if (li == NULL)
8809 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008810 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008811 if (domax ? i > n : i < n)
8812 n = i;
8813 }
8814 }
8815 }
8816 }
8817 else if (argvars[0].v_type == VAR_DICT)
8818 {
8819 dict_T *d;
8820 int first = TRUE;
8821 hashitem_T *hi;
8822 int todo;
8823
8824 d = argvars[0].vval.v_dict;
8825 if (d != NULL)
8826 {
8827 todo = (int)d->dv_hashtab.ht_used;
8828 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8829 {
8830 if (!HASHITEM_EMPTY(hi))
8831 {
8832 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008833 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 if (first)
8835 {
8836 n = i;
8837 first = FALSE;
8838 }
8839 else if (domax ? i > n : i < n)
8840 n = i;
8841 }
8842 }
8843 }
8844 }
8845 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008846 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 rettv->vval.v_number = error ? 0 : n;
8848}
8849
8850/*
8851 * "max()" function
8852 */
8853 static void
8854f_max(typval_T *argvars, typval_T *rettv)
8855{
8856 max_min(argvars, rettv, TRUE);
8857}
8858
8859/*
8860 * "min()" function
8861 */
8862 static void
8863f_min(typval_T *argvars, typval_T *rettv)
8864{
8865 max_min(argvars, rettv, FALSE);
8866}
8867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868/*
8869 * Create the directory in which "dir" is located, and higher levels when
8870 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008871 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008872 */
8873 static int
8874mkdir_recurse(char_u *dir, int prot)
8875{
8876 char_u *p;
8877 char_u *updir;
8878 int r = FAIL;
8879
8880 /* Get end of directory name in "dir".
8881 * We're done when it's "/" or "c:/". */
8882 p = gettail_sep(dir);
8883 if (p <= get_past_head(dir))
8884 return OK;
8885
8886 /* If the directory exists we're done. Otherwise: create it.*/
8887 updir = vim_strnsave(dir, (int)(p - dir));
8888 if (updir == NULL)
8889 return FAIL;
8890 if (mch_isdir(updir))
8891 r = OK;
8892 else if (mkdir_recurse(updir, prot) == OK)
8893 r = vim_mkdir_emsg(updir, prot);
8894 vim_free(updir);
8895 return r;
8896}
8897
8898#ifdef vim_mkdir
8899/*
8900 * "mkdir()" function
8901 */
8902 static void
8903f_mkdir(typval_T *argvars, typval_T *rettv)
8904{
8905 char_u *dir;
8906 char_u buf[NUMBUFLEN];
8907 int prot = 0755;
8908
8909 rettv->vval.v_number = FAIL;
8910 if (check_restricted() || check_secure())
8911 return;
8912
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008913 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008914 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008915 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008916
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008917 if (*gettail(dir) == NUL)
8918 /* remove trailing slashes */
8919 *gettail_sep(dir) = NUL;
8920
8921 if (argvars[1].v_type != VAR_UNKNOWN)
8922 {
8923 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008925 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008926 if (prot == -1)
8927 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008929 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008930 {
8931 if (mch_isdir(dir))
8932 {
8933 /* With the "p" flag it's OK if the dir already exists. */
8934 rettv->vval.v_number = OK;
8935 return;
8936 }
8937 mkdir_recurse(dir, prot);
8938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008940 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941}
8942#endif
8943
8944/*
8945 * "mode()" function
8946 */
8947 static void
8948f_mode(typval_T *argvars, typval_T *rettv)
8949{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008950 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008951
Bram Moolenaar612cc382018-07-29 15:34:26 +02008952 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008953
8954 if (time_for_testing == 93784)
8955 {
8956 /* Testing the two-character code. */
8957 buf[0] = 'x';
8958 buf[1] = '!';
8959 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008960#ifdef FEAT_TERMINAL
8961 else if (term_use_loop())
8962 buf[0] = 't';
8963#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 else if (VIsual_active)
8965 {
8966 if (VIsual_select)
8967 buf[0] = VIsual_mode + 's' - 'v';
8968 else
8969 buf[0] = VIsual_mode;
8970 }
8971 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8972 || State == CONFIRM)
8973 {
8974 buf[0] = 'r';
8975 if (State == ASKMORE)
8976 buf[1] = 'm';
8977 else if (State == CONFIRM)
8978 buf[1] = '?';
8979 }
8980 else if (State == EXTERNCMD)
8981 buf[0] = '!';
8982 else if (State & INSERT)
8983 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008984 if (State & VREPLACE_FLAG)
8985 {
8986 buf[0] = 'R';
8987 buf[1] = 'v';
8988 }
8989 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008990 {
8991 if (State & REPLACE_FLAG)
8992 buf[0] = 'R';
8993 else
8994 buf[0] = 'i';
8995#ifdef FEAT_INS_EXPAND
8996 if (ins_compl_active())
8997 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008998 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008999 buf[1] = 'x';
9000#endif
9001 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009002 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009003 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009004 {
9005 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009006 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009007 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009008 else if (exmode_active == EXMODE_NORMAL)
9009 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 }
9011 else
9012 {
9013 buf[0] = 'n';
9014 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009015 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009016 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009017 // to be able to detect force-linewise/blockwise/characterwise operations
9018 buf[2] = motion_force;
9019 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009020 else if (restart_edit == 'I' || restart_edit == 'R'
9021 || restart_edit == 'V')
9022 {
9023 buf[1] = 'i';
9024 buf[2] = restart_edit;
9025 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 }
9027
9028 /* Clear out the minor mode when the argument is not a non-zero number or
9029 * non-empty string. */
9030 if (!non_zero_arg(&argvars[0]))
9031 buf[1] = NUL;
9032
9033 rettv->vval.v_string = vim_strsave(buf);
9034 rettv->v_type = VAR_STRING;
9035}
9036
9037#if defined(FEAT_MZSCHEME) || defined(PROTO)
9038/*
9039 * "mzeval()" function
9040 */
9041 static void
9042f_mzeval(typval_T *argvars, typval_T *rettv)
9043{
9044 char_u *str;
9045 char_u buf[NUMBUFLEN];
9046
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009047 if (check_restricted() || check_secure())
9048 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009049 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009050 do_mzeval(str, rettv);
9051}
9052
9053 void
9054mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9055{
9056 typval_T argvars[3];
9057
9058 argvars[0].v_type = VAR_STRING;
9059 argvars[0].vval.v_string = name;
9060 copy_tv(args, &argvars[1]);
9061 argvars[2].v_type = VAR_UNKNOWN;
9062 f_call(argvars, rettv);
9063 clear_tv(&argvars[1]);
9064}
9065#endif
9066
9067/*
9068 * "nextnonblank()" function
9069 */
9070 static void
9071f_nextnonblank(typval_T *argvars, typval_T *rettv)
9072{
9073 linenr_T lnum;
9074
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009075 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009076 {
9077 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9078 {
9079 lnum = 0;
9080 break;
9081 }
9082 if (*skipwhite(ml_get(lnum)) != NUL)
9083 break;
9084 }
9085 rettv->vval.v_number = lnum;
9086}
9087
9088/*
9089 * "nr2char()" function
9090 */
9091 static void
9092f_nr2char(typval_T *argvars, typval_T *rettv)
9093{
9094 char_u buf[NUMBUFLEN];
9095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 if (has_mbyte)
9097 {
9098 int utf8 = 0;
9099
9100 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009101 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009102 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009103 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009105 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 }
9107 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009108 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009109 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 buf[1] = NUL;
9111 }
9112 rettv->v_type = VAR_STRING;
9113 rettv->vval.v_string = vim_strsave(buf);
9114}
9115
9116/*
9117 * "or(expr, expr)" function
9118 */
9119 static void
9120f_or(typval_T *argvars, typval_T *rettv)
9121{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009122 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9123 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009124}
9125
9126/*
9127 * "pathshorten()" function
9128 */
9129 static void
9130f_pathshorten(typval_T *argvars, typval_T *rettv)
9131{
9132 char_u *p;
9133
9134 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009135 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009136 if (p == NULL)
9137 rettv->vval.v_string = NULL;
9138 else
9139 {
9140 p = vim_strsave(p);
9141 rettv->vval.v_string = p;
9142 if (p != NULL)
9143 shorten_dir(p);
9144 }
9145}
9146
9147#ifdef FEAT_PERL
9148/*
9149 * "perleval()" function
9150 */
9151 static void
9152f_perleval(typval_T *argvars, typval_T *rettv)
9153{
9154 char_u *str;
9155 char_u buf[NUMBUFLEN];
9156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009157 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009158 do_perleval(str, rettv);
9159}
9160#endif
9161
9162#ifdef FEAT_FLOAT
9163/*
9164 * "pow()" function
9165 */
9166 static void
9167f_pow(typval_T *argvars, typval_T *rettv)
9168{
9169 float_T fx = 0.0, fy = 0.0;
9170
9171 rettv->v_type = VAR_FLOAT;
9172 if (get_float_arg(argvars, &fx) == OK
9173 && get_float_arg(&argvars[1], &fy) == OK)
9174 rettv->vval.v_float = pow(fx, fy);
9175 else
9176 rettv->vval.v_float = 0.0;
9177}
9178#endif
9179
9180/*
9181 * "prevnonblank()" function
9182 */
9183 static void
9184f_prevnonblank(typval_T *argvars, typval_T *rettv)
9185{
9186 linenr_T lnum;
9187
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009188 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9190 lnum = 0;
9191 else
9192 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9193 --lnum;
9194 rettv->vval.v_number = lnum;
9195}
9196
9197/* This dummy va_list is here because:
9198 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9199 * - locally in the function results in a "used before set" warning
9200 * - using va_start() to initialize it gives "function with fixed args" error */
9201static va_list ap;
9202
9203/*
9204 * "printf()" function
9205 */
9206 static void
9207f_printf(typval_T *argvars, typval_T *rettv)
9208{
9209 char_u buf[NUMBUFLEN];
9210 int len;
9211 char_u *s;
9212 int saved_did_emsg = did_emsg;
9213 char *fmt;
9214
9215 rettv->v_type = VAR_STRING;
9216 rettv->vval.v_string = NULL;
9217
9218 /* Get the required length, allocate the buffer and do it for real. */
9219 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009220 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009221 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009222 if (!did_emsg)
9223 {
9224 s = alloc(len + 1);
9225 if (s != NULL)
9226 {
9227 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009228 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9229 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230 }
9231 }
9232 did_emsg |= saved_did_emsg;
9233}
9234
Bram Moolenaarf2732452018-06-03 14:47:35 +02009235#ifdef FEAT_JOB_CHANNEL
9236/*
9237 * "prompt_setcallback({buffer}, {callback})" function
9238 */
9239 static void
9240f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9241{
9242 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009243 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009244
9245 if (check_secure())
9246 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009247 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009248 if (buf == NULL)
9249 return;
9250
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009251 callback = get_callback(&argvars[1]);
9252 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009253 return;
9254
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009255 free_callback(&buf->b_prompt_callback);
9256 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009257}
9258
9259/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009260 * "prompt_setinterrupt({buffer}, {callback})" function
9261 */
9262 static void
9263f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9264{
9265 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009266 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009267
9268 if (check_secure())
9269 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009270 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009271 if (buf == NULL)
9272 return;
9273
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009274 callback = get_callback(&argvars[1]);
9275 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009276 return;
9277
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009278 free_callback(&buf->b_prompt_interrupt);
9279 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009280}
9281
9282/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009283 * "prompt_setprompt({buffer}, {text})" function
9284 */
9285 static void
9286f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9287{
9288 buf_T *buf;
9289 char_u *text;
9290
9291 if (check_secure())
9292 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009293 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009294 if (buf == NULL)
9295 return;
9296
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009297 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009298 vim_free(buf->b_prompt_text);
9299 buf->b_prompt_text = vim_strsave(text);
9300}
9301#endif
9302
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303/*
9304 * "pumvisible()" function
9305 */
9306 static void
9307f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9308{
9309#ifdef FEAT_INS_EXPAND
9310 if (pum_visible())
9311 rettv->vval.v_number = 1;
9312#endif
9313}
9314
9315#ifdef FEAT_PYTHON3
9316/*
9317 * "py3eval()" function
9318 */
9319 static void
9320f_py3eval(typval_T *argvars, typval_T *rettv)
9321{
9322 char_u *str;
9323 char_u buf[NUMBUFLEN];
9324
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009325 if (check_restricted() || check_secure())
9326 return;
9327
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009328 if (p_pyx == 0)
9329 p_pyx = 3;
9330
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009331 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009332 do_py3eval(str, rettv);
9333}
9334#endif
9335
9336#ifdef FEAT_PYTHON
9337/*
9338 * "pyeval()" function
9339 */
9340 static void
9341f_pyeval(typval_T *argvars, typval_T *rettv)
9342{
9343 char_u *str;
9344 char_u buf[NUMBUFLEN];
9345
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009346 if (check_restricted() || check_secure())
9347 return;
9348
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009349 if (p_pyx == 0)
9350 p_pyx = 2;
9351
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009352 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009353 do_pyeval(str, rettv);
9354}
9355#endif
9356
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009357#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9358/*
9359 * "pyxeval()" function
9360 */
9361 static void
9362f_pyxeval(typval_T *argvars, typval_T *rettv)
9363{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009364 if (check_restricted() || check_secure())
9365 return;
9366
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009367# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9368 init_pyxversion();
9369 if (p_pyx == 2)
9370 f_pyeval(argvars, rettv);
9371 else
9372 f_py3eval(argvars, rettv);
9373# elif defined(FEAT_PYTHON)
9374 f_pyeval(argvars, rettv);
9375# elif defined(FEAT_PYTHON3)
9376 f_py3eval(argvars, rettv);
9377# endif
9378}
9379#endif
9380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381/*
9382 * "range()" function
9383 */
9384 static void
9385f_range(typval_T *argvars, typval_T *rettv)
9386{
9387 varnumber_T start;
9388 varnumber_T end;
9389 varnumber_T stride = 1;
9390 varnumber_T i;
9391 int error = FALSE;
9392
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009393 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394 if (argvars[1].v_type == VAR_UNKNOWN)
9395 {
9396 end = start - 1;
9397 start = 0;
9398 }
9399 else
9400 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009403 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009404 }
9405
9406 if (error)
9407 return; /* type error; errmsg already given */
9408 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009409 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009411 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 else
9413 {
9414 if (rettv_list_alloc(rettv) == OK)
9415 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9416 if (list_append_number(rettv->vval.v_list,
9417 (varnumber_T)i) == FAIL)
9418 break;
9419 }
9420}
9421
9422/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009423 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009424 */
9425 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009426readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009427{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009428 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009429 typval_T save_val;
9430 typval_T rettv;
9431 typval_T argv[2];
9432 int retval = 0;
9433 int error = FALSE;
9434
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009435 if (expr->v_type == VAR_UNKNOWN)
9436 return 1;
9437
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009438 prepare_vimvar(VV_VAL, &save_val);
9439 set_vim_var_string(VV_VAL, name, -1);
9440 argv[0].v_type = VAR_STRING;
9441 argv[0].vval.v_string = name;
9442
9443 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9444 goto theend;
9445
9446 retval = tv_get_number_chk(&rettv, &error);
9447 if (error)
9448 retval = -1;
9449 clear_tv(&rettv);
9450
9451theend:
9452 set_vim_var_string(VV_VAL, NULL, 0);
9453 restore_vimvar(VV_VAL, &save_val);
9454 return retval;
9455}
9456
9457/*
9458 * "readdir()" function
9459 */
9460 static void
9461f_readdir(typval_T *argvars, typval_T *rettv)
9462{
9463 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009464 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009465 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009466 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009467 garray_T ga;
9468 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009469
9470 if (rettv_list_alloc(rettv) == FAIL)
9471 return;
9472 path = tv_get_string(&argvars[0]);
9473 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009474
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009475 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9476 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009477 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009478 for (i = 0; i < ga.ga_len; i++)
9479 {
9480 p = ((char_u **)ga.ga_data)[i];
9481 list_append_string(rettv->vval.v_list, p, -1);
9482 }
9483 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009484 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009485}
9486
9487/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009488 * "readfile()" function
9489 */
9490 static void
9491f_readfile(typval_T *argvars, typval_T *rettv)
9492{
9493 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009494 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495 int failed = FALSE;
9496 char_u *fname;
9497 FILE *fd;
9498 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9499 int io_size = sizeof(buf);
9500 int readlen; /* size of last fread() */
9501 char_u *prev = NULL; /* previously read bytes, if any */
9502 long prevlen = 0; /* length of data in prev */
9503 long prevsize = 0; /* size of prev buffer */
9504 long maxline = MAXLNUM;
9505 long cnt = 0;
9506 char_u *p; /* position in buf */
9507 char_u *start; /* start of current line */
9508
9509 if (argvars[1].v_type != VAR_UNKNOWN)
9510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009511 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009512 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009513 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9514 blob = TRUE;
9515
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009517 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009518 }
9519
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009520 if (blob)
9521 {
9522 if (rettv_blob_alloc(rettv) == FAIL)
9523 return;
9524 }
9525 else
9526 {
9527 if (rettv_list_alloc(rettv) == FAIL)
9528 return;
9529 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009530
9531 /* Always open the file in binary mode, library functions have a mind of
9532 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009533 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009534 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009536 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009537 return;
9538 }
9539
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009540 if (blob)
9541 {
9542 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009544 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009545 blob_free(rettv->vval.v_blob);
9546 }
9547 fclose(fd);
9548 return;
9549 }
9550
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009551 while (cnt < maxline || maxline < 0)
9552 {
9553 readlen = (int)fread(buf, 1, io_size, fd);
9554
9555 /* This for loop processes what was read, but is also entered at end
9556 * of file so that either:
9557 * - an incomplete line gets written
9558 * - a "binary" file gets an empty line at the end if it ends in a
9559 * newline. */
9560 for (p = buf, start = buf;
9561 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9562 ++p)
9563 {
9564 if (*p == '\n' || readlen <= 0)
9565 {
9566 listitem_T *li;
9567 char_u *s = NULL;
9568 long_u len = p - start;
9569
9570 /* Finished a line. Remove CRs before NL. */
9571 if (readlen > 0 && !binary)
9572 {
9573 while (len > 0 && start[len - 1] == '\r')
9574 --len;
9575 /* removal may cross back to the "prev" string */
9576 if (len == 0)
9577 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9578 --prevlen;
9579 }
9580 if (prevlen == 0)
9581 s = vim_strnsave(start, (int)len);
9582 else
9583 {
9584 /* Change "prev" buffer to be the right size. This way
9585 * the bytes are only copied once, and very long lines are
9586 * allocated only once. */
9587 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9588 {
9589 mch_memmove(s + prevlen, start, len);
9590 s[prevlen + len] = NUL;
9591 prev = NULL; /* the list will own the string */
9592 prevlen = prevsize = 0;
9593 }
9594 }
9595 if (s == NULL)
9596 {
9597 do_outofmem_msg((long_u) prevlen + len + 1);
9598 failed = TRUE;
9599 break;
9600 }
9601
9602 if ((li = listitem_alloc()) == NULL)
9603 {
9604 vim_free(s);
9605 failed = TRUE;
9606 break;
9607 }
9608 li->li_tv.v_type = VAR_STRING;
9609 li->li_tv.v_lock = 0;
9610 li->li_tv.vval.v_string = s;
9611 list_append(rettv->vval.v_list, li);
9612
9613 start = p + 1; /* step over newline */
9614 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9615 break;
9616 }
9617 else if (*p == NUL)
9618 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9620 * when finding the BF and check the previous two bytes. */
9621 else if (*p == 0xbf && enc_utf8 && !binary)
9622 {
9623 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9624 * + 1, these may be in the "prev" string. */
9625 char_u back1 = p >= buf + 1 ? p[-1]
9626 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9627 char_u back2 = p >= buf + 2 ? p[-2]
9628 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9629 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9630
9631 if (back2 == 0xef && back1 == 0xbb)
9632 {
9633 char_u *dest = p - 2;
9634
9635 /* Usually a BOM is at the beginning of a file, and so at
9636 * the beginning of a line; then we can just step over it.
9637 */
9638 if (start == dest)
9639 start = p + 1;
9640 else
9641 {
9642 /* have to shuffle buf to close gap */
9643 int adjust_prevlen = 0;
9644
9645 if (dest < buf)
9646 {
9647 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9648 dest = buf;
9649 }
9650 if (readlen > p - buf + 1)
9651 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9652 readlen -= 3 - adjust_prevlen;
9653 prevlen -= adjust_prevlen;
9654 p = dest - 1;
9655 }
9656 }
9657 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009658 } /* for */
9659
9660 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9661 break;
9662 if (start < p)
9663 {
9664 /* There's part of a line in buf, store it in "prev". */
9665 if (p - start + prevlen >= prevsize)
9666 {
9667 /* need bigger "prev" buffer */
9668 char_u *newprev;
9669
9670 /* A common use case is ordinary text files and "prev" gets a
9671 * fragment of a line, so the first allocation is made
9672 * small, to avoid repeatedly 'allocing' large and
9673 * 'reallocing' small. */
9674 if (prevsize == 0)
9675 prevsize = (long)(p - start);
9676 else
9677 {
9678 long grow50pc = (prevsize * 3) / 2;
9679 long growmin = (long)((p - start) * 2 + prevlen);
9680 prevsize = grow50pc > growmin ? grow50pc : growmin;
9681 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009682 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683 if (newprev == NULL)
9684 {
9685 do_outofmem_msg((long_u)prevsize);
9686 failed = TRUE;
9687 break;
9688 }
9689 prev = newprev;
9690 }
9691 /* Add the line part to end of "prev". */
9692 mch_memmove(prev + prevlen, start, p - start);
9693 prevlen += (long)(p - start);
9694 }
9695 } /* while */
9696
9697 /*
9698 * For a negative line count use only the lines at the end of the file,
9699 * free the rest.
9700 */
9701 if (!failed && maxline < 0)
9702 while (cnt > -maxline)
9703 {
9704 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9705 --cnt;
9706 }
9707
9708 if (failed)
9709 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009710 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009712 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009713 }
9714
9715 vim_free(prev);
9716 fclose(fd);
9717}
9718
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009719 static void
9720return_register(int regname, typval_T *rettv)
9721{
9722 char_u buf[2] = {0, 0};
9723
9724 buf[0] = (char_u)regname;
9725 rettv->v_type = VAR_STRING;
9726 rettv->vval.v_string = vim_strsave(buf);
9727}
9728
9729/*
9730 * "reg_executing()" function
9731 */
9732 static void
9733f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9734{
9735 return_register(reg_executing, rettv);
9736}
9737
9738/*
9739 * "reg_recording()" function
9740 */
9741 static void
9742f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9743{
9744 return_register(reg_recording, rettv);
9745}
9746
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009747#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748/*
9749 * Convert a List to proftime_T.
9750 * Return FAIL when there is something wrong.
9751 */
9752 static int
9753list2proftime(typval_T *arg, proftime_T *tm)
9754{
9755 long n1, n2;
9756 int error = FALSE;
9757
9758 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9759 || arg->vval.v_list->lv_len != 2)
9760 return FAIL;
9761 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9762 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009763# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009764 tm->HighPart = n1;
9765 tm->LowPart = n2;
9766# else
9767 tm->tv_sec = n1;
9768 tm->tv_usec = n2;
9769# endif
9770 return error ? FAIL : OK;
9771}
9772#endif /* FEAT_RELTIME */
9773
9774/*
9775 * "reltime()" function
9776 */
9777 static void
9778f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9779{
9780#ifdef FEAT_RELTIME
9781 proftime_T res;
9782 proftime_T start;
9783
9784 if (argvars[0].v_type == VAR_UNKNOWN)
9785 {
9786 /* No arguments: get current time. */
9787 profile_start(&res);
9788 }
9789 else if (argvars[1].v_type == VAR_UNKNOWN)
9790 {
9791 if (list2proftime(&argvars[0], &res) == FAIL)
9792 return;
9793 profile_end(&res);
9794 }
9795 else
9796 {
9797 /* Two arguments: compute the difference. */
9798 if (list2proftime(&argvars[0], &start) == FAIL
9799 || list2proftime(&argvars[1], &res) == FAIL)
9800 return;
9801 profile_sub(&res, &start);
9802 }
9803
9804 if (rettv_list_alloc(rettv) == OK)
9805 {
9806 long n1, n2;
9807
Bram Moolenaar4f974752019-02-17 17:44:42 +01009808# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009809 n1 = res.HighPart;
9810 n2 = res.LowPart;
9811# else
9812 n1 = res.tv_sec;
9813 n2 = res.tv_usec;
9814# endif
9815 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9816 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9817 }
9818#endif
9819}
9820
9821#ifdef FEAT_FLOAT
9822/*
9823 * "reltimefloat()" function
9824 */
9825 static void
9826f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9827{
9828# ifdef FEAT_RELTIME
9829 proftime_T tm;
9830# endif
9831
9832 rettv->v_type = VAR_FLOAT;
9833 rettv->vval.v_float = 0;
9834# ifdef FEAT_RELTIME
9835 if (list2proftime(&argvars[0], &tm) == OK)
9836 rettv->vval.v_float = profile_float(&tm);
9837# endif
9838}
9839#endif
9840
9841/*
9842 * "reltimestr()" function
9843 */
9844 static void
9845f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9846{
9847#ifdef FEAT_RELTIME
9848 proftime_T tm;
9849#endif
9850
9851 rettv->v_type = VAR_STRING;
9852 rettv->vval.v_string = NULL;
9853#ifdef FEAT_RELTIME
9854 if (list2proftime(&argvars[0], &tm) == OK)
9855 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9856#endif
9857}
9858
9859#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860 static void
9861make_connection(void)
9862{
9863 if (X_DISPLAY == NULL
9864# ifdef FEAT_GUI
9865 && !gui.in_use
9866# endif
9867 )
9868 {
9869 x_force_connect = TRUE;
9870 setup_term_clip();
9871 x_force_connect = FALSE;
9872 }
9873}
9874
9875 static int
9876check_connection(void)
9877{
9878 make_connection();
9879 if (X_DISPLAY == NULL)
9880 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009881 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 return FAIL;
9883 }
9884 return OK;
9885}
9886#endif
9887
9888#ifdef FEAT_CLIENTSERVER
9889 static void
9890remote_common(typval_T *argvars, typval_T *rettv, int expr)
9891{
9892 char_u *server_name;
9893 char_u *keys;
9894 char_u *r = NULL;
9895 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009896 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009897# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009898 HWND w;
9899# else
9900 Window w;
9901# endif
9902
9903 if (check_restricted() || check_secure())
9904 return;
9905
9906# ifdef FEAT_X11
9907 if (check_connection() == FAIL)
9908 return;
9909# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009910 if (argvars[2].v_type != VAR_UNKNOWN
9911 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009912 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009914 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009915 if (server_name == NULL)
9916 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009917 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009918# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009919 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009921 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9922 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923# endif
9924 {
9925 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009927 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009928 vim_free(r);
9929 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009931 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009932 return;
9933 }
9934
9935 rettv->vval.v_string = r;
9936
9937 if (argvars[2].v_type != VAR_UNKNOWN)
9938 {
9939 dictitem_T v;
9940 char_u str[30];
9941 char_u *idvar;
9942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009943 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009944 if (idvar != NULL && *idvar != NUL)
9945 {
9946 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9947 v.di_tv.v_type = VAR_STRING;
9948 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009950 vim_free(v.di_tv.vval.v_string);
9951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009952 }
9953}
9954#endif
9955
9956/*
9957 * "remote_expr()" function
9958 */
9959 static void
9960f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9961{
9962 rettv->v_type = VAR_STRING;
9963 rettv->vval.v_string = NULL;
9964#ifdef FEAT_CLIENTSERVER
9965 remote_common(argvars, rettv, TRUE);
9966#endif
9967}
9968
9969/*
9970 * "remote_foreground()" function
9971 */
9972 static void
9973f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9974{
9975#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009976# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009977 /* On Win32 it's done in this application. */
9978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009979 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980
9981 if (server_name != NULL)
9982 serverForeground(server_name);
9983 }
9984# else
9985 /* Send a foreground() expression to the server. */
9986 argvars[1].v_type = VAR_STRING;
9987 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9988 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009989 rettv->v_type = VAR_STRING;
9990 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 remote_common(argvars, rettv, TRUE);
9992 vim_free(argvars[1].vval.v_string);
9993# endif
9994#endif
9995}
9996
9997 static void
9998f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9999{
10000#ifdef FEAT_CLIENTSERVER
10001 dictitem_T v;
10002 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010003# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 long_u n = 0;
10005# endif
10006 char_u *serverid;
10007
10008 if (check_restricted() || check_secure())
10009 {
10010 rettv->vval.v_number = -1;
10011 return;
10012 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010013 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 if (serverid == NULL)
10015 {
10016 rettv->vval.v_number = -1;
10017 return; /* type error; errmsg already given */
10018 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010019# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010020 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10021 if (n == 0)
10022 rettv->vval.v_number = -1;
10023 else
10024 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010025 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010026 rettv->vval.v_number = (s != NULL);
10027 }
10028# else
10029 if (check_connection() == FAIL)
10030 return;
10031
10032 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10033 serverStrToWin(serverid), &s);
10034# endif
10035
10036 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10037 {
10038 char_u *retvar;
10039
10040 v.di_tv.v_type = VAR_STRING;
10041 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010042 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 if (retvar != NULL)
10044 set_var(retvar, &v.di_tv, FALSE);
10045 vim_free(v.di_tv.vval.v_string);
10046 }
10047#else
10048 rettv->vval.v_number = -1;
10049#endif
10050}
10051
10052 static void
10053f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10054{
10055 char_u *r = NULL;
10056
10057#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010058 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010059
10060 if (serverid != NULL && !check_restricted() && !check_secure())
10061 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010062 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010063# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010064 /* The server's HWND is encoded in the 'id' parameter */
10065 long_u n = 0;
10066# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010067
10068 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010069 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010070
Bram Moolenaar4f974752019-02-17 17:44:42 +010010071# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010072 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10073 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010074 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010075 if (r == NULL)
10076# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010077 if (check_connection() == FAIL
10078 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10079 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010081 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 }
10083#endif
10084 rettv->v_type = VAR_STRING;
10085 rettv->vval.v_string = r;
10086}
10087
10088/*
10089 * "remote_send()" function
10090 */
10091 static void
10092f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10093{
10094 rettv->v_type = VAR_STRING;
10095 rettv->vval.v_string = NULL;
10096#ifdef FEAT_CLIENTSERVER
10097 remote_common(argvars, rettv, FALSE);
10098#endif
10099}
10100
10101/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010102 * "remote_startserver()" function
10103 */
10104 static void
10105f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10106{
10107#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010108 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010109
10110 if (server == NULL)
10111 return; /* type error; errmsg already given */
10112 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010113 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010114 else
10115 {
10116# ifdef FEAT_X11
10117 if (check_connection() == OK)
10118 serverRegisterName(X_DISPLAY, server);
10119# else
10120 serverSetName(server);
10121# endif
10122 }
10123#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010124 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010125#endif
10126}
10127
10128/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 * "remove()" function
10130 */
10131 static void
10132f_remove(typval_T *argvars, typval_T *rettv)
10133{
10134 list_T *l;
10135 listitem_T *item, *item2;
10136 listitem_T *li;
10137 long idx;
10138 long end;
10139 char_u *key;
10140 dict_T *d;
10141 dictitem_T *di;
10142 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010143 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010144
10145 if (argvars[0].v_type == VAR_DICT)
10146 {
10147 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010148 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010149 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010150 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010152 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010153 if (key != NULL)
10154 {
10155 di = dict_find(d, key, -1);
10156 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010157 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10159 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10160 {
10161 *rettv = di->di_tv;
10162 init_tv(&di->di_tv);
10163 dictitem_remove(d, di);
10164 }
10165 }
10166 }
10167 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010168 else if (argvars[0].v_type == VAR_BLOB)
10169 {
10170 idx = (long)tv_get_number_chk(&argvars[1], &error);
10171 if (!error)
10172 {
10173 blob_T *b = argvars[0].vval.v_blob;
10174 int len = blob_len(b);
10175 char_u *p;
10176
10177 if (idx < 0)
10178 // count from the end
10179 idx = len + idx;
10180 if (idx < 0 || idx >= len)
10181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010182 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010183 return;
10184 }
10185 if (argvars[2].v_type == VAR_UNKNOWN)
10186 {
10187 // Remove one item, return its value.
10188 p = (char_u *)b->bv_ga.ga_data;
10189 rettv->vval.v_number = (varnumber_T) *(p + idx);
10190 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10191 --b->bv_ga.ga_len;
10192 }
10193 else
10194 {
10195 blob_T *blob;
10196
10197 // Remove range of items, return list with values.
10198 end = (long)tv_get_number_chk(&argvars[2], &error);
10199 if (error)
10200 return;
10201 if (end < 0)
10202 // count from the end
10203 end = len + end;
10204 if (end >= len || idx > end)
10205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010206 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010207 return;
10208 }
10209 blob = blob_alloc();
10210 if (blob == NULL)
10211 return;
10212 blob->bv_ga.ga_len = end - idx + 1;
10213 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10214 {
10215 vim_free(blob);
10216 return;
10217 }
10218 p = (char_u *)b->bv_ga.ga_data;
10219 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10220 (size_t)(end - idx + 1));
10221 ++blob->bv_refcount;
10222 rettv->v_type = VAR_BLOB;
10223 rettv->vval.v_blob = blob;
10224
10225 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10226 b->bv_ga.ga_len -= end - idx + 1;
10227 }
10228 }
10229 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010230 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010231 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010232 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010233 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010234 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010235 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010237 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010239 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else
10241 {
10242 if (argvars[2].v_type == VAR_UNKNOWN)
10243 {
10244 /* Remove one item, return its value. */
10245 vimlist_remove(l, item, item);
10246 *rettv = item->li_tv;
10247 vim_free(item);
10248 }
10249 else
10250 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010251 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010252 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010254 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010255 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010256 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010257 else
10258 {
10259 int cnt = 0;
10260
10261 for (li = item; li != NULL; li = li->li_next)
10262 {
10263 ++cnt;
10264 if (li == item2)
10265 break;
10266 }
10267 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010268 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010269 else
10270 {
10271 vimlist_remove(l, item, item2);
10272 if (rettv_list_alloc(rettv) == OK)
10273 {
10274 l = rettv->vval.v_list;
10275 l->lv_first = item;
10276 l->lv_last = item2;
10277 item->li_prev = NULL;
10278 item2->li_next = NULL;
10279 l->lv_len = cnt;
10280 }
10281 }
10282 }
10283 }
10284 }
10285 }
10286}
10287
10288/*
10289 * "rename({from}, {to})" function
10290 */
10291 static void
10292f_rename(typval_T *argvars, typval_T *rettv)
10293{
10294 char_u buf[NUMBUFLEN];
10295
10296 if (check_restricted() || check_secure())
10297 rettv->vval.v_number = -1;
10298 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010299 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10300 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301}
10302
10303/*
10304 * "repeat()" function
10305 */
10306 static void
10307f_repeat(typval_T *argvars, typval_T *rettv)
10308{
10309 char_u *p;
10310 int n;
10311 int slen;
10312 int len;
10313 char_u *r;
10314 int i;
10315
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010316 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010317 if (argvars[0].v_type == VAR_LIST)
10318 {
10319 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10320 while (n-- > 0)
10321 if (list_extend(rettv->vval.v_list,
10322 argvars[0].vval.v_list, NULL) == FAIL)
10323 break;
10324 }
10325 else
10326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010327 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 rettv->v_type = VAR_STRING;
10329 rettv->vval.v_string = NULL;
10330
10331 slen = (int)STRLEN(p);
10332 len = slen * n;
10333 if (len <= 0)
10334 return;
10335
10336 r = alloc(len + 1);
10337 if (r != NULL)
10338 {
10339 for (i = 0; i < n; i++)
10340 mch_memmove(r + i * slen, p, (size_t)slen);
10341 r[len] = NUL;
10342 }
10343
10344 rettv->vval.v_string = r;
10345 }
10346}
10347
10348/*
10349 * "resolve()" function
10350 */
10351 static void
10352f_resolve(typval_T *argvars, typval_T *rettv)
10353{
10354 char_u *p;
10355#ifdef HAVE_READLINK
10356 char_u *buf = NULL;
10357#endif
10358
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010359 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010360#ifdef FEAT_SHORTCUT
10361 {
10362 char_u *v = NULL;
10363
Bram Moolenaardce1e892019-02-10 23:18:53 +010010364 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010365 if (v != NULL)
10366 rettv->vval.v_string = v;
10367 else
10368 rettv->vval.v_string = vim_strsave(p);
10369 }
10370#else
10371# ifdef HAVE_READLINK
10372 {
10373 char_u *cpy;
10374 int len;
10375 char_u *remain = NULL;
10376 char_u *q;
10377 int is_relative_to_current = FALSE;
10378 int has_trailing_pathsep = FALSE;
10379 int limit = 100;
10380
10381 p = vim_strsave(p);
10382
10383 if (p[0] == '.' && (vim_ispathsep(p[1])
10384 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10385 is_relative_to_current = TRUE;
10386
10387 len = STRLEN(p);
10388 if (len > 0 && after_pathsep(p, p + len))
10389 {
10390 has_trailing_pathsep = TRUE;
10391 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10392 }
10393
10394 q = getnextcomp(p);
10395 if (*q != NUL)
10396 {
10397 /* Separate the first path component in "p", and keep the
10398 * remainder (beginning with the path separator). */
10399 remain = vim_strsave(q - 1);
10400 q[-1] = NUL;
10401 }
10402
10403 buf = alloc(MAXPATHL + 1);
10404 if (buf == NULL)
10405 goto fail;
10406
10407 for (;;)
10408 {
10409 for (;;)
10410 {
10411 len = readlink((char *)p, (char *)buf, MAXPATHL);
10412 if (len <= 0)
10413 break;
10414 buf[len] = NUL;
10415
10416 if (limit-- == 0)
10417 {
10418 vim_free(p);
10419 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010420 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010421 rettv->vval.v_string = NULL;
10422 goto fail;
10423 }
10424
10425 /* Ensure that the result will have a trailing path separator
10426 * if the argument has one. */
10427 if (remain == NULL && has_trailing_pathsep)
10428 add_pathsep(buf);
10429
10430 /* Separate the first path component in the link value and
10431 * concatenate the remainders. */
10432 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10433 if (*q != NUL)
10434 {
10435 if (remain == NULL)
10436 remain = vim_strsave(q - 1);
10437 else
10438 {
10439 cpy = concat_str(q - 1, remain);
10440 if (cpy != NULL)
10441 {
10442 vim_free(remain);
10443 remain = cpy;
10444 }
10445 }
10446 q[-1] = NUL;
10447 }
10448
10449 q = gettail(p);
10450 if (q > p && *q == NUL)
10451 {
10452 /* Ignore trailing path separator. */
10453 q[-1] = NUL;
10454 q = gettail(p);
10455 }
10456 if (q > p && !mch_isFullName(buf))
10457 {
10458 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010459 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010460 if (cpy != NULL)
10461 {
10462 STRCPY(cpy, p);
10463 STRCPY(gettail(cpy), buf);
10464 vim_free(p);
10465 p = cpy;
10466 }
10467 }
10468 else
10469 {
10470 vim_free(p);
10471 p = vim_strsave(buf);
10472 }
10473 }
10474
10475 if (remain == NULL)
10476 break;
10477
10478 /* Append the first path component of "remain" to "p". */
10479 q = getnextcomp(remain + 1);
10480 len = q - remain - (*q != NUL);
10481 cpy = vim_strnsave(p, STRLEN(p) + len);
10482 if (cpy != NULL)
10483 {
10484 STRNCAT(cpy, remain, len);
10485 vim_free(p);
10486 p = cpy;
10487 }
10488 /* Shorten "remain". */
10489 if (*q != NUL)
10490 STRMOVE(remain, q - 1);
10491 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010492 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010493 }
10494
10495 /* If the result is a relative path name, make it explicitly relative to
10496 * the current directory if and only if the argument had this form. */
10497 if (!vim_ispathsep(*p))
10498 {
10499 if (is_relative_to_current
10500 && *p != NUL
10501 && !(p[0] == '.'
10502 && (p[1] == NUL
10503 || vim_ispathsep(p[1])
10504 || (p[1] == '.'
10505 && (p[2] == NUL
10506 || vim_ispathsep(p[2]))))))
10507 {
10508 /* Prepend "./". */
10509 cpy = concat_str((char_u *)"./", p);
10510 if (cpy != NULL)
10511 {
10512 vim_free(p);
10513 p = cpy;
10514 }
10515 }
10516 else if (!is_relative_to_current)
10517 {
10518 /* Strip leading "./". */
10519 q = p;
10520 while (q[0] == '.' && vim_ispathsep(q[1]))
10521 q += 2;
10522 if (q > p)
10523 STRMOVE(p, p + 2);
10524 }
10525 }
10526
10527 /* Ensure that the result will have no trailing path separator
10528 * if the argument had none. But keep "/" or "//". */
10529 if (!has_trailing_pathsep)
10530 {
10531 q = p + STRLEN(p);
10532 if (after_pathsep(p, q))
10533 *gettail_sep(p) = NUL;
10534 }
10535
10536 rettv->vval.v_string = p;
10537 }
10538# else
10539 rettv->vval.v_string = vim_strsave(p);
10540# endif
10541#endif
10542
10543 simplify_filename(rettv->vval.v_string);
10544
10545#ifdef HAVE_READLINK
10546fail:
10547 vim_free(buf);
10548#endif
10549 rettv->v_type = VAR_STRING;
10550}
10551
10552/*
10553 * "reverse({list})" function
10554 */
10555 static void
10556f_reverse(typval_T *argvars, typval_T *rettv)
10557{
10558 list_T *l;
10559 listitem_T *li, *ni;
10560
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010561 if (argvars[0].v_type == VAR_BLOB)
10562 {
10563 blob_T *b = argvars[0].vval.v_blob;
10564 int i, len = blob_len(b);
10565
10566 for (i = 0; i < len / 2; i++)
10567 {
10568 int tmp = blob_get(b, i);
10569
10570 blob_set(b, i, blob_get(b, len - i - 1));
10571 blob_set(b, len - i - 1, tmp);
10572 }
10573 rettv_blob_set(rettv, b);
10574 return;
10575 }
10576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010578 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010579 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010580 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010581 (char_u *)N_("reverse() argument"), TRUE))
10582 {
10583 li = l->lv_last;
10584 l->lv_first = l->lv_last = NULL;
10585 l->lv_len = 0;
10586 while (li != NULL)
10587 {
10588 ni = li->li_prev;
10589 list_append(l, li);
10590 li = ni;
10591 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010592 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 l->lv_idx = l->lv_len - l->lv_idx - 1;
10594 }
10595}
10596
10597#define SP_NOMOVE 0x01 /* don't move cursor */
10598#define SP_REPEAT 0x02 /* repeat to find outer pair */
10599#define SP_RETCOUNT 0x04 /* return matchcount */
10600#define SP_SETPCMARK 0x08 /* set previous context mark */
10601#define SP_START 0x10 /* accept match at start position */
10602#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10603#define SP_END 0x40 /* leave cursor at end of match */
10604#define SP_COLUMN 0x80 /* start at cursor column */
10605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606/*
10607 * Get flags for a search function.
10608 * Possibly sets "p_ws".
10609 * Returns BACKWARD, FORWARD or zero (for an error).
10610 */
10611 static int
10612get_search_arg(typval_T *varp, int *flagsp)
10613{
10614 int dir = FORWARD;
10615 char_u *flags;
10616 char_u nbuf[NUMBUFLEN];
10617 int mask;
10618
10619 if (varp->v_type != VAR_UNKNOWN)
10620 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010621 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010622 if (flags == NULL)
10623 return 0; /* type error; errmsg already given */
10624 while (*flags != NUL)
10625 {
10626 switch (*flags)
10627 {
10628 case 'b': dir = BACKWARD; break;
10629 case 'w': p_ws = TRUE; break;
10630 case 'W': p_ws = FALSE; break;
10631 default: mask = 0;
10632 if (flagsp != NULL)
10633 switch (*flags)
10634 {
10635 case 'c': mask = SP_START; break;
10636 case 'e': mask = SP_END; break;
10637 case 'm': mask = SP_RETCOUNT; break;
10638 case 'n': mask = SP_NOMOVE; break;
10639 case 'p': mask = SP_SUBPAT; break;
10640 case 'r': mask = SP_REPEAT; break;
10641 case 's': mask = SP_SETPCMARK; break;
10642 case 'z': mask = SP_COLUMN; break;
10643 }
10644 if (mask == 0)
10645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010646 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010647 dir = 0;
10648 }
10649 else
10650 *flagsp |= mask;
10651 }
10652 if (dir == 0)
10653 break;
10654 ++flags;
10655 }
10656 }
10657 return dir;
10658}
10659
10660/*
10661 * Shared by search() and searchpos() functions.
10662 */
10663 static int
10664search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10665{
10666 int flags;
10667 char_u *pat;
10668 pos_T pos;
10669 pos_T save_cursor;
10670 int save_p_ws = p_ws;
10671 int dir;
10672 int retval = 0; /* default: FAIL */
10673 long lnum_stop = 0;
10674 proftime_T tm;
10675#ifdef FEAT_RELTIME
10676 long time_limit = 0;
10677#endif
10678 int options = SEARCH_KEEP;
10679 int subpatnum;
10680
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010681 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010682 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10683 if (dir == 0)
10684 goto theend;
10685 flags = *flagsp;
10686 if (flags & SP_START)
10687 options |= SEARCH_START;
10688 if (flags & SP_END)
10689 options |= SEARCH_END;
10690 if (flags & SP_COLUMN)
10691 options |= SEARCH_COL;
10692
10693 /* Optional arguments: line number to stop searching and timeout. */
10694 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10695 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010696 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010697 if (lnum_stop < 0)
10698 goto theend;
10699#ifdef FEAT_RELTIME
10700 if (argvars[3].v_type != VAR_UNKNOWN)
10701 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010702 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010703 if (time_limit < 0)
10704 goto theend;
10705 }
10706#endif
10707 }
10708
10709#ifdef FEAT_RELTIME
10710 /* Set the time limit, if there is one. */
10711 profile_setlimit(time_limit, &tm);
10712#endif
10713
10714 /*
10715 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10716 * Check to make sure only those flags are set.
10717 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10718 * flags cannot be set. Check for that condition also.
10719 */
10720 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10721 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010723 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010724 goto theend;
10725 }
10726
10727 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010728 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010729 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010730 if (subpatnum != FAIL)
10731 {
10732 if (flags & SP_SUBPAT)
10733 retval = subpatnum;
10734 else
10735 retval = pos.lnum;
10736 if (flags & SP_SETPCMARK)
10737 setpcmark();
10738 curwin->w_cursor = pos;
10739 if (match_pos != NULL)
10740 {
10741 /* Store the match cursor position */
10742 match_pos->lnum = pos.lnum;
10743 match_pos->col = pos.col + 1;
10744 }
10745 /* "/$" will put the cursor after the end of the line, may need to
10746 * correct that here */
10747 check_cursor();
10748 }
10749
10750 /* If 'n' flag is used: restore cursor position. */
10751 if (flags & SP_NOMOVE)
10752 curwin->w_cursor = save_cursor;
10753 else
10754 curwin->w_set_curswant = TRUE;
10755theend:
10756 p_ws = save_p_ws;
10757
10758 return retval;
10759}
10760
10761#ifdef FEAT_FLOAT
10762
10763/*
10764 * round() is not in C90, use ceil() or floor() instead.
10765 */
10766 float_T
10767vim_round(float_T f)
10768{
10769 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10770}
10771
10772/*
10773 * "round({float})" function
10774 */
10775 static void
10776f_round(typval_T *argvars, typval_T *rettv)
10777{
10778 float_T f = 0.0;
10779
10780 rettv->v_type = VAR_FLOAT;
10781 if (get_float_arg(argvars, &f) == OK)
10782 rettv->vval.v_float = vim_round(f);
10783 else
10784 rettv->vval.v_float = 0.0;
10785}
10786#endif
10787
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010788#ifdef FEAT_RUBY
10789/*
10790 * "rubyeval()" function
10791 */
10792 static void
10793f_rubyeval(typval_T *argvars, typval_T *rettv)
10794{
10795 char_u *str;
10796 char_u buf[NUMBUFLEN];
10797
10798 str = tv_get_string_buf(&argvars[0], buf);
10799 do_rubyeval(str, rettv);
10800}
10801#endif
10802
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010803/*
10804 * "screenattr()" function
10805 */
10806 static void
10807f_screenattr(typval_T *argvars, typval_T *rettv)
10808{
10809 int row;
10810 int col;
10811 int c;
10812
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010813 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10814 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 if (row < 0 || row >= screen_Rows
10816 || col < 0 || col >= screen_Columns)
10817 c = -1;
10818 else
10819 c = ScreenAttrs[LineOffset[row] + col];
10820 rettv->vval.v_number = c;
10821}
10822
10823/*
10824 * "screenchar()" function
10825 */
10826 static void
10827f_screenchar(typval_T *argvars, typval_T *rettv)
10828{
10829 int row;
10830 int col;
10831 int off;
10832 int c;
10833
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010834 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10835 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010836 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010837 c = -1;
10838 else
10839 {
10840 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010841 if (enc_utf8 && ScreenLinesUC[off] != 0)
10842 c = ScreenLinesUC[off];
10843 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 c = ScreenLines[off];
10845 }
10846 rettv->vval.v_number = c;
10847}
10848
10849/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010850 * "screenchars()" function
10851 */
10852 static void
10853f_screenchars(typval_T *argvars, typval_T *rettv)
10854{
10855 int row;
10856 int col;
10857 int off;
10858 int c;
10859 int i;
10860
10861 if (rettv_list_alloc(rettv) == FAIL)
10862 return;
10863 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10864 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10865 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10866 return;
10867
10868 off = LineOffset[row] + col;
10869 if (enc_utf8 && ScreenLinesUC[off] != 0)
10870 c = ScreenLinesUC[off];
10871 else
10872 c = ScreenLines[off];
10873 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10874
10875 if (enc_utf8)
10876
10877 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10878 list_append_number(rettv->vval.v_list,
10879 (varnumber_T)ScreenLinesC[i][off]);
10880}
10881
10882/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010883 * "screencol()" function
10884 *
10885 * First column is 1 to be consistent with virtcol().
10886 */
10887 static void
10888f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10889{
10890 rettv->vval.v_number = screen_screencol() + 1;
10891}
10892
10893/*
10894 * "screenrow()" function
10895 */
10896 static void
10897f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10898{
10899 rettv->vval.v_number = screen_screenrow() + 1;
10900}
10901
10902/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010903 * "screenstring()" function
10904 */
10905 static void
10906f_screenstring(typval_T *argvars, typval_T *rettv)
10907{
10908 int row;
10909 int col;
10910 int off;
10911 int c;
10912 int i;
10913 char_u buf[MB_MAXBYTES + 1];
10914 int buflen = 0;
10915
10916 rettv->vval.v_string = NULL;
10917 rettv->v_type = VAR_STRING;
10918
10919 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10920 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10921 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10922 return;
10923
10924 off = LineOffset[row] + col;
10925 if (enc_utf8 && ScreenLinesUC[off] != 0)
10926 c = ScreenLinesUC[off];
10927 else
10928 c = ScreenLines[off];
10929 buflen += mb_char2bytes(c, buf);
10930
10931 if (enc_utf8)
10932 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10933 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10934
10935 buf[buflen] = NUL;
10936 rettv->vval.v_string = vim_strsave(buf);
10937}
10938
10939/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 * "search()" function
10941 */
10942 static void
10943f_search(typval_T *argvars, typval_T *rettv)
10944{
10945 int flags = 0;
10946
10947 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10948}
10949
10950/*
10951 * "searchdecl()" function
10952 */
10953 static void
10954f_searchdecl(typval_T *argvars, typval_T *rettv)
10955{
10956 int locally = 1;
10957 int thisblock = 0;
10958 int error = FALSE;
10959 char_u *name;
10960
10961 rettv->vval.v_number = 1; /* default: FAIL */
10962
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010963 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010964 if (argvars[1].v_type != VAR_UNKNOWN)
10965 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010966 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010967 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010968 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 }
10970 if (!error && name != NULL)
10971 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10972 locally, thisblock, SEARCH_KEEP) == FAIL;
10973}
10974
10975/*
10976 * Used by searchpair() and searchpairpos()
10977 */
10978 static int
10979searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10980{
10981 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010982 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 int save_p_ws = p_ws;
10984 int dir;
10985 int flags = 0;
10986 char_u nbuf1[NUMBUFLEN];
10987 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010988 int retval = 0; /* default: FAIL */
10989 long lnum_stop = 0;
10990 long time_limit = 0;
10991
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010992 /* Get the three pattern arguments: start, middle, end. Will result in an
10993 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010994 spat = tv_get_string_chk(&argvars[0]);
10995 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10996 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010997 if (spat == NULL || mpat == NULL || epat == NULL)
10998 goto theend; /* type error */
10999
11000 /* Handle the optional fourth argument: flags */
11001 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11002 if (dir == 0)
11003 goto theend;
11004
11005 /* Don't accept SP_END or SP_SUBPAT.
11006 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11007 */
11008 if ((flags & (SP_END | SP_SUBPAT)) != 0
11009 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011011 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012 goto theend;
11013 }
11014
11015 /* Using 'r' implies 'W', otherwise it doesn't work. */
11016 if (flags & SP_REPEAT)
11017 p_ws = FALSE;
11018
11019 /* Optional fifth argument: skip expression */
11020 if (argvars[3].v_type == VAR_UNKNOWN
11021 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011022 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 else
11024 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011025 skip = &argvars[4];
11026 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11027 && skip->v_type != VAR_STRING)
11028 {
11029 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011030 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011031 goto theend;
11032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011033 if (argvars[5].v_type != VAR_UNKNOWN)
11034 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011035 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011038 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011039 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011040 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041#ifdef FEAT_RELTIME
11042 if (argvars[6].v_type != VAR_UNKNOWN)
11043 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011044 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011045 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011047 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011049 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 }
11051#endif
11052 }
11053 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054
11055 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11056 match_pos, lnum_stop, time_limit);
11057
11058theend:
11059 p_ws = save_p_ws;
11060
11061 return retval;
11062}
11063
11064/*
11065 * "searchpair()" function
11066 */
11067 static void
11068f_searchpair(typval_T *argvars, typval_T *rettv)
11069{
11070 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11071}
11072
11073/*
11074 * "searchpairpos()" function
11075 */
11076 static void
11077f_searchpairpos(typval_T *argvars, typval_T *rettv)
11078{
11079 pos_T match_pos;
11080 int lnum = 0;
11081 int col = 0;
11082
11083 if (rettv_list_alloc(rettv) == FAIL)
11084 return;
11085
11086 if (searchpair_cmn(argvars, &match_pos) > 0)
11087 {
11088 lnum = match_pos.lnum;
11089 col = match_pos.col;
11090 }
11091
11092 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11093 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11094}
11095
11096/*
11097 * Search for a start/middle/end thing.
11098 * Used by searchpair(), see its documentation for the details.
11099 * Returns 0 or -1 for no match,
11100 */
11101 long
11102do_searchpair(
11103 char_u *spat, /* start pattern */
11104 char_u *mpat, /* middle pattern */
11105 char_u *epat, /* end pattern */
11106 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011107 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011108 int flags, /* SP_SETPCMARK and other SP_ values */
11109 pos_T *match_pos,
11110 linenr_T lnum_stop, /* stop at this line if not zero */
11111 long time_limit UNUSED) /* stop after this many msec */
11112{
11113 char_u *save_cpo;
11114 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11115 long retval = 0;
11116 pos_T pos;
11117 pos_T firstpos;
11118 pos_T foundpos;
11119 pos_T save_cursor;
11120 pos_T save_pos;
11121 int n;
11122 int r;
11123 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011124 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 int err;
11126 int options = SEARCH_KEEP;
11127 proftime_T tm;
11128
11129 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11130 save_cpo = p_cpo;
11131 p_cpo = empty_option;
11132
11133#ifdef FEAT_RELTIME
11134 /* Set the time limit, if there is one. */
11135 profile_setlimit(time_limit, &tm);
11136#endif
11137
11138 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11139 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011140 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11141 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142 if (pat2 == NULL || pat3 == NULL)
11143 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011144 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011145 if (*mpat == NUL)
11146 STRCPY(pat3, pat2);
11147 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011148 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011149 spat, epat, mpat);
11150 if (flags & SP_START)
11151 options |= SEARCH_START;
11152
Bram Moolenaar48570482017-10-30 21:48:41 +010011153 if (skip != NULL)
11154 {
11155 /* Empty string means to not use the skip expression. */
11156 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11157 use_skip = skip->vval.v_string != NULL
11158 && *skip->vval.v_string != NUL;
11159 }
11160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011161 save_cursor = curwin->w_cursor;
11162 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011163 CLEAR_POS(&firstpos);
11164 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011165 pat = pat3;
11166 for (;;)
11167 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011168 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011169 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011170 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011171 /* didn't find it or found the first match again: FAIL */
11172 break;
11173
11174 if (firstpos.lnum == 0)
11175 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011176 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011177 {
11178 /* Found the same position again. Can happen with a pattern that
11179 * has "\zs" at the end and searching backwards. Advance one
11180 * character and try again. */
11181 if (dir == BACKWARD)
11182 decl(&pos);
11183 else
11184 incl(&pos);
11185 }
11186 foundpos = pos;
11187
11188 /* clear the start flag to avoid getting stuck here */
11189 options &= ~SEARCH_START;
11190
11191 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011192 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011193 {
11194 save_pos = curwin->w_cursor;
11195 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011196 err = FALSE;
11197 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011198 curwin->w_cursor = save_pos;
11199 if (err)
11200 {
11201 /* Evaluating {skip} caused an error, break here. */
11202 curwin->w_cursor = save_cursor;
11203 retval = -1;
11204 break;
11205 }
11206 if (r)
11207 continue;
11208 }
11209
11210 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11211 {
11212 /* Found end when searching backwards or start when searching
11213 * forward: nested pair. */
11214 ++nest;
11215 pat = pat2; /* nested, don't search for middle */
11216 }
11217 else
11218 {
11219 /* Found end when searching forward or start when searching
11220 * backward: end of (nested) pair; or found middle in outer pair. */
11221 if (--nest == 1)
11222 pat = pat3; /* outer level, search for middle */
11223 }
11224
11225 if (nest == 0)
11226 {
11227 /* Found the match: return matchcount or line number. */
11228 if (flags & SP_RETCOUNT)
11229 ++retval;
11230 else
11231 retval = pos.lnum;
11232 if (flags & SP_SETPCMARK)
11233 setpcmark();
11234 curwin->w_cursor = pos;
11235 if (!(flags & SP_REPEAT))
11236 break;
11237 nest = 1; /* search for next unmatched */
11238 }
11239 }
11240
11241 if (match_pos != NULL)
11242 {
11243 /* Store the match cursor position */
11244 match_pos->lnum = curwin->w_cursor.lnum;
11245 match_pos->col = curwin->w_cursor.col + 1;
11246 }
11247
11248 /* If 'n' flag is used or search failed: restore cursor position. */
11249 if ((flags & SP_NOMOVE) || retval == 0)
11250 curwin->w_cursor = save_cursor;
11251
11252theend:
11253 vim_free(pat2);
11254 vim_free(pat3);
11255 if (p_cpo == empty_option)
11256 p_cpo = save_cpo;
11257 else
11258 /* Darn, evaluating the {skip} expression changed the value. */
11259 free_string_option(save_cpo);
11260
11261 return retval;
11262}
11263
11264/*
11265 * "searchpos()" function
11266 */
11267 static void
11268f_searchpos(typval_T *argvars, typval_T *rettv)
11269{
11270 pos_T match_pos;
11271 int lnum = 0;
11272 int col = 0;
11273 int n;
11274 int flags = 0;
11275
11276 if (rettv_list_alloc(rettv) == FAIL)
11277 return;
11278
11279 n = search_cmn(argvars, &match_pos, &flags);
11280 if (n > 0)
11281 {
11282 lnum = match_pos.lnum;
11283 col = match_pos.col;
11284 }
11285
11286 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11287 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11288 if (flags & SP_SUBPAT)
11289 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11290}
11291
11292 static void
11293f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11294{
11295#ifdef FEAT_CLIENTSERVER
11296 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011297 char_u *server = tv_get_string_chk(&argvars[0]);
11298 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299
11300 rettv->vval.v_number = -1;
11301 if (server == NULL || reply == NULL)
11302 return;
11303 if (check_restricted() || check_secure())
11304 return;
11305# ifdef FEAT_X11
11306 if (check_connection() == FAIL)
11307 return;
11308# endif
11309
11310 if (serverSendReply(server, reply) < 0)
11311 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011312 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011313 return;
11314 }
11315 rettv->vval.v_number = 0;
11316#else
11317 rettv->vval.v_number = -1;
11318#endif
11319}
11320
11321 static void
11322f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11323{
11324 char_u *r = NULL;
11325
11326#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011327# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011328 r = serverGetVimNames();
11329# else
11330 make_connection();
11331 if (X_DISPLAY != NULL)
11332 r = serverGetVimNames(X_DISPLAY);
11333# endif
11334#endif
11335 rettv->v_type = VAR_STRING;
11336 rettv->vval.v_string = r;
11337}
11338
11339/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011340 * "setbufline()" function
11341 */
11342 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011343f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011344{
11345 linenr_T lnum;
11346 buf_T *buf;
11347
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011348 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011349 if (buf == NULL)
11350 rettv->vval.v_number = 1; /* FAIL */
11351 else
11352 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011353 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011354 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011355 }
11356}
11357
11358/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011359 * "setbufvar()" function
11360 */
11361 static void
11362f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11363{
11364 buf_T *buf;
11365 char_u *varname, *bufvarname;
11366 typval_T *varp;
11367 char_u nbuf[NUMBUFLEN];
11368
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011369 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011371 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11372 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011373 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011374 varp = &argvars[2];
11375
11376 if (buf != NULL && varname != NULL && varp != NULL)
11377 {
11378 if (*varname == '&')
11379 {
11380 long numval;
11381 char_u *strval;
11382 int error = FALSE;
11383 aco_save_T aco;
11384
11385 /* set curbuf to be our buf, temporarily */
11386 aucmd_prepbuf(&aco, buf);
11387
11388 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011389 numval = (long)tv_get_number_chk(varp, &error);
11390 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011391 if (!error && strval != NULL)
11392 set_option_value(varname, numval, strval, OPT_LOCAL);
11393
11394 /* reset notion of buffer */
11395 aucmd_restbuf(&aco);
11396 }
11397 else
11398 {
11399 buf_T *save_curbuf = curbuf;
11400
Bram Moolenaar964b3742019-05-24 18:54:09 +020011401 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402 if (bufvarname != NULL)
11403 {
11404 curbuf = buf;
11405 STRCPY(bufvarname, "b:");
11406 STRCPY(bufvarname + 2, varname);
11407 set_var(bufvarname, varp, TRUE);
11408 vim_free(bufvarname);
11409 curbuf = save_curbuf;
11410 }
11411 }
11412 }
11413}
11414
11415 static void
11416f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11417{
11418 dict_T *d;
11419 dictitem_T *di;
11420 char_u *csearch;
11421
11422 if (argvars[0].v_type != VAR_DICT)
11423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011424 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011425 return;
11426 }
11427
11428 if ((d = argvars[0].vval.v_dict) != NULL)
11429 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011430 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011431 if (csearch != NULL)
11432 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 if (enc_utf8)
11434 {
11435 int pcc[MAX_MCO];
11436 int c = utfc_ptr2char(csearch, pcc);
11437
11438 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11439 }
11440 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011441 set_last_csearch(PTR2CHAR(csearch),
11442 csearch, MB_PTR2LEN(csearch));
11443 }
11444
11445 di = dict_find(d, (char_u *)"forward", -1);
11446 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011447 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 ? FORWARD : BACKWARD);
11449
11450 di = dict_find(d, (char_u *)"until", -1);
11451 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011452 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011453 }
11454}
11455
11456/*
11457 * "setcmdpos()" function
11458 */
11459 static void
11460f_setcmdpos(typval_T *argvars, typval_T *rettv)
11461{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011462 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463
11464 if (pos >= 0)
11465 rettv->vval.v_number = set_cmdline_pos(pos);
11466}
11467
11468/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011469 * "setenv()" function
11470 */
11471 static void
11472f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11473{
11474 char_u namebuf[NUMBUFLEN];
11475 char_u valbuf[NUMBUFLEN];
11476 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11477
11478 if (argvars[1].v_type == VAR_SPECIAL
11479 && argvars[1].vval.v_number == VVAL_NULL)
11480 vim_unsetenv(name);
11481 else
11482 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11483}
11484
11485/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011486 * "setfperm({fname}, {mode})" function
11487 */
11488 static void
11489f_setfperm(typval_T *argvars, typval_T *rettv)
11490{
11491 char_u *fname;
11492 char_u modebuf[NUMBUFLEN];
11493 char_u *mode_str;
11494 int i;
11495 int mask;
11496 int mode = 0;
11497
11498 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011499 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 if (fname == NULL)
11501 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011502 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503 if (mode_str == NULL)
11504 return;
11505 if (STRLEN(mode_str) != 9)
11506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011507 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508 return;
11509 }
11510
11511 mask = 1;
11512 for (i = 8; i >= 0; --i)
11513 {
11514 if (mode_str[i] != '-')
11515 mode |= mask;
11516 mask = mask << 1;
11517 }
11518 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11519}
11520
11521/*
11522 * "setline()" function
11523 */
11524 static void
11525f_setline(typval_T *argvars, typval_T *rettv)
11526{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011527 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528
Bram Moolenaarca851592018-06-06 21:04:07 +020011529 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011530}
11531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532/*
11533 * Used by "setqflist()" and "setloclist()" functions
11534 */
11535 static void
11536set_qf_ll_list(
11537 win_T *wp UNUSED,
11538 typval_T *list_arg UNUSED,
11539 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011540 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 typval_T *rettv)
11542{
11543#ifdef FEAT_QUICKFIX
11544 static char *e_invact = N_("E927: Invalid action: '%s'");
11545 char_u *act;
11546 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011547 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548#endif
11549
11550 rettv->vval.v_number = -1;
11551
11552#ifdef FEAT_QUICKFIX
11553 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011554 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011555 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011556 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557 else
11558 {
11559 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011560 dict_T *d = NULL;
11561 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011562
11563 if (action_arg->v_type == VAR_STRING)
11564 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011565 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011566 if (act == NULL)
11567 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011568 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11569 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 action = *act;
11571 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011572 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 }
11574 else if (action_arg->v_type == VAR_UNKNOWN)
11575 action = ' ';
11576 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011577 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578
Bram Moolenaard823fa92016-08-12 16:29:27 +020011579 if (action_arg->v_type != VAR_UNKNOWN
11580 && what_arg->v_type != VAR_UNKNOWN)
11581 {
11582 if (what_arg->v_type == VAR_DICT)
11583 d = what_arg->vval.v_dict;
11584 else
11585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011586 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011587 valid_dict = FALSE;
11588 }
11589 }
11590
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011591 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011592 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011593 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11594 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011595 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011596 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011597 }
11598#endif
11599}
11600
11601/*
11602 * "setloclist()" function
11603 */
11604 static void
11605f_setloclist(typval_T *argvars, typval_T *rettv)
11606{
11607 win_T *win;
11608
11609 rettv->vval.v_number = -1;
11610
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011611 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011612 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011613 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011614}
11615
11616/*
11617 * "setmatches()" function
11618 */
11619 static void
11620f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11621{
11622#ifdef FEAT_SEARCH_EXTRA
11623 list_T *l;
11624 listitem_T *li;
11625 dict_T *d;
11626 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011627 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011628
11629 rettv->vval.v_number = -1;
11630 if (argvars[0].v_type != VAR_LIST)
11631 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011632 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011633 return;
11634 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011635 if (win == NULL)
11636 return;
11637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011638 if ((l = argvars[0].vval.v_list) != NULL)
11639 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 /* To some extent make sure that we are dealing with a list from
11641 * "getmatches()". */
11642 li = l->lv_first;
11643 while (li != NULL)
11644 {
11645 if (li->li_tv.v_type != VAR_DICT
11646 || (d = li->li_tv.vval.v_dict) == NULL)
11647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011648 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 return;
11650 }
11651 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11652 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11653 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11654 && dict_find(d, (char_u *)"priority", -1) != NULL
11655 && dict_find(d, (char_u *)"id", -1) != NULL))
11656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011657 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658 return;
11659 }
11660 li = li->li_next;
11661 }
11662
Bram Moolenaaraff74912019-03-30 18:11:49 +010011663 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011664 li = l->lv_first;
11665 while (li != NULL)
11666 {
11667 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011668 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011669 dictitem_T *di;
11670 char_u *group;
11671 int priority;
11672 int id;
11673 char_u *conceal;
11674
11675 d = li->li_tv.vval.v_dict;
11676 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11677 {
11678 if (s == NULL)
11679 {
11680 s = list_alloc();
11681 if (s == NULL)
11682 return;
11683 }
11684
11685 /* match from matchaddpos() */
11686 for (i = 1; i < 9; i++)
11687 {
11688 sprintf((char *)buf, (char *)"pos%d", i);
11689 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11690 {
11691 if (di->di_tv.v_type != VAR_LIST)
11692 return;
11693
11694 list_append_tv(s, &di->di_tv);
11695 s->lv_refcount++;
11696 }
11697 else
11698 break;
11699 }
11700 }
11701
Bram Moolenaar8f667172018-12-14 15:38:31 +010011702 group = dict_get_string(d, (char_u *)"group", TRUE);
11703 priority = (int)dict_get_number(d, (char_u *)"priority");
11704 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011705 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011706 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011707 : NULL;
11708 if (i == 0)
11709 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011710 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011711 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011712 priority, id, NULL, conceal);
11713 }
11714 else
11715 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011716 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 list_unref(s);
11718 s = NULL;
11719 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011720 vim_free(group);
11721 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011722
11723 li = li->li_next;
11724 }
11725 rettv->vval.v_number = 0;
11726 }
11727#endif
11728}
11729
11730/*
11731 * "setpos()" function
11732 */
11733 static void
11734f_setpos(typval_T *argvars, typval_T *rettv)
11735{
11736 pos_T pos;
11737 int fnum;
11738 char_u *name;
11739 colnr_T curswant = -1;
11740
11741 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011742 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011743 if (name != NULL)
11744 {
11745 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11746 {
11747 if (--pos.col < 0)
11748 pos.col = 0;
11749 if (name[0] == '.' && name[1] == NUL)
11750 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011751 /* set cursor; "fnum" is ignored */
11752 curwin->w_cursor = pos;
11753 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011755 curwin->w_curswant = curswant - 1;
11756 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011757 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011758 check_cursor();
11759 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011760 }
11761 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11762 {
11763 /* set mark */
11764 if (setmark_pos(name[1], &pos, fnum) == OK)
11765 rettv->vval.v_number = 0;
11766 }
11767 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011768 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011769 }
11770 }
11771}
11772
11773/*
11774 * "setqflist()" function
11775 */
11776 static void
11777f_setqflist(typval_T *argvars, typval_T *rettv)
11778{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011779 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780}
11781
11782/*
11783 * "setreg()" function
11784 */
11785 static void
11786f_setreg(typval_T *argvars, typval_T *rettv)
11787{
11788 int regname;
11789 char_u *strregname;
11790 char_u *stropt;
11791 char_u *strval;
11792 int append;
11793 char_u yank_type;
11794 long block_len;
11795
11796 block_len = -1;
11797 yank_type = MAUTO;
11798 append = FALSE;
11799
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011800 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011801 rettv->vval.v_number = 1; /* FAIL is default */
11802
11803 if (strregname == NULL)
11804 return; /* type error; errmsg already given */
11805 regname = *strregname;
11806 if (regname == 0 || regname == '@')
11807 regname = '"';
11808
11809 if (argvars[2].v_type != VAR_UNKNOWN)
11810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011811 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011812 if (stropt == NULL)
11813 return; /* type error */
11814 for (; *stropt != NUL; ++stropt)
11815 switch (*stropt)
11816 {
11817 case 'a': case 'A': /* append */
11818 append = TRUE;
11819 break;
11820 case 'v': case 'c': /* character-wise selection */
11821 yank_type = MCHAR;
11822 break;
11823 case 'V': case 'l': /* line-wise selection */
11824 yank_type = MLINE;
11825 break;
11826 case 'b': case Ctrl_V: /* block-wise selection */
11827 yank_type = MBLOCK;
11828 if (VIM_ISDIGIT(stropt[1]))
11829 {
11830 ++stropt;
11831 block_len = getdigits(&stropt) - 1;
11832 --stropt;
11833 }
11834 break;
11835 }
11836 }
11837
11838 if (argvars[1].v_type == VAR_LIST)
11839 {
11840 char_u **lstval;
11841 char_u **allocval;
11842 char_u buf[NUMBUFLEN];
11843 char_u **curval;
11844 char_u **curallocval;
11845 list_T *ll = argvars[1].vval.v_list;
11846 listitem_T *li;
11847 int len;
11848
11849 /* If the list is NULL handle like an empty list. */
11850 len = ll == NULL ? 0 : ll->lv_len;
11851
11852 /* First half: use for pointers to result lines; second half: use for
11853 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011854 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011855 if (lstval == NULL)
11856 return;
11857 curval = lstval;
11858 allocval = lstval + len + 2;
11859 curallocval = allocval;
11860
11861 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11862 li = li->li_next)
11863 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011864 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 if (strval == NULL)
11866 goto free_lstval;
11867 if (strval == buf)
11868 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011869 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870 * overwrite the string. */
11871 strval = vim_strsave(buf);
11872 if (strval == NULL)
11873 goto free_lstval;
11874 *curallocval++ = strval;
11875 }
11876 *curval++ = strval;
11877 }
11878 *curval++ = NULL;
11879
11880 write_reg_contents_lst(regname, lstval, -1,
11881 append, yank_type, block_len);
11882free_lstval:
11883 while (curallocval > allocval)
11884 vim_free(*--curallocval);
11885 vim_free(lstval);
11886 }
11887 else
11888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011889 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011890 if (strval == NULL)
11891 return;
11892 write_reg_contents_ex(regname, strval, -1,
11893 append, yank_type, block_len);
11894 }
11895 rettv->vval.v_number = 0;
11896}
11897
11898/*
11899 * "settabvar()" function
11900 */
11901 static void
11902f_settabvar(typval_T *argvars, typval_T *rettv)
11903{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011904 tabpage_T *save_curtab;
11905 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011906 char_u *varname, *tabvarname;
11907 typval_T *varp;
11908
11909 rettv->vval.v_number = 0;
11910
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011911 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 return;
11913
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011914 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11915 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011916 varp = &argvars[2];
11917
Bram Moolenaar4033c552017-09-16 20:54:51 +020011918 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011919 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 save_curtab = curtab;
11921 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011922
Bram Moolenaar964b3742019-05-24 18:54:09 +020011923 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011924 if (tabvarname != NULL)
11925 {
11926 STRCPY(tabvarname, "t:");
11927 STRCPY(tabvarname + 2, varname);
11928 set_var(tabvarname, varp, TRUE);
11929 vim_free(tabvarname);
11930 }
11931
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011932 /* Restore current tabpage */
11933 if (valid_tabpage(save_curtab))
11934 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935 }
11936}
11937
11938/*
11939 * "settabwinvar()" function
11940 */
11941 static void
11942f_settabwinvar(typval_T *argvars, typval_T *rettv)
11943{
11944 setwinvar(argvars, rettv, 1);
11945}
11946
11947/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011948 * "settagstack()" function
11949 */
11950 static void
11951f_settagstack(typval_T *argvars, typval_T *rettv)
11952{
11953 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11954 win_T *wp;
11955 dict_T *d;
11956 int action = 'r';
11957
11958 rettv->vval.v_number = -1;
11959
11960 // first argument: window number or id
11961 wp = find_win_by_nr_or_id(&argvars[0]);
11962 if (wp == NULL)
11963 return;
11964
11965 // second argument: dict with items to set in the tag stack
11966 if (argvars[1].v_type != VAR_DICT)
11967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011968 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011969 return;
11970 }
11971 d = argvars[1].vval.v_dict;
11972 if (d == NULL)
11973 return;
11974
11975 // third argument: action - 'a' for append and 'r' for replace.
11976 // default is to replace the stack.
11977 if (argvars[2].v_type == VAR_UNKNOWN)
11978 action = 'r';
11979 else if (argvars[2].v_type == VAR_STRING)
11980 {
11981 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011982 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011983 if (actstr == NULL)
11984 return;
11985 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11986 action = *actstr;
11987 else
11988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011989 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011990 return;
11991 }
11992 }
11993 else
11994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011995 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011996 return;
11997 }
11998
11999 if (set_tagstack(wp, d, action) == OK)
12000 rettv->vval.v_number = 0;
12001}
12002
12003/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012004 * "setwinvar()" function
12005 */
12006 static void
12007f_setwinvar(typval_T *argvars, typval_T *rettv)
12008{
12009 setwinvar(argvars, rettv, 0);
12010}
12011
12012#ifdef FEAT_CRYPT
12013/*
12014 * "sha256({string})" function
12015 */
12016 static void
12017f_sha256(typval_T *argvars, typval_T *rettv)
12018{
12019 char_u *p;
12020
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012021 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012022 rettv->vval.v_string = vim_strsave(
12023 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12024 rettv->v_type = VAR_STRING;
12025}
12026#endif /* FEAT_CRYPT */
12027
12028/*
12029 * "shellescape({string})" function
12030 */
12031 static void
12032f_shellescape(typval_T *argvars, typval_T *rettv)
12033{
Bram Moolenaar20615522017-06-05 18:46:26 +020012034 int do_special = non_zero_arg(&argvars[1]);
12035
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012036 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012037 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012038 rettv->v_type = VAR_STRING;
12039}
12040
12041/*
12042 * shiftwidth() function
12043 */
12044 static void
12045f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12046{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012047 rettv->vval.v_number = 0;
12048
12049 if (argvars[0].v_type != VAR_UNKNOWN)
12050 {
12051 long col;
12052
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012053 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012054 if (col < 0)
12055 return; // type error; errmsg already given
12056#ifdef FEAT_VARTABS
12057 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12058 return;
12059#endif
12060 }
12061
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012062 rettv->vval.v_number = get_sw_value(curbuf);
12063}
12064
Bram Moolenaar162b7142018-12-21 15:17:36 +010012065#ifdef FEAT_SIGNS
12066/*
12067 * "sign_define()" function
12068 */
12069 static void
12070f_sign_define(typval_T *argvars, typval_T *rettv)
12071{
12072 char_u *name;
12073 dict_T *dict;
12074 char_u *icon = NULL;
12075 char_u *linehl = NULL;
12076 char_u *text = NULL;
12077 char_u *texthl = NULL;
12078
12079 rettv->vval.v_number = -1;
12080
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012081 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012082 if (name == NULL)
12083 return;
12084
12085 if (argvars[1].v_type != VAR_UNKNOWN)
12086 {
12087 if (argvars[1].v_type != VAR_DICT)
12088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012089 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090 return;
12091 }
12092
12093 // sign attributes
12094 dict = argvars[1].vval.v_dict;
12095 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12096 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12097 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12098 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12099 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12100 text = dict_get_string(dict, (char_u *)"text", TRUE);
12101 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12102 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12103 }
12104
12105 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12106 rettv->vval.v_number = 0;
12107
12108 vim_free(icon);
12109 vim_free(linehl);
12110 vim_free(text);
12111 vim_free(texthl);
12112}
12113
12114/*
12115 * "sign_getdefined()" function
12116 */
12117 static void
12118f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12119{
12120 char_u *name = NULL;
12121
12122 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12123 return;
12124
12125 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012126 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012127
12128 sign_getlist(name, rettv->vval.v_list);
12129}
12130
12131/*
12132 * "sign_getplaced()" function
12133 */
12134 static void
12135f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12136{
12137 buf_T *buf = NULL;
12138 dict_T *dict;
12139 dictitem_T *di;
12140 linenr_T lnum = 0;
12141 int sign_id = 0;
12142 char_u *group = NULL;
12143 int notanum = FALSE;
12144
12145 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12146 return;
12147
12148 if (argvars[0].v_type != VAR_UNKNOWN)
12149 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012150 // get signs placed in the specified buffer
12151 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012152 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012153 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012154
12155 if (argvars[1].v_type != VAR_UNKNOWN)
12156 {
12157 if (argvars[1].v_type != VAR_DICT ||
12158 ((dict = argvars[1].vval.v_dict) == NULL))
12159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012160 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012161 return;
12162 }
12163 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12164 {
12165 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012166 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012167 if (notanum)
12168 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012169 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012170 }
12171 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12172 {
12173 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012174 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012175 if (notanum)
12176 return;
12177 }
12178 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012180 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012181 if (group == NULL)
12182 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012183 if (*group == '\0') // empty string means global group
12184 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012185 }
12186 }
12187 }
12188
12189 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12190}
12191
12192/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012193 * "sign_jump()" function
12194 */
12195 static void
12196f_sign_jump(typval_T *argvars, typval_T *rettv)
12197{
12198 int sign_id;
12199 char_u *sign_group = NULL;
12200 buf_T *buf;
12201 int notanum = FALSE;
12202
12203 rettv->vval.v_number = -1;
12204
Bram Moolenaarbdace832019-03-02 10:13:42 +010012205 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012206 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12207 if (notanum)
12208 return;
12209 if (sign_id <= 0)
12210 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012211 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012212 return;
12213 }
12214
12215 // Sign group
12216 sign_group = tv_get_string_chk(&argvars[1]);
12217 if (sign_group == NULL)
12218 return;
12219 if (sign_group[0] == '\0')
12220 sign_group = NULL; // global sign group
12221 else
12222 {
12223 sign_group = vim_strsave(sign_group);
12224 if (sign_group == NULL)
12225 return;
12226 }
12227
12228 // Buffer to place the sign
12229 buf = get_buf_arg(&argvars[2]);
12230 if (buf == NULL)
12231 goto cleanup;
12232
12233 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12234
12235cleanup:
12236 vim_free(sign_group);
12237}
12238
12239/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012240 * "sign_place()" function
12241 */
12242 static void
12243f_sign_place(typval_T *argvars, typval_T *rettv)
12244{
12245 int sign_id;
12246 char_u *group = NULL;
12247 char_u *sign_name;
12248 buf_T *buf;
12249 dict_T *dict;
12250 dictitem_T *di;
12251 linenr_T lnum = 0;
12252 int prio = SIGN_DEF_PRIO;
12253 int notanum = FALSE;
12254
12255 rettv->vval.v_number = -1;
12256
Bram Moolenaarbdace832019-03-02 10:13:42 +010012257 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012258 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012259 if (notanum)
12260 return;
12261 if (sign_id < 0)
12262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012263 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012264 return;
12265 }
12266
12267 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012268 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012269 if (group == NULL)
12270 return;
12271 if (group[0] == '\0')
12272 group = NULL; // global sign group
12273 else
12274 {
12275 group = vim_strsave(group);
12276 if (group == NULL)
12277 return;
12278 }
12279
12280 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012281 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012282 if (sign_name == NULL)
12283 goto cleanup;
12284
12285 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012286 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012287 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012288 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012289
12290 if (argvars[4].v_type != VAR_UNKNOWN)
12291 {
12292 if (argvars[4].v_type != VAR_DICT ||
12293 ((dict = argvars[4].vval.v_dict) == NULL))
12294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012295 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012296 goto cleanup;
12297 }
12298
12299 // Line number where the sign is to be placed
12300 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012302 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012303 if (notanum)
12304 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012305 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012306 }
12307 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12308 {
12309 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012310 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012311 if (notanum)
12312 goto cleanup;
12313 }
12314 }
12315
12316 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12317 rettv->vval.v_number = sign_id;
12318
12319cleanup:
12320 vim_free(group);
12321}
12322
12323/*
12324 * "sign_undefine()" function
12325 */
12326 static void
12327f_sign_undefine(typval_T *argvars, typval_T *rettv)
12328{
12329 char_u *name;
12330
12331 rettv->vval.v_number = -1;
12332
12333 if (argvars[0].v_type == VAR_UNKNOWN)
12334 {
12335 // Free all the signs
12336 free_signs();
12337 rettv->vval.v_number = 0;
12338 }
12339 else
12340 {
12341 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012342 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012343 if (name == NULL)
12344 return;
12345
12346 if (sign_undefine_by_name(name) == OK)
12347 rettv->vval.v_number = 0;
12348 }
12349}
12350
12351/*
12352 * "sign_unplace()" function
12353 */
12354 static void
12355f_sign_unplace(typval_T *argvars, typval_T *rettv)
12356{
12357 dict_T *dict;
12358 dictitem_T *di;
12359 int sign_id = 0;
12360 buf_T *buf = NULL;
12361 char_u *group = NULL;
12362
12363 rettv->vval.v_number = -1;
12364
12365 if (argvars[0].v_type != VAR_STRING)
12366 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012367 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012368 return;
12369 }
12370
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012371 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012372 if (group[0] == '\0')
12373 group = NULL; // global sign group
12374 else
12375 {
12376 group = vim_strsave(group);
12377 if (group == NULL)
12378 return;
12379 }
12380
12381 if (argvars[1].v_type != VAR_UNKNOWN)
12382 {
12383 if (argvars[1].v_type != VAR_DICT)
12384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012385 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012386 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012387 }
12388 dict = argvars[1].vval.v_dict;
12389
12390 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12391 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012392 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012393 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012394 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012395 }
12396 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12397 sign_id = dict_get_number(dict, (char_u *)"id");
12398 }
12399
12400 if (buf == NULL)
12401 {
12402 // Delete the sign in all the buffers
12403 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012404 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012405 rettv->vval.v_number = 0;
12406 }
12407 else
12408 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012409 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012410 rettv->vval.v_number = 0;
12411 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012412
12413cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012414 vim_free(group);
12415}
12416#endif
12417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012418/*
12419 * "simplify()" function
12420 */
12421 static void
12422f_simplify(typval_T *argvars, typval_T *rettv)
12423{
12424 char_u *p;
12425
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012426 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012427 rettv->vval.v_string = vim_strsave(p);
12428 simplify_filename(rettv->vval.v_string); /* simplify in place */
12429 rettv->v_type = VAR_STRING;
12430}
12431
12432#ifdef FEAT_FLOAT
12433/*
12434 * "sin()" function
12435 */
12436 static void
12437f_sin(typval_T *argvars, typval_T *rettv)
12438{
12439 float_T f = 0.0;
12440
12441 rettv->v_type = VAR_FLOAT;
12442 if (get_float_arg(argvars, &f) == OK)
12443 rettv->vval.v_float = sin(f);
12444 else
12445 rettv->vval.v_float = 0.0;
12446}
12447
12448/*
12449 * "sinh()" function
12450 */
12451 static void
12452f_sinh(typval_T *argvars, typval_T *rettv)
12453{
12454 float_T f = 0.0;
12455
12456 rettv->v_type = VAR_FLOAT;
12457 if (get_float_arg(argvars, &f) == OK)
12458 rettv->vval.v_float = sinh(f);
12459 else
12460 rettv->vval.v_float = 0.0;
12461}
12462#endif
12463
Bram Moolenaareae1b912019-05-09 15:12:55 +020012464static int item_compare(const void *s1, const void *s2);
12465static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012466
12467/* struct used in the array that's given to qsort() */
12468typedef struct
12469{
12470 listitem_T *item;
12471 int idx;
12472} sortItem_T;
12473
12474/* struct storing information about current sort */
12475typedef struct
12476{
12477 int item_compare_ic;
12478 int item_compare_numeric;
12479 int item_compare_numbers;
12480#ifdef FEAT_FLOAT
12481 int item_compare_float;
12482#endif
12483 char_u *item_compare_func;
12484 partial_T *item_compare_partial;
12485 dict_T *item_compare_selfdict;
12486 int item_compare_func_err;
12487 int item_compare_keep_zero;
12488} sortinfo_T;
12489static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012490#define ITEM_COMPARE_FAIL 999
12491
12492/*
12493 * Compare functions for f_sort() and f_uniq() below.
12494 */
12495 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012496item_compare(const void *s1, const void *s2)
12497{
12498 sortItem_T *si1, *si2;
12499 typval_T *tv1, *tv2;
12500 char_u *p1, *p2;
12501 char_u *tofree1 = NULL, *tofree2 = NULL;
12502 int res;
12503 char_u numbuf1[NUMBUFLEN];
12504 char_u numbuf2[NUMBUFLEN];
12505
12506 si1 = (sortItem_T *)s1;
12507 si2 = (sortItem_T *)s2;
12508 tv1 = &si1->item->li_tv;
12509 tv2 = &si2->item->li_tv;
12510
12511 if (sortinfo->item_compare_numbers)
12512 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012513 varnumber_T v1 = tv_get_number(tv1);
12514 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515
12516 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12517 }
12518
12519#ifdef FEAT_FLOAT
12520 if (sortinfo->item_compare_float)
12521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012522 float_T v1 = tv_get_float(tv1);
12523 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012524
12525 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12526 }
12527#endif
12528
12529 /* tv2string() puts quotes around a string and allocates memory. Don't do
12530 * that for string variables. Use a single quote when comparing with a
12531 * non-string to do what the docs promise. */
12532 if (tv1->v_type == VAR_STRING)
12533 {
12534 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12535 p1 = (char_u *)"'";
12536 else
12537 p1 = tv1->vval.v_string;
12538 }
12539 else
12540 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12541 if (tv2->v_type == VAR_STRING)
12542 {
12543 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12544 p2 = (char_u *)"'";
12545 else
12546 p2 = tv2->vval.v_string;
12547 }
12548 else
12549 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12550 if (p1 == NULL)
12551 p1 = (char_u *)"";
12552 if (p2 == NULL)
12553 p2 = (char_u *)"";
12554 if (!sortinfo->item_compare_numeric)
12555 {
12556 if (sortinfo->item_compare_ic)
12557 res = STRICMP(p1, p2);
12558 else
12559 res = STRCMP(p1, p2);
12560 }
12561 else
12562 {
12563 double n1, n2;
12564 n1 = strtod((char *)p1, (char **)&p1);
12565 n2 = strtod((char *)p2, (char **)&p2);
12566 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12567 }
12568
12569 /* When the result would be zero, compare the item indexes. Makes the
12570 * sort stable. */
12571 if (res == 0 && !sortinfo->item_compare_keep_zero)
12572 res = si1->idx > si2->idx ? 1 : -1;
12573
12574 vim_free(tofree1);
12575 vim_free(tofree2);
12576 return res;
12577}
12578
12579 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580item_compare2(const void *s1, const void *s2)
12581{
12582 sortItem_T *si1, *si2;
12583 int res;
12584 typval_T rettv;
12585 typval_T argv[3];
12586 int dummy;
12587 char_u *func_name;
12588 partial_T *partial = sortinfo->item_compare_partial;
12589
12590 /* shortcut after failure in previous call; compare all items equal */
12591 if (sortinfo->item_compare_func_err)
12592 return 0;
12593
12594 si1 = (sortItem_T *)s1;
12595 si2 = (sortItem_T *)s2;
12596
12597 if (partial == NULL)
12598 func_name = sortinfo->item_compare_func;
12599 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012600 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012601
12602 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12603 * in the copy without changing the original list items. */
12604 copy_tv(&si1->item->li_tv, &argv[0]);
12605 copy_tv(&si2->item->li_tv, &argv[1]);
12606
12607 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012608 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609 partial, sortinfo->item_compare_selfdict);
12610 clear_tv(&argv[0]);
12611 clear_tv(&argv[1]);
12612
12613 if (res == FAIL)
12614 res = ITEM_COMPARE_FAIL;
12615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012616 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617 if (sortinfo->item_compare_func_err)
12618 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12619 clear_tv(&rettv);
12620
12621 /* When the result would be zero, compare the pointers themselves. Makes
12622 * the sort stable. */
12623 if (res == 0 && !sortinfo->item_compare_keep_zero)
12624 res = si1->idx > si2->idx ? 1 : -1;
12625
12626 return res;
12627}
12628
12629/*
12630 * "sort({list})" function
12631 */
12632 static void
12633do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12634{
12635 list_T *l;
12636 listitem_T *li;
12637 sortItem_T *ptrs;
12638 sortinfo_T *old_sortinfo;
12639 sortinfo_T info;
12640 long len;
12641 long i;
12642
12643 /* Pointer to current info struct used in compare function. Save and
12644 * restore the current one for nested calls. */
12645 old_sortinfo = sortinfo;
12646 sortinfo = &info;
12647
12648 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012649 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012650 else
12651 {
12652 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012653 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012654 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12655 TRUE))
12656 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012657 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658
12659 len = list_len(l);
12660 if (len <= 1)
12661 goto theend; /* short list sorts pretty quickly */
12662
12663 info.item_compare_ic = FALSE;
12664 info.item_compare_numeric = FALSE;
12665 info.item_compare_numbers = FALSE;
12666#ifdef FEAT_FLOAT
12667 info.item_compare_float = FALSE;
12668#endif
12669 info.item_compare_func = NULL;
12670 info.item_compare_partial = NULL;
12671 info.item_compare_selfdict = NULL;
12672 if (argvars[1].v_type != VAR_UNKNOWN)
12673 {
12674 /* optional second argument: {func} */
12675 if (argvars[1].v_type == VAR_FUNC)
12676 info.item_compare_func = argvars[1].vval.v_string;
12677 else if (argvars[1].v_type == VAR_PARTIAL)
12678 info.item_compare_partial = argvars[1].vval.v_partial;
12679 else
12680 {
12681 int error = FALSE;
12682
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012683 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012684 if (error)
12685 goto theend; /* type error; errmsg already given */
12686 if (i == 1)
12687 info.item_compare_ic = TRUE;
12688 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012689 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012690 else if (i != 0)
12691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012692 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012693 goto theend;
12694 }
12695 if (info.item_compare_func != NULL)
12696 {
12697 if (*info.item_compare_func == NUL)
12698 {
12699 /* empty string means default sort */
12700 info.item_compare_func = NULL;
12701 }
12702 else if (STRCMP(info.item_compare_func, "n") == 0)
12703 {
12704 info.item_compare_func = NULL;
12705 info.item_compare_numeric = TRUE;
12706 }
12707 else if (STRCMP(info.item_compare_func, "N") == 0)
12708 {
12709 info.item_compare_func = NULL;
12710 info.item_compare_numbers = TRUE;
12711 }
12712#ifdef FEAT_FLOAT
12713 else if (STRCMP(info.item_compare_func, "f") == 0)
12714 {
12715 info.item_compare_func = NULL;
12716 info.item_compare_float = TRUE;
12717 }
12718#endif
12719 else if (STRCMP(info.item_compare_func, "i") == 0)
12720 {
12721 info.item_compare_func = NULL;
12722 info.item_compare_ic = TRUE;
12723 }
12724 }
12725 }
12726
12727 if (argvars[2].v_type != VAR_UNKNOWN)
12728 {
12729 /* optional third argument: {dict} */
12730 if (argvars[2].v_type != VAR_DICT)
12731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012732 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012733 goto theend;
12734 }
12735 info.item_compare_selfdict = argvars[2].vval.v_dict;
12736 }
12737 }
12738
12739 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012740 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 if (ptrs == NULL)
12742 goto theend;
12743
12744 i = 0;
12745 if (sort)
12746 {
12747 /* sort(): ptrs will be the list to sort */
12748 for (li = l->lv_first; li != NULL; li = li->li_next)
12749 {
12750 ptrs[i].item = li;
12751 ptrs[i].idx = i;
12752 ++i;
12753 }
12754
12755 info.item_compare_func_err = FALSE;
12756 info.item_compare_keep_zero = FALSE;
12757 /* test the compare function */
12758 if ((info.item_compare_func != NULL
12759 || info.item_compare_partial != NULL)
12760 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12761 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012762 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012763 else
12764 {
12765 /* Sort the array with item pointers. */
12766 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12767 info.item_compare_func == NULL
12768 && info.item_compare_partial == NULL
12769 ? item_compare : item_compare2);
12770
12771 if (!info.item_compare_func_err)
12772 {
12773 /* Clear the List and append the items in sorted order. */
12774 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12775 l->lv_len = 0;
12776 for (i = 0; i < len; ++i)
12777 list_append(l, ptrs[i].item);
12778 }
12779 }
12780 }
12781 else
12782 {
12783 int (*item_compare_func_ptr)(const void *, const void *);
12784
12785 /* f_uniq(): ptrs will be a stack of items to remove */
12786 info.item_compare_func_err = FALSE;
12787 info.item_compare_keep_zero = TRUE;
12788 item_compare_func_ptr = info.item_compare_func != NULL
12789 || info.item_compare_partial != NULL
12790 ? item_compare2 : item_compare;
12791
12792 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12793 li = li->li_next)
12794 {
12795 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12796 == 0)
12797 ptrs[i++].item = li;
12798 if (info.item_compare_func_err)
12799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012800 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012801 break;
12802 }
12803 }
12804
12805 if (!info.item_compare_func_err)
12806 {
12807 while (--i >= 0)
12808 {
12809 li = ptrs[i].item->li_next;
12810 ptrs[i].item->li_next = li->li_next;
12811 if (li->li_next != NULL)
12812 li->li_next->li_prev = ptrs[i].item;
12813 else
12814 l->lv_last = ptrs[i].item;
12815 list_fix_watch(l, li);
12816 listitem_free(li);
12817 l->lv_len--;
12818 }
12819 }
12820 }
12821
12822 vim_free(ptrs);
12823 }
12824theend:
12825 sortinfo = old_sortinfo;
12826}
12827
12828/*
12829 * "sort({list})" function
12830 */
12831 static void
12832f_sort(typval_T *argvars, typval_T *rettv)
12833{
12834 do_sort_uniq(argvars, rettv, TRUE);
12835}
12836
12837/*
12838 * "uniq({list})" function
12839 */
12840 static void
12841f_uniq(typval_T *argvars, typval_T *rettv)
12842{
12843 do_sort_uniq(argvars, rettv, FALSE);
12844}
12845
12846/*
12847 * "soundfold({word})" function
12848 */
12849 static void
12850f_soundfold(typval_T *argvars, typval_T *rettv)
12851{
12852 char_u *s;
12853
12854 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012855 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012856#ifdef FEAT_SPELL
12857 rettv->vval.v_string = eval_soundfold(s);
12858#else
12859 rettv->vval.v_string = vim_strsave(s);
12860#endif
12861}
12862
12863/*
12864 * "spellbadword()" function
12865 */
12866 static void
12867f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12868{
12869 char_u *word = (char_u *)"";
12870 hlf_T attr = HLF_COUNT;
12871 int len = 0;
12872
12873 if (rettv_list_alloc(rettv) == FAIL)
12874 return;
12875
12876#ifdef FEAT_SPELL
12877 if (argvars[0].v_type == VAR_UNKNOWN)
12878 {
12879 /* Find the start and length of the badly spelled word. */
12880 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12881 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012882 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012883 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012884 curwin->w_set_curswant = TRUE;
12885 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886 }
12887 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012889 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012890 int capcol = -1;
12891
12892 if (str != NULL)
12893 {
12894 /* Check the argument for spelling. */
12895 while (*str != NUL)
12896 {
12897 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12898 if (attr != HLF_COUNT)
12899 {
12900 word = str;
12901 break;
12902 }
12903 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012904 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 }
12906 }
12907 }
12908#endif
12909
12910 list_append_string(rettv->vval.v_list, word, len);
12911 list_append_string(rettv->vval.v_list, (char_u *)(
12912 attr == HLF_SPB ? "bad" :
12913 attr == HLF_SPR ? "rare" :
12914 attr == HLF_SPL ? "local" :
12915 attr == HLF_SPC ? "caps" :
12916 ""), -1);
12917}
12918
12919/*
12920 * "spellsuggest()" function
12921 */
12922 static void
12923f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12924{
12925#ifdef FEAT_SPELL
12926 char_u *str;
12927 int typeerr = FALSE;
12928 int maxcount;
12929 garray_T ga;
12930 int i;
12931 listitem_T *li;
12932 int need_capital = FALSE;
12933#endif
12934
12935 if (rettv_list_alloc(rettv) == FAIL)
12936 return;
12937
12938#ifdef FEAT_SPELL
12939 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012941 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012942 if (argvars[1].v_type != VAR_UNKNOWN)
12943 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012944 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012945 if (maxcount <= 0)
12946 return;
12947 if (argvars[2].v_type != VAR_UNKNOWN)
12948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012949 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012950 if (typeerr)
12951 return;
12952 }
12953 }
12954 else
12955 maxcount = 25;
12956
12957 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12958
12959 for (i = 0; i < ga.ga_len; ++i)
12960 {
12961 str = ((char_u **)ga.ga_data)[i];
12962
12963 li = listitem_alloc();
12964 if (li == NULL)
12965 vim_free(str);
12966 else
12967 {
12968 li->li_tv.v_type = VAR_STRING;
12969 li->li_tv.v_lock = 0;
12970 li->li_tv.vval.v_string = str;
12971 list_append(rettv->vval.v_list, li);
12972 }
12973 }
12974 ga_clear(&ga);
12975 }
12976#endif
12977}
12978
12979 static void
12980f_split(typval_T *argvars, typval_T *rettv)
12981{
12982 char_u *str;
12983 char_u *end;
12984 char_u *pat = NULL;
12985 regmatch_T regmatch;
12986 char_u patbuf[NUMBUFLEN];
12987 char_u *save_cpo;
12988 int match;
12989 colnr_T col = 0;
12990 int keepempty = FALSE;
12991 int typeerr = FALSE;
12992
12993 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12994 save_cpo = p_cpo;
12995 p_cpo = (char_u *)"";
12996
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012997 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012998 if (argvars[1].v_type != VAR_UNKNOWN)
12999 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013000 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013001 if (pat == NULL)
13002 typeerr = TRUE;
13003 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013004 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005 }
13006 if (pat == NULL || *pat == NUL)
13007 pat = (char_u *)"[\\x01- ]\\+";
13008
13009 if (rettv_list_alloc(rettv) == FAIL)
13010 return;
13011 if (typeerr)
13012 return;
13013
13014 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13015 if (regmatch.regprog != NULL)
13016 {
13017 regmatch.rm_ic = FALSE;
13018 while (*str != NUL || keepempty)
13019 {
13020 if (*str == NUL)
13021 match = FALSE; /* empty item at the end */
13022 else
13023 match = vim_regexec_nl(&regmatch, str, col);
13024 if (match)
13025 end = regmatch.startp[0];
13026 else
13027 end = str + STRLEN(str);
13028 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13029 && *str != NUL && match && end < regmatch.endp[0]))
13030 {
13031 if (list_append_string(rettv->vval.v_list, str,
13032 (int)(end - str)) == FAIL)
13033 break;
13034 }
13035 if (!match)
13036 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013037 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013038 if (regmatch.endp[0] > str)
13039 col = 0;
13040 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013041 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 str = regmatch.endp[0];
13044 }
13045
13046 vim_regfree(regmatch.regprog);
13047 }
13048
13049 p_cpo = save_cpo;
13050}
13051
13052#ifdef FEAT_FLOAT
13053/*
13054 * "sqrt()" function
13055 */
13056 static void
13057f_sqrt(typval_T *argvars, typval_T *rettv)
13058{
13059 float_T f = 0.0;
13060
13061 rettv->v_type = VAR_FLOAT;
13062 if (get_float_arg(argvars, &f) == OK)
13063 rettv->vval.v_float = sqrt(f);
13064 else
13065 rettv->vval.v_float = 0.0;
13066}
13067
13068/*
13069 * "str2float()" function
13070 */
13071 static void
13072f_str2float(typval_T *argvars, typval_T *rettv)
13073{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013074 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013075 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013076
Bram Moolenaar08243d22017-01-10 16:12:29 +010013077 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 p = skipwhite(p + 1);
13079 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013080 if (isneg)
13081 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 rettv->v_type = VAR_FLOAT;
13083}
13084#endif
13085
13086/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013087 * "str2list()" function
13088 */
13089 static void
13090f_str2list(typval_T *argvars, typval_T *rettv)
13091{
13092 char_u *p;
13093 int utf8 = FALSE;
13094
13095 if (rettv_list_alloc(rettv) == FAIL)
13096 return;
13097
13098 if (argvars[1].v_type != VAR_UNKNOWN)
13099 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13100
13101 p = tv_get_string(&argvars[0]);
13102
13103 if (has_mbyte || utf8)
13104 {
13105 int (*ptr2len)(char_u *);
13106 int (*ptr2char)(char_u *);
13107
13108 if (utf8 || enc_utf8)
13109 {
13110 ptr2len = utf_ptr2len;
13111 ptr2char = utf_ptr2char;
13112 }
13113 else
13114 {
13115 ptr2len = mb_ptr2len;
13116 ptr2char = mb_ptr2char;
13117 }
13118
13119 for ( ; *p != NUL; p += (*ptr2len)(p))
13120 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13121 }
13122 else
13123 for ( ; *p != NUL; ++p)
13124 list_append_number(rettv->vval.v_list, *p);
13125}
13126
13127/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013128 * "str2nr()" function
13129 */
13130 static void
13131f_str2nr(typval_T *argvars, typval_T *rettv)
13132{
13133 int base = 10;
13134 char_u *p;
13135 varnumber_T n;
13136 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013137 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138
13139 if (argvars[1].v_type != VAR_UNKNOWN)
13140 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013141 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013142 if (base != 2 && base != 8 && base != 10 && base != 16)
13143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013144 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 return;
13146 }
13147 }
13148
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013149 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013150 isneg = (*p == '-');
13151 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 p = skipwhite(p + 1);
13153 switch (base)
13154 {
13155 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13156 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13157 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13158 default: what = 0;
13159 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013160 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13161 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013162 if (isneg)
13163 rettv->vval.v_number = -n;
13164 else
13165 rettv->vval.v_number = n;
13166
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167}
13168
13169#ifdef HAVE_STRFTIME
13170/*
13171 * "strftime({format}[, {time}])" function
13172 */
13173 static void
13174f_strftime(typval_T *argvars, typval_T *rettv)
13175{
13176 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013177# ifdef HAVE_LOCALTIME_R
13178 struct tm tmval;
13179# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 struct tm *curtime;
13181 time_t seconds;
13182 char_u *p;
13183
13184 rettv->v_type = VAR_STRING;
13185
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013186 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 if (argvars[1].v_type == VAR_UNKNOWN)
13188 seconds = time(NULL);
13189 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013190 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013191# ifdef HAVE_LOCALTIME_R
13192 curtime = localtime_r(&seconds, &tmval);
13193# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013195# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196 /* MSVC returns NULL for an invalid value of seconds. */
13197 if (curtime == NULL)
13198 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13199 else
13200 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 vimconv_T conv;
13202 char_u *enc;
13203
13204 conv.vc_type = CONV_NONE;
13205 enc = enc_locale();
13206 convert_setup(&conv, p_enc, enc);
13207 if (conv.vc_type != CONV_NONE)
13208 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 if (p != NULL)
13210 (void)strftime((char *)result_buf, sizeof(result_buf),
13211 (char *)p, curtime);
13212 else
13213 result_buf[0] = NUL;
13214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013215 if (conv.vc_type != CONV_NONE)
13216 vim_free(p);
13217 convert_setup(&conv, enc, p_enc);
13218 if (conv.vc_type != CONV_NONE)
13219 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13220 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221 rettv->vval.v_string = vim_strsave(result_buf);
13222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 /* Release conversion descriptors */
13224 convert_setup(&conv, NULL, NULL);
13225 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 }
13227}
13228#endif
13229
13230/*
13231 * "strgetchar()" function
13232 */
13233 static void
13234f_strgetchar(typval_T *argvars, typval_T *rettv)
13235{
13236 char_u *str;
13237 int len;
13238 int error = FALSE;
13239 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013240 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013241
13242 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013243 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013244 if (str == NULL)
13245 return;
13246 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013247 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013248 if (error)
13249 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013250
Bram Moolenaar13505972019-01-24 15:04:48 +010013251 while (charidx >= 0 && byteidx < len)
13252 {
13253 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013255 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13256 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013258 --charidx;
13259 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013260 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261}
13262
13263/*
13264 * "stridx()" function
13265 */
13266 static void
13267f_stridx(typval_T *argvars, typval_T *rettv)
13268{
13269 char_u buf[NUMBUFLEN];
13270 char_u *needle;
13271 char_u *haystack;
13272 char_u *save_haystack;
13273 char_u *pos;
13274 int start_idx;
13275
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013276 needle = tv_get_string_chk(&argvars[1]);
13277 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 rettv->vval.v_number = -1;
13279 if (needle == NULL || haystack == NULL)
13280 return; /* type error; errmsg already given */
13281
13282 if (argvars[2].v_type != VAR_UNKNOWN)
13283 {
13284 int error = FALSE;
13285
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013286 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013287 if (error || start_idx >= (int)STRLEN(haystack))
13288 return;
13289 if (start_idx >= 0)
13290 haystack += start_idx;
13291 }
13292
13293 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13294 if (pos != NULL)
13295 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13296}
13297
13298/*
13299 * "string()" function
13300 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013301 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302f_string(typval_T *argvars, typval_T *rettv)
13303{
13304 char_u *tofree;
13305 char_u numbuf[NUMBUFLEN];
13306
13307 rettv->v_type = VAR_STRING;
13308 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13309 get_copyID());
13310 /* Make a copy if we have a value but it's not in allocated memory. */
13311 if (rettv->vval.v_string != NULL && tofree == NULL)
13312 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13313}
13314
13315/*
13316 * "strlen()" function
13317 */
13318 static void
13319f_strlen(typval_T *argvars, typval_T *rettv)
13320{
13321 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013322 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323}
13324
13325/*
13326 * "strchars()" function
13327 */
13328 static void
13329f_strchars(typval_T *argvars, typval_T *rettv)
13330{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013333 varnumber_T len = 0;
13334 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013335
13336 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013337 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013339 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013340 else
13341 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13343 while (*s != NUL)
13344 {
13345 func_mb_ptr2char_adv(&s);
13346 ++len;
13347 }
13348 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013349 }
13350}
13351
13352/*
13353 * "strdisplaywidth()" function
13354 */
13355 static void
13356f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13357{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013358 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013359 int col = 0;
13360
13361 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013362 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013363
13364 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13365}
13366
13367/*
13368 * "strwidth()" function
13369 */
13370 static void
13371f_strwidth(typval_T *argvars, typval_T *rettv)
13372{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013373 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374
Bram Moolenaar13505972019-01-24 15:04:48 +010013375 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013376}
13377
13378/*
13379 * "strcharpart()" function
13380 */
13381 static void
13382f_strcharpart(typval_T *argvars, typval_T *rettv)
13383{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384 char_u *p;
13385 int nchar;
13386 int nbyte = 0;
13387 int charlen;
13388 int len = 0;
13389 int slen;
13390 int error = FALSE;
13391
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013392 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013393 slen = (int)STRLEN(p);
13394
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013395 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013396 if (!error)
13397 {
13398 if (nchar > 0)
13399 while (nchar > 0 && nbyte < slen)
13400 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013401 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013402 --nchar;
13403 }
13404 else
13405 nbyte = nchar;
13406 if (argvars[2].v_type != VAR_UNKNOWN)
13407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013408 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 while (charlen > 0 && nbyte + len < slen)
13410 {
13411 int off = nbyte + len;
13412
13413 if (off < 0)
13414 len += 1;
13415 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013416 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013417 --charlen;
13418 }
13419 }
13420 else
13421 len = slen - nbyte; /* default: all bytes that are available. */
13422 }
13423
13424 /*
13425 * Only return the overlap between the specified part and the actual
13426 * string.
13427 */
13428 if (nbyte < 0)
13429 {
13430 len += nbyte;
13431 nbyte = 0;
13432 }
13433 else if (nbyte > slen)
13434 nbyte = slen;
13435 if (len < 0)
13436 len = 0;
13437 else if (nbyte + len > slen)
13438 len = slen - nbyte;
13439
13440 rettv->v_type = VAR_STRING;
13441 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442}
13443
13444/*
13445 * "strpart()" function
13446 */
13447 static void
13448f_strpart(typval_T *argvars, typval_T *rettv)
13449{
13450 char_u *p;
13451 int n;
13452 int len;
13453 int slen;
13454 int error = FALSE;
13455
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013456 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013457 slen = (int)STRLEN(p);
13458
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013459 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013460 if (error)
13461 len = 0;
13462 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013463 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013464 else
13465 len = slen - n; /* default len: all bytes that are available. */
13466
13467 /*
13468 * Only return the overlap between the specified part and the actual
13469 * string.
13470 */
13471 if (n < 0)
13472 {
13473 len += n;
13474 n = 0;
13475 }
13476 else if (n > slen)
13477 n = slen;
13478 if (len < 0)
13479 len = 0;
13480 else if (n + len > slen)
13481 len = slen - n;
13482
13483 rettv->v_type = VAR_STRING;
13484 rettv->vval.v_string = vim_strnsave(p + n, len);
13485}
13486
13487/*
13488 * "strridx()" function
13489 */
13490 static void
13491f_strridx(typval_T *argvars, typval_T *rettv)
13492{
13493 char_u buf[NUMBUFLEN];
13494 char_u *needle;
13495 char_u *haystack;
13496 char_u *rest;
13497 char_u *lastmatch = NULL;
13498 int haystack_len, end_idx;
13499
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013500 needle = tv_get_string_chk(&argvars[1]);
13501 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013502
13503 rettv->vval.v_number = -1;
13504 if (needle == NULL || haystack == NULL)
13505 return; /* type error; errmsg already given */
13506
13507 haystack_len = (int)STRLEN(haystack);
13508 if (argvars[2].v_type != VAR_UNKNOWN)
13509 {
13510 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013511 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013512 if (end_idx < 0)
13513 return; /* can never find a match */
13514 }
13515 else
13516 end_idx = haystack_len;
13517
13518 if (*needle == NUL)
13519 {
13520 /* Empty string matches past the end. */
13521 lastmatch = haystack + end_idx;
13522 }
13523 else
13524 {
13525 for (rest = haystack; *rest != '\0'; ++rest)
13526 {
13527 rest = (char_u *)strstr((char *)rest, (char *)needle);
13528 if (rest == NULL || rest > haystack + end_idx)
13529 break;
13530 lastmatch = rest;
13531 }
13532 }
13533
13534 if (lastmatch == NULL)
13535 rettv->vval.v_number = -1;
13536 else
13537 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13538}
13539
13540/*
13541 * "strtrans()" function
13542 */
13543 static void
13544f_strtrans(typval_T *argvars, typval_T *rettv)
13545{
13546 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013547 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548}
13549
13550/*
13551 * "submatch()" function
13552 */
13553 static void
13554f_submatch(typval_T *argvars, typval_T *rettv)
13555{
13556 int error = FALSE;
13557 int no;
13558 int retList = 0;
13559
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013560 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561 if (error)
13562 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013563 if (no < 0 || no >= NSUBEXP)
13564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013565 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013566 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013567 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013568 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013569 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013570 if (error)
13571 return;
13572
13573 if (retList == 0)
13574 {
13575 rettv->v_type = VAR_STRING;
13576 rettv->vval.v_string = reg_submatch(no);
13577 }
13578 else
13579 {
13580 rettv->v_type = VAR_LIST;
13581 rettv->vval.v_list = reg_submatch_list(no);
13582 }
13583}
13584
13585/*
13586 * "substitute()" function
13587 */
13588 static void
13589f_substitute(typval_T *argvars, typval_T *rettv)
13590{
13591 char_u patbuf[NUMBUFLEN];
13592 char_u subbuf[NUMBUFLEN];
13593 char_u flagsbuf[NUMBUFLEN];
13594
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013595 char_u *str = tv_get_string_chk(&argvars[0]);
13596 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013597 char_u *sub = NULL;
13598 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013599 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013600
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013601 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13602 expr = &argvars[2];
13603 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013604 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013605
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013606 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013607 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13608 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013609 rettv->vval.v_string = NULL;
13610 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013611 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013612}
13613
13614/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013615 * "swapinfo(swap_filename)" function
13616 */
13617 static void
13618f_swapinfo(typval_T *argvars, typval_T *rettv)
13619{
13620 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013621 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013622}
13623
13624/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013625 * "swapname(expr)" function
13626 */
13627 static void
13628f_swapname(typval_T *argvars, typval_T *rettv)
13629{
13630 buf_T *buf;
13631
13632 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013633 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013634 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13635 || buf->b_ml.ml_mfp->mf_fname == NULL)
13636 rettv->vval.v_string = NULL;
13637 else
13638 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13639}
13640
13641/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013642 * "synID(lnum, col, trans)" function
13643 */
13644 static void
13645f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13646{
13647 int id = 0;
13648#ifdef FEAT_SYN_HL
13649 linenr_T lnum;
13650 colnr_T col;
13651 int trans;
13652 int transerr = FALSE;
13653
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013654 lnum = tv_get_lnum(argvars); /* -1 on type error */
13655 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13656 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013657
13658 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13659 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13660 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13661#endif
13662
13663 rettv->vval.v_number = id;
13664}
13665
13666/*
13667 * "synIDattr(id, what [, mode])" function
13668 */
13669 static void
13670f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13671{
13672 char_u *p = NULL;
13673#ifdef FEAT_SYN_HL
13674 int id;
13675 char_u *what;
13676 char_u *mode;
13677 char_u modebuf[NUMBUFLEN];
13678 int modec;
13679
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013680 id = (int)tv_get_number(&argvars[0]);
13681 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013682 if (argvars[2].v_type != VAR_UNKNOWN)
13683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013684 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013685 modec = TOLOWER_ASC(mode[0]);
13686 if (modec != 't' && modec != 'c' && modec != 'g')
13687 modec = 0; /* replace invalid with current */
13688 }
13689 else
13690 {
13691#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13692 if (USE_24BIT)
13693 modec = 'g';
13694 else
13695#endif
13696 if (t_colors > 1)
13697 modec = 'c';
13698 else
13699 modec = 't';
13700 }
13701
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013702 switch (TOLOWER_ASC(what[0]))
13703 {
13704 case 'b':
13705 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13706 p = highlight_color(id, what, modec);
13707 else /* bold */
13708 p = highlight_has_attr(id, HL_BOLD, modec);
13709 break;
13710
13711 case 'f': /* fg[#] or font */
13712 p = highlight_color(id, what, modec);
13713 break;
13714
13715 case 'i':
13716 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13717 p = highlight_has_attr(id, HL_INVERSE, modec);
13718 else /* italic */
13719 p = highlight_has_attr(id, HL_ITALIC, modec);
13720 break;
13721
13722 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013723 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724 break;
13725
13726 case 'r': /* reverse */
13727 p = highlight_has_attr(id, HL_INVERSE, modec);
13728 break;
13729
13730 case 's':
13731 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13732 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013733 /* strikeout */
13734 else if (TOLOWER_ASC(what[1]) == 't' &&
13735 TOLOWER_ASC(what[2]) == 'r')
13736 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013737 else /* standout */
13738 p = highlight_has_attr(id, HL_STANDOUT, modec);
13739 break;
13740
13741 case 'u':
13742 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13743 /* underline */
13744 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13745 else
13746 /* undercurl */
13747 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13748 break;
13749 }
13750
13751 if (p != NULL)
13752 p = vim_strsave(p);
13753#endif
13754 rettv->v_type = VAR_STRING;
13755 rettv->vval.v_string = p;
13756}
13757
13758/*
13759 * "synIDtrans(id)" function
13760 */
13761 static void
13762f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13763{
13764 int id;
13765
13766#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013767 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013768
13769 if (id > 0)
13770 id = syn_get_final_id(id);
13771 else
13772#endif
13773 id = 0;
13774
13775 rettv->vval.v_number = id;
13776}
13777
13778/*
13779 * "synconcealed(lnum, col)" function
13780 */
13781 static void
13782f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13783{
13784#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13785 linenr_T lnum;
13786 colnr_T col;
13787 int syntax_flags = 0;
13788 int cchar;
13789 int matchid = 0;
13790 char_u str[NUMBUFLEN];
13791#endif
13792
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013793 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013794
13795#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013796 lnum = tv_get_lnum(argvars); /* -1 on type error */
13797 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013798
13799 vim_memset(str, NUL, sizeof(str));
13800
13801 if (rettv_list_alloc(rettv) != FAIL)
13802 {
13803 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13804 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13805 && curwin->w_p_cole > 0)
13806 {
13807 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13808 syntax_flags = get_syntax_info(&matchid);
13809
13810 /* get the conceal character */
13811 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13812 {
13813 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013814 if (cchar == NUL && curwin->w_p_cole == 1)
13815 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 if (cchar != NUL)
13817 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013818 if (has_mbyte)
13819 (*mb_char2bytes)(cchar, str);
13820 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013821 str[0] = cchar;
13822 }
13823 }
13824 }
13825
13826 list_append_number(rettv->vval.v_list,
13827 (syntax_flags & HL_CONCEAL) != 0);
13828 /* -1 to auto-determine strlen */
13829 list_append_string(rettv->vval.v_list, str, -1);
13830 list_append_number(rettv->vval.v_list, matchid);
13831 }
13832#endif
13833}
13834
13835/*
13836 * "synstack(lnum, col)" function
13837 */
13838 static void
13839f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13840{
13841#ifdef FEAT_SYN_HL
13842 linenr_T lnum;
13843 colnr_T col;
13844 int i;
13845 int id;
13846#endif
13847
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013848 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013849
13850#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013851 lnum = tv_get_lnum(argvars); /* -1 on type error */
13852 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013853
13854 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13855 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13856 && rettv_list_alloc(rettv) != FAIL)
13857 {
13858 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13859 for (i = 0; ; ++i)
13860 {
13861 id = syn_get_stack_item(i);
13862 if (id < 0)
13863 break;
13864 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13865 break;
13866 }
13867 }
13868#endif
13869}
13870
13871 static void
13872get_cmd_output_as_rettv(
13873 typval_T *argvars,
13874 typval_T *rettv,
13875 int retlist)
13876{
13877 char_u *res = NULL;
13878 char_u *p;
13879 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880 int err = FALSE;
13881 FILE *fd;
13882 list_T *list = NULL;
13883 int flags = SHELL_SILENT;
13884
13885 rettv->v_type = VAR_STRING;
13886 rettv->vval.v_string = NULL;
13887 if (check_restricted() || check_secure())
13888 goto errret;
13889
13890 if (argvars[1].v_type != VAR_UNKNOWN)
13891 {
13892 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013893 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013894 * command.
13895 */
13896 if ((infile = vim_tempname('i', TRUE)) == NULL)
13897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013898 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013899 goto errret;
13900 }
13901
13902 fd = mch_fopen((char *)infile, WRITEBIN);
13903 if (fd == NULL)
13904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013905 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906 goto errret;
13907 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013908 if (argvars[1].v_type == VAR_NUMBER)
13909 {
13910 linenr_T lnum;
13911 buf_T *buf;
13912
13913 buf = buflist_findnr(argvars[1].vval.v_number);
13914 if (buf == NULL)
13915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013916 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013917 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013918 goto errret;
13919 }
13920
13921 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13922 {
13923 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13924 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13925 {
13926 err = TRUE;
13927 break;
13928 }
13929 if (putc(NL, fd) == EOF)
13930 {
13931 err = TRUE;
13932 break;
13933 }
13934 }
13935 }
13936 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013937 {
13938 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13939 err = TRUE;
13940 }
13941 else
13942 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013943 size_t len;
13944 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013946 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013947 if (p == NULL)
13948 {
13949 fclose(fd);
13950 goto errret; /* type error; errmsg already given */
13951 }
13952 len = STRLEN(p);
13953 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13954 err = TRUE;
13955 }
13956 if (fclose(fd) != 0)
13957 err = TRUE;
13958 if (err)
13959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013960 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013961 goto errret;
13962 }
13963 }
13964
13965 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13966 * echoes typeahead, that messes up the display. */
13967 if (!msg_silent)
13968 flags += SHELL_COOKED;
13969
13970 if (retlist)
13971 {
13972 int len;
13973 listitem_T *li;
13974 char_u *s = NULL;
13975 char_u *start;
13976 char_u *end;
13977 int i;
13978
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013979 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013980 if (res == NULL)
13981 goto errret;
13982
13983 list = list_alloc();
13984 if (list == NULL)
13985 goto errret;
13986
13987 for (i = 0; i < len; ++i)
13988 {
13989 start = res + i;
13990 while (i < len && res[i] != NL)
13991 ++i;
13992 end = res + i;
13993
Bram Moolenaar964b3742019-05-24 18:54:09 +020013994 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013995 if (s == NULL)
13996 goto errret;
13997
13998 for (p = s; start < end; ++p, ++start)
13999 *p = *start == NUL ? NL : *start;
14000 *p = NUL;
14001
14002 li = listitem_alloc();
14003 if (li == NULL)
14004 {
14005 vim_free(s);
14006 goto errret;
14007 }
14008 li->li_tv.v_type = VAR_STRING;
14009 li->li_tv.v_lock = 0;
14010 li->li_tv.vval.v_string = s;
14011 list_append(list, li);
14012 }
14013
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014014 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014015 list = NULL;
14016 }
14017 else
14018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014019 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014020#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014021 /* translate <CR><NL> into <NL> */
14022 if (res != NULL)
14023 {
14024 char_u *s, *d;
14025
14026 d = res;
14027 for (s = res; *s; ++s)
14028 {
14029 if (s[0] == CAR && s[1] == NL)
14030 ++s;
14031 *d++ = *s;
14032 }
14033 *d = NUL;
14034 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014035#endif
14036 rettv->vval.v_string = res;
14037 res = NULL;
14038 }
14039
14040errret:
14041 if (infile != NULL)
14042 {
14043 mch_remove(infile);
14044 vim_free(infile);
14045 }
14046 if (res != NULL)
14047 vim_free(res);
14048 if (list != NULL)
14049 list_free(list);
14050}
14051
14052/*
14053 * "system()" function
14054 */
14055 static void
14056f_system(typval_T *argvars, typval_T *rettv)
14057{
14058 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14059}
14060
14061/*
14062 * "systemlist()" function
14063 */
14064 static void
14065f_systemlist(typval_T *argvars, typval_T *rettv)
14066{
14067 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14068}
14069
14070/*
14071 * "tabpagebuflist()" function
14072 */
14073 static void
14074f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14075{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 tabpage_T *tp;
14077 win_T *wp = NULL;
14078
14079 if (argvars[0].v_type == VAR_UNKNOWN)
14080 wp = firstwin;
14081 else
14082 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014083 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 if (tp != NULL)
14085 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14086 }
14087 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14088 {
14089 for (; wp != NULL; wp = wp->w_next)
14090 if (list_append_number(rettv->vval.v_list,
14091 wp->w_buffer->b_fnum) == FAIL)
14092 break;
14093 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094}
14095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014096/*
14097 * "tabpagenr()" function
14098 */
14099 static void
14100f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14101{
14102 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014103 char_u *arg;
14104
14105 if (argvars[0].v_type != VAR_UNKNOWN)
14106 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014107 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014108 nr = 0;
14109 if (arg != NULL)
14110 {
14111 if (STRCMP(arg, "$") == 0)
14112 nr = tabpage_index(NULL) - 1;
14113 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014114 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 }
14116 }
14117 else
14118 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119 rettv->vval.v_number = nr;
14120}
14121
14122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123/*
14124 * Common code for tabpagewinnr() and winnr().
14125 */
14126 static int
14127get_winnr(tabpage_T *tp, typval_T *argvar)
14128{
14129 win_T *twin;
14130 int nr = 1;
14131 win_T *wp;
14132 char_u *arg;
14133
14134 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14135 if (argvar->v_type != VAR_UNKNOWN)
14136 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014137 int invalid_arg = FALSE;
14138
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014139 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014140 if (arg == NULL)
14141 nr = 0; /* type error; errmsg already given */
14142 else if (STRCMP(arg, "$") == 0)
14143 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14144 else if (STRCMP(arg, "#") == 0)
14145 {
14146 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14147 if (twin == NULL)
14148 nr = 0;
14149 }
14150 else
14151 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014152 long count;
14153 char_u *endp;
14154
14155 // Extract the window count (if specified). e.g. winnr('3j')
14156 count = strtol((char *)arg, (char **)&endp, 10);
14157 if (count <= 0)
14158 count = 1; // if count is not specified, default to 1
14159 if (endp != NULL && *endp != '\0')
14160 {
14161 if (STRCMP(endp, "j") == 0)
14162 twin = win_vert_neighbor(tp, twin, FALSE, count);
14163 else if (STRCMP(endp, "k") == 0)
14164 twin = win_vert_neighbor(tp, twin, TRUE, count);
14165 else if (STRCMP(endp, "h") == 0)
14166 twin = win_horz_neighbor(tp, twin, TRUE, count);
14167 else if (STRCMP(endp, "l") == 0)
14168 twin = win_horz_neighbor(tp, twin, FALSE, count);
14169 else
14170 invalid_arg = TRUE;
14171 }
14172 else
14173 invalid_arg = TRUE;
14174 }
14175
14176 if (invalid_arg)
14177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014178 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179 nr = 0;
14180 }
14181 }
14182
14183 if (nr > 0)
14184 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14185 wp != twin; wp = wp->w_next)
14186 {
14187 if (wp == NULL)
14188 {
14189 /* didn't find it in this tabpage */
14190 nr = 0;
14191 break;
14192 }
14193 ++nr;
14194 }
14195 return nr;
14196}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014197
14198/*
14199 * "tabpagewinnr()" function
14200 */
14201 static void
14202f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14203{
14204 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014205 tabpage_T *tp;
14206
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014207 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014208 if (tp == NULL)
14209 nr = 0;
14210 else
14211 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014212 rettv->vval.v_number = nr;
14213}
14214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014215/*
14216 * "tagfiles()" function
14217 */
14218 static void
14219f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14220{
14221 char_u *fname;
14222 tagname_T tn;
14223 int first;
14224
14225 if (rettv_list_alloc(rettv) == FAIL)
14226 return;
14227 fname = alloc(MAXPATHL);
14228 if (fname == NULL)
14229 return;
14230
14231 for (first = TRUE; ; first = FALSE)
14232 if (get_tagfname(&tn, first, fname) == FAIL
14233 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14234 break;
14235 tagname_free(&tn);
14236 vim_free(fname);
14237}
14238
14239/*
14240 * "taglist()" function
14241 */
14242 static void
14243f_taglist(typval_T *argvars, typval_T *rettv)
14244{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014245 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014246 char_u *tag_pattern;
14247
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014248 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014249
14250 rettv->vval.v_number = FALSE;
14251 if (*tag_pattern == NUL)
14252 return;
14253
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014254 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014255 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014256 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014257 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014258}
14259
14260/*
14261 * "tempname()" function
14262 */
14263 static void
14264f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14265{
14266 static int x = 'A';
14267
14268 rettv->v_type = VAR_STRING;
14269 rettv->vval.v_string = vim_tempname(x, FALSE);
14270
14271 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14272 * names. Skip 'I' and 'O', they are used for shell redirection. */
14273 do
14274 {
14275 if (x == 'Z')
14276 x = '0';
14277 else if (x == '9')
14278 x = 'A';
14279 else
14280 {
14281#ifdef EBCDIC
14282 if (x == 'I')
14283 x = 'J';
14284 else if (x == 'R')
14285 x = 'S';
14286 else
14287#endif
14288 ++x;
14289 }
14290 } while (x == 'I' || x == 'O');
14291}
14292
14293#ifdef FEAT_FLOAT
14294/*
14295 * "tan()" function
14296 */
14297 static void
14298f_tan(typval_T *argvars, typval_T *rettv)
14299{
14300 float_T f = 0.0;
14301
14302 rettv->v_type = VAR_FLOAT;
14303 if (get_float_arg(argvars, &f) == OK)
14304 rettv->vval.v_float = tan(f);
14305 else
14306 rettv->vval.v_float = 0.0;
14307}
14308
14309/*
14310 * "tanh()" function
14311 */
14312 static void
14313f_tanh(typval_T *argvars, typval_T *rettv)
14314{
14315 float_T f = 0.0;
14316
14317 rettv->v_type = VAR_FLOAT;
14318 if (get_float_arg(argvars, &f) == OK)
14319 rettv->vval.v_float = tanh(f);
14320 else
14321 rettv->vval.v_float = 0.0;
14322}
14323#endif
14324
14325/*
14326 * "test_alloc_fail(id, countdown, repeat)" function
14327 */
14328 static void
14329f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14330{
14331 if (argvars[0].v_type != VAR_NUMBER
14332 || argvars[0].vval.v_number <= 0
14333 || argvars[1].v_type != VAR_NUMBER
14334 || argvars[1].vval.v_number < 0
14335 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014336 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014337 else
14338 {
14339 alloc_fail_id = argvars[0].vval.v_number;
14340 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014341 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014342 alloc_fail_countdown = argvars[1].vval.v_number;
14343 alloc_fail_repeat = argvars[2].vval.v_number;
14344 did_outofmem_msg = FALSE;
14345 }
14346}
14347
14348/*
14349 * "test_autochdir()"
14350 */
14351 static void
14352f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14353{
14354#if defined(FEAT_AUTOCHDIR)
14355 test_autochdir = TRUE;
14356#endif
14357}
14358
14359/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014360 * "test_feedinput()"
14361 */
14362 static void
14363f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14364{
14365#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014366 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014367
14368 if (val != NULL)
14369 {
14370 trash_input_buf();
14371 add_to_input_buf_csi(val, (int)STRLEN(val));
14372 }
14373#endif
14374}
14375
14376/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014377 * "test_getvalue({name})" function
14378 */
14379 static void
14380f_test_getvalue(typval_T *argvars, typval_T *rettv)
14381{
14382 if (argvars[0].v_type != VAR_STRING)
14383 emsg(_(e_invarg));
14384 else
14385 {
14386 char_u *name = tv_get_string(&argvars[0]);
14387
14388 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14389 rettv->vval.v_number = need_fileinfo;
14390 else
14391 semsg(_(e_invarg2), name);
14392 }
14393}
14394
14395/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014396 * "test_option_not_set({name})" function
14397 */
14398 static void
14399f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14400{
14401 char_u *name = (char_u *)"";
14402
14403 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014404 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014405 else
14406 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014407 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014408 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014409 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014410 }
14411}
14412
14413/*
14414 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014415 */
14416 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014417f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014418{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014419 char_u *name = (char_u *)"";
14420 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014421 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014422
14423 if (argvars[0].v_type != VAR_STRING
14424 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014425 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014426 else
14427 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014428 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014429 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014430
14431 if (STRCMP(name, (char_u *)"redraw") == 0)
14432 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014433 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14434 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014435 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14436 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014437 else if (STRCMP(name, (char_u *)"starting") == 0)
14438 {
14439 if (val)
14440 {
14441 if (save_starting < 0)
14442 save_starting = starting;
14443 starting = 0;
14444 }
14445 else
14446 {
14447 starting = save_starting;
14448 save_starting = -1;
14449 }
14450 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014451 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14452 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014453 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14454 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014455 else if (STRCMP(name, (char_u *)"ALL") == 0)
14456 {
14457 disable_char_avail_for_testing = FALSE;
14458 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014459 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014460 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014461 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014462 if (save_starting >= 0)
14463 {
14464 starting = save_starting;
14465 save_starting = -1;
14466 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014467 }
14468 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014469 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014470 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014471}
14472
14473/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014474 * "test_refcount({expr})" function
14475 */
14476 static void
14477f_test_refcount(typval_T *argvars, typval_T *rettv)
14478{
14479 int retval = -1;
14480
14481 switch (argvars[0].v_type)
14482 {
14483 case VAR_UNKNOWN:
14484 case VAR_NUMBER:
14485 case VAR_FLOAT:
14486 case VAR_SPECIAL:
14487 case VAR_STRING:
14488 break;
14489 case VAR_JOB:
14490#ifdef FEAT_JOB_CHANNEL
14491 if (argvars[0].vval.v_job != NULL)
14492 retval = argvars[0].vval.v_job->jv_refcount - 1;
14493#endif
14494 break;
14495 case VAR_CHANNEL:
14496#ifdef FEAT_JOB_CHANNEL
14497 if (argvars[0].vval.v_channel != NULL)
14498 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14499#endif
14500 break;
14501 case VAR_FUNC:
14502 if (argvars[0].vval.v_string != NULL)
14503 {
14504 ufunc_T *fp;
14505
14506 fp = find_func(argvars[0].vval.v_string);
14507 if (fp != NULL)
14508 retval = fp->uf_refcount;
14509 }
14510 break;
14511 case VAR_PARTIAL:
14512 if (argvars[0].vval.v_partial != NULL)
14513 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14514 break;
14515 case VAR_BLOB:
14516 if (argvars[0].vval.v_blob != NULL)
14517 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14518 break;
14519 case VAR_LIST:
14520 if (argvars[0].vval.v_list != NULL)
14521 retval = argvars[0].vval.v_list->lv_refcount - 1;
14522 break;
14523 case VAR_DICT:
14524 if (argvars[0].vval.v_dict != NULL)
14525 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14526 break;
14527 }
14528
14529 rettv->v_type = VAR_NUMBER;
14530 rettv->vval.v_number = retval;
14531
14532}
14533
14534/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014535 * "test_garbagecollect_now()" function
14536 */
14537 static void
14538f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14539{
14540 /* This is dangerous, any Lists and Dicts used internally may be freed
14541 * while still in use. */
14542 garbage_collect(TRUE);
14543}
14544
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014545/*
14546 * "test_ignore_error()" function
14547 */
14548 static void
14549f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14550{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014551 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014552}
14553
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014554 static void
14555f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14556{
14557 rettv->v_type = VAR_BLOB;
14558 rettv->vval.v_blob = NULL;
14559}
14560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014561#ifdef FEAT_JOB_CHANNEL
14562 static void
14563f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14564{
14565 rettv->v_type = VAR_CHANNEL;
14566 rettv->vval.v_channel = NULL;
14567}
14568#endif
14569
14570 static void
14571f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14572{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014573 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014574}
14575
14576#ifdef FEAT_JOB_CHANNEL
14577 static void
14578f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14579{
14580 rettv->v_type = VAR_JOB;
14581 rettv->vval.v_job = NULL;
14582}
14583#endif
14584
14585 static void
14586f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14587{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014588 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589}
14590
14591 static void
14592f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14593{
14594 rettv->v_type = VAR_PARTIAL;
14595 rettv->vval.v_partial = NULL;
14596}
14597
14598 static void
14599f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14600{
14601 rettv->v_type = VAR_STRING;
14602 rettv->vval.v_string = NULL;
14603}
14604
Bram Moolenaarab186732018-09-14 21:27:06 +020014605#ifdef FEAT_GUI
14606 static void
14607f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14608{
14609 char_u *which;
14610 long value;
14611 int dragging;
14612 scrollbar_T *sb = NULL;
14613
14614 if (argvars[0].v_type != VAR_STRING
14615 || (argvars[1].v_type) != VAR_NUMBER
14616 || (argvars[2].v_type) != VAR_NUMBER)
14617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014618 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014619 return;
14620 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014621 which = tv_get_string(&argvars[0]);
14622 value = tv_get_number(&argvars[1]);
14623 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014624
14625 if (STRCMP(which, "left") == 0)
14626 sb = &curwin->w_scrollbars[SBAR_LEFT];
14627 else if (STRCMP(which, "right") == 0)
14628 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14629 else if (STRCMP(which, "hor") == 0)
14630 sb = &gui.bottom_sbar;
14631 if (sb == NULL)
14632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014633 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014634 return;
14635 }
14636 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014637# ifndef USE_ON_FLY_SCROLL
14638 // need to loop through normal_cmd() to handle the scroll events
14639 exec_normal(FALSE, TRUE, FALSE);
14640# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014641}
14642#endif
14643
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014644#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014645 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014646f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14647{
14648 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14649 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14650}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014651#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014652
14653 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014654f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14655{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014656 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014657}
14658
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014659/*
14660 * Get a callback from "arg". It can be a Funcref or a function name.
14661 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014662 * "cb_name" is not allocated.
14663 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014665 callback_T
14666get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014668 callback_T res;
14669
14670 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014671 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14672 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014673 res.cb_partial = arg->vval.v_partial;
14674 ++res.cb_partial->pt_refcount;
14675 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014677 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014679 res.cb_partial = NULL;
14680 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14681 {
14682 // Note that we don't make a copy of the string.
14683 res.cb_name = arg->vval.v_string;
14684 func_ref(res.cb_name);
14685 }
14686 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14687 {
14688 res.cb_name = (char_u *)"";
14689 }
14690 else
14691 {
14692 emsg(_("E921: Invalid callback argument"));
14693 res.cb_name = NULL;
14694 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014696 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014697}
14698
14699/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014700 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701 */
14702 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014703put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014705 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014707 tv->v_type = VAR_PARTIAL;
14708 tv->vval.v_partial = cb->cb_partial;
14709 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014711 else
14712 {
14713 tv->v_type = VAR_FUNC;
14714 tv->vval.v_string = vim_strsave(cb->cb_name);
14715 func_ref(cb->cb_name);
14716 }
14717}
14718
14719/*
14720 * Make a copy of "src" into "dest", allocating the function name if needed,
14721 * without incrementing the refcount.
14722 */
14723 void
14724set_callback(callback_T *dest, callback_T *src)
14725{
14726 if (src->cb_partial == NULL)
14727 {
14728 // just a function name, make a copy
14729 dest->cb_name = vim_strsave(src->cb_name);
14730 dest->cb_free_name = TRUE;
14731 }
14732 else
14733 {
14734 // cb_name is a pointer into cb_partial
14735 dest->cb_name = src->cb_name;
14736 dest->cb_free_name = FALSE;
14737 }
14738 dest->cb_partial = src->cb_partial;
14739}
14740
14741/*
14742 * Unref/free "callback" returned by get_callback() or set_callback().
14743 */
14744 void
14745free_callback(callback_T *callback)
14746{
14747 if (callback->cb_partial != NULL)
14748 {
14749 partial_unref(callback->cb_partial);
14750 callback->cb_partial = NULL;
14751 }
14752 else if (callback->cb_name != NULL)
14753 func_unref(callback->cb_name);
14754 if (callback->cb_free_name)
14755 {
14756 vim_free(callback->cb_name);
14757 callback->cb_free_name = FALSE;
14758 }
14759 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761
14762#ifdef FEAT_TIMERS
14763/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014764 * "timer_info([timer])" function
14765 */
14766 static void
14767f_timer_info(typval_T *argvars, typval_T *rettv)
14768{
14769 timer_T *timer = NULL;
14770
14771 if (rettv_list_alloc(rettv) != OK)
14772 return;
14773 if (argvars[0].v_type != VAR_UNKNOWN)
14774 {
14775 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014776 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014777 else
14778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014779 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014780 if (timer != NULL)
14781 add_timer_info(rettv, timer);
14782 }
14783 }
14784 else
14785 add_timer_info_all(rettv);
14786}
14787
14788/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014789 * "timer_pause(timer, paused)" function
14790 */
14791 static void
14792f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14793{
14794 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014795 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014796
14797 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014798 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014799 else
14800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014801 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014802 if (timer != NULL)
14803 timer->tr_paused = paused;
14804 }
14805}
14806
14807/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 * "timer_start(time, callback [, options])" function
14809 */
14810 static void
14811f_timer_start(typval_T *argvars, typval_T *rettv)
14812{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014813 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014814 timer_T *timer;
14815 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014816 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014817 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014818
Bram Moolenaar75537a92016-09-05 22:45:28 +020014819 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014820 if (check_secure())
14821 return;
14822 if (argvars[2].v_type != VAR_UNKNOWN)
14823 {
14824 if (argvars[2].v_type != VAR_DICT
14825 || (dict = argvars[2].vval.v_dict) == NULL)
14826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014827 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828 return;
14829 }
14830 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014831 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014832 }
14833
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014834 callback = get_callback(&argvars[1]);
14835 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014836 return;
14837
14838 timer = create_timer(msec, repeat);
14839 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014840 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014841 else
14842 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014843 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014844 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014845 }
14846}
14847
14848/*
14849 * "timer_stop(timer)" function
14850 */
14851 static void
14852f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14853{
14854 timer_T *timer;
14855
14856 if (argvars[0].v_type != VAR_NUMBER)
14857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014858 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014859 return;
14860 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014861 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014862 if (timer != NULL)
14863 stop_timer(timer);
14864}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014865
14866/*
14867 * "timer_stopall()" function
14868 */
14869 static void
14870f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14871{
14872 stop_all_timers();
14873}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014874#endif
14875
14876/*
14877 * "tolower(string)" function
14878 */
14879 static void
14880f_tolower(typval_T *argvars, typval_T *rettv)
14881{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014882 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014883 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014884}
14885
14886/*
14887 * "toupper(string)" function
14888 */
14889 static void
14890f_toupper(typval_T *argvars, typval_T *rettv)
14891{
14892 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014893 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014894}
14895
14896/*
14897 * "tr(string, fromstr, tostr)" function
14898 */
14899 static void
14900f_tr(typval_T *argvars, typval_T *rettv)
14901{
14902 char_u *in_str;
14903 char_u *fromstr;
14904 char_u *tostr;
14905 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014906 int inlen;
14907 int fromlen;
14908 int tolen;
14909 int idx;
14910 char_u *cpstr;
14911 int cplen;
14912 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014913 char_u buf[NUMBUFLEN];
14914 char_u buf2[NUMBUFLEN];
14915 garray_T ga;
14916
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014917 in_str = tv_get_string(&argvars[0]);
14918 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14919 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014920
14921 /* Default return value: empty string. */
14922 rettv->v_type = VAR_STRING;
14923 rettv->vval.v_string = NULL;
14924 if (fromstr == NULL || tostr == NULL)
14925 return; /* type error; errmsg already given */
14926 ga_init2(&ga, (int)sizeof(char), 80);
14927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014928 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014929 /* not multi-byte: fromstr and tostr must be the same length */
14930 if (STRLEN(fromstr) != STRLEN(tostr))
14931 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014932error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014933 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014934 ga_clear(&ga);
14935 return;
14936 }
14937
14938 /* fromstr and tostr have to contain the same number of chars */
14939 while (*in_str != NUL)
14940 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014941 if (has_mbyte)
14942 {
14943 inlen = (*mb_ptr2len)(in_str);
14944 cpstr = in_str;
14945 cplen = inlen;
14946 idx = 0;
14947 for (p = fromstr; *p != NUL; p += fromlen)
14948 {
14949 fromlen = (*mb_ptr2len)(p);
14950 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14951 {
14952 for (p = tostr; *p != NUL; p += tolen)
14953 {
14954 tolen = (*mb_ptr2len)(p);
14955 if (idx-- == 0)
14956 {
14957 cplen = tolen;
14958 cpstr = p;
14959 break;
14960 }
14961 }
14962 if (*p == NUL) /* tostr is shorter than fromstr */
14963 goto error;
14964 break;
14965 }
14966 ++idx;
14967 }
14968
14969 if (first && cpstr == in_str)
14970 {
14971 /* Check that fromstr and tostr have the same number of
14972 * (multi-byte) characters. Done only once when a character
14973 * of in_str doesn't appear in fromstr. */
14974 first = FALSE;
14975 for (p = tostr; *p != NUL; p += tolen)
14976 {
14977 tolen = (*mb_ptr2len)(p);
14978 --idx;
14979 }
14980 if (idx != 0)
14981 goto error;
14982 }
14983
14984 (void)ga_grow(&ga, cplen);
14985 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14986 ga.ga_len += cplen;
14987
14988 in_str += inlen;
14989 }
14990 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014991 {
14992 /* When not using multi-byte chars we can do it faster. */
14993 p = vim_strchr(fromstr, *in_str);
14994 if (p != NULL)
14995 ga_append(&ga, tostr[p - fromstr]);
14996 else
14997 ga_append(&ga, *in_str);
14998 ++in_str;
14999 }
15000 }
15001
15002 /* add a terminating NUL */
15003 (void)ga_grow(&ga, 1);
15004 ga_append(&ga, NUL);
15005
15006 rettv->vval.v_string = ga.ga_data;
15007}
15008
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015009/*
15010 * "trim({expr})" function
15011 */
15012 static void
15013f_trim(typval_T *argvars, typval_T *rettv)
15014{
15015 char_u buf1[NUMBUFLEN];
15016 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015017 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015018 char_u *mask = NULL;
15019 char_u *tail;
15020 char_u *prev;
15021 char_u *p;
15022 int c1;
15023
15024 rettv->v_type = VAR_STRING;
15025 if (head == NULL)
15026 {
15027 rettv->vval.v_string = NULL;
15028 return;
15029 }
15030
15031 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015032 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015033
15034 while (*head != NUL)
15035 {
15036 c1 = PTR2CHAR(head);
15037 if (mask == NULL)
15038 {
15039 if (c1 > ' ' && c1 != 0xa0)
15040 break;
15041 }
15042 else
15043 {
15044 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15045 if (c1 == PTR2CHAR(p))
15046 break;
15047 if (*p == NUL)
15048 break;
15049 }
15050 MB_PTR_ADV(head);
15051 }
15052
15053 for (tail = head + STRLEN(head); tail > head; tail = prev)
15054 {
15055 prev = tail;
15056 MB_PTR_BACK(head, prev);
15057 c1 = PTR2CHAR(prev);
15058 if (mask == NULL)
15059 {
15060 if (c1 > ' ' && c1 != 0xa0)
15061 break;
15062 }
15063 else
15064 {
15065 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15066 if (c1 == PTR2CHAR(p))
15067 break;
15068 if (*p == NUL)
15069 break;
15070 }
15071 }
15072 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15073}
15074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015075#ifdef FEAT_FLOAT
15076/*
15077 * "trunc({float})" function
15078 */
15079 static void
15080f_trunc(typval_T *argvars, typval_T *rettv)
15081{
15082 float_T f = 0.0;
15083
15084 rettv->v_type = VAR_FLOAT;
15085 if (get_float_arg(argvars, &f) == OK)
15086 /* trunc() is not in C90, use floor() or ceil() instead. */
15087 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15088 else
15089 rettv->vval.v_float = 0.0;
15090}
15091#endif
15092
15093/*
15094 * "type(expr)" function
15095 */
15096 static void
15097f_type(typval_T *argvars, typval_T *rettv)
15098{
15099 int n = -1;
15100
15101 switch (argvars[0].v_type)
15102 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015103 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15104 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015105 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015106 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15107 case VAR_LIST: n = VAR_TYPE_LIST; break;
15108 case VAR_DICT: n = VAR_TYPE_DICT; break;
15109 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015110 case VAR_SPECIAL:
15111 if (argvars[0].vval.v_number == VVAL_FALSE
15112 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015113 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015114 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015115 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015116 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015117 case VAR_JOB: n = VAR_TYPE_JOB; break;
15118 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015119 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015120 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015121 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015122 n = -1;
15123 break;
15124 }
15125 rettv->vval.v_number = n;
15126}
15127
15128/*
15129 * "undofile(name)" function
15130 */
15131 static void
15132f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15133{
15134 rettv->v_type = VAR_STRING;
15135#ifdef FEAT_PERSISTENT_UNDO
15136 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015137 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015138
15139 if (*fname == NUL)
15140 {
15141 /* If there is no file name there will be no undo file. */
15142 rettv->vval.v_string = NULL;
15143 }
15144 else
15145 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015146 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015147
15148 if (ffname != NULL)
15149 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15150 vim_free(ffname);
15151 }
15152 }
15153#else
15154 rettv->vval.v_string = NULL;
15155#endif
15156}
15157
15158/*
15159 * "undotree()" function
15160 */
15161 static void
15162f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15163{
15164 if (rettv_dict_alloc(rettv) == OK)
15165 {
15166 dict_T *dict = rettv->vval.v_dict;
15167 list_T *list;
15168
Bram Moolenaare0be1672018-07-08 16:50:37 +020015169 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15170 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15171 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15172 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15173 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15174 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015175
15176 list = list_alloc();
15177 if (list != NULL)
15178 {
15179 u_eval_tree(curbuf->b_u_oldhead, list);
15180 dict_add_list(dict, "entries", list);
15181 }
15182 }
15183}
15184
15185/*
15186 * "values(dict)" function
15187 */
15188 static void
15189f_values(typval_T *argvars, typval_T *rettv)
15190{
15191 dict_list(argvars, rettv, 1);
15192}
15193
15194/*
15195 * "virtcol(string)" function
15196 */
15197 static void
15198f_virtcol(typval_T *argvars, typval_T *rettv)
15199{
15200 colnr_T vcol = 0;
15201 pos_T *fp;
15202 int fnum = curbuf->b_fnum;
15203
15204 fp = var2fpos(&argvars[0], FALSE, &fnum);
15205 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15206 && fnum == curbuf->b_fnum)
15207 {
15208 getvvcol(curwin, fp, NULL, NULL, &vcol);
15209 ++vcol;
15210 }
15211
15212 rettv->vval.v_number = vcol;
15213}
15214
15215/*
15216 * "visualmode()" function
15217 */
15218 static void
15219f_visualmode(typval_T *argvars, typval_T *rettv)
15220{
15221 char_u str[2];
15222
15223 rettv->v_type = VAR_STRING;
15224 str[0] = curbuf->b_visual_mode_eval;
15225 str[1] = NUL;
15226 rettv->vval.v_string = vim_strsave(str);
15227
15228 /* A non-zero number or non-empty string argument: reset mode. */
15229 if (non_zero_arg(&argvars[0]))
15230 curbuf->b_visual_mode_eval = NUL;
15231}
15232
15233/*
15234 * "wildmenumode()" function
15235 */
15236 static void
15237f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15238{
15239#ifdef FEAT_WILDMENU
15240 if (wild_menu_showing)
15241 rettv->vval.v_number = 1;
15242#endif
15243}
15244
15245/*
15246 * "winbufnr(nr)" function
15247 */
15248 static void
15249f_winbufnr(typval_T *argvars, typval_T *rettv)
15250{
15251 win_T *wp;
15252
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015253 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015254 if (wp == NULL)
15255 rettv->vval.v_number = -1;
15256 else
15257 rettv->vval.v_number = wp->w_buffer->b_fnum;
15258}
15259
15260/*
15261 * "wincol()" function
15262 */
15263 static void
15264f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15265{
15266 validate_cursor();
15267 rettv->vval.v_number = curwin->w_wcol + 1;
15268}
15269
15270/*
15271 * "winheight(nr)" function
15272 */
15273 static void
15274f_winheight(typval_T *argvars, typval_T *rettv)
15275{
15276 win_T *wp;
15277
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015278 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015279 if (wp == NULL)
15280 rettv->vval.v_number = -1;
15281 else
15282 rettv->vval.v_number = wp->w_height;
15283}
15284
15285/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015286 * "winlayout()" function
15287 */
15288 static void
15289f_winlayout(typval_T *argvars, typval_T *rettv)
15290{
15291 tabpage_T *tp;
15292
15293 if (rettv_list_alloc(rettv) != OK)
15294 return;
15295
15296 if (argvars[0].v_type == VAR_UNKNOWN)
15297 tp = curtab;
15298 else
15299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015300 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015301 if (tp == NULL)
15302 return;
15303 }
15304
15305 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15306}
15307
15308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015309 * "winline()" function
15310 */
15311 static void
15312f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15313{
15314 validate_cursor();
15315 rettv->vval.v_number = curwin->w_wrow + 1;
15316}
15317
15318/*
15319 * "winnr()" function
15320 */
15321 static void
15322f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15323{
15324 int nr = 1;
15325
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015326 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327 rettv->vval.v_number = nr;
15328}
15329
15330/*
15331 * "winrestcmd()" function
15332 */
15333 static void
15334f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15335{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015336 win_T *wp;
15337 int winnr = 1;
15338 garray_T ga;
15339 char_u buf[50];
15340
15341 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015342 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015343 {
15344 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15345 ga_concat(&ga, buf);
15346 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15347 ga_concat(&ga, buf);
15348 ++winnr;
15349 }
15350 ga_append(&ga, NUL);
15351
15352 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015353 rettv->v_type = VAR_STRING;
15354}
15355
15356/*
15357 * "winrestview()" function
15358 */
15359 static void
15360f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15361{
15362 dict_T *dict;
15363
15364 if (argvars[0].v_type != VAR_DICT
15365 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015366 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015367 else
15368 {
15369 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015370 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015371 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015372 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015373 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015374 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015375 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15376 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015377 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015378 curwin->w_set_curswant = FALSE;
15379 }
15380
15381 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015382 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015383#ifdef FEAT_DIFF
15384 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015385 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386#endif
15387 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015388 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015389 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015390 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391
15392 check_cursor();
15393 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015394 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015395 changed_window_setting();
15396
15397 if (curwin->w_topline <= 0)
15398 curwin->w_topline = 1;
15399 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15400 curwin->w_topline = curbuf->b_ml.ml_line_count;
15401#ifdef FEAT_DIFF
15402 check_topfill(curwin, TRUE);
15403#endif
15404 }
15405}
15406
15407/*
15408 * "winsaveview()" function
15409 */
15410 static void
15411f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15412{
15413 dict_T *dict;
15414
15415 if (rettv_dict_alloc(rettv) == FAIL)
15416 return;
15417 dict = rettv->vval.v_dict;
15418
Bram Moolenaare0be1672018-07-08 16:50:37 +020015419 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15420 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015421 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015422 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015423 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015424
Bram Moolenaare0be1672018-07-08 16:50:37 +020015425 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015426#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015427 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015428#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015429 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15430 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015431}
15432
15433/*
15434 * "winwidth(nr)" function
15435 */
15436 static void
15437f_winwidth(typval_T *argvars, typval_T *rettv)
15438{
15439 win_T *wp;
15440
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015441 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015442 if (wp == NULL)
15443 rettv->vval.v_number = -1;
15444 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015445 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015446}
15447
15448/*
15449 * "wordcount()" function
15450 */
15451 static void
15452f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15453{
15454 if (rettv_dict_alloc(rettv) == FAIL)
15455 return;
15456 cursor_pos_info(rettv->vval.v_dict);
15457}
15458
15459/*
15460 * "writefile()" function
15461 */
15462 static void
15463f_writefile(typval_T *argvars, typval_T *rettv)
15464{
15465 int binary = FALSE;
15466 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015467#ifdef HAVE_FSYNC
15468 int do_fsync = p_fs;
15469#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015470 char_u *fname;
15471 FILE *fd;
15472 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015473 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015474 list_T *list = NULL;
15475 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015476
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015477 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015478 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015479 return;
15480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015481 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015482 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015483 list = argvars[0].vval.v_list;
15484 if (list == NULL)
15485 return;
15486 for (li = list->lv_first; li != NULL; li = li->li_next)
15487 if (tv_get_string_chk(&li->li_tv) == NULL)
15488 return;
15489 }
15490 else if (argvars[0].v_type == VAR_BLOB)
15491 {
15492 blob = argvars[0].vval.v_blob;
15493 if (blob == NULL)
15494 return;
15495 }
15496 else
15497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015498 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015499 return;
15500 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015501
15502 if (argvars[2].v_type != VAR_UNKNOWN)
15503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015504 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015505
15506 if (arg2 == NULL)
15507 return;
15508 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015509 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015510 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015511 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015512#ifdef HAVE_FSYNC
15513 if (vim_strchr(arg2, 's') != NULL)
15514 do_fsync = TRUE;
15515 else if (vim_strchr(arg2, 'S') != NULL)
15516 do_fsync = FALSE;
15517#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015518 }
15519
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015520 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015521 if (fname == NULL)
15522 return;
15523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015524 /* Always open the file in binary mode, library functions have a mind of
15525 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015526 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15527 append ? APPENDBIN : WRITEBIN)) == NULL)
15528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015529 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015530 ret = -1;
15531 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015532 else if (blob)
15533 {
15534 if (write_blob(fd, blob) == FAIL)
15535 ret = -1;
15536#ifdef HAVE_FSYNC
15537 else if (do_fsync)
15538 // Ignore the error, the user wouldn't know what to do about it.
15539 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015540 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015541#endif
15542 fclose(fd);
15543 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015544 else
15545 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015546 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015547 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015548#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015549 else if (do_fsync)
15550 /* Ignore the error, the user wouldn't know what to do about it.
15551 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015552 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015553#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015554 fclose(fd);
15555 }
15556
15557 rettv->vval.v_number = ret;
15558}
15559
15560/*
15561 * "xor(expr, expr)" function
15562 */
15563 static void
15564f_xor(typval_T *argvars, typval_T *rettv)
15565{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015566 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15567 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015568}
15569
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015570#endif /* FEAT_EVAL */