blob: 134280f6de9ebf4a7398361d23ae92bfe895ed26 [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 Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200818 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200819 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200820 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200821 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200822 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824#ifdef FEAT_FLOAT
825 {"pow", 2, 2, f_pow},
826#endif
827 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100828 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200829#ifdef FEAT_JOB_CHANNEL
830 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200831 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200832 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
833#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100834#ifdef FEAT_TEXT_PROP
835 {"prop_add", 3, 3, f_prop_add},
836 {"prop_clear", 1, 3, f_prop_clear},
837 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100838 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100839 {"prop_type_add", 2, 2, f_prop_type_add},
840 {"prop_type_change", 2, 2, f_prop_type_change},
841 {"prop_type_delete", 1, 2, f_prop_type_delete},
842 {"prop_type_get", 1, 2, f_prop_type_get},
843 {"prop_type_list", 0, 1, f_prop_type_list},
844#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845 {"pumvisible", 0, 0, f_pumvisible},
846#ifdef FEAT_PYTHON3
847 {"py3eval", 1, 1, f_py3eval},
848#endif
849#ifdef FEAT_PYTHON
850 {"pyeval", 1, 1, f_pyeval},
851#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100852#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
853 {"pyxeval", 1, 1, f_pyxeval},
854#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200856 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200858 {"reg_executing", 0, 0, f_reg_executing},
859 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"reltime", 0, 2, f_reltime},
861#ifdef FEAT_FLOAT
862 {"reltimefloat", 1, 1, f_reltimefloat},
863#endif
864 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100865 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"remote_foreground", 1, 1, f_remote_foreground},
867 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100868 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100870 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"remove", 2, 3, f_remove},
872 {"rename", 2, 2, f_rename},
873 {"repeat", 2, 2, f_repeat},
874 {"resolve", 1, 1, f_resolve},
875 {"reverse", 1, 1, f_reverse},
876#ifdef FEAT_FLOAT
877 {"round", 1, 1, f_round},
878#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100879#ifdef FEAT_RUBY
880 {"rubyeval", 1, 1, f_rubyeval},
881#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"screenattr", 2, 2, f_screenattr},
883 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100884 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"screencol", 0, 0, f_screencol},
886 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100887 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888 {"search", 1, 4, f_search},
889 {"searchdecl", 1, 3, f_searchdecl},
890 {"searchpair", 3, 7, f_searchpair},
891 {"searchpairpos", 3, 7, f_searchpairpos},
892 {"searchpos", 1, 4, f_searchpos},
893 {"server2client", 2, 2, f_server2client},
894 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200895 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"setbufvar", 3, 3, f_setbufvar},
897 {"setcharsearch", 1, 1, f_setcharsearch},
898 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200899 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200900 {"setfperm", 2, 2, f_setfperm},
901 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200902 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100903 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200905 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"setreg", 2, 3, f_setreg},
907 {"settabvar", 3, 3, f_settabvar},
908 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100909 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"setwinvar", 3, 3, f_setwinvar},
911#ifdef FEAT_CRYPT
912 {"sha256", 1, 1, f_sha256},
913#endif
914 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100915 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100916#ifdef FEAT_SIGNS
917 {"sign_define", 1, 2, f_sign_define},
918 {"sign_getdefined", 0, 1, f_sign_getdefined},
919 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100920 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100921 {"sign_place", 4, 5, f_sign_place},
922 {"sign_undefine", 0, 1, f_sign_undefine},
923 {"sign_unplace", 1, 2, f_sign_unplace},
924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925 {"simplify", 1, 1, f_simplify},
926#ifdef FEAT_FLOAT
927 {"sin", 1, 1, f_sin},
928 {"sinh", 1, 1, f_sinh},
929#endif
930 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200931#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200932 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200933 {"sound_playevent", 1, 2, f_sound_playevent},
934 {"sound_playfile", 1, 2, f_sound_playfile},
935 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200936#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200937 {"soundfold", 1, 1, f_soundfold},
938 {"spellbadword", 0, 1, f_spellbadword},
939 {"spellsuggest", 1, 3, f_spellsuggest},
940 {"split", 1, 3, f_split},
941#ifdef FEAT_FLOAT
942 {"sqrt", 1, 1, f_sqrt},
943 {"str2float", 1, 1, f_str2float},
944#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200945 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946 {"str2nr", 1, 2, f_str2nr},
947 {"strcharpart", 2, 3, f_strcharpart},
948 {"strchars", 1, 2, f_strchars},
949 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
950#ifdef HAVE_STRFTIME
951 {"strftime", 1, 2, f_strftime},
952#endif
953 {"strgetchar", 2, 2, f_strgetchar},
954 {"stridx", 2, 3, f_stridx},
955 {"string", 1, 1, f_string},
956 {"strlen", 1, 1, f_strlen},
957 {"strpart", 2, 3, f_strpart},
958 {"strridx", 2, 3, f_strridx},
959 {"strtrans", 1, 1, f_strtrans},
960 {"strwidth", 1, 1, f_strwidth},
961 {"submatch", 1, 2, f_submatch},
962 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200963 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200964 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965 {"synID", 3, 3, f_synID},
966 {"synIDattr", 2, 3, f_synIDattr},
967 {"synIDtrans", 1, 1, f_synIDtrans},
968 {"synconcealed", 2, 2, f_synconcealed},
969 {"synstack", 2, 2, f_synstack},
970 {"system", 1, 2, f_system},
971 {"systemlist", 1, 2, f_systemlist},
972 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
973 {"tabpagenr", 0, 1, f_tabpagenr},
974 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
975 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100976 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977#ifdef FEAT_FLOAT
978 {"tan", 1, 1, f_tan},
979 {"tanh", 1, 1, f_tanh},
980#endif
981 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200982#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100983 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
984 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100985 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200986 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200987# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
988 {"term_getansicolors", 1, 1, f_term_getansicolors},
989# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200991 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200993 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200994 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200995 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200996 {"term_getstatus", 1, 1, f_term_getstatus},
997 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200998 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001000 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001001 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001002# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1003 {"term_setansicolors", 2, 2, f_term_setansicolors},
1004# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001005 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001006 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001007 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001008 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001009 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001010#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1012 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001013 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001015 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001016 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001017 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001018#ifdef FEAT_JOB_CHANNEL
1019 {"test_null_channel", 0, 0, f_test_null_channel},
1020#endif
1021 {"test_null_dict", 0, 0, f_test_null_dict},
1022#ifdef FEAT_JOB_CHANNEL
1023 {"test_null_job", 0, 0, f_test_null_job},
1024#endif
1025 {"test_null_list", 0, 0, f_test_null_list},
1026 {"test_null_partial", 0, 0, f_test_null_partial},
1027 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001028 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001029 {"test_override", 2, 2, f_test_override},
1030 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001031#ifdef FEAT_GUI
1032 {"test_scrollbar", 3, 3, f_test_scrollbar},
1033#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001034#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001035 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001036#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037 {"test_settime", 1, 1, f_test_settime},
1038#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001039 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001040 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001041 {"timer_start", 2, 3, f_timer_start},
1042 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001043 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001044#endif
1045 {"tolower", 1, 1, f_tolower},
1046 {"toupper", 1, 1, f_toupper},
1047 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001048 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001049#ifdef FEAT_FLOAT
1050 {"trunc", 1, 1, f_trunc},
1051#endif
1052 {"type", 1, 1, f_type},
1053 {"undofile", 1, 1, f_undofile},
1054 {"undotree", 0, 0, f_undotree},
1055 {"uniq", 1, 3, f_uniq},
1056 {"values", 1, 1, f_values},
1057 {"virtcol", 1, 1, f_virtcol},
1058 {"visualmode", 0, 1, f_visualmode},
1059 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001060 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001061 {"win_findbuf", 1, 1, f_win_findbuf},
1062 {"win_getid", 0, 2, f_win_getid},
1063 {"win_gotoid", 1, 1, f_win_gotoid},
1064 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1065 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001066 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001067 {"winbufnr", 1, 1, f_winbufnr},
1068 {"wincol", 0, 0, f_wincol},
1069 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001070 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001071 {"winline", 0, 0, f_winline},
1072 {"winnr", 0, 1, f_winnr},
1073 {"winrestcmd", 0, 0, f_winrestcmd},
1074 {"winrestview", 1, 1, f_winrestview},
1075 {"winsaveview", 0, 0, f_winsaveview},
1076 {"winwidth", 1, 1, f_winwidth},
1077 {"wordcount", 0, 0, f_wordcount},
1078 {"writefile", 2, 3, f_writefile},
1079 {"xor", 2, 2, f_xor},
1080};
1081
1082#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1083
1084/*
1085 * Function given to ExpandGeneric() to obtain the list of internal
1086 * or user defined function names.
1087 */
1088 char_u *
1089get_function_name(expand_T *xp, int idx)
1090{
1091 static int intidx = -1;
1092 char_u *name;
1093
1094 if (idx == 0)
1095 intidx = -1;
1096 if (intidx < 0)
1097 {
1098 name = get_user_func_name(xp, idx);
1099 if (name != NULL)
1100 return name;
1101 }
1102 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1103 {
1104 STRCPY(IObuff, functions[intidx].f_name);
1105 STRCAT(IObuff, "(");
1106 if (functions[intidx].f_max_argc == 0)
1107 STRCAT(IObuff, ")");
1108 return IObuff;
1109 }
1110
1111 return NULL;
1112}
1113
1114/*
1115 * Function given to ExpandGeneric() to obtain the list of internal or
1116 * user defined variable or function names.
1117 */
1118 char_u *
1119get_expr_name(expand_T *xp, int idx)
1120{
1121 static int intidx = -1;
1122 char_u *name;
1123
1124 if (idx == 0)
1125 intidx = -1;
1126 if (intidx < 0)
1127 {
1128 name = get_function_name(xp, idx);
1129 if (name != NULL)
1130 return name;
1131 }
1132 return get_user_var_name(xp, ++intidx);
1133}
1134
1135#endif /* FEAT_CMDL_COMPL */
1136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001137/*
1138 * Find internal function in table above.
1139 * Return index, or -1 if not found
1140 */
1141 int
1142find_internal_func(
1143 char_u *name) /* name of the function */
1144{
1145 int first = 0;
1146 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1147 int cmp;
1148 int x;
1149
1150 /*
1151 * Find the function name in the table. Binary search.
1152 */
1153 while (first <= last)
1154 {
1155 x = first + ((unsigned)(last - first) >> 1);
1156 cmp = STRCMP(name, functions[x].f_name);
1157 if (cmp < 0)
1158 last = x - 1;
1159 else if (cmp > 0)
1160 first = x + 1;
1161 else
1162 return x;
1163 }
1164 return -1;
1165}
1166
1167 int
1168call_internal_func(
1169 char_u *name,
1170 int argcount,
1171 typval_T *argvars,
1172 typval_T *rettv)
1173{
1174 int i;
1175
1176 i = find_internal_func(name);
1177 if (i < 0)
1178 return ERROR_UNKNOWN;
1179 if (argcount < functions[i].f_min_argc)
1180 return ERROR_TOOFEW;
1181 if (argcount > functions[i].f_max_argc)
1182 return ERROR_TOOMANY;
1183 argvars[argcount].v_type = VAR_UNKNOWN;
1184 functions[i].f_func(argvars, rettv);
1185 return ERROR_NONE;
1186}
1187
1188/*
1189 * Return TRUE for a non-zero Number and a non-empty String.
1190 */
1191 static int
1192non_zero_arg(typval_T *argvars)
1193{
1194 return ((argvars[0].v_type == VAR_NUMBER
1195 && argvars[0].vval.v_number != 0)
1196 || (argvars[0].v_type == VAR_SPECIAL
1197 && argvars[0].vval.v_number == VVAL_TRUE)
1198 || (argvars[0].v_type == VAR_STRING
1199 && argvars[0].vval.v_string != NULL
1200 && *argvars[0].vval.v_string != NUL));
1201}
1202
1203/*
1204 * Get the lnum from the first argument.
1205 * Also accepts ".", "$", etc., but that only works for the current buffer.
1206 * Returns -1 on error.
1207 */
1208 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001209tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001210{
1211 typval_T rettv;
1212 linenr_T lnum;
1213
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001215 if (lnum == 0) /* no valid number, try using line() */
1216 {
1217 rettv.v_type = VAR_NUMBER;
1218 f_line(argvars, &rettv);
1219 lnum = (linenr_T)rettv.vval.v_number;
1220 clear_tv(&rettv);
1221 }
1222 return lnum;
1223}
1224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001225/*
1226 * Get the lnum from the first argument.
1227 * Also accepts "$", then "buf" is used.
1228 * Returns 0 on error.
1229 */
1230 static linenr_T
1231tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1232{
1233 if (argvars[0].v_type == VAR_STRING
1234 && argvars[0].vval.v_string != NULL
1235 && argvars[0].vval.v_string[0] == '$'
1236 && buf != NULL)
1237 return buf->b_ml.ml_line_count;
1238 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1239}
1240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001241#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242/*
1243 * Get the float value of "argvars[0]" into "f".
1244 * Returns FAIL when the argument is not a Number or Float.
1245 */
1246 static int
1247get_float_arg(typval_T *argvars, float_T *f)
1248{
1249 if (argvars[0].v_type == VAR_FLOAT)
1250 {
1251 *f = argvars[0].vval.v_float;
1252 return OK;
1253 }
1254 if (argvars[0].v_type == VAR_NUMBER)
1255 {
1256 *f = (float_T)argvars[0].vval.v_number;
1257 return OK;
1258 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260 return FAIL;
1261}
1262
1263/*
1264 * "abs(expr)" function
1265 */
1266 static void
1267f_abs(typval_T *argvars, typval_T *rettv)
1268{
1269 if (argvars[0].v_type == VAR_FLOAT)
1270 {
1271 rettv->v_type = VAR_FLOAT;
1272 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1273 }
1274 else
1275 {
1276 varnumber_T n;
1277 int error = FALSE;
1278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001279 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280 if (error)
1281 rettv->vval.v_number = -1;
1282 else if (n > 0)
1283 rettv->vval.v_number = n;
1284 else
1285 rettv->vval.v_number = -n;
1286 }
1287}
1288
1289/*
1290 * "acos()" function
1291 */
1292 static void
1293f_acos(typval_T *argvars, typval_T *rettv)
1294{
1295 float_T f = 0.0;
1296
1297 rettv->v_type = VAR_FLOAT;
1298 if (get_float_arg(argvars, &f) == OK)
1299 rettv->vval.v_float = acos(f);
1300 else
1301 rettv->vval.v_float = 0.0;
1302}
1303#endif
1304
1305/*
1306 * "add(list, item)" function
1307 */
1308 static void
1309f_add(typval_T *argvars, typval_T *rettv)
1310{
1311 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313
1314 rettv->vval.v_number = 1; /* Default: Failed */
1315 if (argvars[0].v_type == VAR_LIST)
1316 {
1317 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001318 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 (char_u *)N_("add() argument"), TRUE)
1320 && list_append_tv(l, &argvars[1]) == OK)
1321 copy_tv(&argvars[0], rettv);
1322 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 else if (argvars[0].v_type == VAR_BLOB)
1324 {
1325 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001326 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327 (char_u *)N_("add() argument"), TRUE))
1328 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001329 int error = FALSE;
1330 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1331
1332 if (!error)
1333 {
1334 ga_append(&b->bv_ga, (int)n);
1335 copy_tv(&argvars[0], rettv);
1336 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001340 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341}
1342
1343/*
1344 * "and(expr, expr)" function
1345 */
1346 static void
1347f_and(typval_T *argvars, typval_T *rettv)
1348{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001349 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1350 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001351}
1352
1353/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001354 * If there is a window for "curbuf", make it the current window.
1355 */
1356 static void
1357find_win_for_curbuf(void)
1358{
1359 wininfo_T *wip;
1360
1361 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1362 {
1363 if (wip->wi_win != NULL)
1364 {
1365 curwin = wip->wi_win;
1366 break;
1367 }
1368 }
1369}
1370
1371/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 */
1374 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001375set_buffer_lines(
1376 buf_T *buf,
1377 linenr_T lnum_arg,
1378 int append,
1379 typval_T *lines,
1380 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381{
Bram Moolenaarca851592018-06-06 21:04:07 +02001382 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1383 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384 list_T *l = NULL;
1385 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 linenr_T append_lnum;
1388 buf_T *curbuf_save = NULL;
1389 win_T *curwin_save = NULL;
1390 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001391
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 /* When using the current buffer ml_mfp will be set if needed. Useful when
1393 * setline() is used on startup. For other buffers the buffer must be
1394 * loaded. */
1395 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001396 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001397 rettv->vval.v_number = 1; /* FAIL */
1398 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 }
1400
Bram Moolenaarca851592018-06-06 21:04:07 +02001401 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 curbuf_save = curbuf;
1404 curwin_save = curwin;
1405 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001406 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 }
1408
1409 if (append)
1410 // appendbufline() uses the line number below which we insert
1411 append_lnum = lnum - 1;
1412 else
1413 // setbufline() uses the line number above which we insert, we only
1414 // append if it's below the last line
1415 append_lnum = curbuf->b_ml.ml_line_count;
1416
1417 if (lines->v_type == VAR_LIST)
1418 {
1419 l = lines->vval.v_list;
1420 li = l->lv_first;
1421 }
1422 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001423 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001424
1425 /* default result is zero == OK */
1426 for (;;)
1427 {
1428 if (l != NULL)
1429 {
1430 /* list argument, get next string */
1431 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001432 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001433 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001434 li = li->li_next;
1435 }
1436
Bram Moolenaarca851592018-06-06 21:04:07 +02001437 rettv->vval.v_number = 1; /* FAIL */
1438 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1439 break;
1440
1441 /* When coming here from Insert mode, sync undo, so that this can be
1442 * undone separately from what was previously inserted. */
1443 if (u_sync_once == 2)
1444 {
1445 u_sync_once = 1; /* notify that u_sync() was called */
1446 u_sync(TRUE);
1447 }
1448
1449 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1450 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001451 // Existing line, replace it.
1452 // Removes any existing text properties.
1453 if (u_savesub(lnum) == OK && ml_replace_len(
1454 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001455 {
1456 changed_bytes(lnum, 0);
1457 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1458 check_cursor_col();
1459 rettv->vval.v_number = 0; /* OK */
1460 }
1461 }
1462 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1463 {
1464 /* append the line */
1465 ++added;
1466 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1467 rettv->vval.v_number = 0; /* OK */
1468 }
1469
1470 if (l == NULL) /* only one string argument */
1471 break;
1472 ++lnum;
1473 }
1474
1475 if (added > 0)
1476 {
1477 win_T *wp;
1478 tabpage_T *tp;
1479
1480 appended_lines_mark(append_lnum, added);
1481 FOR_ALL_TAB_WINDOWS(tp, wp)
1482 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1483 wp->w_cursor.lnum += added;
1484 check_cursor_col();
1485
Bram Moolenaarf2732452018-06-03 14:47:35 +02001486#ifdef FEAT_JOB_CHANNEL
1487 if (bt_prompt(curbuf) && (State & INSERT))
1488 // show the line with the prompt
1489 update_topline();
1490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001491 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001492
1493 if (!is_curbuf)
1494 {
1495 curbuf = curbuf_save;
1496 curwin = curwin_save;
1497 }
1498}
1499
1500/*
1501 * "append(lnum, string/list)" function
1502 */
1503 static void
1504f_append(typval_T *argvars, typval_T *rettv)
1505{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001507
1508 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1509}
1510
1511/*
1512 * "appendbufline(buf, lnum, string/list)" function
1513 */
1514 static void
1515f_appendbufline(typval_T *argvars, typval_T *rettv)
1516{
1517 linenr_T lnum;
1518 buf_T *buf;
1519
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001520 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001521 if (buf == NULL)
1522 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001525 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001526 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1527 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001528}
1529
1530/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001531 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001532 */
1533 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001536 win_T *wp;
1537
1538 if (argvars[0].v_type == VAR_UNKNOWN)
1539 // use the current window
1540 rettv->vval.v_number = ARGCOUNT;
1541 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001542 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001543 // use the global argument list
1544 rettv->vval.v_number = GARGCOUNT;
1545 else
1546 {
1547 // use the argument list of the specified window
1548 wp = find_win_by_nr_or_id(&argvars[0]);
1549 if (wp != NULL)
1550 rettv->vval.v_number = WARGCOUNT(wp);
1551 else
1552 rettv->vval.v_number = -1;
1553 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554}
1555
1556/*
1557 * "argidx()" function
1558 */
1559 static void
1560f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1561{
1562 rettv->vval.v_number = curwin->w_arg_idx;
1563}
1564
1565/*
1566 * "arglistid()" function
1567 */
1568 static void
1569f_arglistid(typval_T *argvars, typval_T *rettv)
1570{
1571 win_T *wp;
1572
1573 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001574 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 if (wp != NULL)
1576 rettv->vval.v_number = wp->w_alist->id;
1577}
1578
1579/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001580 * Get the argument list for a given window
1581 */
1582 static void
1583get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1584{
1585 int idx;
1586
1587 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1588 for (idx = 0; idx < argcount; ++idx)
1589 list_append_string(rettv->vval.v_list,
1590 alist_name(&arglist[idx]), -1);
1591}
1592
1593/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 * "argv(nr)" function
1595 */
1596 static void
1597f_argv(typval_T *argvars, typval_T *rettv)
1598{
1599 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001600 aentry_T *arglist = NULL;
1601 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602
1603 if (argvars[0].v_type != VAR_UNKNOWN)
1604 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001605 if (argvars[1].v_type == VAR_UNKNOWN)
1606 {
1607 arglist = ARGLIST;
1608 argcount = ARGCOUNT;
1609 }
1610 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001611 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001612 {
1613 arglist = GARGLIST;
1614 argcount = GARGCOUNT;
1615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001617 {
1618 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1619
1620 if (wp != NULL)
1621 {
1622 /* Use the argument list of the specified window */
1623 arglist = WARGLIST(wp);
1624 argcount = WARGCOUNT(wp);
1625 }
1626 }
1627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001629 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001630 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001631 if (arglist != NULL && idx >= 0 && idx < argcount)
1632 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1633 else if (idx == -1)
1634 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001636 else
1637 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638}
1639
1640/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001641 * "assert_beeps(cmd [, error])" function
1642 */
1643 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001645{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001647}
1648
1649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650 * "assert_equal(expected, actual[, msg])" function
1651 */
1652 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656}
1657
1658/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001659 * "assert_equalfile(fname-one, fname-two)" function
1660 */
1661 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001663{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001665}
1666
1667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668 * "assert_notequal(expected, actual[, msg])" function
1669 */
1670 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674}
1675
1676/*
1677 * "assert_exception(string[, msg])" function
1678 */
1679 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683}
1684
1685/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001686 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 */
1688 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692}
1693
1694/*
1695 * "assert_false(actual[, msg])" function
1696 */
1697 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001701}
1702
1703/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001704 * "assert_inrange(lower, upper[, msg])" function
1705 */
1706 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001708{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001710}
1711
1712/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713 * "assert_match(pattern, actual[, msg])" function
1714 */
1715 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001718 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719}
1720
1721/*
1722 * "assert_notmatch(pattern, actual[, msg])" function
1723 */
1724 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001725f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001727 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001728}
1729
1730/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001731 * "assert_report(msg)" function
1732 */
1733 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001734f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001735{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001736 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001737}
1738
1739/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001740 * "assert_true(actual[, msg])" function
1741 */
1742 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001743f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001745 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746}
1747
1748#ifdef FEAT_FLOAT
1749/*
1750 * "asin()" function
1751 */
1752 static void
1753f_asin(typval_T *argvars, typval_T *rettv)
1754{
1755 float_T f = 0.0;
1756
1757 rettv->v_type = VAR_FLOAT;
1758 if (get_float_arg(argvars, &f) == OK)
1759 rettv->vval.v_float = asin(f);
1760 else
1761 rettv->vval.v_float = 0.0;
1762}
1763
1764/*
1765 * "atan()" function
1766 */
1767 static void
1768f_atan(typval_T *argvars, typval_T *rettv)
1769{
1770 float_T f = 0.0;
1771
1772 rettv->v_type = VAR_FLOAT;
1773 if (get_float_arg(argvars, &f) == OK)
1774 rettv->vval.v_float = atan(f);
1775 else
1776 rettv->vval.v_float = 0.0;
1777}
1778
1779/*
1780 * "atan2()" function
1781 */
1782 static void
1783f_atan2(typval_T *argvars, typval_T *rettv)
1784{
1785 float_T fx = 0.0, fy = 0.0;
1786
1787 rettv->v_type = VAR_FLOAT;
1788 if (get_float_arg(argvars, &fx) == OK
1789 && get_float_arg(&argvars[1], &fy) == OK)
1790 rettv->vval.v_float = atan2(fx, fy);
1791 else
1792 rettv->vval.v_float = 0.0;
1793}
1794#endif
1795
1796/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001797 * "balloon_show()" function
1798 */
1799#ifdef FEAT_BEVAL
1800 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001801f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1802{
1803 rettv->v_type = VAR_STRING;
1804 if (balloonEval != NULL)
1805 {
1806 if (balloonEval->msg == NULL)
1807 rettv->vval.v_string = NULL;
1808 else
1809 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1810 }
1811}
1812
1813 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001814f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1815{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001816 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817 {
1818 if (argvars[0].v_type == VAR_LIST
1819# ifdef FEAT_GUI
1820 && !gui.in_use
1821# endif
1822 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001823 {
1824 list_T *l = argvars[0].vval.v_list;
1825
1826 // empty list removes the balloon
1827 post_balloon(balloonEval, NULL,
1828 l == NULL || l->lv_len == 0 ? NULL : l);
1829 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001830 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001831 {
1832 char_u *mesg = tv_get_string_chk(&argvars[0]);
1833
1834 if (mesg != NULL)
1835 // empty string removes the balloon
1836 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1837 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001838 }
1839}
1840
Bram Moolenaar669a8282017-11-19 20:13:05 +01001841# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001842 static void
1843f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1844{
1845 if (rettv_list_alloc(rettv) == OK)
1846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001847 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001848
1849 if (msg != NULL)
1850 {
1851 pumitem_T *array;
1852 int size = split_message(msg, &array);
1853 int i;
1854
1855 /* Skip the first and last item, they are always empty. */
1856 for (i = 1; i < size - 1; ++i)
1857 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001858 while (size > 0)
1859 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001860 vim_free(array);
1861 }
1862 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001863}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001864# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001865#endif
1866
1867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 * "browse(save, title, initdir, default)" function
1869 */
1870 static void
1871f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1872{
1873#ifdef FEAT_BROWSE
1874 int save;
1875 char_u *title;
1876 char_u *initdir;
1877 char_u *defname;
1878 char_u buf[NUMBUFLEN];
1879 char_u buf2[NUMBUFLEN];
1880 int error = FALSE;
1881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 save = (int)tv_get_number_chk(&argvars[0], &error);
1883 title = tv_get_string_chk(&argvars[1]);
1884 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1885 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886
1887 if (error || title == NULL || initdir == NULL || defname == NULL)
1888 rettv->vval.v_string = NULL;
1889 else
1890 rettv->vval.v_string =
1891 do_browse(save ? BROWSE_SAVE : 0,
1892 title, defname, NULL, initdir, NULL, curbuf);
1893#else
1894 rettv->vval.v_string = NULL;
1895#endif
1896 rettv->v_type = VAR_STRING;
1897}
1898
1899/*
1900 * "browsedir(title, initdir)" function
1901 */
1902 static void
1903f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1904{
1905#ifdef FEAT_BROWSE
1906 char_u *title;
1907 char_u *initdir;
1908 char_u buf[NUMBUFLEN];
1909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001910 title = tv_get_string_chk(&argvars[0]);
1911 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912
1913 if (title == NULL || initdir == NULL)
1914 rettv->vval.v_string = NULL;
1915 else
1916 rettv->vval.v_string = do_browse(BROWSE_DIR,
1917 title, NULL, NULL, initdir, NULL, curbuf);
1918#else
1919 rettv->vval.v_string = NULL;
1920#endif
1921 rettv->v_type = VAR_STRING;
1922}
1923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924/*
1925 * Find a buffer by number or exact name.
1926 */
1927 static buf_T *
1928find_buffer(typval_T *avar)
1929{
1930 buf_T *buf = NULL;
1931
1932 if (avar->v_type == VAR_NUMBER)
1933 buf = buflist_findnr((int)avar->vval.v_number);
1934 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1935 {
1936 buf = buflist_findname_exp(avar->vval.v_string);
1937 if (buf == NULL)
1938 {
1939 /* No full path name match, try a match with a URL or a "nofile"
1940 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001941 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001942 if (buf->b_fname != NULL
1943 && (path_with_url(buf->b_fname)
1944#ifdef FEAT_QUICKFIX
1945 || bt_nofile(buf)
1946#endif
1947 )
1948 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1949 break;
1950 }
1951 }
1952 return buf;
1953}
1954
1955/*
1956 * "bufexists(expr)" function
1957 */
1958 static void
1959f_bufexists(typval_T *argvars, typval_T *rettv)
1960{
1961 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1962}
1963
1964/*
1965 * "buflisted(expr)" function
1966 */
1967 static void
1968f_buflisted(typval_T *argvars, typval_T *rettv)
1969{
1970 buf_T *buf;
1971
1972 buf = find_buffer(&argvars[0]);
1973 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1974}
1975
1976/*
1977 * "bufloaded(expr)" function
1978 */
1979 static void
1980f_bufloaded(typval_T *argvars, typval_T *rettv)
1981{
1982 buf_T *buf;
1983
1984 buf = find_buffer(&argvars[0]);
1985 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1986}
1987
1988 buf_T *
1989buflist_find_by_name(char_u *name, int curtab_only)
1990{
1991 int save_magic;
1992 char_u *save_cpo;
1993 buf_T *buf;
1994
1995 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1996 save_magic = p_magic;
1997 p_magic = TRUE;
1998 save_cpo = p_cpo;
1999 p_cpo = (char_u *)"";
2000
2001 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2002 TRUE, FALSE, curtab_only));
2003
2004 p_magic = save_magic;
2005 p_cpo = save_cpo;
2006 return buf;
2007}
2008
2009/*
2010 * Get buffer by number or pattern.
2011 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002012 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002013tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014{
2015 char_u *name = tv->vval.v_string;
2016 buf_T *buf;
2017
2018 if (tv->v_type == VAR_NUMBER)
2019 return buflist_findnr((int)tv->vval.v_number);
2020 if (tv->v_type != VAR_STRING)
2021 return NULL;
2022 if (name == NULL || *name == NUL)
2023 return curbuf;
2024 if (name[0] == '$' && name[1] == NUL)
2025 return lastbuf;
2026
2027 buf = buflist_find_by_name(name, curtab_only);
2028
2029 /* If not found, try expanding the name, like done for bufexists(). */
2030 if (buf == NULL)
2031 buf = find_buffer(tv);
2032
2033 return buf;
2034}
2035
2036/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002037 * Get the buffer from "arg" and give an error and return NULL if it is not
2038 * valid.
2039 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002040 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002041get_buf_arg(typval_T *arg)
2042{
2043 buf_T *buf;
2044
2045 ++emsg_off;
2046 buf = tv_get_buf(arg, FALSE);
2047 --emsg_off;
2048 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002049 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002050 return buf;
2051}
2052
2053/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 * "bufname(expr)" function
2055 */
2056 static void
2057f_bufname(typval_T *argvars, typval_T *rettv)
2058{
2059 buf_T *buf;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002063 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 rettv->v_type = VAR_STRING;
2065 if (buf != NULL && buf->b_fname != NULL)
2066 rettv->vval.v_string = vim_strsave(buf->b_fname);
2067 else
2068 rettv->vval.v_string = NULL;
2069 --emsg_off;
2070}
2071
2072/*
2073 * "bufnr(expr)" function
2074 */
2075 static void
2076f_bufnr(typval_T *argvars, typval_T *rettv)
2077{
2078 buf_T *buf;
2079 int error = FALSE;
2080 char_u *name;
2081
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002082 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002084 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 --emsg_off;
2086
2087 /* If the buffer isn't found and the second argument is not zero create a
2088 * new buffer. */
2089 if (buf == NULL
2090 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002091 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002092 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 && !error)
2095 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2096
2097 if (buf != NULL)
2098 rettv->vval.v_number = buf->b_fnum;
2099 else
2100 rettv->vval.v_number = -1;
2101}
2102
2103 static void
2104buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2105{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 win_T *wp;
2107 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108 buf_T *buf;
2109
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002110 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002112 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002113 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 {
2115 ++winnr;
2116 if (wp->w_buffer == buf)
2117 break;
2118 }
2119 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120 --emsg_off;
2121}
2122
2123/*
2124 * "bufwinid(nr)" function
2125 */
2126 static void
2127f_bufwinid(typval_T *argvars, typval_T *rettv)
2128{
2129 buf_win_common(argvars, rettv, FALSE);
2130}
2131
2132/*
2133 * "bufwinnr(nr)" function
2134 */
2135 static void
2136f_bufwinnr(typval_T *argvars, typval_T *rettv)
2137{
2138 buf_win_common(argvars, rettv, TRUE);
2139}
2140
2141/*
2142 * "byte2line(byte)" function
2143 */
2144 static void
2145f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2146{
2147#ifndef FEAT_BYTEOFF
2148 rettv->vval.v_number = -1;
2149#else
2150 long boff = 0;
2151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002152 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 if (boff < 0)
2154 rettv->vval.v_number = -1;
2155 else
2156 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2157 (linenr_T)0, &boff);
2158#endif
2159}
2160
2161 static void
2162byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2163{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 char_u *str;
2166 varnumber_T idx;
2167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002168 str = tv_get_string_chk(&argvars[0]);
2169 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 rettv->vval.v_number = -1;
2171 if (str == NULL || idx < 0)
2172 return;
2173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174 t = str;
2175 for ( ; idx > 0; idx--)
2176 {
2177 if (*t == NUL) /* EOL reached */
2178 return;
2179 if (enc_utf8 && comp)
2180 t += utf_ptr2len(t);
2181 else
2182 t += (*mb_ptr2len)(t);
2183 }
2184 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002185}
2186
2187/*
2188 * "byteidx()" function
2189 */
2190 static void
2191f_byteidx(typval_T *argvars, typval_T *rettv)
2192{
2193 byteidx(argvars, rettv, FALSE);
2194}
2195
2196/*
2197 * "byteidxcomp()" function
2198 */
2199 static void
2200f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2201{
2202 byteidx(argvars, rettv, TRUE);
2203}
2204
2205/*
2206 * "call(func, arglist [, dict])" function
2207 */
2208 static void
2209f_call(typval_T *argvars, typval_T *rettv)
2210{
2211 char_u *func;
2212 partial_T *partial = NULL;
2213 dict_T *selfdict = NULL;
2214
2215 if (argvars[1].v_type != VAR_LIST)
2216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 return;
2219 }
2220 if (argvars[1].vval.v_list == NULL)
2221 return;
2222
2223 if (argvars[0].v_type == VAR_FUNC)
2224 func = argvars[0].vval.v_string;
2225 else if (argvars[0].v_type == VAR_PARTIAL)
2226 {
2227 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002228 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 }
2230 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002231 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 if (*func == NUL)
2233 return; /* type error or empty name */
2234
2235 if (argvars[2].v_type != VAR_UNKNOWN)
2236 {
2237 if (argvars[2].v_type != VAR_DICT)
2238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240 return;
2241 }
2242 selfdict = argvars[2].vval.v_dict;
2243 }
2244
2245 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2246}
2247
2248#ifdef FEAT_FLOAT
2249/*
2250 * "ceil({float})" function
2251 */
2252 static void
2253f_ceil(typval_T *argvars, typval_T *rettv)
2254{
2255 float_T f = 0.0;
2256
2257 rettv->v_type = VAR_FLOAT;
2258 if (get_float_arg(argvars, &f) == OK)
2259 rettv->vval.v_float = ceil(f);
2260 else
2261 rettv->vval.v_float = 0.0;
2262}
2263#endif
2264
2265#ifdef FEAT_JOB_CHANNEL
2266/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002267 * "ch_canread()" function
2268 */
2269 static void
2270f_ch_canread(typval_T *argvars, typval_T *rettv)
2271{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002272 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002273
2274 rettv->vval.v_number = 0;
2275 if (channel != NULL)
2276 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2277 || channel_has_readahead(channel, PART_OUT)
2278 || channel_has_readahead(channel, PART_ERR);
2279}
2280
2281/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 * "ch_close()" function
2283 */
2284 static void
2285f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2286{
2287 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2288
2289 if (channel != NULL)
2290 {
2291 channel_close(channel, FALSE);
2292 channel_clear(channel);
2293 }
2294}
2295
2296/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002297 * "ch_close()" function
2298 */
2299 static void
2300f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2301{
2302 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2303
2304 if (channel != NULL)
2305 channel_close_in(channel);
2306}
2307
2308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002309 * "ch_getbufnr()" function
2310 */
2311 static void
2312f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2313{
2314 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2315
2316 rettv->vval.v_number = -1;
2317 if (channel != NULL)
2318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002319 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002320 int part;
2321
2322 if (STRCMP(what, "err") == 0)
2323 part = PART_ERR;
2324 else if (STRCMP(what, "out") == 0)
2325 part = PART_OUT;
2326 else if (STRCMP(what, "in") == 0)
2327 part = PART_IN;
2328 else
2329 part = PART_SOCK;
2330 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2331 rettv->vval.v_number =
2332 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2333 }
2334}
2335
2336/*
2337 * "ch_getjob()" function
2338 */
2339 static void
2340f_ch_getjob(typval_T *argvars, typval_T *rettv)
2341{
2342 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2343
2344 if (channel != NULL)
2345 {
2346 rettv->v_type = VAR_JOB;
2347 rettv->vval.v_job = channel->ch_job;
2348 if (channel->ch_job != NULL)
2349 ++channel->ch_job->jv_refcount;
2350 }
2351}
2352
2353/*
2354 * "ch_info()" function
2355 */
2356 static void
2357f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2358{
2359 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2360
2361 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2362 channel_info(channel, rettv->vval.v_dict);
2363}
2364
2365/*
2366 * "ch_log()" function
2367 */
2368 static void
2369f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2370{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002371 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 channel_T *channel = NULL;
2373
2374 if (argvars[1].v_type != VAR_UNKNOWN)
2375 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2376
Bram Moolenaard5359b22018-04-05 22:44:39 +02002377 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378}
2379
2380/*
2381 * "ch_logfile()" function
2382 */
2383 static void
2384f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2385{
2386 char_u *fname;
2387 char_u *opt = (char_u *)"";
2388 char_u buf[NUMBUFLEN];
2389
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002390 /* Don't open a file in restricted mode. */
2391 if (check_restricted() || check_secure())
2392 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002393 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002395 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 ch_logfile(fname, opt);
2397}
2398
2399/*
2400 * "ch_open()" function
2401 */
2402 static void
2403f_ch_open(typval_T *argvars, typval_T *rettv)
2404{
2405 rettv->v_type = VAR_CHANNEL;
2406 if (check_restricted() || check_secure())
2407 return;
2408 rettv->vval.v_channel = channel_open_func(argvars);
2409}
2410
2411/*
2412 * "ch_read()" function
2413 */
2414 static void
2415f_ch_read(typval_T *argvars, typval_T *rettv)
2416{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002417 common_channel_read(argvars, rettv, FALSE, FALSE);
2418}
2419
2420/*
2421 * "ch_readblob()" function
2422 */
2423 static void
2424f_ch_readblob(typval_T *argvars, typval_T *rettv)
2425{
2426 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427}
2428
2429/*
2430 * "ch_readraw()" function
2431 */
2432 static void
2433f_ch_readraw(typval_T *argvars, typval_T *rettv)
2434{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002435 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002436}
2437
2438/*
2439 * "ch_evalexpr()" function
2440 */
2441 static void
2442f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2443{
2444 ch_expr_common(argvars, rettv, TRUE);
2445}
2446
2447/*
2448 * "ch_sendexpr()" function
2449 */
2450 static void
2451f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2452{
2453 ch_expr_common(argvars, rettv, FALSE);
2454}
2455
2456/*
2457 * "ch_evalraw()" function
2458 */
2459 static void
2460f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2461{
2462 ch_raw_common(argvars, rettv, TRUE);
2463}
2464
2465/*
2466 * "ch_sendraw()" function
2467 */
2468 static void
2469f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2470{
2471 ch_raw_common(argvars, rettv, FALSE);
2472}
2473
2474/*
2475 * "ch_setoptions()" function
2476 */
2477 static void
2478f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2479{
2480 channel_T *channel;
2481 jobopt_T opt;
2482
2483 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2484 if (channel == NULL)
2485 return;
2486 clear_job_options(&opt);
2487 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002488 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002489 channel_set_options(channel, &opt);
2490 free_job_options(&opt);
2491}
2492
2493/*
2494 * "ch_status()" function
2495 */
2496 static void
2497f_ch_status(typval_T *argvars, typval_T *rettv)
2498{
2499 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002500 jobopt_T opt;
2501 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502
2503 /* return an empty string by default */
2504 rettv->v_type = VAR_STRING;
2505 rettv->vval.v_string = NULL;
2506
2507 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002508
2509 if (argvars[1].v_type != VAR_UNKNOWN)
2510 {
2511 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002512 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002513 && (opt.jo_set & JO_PART))
2514 part = opt.jo_part;
2515 }
2516
2517 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518}
2519#endif
2520
2521/*
2522 * "changenr()" function
2523 */
2524 static void
2525f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2526{
2527 rettv->vval.v_number = curbuf->b_u_seq_cur;
2528}
2529
2530/*
2531 * "char2nr(string)" function
2532 */
2533 static void
2534f_char2nr(typval_T *argvars, typval_T *rettv)
2535{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 if (has_mbyte)
2537 {
2538 int utf8 = 0;
2539
2540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542
2543 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002544 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002546 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002547 }
2548 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002549 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550}
2551
2552/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002553 * "chdir(dir)" function
2554 */
2555 static void
2556f_chdir(typval_T *argvars, typval_T *rettv)
2557{
2558 char_u *cwd;
2559 cdscope_T scope = CDSCOPE_GLOBAL;
2560
2561 rettv->v_type = VAR_STRING;
2562 rettv->vval.v_string = NULL;
2563
2564 if (argvars[0].v_type != VAR_STRING)
2565 return;
2566
2567 // Return the current directory
2568 cwd = alloc(MAXPATHL);
2569 if (cwd != NULL)
2570 {
2571 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2572 {
2573#ifdef BACKSLASH_IN_FILENAME
2574 slash_adjust(cwd);
2575#endif
2576 rettv->vval.v_string = vim_strsave(cwd);
2577 }
2578 vim_free(cwd);
2579 }
2580
2581 if (curwin->w_localdir != NULL)
2582 scope = CDSCOPE_WINDOW;
2583 else if (curtab->tp_localdir != NULL)
2584 scope = CDSCOPE_TABPAGE;
2585
2586 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2587 // Directory change failed
2588 VIM_CLEAR(rettv->vval.v_string);
2589}
2590
2591/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 * "cindent(lnum)" function
2593 */
2594 static void
2595f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2596{
2597#ifdef FEAT_CINDENT
2598 pos_T pos;
2599 linenr_T lnum;
2600
2601 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002602 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2604 {
2605 curwin->w_cursor.lnum = lnum;
2606 rettv->vval.v_number = get_c_indent();
2607 curwin->w_cursor = pos;
2608 }
2609 else
2610#endif
2611 rettv->vval.v_number = -1;
2612}
2613
Bram Moolenaaraff74912019-03-30 18:11:49 +01002614 static win_T *
2615get_optional_window(typval_T *argvars, int idx)
2616{
2617 win_T *win = curwin;
2618
2619 if (argvars[idx].v_type != VAR_UNKNOWN)
2620 {
2621 win = find_win_by_nr_or_id(&argvars[idx]);
2622 if (win == NULL)
2623 {
2624 emsg(_(e_invalwindow));
2625 return NULL;
2626 }
2627 }
2628 return win;
2629}
2630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631/*
2632 * "clearmatches()" function
2633 */
2634 static void
2635f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2636{
2637#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002638 win_T *win = get_optional_window(argvars, 0);
2639
2640 if (win != NULL)
2641 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642#endif
2643}
2644
2645/*
2646 * "col(string)" function
2647 */
2648 static void
2649f_col(typval_T *argvars, typval_T *rettv)
2650{
2651 colnr_T col = 0;
2652 pos_T *fp;
2653 int fnum = curbuf->b_fnum;
2654
2655 fp = var2fpos(&argvars[0], FALSE, &fnum);
2656 if (fp != NULL && fnum == curbuf->b_fnum)
2657 {
2658 if (fp->col == MAXCOL)
2659 {
2660 /* '> can be MAXCOL, get the length of the line then */
2661 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2662 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2663 else
2664 col = MAXCOL;
2665 }
2666 else
2667 {
2668 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 /* col(".") when the cursor is on the NUL at the end of the line
2670 * because of "coladd" can be seen as an extra column. */
2671 if (virtual_active() && fp == &curwin->w_cursor)
2672 {
2673 char_u *p = ml_get_cursor();
2674
2675 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2676 curwin->w_virtcol - curwin->w_cursor.coladd))
2677 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 int l;
2679
2680 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2681 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 }
2683 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 }
2685 }
2686 rettv->vval.v_number = col;
2687}
2688
2689#if defined(FEAT_INS_EXPAND)
2690/*
2691 * "complete()" function
2692 */
2693 static void
2694f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2695{
2696 int startcol;
2697
2698 if ((State & INSERT) == 0)
2699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002700 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 return;
2702 }
2703
2704 /* Check for undo allowed here, because if something was already inserted
2705 * the line was already saved for undo and this check isn't done. */
2706 if (!undo_allowed())
2707 return;
2708
2709 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002711 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 return;
2713 }
2714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (startcol <= 0)
2717 return;
2718
2719 set_completion(startcol - 1, argvars[1].vval.v_list);
2720}
2721
2722/*
2723 * "complete_add()" function
2724 */
2725 static void
2726f_complete_add(typval_T *argvars, typval_T *rettv)
2727{
2728 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2729}
2730
2731/*
2732 * "complete_check()" function
2733 */
2734 static void
2735f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2736{
2737 int saved = RedrawingDisabled;
2738
2739 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002740 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002741 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 RedrawingDisabled = saved;
2743}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002744
2745/*
2746 * "complete_info()" function
2747 */
2748 static void
2749f_complete_info(typval_T *argvars, typval_T *rettv)
2750{
2751 list_T *what_list = NULL;
2752
2753 if (rettv_dict_alloc(rettv) != OK)
2754 return;
2755
2756 if (argvars[0].v_type != VAR_UNKNOWN)
2757 {
2758 if (argvars[0].v_type != VAR_LIST)
2759 {
2760 emsg(_(e_listreq));
2761 return;
2762 }
2763 what_list = argvars[0].vval.v_list;
2764 }
2765 get_complete_info(what_list, rettv->vval.v_dict);
2766}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767#endif
2768
2769/*
2770 * "confirm(message, buttons[, default [, type]])" function
2771 */
2772 static void
2773f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2774{
2775#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2776 char_u *message;
2777 char_u *buttons = NULL;
2778 char_u buf[NUMBUFLEN];
2779 char_u buf2[NUMBUFLEN];
2780 int def = 1;
2781 int type = VIM_GENERIC;
2782 char_u *typestr;
2783 int error = FALSE;
2784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 if (message == NULL)
2787 error = TRUE;
2788 if (argvars[1].v_type != VAR_UNKNOWN)
2789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002790 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791 if (buttons == NULL)
2792 error = TRUE;
2793 if (argvars[2].v_type != VAR_UNKNOWN)
2794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002795 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 if (argvars[3].v_type != VAR_UNKNOWN)
2797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002798 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 if (typestr == NULL)
2800 error = TRUE;
2801 else
2802 {
2803 switch (TOUPPER_ASC(*typestr))
2804 {
2805 case 'E': type = VIM_ERROR; break;
2806 case 'Q': type = VIM_QUESTION; break;
2807 case 'I': type = VIM_INFO; break;
2808 case 'W': type = VIM_WARNING; break;
2809 case 'G': type = VIM_GENERIC; break;
2810 }
2811 }
2812 }
2813 }
2814 }
2815
2816 if (buttons == NULL || *buttons == NUL)
2817 buttons = (char_u *)_("&Ok");
2818
2819 if (!error)
2820 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2821 def, NULL, FALSE);
2822#endif
2823}
2824
2825/*
2826 * "copy()" function
2827 */
2828 static void
2829f_copy(typval_T *argvars, typval_T *rettv)
2830{
2831 item_copy(&argvars[0], rettv, FALSE, 0);
2832}
2833
2834#ifdef FEAT_FLOAT
2835/*
2836 * "cos()" function
2837 */
2838 static void
2839f_cos(typval_T *argvars, typval_T *rettv)
2840{
2841 float_T f = 0.0;
2842
2843 rettv->v_type = VAR_FLOAT;
2844 if (get_float_arg(argvars, &f) == OK)
2845 rettv->vval.v_float = cos(f);
2846 else
2847 rettv->vval.v_float = 0.0;
2848}
2849
2850/*
2851 * "cosh()" function
2852 */
2853 static void
2854f_cosh(typval_T *argvars, typval_T *rettv)
2855{
2856 float_T f = 0.0;
2857
2858 rettv->v_type = VAR_FLOAT;
2859 if (get_float_arg(argvars, &f) == OK)
2860 rettv->vval.v_float = cosh(f);
2861 else
2862 rettv->vval.v_float = 0.0;
2863}
2864#endif
2865
2866/*
2867 * "count()" function
2868 */
2869 static void
2870f_count(typval_T *argvars, typval_T *rettv)
2871{
2872 long n = 0;
2873 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002874 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875
Bram Moolenaar9966b212017-07-28 16:46:57 +02002876 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002877 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002878
2879 if (argvars[0].v_type == VAR_STRING)
2880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002881 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002882 char_u *p = argvars[0].vval.v_string;
2883 char_u *next;
2884
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002885 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002886 {
2887 if (ic)
2888 {
2889 size_t len = STRLEN(expr);
2890
2891 while (*p != NUL)
2892 {
2893 if (MB_STRNICMP(p, expr, len) == 0)
2894 {
2895 ++n;
2896 p += len;
2897 }
2898 else
2899 MB_PTR_ADV(p);
2900 }
2901 }
2902 else
2903 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2904 != NULL)
2905 {
2906 ++n;
2907 p = next + STRLEN(expr);
2908 }
2909 }
2910
2911 }
2912 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 {
2914 listitem_T *li;
2915 list_T *l;
2916 long idx;
2917
2918 if ((l = argvars[0].vval.v_list) != NULL)
2919 {
2920 li = l->lv_first;
2921 if (argvars[2].v_type != VAR_UNKNOWN)
2922 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 if (argvars[3].v_type != VAR_UNKNOWN)
2924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002925 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 if (!error)
2927 {
2928 li = list_find(l, idx);
2929 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 }
2932 }
2933 if (error)
2934 li = NULL;
2935 }
2936
2937 for ( ; li != NULL; li = li->li_next)
2938 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2939 ++n;
2940 }
2941 }
2942 else if (argvars[0].v_type == VAR_DICT)
2943 {
2944 int todo;
2945 dict_T *d;
2946 hashitem_T *hi;
2947
2948 if ((d = argvars[0].vval.v_dict) != NULL)
2949 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950 if (argvars[2].v_type != VAR_UNKNOWN)
2951 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 }
2955
2956 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2957 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2958 {
2959 if (!HASHITEM_EMPTY(hi))
2960 {
2961 --todo;
2962 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2963 ++n;
2964 }
2965 }
2966 }
2967 }
2968 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 rettv->vval.v_number = n;
2971}
2972
2973/*
2974 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2975 *
2976 * Checks the existence of a cscope connection.
2977 */
2978 static void
2979f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2980{
2981#ifdef FEAT_CSCOPE
2982 int num = 0;
2983 char_u *dbpath = NULL;
2984 char_u *prepend = NULL;
2985 char_u buf[NUMBUFLEN];
2986
2987 if (argvars[0].v_type != VAR_UNKNOWN
2988 && argvars[1].v_type != VAR_UNKNOWN)
2989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002990 num = (int)tv_get_number(&argvars[0]);
2991 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 }
2995
2996 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2997#endif
2998}
2999
3000/*
3001 * "cursor(lnum, col)" function, or
3002 * "cursor(list)"
3003 *
3004 * Moves the cursor to the specified line and column.
3005 * Returns 0 when the position could be set, -1 otherwise.
3006 */
3007 static void
3008f_cursor(typval_T *argvars, typval_T *rettv)
3009{
3010 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 int set_curswant = TRUE;
3013
3014 rettv->vval.v_number = -1;
3015 if (argvars[1].v_type == VAR_UNKNOWN)
3016 {
3017 pos_T pos;
3018 colnr_T curswant = -1;
3019
3020 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 return;
3024 }
3025 line = pos.lnum;
3026 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 if (curswant >= 0)
3029 {
3030 curwin->w_curswant = curswant - 1;
3031 set_curswant = FALSE;
3032 }
3033 }
3034 else
3035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 line = tv_get_lnum(argvars);
3037 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003039 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003041 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 return; /* type error; errmsg already given */
3043 if (line > 0)
3044 curwin->w_cursor.lnum = line;
3045 if (col > 0)
3046 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048
3049 /* Make sure the cursor is in a valid position. */
3050 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 /* Correct cursor for multi-byte character. */
3052 if (has_mbyte)
3053 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054
3055 curwin->w_set_curswant = set_curswant;
3056 rettv->vval.v_number = 0;
3057}
3058
Bram Moolenaar4f974752019-02-17 17:44:42 +01003059#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003060/*
3061 * "debugbreak()" function
3062 */
3063 static void
3064f_debugbreak(typval_T *argvars, typval_T *rettv)
3065{
3066 int pid;
3067
3068 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003069 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003070 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003071 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003072 else
3073 {
3074 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3075
3076 if (hProcess != NULL)
3077 {
3078 DebugBreakProcess(hProcess);
3079 CloseHandle(hProcess);
3080 rettv->vval.v_number = OK;
3081 }
3082 }
3083}
3084#endif
3085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086/*
3087 * "deepcopy()" function
3088 */
3089 static void
3090f_deepcopy(typval_T *argvars, typval_T *rettv)
3091{
3092 int noref = 0;
3093 int copyID;
3094
3095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003096 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003098 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 else
3100 {
3101 copyID = get_copyID();
3102 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3103 }
3104}
3105
3106/*
3107 * "delete()" function
3108 */
3109 static void
3110f_delete(typval_T *argvars, typval_T *rettv)
3111{
3112 char_u nbuf[NUMBUFLEN];
3113 char_u *name;
3114 char_u *flags;
3115
3116 rettv->vval.v_number = -1;
3117 if (check_restricted() || check_secure())
3118 return;
3119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003120 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 if (name == NULL || *name == NUL)
3122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003123 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 return;
3125 }
3126
3127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003128 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129 else
3130 flags = (char_u *)"";
3131
3132 if (*flags == NUL)
3133 /* delete a file */
3134 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3135 else if (STRCMP(flags, "d") == 0)
3136 /* delete an empty directory */
3137 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3138 else if (STRCMP(flags, "rf") == 0)
3139 /* delete a directory recursively */
3140 rettv->vval.v_number = delete_recursive(name);
3141 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003142 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143}
3144
3145/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003146 * "deletebufline()" function
3147 */
3148 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003149f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003150{
3151 buf_T *buf;
3152 linenr_T first, last;
3153 linenr_T lnum;
3154 long count;
3155 int is_curbuf;
3156 buf_T *curbuf_save = NULL;
3157 win_T *curwin_save = NULL;
3158 tabpage_T *tp;
3159 win_T *wp;
3160
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003161 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003162 if (buf == NULL)
3163 {
3164 rettv->vval.v_number = 1; /* FAIL */
3165 return;
3166 }
3167 is_curbuf = buf == curbuf;
3168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003171 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003172 else
3173 last = first;
3174
3175 if (buf->b_ml.ml_mfp == NULL || first < 1
3176 || first > buf->b_ml.ml_line_count || last < first)
3177 {
3178 rettv->vval.v_number = 1; /* FAIL */
3179 return;
3180 }
3181
3182 if (!is_curbuf)
3183 {
3184 curbuf_save = curbuf;
3185 curwin_save = curwin;
3186 curbuf = buf;
3187 find_win_for_curbuf();
3188 }
3189 if (last > curbuf->b_ml.ml_line_count)
3190 last = curbuf->b_ml.ml_line_count;
3191 count = last - first + 1;
3192
3193 // When coming here from Insert mode, sync undo, so that this can be
3194 // undone separately from what was previously inserted.
3195 if (u_sync_once == 2)
3196 {
3197 u_sync_once = 1; // notify that u_sync() was called
3198 u_sync(TRUE);
3199 }
3200
3201 if (u_save(first - 1, last + 1) == FAIL)
3202 {
3203 rettv->vval.v_number = 1; /* FAIL */
3204 return;
3205 }
3206
3207 for (lnum = first; lnum <= last; ++lnum)
3208 ml_delete(first, TRUE);
3209
3210 FOR_ALL_TAB_WINDOWS(tp, wp)
3211 if (wp->w_buffer == buf)
3212 {
3213 if (wp->w_cursor.lnum > last)
3214 wp->w_cursor.lnum -= count;
3215 else if (wp->w_cursor.lnum> first)
3216 wp->w_cursor.lnum = first;
3217 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3218 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3219 }
3220 check_cursor_col();
3221 deleted_lines_mark(first, count);
3222
3223 if (!is_curbuf)
3224 {
3225 curbuf = curbuf_save;
3226 curwin = curwin_save;
3227 }
3228}
3229
3230/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003231 * "did_filetype()" function
3232 */
3233 static void
3234f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3235{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237}
3238
3239/*
3240 * "diff_filler()" function
3241 */
3242 static void
3243f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3244{
3245#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003246 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247#endif
3248}
3249
3250/*
3251 * "diff_hlID()" function
3252 */
3253 static void
3254f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3255{
3256#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003257 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003259 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 static int fnum = 0;
3261 static int change_start = 0;
3262 static int change_end = 0;
3263 static hlf_T hlID = (hlf_T)0;
3264 int filler_lines;
3265 int col;
3266
3267 if (lnum < 0) /* ignore type error in {lnum} arg */
3268 lnum = 0;
3269 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003270 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 || fnum != curbuf->b_fnum)
3272 {
3273 /* New line, buffer, change: need to get the values. */
3274 filler_lines = diff_check(curwin, lnum);
3275 if (filler_lines < 0)
3276 {
3277 if (filler_lines == -1)
3278 {
3279 change_start = MAXCOL;
3280 change_end = -1;
3281 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3282 hlID = HLF_ADD; /* added line */
3283 else
3284 hlID = HLF_CHD; /* changed line */
3285 }
3286 else
3287 hlID = HLF_ADD; /* added line */
3288 }
3289 else
3290 hlID = (hlf_T)0;
3291 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003292 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 fnum = curbuf->b_fnum;
3294 }
3295
3296 if (hlID == HLF_CHD || hlID == HLF_TXD)
3297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003298 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 if (col >= change_start && col <= change_end)
3300 hlID = HLF_TXD; /* changed text */
3301 else
3302 hlID = HLF_CHD; /* changed line */
3303 }
3304 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3305#endif
3306}
3307
3308/*
3309 * "empty({expr})" function
3310 */
3311 static void
3312f_empty(typval_T *argvars, typval_T *rettv)
3313{
3314 int n = FALSE;
3315
3316 switch (argvars[0].v_type)
3317 {
3318 case VAR_STRING:
3319 case VAR_FUNC:
3320 n = argvars[0].vval.v_string == NULL
3321 || *argvars[0].vval.v_string == NUL;
3322 break;
3323 case VAR_PARTIAL:
3324 n = FALSE;
3325 break;
3326 case VAR_NUMBER:
3327 n = argvars[0].vval.v_number == 0;
3328 break;
3329 case VAR_FLOAT:
3330#ifdef FEAT_FLOAT
3331 n = argvars[0].vval.v_float == 0.0;
3332 break;
3333#endif
3334 case VAR_LIST:
3335 n = argvars[0].vval.v_list == NULL
3336 || argvars[0].vval.v_list->lv_first == NULL;
3337 break;
3338 case VAR_DICT:
3339 n = argvars[0].vval.v_dict == NULL
3340 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3341 break;
3342 case VAR_SPECIAL:
3343 n = argvars[0].vval.v_number != VVAL_TRUE;
3344 break;
3345
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003346 case VAR_BLOB:
3347 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003348 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3349 break;
3350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 case VAR_JOB:
3352#ifdef FEAT_JOB_CHANNEL
3353 n = argvars[0].vval.v_job == NULL
3354 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3355 break;
3356#endif
3357 case VAR_CHANNEL:
3358#ifdef FEAT_JOB_CHANNEL
3359 n = argvars[0].vval.v_channel == NULL
3360 || !channel_is_open(argvars[0].vval.v_channel);
3361 break;
3362#endif
3363 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003364 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 n = TRUE;
3366 break;
3367 }
3368
3369 rettv->vval.v_number = n;
3370}
3371
3372/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003373 * "environ()" function
3374 */
3375 static void
3376f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3377{
3378#if !defined(AMIGA)
3379 int i = 0;
3380 char_u *entry, *value;
3381# ifdef MSWIN
3382 extern wchar_t **_wenviron;
3383# else
3384 extern char **environ;
3385# endif
3386
3387 if (rettv_dict_alloc(rettv) != OK)
3388 return;
3389
3390# ifdef MSWIN
3391 if (*_wenviron == NULL)
3392 return;
3393# else
3394 if (*environ == NULL)
3395 return;
3396# endif
3397
3398 for (i = 0; ; ++i)
3399 {
3400# ifdef MSWIN
3401 short_u *p;
3402
3403 if ((p = (short_u *)_wenviron[i]) == NULL)
3404 return;
3405 entry = utf16_to_enc(p, NULL);
3406# else
3407 if ((entry = (char_u *)environ[i]) == NULL)
3408 return;
3409 entry = vim_strsave(entry);
3410# endif
3411 if (entry == NULL) // out of memory
3412 return;
3413 if ((value = vim_strchr(entry, '=')) == NULL)
3414 {
3415 vim_free(entry);
3416 continue;
3417 }
3418 *value++ = NUL;
3419 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3420 vim_free(entry);
3421 }
3422#endif
3423}
3424
3425/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 * "escape({string}, {chars})" function
3427 */
3428 static void
3429f_escape(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u buf[NUMBUFLEN];
3432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3434 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 rettv->v_type = VAR_STRING;
3436}
3437
3438/*
3439 * "eval()" function
3440 */
3441 static void
3442f_eval(typval_T *argvars, typval_T *rettv)
3443{
3444 char_u *s, *p;
3445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003446 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447 if (s != NULL)
3448 s = skipwhite(s);
3449
3450 p = s;
3451 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3452 {
3453 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 need_clr_eos = FALSE;
3456 rettv->v_type = VAR_NUMBER;
3457 rettv->vval.v_number = 0;
3458 }
3459 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003460 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461}
3462
3463/*
3464 * "eventhandler()" function
3465 */
3466 static void
3467f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3468{
3469 rettv->vval.v_number = vgetc_busy;
3470}
3471
3472/*
3473 * "executable()" function
3474 */
3475 static void
3476f_executable(typval_T *argvars, typval_T *rettv)
3477{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003478 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003479
3480 /* Check in $PATH and also check directly if there is a directory name. */
3481 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3482 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3483}
3484
3485static garray_T redir_execute_ga;
3486
3487/*
3488 * Append "value[value_len]" to the execute() output.
3489 */
3490 void
3491execute_redir_str(char_u *value, int value_len)
3492{
3493 int len;
3494
3495 if (value_len == -1)
3496 len = (int)STRLEN(value); /* Append the entire string */
3497 else
3498 len = value_len; /* Append only "value_len" characters */
3499 if (ga_grow(&redir_execute_ga, len) == OK)
3500 {
3501 mch_memmove((char *)redir_execute_ga.ga_data
3502 + redir_execute_ga.ga_len, value, len);
3503 redir_execute_ga.ga_len += len;
3504 }
3505}
3506
3507/*
3508 * Get next line from a list.
3509 * Called by do_cmdline() to get the next line.
3510 * Returns allocated string, or NULL for end of function.
3511 */
3512
3513 static char_u *
3514get_list_line(
3515 int c UNUSED,
3516 void *cookie,
3517 int indent UNUSED)
3518{
3519 listitem_T **p = (listitem_T **)cookie;
3520 listitem_T *item = *p;
3521 char_u buf[NUMBUFLEN];
3522 char_u *s;
3523
3524 if (item == NULL)
3525 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003526 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527 *p = item->li_next;
3528 return s == NULL ? NULL : vim_strsave(s);
3529}
3530
3531/*
3532 * "execute()" function
3533 */
3534 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003535execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536{
3537 char_u *cmd = NULL;
3538 list_T *list = NULL;
3539 int save_msg_silent = msg_silent;
3540 int save_emsg_silent = emsg_silent;
3541 int save_emsg_noredir = emsg_noredir;
3542 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003543 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003545 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003546 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547
3548 rettv->vval.v_string = NULL;
3549 rettv->v_type = VAR_STRING;
3550
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003551 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003553 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 if (list == NULL || list->lv_first == NULL)
3555 /* empty list, no commands, empty output */
3556 return;
3557 ++list->lv_refcount;
3558 }
3559 else
3560 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003561 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 if (cmd == NULL)
3563 return;
3564 }
3565
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003566 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 {
3568 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003569 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570
3571 if (s == NULL)
3572 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003573 if (*s == NUL)
3574 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 if (STRNCMP(s, "silent", 6) == 0)
3576 ++msg_silent;
3577 if (STRCMP(s, "silent!") == 0)
3578 {
3579 emsg_silent = TRUE;
3580 emsg_noredir = TRUE;
3581 }
3582 }
3583 else
3584 ++msg_silent;
3585
3586 if (redir_execute)
3587 save_ga = redir_execute_ga;
3588 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3589 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003590 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003591 if (!echo_output)
3592 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593
3594 if (cmd != NULL)
3595 do_cmdline_cmd(cmd);
3596 else
3597 {
3598 listitem_T *item = list->lv_first;
3599
3600 do_cmdline(NULL, get_list_line, (void *)&item,
3601 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3602 --list->lv_refcount;
3603 }
3604
Bram Moolenaard297f352017-01-29 20:31:21 +01003605 /* Need to append a NUL to the result. */
3606 if (ga_grow(&redir_execute_ga, 1) == OK)
3607 {
3608 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3609 rettv->vval.v_string = redir_execute_ga.ga_data;
3610 }
3611 else
3612 {
3613 ga_clear(&redir_execute_ga);
3614 rettv->vval.v_string = NULL;
3615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616 msg_silent = save_msg_silent;
3617 emsg_silent = save_emsg_silent;
3618 emsg_noredir = save_emsg_noredir;
3619
3620 redir_execute = save_redir_execute;
3621 if (redir_execute)
3622 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003623 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003625 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003626 if (echo_output)
3627 // When not working silently: put it in column zero. A following
3628 // "echon" will overwrite the message, unavoidably.
3629 msg_col = 0;
3630 else
3631 // When working silently: Put it back where it was, since nothing
3632 // should have been written.
3633 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634}
3635
3636/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003637 * "execute()" function
3638 */
3639 static void
3640f_execute(typval_T *argvars, typval_T *rettv)
3641{
3642 execute_common(argvars, rettv, 0);
3643}
3644
3645/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646 * "exepath()" function
3647 */
3648 static void
3649f_exepath(typval_T *argvars, typval_T *rettv)
3650{
3651 char_u *p = NULL;
3652
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 rettv->v_type = VAR_STRING;
3655 rettv->vval.v_string = p;
3656}
3657
3658/*
3659 * "exists()" function
3660 */
3661 static void
3662f_exists(typval_T *argvars, typval_T *rettv)
3663{
3664 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003667 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 if (*p == '$') /* environment variable */
3669 {
3670 /* first try "normal" environment variables (fast) */
3671 if (mch_getenv(p + 1) != NULL)
3672 n = TRUE;
3673 else
3674 {
3675 /* try expanding things like $VIM and ${HOME} */
3676 p = expand_env_save(p);
3677 if (p != NULL && *p != '$')
3678 n = TRUE;
3679 vim_free(p);
3680 }
3681 }
3682 else if (*p == '&' || *p == '+') /* option */
3683 {
3684 n = (get_option_tv(&p, NULL, TRUE) == OK);
3685 if (*skipwhite(p) != NUL)
3686 n = FALSE; /* trailing garbage */
3687 }
3688 else if (*p == '*') /* internal or user defined function */
3689 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003690 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 }
3692 else if (*p == ':')
3693 {
3694 n = cmd_exists(p + 1);
3695 }
3696 else if (*p == '#')
3697 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 if (p[1] == '#')
3699 n = autocmd_supported(p + 2);
3700 else
3701 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 }
3703 else /* internal variable */
3704 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003705 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707
3708 rettv->vval.v_number = n;
3709}
3710
3711#ifdef FEAT_FLOAT
3712/*
3713 * "exp()" function
3714 */
3715 static void
3716f_exp(typval_T *argvars, typval_T *rettv)
3717{
3718 float_T f = 0.0;
3719
3720 rettv->v_type = VAR_FLOAT;
3721 if (get_float_arg(argvars, &f) == OK)
3722 rettv->vval.v_float = exp(f);
3723 else
3724 rettv->vval.v_float = 0.0;
3725}
3726#endif
3727
3728/*
3729 * "expand()" function
3730 */
3731 static void
3732f_expand(typval_T *argvars, typval_T *rettv)
3733{
3734 char_u *s;
3735 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003736 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3738 expand_T xpc;
3739 int error = FALSE;
3740 char_u *result;
3741
3742 rettv->v_type = VAR_STRING;
3743 if (argvars[1].v_type != VAR_UNKNOWN
3744 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003745 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003747 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003749 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 if (*s == '%' || *s == '#' || *s == '<')
3751 {
3752 ++emsg_off;
3753 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3754 --emsg_off;
3755 if (rettv->v_type == VAR_LIST)
3756 {
3757 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3758 list_append_string(rettv->vval.v_list, result, -1);
3759 else
3760 vim_free(result);
3761 }
3762 else
3763 rettv->vval.v_string = result;
3764 }
3765 else
3766 {
3767 /* When the optional second argument is non-zero, don't remove matches
3768 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3769 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003770 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003771 options |= WILD_KEEP_ALL;
3772 if (!error)
3773 {
3774 ExpandInit(&xpc);
3775 xpc.xp_context = EXPAND_FILES;
3776 if (p_wic)
3777 options += WILD_ICASE;
3778 if (rettv->v_type == VAR_STRING)
3779 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3780 options, WILD_ALL);
3781 else if (rettv_list_alloc(rettv) != FAIL)
3782 {
3783 int i;
3784
3785 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3786 for (i = 0; i < xpc.xp_numfiles; i++)
3787 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3788 ExpandCleanup(&xpc);
3789 }
3790 }
3791 else
3792 rettv->vval.v_string = NULL;
3793 }
3794}
3795
3796/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003797 * "expandcmd()" function
3798 * Expand all the special characters in a command string.
3799 */
3800 static void
3801f_expandcmd(typval_T *argvars, typval_T *rettv)
3802{
3803 exarg_T eap;
3804 char_u *cmdstr;
3805 char *errormsg = NULL;
3806
3807 rettv->v_type = VAR_STRING;
3808 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3809
3810 memset(&eap, 0, sizeof(eap));
3811 eap.cmd = cmdstr;
3812 eap.arg = cmdstr;
3813 eap.argt |= NOSPC;
3814 eap.usefilter = FALSE;
3815 eap.nextcmd = NULL;
3816 eap.cmdidx = CMD_USER;
3817
3818 expand_filename(&eap, &cmdstr, &errormsg);
3819 if (errormsg != NULL && *errormsg != NUL)
3820 emsg(errormsg);
3821
3822 rettv->vval.v_string = cmdstr;
3823}
3824
3825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 * "extend(list, list [, idx])" function
3827 * "extend(dict, dict [, action])" function
3828 */
3829 static void
3830f_extend(typval_T *argvars, typval_T *rettv)
3831{
3832 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3833
3834 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3835 {
3836 list_T *l1, *l2;
3837 listitem_T *item;
3838 long before;
3839 int error = FALSE;
3840
3841 l1 = argvars[0].vval.v_list;
3842 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003843 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 && l2 != NULL)
3845 {
3846 if (argvars[2].v_type != VAR_UNKNOWN)
3847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003848 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003849 if (error)
3850 return; /* type error; errmsg already given */
3851
3852 if (before == l1->lv_len)
3853 item = NULL;
3854 else
3855 {
3856 item = list_find(l1, before);
3857 if (item == NULL)
3858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003859 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003860 return;
3861 }
3862 }
3863 }
3864 else
3865 item = NULL;
3866 list_extend(l1, l2, item);
3867
3868 copy_tv(&argvars[0], rettv);
3869 }
3870 }
3871 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3872 {
3873 dict_T *d1, *d2;
3874 char_u *action;
3875 int i;
3876
3877 d1 = argvars[0].vval.v_dict;
3878 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003879 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 && d2 != NULL)
3881 {
3882 /* Check the third argument. */
3883 if (argvars[2].v_type != VAR_UNKNOWN)
3884 {
3885 static char *(av[]) = {"keep", "force", "error"};
3886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 if (action == NULL)
3889 return; /* type error; errmsg already given */
3890 for (i = 0; i < 3; ++i)
3891 if (STRCMP(action, av[i]) == 0)
3892 break;
3893 if (i == 3)
3894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 return;
3897 }
3898 }
3899 else
3900 action = (char_u *)"force";
3901
3902 dict_extend(d1, d2, action);
3903
3904 copy_tv(&argvars[0], rettv);
3905 }
3906 }
3907 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003908 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909}
3910
3911/*
3912 * "feedkeys()" function
3913 */
3914 static void
3915f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3916{
3917 int remap = TRUE;
3918 int insert = FALSE;
3919 char_u *keys, *flags;
3920 char_u nbuf[NUMBUFLEN];
3921 int typed = FALSE;
3922 int execute = FALSE;
3923 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003924 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 char_u *keys_esc;
3926
3927 /* This is not allowed in the sandbox. If the commands would still be
3928 * executed in the sandbox it would be OK, but it probably happens later,
3929 * when "sandbox" is no longer set. */
3930 if (check_secure())
3931 return;
3932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003933 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934
3935 if (argvars[1].v_type != VAR_UNKNOWN)
3936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003937 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 for ( ; *flags != NUL; ++flags)
3939 {
3940 switch (*flags)
3941 {
3942 case 'n': remap = FALSE; break;
3943 case 'm': remap = TRUE; break;
3944 case 't': typed = TRUE; break;
3945 case 'i': insert = TRUE; break;
3946 case 'x': execute = TRUE; break;
3947 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003948 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950 }
3951 }
3952
3953 if (*keys != NUL || execute)
3954 {
3955 /* Need to escape K_SPECIAL and CSI before putting the string in the
3956 * typeahead buffer. */
3957 keys_esc = vim_strsave_escape_csi(keys);
3958 if (keys_esc != NULL)
3959 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003960 if (lowlevel)
3961 {
3962#ifdef USE_INPUT_BUF
3963 add_to_input_buf(keys, (int)STRLEN(keys));
3964#else
3965 emsg(_("E980: lowlevel input not supported"));
3966#endif
3967 }
3968 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003969 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003970 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003972 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003973#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003974 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003975#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003976 )
3977 typebuf_was_filled = TRUE;
3978 }
3979 vim_free(keys_esc);
3980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981 if (execute)
3982 {
3983 int save_msg_scroll = msg_scroll;
3984
3985 /* Avoid a 1 second delay when the keys start Insert mode. */
3986 msg_scroll = FALSE;
3987
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003988 if (!dangerous)
3989 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003990 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003991 if (!dangerous)
3992 --ex_normal_busy;
3993
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994 msg_scroll |= save_msg_scroll;
3995 }
3996 }
3997 }
3998}
3999
4000/*
4001 * "filereadable()" function
4002 */
4003 static void
4004f_filereadable(typval_T *argvars, typval_T *rettv)
4005{
4006 int fd;
4007 char_u *p;
4008 int n;
4009
4010#ifndef O_NONBLOCK
4011# define O_NONBLOCK 0
4012#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004013 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4015 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4016 {
4017 n = TRUE;
4018 close(fd);
4019 }
4020 else
4021 n = FALSE;
4022
4023 rettv->vval.v_number = n;
4024}
4025
4026/*
4027 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4028 * rights to write into.
4029 */
4030 static void
4031f_filewritable(typval_T *argvars, typval_T *rettv)
4032{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004033 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034}
4035
4036 static void
4037findfilendir(
4038 typval_T *argvars UNUSED,
4039 typval_T *rettv,
4040 int find_what UNUSED)
4041{
4042#ifdef FEAT_SEARCHPATH
4043 char_u *fname;
4044 char_u *fresult = NULL;
4045 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4046 char_u *p;
4047 char_u pathbuf[NUMBUFLEN];
4048 int count = 1;
4049 int first = TRUE;
4050 int error = FALSE;
4051#endif
4052
4053 rettv->vval.v_string = NULL;
4054 rettv->v_type = VAR_STRING;
4055
4056#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058
4059 if (argvars[1].v_type != VAR_UNKNOWN)
4060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004061 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004062 if (p == NULL)
4063 error = TRUE;
4064 else
4065 {
4066 if (*p != NUL)
4067 path = p;
4068
4069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004070 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004071 }
4072 }
4073
4074 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4075 error = TRUE;
4076
4077 if (*fname != NUL && !error)
4078 {
4079 do
4080 {
4081 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4082 vim_free(fresult);
4083 fresult = find_file_in_path_option(first ? fname : NULL,
4084 first ? (int)STRLEN(fname) : 0,
4085 0, first, path,
4086 find_what,
4087 curbuf->b_ffname,
4088 find_what == FINDFILE_DIR
4089 ? (char_u *)"" : curbuf->b_p_sua);
4090 first = FALSE;
4091
4092 if (fresult != NULL && rettv->v_type == VAR_LIST)
4093 list_append_string(rettv->vval.v_list, fresult, -1);
4094
4095 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4096 }
4097
4098 if (rettv->v_type == VAR_STRING)
4099 rettv->vval.v_string = fresult;
4100#endif
4101}
4102
4103/*
4104 * "filter()" function
4105 */
4106 static void
4107f_filter(typval_T *argvars, typval_T *rettv)
4108{
4109 filter_map(argvars, rettv, FALSE);
4110}
4111
4112/*
4113 * "finddir({fname}[, {path}[, {count}]])" function
4114 */
4115 static void
4116f_finddir(typval_T *argvars, typval_T *rettv)
4117{
4118 findfilendir(argvars, rettv, FINDFILE_DIR);
4119}
4120
4121/*
4122 * "findfile({fname}[, {path}[, {count}]])" function
4123 */
4124 static void
4125f_findfile(typval_T *argvars, typval_T *rettv)
4126{
4127 findfilendir(argvars, rettv, FINDFILE_FILE);
4128}
4129
4130#ifdef FEAT_FLOAT
4131/*
4132 * "float2nr({float})" function
4133 */
4134 static void
4135f_float2nr(typval_T *argvars, typval_T *rettv)
4136{
4137 float_T f = 0.0;
4138
4139 if (get_float_arg(argvars, &f) == OK)
4140 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004141 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004142 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004143 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004144 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 else
4146 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 }
4148}
4149
4150/*
4151 * "floor({float})" function
4152 */
4153 static void
4154f_floor(typval_T *argvars, typval_T *rettv)
4155{
4156 float_T f = 0.0;
4157
4158 rettv->v_type = VAR_FLOAT;
4159 if (get_float_arg(argvars, &f) == OK)
4160 rettv->vval.v_float = floor(f);
4161 else
4162 rettv->vval.v_float = 0.0;
4163}
4164
4165/*
4166 * "fmod()" function
4167 */
4168 static void
4169f_fmod(typval_T *argvars, typval_T *rettv)
4170{
4171 float_T fx = 0.0, fy = 0.0;
4172
4173 rettv->v_type = VAR_FLOAT;
4174 if (get_float_arg(argvars, &fx) == OK
4175 && get_float_arg(&argvars[1], &fy) == OK)
4176 rettv->vval.v_float = fmod(fx, fy);
4177 else
4178 rettv->vval.v_float = 0.0;
4179}
4180#endif
4181
4182/*
4183 * "fnameescape({string})" function
4184 */
4185 static void
4186f_fnameescape(typval_T *argvars, typval_T *rettv)
4187{
4188 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004189 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004190 rettv->v_type = VAR_STRING;
4191}
4192
4193/*
4194 * "fnamemodify({fname}, {mods})" function
4195 */
4196 static void
4197f_fnamemodify(typval_T *argvars, typval_T *rettv)
4198{
4199 char_u *fname;
4200 char_u *mods;
4201 int usedlen = 0;
4202 int len;
4203 char_u *fbuf = NULL;
4204 char_u buf[NUMBUFLEN];
4205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004206 fname = tv_get_string_chk(&argvars[0]);
4207 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 if (fname == NULL || mods == NULL)
4209 fname = NULL;
4210 else
4211 {
4212 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004213 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214 }
4215
4216 rettv->v_type = VAR_STRING;
4217 if (fname == NULL)
4218 rettv->vval.v_string = NULL;
4219 else
4220 rettv->vval.v_string = vim_strnsave(fname, len);
4221 vim_free(fbuf);
4222}
4223
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004224/*
4225 * "foldclosed()" function
4226 */
4227 static void
4228foldclosed_both(
4229 typval_T *argvars UNUSED,
4230 typval_T *rettv,
4231 int end UNUSED)
4232{
4233#ifdef FEAT_FOLDING
4234 linenr_T lnum;
4235 linenr_T first, last;
4236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004237 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4239 {
4240 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4241 {
4242 if (end)
4243 rettv->vval.v_number = (varnumber_T)last;
4244 else
4245 rettv->vval.v_number = (varnumber_T)first;
4246 return;
4247 }
4248 }
4249#endif
4250 rettv->vval.v_number = -1;
4251}
4252
4253/*
4254 * "foldclosed()" function
4255 */
4256 static void
4257f_foldclosed(typval_T *argvars, typval_T *rettv)
4258{
4259 foldclosed_both(argvars, rettv, FALSE);
4260}
4261
4262/*
4263 * "foldclosedend()" function
4264 */
4265 static void
4266f_foldclosedend(typval_T *argvars, typval_T *rettv)
4267{
4268 foldclosed_both(argvars, rettv, TRUE);
4269}
4270
4271/*
4272 * "foldlevel()" function
4273 */
4274 static void
4275f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4276{
4277#ifdef FEAT_FOLDING
4278 linenr_T lnum;
4279
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004280 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4282 rettv->vval.v_number = foldLevel(lnum);
4283#endif
4284}
4285
4286/*
4287 * "foldtext()" function
4288 */
4289 static void
4290f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4291{
4292#ifdef FEAT_FOLDING
4293 linenr_T foldstart;
4294 linenr_T foldend;
4295 char_u *dashes;
4296 linenr_T lnum;
4297 char_u *s;
4298 char_u *r;
4299 int len;
4300 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004301 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302#endif
4303
4304 rettv->v_type = VAR_STRING;
4305 rettv->vval.v_string = NULL;
4306#ifdef FEAT_FOLDING
4307 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4308 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4309 dashes = get_vim_var_str(VV_FOLDDASHES);
4310 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4311 && dashes != NULL)
4312 {
4313 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004314 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 if (!linewhite(lnum))
4316 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317
4318 /* Find interesting text in this line. */
4319 s = skipwhite(ml_get(lnum));
4320 /* skip C comment-start */
4321 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4322 {
4323 s = skipwhite(s + 2);
4324 if (*skipwhite(s) == NUL
4325 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4326 {
4327 s = skipwhite(ml_get(lnum + 1));
4328 if (*s == '*')
4329 s = skipwhite(s + 1);
4330 }
4331 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004332 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004333 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004334 r = alloc(STRLEN(txt)
4335 + STRLEN(dashes) // for %s
4336 + 20 // for %3ld
4337 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338 if (r != NULL)
4339 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004340 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 len = (int)STRLEN(r);
4342 STRCAT(r, s);
4343 /* remove 'foldmarker' and 'commentstring' */
4344 foldtext_cleanup(r + len);
4345 rettv->vval.v_string = r;
4346 }
4347 }
4348#endif
4349}
4350
4351/*
4352 * "foldtextresult(lnum)" function
4353 */
4354 static void
4355f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4356{
4357#ifdef FEAT_FOLDING
4358 linenr_T lnum;
4359 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004360 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 foldinfo_T foldinfo;
4362 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004363 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364#endif
4365
4366 rettv->v_type = VAR_STRING;
4367 rettv->vval.v_string = NULL;
4368#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004369 if (entered)
4370 return; /* reject recursive use */
4371 entered = TRUE;
4372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004373 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374 /* treat illegal types and illegal string values for {lnum} the same */
4375 if (lnum < 0)
4376 lnum = 0;
4377 fold_count = foldedCount(curwin, lnum, &foldinfo);
4378 if (fold_count > 0)
4379 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004380 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4381 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 if (text == buf)
4383 text = vim_strsave(text);
4384 rettv->vval.v_string = text;
4385 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004386
4387 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388#endif
4389}
4390
4391/*
4392 * "foreground()" function
4393 */
4394 static void
4395f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4396{
4397#ifdef FEAT_GUI
4398 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004399 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004400 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004401 return;
4402 }
4403#endif
4404#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#endif
4407}
4408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004410common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411{
4412 char_u *s;
4413 char_u *name;
4414 int use_string = FALSE;
4415 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004416 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004417
4418 if (argvars[0].v_type == VAR_FUNC)
4419 {
4420 /* function(MyFunc, [arg], dict) */
4421 s = argvars[0].vval.v_string;
4422 }
4423 else if (argvars[0].v_type == VAR_PARTIAL
4424 && argvars[0].vval.v_partial != NULL)
4425 {
4426 /* function(dict.MyFunc, [arg]) */
4427 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004428 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429 }
4430 else
4431 {
4432 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004433 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434 use_string = TRUE;
4435 }
4436
Bram Moolenaar843b8842016-08-21 14:36:15 +02004437 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004438 {
4439 name = s;
4440 trans_name = trans_function_name(&name, FALSE,
4441 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4442 if (*name != NUL)
4443 s = NULL;
4444 }
4445
Bram Moolenaar843b8842016-08-21 14:36:15 +02004446 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4447 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004448 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 else if (trans_name != NULL && (is_funcref
4451 ? find_func(trans_name) == NULL
4452 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004453 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004454 else
4455 {
4456 int dict_idx = 0;
4457 int arg_idx = 0;
4458 list_T *list = NULL;
4459
4460 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4461 {
4462 char sid_buf[25];
4463 int off = *s == 's' ? 2 : 5;
4464
4465 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4466 * also be called from another script. Using trans_function_name()
4467 * would also work, but some plugins depend on the name being
4468 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004469 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004470 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 if (name != NULL)
4472 {
4473 STRCPY(name, sid_buf);
4474 STRCAT(name, s + off);
4475 }
4476 }
4477 else
4478 name = vim_strsave(s);
4479
4480 if (argvars[1].v_type != VAR_UNKNOWN)
4481 {
4482 if (argvars[2].v_type != VAR_UNKNOWN)
4483 {
4484 /* function(name, [args], dict) */
4485 arg_idx = 1;
4486 dict_idx = 2;
4487 }
4488 else if (argvars[1].v_type == VAR_DICT)
4489 /* function(name, dict) */
4490 dict_idx = 1;
4491 else
4492 /* function(name, [args]) */
4493 arg_idx = 1;
4494 if (dict_idx > 0)
4495 {
4496 if (argvars[dict_idx].v_type != VAR_DICT)
4497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004498 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004500 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 }
4502 if (argvars[dict_idx].vval.v_dict == NULL)
4503 dict_idx = 0;
4504 }
4505 if (arg_idx > 0)
4506 {
4507 if (argvars[arg_idx].v_type != VAR_LIST)
4508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004509 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004511 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512 }
4513 list = argvars[arg_idx].vval.v_list;
4514 if (list == NULL || list->lv_len == 0)
4515 arg_idx = 0;
4516 }
4517 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004518 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004520 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521
4522 /* result is a VAR_PARTIAL */
4523 if (pt == NULL)
4524 vim_free(name);
4525 else
4526 {
4527 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4528 {
4529 listitem_T *li;
4530 int i = 0;
4531 int arg_len = 0;
4532 int lv_len = 0;
4533
4534 if (arg_pt != NULL)
4535 arg_len = arg_pt->pt_argc;
4536 if (list != NULL)
4537 lv_len = list->lv_len;
4538 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004539 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 if (pt->pt_argv == NULL)
4541 {
4542 vim_free(pt);
4543 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004546 for (i = 0; i < arg_len; i++)
4547 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4548 if (lv_len > 0)
4549 for (li = list->lv_first; li != NULL;
4550 li = li->li_next)
4551 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 }
4553
4554 /* For "function(dict.func, [], dict)" and "func" is a partial
4555 * use "dict". That is backwards compatible. */
4556 if (dict_idx > 0)
4557 {
4558 /* The dict is bound explicitly, pt_auto is FALSE. */
4559 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4560 ++pt->pt_dict->dv_refcount;
4561 }
4562 else if (arg_pt != NULL)
4563 {
4564 /* If the dict was bound automatically the result is also
4565 * bound automatically. */
4566 pt->pt_dict = arg_pt->pt_dict;
4567 pt->pt_auto = arg_pt->pt_auto;
4568 if (pt->pt_dict != NULL)
4569 ++pt->pt_dict->dv_refcount;
4570 }
4571
4572 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004573 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4574 {
4575 pt->pt_func = arg_pt->pt_func;
4576 func_ptr_ref(pt->pt_func);
4577 vim_free(name);
4578 }
4579 else if (is_funcref)
4580 {
4581 pt->pt_func = find_func(trans_name);
4582 func_ptr_ref(pt->pt_func);
4583 vim_free(name);
4584 }
4585 else
4586 {
4587 pt->pt_name = name;
4588 func_ref(name);
4589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590 }
4591 rettv->v_type = VAR_PARTIAL;
4592 rettv->vval.v_partial = pt;
4593 }
4594 else
4595 {
4596 /* result is a VAR_FUNC */
4597 rettv->v_type = VAR_FUNC;
4598 rettv->vval.v_string = name;
4599 func_ref(name);
4600 }
4601 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004602theend:
4603 vim_free(trans_name);
4604}
4605
4606/*
4607 * "funcref()" function
4608 */
4609 static void
4610f_funcref(typval_T *argvars, typval_T *rettv)
4611{
4612 common_function(argvars, rettv, TRUE);
4613}
4614
4615/*
4616 * "function()" function
4617 */
4618 static void
4619f_function(typval_T *argvars, typval_T *rettv)
4620{
4621 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622}
4623
4624/*
4625 * "garbagecollect()" function
4626 */
4627 static void
4628f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4629{
4630 /* This is postponed until we are back at the toplevel, because we may be
4631 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4632 want_garbage_collect = TRUE;
4633
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004634 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004635 garbage_collect_at_exit = TRUE;
4636}
4637
4638/*
4639 * "get()" function
4640 */
4641 static void
4642f_get(typval_T *argvars, typval_T *rettv)
4643{
4644 listitem_T *li;
4645 list_T *l;
4646 dictitem_T *di;
4647 dict_T *d;
4648 typval_T *tv = NULL;
4649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004650 if (argvars[0].v_type == VAR_BLOB)
4651 {
4652 int error = FALSE;
4653 int idx = tv_get_number_chk(&argvars[1], &error);
4654
4655 if (!error)
4656 {
4657 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004658 if (idx < 0)
4659 idx = blob_len(argvars[0].vval.v_blob) + idx;
4660 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4661 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004662 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004663 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004664 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004665 tv = rettv;
4666 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004667 }
4668 }
4669 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670 {
4671 if ((l = argvars[0].vval.v_list) != NULL)
4672 {
4673 int error = FALSE;
4674
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004675 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 if (!error && li != NULL)
4677 tv = &li->li_tv;
4678 }
4679 }
4680 else if (argvars[0].v_type == VAR_DICT)
4681 {
4682 if ((d = argvars[0].vval.v_dict) != NULL)
4683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004684 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 if (di != NULL)
4686 tv = &di->di_tv;
4687 }
4688 }
4689 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4690 {
4691 partial_T *pt;
4692 partial_T fref_pt;
4693
4694 if (argvars[0].v_type == VAR_PARTIAL)
4695 pt = argvars[0].vval.v_partial;
4696 else
4697 {
4698 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4699 fref_pt.pt_name = argvars[0].vval.v_string;
4700 pt = &fref_pt;
4701 }
4702
4703 if (pt != NULL)
4704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004705 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004706 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004707
4708 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4709 {
4710 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004711 n = partial_name(pt);
4712 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 rettv->vval.v_string = NULL;
4714 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004715 {
4716 rettv->vval.v_string = vim_strsave(n);
4717 if (rettv->v_type == VAR_FUNC)
4718 func_ref(rettv->vval.v_string);
4719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004720 }
4721 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004722 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004723 else if (STRCMP(what, "args") == 0)
4724 {
4725 rettv->v_type = VAR_LIST;
4726 if (rettv_list_alloc(rettv) == OK)
4727 {
4728 int i;
4729
4730 for (i = 0; i < pt->pt_argc; ++i)
4731 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4732 }
4733 }
4734 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004735 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004736 return;
4737 }
4738 }
4739 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004740 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741
4742 if (tv == NULL)
4743 {
4744 if (argvars[2].v_type != VAR_UNKNOWN)
4745 copy_tv(&argvars[2], rettv);
4746 }
4747 else
4748 copy_tv(tv, rettv);
4749}
4750
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004751/*
4752 * Returns buffer options, variables and other attributes in a dictionary.
4753 */
4754 static dict_T *
4755get_buffer_info(buf_T *buf)
4756{
4757 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004758 tabpage_T *tp;
4759 win_T *wp;
4760 list_T *windows;
4761
4762 dict = dict_alloc();
4763 if (dict == NULL)
4764 return NULL;
4765
Bram Moolenaare0be1672018-07-08 16:50:37 +02004766 dict_add_number(dict, "bufnr", buf->b_fnum);
4767 dict_add_string(dict, "name", buf->b_ffname);
4768 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4769 : buflist_findlnum(buf));
4770 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4771 dict_add_number(dict, "listed", buf->b_p_bl);
4772 dict_add_number(dict, "changed", bufIsChanged(buf));
4773 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4774 dict_add_number(dict, "hidden",
4775 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004776
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004777 /* Get a reference to buffer variables */
4778 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004779
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004780 /* List of windows displaying this buffer */
4781 windows = list_alloc();
4782 if (windows != NULL)
4783 {
4784 FOR_ALL_TAB_WINDOWS(tp, wp)
4785 if (wp->w_buffer == buf)
4786 list_append_number(windows, (varnumber_T)wp->w_id);
4787 dict_add_list(dict, "windows", windows);
4788 }
4789
4790#ifdef FEAT_SIGNS
4791 if (buf->b_signlist != NULL)
4792 {
4793 /* List of signs placed in this buffer */
4794 list_T *signs = list_alloc();
4795 if (signs != NULL)
4796 {
4797 get_buffer_signs(buf, signs);
4798 dict_add_list(dict, "signs", signs);
4799 }
4800 }
4801#endif
4802
4803 return dict;
4804}
4805
4806/*
4807 * "getbufinfo()" function
4808 */
4809 static void
4810f_getbufinfo(typval_T *argvars, typval_T *rettv)
4811{
4812 buf_T *buf = NULL;
4813 buf_T *argbuf = NULL;
4814 dict_T *d;
4815 int filtered = FALSE;
4816 int sel_buflisted = FALSE;
4817 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004818 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004819
4820 if (rettv_list_alloc(rettv) != OK)
4821 return;
4822
4823 /* List of all the buffers or selected buffers */
4824 if (argvars[0].v_type == VAR_DICT)
4825 {
4826 dict_T *sel_d = argvars[0].vval.v_dict;
4827
4828 if (sel_d != NULL)
4829 {
4830 dictitem_T *di;
4831
4832 filtered = TRUE;
4833
4834 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004835 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004836 sel_buflisted = TRUE;
4837
4838 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004839 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004840 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004841
4842 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004843 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004844 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004845 }
4846 }
4847 else if (argvars[0].v_type != VAR_UNKNOWN)
4848 {
4849 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004851 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004852 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004853 --emsg_off;
4854 if (argbuf == NULL)
4855 return;
4856 }
4857
4858 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004859 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004860 {
4861 if (argbuf != NULL && argbuf != buf)
4862 continue;
4863 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004864 || (sel_buflisted && !buf->b_p_bl)
4865 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004866 continue;
4867
4868 d = get_buffer_info(buf);
4869 if (d != NULL)
4870 list_append_dict(rettv->vval.v_list, d);
4871 if (argbuf != NULL)
4872 return;
4873 }
4874}
4875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876/*
4877 * Get line or list of lines from buffer "buf" into "rettv".
4878 * Return a range (from start to end) of lines in rettv from the specified
4879 * buffer.
4880 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4881 */
4882 static void
4883get_buffer_lines(
4884 buf_T *buf,
4885 linenr_T start,
4886 linenr_T end,
4887 int retlist,
4888 typval_T *rettv)
4889{
4890 char_u *p;
4891
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = NULL;
4894 if (retlist && rettv_list_alloc(rettv) == FAIL)
4895 return;
4896
4897 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4898 return;
4899
4900 if (!retlist)
4901 {
4902 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4903 p = ml_get_buf(buf, start, FALSE);
4904 else
4905 p = (char_u *)"";
4906 rettv->vval.v_string = vim_strsave(p);
4907 }
4908 else
4909 {
4910 if (end < start)
4911 return;
4912
4913 if (start < 1)
4914 start = 1;
4915 if (end > buf->b_ml.ml_line_count)
4916 end = buf->b_ml.ml_line_count;
4917 while (start <= end)
4918 if (list_append_string(rettv->vval.v_list,
4919 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4920 break;
4921 }
4922}
4923
4924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925 * "getbufline()" function
4926 */
4927 static void
4928f_getbufline(typval_T *argvars, typval_T *rettv)
4929{
4930 linenr_T lnum;
4931 linenr_T end;
4932 buf_T *buf;
4933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004934 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004936 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 --emsg_off;
4938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 if (argvars[2].v_type == VAR_UNKNOWN)
4941 end = lnum;
4942 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004943 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944
4945 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4946}
4947
4948/*
4949 * "getbufvar()" function
4950 */
4951 static void
4952f_getbufvar(typval_T *argvars, typval_T *rettv)
4953{
4954 buf_T *buf;
4955 buf_T *save_curbuf;
4956 char_u *varname;
4957 dictitem_T *v;
4958 int done = FALSE;
4959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004960 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4961 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004963 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964
4965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = NULL;
4967
4968 if (buf != NULL && varname != NULL)
4969 {
4970 /* set curbuf to be our buf, temporarily */
4971 save_curbuf = curbuf;
4972 curbuf = buf;
4973
Bram Moolenaar30567352016-08-27 21:25:44 +02004974 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004976 if (varname[1] == NUL)
4977 {
4978 /* get all buffer-local options in a dict */
4979 dict_T *opts = get_winbuf_options(TRUE);
4980
4981 if (opts != NULL)
4982 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004983 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004984 done = TRUE;
4985 }
4986 }
4987 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4988 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989 done = TRUE;
4990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 else
4992 {
4993 /* Look up the variable. */
4994 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4995 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4996 'b', varname, FALSE);
4997 if (v != NULL)
4998 {
4999 copy_tv(&v->di_tv, rettv);
5000 done = TRUE;
5001 }
5002 }
5003
5004 /* restore previous notion of curbuf */
5005 curbuf = save_curbuf;
5006 }
5007
5008 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5009 /* use the default value */
5010 copy_tv(&argvars[2], rettv);
5011
5012 --emsg_off;
5013}
5014
5015/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005016 * "getchangelist()" function
5017 */
5018 static void
5019f_getchangelist(typval_T *argvars, typval_T *rettv)
5020{
5021#ifdef FEAT_JUMPLIST
5022 buf_T *buf;
5023 int i;
5024 list_T *l;
5025 dict_T *d;
5026#endif
5027
5028 if (rettv_list_alloc(rettv) != OK)
5029 return;
5030
5031#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005032 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005033 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005034 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005035 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005036 if (buf == NULL)
5037 return;
5038
5039 l = list_alloc();
5040 if (l == NULL)
5041 return;
5042
5043 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5044 return;
5045 /*
5046 * The current window change list index tracks only the position in the
5047 * current buffer change list. For other buffers, use the change list
5048 * length as the current index.
5049 */
5050 list_append_number(rettv->vval.v_list,
5051 (varnumber_T)((buf == curwin->w_buffer)
5052 ? curwin->w_changelistidx : buf->b_changelistlen));
5053
5054 for (i = 0; i < buf->b_changelistlen; ++i)
5055 {
5056 if (buf->b_changelist[i].lnum == 0)
5057 continue;
5058 if ((d = dict_alloc()) == NULL)
5059 return;
5060 if (list_append_dict(l, d) == FAIL)
5061 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005062 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5063 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005064 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005065 }
5066#endif
5067}
5068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069 * "getchar()" function
5070 */
5071 static void
5072f_getchar(typval_T *argvars, typval_T *rettv)
5073{
5074 varnumber_T n;
5075 int error = FALSE;
5076
Bram Moolenaar84d93902018-09-11 20:10:20 +02005077#ifdef MESSAGE_QUEUE
5078 // vpeekc() used to check for messages, but that caused problems, invoking
5079 // a callback where it was not expected. Some plugins use getchar(1) in a
5080 // loop to await a message, therefore make sure we check for messages here.
5081 parse_queued_messages();
5082#endif
5083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 /* Position the cursor. Needed after a message that ends in a space. */
5085 windgoto(msg_row, msg_col);
5086
5087 ++no_mapping;
5088 ++allow_keys;
5089 for (;;)
5090 {
5091 if (argvars[0].v_type == VAR_UNKNOWN)
5092 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005093 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005094 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 /* getchar(1): only check if char avail */
5096 n = vpeekc_any();
5097 else if (error || vpeekc_any() == NUL)
5098 /* illegal argument or getchar(0) and no char avail: return zero */
5099 n = 0;
5100 else
5101 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005102 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103
5104 if (n == K_IGNORE)
5105 continue;
5106 break;
5107 }
5108 --no_mapping;
5109 --allow_keys;
5110
5111 set_vim_var_nr(VV_MOUSE_WIN, 0);
5112 set_vim_var_nr(VV_MOUSE_WINID, 0);
5113 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5114 set_vim_var_nr(VV_MOUSE_COL, 0);
5115
5116 rettv->vval.v_number = n;
5117 if (IS_SPECIAL(n) || mod_mask != 0)
5118 {
5119 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5120 int i = 0;
5121
5122 /* Turn a special key into three bytes, plus modifier. */
5123 if (mod_mask != 0)
5124 {
5125 temp[i++] = K_SPECIAL;
5126 temp[i++] = KS_MODIFIER;
5127 temp[i++] = mod_mask;
5128 }
5129 if (IS_SPECIAL(n))
5130 {
5131 temp[i++] = K_SPECIAL;
5132 temp[i++] = K_SECOND(n);
5133 temp[i++] = K_THIRD(n);
5134 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 else if (has_mbyte)
5136 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 else
5138 temp[i++] = n;
5139 temp[i++] = NUL;
5140 rettv->v_type = VAR_STRING;
5141 rettv->vval.v_string = vim_strsave(temp);
5142
5143#ifdef FEAT_MOUSE
5144 if (is_mouse_key(n))
5145 {
5146 int row = mouse_row;
5147 int col = mouse_col;
5148 win_T *win;
5149 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 int winnr = 1;
5152
5153 if (row >= 0 && col >= 0)
5154 {
5155 /* Find the window at the mouse coordinates and compute the
5156 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005157 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005158 if (win == NULL)
5159 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005161# ifdef FEAT_TEXT_PROP
5162 if (bt_popup(win->w_buffer))
5163 winnr = 0;
5164 else
5165# endif
5166 for (wp = firstwin; wp != win && wp != NULL;
5167 wp = wp->w_next)
5168 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005169 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5170 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5171 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5172 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5173 }
5174 }
5175#endif
5176 }
5177}
5178
5179/*
5180 * "getcharmod()" function
5181 */
5182 static void
5183f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5184{
5185 rettv->vval.v_number = mod_mask;
5186}
5187
5188/*
5189 * "getcharsearch()" function
5190 */
5191 static void
5192f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5193{
5194 if (rettv_dict_alloc(rettv) != FAIL)
5195 {
5196 dict_T *dict = rettv->vval.v_dict;
5197
Bram Moolenaare0be1672018-07-08 16:50:37 +02005198 dict_add_string(dict, "char", last_csearch());
5199 dict_add_number(dict, "forward", last_csearch_forward());
5200 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005201 }
5202}
5203
5204/*
5205 * "getcmdline()" function
5206 */
5207 static void
5208f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5209{
5210 rettv->v_type = VAR_STRING;
5211 rettv->vval.v_string = get_cmdline_str();
5212}
5213
5214/*
5215 * "getcmdpos()" function
5216 */
5217 static void
5218f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5219{
5220 rettv->vval.v_number = get_cmdline_pos() + 1;
5221}
5222
5223/*
5224 * "getcmdtype()" function
5225 */
5226 static void
5227f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5228{
5229 rettv->v_type = VAR_STRING;
5230 rettv->vval.v_string = alloc(2);
5231 if (rettv->vval.v_string != NULL)
5232 {
5233 rettv->vval.v_string[0] = get_cmdline_type();
5234 rettv->vval.v_string[1] = NUL;
5235 }
5236}
5237
5238/*
5239 * "getcmdwintype()" function
5240 */
5241 static void
5242f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5243{
5244 rettv->v_type = VAR_STRING;
5245 rettv->vval.v_string = NULL;
5246#ifdef FEAT_CMDWIN
5247 rettv->vval.v_string = alloc(2);
5248 if (rettv->vval.v_string != NULL)
5249 {
5250 rettv->vval.v_string[0] = cmdwin_type;
5251 rettv->vval.v_string[1] = NUL;
5252 }
5253#endif
5254}
5255
5256#if defined(FEAT_CMDL_COMPL)
5257/*
5258 * "getcompletion()" function
5259 */
5260 static void
5261f_getcompletion(typval_T *argvars, typval_T *rettv)
5262{
5263 char_u *pat;
5264 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005265 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005266 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5267 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005268
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005269 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005270 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005271
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272 if (p_wic)
5273 options |= WILD_ICASE;
5274
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005275 /* For filtered results, 'wildignore' is used */
5276 if (!filtered)
5277 options |= WILD_KEEP_ALL;
5278
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005279 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005280 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005282 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 if (xpc.xp_context == EXPAND_NOTHING)
5284 {
5285 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005286 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005288 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289 return;
5290 }
5291
5292# if defined(FEAT_MENU)
5293 if (xpc.xp_context == EXPAND_MENUS)
5294 {
5295 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5296 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5297 }
5298# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005299#ifdef FEAT_CSCOPE
5300 if (xpc.xp_context == EXPAND_CSCOPE)
5301 {
5302 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5303 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5304 }
5305#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005306#ifdef FEAT_SIGNS
5307 if (xpc.xp_context == EXPAND_SIGN)
5308 {
5309 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5310 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5311 }
5312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313
5314 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5315 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5316 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005317 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318
5319 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5320
5321 for (i = 0; i < xpc.xp_numfiles; i++)
5322 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5323 }
5324 vim_free(pat);
5325 ExpandCleanup(&xpc);
5326}
5327#endif
5328
5329/*
5330 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005331 *
5332 * Return the current working directory of a window in a tab page.
5333 * First optional argument 'winnr' is the window number or -1 and the second
5334 * optional argument 'tabnr' is the tab page number.
5335 *
5336 * If no arguments are supplied, then return the directory of the current
5337 * window.
5338 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5339 * the specified window.
5340 * If 'winnr' is 0 then return the directory of the current window.
5341 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5342 * directory of the specified tab page. Otherwise return the directory of the
5343 * specified window in the specified tab page.
5344 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345 */
5346 static void
5347f_getcwd(typval_T *argvars, typval_T *rettv)
5348{
5349 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005350 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005352 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353
5354 rettv->v_type = VAR_STRING;
5355 rettv->vval.v_string = NULL;
5356
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005357 if (argvars[0].v_type == VAR_NUMBER
5358 && argvars[0].vval.v_number == -1
5359 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005360 global = TRUE;
5361 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005362 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005363
5364 if (wp != NULL && wp->w_localdir != NULL)
5365 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005366 else if (tp != NULL && tp->tp_localdir != NULL)
5367 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5368 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005369 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005370 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005371 rettv->vval.v_string = vim_strsave(globaldir);
5372 else
5373 {
5374 cwd = alloc(MAXPATHL);
5375 if (cwd != NULL)
5376 {
5377 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5378 rettv->vval.v_string = vim_strsave(cwd);
5379 vim_free(cwd);
5380 }
5381 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005382 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005383#ifdef BACKSLASH_IN_FILENAME
5384 if (rettv->vval.v_string != NULL)
5385 slash_adjust(rettv->vval.v_string);
5386#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005387}
5388
5389/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005390 * "getenv()" function
5391 */
5392 static void
5393f_getenv(typval_T *argvars, typval_T *rettv)
5394{
5395 int mustfree = FALSE;
5396 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5397
5398 if (p == NULL)
5399 {
5400 rettv->v_type = VAR_SPECIAL;
5401 rettv->vval.v_number = VVAL_NULL;
5402 return;
5403 }
5404 if (!mustfree)
5405 p = vim_strsave(p);
5406 rettv->vval.v_string = p;
5407 rettv->v_type = VAR_STRING;
5408}
5409
5410/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005411 * "getfontname()" function
5412 */
5413 static void
5414f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5415{
5416 rettv->v_type = VAR_STRING;
5417 rettv->vval.v_string = NULL;
5418#ifdef FEAT_GUI
5419 if (gui.in_use)
5420 {
5421 GuiFont font;
5422 char_u *name = NULL;
5423
5424 if (argvars[0].v_type == VAR_UNKNOWN)
5425 {
5426 /* Get the "Normal" font. Either the name saved by
5427 * hl_set_font_name() or from the font ID. */
5428 font = gui.norm_font;
5429 name = hl_get_font_name();
5430 }
5431 else
5432 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005433 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005434 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5435 return;
5436 font = gui_mch_get_font(name, FALSE);
5437 if (font == NOFONT)
5438 return; /* Invalid font name, return empty string. */
5439 }
5440 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5441 if (argvars[0].v_type != VAR_UNKNOWN)
5442 gui_mch_free_font(font);
5443 }
5444#endif
5445}
5446
5447/*
5448 * "getfperm({fname})" function
5449 */
5450 static void
5451f_getfperm(typval_T *argvars, typval_T *rettv)
5452{
5453 char_u *fname;
5454 stat_T st;
5455 char_u *perm = NULL;
5456 char_u flags[] = "rwx";
5457 int i;
5458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005459 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460
5461 rettv->v_type = VAR_STRING;
5462 if (mch_stat((char *)fname, &st) >= 0)
5463 {
5464 perm = vim_strsave((char_u *)"---------");
5465 if (perm != NULL)
5466 {
5467 for (i = 0; i < 9; i++)
5468 {
5469 if (st.st_mode & (1 << (8 - i)))
5470 perm[i] = flags[i % 3];
5471 }
5472 }
5473 }
5474 rettv->vval.v_string = perm;
5475}
5476
5477/*
5478 * "getfsize({fname})" function
5479 */
5480 static void
5481f_getfsize(typval_T *argvars, typval_T *rettv)
5482{
5483 char_u *fname;
5484 stat_T st;
5485
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005486 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487
5488 rettv->v_type = VAR_NUMBER;
5489
5490 if (mch_stat((char *)fname, &st) >= 0)
5491 {
5492 if (mch_isdir(fname))
5493 rettv->vval.v_number = 0;
5494 else
5495 {
5496 rettv->vval.v_number = (varnumber_T)st.st_size;
5497
5498 /* non-perfect check for overflow */
5499 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5500 rettv->vval.v_number = -2;
5501 }
5502 }
5503 else
5504 rettv->vval.v_number = -1;
5505}
5506
5507/*
5508 * "getftime({fname})" function
5509 */
5510 static void
5511f_getftime(typval_T *argvars, typval_T *rettv)
5512{
5513 char_u *fname;
5514 stat_T st;
5515
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005516 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005517
5518 if (mch_stat((char *)fname, &st) >= 0)
5519 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5520 else
5521 rettv->vval.v_number = -1;
5522}
5523
5524/*
5525 * "getftype({fname})" function
5526 */
5527 static void
5528f_getftype(typval_T *argvars, typval_T *rettv)
5529{
5530 char_u *fname;
5531 stat_T st;
5532 char_u *type = NULL;
5533 char *t;
5534
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005535 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005536
5537 rettv->v_type = VAR_STRING;
5538 if (mch_lstat((char *)fname, &st) >= 0)
5539 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 if (S_ISREG(st.st_mode))
5541 t = "file";
5542 else if (S_ISDIR(st.st_mode))
5543 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 else if (S_ISLNK(st.st_mode))
5545 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 else if (S_ISBLK(st.st_mode))
5547 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 else if (S_ISCHR(st.st_mode))
5549 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 else if (S_ISFIFO(st.st_mode))
5551 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005553 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 else
5555 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 type = vim_strsave((char_u *)t);
5557 }
5558 rettv->vval.v_string = type;
5559}
5560
5561/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005562 * "getjumplist()" function
5563 */
5564 static void
5565f_getjumplist(typval_T *argvars, typval_T *rettv)
5566{
5567#ifdef FEAT_JUMPLIST
5568 win_T *wp;
5569 int i;
5570 list_T *l;
5571 dict_T *d;
5572#endif
5573
5574 if (rettv_list_alloc(rettv) != OK)
5575 return;
5576
5577#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005578 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005579 if (wp == NULL)
5580 return;
5581
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005582 cleanup_jumplist(wp, TRUE);
5583
Bram Moolenaar4f505882018-02-10 21:06:32 +01005584 l = list_alloc();
5585 if (l == NULL)
5586 return;
5587
5588 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5589 return;
5590 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5591
5592 for (i = 0; i < wp->w_jumplistlen; ++i)
5593 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005594 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5595 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005596 if ((d = dict_alloc()) == NULL)
5597 return;
5598 if (list_append_dict(l, d) == FAIL)
5599 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005600 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5601 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005602 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005603 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005604 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005605 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005606 }
5607#endif
5608}
5609
5610/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005611 * "getline(lnum, [end])" function
5612 */
5613 static void
5614f_getline(typval_T *argvars, typval_T *rettv)
5615{
5616 linenr_T lnum;
5617 linenr_T end;
5618 int retlist;
5619
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005620 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621 if (argvars[1].v_type == VAR_UNKNOWN)
5622 {
5623 end = 0;
5624 retlist = FALSE;
5625 }
5626 else
5627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005628 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 retlist = TRUE;
5630 }
5631
5632 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5633}
5634
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005635#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005636 static void
5637get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5638{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005639 if (what_arg->v_type == VAR_UNKNOWN)
5640 {
5641 if (rettv_list_alloc(rettv) == OK)
5642 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005643 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005644 }
5645 else
5646 {
5647 if (rettv_dict_alloc(rettv) == OK)
5648 if (is_qf || (wp != NULL))
5649 {
5650 if (what_arg->v_type == VAR_DICT)
5651 {
5652 dict_T *d = what_arg->vval.v_dict;
5653
5654 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005655 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005656 }
5657 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005658 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005659 }
5660 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005661}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005662#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005663
5664/*
5665 * "getloclist()" function
5666 */
5667 static void
5668f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5669{
5670#ifdef FEAT_QUICKFIX
5671 win_T *wp;
5672
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005673 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005674 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5675#endif
5676}
5677
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678/*
5679 * "getmatches()" function
5680 */
5681 static void
5682f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5683{
5684#ifdef FEAT_SEARCH_EXTRA
5685 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005686 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005687 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005688 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689
Bram Moolenaaraff74912019-03-30 18:11:49 +01005690 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5691 return;
5692
5693 cur = win->w_match_head;
5694 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005696 dict = dict_alloc();
5697 if (dict == NULL)
5698 return;
5699 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005700 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005701 /* match added with matchaddpos() */
5702 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005703 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005704 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005705 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005706 list_T *l;
5707
5708 llpos = &cur->pos.pos[i];
5709 if (llpos->lnum == 0)
5710 break;
5711 l = list_alloc();
5712 if (l == NULL)
5713 break;
5714 list_append_number(l, (varnumber_T)llpos->lnum);
5715 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005717 list_append_number(l, (varnumber_T)llpos->col);
5718 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005719 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005720 sprintf(buf, "pos%d", i + 1);
5721 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005724 else
5725 {
5726 dict_add_string(dict, "pattern", cur->pattern);
5727 }
5728 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5729 dict_add_number(dict, "priority", (long)cur->priority);
5730 dict_add_number(dict, "id", (long)cur->id);
5731# if defined(FEAT_CONCEAL)
5732 if (cur->conceal_char)
5733 {
5734 char_u buf[MB_MAXBYTES + 1];
5735
5736 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5737 dict_add_string(dict, "conceal", (char_u *)&buf);
5738 }
5739# endif
5740 list_append_dict(rettv->vval.v_list, dict);
5741 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005742 }
5743#endif
5744}
5745
5746/*
5747 * "getpid()" function
5748 */
5749 static void
5750f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5751{
5752 rettv->vval.v_number = mch_get_pid();
5753}
5754
5755 static void
5756getpos_both(
5757 typval_T *argvars,
5758 typval_T *rettv,
5759 int getcurpos)
5760{
5761 pos_T *fp;
5762 list_T *l;
5763 int fnum = -1;
5764
5765 if (rettv_list_alloc(rettv) == OK)
5766 {
5767 l = rettv->vval.v_list;
5768 if (getcurpos)
5769 fp = &curwin->w_cursor;
5770 else
5771 fp = var2fpos(&argvars[0], TRUE, &fnum);
5772 if (fnum != -1)
5773 list_append_number(l, (varnumber_T)fnum);
5774 else
5775 list_append_number(l, (varnumber_T)0);
5776 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5777 : (varnumber_T)0);
5778 list_append_number(l, (fp != NULL)
5779 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5780 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005781 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 (varnumber_T)0);
5783 if (getcurpos)
5784 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005785 int save_set_curswant = curwin->w_set_curswant;
5786 colnr_T save_curswant = curwin->w_curswant;
5787 colnr_T save_virtcol = curwin->w_virtcol;
5788
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005789 update_curswant();
5790 list_append_number(l, curwin->w_curswant == MAXCOL ?
5791 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005792
5793 // Do not change "curswant", as it is unexpected that a get
5794 // function has a side effect.
5795 if (save_set_curswant)
5796 {
5797 curwin->w_set_curswant = save_set_curswant;
5798 curwin->w_curswant = save_curswant;
5799 curwin->w_virtcol = save_virtcol;
5800 curwin->w_valid &= ~VALID_VIRTCOL;
5801 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802 }
5803 }
5804 else
5805 rettv->vval.v_number = FALSE;
5806}
5807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808/*
5809 * "getcurpos()" function
5810 */
5811 static void
5812f_getcurpos(typval_T *argvars, typval_T *rettv)
5813{
5814 getpos_both(argvars, rettv, TRUE);
5815}
5816
5817/*
5818 * "getpos(string)" function
5819 */
5820 static void
5821f_getpos(typval_T *argvars, typval_T *rettv)
5822{
5823 getpos_both(argvars, rettv, FALSE);
5824}
5825
5826/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005827 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828 */
5829 static void
5830f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5831{
5832#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005833 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834#endif
5835}
5836
5837/*
5838 * "getreg()" function
5839 */
5840 static void
5841f_getreg(typval_T *argvars, typval_T *rettv)
5842{
5843 char_u *strregname;
5844 int regname;
5845 int arg2 = FALSE;
5846 int return_list = FALSE;
5847 int error = FALSE;
5848
5849 if (argvars[0].v_type != VAR_UNKNOWN)
5850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005851 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 error = strregname == NULL;
5853 if (argvars[1].v_type != VAR_UNKNOWN)
5854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 }
5859 }
5860 else
5861 strregname = get_vim_var_str(VV_REG);
5862
5863 if (error)
5864 return;
5865
5866 regname = (strregname == NULL ? '"' : *strregname);
5867 if (regname == 0)
5868 regname = '"';
5869
5870 if (return_list)
5871 {
5872 rettv->v_type = VAR_LIST;
5873 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5874 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5875 if (rettv->vval.v_list == NULL)
5876 (void)rettv_list_alloc(rettv);
5877 else
5878 ++rettv->vval.v_list->lv_refcount;
5879 }
5880 else
5881 {
5882 rettv->v_type = VAR_STRING;
5883 rettv->vval.v_string = get_reg_contents(regname,
5884 arg2 ? GREG_EXPR_SRC : 0);
5885 }
5886}
5887
5888/*
5889 * "getregtype()" function
5890 */
5891 static void
5892f_getregtype(typval_T *argvars, typval_T *rettv)
5893{
5894 char_u *strregname;
5895 int regname;
5896 char_u buf[NUMBUFLEN + 2];
5897 long reglen = 0;
5898
5899 if (argvars[0].v_type != VAR_UNKNOWN)
5900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005901 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005902 if (strregname == NULL) /* type error; errmsg already given */
5903 {
5904 rettv->v_type = VAR_STRING;
5905 rettv->vval.v_string = NULL;
5906 return;
5907 }
5908 }
5909 else
5910 /* Default to v:register */
5911 strregname = get_vim_var_str(VV_REG);
5912
5913 regname = (strregname == NULL ? '"' : *strregname);
5914 if (regname == 0)
5915 regname = '"';
5916
5917 buf[0] = NUL;
5918 buf[1] = NUL;
5919 switch (get_reg_type(regname, &reglen))
5920 {
5921 case MLINE: buf[0] = 'V'; break;
5922 case MCHAR: buf[0] = 'v'; break;
5923 case MBLOCK:
5924 buf[0] = Ctrl_V;
5925 sprintf((char *)buf + 1, "%ld", reglen + 1);
5926 break;
5927 }
5928 rettv->v_type = VAR_STRING;
5929 rettv->vval.v_string = vim_strsave(buf);
5930}
5931
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005932/*
5933 * Returns information (variables, options, etc.) about a tab page
5934 * as a dictionary.
5935 */
5936 static dict_T *
5937get_tabpage_info(tabpage_T *tp, int tp_idx)
5938{
5939 win_T *wp;
5940 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005941 list_T *l;
5942
5943 dict = dict_alloc();
5944 if (dict == NULL)
5945 return NULL;
5946
Bram Moolenaare0be1672018-07-08 16:50:37 +02005947 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005948
5949 l = list_alloc();
5950 if (l != NULL)
5951 {
5952 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5953 wp; wp = wp->w_next)
5954 list_append_number(l, (varnumber_T)wp->w_id);
5955 dict_add_list(dict, "windows", l);
5956 }
5957
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005958 /* Make a reference to tabpage variables */
5959 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005960
5961 return dict;
5962}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005963
5964/*
5965 * "gettabinfo()" function
5966 */
5967 static void
5968f_gettabinfo(typval_T *argvars, typval_T *rettv)
5969{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005970 tabpage_T *tp, *tparg = NULL;
5971 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005972 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005973
5974 if (rettv_list_alloc(rettv) != OK)
5975 return;
5976
5977 if (argvars[0].v_type != VAR_UNKNOWN)
5978 {
5979 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005980 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005981 if (tparg == NULL)
5982 return;
5983 }
5984
5985 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005986 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005987 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005988 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005989 if (tparg != NULL && tp != tparg)
5990 continue;
5991 d = get_tabpage_info(tp, tpnr);
5992 if (d != NULL)
5993 list_append_dict(rettv->vval.v_list, d);
5994 if (tparg != NULL)
5995 return;
5996 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005997}
5998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005999/*
6000 * "gettabvar()" function
6001 */
6002 static void
6003f_gettabvar(typval_T *argvars, typval_T *rettv)
6004{
6005 win_T *oldcurwin;
6006 tabpage_T *tp, *oldtabpage;
6007 dictitem_T *v;
6008 char_u *varname;
6009 int done = FALSE;
6010
6011 rettv->v_type = VAR_STRING;
6012 rettv->vval.v_string = NULL;
6013
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006014 varname = tv_get_string_chk(&argvars[1]);
6015 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006016 if (tp != NULL && varname != NULL)
6017 {
6018 /* Set tp to be our tabpage, temporarily. Also set the window to the
6019 * first window in the tabpage, otherwise the window is not valid. */
6020 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006021 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6022 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006023 {
6024 /* look up the variable */
6025 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6026 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6027 if (v != NULL)
6028 {
6029 copy_tv(&v->di_tv, rettv);
6030 done = TRUE;
6031 }
6032 }
6033
6034 /* restore previous notion of curwin */
6035 restore_win(oldcurwin, oldtabpage, TRUE);
6036 }
6037
6038 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6039 copy_tv(&argvars[2], rettv);
6040}
6041
6042/*
6043 * "gettabwinvar()" function
6044 */
6045 static void
6046f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6047{
6048 getwinvar(argvars, rettv, 1);
6049}
6050
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006051/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006052 * "gettagstack()" function
6053 */
6054 static void
6055f_gettagstack(typval_T *argvars, typval_T *rettv)
6056{
6057 win_T *wp = curwin; // default is current window
6058
6059 if (rettv_dict_alloc(rettv) != OK)
6060 return;
6061
6062 if (argvars[0].v_type != VAR_UNKNOWN)
6063 {
6064 wp = find_win_by_nr_or_id(&argvars[0]);
6065 if (wp == NULL)
6066 return;
6067 }
6068
6069 get_tagstack(wp, rettv->vval.v_dict);
6070}
6071
6072/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006073 * Returns information about a window as a dictionary.
6074 */
6075 static dict_T *
6076get_win_info(win_T *wp, short tpnr, short winnr)
6077{
6078 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006079
6080 dict = dict_alloc();
6081 if (dict == NULL)
6082 return NULL;
6083
Bram Moolenaare0be1672018-07-08 16:50:37 +02006084 dict_add_number(dict, "tabnr", tpnr);
6085 dict_add_number(dict, "winnr", winnr);
6086 dict_add_number(dict, "winid", wp->w_id);
6087 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006088 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006089 dict_add_number(dict, "topline", wp->w_topline);
6090 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006091#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006092 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006093#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006094 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006095 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006096 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006097
Bram Moolenaar69905d12017-08-13 18:14:47 +02006098#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006099 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006100#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006101#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006102 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6103 dict_add_number(dict, "loclist",
6104 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006105#endif
6106
Bram Moolenaar30567352016-08-27 21:25:44 +02006107 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006108 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006109
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006110 return dict;
6111}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006112
6113/*
6114 * "getwininfo()" function
6115 */
6116 static void
6117f_getwininfo(typval_T *argvars, typval_T *rettv)
6118{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006119 tabpage_T *tp;
6120 win_T *wp = NULL, *wparg = NULL;
6121 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006122 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006123
6124 if (rettv_list_alloc(rettv) != OK)
6125 return;
6126
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006127 if (argvars[0].v_type != VAR_UNKNOWN)
6128 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006129 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006130 if (wparg == NULL)
6131 return;
6132 }
6133
6134 /* Collect information about either all the windows across all the tab
6135 * pages or one particular window.
6136 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006137 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006138 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006139 tabnr++;
6140 winnr = 0;
6141 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006142 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006143 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006144 if (wparg != NULL && wp != wparg)
6145 continue;
6146 d = get_win_info(wp, tabnr, winnr);
6147 if (d != NULL)
6148 list_append_dict(rettv->vval.v_list, d);
6149 if (wparg != NULL)
6150 /* found information about a specific window */
6151 return;
6152 }
6153 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006154}
6155
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006156/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006157 * "win_execute()" function
6158 */
6159 static void
6160f_win_execute(typval_T *argvars, typval_T *rettv)
6161{
6162 int id = (int)tv_get_number(argvars);
6163 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006164 win_T *save_curwin;
6165 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006166
6167 if (wp != NULL)
6168 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006169 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6170 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006171 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006172 check_cursor();
6173 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006174 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006175 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006176 }
6177}
6178
6179/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006180 * "win_findbuf()" function
6181 */
6182 static void
6183f_win_findbuf(typval_T *argvars, typval_T *rettv)
6184{
6185 if (rettv_list_alloc(rettv) != FAIL)
6186 win_findbuf(argvars, rettv->vval.v_list);
6187}
6188
6189/*
6190 * "win_getid()" function
6191 */
6192 static void
6193f_win_getid(typval_T *argvars, typval_T *rettv)
6194{
6195 rettv->vval.v_number = win_getid(argvars);
6196}
6197
6198/*
6199 * "win_gotoid()" function
6200 */
6201 static void
6202f_win_gotoid(typval_T *argvars, typval_T *rettv)
6203{
6204 rettv->vval.v_number = win_gotoid(argvars);
6205}
6206
6207/*
6208 * "win_id2tabwin()" function
6209 */
6210 static void
6211f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6212{
6213 if (rettv_list_alloc(rettv) != FAIL)
6214 win_id2tabwin(argvars, rettv->vval.v_list);
6215}
6216
6217/*
6218 * "win_id2win()" function
6219 */
6220 static void
6221f_win_id2win(typval_T *argvars, typval_T *rettv)
6222{
6223 rettv->vval.v_number = win_id2win(argvars);
6224}
6225
6226/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006227 * "win_screenpos()" function
6228 */
6229 static void
6230f_win_screenpos(typval_T *argvars, typval_T *rettv)
6231{
6232 win_T *wp;
6233
6234 if (rettv_list_alloc(rettv) == FAIL)
6235 return;
6236
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006237 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006238 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6239 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6240}
6241
6242/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006243 * "getwinpos({timeout})" function
6244 */
6245 static void
6246f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6247{
6248 int x = -1;
6249 int y = -1;
6250
6251 if (rettv_list_alloc(rettv) == FAIL)
6252 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006253#if defined(FEAT_GUI) \
6254 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6255 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006256 {
6257 varnumber_T timeout = 100;
6258
6259 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006260 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006261
6262 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006263 }
6264#endif
6265 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6266 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6267}
6268
6269
6270/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006271 * "getwinposx()" function
6272 */
6273 static void
6274f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6275{
6276 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006277#if defined(FEAT_GUI) \
6278 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6279 || defined(MSWIN)
6280
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006281 {
6282 int x, y;
6283
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006284 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006285 rettv->vval.v_number = x;
6286 }
6287#endif
6288}
6289
6290/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 * "getwinposy()" function
6292 */
6293 static void
6294f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6295{
6296 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006297#if defined(FEAT_GUI) \
6298 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6299 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006300 {
6301 int x, y;
6302
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006303 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006304 rettv->vval.v_number = y;
6305 }
6306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307}
6308
6309/*
6310 * "getwinvar()" function
6311 */
6312 static void
6313f_getwinvar(typval_T *argvars, typval_T *rettv)
6314{
6315 getwinvar(argvars, rettv, 0);
6316}
6317
6318/*
6319 * "glob()" function
6320 */
6321 static void
6322f_glob(typval_T *argvars, typval_T *rettv)
6323{
6324 int options = WILD_SILENT|WILD_USE_NL;
6325 expand_T xpc;
6326 int error = FALSE;
6327
6328 /* When the optional second argument is non-zero, don't remove matches
6329 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6330 rettv->v_type = VAR_STRING;
6331 if (argvars[1].v_type != VAR_UNKNOWN)
6332 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 options |= WILD_KEEP_ALL;
6335 if (argvars[2].v_type != VAR_UNKNOWN)
6336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006337 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006338 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006339 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006340 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341 options |= WILD_ALLLINKS;
6342 }
6343 }
6344 if (!error)
6345 {
6346 ExpandInit(&xpc);
6347 xpc.xp_context = EXPAND_FILES;
6348 if (p_wic)
6349 options += WILD_ICASE;
6350 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006351 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352 NULL, options, WILD_ALL);
6353 else if (rettv_list_alloc(rettv) != FAIL)
6354 {
6355 int i;
6356
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006357 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 NULL, options, WILD_ALL_KEEP);
6359 for (i = 0; i < xpc.xp_numfiles; i++)
6360 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6361
6362 ExpandCleanup(&xpc);
6363 }
6364 }
6365 else
6366 rettv->vval.v_string = NULL;
6367}
6368
6369/*
6370 * "globpath()" function
6371 */
6372 static void
6373f_globpath(typval_T *argvars, typval_T *rettv)
6374{
6375 int flags = 0;
6376 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006377 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006378 int error = FALSE;
6379 garray_T ga;
6380 int i;
6381
6382 /* When the optional second argument is non-zero, don't remove matches
6383 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6384 rettv->v_type = VAR_STRING;
6385 if (argvars[2].v_type != VAR_UNKNOWN)
6386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006387 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388 flags |= WILD_KEEP_ALL;
6389 if (argvars[3].v_type != VAR_UNKNOWN)
6390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006391 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006392 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006393 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006394 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 flags |= WILD_ALLLINKS;
6396 }
6397 }
6398 if (file != NULL && !error)
6399 {
6400 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006401 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 if (rettv->v_type == VAR_STRING)
6403 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6404 else if (rettv_list_alloc(rettv) != FAIL)
6405 for (i = 0; i < ga.ga_len; ++i)
6406 list_append_string(rettv->vval.v_list,
6407 ((char_u **)(ga.ga_data))[i], -1);
6408 ga_clear_strings(&ga);
6409 }
6410 else
6411 rettv->vval.v_string = NULL;
6412}
6413
6414/*
6415 * "glob2regpat()" function
6416 */
6417 static void
6418f_glob2regpat(typval_T *argvars, typval_T *rettv)
6419{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006420 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421
6422 rettv->v_type = VAR_STRING;
6423 rettv->vval.v_string = (pat == NULL)
6424 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6425}
6426
6427/* for VIM_VERSION_ defines */
6428#include "version.h"
6429
6430/*
6431 * "has()" function
6432 */
6433 static void
6434f_has(typval_T *argvars, typval_T *rettv)
6435{
6436 int i;
6437 char_u *name;
6438 int n = FALSE;
6439 static char *(has_list[]) =
6440 {
6441#ifdef AMIGA
6442 "amiga",
6443# ifdef FEAT_ARP
6444 "arp",
6445# endif
6446#endif
6447#ifdef __BEOS__
6448 "beos",
6449#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006450#if defined(BSD) && !defined(MACOS_X)
6451 "bsd",
6452#endif
6453#ifdef hpux
6454 "hpux",
6455#endif
6456#ifdef __linux__
6457 "linux",
6458#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006459#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006460 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6461 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006462# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006463 "macunix", /* Mac OS X, with the darwin feature */
6464 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006465# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466#endif
6467#ifdef __QNX__
6468 "qnx",
6469#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006470#ifdef SUN_SYSTEM
6471 "sun",
6472#else
6473 "moon",
6474#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006475#ifdef UNIX
6476 "unix",
6477#endif
6478#ifdef VMS
6479 "vms",
6480#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006481#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 "win32",
6483#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006484#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485 "win32unix",
6486#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006487#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "win64",
6489#endif
6490#ifdef EBCDIC
6491 "ebcdic",
6492#endif
6493#ifndef CASE_INSENSITIVE_FILENAME
6494 "fname_case",
6495#endif
6496#ifdef HAVE_ACL
6497 "acl",
6498#endif
6499#ifdef FEAT_ARABIC
6500 "arabic",
6501#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006502 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006503#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006504 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006505#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006506#ifdef FEAT_AUTOSERVERNAME
6507 "autoservername",
6508#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006509#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006511# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512 "balloon_multiline",
6513# endif
6514#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006515#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006516 "balloon_eval_term",
6517#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6519 "builtin_terms",
6520# ifdef ALL_BUILTIN_TCAPS
6521 "all_builtin_terms",
6522# endif
6523#endif
6524#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006525 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006526 || defined(FEAT_GUI_MOTIF))
6527 "browsefilter",
6528#endif
6529#ifdef FEAT_BYTEOFF
6530 "byte_offset",
6531#endif
6532#ifdef FEAT_JOB_CHANNEL
6533 "channel",
6534#endif
6535#ifdef FEAT_CINDENT
6536 "cindent",
6537#endif
6538#ifdef FEAT_CLIENTSERVER
6539 "clientserver",
6540#endif
6541#ifdef FEAT_CLIPBOARD
6542 "clipboard",
6543#endif
6544#ifdef FEAT_CMDL_COMPL
6545 "cmdline_compl",
6546#endif
6547#ifdef FEAT_CMDHIST
6548 "cmdline_hist",
6549#endif
6550#ifdef FEAT_COMMENTS
6551 "comments",
6552#endif
6553#ifdef FEAT_CONCEAL
6554 "conceal",
6555#endif
6556#ifdef FEAT_CRYPT
6557 "cryptv",
6558 "crypt-blowfish",
6559 "crypt-blowfish2",
6560#endif
6561#ifdef FEAT_CSCOPE
6562 "cscope",
6563#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006564 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565#ifdef CURSOR_SHAPE
6566 "cursorshape",
6567#endif
6568#ifdef DEBUG
6569 "debug",
6570#endif
6571#ifdef FEAT_CON_DIALOG
6572 "dialog_con",
6573#endif
6574#ifdef FEAT_GUI_DIALOG
6575 "dialog_gui",
6576#endif
6577#ifdef FEAT_DIFF
6578 "diff",
6579#endif
6580#ifdef FEAT_DIGRAPHS
6581 "digraphs",
6582#endif
6583#ifdef FEAT_DIRECTX
6584 "directx",
6585#endif
6586#ifdef FEAT_DND
6587 "dnd",
6588#endif
6589#ifdef FEAT_EMACS_TAGS
6590 "emacs_tags",
6591#endif
6592 "eval", /* always present, of course! */
6593 "ex_extra", /* graduated feature */
6594#ifdef FEAT_SEARCH_EXTRA
6595 "extra_search",
6596#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006597#ifdef FEAT_SEARCHPATH
6598 "file_in_path",
6599#endif
6600#ifdef FEAT_FILTERPIPE
6601 "filterpipe",
6602#endif
6603#ifdef FEAT_FIND_ID
6604 "find_in_path",
6605#endif
6606#ifdef FEAT_FLOAT
6607 "float",
6608#endif
6609#ifdef FEAT_FOLDING
6610 "folding",
6611#endif
6612#ifdef FEAT_FOOTER
6613 "footer",
6614#endif
6615#if !defined(USE_SYSTEM) && defined(UNIX)
6616 "fork",
6617#endif
6618#ifdef FEAT_GETTEXT
6619 "gettext",
6620#endif
6621#ifdef FEAT_GUI
6622 "gui",
6623#endif
6624#ifdef FEAT_GUI_ATHENA
6625# ifdef FEAT_GUI_NEXTAW
6626 "gui_neXtaw",
6627# else
6628 "gui_athena",
6629# endif
6630#endif
6631#ifdef FEAT_GUI_GTK
6632 "gui_gtk",
6633# ifdef USE_GTK3
6634 "gui_gtk3",
6635# else
6636 "gui_gtk2",
6637# endif
6638#endif
6639#ifdef FEAT_GUI_GNOME
6640 "gui_gnome",
6641#endif
6642#ifdef FEAT_GUI_MAC
6643 "gui_mac",
6644#endif
6645#ifdef FEAT_GUI_MOTIF
6646 "gui_motif",
6647#endif
6648#ifdef FEAT_GUI_PHOTON
6649 "gui_photon",
6650#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006651#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652 "gui_win32",
6653#endif
6654#ifdef FEAT_HANGULIN
6655 "hangul_input",
6656#endif
6657#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6658 "iconv",
6659#endif
6660#ifdef FEAT_INS_EXPAND
6661 "insert_expand",
6662#endif
6663#ifdef FEAT_JOB_CHANNEL
6664 "job",
6665#endif
6666#ifdef FEAT_JUMPLIST
6667 "jumplist",
6668#endif
6669#ifdef FEAT_KEYMAP
6670 "keymap",
6671#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006672 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006673#ifdef FEAT_LANGMAP
6674 "langmap",
6675#endif
6676#ifdef FEAT_LIBCALL
6677 "libcall",
6678#endif
6679#ifdef FEAT_LINEBREAK
6680 "linebreak",
6681#endif
6682#ifdef FEAT_LISP
6683 "lispindent",
6684#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006685 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686#ifdef FEAT_LOCALMAP
6687 "localmap",
6688#endif
6689#ifdef FEAT_LUA
6690# ifndef DYNAMIC_LUA
6691 "lua",
6692# endif
6693#endif
6694#ifdef FEAT_MENU
6695 "menu",
6696#endif
6697#ifdef FEAT_SESSION
6698 "mksession",
6699#endif
6700#ifdef FEAT_MODIFY_FNAME
6701 "modify_fname",
6702#endif
6703#ifdef FEAT_MOUSE
6704 "mouse",
6705#endif
6706#ifdef FEAT_MOUSESHAPE
6707 "mouseshape",
6708#endif
6709#if defined(UNIX) || defined(VMS)
6710# ifdef FEAT_MOUSE_DEC
6711 "mouse_dec",
6712# endif
6713# ifdef FEAT_MOUSE_GPM
6714 "mouse_gpm",
6715# endif
6716# ifdef FEAT_MOUSE_JSB
6717 "mouse_jsbterm",
6718# endif
6719# ifdef FEAT_MOUSE_NET
6720 "mouse_netterm",
6721# endif
6722# ifdef FEAT_MOUSE_PTERM
6723 "mouse_pterm",
6724# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006725# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006726 "mouse_sgr",
6727# endif
6728# ifdef FEAT_SYSMOUSE
6729 "mouse_sysmouse",
6730# endif
6731# ifdef FEAT_MOUSE_URXVT
6732 "mouse_urxvt",
6733# endif
6734# ifdef FEAT_MOUSE_XTERM
6735 "mouse_xterm",
6736# endif
6737#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006738 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739#ifdef FEAT_MBYTE_IME
6740 "multi_byte_ime",
6741#endif
6742#ifdef FEAT_MULTI_LANG
6743 "multi_lang",
6744#endif
6745#ifdef FEAT_MZSCHEME
6746#ifndef DYNAMIC_MZSCHEME
6747 "mzscheme",
6748#endif
6749#endif
6750#ifdef FEAT_NUM64
6751 "num64",
6752#endif
6753#ifdef FEAT_OLE
6754 "ole",
6755#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006756#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006757 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006758#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006759#ifdef FEAT_PATH_EXTRA
6760 "path_extra",
6761#endif
6762#ifdef FEAT_PERL
6763#ifndef DYNAMIC_PERL
6764 "perl",
6765#endif
6766#endif
6767#ifdef FEAT_PERSISTENT_UNDO
6768 "persistent_undo",
6769#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006770#if defined(FEAT_PYTHON)
6771 "python_compiled",
6772# if defined(DYNAMIC_PYTHON)
6773 "python_dynamic",
6774# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006775 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006776 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006777# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006779#if defined(FEAT_PYTHON3)
6780 "python3_compiled",
6781# if defined(DYNAMIC_PYTHON3)
6782 "python3_dynamic",
6783# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006785 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006786# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006787#endif
6788#ifdef FEAT_POSTSCRIPT
6789 "postscript",
6790#endif
6791#ifdef FEAT_PRINTER
6792 "printer",
6793#endif
6794#ifdef FEAT_PROFILE
6795 "profile",
6796#endif
6797#ifdef FEAT_RELTIME
6798 "reltime",
6799#endif
6800#ifdef FEAT_QUICKFIX
6801 "quickfix",
6802#endif
6803#ifdef FEAT_RIGHTLEFT
6804 "rightleft",
6805#endif
6806#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6807 "ruby",
6808#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006809 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810#ifdef FEAT_CMDL_INFO
6811 "showcmd",
6812 "cmdline_info",
6813#endif
6814#ifdef FEAT_SIGNS
6815 "signs",
6816#endif
6817#ifdef FEAT_SMARTINDENT
6818 "smartindent",
6819#endif
6820#ifdef STARTUPTIME
6821 "startuptime",
6822#endif
6823#ifdef FEAT_STL_OPT
6824 "statusline",
6825#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826#ifdef FEAT_NETBEANS_INTG
6827 "netbeans_intg",
6828#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006829#ifdef FEAT_SOUND
6830 "sound",
6831#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006832#ifdef FEAT_SPELL
6833 "spell",
6834#endif
6835#ifdef FEAT_SYN_HL
6836 "syntax",
6837#endif
6838#if defined(USE_SYSTEM) || !defined(UNIX)
6839 "system",
6840#endif
6841#ifdef FEAT_TAG_BINS
6842 "tag_binary",
6843#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006844#ifdef FEAT_TCL
6845# ifndef DYNAMIC_TCL
6846 "tcl",
6847# endif
6848#endif
6849#ifdef FEAT_TERMGUICOLORS
6850 "termguicolors",
6851#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006852#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006853 "terminal",
6854#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006855#ifdef TERMINFO
6856 "terminfo",
6857#endif
6858#ifdef FEAT_TERMRESPONSE
6859 "termresponse",
6860#endif
6861#ifdef FEAT_TEXTOBJ
6862 "textobjects",
6863#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006864#ifdef FEAT_TEXT_PROP
6865 "textprop",
6866#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006867#ifdef HAVE_TGETENT
6868 "tgetent",
6869#endif
6870#ifdef FEAT_TIMERS
6871 "timers",
6872#endif
6873#ifdef FEAT_TITLE
6874 "title",
6875#endif
6876#ifdef FEAT_TOOLBAR
6877 "toolbar",
6878#endif
6879#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6880 "unnamedplus",
6881#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882 "user-commands", /* was accidentally included in 5.4 */
6883 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006884#ifdef FEAT_VARTABS
6885 "vartabs",
6886#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006887 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888#ifdef FEAT_VIMINFO
6889 "viminfo",
6890#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006891 "vimscript-1",
6892 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006893 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006898#ifdef FEAT_VTP
6899 "vtp",
6900#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901#ifdef FEAT_WILDIGN
6902 "wildignore",
6903#endif
6904#ifdef FEAT_WILDMENU
6905 "wildmenu",
6906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908#ifdef FEAT_WAK
6909 "winaltkeys",
6910#endif
6911#ifdef FEAT_WRITEBACKUP
6912 "writebackup",
6913#endif
6914#ifdef FEAT_XIM
6915 "xim",
6916#endif
6917#ifdef FEAT_XFONTSET
6918 "xfontset",
6919#endif
6920#ifdef FEAT_XPM_W32
6921 "xpm",
6922 "xpm_w32", /* for backward compatibility */
6923#else
6924# if defined(HAVE_XPM)
6925 "xpm",
6926# endif
6927#endif
6928#ifdef USE_XSMP
6929 "xsmp",
6930#endif
6931#ifdef USE_XSMP_INTERACT
6932 "xsmp_interact",
6933#endif
6934#ifdef FEAT_XCLIPBOARD
6935 "xterm_clipboard",
6936#endif
6937#ifdef FEAT_XTERM_SAVE
6938 "xterm_save",
6939#endif
6940#if defined(UNIX) && defined(FEAT_X11)
6941 "X11",
6942#endif
6943 NULL
6944 };
6945
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006946 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006947 for (i = 0; has_list[i] != NULL; ++i)
6948 if (STRICMP(name, has_list[i]) == 0)
6949 {
6950 n = TRUE;
6951 break;
6952 }
6953
6954 if (n == FALSE)
6955 {
6956 if (STRNICMP(name, "patch", 5) == 0)
6957 {
6958 if (name[5] == '-'
6959 && STRLEN(name) >= 11
6960 && vim_isdigit(name[6])
6961 && vim_isdigit(name[8])
6962 && vim_isdigit(name[10]))
6963 {
6964 int major = atoi((char *)name + 6);
6965 int minor = atoi((char *)name + 8);
6966
6967 /* Expect "patch-9.9.01234". */
6968 n = (major < VIM_VERSION_MAJOR
6969 || (major == VIM_VERSION_MAJOR
6970 && (minor < VIM_VERSION_MINOR
6971 || (minor == VIM_VERSION_MINOR
6972 && has_patch(atoi((char *)name + 10))))));
6973 }
6974 else
6975 n = has_patch(atoi((char *)name + 5));
6976 }
6977 else if (STRICMP(name, "vim_starting") == 0)
6978 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006979 else if (STRICMP(name, "ttyin") == 0)
6980 n = mch_input_isatty();
6981 else if (STRICMP(name, "ttyout") == 0)
6982 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006983 else if (STRICMP(name, "multi_byte_encoding") == 0)
6984 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006985#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 else if (STRICMP(name, "balloon_multiline") == 0)
6987 n = multiline_balloon_available();
6988#endif
6989#ifdef DYNAMIC_TCL
6990 else if (STRICMP(name, "tcl") == 0)
6991 n = tcl_enabled(FALSE);
6992#endif
6993#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6994 else if (STRICMP(name, "iconv") == 0)
6995 n = iconv_enabled(FALSE);
6996#endif
6997#ifdef DYNAMIC_LUA
6998 else if (STRICMP(name, "lua") == 0)
6999 n = lua_enabled(FALSE);
7000#endif
7001#ifdef DYNAMIC_MZSCHEME
7002 else if (STRICMP(name, "mzscheme") == 0)
7003 n = mzscheme_enabled(FALSE);
7004#endif
7005#ifdef DYNAMIC_RUBY
7006 else if (STRICMP(name, "ruby") == 0)
7007 n = ruby_enabled(FALSE);
7008#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007009#ifdef DYNAMIC_PYTHON
7010 else if (STRICMP(name, "python") == 0)
7011 n = python_enabled(FALSE);
7012#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013#ifdef DYNAMIC_PYTHON3
7014 else if (STRICMP(name, "python3") == 0)
7015 n = python3_enabled(FALSE);
7016#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007017#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7018 else if (STRICMP(name, "pythonx") == 0)
7019 {
7020# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7021 if (p_pyx == 0)
7022 n = python3_enabled(FALSE) || python_enabled(FALSE);
7023 else if (p_pyx == 3)
7024 n = python3_enabled(FALSE);
7025 else if (p_pyx == 2)
7026 n = python_enabled(FALSE);
7027# elif defined(DYNAMIC_PYTHON)
7028 n = python_enabled(FALSE);
7029# elif defined(DYNAMIC_PYTHON3)
7030 n = python3_enabled(FALSE);
7031# endif
7032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007033#endif
7034#ifdef DYNAMIC_PERL
7035 else if (STRICMP(name, "perl") == 0)
7036 n = perl_enabled(FALSE);
7037#endif
7038#ifdef FEAT_GUI
7039 else if (STRICMP(name, "gui_running") == 0)
7040 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041# ifdef FEAT_BROWSE
7042 else if (STRICMP(name, "browse") == 0)
7043 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7044# endif
7045#endif
7046#ifdef FEAT_SYN_HL
7047 else if (STRICMP(name, "syntax_items") == 0)
7048 n = syntax_present(curwin);
7049#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007050#ifdef FEAT_VTP
7051 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007052 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053#endif
7054#ifdef FEAT_NETBEANS_INTG
7055 else if (STRICMP(name, "netbeans_enabled") == 0)
7056 n = netbeans_active();
7057#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007058#ifdef FEAT_MOUSE_GPM
7059 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7060 n = gpm_enabled();
7061#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007062#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007063 else if (STRICMP(name, "terminal") == 0)
7064 n = terminal_enabled();
7065#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007066#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007067 else if (STRICMP(name, "conpty") == 0)
7068 n = use_conpty();
7069#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 }
7071
7072 rettv->vval.v_number = n;
7073}
7074
7075/*
7076 * "has_key()" function
7077 */
7078 static void
7079f_has_key(typval_T *argvars, typval_T *rettv)
7080{
7081 if (argvars[0].v_type != VAR_DICT)
7082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007083 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 return;
7085 }
7086 if (argvars[0].vval.v_dict == NULL)
7087 return;
7088
7089 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007090 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007091}
7092
7093/*
7094 * "haslocaldir()" function
7095 */
7096 static void
7097f_haslocaldir(typval_T *argvars, typval_T *rettv)
7098{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007099 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 win_T *wp = NULL;
7101
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007102 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7103
7104 // Check for window-local and tab-local directories
7105 if (wp != NULL && wp->w_localdir != NULL)
7106 rettv->vval.v_number = 1;
7107 else if (tp != NULL && tp->tp_localdir != NULL)
7108 rettv->vval.v_number = 2;
7109 else
7110 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007111}
7112
7113/*
7114 * "hasmapto()" function
7115 */
7116 static void
7117f_hasmapto(typval_T *argvars, typval_T *rettv)
7118{
7119 char_u *name;
7120 char_u *mode;
7121 char_u buf[NUMBUFLEN];
7122 int abbr = FALSE;
7123
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007124 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007125 if (argvars[1].v_type == VAR_UNKNOWN)
7126 mode = (char_u *)"nvo";
7127 else
7128 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 }
7133
7134 if (map_to_exists(name, mode, abbr))
7135 rettv->vval.v_number = TRUE;
7136 else
7137 rettv->vval.v_number = FALSE;
7138}
7139
7140/*
7141 * "histadd()" function
7142 */
7143 static void
7144f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7145{
7146#ifdef FEAT_CMDHIST
7147 int histype;
7148 char_u *str;
7149 char_u buf[NUMBUFLEN];
7150#endif
7151
7152 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007153 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007154 return;
7155#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007156 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157 histype = str != NULL ? get_histtype(str) : -1;
7158 if (histype >= 0)
7159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007160 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 if (*str != NUL)
7162 {
7163 init_history();
7164 add_to_history(histype, str, FALSE, NUL);
7165 rettv->vval.v_number = TRUE;
7166 return;
7167 }
7168 }
7169#endif
7170}
7171
7172/*
7173 * "histdel()" function
7174 */
7175 static void
7176f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7177{
7178#ifdef FEAT_CMDHIST
7179 int n;
7180 char_u buf[NUMBUFLEN];
7181 char_u *str;
7182
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184 if (str == NULL)
7185 n = 0;
7186 else if (argvars[1].v_type == VAR_UNKNOWN)
7187 /* only one argument: clear entire history */
7188 n = clr_history(get_histtype(str));
7189 else if (argvars[1].v_type == VAR_NUMBER)
7190 /* index given: remove that entry */
7191 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007192 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007193 else
7194 /* string given: remove all matching entries */
7195 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007196 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007197 rettv->vval.v_number = n;
7198#endif
7199}
7200
7201/*
7202 * "histget()" function
7203 */
7204 static void
7205f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7206{
7207#ifdef FEAT_CMDHIST
7208 int type;
7209 int idx;
7210 char_u *str;
7211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007212 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007213 if (str == NULL)
7214 rettv->vval.v_string = NULL;
7215 else
7216 {
7217 type = get_histtype(str);
7218 if (argvars[1].v_type == VAR_UNKNOWN)
7219 idx = get_history_idx(type);
7220 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007221 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007222 /* -1 on type error */
7223 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7224 }
7225#else
7226 rettv->vval.v_string = NULL;
7227#endif
7228 rettv->v_type = VAR_STRING;
7229}
7230
7231/*
7232 * "histnr()" function
7233 */
7234 static void
7235f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7236{
7237 int i;
7238
7239#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007240 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241
7242 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7243 if (i >= HIST_CMD && i < HIST_COUNT)
7244 i = get_history_idx(i);
7245 else
7246#endif
7247 i = -1;
7248 rettv->vval.v_number = i;
7249}
7250
7251/*
7252 * "highlightID(name)" function
7253 */
7254 static void
7255f_hlID(typval_T *argvars, typval_T *rettv)
7256{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007257 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007258}
7259
7260/*
7261 * "highlight_exists()" function
7262 */
7263 static void
7264f_hlexists(typval_T *argvars, typval_T *rettv)
7265{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007266 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007267}
7268
7269/*
7270 * "hostname()" function
7271 */
7272 static void
7273f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7274{
7275 char_u hostname[256];
7276
7277 mch_get_host_name(hostname, 256);
7278 rettv->v_type = VAR_STRING;
7279 rettv->vval.v_string = vim_strsave(hostname);
7280}
7281
7282/*
7283 * iconv() function
7284 */
7285 static void
7286f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7287{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 char_u buf1[NUMBUFLEN];
7289 char_u buf2[NUMBUFLEN];
7290 char_u *from, *to, *str;
7291 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292
7293 rettv->v_type = VAR_STRING;
7294 rettv->vval.v_string = NULL;
7295
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007296 str = tv_get_string(&argvars[0]);
7297 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7298 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007299 vimconv.vc_type = CONV_NONE;
7300 convert_setup(&vimconv, from, to);
7301
7302 /* If the encodings are equal, no conversion needed. */
7303 if (vimconv.vc_type == CONV_NONE)
7304 rettv->vval.v_string = vim_strsave(str);
7305 else
7306 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7307
7308 convert_setup(&vimconv, NULL, NULL);
7309 vim_free(from);
7310 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007311}
7312
7313/*
7314 * "indent()" function
7315 */
7316 static void
7317f_indent(typval_T *argvars, typval_T *rettv)
7318{
7319 linenr_T lnum;
7320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007321 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007322 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7323 rettv->vval.v_number = get_indent_lnum(lnum);
7324 else
7325 rettv->vval.v_number = -1;
7326}
7327
7328/*
7329 * "index()" function
7330 */
7331 static void
7332f_index(typval_T *argvars, typval_T *rettv)
7333{
7334 list_T *l;
7335 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007336 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007337 long idx = 0;
7338 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007339 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340
7341 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007342 if (argvars[0].v_type == VAR_BLOB)
7343 {
7344 typval_T tv;
7345 int start = 0;
7346
7347 if (argvars[2].v_type != VAR_UNKNOWN)
7348 {
7349 start = tv_get_number_chk(&argvars[2], &error);
7350 if (error)
7351 return;
7352 }
7353 b = argvars[0].vval.v_blob;
7354 if (b == NULL)
7355 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007356 if (start < 0)
7357 {
7358 start = blob_len(b) + start;
7359 if (start < 0)
7360 start = 0;
7361 }
7362
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007363 for (idx = start; idx < blob_len(b); ++idx)
7364 {
7365 tv.v_type = VAR_NUMBER;
7366 tv.vval.v_number = blob_get(b, idx);
7367 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7368 {
7369 rettv->vval.v_number = idx;
7370 return;
7371 }
7372 }
7373 return;
7374 }
7375 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007376 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007377 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007378 return;
7379 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007380
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 l = argvars[0].vval.v_list;
7382 if (l != NULL)
7383 {
7384 item = l->lv_first;
7385 if (argvars[2].v_type != VAR_UNKNOWN)
7386 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 /* Start at specified item. Use the cached index that list_find()
7388 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007389 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 idx = l->lv_idx;
7391 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007392 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 if (error)
7394 item = NULL;
7395 }
7396
7397 for ( ; item != NULL; item = item->li_next, ++idx)
7398 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7399 {
7400 rettv->vval.v_number = idx;
7401 break;
7402 }
7403 }
7404}
7405
7406static int inputsecret_flag = 0;
7407
7408/*
7409 * "input()" function
7410 * Also handles inputsecret() when inputsecret is set.
7411 */
7412 static void
7413f_input(typval_T *argvars, typval_T *rettv)
7414{
7415 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7416}
7417
7418/*
7419 * "inputdialog()" function
7420 */
7421 static void
7422f_inputdialog(typval_T *argvars, typval_T *rettv)
7423{
7424#if defined(FEAT_GUI_TEXTDIALOG)
7425 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7426 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7427 {
7428 char_u *message;
7429 char_u buf[NUMBUFLEN];
7430 char_u *defstr = (char_u *)"";
7431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007432 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007433 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007434 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7436 else
7437 IObuff[0] = NUL;
7438 if (message != NULL && defstr != NULL
7439 && do_dialog(VIM_QUESTION, NULL, message,
7440 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7441 rettv->vval.v_string = vim_strsave(IObuff);
7442 else
7443 {
7444 if (message != NULL && defstr != NULL
7445 && argvars[1].v_type != VAR_UNKNOWN
7446 && argvars[2].v_type != VAR_UNKNOWN)
7447 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007448 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007449 else
7450 rettv->vval.v_string = NULL;
7451 }
7452 rettv->v_type = VAR_STRING;
7453 }
7454 else
7455#endif
7456 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7457}
7458
7459/*
7460 * "inputlist()" function
7461 */
7462 static void
7463f_inputlist(typval_T *argvars, typval_T *rettv)
7464{
7465 listitem_T *li;
7466 int selected;
7467 int mouse_used;
7468
7469#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007470 /* While starting up, there is no place to enter text. When running tests
7471 * with --not-a-term we assume feedkeys() will be used. */
7472 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007473 return;
7474#endif
7475 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007477 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007478 return;
7479 }
7480
7481 msg_start();
7482 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7483 lines_left = Rows; /* avoid more prompt */
7484 msg_scroll = TRUE;
7485 msg_clr_eos();
7486
7487 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7488 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007489 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007490 msg_putchar('\n');
7491 }
7492
7493 /* Ask for choice. */
7494 selected = prompt_for_number(&mouse_used);
7495 if (mouse_used)
7496 selected -= lines_left;
7497
7498 rettv->vval.v_number = selected;
7499}
7500
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007501static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7502
7503/*
7504 * "inputrestore()" function
7505 */
7506 static void
7507f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7508{
7509 if (ga_userinput.ga_len > 0)
7510 {
7511 --ga_userinput.ga_len;
7512 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7513 + ga_userinput.ga_len);
7514 /* default return is zero == OK */
7515 }
7516 else if (p_verbose > 1)
7517 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007518 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007519 rettv->vval.v_number = 1; /* Failed */
7520 }
7521}
7522
7523/*
7524 * "inputsave()" function
7525 */
7526 static void
7527f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7528{
7529 /* Add an entry to the stack of typeahead storage. */
7530 if (ga_grow(&ga_userinput, 1) == OK)
7531 {
7532 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7533 + ga_userinput.ga_len);
7534 ++ga_userinput.ga_len;
7535 /* default return is zero == OK */
7536 }
7537 else
7538 rettv->vval.v_number = 1; /* Failed */
7539}
7540
7541/*
7542 * "inputsecret()" function
7543 */
7544 static void
7545f_inputsecret(typval_T *argvars, typval_T *rettv)
7546{
7547 ++cmdline_star;
7548 ++inputsecret_flag;
7549 f_input(argvars, rettv);
7550 --cmdline_star;
7551 --inputsecret_flag;
7552}
7553
7554/*
7555 * "insert()" function
7556 */
7557 static void
7558f_insert(typval_T *argvars, typval_T *rettv)
7559{
7560 long before = 0;
7561 listitem_T *item;
7562 list_T *l;
7563 int error = FALSE;
7564
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007565 if (argvars[0].v_type == VAR_BLOB)
7566 {
7567 int val, len;
7568 char_u *p;
7569
7570 len = blob_len(argvars[0].vval.v_blob);
7571 if (argvars[2].v_type != VAR_UNKNOWN)
7572 {
7573 before = (long)tv_get_number_chk(&argvars[2], &error);
7574 if (error)
7575 return; // type error; errmsg already given
7576 if (before < 0 || before > len)
7577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007578 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007579 return;
7580 }
7581 }
7582 val = tv_get_number_chk(&argvars[1], &error);
7583 if (error)
7584 return;
7585 if (val < 0 || val > 255)
7586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007588 return;
7589 }
7590
7591 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7592 return;
7593 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7594 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7595 *(p + before) = val;
7596 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7597
7598 copy_tv(&argvars[0], rettv);
7599 }
7600 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007601 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007602 else if ((l = argvars[0].vval.v_list) != NULL
7603 && !var_check_lock(l->lv_lock,
7604 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 {
7606 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007607 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007608 if (error)
7609 return; /* type error; errmsg already given */
7610
7611 if (before == l->lv_len)
7612 item = NULL;
7613 else
7614 {
7615 item = list_find(l, before);
7616 if (item == NULL)
7617 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007618 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007619 l = NULL;
7620 }
7621 }
7622 if (l != NULL)
7623 {
7624 list_insert_tv(l, &argvars[1], item);
7625 copy_tv(&argvars[0], rettv);
7626 }
7627 }
7628}
7629
7630/*
7631 * "invert(expr)" function
7632 */
7633 static void
7634f_invert(typval_T *argvars, typval_T *rettv)
7635{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007636 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007637}
7638
7639/*
7640 * "isdirectory()" function
7641 */
7642 static void
7643f_isdirectory(typval_T *argvars, typval_T *rettv)
7644{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007645 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007646}
7647
7648/*
7649 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7650 * or it refers to a List or Dictionary that is locked.
7651 */
7652 static int
7653tv_islocked(typval_T *tv)
7654{
7655 return (tv->v_lock & VAR_LOCKED)
7656 || (tv->v_type == VAR_LIST
7657 && tv->vval.v_list != NULL
7658 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7659 || (tv->v_type == VAR_DICT
7660 && tv->vval.v_dict != NULL
7661 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7662}
7663
7664/*
7665 * "islocked()" function
7666 */
7667 static void
7668f_islocked(typval_T *argvars, typval_T *rettv)
7669{
7670 lval_T lv;
7671 char_u *end;
7672 dictitem_T *di;
7673
7674 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007675 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007676 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 if (end != NULL && lv.ll_name != NULL)
7678 {
7679 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 else
7682 {
7683 if (lv.ll_tv == NULL)
7684 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007685 di = find_var(lv.ll_name, NULL, TRUE);
7686 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007688 /* Consider a variable locked when:
7689 * 1. the variable itself is locked
7690 * 2. the value of the variable is locked.
7691 * 3. the List or Dict value is locked.
7692 */
7693 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7694 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 }
7696 }
7697 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007698 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 else if (lv.ll_list != NULL)
7702 /* List item. */
7703 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7704 else
7705 /* Dictionary item. */
7706 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7707 }
7708 }
7709
7710 clear_lval(&lv);
7711}
7712
7713#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7714/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007715 * "isinf()" function
7716 */
7717 static void
7718f_isinf(typval_T *argvars, typval_T *rettv)
7719{
7720 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7721 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7722}
7723
7724/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007725 * "isnan()" function
7726 */
7727 static void
7728f_isnan(typval_T *argvars, typval_T *rettv)
7729{
7730 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7731 && isnan(argvars[0].vval.v_float);
7732}
7733#endif
7734
7735/*
7736 * "items(dict)" function
7737 */
7738 static void
7739f_items(typval_T *argvars, typval_T *rettv)
7740{
7741 dict_list(argvars, rettv, 2);
7742}
7743
7744#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7745/*
7746 * Get the job from the argument.
7747 * Returns NULL if the job is invalid.
7748 */
7749 static job_T *
7750get_job_arg(typval_T *tv)
7751{
7752 job_T *job;
7753
7754 if (tv->v_type != VAR_JOB)
7755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007756 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 return NULL;
7758 }
7759 job = tv->vval.v_job;
7760
7761 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007762 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 return job;
7764}
7765
7766/*
7767 * "job_getchannel()" function
7768 */
7769 static void
7770f_job_getchannel(typval_T *argvars, typval_T *rettv)
7771{
7772 job_T *job = get_job_arg(&argvars[0]);
7773
7774 if (job != NULL)
7775 {
7776 rettv->v_type = VAR_CHANNEL;
7777 rettv->vval.v_channel = job->jv_channel;
7778 if (job->jv_channel != NULL)
7779 ++job->jv_channel->ch_refcount;
7780 }
7781}
7782
7783/*
7784 * "job_info()" function
7785 */
7786 static void
7787f_job_info(typval_T *argvars, typval_T *rettv)
7788{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007789 if (argvars[0].v_type != VAR_UNKNOWN)
7790 {
7791 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007793 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7794 job_info(job, rettv->vval.v_dict);
7795 }
7796 else if (rettv_list_alloc(rettv) == OK)
7797 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798}
7799
7800/*
7801 * "job_setoptions()" function
7802 */
7803 static void
7804f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7805{
7806 job_T *job = get_job_arg(&argvars[0]);
7807 jobopt_T opt;
7808
7809 if (job == NULL)
7810 return;
7811 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007812 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 job_set_options(job, &opt);
7814 free_job_options(&opt);
7815}
7816
7817/*
7818 * "job_start()" function
7819 */
7820 static void
7821f_job_start(typval_T *argvars, typval_T *rettv)
7822{
7823 rettv->v_type = VAR_JOB;
7824 if (check_restricted() || check_secure())
7825 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007826 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007827}
7828
7829/*
7830 * "job_status()" function
7831 */
7832 static void
7833f_job_status(typval_T *argvars, typval_T *rettv)
7834{
7835 job_T *job = get_job_arg(&argvars[0]);
7836
7837 if (job != NULL)
7838 {
7839 rettv->v_type = VAR_STRING;
7840 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7841 }
7842}
7843
7844/*
7845 * "job_stop()" function
7846 */
7847 static void
7848f_job_stop(typval_T *argvars, typval_T *rettv)
7849{
7850 job_T *job = get_job_arg(&argvars[0]);
7851
7852 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007853 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007854}
7855#endif
7856
7857/*
7858 * "join()" function
7859 */
7860 static void
7861f_join(typval_T *argvars, typval_T *rettv)
7862{
7863 garray_T ga;
7864 char_u *sep;
7865
7866 if (argvars[0].v_type != VAR_LIST)
7867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007868 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 return;
7870 }
7871 if (argvars[0].vval.v_list == NULL)
7872 return;
7873 if (argvars[1].v_type == VAR_UNKNOWN)
7874 sep = (char_u *)" ";
7875 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007876 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877
7878 rettv->v_type = VAR_STRING;
7879
7880 if (sep != NULL)
7881 {
7882 ga_init2(&ga, (int)sizeof(char), 80);
7883 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7884 ga_append(&ga, NUL);
7885 rettv->vval.v_string = (char_u *)ga.ga_data;
7886 }
7887 else
7888 rettv->vval.v_string = NULL;
7889}
7890
7891/*
7892 * "js_decode()" function
7893 */
7894 static void
7895f_js_decode(typval_T *argvars, typval_T *rettv)
7896{
7897 js_read_T reader;
7898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007899 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007900 reader.js_fill = NULL;
7901 reader.js_used = 0;
7902 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007903 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904}
7905
7906/*
7907 * "js_encode()" function
7908 */
7909 static void
7910f_js_encode(typval_T *argvars, typval_T *rettv)
7911{
7912 rettv->v_type = VAR_STRING;
7913 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7914}
7915
7916/*
7917 * "json_decode()" function
7918 */
7919 static void
7920f_json_decode(typval_T *argvars, typval_T *rettv)
7921{
7922 js_read_T reader;
7923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007924 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007925 reader.js_fill = NULL;
7926 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007927 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007928}
7929
7930/*
7931 * "json_encode()" function
7932 */
7933 static void
7934f_json_encode(typval_T *argvars, typval_T *rettv)
7935{
7936 rettv->v_type = VAR_STRING;
7937 rettv->vval.v_string = json_encode(&argvars[0], 0);
7938}
7939
7940/*
7941 * "keys()" function
7942 */
7943 static void
7944f_keys(typval_T *argvars, typval_T *rettv)
7945{
7946 dict_list(argvars, rettv, 0);
7947}
7948
7949/*
7950 * "last_buffer_nr()" function.
7951 */
7952 static void
7953f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7954{
7955 int n = 0;
7956 buf_T *buf;
7957
Bram Moolenaar29323592016-07-24 22:04:11 +02007958 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007959 if (n < buf->b_fnum)
7960 n = buf->b_fnum;
7961
7962 rettv->vval.v_number = n;
7963}
7964
7965/*
7966 * "len()" function
7967 */
7968 static void
7969f_len(typval_T *argvars, typval_T *rettv)
7970{
7971 switch (argvars[0].v_type)
7972 {
7973 case VAR_STRING:
7974 case VAR_NUMBER:
7975 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007976 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007977 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007978 case VAR_BLOB:
7979 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7980 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 case VAR_LIST:
7982 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7983 break;
7984 case VAR_DICT:
7985 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7986 break;
7987 case VAR_UNKNOWN:
7988 case VAR_SPECIAL:
7989 case VAR_FLOAT:
7990 case VAR_FUNC:
7991 case VAR_PARTIAL:
7992 case VAR_JOB:
7993 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007994 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995 break;
7996 }
7997}
7998
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01008000libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001{
8002#ifdef FEAT_LIBCALL
8003 char_u *string_in;
8004 char_u **string_result;
8005 int nr_result;
8006#endif
8007
8008 rettv->v_type = type;
8009 if (type != VAR_NUMBER)
8010 rettv->vval.v_string = NULL;
8011
8012 if (check_restricted() || check_secure())
8013 return;
8014
8015#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008016 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008017 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8018 {
8019 string_in = NULL;
8020 if (argvars[2].v_type == VAR_STRING)
8021 string_in = argvars[2].vval.v_string;
8022 if (type == VAR_NUMBER)
8023 string_result = NULL;
8024 else
8025 string_result = &rettv->vval.v_string;
8026 if (mch_libcall(argvars[0].vval.v_string,
8027 argvars[1].vval.v_string,
8028 string_in,
8029 argvars[2].vval.v_number,
8030 string_result,
8031 &nr_result) == OK
8032 && type == VAR_NUMBER)
8033 rettv->vval.v_number = nr_result;
8034 }
8035#endif
8036}
8037
8038/*
8039 * "libcall()" function
8040 */
8041 static void
8042f_libcall(typval_T *argvars, typval_T *rettv)
8043{
8044 libcall_common(argvars, rettv, VAR_STRING);
8045}
8046
8047/*
8048 * "libcallnr()" function
8049 */
8050 static void
8051f_libcallnr(typval_T *argvars, typval_T *rettv)
8052{
8053 libcall_common(argvars, rettv, VAR_NUMBER);
8054}
8055
8056/*
8057 * "line(string)" function
8058 */
8059 static void
8060f_line(typval_T *argvars, typval_T *rettv)
8061{
8062 linenr_T lnum = 0;
8063 pos_T *fp;
8064 int fnum;
8065
8066 fp = var2fpos(&argvars[0], TRUE, &fnum);
8067 if (fp != NULL)
8068 lnum = fp->lnum;
8069 rettv->vval.v_number = lnum;
8070}
8071
8072/*
8073 * "line2byte(lnum)" function
8074 */
8075 static void
8076f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8077{
8078#ifndef FEAT_BYTEOFF
8079 rettv->vval.v_number = -1;
8080#else
8081 linenr_T lnum;
8082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008083 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008084 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8085 rettv->vval.v_number = -1;
8086 else
8087 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8088 if (rettv->vval.v_number >= 0)
8089 ++rettv->vval.v_number;
8090#endif
8091}
8092
8093/*
8094 * "lispindent(lnum)" function
8095 */
8096 static void
8097f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8098{
8099#ifdef FEAT_LISP
8100 pos_T pos;
8101 linenr_T lnum;
8102
8103 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008104 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008105 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8106 {
8107 curwin->w_cursor.lnum = lnum;
8108 rettv->vval.v_number = get_lisp_indent();
8109 curwin->w_cursor = pos;
8110 }
8111 else
8112#endif
8113 rettv->vval.v_number = -1;
8114}
8115
8116/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008117 * "list2str()" function
8118 */
8119 static void
8120f_list2str(typval_T *argvars, typval_T *rettv)
8121{
8122 list_T *l;
8123 listitem_T *li;
8124 garray_T ga;
8125 int utf8 = FALSE;
8126
8127 rettv->v_type = VAR_STRING;
8128 rettv->vval.v_string = NULL;
8129 if (argvars[0].v_type != VAR_LIST)
8130 {
8131 emsg(_(e_invarg));
8132 return;
8133 }
8134
8135 l = argvars[0].vval.v_list;
8136 if (l == NULL)
8137 return; // empty list results in empty string
8138
8139 if (argvars[1].v_type != VAR_UNKNOWN)
8140 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8141
8142 ga_init2(&ga, 1, 80);
8143 if (has_mbyte || utf8)
8144 {
8145 char_u buf[MB_MAXBYTES + 1];
8146 int (*char2bytes)(int, char_u *);
8147
8148 if (utf8 || enc_utf8)
8149 char2bytes = utf_char2bytes;
8150 else
8151 char2bytes = mb_char2bytes;
8152
8153 for (li = l->lv_first; li != NULL; li = li->li_next)
8154 {
8155 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8156 ga_concat(&ga, buf);
8157 }
8158 ga_append(&ga, NUL);
8159 }
8160 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8161 {
8162 for (li = l->lv_first; li != NULL; li = li->li_next)
8163 ga_append(&ga, tv_get_number(&li->li_tv));
8164 ga_append(&ga, NUL);
8165 }
8166
8167 rettv->v_type = VAR_STRING;
8168 rettv->vval.v_string = ga.ga_data;
8169}
8170
8171/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008172 * "localtime()" function
8173 */
8174 static void
8175f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8176{
8177 rettv->vval.v_number = (varnumber_T)time(NULL);
8178}
8179
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008180 static void
8181get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8182{
8183 char_u *keys;
8184 char_u *which;
8185 char_u buf[NUMBUFLEN];
8186 char_u *keys_buf = NULL;
8187 char_u *rhs;
8188 int mode;
8189 int abbr = FALSE;
8190 int get_dict = FALSE;
8191 mapblock_T *mp;
8192 int buffer_local;
8193
8194 /* return empty string for failure */
8195 rettv->v_type = VAR_STRING;
8196 rettv->vval.v_string = NULL;
8197
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008198 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 if (*keys == NUL)
8200 return;
8201
8202 if (argvars[1].v_type != VAR_UNKNOWN)
8203 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008204 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 if (argvars[2].v_type != VAR_UNKNOWN)
8206 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008207 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008208 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008209 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008210 }
8211 }
8212 else
8213 which = (char_u *)"";
8214 if (which == NULL)
8215 return;
8216
8217 mode = get_map_mode(&which, 0);
8218
8219 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8220 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8221 vim_free(keys_buf);
8222
8223 if (!get_dict)
8224 {
8225 /* Return a string. */
8226 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008227 {
8228 if (*rhs == NUL)
8229 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8230 else
8231 rettv->vval.v_string = str2special_save(rhs, FALSE);
8232 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008233
8234 }
8235 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8236 {
8237 /* Return a dictionary. */
8238 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8239 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8240 dict_T *dict = rettv->vval.v_dict;
8241
Bram Moolenaare0be1672018-07-08 16:50:37 +02008242 dict_add_string(dict, "lhs", lhs);
8243 dict_add_string(dict, "rhs", mp->m_orig_str);
8244 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8245 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8246 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008247 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8248 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008249 dict_add_number(dict, "buffer", (long)buffer_local);
8250 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8251 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008252
8253 vim_free(lhs);
8254 vim_free(mapmode);
8255 }
8256}
8257
8258#ifdef FEAT_FLOAT
8259/*
8260 * "log()" function
8261 */
8262 static void
8263f_log(typval_T *argvars, typval_T *rettv)
8264{
8265 float_T f = 0.0;
8266
8267 rettv->v_type = VAR_FLOAT;
8268 if (get_float_arg(argvars, &f) == OK)
8269 rettv->vval.v_float = log(f);
8270 else
8271 rettv->vval.v_float = 0.0;
8272}
8273
8274/*
8275 * "log10()" function
8276 */
8277 static void
8278f_log10(typval_T *argvars, typval_T *rettv)
8279{
8280 float_T f = 0.0;
8281
8282 rettv->v_type = VAR_FLOAT;
8283 if (get_float_arg(argvars, &f) == OK)
8284 rettv->vval.v_float = log10(f);
8285 else
8286 rettv->vval.v_float = 0.0;
8287}
8288#endif
8289
8290#ifdef FEAT_LUA
8291/*
8292 * "luaeval()" function
8293 */
8294 static void
8295f_luaeval(typval_T *argvars, typval_T *rettv)
8296{
8297 char_u *str;
8298 char_u buf[NUMBUFLEN];
8299
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008300 if (check_restricted() || check_secure())
8301 return;
8302
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008303 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008304 do_luaeval(str, argvars + 1, rettv);
8305}
8306#endif
8307
8308/*
8309 * "map()" function
8310 */
8311 static void
8312f_map(typval_T *argvars, typval_T *rettv)
8313{
8314 filter_map(argvars, rettv, TRUE);
8315}
8316
8317/*
8318 * "maparg()" function
8319 */
8320 static void
8321f_maparg(typval_T *argvars, typval_T *rettv)
8322{
8323 get_maparg(argvars, rettv, TRUE);
8324}
8325
8326/*
8327 * "mapcheck()" function
8328 */
8329 static void
8330f_mapcheck(typval_T *argvars, typval_T *rettv)
8331{
8332 get_maparg(argvars, rettv, FALSE);
8333}
8334
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008335typedef enum
8336{
8337 MATCH_END, /* matchend() */
8338 MATCH_MATCH, /* match() */
8339 MATCH_STR, /* matchstr() */
8340 MATCH_LIST, /* matchlist() */
8341 MATCH_POS /* matchstrpos() */
8342} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343
8344 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008345find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008346{
8347 char_u *str = NULL;
8348 long len = 0;
8349 char_u *expr = NULL;
8350 char_u *pat;
8351 regmatch_T regmatch;
8352 char_u patbuf[NUMBUFLEN];
8353 char_u strbuf[NUMBUFLEN];
8354 char_u *save_cpo;
8355 long start = 0;
8356 long nth = 1;
8357 colnr_T startcol = 0;
8358 int match = 0;
8359 list_T *l = NULL;
8360 listitem_T *li = NULL;
8361 long idx = 0;
8362 char_u *tofree = NULL;
8363
8364 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8365 save_cpo = p_cpo;
8366 p_cpo = (char_u *)"";
8367
8368 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008369 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008371 /* type MATCH_LIST: return empty list when there are no matches.
8372 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008373 if (rettv_list_alloc(rettv) == FAIL)
8374 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008375 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 && (list_append_string(rettv->vval.v_list,
8377 (char_u *)"", 0) == FAIL
8378 || list_append_number(rettv->vval.v_list,
8379 (varnumber_T)-1) == FAIL
8380 || list_append_number(rettv->vval.v_list,
8381 (varnumber_T)-1) == FAIL
8382 || list_append_number(rettv->vval.v_list,
8383 (varnumber_T)-1) == FAIL))
8384 {
8385 list_free(rettv->vval.v_list);
8386 rettv->vval.v_list = NULL;
8387 goto theend;
8388 }
8389 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008390 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008391 {
8392 rettv->v_type = VAR_STRING;
8393 rettv->vval.v_string = NULL;
8394 }
8395
8396 if (argvars[0].v_type == VAR_LIST)
8397 {
8398 if ((l = argvars[0].vval.v_list) == NULL)
8399 goto theend;
8400 li = l->lv_first;
8401 }
8402 else
8403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008404 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405 len = (long)STRLEN(str);
8406 }
8407
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 if (pat == NULL)
8410 goto theend;
8411
8412 if (argvars[2].v_type != VAR_UNKNOWN)
8413 {
8414 int error = FALSE;
8415
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008416 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008417 if (error)
8418 goto theend;
8419 if (l != NULL)
8420 {
8421 li = list_find(l, start);
8422 if (li == NULL)
8423 goto theend;
8424 idx = l->lv_idx; /* use the cached index */
8425 }
8426 else
8427 {
8428 if (start < 0)
8429 start = 0;
8430 if (start > len)
8431 goto theend;
8432 /* When "count" argument is there ignore matches before "start",
8433 * otherwise skip part of the string. Differs when pattern is "^"
8434 * or "\<". */
8435 if (argvars[3].v_type != VAR_UNKNOWN)
8436 startcol = start;
8437 else
8438 {
8439 str += start;
8440 len -= start;
8441 }
8442 }
8443
8444 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008445 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 if (error)
8447 goto theend;
8448 }
8449
8450 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8451 if (regmatch.regprog != NULL)
8452 {
8453 regmatch.rm_ic = p_ic;
8454
8455 for (;;)
8456 {
8457 if (l != NULL)
8458 {
8459 if (li == NULL)
8460 {
8461 match = FALSE;
8462 break;
8463 }
8464 vim_free(tofree);
8465 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8466 if (str == NULL)
8467 break;
8468 }
8469
8470 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8471
8472 if (match && --nth <= 0)
8473 break;
8474 if (l == NULL && !match)
8475 break;
8476
8477 /* Advance to just after the match. */
8478 if (l != NULL)
8479 {
8480 li = li->li_next;
8481 ++idx;
8482 }
8483 else
8484 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008485 startcol = (colnr_T)(regmatch.startp[0]
8486 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008487 if (startcol > (colnr_T)len
8488 || str + startcol <= regmatch.startp[0])
8489 {
8490 match = FALSE;
8491 break;
8492 }
8493 }
8494 }
8495
8496 if (match)
8497 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008498 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008499 {
8500 listitem_T *li1 = rettv->vval.v_list->lv_first;
8501 listitem_T *li2 = li1->li_next;
8502 listitem_T *li3 = li2->li_next;
8503 listitem_T *li4 = li3->li_next;
8504
8505 vim_free(li1->li_tv.vval.v_string);
8506 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8507 (int)(regmatch.endp[0] - regmatch.startp[0]));
8508 li3->li_tv.vval.v_number =
8509 (varnumber_T)(regmatch.startp[0] - expr);
8510 li4->li_tv.vval.v_number =
8511 (varnumber_T)(regmatch.endp[0] - expr);
8512 if (l != NULL)
8513 li2->li_tv.vval.v_number = (varnumber_T)idx;
8514 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008515 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008516 {
8517 int i;
8518
8519 /* return list with matched string and submatches */
8520 for (i = 0; i < NSUBEXP; ++i)
8521 {
8522 if (regmatch.endp[i] == NULL)
8523 {
8524 if (list_append_string(rettv->vval.v_list,
8525 (char_u *)"", 0) == FAIL)
8526 break;
8527 }
8528 else if (list_append_string(rettv->vval.v_list,
8529 regmatch.startp[i],
8530 (int)(regmatch.endp[i] - regmatch.startp[i]))
8531 == FAIL)
8532 break;
8533 }
8534 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008535 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008536 {
8537 /* return matched string */
8538 if (l != NULL)
8539 copy_tv(&li->li_tv, rettv);
8540 else
8541 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8542 (int)(regmatch.endp[0] - regmatch.startp[0]));
8543 }
8544 else if (l != NULL)
8545 rettv->vval.v_number = idx;
8546 else
8547 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008548 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008549 rettv->vval.v_number =
8550 (varnumber_T)(regmatch.startp[0] - str);
8551 else
8552 rettv->vval.v_number =
8553 (varnumber_T)(regmatch.endp[0] - str);
8554 rettv->vval.v_number += (varnumber_T)(str - expr);
8555 }
8556 }
8557 vim_regfree(regmatch.regprog);
8558 }
8559
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008560theend:
8561 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008562 /* matchstrpos() without a list: drop the second item. */
8563 listitem_remove(rettv->vval.v_list,
8564 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 vim_free(tofree);
8566 p_cpo = save_cpo;
8567}
8568
8569/*
8570 * "match()" function
8571 */
8572 static void
8573f_match(typval_T *argvars, typval_T *rettv)
8574{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008575 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576}
8577
Bram Moolenaar95e51472018-07-28 16:55:56 +02008578#ifdef FEAT_SEARCH_EXTRA
8579 static int
8580matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8581{
8582 dictitem_T *di;
8583
8584 if (tv->v_type != VAR_DICT)
8585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008587 return FAIL;
8588 }
8589
8590 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008591 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008592 (char_u *)"conceal", FALSE);
8593
8594 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8595 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008596 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008597 if (*win == NULL)
8598 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008599 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008600 return FAIL;
8601 }
8602 }
8603
8604 return OK;
8605}
8606#endif
8607
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008608/*
8609 * "matchadd()" function
8610 */
8611 static void
8612f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8613{
8614#ifdef FEAT_SEARCH_EXTRA
8615 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008616 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8617 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 int prio = 10; /* default priority */
8619 int id = -1;
8620 int error = FALSE;
8621 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008622 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623
8624 rettv->vval.v_number = -1;
8625
8626 if (grp == NULL || pat == NULL)
8627 return;
8628 if (argvars[2].v_type != VAR_UNKNOWN)
8629 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008630 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 if (argvars[3].v_type != VAR_UNKNOWN)
8632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008633 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008634 if (argvars[4].v_type != VAR_UNKNOWN
8635 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8636 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 }
8638 }
8639 if (error == TRUE)
8640 return;
8641 if (id >= 1 && id <= 3)
8642 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008643 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008644 return;
8645 }
8646
Bram Moolenaar95e51472018-07-28 16:55:56 +02008647 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 conceal_char);
8649#endif
8650}
8651
8652/*
8653 * "matchaddpos()" function
8654 */
8655 static void
8656f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8657{
8658#ifdef FEAT_SEARCH_EXTRA
8659 char_u buf[NUMBUFLEN];
8660 char_u *group;
8661 int prio = 10;
8662 int id = -1;
8663 int error = FALSE;
8664 list_T *l;
8665 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008666 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008667
8668 rettv->vval.v_number = -1;
8669
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008670 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 if (group == NULL)
8672 return;
8673
8674 if (argvars[1].v_type != VAR_LIST)
8675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008676 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 return;
8678 }
8679 l = argvars[1].vval.v_list;
8680 if (l == NULL)
8681 return;
8682
8683 if (argvars[2].v_type != VAR_UNKNOWN)
8684 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008685 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008686 if (argvars[3].v_type != VAR_UNKNOWN)
8687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008688 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008689
8690 if (argvars[4].v_type != VAR_UNKNOWN
8691 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8692 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008693 }
8694 }
8695 if (error == TRUE)
8696 return;
8697
8698 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8699 if (id == 1 || id == 2)
8700 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008701 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008702 return;
8703 }
8704
Bram Moolenaar95e51472018-07-28 16:55:56 +02008705 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 conceal_char);
8707#endif
8708}
8709
8710/*
8711 * "matcharg()" function
8712 */
8713 static void
8714f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8715{
8716 if (rettv_list_alloc(rettv) == OK)
8717 {
8718#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008719 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008720 matchitem_T *m;
8721
8722 if (id >= 1 && id <= 3)
8723 {
8724 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8725 {
8726 list_append_string(rettv->vval.v_list,
8727 syn_id2name(m->hlg_id), -1);
8728 list_append_string(rettv->vval.v_list, m->pattern, -1);
8729 }
8730 else
8731 {
8732 list_append_string(rettv->vval.v_list, NULL, -1);
8733 list_append_string(rettv->vval.v_list, NULL, -1);
8734 }
8735 }
8736#endif
8737 }
8738}
8739
8740/*
8741 * "matchdelete()" function
8742 */
8743 static void
8744f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8745{
8746#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008747 win_T *win = get_optional_window(argvars, 1);
8748
8749 if (win == NULL)
8750 rettv->vval.v_number = -1;
8751 else
8752 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008753 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008754#endif
8755}
8756
8757/*
8758 * "matchend()" function
8759 */
8760 static void
8761f_matchend(typval_T *argvars, typval_T *rettv)
8762{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008763 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764}
8765
8766/*
8767 * "matchlist()" function
8768 */
8769 static void
8770f_matchlist(typval_T *argvars, typval_T *rettv)
8771{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008772 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008773}
8774
8775/*
8776 * "matchstr()" function
8777 */
8778 static void
8779f_matchstr(typval_T *argvars, typval_T *rettv)
8780{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008781 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008782}
8783
8784/*
8785 * "matchstrpos()" function
8786 */
8787 static void
8788f_matchstrpos(typval_T *argvars, typval_T *rettv)
8789{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008790 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791}
8792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 static void
8794max_min(typval_T *argvars, typval_T *rettv, int domax)
8795{
8796 varnumber_T n = 0;
8797 varnumber_T i;
8798 int error = FALSE;
8799
8800 if (argvars[0].v_type == VAR_LIST)
8801 {
8802 list_T *l;
8803 listitem_T *li;
8804
8805 l = argvars[0].vval.v_list;
8806 if (l != NULL)
8807 {
8808 li = l->lv_first;
8809 if (li != NULL)
8810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008811 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 for (;;)
8813 {
8814 li = li->li_next;
8815 if (li == NULL)
8816 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 if (domax ? i > n : i < n)
8819 n = i;
8820 }
8821 }
8822 }
8823 }
8824 else if (argvars[0].v_type == VAR_DICT)
8825 {
8826 dict_T *d;
8827 int first = TRUE;
8828 hashitem_T *hi;
8829 int todo;
8830
8831 d = argvars[0].vval.v_dict;
8832 if (d != NULL)
8833 {
8834 todo = (int)d->dv_hashtab.ht_used;
8835 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8836 {
8837 if (!HASHITEM_EMPTY(hi))
8838 {
8839 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008840 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 if (first)
8842 {
8843 n = i;
8844 first = FALSE;
8845 }
8846 else if (domax ? i > n : i < n)
8847 n = i;
8848 }
8849 }
8850 }
8851 }
8852 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008853 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008854 rettv->vval.v_number = error ? 0 : n;
8855}
8856
8857/*
8858 * "max()" function
8859 */
8860 static void
8861f_max(typval_T *argvars, typval_T *rettv)
8862{
8863 max_min(argvars, rettv, TRUE);
8864}
8865
8866/*
8867 * "min()" function
8868 */
8869 static void
8870f_min(typval_T *argvars, typval_T *rettv)
8871{
8872 max_min(argvars, rettv, FALSE);
8873}
8874
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875/*
8876 * Create the directory in which "dir" is located, and higher levels when
8877 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008878 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879 */
8880 static int
8881mkdir_recurse(char_u *dir, int prot)
8882{
8883 char_u *p;
8884 char_u *updir;
8885 int r = FAIL;
8886
8887 /* Get end of directory name in "dir".
8888 * We're done when it's "/" or "c:/". */
8889 p = gettail_sep(dir);
8890 if (p <= get_past_head(dir))
8891 return OK;
8892
8893 /* If the directory exists we're done. Otherwise: create it.*/
8894 updir = vim_strnsave(dir, (int)(p - dir));
8895 if (updir == NULL)
8896 return FAIL;
8897 if (mch_isdir(updir))
8898 r = OK;
8899 else if (mkdir_recurse(updir, prot) == OK)
8900 r = vim_mkdir_emsg(updir, prot);
8901 vim_free(updir);
8902 return r;
8903}
8904
8905#ifdef vim_mkdir
8906/*
8907 * "mkdir()" function
8908 */
8909 static void
8910f_mkdir(typval_T *argvars, typval_T *rettv)
8911{
8912 char_u *dir;
8913 char_u buf[NUMBUFLEN];
8914 int prot = 0755;
8915
8916 rettv->vval.v_number = FAIL;
8917 if (check_restricted() || check_secure())
8918 return;
8919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008920 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008922 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008924 if (*gettail(dir) == NUL)
8925 /* remove trailing slashes */
8926 *gettail_sep(dir) = NUL;
8927
8928 if (argvars[1].v_type != VAR_UNKNOWN)
8929 {
8930 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008932 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008933 if (prot == -1)
8934 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008936 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008937 {
8938 if (mch_isdir(dir))
8939 {
8940 /* With the "p" flag it's OK if the dir already exists. */
8941 rettv->vval.v_number = OK;
8942 return;
8943 }
8944 mkdir_recurse(dir, prot);
8945 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008946 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008947 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008948}
8949#endif
8950
8951/*
8952 * "mode()" function
8953 */
8954 static void
8955f_mode(typval_T *argvars, typval_T *rettv)
8956{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008957 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008958
Bram Moolenaar612cc382018-07-29 15:34:26 +02008959 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008960
8961 if (time_for_testing == 93784)
8962 {
8963 /* Testing the two-character code. */
8964 buf[0] = 'x';
8965 buf[1] = '!';
8966 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008967#ifdef FEAT_TERMINAL
8968 else if (term_use_loop())
8969 buf[0] = 't';
8970#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008971 else if (VIsual_active)
8972 {
8973 if (VIsual_select)
8974 buf[0] = VIsual_mode + 's' - 'v';
8975 else
8976 buf[0] = VIsual_mode;
8977 }
8978 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8979 || State == CONFIRM)
8980 {
8981 buf[0] = 'r';
8982 if (State == ASKMORE)
8983 buf[1] = 'm';
8984 else if (State == CONFIRM)
8985 buf[1] = '?';
8986 }
8987 else if (State == EXTERNCMD)
8988 buf[0] = '!';
8989 else if (State & INSERT)
8990 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008991 if (State & VREPLACE_FLAG)
8992 {
8993 buf[0] = 'R';
8994 buf[1] = 'v';
8995 }
8996 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008997 {
8998 if (State & REPLACE_FLAG)
8999 buf[0] = 'R';
9000 else
9001 buf[0] = 'i';
9002#ifdef FEAT_INS_EXPAND
9003 if (ins_compl_active())
9004 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01009005 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009006 buf[1] = 'x';
9007#endif
9008 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009010 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 {
9012 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009013 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009014 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009015 else if (exmode_active == EXMODE_NORMAL)
9016 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 }
9018 else
9019 {
9020 buf[0] = 'n';
9021 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009022 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009024 // to be able to detect force-linewise/blockwise/characterwise operations
9025 buf[2] = motion_force;
9026 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009027 else if (restart_edit == 'I' || restart_edit == 'R'
9028 || restart_edit == 'V')
9029 {
9030 buf[1] = 'i';
9031 buf[2] = restart_edit;
9032 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 }
9034
9035 /* Clear out the minor mode when the argument is not a non-zero number or
9036 * non-empty string. */
9037 if (!non_zero_arg(&argvars[0]))
9038 buf[1] = NUL;
9039
9040 rettv->vval.v_string = vim_strsave(buf);
9041 rettv->v_type = VAR_STRING;
9042}
9043
9044#if defined(FEAT_MZSCHEME) || defined(PROTO)
9045/*
9046 * "mzeval()" function
9047 */
9048 static void
9049f_mzeval(typval_T *argvars, typval_T *rettv)
9050{
9051 char_u *str;
9052 char_u buf[NUMBUFLEN];
9053
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009054 if (check_restricted() || check_secure())
9055 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009056 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009057 do_mzeval(str, rettv);
9058}
9059
9060 void
9061mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9062{
9063 typval_T argvars[3];
9064
9065 argvars[0].v_type = VAR_STRING;
9066 argvars[0].vval.v_string = name;
9067 copy_tv(args, &argvars[1]);
9068 argvars[2].v_type = VAR_UNKNOWN;
9069 f_call(argvars, rettv);
9070 clear_tv(&argvars[1]);
9071}
9072#endif
9073
9074/*
9075 * "nextnonblank()" function
9076 */
9077 static void
9078f_nextnonblank(typval_T *argvars, typval_T *rettv)
9079{
9080 linenr_T lnum;
9081
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083 {
9084 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9085 {
9086 lnum = 0;
9087 break;
9088 }
9089 if (*skipwhite(ml_get(lnum)) != NUL)
9090 break;
9091 }
9092 rettv->vval.v_number = lnum;
9093}
9094
9095/*
9096 * "nr2char()" function
9097 */
9098 static void
9099f_nr2char(typval_T *argvars, typval_T *rettv)
9100{
9101 char_u buf[NUMBUFLEN];
9102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 if (has_mbyte)
9104 {
9105 int utf8 = 0;
9106
9107 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009108 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009110 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009112 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 }
9114 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 buf[1] = NUL;
9118 }
9119 rettv->v_type = VAR_STRING;
9120 rettv->vval.v_string = vim_strsave(buf);
9121}
9122
9123/*
9124 * "or(expr, expr)" function
9125 */
9126 static void
9127f_or(typval_T *argvars, typval_T *rettv)
9128{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009129 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9130 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009131}
9132
9133/*
9134 * "pathshorten()" function
9135 */
9136 static void
9137f_pathshorten(typval_T *argvars, typval_T *rettv)
9138{
9139 char_u *p;
9140
9141 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009142 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009143 if (p == NULL)
9144 rettv->vval.v_string = NULL;
9145 else
9146 {
9147 p = vim_strsave(p);
9148 rettv->vval.v_string = p;
9149 if (p != NULL)
9150 shorten_dir(p);
9151 }
9152}
9153
9154#ifdef FEAT_PERL
9155/*
9156 * "perleval()" function
9157 */
9158 static void
9159f_perleval(typval_T *argvars, typval_T *rettv)
9160{
9161 char_u *str;
9162 char_u buf[NUMBUFLEN];
9163
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009164 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009165 do_perleval(str, rettv);
9166}
9167#endif
9168
9169#ifdef FEAT_FLOAT
9170/*
9171 * "pow()" function
9172 */
9173 static void
9174f_pow(typval_T *argvars, typval_T *rettv)
9175{
9176 float_T fx = 0.0, fy = 0.0;
9177
9178 rettv->v_type = VAR_FLOAT;
9179 if (get_float_arg(argvars, &fx) == OK
9180 && get_float_arg(&argvars[1], &fy) == OK)
9181 rettv->vval.v_float = pow(fx, fy);
9182 else
9183 rettv->vval.v_float = 0.0;
9184}
9185#endif
9186
9187/*
9188 * "prevnonblank()" function
9189 */
9190 static void
9191f_prevnonblank(typval_T *argvars, typval_T *rettv)
9192{
9193 linenr_T lnum;
9194
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009195 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009196 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9197 lnum = 0;
9198 else
9199 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9200 --lnum;
9201 rettv->vval.v_number = lnum;
9202}
9203
9204/* This dummy va_list is here because:
9205 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9206 * - locally in the function results in a "used before set" warning
9207 * - using va_start() to initialize it gives "function with fixed args" error */
9208static va_list ap;
9209
9210/*
9211 * "printf()" function
9212 */
9213 static void
9214f_printf(typval_T *argvars, typval_T *rettv)
9215{
9216 char_u buf[NUMBUFLEN];
9217 int len;
9218 char_u *s;
9219 int saved_did_emsg = did_emsg;
9220 char *fmt;
9221
9222 rettv->v_type = VAR_STRING;
9223 rettv->vval.v_string = NULL;
9224
9225 /* Get the required length, allocate the buffer and do it for real. */
9226 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009227 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009228 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009229 if (!did_emsg)
9230 {
9231 s = alloc(len + 1);
9232 if (s != NULL)
9233 {
9234 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009235 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9236 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009237 }
9238 }
9239 did_emsg |= saved_did_emsg;
9240}
9241
Bram Moolenaarf2732452018-06-03 14:47:35 +02009242#ifdef FEAT_JOB_CHANNEL
9243/*
9244 * "prompt_setcallback({buffer}, {callback})" function
9245 */
9246 static void
9247f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9248{
9249 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009250 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009251
9252 if (check_secure())
9253 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009254 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009255 if (buf == NULL)
9256 return;
9257
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009258 callback = get_callback(&argvars[1]);
9259 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009260 return;
9261
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009262 free_callback(&buf->b_prompt_callback);
9263 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009264}
9265
9266/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009267 * "prompt_setinterrupt({buffer}, {callback})" function
9268 */
9269 static void
9270f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9271{
9272 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009273 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009274
9275 if (check_secure())
9276 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009277 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009278 if (buf == NULL)
9279 return;
9280
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009281 callback = get_callback(&argvars[1]);
9282 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009283 return;
9284
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009285 free_callback(&buf->b_prompt_interrupt);
9286 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009287}
9288
9289/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009290 * "prompt_setprompt({buffer}, {text})" function
9291 */
9292 static void
9293f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9294{
9295 buf_T *buf;
9296 char_u *text;
9297
9298 if (check_secure())
9299 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009300 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009301 if (buf == NULL)
9302 return;
9303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009304 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009305 vim_free(buf->b_prompt_text);
9306 buf->b_prompt_text = vim_strsave(text);
9307}
9308#endif
9309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310/*
9311 * "pumvisible()" function
9312 */
9313 static void
9314f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9315{
9316#ifdef FEAT_INS_EXPAND
9317 if (pum_visible())
9318 rettv->vval.v_number = 1;
9319#endif
9320}
9321
9322#ifdef FEAT_PYTHON3
9323/*
9324 * "py3eval()" function
9325 */
9326 static void
9327f_py3eval(typval_T *argvars, typval_T *rettv)
9328{
9329 char_u *str;
9330 char_u buf[NUMBUFLEN];
9331
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009332 if (check_restricted() || check_secure())
9333 return;
9334
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009335 if (p_pyx == 0)
9336 p_pyx = 3;
9337
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009338 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 do_py3eval(str, rettv);
9340}
9341#endif
9342
9343#ifdef FEAT_PYTHON
9344/*
9345 * "pyeval()" function
9346 */
9347 static void
9348f_pyeval(typval_T *argvars, typval_T *rettv)
9349{
9350 char_u *str;
9351 char_u buf[NUMBUFLEN];
9352
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009353 if (check_restricted() || check_secure())
9354 return;
9355
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009356 if (p_pyx == 0)
9357 p_pyx = 2;
9358
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009359 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009360 do_pyeval(str, rettv);
9361}
9362#endif
9363
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009364#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9365/*
9366 * "pyxeval()" function
9367 */
9368 static void
9369f_pyxeval(typval_T *argvars, typval_T *rettv)
9370{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009371 if (check_restricted() || check_secure())
9372 return;
9373
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009374# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9375 init_pyxversion();
9376 if (p_pyx == 2)
9377 f_pyeval(argvars, rettv);
9378 else
9379 f_py3eval(argvars, rettv);
9380# elif defined(FEAT_PYTHON)
9381 f_pyeval(argvars, rettv);
9382# elif defined(FEAT_PYTHON3)
9383 f_py3eval(argvars, rettv);
9384# endif
9385}
9386#endif
9387
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009388/*
9389 * "range()" function
9390 */
9391 static void
9392f_range(typval_T *argvars, typval_T *rettv)
9393{
9394 varnumber_T start;
9395 varnumber_T end;
9396 varnumber_T stride = 1;
9397 varnumber_T i;
9398 int error = FALSE;
9399
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009400 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009401 if (argvars[1].v_type == VAR_UNKNOWN)
9402 {
9403 end = start - 1;
9404 start = 0;
9405 }
9406 else
9407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009408 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009410 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 }
9412
9413 if (error)
9414 return; /* type error; errmsg already given */
9415 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009416 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009418 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009419 else
9420 {
9421 if (rettv_list_alloc(rettv) == OK)
9422 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9423 if (list_append_number(rettv->vval.v_list,
9424 (varnumber_T)i) == FAIL)
9425 break;
9426 }
9427}
9428
9429/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009430 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009431 */
9432 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009433readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009434{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009435 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009436 typval_T save_val;
9437 typval_T rettv;
9438 typval_T argv[2];
9439 int retval = 0;
9440 int error = FALSE;
9441
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009442 if (expr->v_type == VAR_UNKNOWN)
9443 return 1;
9444
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009445 prepare_vimvar(VV_VAL, &save_val);
9446 set_vim_var_string(VV_VAL, name, -1);
9447 argv[0].v_type = VAR_STRING;
9448 argv[0].vval.v_string = name;
9449
9450 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9451 goto theend;
9452
9453 retval = tv_get_number_chk(&rettv, &error);
9454 if (error)
9455 retval = -1;
9456 clear_tv(&rettv);
9457
9458theend:
9459 set_vim_var_string(VV_VAL, NULL, 0);
9460 restore_vimvar(VV_VAL, &save_val);
9461 return retval;
9462}
9463
9464/*
9465 * "readdir()" function
9466 */
9467 static void
9468f_readdir(typval_T *argvars, typval_T *rettv)
9469{
9470 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009471 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009472 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009473 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009474 garray_T ga;
9475 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009476
9477 if (rettv_list_alloc(rettv) == FAIL)
9478 return;
9479 path = tv_get_string(&argvars[0]);
9480 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009481
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009482 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9483 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009484 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009485 for (i = 0; i < ga.ga_len; i++)
9486 {
9487 p = ((char_u **)ga.ga_data)[i];
9488 list_append_string(rettv->vval.v_list, p, -1);
9489 }
9490 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009491 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009492}
9493
9494/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009495 * "readfile()" function
9496 */
9497 static void
9498f_readfile(typval_T *argvars, typval_T *rettv)
9499{
9500 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009501 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009502 int failed = FALSE;
9503 char_u *fname;
9504 FILE *fd;
9505 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9506 int io_size = sizeof(buf);
9507 int readlen; /* size of last fread() */
9508 char_u *prev = NULL; /* previously read bytes, if any */
9509 long prevlen = 0; /* length of data in prev */
9510 long prevsize = 0; /* size of prev buffer */
9511 long maxline = MAXLNUM;
9512 long cnt = 0;
9513 char_u *p; /* position in buf */
9514 char_u *start; /* start of current line */
9515
9516 if (argvars[1].v_type != VAR_UNKNOWN)
9517 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009520 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9521 blob = TRUE;
9522
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009524 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009525 }
9526
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009527 if (blob)
9528 {
9529 if (rettv_blob_alloc(rettv) == FAIL)
9530 return;
9531 }
9532 else
9533 {
9534 if (rettv_list_alloc(rettv) == FAIL)
9535 return;
9536 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009537
9538 /* Always open the file in binary mode, library functions have a mind of
9539 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009540 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009543 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009544 return;
9545 }
9546
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009547 if (blob)
9548 {
9549 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009551 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009552 blob_free(rettv->vval.v_blob);
9553 }
9554 fclose(fd);
9555 return;
9556 }
9557
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009558 while (cnt < maxline || maxline < 0)
9559 {
9560 readlen = (int)fread(buf, 1, io_size, fd);
9561
9562 /* This for loop processes what was read, but is also entered at end
9563 * of file so that either:
9564 * - an incomplete line gets written
9565 * - a "binary" file gets an empty line at the end if it ends in a
9566 * newline. */
9567 for (p = buf, start = buf;
9568 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9569 ++p)
9570 {
9571 if (*p == '\n' || readlen <= 0)
9572 {
9573 listitem_T *li;
9574 char_u *s = NULL;
9575 long_u len = p - start;
9576
9577 /* Finished a line. Remove CRs before NL. */
9578 if (readlen > 0 && !binary)
9579 {
9580 while (len > 0 && start[len - 1] == '\r')
9581 --len;
9582 /* removal may cross back to the "prev" string */
9583 if (len == 0)
9584 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9585 --prevlen;
9586 }
9587 if (prevlen == 0)
9588 s = vim_strnsave(start, (int)len);
9589 else
9590 {
9591 /* Change "prev" buffer to be the right size. This way
9592 * the bytes are only copied once, and very long lines are
9593 * allocated only once. */
9594 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9595 {
9596 mch_memmove(s + prevlen, start, len);
9597 s[prevlen + len] = NUL;
9598 prev = NULL; /* the list will own the string */
9599 prevlen = prevsize = 0;
9600 }
9601 }
9602 if (s == NULL)
9603 {
9604 do_outofmem_msg((long_u) prevlen + len + 1);
9605 failed = TRUE;
9606 break;
9607 }
9608
9609 if ((li = listitem_alloc()) == NULL)
9610 {
9611 vim_free(s);
9612 failed = TRUE;
9613 break;
9614 }
9615 li->li_tv.v_type = VAR_STRING;
9616 li->li_tv.v_lock = 0;
9617 li->li_tv.vval.v_string = s;
9618 list_append(rettv->vval.v_list, li);
9619
9620 start = p + 1; /* step over newline */
9621 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9622 break;
9623 }
9624 else if (*p == NUL)
9625 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009626 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9627 * when finding the BF and check the previous two bytes. */
9628 else if (*p == 0xbf && enc_utf8 && !binary)
9629 {
9630 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9631 * + 1, these may be in the "prev" string. */
9632 char_u back1 = p >= buf + 1 ? p[-1]
9633 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9634 char_u back2 = p >= buf + 2 ? p[-2]
9635 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9636 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9637
9638 if (back2 == 0xef && back1 == 0xbb)
9639 {
9640 char_u *dest = p - 2;
9641
9642 /* Usually a BOM is at the beginning of a file, and so at
9643 * the beginning of a line; then we can just step over it.
9644 */
9645 if (start == dest)
9646 start = p + 1;
9647 else
9648 {
9649 /* have to shuffle buf to close gap */
9650 int adjust_prevlen = 0;
9651
9652 if (dest < buf)
9653 {
9654 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9655 dest = buf;
9656 }
9657 if (readlen > p - buf + 1)
9658 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9659 readlen -= 3 - adjust_prevlen;
9660 prevlen -= adjust_prevlen;
9661 p = dest - 1;
9662 }
9663 }
9664 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009665 } /* for */
9666
9667 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9668 break;
9669 if (start < p)
9670 {
9671 /* There's part of a line in buf, store it in "prev". */
9672 if (p - start + prevlen >= prevsize)
9673 {
9674 /* need bigger "prev" buffer */
9675 char_u *newprev;
9676
9677 /* A common use case is ordinary text files and "prev" gets a
9678 * fragment of a line, so the first allocation is made
9679 * small, to avoid repeatedly 'allocing' large and
9680 * 'reallocing' small. */
9681 if (prevsize == 0)
9682 prevsize = (long)(p - start);
9683 else
9684 {
9685 long grow50pc = (prevsize * 3) / 2;
9686 long growmin = (long)((p - start) * 2 + prevlen);
9687 prevsize = grow50pc > growmin ? grow50pc : growmin;
9688 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009689 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009690 if (newprev == NULL)
9691 {
9692 do_outofmem_msg((long_u)prevsize);
9693 failed = TRUE;
9694 break;
9695 }
9696 prev = newprev;
9697 }
9698 /* Add the line part to end of "prev". */
9699 mch_memmove(prev + prevlen, start, p - start);
9700 prevlen += (long)(p - start);
9701 }
9702 } /* while */
9703
9704 /*
9705 * For a negative line count use only the lines at the end of the file,
9706 * free the rest.
9707 */
9708 if (!failed && maxline < 0)
9709 while (cnt > -maxline)
9710 {
9711 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9712 --cnt;
9713 }
9714
9715 if (failed)
9716 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009717 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009719 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009720 }
9721
9722 vim_free(prev);
9723 fclose(fd);
9724}
9725
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009726 static void
9727return_register(int regname, typval_T *rettv)
9728{
9729 char_u buf[2] = {0, 0};
9730
9731 buf[0] = (char_u)regname;
9732 rettv->v_type = VAR_STRING;
9733 rettv->vval.v_string = vim_strsave(buf);
9734}
9735
9736/*
9737 * "reg_executing()" function
9738 */
9739 static void
9740f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9741{
9742 return_register(reg_executing, rettv);
9743}
9744
9745/*
9746 * "reg_recording()" function
9747 */
9748 static void
9749f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9750{
9751 return_register(reg_recording, rettv);
9752}
9753
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009754#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755/*
9756 * Convert a List to proftime_T.
9757 * Return FAIL when there is something wrong.
9758 */
9759 static int
9760list2proftime(typval_T *arg, proftime_T *tm)
9761{
9762 long n1, n2;
9763 int error = FALSE;
9764
9765 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9766 || arg->vval.v_list->lv_len != 2)
9767 return FAIL;
9768 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9769 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009770# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009771 tm->HighPart = n1;
9772 tm->LowPart = n2;
9773# else
9774 tm->tv_sec = n1;
9775 tm->tv_usec = n2;
9776# endif
9777 return error ? FAIL : OK;
9778}
9779#endif /* FEAT_RELTIME */
9780
9781/*
9782 * "reltime()" function
9783 */
9784 static void
9785f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9786{
9787#ifdef FEAT_RELTIME
9788 proftime_T res;
9789 proftime_T start;
9790
9791 if (argvars[0].v_type == VAR_UNKNOWN)
9792 {
9793 /* No arguments: get current time. */
9794 profile_start(&res);
9795 }
9796 else if (argvars[1].v_type == VAR_UNKNOWN)
9797 {
9798 if (list2proftime(&argvars[0], &res) == FAIL)
9799 return;
9800 profile_end(&res);
9801 }
9802 else
9803 {
9804 /* Two arguments: compute the difference. */
9805 if (list2proftime(&argvars[0], &start) == FAIL
9806 || list2proftime(&argvars[1], &res) == FAIL)
9807 return;
9808 profile_sub(&res, &start);
9809 }
9810
9811 if (rettv_list_alloc(rettv) == OK)
9812 {
9813 long n1, n2;
9814
Bram Moolenaar4f974752019-02-17 17:44:42 +01009815# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009816 n1 = res.HighPart;
9817 n2 = res.LowPart;
9818# else
9819 n1 = res.tv_sec;
9820 n2 = res.tv_usec;
9821# endif
9822 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9823 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9824 }
9825#endif
9826}
9827
9828#ifdef FEAT_FLOAT
9829/*
9830 * "reltimefloat()" function
9831 */
9832 static void
9833f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9834{
9835# ifdef FEAT_RELTIME
9836 proftime_T tm;
9837# endif
9838
9839 rettv->v_type = VAR_FLOAT;
9840 rettv->vval.v_float = 0;
9841# ifdef FEAT_RELTIME
9842 if (list2proftime(&argvars[0], &tm) == OK)
9843 rettv->vval.v_float = profile_float(&tm);
9844# endif
9845}
9846#endif
9847
9848/*
9849 * "reltimestr()" function
9850 */
9851 static void
9852f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9853{
9854#ifdef FEAT_RELTIME
9855 proftime_T tm;
9856#endif
9857
9858 rettv->v_type = VAR_STRING;
9859 rettv->vval.v_string = NULL;
9860#ifdef FEAT_RELTIME
9861 if (list2proftime(&argvars[0], &tm) == OK)
9862 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9863#endif
9864}
9865
9866#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 static void
9868make_connection(void)
9869{
9870 if (X_DISPLAY == NULL
9871# ifdef FEAT_GUI
9872 && !gui.in_use
9873# endif
9874 )
9875 {
9876 x_force_connect = TRUE;
9877 setup_term_clip();
9878 x_force_connect = FALSE;
9879 }
9880}
9881
9882 static int
9883check_connection(void)
9884{
9885 make_connection();
9886 if (X_DISPLAY == NULL)
9887 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009888 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009889 return FAIL;
9890 }
9891 return OK;
9892}
9893#endif
9894
9895#ifdef FEAT_CLIENTSERVER
9896 static void
9897remote_common(typval_T *argvars, typval_T *rettv, int expr)
9898{
9899 char_u *server_name;
9900 char_u *keys;
9901 char_u *r = NULL;
9902 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009903 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009904# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009905 HWND w;
9906# else
9907 Window w;
9908# endif
9909
9910 if (check_restricted() || check_secure())
9911 return;
9912
9913# ifdef FEAT_X11
9914 if (check_connection() == FAIL)
9915 return;
9916# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009917 if (argvars[2].v_type != VAR_UNKNOWN
9918 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009919 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009921 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009922 if (server_name == NULL)
9923 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009924 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009925# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009926 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009928 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9929 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009930# endif
9931 {
9932 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009934 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009935 vim_free(r);
9936 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009938 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 return;
9940 }
9941
9942 rettv->vval.v_string = r;
9943
9944 if (argvars[2].v_type != VAR_UNKNOWN)
9945 {
9946 dictitem_T v;
9947 char_u str[30];
9948 char_u *idvar;
9949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009950 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009951 if (idvar != NULL && *idvar != NUL)
9952 {
9953 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9954 v.di_tv.v_type = VAR_STRING;
9955 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009957 vim_free(v.di_tv.vval.v_string);
9958 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009959 }
9960}
9961#endif
9962
9963/*
9964 * "remote_expr()" function
9965 */
9966 static void
9967f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9968{
9969 rettv->v_type = VAR_STRING;
9970 rettv->vval.v_string = NULL;
9971#ifdef FEAT_CLIENTSERVER
9972 remote_common(argvars, rettv, TRUE);
9973#endif
9974}
9975
9976/*
9977 * "remote_foreground()" function
9978 */
9979 static void
9980f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9981{
9982#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009983# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009984 /* On Win32 it's done in this application. */
9985 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009986 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009987
9988 if (server_name != NULL)
9989 serverForeground(server_name);
9990 }
9991# else
9992 /* Send a foreground() expression to the server. */
9993 argvars[1].v_type = VAR_STRING;
9994 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9995 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009996 rettv->v_type = VAR_STRING;
9997 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009998 remote_common(argvars, rettv, TRUE);
9999 vim_free(argvars[1].vval.v_string);
10000# endif
10001#endif
10002}
10003
10004 static void
10005f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10006{
10007#ifdef FEAT_CLIENTSERVER
10008 dictitem_T v;
10009 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010010# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 long_u n = 0;
10012# endif
10013 char_u *serverid;
10014
10015 if (check_restricted() || check_secure())
10016 {
10017 rettv->vval.v_number = -1;
10018 return;
10019 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010020 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 if (serverid == NULL)
10022 {
10023 rettv->vval.v_number = -1;
10024 return; /* type error; errmsg already given */
10025 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010026# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10028 if (n == 0)
10029 rettv->vval.v_number = -1;
10030 else
10031 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010032 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010033 rettv->vval.v_number = (s != NULL);
10034 }
10035# else
10036 if (check_connection() == FAIL)
10037 return;
10038
10039 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10040 serverStrToWin(serverid), &s);
10041# endif
10042
10043 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10044 {
10045 char_u *retvar;
10046
10047 v.di_tv.v_type = VAR_STRING;
10048 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010049 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010050 if (retvar != NULL)
10051 set_var(retvar, &v.di_tv, FALSE);
10052 vim_free(v.di_tv.vval.v_string);
10053 }
10054#else
10055 rettv->vval.v_number = -1;
10056#endif
10057}
10058
10059 static void
10060f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10061{
10062 char_u *r = NULL;
10063
10064#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010065 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010066
10067 if (serverid != NULL && !check_restricted() && !check_secure())
10068 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010069 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010070# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010071 /* The server's HWND is encoded in the 'id' parameter */
10072 long_u n = 0;
10073# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010074
10075 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010076 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010077
Bram Moolenaar4f974752019-02-17 17:44:42 +010010078# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010079 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10080 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010081 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010082 if (r == NULL)
10083# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010084 if (check_connection() == FAIL
10085 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10086 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010087# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010088 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089 }
10090#endif
10091 rettv->v_type = VAR_STRING;
10092 rettv->vval.v_string = r;
10093}
10094
10095/*
10096 * "remote_send()" function
10097 */
10098 static void
10099f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10100{
10101 rettv->v_type = VAR_STRING;
10102 rettv->vval.v_string = NULL;
10103#ifdef FEAT_CLIENTSERVER
10104 remote_common(argvars, rettv, FALSE);
10105#endif
10106}
10107
10108/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010109 * "remote_startserver()" function
10110 */
10111 static void
10112f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10113{
10114#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010115 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010116
10117 if (server == NULL)
10118 return; /* type error; errmsg already given */
10119 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010120 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010121 else
10122 {
10123# ifdef FEAT_X11
10124 if (check_connection() == OK)
10125 serverRegisterName(X_DISPLAY, server);
10126# else
10127 serverSetName(server);
10128# endif
10129 }
10130#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010131 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010132#endif
10133}
10134
10135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010136 * "remove()" function
10137 */
10138 static void
10139f_remove(typval_T *argvars, typval_T *rettv)
10140{
10141 list_T *l;
10142 listitem_T *item, *item2;
10143 listitem_T *li;
10144 long idx;
10145 long end;
10146 char_u *key;
10147 dict_T *d;
10148 dictitem_T *di;
10149 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010150 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010151
10152 if (argvars[0].v_type == VAR_DICT)
10153 {
10154 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010155 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010156 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010157 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010159 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 if (key != NULL)
10161 {
10162 di = dict_find(d, key, -1);
10163 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010164 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10166 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10167 {
10168 *rettv = di->di_tv;
10169 init_tv(&di->di_tv);
10170 dictitem_remove(d, di);
10171 }
10172 }
10173 }
10174 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010175 else if (argvars[0].v_type == VAR_BLOB)
10176 {
10177 idx = (long)tv_get_number_chk(&argvars[1], &error);
10178 if (!error)
10179 {
10180 blob_T *b = argvars[0].vval.v_blob;
10181 int len = blob_len(b);
10182 char_u *p;
10183
10184 if (idx < 0)
10185 // count from the end
10186 idx = len + idx;
10187 if (idx < 0 || idx >= len)
10188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010189 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010190 return;
10191 }
10192 if (argvars[2].v_type == VAR_UNKNOWN)
10193 {
10194 // Remove one item, return its value.
10195 p = (char_u *)b->bv_ga.ga_data;
10196 rettv->vval.v_number = (varnumber_T) *(p + idx);
10197 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10198 --b->bv_ga.ga_len;
10199 }
10200 else
10201 {
10202 blob_T *blob;
10203
10204 // Remove range of items, return list with values.
10205 end = (long)tv_get_number_chk(&argvars[2], &error);
10206 if (error)
10207 return;
10208 if (end < 0)
10209 // count from the end
10210 end = len + end;
10211 if (end >= len || idx > end)
10212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010213 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010214 return;
10215 }
10216 blob = blob_alloc();
10217 if (blob == NULL)
10218 return;
10219 blob->bv_ga.ga_len = end - idx + 1;
10220 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10221 {
10222 vim_free(blob);
10223 return;
10224 }
10225 p = (char_u *)b->bv_ga.ga_data;
10226 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10227 (size_t)(end - idx + 1));
10228 ++blob->bv_refcount;
10229 rettv->v_type = VAR_BLOB;
10230 rettv->vval.v_blob = blob;
10231
10232 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10233 b->bv_ga.ga_len -= end - idx + 1;
10234 }
10235 }
10236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010238 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010239 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010240 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010242 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010244 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010245 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010246 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 else
10248 {
10249 if (argvars[2].v_type == VAR_UNKNOWN)
10250 {
10251 /* Remove one item, return its value. */
10252 vimlist_remove(l, item, item);
10253 *rettv = item->li_tv;
10254 vim_free(item);
10255 }
10256 else
10257 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010258 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010259 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010260 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010261 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010262 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010263 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 else
10265 {
10266 int cnt = 0;
10267
10268 for (li = item; li != NULL; li = li->li_next)
10269 {
10270 ++cnt;
10271 if (li == item2)
10272 break;
10273 }
10274 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010275 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010276 else
10277 {
10278 vimlist_remove(l, item, item2);
10279 if (rettv_list_alloc(rettv) == OK)
10280 {
10281 l = rettv->vval.v_list;
10282 l->lv_first = item;
10283 l->lv_last = item2;
10284 item->li_prev = NULL;
10285 item2->li_next = NULL;
10286 l->lv_len = cnt;
10287 }
10288 }
10289 }
10290 }
10291 }
10292 }
10293}
10294
10295/*
10296 * "rename({from}, {to})" function
10297 */
10298 static void
10299f_rename(typval_T *argvars, typval_T *rettv)
10300{
10301 char_u buf[NUMBUFLEN];
10302
10303 if (check_restricted() || check_secure())
10304 rettv->vval.v_number = -1;
10305 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010306 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10307 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010308}
10309
10310/*
10311 * "repeat()" function
10312 */
10313 static void
10314f_repeat(typval_T *argvars, typval_T *rettv)
10315{
10316 char_u *p;
10317 int n;
10318 int slen;
10319 int len;
10320 char_u *r;
10321 int i;
10322
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010323 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010324 if (argvars[0].v_type == VAR_LIST)
10325 {
10326 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10327 while (n-- > 0)
10328 if (list_extend(rettv->vval.v_list,
10329 argvars[0].vval.v_list, NULL) == FAIL)
10330 break;
10331 }
10332 else
10333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010334 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010335 rettv->v_type = VAR_STRING;
10336 rettv->vval.v_string = NULL;
10337
10338 slen = (int)STRLEN(p);
10339 len = slen * n;
10340 if (len <= 0)
10341 return;
10342
10343 r = alloc(len + 1);
10344 if (r != NULL)
10345 {
10346 for (i = 0; i < n; i++)
10347 mch_memmove(r + i * slen, p, (size_t)slen);
10348 r[len] = NUL;
10349 }
10350
10351 rettv->vval.v_string = r;
10352 }
10353}
10354
10355/*
10356 * "resolve()" function
10357 */
10358 static void
10359f_resolve(typval_T *argvars, typval_T *rettv)
10360{
10361 char_u *p;
10362#ifdef HAVE_READLINK
10363 char_u *buf = NULL;
10364#endif
10365
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010366 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010367#ifdef FEAT_SHORTCUT
10368 {
10369 char_u *v = NULL;
10370
Bram Moolenaardce1e892019-02-10 23:18:53 +010010371 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010372 if (v != NULL)
10373 rettv->vval.v_string = v;
10374 else
10375 rettv->vval.v_string = vim_strsave(p);
10376 }
10377#else
10378# ifdef HAVE_READLINK
10379 {
10380 char_u *cpy;
10381 int len;
10382 char_u *remain = NULL;
10383 char_u *q;
10384 int is_relative_to_current = FALSE;
10385 int has_trailing_pathsep = FALSE;
10386 int limit = 100;
10387
10388 p = vim_strsave(p);
10389
10390 if (p[0] == '.' && (vim_ispathsep(p[1])
10391 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10392 is_relative_to_current = TRUE;
10393
10394 len = STRLEN(p);
10395 if (len > 0 && after_pathsep(p, p + len))
10396 {
10397 has_trailing_pathsep = TRUE;
10398 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10399 }
10400
10401 q = getnextcomp(p);
10402 if (*q != NUL)
10403 {
10404 /* Separate the first path component in "p", and keep the
10405 * remainder (beginning with the path separator). */
10406 remain = vim_strsave(q - 1);
10407 q[-1] = NUL;
10408 }
10409
10410 buf = alloc(MAXPATHL + 1);
10411 if (buf == NULL)
10412 goto fail;
10413
10414 for (;;)
10415 {
10416 for (;;)
10417 {
10418 len = readlink((char *)p, (char *)buf, MAXPATHL);
10419 if (len <= 0)
10420 break;
10421 buf[len] = NUL;
10422
10423 if (limit-- == 0)
10424 {
10425 vim_free(p);
10426 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010427 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 rettv->vval.v_string = NULL;
10429 goto fail;
10430 }
10431
10432 /* Ensure that the result will have a trailing path separator
10433 * if the argument has one. */
10434 if (remain == NULL && has_trailing_pathsep)
10435 add_pathsep(buf);
10436
10437 /* Separate the first path component in the link value and
10438 * concatenate the remainders. */
10439 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10440 if (*q != NUL)
10441 {
10442 if (remain == NULL)
10443 remain = vim_strsave(q - 1);
10444 else
10445 {
10446 cpy = concat_str(q - 1, remain);
10447 if (cpy != NULL)
10448 {
10449 vim_free(remain);
10450 remain = cpy;
10451 }
10452 }
10453 q[-1] = NUL;
10454 }
10455
10456 q = gettail(p);
10457 if (q > p && *q == NUL)
10458 {
10459 /* Ignore trailing path separator. */
10460 q[-1] = NUL;
10461 q = gettail(p);
10462 }
10463 if (q > p && !mch_isFullName(buf))
10464 {
10465 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010466 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010467 if (cpy != NULL)
10468 {
10469 STRCPY(cpy, p);
10470 STRCPY(gettail(cpy), buf);
10471 vim_free(p);
10472 p = cpy;
10473 }
10474 }
10475 else
10476 {
10477 vim_free(p);
10478 p = vim_strsave(buf);
10479 }
10480 }
10481
10482 if (remain == NULL)
10483 break;
10484
10485 /* Append the first path component of "remain" to "p". */
10486 q = getnextcomp(remain + 1);
10487 len = q - remain - (*q != NUL);
10488 cpy = vim_strnsave(p, STRLEN(p) + len);
10489 if (cpy != NULL)
10490 {
10491 STRNCAT(cpy, remain, len);
10492 vim_free(p);
10493 p = cpy;
10494 }
10495 /* Shorten "remain". */
10496 if (*q != NUL)
10497 STRMOVE(remain, q - 1);
10498 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010499 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010500 }
10501
10502 /* If the result is a relative path name, make it explicitly relative to
10503 * the current directory if and only if the argument had this form. */
10504 if (!vim_ispathsep(*p))
10505 {
10506 if (is_relative_to_current
10507 && *p != NUL
10508 && !(p[0] == '.'
10509 && (p[1] == NUL
10510 || vim_ispathsep(p[1])
10511 || (p[1] == '.'
10512 && (p[2] == NUL
10513 || vim_ispathsep(p[2]))))))
10514 {
10515 /* Prepend "./". */
10516 cpy = concat_str((char_u *)"./", p);
10517 if (cpy != NULL)
10518 {
10519 vim_free(p);
10520 p = cpy;
10521 }
10522 }
10523 else if (!is_relative_to_current)
10524 {
10525 /* Strip leading "./". */
10526 q = p;
10527 while (q[0] == '.' && vim_ispathsep(q[1]))
10528 q += 2;
10529 if (q > p)
10530 STRMOVE(p, p + 2);
10531 }
10532 }
10533
10534 /* Ensure that the result will have no trailing path separator
10535 * if the argument had none. But keep "/" or "//". */
10536 if (!has_trailing_pathsep)
10537 {
10538 q = p + STRLEN(p);
10539 if (after_pathsep(p, q))
10540 *gettail_sep(p) = NUL;
10541 }
10542
10543 rettv->vval.v_string = p;
10544 }
10545# else
10546 rettv->vval.v_string = vim_strsave(p);
10547# endif
10548#endif
10549
10550 simplify_filename(rettv->vval.v_string);
10551
10552#ifdef HAVE_READLINK
10553fail:
10554 vim_free(buf);
10555#endif
10556 rettv->v_type = VAR_STRING;
10557}
10558
10559/*
10560 * "reverse({list})" function
10561 */
10562 static void
10563f_reverse(typval_T *argvars, typval_T *rettv)
10564{
10565 list_T *l;
10566 listitem_T *li, *ni;
10567
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010568 if (argvars[0].v_type == VAR_BLOB)
10569 {
10570 blob_T *b = argvars[0].vval.v_blob;
10571 int i, len = blob_len(b);
10572
10573 for (i = 0; i < len / 2; i++)
10574 {
10575 int tmp = blob_get(b, i);
10576
10577 blob_set(b, i, blob_get(b, len - i - 1));
10578 blob_set(b, len - i - 1, tmp);
10579 }
10580 rettv_blob_set(rettv, b);
10581 return;
10582 }
10583
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010584 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010585 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010586 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010587 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 (char_u *)N_("reverse() argument"), TRUE))
10589 {
10590 li = l->lv_last;
10591 l->lv_first = l->lv_last = NULL;
10592 l->lv_len = 0;
10593 while (li != NULL)
10594 {
10595 ni = li->li_prev;
10596 list_append(l, li);
10597 li = ni;
10598 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010599 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010600 l->lv_idx = l->lv_len - l->lv_idx - 1;
10601 }
10602}
10603
10604#define SP_NOMOVE 0x01 /* don't move cursor */
10605#define SP_REPEAT 0x02 /* repeat to find outer pair */
10606#define SP_RETCOUNT 0x04 /* return matchcount */
10607#define SP_SETPCMARK 0x08 /* set previous context mark */
10608#define SP_START 0x10 /* accept match at start position */
10609#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10610#define SP_END 0x40 /* leave cursor at end of match */
10611#define SP_COLUMN 0x80 /* start at cursor column */
10612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010613/*
10614 * Get flags for a search function.
10615 * Possibly sets "p_ws".
10616 * Returns BACKWARD, FORWARD or zero (for an error).
10617 */
10618 static int
10619get_search_arg(typval_T *varp, int *flagsp)
10620{
10621 int dir = FORWARD;
10622 char_u *flags;
10623 char_u nbuf[NUMBUFLEN];
10624 int mask;
10625
10626 if (varp->v_type != VAR_UNKNOWN)
10627 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010628 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010629 if (flags == NULL)
10630 return 0; /* type error; errmsg already given */
10631 while (*flags != NUL)
10632 {
10633 switch (*flags)
10634 {
10635 case 'b': dir = BACKWARD; break;
10636 case 'w': p_ws = TRUE; break;
10637 case 'W': p_ws = FALSE; break;
10638 default: mask = 0;
10639 if (flagsp != NULL)
10640 switch (*flags)
10641 {
10642 case 'c': mask = SP_START; break;
10643 case 'e': mask = SP_END; break;
10644 case 'm': mask = SP_RETCOUNT; break;
10645 case 'n': mask = SP_NOMOVE; break;
10646 case 'p': mask = SP_SUBPAT; break;
10647 case 'r': mask = SP_REPEAT; break;
10648 case 's': mask = SP_SETPCMARK; break;
10649 case 'z': mask = SP_COLUMN; break;
10650 }
10651 if (mask == 0)
10652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010653 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 dir = 0;
10655 }
10656 else
10657 *flagsp |= mask;
10658 }
10659 if (dir == 0)
10660 break;
10661 ++flags;
10662 }
10663 }
10664 return dir;
10665}
10666
10667/*
10668 * Shared by search() and searchpos() functions.
10669 */
10670 static int
10671search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10672{
10673 int flags;
10674 char_u *pat;
10675 pos_T pos;
10676 pos_T save_cursor;
10677 int save_p_ws = p_ws;
10678 int dir;
10679 int retval = 0; /* default: FAIL */
10680 long lnum_stop = 0;
10681 proftime_T tm;
10682#ifdef FEAT_RELTIME
10683 long time_limit = 0;
10684#endif
10685 int options = SEARCH_KEEP;
10686 int subpatnum;
10687
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010688 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010689 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10690 if (dir == 0)
10691 goto theend;
10692 flags = *flagsp;
10693 if (flags & SP_START)
10694 options |= SEARCH_START;
10695 if (flags & SP_END)
10696 options |= SEARCH_END;
10697 if (flags & SP_COLUMN)
10698 options |= SEARCH_COL;
10699
10700 /* Optional arguments: line number to stop searching and timeout. */
10701 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010703 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010704 if (lnum_stop < 0)
10705 goto theend;
10706#ifdef FEAT_RELTIME
10707 if (argvars[3].v_type != VAR_UNKNOWN)
10708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010709 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010710 if (time_limit < 0)
10711 goto theend;
10712 }
10713#endif
10714 }
10715
10716#ifdef FEAT_RELTIME
10717 /* Set the time limit, if there is one. */
10718 profile_setlimit(time_limit, &tm);
10719#endif
10720
10721 /*
10722 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10723 * Check to make sure only those flags are set.
10724 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10725 * flags cannot be set. Check for that condition also.
10726 */
10727 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10728 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010730 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010731 goto theend;
10732 }
10733
10734 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010735 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010736 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 if (subpatnum != FAIL)
10738 {
10739 if (flags & SP_SUBPAT)
10740 retval = subpatnum;
10741 else
10742 retval = pos.lnum;
10743 if (flags & SP_SETPCMARK)
10744 setpcmark();
10745 curwin->w_cursor = pos;
10746 if (match_pos != NULL)
10747 {
10748 /* Store the match cursor position */
10749 match_pos->lnum = pos.lnum;
10750 match_pos->col = pos.col + 1;
10751 }
10752 /* "/$" will put the cursor after the end of the line, may need to
10753 * correct that here */
10754 check_cursor();
10755 }
10756
10757 /* If 'n' flag is used: restore cursor position. */
10758 if (flags & SP_NOMOVE)
10759 curwin->w_cursor = save_cursor;
10760 else
10761 curwin->w_set_curswant = TRUE;
10762theend:
10763 p_ws = save_p_ws;
10764
10765 return retval;
10766}
10767
10768#ifdef FEAT_FLOAT
10769
10770/*
10771 * round() is not in C90, use ceil() or floor() instead.
10772 */
10773 float_T
10774vim_round(float_T f)
10775{
10776 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10777}
10778
10779/*
10780 * "round({float})" function
10781 */
10782 static void
10783f_round(typval_T *argvars, typval_T *rettv)
10784{
10785 float_T f = 0.0;
10786
10787 rettv->v_type = VAR_FLOAT;
10788 if (get_float_arg(argvars, &f) == OK)
10789 rettv->vval.v_float = vim_round(f);
10790 else
10791 rettv->vval.v_float = 0.0;
10792}
10793#endif
10794
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010795#ifdef FEAT_RUBY
10796/*
10797 * "rubyeval()" function
10798 */
10799 static void
10800f_rubyeval(typval_T *argvars, typval_T *rettv)
10801{
10802 char_u *str;
10803 char_u buf[NUMBUFLEN];
10804
10805 str = tv_get_string_buf(&argvars[0], buf);
10806 do_rubyeval(str, rettv);
10807}
10808#endif
10809
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010810/*
10811 * "screenattr()" function
10812 */
10813 static void
10814f_screenattr(typval_T *argvars, typval_T *rettv)
10815{
10816 int row;
10817 int col;
10818 int c;
10819
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010820 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10821 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010822 if (row < 0 || row >= screen_Rows
10823 || col < 0 || col >= screen_Columns)
10824 c = -1;
10825 else
10826 c = ScreenAttrs[LineOffset[row] + col];
10827 rettv->vval.v_number = c;
10828}
10829
10830/*
10831 * "screenchar()" function
10832 */
10833 static void
10834f_screenchar(typval_T *argvars, typval_T *rettv)
10835{
10836 int row;
10837 int col;
10838 int off;
10839 int c;
10840
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010841 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10842 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010843 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010844 c = -1;
10845 else
10846 {
10847 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 if (enc_utf8 && ScreenLinesUC[off] != 0)
10849 c = ScreenLinesUC[off];
10850 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010851 c = ScreenLines[off];
10852 }
10853 rettv->vval.v_number = c;
10854}
10855
10856/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010857 * "screenchars()" function
10858 */
10859 static void
10860f_screenchars(typval_T *argvars, typval_T *rettv)
10861{
10862 int row;
10863 int col;
10864 int off;
10865 int c;
10866 int i;
10867
10868 if (rettv_list_alloc(rettv) == FAIL)
10869 return;
10870 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10871 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10872 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10873 return;
10874
10875 off = LineOffset[row] + col;
10876 if (enc_utf8 && ScreenLinesUC[off] != 0)
10877 c = ScreenLinesUC[off];
10878 else
10879 c = ScreenLines[off];
10880 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10881
10882 if (enc_utf8)
10883
10884 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10885 list_append_number(rettv->vval.v_list,
10886 (varnumber_T)ScreenLinesC[i][off]);
10887}
10888
10889/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010890 * "screencol()" function
10891 *
10892 * First column is 1 to be consistent with virtcol().
10893 */
10894 static void
10895f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10896{
10897 rettv->vval.v_number = screen_screencol() + 1;
10898}
10899
10900/*
10901 * "screenrow()" function
10902 */
10903 static void
10904f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10905{
10906 rettv->vval.v_number = screen_screenrow() + 1;
10907}
10908
10909/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010910 * "screenstring()" function
10911 */
10912 static void
10913f_screenstring(typval_T *argvars, typval_T *rettv)
10914{
10915 int row;
10916 int col;
10917 int off;
10918 int c;
10919 int i;
10920 char_u buf[MB_MAXBYTES + 1];
10921 int buflen = 0;
10922
10923 rettv->vval.v_string = NULL;
10924 rettv->v_type = VAR_STRING;
10925
10926 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10927 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10928 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10929 return;
10930
10931 off = LineOffset[row] + col;
10932 if (enc_utf8 && ScreenLinesUC[off] != 0)
10933 c = ScreenLinesUC[off];
10934 else
10935 c = ScreenLines[off];
10936 buflen += mb_char2bytes(c, buf);
10937
10938 if (enc_utf8)
10939 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10940 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10941
10942 buf[buflen] = NUL;
10943 rettv->vval.v_string = vim_strsave(buf);
10944}
10945
10946/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010947 * "search()" function
10948 */
10949 static void
10950f_search(typval_T *argvars, typval_T *rettv)
10951{
10952 int flags = 0;
10953
10954 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10955}
10956
10957/*
10958 * "searchdecl()" function
10959 */
10960 static void
10961f_searchdecl(typval_T *argvars, typval_T *rettv)
10962{
10963 int locally = 1;
10964 int thisblock = 0;
10965 int error = FALSE;
10966 char_u *name;
10967
10968 rettv->vval.v_number = 1; /* default: FAIL */
10969
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010970 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 if (argvars[1].v_type != VAR_UNKNOWN)
10972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010973 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010974 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010975 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010976 }
10977 if (!error && name != NULL)
10978 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10979 locally, thisblock, SEARCH_KEEP) == FAIL;
10980}
10981
10982/*
10983 * Used by searchpair() and searchpairpos()
10984 */
10985 static int
10986searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10987{
10988 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010989 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990 int save_p_ws = p_ws;
10991 int dir;
10992 int flags = 0;
10993 char_u nbuf1[NUMBUFLEN];
10994 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010995 int retval = 0; /* default: FAIL */
10996 long lnum_stop = 0;
10997 long time_limit = 0;
10998
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010999 /* Get the three pattern arguments: start, middle, end. Will result in an
11000 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011001 spat = tv_get_string_chk(&argvars[0]);
11002 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11003 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 if (spat == NULL || mpat == NULL || epat == NULL)
11005 goto theend; /* type error */
11006
11007 /* Handle the optional fourth argument: flags */
11008 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11009 if (dir == 0)
11010 goto theend;
11011
11012 /* Don't accept SP_END or SP_SUBPAT.
11013 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11014 */
11015 if ((flags & (SP_END | SP_SUBPAT)) != 0
11016 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011018 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 goto theend;
11020 }
11021
11022 /* Using 'r' implies 'W', otherwise it doesn't work. */
11023 if (flags & SP_REPEAT)
11024 p_ws = FALSE;
11025
11026 /* Optional fifth argument: skip expression */
11027 if (argvars[3].v_type == VAR_UNKNOWN
11028 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011029 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011030 else
11031 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011032 skip = &argvars[4];
11033 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11034 && skip->v_type != VAR_STRING)
11035 {
11036 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011037 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011038 goto theend;
11039 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 if (argvars[5].v_type != VAR_UNKNOWN)
11041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011042 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011045 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011047 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011048#ifdef FEAT_RELTIME
11049 if (argvars[6].v_type != VAR_UNKNOWN)
11050 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011051 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011054 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011056 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011057 }
11058#endif
11059 }
11060 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061
11062 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11063 match_pos, lnum_stop, time_limit);
11064
11065theend:
11066 p_ws = save_p_ws;
11067
11068 return retval;
11069}
11070
11071/*
11072 * "searchpair()" function
11073 */
11074 static void
11075f_searchpair(typval_T *argvars, typval_T *rettv)
11076{
11077 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11078}
11079
11080/*
11081 * "searchpairpos()" function
11082 */
11083 static void
11084f_searchpairpos(typval_T *argvars, typval_T *rettv)
11085{
11086 pos_T match_pos;
11087 int lnum = 0;
11088 int col = 0;
11089
11090 if (rettv_list_alloc(rettv) == FAIL)
11091 return;
11092
11093 if (searchpair_cmn(argvars, &match_pos) > 0)
11094 {
11095 lnum = match_pos.lnum;
11096 col = match_pos.col;
11097 }
11098
11099 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11100 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11101}
11102
11103/*
11104 * Search for a start/middle/end thing.
11105 * Used by searchpair(), see its documentation for the details.
11106 * Returns 0 or -1 for no match,
11107 */
11108 long
11109do_searchpair(
11110 char_u *spat, /* start pattern */
11111 char_u *mpat, /* middle pattern */
11112 char_u *epat, /* end pattern */
11113 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011114 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011115 int flags, /* SP_SETPCMARK and other SP_ values */
11116 pos_T *match_pos,
11117 linenr_T lnum_stop, /* stop at this line if not zero */
11118 long time_limit UNUSED) /* stop after this many msec */
11119{
11120 char_u *save_cpo;
11121 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11122 long retval = 0;
11123 pos_T pos;
11124 pos_T firstpos;
11125 pos_T foundpos;
11126 pos_T save_cursor;
11127 pos_T save_pos;
11128 int n;
11129 int r;
11130 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011131 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132 int err;
11133 int options = SEARCH_KEEP;
11134 proftime_T tm;
11135
11136 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11137 save_cpo = p_cpo;
11138 p_cpo = empty_option;
11139
11140#ifdef FEAT_RELTIME
11141 /* Set the time limit, if there is one. */
11142 profile_setlimit(time_limit, &tm);
11143#endif
11144
11145 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11146 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011147 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11148 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011149 if (pat2 == NULL || pat3 == NULL)
11150 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011151 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011152 if (*mpat == NUL)
11153 STRCPY(pat3, pat2);
11154 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011155 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156 spat, epat, mpat);
11157 if (flags & SP_START)
11158 options |= SEARCH_START;
11159
Bram Moolenaar48570482017-10-30 21:48:41 +010011160 if (skip != NULL)
11161 {
11162 /* Empty string means to not use the skip expression. */
11163 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11164 use_skip = skip->vval.v_string != NULL
11165 && *skip->vval.v_string != NUL;
11166 }
11167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011168 save_cursor = curwin->w_cursor;
11169 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011170 CLEAR_POS(&firstpos);
11171 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011172 pat = pat3;
11173 for (;;)
11174 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011175 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011176 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011177 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 /* didn't find it or found the first match again: FAIL */
11179 break;
11180
11181 if (firstpos.lnum == 0)
11182 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011183 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184 {
11185 /* Found the same position again. Can happen with a pattern that
11186 * has "\zs" at the end and searching backwards. Advance one
11187 * character and try again. */
11188 if (dir == BACKWARD)
11189 decl(&pos);
11190 else
11191 incl(&pos);
11192 }
11193 foundpos = pos;
11194
11195 /* clear the start flag to avoid getting stuck here */
11196 options &= ~SEARCH_START;
11197
11198 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011199 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011200 {
11201 save_pos = curwin->w_cursor;
11202 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011203 err = FALSE;
11204 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011205 curwin->w_cursor = save_pos;
11206 if (err)
11207 {
11208 /* Evaluating {skip} caused an error, break here. */
11209 curwin->w_cursor = save_cursor;
11210 retval = -1;
11211 break;
11212 }
11213 if (r)
11214 continue;
11215 }
11216
11217 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11218 {
11219 /* Found end when searching backwards or start when searching
11220 * forward: nested pair. */
11221 ++nest;
11222 pat = pat2; /* nested, don't search for middle */
11223 }
11224 else
11225 {
11226 /* Found end when searching forward or start when searching
11227 * backward: end of (nested) pair; or found middle in outer pair. */
11228 if (--nest == 1)
11229 pat = pat3; /* outer level, search for middle */
11230 }
11231
11232 if (nest == 0)
11233 {
11234 /* Found the match: return matchcount or line number. */
11235 if (flags & SP_RETCOUNT)
11236 ++retval;
11237 else
11238 retval = pos.lnum;
11239 if (flags & SP_SETPCMARK)
11240 setpcmark();
11241 curwin->w_cursor = pos;
11242 if (!(flags & SP_REPEAT))
11243 break;
11244 nest = 1; /* search for next unmatched */
11245 }
11246 }
11247
11248 if (match_pos != NULL)
11249 {
11250 /* Store the match cursor position */
11251 match_pos->lnum = curwin->w_cursor.lnum;
11252 match_pos->col = curwin->w_cursor.col + 1;
11253 }
11254
11255 /* If 'n' flag is used or search failed: restore cursor position. */
11256 if ((flags & SP_NOMOVE) || retval == 0)
11257 curwin->w_cursor = save_cursor;
11258
11259theend:
11260 vim_free(pat2);
11261 vim_free(pat3);
11262 if (p_cpo == empty_option)
11263 p_cpo = save_cpo;
11264 else
11265 /* Darn, evaluating the {skip} expression changed the value. */
11266 free_string_option(save_cpo);
11267
11268 return retval;
11269}
11270
11271/*
11272 * "searchpos()" function
11273 */
11274 static void
11275f_searchpos(typval_T *argvars, typval_T *rettv)
11276{
11277 pos_T match_pos;
11278 int lnum = 0;
11279 int col = 0;
11280 int n;
11281 int flags = 0;
11282
11283 if (rettv_list_alloc(rettv) == FAIL)
11284 return;
11285
11286 n = search_cmn(argvars, &match_pos, &flags);
11287 if (n > 0)
11288 {
11289 lnum = match_pos.lnum;
11290 col = match_pos.col;
11291 }
11292
11293 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11294 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11295 if (flags & SP_SUBPAT)
11296 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11297}
11298
11299 static void
11300f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11301{
11302#ifdef FEAT_CLIENTSERVER
11303 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011304 char_u *server = tv_get_string_chk(&argvars[0]);
11305 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306
11307 rettv->vval.v_number = -1;
11308 if (server == NULL || reply == NULL)
11309 return;
11310 if (check_restricted() || check_secure())
11311 return;
11312# ifdef FEAT_X11
11313 if (check_connection() == FAIL)
11314 return;
11315# endif
11316
11317 if (serverSendReply(server, reply) < 0)
11318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011319 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011320 return;
11321 }
11322 rettv->vval.v_number = 0;
11323#else
11324 rettv->vval.v_number = -1;
11325#endif
11326}
11327
11328 static void
11329f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11330{
11331 char_u *r = NULL;
11332
11333#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011334# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011335 r = serverGetVimNames();
11336# else
11337 make_connection();
11338 if (X_DISPLAY != NULL)
11339 r = serverGetVimNames(X_DISPLAY);
11340# endif
11341#endif
11342 rettv->v_type = VAR_STRING;
11343 rettv->vval.v_string = r;
11344}
11345
11346/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011347 * "setbufline()" function
11348 */
11349 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011350f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011351{
11352 linenr_T lnum;
11353 buf_T *buf;
11354
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011355 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011356 if (buf == NULL)
11357 rettv->vval.v_number = 1; /* FAIL */
11358 else
11359 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011360 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011361 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011362 }
11363}
11364
11365/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 * "setbufvar()" function
11367 */
11368 static void
11369f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11370{
11371 buf_T *buf;
11372 char_u *varname, *bufvarname;
11373 typval_T *varp;
11374 char_u nbuf[NUMBUFLEN];
11375
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011376 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011378 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11379 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011380 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 varp = &argvars[2];
11382
11383 if (buf != NULL && varname != NULL && varp != NULL)
11384 {
11385 if (*varname == '&')
11386 {
11387 long numval;
11388 char_u *strval;
11389 int error = FALSE;
11390 aco_save_T aco;
11391
11392 /* set curbuf to be our buf, temporarily */
11393 aucmd_prepbuf(&aco, buf);
11394
11395 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011396 numval = (long)tv_get_number_chk(varp, &error);
11397 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011398 if (!error && strval != NULL)
11399 set_option_value(varname, numval, strval, OPT_LOCAL);
11400
11401 /* reset notion of buffer */
11402 aucmd_restbuf(&aco);
11403 }
11404 else
11405 {
11406 buf_T *save_curbuf = curbuf;
11407
Bram Moolenaar964b3742019-05-24 18:54:09 +020011408 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011409 if (bufvarname != NULL)
11410 {
11411 curbuf = buf;
11412 STRCPY(bufvarname, "b:");
11413 STRCPY(bufvarname + 2, varname);
11414 set_var(bufvarname, varp, TRUE);
11415 vim_free(bufvarname);
11416 curbuf = save_curbuf;
11417 }
11418 }
11419 }
11420}
11421
11422 static void
11423f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11424{
11425 dict_T *d;
11426 dictitem_T *di;
11427 char_u *csearch;
11428
11429 if (argvars[0].v_type != VAR_DICT)
11430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011431 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432 return;
11433 }
11434
11435 if ((d = argvars[0].vval.v_dict) != NULL)
11436 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011437 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 if (csearch != NULL)
11439 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011440 if (enc_utf8)
11441 {
11442 int pcc[MAX_MCO];
11443 int c = utfc_ptr2char(csearch, pcc);
11444
11445 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11446 }
11447 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011448 set_last_csearch(PTR2CHAR(csearch),
11449 csearch, MB_PTR2LEN(csearch));
11450 }
11451
11452 di = dict_find(d, (char_u *)"forward", -1);
11453 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011454 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011455 ? FORWARD : BACKWARD);
11456
11457 di = dict_find(d, (char_u *)"until", -1);
11458 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011459 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 }
11461}
11462
11463/*
11464 * "setcmdpos()" function
11465 */
11466 static void
11467f_setcmdpos(typval_T *argvars, typval_T *rettv)
11468{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011469 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011470
11471 if (pos >= 0)
11472 rettv->vval.v_number = set_cmdline_pos(pos);
11473}
11474
11475/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011476 * "setenv()" function
11477 */
11478 static void
11479f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11480{
11481 char_u namebuf[NUMBUFLEN];
11482 char_u valbuf[NUMBUFLEN];
11483 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11484
11485 if (argvars[1].v_type == VAR_SPECIAL
11486 && argvars[1].vval.v_number == VVAL_NULL)
11487 vim_unsetenv(name);
11488 else
11489 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11490}
11491
11492/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 * "setfperm({fname}, {mode})" function
11494 */
11495 static void
11496f_setfperm(typval_T *argvars, typval_T *rettv)
11497{
11498 char_u *fname;
11499 char_u modebuf[NUMBUFLEN];
11500 char_u *mode_str;
11501 int i;
11502 int mask;
11503 int mode = 0;
11504
11505 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011506 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011507 if (fname == NULL)
11508 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011509 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011510 if (mode_str == NULL)
11511 return;
11512 if (STRLEN(mode_str) != 9)
11513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011514 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011515 return;
11516 }
11517
11518 mask = 1;
11519 for (i = 8; i >= 0; --i)
11520 {
11521 if (mode_str[i] != '-')
11522 mode |= mask;
11523 mask = mask << 1;
11524 }
11525 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11526}
11527
11528/*
11529 * "setline()" function
11530 */
11531 static void
11532f_setline(typval_T *argvars, typval_T *rettv)
11533{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011534 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011535
Bram Moolenaarca851592018-06-06 21:04:07 +020011536 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537}
11538
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011539/*
11540 * Used by "setqflist()" and "setloclist()" functions
11541 */
11542 static void
11543set_qf_ll_list(
11544 win_T *wp UNUSED,
11545 typval_T *list_arg UNUSED,
11546 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011547 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 typval_T *rettv)
11549{
11550#ifdef FEAT_QUICKFIX
11551 static char *e_invact = N_("E927: Invalid action: '%s'");
11552 char_u *act;
11553 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011554 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011555#endif
11556
11557 rettv->vval.v_number = -1;
11558
11559#ifdef FEAT_QUICKFIX
11560 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011561 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011562 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011563 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564 else
11565 {
11566 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011567 dict_T *d = NULL;
11568 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569
11570 if (action_arg->v_type == VAR_STRING)
11571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011572 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 if (act == NULL)
11574 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011575 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11576 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011577 action = *act;
11578 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011579 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011580 }
11581 else if (action_arg->v_type == VAR_UNKNOWN)
11582 action = ' ';
11583 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011584 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585
Bram Moolenaard823fa92016-08-12 16:29:27 +020011586 if (action_arg->v_type != VAR_UNKNOWN
11587 && what_arg->v_type != VAR_UNKNOWN)
11588 {
11589 if (what_arg->v_type == VAR_DICT)
11590 d = what_arg->vval.v_dict;
11591 else
11592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011593 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011594 valid_dict = FALSE;
11595 }
11596 }
11597
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011598 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011599 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011600 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11601 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011602 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011603 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 }
11605#endif
11606}
11607
11608/*
11609 * "setloclist()" function
11610 */
11611 static void
11612f_setloclist(typval_T *argvars, typval_T *rettv)
11613{
11614 win_T *win;
11615
11616 rettv->vval.v_number = -1;
11617
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011618 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011619 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011620 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011621}
11622
11623/*
11624 * "setmatches()" function
11625 */
11626 static void
11627f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11628{
11629#ifdef FEAT_SEARCH_EXTRA
11630 list_T *l;
11631 listitem_T *li;
11632 dict_T *d;
11633 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011634 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011635
11636 rettv->vval.v_number = -1;
11637 if (argvars[0].v_type != VAR_LIST)
11638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011639 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 return;
11641 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011642 if (win == NULL)
11643 return;
11644
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011645 if ((l = argvars[0].vval.v_list) != NULL)
11646 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011647 /* To some extent make sure that we are dealing with a list from
11648 * "getmatches()". */
11649 li = l->lv_first;
11650 while (li != NULL)
11651 {
11652 if (li->li_tv.v_type != VAR_DICT
11653 || (d = li->li_tv.vval.v_dict) == NULL)
11654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011655 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011656 return;
11657 }
11658 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11659 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11660 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11661 && dict_find(d, (char_u *)"priority", -1) != NULL
11662 && dict_find(d, (char_u *)"id", -1) != NULL))
11663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011664 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 return;
11666 }
11667 li = li->li_next;
11668 }
11669
Bram Moolenaaraff74912019-03-30 18:11:49 +010011670 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011671 li = l->lv_first;
11672 while (li != NULL)
11673 {
11674 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011675 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011676 dictitem_T *di;
11677 char_u *group;
11678 int priority;
11679 int id;
11680 char_u *conceal;
11681
11682 d = li->li_tv.vval.v_dict;
11683 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11684 {
11685 if (s == NULL)
11686 {
11687 s = list_alloc();
11688 if (s == NULL)
11689 return;
11690 }
11691
11692 /* match from matchaddpos() */
11693 for (i = 1; i < 9; i++)
11694 {
11695 sprintf((char *)buf, (char *)"pos%d", i);
11696 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11697 {
11698 if (di->di_tv.v_type != VAR_LIST)
11699 return;
11700
11701 list_append_tv(s, &di->di_tv);
11702 s->lv_refcount++;
11703 }
11704 else
11705 break;
11706 }
11707 }
11708
Bram Moolenaar8f667172018-12-14 15:38:31 +010011709 group = dict_get_string(d, (char_u *)"group", TRUE);
11710 priority = (int)dict_get_number(d, (char_u *)"priority");
11711 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011712 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011713 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011714 : NULL;
11715 if (i == 0)
11716 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011717 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011718 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011719 priority, id, NULL, conceal);
11720 }
11721 else
11722 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011723 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011724 list_unref(s);
11725 s = NULL;
11726 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011727 vim_free(group);
11728 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011729
11730 li = li->li_next;
11731 }
11732 rettv->vval.v_number = 0;
11733 }
11734#endif
11735}
11736
11737/*
11738 * "setpos()" function
11739 */
11740 static void
11741f_setpos(typval_T *argvars, typval_T *rettv)
11742{
11743 pos_T pos;
11744 int fnum;
11745 char_u *name;
11746 colnr_T curswant = -1;
11747
11748 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011749 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011750 if (name != NULL)
11751 {
11752 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11753 {
11754 if (--pos.col < 0)
11755 pos.col = 0;
11756 if (name[0] == '.' && name[1] == NUL)
11757 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011758 /* set cursor; "fnum" is ignored */
11759 curwin->w_cursor = pos;
11760 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011761 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011762 curwin->w_curswant = curswant - 1;
11763 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011764 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011765 check_cursor();
11766 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 }
11768 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11769 {
11770 /* set mark */
11771 if (setmark_pos(name[1], &pos, fnum) == OK)
11772 rettv->vval.v_number = 0;
11773 }
11774 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011775 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011776 }
11777 }
11778}
11779
11780/*
11781 * "setqflist()" function
11782 */
11783 static void
11784f_setqflist(typval_T *argvars, typval_T *rettv)
11785{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011786 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011787}
11788
11789/*
11790 * "setreg()" function
11791 */
11792 static void
11793f_setreg(typval_T *argvars, typval_T *rettv)
11794{
11795 int regname;
11796 char_u *strregname;
11797 char_u *stropt;
11798 char_u *strval;
11799 int append;
11800 char_u yank_type;
11801 long block_len;
11802
11803 block_len = -1;
11804 yank_type = MAUTO;
11805 append = FALSE;
11806
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011807 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011808 rettv->vval.v_number = 1; /* FAIL is default */
11809
11810 if (strregname == NULL)
11811 return; /* type error; errmsg already given */
11812 regname = *strregname;
11813 if (regname == 0 || regname == '@')
11814 regname = '"';
11815
11816 if (argvars[2].v_type != VAR_UNKNOWN)
11817 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011818 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011819 if (stropt == NULL)
11820 return; /* type error */
11821 for (; *stropt != NUL; ++stropt)
11822 switch (*stropt)
11823 {
11824 case 'a': case 'A': /* append */
11825 append = TRUE;
11826 break;
11827 case 'v': case 'c': /* character-wise selection */
11828 yank_type = MCHAR;
11829 break;
11830 case 'V': case 'l': /* line-wise selection */
11831 yank_type = MLINE;
11832 break;
11833 case 'b': case Ctrl_V: /* block-wise selection */
11834 yank_type = MBLOCK;
11835 if (VIM_ISDIGIT(stropt[1]))
11836 {
11837 ++stropt;
11838 block_len = getdigits(&stropt) - 1;
11839 --stropt;
11840 }
11841 break;
11842 }
11843 }
11844
11845 if (argvars[1].v_type == VAR_LIST)
11846 {
11847 char_u **lstval;
11848 char_u **allocval;
11849 char_u buf[NUMBUFLEN];
11850 char_u **curval;
11851 char_u **curallocval;
11852 list_T *ll = argvars[1].vval.v_list;
11853 listitem_T *li;
11854 int len;
11855
11856 /* If the list is NULL handle like an empty list. */
11857 len = ll == NULL ? 0 : ll->lv_len;
11858
11859 /* First half: use for pointers to result lines; second half: use for
11860 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011861 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 if (lstval == NULL)
11863 return;
11864 curval = lstval;
11865 allocval = lstval + len + 2;
11866 curallocval = allocval;
11867
11868 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11869 li = li->li_next)
11870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011871 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011872 if (strval == NULL)
11873 goto free_lstval;
11874 if (strval == buf)
11875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011876 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011877 * overwrite the string. */
11878 strval = vim_strsave(buf);
11879 if (strval == NULL)
11880 goto free_lstval;
11881 *curallocval++ = strval;
11882 }
11883 *curval++ = strval;
11884 }
11885 *curval++ = NULL;
11886
11887 write_reg_contents_lst(regname, lstval, -1,
11888 append, yank_type, block_len);
11889free_lstval:
11890 while (curallocval > allocval)
11891 vim_free(*--curallocval);
11892 vim_free(lstval);
11893 }
11894 else
11895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011896 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011897 if (strval == NULL)
11898 return;
11899 write_reg_contents_ex(regname, strval, -1,
11900 append, yank_type, block_len);
11901 }
11902 rettv->vval.v_number = 0;
11903}
11904
11905/*
11906 * "settabvar()" function
11907 */
11908 static void
11909f_settabvar(typval_T *argvars, typval_T *rettv)
11910{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011911 tabpage_T *save_curtab;
11912 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011913 char_u *varname, *tabvarname;
11914 typval_T *varp;
11915
11916 rettv->vval.v_number = 0;
11917
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011918 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011919 return;
11920
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011921 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11922 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923 varp = &argvars[2];
11924
Bram Moolenaar4033c552017-09-16 20:54:51 +020011925 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011926 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011927 save_curtab = curtab;
11928 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011929
Bram Moolenaar964b3742019-05-24 18:54:09 +020011930 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011931 if (tabvarname != NULL)
11932 {
11933 STRCPY(tabvarname, "t:");
11934 STRCPY(tabvarname + 2, varname);
11935 set_var(tabvarname, varp, TRUE);
11936 vim_free(tabvarname);
11937 }
11938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011939 /* Restore current tabpage */
11940 if (valid_tabpage(save_curtab))
11941 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011942 }
11943}
11944
11945/*
11946 * "settabwinvar()" function
11947 */
11948 static void
11949f_settabwinvar(typval_T *argvars, typval_T *rettv)
11950{
11951 setwinvar(argvars, rettv, 1);
11952}
11953
11954/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011955 * "settagstack()" function
11956 */
11957 static void
11958f_settagstack(typval_T *argvars, typval_T *rettv)
11959{
11960 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11961 win_T *wp;
11962 dict_T *d;
11963 int action = 'r';
11964
11965 rettv->vval.v_number = -1;
11966
11967 // first argument: window number or id
11968 wp = find_win_by_nr_or_id(&argvars[0]);
11969 if (wp == NULL)
11970 return;
11971
11972 // second argument: dict with items to set in the tag stack
11973 if (argvars[1].v_type != VAR_DICT)
11974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011975 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011976 return;
11977 }
11978 d = argvars[1].vval.v_dict;
11979 if (d == NULL)
11980 return;
11981
11982 // third argument: action - 'a' for append and 'r' for replace.
11983 // default is to replace the stack.
11984 if (argvars[2].v_type == VAR_UNKNOWN)
11985 action = 'r';
11986 else if (argvars[2].v_type == VAR_STRING)
11987 {
11988 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011989 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011990 if (actstr == NULL)
11991 return;
11992 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11993 action = *actstr;
11994 else
11995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011996 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011997 return;
11998 }
11999 }
12000 else
12001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012002 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012003 return;
12004 }
12005
12006 if (set_tagstack(wp, d, action) == OK)
12007 rettv->vval.v_number = 0;
12008}
12009
12010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012011 * "setwinvar()" function
12012 */
12013 static void
12014f_setwinvar(typval_T *argvars, typval_T *rettv)
12015{
12016 setwinvar(argvars, rettv, 0);
12017}
12018
12019#ifdef FEAT_CRYPT
12020/*
12021 * "sha256({string})" function
12022 */
12023 static void
12024f_sha256(typval_T *argvars, typval_T *rettv)
12025{
12026 char_u *p;
12027
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012028 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012029 rettv->vval.v_string = vim_strsave(
12030 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12031 rettv->v_type = VAR_STRING;
12032}
12033#endif /* FEAT_CRYPT */
12034
12035/*
12036 * "shellescape({string})" function
12037 */
12038 static void
12039f_shellescape(typval_T *argvars, typval_T *rettv)
12040{
Bram Moolenaar20615522017-06-05 18:46:26 +020012041 int do_special = non_zero_arg(&argvars[1]);
12042
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012043 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012044 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012045 rettv->v_type = VAR_STRING;
12046}
12047
12048/*
12049 * shiftwidth() function
12050 */
12051 static void
12052f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12053{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012054 rettv->vval.v_number = 0;
12055
12056 if (argvars[0].v_type != VAR_UNKNOWN)
12057 {
12058 long col;
12059
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012060 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012061 if (col < 0)
12062 return; // type error; errmsg already given
12063#ifdef FEAT_VARTABS
12064 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12065 return;
12066#endif
12067 }
12068
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012069 rettv->vval.v_number = get_sw_value(curbuf);
12070}
12071
Bram Moolenaar162b7142018-12-21 15:17:36 +010012072#ifdef FEAT_SIGNS
12073/*
12074 * "sign_define()" function
12075 */
12076 static void
12077f_sign_define(typval_T *argvars, typval_T *rettv)
12078{
12079 char_u *name;
12080 dict_T *dict;
12081 char_u *icon = NULL;
12082 char_u *linehl = NULL;
12083 char_u *text = NULL;
12084 char_u *texthl = NULL;
12085
12086 rettv->vval.v_number = -1;
12087
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012088 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089 if (name == NULL)
12090 return;
12091
12092 if (argvars[1].v_type != VAR_UNKNOWN)
12093 {
12094 if (argvars[1].v_type != VAR_DICT)
12095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012096 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012097 return;
12098 }
12099
12100 // sign attributes
12101 dict = argvars[1].vval.v_dict;
12102 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12103 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12104 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12105 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12106 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12107 text = dict_get_string(dict, (char_u *)"text", TRUE);
12108 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12109 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12110 }
12111
12112 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12113 rettv->vval.v_number = 0;
12114
12115 vim_free(icon);
12116 vim_free(linehl);
12117 vim_free(text);
12118 vim_free(texthl);
12119}
12120
12121/*
12122 * "sign_getdefined()" function
12123 */
12124 static void
12125f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12126{
12127 char_u *name = NULL;
12128
12129 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12130 return;
12131
12132 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012133 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012134
12135 sign_getlist(name, rettv->vval.v_list);
12136}
12137
12138/*
12139 * "sign_getplaced()" function
12140 */
12141 static void
12142f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12143{
12144 buf_T *buf = NULL;
12145 dict_T *dict;
12146 dictitem_T *di;
12147 linenr_T lnum = 0;
12148 int sign_id = 0;
12149 char_u *group = NULL;
12150 int notanum = FALSE;
12151
12152 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12153 return;
12154
12155 if (argvars[0].v_type != VAR_UNKNOWN)
12156 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012157 // get signs placed in the specified buffer
12158 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012159 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012160 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012161
12162 if (argvars[1].v_type != VAR_UNKNOWN)
12163 {
12164 if (argvars[1].v_type != VAR_DICT ||
12165 ((dict = argvars[1].vval.v_dict) == NULL))
12166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012167 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012168 return;
12169 }
12170 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12171 {
12172 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012173 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012174 if (notanum)
12175 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012176 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012177 }
12178 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12179 {
12180 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012181 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012182 if (notanum)
12183 return;
12184 }
12185 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12186 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012187 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012188 if (group == NULL)
12189 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012190 if (*group == '\0') // empty string means global group
12191 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012192 }
12193 }
12194 }
12195
12196 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12197}
12198
12199/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012200 * "sign_jump()" function
12201 */
12202 static void
12203f_sign_jump(typval_T *argvars, typval_T *rettv)
12204{
12205 int sign_id;
12206 char_u *sign_group = NULL;
12207 buf_T *buf;
12208 int notanum = FALSE;
12209
12210 rettv->vval.v_number = -1;
12211
Bram Moolenaarbdace832019-03-02 10:13:42 +010012212 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012213 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12214 if (notanum)
12215 return;
12216 if (sign_id <= 0)
12217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012218 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012219 return;
12220 }
12221
12222 // Sign group
12223 sign_group = tv_get_string_chk(&argvars[1]);
12224 if (sign_group == NULL)
12225 return;
12226 if (sign_group[0] == '\0')
12227 sign_group = NULL; // global sign group
12228 else
12229 {
12230 sign_group = vim_strsave(sign_group);
12231 if (sign_group == NULL)
12232 return;
12233 }
12234
12235 // Buffer to place the sign
12236 buf = get_buf_arg(&argvars[2]);
12237 if (buf == NULL)
12238 goto cleanup;
12239
12240 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12241
12242cleanup:
12243 vim_free(sign_group);
12244}
12245
12246/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012247 * "sign_place()" function
12248 */
12249 static void
12250f_sign_place(typval_T *argvars, typval_T *rettv)
12251{
12252 int sign_id;
12253 char_u *group = NULL;
12254 char_u *sign_name;
12255 buf_T *buf;
12256 dict_T *dict;
12257 dictitem_T *di;
12258 linenr_T lnum = 0;
12259 int prio = SIGN_DEF_PRIO;
12260 int notanum = FALSE;
12261
12262 rettv->vval.v_number = -1;
12263
Bram Moolenaarbdace832019-03-02 10:13:42 +010012264 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012265 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012266 if (notanum)
12267 return;
12268 if (sign_id < 0)
12269 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012270 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012271 return;
12272 }
12273
12274 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012275 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012276 if (group == NULL)
12277 return;
12278 if (group[0] == '\0')
12279 group = NULL; // global sign group
12280 else
12281 {
12282 group = vim_strsave(group);
12283 if (group == NULL)
12284 return;
12285 }
12286
12287 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012288 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012289 if (sign_name == NULL)
12290 goto cleanup;
12291
12292 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012293 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012294 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012295 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012296
12297 if (argvars[4].v_type != VAR_UNKNOWN)
12298 {
12299 if (argvars[4].v_type != VAR_DICT ||
12300 ((dict = argvars[4].vval.v_dict) == NULL))
12301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012302 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012303 goto cleanup;
12304 }
12305
12306 // Line number where the sign is to be placed
12307 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12308 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012309 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012310 if (notanum)
12311 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012312 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012313 }
12314 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12315 {
12316 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012317 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012318 if (notanum)
12319 goto cleanup;
12320 }
12321 }
12322
12323 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12324 rettv->vval.v_number = sign_id;
12325
12326cleanup:
12327 vim_free(group);
12328}
12329
12330/*
12331 * "sign_undefine()" function
12332 */
12333 static void
12334f_sign_undefine(typval_T *argvars, typval_T *rettv)
12335{
12336 char_u *name;
12337
12338 rettv->vval.v_number = -1;
12339
12340 if (argvars[0].v_type == VAR_UNKNOWN)
12341 {
12342 // Free all the signs
12343 free_signs();
12344 rettv->vval.v_number = 0;
12345 }
12346 else
12347 {
12348 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012349 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012350 if (name == NULL)
12351 return;
12352
12353 if (sign_undefine_by_name(name) == OK)
12354 rettv->vval.v_number = 0;
12355 }
12356}
12357
12358/*
12359 * "sign_unplace()" function
12360 */
12361 static void
12362f_sign_unplace(typval_T *argvars, typval_T *rettv)
12363{
12364 dict_T *dict;
12365 dictitem_T *di;
12366 int sign_id = 0;
12367 buf_T *buf = NULL;
12368 char_u *group = NULL;
12369
12370 rettv->vval.v_number = -1;
12371
12372 if (argvars[0].v_type != VAR_STRING)
12373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012374 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012375 return;
12376 }
12377
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012378 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012379 if (group[0] == '\0')
12380 group = NULL; // global sign group
12381 else
12382 {
12383 group = vim_strsave(group);
12384 if (group == NULL)
12385 return;
12386 }
12387
12388 if (argvars[1].v_type != VAR_UNKNOWN)
12389 {
12390 if (argvars[1].v_type != VAR_DICT)
12391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012392 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012393 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012394 }
12395 dict = argvars[1].vval.v_dict;
12396
12397 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12398 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012399 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012400 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012401 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012402 }
12403 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12404 sign_id = dict_get_number(dict, (char_u *)"id");
12405 }
12406
12407 if (buf == NULL)
12408 {
12409 // Delete the sign in all the buffers
12410 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012411 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012412 rettv->vval.v_number = 0;
12413 }
12414 else
12415 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012416 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012417 rettv->vval.v_number = 0;
12418 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012419
12420cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012421 vim_free(group);
12422}
12423#endif
12424
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425/*
12426 * "simplify()" function
12427 */
12428 static void
12429f_simplify(typval_T *argvars, typval_T *rettv)
12430{
12431 char_u *p;
12432
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012433 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012434 rettv->vval.v_string = vim_strsave(p);
12435 simplify_filename(rettv->vval.v_string); /* simplify in place */
12436 rettv->v_type = VAR_STRING;
12437}
12438
12439#ifdef FEAT_FLOAT
12440/*
12441 * "sin()" function
12442 */
12443 static void
12444f_sin(typval_T *argvars, typval_T *rettv)
12445{
12446 float_T f = 0.0;
12447
12448 rettv->v_type = VAR_FLOAT;
12449 if (get_float_arg(argvars, &f) == OK)
12450 rettv->vval.v_float = sin(f);
12451 else
12452 rettv->vval.v_float = 0.0;
12453}
12454
12455/*
12456 * "sinh()" function
12457 */
12458 static void
12459f_sinh(typval_T *argvars, typval_T *rettv)
12460{
12461 float_T f = 0.0;
12462
12463 rettv->v_type = VAR_FLOAT;
12464 if (get_float_arg(argvars, &f) == OK)
12465 rettv->vval.v_float = sinh(f);
12466 else
12467 rettv->vval.v_float = 0.0;
12468}
12469#endif
12470
Bram Moolenaareae1b912019-05-09 15:12:55 +020012471static int item_compare(const void *s1, const void *s2);
12472static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012473
12474/* struct used in the array that's given to qsort() */
12475typedef struct
12476{
12477 listitem_T *item;
12478 int idx;
12479} sortItem_T;
12480
12481/* struct storing information about current sort */
12482typedef struct
12483{
12484 int item_compare_ic;
12485 int item_compare_numeric;
12486 int item_compare_numbers;
12487#ifdef FEAT_FLOAT
12488 int item_compare_float;
12489#endif
12490 char_u *item_compare_func;
12491 partial_T *item_compare_partial;
12492 dict_T *item_compare_selfdict;
12493 int item_compare_func_err;
12494 int item_compare_keep_zero;
12495} sortinfo_T;
12496static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012497#define ITEM_COMPARE_FAIL 999
12498
12499/*
12500 * Compare functions for f_sort() and f_uniq() below.
12501 */
12502 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012503item_compare(const void *s1, const void *s2)
12504{
12505 sortItem_T *si1, *si2;
12506 typval_T *tv1, *tv2;
12507 char_u *p1, *p2;
12508 char_u *tofree1 = NULL, *tofree2 = NULL;
12509 int res;
12510 char_u numbuf1[NUMBUFLEN];
12511 char_u numbuf2[NUMBUFLEN];
12512
12513 si1 = (sortItem_T *)s1;
12514 si2 = (sortItem_T *)s2;
12515 tv1 = &si1->item->li_tv;
12516 tv2 = &si2->item->li_tv;
12517
12518 if (sortinfo->item_compare_numbers)
12519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012520 varnumber_T v1 = tv_get_number(tv1);
12521 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012522
12523 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12524 }
12525
12526#ifdef FEAT_FLOAT
12527 if (sortinfo->item_compare_float)
12528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012529 float_T v1 = tv_get_float(tv1);
12530 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012531
12532 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12533 }
12534#endif
12535
12536 /* tv2string() puts quotes around a string and allocates memory. Don't do
12537 * that for string variables. Use a single quote when comparing with a
12538 * non-string to do what the docs promise. */
12539 if (tv1->v_type == VAR_STRING)
12540 {
12541 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12542 p1 = (char_u *)"'";
12543 else
12544 p1 = tv1->vval.v_string;
12545 }
12546 else
12547 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12548 if (tv2->v_type == VAR_STRING)
12549 {
12550 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12551 p2 = (char_u *)"'";
12552 else
12553 p2 = tv2->vval.v_string;
12554 }
12555 else
12556 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12557 if (p1 == NULL)
12558 p1 = (char_u *)"";
12559 if (p2 == NULL)
12560 p2 = (char_u *)"";
12561 if (!sortinfo->item_compare_numeric)
12562 {
12563 if (sortinfo->item_compare_ic)
12564 res = STRICMP(p1, p2);
12565 else
12566 res = STRCMP(p1, p2);
12567 }
12568 else
12569 {
12570 double n1, n2;
12571 n1 = strtod((char *)p1, (char **)&p1);
12572 n2 = strtod((char *)p2, (char **)&p2);
12573 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12574 }
12575
12576 /* When the result would be zero, compare the item indexes. Makes the
12577 * sort stable. */
12578 if (res == 0 && !sortinfo->item_compare_keep_zero)
12579 res = si1->idx > si2->idx ? 1 : -1;
12580
12581 vim_free(tofree1);
12582 vim_free(tofree2);
12583 return res;
12584}
12585
12586 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012587item_compare2(const void *s1, const void *s2)
12588{
12589 sortItem_T *si1, *si2;
12590 int res;
12591 typval_T rettv;
12592 typval_T argv[3];
12593 int dummy;
12594 char_u *func_name;
12595 partial_T *partial = sortinfo->item_compare_partial;
12596
12597 /* shortcut after failure in previous call; compare all items equal */
12598 if (sortinfo->item_compare_func_err)
12599 return 0;
12600
12601 si1 = (sortItem_T *)s1;
12602 si2 = (sortItem_T *)s2;
12603
12604 if (partial == NULL)
12605 func_name = sortinfo->item_compare_func;
12606 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012607 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012608
12609 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12610 * in the copy without changing the original list items. */
12611 copy_tv(&si1->item->li_tv, &argv[0]);
12612 copy_tv(&si2->item->li_tv, &argv[1]);
12613
12614 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012615 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012616 partial, sortinfo->item_compare_selfdict);
12617 clear_tv(&argv[0]);
12618 clear_tv(&argv[1]);
12619
12620 if (res == FAIL)
12621 res = ITEM_COMPARE_FAIL;
12622 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012623 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012624 if (sortinfo->item_compare_func_err)
12625 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12626 clear_tv(&rettv);
12627
12628 /* When the result would be zero, compare the pointers themselves. Makes
12629 * the sort stable. */
12630 if (res == 0 && !sortinfo->item_compare_keep_zero)
12631 res = si1->idx > si2->idx ? 1 : -1;
12632
12633 return res;
12634}
12635
12636/*
12637 * "sort({list})" function
12638 */
12639 static void
12640do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12641{
12642 list_T *l;
12643 listitem_T *li;
12644 sortItem_T *ptrs;
12645 sortinfo_T *old_sortinfo;
12646 sortinfo_T info;
12647 long len;
12648 long i;
12649
12650 /* Pointer to current info struct used in compare function. Save and
12651 * restore the current one for nested calls. */
12652 old_sortinfo = sortinfo;
12653 sortinfo = &info;
12654
12655 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012656 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012657 else
12658 {
12659 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012660 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12662 TRUE))
12663 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012664 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665
12666 len = list_len(l);
12667 if (len <= 1)
12668 goto theend; /* short list sorts pretty quickly */
12669
12670 info.item_compare_ic = FALSE;
12671 info.item_compare_numeric = FALSE;
12672 info.item_compare_numbers = FALSE;
12673#ifdef FEAT_FLOAT
12674 info.item_compare_float = FALSE;
12675#endif
12676 info.item_compare_func = NULL;
12677 info.item_compare_partial = NULL;
12678 info.item_compare_selfdict = NULL;
12679 if (argvars[1].v_type != VAR_UNKNOWN)
12680 {
12681 /* optional second argument: {func} */
12682 if (argvars[1].v_type == VAR_FUNC)
12683 info.item_compare_func = argvars[1].vval.v_string;
12684 else if (argvars[1].v_type == VAR_PARTIAL)
12685 info.item_compare_partial = argvars[1].vval.v_partial;
12686 else
12687 {
12688 int error = FALSE;
12689
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012690 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691 if (error)
12692 goto theend; /* type error; errmsg already given */
12693 if (i == 1)
12694 info.item_compare_ic = TRUE;
12695 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012696 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012697 else if (i != 0)
12698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012699 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012700 goto theend;
12701 }
12702 if (info.item_compare_func != NULL)
12703 {
12704 if (*info.item_compare_func == NUL)
12705 {
12706 /* empty string means default sort */
12707 info.item_compare_func = NULL;
12708 }
12709 else if (STRCMP(info.item_compare_func, "n") == 0)
12710 {
12711 info.item_compare_func = NULL;
12712 info.item_compare_numeric = TRUE;
12713 }
12714 else if (STRCMP(info.item_compare_func, "N") == 0)
12715 {
12716 info.item_compare_func = NULL;
12717 info.item_compare_numbers = TRUE;
12718 }
12719#ifdef FEAT_FLOAT
12720 else if (STRCMP(info.item_compare_func, "f") == 0)
12721 {
12722 info.item_compare_func = NULL;
12723 info.item_compare_float = TRUE;
12724 }
12725#endif
12726 else if (STRCMP(info.item_compare_func, "i") == 0)
12727 {
12728 info.item_compare_func = NULL;
12729 info.item_compare_ic = TRUE;
12730 }
12731 }
12732 }
12733
12734 if (argvars[2].v_type != VAR_UNKNOWN)
12735 {
12736 /* optional third argument: {dict} */
12737 if (argvars[2].v_type != VAR_DICT)
12738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012739 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012740 goto theend;
12741 }
12742 info.item_compare_selfdict = argvars[2].vval.v_dict;
12743 }
12744 }
12745
12746 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012747 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012748 if (ptrs == NULL)
12749 goto theend;
12750
12751 i = 0;
12752 if (sort)
12753 {
12754 /* sort(): ptrs will be the list to sort */
12755 for (li = l->lv_first; li != NULL; li = li->li_next)
12756 {
12757 ptrs[i].item = li;
12758 ptrs[i].idx = i;
12759 ++i;
12760 }
12761
12762 info.item_compare_func_err = FALSE;
12763 info.item_compare_keep_zero = FALSE;
12764 /* test the compare function */
12765 if ((info.item_compare_func != NULL
12766 || info.item_compare_partial != NULL)
12767 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12768 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012769 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012770 else
12771 {
12772 /* Sort the array with item pointers. */
12773 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12774 info.item_compare_func == NULL
12775 && info.item_compare_partial == NULL
12776 ? item_compare : item_compare2);
12777
12778 if (!info.item_compare_func_err)
12779 {
12780 /* Clear the List and append the items in sorted order. */
12781 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12782 l->lv_len = 0;
12783 for (i = 0; i < len; ++i)
12784 list_append(l, ptrs[i].item);
12785 }
12786 }
12787 }
12788 else
12789 {
12790 int (*item_compare_func_ptr)(const void *, const void *);
12791
12792 /* f_uniq(): ptrs will be a stack of items to remove */
12793 info.item_compare_func_err = FALSE;
12794 info.item_compare_keep_zero = TRUE;
12795 item_compare_func_ptr = info.item_compare_func != NULL
12796 || info.item_compare_partial != NULL
12797 ? item_compare2 : item_compare;
12798
12799 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12800 li = li->li_next)
12801 {
12802 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12803 == 0)
12804 ptrs[i++].item = li;
12805 if (info.item_compare_func_err)
12806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012807 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012808 break;
12809 }
12810 }
12811
12812 if (!info.item_compare_func_err)
12813 {
12814 while (--i >= 0)
12815 {
12816 li = ptrs[i].item->li_next;
12817 ptrs[i].item->li_next = li->li_next;
12818 if (li->li_next != NULL)
12819 li->li_next->li_prev = ptrs[i].item;
12820 else
12821 l->lv_last = ptrs[i].item;
12822 list_fix_watch(l, li);
12823 listitem_free(li);
12824 l->lv_len--;
12825 }
12826 }
12827 }
12828
12829 vim_free(ptrs);
12830 }
12831theend:
12832 sortinfo = old_sortinfo;
12833}
12834
12835/*
12836 * "sort({list})" function
12837 */
12838 static void
12839f_sort(typval_T *argvars, typval_T *rettv)
12840{
12841 do_sort_uniq(argvars, rettv, TRUE);
12842}
12843
12844/*
12845 * "uniq({list})" function
12846 */
12847 static void
12848f_uniq(typval_T *argvars, typval_T *rettv)
12849{
12850 do_sort_uniq(argvars, rettv, FALSE);
12851}
12852
12853/*
12854 * "soundfold({word})" function
12855 */
12856 static void
12857f_soundfold(typval_T *argvars, typval_T *rettv)
12858{
12859 char_u *s;
12860
12861 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012862 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012863#ifdef FEAT_SPELL
12864 rettv->vval.v_string = eval_soundfold(s);
12865#else
12866 rettv->vval.v_string = vim_strsave(s);
12867#endif
12868}
12869
12870/*
12871 * "spellbadword()" function
12872 */
12873 static void
12874f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12875{
12876 char_u *word = (char_u *)"";
12877 hlf_T attr = HLF_COUNT;
12878 int len = 0;
12879
12880 if (rettv_list_alloc(rettv) == FAIL)
12881 return;
12882
12883#ifdef FEAT_SPELL
12884 if (argvars[0].v_type == VAR_UNKNOWN)
12885 {
12886 /* Find the start and length of the badly spelled word. */
12887 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12888 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012889 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012890 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012891 curwin->w_set_curswant = TRUE;
12892 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012893 }
12894 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012896 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012897 int capcol = -1;
12898
12899 if (str != NULL)
12900 {
12901 /* Check the argument for spelling. */
12902 while (*str != NUL)
12903 {
12904 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12905 if (attr != HLF_COUNT)
12906 {
12907 word = str;
12908 break;
12909 }
12910 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012911 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012912 }
12913 }
12914 }
12915#endif
12916
12917 list_append_string(rettv->vval.v_list, word, len);
12918 list_append_string(rettv->vval.v_list, (char_u *)(
12919 attr == HLF_SPB ? "bad" :
12920 attr == HLF_SPR ? "rare" :
12921 attr == HLF_SPL ? "local" :
12922 attr == HLF_SPC ? "caps" :
12923 ""), -1);
12924}
12925
12926/*
12927 * "spellsuggest()" function
12928 */
12929 static void
12930f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12931{
12932#ifdef FEAT_SPELL
12933 char_u *str;
12934 int typeerr = FALSE;
12935 int maxcount;
12936 garray_T ga;
12937 int i;
12938 listitem_T *li;
12939 int need_capital = FALSE;
12940#endif
12941
12942 if (rettv_list_alloc(rettv) == FAIL)
12943 return;
12944
12945#ifdef FEAT_SPELL
12946 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012948 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012949 if (argvars[1].v_type != VAR_UNKNOWN)
12950 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012951 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012952 if (maxcount <= 0)
12953 return;
12954 if (argvars[2].v_type != VAR_UNKNOWN)
12955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012956 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012957 if (typeerr)
12958 return;
12959 }
12960 }
12961 else
12962 maxcount = 25;
12963
12964 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12965
12966 for (i = 0; i < ga.ga_len; ++i)
12967 {
12968 str = ((char_u **)ga.ga_data)[i];
12969
12970 li = listitem_alloc();
12971 if (li == NULL)
12972 vim_free(str);
12973 else
12974 {
12975 li->li_tv.v_type = VAR_STRING;
12976 li->li_tv.v_lock = 0;
12977 li->li_tv.vval.v_string = str;
12978 list_append(rettv->vval.v_list, li);
12979 }
12980 }
12981 ga_clear(&ga);
12982 }
12983#endif
12984}
12985
12986 static void
12987f_split(typval_T *argvars, typval_T *rettv)
12988{
12989 char_u *str;
12990 char_u *end;
12991 char_u *pat = NULL;
12992 regmatch_T regmatch;
12993 char_u patbuf[NUMBUFLEN];
12994 char_u *save_cpo;
12995 int match;
12996 colnr_T col = 0;
12997 int keepempty = FALSE;
12998 int typeerr = FALSE;
12999
13000 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13001 save_cpo = p_cpo;
13002 p_cpo = (char_u *)"";
13003
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013004 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013005 if (argvars[1].v_type != VAR_UNKNOWN)
13006 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013007 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 if (pat == NULL)
13009 typeerr = TRUE;
13010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013011 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 }
13013 if (pat == NULL || *pat == NUL)
13014 pat = (char_u *)"[\\x01- ]\\+";
13015
13016 if (rettv_list_alloc(rettv) == FAIL)
13017 return;
13018 if (typeerr)
13019 return;
13020
13021 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13022 if (regmatch.regprog != NULL)
13023 {
13024 regmatch.rm_ic = FALSE;
13025 while (*str != NUL || keepempty)
13026 {
13027 if (*str == NUL)
13028 match = FALSE; /* empty item at the end */
13029 else
13030 match = vim_regexec_nl(&regmatch, str, col);
13031 if (match)
13032 end = regmatch.startp[0];
13033 else
13034 end = str + STRLEN(str);
13035 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13036 && *str != NUL && match && end < regmatch.endp[0]))
13037 {
13038 if (list_append_string(rettv->vval.v_list, str,
13039 (int)(end - str)) == FAIL)
13040 break;
13041 }
13042 if (!match)
13043 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013044 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013045 if (regmatch.endp[0] > str)
13046 col = 0;
13047 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013048 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 str = regmatch.endp[0];
13051 }
13052
13053 vim_regfree(regmatch.regprog);
13054 }
13055
13056 p_cpo = save_cpo;
13057}
13058
13059#ifdef FEAT_FLOAT
13060/*
13061 * "sqrt()" function
13062 */
13063 static void
13064f_sqrt(typval_T *argvars, typval_T *rettv)
13065{
13066 float_T f = 0.0;
13067
13068 rettv->v_type = VAR_FLOAT;
13069 if (get_float_arg(argvars, &f) == OK)
13070 rettv->vval.v_float = sqrt(f);
13071 else
13072 rettv->vval.v_float = 0.0;
13073}
13074
13075/*
13076 * "str2float()" function
13077 */
13078 static void
13079f_str2float(typval_T *argvars, typval_T *rettv)
13080{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013081 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013082 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083
Bram Moolenaar08243d22017-01-10 16:12:29 +010013084 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013085 p = skipwhite(p + 1);
13086 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013087 if (isneg)
13088 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 rettv->v_type = VAR_FLOAT;
13090}
13091#endif
13092
13093/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013094 * "str2list()" function
13095 */
13096 static void
13097f_str2list(typval_T *argvars, typval_T *rettv)
13098{
13099 char_u *p;
13100 int utf8 = FALSE;
13101
13102 if (rettv_list_alloc(rettv) == FAIL)
13103 return;
13104
13105 if (argvars[1].v_type != VAR_UNKNOWN)
13106 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13107
13108 p = tv_get_string(&argvars[0]);
13109
13110 if (has_mbyte || utf8)
13111 {
13112 int (*ptr2len)(char_u *);
13113 int (*ptr2char)(char_u *);
13114
13115 if (utf8 || enc_utf8)
13116 {
13117 ptr2len = utf_ptr2len;
13118 ptr2char = utf_ptr2char;
13119 }
13120 else
13121 {
13122 ptr2len = mb_ptr2len;
13123 ptr2char = mb_ptr2char;
13124 }
13125
13126 for ( ; *p != NUL; p += (*ptr2len)(p))
13127 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13128 }
13129 else
13130 for ( ; *p != NUL; ++p)
13131 list_append_number(rettv->vval.v_list, *p);
13132}
13133
13134/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013135 * "str2nr()" function
13136 */
13137 static void
13138f_str2nr(typval_T *argvars, typval_T *rettv)
13139{
13140 int base = 10;
13141 char_u *p;
13142 varnumber_T n;
13143 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013144 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145
13146 if (argvars[1].v_type != VAR_UNKNOWN)
13147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013148 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013149 if (base != 2 && base != 8 && base != 10 && base != 16)
13150 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013151 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013152 return;
13153 }
13154 }
13155
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013156 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013157 isneg = (*p == '-');
13158 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 p = skipwhite(p + 1);
13160 switch (base)
13161 {
13162 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13163 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13164 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13165 default: what = 0;
13166 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013167 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13168 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013169 if (isneg)
13170 rettv->vval.v_number = -n;
13171 else
13172 rettv->vval.v_number = n;
13173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174}
13175
13176#ifdef HAVE_STRFTIME
13177/*
13178 * "strftime({format}[, {time}])" function
13179 */
13180 static void
13181f_strftime(typval_T *argvars, typval_T *rettv)
13182{
13183 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013184# ifdef HAVE_LOCALTIME_R
13185 struct tm tmval;
13186# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 struct tm *curtime;
13188 time_t seconds;
13189 char_u *p;
13190
13191 rettv->v_type = VAR_STRING;
13192
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013193 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 if (argvars[1].v_type == VAR_UNKNOWN)
13195 seconds = time(NULL);
13196 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013197 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013198# ifdef HAVE_LOCALTIME_R
13199 curtime = localtime_r(&seconds, &tmval);
13200# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013202# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013203 /* MSVC returns NULL for an invalid value of seconds. */
13204 if (curtime == NULL)
13205 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13206 else
13207 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 vimconv_T conv;
13209 char_u *enc;
13210
13211 conv.vc_type = CONV_NONE;
13212 enc = enc_locale();
13213 convert_setup(&conv, p_enc, enc);
13214 if (conv.vc_type != CONV_NONE)
13215 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 if (p != NULL)
13217 (void)strftime((char *)result_buf, sizeof(result_buf),
13218 (char *)p, curtime);
13219 else
13220 result_buf[0] = NUL;
13221
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222 if (conv.vc_type != CONV_NONE)
13223 vim_free(p);
13224 convert_setup(&conv, enc, p_enc);
13225 if (conv.vc_type != CONV_NONE)
13226 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13227 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 rettv->vval.v_string = vim_strsave(result_buf);
13229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013230 /* Release conversion descriptors */
13231 convert_setup(&conv, NULL, NULL);
13232 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013233 }
13234}
13235#endif
13236
13237/*
13238 * "strgetchar()" function
13239 */
13240 static void
13241f_strgetchar(typval_T *argvars, typval_T *rettv)
13242{
13243 char_u *str;
13244 int len;
13245 int error = FALSE;
13246 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013247 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013248
13249 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013250 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013251 if (str == NULL)
13252 return;
13253 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013254 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 if (error)
13256 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257
Bram Moolenaar13505972019-01-24 15:04:48 +010013258 while (charidx >= 0 && byteidx < len)
13259 {
13260 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013262 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13263 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013264 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013265 --charidx;
13266 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268}
13269
13270/*
13271 * "stridx()" function
13272 */
13273 static void
13274f_stridx(typval_T *argvars, typval_T *rettv)
13275{
13276 char_u buf[NUMBUFLEN];
13277 char_u *needle;
13278 char_u *haystack;
13279 char_u *save_haystack;
13280 char_u *pos;
13281 int start_idx;
13282
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013283 needle = tv_get_string_chk(&argvars[1]);
13284 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 rettv->vval.v_number = -1;
13286 if (needle == NULL || haystack == NULL)
13287 return; /* type error; errmsg already given */
13288
13289 if (argvars[2].v_type != VAR_UNKNOWN)
13290 {
13291 int error = FALSE;
13292
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013293 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 if (error || start_idx >= (int)STRLEN(haystack))
13295 return;
13296 if (start_idx >= 0)
13297 haystack += start_idx;
13298 }
13299
13300 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13301 if (pos != NULL)
13302 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13303}
13304
13305/*
13306 * "string()" function
13307 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013308 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309f_string(typval_T *argvars, typval_T *rettv)
13310{
13311 char_u *tofree;
13312 char_u numbuf[NUMBUFLEN];
13313
13314 rettv->v_type = VAR_STRING;
13315 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13316 get_copyID());
13317 /* Make a copy if we have a value but it's not in allocated memory. */
13318 if (rettv->vval.v_string != NULL && tofree == NULL)
13319 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13320}
13321
13322/*
13323 * "strlen()" function
13324 */
13325 static void
13326f_strlen(typval_T *argvars, typval_T *rettv)
13327{
13328 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013329 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330}
13331
13332/*
13333 * "strchars()" function
13334 */
13335 static void
13336f_strchars(typval_T *argvars, typval_T *rettv)
13337{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013338 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013340 varnumber_T len = 0;
13341 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013342
13343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013346 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013347 else
13348 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013349 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13350 while (*s != NUL)
13351 {
13352 func_mb_ptr2char_adv(&s);
13353 ++len;
13354 }
13355 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013356 }
13357}
13358
13359/*
13360 * "strdisplaywidth()" function
13361 */
13362 static void
13363f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13364{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013365 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013366 int col = 0;
13367
13368 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013369 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370
13371 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13372}
13373
13374/*
13375 * "strwidth()" function
13376 */
13377 static void
13378f_strwidth(typval_T *argvars, typval_T *rettv)
13379{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013380 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013381
Bram Moolenaar13505972019-01-24 15:04:48 +010013382 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013383}
13384
13385/*
13386 * "strcharpart()" function
13387 */
13388 static void
13389f_strcharpart(typval_T *argvars, typval_T *rettv)
13390{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013391 char_u *p;
13392 int nchar;
13393 int nbyte = 0;
13394 int charlen;
13395 int len = 0;
13396 int slen;
13397 int error = FALSE;
13398
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013399 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400 slen = (int)STRLEN(p);
13401
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013402 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 if (!error)
13404 {
13405 if (nchar > 0)
13406 while (nchar > 0 && nbyte < slen)
13407 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013408 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 --nchar;
13410 }
13411 else
13412 nbyte = nchar;
13413 if (argvars[2].v_type != VAR_UNKNOWN)
13414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013415 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013416 while (charlen > 0 && nbyte + len < slen)
13417 {
13418 int off = nbyte + len;
13419
13420 if (off < 0)
13421 len += 1;
13422 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013423 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 --charlen;
13425 }
13426 }
13427 else
13428 len = slen - nbyte; /* default: all bytes that are available. */
13429 }
13430
13431 /*
13432 * Only return the overlap between the specified part and the actual
13433 * string.
13434 */
13435 if (nbyte < 0)
13436 {
13437 len += nbyte;
13438 nbyte = 0;
13439 }
13440 else if (nbyte > slen)
13441 nbyte = slen;
13442 if (len < 0)
13443 len = 0;
13444 else if (nbyte + len > slen)
13445 len = slen - nbyte;
13446
13447 rettv->v_type = VAR_STRING;
13448 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013449}
13450
13451/*
13452 * "strpart()" function
13453 */
13454 static void
13455f_strpart(typval_T *argvars, typval_T *rettv)
13456{
13457 char_u *p;
13458 int n;
13459 int len;
13460 int slen;
13461 int error = FALSE;
13462
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013463 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013464 slen = (int)STRLEN(p);
13465
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013466 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013467 if (error)
13468 len = 0;
13469 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013470 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471 else
13472 len = slen - n; /* default len: all bytes that are available. */
13473
13474 /*
13475 * Only return the overlap between the specified part and the actual
13476 * string.
13477 */
13478 if (n < 0)
13479 {
13480 len += n;
13481 n = 0;
13482 }
13483 else if (n > slen)
13484 n = slen;
13485 if (len < 0)
13486 len = 0;
13487 else if (n + len > slen)
13488 len = slen - n;
13489
13490 rettv->v_type = VAR_STRING;
13491 rettv->vval.v_string = vim_strnsave(p + n, len);
13492}
13493
13494/*
13495 * "strridx()" function
13496 */
13497 static void
13498f_strridx(typval_T *argvars, typval_T *rettv)
13499{
13500 char_u buf[NUMBUFLEN];
13501 char_u *needle;
13502 char_u *haystack;
13503 char_u *rest;
13504 char_u *lastmatch = NULL;
13505 int haystack_len, end_idx;
13506
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013507 needle = tv_get_string_chk(&argvars[1]);
13508 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013509
13510 rettv->vval.v_number = -1;
13511 if (needle == NULL || haystack == NULL)
13512 return; /* type error; errmsg already given */
13513
13514 haystack_len = (int)STRLEN(haystack);
13515 if (argvars[2].v_type != VAR_UNKNOWN)
13516 {
13517 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013518 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013519 if (end_idx < 0)
13520 return; /* can never find a match */
13521 }
13522 else
13523 end_idx = haystack_len;
13524
13525 if (*needle == NUL)
13526 {
13527 /* Empty string matches past the end. */
13528 lastmatch = haystack + end_idx;
13529 }
13530 else
13531 {
13532 for (rest = haystack; *rest != '\0'; ++rest)
13533 {
13534 rest = (char_u *)strstr((char *)rest, (char *)needle);
13535 if (rest == NULL || rest > haystack + end_idx)
13536 break;
13537 lastmatch = rest;
13538 }
13539 }
13540
13541 if (lastmatch == NULL)
13542 rettv->vval.v_number = -1;
13543 else
13544 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13545}
13546
13547/*
13548 * "strtrans()" function
13549 */
13550 static void
13551f_strtrans(typval_T *argvars, typval_T *rettv)
13552{
13553 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013554 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013555}
13556
13557/*
13558 * "submatch()" function
13559 */
13560 static void
13561f_submatch(typval_T *argvars, typval_T *rettv)
13562{
13563 int error = FALSE;
13564 int no;
13565 int retList = 0;
13566
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013567 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013568 if (error)
13569 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013570 if (no < 0 || no >= NSUBEXP)
13571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013572 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013573 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013574 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013576 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577 if (error)
13578 return;
13579
13580 if (retList == 0)
13581 {
13582 rettv->v_type = VAR_STRING;
13583 rettv->vval.v_string = reg_submatch(no);
13584 }
13585 else
13586 {
13587 rettv->v_type = VAR_LIST;
13588 rettv->vval.v_list = reg_submatch_list(no);
13589 }
13590}
13591
13592/*
13593 * "substitute()" function
13594 */
13595 static void
13596f_substitute(typval_T *argvars, typval_T *rettv)
13597{
13598 char_u patbuf[NUMBUFLEN];
13599 char_u subbuf[NUMBUFLEN];
13600 char_u flagsbuf[NUMBUFLEN];
13601
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013602 char_u *str = tv_get_string_chk(&argvars[0]);
13603 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013604 char_u *sub = NULL;
13605 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013606 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013608 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13609 expr = &argvars[2];
13610 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013611 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013612
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013614 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13615 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013616 rettv->vval.v_string = NULL;
13617 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013618 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619}
13620
13621/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013622 * "swapinfo(swap_filename)" function
13623 */
13624 static void
13625f_swapinfo(typval_T *argvars, typval_T *rettv)
13626{
13627 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013628 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013629}
13630
13631/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013632 * "swapname(expr)" function
13633 */
13634 static void
13635f_swapname(typval_T *argvars, typval_T *rettv)
13636{
13637 buf_T *buf;
13638
13639 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013640 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013641 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13642 || buf->b_ml.ml_mfp->mf_fname == NULL)
13643 rettv->vval.v_string = NULL;
13644 else
13645 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13646}
13647
13648/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013649 * "synID(lnum, col, trans)" function
13650 */
13651 static void
13652f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13653{
13654 int id = 0;
13655#ifdef FEAT_SYN_HL
13656 linenr_T lnum;
13657 colnr_T col;
13658 int trans;
13659 int transerr = FALSE;
13660
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013661 lnum = tv_get_lnum(argvars); /* -1 on type error */
13662 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13663 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013664
13665 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13666 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13667 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13668#endif
13669
13670 rettv->vval.v_number = id;
13671}
13672
13673/*
13674 * "synIDattr(id, what [, mode])" function
13675 */
13676 static void
13677f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13678{
13679 char_u *p = NULL;
13680#ifdef FEAT_SYN_HL
13681 int id;
13682 char_u *what;
13683 char_u *mode;
13684 char_u modebuf[NUMBUFLEN];
13685 int modec;
13686
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013687 id = (int)tv_get_number(&argvars[0]);
13688 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013689 if (argvars[2].v_type != VAR_UNKNOWN)
13690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013691 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013692 modec = TOLOWER_ASC(mode[0]);
13693 if (modec != 't' && modec != 'c' && modec != 'g')
13694 modec = 0; /* replace invalid with current */
13695 }
13696 else
13697 {
13698#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13699 if (USE_24BIT)
13700 modec = 'g';
13701 else
13702#endif
13703 if (t_colors > 1)
13704 modec = 'c';
13705 else
13706 modec = 't';
13707 }
13708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013709 switch (TOLOWER_ASC(what[0]))
13710 {
13711 case 'b':
13712 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13713 p = highlight_color(id, what, modec);
13714 else /* bold */
13715 p = highlight_has_attr(id, HL_BOLD, modec);
13716 break;
13717
13718 case 'f': /* fg[#] or font */
13719 p = highlight_color(id, what, modec);
13720 break;
13721
13722 case 'i':
13723 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13724 p = highlight_has_attr(id, HL_INVERSE, modec);
13725 else /* italic */
13726 p = highlight_has_attr(id, HL_ITALIC, modec);
13727 break;
13728
13729 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013730 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013731 break;
13732
13733 case 'r': /* reverse */
13734 p = highlight_has_attr(id, HL_INVERSE, modec);
13735 break;
13736
13737 case 's':
13738 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13739 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013740 /* strikeout */
13741 else if (TOLOWER_ASC(what[1]) == 't' &&
13742 TOLOWER_ASC(what[2]) == 'r')
13743 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013744 else /* standout */
13745 p = highlight_has_attr(id, HL_STANDOUT, modec);
13746 break;
13747
13748 case 'u':
13749 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13750 /* underline */
13751 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13752 else
13753 /* undercurl */
13754 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13755 break;
13756 }
13757
13758 if (p != NULL)
13759 p = vim_strsave(p);
13760#endif
13761 rettv->v_type = VAR_STRING;
13762 rettv->vval.v_string = p;
13763}
13764
13765/*
13766 * "synIDtrans(id)" function
13767 */
13768 static void
13769f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13770{
13771 int id;
13772
13773#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013774 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013775
13776 if (id > 0)
13777 id = syn_get_final_id(id);
13778 else
13779#endif
13780 id = 0;
13781
13782 rettv->vval.v_number = id;
13783}
13784
13785/*
13786 * "synconcealed(lnum, col)" function
13787 */
13788 static void
13789f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13790{
13791#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13792 linenr_T lnum;
13793 colnr_T col;
13794 int syntax_flags = 0;
13795 int cchar;
13796 int matchid = 0;
13797 char_u str[NUMBUFLEN];
13798#endif
13799
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013800 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013801
13802#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013803 lnum = tv_get_lnum(argvars); /* -1 on type error */
13804 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805
13806 vim_memset(str, NUL, sizeof(str));
13807
13808 if (rettv_list_alloc(rettv) != FAIL)
13809 {
13810 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13811 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13812 && curwin->w_p_cole > 0)
13813 {
13814 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13815 syntax_flags = get_syntax_info(&matchid);
13816
13817 /* get the conceal character */
13818 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13819 {
13820 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013821 if (cchar == NUL && curwin->w_p_cole == 1)
13822 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013823 if (cchar != NUL)
13824 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013825 if (has_mbyte)
13826 (*mb_char2bytes)(cchar, str);
13827 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013828 str[0] = cchar;
13829 }
13830 }
13831 }
13832
13833 list_append_number(rettv->vval.v_list,
13834 (syntax_flags & HL_CONCEAL) != 0);
13835 /* -1 to auto-determine strlen */
13836 list_append_string(rettv->vval.v_list, str, -1);
13837 list_append_number(rettv->vval.v_list, matchid);
13838 }
13839#endif
13840}
13841
13842/*
13843 * "synstack(lnum, col)" function
13844 */
13845 static void
13846f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13847{
13848#ifdef FEAT_SYN_HL
13849 linenr_T lnum;
13850 colnr_T col;
13851 int i;
13852 int id;
13853#endif
13854
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013855 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013856
13857#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013858 lnum = tv_get_lnum(argvars); /* -1 on type error */
13859 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860
13861 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13862 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13863 && rettv_list_alloc(rettv) != FAIL)
13864 {
13865 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13866 for (i = 0; ; ++i)
13867 {
13868 id = syn_get_stack_item(i);
13869 if (id < 0)
13870 break;
13871 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13872 break;
13873 }
13874 }
13875#endif
13876}
13877
13878 static void
13879get_cmd_output_as_rettv(
13880 typval_T *argvars,
13881 typval_T *rettv,
13882 int retlist)
13883{
13884 char_u *res = NULL;
13885 char_u *p;
13886 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013887 int err = FALSE;
13888 FILE *fd;
13889 list_T *list = NULL;
13890 int flags = SHELL_SILENT;
13891
13892 rettv->v_type = VAR_STRING;
13893 rettv->vval.v_string = NULL;
13894 if (check_restricted() || check_secure())
13895 goto errret;
13896
13897 if (argvars[1].v_type != VAR_UNKNOWN)
13898 {
13899 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013900 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013901 * command.
13902 */
13903 if ((infile = vim_tempname('i', TRUE)) == NULL)
13904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013905 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013906 goto errret;
13907 }
13908
13909 fd = mch_fopen((char *)infile, WRITEBIN);
13910 if (fd == NULL)
13911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013912 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013913 goto errret;
13914 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013915 if (argvars[1].v_type == VAR_NUMBER)
13916 {
13917 linenr_T lnum;
13918 buf_T *buf;
13919
13920 buf = buflist_findnr(argvars[1].vval.v_number);
13921 if (buf == NULL)
13922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013923 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013924 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013925 goto errret;
13926 }
13927
13928 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13929 {
13930 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13931 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13932 {
13933 err = TRUE;
13934 break;
13935 }
13936 if (putc(NL, fd) == EOF)
13937 {
13938 err = TRUE;
13939 break;
13940 }
13941 }
13942 }
13943 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013944 {
13945 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13946 err = TRUE;
13947 }
13948 else
13949 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013950 size_t len;
13951 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013952
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013953 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013954 if (p == NULL)
13955 {
13956 fclose(fd);
13957 goto errret; /* type error; errmsg already given */
13958 }
13959 len = STRLEN(p);
13960 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13961 err = TRUE;
13962 }
13963 if (fclose(fd) != 0)
13964 err = TRUE;
13965 if (err)
13966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013967 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013968 goto errret;
13969 }
13970 }
13971
13972 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13973 * echoes typeahead, that messes up the display. */
13974 if (!msg_silent)
13975 flags += SHELL_COOKED;
13976
13977 if (retlist)
13978 {
13979 int len;
13980 listitem_T *li;
13981 char_u *s = NULL;
13982 char_u *start;
13983 char_u *end;
13984 int i;
13985
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013986 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013987 if (res == NULL)
13988 goto errret;
13989
13990 list = list_alloc();
13991 if (list == NULL)
13992 goto errret;
13993
13994 for (i = 0; i < len; ++i)
13995 {
13996 start = res + i;
13997 while (i < len && res[i] != NL)
13998 ++i;
13999 end = res + i;
14000
Bram Moolenaar964b3742019-05-24 18:54:09 +020014001 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014002 if (s == NULL)
14003 goto errret;
14004
14005 for (p = s; start < end; ++p, ++start)
14006 *p = *start == NUL ? NL : *start;
14007 *p = NUL;
14008
14009 li = listitem_alloc();
14010 if (li == NULL)
14011 {
14012 vim_free(s);
14013 goto errret;
14014 }
14015 li->li_tv.v_type = VAR_STRING;
14016 li->li_tv.v_lock = 0;
14017 li->li_tv.vval.v_string = s;
14018 list_append(list, li);
14019 }
14020
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014021 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022 list = NULL;
14023 }
14024 else
14025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014026 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014027#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014028 /* translate <CR><NL> into <NL> */
14029 if (res != NULL)
14030 {
14031 char_u *s, *d;
14032
14033 d = res;
14034 for (s = res; *s; ++s)
14035 {
14036 if (s[0] == CAR && s[1] == NL)
14037 ++s;
14038 *d++ = *s;
14039 }
14040 *d = NUL;
14041 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014042#endif
14043 rettv->vval.v_string = res;
14044 res = NULL;
14045 }
14046
14047errret:
14048 if (infile != NULL)
14049 {
14050 mch_remove(infile);
14051 vim_free(infile);
14052 }
14053 if (res != NULL)
14054 vim_free(res);
14055 if (list != NULL)
14056 list_free(list);
14057}
14058
14059/*
14060 * "system()" function
14061 */
14062 static void
14063f_system(typval_T *argvars, typval_T *rettv)
14064{
14065 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14066}
14067
14068/*
14069 * "systemlist()" function
14070 */
14071 static void
14072f_systemlist(typval_T *argvars, typval_T *rettv)
14073{
14074 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14075}
14076
14077/*
14078 * "tabpagebuflist()" function
14079 */
14080 static void
14081f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14082{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014083 tabpage_T *tp;
14084 win_T *wp = NULL;
14085
14086 if (argvars[0].v_type == VAR_UNKNOWN)
14087 wp = firstwin;
14088 else
14089 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014090 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014091 if (tp != NULL)
14092 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14093 }
14094 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14095 {
14096 for (; wp != NULL; wp = wp->w_next)
14097 if (list_append_number(rettv->vval.v_list,
14098 wp->w_buffer->b_fnum) == FAIL)
14099 break;
14100 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014101}
14102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014103/*
14104 * "tabpagenr()" function
14105 */
14106 static void
14107f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14108{
14109 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014110 char_u *arg;
14111
14112 if (argvars[0].v_type != VAR_UNKNOWN)
14113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014114 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 nr = 0;
14116 if (arg != NULL)
14117 {
14118 if (STRCMP(arg, "$") == 0)
14119 nr = tabpage_index(NULL) - 1;
14120 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014121 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014122 }
14123 }
14124 else
14125 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014126 rettv->vval.v_number = nr;
14127}
14128
14129
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130/*
14131 * Common code for tabpagewinnr() and winnr().
14132 */
14133 static int
14134get_winnr(tabpage_T *tp, typval_T *argvar)
14135{
14136 win_T *twin;
14137 int nr = 1;
14138 win_T *wp;
14139 char_u *arg;
14140
14141 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14142 if (argvar->v_type != VAR_UNKNOWN)
14143 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014144 int invalid_arg = FALSE;
14145
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014146 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 if (arg == NULL)
14148 nr = 0; /* type error; errmsg already given */
14149 else if (STRCMP(arg, "$") == 0)
14150 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14151 else if (STRCMP(arg, "#") == 0)
14152 {
14153 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14154 if (twin == NULL)
14155 nr = 0;
14156 }
14157 else
14158 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014159 long count;
14160 char_u *endp;
14161
14162 // Extract the window count (if specified). e.g. winnr('3j')
14163 count = strtol((char *)arg, (char **)&endp, 10);
14164 if (count <= 0)
14165 count = 1; // if count is not specified, default to 1
14166 if (endp != NULL && *endp != '\0')
14167 {
14168 if (STRCMP(endp, "j") == 0)
14169 twin = win_vert_neighbor(tp, twin, FALSE, count);
14170 else if (STRCMP(endp, "k") == 0)
14171 twin = win_vert_neighbor(tp, twin, TRUE, count);
14172 else if (STRCMP(endp, "h") == 0)
14173 twin = win_horz_neighbor(tp, twin, TRUE, count);
14174 else if (STRCMP(endp, "l") == 0)
14175 twin = win_horz_neighbor(tp, twin, FALSE, count);
14176 else
14177 invalid_arg = TRUE;
14178 }
14179 else
14180 invalid_arg = TRUE;
14181 }
14182
14183 if (invalid_arg)
14184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014185 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186 nr = 0;
14187 }
14188 }
14189
14190 if (nr > 0)
14191 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14192 wp != twin; wp = wp->w_next)
14193 {
14194 if (wp == NULL)
14195 {
14196 /* didn't find it in this tabpage */
14197 nr = 0;
14198 break;
14199 }
14200 ++nr;
14201 }
14202 return nr;
14203}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014204
14205/*
14206 * "tabpagewinnr()" function
14207 */
14208 static void
14209f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14210{
14211 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014212 tabpage_T *tp;
14213
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014214 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014215 if (tp == NULL)
14216 nr = 0;
14217 else
14218 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014219 rettv->vval.v_number = nr;
14220}
14221
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014222/*
14223 * "tagfiles()" function
14224 */
14225 static void
14226f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14227{
14228 char_u *fname;
14229 tagname_T tn;
14230 int first;
14231
14232 if (rettv_list_alloc(rettv) == FAIL)
14233 return;
14234 fname = alloc(MAXPATHL);
14235 if (fname == NULL)
14236 return;
14237
14238 for (first = TRUE; ; first = FALSE)
14239 if (get_tagfname(&tn, first, fname) == FAIL
14240 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14241 break;
14242 tagname_free(&tn);
14243 vim_free(fname);
14244}
14245
14246/*
14247 * "taglist()" function
14248 */
14249 static void
14250f_taglist(typval_T *argvars, typval_T *rettv)
14251{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014252 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014253 char_u *tag_pattern;
14254
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014255 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014256
14257 rettv->vval.v_number = FALSE;
14258 if (*tag_pattern == NUL)
14259 return;
14260
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014261 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014262 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014263 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014264 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014265}
14266
14267/*
14268 * "tempname()" function
14269 */
14270 static void
14271f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14272{
14273 static int x = 'A';
14274
14275 rettv->v_type = VAR_STRING;
14276 rettv->vval.v_string = vim_tempname(x, FALSE);
14277
14278 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14279 * names. Skip 'I' and 'O', they are used for shell redirection. */
14280 do
14281 {
14282 if (x == 'Z')
14283 x = '0';
14284 else if (x == '9')
14285 x = 'A';
14286 else
14287 {
14288#ifdef EBCDIC
14289 if (x == 'I')
14290 x = 'J';
14291 else if (x == 'R')
14292 x = 'S';
14293 else
14294#endif
14295 ++x;
14296 }
14297 } while (x == 'I' || x == 'O');
14298}
14299
14300#ifdef FEAT_FLOAT
14301/*
14302 * "tan()" function
14303 */
14304 static void
14305f_tan(typval_T *argvars, typval_T *rettv)
14306{
14307 float_T f = 0.0;
14308
14309 rettv->v_type = VAR_FLOAT;
14310 if (get_float_arg(argvars, &f) == OK)
14311 rettv->vval.v_float = tan(f);
14312 else
14313 rettv->vval.v_float = 0.0;
14314}
14315
14316/*
14317 * "tanh()" function
14318 */
14319 static void
14320f_tanh(typval_T *argvars, typval_T *rettv)
14321{
14322 float_T f = 0.0;
14323
14324 rettv->v_type = VAR_FLOAT;
14325 if (get_float_arg(argvars, &f) == OK)
14326 rettv->vval.v_float = tanh(f);
14327 else
14328 rettv->vval.v_float = 0.0;
14329}
14330#endif
14331
14332/*
14333 * "test_alloc_fail(id, countdown, repeat)" function
14334 */
14335 static void
14336f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14337{
14338 if (argvars[0].v_type != VAR_NUMBER
14339 || argvars[0].vval.v_number <= 0
14340 || argvars[1].v_type != VAR_NUMBER
14341 || argvars[1].vval.v_number < 0
14342 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014343 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014344 else
14345 {
14346 alloc_fail_id = argvars[0].vval.v_number;
14347 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014348 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014349 alloc_fail_countdown = argvars[1].vval.v_number;
14350 alloc_fail_repeat = argvars[2].vval.v_number;
14351 did_outofmem_msg = FALSE;
14352 }
14353}
14354
14355/*
14356 * "test_autochdir()"
14357 */
14358 static void
14359f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14360{
14361#if defined(FEAT_AUTOCHDIR)
14362 test_autochdir = TRUE;
14363#endif
14364}
14365
14366/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014367 * "test_feedinput()"
14368 */
14369 static void
14370f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14371{
14372#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014373 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014374
14375 if (val != NULL)
14376 {
14377 trash_input_buf();
14378 add_to_input_buf_csi(val, (int)STRLEN(val));
14379 }
14380#endif
14381}
14382
14383/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014384 * "test_getvalue({name})" function
14385 */
14386 static void
14387f_test_getvalue(typval_T *argvars, typval_T *rettv)
14388{
14389 if (argvars[0].v_type != VAR_STRING)
14390 emsg(_(e_invarg));
14391 else
14392 {
14393 char_u *name = tv_get_string(&argvars[0]);
14394
14395 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14396 rettv->vval.v_number = need_fileinfo;
14397 else
14398 semsg(_(e_invarg2), name);
14399 }
14400}
14401
14402/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014403 * "test_option_not_set({name})" function
14404 */
14405 static void
14406f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14407{
14408 char_u *name = (char_u *)"";
14409
14410 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014411 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014412 else
14413 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014414 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014415 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014416 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014417 }
14418}
14419
14420/*
14421 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014422 */
14423 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014424f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014425{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014426 char_u *name = (char_u *)"";
14427 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014428 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014429
14430 if (argvars[0].v_type != VAR_STRING
14431 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014432 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014433 else
14434 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014435 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014436 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014437
14438 if (STRCMP(name, (char_u *)"redraw") == 0)
14439 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014440 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14441 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014442 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14443 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014444 else if (STRCMP(name, (char_u *)"starting") == 0)
14445 {
14446 if (val)
14447 {
14448 if (save_starting < 0)
14449 save_starting = starting;
14450 starting = 0;
14451 }
14452 else
14453 {
14454 starting = save_starting;
14455 save_starting = -1;
14456 }
14457 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014458 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14459 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014460 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14461 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014462 else if (STRCMP(name, (char_u *)"ALL") == 0)
14463 {
14464 disable_char_avail_for_testing = FALSE;
14465 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014466 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014467 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014468 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014469 if (save_starting >= 0)
14470 {
14471 starting = save_starting;
14472 save_starting = -1;
14473 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014474 }
14475 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014476 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014477 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014478}
14479
14480/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014481 * "test_refcount({expr})" function
14482 */
14483 static void
14484f_test_refcount(typval_T *argvars, typval_T *rettv)
14485{
14486 int retval = -1;
14487
14488 switch (argvars[0].v_type)
14489 {
14490 case VAR_UNKNOWN:
14491 case VAR_NUMBER:
14492 case VAR_FLOAT:
14493 case VAR_SPECIAL:
14494 case VAR_STRING:
14495 break;
14496 case VAR_JOB:
14497#ifdef FEAT_JOB_CHANNEL
14498 if (argvars[0].vval.v_job != NULL)
14499 retval = argvars[0].vval.v_job->jv_refcount - 1;
14500#endif
14501 break;
14502 case VAR_CHANNEL:
14503#ifdef FEAT_JOB_CHANNEL
14504 if (argvars[0].vval.v_channel != NULL)
14505 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14506#endif
14507 break;
14508 case VAR_FUNC:
14509 if (argvars[0].vval.v_string != NULL)
14510 {
14511 ufunc_T *fp;
14512
14513 fp = find_func(argvars[0].vval.v_string);
14514 if (fp != NULL)
14515 retval = fp->uf_refcount;
14516 }
14517 break;
14518 case VAR_PARTIAL:
14519 if (argvars[0].vval.v_partial != NULL)
14520 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14521 break;
14522 case VAR_BLOB:
14523 if (argvars[0].vval.v_blob != NULL)
14524 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14525 break;
14526 case VAR_LIST:
14527 if (argvars[0].vval.v_list != NULL)
14528 retval = argvars[0].vval.v_list->lv_refcount - 1;
14529 break;
14530 case VAR_DICT:
14531 if (argvars[0].vval.v_dict != NULL)
14532 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14533 break;
14534 }
14535
14536 rettv->v_type = VAR_NUMBER;
14537 rettv->vval.v_number = retval;
14538
14539}
14540
14541/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014542 * "test_garbagecollect_now()" function
14543 */
14544 static void
14545f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14546{
14547 /* This is dangerous, any Lists and Dicts used internally may be freed
14548 * while still in use. */
14549 garbage_collect(TRUE);
14550}
14551
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014552/*
14553 * "test_ignore_error()" function
14554 */
14555 static void
14556f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14557{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014558 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014559}
14560
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014561 static void
14562f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14563{
14564 rettv->v_type = VAR_BLOB;
14565 rettv->vval.v_blob = NULL;
14566}
14567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014568#ifdef FEAT_JOB_CHANNEL
14569 static void
14570f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14571{
14572 rettv->v_type = VAR_CHANNEL;
14573 rettv->vval.v_channel = NULL;
14574}
14575#endif
14576
14577 static void
14578f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14579{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014580 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581}
14582
14583#ifdef FEAT_JOB_CHANNEL
14584 static void
14585f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14586{
14587 rettv->v_type = VAR_JOB;
14588 rettv->vval.v_job = NULL;
14589}
14590#endif
14591
14592 static void
14593f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14594{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014595 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014596}
14597
14598 static void
14599f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14600{
14601 rettv->v_type = VAR_PARTIAL;
14602 rettv->vval.v_partial = NULL;
14603}
14604
14605 static void
14606f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14607{
14608 rettv->v_type = VAR_STRING;
14609 rettv->vval.v_string = NULL;
14610}
14611
Bram Moolenaarab186732018-09-14 21:27:06 +020014612#ifdef FEAT_GUI
14613 static void
14614f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14615{
14616 char_u *which;
14617 long value;
14618 int dragging;
14619 scrollbar_T *sb = NULL;
14620
14621 if (argvars[0].v_type != VAR_STRING
14622 || (argvars[1].v_type) != VAR_NUMBER
14623 || (argvars[2].v_type) != VAR_NUMBER)
14624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014625 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014626 return;
14627 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014628 which = tv_get_string(&argvars[0]);
14629 value = tv_get_number(&argvars[1]);
14630 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014631
14632 if (STRCMP(which, "left") == 0)
14633 sb = &curwin->w_scrollbars[SBAR_LEFT];
14634 else if (STRCMP(which, "right") == 0)
14635 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14636 else if (STRCMP(which, "hor") == 0)
14637 sb = &gui.bottom_sbar;
14638 if (sb == NULL)
14639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014640 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014641 return;
14642 }
14643 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014644# ifndef USE_ON_FLY_SCROLL
14645 // need to loop through normal_cmd() to handle the scroll events
14646 exec_normal(FALSE, TRUE, FALSE);
14647# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014648}
14649#endif
14650
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014651#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014652 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014653f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14654{
14655 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14656 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14657}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014658#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014659
14660 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14662{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014663 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664}
14665
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666/*
14667 * Get a callback from "arg". It can be a Funcref or a function name.
14668 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014669 * "cb_name" is not allocated.
14670 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014671 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014672 callback_T
14673get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014674{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014675 callback_T res;
14676
14677 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14679 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014680 res.cb_partial = arg->vval.v_partial;
14681 ++res.cb_partial->pt_refcount;
14682 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014683 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014684 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014685 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014686 res.cb_partial = NULL;
14687 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14688 {
14689 // Note that we don't make a copy of the string.
14690 res.cb_name = arg->vval.v_string;
14691 func_ref(res.cb_name);
14692 }
14693 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14694 {
14695 res.cb_name = (char_u *)"";
14696 }
14697 else
14698 {
14699 emsg(_("E921: Invalid callback argument"));
14700 res.cb_name = NULL;
14701 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014703 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704}
14705
14706/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014707 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708 */
14709 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014710put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014712 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014713 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014714 tv->v_type = VAR_PARTIAL;
14715 tv->vval.v_partial = cb->cb_partial;
14716 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014718 else
14719 {
14720 tv->v_type = VAR_FUNC;
14721 tv->vval.v_string = vim_strsave(cb->cb_name);
14722 func_ref(cb->cb_name);
14723 }
14724}
14725
14726/*
14727 * Make a copy of "src" into "dest", allocating the function name if needed,
14728 * without incrementing the refcount.
14729 */
14730 void
14731set_callback(callback_T *dest, callback_T *src)
14732{
14733 if (src->cb_partial == NULL)
14734 {
14735 // just a function name, make a copy
14736 dest->cb_name = vim_strsave(src->cb_name);
14737 dest->cb_free_name = TRUE;
14738 }
14739 else
14740 {
14741 // cb_name is a pointer into cb_partial
14742 dest->cb_name = src->cb_name;
14743 dest->cb_free_name = FALSE;
14744 }
14745 dest->cb_partial = src->cb_partial;
14746}
14747
14748/*
14749 * Unref/free "callback" returned by get_callback() or set_callback().
14750 */
14751 void
14752free_callback(callback_T *callback)
14753{
14754 if (callback->cb_partial != NULL)
14755 {
14756 partial_unref(callback->cb_partial);
14757 callback->cb_partial = NULL;
14758 }
14759 else if (callback->cb_name != NULL)
14760 func_unref(callback->cb_name);
14761 if (callback->cb_free_name)
14762 {
14763 vim_free(callback->cb_name);
14764 callback->cb_free_name = FALSE;
14765 }
14766 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014767}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014768
14769#ifdef FEAT_TIMERS
14770/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014771 * "timer_info([timer])" function
14772 */
14773 static void
14774f_timer_info(typval_T *argvars, typval_T *rettv)
14775{
14776 timer_T *timer = NULL;
14777
14778 if (rettv_list_alloc(rettv) != OK)
14779 return;
14780 if (argvars[0].v_type != VAR_UNKNOWN)
14781 {
14782 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014783 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014784 else
14785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014786 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014787 if (timer != NULL)
14788 add_timer_info(rettv, timer);
14789 }
14790 }
14791 else
14792 add_timer_info_all(rettv);
14793}
14794
14795/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014796 * "timer_pause(timer, paused)" function
14797 */
14798 static void
14799f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14800{
14801 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014802 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014803
14804 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014805 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014806 else
14807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014808 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014809 if (timer != NULL)
14810 timer->tr_paused = paused;
14811 }
14812}
14813
14814/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815 * "timer_start(time, callback [, options])" function
14816 */
14817 static void
14818f_timer_start(typval_T *argvars, typval_T *rettv)
14819{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014820 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014821 timer_T *timer;
14822 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014823 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014824 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014825
Bram Moolenaar75537a92016-09-05 22:45:28 +020014826 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014827 if (check_secure())
14828 return;
14829 if (argvars[2].v_type != VAR_UNKNOWN)
14830 {
14831 if (argvars[2].v_type != VAR_DICT
14832 || (dict = argvars[2].vval.v_dict) == NULL)
14833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014834 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014835 return;
14836 }
14837 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014838 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014839 }
14840
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014841 callback = get_callback(&argvars[1]);
14842 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014843 return;
14844
14845 timer = create_timer(msec, repeat);
14846 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014847 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014848 else
14849 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014850 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014851 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014852 }
14853}
14854
14855/*
14856 * "timer_stop(timer)" function
14857 */
14858 static void
14859f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14860{
14861 timer_T *timer;
14862
14863 if (argvars[0].v_type != VAR_NUMBER)
14864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014865 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014866 return;
14867 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014868 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014869 if (timer != NULL)
14870 stop_timer(timer);
14871}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014872
14873/*
14874 * "timer_stopall()" function
14875 */
14876 static void
14877f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14878{
14879 stop_all_timers();
14880}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014881#endif
14882
14883/*
14884 * "tolower(string)" function
14885 */
14886 static void
14887f_tolower(typval_T *argvars, typval_T *rettv)
14888{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014889 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014890 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014891}
14892
14893/*
14894 * "toupper(string)" function
14895 */
14896 static void
14897f_toupper(typval_T *argvars, typval_T *rettv)
14898{
14899 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014900 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014901}
14902
14903/*
14904 * "tr(string, fromstr, tostr)" function
14905 */
14906 static void
14907f_tr(typval_T *argvars, typval_T *rettv)
14908{
14909 char_u *in_str;
14910 char_u *fromstr;
14911 char_u *tostr;
14912 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014913 int inlen;
14914 int fromlen;
14915 int tolen;
14916 int idx;
14917 char_u *cpstr;
14918 int cplen;
14919 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014920 char_u buf[NUMBUFLEN];
14921 char_u buf2[NUMBUFLEN];
14922 garray_T ga;
14923
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014924 in_str = tv_get_string(&argvars[0]);
14925 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14926 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014927
14928 /* Default return value: empty string. */
14929 rettv->v_type = VAR_STRING;
14930 rettv->vval.v_string = NULL;
14931 if (fromstr == NULL || tostr == NULL)
14932 return; /* type error; errmsg already given */
14933 ga_init2(&ga, (int)sizeof(char), 80);
14934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014935 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014936 /* not multi-byte: fromstr and tostr must be the same length */
14937 if (STRLEN(fromstr) != STRLEN(tostr))
14938 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014939error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014940 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014941 ga_clear(&ga);
14942 return;
14943 }
14944
14945 /* fromstr and tostr have to contain the same number of chars */
14946 while (*in_str != NUL)
14947 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014948 if (has_mbyte)
14949 {
14950 inlen = (*mb_ptr2len)(in_str);
14951 cpstr = in_str;
14952 cplen = inlen;
14953 idx = 0;
14954 for (p = fromstr; *p != NUL; p += fromlen)
14955 {
14956 fromlen = (*mb_ptr2len)(p);
14957 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14958 {
14959 for (p = tostr; *p != NUL; p += tolen)
14960 {
14961 tolen = (*mb_ptr2len)(p);
14962 if (idx-- == 0)
14963 {
14964 cplen = tolen;
14965 cpstr = p;
14966 break;
14967 }
14968 }
14969 if (*p == NUL) /* tostr is shorter than fromstr */
14970 goto error;
14971 break;
14972 }
14973 ++idx;
14974 }
14975
14976 if (first && cpstr == in_str)
14977 {
14978 /* Check that fromstr and tostr have the same number of
14979 * (multi-byte) characters. Done only once when a character
14980 * of in_str doesn't appear in fromstr. */
14981 first = FALSE;
14982 for (p = tostr; *p != NUL; p += tolen)
14983 {
14984 tolen = (*mb_ptr2len)(p);
14985 --idx;
14986 }
14987 if (idx != 0)
14988 goto error;
14989 }
14990
14991 (void)ga_grow(&ga, cplen);
14992 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14993 ga.ga_len += cplen;
14994
14995 in_str += inlen;
14996 }
14997 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014998 {
14999 /* When not using multi-byte chars we can do it faster. */
15000 p = vim_strchr(fromstr, *in_str);
15001 if (p != NULL)
15002 ga_append(&ga, tostr[p - fromstr]);
15003 else
15004 ga_append(&ga, *in_str);
15005 ++in_str;
15006 }
15007 }
15008
15009 /* add a terminating NUL */
15010 (void)ga_grow(&ga, 1);
15011 ga_append(&ga, NUL);
15012
15013 rettv->vval.v_string = ga.ga_data;
15014}
15015
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015016/*
15017 * "trim({expr})" function
15018 */
15019 static void
15020f_trim(typval_T *argvars, typval_T *rettv)
15021{
15022 char_u buf1[NUMBUFLEN];
15023 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015024 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015025 char_u *mask = NULL;
15026 char_u *tail;
15027 char_u *prev;
15028 char_u *p;
15029 int c1;
15030
15031 rettv->v_type = VAR_STRING;
15032 if (head == NULL)
15033 {
15034 rettv->vval.v_string = NULL;
15035 return;
15036 }
15037
15038 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015039 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015040
15041 while (*head != NUL)
15042 {
15043 c1 = PTR2CHAR(head);
15044 if (mask == NULL)
15045 {
15046 if (c1 > ' ' && c1 != 0xa0)
15047 break;
15048 }
15049 else
15050 {
15051 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15052 if (c1 == PTR2CHAR(p))
15053 break;
15054 if (*p == NUL)
15055 break;
15056 }
15057 MB_PTR_ADV(head);
15058 }
15059
15060 for (tail = head + STRLEN(head); tail > head; tail = prev)
15061 {
15062 prev = tail;
15063 MB_PTR_BACK(head, prev);
15064 c1 = PTR2CHAR(prev);
15065 if (mask == NULL)
15066 {
15067 if (c1 > ' ' && c1 != 0xa0)
15068 break;
15069 }
15070 else
15071 {
15072 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15073 if (c1 == PTR2CHAR(p))
15074 break;
15075 if (*p == NUL)
15076 break;
15077 }
15078 }
15079 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15080}
15081
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015082#ifdef FEAT_FLOAT
15083/*
15084 * "trunc({float})" function
15085 */
15086 static void
15087f_trunc(typval_T *argvars, typval_T *rettv)
15088{
15089 float_T f = 0.0;
15090
15091 rettv->v_type = VAR_FLOAT;
15092 if (get_float_arg(argvars, &f) == OK)
15093 /* trunc() is not in C90, use floor() or ceil() instead. */
15094 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15095 else
15096 rettv->vval.v_float = 0.0;
15097}
15098#endif
15099
15100/*
15101 * "type(expr)" function
15102 */
15103 static void
15104f_type(typval_T *argvars, typval_T *rettv)
15105{
15106 int n = -1;
15107
15108 switch (argvars[0].v_type)
15109 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015110 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15111 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015112 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015113 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15114 case VAR_LIST: n = VAR_TYPE_LIST; break;
15115 case VAR_DICT: n = VAR_TYPE_DICT; break;
15116 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015117 case VAR_SPECIAL:
15118 if (argvars[0].vval.v_number == VVAL_FALSE
15119 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015120 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015121 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015122 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015123 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015124 case VAR_JOB: n = VAR_TYPE_JOB; break;
15125 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015126 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015127 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015128 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015129 n = -1;
15130 break;
15131 }
15132 rettv->vval.v_number = n;
15133}
15134
15135/*
15136 * "undofile(name)" function
15137 */
15138 static void
15139f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15140{
15141 rettv->v_type = VAR_STRING;
15142#ifdef FEAT_PERSISTENT_UNDO
15143 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015144 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015145
15146 if (*fname == NUL)
15147 {
15148 /* If there is no file name there will be no undo file. */
15149 rettv->vval.v_string = NULL;
15150 }
15151 else
15152 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015153 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015154
15155 if (ffname != NULL)
15156 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15157 vim_free(ffname);
15158 }
15159 }
15160#else
15161 rettv->vval.v_string = NULL;
15162#endif
15163}
15164
15165/*
15166 * "undotree()" function
15167 */
15168 static void
15169f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15170{
15171 if (rettv_dict_alloc(rettv) == OK)
15172 {
15173 dict_T *dict = rettv->vval.v_dict;
15174 list_T *list;
15175
Bram Moolenaare0be1672018-07-08 16:50:37 +020015176 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15177 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15178 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15179 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15180 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15181 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015182
15183 list = list_alloc();
15184 if (list != NULL)
15185 {
15186 u_eval_tree(curbuf->b_u_oldhead, list);
15187 dict_add_list(dict, "entries", list);
15188 }
15189 }
15190}
15191
15192/*
15193 * "values(dict)" function
15194 */
15195 static void
15196f_values(typval_T *argvars, typval_T *rettv)
15197{
15198 dict_list(argvars, rettv, 1);
15199}
15200
15201/*
15202 * "virtcol(string)" function
15203 */
15204 static void
15205f_virtcol(typval_T *argvars, typval_T *rettv)
15206{
15207 colnr_T vcol = 0;
15208 pos_T *fp;
15209 int fnum = curbuf->b_fnum;
15210
15211 fp = var2fpos(&argvars[0], FALSE, &fnum);
15212 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15213 && fnum == curbuf->b_fnum)
15214 {
15215 getvvcol(curwin, fp, NULL, NULL, &vcol);
15216 ++vcol;
15217 }
15218
15219 rettv->vval.v_number = vcol;
15220}
15221
15222/*
15223 * "visualmode()" function
15224 */
15225 static void
15226f_visualmode(typval_T *argvars, typval_T *rettv)
15227{
15228 char_u str[2];
15229
15230 rettv->v_type = VAR_STRING;
15231 str[0] = curbuf->b_visual_mode_eval;
15232 str[1] = NUL;
15233 rettv->vval.v_string = vim_strsave(str);
15234
15235 /* A non-zero number or non-empty string argument: reset mode. */
15236 if (non_zero_arg(&argvars[0]))
15237 curbuf->b_visual_mode_eval = NUL;
15238}
15239
15240/*
15241 * "wildmenumode()" function
15242 */
15243 static void
15244f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15245{
15246#ifdef FEAT_WILDMENU
15247 if (wild_menu_showing)
15248 rettv->vval.v_number = 1;
15249#endif
15250}
15251
15252/*
15253 * "winbufnr(nr)" function
15254 */
15255 static void
15256f_winbufnr(typval_T *argvars, typval_T *rettv)
15257{
15258 win_T *wp;
15259
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015260 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015261 if (wp == NULL)
15262 rettv->vval.v_number = -1;
15263 else
15264 rettv->vval.v_number = wp->w_buffer->b_fnum;
15265}
15266
15267/*
15268 * "wincol()" function
15269 */
15270 static void
15271f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15272{
15273 validate_cursor();
15274 rettv->vval.v_number = curwin->w_wcol + 1;
15275}
15276
15277/*
15278 * "winheight(nr)" function
15279 */
15280 static void
15281f_winheight(typval_T *argvars, typval_T *rettv)
15282{
15283 win_T *wp;
15284
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015285 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015286 if (wp == NULL)
15287 rettv->vval.v_number = -1;
15288 else
15289 rettv->vval.v_number = wp->w_height;
15290}
15291
15292/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015293 * "winlayout()" function
15294 */
15295 static void
15296f_winlayout(typval_T *argvars, typval_T *rettv)
15297{
15298 tabpage_T *tp;
15299
15300 if (rettv_list_alloc(rettv) != OK)
15301 return;
15302
15303 if (argvars[0].v_type == VAR_UNKNOWN)
15304 tp = curtab;
15305 else
15306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015307 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015308 if (tp == NULL)
15309 return;
15310 }
15311
15312 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15313}
15314
15315/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015316 * "winline()" function
15317 */
15318 static void
15319f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15320{
15321 validate_cursor();
15322 rettv->vval.v_number = curwin->w_wrow + 1;
15323}
15324
15325/*
15326 * "winnr()" function
15327 */
15328 static void
15329f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15330{
15331 int nr = 1;
15332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015333 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015334 rettv->vval.v_number = nr;
15335}
15336
15337/*
15338 * "winrestcmd()" function
15339 */
15340 static void
15341f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15342{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015343 win_T *wp;
15344 int winnr = 1;
15345 garray_T ga;
15346 char_u buf[50];
15347
15348 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015349 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350 {
15351 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15352 ga_concat(&ga, buf);
15353 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15354 ga_concat(&ga, buf);
15355 ++winnr;
15356 }
15357 ga_append(&ga, NUL);
15358
15359 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015360 rettv->v_type = VAR_STRING;
15361}
15362
15363/*
15364 * "winrestview()" function
15365 */
15366 static void
15367f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15368{
15369 dict_T *dict;
15370
15371 if (argvars[0].v_type != VAR_DICT
15372 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015373 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015374 else
15375 {
15376 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015377 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015378 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015379 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015380 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015381 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015382 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15383 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015384 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015385 curwin->w_set_curswant = FALSE;
15386 }
15387
15388 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015389 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015390#ifdef FEAT_DIFF
15391 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015392 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015393#endif
15394 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015395 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015396 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015397 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015398
15399 check_cursor();
15400 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015401 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015402 changed_window_setting();
15403
15404 if (curwin->w_topline <= 0)
15405 curwin->w_topline = 1;
15406 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15407 curwin->w_topline = curbuf->b_ml.ml_line_count;
15408#ifdef FEAT_DIFF
15409 check_topfill(curwin, TRUE);
15410#endif
15411 }
15412}
15413
15414/*
15415 * "winsaveview()" function
15416 */
15417 static void
15418f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15419{
15420 dict_T *dict;
15421
15422 if (rettv_dict_alloc(rettv) == FAIL)
15423 return;
15424 dict = rettv->vval.v_dict;
15425
Bram Moolenaare0be1672018-07-08 16:50:37 +020015426 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15427 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015428 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015429 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015430 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015431
Bram Moolenaare0be1672018-07-08 16:50:37 +020015432 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015433#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015434 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015435#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015436 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15437 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015438}
15439
15440/*
15441 * "winwidth(nr)" function
15442 */
15443 static void
15444f_winwidth(typval_T *argvars, typval_T *rettv)
15445{
15446 win_T *wp;
15447
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015448 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015449 if (wp == NULL)
15450 rettv->vval.v_number = -1;
15451 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015452 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015453}
15454
15455/*
15456 * "wordcount()" function
15457 */
15458 static void
15459f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15460{
15461 if (rettv_dict_alloc(rettv) == FAIL)
15462 return;
15463 cursor_pos_info(rettv->vval.v_dict);
15464}
15465
15466/*
15467 * "writefile()" function
15468 */
15469 static void
15470f_writefile(typval_T *argvars, typval_T *rettv)
15471{
15472 int binary = FALSE;
15473 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015474#ifdef HAVE_FSYNC
15475 int do_fsync = p_fs;
15476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015477 char_u *fname;
15478 FILE *fd;
15479 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015480 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015481 list_T *list = NULL;
15482 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015483
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015484 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015485 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015486 return;
15487
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015488 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015489 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015490 list = argvars[0].vval.v_list;
15491 if (list == NULL)
15492 return;
15493 for (li = list->lv_first; li != NULL; li = li->li_next)
15494 if (tv_get_string_chk(&li->li_tv) == NULL)
15495 return;
15496 }
15497 else if (argvars[0].v_type == VAR_BLOB)
15498 {
15499 blob = argvars[0].vval.v_blob;
15500 if (blob == NULL)
15501 return;
15502 }
15503 else
15504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015505 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015506 return;
15507 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015508
15509 if (argvars[2].v_type != VAR_UNKNOWN)
15510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015511 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015512
15513 if (arg2 == NULL)
15514 return;
15515 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015516 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015517 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015518 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015519#ifdef HAVE_FSYNC
15520 if (vim_strchr(arg2, 's') != NULL)
15521 do_fsync = TRUE;
15522 else if (vim_strchr(arg2, 'S') != NULL)
15523 do_fsync = FALSE;
15524#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015525 }
15526
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015527 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015528 if (fname == NULL)
15529 return;
15530
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015531 /* Always open the file in binary mode, library functions have a mind of
15532 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015533 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15534 append ? APPENDBIN : WRITEBIN)) == NULL)
15535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015536 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015537 ret = -1;
15538 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015539 else if (blob)
15540 {
15541 if (write_blob(fd, blob) == FAIL)
15542 ret = -1;
15543#ifdef HAVE_FSYNC
15544 else if (do_fsync)
15545 // Ignore the error, the user wouldn't know what to do about it.
15546 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015547 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015548#endif
15549 fclose(fd);
15550 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015551 else
15552 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015553 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015554 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015555#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015556 else if (do_fsync)
15557 /* Ignore the error, the user wouldn't know what to do about it.
15558 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015559 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015560#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015561 fclose(fd);
15562 }
15563
15564 rettv->vval.v_number = ret;
15565}
15566
15567/*
15568 * "xor(expr, expr)" function
15569 */
15570 static void
15571f_xor(typval_T *argvars, typval_T *rettv)
15572{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015573 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15574 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015575}
15576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015577#endif /* FEAT_EVAL */