blob: b98c7e72ec1fa5b661d321d0906d64430f7ec03d [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 Moolenaara42d9452019-06-15 21:46:30 +0200818 {"popup_dialog", 2, 2, f_popup_dialog},
Bram Moolenaara730e552019-06-16 19:05:31 +0200819 {"popup_filter_menu", 2, 2, f_popup_filter_menu},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200820 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200821 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200822 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200823 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaara730e552019-06-16 19:05:31 +0200824 {"popup_menu", 2, 2, f_popup_menu},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200825 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200826 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200827 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200828 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200829#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200830#ifdef FEAT_FLOAT
831 {"pow", 2, 2, f_pow},
832#endif
833 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100834 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200835#ifdef FEAT_JOB_CHANNEL
836 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200837 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200838 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
839#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100840#ifdef FEAT_TEXT_PROP
841 {"prop_add", 3, 3, f_prop_add},
842 {"prop_clear", 1, 3, f_prop_clear},
843 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100844 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100845 {"prop_type_add", 2, 2, f_prop_type_add},
846 {"prop_type_change", 2, 2, f_prop_type_change},
847 {"prop_type_delete", 1, 2, f_prop_type_delete},
848 {"prop_type_get", 1, 2, f_prop_type_get},
849 {"prop_type_list", 0, 1, f_prop_type_list},
850#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"pumvisible", 0, 0, f_pumvisible},
852#ifdef FEAT_PYTHON3
853 {"py3eval", 1, 1, f_py3eval},
854#endif
855#ifdef FEAT_PYTHON
856 {"pyeval", 1, 1, f_pyeval},
857#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100858#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
859 {"pyxeval", 1, 1, f_pyxeval},
860#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200862 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200864 {"reg_executing", 0, 0, f_reg_executing},
865 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"reltime", 0, 2, f_reltime},
867#ifdef FEAT_FLOAT
868 {"reltimefloat", 1, 1, f_reltimefloat},
869#endif
870 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100871 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"remote_foreground", 1, 1, f_remote_foreground},
873 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100874 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100876 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877 {"remove", 2, 3, f_remove},
878 {"rename", 2, 2, f_rename},
879 {"repeat", 2, 2, f_repeat},
880 {"resolve", 1, 1, f_resolve},
881 {"reverse", 1, 1, f_reverse},
882#ifdef FEAT_FLOAT
883 {"round", 1, 1, f_round},
884#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100885#ifdef FEAT_RUBY
886 {"rubyeval", 1, 1, f_rubyeval},
887#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888 {"screenattr", 2, 2, f_screenattr},
889 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100890 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"screencol", 0, 0, f_screencol},
892 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100893 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"search", 1, 4, f_search},
895 {"searchdecl", 1, 3, f_searchdecl},
896 {"searchpair", 3, 7, f_searchpair},
897 {"searchpairpos", 3, 7, f_searchpairpos},
898 {"searchpos", 1, 4, f_searchpos},
899 {"server2client", 2, 2, f_server2client},
900 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200901 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200902 {"setbufvar", 3, 3, f_setbufvar},
903 {"setcharsearch", 1, 1, f_setcharsearch},
904 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200905 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"setfperm", 2, 2, f_setfperm},
907 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200908 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100909 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200911 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200912 {"setreg", 2, 3, f_setreg},
913 {"settabvar", 3, 3, f_settabvar},
914 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100915 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200916 {"setwinvar", 3, 3, f_setwinvar},
917#ifdef FEAT_CRYPT
918 {"sha256", 1, 1, f_sha256},
919#endif
920 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100921 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100922#ifdef FEAT_SIGNS
923 {"sign_define", 1, 2, f_sign_define},
924 {"sign_getdefined", 0, 1, f_sign_getdefined},
925 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100926 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100927 {"sign_place", 4, 5, f_sign_place},
928 {"sign_undefine", 0, 1, f_sign_undefine},
929 {"sign_unplace", 1, 2, f_sign_unplace},
930#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200931 {"simplify", 1, 1, f_simplify},
932#ifdef FEAT_FLOAT
933 {"sin", 1, 1, f_sin},
934 {"sinh", 1, 1, f_sinh},
935#endif
936 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200937#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200938 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200939 {"sound_playevent", 1, 2, f_sound_playevent},
940 {"sound_playfile", 1, 2, f_sound_playfile},
941 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200942#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200943 {"soundfold", 1, 1, f_soundfold},
944 {"spellbadword", 0, 1, f_spellbadword},
945 {"spellsuggest", 1, 3, f_spellsuggest},
946 {"split", 1, 3, f_split},
947#ifdef FEAT_FLOAT
948 {"sqrt", 1, 1, f_sqrt},
949 {"str2float", 1, 1, f_str2float},
950#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200951 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"str2nr", 1, 2, f_str2nr},
953 {"strcharpart", 2, 3, f_strcharpart},
954 {"strchars", 1, 2, f_strchars},
955 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
956#ifdef HAVE_STRFTIME
957 {"strftime", 1, 2, f_strftime},
958#endif
959 {"strgetchar", 2, 2, f_strgetchar},
960 {"stridx", 2, 3, f_stridx},
961 {"string", 1, 1, f_string},
962 {"strlen", 1, 1, f_strlen},
963 {"strpart", 2, 3, f_strpart},
964 {"strridx", 2, 3, f_strridx},
965 {"strtrans", 1, 1, f_strtrans},
966 {"strwidth", 1, 1, f_strwidth},
967 {"submatch", 1, 2, f_submatch},
968 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200969 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200970 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200971 {"synID", 3, 3, f_synID},
972 {"synIDattr", 2, 3, f_synIDattr},
973 {"synIDtrans", 1, 1, f_synIDtrans},
974 {"synconcealed", 2, 2, f_synconcealed},
975 {"synstack", 2, 2, f_synstack},
976 {"system", 1, 2, f_system},
977 {"systemlist", 1, 2, f_systemlist},
978 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
979 {"tabpagenr", 0, 1, f_tabpagenr},
980 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
981 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100982 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200983#ifdef FEAT_FLOAT
984 {"tan", 1, 1, f_tan},
985 {"tanh", 1, 1, f_tanh},
986#endif
987 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100989 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
990 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100991 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200992 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200993# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
994 {"term_getansicolors", 1, 1, f_term_getansicolors},
995# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200997 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200998 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200999 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02001000 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001001 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +02001002 {"term_getstatus", 1, 1, f_term_getstatus},
1003 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001004 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001005 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001006 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001007 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001008# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1009 {"term_setansicolors", 2, 2, f_term_setansicolors},
1010# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001011 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001012 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001013 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001014 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001015 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001017 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1018 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001019 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001020 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001021 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001022 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001023 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001024#ifdef FEAT_JOB_CHANNEL
1025 {"test_null_channel", 0, 0, f_test_null_channel},
1026#endif
1027 {"test_null_dict", 0, 0, f_test_null_dict},
1028#ifdef FEAT_JOB_CHANNEL
1029 {"test_null_job", 0, 0, f_test_null_job},
1030#endif
1031 {"test_null_list", 0, 0, f_test_null_list},
1032 {"test_null_partial", 0, 0, f_test_null_partial},
1033 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001034 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001035 {"test_override", 2, 2, f_test_override},
1036 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001037#ifdef FEAT_GUI
1038 {"test_scrollbar", 3, 3, f_test_scrollbar},
1039#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001040#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001041 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001042#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001043 {"test_settime", 1, 1, f_test_settime},
1044#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001045 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001046 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001047 {"timer_start", 2, 3, f_timer_start},
1048 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001049 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001050#endif
1051 {"tolower", 1, 1, f_tolower},
1052 {"toupper", 1, 1, f_toupper},
1053 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001054 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001055#ifdef FEAT_FLOAT
1056 {"trunc", 1, 1, f_trunc},
1057#endif
1058 {"type", 1, 1, f_type},
1059 {"undofile", 1, 1, f_undofile},
1060 {"undotree", 0, 0, f_undotree},
1061 {"uniq", 1, 3, f_uniq},
1062 {"values", 1, 1, f_values},
1063 {"virtcol", 1, 1, f_virtcol},
1064 {"visualmode", 0, 1, f_visualmode},
1065 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001066 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001067 {"win_findbuf", 1, 1, f_win_findbuf},
1068 {"win_getid", 0, 2, f_win_getid},
1069 {"win_gotoid", 1, 1, f_win_gotoid},
1070 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1071 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001072 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001073 {"winbufnr", 1, 1, f_winbufnr},
1074 {"wincol", 0, 0, f_wincol},
1075 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001076 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001077 {"winline", 0, 0, f_winline},
1078 {"winnr", 0, 1, f_winnr},
1079 {"winrestcmd", 0, 0, f_winrestcmd},
1080 {"winrestview", 1, 1, f_winrestview},
1081 {"winsaveview", 0, 0, f_winsaveview},
1082 {"winwidth", 1, 1, f_winwidth},
1083 {"wordcount", 0, 0, f_wordcount},
1084 {"writefile", 2, 3, f_writefile},
1085 {"xor", 2, 2, f_xor},
1086};
1087
1088#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1089
1090/*
1091 * Function given to ExpandGeneric() to obtain the list of internal
1092 * or user defined function names.
1093 */
1094 char_u *
1095get_function_name(expand_T *xp, int idx)
1096{
1097 static int intidx = -1;
1098 char_u *name;
1099
1100 if (idx == 0)
1101 intidx = -1;
1102 if (intidx < 0)
1103 {
1104 name = get_user_func_name(xp, idx);
1105 if (name != NULL)
1106 return name;
1107 }
1108 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1109 {
1110 STRCPY(IObuff, functions[intidx].f_name);
1111 STRCAT(IObuff, "(");
1112 if (functions[intidx].f_max_argc == 0)
1113 STRCAT(IObuff, ")");
1114 return IObuff;
1115 }
1116
1117 return NULL;
1118}
1119
1120/*
1121 * Function given to ExpandGeneric() to obtain the list of internal or
1122 * user defined variable or function names.
1123 */
1124 char_u *
1125get_expr_name(expand_T *xp, int idx)
1126{
1127 static int intidx = -1;
1128 char_u *name;
1129
1130 if (idx == 0)
1131 intidx = -1;
1132 if (intidx < 0)
1133 {
1134 name = get_function_name(xp, idx);
1135 if (name != NULL)
1136 return name;
1137 }
1138 return get_user_var_name(xp, ++intidx);
1139}
1140
1141#endif /* FEAT_CMDL_COMPL */
1142
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001143/*
1144 * Find internal function in table above.
1145 * Return index, or -1 if not found
1146 */
1147 int
1148find_internal_func(
1149 char_u *name) /* name of the function */
1150{
1151 int first = 0;
1152 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1153 int cmp;
1154 int x;
1155
1156 /*
1157 * Find the function name in the table. Binary search.
1158 */
1159 while (first <= last)
1160 {
1161 x = first + ((unsigned)(last - first) >> 1);
1162 cmp = STRCMP(name, functions[x].f_name);
1163 if (cmp < 0)
1164 last = x - 1;
1165 else if (cmp > 0)
1166 first = x + 1;
1167 else
1168 return x;
1169 }
1170 return -1;
1171}
1172
1173 int
1174call_internal_func(
1175 char_u *name,
1176 int argcount,
1177 typval_T *argvars,
1178 typval_T *rettv)
1179{
1180 int i;
1181
1182 i = find_internal_func(name);
1183 if (i < 0)
1184 return ERROR_UNKNOWN;
1185 if (argcount < functions[i].f_min_argc)
1186 return ERROR_TOOFEW;
1187 if (argcount > functions[i].f_max_argc)
1188 return ERROR_TOOMANY;
1189 argvars[argcount].v_type = VAR_UNKNOWN;
1190 functions[i].f_func(argvars, rettv);
1191 return ERROR_NONE;
1192}
1193
1194/*
1195 * Return TRUE for a non-zero Number and a non-empty String.
1196 */
1197 static int
1198non_zero_arg(typval_T *argvars)
1199{
1200 return ((argvars[0].v_type == VAR_NUMBER
1201 && argvars[0].vval.v_number != 0)
1202 || (argvars[0].v_type == VAR_SPECIAL
1203 && argvars[0].vval.v_number == VVAL_TRUE)
1204 || (argvars[0].v_type == VAR_STRING
1205 && argvars[0].vval.v_string != NULL
1206 && *argvars[0].vval.v_string != NUL));
1207}
1208
1209/*
1210 * Get the lnum from the first argument.
1211 * Also accepts ".", "$", etc., but that only works for the current buffer.
1212 * Returns -1 on error.
1213 */
1214 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001215tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001216{
1217 typval_T rettv;
1218 linenr_T lnum;
1219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001220 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221 if (lnum == 0) /* no valid number, try using line() */
1222 {
1223 rettv.v_type = VAR_NUMBER;
1224 f_line(argvars, &rettv);
1225 lnum = (linenr_T)rettv.vval.v_number;
1226 clear_tv(&rettv);
1227 }
1228 return lnum;
1229}
1230
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001231/*
1232 * Get the lnum from the first argument.
1233 * Also accepts "$", then "buf" is used.
1234 * Returns 0 on error.
1235 */
1236 static linenr_T
1237tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1238{
1239 if (argvars[0].v_type == VAR_STRING
1240 && argvars[0].vval.v_string != NULL
1241 && argvars[0].vval.v_string[0] == '$'
1242 && buf != NULL)
1243 return buf->b_ml.ml_line_count;
1244 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1245}
1246
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001248/*
1249 * Get the float value of "argvars[0]" into "f".
1250 * Returns FAIL when the argument is not a Number or Float.
1251 */
1252 static int
1253get_float_arg(typval_T *argvars, float_T *f)
1254{
1255 if (argvars[0].v_type == VAR_FLOAT)
1256 {
1257 *f = argvars[0].vval.v_float;
1258 return OK;
1259 }
1260 if (argvars[0].v_type == VAR_NUMBER)
1261 {
1262 *f = (float_T)argvars[0].vval.v_number;
1263 return OK;
1264 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266 return FAIL;
1267}
1268
1269/*
1270 * "abs(expr)" function
1271 */
1272 static void
1273f_abs(typval_T *argvars, typval_T *rettv)
1274{
1275 if (argvars[0].v_type == VAR_FLOAT)
1276 {
1277 rettv->v_type = VAR_FLOAT;
1278 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1279 }
1280 else
1281 {
1282 varnumber_T n;
1283 int error = FALSE;
1284
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001285 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001286 if (error)
1287 rettv->vval.v_number = -1;
1288 else if (n > 0)
1289 rettv->vval.v_number = n;
1290 else
1291 rettv->vval.v_number = -n;
1292 }
1293}
1294
1295/*
1296 * "acos()" function
1297 */
1298 static void
1299f_acos(typval_T *argvars, typval_T *rettv)
1300{
1301 float_T f = 0.0;
1302
1303 rettv->v_type = VAR_FLOAT;
1304 if (get_float_arg(argvars, &f) == OK)
1305 rettv->vval.v_float = acos(f);
1306 else
1307 rettv->vval.v_float = 0.0;
1308}
1309#endif
1310
1311/*
1312 * "add(list, item)" function
1313 */
1314 static void
1315f_add(typval_T *argvars, typval_T *rettv)
1316{
1317 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319
1320 rettv->vval.v_number = 1; /* Default: Failed */
1321 if (argvars[0].v_type == VAR_LIST)
1322 {
1323 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001324 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001325 (char_u *)N_("add() argument"), TRUE)
1326 && list_append_tv(l, &argvars[1]) == OK)
1327 copy_tv(&argvars[0], rettv);
1328 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001329 else if (argvars[0].v_type == VAR_BLOB)
1330 {
1331 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001332 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001333 (char_u *)N_("add() argument"), TRUE))
1334 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001335 int error = FALSE;
1336 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1337
1338 if (!error)
1339 {
1340 ga_append(&b->bv_ga, (int)n);
1341 copy_tv(&argvars[0], rettv);
1342 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001343 }
1344 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001346 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001347}
1348
1349/*
1350 * "and(expr, expr)" function
1351 */
1352 static void
1353f_and(typval_T *argvars, typval_T *rettv)
1354{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001355 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1356 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001357}
1358
1359/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001360 * If there is a window for "curbuf", make it the current window.
1361 */
1362 static void
1363find_win_for_curbuf(void)
1364{
1365 wininfo_T *wip;
1366
1367 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1368 {
1369 if (wip->wi_win != NULL)
1370 {
1371 curwin = wip->wi_win;
1372 break;
1373 }
1374 }
1375}
1376
1377/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001379 */
1380 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001381set_buffer_lines(
1382 buf_T *buf,
1383 linenr_T lnum_arg,
1384 int append,
1385 typval_T *lines,
1386 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387{
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1389 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 list_T *l = NULL;
1391 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001393 linenr_T append_lnum;
1394 buf_T *curbuf_save = NULL;
1395 win_T *curwin_save = NULL;
1396 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397
Bram Moolenaarca851592018-06-06 21:04:07 +02001398 /* When using the current buffer ml_mfp will be set if needed. Useful when
1399 * setline() is used on startup. For other buffers the buffer must be
1400 * loaded. */
1401 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 rettv->vval.v_number = 1; /* FAIL */
1404 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001405 }
1406
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001408 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001409 curbuf_save = curbuf;
1410 curwin_save = curwin;
1411 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001412 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001413 }
1414
1415 if (append)
1416 // appendbufline() uses the line number below which we insert
1417 append_lnum = lnum - 1;
1418 else
1419 // setbufline() uses the line number above which we insert, we only
1420 // append if it's below the last line
1421 append_lnum = curbuf->b_ml.ml_line_count;
1422
1423 if (lines->v_type == VAR_LIST)
1424 {
1425 l = lines->vval.v_list;
1426 li = l->lv_first;
1427 }
1428 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001429 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001430
1431 /* default result is zero == OK */
1432 for (;;)
1433 {
1434 if (l != NULL)
1435 {
1436 /* list argument, get next string */
1437 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001439 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001440 li = li->li_next;
1441 }
1442
Bram Moolenaarca851592018-06-06 21:04:07 +02001443 rettv->vval.v_number = 1; /* FAIL */
1444 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1445 break;
1446
1447 /* When coming here from Insert mode, sync undo, so that this can be
1448 * undone separately from what was previously inserted. */
1449 if (u_sync_once == 2)
1450 {
1451 u_sync_once = 1; /* notify that u_sync() was called */
1452 u_sync(TRUE);
1453 }
1454
1455 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1456 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001457 // Existing line, replace it.
1458 // Removes any existing text properties.
1459 if (u_savesub(lnum) == OK && ml_replace_len(
1460 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001461 {
1462 changed_bytes(lnum, 0);
1463 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1464 check_cursor_col();
1465 rettv->vval.v_number = 0; /* OK */
1466 }
1467 }
1468 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1469 {
1470 /* append the line */
1471 ++added;
1472 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1473 rettv->vval.v_number = 0; /* OK */
1474 }
1475
1476 if (l == NULL) /* only one string argument */
1477 break;
1478 ++lnum;
1479 }
1480
1481 if (added > 0)
1482 {
1483 win_T *wp;
1484 tabpage_T *tp;
1485
1486 appended_lines_mark(append_lnum, added);
1487 FOR_ALL_TAB_WINDOWS(tp, wp)
1488 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1489 wp->w_cursor.lnum += added;
1490 check_cursor_col();
1491
Bram Moolenaarf2732452018-06-03 14:47:35 +02001492#ifdef FEAT_JOB_CHANNEL
1493 if (bt_prompt(curbuf) && (State & INSERT))
1494 // show the line with the prompt
1495 update_topline();
1496#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001497 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001498
1499 if (!is_curbuf)
1500 {
1501 curbuf = curbuf_save;
1502 curwin = curwin_save;
1503 }
1504}
1505
1506/*
1507 * "append(lnum, string/list)" function
1508 */
1509 static void
1510f_append(typval_T *argvars, typval_T *rettv)
1511{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001513
1514 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1515}
1516
1517/*
1518 * "appendbufline(buf, lnum, string/list)" function
1519 */
1520 static void
1521f_appendbufline(typval_T *argvars, typval_T *rettv)
1522{
1523 linenr_T lnum;
1524 buf_T *buf;
1525
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001526 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001527 if (buf == NULL)
1528 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001529 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001530 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001531 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001532 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1533 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001534}
1535
1536/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001538 */
1539 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001540f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001542 win_T *wp;
1543
1544 if (argvars[0].v_type == VAR_UNKNOWN)
1545 // use the current window
1546 rettv->vval.v_number = ARGCOUNT;
1547 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001548 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001549 // use the global argument list
1550 rettv->vval.v_number = GARGCOUNT;
1551 else
1552 {
1553 // use the argument list of the specified window
1554 wp = find_win_by_nr_or_id(&argvars[0]);
1555 if (wp != NULL)
1556 rettv->vval.v_number = WARGCOUNT(wp);
1557 else
1558 rettv->vval.v_number = -1;
1559 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001560}
1561
1562/*
1563 * "argidx()" function
1564 */
1565 static void
1566f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1567{
1568 rettv->vval.v_number = curwin->w_arg_idx;
1569}
1570
1571/*
1572 * "arglistid()" function
1573 */
1574 static void
1575f_arglistid(typval_T *argvars, typval_T *rettv)
1576{
1577 win_T *wp;
1578
1579 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001580 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 if (wp != NULL)
1582 rettv->vval.v_number = wp->w_alist->id;
1583}
1584
1585/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001586 * Get the argument list for a given window
1587 */
1588 static void
1589get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1590{
1591 int idx;
1592
1593 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1594 for (idx = 0; idx < argcount; ++idx)
1595 list_append_string(rettv->vval.v_list,
1596 alist_name(&arglist[idx]), -1);
1597}
1598
1599/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001600 * "argv(nr)" function
1601 */
1602 static void
1603f_argv(typval_T *argvars, typval_T *rettv)
1604{
1605 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001606 aentry_T *arglist = NULL;
1607 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001608
1609 if (argvars[0].v_type != VAR_UNKNOWN)
1610 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001611 if (argvars[1].v_type == VAR_UNKNOWN)
1612 {
1613 arglist = ARGLIST;
1614 argcount = ARGCOUNT;
1615 }
1616 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001617 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001618 {
1619 arglist = GARGLIST;
1620 argcount = GARGCOUNT;
1621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001623 {
1624 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1625
1626 if (wp != NULL)
1627 {
1628 /* Use the argument list of the specified window */
1629 arglist = WARGLIST(wp);
1630 argcount = WARGCOUNT(wp);
1631 }
1632 }
1633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001634 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001635 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001636 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001637 if (arglist != NULL && idx >= 0 && idx < argcount)
1638 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1639 else if (idx == -1)
1640 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001642 else
1643 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001644}
1645
1646/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001647 * "assert_beeps(cmd [, error])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001653}
1654
1655/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656 * "assert_equal(expected, actual[, msg])" function
1657 */
1658 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662}
1663
1664/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001665 * "assert_equalfile(fname-one, fname-two)" function
1666 */
1667 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001669{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001671}
1672
1673/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674 * "assert_notequal(expected, actual[, msg])" function
1675 */
1676 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680}
1681
1682/*
1683 * "assert_exception(string[, msg])" function
1684 */
1685 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001689}
1690
1691/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001692 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693 */
1694 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698}
1699
1700/*
1701 * "assert_false(actual[, msg])" function
1702 */
1703 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707}
1708
1709/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001710 * "assert_inrange(lower, upper[, msg])" function
1711 */
1712 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001714{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001716}
1717
1718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719 * "assert_match(pattern, actual[, msg])" function
1720 */
1721 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001724 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725}
1726
1727/*
1728 * "assert_notmatch(pattern, actual[, msg])" function
1729 */
1730 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001731f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001733 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001734}
1735
1736/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001737 * "assert_report(msg)" function
1738 */
1739 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001740f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001741{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001742 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001743}
1744
1745/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746 * "assert_true(actual[, msg])" function
1747 */
1748 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001749f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001750{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001751 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001752}
1753
1754#ifdef FEAT_FLOAT
1755/*
1756 * "asin()" function
1757 */
1758 static void
1759f_asin(typval_T *argvars, typval_T *rettv)
1760{
1761 float_T f = 0.0;
1762
1763 rettv->v_type = VAR_FLOAT;
1764 if (get_float_arg(argvars, &f) == OK)
1765 rettv->vval.v_float = asin(f);
1766 else
1767 rettv->vval.v_float = 0.0;
1768}
1769
1770/*
1771 * "atan()" function
1772 */
1773 static void
1774f_atan(typval_T *argvars, typval_T *rettv)
1775{
1776 float_T f = 0.0;
1777
1778 rettv->v_type = VAR_FLOAT;
1779 if (get_float_arg(argvars, &f) == OK)
1780 rettv->vval.v_float = atan(f);
1781 else
1782 rettv->vval.v_float = 0.0;
1783}
1784
1785/*
1786 * "atan2()" function
1787 */
1788 static void
1789f_atan2(typval_T *argvars, typval_T *rettv)
1790{
1791 float_T fx = 0.0, fy = 0.0;
1792
1793 rettv->v_type = VAR_FLOAT;
1794 if (get_float_arg(argvars, &fx) == OK
1795 && get_float_arg(&argvars[1], &fy) == OK)
1796 rettv->vval.v_float = atan2(fx, fy);
1797 else
1798 rettv->vval.v_float = 0.0;
1799}
1800#endif
1801
1802/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001803 * "balloon_show()" function
1804 */
1805#ifdef FEAT_BEVAL
1806 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001807f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1808{
1809 rettv->v_type = VAR_STRING;
1810 if (balloonEval != NULL)
1811 {
1812 if (balloonEval->msg == NULL)
1813 rettv->vval.v_string = NULL;
1814 else
1815 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1816 }
1817}
1818
1819 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001820f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1821{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001822 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001823 {
1824 if (argvars[0].v_type == VAR_LIST
1825# ifdef FEAT_GUI
1826 && !gui.in_use
1827# endif
1828 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001829 {
1830 list_T *l = argvars[0].vval.v_list;
1831
1832 // empty list removes the balloon
1833 post_balloon(balloonEval, NULL,
1834 l == NULL || l->lv_len == 0 ? NULL : l);
1835 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001836 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001837 {
1838 char_u *mesg = tv_get_string_chk(&argvars[0]);
1839
1840 if (mesg != NULL)
1841 // empty string removes the balloon
1842 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1843 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001844 }
1845}
1846
Bram Moolenaar669a8282017-11-19 20:13:05 +01001847# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001848 static void
1849f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1850{
1851 if (rettv_list_alloc(rettv) == OK)
1852 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001853 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001854
1855 if (msg != NULL)
1856 {
1857 pumitem_T *array;
1858 int size = split_message(msg, &array);
1859 int i;
1860
1861 /* Skip the first and last item, they are always empty. */
1862 for (i = 1; i < size - 1; ++i)
1863 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001864 while (size > 0)
1865 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001866 vim_free(array);
1867 }
1868 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001869}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001870# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001871#endif
1872
1873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001874 * "browse(save, title, initdir, default)" function
1875 */
1876 static void
1877f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1878{
1879#ifdef FEAT_BROWSE
1880 int save;
1881 char_u *title;
1882 char_u *initdir;
1883 char_u *defname;
1884 char_u buf[NUMBUFLEN];
1885 char_u buf2[NUMBUFLEN];
1886 int error = FALSE;
1887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001888 save = (int)tv_get_number_chk(&argvars[0], &error);
1889 title = tv_get_string_chk(&argvars[1]);
1890 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1891 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001892
1893 if (error || title == NULL || initdir == NULL || defname == NULL)
1894 rettv->vval.v_string = NULL;
1895 else
1896 rettv->vval.v_string =
1897 do_browse(save ? BROWSE_SAVE : 0,
1898 title, defname, NULL, initdir, NULL, curbuf);
1899#else
1900 rettv->vval.v_string = NULL;
1901#endif
1902 rettv->v_type = VAR_STRING;
1903}
1904
1905/*
1906 * "browsedir(title, initdir)" function
1907 */
1908 static void
1909f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1910{
1911#ifdef FEAT_BROWSE
1912 char_u *title;
1913 char_u *initdir;
1914 char_u buf[NUMBUFLEN];
1915
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 title = tv_get_string_chk(&argvars[0]);
1917 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001918
1919 if (title == NULL || initdir == NULL)
1920 rettv->vval.v_string = NULL;
1921 else
1922 rettv->vval.v_string = do_browse(BROWSE_DIR,
1923 title, NULL, NULL, initdir, NULL, curbuf);
1924#else
1925 rettv->vval.v_string = NULL;
1926#endif
1927 rettv->v_type = VAR_STRING;
1928}
1929
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001930/*
1931 * Find a buffer by number or exact name.
1932 */
1933 static buf_T *
1934find_buffer(typval_T *avar)
1935{
1936 buf_T *buf = NULL;
1937
1938 if (avar->v_type == VAR_NUMBER)
1939 buf = buflist_findnr((int)avar->vval.v_number);
1940 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1941 {
1942 buf = buflist_findname_exp(avar->vval.v_string);
1943 if (buf == NULL)
1944 {
1945 /* No full path name match, try a match with a URL or a "nofile"
1946 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001947 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001948 if (buf->b_fname != NULL
1949 && (path_with_url(buf->b_fname)
1950#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001951 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001952#endif
1953 )
1954 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1955 break;
1956 }
1957 }
1958 return buf;
1959}
1960
1961/*
1962 * "bufexists(expr)" function
1963 */
1964 static void
1965f_bufexists(typval_T *argvars, typval_T *rettv)
1966{
1967 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1968}
1969
1970/*
1971 * "buflisted(expr)" function
1972 */
1973 static void
1974f_buflisted(typval_T *argvars, typval_T *rettv)
1975{
1976 buf_T *buf;
1977
1978 buf = find_buffer(&argvars[0]);
1979 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1980}
1981
1982/*
1983 * "bufloaded(expr)" function
1984 */
1985 static void
1986f_bufloaded(typval_T *argvars, typval_T *rettv)
1987{
1988 buf_T *buf;
1989
1990 buf = find_buffer(&argvars[0]);
1991 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1992}
1993
1994 buf_T *
1995buflist_find_by_name(char_u *name, int curtab_only)
1996{
1997 int save_magic;
1998 char_u *save_cpo;
1999 buf_T *buf;
2000
2001 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
2002 save_magic = p_magic;
2003 p_magic = TRUE;
2004 save_cpo = p_cpo;
2005 p_cpo = (char_u *)"";
2006
2007 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2008 TRUE, FALSE, curtab_only));
2009
2010 p_magic = save_magic;
2011 p_cpo = save_cpo;
2012 return buf;
2013}
2014
2015/*
2016 * Get buffer by number or pattern.
2017 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002018 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002019tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002020{
2021 char_u *name = tv->vval.v_string;
2022 buf_T *buf;
2023
2024 if (tv->v_type == VAR_NUMBER)
2025 return buflist_findnr((int)tv->vval.v_number);
2026 if (tv->v_type != VAR_STRING)
2027 return NULL;
2028 if (name == NULL || *name == NUL)
2029 return curbuf;
2030 if (name[0] == '$' && name[1] == NUL)
2031 return lastbuf;
2032
2033 buf = buflist_find_by_name(name, curtab_only);
2034
2035 /* If not found, try expanding the name, like done for bufexists(). */
2036 if (buf == NULL)
2037 buf = find_buffer(tv);
2038
2039 return buf;
2040}
2041
2042/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002043 * Get the buffer from "arg" and give an error and return NULL if it is not
2044 * valid.
2045 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002046 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002047get_buf_arg(typval_T *arg)
2048{
2049 buf_T *buf;
2050
2051 ++emsg_off;
2052 buf = tv_get_buf(arg, FALSE);
2053 --emsg_off;
2054 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002055 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002056 return buf;
2057}
2058
2059/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002060 * "bufname(expr)" function
2061 */
2062 static void
2063f_bufname(typval_T *argvars, typval_T *rettv)
2064{
2065 buf_T *buf;
2066
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002067 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002068 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002069 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002070 rettv->v_type = VAR_STRING;
2071 if (buf != NULL && buf->b_fname != NULL)
2072 rettv->vval.v_string = vim_strsave(buf->b_fname);
2073 else
2074 rettv->vval.v_string = NULL;
2075 --emsg_off;
2076}
2077
2078/*
2079 * "bufnr(expr)" function
2080 */
2081 static void
2082f_bufnr(typval_T *argvars, typval_T *rettv)
2083{
2084 buf_T *buf;
2085 int error = FALSE;
2086 char_u *name;
2087
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002088 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002089 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002090 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002091 --emsg_off;
2092
2093 /* If the buffer isn't found and the second argument is not zero create a
2094 * new buffer. */
2095 if (buf == NULL
2096 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002097 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002099 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 && !error)
2101 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2102
2103 if (buf != NULL)
2104 rettv->vval.v_number = buf->b_fnum;
2105 else
2106 rettv->vval.v_number = -1;
2107}
2108
2109 static void
2110buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2111{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 win_T *wp;
2113 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 buf_T *buf;
2115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002116 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002117 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002118 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002119 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120 {
2121 ++winnr;
2122 if (wp->w_buffer == buf)
2123 break;
2124 }
2125 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002126 --emsg_off;
2127}
2128
2129/*
2130 * "bufwinid(nr)" function
2131 */
2132 static void
2133f_bufwinid(typval_T *argvars, typval_T *rettv)
2134{
2135 buf_win_common(argvars, rettv, FALSE);
2136}
2137
2138/*
2139 * "bufwinnr(nr)" function
2140 */
2141 static void
2142f_bufwinnr(typval_T *argvars, typval_T *rettv)
2143{
2144 buf_win_common(argvars, rettv, TRUE);
2145}
2146
2147/*
2148 * "byte2line(byte)" function
2149 */
2150 static void
2151f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2152{
2153#ifndef FEAT_BYTEOFF
2154 rettv->vval.v_number = -1;
2155#else
2156 long boff = 0;
2157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002158 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 if (boff < 0)
2160 rettv->vval.v_number = -1;
2161 else
2162 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2163 (linenr_T)0, &boff);
2164#endif
2165}
2166
2167 static void
2168byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2169{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171 char_u *str;
2172 varnumber_T idx;
2173
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002174 str = tv_get_string_chk(&argvars[0]);
2175 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176 rettv->vval.v_number = -1;
2177 if (str == NULL || idx < 0)
2178 return;
2179
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002180 t = str;
2181 for ( ; idx > 0; idx--)
2182 {
2183 if (*t == NUL) /* EOL reached */
2184 return;
2185 if (enc_utf8 && comp)
2186 t += utf_ptr2len(t);
2187 else
2188 t += (*mb_ptr2len)(t);
2189 }
2190 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002191}
2192
2193/*
2194 * "byteidx()" function
2195 */
2196 static void
2197f_byteidx(typval_T *argvars, typval_T *rettv)
2198{
2199 byteidx(argvars, rettv, FALSE);
2200}
2201
2202/*
2203 * "byteidxcomp()" function
2204 */
2205 static void
2206f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2207{
2208 byteidx(argvars, rettv, TRUE);
2209}
2210
2211/*
2212 * "call(func, arglist [, dict])" function
2213 */
2214 static void
2215f_call(typval_T *argvars, typval_T *rettv)
2216{
2217 char_u *func;
2218 partial_T *partial = NULL;
2219 dict_T *selfdict = NULL;
2220
2221 if (argvars[1].v_type != VAR_LIST)
2222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002223 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002224 return;
2225 }
2226 if (argvars[1].vval.v_list == NULL)
2227 return;
2228
2229 if (argvars[0].v_type == VAR_FUNC)
2230 func = argvars[0].vval.v_string;
2231 else if (argvars[0].v_type == VAR_PARTIAL)
2232 {
2233 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002234 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002235 }
2236 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002237 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002238 if (*func == NUL)
2239 return; /* type error or empty name */
2240
2241 if (argvars[2].v_type != VAR_UNKNOWN)
2242 {
2243 if (argvars[2].v_type != VAR_DICT)
2244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002245 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002246 return;
2247 }
2248 selfdict = argvars[2].vval.v_dict;
2249 }
2250
2251 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2252}
2253
2254#ifdef FEAT_FLOAT
2255/*
2256 * "ceil({float})" function
2257 */
2258 static void
2259f_ceil(typval_T *argvars, typval_T *rettv)
2260{
2261 float_T f = 0.0;
2262
2263 rettv->v_type = VAR_FLOAT;
2264 if (get_float_arg(argvars, &f) == OK)
2265 rettv->vval.v_float = ceil(f);
2266 else
2267 rettv->vval.v_float = 0.0;
2268}
2269#endif
2270
2271#ifdef FEAT_JOB_CHANNEL
2272/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002273 * "ch_canread()" function
2274 */
2275 static void
2276f_ch_canread(typval_T *argvars, typval_T *rettv)
2277{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002278 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002279
2280 rettv->vval.v_number = 0;
2281 if (channel != NULL)
2282 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2283 || channel_has_readahead(channel, PART_OUT)
2284 || channel_has_readahead(channel, PART_ERR);
2285}
2286
2287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288 * "ch_close()" function
2289 */
2290 static void
2291f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2292{
2293 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2294
2295 if (channel != NULL)
2296 {
2297 channel_close(channel, FALSE);
2298 channel_clear(channel);
2299 }
2300}
2301
2302/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002303 * "ch_close()" function
2304 */
2305 static void
2306f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2307{
2308 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2309
2310 if (channel != NULL)
2311 channel_close_in(channel);
2312}
2313
2314/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002315 * "ch_getbufnr()" function
2316 */
2317 static void
2318f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2319{
2320 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2321
2322 rettv->vval.v_number = -1;
2323 if (channel != NULL)
2324 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002325 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002326 int part;
2327
2328 if (STRCMP(what, "err") == 0)
2329 part = PART_ERR;
2330 else if (STRCMP(what, "out") == 0)
2331 part = PART_OUT;
2332 else if (STRCMP(what, "in") == 0)
2333 part = PART_IN;
2334 else
2335 part = PART_SOCK;
2336 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2337 rettv->vval.v_number =
2338 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2339 }
2340}
2341
2342/*
2343 * "ch_getjob()" function
2344 */
2345 static void
2346f_ch_getjob(typval_T *argvars, typval_T *rettv)
2347{
2348 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2349
2350 if (channel != NULL)
2351 {
2352 rettv->v_type = VAR_JOB;
2353 rettv->vval.v_job = channel->ch_job;
2354 if (channel->ch_job != NULL)
2355 ++channel->ch_job->jv_refcount;
2356 }
2357}
2358
2359/*
2360 * "ch_info()" function
2361 */
2362 static void
2363f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2364{
2365 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2366
2367 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2368 channel_info(channel, rettv->vval.v_dict);
2369}
2370
2371/*
2372 * "ch_log()" function
2373 */
2374 static void
2375f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2376{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002377 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378 channel_T *channel = NULL;
2379
2380 if (argvars[1].v_type != VAR_UNKNOWN)
2381 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2382
Bram Moolenaard5359b22018-04-05 22:44:39 +02002383 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002384}
2385
2386/*
2387 * "ch_logfile()" function
2388 */
2389 static void
2390f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2391{
2392 char_u *fname;
2393 char_u *opt = (char_u *)"";
2394 char_u buf[NUMBUFLEN];
2395
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002396 /* Don't open a file in restricted mode. */
2397 if (check_restricted() || check_secure())
2398 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002399 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002401 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002402 ch_logfile(fname, opt);
2403}
2404
2405/*
2406 * "ch_open()" function
2407 */
2408 static void
2409f_ch_open(typval_T *argvars, typval_T *rettv)
2410{
2411 rettv->v_type = VAR_CHANNEL;
2412 if (check_restricted() || check_secure())
2413 return;
2414 rettv->vval.v_channel = channel_open_func(argvars);
2415}
2416
2417/*
2418 * "ch_read()" function
2419 */
2420 static void
2421f_ch_read(typval_T *argvars, typval_T *rettv)
2422{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002423 common_channel_read(argvars, rettv, FALSE, FALSE);
2424}
2425
2426/*
2427 * "ch_readblob()" function
2428 */
2429 static void
2430f_ch_readblob(typval_T *argvars, typval_T *rettv)
2431{
2432 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002433}
2434
2435/*
2436 * "ch_readraw()" function
2437 */
2438 static void
2439f_ch_readraw(typval_T *argvars, typval_T *rettv)
2440{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002441 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002442}
2443
2444/*
2445 * "ch_evalexpr()" function
2446 */
2447 static void
2448f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2449{
2450 ch_expr_common(argvars, rettv, TRUE);
2451}
2452
2453/*
2454 * "ch_sendexpr()" function
2455 */
2456 static void
2457f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2458{
2459 ch_expr_common(argvars, rettv, FALSE);
2460}
2461
2462/*
2463 * "ch_evalraw()" function
2464 */
2465 static void
2466f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2467{
2468 ch_raw_common(argvars, rettv, TRUE);
2469}
2470
2471/*
2472 * "ch_sendraw()" function
2473 */
2474 static void
2475f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2476{
2477 ch_raw_common(argvars, rettv, FALSE);
2478}
2479
2480/*
2481 * "ch_setoptions()" function
2482 */
2483 static void
2484f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2485{
2486 channel_T *channel;
2487 jobopt_T opt;
2488
2489 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2490 if (channel == NULL)
2491 return;
2492 clear_job_options(&opt);
2493 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002494 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002495 channel_set_options(channel, &opt);
2496 free_job_options(&opt);
2497}
2498
2499/*
2500 * "ch_status()" function
2501 */
2502 static void
2503f_ch_status(typval_T *argvars, typval_T *rettv)
2504{
2505 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002506 jobopt_T opt;
2507 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002508
2509 /* return an empty string by default */
2510 rettv->v_type = VAR_STRING;
2511 rettv->vval.v_string = NULL;
2512
2513 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002514
2515 if (argvars[1].v_type != VAR_UNKNOWN)
2516 {
2517 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002518 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002519 && (opt.jo_set & JO_PART))
2520 part = opt.jo_part;
2521 }
2522
2523 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524}
2525#endif
2526
2527/*
2528 * "changenr()" function
2529 */
2530 static void
2531f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2532{
2533 rettv->vval.v_number = curbuf->b_u_seq_cur;
2534}
2535
2536/*
2537 * "char2nr(string)" function
2538 */
2539 static void
2540f_char2nr(typval_T *argvars, typval_T *rettv)
2541{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542 if (has_mbyte)
2543 {
2544 int utf8 = 0;
2545
2546 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548
2549 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002550 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002552 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002553 }
2554 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002555 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002556}
2557
2558/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002559 * "chdir(dir)" function
2560 */
2561 static void
2562f_chdir(typval_T *argvars, typval_T *rettv)
2563{
2564 char_u *cwd;
2565 cdscope_T scope = CDSCOPE_GLOBAL;
2566
2567 rettv->v_type = VAR_STRING;
2568 rettv->vval.v_string = NULL;
2569
2570 if (argvars[0].v_type != VAR_STRING)
2571 return;
2572
2573 // Return the current directory
2574 cwd = alloc(MAXPATHL);
2575 if (cwd != NULL)
2576 {
2577 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2578 {
2579#ifdef BACKSLASH_IN_FILENAME
2580 slash_adjust(cwd);
2581#endif
2582 rettv->vval.v_string = vim_strsave(cwd);
2583 }
2584 vim_free(cwd);
2585 }
2586
2587 if (curwin->w_localdir != NULL)
2588 scope = CDSCOPE_WINDOW;
2589 else if (curtab->tp_localdir != NULL)
2590 scope = CDSCOPE_TABPAGE;
2591
2592 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2593 // Directory change failed
2594 VIM_CLEAR(rettv->vval.v_string);
2595}
2596
2597/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002598 * "cindent(lnum)" function
2599 */
2600 static void
2601f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2602{
2603#ifdef FEAT_CINDENT
2604 pos_T pos;
2605 linenr_T lnum;
2606
2607 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002608 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002609 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2610 {
2611 curwin->w_cursor.lnum = lnum;
2612 rettv->vval.v_number = get_c_indent();
2613 curwin->w_cursor = pos;
2614 }
2615 else
2616#endif
2617 rettv->vval.v_number = -1;
2618}
2619
Bram Moolenaaraff74912019-03-30 18:11:49 +01002620 static win_T *
2621get_optional_window(typval_T *argvars, int idx)
2622{
2623 win_T *win = curwin;
2624
2625 if (argvars[idx].v_type != VAR_UNKNOWN)
2626 {
2627 win = find_win_by_nr_or_id(&argvars[idx]);
2628 if (win == NULL)
2629 {
2630 emsg(_(e_invalwindow));
2631 return NULL;
2632 }
2633 }
2634 return win;
2635}
2636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002637/*
2638 * "clearmatches()" function
2639 */
2640 static void
2641f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2642{
2643#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002644 win_T *win = get_optional_window(argvars, 0);
2645
2646 if (win != NULL)
2647 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648#endif
2649}
2650
2651/*
2652 * "col(string)" function
2653 */
2654 static void
2655f_col(typval_T *argvars, typval_T *rettv)
2656{
2657 colnr_T col = 0;
2658 pos_T *fp;
2659 int fnum = curbuf->b_fnum;
2660
2661 fp = var2fpos(&argvars[0], FALSE, &fnum);
2662 if (fp != NULL && fnum == curbuf->b_fnum)
2663 {
2664 if (fp->col == MAXCOL)
2665 {
2666 /* '> can be MAXCOL, get the length of the line then */
2667 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2668 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2669 else
2670 col = MAXCOL;
2671 }
2672 else
2673 {
2674 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 /* col(".") when the cursor is on the NUL at the end of the line
2676 * because of "coladd" can be seen as an extra column. */
2677 if (virtual_active() && fp == &curwin->w_cursor)
2678 {
2679 char_u *p = ml_get_cursor();
2680
2681 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2682 curwin->w_virtcol - curwin->w_cursor.coladd))
2683 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 int l;
2685
2686 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2687 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 }
2689 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 }
2691 }
2692 rettv->vval.v_number = col;
2693}
2694
2695#if defined(FEAT_INS_EXPAND)
2696/*
2697 * "complete()" function
2698 */
2699 static void
2700f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2701{
2702 int startcol;
2703
2704 if ((State & INSERT) == 0)
2705 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002706 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 return;
2708 }
2709
2710 /* Check for undo allowed here, because if something was already inserted
2711 * the line was already saved for undo and this check isn't done. */
2712 if (!undo_allowed())
2713 return;
2714
2715 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2716 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002717 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002718 return;
2719 }
2720
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002721 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002722 if (startcol <= 0)
2723 return;
2724
2725 set_completion(startcol - 1, argvars[1].vval.v_list);
2726}
2727
2728/*
2729 * "complete_add()" function
2730 */
2731 static void
2732f_complete_add(typval_T *argvars, typval_T *rettv)
2733{
2734 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2735}
2736
2737/*
2738 * "complete_check()" function
2739 */
2740 static void
2741f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2742{
2743 int saved = RedrawingDisabled;
2744
2745 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002746 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002747 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002748 RedrawingDisabled = saved;
2749}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002750
2751/*
2752 * "complete_info()" function
2753 */
2754 static void
2755f_complete_info(typval_T *argvars, typval_T *rettv)
2756{
2757 list_T *what_list = NULL;
2758
2759 if (rettv_dict_alloc(rettv) != OK)
2760 return;
2761
2762 if (argvars[0].v_type != VAR_UNKNOWN)
2763 {
2764 if (argvars[0].v_type != VAR_LIST)
2765 {
2766 emsg(_(e_listreq));
2767 return;
2768 }
2769 what_list = argvars[0].vval.v_list;
2770 }
2771 get_complete_info(what_list, rettv->vval.v_dict);
2772}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773#endif
2774
2775/*
2776 * "confirm(message, buttons[, default [, type]])" function
2777 */
2778 static void
2779f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2780{
2781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2782 char_u *message;
2783 char_u *buttons = NULL;
2784 char_u buf[NUMBUFLEN];
2785 char_u buf2[NUMBUFLEN];
2786 int def = 1;
2787 int type = VIM_GENERIC;
2788 char_u *typestr;
2789 int error = FALSE;
2790
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002791 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792 if (message == NULL)
2793 error = TRUE;
2794 if (argvars[1].v_type != VAR_UNKNOWN)
2795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002796 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 if (buttons == NULL)
2798 error = TRUE;
2799 if (argvars[2].v_type != VAR_UNKNOWN)
2800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002801 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002802 if (argvars[3].v_type != VAR_UNKNOWN)
2803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002804 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002805 if (typestr == NULL)
2806 error = TRUE;
2807 else
2808 {
2809 switch (TOUPPER_ASC(*typestr))
2810 {
2811 case 'E': type = VIM_ERROR; break;
2812 case 'Q': type = VIM_QUESTION; break;
2813 case 'I': type = VIM_INFO; break;
2814 case 'W': type = VIM_WARNING; break;
2815 case 'G': type = VIM_GENERIC; break;
2816 }
2817 }
2818 }
2819 }
2820 }
2821
2822 if (buttons == NULL || *buttons == NUL)
2823 buttons = (char_u *)_("&Ok");
2824
2825 if (!error)
2826 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2827 def, NULL, FALSE);
2828#endif
2829}
2830
2831/*
2832 * "copy()" function
2833 */
2834 static void
2835f_copy(typval_T *argvars, typval_T *rettv)
2836{
2837 item_copy(&argvars[0], rettv, FALSE, 0);
2838}
2839
2840#ifdef FEAT_FLOAT
2841/*
2842 * "cos()" function
2843 */
2844 static void
2845f_cos(typval_T *argvars, typval_T *rettv)
2846{
2847 float_T f = 0.0;
2848
2849 rettv->v_type = VAR_FLOAT;
2850 if (get_float_arg(argvars, &f) == OK)
2851 rettv->vval.v_float = cos(f);
2852 else
2853 rettv->vval.v_float = 0.0;
2854}
2855
2856/*
2857 * "cosh()" function
2858 */
2859 static void
2860f_cosh(typval_T *argvars, typval_T *rettv)
2861{
2862 float_T f = 0.0;
2863
2864 rettv->v_type = VAR_FLOAT;
2865 if (get_float_arg(argvars, &f) == OK)
2866 rettv->vval.v_float = cosh(f);
2867 else
2868 rettv->vval.v_float = 0.0;
2869}
2870#endif
2871
2872/*
2873 * "count()" function
2874 */
2875 static void
2876f_count(typval_T *argvars, typval_T *rettv)
2877{
2878 long n = 0;
2879 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002880 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002881
Bram Moolenaar9966b212017-07-28 16:46:57 +02002882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002883 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002884
2885 if (argvars[0].v_type == VAR_STRING)
2886 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002887 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002888 char_u *p = argvars[0].vval.v_string;
2889 char_u *next;
2890
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002891 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002892 {
2893 if (ic)
2894 {
2895 size_t len = STRLEN(expr);
2896
2897 while (*p != NUL)
2898 {
2899 if (MB_STRNICMP(p, expr, len) == 0)
2900 {
2901 ++n;
2902 p += len;
2903 }
2904 else
2905 MB_PTR_ADV(p);
2906 }
2907 }
2908 else
2909 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2910 != NULL)
2911 {
2912 ++n;
2913 p = next + STRLEN(expr);
2914 }
2915 }
2916
2917 }
2918 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002919 {
2920 listitem_T *li;
2921 list_T *l;
2922 long idx;
2923
2924 if ((l = argvars[0].vval.v_list) != NULL)
2925 {
2926 li = l->lv_first;
2927 if (argvars[2].v_type != VAR_UNKNOWN)
2928 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 if (argvars[3].v_type != VAR_UNKNOWN)
2930 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002931 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 if (!error)
2933 {
2934 li = list_find(l, idx);
2935 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002936 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 }
2938 }
2939 if (error)
2940 li = NULL;
2941 }
2942
2943 for ( ; li != NULL; li = li->li_next)
2944 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2945 ++n;
2946 }
2947 }
2948 else if (argvars[0].v_type == VAR_DICT)
2949 {
2950 int todo;
2951 dict_T *d;
2952 hashitem_T *hi;
2953
2954 if ((d = argvars[0].vval.v_dict) != NULL)
2955 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 if (argvars[2].v_type != VAR_UNKNOWN)
2957 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002960 }
2961
2962 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2963 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2964 {
2965 if (!HASHITEM_EMPTY(hi))
2966 {
2967 --todo;
2968 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2969 ++n;
2970 }
2971 }
2972 }
2973 }
2974 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002975 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002976 rettv->vval.v_number = n;
2977}
2978
2979/*
2980 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2981 *
2982 * Checks the existence of a cscope connection.
2983 */
2984 static void
2985f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2986{
2987#ifdef FEAT_CSCOPE
2988 int num = 0;
2989 char_u *dbpath = NULL;
2990 char_u *prepend = NULL;
2991 char_u buf[NUMBUFLEN];
2992
2993 if (argvars[0].v_type != VAR_UNKNOWN
2994 && argvars[1].v_type != VAR_UNKNOWN)
2995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002996 num = (int)tv_get_number(&argvars[0]);
2997 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002999 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000 }
3001
3002 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
3003#endif
3004}
3005
3006/*
3007 * "cursor(lnum, col)" function, or
3008 * "cursor(list)"
3009 *
3010 * Moves the cursor to the specified line and column.
3011 * Returns 0 when the position could be set, -1 otherwise.
3012 */
3013 static void
3014f_cursor(typval_T *argvars, typval_T *rettv)
3015{
3016 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 int set_curswant = TRUE;
3019
3020 rettv->vval.v_number = -1;
3021 if (argvars[1].v_type == VAR_UNKNOWN)
3022 {
3023 pos_T pos;
3024 colnr_T curswant = -1;
3025
3026 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003028 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 return;
3030 }
3031 line = pos.lnum;
3032 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034 if (curswant >= 0)
3035 {
3036 curwin->w_curswant = curswant - 1;
3037 set_curswant = FALSE;
3038 }
3039 }
3040 else
3041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003042 line = tv_get_lnum(argvars);
3043 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003045 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003047 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048 return; /* type error; errmsg already given */
3049 if (line > 0)
3050 curwin->w_cursor.lnum = line;
3051 if (col > 0)
3052 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003053 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054
3055 /* Make sure the cursor is in a valid position. */
3056 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003057 /* Correct cursor for multi-byte character. */
3058 if (has_mbyte)
3059 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003060
3061 curwin->w_set_curswant = set_curswant;
3062 rettv->vval.v_number = 0;
3063}
3064
Bram Moolenaar4f974752019-02-17 17:44:42 +01003065#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003066/*
3067 * "debugbreak()" function
3068 */
3069 static void
3070f_debugbreak(typval_T *argvars, typval_T *rettv)
3071{
3072 int pid;
3073
3074 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003075 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003076 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003077 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003078 else
3079 {
3080 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3081
3082 if (hProcess != NULL)
3083 {
3084 DebugBreakProcess(hProcess);
3085 CloseHandle(hProcess);
3086 rettv->vval.v_number = OK;
3087 }
3088 }
3089}
3090#endif
3091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003092/*
3093 * "deepcopy()" function
3094 */
3095 static void
3096f_deepcopy(typval_T *argvars, typval_T *rettv)
3097{
3098 int noref = 0;
3099 int copyID;
3100
3101 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003102 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003104 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003105 else
3106 {
3107 copyID = get_copyID();
3108 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3109 }
3110}
3111
3112/*
3113 * "delete()" function
3114 */
3115 static void
3116f_delete(typval_T *argvars, typval_T *rettv)
3117{
3118 char_u nbuf[NUMBUFLEN];
3119 char_u *name;
3120 char_u *flags;
3121
3122 rettv->vval.v_number = -1;
3123 if (check_restricted() || check_secure())
3124 return;
3125
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003126 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003127 if (name == NULL || *name == NUL)
3128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003129 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130 return;
3131 }
3132
3133 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003134 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003135 else
3136 flags = (char_u *)"";
3137
3138 if (*flags == NUL)
3139 /* delete a file */
3140 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3141 else if (STRCMP(flags, "d") == 0)
3142 /* delete an empty directory */
3143 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3144 else if (STRCMP(flags, "rf") == 0)
3145 /* delete a directory recursively */
3146 rettv->vval.v_number = delete_recursive(name);
3147 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003148 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003149}
3150
3151/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003152 * "deletebufline()" function
3153 */
3154 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003155f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003156{
3157 buf_T *buf;
3158 linenr_T first, last;
3159 linenr_T lnum;
3160 long count;
3161 int is_curbuf;
3162 buf_T *curbuf_save = NULL;
3163 win_T *curwin_save = NULL;
3164 tabpage_T *tp;
3165 win_T *wp;
3166
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003167 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003168 if (buf == NULL)
3169 {
3170 rettv->vval.v_number = 1; /* FAIL */
3171 return;
3172 }
3173 is_curbuf = buf == curbuf;
3174
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003175 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003176 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003177 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003178 else
3179 last = first;
3180
3181 if (buf->b_ml.ml_mfp == NULL || first < 1
3182 || first > buf->b_ml.ml_line_count || last < first)
3183 {
3184 rettv->vval.v_number = 1; /* FAIL */
3185 return;
3186 }
3187
3188 if (!is_curbuf)
3189 {
3190 curbuf_save = curbuf;
3191 curwin_save = curwin;
3192 curbuf = buf;
3193 find_win_for_curbuf();
3194 }
3195 if (last > curbuf->b_ml.ml_line_count)
3196 last = curbuf->b_ml.ml_line_count;
3197 count = last - first + 1;
3198
3199 // When coming here from Insert mode, sync undo, so that this can be
3200 // undone separately from what was previously inserted.
3201 if (u_sync_once == 2)
3202 {
3203 u_sync_once = 1; // notify that u_sync() was called
3204 u_sync(TRUE);
3205 }
3206
3207 if (u_save(first - 1, last + 1) == FAIL)
3208 {
3209 rettv->vval.v_number = 1; /* FAIL */
3210 return;
3211 }
3212
3213 for (lnum = first; lnum <= last; ++lnum)
3214 ml_delete(first, TRUE);
3215
3216 FOR_ALL_TAB_WINDOWS(tp, wp)
3217 if (wp->w_buffer == buf)
3218 {
3219 if (wp->w_cursor.lnum > last)
3220 wp->w_cursor.lnum -= count;
3221 else if (wp->w_cursor.lnum> first)
3222 wp->w_cursor.lnum = first;
3223 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3224 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3225 }
3226 check_cursor_col();
3227 deleted_lines_mark(first, count);
3228
3229 if (!is_curbuf)
3230 {
3231 curbuf = curbuf_save;
3232 curwin = curwin_save;
3233 }
3234}
3235
3236/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 * "did_filetype()" function
3238 */
3239 static void
3240f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3241{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003242 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003243}
3244
3245/*
3246 * "diff_filler()" function
3247 */
3248 static void
3249f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3250{
3251#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003252 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003253#endif
3254}
3255
3256/*
3257 * "diff_hlID()" function
3258 */
3259 static void
3260f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3261{
3262#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003263 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003265 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003266 static int fnum = 0;
3267 static int change_start = 0;
3268 static int change_end = 0;
3269 static hlf_T hlID = (hlf_T)0;
3270 int filler_lines;
3271 int col;
3272
3273 if (lnum < 0) /* ignore type error in {lnum} arg */
3274 lnum = 0;
3275 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003276 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003277 || fnum != curbuf->b_fnum)
3278 {
3279 /* New line, buffer, change: need to get the values. */
3280 filler_lines = diff_check(curwin, lnum);
3281 if (filler_lines < 0)
3282 {
3283 if (filler_lines == -1)
3284 {
3285 change_start = MAXCOL;
3286 change_end = -1;
3287 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3288 hlID = HLF_ADD; /* added line */
3289 else
3290 hlID = HLF_CHD; /* changed line */
3291 }
3292 else
3293 hlID = HLF_ADD; /* added line */
3294 }
3295 else
3296 hlID = (hlf_T)0;
3297 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003298 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 fnum = curbuf->b_fnum;
3300 }
3301
3302 if (hlID == HLF_CHD || hlID == HLF_TXD)
3303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003304 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003305 if (col >= change_start && col <= change_end)
3306 hlID = HLF_TXD; /* changed text */
3307 else
3308 hlID = HLF_CHD; /* changed line */
3309 }
3310 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3311#endif
3312}
3313
3314/*
3315 * "empty({expr})" function
3316 */
3317 static void
3318f_empty(typval_T *argvars, typval_T *rettv)
3319{
3320 int n = FALSE;
3321
3322 switch (argvars[0].v_type)
3323 {
3324 case VAR_STRING:
3325 case VAR_FUNC:
3326 n = argvars[0].vval.v_string == NULL
3327 || *argvars[0].vval.v_string == NUL;
3328 break;
3329 case VAR_PARTIAL:
3330 n = FALSE;
3331 break;
3332 case VAR_NUMBER:
3333 n = argvars[0].vval.v_number == 0;
3334 break;
3335 case VAR_FLOAT:
3336#ifdef FEAT_FLOAT
3337 n = argvars[0].vval.v_float == 0.0;
3338 break;
3339#endif
3340 case VAR_LIST:
3341 n = argvars[0].vval.v_list == NULL
3342 || argvars[0].vval.v_list->lv_first == NULL;
3343 break;
3344 case VAR_DICT:
3345 n = argvars[0].vval.v_dict == NULL
3346 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3347 break;
3348 case VAR_SPECIAL:
3349 n = argvars[0].vval.v_number != VVAL_TRUE;
3350 break;
3351
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003352 case VAR_BLOB:
3353 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003354 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3355 break;
3356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003357 case VAR_JOB:
3358#ifdef FEAT_JOB_CHANNEL
3359 n = argvars[0].vval.v_job == NULL
3360 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3361 break;
3362#endif
3363 case VAR_CHANNEL:
3364#ifdef FEAT_JOB_CHANNEL
3365 n = argvars[0].vval.v_channel == NULL
3366 || !channel_is_open(argvars[0].vval.v_channel);
3367 break;
3368#endif
3369 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003370 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003371 n = TRUE;
3372 break;
3373 }
3374
3375 rettv->vval.v_number = n;
3376}
3377
3378/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003379 * "environ()" function
3380 */
3381 static void
3382f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3383{
3384#if !defined(AMIGA)
3385 int i = 0;
3386 char_u *entry, *value;
3387# ifdef MSWIN
3388 extern wchar_t **_wenviron;
3389# else
3390 extern char **environ;
3391# endif
3392
3393 if (rettv_dict_alloc(rettv) != OK)
3394 return;
3395
3396# ifdef MSWIN
3397 if (*_wenviron == NULL)
3398 return;
3399# else
3400 if (*environ == NULL)
3401 return;
3402# endif
3403
3404 for (i = 0; ; ++i)
3405 {
3406# ifdef MSWIN
3407 short_u *p;
3408
3409 if ((p = (short_u *)_wenviron[i]) == NULL)
3410 return;
3411 entry = utf16_to_enc(p, NULL);
3412# else
3413 if ((entry = (char_u *)environ[i]) == NULL)
3414 return;
3415 entry = vim_strsave(entry);
3416# endif
3417 if (entry == NULL) // out of memory
3418 return;
3419 if ((value = vim_strchr(entry, '=')) == NULL)
3420 {
3421 vim_free(entry);
3422 continue;
3423 }
3424 *value++ = NUL;
3425 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3426 vim_free(entry);
3427 }
3428#endif
3429}
3430
3431/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003432 * "escape({string}, {chars})" function
3433 */
3434 static void
3435f_escape(typval_T *argvars, typval_T *rettv)
3436{
3437 char_u buf[NUMBUFLEN];
3438
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003439 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3440 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 rettv->v_type = VAR_STRING;
3442}
3443
3444/*
3445 * "eval()" function
3446 */
3447 static void
3448f_eval(typval_T *argvars, typval_T *rettv)
3449{
3450 char_u *s, *p;
3451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003452 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003453 if (s != NULL)
3454 s = skipwhite(s);
3455
3456 p = s;
3457 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3458 {
3459 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003460 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461 need_clr_eos = FALSE;
3462 rettv->v_type = VAR_NUMBER;
3463 rettv->vval.v_number = 0;
3464 }
3465 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003466 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003467}
3468
3469/*
3470 * "eventhandler()" function
3471 */
3472 static void
3473f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3474{
3475 rettv->vval.v_number = vgetc_busy;
3476}
3477
3478/*
3479 * "executable()" function
3480 */
3481 static void
3482f_executable(typval_T *argvars, typval_T *rettv)
3483{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003484 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003485
3486 /* Check in $PATH and also check directly if there is a directory name. */
3487 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3488 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3489}
3490
3491static garray_T redir_execute_ga;
3492
3493/*
3494 * Append "value[value_len]" to the execute() output.
3495 */
3496 void
3497execute_redir_str(char_u *value, int value_len)
3498{
3499 int len;
3500
3501 if (value_len == -1)
3502 len = (int)STRLEN(value); /* Append the entire string */
3503 else
3504 len = value_len; /* Append only "value_len" characters */
3505 if (ga_grow(&redir_execute_ga, len) == OK)
3506 {
3507 mch_memmove((char *)redir_execute_ga.ga_data
3508 + redir_execute_ga.ga_len, value, len);
3509 redir_execute_ga.ga_len += len;
3510 }
3511}
3512
3513/*
3514 * Get next line from a list.
3515 * Called by do_cmdline() to get the next line.
3516 * Returns allocated string, or NULL for end of function.
3517 */
3518
3519 static char_u *
3520get_list_line(
3521 int c UNUSED,
3522 void *cookie,
3523 int indent UNUSED)
3524{
3525 listitem_T **p = (listitem_T **)cookie;
3526 listitem_T *item = *p;
3527 char_u buf[NUMBUFLEN];
3528 char_u *s;
3529
3530 if (item == NULL)
3531 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003532 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 *p = item->li_next;
3534 return s == NULL ? NULL : vim_strsave(s);
3535}
3536
3537/*
3538 * "execute()" function
3539 */
3540 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003541execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003542{
3543 char_u *cmd = NULL;
3544 list_T *list = NULL;
3545 int save_msg_silent = msg_silent;
3546 int save_emsg_silent = emsg_silent;
3547 int save_emsg_noredir = emsg_noredir;
3548 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003549 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003550 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003551 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003552 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553
3554 rettv->vval.v_string = NULL;
3555 rettv->v_type = VAR_STRING;
3556
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003557 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003559 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003560 if (list == NULL || list->lv_first == NULL)
3561 /* empty list, no commands, empty output */
3562 return;
3563 ++list->lv_refcount;
3564 }
3565 else
3566 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003567 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 if (cmd == NULL)
3569 return;
3570 }
3571
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003572 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003573 {
3574 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003575 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576
3577 if (s == NULL)
3578 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003579 if (*s == NUL)
3580 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003581 if (STRNCMP(s, "silent", 6) == 0)
3582 ++msg_silent;
3583 if (STRCMP(s, "silent!") == 0)
3584 {
3585 emsg_silent = TRUE;
3586 emsg_noredir = TRUE;
3587 }
3588 }
3589 else
3590 ++msg_silent;
3591
3592 if (redir_execute)
3593 save_ga = redir_execute_ga;
3594 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3595 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003596 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003597 if (!echo_output)
3598 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003599
3600 if (cmd != NULL)
3601 do_cmdline_cmd(cmd);
3602 else
3603 {
3604 listitem_T *item = list->lv_first;
3605
3606 do_cmdline(NULL, get_list_line, (void *)&item,
3607 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3608 --list->lv_refcount;
3609 }
3610
Bram Moolenaard297f352017-01-29 20:31:21 +01003611 /* Need to append a NUL to the result. */
3612 if (ga_grow(&redir_execute_ga, 1) == OK)
3613 {
3614 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3615 rettv->vval.v_string = redir_execute_ga.ga_data;
3616 }
3617 else
3618 {
3619 ga_clear(&redir_execute_ga);
3620 rettv->vval.v_string = NULL;
3621 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003622 msg_silent = save_msg_silent;
3623 emsg_silent = save_emsg_silent;
3624 emsg_noredir = save_emsg_noredir;
3625
3626 redir_execute = save_redir_execute;
3627 if (redir_execute)
3628 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003629 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003630
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003631 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003632 if (echo_output)
3633 // When not working silently: put it in column zero. A following
3634 // "echon" will overwrite the message, unavoidably.
3635 msg_col = 0;
3636 else
3637 // When working silently: Put it back where it was, since nothing
3638 // should have been written.
3639 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003640}
3641
3642/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003643 * "execute()" function
3644 */
3645 static void
3646f_execute(typval_T *argvars, typval_T *rettv)
3647{
3648 execute_common(argvars, rettv, 0);
3649}
3650
3651/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003652 * "exepath()" function
3653 */
3654 static void
3655f_exepath(typval_T *argvars, typval_T *rettv)
3656{
3657 char_u *p = NULL;
3658
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003659 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003660 rettv->v_type = VAR_STRING;
3661 rettv->vval.v_string = p;
3662}
3663
3664/*
3665 * "exists()" function
3666 */
3667 static void
3668f_exists(typval_T *argvars, typval_T *rettv)
3669{
3670 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003671 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003673 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003674 if (*p == '$') /* environment variable */
3675 {
3676 /* first try "normal" environment variables (fast) */
3677 if (mch_getenv(p + 1) != NULL)
3678 n = TRUE;
3679 else
3680 {
3681 /* try expanding things like $VIM and ${HOME} */
3682 p = expand_env_save(p);
3683 if (p != NULL && *p != '$')
3684 n = TRUE;
3685 vim_free(p);
3686 }
3687 }
3688 else if (*p == '&' || *p == '+') /* option */
3689 {
3690 n = (get_option_tv(&p, NULL, TRUE) == OK);
3691 if (*skipwhite(p) != NUL)
3692 n = FALSE; /* trailing garbage */
3693 }
3694 else if (*p == '*') /* internal or user defined function */
3695 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003696 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 }
3698 else if (*p == ':')
3699 {
3700 n = cmd_exists(p + 1);
3701 }
3702 else if (*p == '#')
3703 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003704 if (p[1] == '#')
3705 n = autocmd_supported(p + 2);
3706 else
3707 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003708 }
3709 else /* internal variable */
3710 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003711 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003712 }
3713
3714 rettv->vval.v_number = n;
3715}
3716
3717#ifdef FEAT_FLOAT
3718/*
3719 * "exp()" function
3720 */
3721 static void
3722f_exp(typval_T *argvars, typval_T *rettv)
3723{
3724 float_T f = 0.0;
3725
3726 rettv->v_type = VAR_FLOAT;
3727 if (get_float_arg(argvars, &f) == OK)
3728 rettv->vval.v_float = exp(f);
3729 else
3730 rettv->vval.v_float = 0.0;
3731}
3732#endif
3733
3734/*
3735 * "expand()" function
3736 */
3737 static void
3738f_expand(typval_T *argvars, typval_T *rettv)
3739{
3740 char_u *s;
3741 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003742 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003743 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3744 expand_T xpc;
3745 int error = FALSE;
3746 char_u *result;
3747
3748 rettv->v_type = VAR_STRING;
3749 if (argvars[1].v_type != VAR_UNKNOWN
3750 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003751 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003753 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003755 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003756 if (*s == '%' || *s == '#' || *s == '<')
3757 {
3758 ++emsg_off;
3759 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3760 --emsg_off;
3761 if (rettv->v_type == VAR_LIST)
3762 {
3763 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3764 list_append_string(rettv->vval.v_list, result, -1);
3765 else
3766 vim_free(result);
3767 }
3768 else
3769 rettv->vval.v_string = result;
3770 }
3771 else
3772 {
3773 /* When the optional second argument is non-zero, don't remove matches
3774 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3775 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003776 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003777 options |= WILD_KEEP_ALL;
3778 if (!error)
3779 {
3780 ExpandInit(&xpc);
3781 xpc.xp_context = EXPAND_FILES;
3782 if (p_wic)
3783 options += WILD_ICASE;
3784 if (rettv->v_type == VAR_STRING)
3785 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3786 options, WILD_ALL);
3787 else if (rettv_list_alloc(rettv) != FAIL)
3788 {
3789 int i;
3790
3791 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3792 for (i = 0; i < xpc.xp_numfiles; i++)
3793 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3794 ExpandCleanup(&xpc);
3795 }
3796 }
3797 else
3798 rettv->vval.v_string = NULL;
3799 }
3800}
3801
3802/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003803 * "expandcmd()" function
3804 * Expand all the special characters in a command string.
3805 */
3806 static void
3807f_expandcmd(typval_T *argvars, typval_T *rettv)
3808{
3809 exarg_T eap;
3810 char_u *cmdstr;
3811 char *errormsg = NULL;
3812
3813 rettv->v_type = VAR_STRING;
3814 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3815
3816 memset(&eap, 0, sizeof(eap));
3817 eap.cmd = cmdstr;
3818 eap.arg = cmdstr;
3819 eap.argt |= NOSPC;
3820 eap.usefilter = FALSE;
3821 eap.nextcmd = NULL;
3822 eap.cmdidx = CMD_USER;
3823
3824 expand_filename(&eap, &cmdstr, &errormsg);
3825 if (errormsg != NULL && *errormsg != NUL)
3826 emsg(errormsg);
3827
3828 rettv->vval.v_string = cmdstr;
3829}
3830
3831/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003832 * "extend(list, list [, idx])" function
3833 * "extend(dict, dict [, action])" function
3834 */
3835 static void
3836f_extend(typval_T *argvars, typval_T *rettv)
3837{
3838 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3839
3840 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3841 {
3842 list_T *l1, *l2;
3843 listitem_T *item;
3844 long before;
3845 int error = FALSE;
3846
3847 l1 = argvars[0].vval.v_list;
3848 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003849 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 && l2 != NULL)
3851 {
3852 if (argvars[2].v_type != VAR_UNKNOWN)
3853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003854 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003855 if (error)
3856 return; /* type error; errmsg already given */
3857
3858 if (before == l1->lv_len)
3859 item = NULL;
3860 else
3861 {
3862 item = list_find(l1, before);
3863 if (item == NULL)
3864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003865 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 return;
3867 }
3868 }
3869 }
3870 else
3871 item = NULL;
3872 list_extend(l1, l2, item);
3873
3874 copy_tv(&argvars[0], rettv);
3875 }
3876 }
3877 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3878 {
3879 dict_T *d1, *d2;
3880 char_u *action;
3881 int i;
3882
3883 d1 = argvars[0].vval.v_dict;
3884 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003885 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 && d2 != NULL)
3887 {
3888 /* Check the third argument. */
3889 if (argvars[2].v_type != VAR_UNKNOWN)
3890 {
3891 static char *(av[]) = {"keep", "force", "error"};
3892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894 if (action == NULL)
3895 return; /* type error; errmsg already given */
3896 for (i = 0; i < 3; ++i)
3897 if (STRCMP(action, av[i]) == 0)
3898 break;
3899 if (i == 3)
3900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003901 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003902 return;
3903 }
3904 }
3905 else
3906 action = (char_u *)"force";
3907
3908 dict_extend(d1, d2, action);
3909
3910 copy_tv(&argvars[0], rettv);
3911 }
3912 }
3913 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003914 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003915}
3916
3917/*
3918 * "feedkeys()" function
3919 */
3920 static void
3921f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3922{
3923 int remap = TRUE;
3924 int insert = FALSE;
3925 char_u *keys, *flags;
3926 char_u nbuf[NUMBUFLEN];
3927 int typed = FALSE;
3928 int execute = FALSE;
3929 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003930 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 char_u *keys_esc;
3932
3933 /* This is not allowed in the sandbox. If the commands would still be
3934 * executed in the sandbox it would be OK, but it probably happens later,
3935 * when "sandbox" is no longer set. */
3936 if (check_secure())
3937 return;
3938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003939 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003940
3941 if (argvars[1].v_type != VAR_UNKNOWN)
3942 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003943 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003944 for ( ; *flags != NUL; ++flags)
3945 {
3946 switch (*flags)
3947 {
3948 case 'n': remap = FALSE; break;
3949 case 'm': remap = TRUE; break;
3950 case 't': typed = TRUE; break;
3951 case 'i': insert = TRUE; break;
3952 case 'x': execute = TRUE; break;
3953 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003954 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003955 }
3956 }
3957 }
3958
3959 if (*keys != NUL || execute)
3960 {
3961 /* Need to escape K_SPECIAL and CSI before putting the string in the
3962 * typeahead buffer. */
3963 keys_esc = vim_strsave_escape_csi(keys);
3964 if (keys_esc != NULL)
3965 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003966 if (lowlevel)
3967 {
3968#ifdef USE_INPUT_BUF
3969 add_to_input_buf(keys, (int)STRLEN(keys));
3970#else
3971 emsg(_("E980: lowlevel input not supported"));
3972#endif
3973 }
3974 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003975 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003976 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003977 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003978 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003979#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003980 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003981#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003982 )
3983 typebuf_was_filled = TRUE;
3984 }
3985 vim_free(keys_esc);
3986
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003987 if (execute)
3988 {
3989 int save_msg_scroll = msg_scroll;
3990
3991 /* Avoid a 1 second delay when the keys start Insert mode. */
3992 msg_scroll = FALSE;
3993
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003994 if (!dangerous)
3995 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003996 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003997 if (!dangerous)
3998 --ex_normal_busy;
3999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004000 msg_scroll |= save_msg_scroll;
4001 }
4002 }
4003 }
4004}
4005
4006/*
4007 * "filereadable()" function
4008 */
4009 static void
4010f_filereadable(typval_T *argvars, typval_T *rettv)
4011{
4012 int fd;
4013 char_u *p;
4014 int n;
4015
4016#ifndef O_NONBLOCK
4017# define O_NONBLOCK 0
4018#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4021 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4022 {
4023 n = TRUE;
4024 close(fd);
4025 }
4026 else
4027 n = FALSE;
4028
4029 rettv->vval.v_number = n;
4030}
4031
4032/*
4033 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4034 * rights to write into.
4035 */
4036 static void
4037f_filewritable(typval_T *argvars, typval_T *rettv)
4038{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004039 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004040}
4041
4042 static void
4043findfilendir(
4044 typval_T *argvars UNUSED,
4045 typval_T *rettv,
4046 int find_what UNUSED)
4047{
4048#ifdef FEAT_SEARCHPATH
4049 char_u *fname;
4050 char_u *fresult = NULL;
4051 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4052 char_u *p;
4053 char_u pathbuf[NUMBUFLEN];
4054 int count = 1;
4055 int first = TRUE;
4056 int error = FALSE;
4057#endif
4058
4059 rettv->vval.v_string = NULL;
4060 rettv->v_type = VAR_STRING;
4061
4062#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004063 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004064
4065 if (argvars[1].v_type != VAR_UNKNOWN)
4066 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004067 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004068 if (p == NULL)
4069 error = TRUE;
4070 else
4071 {
4072 if (*p != NUL)
4073 path = p;
4074
4075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004076 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004077 }
4078 }
4079
4080 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4081 error = TRUE;
4082
4083 if (*fname != NUL && !error)
4084 {
4085 do
4086 {
4087 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4088 vim_free(fresult);
4089 fresult = find_file_in_path_option(first ? fname : NULL,
4090 first ? (int)STRLEN(fname) : 0,
4091 0, first, path,
4092 find_what,
4093 curbuf->b_ffname,
4094 find_what == FINDFILE_DIR
4095 ? (char_u *)"" : curbuf->b_p_sua);
4096 first = FALSE;
4097
4098 if (fresult != NULL && rettv->v_type == VAR_LIST)
4099 list_append_string(rettv->vval.v_list, fresult, -1);
4100
4101 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4102 }
4103
4104 if (rettv->v_type == VAR_STRING)
4105 rettv->vval.v_string = fresult;
4106#endif
4107}
4108
4109/*
4110 * "filter()" function
4111 */
4112 static void
4113f_filter(typval_T *argvars, typval_T *rettv)
4114{
4115 filter_map(argvars, rettv, FALSE);
4116}
4117
4118/*
4119 * "finddir({fname}[, {path}[, {count}]])" function
4120 */
4121 static void
4122f_finddir(typval_T *argvars, typval_T *rettv)
4123{
4124 findfilendir(argvars, rettv, FINDFILE_DIR);
4125}
4126
4127/*
4128 * "findfile({fname}[, {path}[, {count}]])" function
4129 */
4130 static void
4131f_findfile(typval_T *argvars, typval_T *rettv)
4132{
4133 findfilendir(argvars, rettv, FINDFILE_FILE);
4134}
4135
4136#ifdef FEAT_FLOAT
4137/*
4138 * "float2nr({float})" function
4139 */
4140 static void
4141f_float2nr(typval_T *argvars, typval_T *rettv)
4142{
4143 float_T f = 0.0;
4144
4145 if (get_float_arg(argvars, &f) == OK)
4146 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004147 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004148 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004149 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004150 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151 else
4152 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004153 }
4154}
4155
4156/*
4157 * "floor({float})" function
4158 */
4159 static void
4160f_floor(typval_T *argvars, typval_T *rettv)
4161{
4162 float_T f = 0.0;
4163
4164 rettv->v_type = VAR_FLOAT;
4165 if (get_float_arg(argvars, &f) == OK)
4166 rettv->vval.v_float = floor(f);
4167 else
4168 rettv->vval.v_float = 0.0;
4169}
4170
4171/*
4172 * "fmod()" function
4173 */
4174 static void
4175f_fmod(typval_T *argvars, typval_T *rettv)
4176{
4177 float_T fx = 0.0, fy = 0.0;
4178
4179 rettv->v_type = VAR_FLOAT;
4180 if (get_float_arg(argvars, &fx) == OK
4181 && get_float_arg(&argvars[1], &fy) == OK)
4182 rettv->vval.v_float = fmod(fx, fy);
4183 else
4184 rettv->vval.v_float = 0.0;
4185}
4186#endif
4187
4188/*
4189 * "fnameescape({string})" function
4190 */
4191 static void
4192f_fnameescape(typval_T *argvars, typval_T *rettv)
4193{
4194 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004195 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004196 rettv->v_type = VAR_STRING;
4197}
4198
4199/*
4200 * "fnamemodify({fname}, {mods})" function
4201 */
4202 static void
4203f_fnamemodify(typval_T *argvars, typval_T *rettv)
4204{
4205 char_u *fname;
4206 char_u *mods;
4207 int usedlen = 0;
4208 int len;
4209 char_u *fbuf = NULL;
4210 char_u buf[NUMBUFLEN];
4211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004212 fname = tv_get_string_chk(&argvars[0]);
4213 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214 if (fname == NULL || mods == NULL)
4215 fname = NULL;
4216 else
4217 {
4218 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004219 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004220 }
4221
4222 rettv->v_type = VAR_STRING;
4223 if (fname == NULL)
4224 rettv->vval.v_string = NULL;
4225 else
4226 rettv->vval.v_string = vim_strnsave(fname, len);
4227 vim_free(fbuf);
4228}
4229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230/*
4231 * "foldclosed()" function
4232 */
4233 static void
4234foldclosed_both(
4235 typval_T *argvars UNUSED,
4236 typval_T *rettv,
4237 int end UNUSED)
4238{
4239#ifdef FEAT_FOLDING
4240 linenr_T lnum;
4241 linenr_T first, last;
4242
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004243 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004244 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4245 {
4246 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4247 {
4248 if (end)
4249 rettv->vval.v_number = (varnumber_T)last;
4250 else
4251 rettv->vval.v_number = (varnumber_T)first;
4252 return;
4253 }
4254 }
4255#endif
4256 rettv->vval.v_number = -1;
4257}
4258
4259/*
4260 * "foldclosed()" function
4261 */
4262 static void
4263f_foldclosed(typval_T *argvars, typval_T *rettv)
4264{
4265 foldclosed_both(argvars, rettv, FALSE);
4266}
4267
4268/*
4269 * "foldclosedend()" function
4270 */
4271 static void
4272f_foldclosedend(typval_T *argvars, typval_T *rettv)
4273{
4274 foldclosed_both(argvars, rettv, TRUE);
4275}
4276
4277/*
4278 * "foldlevel()" function
4279 */
4280 static void
4281f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4282{
4283#ifdef FEAT_FOLDING
4284 linenr_T lnum;
4285
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004286 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4288 rettv->vval.v_number = foldLevel(lnum);
4289#endif
4290}
4291
4292/*
4293 * "foldtext()" function
4294 */
4295 static void
4296f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4297{
4298#ifdef FEAT_FOLDING
4299 linenr_T foldstart;
4300 linenr_T foldend;
4301 char_u *dashes;
4302 linenr_T lnum;
4303 char_u *s;
4304 char_u *r;
4305 int len;
4306 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004307 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004308#endif
4309
4310 rettv->v_type = VAR_STRING;
4311 rettv->vval.v_string = NULL;
4312#ifdef FEAT_FOLDING
4313 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4314 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4315 dashes = get_vim_var_str(VV_FOLDDASHES);
4316 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4317 && dashes != NULL)
4318 {
4319 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004320 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 if (!linewhite(lnum))
4322 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323
4324 /* Find interesting text in this line. */
4325 s = skipwhite(ml_get(lnum));
4326 /* skip C comment-start */
4327 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4328 {
4329 s = skipwhite(s + 2);
4330 if (*skipwhite(s) == NUL
4331 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4332 {
4333 s = skipwhite(ml_get(lnum + 1));
4334 if (*s == '*')
4335 s = skipwhite(s + 1);
4336 }
4337 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004338 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004339 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004340 r = alloc(STRLEN(txt)
4341 + STRLEN(dashes) // for %s
4342 + 20 // for %3ld
4343 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 if (r != NULL)
4345 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004346 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347 len = (int)STRLEN(r);
4348 STRCAT(r, s);
4349 /* remove 'foldmarker' and 'commentstring' */
4350 foldtext_cleanup(r + len);
4351 rettv->vval.v_string = r;
4352 }
4353 }
4354#endif
4355}
4356
4357/*
4358 * "foldtextresult(lnum)" function
4359 */
4360 static void
4361f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4362{
4363#ifdef FEAT_FOLDING
4364 linenr_T lnum;
4365 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004366 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004367 foldinfo_T foldinfo;
4368 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004369 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370#endif
4371
4372 rettv->v_type = VAR_STRING;
4373 rettv->vval.v_string = NULL;
4374#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004375 if (entered)
4376 return; /* reject recursive use */
4377 entered = TRUE;
4378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004379 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004380 /* treat illegal types and illegal string values for {lnum} the same */
4381 if (lnum < 0)
4382 lnum = 0;
4383 fold_count = foldedCount(curwin, lnum, &foldinfo);
4384 if (fold_count > 0)
4385 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004386 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4387 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388 if (text == buf)
4389 text = vim_strsave(text);
4390 rettv->vval.v_string = text;
4391 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004392
4393 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004394#endif
4395}
4396
4397/*
4398 * "foreground()" function
4399 */
4400 static void
4401f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4402{
4403#ifdef FEAT_GUI
4404 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004405 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004407 return;
4408 }
4409#endif
4410#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412#endif
4413}
4414
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004415 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004416common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004417{
4418 char_u *s;
4419 char_u *name;
4420 int use_string = FALSE;
4421 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004422 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004423
4424 if (argvars[0].v_type == VAR_FUNC)
4425 {
4426 /* function(MyFunc, [arg], dict) */
4427 s = argvars[0].vval.v_string;
4428 }
4429 else if (argvars[0].v_type == VAR_PARTIAL
4430 && argvars[0].vval.v_partial != NULL)
4431 {
4432 /* function(dict.MyFunc, [arg]) */
4433 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004434 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 }
4436 else
4437 {
4438 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004439 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 use_string = TRUE;
4441 }
4442
Bram Moolenaar843b8842016-08-21 14:36:15 +02004443 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004444 {
4445 name = s;
4446 trans_name = trans_function_name(&name, FALSE,
4447 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4448 if (*name != NUL)
4449 s = NULL;
4450 }
4451
Bram Moolenaar843b8842016-08-21 14:36:15 +02004452 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4453 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004454 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004456 else if (trans_name != NULL && (is_funcref
4457 ? find_func(trans_name) == NULL
4458 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004459 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 else
4461 {
4462 int dict_idx = 0;
4463 int arg_idx = 0;
4464 list_T *list = NULL;
4465
4466 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4467 {
4468 char sid_buf[25];
4469 int off = *s == 's' ? 2 : 5;
4470
4471 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4472 * also be called from another script. Using trans_function_name()
4473 * would also work, but some plugins depend on the name being
4474 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004475 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004476 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004477 if (name != NULL)
4478 {
4479 STRCPY(name, sid_buf);
4480 STRCAT(name, s + off);
4481 }
4482 }
4483 else
4484 name = vim_strsave(s);
4485
4486 if (argvars[1].v_type != VAR_UNKNOWN)
4487 {
4488 if (argvars[2].v_type != VAR_UNKNOWN)
4489 {
4490 /* function(name, [args], dict) */
4491 arg_idx = 1;
4492 dict_idx = 2;
4493 }
4494 else if (argvars[1].v_type == VAR_DICT)
4495 /* function(name, dict) */
4496 dict_idx = 1;
4497 else
4498 /* function(name, [args]) */
4499 arg_idx = 1;
4500 if (dict_idx > 0)
4501 {
4502 if (argvars[dict_idx].v_type != VAR_DICT)
4503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004504 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004505 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004506 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 }
4508 if (argvars[dict_idx].vval.v_dict == NULL)
4509 dict_idx = 0;
4510 }
4511 if (arg_idx > 0)
4512 {
4513 if (argvars[arg_idx].v_type != VAR_LIST)
4514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004515 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004517 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004518 }
4519 list = argvars[arg_idx].vval.v_list;
4520 if (list == NULL || list->lv_len == 0)
4521 arg_idx = 0;
4522 }
4523 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004524 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004526 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004527
4528 /* result is a VAR_PARTIAL */
4529 if (pt == NULL)
4530 vim_free(name);
4531 else
4532 {
4533 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4534 {
4535 listitem_T *li;
4536 int i = 0;
4537 int arg_len = 0;
4538 int lv_len = 0;
4539
4540 if (arg_pt != NULL)
4541 arg_len = arg_pt->pt_argc;
4542 if (list != NULL)
4543 lv_len = list->lv_len;
4544 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004545 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 if (pt->pt_argv == NULL)
4547 {
4548 vim_free(pt);
4549 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004550 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004551 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004552 for (i = 0; i < arg_len; i++)
4553 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4554 if (lv_len > 0)
4555 for (li = list->lv_first; li != NULL;
4556 li = li->li_next)
4557 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004558 }
4559
4560 /* For "function(dict.func, [], dict)" and "func" is a partial
4561 * use "dict". That is backwards compatible. */
4562 if (dict_idx > 0)
4563 {
4564 /* The dict is bound explicitly, pt_auto is FALSE. */
4565 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4566 ++pt->pt_dict->dv_refcount;
4567 }
4568 else if (arg_pt != NULL)
4569 {
4570 /* If the dict was bound automatically the result is also
4571 * bound automatically. */
4572 pt->pt_dict = arg_pt->pt_dict;
4573 pt->pt_auto = arg_pt->pt_auto;
4574 if (pt->pt_dict != NULL)
4575 ++pt->pt_dict->dv_refcount;
4576 }
4577
4578 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004579 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4580 {
4581 pt->pt_func = arg_pt->pt_func;
4582 func_ptr_ref(pt->pt_func);
4583 vim_free(name);
4584 }
4585 else if (is_funcref)
4586 {
4587 pt->pt_func = find_func(trans_name);
4588 func_ptr_ref(pt->pt_func);
4589 vim_free(name);
4590 }
4591 else
4592 {
4593 pt->pt_name = name;
4594 func_ref(name);
4595 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004596 }
4597 rettv->v_type = VAR_PARTIAL;
4598 rettv->vval.v_partial = pt;
4599 }
4600 else
4601 {
4602 /* result is a VAR_FUNC */
4603 rettv->v_type = VAR_FUNC;
4604 rettv->vval.v_string = name;
4605 func_ref(name);
4606 }
4607 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004608theend:
4609 vim_free(trans_name);
4610}
4611
4612/*
4613 * "funcref()" function
4614 */
4615 static void
4616f_funcref(typval_T *argvars, typval_T *rettv)
4617{
4618 common_function(argvars, rettv, TRUE);
4619}
4620
4621/*
4622 * "function()" function
4623 */
4624 static void
4625f_function(typval_T *argvars, typval_T *rettv)
4626{
4627 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004628}
4629
4630/*
4631 * "garbagecollect()" function
4632 */
4633 static void
4634f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4635{
4636 /* This is postponed until we are back at the toplevel, because we may be
4637 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4638 want_garbage_collect = TRUE;
4639
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004640 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004641 garbage_collect_at_exit = TRUE;
4642}
4643
4644/*
4645 * "get()" function
4646 */
4647 static void
4648f_get(typval_T *argvars, typval_T *rettv)
4649{
4650 listitem_T *li;
4651 list_T *l;
4652 dictitem_T *di;
4653 dict_T *d;
4654 typval_T *tv = NULL;
4655
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004656 if (argvars[0].v_type == VAR_BLOB)
4657 {
4658 int error = FALSE;
4659 int idx = tv_get_number_chk(&argvars[1], &error);
4660
4661 if (!error)
4662 {
4663 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004664 if (idx < 0)
4665 idx = blob_len(argvars[0].vval.v_blob) + idx;
4666 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4667 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004668 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004669 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004670 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004671 tv = rettv;
4672 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004673 }
4674 }
4675 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 {
4677 if ((l = argvars[0].vval.v_list) != NULL)
4678 {
4679 int error = FALSE;
4680
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004681 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682 if (!error && li != NULL)
4683 tv = &li->li_tv;
4684 }
4685 }
4686 else if (argvars[0].v_type == VAR_DICT)
4687 {
4688 if ((d = argvars[0].vval.v_dict) != NULL)
4689 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004690 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004691 if (di != NULL)
4692 tv = &di->di_tv;
4693 }
4694 }
4695 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4696 {
4697 partial_T *pt;
4698 partial_T fref_pt;
4699
4700 if (argvars[0].v_type == VAR_PARTIAL)
4701 pt = argvars[0].vval.v_partial;
4702 else
4703 {
4704 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4705 fref_pt.pt_name = argvars[0].vval.v_string;
4706 pt = &fref_pt;
4707 }
4708
4709 if (pt != NULL)
4710 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004711 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004712 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713
4714 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4715 {
4716 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004717 n = partial_name(pt);
4718 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004719 rettv->vval.v_string = NULL;
4720 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004721 {
4722 rettv->vval.v_string = vim_strsave(n);
4723 if (rettv->v_type == VAR_FUNC)
4724 func_ref(rettv->vval.v_string);
4725 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004726 }
4727 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004728 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004729 else if (STRCMP(what, "args") == 0)
4730 {
4731 rettv->v_type = VAR_LIST;
4732 if (rettv_list_alloc(rettv) == OK)
4733 {
4734 int i;
4735
4736 for (i = 0; i < pt->pt_argc; ++i)
4737 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4738 }
4739 }
4740 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004741 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004742 return;
4743 }
4744 }
4745 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004746 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004747
4748 if (tv == NULL)
4749 {
4750 if (argvars[2].v_type != VAR_UNKNOWN)
4751 copy_tv(&argvars[2], rettv);
4752 }
4753 else
4754 copy_tv(tv, rettv);
4755}
4756
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004757/*
4758 * Returns buffer options, variables and other attributes in a dictionary.
4759 */
4760 static dict_T *
4761get_buffer_info(buf_T *buf)
4762{
4763 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004764 tabpage_T *tp;
4765 win_T *wp;
4766 list_T *windows;
4767
4768 dict = dict_alloc();
4769 if (dict == NULL)
4770 return NULL;
4771
Bram Moolenaare0be1672018-07-08 16:50:37 +02004772 dict_add_number(dict, "bufnr", buf->b_fnum);
4773 dict_add_string(dict, "name", buf->b_ffname);
4774 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4775 : buflist_findlnum(buf));
4776 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4777 dict_add_number(dict, "listed", buf->b_p_bl);
4778 dict_add_number(dict, "changed", bufIsChanged(buf));
4779 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4780 dict_add_number(dict, "hidden",
4781 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004782
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004783 /* Get a reference to buffer variables */
4784 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004785
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004786 /* List of windows displaying this buffer */
4787 windows = list_alloc();
4788 if (windows != NULL)
4789 {
4790 FOR_ALL_TAB_WINDOWS(tp, wp)
4791 if (wp->w_buffer == buf)
4792 list_append_number(windows, (varnumber_T)wp->w_id);
4793 dict_add_list(dict, "windows", windows);
4794 }
4795
4796#ifdef FEAT_SIGNS
4797 if (buf->b_signlist != NULL)
4798 {
4799 /* List of signs placed in this buffer */
4800 list_T *signs = list_alloc();
4801 if (signs != NULL)
4802 {
4803 get_buffer_signs(buf, signs);
4804 dict_add_list(dict, "signs", signs);
4805 }
4806 }
4807#endif
4808
4809 return dict;
4810}
4811
4812/*
4813 * "getbufinfo()" function
4814 */
4815 static void
4816f_getbufinfo(typval_T *argvars, typval_T *rettv)
4817{
4818 buf_T *buf = NULL;
4819 buf_T *argbuf = NULL;
4820 dict_T *d;
4821 int filtered = FALSE;
4822 int sel_buflisted = FALSE;
4823 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004824 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004825
4826 if (rettv_list_alloc(rettv) != OK)
4827 return;
4828
4829 /* List of all the buffers or selected buffers */
4830 if (argvars[0].v_type == VAR_DICT)
4831 {
4832 dict_T *sel_d = argvars[0].vval.v_dict;
4833
4834 if (sel_d != NULL)
4835 {
4836 dictitem_T *di;
4837
4838 filtered = TRUE;
4839
4840 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004841 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004842 sel_buflisted = TRUE;
4843
4844 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004846 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004847
4848 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004849 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004850 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004851 }
4852 }
4853 else if (argvars[0].v_type != VAR_UNKNOWN)
4854 {
4855 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004856 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004857 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004858 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004859 --emsg_off;
4860 if (argbuf == NULL)
4861 return;
4862 }
4863
4864 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004865 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004866 {
4867 if (argbuf != NULL && argbuf != buf)
4868 continue;
4869 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004870 || (sel_buflisted && !buf->b_p_bl)
4871 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004872 continue;
4873
4874 d = get_buffer_info(buf);
4875 if (d != NULL)
4876 list_append_dict(rettv->vval.v_list, d);
4877 if (argbuf != NULL)
4878 return;
4879 }
4880}
4881
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882/*
4883 * Get line or list of lines from buffer "buf" into "rettv".
4884 * Return a range (from start to end) of lines in rettv from the specified
4885 * buffer.
4886 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4887 */
4888 static void
4889get_buffer_lines(
4890 buf_T *buf,
4891 linenr_T start,
4892 linenr_T end,
4893 int retlist,
4894 typval_T *rettv)
4895{
4896 char_u *p;
4897
4898 rettv->v_type = VAR_STRING;
4899 rettv->vval.v_string = NULL;
4900 if (retlist && rettv_list_alloc(rettv) == FAIL)
4901 return;
4902
4903 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4904 return;
4905
4906 if (!retlist)
4907 {
4908 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4909 p = ml_get_buf(buf, start, FALSE);
4910 else
4911 p = (char_u *)"";
4912 rettv->vval.v_string = vim_strsave(p);
4913 }
4914 else
4915 {
4916 if (end < start)
4917 return;
4918
4919 if (start < 1)
4920 start = 1;
4921 if (end > buf->b_ml.ml_line_count)
4922 end = buf->b_ml.ml_line_count;
4923 while (start <= end)
4924 if (list_append_string(rettv->vval.v_list,
4925 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4926 break;
4927 }
4928}
4929
4930/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 * "getbufline()" function
4932 */
4933 static void
4934f_getbufline(typval_T *argvars, typval_T *rettv)
4935{
4936 linenr_T lnum;
4937 linenr_T end;
4938 buf_T *buf;
4939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004940 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004942 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004943 --emsg_off;
4944
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004945 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004946 if (argvars[2].v_type == VAR_UNKNOWN)
4947 end = lnum;
4948 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004949 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004950
4951 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4952}
4953
4954/*
4955 * "getbufvar()" function
4956 */
4957 static void
4958f_getbufvar(typval_T *argvars, typval_T *rettv)
4959{
4960 buf_T *buf;
4961 buf_T *save_curbuf;
4962 char_u *varname;
4963 dictitem_T *v;
4964 int done = FALSE;
4965
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004966 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4967 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004968 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004969 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004970
4971 rettv->v_type = VAR_STRING;
4972 rettv->vval.v_string = NULL;
4973
4974 if (buf != NULL && varname != NULL)
4975 {
4976 /* set curbuf to be our buf, temporarily */
4977 save_curbuf = curbuf;
4978 curbuf = buf;
4979
Bram Moolenaar30567352016-08-27 21:25:44 +02004980 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004981 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004982 if (varname[1] == NUL)
4983 {
4984 /* get all buffer-local options in a dict */
4985 dict_T *opts = get_winbuf_options(TRUE);
4986
4987 if (opts != NULL)
4988 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004989 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004990 done = TRUE;
4991 }
4992 }
4993 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4994 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004995 done = TRUE;
4996 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004997 else
4998 {
4999 /* Look up the variable. */
5000 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
5001 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
5002 'b', varname, FALSE);
5003 if (v != NULL)
5004 {
5005 copy_tv(&v->di_tv, rettv);
5006 done = TRUE;
5007 }
5008 }
5009
5010 /* restore previous notion of curbuf */
5011 curbuf = save_curbuf;
5012 }
5013
5014 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5015 /* use the default value */
5016 copy_tv(&argvars[2], rettv);
5017
5018 --emsg_off;
5019}
5020
5021/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005022 * "getchangelist()" function
5023 */
5024 static void
5025f_getchangelist(typval_T *argvars, typval_T *rettv)
5026{
5027#ifdef FEAT_JUMPLIST
5028 buf_T *buf;
5029 int i;
5030 list_T *l;
5031 dict_T *d;
5032#endif
5033
5034 if (rettv_list_alloc(rettv) != OK)
5035 return;
5036
5037#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005038 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005039 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005040 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005041 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005042 if (buf == NULL)
5043 return;
5044
5045 l = list_alloc();
5046 if (l == NULL)
5047 return;
5048
5049 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5050 return;
5051 /*
5052 * The current window change list index tracks only the position in the
5053 * current buffer change list. For other buffers, use the change list
5054 * length as the current index.
5055 */
5056 list_append_number(rettv->vval.v_list,
5057 (varnumber_T)((buf == curwin->w_buffer)
5058 ? curwin->w_changelistidx : buf->b_changelistlen));
5059
5060 for (i = 0; i < buf->b_changelistlen; ++i)
5061 {
5062 if (buf->b_changelist[i].lnum == 0)
5063 continue;
5064 if ((d = dict_alloc()) == NULL)
5065 return;
5066 if (list_append_dict(l, d) == FAIL)
5067 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005068 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5069 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005070 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005071 }
5072#endif
5073}
5074/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005075 * "getchar()" function
5076 */
5077 static void
5078f_getchar(typval_T *argvars, typval_T *rettv)
5079{
5080 varnumber_T n;
5081 int error = FALSE;
5082
Bram Moolenaar84d93902018-09-11 20:10:20 +02005083#ifdef MESSAGE_QUEUE
5084 // vpeekc() used to check for messages, but that caused problems, invoking
5085 // a callback where it was not expected. Some plugins use getchar(1) in a
5086 // loop to await a message, therefore make sure we check for messages here.
5087 parse_queued_messages();
5088#endif
5089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005090 /* Position the cursor. Needed after a message that ends in a space. */
5091 windgoto(msg_row, msg_col);
5092
5093 ++no_mapping;
5094 ++allow_keys;
5095 for (;;)
5096 {
5097 if (argvars[0].v_type == VAR_UNKNOWN)
5098 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005099 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005100 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005101 /* getchar(1): only check if char avail */
5102 n = vpeekc_any();
5103 else if (error || vpeekc_any() == NUL)
5104 /* illegal argument or getchar(0) and no char avail: return zero */
5105 n = 0;
5106 else
5107 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005108 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109
5110 if (n == K_IGNORE)
5111 continue;
5112 break;
5113 }
5114 --no_mapping;
5115 --allow_keys;
5116
5117 set_vim_var_nr(VV_MOUSE_WIN, 0);
5118 set_vim_var_nr(VV_MOUSE_WINID, 0);
5119 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5120 set_vim_var_nr(VV_MOUSE_COL, 0);
5121
5122 rettv->vval.v_number = n;
5123 if (IS_SPECIAL(n) || mod_mask != 0)
5124 {
5125 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5126 int i = 0;
5127
5128 /* Turn a special key into three bytes, plus modifier. */
5129 if (mod_mask != 0)
5130 {
5131 temp[i++] = K_SPECIAL;
5132 temp[i++] = KS_MODIFIER;
5133 temp[i++] = mod_mask;
5134 }
5135 if (IS_SPECIAL(n))
5136 {
5137 temp[i++] = K_SPECIAL;
5138 temp[i++] = K_SECOND(n);
5139 temp[i++] = K_THIRD(n);
5140 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141 else if (has_mbyte)
5142 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 else
5144 temp[i++] = n;
5145 temp[i++] = NUL;
5146 rettv->v_type = VAR_STRING;
5147 rettv->vval.v_string = vim_strsave(temp);
5148
5149#ifdef FEAT_MOUSE
5150 if (is_mouse_key(n))
5151 {
5152 int row = mouse_row;
5153 int col = mouse_col;
5154 win_T *win;
5155 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005156 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 int winnr = 1;
5158
5159 if (row >= 0 && col >= 0)
5160 {
5161 /* Find the window at the mouse coordinates and compute the
5162 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005163 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005164 if (win == NULL)
5165 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005166 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005167# ifdef FEAT_TEXT_PROP
5168 if (bt_popup(win->w_buffer))
5169 winnr = 0;
5170 else
5171# endif
5172 for (wp = firstwin; wp != win && wp != NULL;
5173 wp = wp->w_next)
5174 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005175 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5176 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5177 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5178 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5179 }
5180 }
5181#endif
5182 }
5183}
5184
5185/*
5186 * "getcharmod()" function
5187 */
5188 static void
5189f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5190{
5191 rettv->vval.v_number = mod_mask;
5192}
5193
5194/*
5195 * "getcharsearch()" function
5196 */
5197 static void
5198f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5199{
5200 if (rettv_dict_alloc(rettv) != FAIL)
5201 {
5202 dict_T *dict = rettv->vval.v_dict;
5203
Bram Moolenaare0be1672018-07-08 16:50:37 +02005204 dict_add_string(dict, "char", last_csearch());
5205 dict_add_number(dict, "forward", last_csearch_forward());
5206 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005207 }
5208}
5209
5210/*
5211 * "getcmdline()" function
5212 */
5213 static void
5214f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5215{
5216 rettv->v_type = VAR_STRING;
5217 rettv->vval.v_string = get_cmdline_str();
5218}
5219
5220/*
5221 * "getcmdpos()" function
5222 */
5223 static void
5224f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5225{
5226 rettv->vval.v_number = get_cmdline_pos() + 1;
5227}
5228
5229/*
5230 * "getcmdtype()" function
5231 */
5232 static void
5233f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5234{
5235 rettv->v_type = VAR_STRING;
5236 rettv->vval.v_string = alloc(2);
5237 if (rettv->vval.v_string != NULL)
5238 {
5239 rettv->vval.v_string[0] = get_cmdline_type();
5240 rettv->vval.v_string[1] = NUL;
5241 }
5242}
5243
5244/*
5245 * "getcmdwintype()" function
5246 */
5247 static void
5248f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5249{
5250 rettv->v_type = VAR_STRING;
5251 rettv->vval.v_string = NULL;
5252#ifdef FEAT_CMDWIN
5253 rettv->vval.v_string = alloc(2);
5254 if (rettv->vval.v_string != NULL)
5255 {
5256 rettv->vval.v_string[0] = cmdwin_type;
5257 rettv->vval.v_string[1] = NUL;
5258 }
5259#endif
5260}
5261
5262#if defined(FEAT_CMDL_COMPL)
5263/*
5264 * "getcompletion()" function
5265 */
5266 static void
5267f_getcompletion(typval_T *argvars, typval_T *rettv)
5268{
5269 char_u *pat;
5270 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005271 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005272 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5273 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005276 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005277
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005278 if (p_wic)
5279 options |= WILD_ICASE;
5280
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005281 /* For filtered results, 'wildignore' is used */
5282 if (!filtered)
5283 options |= WILD_KEEP_ALL;
5284
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005286 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005288 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289 if (xpc.xp_context == EXPAND_NOTHING)
5290 {
5291 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005292 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005294 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295 return;
5296 }
5297
5298# if defined(FEAT_MENU)
5299 if (xpc.xp_context == EXPAND_MENUS)
5300 {
5301 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5302 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5303 }
5304# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005305#ifdef FEAT_CSCOPE
5306 if (xpc.xp_context == EXPAND_CSCOPE)
5307 {
5308 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5309 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5310 }
5311#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005312#ifdef FEAT_SIGNS
5313 if (xpc.xp_context == EXPAND_SIGN)
5314 {
5315 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5316 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5317 }
5318#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319
5320 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5321 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5322 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005323 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324
5325 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5326
5327 for (i = 0; i < xpc.xp_numfiles; i++)
5328 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5329 }
5330 vim_free(pat);
5331 ExpandCleanup(&xpc);
5332}
5333#endif
5334
5335/*
5336 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005337 *
5338 * Return the current working directory of a window in a tab page.
5339 * First optional argument 'winnr' is the window number or -1 and the second
5340 * optional argument 'tabnr' is the tab page number.
5341 *
5342 * If no arguments are supplied, then return the directory of the current
5343 * window.
5344 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5345 * the specified window.
5346 * If 'winnr' is 0 then return the directory of the current window.
5347 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5348 * directory of the specified tab page. Otherwise return the directory of the
5349 * specified window in the specified tab page.
5350 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005351 */
5352 static void
5353f_getcwd(typval_T *argvars, typval_T *rettv)
5354{
5355 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005356 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005357 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005358 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005359
5360 rettv->v_type = VAR_STRING;
5361 rettv->vval.v_string = NULL;
5362
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005363 if (argvars[0].v_type == VAR_NUMBER
5364 && argvars[0].vval.v_number == -1
5365 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005366 global = TRUE;
5367 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005368 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005369
5370 if (wp != NULL && wp->w_localdir != NULL)
5371 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005372 else if (tp != NULL && tp->tp_localdir != NULL)
5373 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5374 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005376 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377 rettv->vval.v_string = vim_strsave(globaldir);
5378 else
5379 {
5380 cwd = alloc(MAXPATHL);
5381 if (cwd != NULL)
5382 {
5383 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5384 rettv->vval.v_string = vim_strsave(cwd);
5385 vim_free(cwd);
5386 }
5387 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005389#ifdef BACKSLASH_IN_FILENAME
5390 if (rettv->vval.v_string != NULL)
5391 slash_adjust(rettv->vval.v_string);
5392#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005393}
5394
5395/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005396 * "getenv()" function
5397 */
5398 static void
5399f_getenv(typval_T *argvars, typval_T *rettv)
5400{
5401 int mustfree = FALSE;
5402 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5403
5404 if (p == NULL)
5405 {
5406 rettv->v_type = VAR_SPECIAL;
5407 rettv->vval.v_number = VVAL_NULL;
5408 return;
5409 }
5410 if (!mustfree)
5411 p = vim_strsave(p);
5412 rettv->vval.v_string = p;
5413 rettv->v_type = VAR_STRING;
5414}
5415
5416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005417 * "getfontname()" function
5418 */
5419 static void
5420f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5421{
5422 rettv->v_type = VAR_STRING;
5423 rettv->vval.v_string = NULL;
5424#ifdef FEAT_GUI
5425 if (gui.in_use)
5426 {
5427 GuiFont font;
5428 char_u *name = NULL;
5429
5430 if (argvars[0].v_type == VAR_UNKNOWN)
5431 {
5432 /* Get the "Normal" font. Either the name saved by
5433 * hl_set_font_name() or from the font ID. */
5434 font = gui.norm_font;
5435 name = hl_get_font_name();
5436 }
5437 else
5438 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005439 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005440 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5441 return;
5442 font = gui_mch_get_font(name, FALSE);
5443 if (font == NOFONT)
5444 return; /* Invalid font name, return empty string. */
5445 }
5446 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5447 if (argvars[0].v_type != VAR_UNKNOWN)
5448 gui_mch_free_font(font);
5449 }
5450#endif
5451}
5452
5453/*
5454 * "getfperm({fname})" function
5455 */
5456 static void
5457f_getfperm(typval_T *argvars, typval_T *rettv)
5458{
5459 char_u *fname;
5460 stat_T st;
5461 char_u *perm = NULL;
5462 char_u flags[] = "rwx";
5463 int i;
5464
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005465 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005466
5467 rettv->v_type = VAR_STRING;
5468 if (mch_stat((char *)fname, &st) >= 0)
5469 {
5470 perm = vim_strsave((char_u *)"---------");
5471 if (perm != NULL)
5472 {
5473 for (i = 0; i < 9; i++)
5474 {
5475 if (st.st_mode & (1 << (8 - i)))
5476 perm[i] = flags[i % 3];
5477 }
5478 }
5479 }
5480 rettv->vval.v_string = perm;
5481}
5482
5483/*
5484 * "getfsize({fname})" function
5485 */
5486 static void
5487f_getfsize(typval_T *argvars, typval_T *rettv)
5488{
5489 char_u *fname;
5490 stat_T st;
5491
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005492 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493
5494 rettv->v_type = VAR_NUMBER;
5495
5496 if (mch_stat((char *)fname, &st) >= 0)
5497 {
5498 if (mch_isdir(fname))
5499 rettv->vval.v_number = 0;
5500 else
5501 {
5502 rettv->vval.v_number = (varnumber_T)st.st_size;
5503
5504 /* non-perfect check for overflow */
5505 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5506 rettv->vval.v_number = -2;
5507 }
5508 }
5509 else
5510 rettv->vval.v_number = -1;
5511}
5512
5513/*
5514 * "getftime({fname})" function
5515 */
5516 static void
5517f_getftime(typval_T *argvars, typval_T *rettv)
5518{
5519 char_u *fname;
5520 stat_T st;
5521
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005522 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005523
5524 if (mch_stat((char *)fname, &st) >= 0)
5525 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5526 else
5527 rettv->vval.v_number = -1;
5528}
5529
5530/*
5531 * "getftype({fname})" function
5532 */
5533 static void
5534f_getftype(typval_T *argvars, typval_T *rettv)
5535{
5536 char_u *fname;
5537 stat_T st;
5538 char_u *type = NULL;
5539 char *t;
5540
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005541 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542
5543 rettv->v_type = VAR_STRING;
5544 if (mch_lstat((char *)fname, &st) >= 0)
5545 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 if (S_ISREG(st.st_mode))
5547 t = "file";
5548 else if (S_ISDIR(st.st_mode))
5549 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 else if (S_ISLNK(st.st_mode))
5551 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 else if (S_ISBLK(st.st_mode))
5553 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 else if (S_ISCHR(st.st_mode))
5555 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 else if (S_ISFIFO(st.st_mode))
5557 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005559 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 else
5561 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005562 type = vim_strsave((char_u *)t);
5563 }
5564 rettv->vval.v_string = type;
5565}
5566
5567/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005568 * "getjumplist()" function
5569 */
5570 static void
5571f_getjumplist(typval_T *argvars, typval_T *rettv)
5572{
5573#ifdef FEAT_JUMPLIST
5574 win_T *wp;
5575 int i;
5576 list_T *l;
5577 dict_T *d;
5578#endif
5579
5580 if (rettv_list_alloc(rettv) != OK)
5581 return;
5582
5583#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005584 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005585 if (wp == NULL)
5586 return;
5587
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005588 cleanup_jumplist(wp, TRUE);
5589
Bram Moolenaar4f505882018-02-10 21:06:32 +01005590 l = list_alloc();
5591 if (l == NULL)
5592 return;
5593
5594 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5595 return;
5596 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5597
5598 for (i = 0; i < wp->w_jumplistlen; ++i)
5599 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005600 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5601 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005602 if ((d = dict_alloc()) == NULL)
5603 return;
5604 if (list_append_dict(l, d) == FAIL)
5605 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005606 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5607 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005608 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005609 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005610 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005611 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005612 }
5613#endif
5614}
5615
5616/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005617 * "getline(lnum, [end])" function
5618 */
5619 static void
5620f_getline(typval_T *argvars, typval_T *rettv)
5621{
5622 linenr_T lnum;
5623 linenr_T end;
5624 int retlist;
5625
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005626 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005627 if (argvars[1].v_type == VAR_UNKNOWN)
5628 {
5629 end = 0;
5630 retlist = FALSE;
5631 }
5632 else
5633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005634 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005635 retlist = TRUE;
5636 }
5637
5638 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5639}
5640
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005641#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005642 static void
5643get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5644{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005645 if (what_arg->v_type == VAR_UNKNOWN)
5646 {
5647 if (rettv_list_alloc(rettv) == OK)
5648 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005649 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005650 }
5651 else
5652 {
5653 if (rettv_dict_alloc(rettv) == OK)
5654 if (is_qf || (wp != NULL))
5655 {
5656 if (what_arg->v_type == VAR_DICT)
5657 {
5658 dict_T *d = what_arg->vval.v_dict;
5659
5660 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005661 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005662 }
5663 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005664 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005665 }
5666 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005667}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005668#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005669
5670/*
5671 * "getloclist()" function
5672 */
5673 static void
5674f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5675{
5676#ifdef FEAT_QUICKFIX
5677 win_T *wp;
5678
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005679 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005680 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5681#endif
5682}
5683
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684/*
5685 * "getmatches()" function
5686 */
5687 static void
5688f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5689{
5690#ifdef FEAT_SEARCH_EXTRA
5691 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005692 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005694 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005695
Bram Moolenaaraff74912019-03-30 18:11:49 +01005696 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5697 return;
5698
5699 cur = win->w_match_head;
5700 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005702 dict = dict_alloc();
5703 if (dict == NULL)
5704 return;
5705 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005706 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005707 /* match added with matchaddpos() */
5708 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005709 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005710 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005711 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005712 list_T *l;
5713
5714 llpos = &cur->pos.pos[i];
5715 if (llpos->lnum == 0)
5716 break;
5717 l = list_alloc();
5718 if (l == NULL)
5719 break;
5720 list_append_number(l, (varnumber_T)llpos->lnum);
5721 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005722 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005723 list_append_number(l, (varnumber_T)llpos->col);
5724 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005726 sprintf(buf, "pos%d", i + 1);
5727 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005728 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005729 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005730 else
5731 {
5732 dict_add_string(dict, "pattern", cur->pattern);
5733 }
5734 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5735 dict_add_number(dict, "priority", (long)cur->priority);
5736 dict_add_number(dict, "id", (long)cur->id);
5737# if defined(FEAT_CONCEAL)
5738 if (cur->conceal_char)
5739 {
5740 char_u buf[MB_MAXBYTES + 1];
5741
5742 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5743 dict_add_string(dict, "conceal", (char_u *)&buf);
5744 }
5745# endif
5746 list_append_dict(rettv->vval.v_list, dict);
5747 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005748 }
5749#endif
5750}
5751
5752/*
5753 * "getpid()" function
5754 */
5755 static void
5756f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5757{
5758 rettv->vval.v_number = mch_get_pid();
5759}
5760
5761 static void
5762getpos_both(
5763 typval_T *argvars,
5764 typval_T *rettv,
5765 int getcurpos)
5766{
5767 pos_T *fp;
5768 list_T *l;
5769 int fnum = -1;
5770
5771 if (rettv_list_alloc(rettv) == OK)
5772 {
5773 l = rettv->vval.v_list;
5774 if (getcurpos)
5775 fp = &curwin->w_cursor;
5776 else
5777 fp = var2fpos(&argvars[0], TRUE, &fnum);
5778 if (fnum != -1)
5779 list_append_number(l, (varnumber_T)fnum);
5780 else
5781 list_append_number(l, (varnumber_T)0);
5782 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5783 : (varnumber_T)0);
5784 list_append_number(l, (fp != NULL)
5785 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5786 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005787 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 (varnumber_T)0);
5789 if (getcurpos)
5790 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005791 int save_set_curswant = curwin->w_set_curswant;
5792 colnr_T save_curswant = curwin->w_curswant;
5793 colnr_T save_virtcol = curwin->w_virtcol;
5794
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 update_curswant();
5796 list_append_number(l, curwin->w_curswant == MAXCOL ?
5797 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005798
5799 // Do not change "curswant", as it is unexpected that a get
5800 // function has a side effect.
5801 if (save_set_curswant)
5802 {
5803 curwin->w_set_curswant = save_set_curswant;
5804 curwin->w_curswant = save_curswant;
5805 curwin->w_virtcol = save_virtcol;
5806 curwin->w_valid &= ~VALID_VIRTCOL;
5807 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 }
5809 }
5810 else
5811 rettv->vval.v_number = FALSE;
5812}
5813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814/*
5815 * "getcurpos()" function
5816 */
5817 static void
5818f_getcurpos(typval_T *argvars, typval_T *rettv)
5819{
5820 getpos_both(argvars, rettv, TRUE);
5821}
5822
5823/*
5824 * "getpos(string)" function
5825 */
5826 static void
5827f_getpos(typval_T *argvars, typval_T *rettv)
5828{
5829 getpos_both(argvars, rettv, FALSE);
5830}
5831
5832/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005833 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005834 */
5835 static void
5836f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5837{
5838#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005839 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005840#endif
5841}
5842
5843/*
5844 * "getreg()" function
5845 */
5846 static void
5847f_getreg(typval_T *argvars, typval_T *rettv)
5848{
5849 char_u *strregname;
5850 int regname;
5851 int arg2 = FALSE;
5852 int return_list = FALSE;
5853 int error = FALSE;
5854
5855 if (argvars[0].v_type != VAR_UNKNOWN)
5856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 error = strregname == NULL;
5859 if (argvars[1].v_type != VAR_UNKNOWN)
5860 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005863 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005864 }
5865 }
5866 else
5867 strregname = get_vim_var_str(VV_REG);
5868
5869 if (error)
5870 return;
5871
5872 regname = (strregname == NULL ? '"' : *strregname);
5873 if (regname == 0)
5874 regname = '"';
5875
5876 if (return_list)
5877 {
5878 rettv->v_type = VAR_LIST;
5879 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5880 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5881 if (rettv->vval.v_list == NULL)
5882 (void)rettv_list_alloc(rettv);
5883 else
5884 ++rettv->vval.v_list->lv_refcount;
5885 }
5886 else
5887 {
5888 rettv->v_type = VAR_STRING;
5889 rettv->vval.v_string = get_reg_contents(regname,
5890 arg2 ? GREG_EXPR_SRC : 0);
5891 }
5892}
5893
5894/*
5895 * "getregtype()" function
5896 */
5897 static void
5898f_getregtype(typval_T *argvars, typval_T *rettv)
5899{
5900 char_u *strregname;
5901 int regname;
5902 char_u buf[NUMBUFLEN + 2];
5903 long reglen = 0;
5904
5905 if (argvars[0].v_type != VAR_UNKNOWN)
5906 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005907 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005908 if (strregname == NULL) /* type error; errmsg already given */
5909 {
5910 rettv->v_type = VAR_STRING;
5911 rettv->vval.v_string = NULL;
5912 return;
5913 }
5914 }
5915 else
5916 /* Default to v:register */
5917 strregname = get_vim_var_str(VV_REG);
5918
5919 regname = (strregname == NULL ? '"' : *strregname);
5920 if (regname == 0)
5921 regname = '"';
5922
5923 buf[0] = NUL;
5924 buf[1] = NUL;
5925 switch (get_reg_type(regname, &reglen))
5926 {
5927 case MLINE: buf[0] = 'V'; break;
5928 case MCHAR: buf[0] = 'v'; break;
5929 case MBLOCK:
5930 buf[0] = Ctrl_V;
5931 sprintf((char *)buf + 1, "%ld", reglen + 1);
5932 break;
5933 }
5934 rettv->v_type = VAR_STRING;
5935 rettv->vval.v_string = vim_strsave(buf);
5936}
5937
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005938/*
5939 * Returns information (variables, options, etc.) about a tab page
5940 * as a dictionary.
5941 */
5942 static dict_T *
5943get_tabpage_info(tabpage_T *tp, int tp_idx)
5944{
5945 win_T *wp;
5946 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005947 list_T *l;
5948
5949 dict = dict_alloc();
5950 if (dict == NULL)
5951 return NULL;
5952
Bram Moolenaare0be1672018-07-08 16:50:37 +02005953 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005954
5955 l = list_alloc();
5956 if (l != NULL)
5957 {
5958 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5959 wp; wp = wp->w_next)
5960 list_append_number(l, (varnumber_T)wp->w_id);
5961 dict_add_list(dict, "windows", l);
5962 }
5963
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005964 /* Make a reference to tabpage variables */
5965 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005966
5967 return dict;
5968}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005969
5970/*
5971 * "gettabinfo()" function
5972 */
5973 static void
5974f_gettabinfo(typval_T *argvars, typval_T *rettv)
5975{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005976 tabpage_T *tp, *tparg = NULL;
5977 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005978 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005979
5980 if (rettv_list_alloc(rettv) != OK)
5981 return;
5982
5983 if (argvars[0].v_type != VAR_UNKNOWN)
5984 {
5985 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005986 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005987 if (tparg == NULL)
5988 return;
5989 }
5990
5991 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005992 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005993 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005994 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005995 if (tparg != NULL && tp != tparg)
5996 continue;
5997 d = get_tabpage_info(tp, tpnr);
5998 if (d != NULL)
5999 list_append_dict(rettv->vval.v_list, d);
6000 if (tparg != NULL)
6001 return;
6002 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006003}
6004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006005/*
6006 * "gettabvar()" function
6007 */
6008 static void
6009f_gettabvar(typval_T *argvars, typval_T *rettv)
6010{
6011 win_T *oldcurwin;
6012 tabpage_T *tp, *oldtabpage;
6013 dictitem_T *v;
6014 char_u *varname;
6015 int done = FALSE;
6016
6017 rettv->v_type = VAR_STRING;
6018 rettv->vval.v_string = NULL;
6019
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006020 varname = tv_get_string_chk(&argvars[1]);
6021 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006022 if (tp != NULL && varname != NULL)
6023 {
6024 /* Set tp to be our tabpage, temporarily. Also set the window to the
6025 * first window in the tabpage, otherwise the window is not valid. */
6026 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006027 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6028 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006029 {
6030 /* look up the variable */
6031 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6032 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6033 if (v != NULL)
6034 {
6035 copy_tv(&v->di_tv, rettv);
6036 done = TRUE;
6037 }
6038 }
6039
6040 /* restore previous notion of curwin */
6041 restore_win(oldcurwin, oldtabpage, TRUE);
6042 }
6043
6044 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6045 copy_tv(&argvars[2], rettv);
6046}
6047
6048/*
6049 * "gettabwinvar()" function
6050 */
6051 static void
6052f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6053{
6054 getwinvar(argvars, rettv, 1);
6055}
6056
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006057/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006058 * "gettagstack()" function
6059 */
6060 static void
6061f_gettagstack(typval_T *argvars, typval_T *rettv)
6062{
6063 win_T *wp = curwin; // default is current window
6064
6065 if (rettv_dict_alloc(rettv) != OK)
6066 return;
6067
6068 if (argvars[0].v_type != VAR_UNKNOWN)
6069 {
6070 wp = find_win_by_nr_or_id(&argvars[0]);
6071 if (wp == NULL)
6072 return;
6073 }
6074
6075 get_tagstack(wp, rettv->vval.v_dict);
6076}
6077
6078/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006079 * Returns information about a window as a dictionary.
6080 */
6081 static dict_T *
6082get_win_info(win_T *wp, short tpnr, short winnr)
6083{
6084 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006085
6086 dict = dict_alloc();
6087 if (dict == NULL)
6088 return NULL;
6089
Bram Moolenaare0be1672018-07-08 16:50:37 +02006090 dict_add_number(dict, "tabnr", tpnr);
6091 dict_add_number(dict, "winnr", winnr);
6092 dict_add_number(dict, "winid", wp->w_id);
6093 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006094 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006095 dict_add_number(dict, "topline", wp->w_topline);
6096 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006097#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006098 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006099#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006100 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006101 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006102 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006103
Bram Moolenaar69905d12017-08-13 18:14:47 +02006104#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006105 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006106#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006107#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006108 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6109 dict_add_number(dict, "loclist",
6110 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006111#endif
6112
Bram Moolenaar30567352016-08-27 21:25:44 +02006113 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006114 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006115
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006116 return dict;
6117}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006118
6119/*
6120 * "getwininfo()" function
6121 */
6122 static void
6123f_getwininfo(typval_T *argvars, typval_T *rettv)
6124{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006125 tabpage_T *tp;
6126 win_T *wp = NULL, *wparg = NULL;
6127 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006128 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006129
6130 if (rettv_list_alloc(rettv) != OK)
6131 return;
6132
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006133 if (argvars[0].v_type != VAR_UNKNOWN)
6134 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006135 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006136 if (wparg == NULL)
6137 return;
6138 }
6139
6140 /* Collect information about either all the windows across all the tab
6141 * pages or one particular window.
6142 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006143 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006144 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006145 tabnr++;
6146 winnr = 0;
6147 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006148 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006149 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006150 if (wparg != NULL && wp != wparg)
6151 continue;
6152 d = get_win_info(wp, tabnr, winnr);
6153 if (d != NULL)
6154 list_append_dict(rettv->vval.v_list, d);
6155 if (wparg != NULL)
6156 /* found information about a specific window */
6157 return;
6158 }
6159 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006160}
6161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006162/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006163 * "win_execute()" function
6164 */
6165 static void
6166f_win_execute(typval_T *argvars, typval_T *rettv)
6167{
6168 int id = (int)tv_get_number(argvars);
6169 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006170 win_T *save_curwin;
6171 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006172
6173 if (wp != NULL)
6174 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006175 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6176 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006177 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006178 check_cursor();
6179 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006180 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006181 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006182 }
6183}
6184
6185/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006186 * "win_findbuf()" function
6187 */
6188 static void
6189f_win_findbuf(typval_T *argvars, typval_T *rettv)
6190{
6191 if (rettv_list_alloc(rettv) != FAIL)
6192 win_findbuf(argvars, rettv->vval.v_list);
6193}
6194
6195/*
6196 * "win_getid()" function
6197 */
6198 static void
6199f_win_getid(typval_T *argvars, typval_T *rettv)
6200{
6201 rettv->vval.v_number = win_getid(argvars);
6202}
6203
6204/*
6205 * "win_gotoid()" function
6206 */
6207 static void
6208f_win_gotoid(typval_T *argvars, typval_T *rettv)
6209{
6210 rettv->vval.v_number = win_gotoid(argvars);
6211}
6212
6213/*
6214 * "win_id2tabwin()" function
6215 */
6216 static void
6217f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6218{
6219 if (rettv_list_alloc(rettv) != FAIL)
6220 win_id2tabwin(argvars, rettv->vval.v_list);
6221}
6222
6223/*
6224 * "win_id2win()" function
6225 */
6226 static void
6227f_win_id2win(typval_T *argvars, typval_T *rettv)
6228{
6229 rettv->vval.v_number = win_id2win(argvars);
6230}
6231
6232/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006233 * "win_screenpos()" function
6234 */
6235 static void
6236f_win_screenpos(typval_T *argvars, typval_T *rettv)
6237{
6238 win_T *wp;
6239
6240 if (rettv_list_alloc(rettv) == FAIL)
6241 return;
6242
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006243 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006244 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6245 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6246}
6247
6248/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006249 * "getwinpos({timeout})" function
6250 */
6251 static void
6252f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6253{
6254 int x = -1;
6255 int y = -1;
6256
6257 if (rettv_list_alloc(rettv) == FAIL)
6258 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006259#if defined(FEAT_GUI) \
6260 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6261 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006262 {
6263 varnumber_T timeout = 100;
6264
6265 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006266 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006267
6268 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006269 }
6270#endif
6271 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6272 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6273}
6274
6275
6276/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006277 * "getwinposx()" function
6278 */
6279 static void
6280f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6281{
6282 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006283#if defined(FEAT_GUI) \
6284 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6285 || defined(MSWIN)
6286
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006287 {
6288 int x, y;
6289
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006290 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006291 rettv->vval.v_number = x;
6292 }
6293#endif
6294}
6295
6296/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 * "getwinposy()" function
6298 */
6299 static void
6300f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6301{
6302 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006303#if defined(FEAT_GUI) \
6304 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6305 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 {
6307 int x, y;
6308
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006309 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006310 rettv->vval.v_number = y;
6311 }
6312#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313}
6314
6315/*
6316 * "getwinvar()" function
6317 */
6318 static void
6319f_getwinvar(typval_T *argvars, typval_T *rettv)
6320{
6321 getwinvar(argvars, rettv, 0);
6322}
6323
6324/*
6325 * "glob()" function
6326 */
6327 static void
6328f_glob(typval_T *argvars, typval_T *rettv)
6329{
6330 int options = WILD_SILENT|WILD_USE_NL;
6331 expand_T xpc;
6332 int error = FALSE;
6333
6334 /* When the optional second argument is non-zero, don't remove matches
6335 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6336 rettv->v_type = VAR_STRING;
6337 if (argvars[1].v_type != VAR_UNKNOWN)
6338 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006339 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 options |= WILD_KEEP_ALL;
6341 if (argvars[2].v_type != VAR_UNKNOWN)
6342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006343 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006344 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006345 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006346 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006347 options |= WILD_ALLLINKS;
6348 }
6349 }
6350 if (!error)
6351 {
6352 ExpandInit(&xpc);
6353 xpc.xp_context = EXPAND_FILES;
6354 if (p_wic)
6355 options += WILD_ICASE;
6356 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006357 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 NULL, options, WILD_ALL);
6359 else if (rettv_list_alloc(rettv) != FAIL)
6360 {
6361 int i;
6362
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006363 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006364 NULL, options, WILD_ALL_KEEP);
6365 for (i = 0; i < xpc.xp_numfiles; i++)
6366 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6367
6368 ExpandCleanup(&xpc);
6369 }
6370 }
6371 else
6372 rettv->vval.v_string = NULL;
6373}
6374
6375/*
6376 * "globpath()" function
6377 */
6378 static void
6379f_globpath(typval_T *argvars, typval_T *rettv)
6380{
6381 int flags = 0;
6382 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006383 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006384 int error = FALSE;
6385 garray_T ga;
6386 int i;
6387
6388 /* When the optional second argument is non-zero, don't remove matches
6389 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6390 rettv->v_type = VAR_STRING;
6391 if (argvars[2].v_type != VAR_UNKNOWN)
6392 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006393 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394 flags |= WILD_KEEP_ALL;
6395 if (argvars[3].v_type != VAR_UNKNOWN)
6396 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006397 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006398 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006399 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006400 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 flags |= WILD_ALLLINKS;
6402 }
6403 }
6404 if (file != NULL && !error)
6405 {
6406 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006407 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 if (rettv->v_type == VAR_STRING)
6409 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6410 else if (rettv_list_alloc(rettv) != FAIL)
6411 for (i = 0; i < ga.ga_len; ++i)
6412 list_append_string(rettv->vval.v_list,
6413 ((char_u **)(ga.ga_data))[i], -1);
6414 ga_clear_strings(&ga);
6415 }
6416 else
6417 rettv->vval.v_string = NULL;
6418}
6419
6420/*
6421 * "glob2regpat()" function
6422 */
6423 static void
6424f_glob2regpat(typval_T *argvars, typval_T *rettv)
6425{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006426 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006427
6428 rettv->v_type = VAR_STRING;
6429 rettv->vval.v_string = (pat == NULL)
6430 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6431}
6432
6433/* for VIM_VERSION_ defines */
6434#include "version.h"
6435
6436/*
6437 * "has()" function
6438 */
6439 static void
6440f_has(typval_T *argvars, typval_T *rettv)
6441{
6442 int i;
6443 char_u *name;
6444 int n = FALSE;
6445 static char *(has_list[]) =
6446 {
6447#ifdef AMIGA
6448 "amiga",
6449# ifdef FEAT_ARP
6450 "arp",
6451# endif
6452#endif
6453#ifdef __BEOS__
6454 "beos",
6455#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006456#if defined(BSD) && !defined(MACOS_X)
6457 "bsd",
6458#endif
6459#ifdef hpux
6460 "hpux",
6461#endif
6462#ifdef __linux__
6463 "linux",
6464#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006465#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006466 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6467 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006468# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006469 "macunix", /* Mac OS X, with the darwin feature */
6470 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006471# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006472#endif
6473#ifdef __QNX__
6474 "qnx",
6475#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006476#ifdef SUN_SYSTEM
6477 "sun",
6478#else
6479 "moon",
6480#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481#ifdef UNIX
6482 "unix",
6483#endif
6484#ifdef VMS
6485 "vms",
6486#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006487#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006488 "win32",
6489#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006490#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006491 "win32unix",
6492#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006493#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006494 "win64",
6495#endif
6496#ifdef EBCDIC
6497 "ebcdic",
6498#endif
6499#ifndef CASE_INSENSITIVE_FILENAME
6500 "fname_case",
6501#endif
6502#ifdef HAVE_ACL
6503 "acl",
6504#endif
6505#ifdef FEAT_ARABIC
6506 "arabic",
6507#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006508 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006509#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006510 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006511#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006512#ifdef FEAT_AUTOSERVERNAME
6513 "autoservername",
6514#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006515#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006517# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006518 "balloon_multiline",
6519# endif
6520#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006521#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006522 "balloon_eval_term",
6523#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006524#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6525 "builtin_terms",
6526# ifdef ALL_BUILTIN_TCAPS
6527 "all_builtin_terms",
6528# endif
6529#endif
6530#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006531 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006532 || defined(FEAT_GUI_MOTIF))
6533 "browsefilter",
6534#endif
6535#ifdef FEAT_BYTEOFF
6536 "byte_offset",
6537#endif
6538#ifdef FEAT_JOB_CHANNEL
6539 "channel",
6540#endif
6541#ifdef FEAT_CINDENT
6542 "cindent",
6543#endif
6544#ifdef FEAT_CLIENTSERVER
6545 "clientserver",
6546#endif
6547#ifdef FEAT_CLIPBOARD
6548 "clipboard",
6549#endif
6550#ifdef FEAT_CMDL_COMPL
6551 "cmdline_compl",
6552#endif
6553#ifdef FEAT_CMDHIST
6554 "cmdline_hist",
6555#endif
6556#ifdef FEAT_COMMENTS
6557 "comments",
6558#endif
6559#ifdef FEAT_CONCEAL
6560 "conceal",
6561#endif
6562#ifdef FEAT_CRYPT
6563 "cryptv",
6564 "crypt-blowfish",
6565 "crypt-blowfish2",
6566#endif
6567#ifdef FEAT_CSCOPE
6568 "cscope",
6569#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006570 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571#ifdef CURSOR_SHAPE
6572 "cursorshape",
6573#endif
6574#ifdef DEBUG
6575 "debug",
6576#endif
6577#ifdef FEAT_CON_DIALOG
6578 "dialog_con",
6579#endif
6580#ifdef FEAT_GUI_DIALOG
6581 "dialog_gui",
6582#endif
6583#ifdef FEAT_DIFF
6584 "diff",
6585#endif
6586#ifdef FEAT_DIGRAPHS
6587 "digraphs",
6588#endif
6589#ifdef FEAT_DIRECTX
6590 "directx",
6591#endif
6592#ifdef FEAT_DND
6593 "dnd",
6594#endif
6595#ifdef FEAT_EMACS_TAGS
6596 "emacs_tags",
6597#endif
6598 "eval", /* always present, of course! */
6599 "ex_extra", /* graduated feature */
6600#ifdef FEAT_SEARCH_EXTRA
6601 "extra_search",
6602#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006603#ifdef FEAT_SEARCHPATH
6604 "file_in_path",
6605#endif
6606#ifdef FEAT_FILTERPIPE
6607 "filterpipe",
6608#endif
6609#ifdef FEAT_FIND_ID
6610 "find_in_path",
6611#endif
6612#ifdef FEAT_FLOAT
6613 "float",
6614#endif
6615#ifdef FEAT_FOLDING
6616 "folding",
6617#endif
6618#ifdef FEAT_FOOTER
6619 "footer",
6620#endif
6621#if !defined(USE_SYSTEM) && defined(UNIX)
6622 "fork",
6623#endif
6624#ifdef FEAT_GETTEXT
6625 "gettext",
6626#endif
6627#ifdef FEAT_GUI
6628 "gui",
6629#endif
6630#ifdef FEAT_GUI_ATHENA
6631# ifdef FEAT_GUI_NEXTAW
6632 "gui_neXtaw",
6633# else
6634 "gui_athena",
6635# endif
6636#endif
6637#ifdef FEAT_GUI_GTK
6638 "gui_gtk",
6639# ifdef USE_GTK3
6640 "gui_gtk3",
6641# else
6642 "gui_gtk2",
6643# endif
6644#endif
6645#ifdef FEAT_GUI_GNOME
6646 "gui_gnome",
6647#endif
6648#ifdef FEAT_GUI_MAC
6649 "gui_mac",
6650#endif
6651#ifdef FEAT_GUI_MOTIF
6652 "gui_motif",
6653#endif
6654#ifdef FEAT_GUI_PHOTON
6655 "gui_photon",
6656#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006657#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658 "gui_win32",
6659#endif
6660#ifdef FEAT_HANGULIN
6661 "hangul_input",
6662#endif
6663#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6664 "iconv",
6665#endif
6666#ifdef FEAT_INS_EXPAND
6667 "insert_expand",
6668#endif
6669#ifdef FEAT_JOB_CHANNEL
6670 "job",
6671#endif
6672#ifdef FEAT_JUMPLIST
6673 "jumplist",
6674#endif
6675#ifdef FEAT_KEYMAP
6676 "keymap",
6677#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006678 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679#ifdef FEAT_LANGMAP
6680 "langmap",
6681#endif
6682#ifdef FEAT_LIBCALL
6683 "libcall",
6684#endif
6685#ifdef FEAT_LINEBREAK
6686 "linebreak",
6687#endif
6688#ifdef FEAT_LISP
6689 "lispindent",
6690#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006692#ifdef FEAT_LOCALMAP
6693 "localmap",
6694#endif
6695#ifdef FEAT_LUA
6696# ifndef DYNAMIC_LUA
6697 "lua",
6698# endif
6699#endif
6700#ifdef FEAT_MENU
6701 "menu",
6702#endif
6703#ifdef FEAT_SESSION
6704 "mksession",
6705#endif
6706#ifdef FEAT_MODIFY_FNAME
6707 "modify_fname",
6708#endif
6709#ifdef FEAT_MOUSE
6710 "mouse",
6711#endif
6712#ifdef FEAT_MOUSESHAPE
6713 "mouseshape",
6714#endif
6715#if defined(UNIX) || defined(VMS)
6716# ifdef FEAT_MOUSE_DEC
6717 "mouse_dec",
6718# endif
6719# ifdef FEAT_MOUSE_GPM
6720 "mouse_gpm",
6721# endif
6722# ifdef FEAT_MOUSE_JSB
6723 "mouse_jsbterm",
6724# endif
6725# ifdef FEAT_MOUSE_NET
6726 "mouse_netterm",
6727# endif
6728# ifdef FEAT_MOUSE_PTERM
6729 "mouse_pterm",
6730# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006731# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 "mouse_sgr",
6733# endif
6734# ifdef FEAT_SYSMOUSE
6735 "mouse_sysmouse",
6736# endif
6737# ifdef FEAT_MOUSE_URXVT
6738 "mouse_urxvt",
6739# endif
6740# ifdef FEAT_MOUSE_XTERM
6741 "mouse_xterm",
6742# endif
6743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006744 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745#ifdef FEAT_MBYTE_IME
6746 "multi_byte_ime",
6747#endif
6748#ifdef FEAT_MULTI_LANG
6749 "multi_lang",
6750#endif
6751#ifdef FEAT_MZSCHEME
6752#ifndef DYNAMIC_MZSCHEME
6753 "mzscheme",
6754#endif
6755#endif
6756#ifdef FEAT_NUM64
6757 "num64",
6758#endif
6759#ifdef FEAT_OLE
6760 "ole",
6761#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006762#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef FEAT_PATH_EXTRA
6766 "path_extra",
6767#endif
6768#ifdef FEAT_PERL
6769#ifndef DYNAMIC_PERL
6770 "perl",
6771#endif
6772#endif
6773#ifdef FEAT_PERSISTENT_UNDO
6774 "persistent_undo",
6775#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006776#if defined(FEAT_PYTHON)
6777 "python_compiled",
6778# if defined(DYNAMIC_PYTHON)
6779 "python_dynamic",
6780# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006782 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006783# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006785#if defined(FEAT_PYTHON3)
6786 "python3_compiled",
6787# if defined(DYNAMIC_PYTHON3)
6788 "python3_dynamic",
6789# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006790 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006791 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006792# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006793#endif
6794#ifdef FEAT_POSTSCRIPT
6795 "postscript",
6796#endif
6797#ifdef FEAT_PRINTER
6798 "printer",
6799#endif
6800#ifdef FEAT_PROFILE
6801 "profile",
6802#endif
6803#ifdef FEAT_RELTIME
6804 "reltime",
6805#endif
6806#ifdef FEAT_QUICKFIX
6807 "quickfix",
6808#endif
6809#ifdef FEAT_RIGHTLEFT
6810 "rightleft",
6811#endif
6812#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6813 "ruby",
6814#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006815 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006816#ifdef FEAT_CMDL_INFO
6817 "showcmd",
6818 "cmdline_info",
6819#endif
6820#ifdef FEAT_SIGNS
6821 "signs",
6822#endif
6823#ifdef FEAT_SMARTINDENT
6824 "smartindent",
6825#endif
6826#ifdef STARTUPTIME
6827 "startuptime",
6828#endif
6829#ifdef FEAT_STL_OPT
6830 "statusline",
6831#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006832#ifdef FEAT_NETBEANS_INTG
6833 "netbeans_intg",
6834#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006835#ifdef FEAT_SOUND
6836 "sound",
6837#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838#ifdef FEAT_SPELL
6839 "spell",
6840#endif
6841#ifdef FEAT_SYN_HL
6842 "syntax",
6843#endif
6844#if defined(USE_SYSTEM) || !defined(UNIX)
6845 "system",
6846#endif
6847#ifdef FEAT_TAG_BINS
6848 "tag_binary",
6849#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850#ifdef FEAT_TCL
6851# ifndef DYNAMIC_TCL
6852 "tcl",
6853# endif
6854#endif
6855#ifdef FEAT_TERMGUICOLORS
6856 "termguicolors",
6857#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006858#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006859 "terminal",
6860#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861#ifdef TERMINFO
6862 "terminfo",
6863#endif
6864#ifdef FEAT_TERMRESPONSE
6865 "termresponse",
6866#endif
6867#ifdef FEAT_TEXTOBJ
6868 "textobjects",
6869#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006870#ifdef FEAT_TEXT_PROP
6871 "textprop",
6872#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006873#ifdef HAVE_TGETENT
6874 "tgetent",
6875#endif
6876#ifdef FEAT_TIMERS
6877 "timers",
6878#endif
6879#ifdef FEAT_TITLE
6880 "title",
6881#endif
6882#ifdef FEAT_TOOLBAR
6883 "toolbar",
6884#endif
6885#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6886 "unnamedplus",
6887#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 "user-commands", /* was accidentally included in 5.4 */
6889 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006890#ifdef FEAT_VARTABS
6891 "vartabs",
6892#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006893 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006894#ifdef FEAT_VIMINFO
6895 "viminfo",
6896#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006897 "vimscript-1",
6898 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006899 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006904#ifdef FEAT_VTP
6905 "vtp",
6906#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006907#ifdef FEAT_WILDIGN
6908 "wildignore",
6909#endif
6910#ifdef FEAT_WILDMENU
6911 "wildmenu",
6912#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006913 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006914#ifdef FEAT_WAK
6915 "winaltkeys",
6916#endif
6917#ifdef FEAT_WRITEBACKUP
6918 "writebackup",
6919#endif
6920#ifdef FEAT_XIM
6921 "xim",
6922#endif
6923#ifdef FEAT_XFONTSET
6924 "xfontset",
6925#endif
6926#ifdef FEAT_XPM_W32
6927 "xpm",
6928 "xpm_w32", /* for backward compatibility */
6929#else
6930# if defined(HAVE_XPM)
6931 "xpm",
6932# endif
6933#endif
6934#ifdef USE_XSMP
6935 "xsmp",
6936#endif
6937#ifdef USE_XSMP_INTERACT
6938 "xsmp_interact",
6939#endif
6940#ifdef FEAT_XCLIPBOARD
6941 "xterm_clipboard",
6942#endif
6943#ifdef FEAT_XTERM_SAVE
6944 "xterm_save",
6945#endif
6946#if defined(UNIX) && defined(FEAT_X11)
6947 "X11",
6948#endif
6949 NULL
6950 };
6951
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006952 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006953 for (i = 0; has_list[i] != NULL; ++i)
6954 if (STRICMP(name, has_list[i]) == 0)
6955 {
6956 n = TRUE;
6957 break;
6958 }
6959
6960 if (n == FALSE)
6961 {
6962 if (STRNICMP(name, "patch", 5) == 0)
6963 {
6964 if (name[5] == '-'
6965 && STRLEN(name) >= 11
6966 && vim_isdigit(name[6])
6967 && vim_isdigit(name[8])
6968 && vim_isdigit(name[10]))
6969 {
6970 int major = atoi((char *)name + 6);
6971 int minor = atoi((char *)name + 8);
6972
6973 /* Expect "patch-9.9.01234". */
6974 n = (major < VIM_VERSION_MAJOR
6975 || (major == VIM_VERSION_MAJOR
6976 && (minor < VIM_VERSION_MINOR
6977 || (minor == VIM_VERSION_MINOR
6978 && has_patch(atoi((char *)name + 10))))));
6979 }
6980 else
6981 n = has_patch(atoi((char *)name + 5));
6982 }
6983 else if (STRICMP(name, "vim_starting") == 0)
6984 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006985 else if (STRICMP(name, "ttyin") == 0)
6986 n = mch_input_isatty();
6987 else if (STRICMP(name, "ttyout") == 0)
6988 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006989 else if (STRICMP(name, "multi_byte_encoding") == 0)
6990 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006991#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006992 else if (STRICMP(name, "balloon_multiline") == 0)
6993 n = multiline_balloon_available();
6994#endif
6995#ifdef DYNAMIC_TCL
6996 else if (STRICMP(name, "tcl") == 0)
6997 n = tcl_enabled(FALSE);
6998#endif
6999#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
7000 else if (STRICMP(name, "iconv") == 0)
7001 n = iconv_enabled(FALSE);
7002#endif
7003#ifdef DYNAMIC_LUA
7004 else if (STRICMP(name, "lua") == 0)
7005 n = lua_enabled(FALSE);
7006#endif
7007#ifdef DYNAMIC_MZSCHEME
7008 else if (STRICMP(name, "mzscheme") == 0)
7009 n = mzscheme_enabled(FALSE);
7010#endif
7011#ifdef DYNAMIC_RUBY
7012 else if (STRICMP(name, "ruby") == 0)
7013 n = ruby_enabled(FALSE);
7014#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007015#ifdef DYNAMIC_PYTHON
7016 else if (STRICMP(name, "python") == 0)
7017 n = python_enabled(FALSE);
7018#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007019#ifdef DYNAMIC_PYTHON3
7020 else if (STRICMP(name, "python3") == 0)
7021 n = python3_enabled(FALSE);
7022#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007023#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7024 else if (STRICMP(name, "pythonx") == 0)
7025 {
7026# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7027 if (p_pyx == 0)
7028 n = python3_enabled(FALSE) || python_enabled(FALSE);
7029 else if (p_pyx == 3)
7030 n = python3_enabled(FALSE);
7031 else if (p_pyx == 2)
7032 n = python_enabled(FALSE);
7033# elif defined(DYNAMIC_PYTHON)
7034 n = python_enabled(FALSE);
7035# elif defined(DYNAMIC_PYTHON3)
7036 n = python3_enabled(FALSE);
7037# endif
7038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007039#endif
7040#ifdef DYNAMIC_PERL
7041 else if (STRICMP(name, "perl") == 0)
7042 n = perl_enabled(FALSE);
7043#endif
7044#ifdef FEAT_GUI
7045 else if (STRICMP(name, "gui_running") == 0)
7046 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047# ifdef FEAT_BROWSE
7048 else if (STRICMP(name, "browse") == 0)
7049 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7050# endif
7051#endif
7052#ifdef FEAT_SYN_HL
7053 else if (STRICMP(name, "syntax_items") == 0)
7054 n = syntax_present(curwin);
7055#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007056#ifdef FEAT_VTP
7057 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007058 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007059#endif
7060#ifdef FEAT_NETBEANS_INTG
7061 else if (STRICMP(name, "netbeans_enabled") == 0)
7062 n = netbeans_active();
7063#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007064#ifdef FEAT_MOUSE_GPM
7065 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7066 n = gpm_enabled();
7067#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007068#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007069 else if (STRICMP(name, "terminal") == 0)
7070 n = terminal_enabled();
7071#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007072#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007073 else if (STRICMP(name, "conpty") == 0)
7074 n = use_conpty();
7075#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007076 }
7077
7078 rettv->vval.v_number = n;
7079}
7080
7081/*
7082 * "has_key()" function
7083 */
7084 static void
7085f_has_key(typval_T *argvars, typval_T *rettv)
7086{
7087 if (argvars[0].v_type != VAR_DICT)
7088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007089 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007090 return;
7091 }
7092 if (argvars[0].vval.v_dict == NULL)
7093 return;
7094
7095 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007096 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007097}
7098
7099/*
7100 * "haslocaldir()" function
7101 */
7102 static void
7103f_haslocaldir(typval_T *argvars, typval_T *rettv)
7104{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007105 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 win_T *wp = NULL;
7107
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007108 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7109
7110 // Check for window-local and tab-local directories
7111 if (wp != NULL && wp->w_localdir != NULL)
7112 rettv->vval.v_number = 1;
7113 else if (tp != NULL && tp->tp_localdir != NULL)
7114 rettv->vval.v_number = 2;
7115 else
7116 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007117}
7118
7119/*
7120 * "hasmapto()" function
7121 */
7122 static void
7123f_hasmapto(typval_T *argvars, typval_T *rettv)
7124{
7125 char_u *name;
7126 char_u *mode;
7127 char_u buf[NUMBUFLEN];
7128 int abbr = FALSE;
7129
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007130 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 if (argvars[1].v_type == VAR_UNKNOWN)
7132 mode = (char_u *)"nvo";
7133 else
7134 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007135 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007137 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 }
7139
7140 if (map_to_exists(name, mode, abbr))
7141 rettv->vval.v_number = TRUE;
7142 else
7143 rettv->vval.v_number = FALSE;
7144}
7145
7146/*
7147 * "histadd()" function
7148 */
7149 static void
7150f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7151{
7152#ifdef FEAT_CMDHIST
7153 int histype;
7154 char_u *str;
7155 char_u buf[NUMBUFLEN];
7156#endif
7157
7158 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007159 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007160 return;
7161#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007162 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007163 histype = str != NULL ? get_histtype(str) : -1;
7164 if (histype >= 0)
7165 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007166 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007167 if (*str != NUL)
7168 {
7169 init_history();
7170 add_to_history(histype, str, FALSE, NUL);
7171 rettv->vval.v_number = TRUE;
7172 return;
7173 }
7174 }
7175#endif
7176}
7177
7178/*
7179 * "histdel()" function
7180 */
7181 static void
7182f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7183{
7184#ifdef FEAT_CMDHIST
7185 int n;
7186 char_u buf[NUMBUFLEN];
7187 char_u *str;
7188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007189 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007190 if (str == NULL)
7191 n = 0;
7192 else if (argvars[1].v_type == VAR_UNKNOWN)
7193 /* only one argument: clear entire history */
7194 n = clr_history(get_histtype(str));
7195 else if (argvars[1].v_type == VAR_NUMBER)
7196 /* index given: remove that entry */
7197 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007198 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007199 else
7200 /* string given: remove all matching entries */
7201 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007202 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007203 rettv->vval.v_number = n;
7204#endif
7205}
7206
7207/*
7208 * "histget()" function
7209 */
7210 static void
7211f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7212{
7213#ifdef FEAT_CMDHIST
7214 int type;
7215 int idx;
7216 char_u *str;
7217
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007218 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219 if (str == NULL)
7220 rettv->vval.v_string = NULL;
7221 else
7222 {
7223 type = get_histtype(str);
7224 if (argvars[1].v_type == VAR_UNKNOWN)
7225 idx = get_history_idx(type);
7226 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007227 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228 /* -1 on type error */
7229 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7230 }
7231#else
7232 rettv->vval.v_string = NULL;
7233#endif
7234 rettv->v_type = VAR_STRING;
7235}
7236
7237/*
7238 * "histnr()" function
7239 */
7240 static void
7241f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7242{
7243 int i;
7244
7245#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007246 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247
7248 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7249 if (i >= HIST_CMD && i < HIST_COUNT)
7250 i = get_history_idx(i);
7251 else
7252#endif
7253 i = -1;
7254 rettv->vval.v_number = i;
7255}
7256
7257/*
7258 * "highlightID(name)" function
7259 */
7260 static void
7261f_hlID(typval_T *argvars, typval_T *rettv)
7262{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007263 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264}
7265
7266/*
7267 * "highlight_exists()" function
7268 */
7269 static void
7270f_hlexists(typval_T *argvars, typval_T *rettv)
7271{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007272 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007273}
7274
7275/*
7276 * "hostname()" function
7277 */
7278 static void
7279f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7280{
7281 char_u hostname[256];
7282
7283 mch_get_host_name(hostname, 256);
7284 rettv->v_type = VAR_STRING;
7285 rettv->vval.v_string = vim_strsave(hostname);
7286}
7287
7288/*
7289 * iconv() function
7290 */
7291 static void
7292f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7293{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 char_u buf1[NUMBUFLEN];
7295 char_u buf2[NUMBUFLEN];
7296 char_u *from, *to, *str;
7297 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298
7299 rettv->v_type = VAR_STRING;
7300 rettv->vval.v_string = NULL;
7301
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007302 str = tv_get_string(&argvars[0]);
7303 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7304 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305 vimconv.vc_type = CONV_NONE;
7306 convert_setup(&vimconv, from, to);
7307
7308 /* If the encodings are equal, no conversion needed. */
7309 if (vimconv.vc_type == CONV_NONE)
7310 rettv->vval.v_string = vim_strsave(str);
7311 else
7312 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7313
7314 convert_setup(&vimconv, NULL, NULL);
7315 vim_free(from);
7316 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007317}
7318
7319/*
7320 * "indent()" function
7321 */
7322 static void
7323f_indent(typval_T *argvars, typval_T *rettv)
7324{
7325 linenr_T lnum;
7326
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007327 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7329 rettv->vval.v_number = get_indent_lnum(lnum);
7330 else
7331 rettv->vval.v_number = -1;
7332}
7333
7334/*
7335 * "index()" function
7336 */
7337 static void
7338f_index(typval_T *argvars, typval_T *rettv)
7339{
7340 list_T *l;
7341 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007342 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 long idx = 0;
7344 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007345 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346
7347 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007348 if (argvars[0].v_type == VAR_BLOB)
7349 {
7350 typval_T tv;
7351 int start = 0;
7352
7353 if (argvars[2].v_type != VAR_UNKNOWN)
7354 {
7355 start = tv_get_number_chk(&argvars[2], &error);
7356 if (error)
7357 return;
7358 }
7359 b = argvars[0].vval.v_blob;
7360 if (b == NULL)
7361 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007362 if (start < 0)
7363 {
7364 start = blob_len(b) + start;
7365 if (start < 0)
7366 start = 0;
7367 }
7368
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007369 for (idx = start; idx < blob_len(b); ++idx)
7370 {
7371 tv.v_type = VAR_NUMBER;
7372 tv.vval.v_number = blob_get(b, idx);
7373 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7374 {
7375 rettv->vval.v_number = idx;
7376 return;
7377 }
7378 }
7379 return;
7380 }
7381 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007383 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007384 return;
7385 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007386
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 l = argvars[0].vval.v_list;
7388 if (l != NULL)
7389 {
7390 item = l->lv_first;
7391 if (argvars[2].v_type != VAR_UNKNOWN)
7392 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007393 /* Start at specified item. Use the cached index that list_find()
7394 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007395 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007396 idx = l->lv_idx;
7397 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007398 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007399 if (error)
7400 item = NULL;
7401 }
7402
7403 for ( ; item != NULL; item = item->li_next, ++idx)
7404 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7405 {
7406 rettv->vval.v_number = idx;
7407 break;
7408 }
7409 }
7410}
7411
7412static int inputsecret_flag = 0;
7413
7414/*
7415 * "input()" function
7416 * Also handles inputsecret() when inputsecret is set.
7417 */
7418 static void
7419f_input(typval_T *argvars, typval_T *rettv)
7420{
7421 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7422}
7423
7424/*
7425 * "inputdialog()" function
7426 */
7427 static void
7428f_inputdialog(typval_T *argvars, typval_T *rettv)
7429{
7430#if defined(FEAT_GUI_TEXTDIALOG)
7431 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7432 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7433 {
7434 char_u *message;
7435 char_u buf[NUMBUFLEN];
7436 char_u *defstr = (char_u *)"";
7437
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007438 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007440 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007441 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7442 else
7443 IObuff[0] = NUL;
7444 if (message != NULL && defstr != NULL
7445 && do_dialog(VIM_QUESTION, NULL, message,
7446 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7447 rettv->vval.v_string = vim_strsave(IObuff);
7448 else
7449 {
7450 if (message != NULL && defstr != NULL
7451 && argvars[1].v_type != VAR_UNKNOWN
7452 && argvars[2].v_type != VAR_UNKNOWN)
7453 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007454 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007455 else
7456 rettv->vval.v_string = NULL;
7457 }
7458 rettv->v_type = VAR_STRING;
7459 }
7460 else
7461#endif
7462 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7463}
7464
7465/*
7466 * "inputlist()" function
7467 */
7468 static void
7469f_inputlist(typval_T *argvars, typval_T *rettv)
7470{
7471 listitem_T *li;
7472 int selected;
7473 int mouse_used;
7474
7475#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007476 /* While starting up, there is no place to enter text. When running tests
7477 * with --not-a-term we assume feedkeys() will be used. */
7478 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 return;
7480#endif
7481 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 return;
7485 }
7486
7487 msg_start();
7488 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7489 lines_left = Rows; /* avoid more prompt */
7490 msg_scroll = TRUE;
7491 msg_clr_eos();
7492
7493 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7494 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007495 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007496 msg_putchar('\n');
7497 }
7498
7499 /* Ask for choice. */
7500 selected = prompt_for_number(&mouse_used);
7501 if (mouse_used)
7502 selected -= lines_left;
7503
7504 rettv->vval.v_number = selected;
7505}
7506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007507static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7508
7509/*
7510 * "inputrestore()" function
7511 */
7512 static void
7513f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7514{
7515 if (ga_userinput.ga_len > 0)
7516 {
7517 --ga_userinput.ga_len;
7518 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7519 + ga_userinput.ga_len);
7520 /* default return is zero == OK */
7521 }
7522 else if (p_verbose > 1)
7523 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007524 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 rettv->vval.v_number = 1; /* Failed */
7526 }
7527}
7528
7529/*
7530 * "inputsave()" function
7531 */
7532 static void
7533f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7534{
7535 /* Add an entry to the stack of typeahead storage. */
7536 if (ga_grow(&ga_userinput, 1) == OK)
7537 {
7538 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7539 + ga_userinput.ga_len);
7540 ++ga_userinput.ga_len;
7541 /* default return is zero == OK */
7542 }
7543 else
7544 rettv->vval.v_number = 1; /* Failed */
7545}
7546
7547/*
7548 * "inputsecret()" function
7549 */
7550 static void
7551f_inputsecret(typval_T *argvars, typval_T *rettv)
7552{
7553 ++cmdline_star;
7554 ++inputsecret_flag;
7555 f_input(argvars, rettv);
7556 --cmdline_star;
7557 --inputsecret_flag;
7558}
7559
7560/*
7561 * "insert()" function
7562 */
7563 static void
7564f_insert(typval_T *argvars, typval_T *rettv)
7565{
7566 long before = 0;
7567 listitem_T *item;
7568 list_T *l;
7569 int error = FALSE;
7570
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007571 if (argvars[0].v_type == VAR_BLOB)
7572 {
7573 int val, len;
7574 char_u *p;
7575
7576 len = blob_len(argvars[0].vval.v_blob);
7577 if (argvars[2].v_type != VAR_UNKNOWN)
7578 {
7579 before = (long)tv_get_number_chk(&argvars[2], &error);
7580 if (error)
7581 return; // type error; errmsg already given
7582 if (before < 0 || before > len)
7583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007584 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007585 return;
7586 }
7587 }
7588 val = tv_get_number_chk(&argvars[1], &error);
7589 if (error)
7590 return;
7591 if (val < 0 || val > 255)
7592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007594 return;
7595 }
7596
7597 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7598 return;
7599 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7600 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7601 *(p + before) = val;
7602 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7603
7604 copy_tv(&argvars[0], rettv);
7605 }
7606 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007607 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007608 else if ((l = argvars[0].vval.v_list) != NULL
7609 && !var_check_lock(l->lv_lock,
7610 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 {
7612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007613 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007614 if (error)
7615 return; /* type error; errmsg already given */
7616
7617 if (before == l->lv_len)
7618 item = NULL;
7619 else
7620 {
7621 item = list_find(l, before);
7622 if (item == NULL)
7623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007624 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007625 l = NULL;
7626 }
7627 }
7628 if (l != NULL)
7629 {
7630 list_insert_tv(l, &argvars[1], item);
7631 copy_tv(&argvars[0], rettv);
7632 }
7633 }
7634}
7635
7636/*
7637 * "invert(expr)" function
7638 */
7639 static void
7640f_invert(typval_T *argvars, typval_T *rettv)
7641{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007642 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007643}
7644
7645/*
7646 * "isdirectory()" function
7647 */
7648 static void
7649f_isdirectory(typval_T *argvars, typval_T *rettv)
7650{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007651 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652}
7653
7654/*
7655 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7656 * or it refers to a List or Dictionary that is locked.
7657 */
7658 static int
7659tv_islocked(typval_T *tv)
7660{
7661 return (tv->v_lock & VAR_LOCKED)
7662 || (tv->v_type == VAR_LIST
7663 && tv->vval.v_list != NULL
7664 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7665 || (tv->v_type == VAR_DICT
7666 && tv->vval.v_dict != NULL
7667 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7668}
7669
7670/*
7671 * "islocked()" function
7672 */
7673 static void
7674f_islocked(typval_T *argvars, typval_T *rettv)
7675{
7676 lval_T lv;
7677 char_u *end;
7678 dictitem_T *di;
7679
7680 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007681 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007682 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007683 if (end != NULL && lv.ll_name != NULL)
7684 {
7685 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007686 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007687 else
7688 {
7689 if (lv.ll_tv == NULL)
7690 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007691 di = find_var(lv.ll_name, NULL, TRUE);
7692 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007694 /* Consider a variable locked when:
7695 * 1. the variable itself is locked
7696 * 2. the value of the variable is locked.
7697 * 3. the List or Dict value is locked.
7698 */
7699 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7700 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007701 }
7702 }
7703 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007704 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007706 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007707 else if (lv.ll_list != NULL)
7708 /* List item. */
7709 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7710 else
7711 /* Dictionary item. */
7712 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7713 }
7714 }
7715
7716 clear_lval(&lv);
7717}
7718
7719#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7720/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007721 * "isinf()" function
7722 */
7723 static void
7724f_isinf(typval_T *argvars, typval_T *rettv)
7725{
7726 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7727 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7728}
7729
7730/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007731 * "isnan()" function
7732 */
7733 static void
7734f_isnan(typval_T *argvars, typval_T *rettv)
7735{
7736 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7737 && isnan(argvars[0].vval.v_float);
7738}
7739#endif
7740
7741/*
7742 * "items(dict)" function
7743 */
7744 static void
7745f_items(typval_T *argvars, typval_T *rettv)
7746{
7747 dict_list(argvars, rettv, 2);
7748}
7749
7750#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7751/*
7752 * Get the job from the argument.
7753 * Returns NULL if the job is invalid.
7754 */
7755 static job_T *
7756get_job_arg(typval_T *tv)
7757{
7758 job_T *job;
7759
7760 if (tv->v_type != VAR_JOB)
7761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007762 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007763 return NULL;
7764 }
7765 job = tv->vval.v_job;
7766
7767 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007768 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007769 return job;
7770}
7771
7772/*
7773 * "job_getchannel()" function
7774 */
7775 static void
7776f_job_getchannel(typval_T *argvars, typval_T *rettv)
7777{
7778 job_T *job = get_job_arg(&argvars[0]);
7779
7780 if (job != NULL)
7781 {
7782 rettv->v_type = VAR_CHANNEL;
7783 rettv->vval.v_channel = job->jv_channel;
7784 if (job->jv_channel != NULL)
7785 ++job->jv_channel->ch_refcount;
7786 }
7787}
7788
7789/*
7790 * "job_info()" function
7791 */
7792 static void
7793f_job_info(typval_T *argvars, typval_T *rettv)
7794{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007795 if (argvars[0].v_type != VAR_UNKNOWN)
7796 {
7797 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007798
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007799 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7800 job_info(job, rettv->vval.v_dict);
7801 }
7802 else if (rettv_list_alloc(rettv) == OK)
7803 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007804}
7805
7806/*
7807 * "job_setoptions()" function
7808 */
7809 static void
7810f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7811{
7812 job_T *job = get_job_arg(&argvars[0]);
7813 jobopt_T opt;
7814
7815 if (job == NULL)
7816 return;
7817 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007818 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007819 job_set_options(job, &opt);
7820 free_job_options(&opt);
7821}
7822
7823/*
7824 * "job_start()" function
7825 */
7826 static void
7827f_job_start(typval_T *argvars, typval_T *rettv)
7828{
7829 rettv->v_type = VAR_JOB;
7830 if (check_restricted() || check_secure())
7831 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007832 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007833}
7834
7835/*
7836 * "job_status()" function
7837 */
7838 static void
7839f_job_status(typval_T *argvars, typval_T *rettv)
7840{
7841 job_T *job = get_job_arg(&argvars[0]);
7842
7843 if (job != NULL)
7844 {
7845 rettv->v_type = VAR_STRING;
7846 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7847 }
7848}
7849
7850/*
7851 * "job_stop()" function
7852 */
7853 static void
7854f_job_stop(typval_T *argvars, typval_T *rettv)
7855{
7856 job_T *job = get_job_arg(&argvars[0]);
7857
7858 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007859 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007860}
7861#endif
7862
7863/*
7864 * "join()" function
7865 */
7866 static void
7867f_join(typval_T *argvars, typval_T *rettv)
7868{
7869 garray_T ga;
7870 char_u *sep;
7871
7872 if (argvars[0].v_type != VAR_LIST)
7873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007874 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 return;
7876 }
7877 if (argvars[0].vval.v_list == NULL)
7878 return;
7879 if (argvars[1].v_type == VAR_UNKNOWN)
7880 sep = (char_u *)" ";
7881 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007882 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007883
7884 rettv->v_type = VAR_STRING;
7885
7886 if (sep != NULL)
7887 {
7888 ga_init2(&ga, (int)sizeof(char), 80);
7889 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7890 ga_append(&ga, NUL);
7891 rettv->vval.v_string = (char_u *)ga.ga_data;
7892 }
7893 else
7894 rettv->vval.v_string = NULL;
7895}
7896
7897/*
7898 * "js_decode()" function
7899 */
7900 static void
7901f_js_decode(typval_T *argvars, typval_T *rettv)
7902{
7903 js_read_T reader;
7904
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007905 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007906 reader.js_fill = NULL;
7907 reader.js_used = 0;
7908 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007909 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007910}
7911
7912/*
7913 * "js_encode()" function
7914 */
7915 static void
7916f_js_encode(typval_T *argvars, typval_T *rettv)
7917{
7918 rettv->v_type = VAR_STRING;
7919 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7920}
7921
7922/*
7923 * "json_decode()" function
7924 */
7925 static void
7926f_json_decode(typval_T *argvars, typval_T *rettv)
7927{
7928 js_read_T reader;
7929
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007930 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007931 reader.js_fill = NULL;
7932 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007933 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934}
7935
7936/*
7937 * "json_encode()" function
7938 */
7939 static void
7940f_json_encode(typval_T *argvars, typval_T *rettv)
7941{
7942 rettv->v_type = VAR_STRING;
7943 rettv->vval.v_string = json_encode(&argvars[0], 0);
7944}
7945
7946/*
7947 * "keys()" function
7948 */
7949 static void
7950f_keys(typval_T *argvars, typval_T *rettv)
7951{
7952 dict_list(argvars, rettv, 0);
7953}
7954
7955/*
7956 * "last_buffer_nr()" function.
7957 */
7958 static void
7959f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7960{
7961 int n = 0;
7962 buf_T *buf;
7963
Bram Moolenaar29323592016-07-24 22:04:11 +02007964 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007965 if (n < buf->b_fnum)
7966 n = buf->b_fnum;
7967
7968 rettv->vval.v_number = n;
7969}
7970
7971/*
7972 * "len()" function
7973 */
7974 static void
7975f_len(typval_T *argvars, typval_T *rettv)
7976{
7977 switch (argvars[0].v_type)
7978 {
7979 case VAR_STRING:
7980 case VAR_NUMBER:
7981 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007982 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007983 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007984 case VAR_BLOB:
7985 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7986 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007987 case VAR_LIST:
7988 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7989 break;
7990 case VAR_DICT:
7991 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7992 break;
7993 case VAR_UNKNOWN:
7994 case VAR_SPECIAL:
7995 case VAR_FLOAT:
7996 case VAR_FUNC:
7997 case VAR_PARTIAL:
7998 case VAR_JOB:
7999 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008000 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 break;
8002 }
8003}
8004
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01008006libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008007{
8008#ifdef FEAT_LIBCALL
8009 char_u *string_in;
8010 char_u **string_result;
8011 int nr_result;
8012#endif
8013
8014 rettv->v_type = type;
8015 if (type != VAR_NUMBER)
8016 rettv->vval.v_string = NULL;
8017
8018 if (check_restricted() || check_secure())
8019 return;
8020
8021#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008022 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008023 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8024 {
8025 string_in = NULL;
8026 if (argvars[2].v_type == VAR_STRING)
8027 string_in = argvars[2].vval.v_string;
8028 if (type == VAR_NUMBER)
8029 string_result = NULL;
8030 else
8031 string_result = &rettv->vval.v_string;
8032 if (mch_libcall(argvars[0].vval.v_string,
8033 argvars[1].vval.v_string,
8034 string_in,
8035 argvars[2].vval.v_number,
8036 string_result,
8037 &nr_result) == OK
8038 && type == VAR_NUMBER)
8039 rettv->vval.v_number = nr_result;
8040 }
8041#endif
8042}
8043
8044/*
8045 * "libcall()" function
8046 */
8047 static void
8048f_libcall(typval_T *argvars, typval_T *rettv)
8049{
8050 libcall_common(argvars, rettv, VAR_STRING);
8051}
8052
8053/*
8054 * "libcallnr()" function
8055 */
8056 static void
8057f_libcallnr(typval_T *argvars, typval_T *rettv)
8058{
8059 libcall_common(argvars, rettv, VAR_NUMBER);
8060}
8061
8062/*
8063 * "line(string)" function
8064 */
8065 static void
8066f_line(typval_T *argvars, typval_T *rettv)
8067{
8068 linenr_T lnum = 0;
8069 pos_T *fp;
8070 int fnum;
8071
8072 fp = var2fpos(&argvars[0], TRUE, &fnum);
8073 if (fp != NULL)
8074 lnum = fp->lnum;
8075 rettv->vval.v_number = lnum;
8076}
8077
8078/*
8079 * "line2byte(lnum)" function
8080 */
8081 static void
8082f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8083{
8084#ifndef FEAT_BYTEOFF
8085 rettv->vval.v_number = -1;
8086#else
8087 linenr_T lnum;
8088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008089 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8091 rettv->vval.v_number = -1;
8092 else
8093 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8094 if (rettv->vval.v_number >= 0)
8095 ++rettv->vval.v_number;
8096#endif
8097}
8098
8099/*
8100 * "lispindent(lnum)" function
8101 */
8102 static void
8103f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8104{
8105#ifdef FEAT_LISP
8106 pos_T pos;
8107 linenr_T lnum;
8108
8109 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008110 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008111 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8112 {
8113 curwin->w_cursor.lnum = lnum;
8114 rettv->vval.v_number = get_lisp_indent();
8115 curwin->w_cursor = pos;
8116 }
8117 else
8118#endif
8119 rettv->vval.v_number = -1;
8120}
8121
8122/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008123 * "list2str()" function
8124 */
8125 static void
8126f_list2str(typval_T *argvars, typval_T *rettv)
8127{
8128 list_T *l;
8129 listitem_T *li;
8130 garray_T ga;
8131 int utf8 = FALSE;
8132
8133 rettv->v_type = VAR_STRING;
8134 rettv->vval.v_string = NULL;
8135 if (argvars[0].v_type != VAR_LIST)
8136 {
8137 emsg(_(e_invarg));
8138 return;
8139 }
8140
8141 l = argvars[0].vval.v_list;
8142 if (l == NULL)
8143 return; // empty list results in empty string
8144
8145 if (argvars[1].v_type != VAR_UNKNOWN)
8146 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8147
8148 ga_init2(&ga, 1, 80);
8149 if (has_mbyte || utf8)
8150 {
8151 char_u buf[MB_MAXBYTES + 1];
8152 int (*char2bytes)(int, char_u *);
8153
8154 if (utf8 || enc_utf8)
8155 char2bytes = utf_char2bytes;
8156 else
8157 char2bytes = mb_char2bytes;
8158
8159 for (li = l->lv_first; li != NULL; li = li->li_next)
8160 {
8161 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8162 ga_concat(&ga, buf);
8163 }
8164 ga_append(&ga, NUL);
8165 }
8166 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8167 {
8168 for (li = l->lv_first; li != NULL; li = li->li_next)
8169 ga_append(&ga, tv_get_number(&li->li_tv));
8170 ga_append(&ga, NUL);
8171 }
8172
8173 rettv->v_type = VAR_STRING;
8174 rettv->vval.v_string = ga.ga_data;
8175}
8176
8177/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008178 * "localtime()" function
8179 */
8180 static void
8181f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8182{
8183 rettv->vval.v_number = (varnumber_T)time(NULL);
8184}
8185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186 static void
8187get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8188{
8189 char_u *keys;
8190 char_u *which;
8191 char_u buf[NUMBUFLEN];
8192 char_u *keys_buf = NULL;
8193 char_u *rhs;
8194 int mode;
8195 int abbr = FALSE;
8196 int get_dict = FALSE;
8197 mapblock_T *mp;
8198 int buffer_local;
8199
8200 /* return empty string for failure */
8201 rettv->v_type = VAR_STRING;
8202 rettv->vval.v_string = NULL;
8203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008204 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205 if (*keys == NUL)
8206 return;
8207
8208 if (argvars[1].v_type != VAR_UNKNOWN)
8209 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008210 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 if (argvars[2].v_type != VAR_UNKNOWN)
8212 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008213 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008215 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008216 }
8217 }
8218 else
8219 which = (char_u *)"";
8220 if (which == NULL)
8221 return;
8222
8223 mode = get_map_mode(&which, 0);
8224
8225 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8226 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8227 vim_free(keys_buf);
8228
8229 if (!get_dict)
8230 {
8231 /* Return a string. */
8232 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008233 {
8234 if (*rhs == NUL)
8235 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8236 else
8237 rettv->vval.v_string = str2special_save(rhs, FALSE);
8238 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008239
8240 }
8241 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8242 {
8243 /* Return a dictionary. */
8244 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8245 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8246 dict_T *dict = rettv->vval.v_dict;
8247
Bram Moolenaare0be1672018-07-08 16:50:37 +02008248 dict_add_string(dict, "lhs", lhs);
8249 dict_add_string(dict, "rhs", mp->m_orig_str);
8250 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8251 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8252 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008253 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8254 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008255 dict_add_number(dict, "buffer", (long)buffer_local);
8256 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8257 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008258
8259 vim_free(lhs);
8260 vim_free(mapmode);
8261 }
8262}
8263
8264#ifdef FEAT_FLOAT
8265/*
8266 * "log()" function
8267 */
8268 static void
8269f_log(typval_T *argvars, typval_T *rettv)
8270{
8271 float_T f = 0.0;
8272
8273 rettv->v_type = VAR_FLOAT;
8274 if (get_float_arg(argvars, &f) == OK)
8275 rettv->vval.v_float = log(f);
8276 else
8277 rettv->vval.v_float = 0.0;
8278}
8279
8280/*
8281 * "log10()" function
8282 */
8283 static void
8284f_log10(typval_T *argvars, typval_T *rettv)
8285{
8286 float_T f = 0.0;
8287
8288 rettv->v_type = VAR_FLOAT;
8289 if (get_float_arg(argvars, &f) == OK)
8290 rettv->vval.v_float = log10(f);
8291 else
8292 rettv->vval.v_float = 0.0;
8293}
8294#endif
8295
8296#ifdef FEAT_LUA
8297/*
8298 * "luaeval()" function
8299 */
8300 static void
8301f_luaeval(typval_T *argvars, typval_T *rettv)
8302{
8303 char_u *str;
8304 char_u buf[NUMBUFLEN];
8305
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008306 if (check_restricted() || check_secure())
8307 return;
8308
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008309 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008310 do_luaeval(str, argvars + 1, rettv);
8311}
8312#endif
8313
8314/*
8315 * "map()" function
8316 */
8317 static void
8318f_map(typval_T *argvars, typval_T *rettv)
8319{
8320 filter_map(argvars, rettv, TRUE);
8321}
8322
8323/*
8324 * "maparg()" function
8325 */
8326 static void
8327f_maparg(typval_T *argvars, typval_T *rettv)
8328{
8329 get_maparg(argvars, rettv, TRUE);
8330}
8331
8332/*
8333 * "mapcheck()" function
8334 */
8335 static void
8336f_mapcheck(typval_T *argvars, typval_T *rettv)
8337{
8338 get_maparg(argvars, rettv, FALSE);
8339}
8340
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008341typedef enum
8342{
8343 MATCH_END, /* matchend() */
8344 MATCH_MATCH, /* match() */
8345 MATCH_STR, /* matchstr() */
8346 MATCH_LIST, /* matchlist() */
8347 MATCH_POS /* matchstrpos() */
8348} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008349
8350 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008351find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008352{
8353 char_u *str = NULL;
8354 long len = 0;
8355 char_u *expr = NULL;
8356 char_u *pat;
8357 regmatch_T regmatch;
8358 char_u patbuf[NUMBUFLEN];
8359 char_u strbuf[NUMBUFLEN];
8360 char_u *save_cpo;
8361 long start = 0;
8362 long nth = 1;
8363 colnr_T startcol = 0;
8364 int match = 0;
8365 list_T *l = NULL;
8366 listitem_T *li = NULL;
8367 long idx = 0;
8368 char_u *tofree = NULL;
8369
8370 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8371 save_cpo = p_cpo;
8372 p_cpo = (char_u *)"";
8373
8374 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008375 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008376 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008377 /* type MATCH_LIST: return empty list when there are no matches.
8378 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008379 if (rettv_list_alloc(rettv) == FAIL)
8380 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008381 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008382 && (list_append_string(rettv->vval.v_list,
8383 (char_u *)"", 0) == FAIL
8384 || list_append_number(rettv->vval.v_list,
8385 (varnumber_T)-1) == FAIL
8386 || list_append_number(rettv->vval.v_list,
8387 (varnumber_T)-1) == FAIL
8388 || list_append_number(rettv->vval.v_list,
8389 (varnumber_T)-1) == FAIL))
8390 {
8391 list_free(rettv->vval.v_list);
8392 rettv->vval.v_list = NULL;
8393 goto theend;
8394 }
8395 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008396 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 {
8398 rettv->v_type = VAR_STRING;
8399 rettv->vval.v_string = NULL;
8400 }
8401
8402 if (argvars[0].v_type == VAR_LIST)
8403 {
8404 if ((l = argvars[0].vval.v_list) == NULL)
8405 goto theend;
8406 li = l->lv_first;
8407 }
8408 else
8409 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008410 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 len = (long)STRLEN(str);
8412 }
8413
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008414 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 if (pat == NULL)
8416 goto theend;
8417
8418 if (argvars[2].v_type != VAR_UNKNOWN)
8419 {
8420 int error = FALSE;
8421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008422 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008423 if (error)
8424 goto theend;
8425 if (l != NULL)
8426 {
8427 li = list_find(l, start);
8428 if (li == NULL)
8429 goto theend;
8430 idx = l->lv_idx; /* use the cached index */
8431 }
8432 else
8433 {
8434 if (start < 0)
8435 start = 0;
8436 if (start > len)
8437 goto theend;
8438 /* When "count" argument is there ignore matches before "start",
8439 * otherwise skip part of the string. Differs when pattern is "^"
8440 * or "\<". */
8441 if (argvars[3].v_type != VAR_UNKNOWN)
8442 startcol = start;
8443 else
8444 {
8445 str += start;
8446 len -= start;
8447 }
8448 }
8449
8450 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008451 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 if (error)
8453 goto theend;
8454 }
8455
8456 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8457 if (regmatch.regprog != NULL)
8458 {
8459 regmatch.rm_ic = p_ic;
8460
8461 for (;;)
8462 {
8463 if (l != NULL)
8464 {
8465 if (li == NULL)
8466 {
8467 match = FALSE;
8468 break;
8469 }
8470 vim_free(tofree);
8471 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8472 if (str == NULL)
8473 break;
8474 }
8475
8476 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8477
8478 if (match && --nth <= 0)
8479 break;
8480 if (l == NULL && !match)
8481 break;
8482
8483 /* Advance to just after the match. */
8484 if (l != NULL)
8485 {
8486 li = li->li_next;
8487 ++idx;
8488 }
8489 else
8490 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 startcol = (colnr_T)(regmatch.startp[0]
8492 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 if (startcol > (colnr_T)len
8494 || str + startcol <= regmatch.startp[0])
8495 {
8496 match = FALSE;
8497 break;
8498 }
8499 }
8500 }
8501
8502 if (match)
8503 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008504 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008505 {
8506 listitem_T *li1 = rettv->vval.v_list->lv_first;
8507 listitem_T *li2 = li1->li_next;
8508 listitem_T *li3 = li2->li_next;
8509 listitem_T *li4 = li3->li_next;
8510
8511 vim_free(li1->li_tv.vval.v_string);
8512 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8513 (int)(regmatch.endp[0] - regmatch.startp[0]));
8514 li3->li_tv.vval.v_number =
8515 (varnumber_T)(regmatch.startp[0] - expr);
8516 li4->li_tv.vval.v_number =
8517 (varnumber_T)(regmatch.endp[0] - expr);
8518 if (l != NULL)
8519 li2->li_tv.vval.v_number = (varnumber_T)idx;
8520 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008521 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008522 {
8523 int i;
8524
8525 /* return list with matched string and submatches */
8526 for (i = 0; i < NSUBEXP; ++i)
8527 {
8528 if (regmatch.endp[i] == NULL)
8529 {
8530 if (list_append_string(rettv->vval.v_list,
8531 (char_u *)"", 0) == FAIL)
8532 break;
8533 }
8534 else if (list_append_string(rettv->vval.v_list,
8535 regmatch.startp[i],
8536 (int)(regmatch.endp[i] - regmatch.startp[i]))
8537 == FAIL)
8538 break;
8539 }
8540 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008541 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008542 {
8543 /* return matched string */
8544 if (l != NULL)
8545 copy_tv(&li->li_tv, rettv);
8546 else
8547 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8548 (int)(regmatch.endp[0] - regmatch.startp[0]));
8549 }
8550 else if (l != NULL)
8551 rettv->vval.v_number = idx;
8552 else
8553 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008554 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008555 rettv->vval.v_number =
8556 (varnumber_T)(regmatch.startp[0] - str);
8557 else
8558 rettv->vval.v_number =
8559 (varnumber_T)(regmatch.endp[0] - str);
8560 rettv->vval.v_number += (varnumber_T)(str - expr);
8561 }
8562 }
8563 vim_regfree(regmatch.regprog);
8564 }
8565
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008566theend:
8567 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008568 /* matchstrpos() without a list: drop the second item. */
8569 listitem_remove(rettv->vval.v_list,
8570 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 vim_free(tofree);
8572 p_cpo = save_cpo;
8573}
8574
8575/*
8576 * "match()" function
8577 */
8578 static void
8579f_match(typval_T *argvars, typval_T *rettv)
8580{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008581 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008582}
8583
Bram Moolenaar95e51472018-07-28 16:55:56 +02008584#ifdef FEAT_SEARCH_EXTRA
8585 static int
8586matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8587{
8588 dictitem_T *di;
8589
8590 if (tv->v_type != VAR_DICT)
8591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008593 return FAIL;
8594 }
8595
8596 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008597 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008598 (char_u *)"conceal", FALSE);
8599
8600 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8601 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008602 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008603 if (*win == NULL)
8604 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008605 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008606 return FAIL;
8607 }
8608 }
8609
8610 return OK;
8611}
8612#endif
8613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008614/*
8615 * "matchadd()" function
8616 */
8617 static void
8618f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8619{
8620#ifdef FEAT_SEARCH_EXTRA
8621 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008622 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8623 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 int prio = 10; /* default priority */
8625 int id = -1;
8626 int error = FALSE;
8627 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008628 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629
8630 rettv->vval.v_number = -1;
8631
8632 if (grp == NULL || pat == NULL)
8633 return;
8634 if (argvars[2].v_type != VAR_UNKNOWN)
8635 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008636 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 if (argvars[3].v_type != VAR_UNKNOWN)
8638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008639 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008640 if (argvars[4].v_type != VAR_UNKNOWN
8641 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8642 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008643 }
8644 }
8645 if (error == TRUE)
8646 return;
8647 if (id >= 1 && id <= 3)
8648 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008649 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008650 return;
8651 }
8652
Bram Moolenaar95e51472018-07-28 16:55:56 +02008653 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 conceal_char);
8655#endif
8656}
8657
8658/*
8659 * "matchaddpos()" function
8660 */
8661 static void
8662f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8663{
8664#ifdef FEAT_SEARCH_EXTRA
8665 char_u buf[NUMBUFLEN];
8666 char_u *group;
8667 int prio = 10;
8668 int id = -1;
8669 int error = FALSE;
8670 list_T *l;
8671 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008672 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008673
8674 rettv->vval.v_number = -1;
8675
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008676 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008677 if (group == NULL)
8678 return;
8679
8680 if (argvars[1].v_type != VAR_LIST)
8681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008682 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683 return;
8684 }
8685 l = argvars[1].vval.v_list;
8686 if (l == NULL)
8687 return;
8688
8689 if (argvars[2].v_type != VAR_UNKNOWN)
8690 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008691 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692 if (argvars[3].v_type != VAR_UNKNOWN)
8693 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008694 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008695
8696 if (argvars[4].v_type != VAR_UNKNOWN
8697 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8698 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699 }
8700 }
8701 if (error == TRUE)
8702 return;
8703
8704 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8705 if (id == 1 || id == 2)
8706 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008707 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708 return;
8709 }
8710
Bram Moolenaar95e51472018-07-28 16:55:56 +02008711 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008712 conceal_char);
8713#endif
8714}
8715
8716/*
8717 * "matcharg()" function
8718 */
8719 static void
8720f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8721{
8722 if (rettv_list_alloc(rettv) == OK)
8723 {
8724#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008725 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726 matchitem_T *m;
8727
8728 if (id >= 1 && id <= 3)
8729 {
8730 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8731 {
8732 list_append_string(rettv->vval.v_list,
8733 syn_id2name(m->hlg_id), -1);
8734 list_append_string(rettv->vval.v_list, m->pattern, -1);
8735 }
8736 else
8737 {
8738 list_append_string(rettv->vval.v_list, NULL, -1);
8739 list_append_string(rettv->vval.v_list, NULL, -1);
8740 }
8741 }
8742#endif
8743 }
8744}
8745
8746/*
8747 * "matchdelete()" function
8748 */
8749 static void
8750f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8751{
8752#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008753 win_T *win = get_optional_window(argvars, 1);
8754
8755 if (win == NULL)
8756 rettv->vval.v_number = -1;
8757 else
8758 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008759 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008760#endif
8761}
8762
8763/*
8764 * "matchend()" function
8765 */
8766 static void
8767f_matchend(typval_T *argvars, typval_T *rettv)
8768{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008769 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770}
8771
8772/*
8773 * "matchlist()" function
8774 */
8775 static void
8776f_matchlist(typval_T *argvars, typval_T *rettv)
8777{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008778 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008779}
8780
8781/*
8782 * "matchstr()" function
8783 */
8784 static void
8785f_matchstr(typval_T *argvars, typval_T *rettv)
8786{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008787 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788}
8789
8790/*
8791 * "matchstrpos()" function
8792 */
8793 static void
8794f_matchstrpos(typval_T *argvars, typval_T *rettv)
8795{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008796 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797}
8798
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008799 static void
8800max_min(typval_T *argvars, typval_T *rettv, int domax)
8801{
8802 varnumber_T n = 0;
8803 varnumber_T i;
8804 int error = FALSE;
8805
8806 if (argvars[0].v_type == VAR_LIST)
8807 {
8808 list_T *l;
8809 listitem_T *li;
8810
8811 l = argvars[0].vval.v_list;
8812 if (l != NULL)
8813 {
8814 li = l->lv_first;
8815 if (li != NULL)
8816 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008818 for (;;)
8819 {
8820 li = li->li_next;
8821 if (li == NULL)
8822 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008823 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008824 if (domax ? i > n : i < n)
8825 n = i;
8826 }
8827 }
8828 }
8829 }
8830 else if (argvars[0].v_type == VAR_DICT)
8831 {
8832 dict_T *d;
8833 int first = TRUE;
8834 hashitem_T *hi;
8835 int todo;
8836
8837 d = argvars[0].vval.v_dict;
8838 if (d != NULL)
8839 {
8840 todo = (int)d->dv_hashtab.ht_used;
8841 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8842 {
8843 if (!HASHITEM_EMPTY(hi))
8844 {
8845 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008846 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 if (first)
8848 {
8849 n = i;
8850 first = FALSE;
8851 }
8852 else if (domax ? i > n : i < n)
8853 n = i;
8854 }
8855 }
8856 }
8857 }
8858 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008859 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008860 rettv->vval.v_number = error ? 0 : n;
8861}
8862
8863/*
8864 * "max()" function
8865 */
8866 static void
8867f_max(typval_T *argvars, typval_T *rettv)
8868{
8869 max_min(argvars, rettv, TRUE);
8870}
8871
8872/*
8873 * "min()" function
8874 */
8875 static void
8876f_min(typval_T *argvars, typval_T *rettv)
8877{
8878 max_min(argvars, rettv, FALSE);
8879}
8880
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881/*
8882 * Create the directory in which "dir" is located, and higher levels when
8883 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008884 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008885 */
8886 static int
8887mkdir_recurse(char_u *dir, int prot)
8888{
8889 char_u *p;
8890 char_u *updir;
8891 int r = FAIL;
8892
8893 /* Get end of directory name in "dir".
8894 * We're done when it's "/" or "c:/". */
8895 p = gettail_sep(dir);
8896 if (p <= get_past_head(dir))
8897 return OK;
8898
8899 /* If the directory exists we're done. Otherwise: create it.*/
8900 updir = vim_strnsave(dir, (int)(p - dir));
8901 if (updir == NULL)
8902 return FAIL;
8903 if (mch_isdir(updir))
8904 r = OK;
8905 else if (mkdir_recurse(updir, prot) == OK)
8906 r = vim_mkdir_emsg(updir, prot);
8907 vim_free(updir);
8908 return r;
8909}
8910
8911#ifdef vim_mkdir
8912/*
8913 * "mkdir()" function
8914 */
8915 static void
8916f_mkdir(typval_T *argvars, typval_T *rettv)
8917{
8918 char_u *dir;
8919 char_u buf[NUMBUFLEN];
8920 int prot = 0755;
8921
8922 rettv->vval.v_number = FAIL;
8923 if (check_restricted() || check_secure())
8924 return;
8925
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008926 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008928 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008930 if (*gettail(dir) == NUL)
8931 /* remove trailing slashes */
8932 *gettail_sep(dir) = NUL;
8933
8934 if (argvars[1].v_type != VAR_UNKNOWN)
8935 {
8936 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008938 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008939 if (prot == -1)
8940 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008941 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008942 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008943 {
8944 if (mch_isdir(dir))
8945 {
8946 /* With the "p" flag it's OK if the dir already exists. */
8947 rettv->vval.v_number = OK;
8948 return;
8949 }
8950 mkdir_recurse(dir, prot);
8951 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008953 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954}
8955#endif
8956
8957/*
8958 * "mode()" function
8959 */
8960 static void
8961f_mode(typval_T *argvars, typval_T *rettv)
8962{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008963 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964
Bram Moolenaar612cc382018-07-29 15:34:26 +02008965 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966
8967 if (time_for_testing == 93784)
8968 {
8969 /* Testing the two-character code. */
8970 buf[0] = 'x';
8971 buf[1] = '!';
8972 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008973#ifdef FEAT_TERMINAL
8974 else if (term_use_loop())
8975 buf[0] = 't';
8976#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008977 else if (VIsual_active)
8978 {
8979 if (VIsual_select)
8980 buf[0] = VIsual_mode + 's' - 'v';
8981 else
8982 buf[0] = VIsual_mode;
8983 }
8984 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8985 || State == CONFIRM)
8986 {
8987 buf[0] = 'r';
8988 if (State == ASKMORE)
8989 buf[1] = 'm';
8990 else if (State == CONFIRM)
8991 buf[1] = '?';
8992 }
8993 else if (State == EXTERNCMD)
8994 buf[0] = '!';
8995 else if (State & INSERT)
8996 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008997 if (State & VREPLACE_FLAG)
8998 {
8999 buf[0] = 'R';
9000 buf[1] = 'v';
9001 }
9002 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01009003 {
9004 if (State & REPLACE_FLAG)
9005 buf[0] = 'R';
9006 else
9007 buf[0] = 'i';
9008#ifdef FEAT_INS_EXPAND
9009 if (ins_compl_active())
9010 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01009011 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009012 buf[1] = 'x';
9013#endif
9014 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009016 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 {
9018 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009019 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009021 else if (exmode_active == EXMODE_NORMAL)
9022 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 }
9024 else
9025 {
9026 buf[0] = 'n';
9027 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009028 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009030 // to be able to detect force-linewise/blockwise/characterwise operations
9031 buf[2] = motion_force;
9032 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009033 else if (restart_edit == 'I' || restart_edit == 'R'
9034 || restart_edit == 'V')
9035 {
9036 buf[1] = 'i';
9037 buf[2] = restart_edit;
9038 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009039 }
9040
9041 /* Clear out the minor mode when the argument is not a non-zero number or
9042 * non-empty string. */
9043 if (!non_zero_arg(&argvars[0]))
9044 buf[1] = NUL;
9045
9046 rettv->vval.v_string = vim_strsave(buf);
9047 rettv->v_type = VAR_STRING;
9048}
9049
9050#if defined(FEAT_MZSCHEME) || defined(PROTO)
9051/*
9052 * "mzeval()" function
9053 */
9054 static void
9055f_mzeval(typval_T *argvars, typval_T *rettv)
9056{
9057 char_u *str;
9058 char_u buf[NUMBUFLEN];
9059
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009060 if (check_restricted() || check_secure())
9061 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009062 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 do_mzeval(str, rettv);
9064}
9065
9066 void
9067mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9068{
9069 typval_T argvars[3];
9070
9071 argvars[0].v_type = VAR_STRING;
9072 argvars[0].vval.v_string = name;
9073 copy_tv(args, &argvars[1]);
9074 argvars[2].v_type = VAR_UNKNOWN;
9075 f_call(argvars, rettv);
9076 clear_tv(&argvars[1]);
9077}
9078#endif
9079
9080/*
9081 * "nextnonblank()" function
9082 */
9083 static void
9084f_nextnonblank(typval_T *argvars, typval_T *rettv)
9085{
9086 linenr_T lnum;
9087
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009088 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009089 {
9090 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9091 {
9092 lnum = 0;
9093 break;
9094 }
9095 if (*skipwhite(ml_get(lnum)) != NUL)
9096 break;
9097 }
9098 rettv->vval.v_number = lnum;
9099}
9100
9101/*
9102 * "nr2char()" function
9103 */
9104 static void
9105f_nr2char(typval_T *argvars, typval_T *rettv)
9106{
9107 char_u buf[NUMBUFLEN];
9108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 if (has_mbyte)
9110 {
9111 int utf8 = 0;
9112
9113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009114 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009116 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009118 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 }
9120 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009122 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009123 buf[1] = NUL;
9124 }
9125 rettv->v_type = VAR_STRING;
9126 rettv->vval.v_string = vim_strsave(buf);
9127}
9128
9129/*
9130 * "or(expr, expr)" function
9131 */
9132 static void
9133f_or(typval_T *argvars, typval_T *rettv)
9134{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009135 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9136 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137}
9138
9139/*
9140 * "pathshorten()" function
9141 */
9142 static void
9143f_pathshorten(typval_T *argvars, typval_T *rettv)
9144{
9145 char_u *p;
9146
9147 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009148 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 if (p == NULL)
9150 rettv->vval.v_string = NULL;
9151 else
9152 {
9153 p = vim_strsave(p);
9154 rettv->vval.v_string = p;
9155 if (p != NULL)
9156 shorten_dir(p);
9157 }
9158}
9159
9160#ifdef FEAT_PERL
9161/*
9162 * "perleval()" function
9163 */
9164 static void
9165f_perleval(typval_T *argvars, typval_T *rettv)
9166{
9167 char_u *str;
9168 char_u buf[NUMBUFLEN];
9169
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009170 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009171 do_perleval(str, rettv);
9172}
9173#endif
9174
9175#ifdef FEAT_FLOAT
9176/*
9177 * "pow()" function
9178 */
9179 static void
9180f_pow(typval_T *argvars, typval_T *rettv)
9181{
9182 float_T fx = 0.0, fy = 0.0;
9183
9184 rettv->v_type = VAR_FLOAT;
9185 if (get_float_arg(argvars, &fx) == OK
9186 && get_float_arg(&argvars[1], &fy) == OK)
9187 rettv->vval.v_float = pow(fx, fy);
9188 else
9189 rettv->vval.v_float = 0.0;
9190}
9191#endif
9192
9193/*
9194 * "prevnonblank()" function
9195 */
9196 static void
9197f_prevnonblank(typval_T *argvars, typval_T *rettv)
9198{
9199 linenr_T lnum;
9200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009201 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009202 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9203 lnum = 0;
9204 else
9205 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9206 --lnum;
9207 rettv->vval.v_number = lnum;
9208}
9209
9210/* This dummy va_list is here because:
9211 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9212 * - locally in the function results in a "used before set" warning
9213 * - using va_start() to initialize it gives "function with fixed args" error */
9214static va_list ap;
9215
9216/*
9217 * "printf()" function
9218 */
9219 static void
9220f_printf(typval_T *argvars, typval_T *rettv)
9221{
9222 char_u buf[NUMBUFLEN];
9223 int len;
9224 char_u *s;
9225 int saved_did_emsg = did_emsg;
9226 char *fmt;
9227
9228 rettv->v_type = VAR_STRING;
9229 rettv->vval.v_string = NULL;
9230
9231 /* Get the required length, allocate the buffer and do it for real. */
9232 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009233 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009234 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009235 if (!did_emsg)
9236 {
9237 s = alloc(len + 1);
9238 if (s != NULL)
9239 {
9240 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009241 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9242 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009243 }
9244 }
9245 did_emsg |= saved_did_emsg;
9246}
9247
Bram Moolenaarf2732452018-06-03 14:47:35 +02009248#ifdef FEAT_JOB_CHANNEL
9249/*
9250 * "prompt_setcallback({buffer}, {callback})" function
9251 */
9252 static void
9253f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9254{
9255 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009256 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009257
9258 if (check_secure())
9259 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009260 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009261 if (buf == NULL)
9262 return;
9263
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009264 callback = get_callback(&argvars[1]);
9265 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009266 return;
9267
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009268 free_callback(&buf->b_prompt_callback);
9269 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009270}
9271
9272/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009273 * "prompt_setinterrupt({buffer}, {callback})" function
9274 */
9275 static void
9276f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9277{
9278 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009279 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009280
9281 if (check_secure())
9282 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009283 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009284 if (buf == NULL)
9285 return;
9286
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009287 callback = get_callback(&argvars[1]);
9288 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009289 return;
9290
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009291 free_callback(&buf->b_prompt_interrupt);
9292 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009293}
9294
9295/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009296 * "prompt_setprompt({buffer}, {text})" function
9297 */
9298 static void
9299f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9300{
9301 buf_T *buf;
9302 char_u *text;
9303
9304 if (check_secure())
9305 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009306 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009307 if (buf == NULL)
9308 return;
9309
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009310 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009311 vim_free(buf->b_prompt_text);
9312 buf->b_prompt_text = vim_strsave(text);
9313}
9314#endif
9315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316/*
9317 * "pumvisible()" function
9318 */
9319 static void
9320f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9321{
9322#ifdef FEAT_INS_EXPAND
9323 if (pum_visible())
9324 rettv->vval.v_number = 1;
9325#endif
9326}
9327
9328#ifdef FEAT_PYTHON3
9329/*
9330 * "py3eval()" function
9331 */
9332 static void
9333f_py3eval(typval_T *argvars, typval_T *rettv)
9334{
9335 char_u *str;
9336 char_u buf[NUMBUFLEN];
9337
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009338 if (check_restricted() || check_secure())
9339 return;
9340
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009341 if (p_pyx == 0)
9342 p_pyx = 3;
9343
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009344 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 do_py3eval(str, rettv);
9346}
9347#endif
9348
9349#ifdef FEAT_PYTHON
9350/*
9351 * "pyeval()" function
9352 */
9353 static void
9354f_pyeval(typval_T *argvars, typval_T *rettv)
9355{
9356 char_u *str;
9357 char_u buf[NUMBUFLEN];
9358
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009359 if (check_restricted() || check_secure())
9360 return;
9361
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009362 if (p_pyx == 0)
9363 p_pyx = 2;
9364
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009365 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009366 do_pyeval(str, rettv);
9367}
9368#endif
9369
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009370#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9371/*
9372 * "pyxeval()" function
9373 */
9374 static void
9375f_pyxeval(typval_T *argvars, typval_T *rettv)
9376{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009377 if (check_restricted() || check_secure())
9378 return;
9379
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009380# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9381 init_pyxversion();
9382 if (p_pyx == 2)
9383 f_pyeval(argvars, rettv);
9384 else
9385 f_py3eval(argvars, rettv);
9386# elif defined(FEAT_PYTHON)
9387 f_pyeval(argvars, rettv);
9388# elif defined(FEAT_PYTHON3)
9389 f_py3eval(argvars, rettv);
9390# endif
9391}
9392#endif
9393
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009394/*
9395 * "range()" function
9396 */
9397 static void
9398f_range(typval_T *argvars, typval_T *rettv)
9399{
9400 varnumber_T start;
9401 varnumber_T end;
9402 varnumber_T stride = 1;
9403 varnumber_T i;
9404 int error = FALSE;
9405
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009406 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009407 if (argvars[1].v_type == VAR_UNKNOWN)
9408 {
9409 end = start - 1;
9410 start = 0;
9411 }
9412 else
9413 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009414 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009416 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417 }
9418
9419 if (error)
9420 return; /* type error; errmsg already given */
9421 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009422 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009424 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009425 else
9426 {
9427 if (rettv_list_alloc(rettv) == OK)
9428 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9429 if (list_append_number(rettv->vval.v_list,
9430 (varnumber_T)i) == FAIL)
9431 break;
9432 }
9433}
9434
9435/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009436 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009437 */
9438 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009439readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009440{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009441 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009442 typval_T save_val;
9443 typval_T rettv;
9444 typval_T argv[2];
9445 int retval = 0;
9446 int error = FALSE;
9447
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009448 if (expr->v_type == VAR_UNKNOWN)
9449 return 1;
9450
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009451 prepare_vimvar(VV_VAL, &save_val);
9452 set_vim_var_string(VV_VAL, name, -1);
9453 argv[0].v_type = VAR_STRING;
9454 argv[0].vval.v_string = name;
9455
9456 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9457 goto theend;
9458
9459 retval = tv_get_number_chk(&rettv, &error);
9460 if (error)
9461 retval = -1;
9462 clear_tv(&rettv);
9463
9464theend:
9465 set_vim_var_string(VV_VAL, NULL, 0);
9466 restore_vimvar(VV_VAL, &save_val);
9467 return retval;
9468}
9469
9470/*
9471 * "readdir()" function
9472 */
9473 static void
9474f_readdir(typval_T *argvars, typval_T *rettv)
9475{
9476 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009477 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009478 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009479 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009480 garray_T ga;
9481 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009482
9483 if (rettv_list_alloc(rettv) == FAIL)
9484 return;
9485 path = tv_get_string(&argvars[0]);
9486 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009487
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009488 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9489 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009490 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009491 for (i = 0; i < ga.ga_len; i++)
9492 {
9493 p = ((char_u **)ga.ga_data)[i];
9494 list_append_string(rettv->vval.v_list, p, -1);
9495 }
9496 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009497 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009498}
9499
9500/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501 * "readfile()" function
9502 */
9503 static void
9504f_readfile(typval_T *argvars, typval_T *rettv)
9505{
9506 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009507 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508 int failed = FALSE;
9509 char_u *fname;
9510 FILE *fd;
9511 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9512 int io_size = sizeof(buf);
9513 int readlen; /* size of last fread() */
9514 char_u *prev = NULL; /* previously read bytes, if any */
9515 long prevlen = 0; /* length of data in prev */
9516 long prevsize = 0; /* size of prev buffer */
9517 long maxline = MAXLNUM;
9518 long cnt = 0;
9519 char_u *p; /* position in buf */
9520 char_u *start; /* start of current line */
9521
9522 if (argvars[1].v_type != VAR_UNKNOWN)
9523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009524 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009525 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009526 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9527 blob = TRUE;
9528
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009530 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531 }
9532
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009533 if (blob)
9534 {
9535 if (rettv_blob_alloc(rettv) == FAIL)
9536 return;
9537 }
9538 else
9539 {
9540 if (rettv_list_alloc(rettv) == FAIL)
9541 return;
9542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009543
9544 /* Always open the file in binary mode, library functions have a mind of
9545 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009546 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009547 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9548 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009549 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009550 return;
9551 }
9552
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009553 if (blob)
9554 {
9555 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009557 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009558 blob_free(rettv->vval.v_blob);
9559 }
9560 fclose(fd);
9561 return;
9562 }
9563
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009564 while (cnt < maxline || maxline < 0)
9565 {
9566 readlen = (int)fread(buf, 1, io_size, fd);
9567
9568 /* This for loop processes what was read, but is also entered at end
9569 * of file so that either:
9570 * - an incomplete line gets written
9571 * - a "binary" file gets an empty line at the end if it ends in a
9572 * newline. */
9573 for (p = buf, start = buf;
9574 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9575 ++p)
9576 {
9577 if (*p == '\n' || readlen <= 0)
9578 {
9579 listitem_T *li;
9580 char_u *s = NULL;
9581 long_u len = p - start;
9582
9583 /* Finished a line. Remove CRs before NL. */
9584 if (readlen > 0 && !binary)
9585 {
9586 while (len > 0 && start[len - 1] == '\r')
9587 --len;
9588 /* removal may cross back to the "prev" string */
9589 if (len == 0)
9590 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9591 --prevlen;
9592 }
9593 if (prevlen == 0)
9594 s = vim_strnsave(start, (int)len);
9595 else
9596 {
9597 /* Change "prev" buffer to be the right size. This way
9598 * the bytes are only copied once, and very long lines are
9599 * allocated only once. */
9600 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9601 {
9602 mch_memmove(s + prevlen, start, len);
9603 s[prevlen + len] = NUL;
9604 prev = NULL; /* the list will own the string */
9605 prevlen = prevsize = 0;
9606 }
9607 }
9608 if (s == NULL)
9609 {
9610 do_outofmem_msg((long_u) prevlen + len + 1);
9611 failed = TRUE;
9612 break;
9613 }
9614
9615 if ((li = listitem_alloc()) == NULL)
9616 {
9617 vim_free(s);
9618 failed = TRUE;
9619 break;
9620 }
9621 li->li_tv.v_type = VAR_STRING;
9622 li->li_tv.v_lock = 0;
9623 li->li_tv.vval.v_string = s;
9624 list_append(rettv->vval.v_list, li);
9625
9626 start = p + 1; /* step over newline */
9627 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9628 break;
9629 }
9630 else if (*p == NUL)
9631 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009632 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9633 * when finding the BF and check the previous two bytes. */
9634 else if (*p == 0xbf && enc_utf8 && !binary)
9635 {
9636 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9637 * + 1, these may be in the "prev" string. */
9638 char_u back1 = p >= buf + 1 ? p[-1]
9639 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9640 char_u back2 = p >= buf + 2 ? p[-2]
9641 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9642 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9643
9644 if (back2 == 0xef && back1 == 0xbb)
9645 {
9646 char_u *dest = p - 2;
9647
9648 /* Usually a BOM is at the beginning of a file, and so at
9649 * the beginning of a line; then we can just step over it.
9650 */
9651 if (start == dest)
9652 start = p + 1;
9653 else
9654 {
9655 /* have to shuffle buf to close gap */
9656 int adjust_prevlen = 0;
9657
9658 if (dest < buf)
9659 {
9660 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9661 dest = buf;
9662 }
9663 if (readlen > p - buf + 1)
9664 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9665 readlen -= 3 - adjust_prevlen;
9666 prevlen -= adjust_prevlen;
9667 p = dest - 1;
9668 }
9669 }
9670 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671 } /* for */
9672
9673 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9674 break;
9675 if (start < p)
9676 {
9677 /* There's part of a line in buf, store it in "prev". */
9678 if (p - start + prevlen >= prevsize)
9679 {
9680 /* need bigger "prev" buffer */
9681 char_u *newprev;
9682
9683 /* A common use case is ordinary text files and "prev" gets a
9684 * fragment of a line, so the first allocation is made
9685 * small, to avoid repeatedly 'allocing' large and
9686 * 'reallocing' small. */
9687 if (prevsize == 0)
9688 prevsize = (long)(p - start);
9689 else
9690 {
9691 long grow50pc = (prevsize * 3) / 2;
9692 long growmin = (long)((p - start) * 2 + prevlen);
9693 prevsize = grow50pc > growmin ? grow50pc : growmin;
9694 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009695 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009696 if (newprev == NULL)
9697 {
9698 do_outofmem_msg((long_u)prevsize);
9699 failed = TRUE;
9700 break;
9701 }
9702 prev = newprev;
9703 }
9704 /* Add the line part to end of "prev". */
9705 mch_memmove(prev + prevlen, start, p - start);
9706 prevlen += (long)(p - start);
9707 }
9708 } /* while */
9709
9710 /*
9711 * For a negative line count use only the lines at the end of the file,
9712 * free the rest.
9713 */
9714 if (!failed && maxline < 0)
9715 while (cnt > -maxline)
9716 {
9717 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9718 --cnt;
9719 }
9720
9721 if (failed)
9722 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009723 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009725 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009726 }
9727
9728 vim_free(prev);
9729 fclose(fd);
9730}
9731
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009732 static void
9733return_register(int regname, typval_T *rettv)
9734{
9735 char_u buf[2] = {0, 0};
9736
9737 buf[0] = (char_u)regname;
9738 rettv->v_type = VAR_STRING;
9739 rettv->vval.v_string = vim_strsave(buf);
9740}
9741
9742/*
9743 * "reg_executing()" function
9744 */
9745 static void
9746f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9747{
9748 return_register(reg_executing, rettv);
9749}
9750
9751/*
9752 * "reg_recording()" function
9753 */
9754 static void
9755f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9756{
9757 return_register(reg_recording, rettv);
9758}
9759
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009760#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009761/*
9762 * Convert a List to proftime_T.
9763 * Return FAIL when there is something wrong.
9764 */
9765 static int
9766list2proftime(typval_T *arg, proftime_T *tm)
9767{
9768 long n1, n2;
9769 int error = FALSE;
9770
9771 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9772 || arg->vval.v_list->lv_len != 2)
9773 return FAIL;
9774 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9775 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009776# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009777 tm->HighPart = n1;
9778 tm->LowPart = n2;
9779# else
9780 tm->tv_sec = n1;
9781 tm->tv_usec = n2;
9782# endif
9783 return error ? FAIL : OK;
9784}
9785#endif /* FEAT_RELTIME */
9786
9787/*
9788 * "reltime()" function
9789 */
9790 static void
9791f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9792{
9793#ifdef FEAT_RELTIME
9794 proftime_T res;
9795 proftime_T start;
9796
9797 if (argvars[0].v_type == VAR_UNKNOWN)
9798 {
9799 /* No arguments: get current time. */
9800 profile_start(&res);
9801 }
9802 else if (argvars[1].v_type == VAR_UNKNOWN)
9803 {
9804 if (list2proftime(&argvars[0], &res) == FAIL)
9805 return;
9806 profile_end(&res);
9807 }
9808 else
9809 {
9810 /* Two arguments: compute the difference. */
9811 if (list2proftime(&argvars[0], &start) == FAIL
9812 || list2proftime(&argvars[1], &res) == FAIL)
9813 return;
9814 profile_sub(&res, &start);
9815 }
9816
9817 if (rettv_list_alloc(rettv) == OK)
9818 {
9819 long n1, n2;
9820
Bram Moolenaar4f974752019-02-17 17:44:42 +01009821# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009822 n1 = res.HighPart;
9823 n2 = res.LowPart;
9824# else
9825 n1 = res.tv_sec;
9826 n2 = res.tv_usec;
9827# endif
9828 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9829 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9830 }
9831#endif
9832}
9833
9834#ifdef FEAT_FLOAT
9835/*
9836 * "reltimefloat()" function
9837 */
9838 static void
9839f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9840{
9841# ifdef FEAT_RELTIME
9842 proftime_T tm;
9843# endif
9844
9845 rettv->v_type = VAR_FLOAT;
9846 rettv->vval.v_float = 0;
9847# ifdef FEAT_RELTIME
9848 if (list2proftime(&argvars[0], &tm) == OK)
9849 rettv->vval.v_float = profile_float(&tm);
9850# endif
9851}
9852#endif
9853
9854/*
9855 * "reltimestr()" function
9856 */
9857 static void
9858f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9859{
9860#ifdef FEAT_RELTIME
9861 proftime_T tm;
9862#endif
9863
9864 rettv->v_type = VAR_STRING;
9865 rettv->vval.v_string = NULL;
9866#ifdef FEAT_RELTIME
9867 if (list2proftime(&argvars[0], &tm) == OK)
9868 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9869#endif
9870}
9871
9872#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009873 static void
9874make_connection(void)
9875{
9876 if (X_DISPLAY == NULL
9877# ifdef FEAT_GUI
9878 && !gui.in_use
9879# endif
9880 )
9881 {
9882 x_force_connect = TRUE;
9883 setup_term_clip();
9884 x_force_connect = FALSE;
9885 }
9886}
9887
9888 static int
9889check_connection(void)
9890{
9891 make_connection();
9892 if (X_DISPLAY == NULL)
9893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009894 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009895 return FAIL;
9896 }
9897 return OK;
9898}
9899#endif
9900
9901#ifdef FEAT_CLIENTSERVER
9902 static void
9903remote_common(typval_T *argvars, typval_T *rettv, int expr)
9904{
9905 char_u *server_name;
9906 char_u *keys;
9907 char_u *r = NULL;
9908 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009909 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009910# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009911 HWND w;
9912# else
9913 Window w;
9914# endif
9915
9916 if (check_restricted() || check_secure())
9917 return;
9918
9919# ifdef FEAT_X11
9920 if (check_connection() == FAIL)
9921 return;
9922# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009923 if (argvars[2].v_type != VAR_UNKNOWN
9924 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009925 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009927 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928 if (server_name == NULL)
9929 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009930 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009931# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009932 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009934 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9935 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009936# endif
9937 {
9938 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009939 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009940 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009941 vim_free(r);
9942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009944 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009945 return;
9946 }
9947
9948 rettv->vval.v_string = r;
9949
9950 if (argvars[2].v_type != VAR_UNKNOWN)
9951 {
9952 dictitem_T v;
9953 char_u str[30];
9954 char_u *idvar;
9955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009956 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009957 if (idvar != NULL && *idvar != NUL)
9958 {
9959 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9960 v.di_tv.v_type = VAR_STRING;
9961 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009963 vim_free(v.di_tv.vval.v_string);
9964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009965 }
9966}
9967#endif
9968
9969/*
9970 * "remote_expr()" function
9971 */
9972 static void
9973f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9974{
9975 rettv->v_type = VAR_STRING;
9976 rettv->vval.v_string = NULL;
9977#ifdef FEAT_CLIENTSERVER
9978 remote_common(argvars, rettv, TRUE);
9979#endif
9980}
9981
9982/*
9983 * "remote_foreground()" function
9984 */
9985 static void
9986f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9987{
9988#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009989# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009990 /* On Win32 it's done in this application. */
9991 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009992 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009993
9994 if (server_name != NULL)
9995 serverForeground(server_name);
9996 }
9997# else
9998 /* Send a foreground() expression to the server. */
9999 argvars[1].v_type = VAR_STRING;
10000 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10001 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +020010002 rettv->v_type = VAR_STRING;
10003 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010004 remote_common(argvars, rettv, TRUE);
10005 vim_free(argvars[1].vval.v_string);
10006# endif
10007#endif
10008}
10009
10010 static void
10011f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10012{
10013#ifdef FEAT_CLIENTSERVER
10014 dictitem_T v;
10015 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010016# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 long_u n = 0;
10018# endif
10019 char_u *serverid;
10020
10021 if (check_restricted() || check_secure())
10022 {
10023 rettv->vval.v_number = -1;
10024 return;
10025 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010026 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 if (serverid == NULL)
10028 {
10029 rettv->vval.v_number = -1;
10030 return; /* type error; errmsg already given */
10031 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010032# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010033 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10034 if (n == 0)
10035 rettv->vval.v_number = -1;
10036 else
10037 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010038 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010039 rettv->vval.v_number = (s != NULL);
10040 }
10041# else
10042 if (check_connection() == FAIL)
10043 return;
10044
10045 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10046 serverStrToWin(serverid), &s);
10047# endif
10048
10049 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10050 {
10051 char_u *retvar;
10052
10053 v.di_tv.v_type = VAR_STRING;
10054 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010055 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010056 if (retvar != NULL)
10057 set_var(retvar, &v.di_tv, FALSE);
10058 vim_free(v.di_tv.vval.v_string);
10059 }
10060#else
10061 rettv->vval.v_number = -1;
10062#endif
10063}
10064
10065 static void
10066f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10067{
10068 char_u *r = NULL;
10069
10070#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010071 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010072
10073 if (serverid != NULL && !check_restricted() && !check_secure())
10074 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010075 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010076# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010077 /* The server's HWND is encoded in the 'id' parameter */
10078 long_u n = 0;
10079# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010080
10081 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010082 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010083
Bram Moolenaar4f974752019-02-17 17:44:42 +010010084# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10086 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010087 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 if (r == NULL)
10089# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010090 if (check_connection() == FAIL
10091 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10092 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010093# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010094 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010095 }
10096#endif
10097 rettv->v_type = VAR_STRING;
10098 rettv->vval.v_string = r;
10099}
10100
10101/*
10102 * "remote_send()" function
10103 */
10104 static void
10105f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10106{
10107 rettv->v_type = VAR_STRING;
10108 rettv->vval.v_string = NULL;
10109#ifdef FEAT_CLIENTSERVER
10110 remote_common(argvars, rettv, FALSE);
10111#endif
10112}
10113
10114/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010115 * "remote_startserver()" function
10116 */
10117 static void
10118f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10119{
10120#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010121 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010122
10123 if (server == NULL)
10124 return; /* type error; errmsg already given */
10125 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010126 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010127 else
10128 {
10129# ifdef FEAT_X11
10130 if (check_connection() == OK)
10131 serverRegisterName(X_DISPLAY, server);
10132# else
10133 serverSetName(server);
10134# endif
10135 }
10136#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010137 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010138#endif
10139}
10140
10141/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010142 * "remove()" function
10143 */
10144 static void
10145f_remove(typval_T *argvars, typval_T *rettv)
10146{
10147 list_T *l;
10148 listitem_T *item, *item2;
10149 listitem_T *li;
10150 long idx;
10151 long end;
10152 char_u *key;
10153 dict_T *d;
10154 dictitem_T *di;
10155 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010156 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157
10158 if (argvars[0].v_type == VAR_DICT)
10159 {
10160 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010161 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010163 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010165 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 if (key != NULL)
10167 {
10168 di = dict_find(d, key, -1);
10169 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010170 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10172 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10173 {
10174 *rettv = di->di_tv;
10175 init_tv(&di->di_tv);
10176 dictitem_remove(d, di);
10177 }
10178 }
10179 }
10180 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010181 else if (argvars[0].v_type == VAR_BLOB)
10182 {
10183 idx = (long)tv_get_number_chk(&argvars[1], &error);
10184 if (!error)
10185 {
10186 blob_T *b = argvars[0].vval.v_blob;
10187 int len = blob_len(b);
10188 char_u *p;
10189
10190 if (idx < 0)
10191 // count from the end
10192 idx = len + idx;
10193 if (idx < 0 || idx >= len)
10194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010195 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010196 return;
10197 }
10198 if (argvars[2].v_type == VAR_UNKNOWN)
10199 {
10200 // Remove one item, return its value.
10201 p = (char_u *)b->bv_ga.ga_data;
10202 rettv->vval.v_number = (varnumber_T) *(p + idx);
10203 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10204 --b->bv_ga.ga_len;
10205 }
10206 else
10207 {
10208 blob_T *blob;
10209
10210 // Remove range of items, return list with values.
10211 end = (long)tv_get_number_chk(&argvars[2], &error);
10212 if (error)
10213 return;
10214 if (end < 0)
10215 // count from the end
10216 end = len + end;
10217 if (end >= len || idx > end)
10218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010219 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010220 return;
10221 }
10222 blob = blob_alloc();
10223 if (blob == NULL)
10224 return;
10225 blob->bv_ga.ga_len = end - idx + 1;
10226 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10227 {
10228 vim_free(blob);
10229 return;
10230 }
10231 p = (char_u *)b->bv_ga.ga_data;
10232 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10233 (size_t)(end - idx + 1));
10234 ++blob->bv_refcount;
10235 rettv->v_type = VAR_BLOB;
10236 rettv->vval.v_blob = blob;
10237
10238 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10239 b->bv_ga.ga_len -= end - idx + 1;
10240 }
10241 }
10242 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010244 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010245 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010246 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010248 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010250 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010251 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010252 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 else
10254 {
10255 if (argvars[2].v_type == VAR_UNKNOWN)
10256 {
10257 /* Remove one item, return its value. */
10258 vimlist_remove(l, item, item);
10259 *rettv = item->li_tv;
10260 vim_free(item);
10261 }
10262 else
10263 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010264 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010265 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010267 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010269 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 else
10271 {
10272 int cnt = 0;
10273
10274 for (li = item; li != NULL; li = li->li_next)
10275 {
10276 ++cnt;
10277 if (li == item2)
10278 break;
10279 }
10280 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010281 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010282 else
10283 {
10284 vimlist_remove(l, item, item2);
10285 if (rettv_list_alloc(rettv) == OK)
10286 {
10287 l = rettv->vval.v_list;
10288 l->lv_first = item;
10289 l->lv_last = item2;
10290 item->li_prev = NULL;
10291 item2->li_next = NULL;
10292 l->lv_len = cnt;
10293 }
10294 }
10295 }
10296 }
10297 }
10298 }
10299}
10300
10301/*
10302 * "rename({from}, {to})" function
10303 */
10304 static void
10305f_rename(typval_T *argvars, typval_T *rettv)
10306{
10307 char_u buf[NUMBUFLEN];
10308
10309 if (check_restricted() || check_secure())
10310 rettv->vval.v_number = -1;
10311 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010312 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10313 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010314}
10315
10316/*
10317 * "repeat()" function
10318 */
10319 static void
10320f_repeat(typval_T *argvars, typval_T *rettv)
10321{
10322 char_u *p;
10323 int n;
10324 int slen;
10325 int len;
10326 char_u *r;
10327 int i;
10328
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010329 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010330 if (argvars[0].v_type == VAR_LIST)
10331 {
10332 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10333 while (n-- > 0)
10334 if (list_extend(rettv->vval.v_list,
10335 argvars[0].vval.v_list, NULL) == FAIL)
10336 break;
10337 }
10338 else
10339 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010340 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010341 rettv->v_type = VAR_STRING;
10342 rettv->vval.v_string = NULL;
10343
10344 slen = (int)STRLEN(p);
10345 len = slen * n;
10346 if (len <= 0)
10347 return;
10348
10349 r = alloc(len + 1);
10350 if (r != NULL)
10351 {
10352 for (i = 0; i < n; i++)
10353 mch_memmove(r + i * slen, p, (size_t)slen);
10354 r[len] = NUL;
10355 }
10356
10357 rettv->vval.v_string = r;
10358 }
10359}
10360
10361/*
10362 * "resolve()" function
10363 */
10364 static void
10365f_resolve(typval_T *argvars, typval_T *rettv)
10366{
10367 char_u *p;
10368#ifdef HAVE_READLINK
10369 char_u *buf = NULL;
10370#endif
10371
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010372 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010373#ifdef FEAT_SHORTCUT
10374 {
10375 char_u *v = NULL;
10376
Bram Moolenaardce1e892019-02-10 23:18:53 +010010377 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010378 if (v != NULL)
10379 rettv->vval.v_string = v;
10380 else
10381 rettv->vval.v_string = vim_strsave(p);
10382 }
10383#else
10384# ifdef HAVE_READLINK
10385 {
10386 char_u *cpy;
10387 int len;
10388 char_u *remain = NULL;
10389 char_u *q;
10390 int is_relative_to_current = FALSE;
10391 int has_trailing_pathsep = FALSE;
10392 int limit = 100;
10393
10394 p = vim_strsave(p);
10395
10396 if (p[0] == '.' && (vim_ispathsep(p[1])
10397 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10398 is_relative_to_current = TRUE;
10399
10400 len = STRLEN(p);
10401 if (len > 0 && after_pathsep(p, p + len))
10402 {
10403 has_trailing_pathsep = TRUE;
10404 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10405 }
10406
10407 q = getnextcomp(p);
10408 if (*q != NUL)
10409 {
10410 /* Separate the first path component in "p", and keep the
10411 * remainder (beginning with the path separator). */
10412 remain = vim_strsave(q - 1);
10413 q[-1] = NUL;
10414 }
10415
10416 buf = alloc(MAXPATHL + 1);
10417 if (buf == NULL)
10418 goto fail;
10419
10420 for (;;)
10421 {
10422 for (;;)
10423 {
10424 len = readlink((char *)p, (char *)buf, MAXPATHL);
10425 if (len <= 0)
10426 break;
10427 buf[len] = NUL;
10428
10429 if (limit-- == 0)
10430 {
10431 vim_free(p);
10432 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010433 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010434 rettv->vval.v_string = NULL;
10435 goto fail;
10436 }
10437
10438 /* Ensure that the result will have a trailing path separator
10439 * if the argument has one. */
10440 if (remain == NULL && has_trailing_pathsep)
10441 add_pathsep(buf);
10442
10443 /* Separate the first path component in the link value and
10444 * concatenate the remainders. */
10445 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10446 if (*q != NUL)
10447 {
10448 if (remain == NULL)
10449 remain = vim_strsave(q - 1);
10450 else
10451 {
10452 cpy = concat_str(q - 1, remain);
10453 if (cpy != NULL)
10454 {
10455 vim_free(remain);
10456 remain = cpy;
10457 }
10458 }
10459 q[-1] = NUL;
10460 }
10461
10462 q = gettail(p);
10463 if (q > p && *q == NUL)
10464 {
10465 /* Ignore trailing path separator. */
10466 q[-1] = NUL;
10467 q = gettail(p);
10468 }
10469 if (q > p && !mch_isFullName(buf))
10470 {
10471 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010472 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010473 if (cpy != NULL)
10474 {
10475 STRCPY(cpy, p);
10476 STRCPY(gettail(cpy), buf);
10477 vim_free(p);
10478 p = cpy;
10479 }
10480 }
10481 else
10482 {
10483 vim_free(p);
10484 p = vim_strsave(buf);
10485 }
10486 }
10487
10488 if (remain == NULL)
10489 break;
10490
10491 /* Append the first path component of "remain" to "p". */
10492 q = getnextcomp(remain + 1);
10493 len = q - remain - (*q != NUL);
10494 cpy = vim_strnsave(p, STRLEN(p) + len);
10495 if (cpy != NULL)
10496 {
10497 STRNCAT(cpy, remain, len);
10498 vim_free(p);
10499 p = cpy;
10500 }
10501 /* Shorten "remain". */
10502 if (*q != NUL)
10503 STRMOVE(remain, q - 1);
10504 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010505 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010506 }
10507
10508 /* If the result is a relative path name, make it explicitly relative to
10509 * the current directory if and only if the argument had this form. */
10510 if (!vim_ispathsep(*p))
10511 {
10512 if (is_relative_to_current
10513 && *p != NUL
10514 && !(p[0] == '.'
10515 && (p[1] == NUL
10516 || vim_ispathsep(p[1])
10517 || (p[1] == '.'
10518 && (p[2] == NUL
10519 || vim_ispathsep(p[2]))))))
10520 {
10521 /* Prepend "./". */
10522 cpy = concat_str((char_u *)"./", p);
10523 if (cpy != NULL)
10524 {
10525 vim_free(p);
10526 p = cpy;
10527 }
10528 }
10529 else if (!is_relative_to_current)
10530 {
10531 /* Strip leading "./". */
10532 q = p;
10533 while (q[0] == '.' && vim_ispathsep(q[1]))
10534 q += 2;
10535 if (q > p)
10536 STRMOVE(p, p + 2);
10537 }
10538 }
10539
10540 /* Ensure that the result will have no trailing path separator
10541 * if the argument had none. But keep "/" or "//". */
10542 if (!has_trailing_pathsep)
10543 {
10544 q = p + STRLEN(p);
10545 if (after_pathsep(p, q))
10546 *gettail_sep(p) = NUL;
10547 }
10548
10549 rettv->vval.v_string = p;
10550 }
10551# else
10552 rettv->vval.v_string = vim_strsave(p);
10553# endif
10554#endif
10555
10556 simplify_filename(rettv->vval.v_string);
10557
10558#ifdef HAVE_READLINK
10559fail:
10560 vim_free(buf);
10561#endif
10562 rettv->v_type = VAR_STRING;
10563}
10564
10565/*
10566 * "reverse({list})" function
10567 */
10568 static void
10569f_reverse(typval_T *argvars, typval_T *rettv)
10570{
10571 list_T *l;
10572 listitem_T *li, *ni;
10573
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010574 if (argvars[0].v_type == VAR_BLOB)
10575 {
10576 blob_T *b = argvars[0].vval.v_blob;
10577 int i, len = blob_len(b);
10578
10579 for (i = 0; i < len / 2; i++)
10580 {
10581 int tmp = blob_get(b, i);
10582
10583 blob_set(b, i, blob_get(b, len - i - 1));
10584 blob_set(b, len - i - 1, tmp);
10585 }
10586 rettv_blob_set(rettv, b);
10587 return;
10588 }
10589
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010591 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010593 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 (char_u *)N_("reverse() argument"), TRUE))
10595 {
10596 li = l->lv_last;
10597 l->lv_first = l->lv_last = NULL;
10598 l->lv_len = 0;
10599 while (li != NULL)
10600 {
10601 ni = li->li_prev;
10602 list_append(l, li);
10603 li = ni;
10604 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010605 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010606 l->lv_idx = l->lv_len - l->lv_idx - 1;
10607 }
10608}
10609
10610#define SP_NOMOVE 0x01 /* don't move cursor */
10611#define SP_REPEAT 0x02 /* repeat to find outer pair */
10612#define SP_RETCOUNT 0x04 /* return matchcount */
10613#define SP_SETPCMARK 0x08 /* set previous context mark */
10614#define SP_START 0x10 /* accept match at start position */
10615#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10616#define SP_END 0x40 /* leave cursor at end of match */
10617#define SP_COLUMN 0x80 /* start at cursor column */
10618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010619/*
10620 * Get flags for a search function.
10621 * Possibly sets "p_ws".
10622 * Returns BACKWARD, FORWARD or zero (for an error).
10623 */
10624 static int
10625get_search_arg(typval_T *varp, int *flagsp)
10626{
10627 int dir = FORWARD;
10628 char_u *flags;
10629 char_u nbuf[NUMBUFLEN];
10630 int mask;
10631
10632 if (varp->v_type != VAR_UNKNOWN)
10633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010634 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010635 if (flags == NULL)
10636 return 0; /* type error; errmsg already given */
10637 while (*flags != NUL)
10638 {
10639 switch (*flags)
10640 {
10641 case 'b': dir = BACKWARD; break;
10642 case 'w': p_ws = TRUE; break;
10643 case 'W': p_ws = FALSE; break;
10644 default: mask = 0;
10645 if (flagsp != NULL)
10646 switch (*flags)
10647 {
10648 case 'c': mask = SP_START; break;
10649 case 'e': mask = SP_END; break;
10650 case 'm': mask = SP_RETCOUNT; break;
10651 case 'n': mask = SP_NOMOVE; break;
10652 case 'p': mask = SP_SUBPAT; break;
10653 case 'r': mask = SP_REPEAT; break;
10654 case 's': mask = SP_SETPCMARK; break;
10655 case 'z': mask = SP_COLUMN; break;
10656 }
10657 if (mask == 0)
10658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010659 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 dir = 0;
10661 }
10662 else
10663 *flagsp |= mask;
10664 }
10665 if (dir == 0)
10666 break;
10667 ++flags;
10668 }
10669 }
10670 return dir;
10671}
10672
10673/*
10674 * Shared by search() and searchpos() functions.
10675 */
10676 static int
10677search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10678{
10679 int flags;
10680 char_u *pat;
10681 pos_T pos;
10682 pos_T save_cursor;
10683 int save_p_ws = p_ws;
10684 int dir;
10685 int retval = 0; /* default: FAIL */
10686 long lnum_stop = 0;
10687 proftime_T tm;
10688#ifdef FEAT_RELTIME
10689 long time_limit = 0;
10690#endif
10691 int options = SEARCH_KEEP;
10692 int subpatnum;
10693
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010694 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10696 if (dir == 0)
10697 goto theend;
10698 flags = *flagsp;
10699 if (flags & SP_START)
10700 options |= SEARCH_START;
10701 if (flags & SP_END)
10702 options |= SEARCH_END;
10703 if (flags & SP_COLUMN)
10704 options |= SEARCH_COL;
10705
10706 /* Optional arguments: line number to stop searching and timeout. */
10707 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010709 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010710 if (lnum_stop < 0)
10711 goto theend;
10712#ifdef FEAT_RELTIME
10713 if (argvars[3].v_type != VAR_UNKNOWN)
10714 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010715 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010716 if (time_limit < 0)
10717 goto theend;
10718 }
10719#endif
10720 }
10721
10722#ifdef FEAT_RELTIME
10723 /* Set the time limit, if there is one. */
10724 profile_setlimit(time_limit, &tm);
10725#endif
10726
10727 /*
10728 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10729 * Check to make sure only those flags are set.
10730 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10731 * flags cannot be set. Check for that condition also.
10732 */
10733 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10734 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010736 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010737 goto theend;
10738 }
10739
10740 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010741 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010742 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010743 if (subpatnum != FAIL)
10744 {
10745 if (flags & SP_SUBPAT)
10746 retval = subpatnum;
10747 else
10748 retval = pos.lnum;
10749 if (flags & SP_SETPCMARK)
10750 setpcmark();
10751 curwin->w_cursor = pos;
10752 if (match_pos != NULL)
10753 {
10754 /* Store the match cursor position */
10755 match_pos->lnum = pos.lnum;
10756 match_pos->col = pos.col + 1;
10757 }
10758 /* "/$" will put the cursor after the end of the line, may need to
10759 * correct that here */
10760 check_cursor();
10761 }
10762
10763 /* If 'n' flag is used: restore cursor position. */
10764 if (flags & SP_NOMOVE)
10765 curwin->w_cursor = save_cursor;
10766 else
10767 curwin->w_set_curswant = TRUE;
10768theend:
10769 p_ws = save_p_ws;
10770
10771 return retval;
10772}
10773
10774#ifdef FEAT_FLOAT
10775
10776/*
10777 * round() is not in C90, use ceil() or floor() instead.
10778 */
10779 float_T
10780vim_round(float_T f)
10781{
10782 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10783}
10784
10785/*
10786 * "round({float})" function
10787 */
10788 static void
10789f_round(typval_T *argvars, typval_T *rettv)
10790{
10791 float_T f = 0.0;
10792
10793 rettv->v_type = VAR_FLOAT;
10794 if (get_float_arg(argvars, &f) == OK)
10795 rettv->vval.v_float = vim_round(f);
10796 else
10797 rettv->vval.v_float = 0.0;
10798}
10799#endif
10800
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010801#ifdef FEAT_RUBY
10802/*
10803 * "rubyeval()" function
10804 */
10805 static void
10806f_rubyeval(typval_T *argvars, typval_T *rettv)
10807{
10808 char_u *str;
10809 char_u buf[NUMBUFLEN];
10810
10811 str = tv_get_string_buf(&argvars[0], buf);
10812 do_rubyeval(str, rettv);
10813}
10814#endif
10815
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816/*
10817 * "screenattr()" function
10818 */
10819 static void
10820f_screenattr(typval_T *argvars, typval_T *rettv)
10821{
10822 int row;
10823 int col;
10824 int c;
10825
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010826 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10827 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010828 if (row < 0 || row >= screen_Rows
10829 || col < 0 || col >= screen_Columns)
10830 c = -1;
10831 else
10832 c = ScreenAttrs[LineOffset[row] + col];
10833 rettv->vval.v_number = c;
10834}
10835
10836/*
10837 * "screenchar()" function
10838 */
10839 static void
10840f_screenchar(typval_T *argvars, typval_T *rettv)
10841{
10842 int row;
10843 int col;
10844 int off;
10845 int c;
10846
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010847 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10848 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010849 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010850 c = -1;
10851 else
10852 {
10853 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 if (enc_utf8 && ScreenLinesUC[off] != 0)
10855 c = ScreenLinesUC[off];
10856 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010857 c = ScreenLines[off];
10858 }
10859 rettv->vval.v_number = c;
10860}
10861
10862/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010863 * "screenchars()" function
10864 */
10865 static void
10866f_screenchars(typval_T *argvars, typval_T *rettv)
10867{
10868 int row;
10869 int col;
10870 int off;
10871 int c;
10872 int i;
10873
10874 if (rettv_list_alloc(rettv) == FAIL)
10875 return;
10876 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10877 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10878 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10879 return;
10880
10881 off = LineOffset[row] + col;
10882 if (enc_utf8 && ScreenLinesUC[off] != 0)
10883 c = ScreenLinesUC[off];
10884 else
10885 c = ScreenLines[off];
10886 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10887
10888 if (enc_utf8)
10889
10890 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10891 list_append_number(rettv->vval.v_list,
10892 (varnumber_T)ScreenLinesC[i][off]);
10893}
10894
10895/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010896 * "screencol()" function
10897 *
10898 * First column is 1 to be consistent with virtcol().
10899 */
10900 static void
10901f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10902{
10903 rettv->vval.v_number = screen_screencol() + 1;
10904}
10905
10906/*
10907 * "screenrow()" function
10908 */
10909 static void
10910f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10911{
10912 rettv->vval.v_number = screen_screenrow() + 1;
10913}
10914
10915/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010916 * "screenstring()" function
10917 */
10918 static void
10919f_screenstring(typval_T *argvars, typval_T *rettv)
10920{
10921 int row;
10922 int col;
10923 int off;
10924 int c;
10925 int i;
10926 char_u buf[MB_MAXBYTES + 1];
10927 int buflen = 0;
10928
10929 rettv->vval.v_string = NULL;
10930 rettv->v_type = VAR_STRING;
10931
10932 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10933 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10934 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10935 return;
10936
10937 off = LineOffset[row] + col;
10938 if (enc_utf8 && ScreenLinesUC[off] != 0)
10939 c = ScreenLinesUC[off];
10940 else
10941 c = ScreenLines[off];
10942 buflen += mb_char2bytes(c, buf);
10943
10944 if (enc_utf8)
10945 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10946 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10947
10948 buf[buflen] = NUL;
10949 rettv->vval.v_string = vim_strsave(buf);
10950}
10951
10952/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 * "search()" function
10954 */
10955 static void
10956f_search(typval_T *argvars, typval_T *rettv)
10957{
10958 int flags = 0;
10959
10960 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10961}
10962
10963/*
10964 * "searchdecl()" function
10965 */
10966 static void
10967f_searchdecl(typval_T *argvars, typval_T *rettv)
10968{
10969 int locally = 1;
10970 int thisblock = 0;
10971 int error = FALSE;
10972 char_u *name;
10973
10974 rettv->vval.v_number = 1; /* default: FAIL */
10975
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 if (argvars[1].v_type != VAR_UNKNOWN)
10978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010981 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010982 }
10983 if (!error && name != NULL)
10984 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10985 locally, thisblock, SEARCH_KEEP) == FAIL;
10986}
10987
10988/*
10989 * Used by searchpair() and searchpairpos()
10990 */
10991 static int
10992searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10993{
10994 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010995 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 int save_p_ws = p_ws;
10997 int dir;
10998 int flags = 0;
10999 char_u nbuf1[NUMBUFLEN];
11000 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001 int retval = 0; /* default: FAIL */
11002 long lnum_stop = 0;
11003 long time_limit = 0;
11004
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011005 /* Get the three pattern arguments: start, middle, end. Will result in an
11006 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011007 spat = tv_get_string_chk(&argvars[0]);
11008 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11009 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011010 if (spat == NULL || mpat == NULL || epat == NULL)
11011 goto theend; /* type error */
11012
11013 /* Handle the optional fourth argument: flags */
11014 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11015 if (dir == 0)
11016 goto theend;
11017
11018 /* Don't accept SP_END or SP_SUBPAT.
11019 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11020 */
11021 if ((flags & (SP_END | SP_SUBPAT)) != 0
11022 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011024 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025 goto theend;
11026 }
11027
11028 /* Using 'r' implies 'W', otherwise it doesn't work. */
11029 if (flags & SP_REPEAT)
11030 p_ws = FALSE;
11031
11032 /* Optional fifth argument: skip expression */
11033 if (argvars[3].v_type == VAR_UNKNOWN
11034 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011035 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011036 else
11037 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011038 skip = &argvars[4];
11039 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11040 && skip->v_type != VAR_STRING)
11041 {
11042 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011043 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011044 goto theend;
11045 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 if (argvars[5].v_type != VAR_UNKNOWN)
11047 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011048 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011051 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011053 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011054#ifdef FEAT_RELTIME
11055 if (argvars[6].v_type != VAR_UNKNOWN)
11056 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011057 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011060 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011062 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011063 }
11064#endif
11065 }
11066 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011067
11068 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11069 match_pos, lnum_stop, time_limit);
11070
11071theend:
11072 p_ws = save_p_ws;
11073
11074 return retval;
11075}
11076
11077/*
11078 * "searchpair()" function
11079 */
11080 static void
11081f_searchpair(typval_T *argvars, typval_T *rettv)
11082{
11083 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11084}
11085
11086/*
11087 * "searchpairpos()" function
11088 */
11089 static void
11090f_searchpairpos(typval_T *argvars, typval_T *rettv)
11091{
11092 pos_T match_pos;
11093 int lnum = 0;
11094 int col = 0;
11095
11096 if (rettv_list_alloc(rettv) == FAIL)
11097 return;
11098
11099 if (searchpair_cmn(argvars, &match_pos) > 0)
11100 {
11101 lnum = match_pos.lnum;
11102 col = match_pos.col;
11103 }
11104
11105 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11106 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11107}
11108
11109/*
11110 * Search for a start/middle/end thing.
11111 * Used by searchpair(), see its documentation for the details.
11112 * Returns 0 or -1 for no match,
11113 */
11114 long
11115do_searchpair(
11116 char_u *spat, /* start pattern */
11117 char_u *mpat, /* middle pattern */
11118 char_u *epat, /* end pattern */
11119 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011120 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 int flags, /* SP_SETPCMARK and other SP_ values */
11122 pos_T *match_pos,
11123 linenr_T lnum_stop, /* stop at this line if not zero */
11124 long time_limit UNUSED) /* stop after this many msec */
11125{
11126 char_u *save_cpo;
11127 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11128 long retval = 0;
11129 pos_T pos;
11130 pos_T firstpos;
11131 pos_T foundpos;
11132 pos_T save_cursor;
11133 pos_T save_pos;
11134 int n;
11135 int r;
11136 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011137 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011138 int err;
11139 int options = SEARCH_KEEP;
11140 proftime_T tm;
11141
11142 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11143 save_cpo = p_cpo;
11144 p_cpo = empty_option;
11145
11146#ifdef FEAT_RELTIME
11147 /* Set the time limit, if there is one. */
11148 profile_setlimit(time_limit, &tm);
11149#endif
11150
11151 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11152 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011153 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11154 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011155 if (pat2 == NULL || pat3 == NULL)
11156 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011157 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158 if (*mpat == NUL)
11159 STRCPY(pat3, pat2);
11160 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011161 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 spat, epat, mpat);
11163 if (flags & SP_START)
11164 options |= SEARCH_START;
11165
Bram Moolenaar48570482017-10-30 21:48:41 +010011166 if (skip != NULL)
11167 {
11168 /* Empty string means to not use the skip expression. */
11169 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11170 use_skip = skip->vval.v_string != NULL
11171 && *skip->vval.v_string != NUL;
11172 }
11173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011174 save_cursor = curwin->w_cursor;
11175 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011176 CLEAR_POS(&firstpos);
11177 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 pat = pat3;
11179 for (;;)
11180 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011181 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011182 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011183 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011184 /* didn't find it or found the first match again: FAIL */
11185 break;
11186
11187 if (firstpos.lnum == 0)
11188 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011189 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011190 {
11191 /* Found the same position again. Can happen with a pattern that
11192 * has "\zs" at the end and searching backwards. Advance one
11193 * character and try again. */
11194 if (dir == BACKWARD)
11195 decl(&pos);
11196 else
11197 incl(&pos);
11198 }
11199 foundpos = pos;
11200
11201 /* clear the start flag to avoid getting stuck here */
11202 options &= ~SEARCH_START;
11203
11204 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011205 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206 {
11207 save_pos = curwin->w_cursor;
11208 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011209 err = FALSE;
11210 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011211 curwin->w_cursor = save_pos;
11212 if (err)
11213 {
11214 /* Evaluating {skip} caused an error, break here. */
11215 curwin->w_cursor = save_cursor;
11216 retval = -1;
11217 break;
11218 }
11219 if (r)
11220 continue;
11221 }
11222
11223 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11224 {
11225 /* Found end when searching backwards or start when searching
11226 * forward: nested pair. */
11227 ++nest;
11228 pat = pat2; /* nested, don't search for middle */
11229 }
11230 else
11231 {
11232 /* Found end when searching forward or start when searching
11233 * backward: end of (nested) pair; or found middle in outer pair. */
11234 if (--nest == 1)
11235 pat = pat3; /* outer level, search for middle */
11236 }
11237
11238 if (nest == 0)
11239 {
11240 /* Found the match: return matchcount or line number. */
11241 if (flags & SP_RETCOUNT)
11242 ++retval;
11243 else
11244 retval = pos.lnum;
11245 if (flags & SP_SETPCMARK)
11246 setpcmark();
11247 curwin->w_cursor = pos;
11248 if (!(flags & SP_REPEAT))
11249 break;
11250 nest = 1; /* search for next unmatched */
11251 }
11252 }
11253
11254 if (match_pos != NULL)
11255 {
11256 /* Store the match cursor position */
11257 match_pos->lnum = curwin->w_cursor.lnum;
11258 match_pos->col = curwin->w_cursor.col + 1;
11259 }
11260
11261 /* If 'n' flag is used or search failed: restore cursor position. */
11262 if ((flags & SP_NOMOVE) || retval == 0)
11263 curwin->w_cursor = save_cursor;
11264
11265theend:
11266 vim_free(pat2);
11267 vim_free(pat3);
11268 if (p_cpo == empty_option)
11269 p_cpo = save_cpo;
11270 else
11271 /* Darn, evaluating the {skip} expression changed the value. */
11272 free_string_option(save_cpo);
11273
11274 return retval;
11275}
11276
11277/*
11278 * "searchpos()" function
11279 */
11280 static void
11281f_searchpos(typval_T *argvars, typval_T *rettv)
11282{
11283 pos_T match_pos;
11284 int lnum = 0;
11285 int col = 0;
11286 int n;
11287 int flags = 0;
11288
11289 if (rettv_list_alloc(rettv) == FAIL)
11290 return;
11291
11292 n = search_cmn(argvars, &match_pos, &flags);
11293 if (n > 0)
11294 {
11295 lnum = match_pos.lnum;
11296 col = match_pos.col;
11297 }
11298
11299 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11300 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11301 if (flags & SP_SUBPAT)
11302 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11303}
11304
11305 static void
11306f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11307{
11308#ifdef FEAT_CLIENTSERVER
11309 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011310 char_u *server = tv_get_string_chk(&argvars[0]);
11311 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011312
11313 rettv->vval.v_number = -1;
11314 if (server == NULL || reply == NULL)
11315 return;
11316 if (check_restricted() || check_secure())
11317 return;
11318# ifdef FEAT_X11
11319 if (check_connection() == FAIL)
11320 return;
11321# endif
11322
11323 if (serverSendReply(server, reply) < 0)
11324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011325 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011326 return;
11327 }
11328 rettv->vval.v_number = 0;
11329#else
11330 rettv->vval.v_number = -1;
11331#endif
11332}
11333
11334 static void
11335f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11336{
11337 char_u *r = NULL;
11338
11339#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011340# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 r = serverGetVimNames();
11342# else
11343 make_connection();
11344 if (X_DISPLAY != NULL)
11345 r = serverGetVimNames(X_DISPLAY);
11346# endif
11347#endif
11348 rettv->v_type = VAR_STRING;
11349 rettv->vval.v_string = r;
11350}
11351
11352/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011353 * "setbufline()" function
11354 */
11355 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011356f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011357{
11358 linenr_T lnum;
11359 buf_T *buf;
11360
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011361 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011362 if (buf == NULL)
11363 rettv->vval.v_number = 1; /* FAIL */
11364 else
11365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011366 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011367 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011368 }
11369}
11370
11371/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011372 * "setbufvar()" function
11373 */
11374 static void
11375f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11376{
11377 buf_T *buf;
11378 char_u *varname, *bufvarname;
11379 typval_T *varp;
11380 char_u nbuf[NUMBUFLEN];
11381
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011382 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011384 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11385 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011386 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011387 varp = &argvars[2];
11388
11389 if (buf != NULL && varname != NULL && varp != NULL)
11390 {
11391 if (*varname == '&')
11392 {
11393 long numval;
11394 char_u *strval;
11395 int error = FALSE;
11396 aco_save_T aco;
11397
11398 /* set curbuf to be our buf, temporarily */
11399 aucmd_prepbuf(&aco, buf);
11400
11401 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011402 numval = (long)tv_get_number_chk(varp, &error);
11403 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011404 if (!error && strval != NULL)
11405 set_option_value(varname, numval, strval, OPT_LOCAL);
11406
11407 /* reset notion of buffer */
11408 aucmd_restbuf(&aco);
11409 }
11410 else
11411 {
11412 buf_T *save_curbuf = curbuf;
11413
Bram Moolenaar964b3742019-05-24 18:54:09 +020011414 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011415 if (bufvarname != NULL)
11416 {
11417 curbuf = buf;
11418 STRCPY(bufvarname, "b:");
11419 STRCPY(bufvarname + 2, varname);
11420 set_var(bufvarname, varp, TRUE);
11421 vim_free(bufvarname);
11422 curbuf = save_curbuf;
11423 }
11424 }
11425 }
11426}
11427
11428 static void
11429f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11430{
11431 dict_T *d;
11432 dictitem_T *di;
11433 char_u *csearch;
11434
11435 if (argvars[0].v_type != VAR_DICT)
11436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011437 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 return;
11439 }
11440
11441 if ((d = argvars[0].vval.v_dict) != NULL)
11442 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011443 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 if (csearch != NULL)
11445 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 if (enc_utf8)
11447 {
11448 int pcc[MAX_MCO];
11449 int c = utfc_ptr2char(csearch, pcc);
11450
11451 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11452 }
11453 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011454 set_last_csearch(PTR2CHAR(csearch),
11455 csearch, MB_PTR2LEN(csearch));
11456 }
11457
11458 di = dict_find(d, (char_u *)"forward", -1);
11459 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011460 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011461 ? FORWARD : BACKWARD);
11462
11463 di = dict_find(d, (char_u *)"until", -1);
11464 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011465 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466 }
11467}
11468
11469/*
11470 * "setcmdpos()" function
11471 */
11472 static void
11473f_setcmdpos(typval_T *argvars, typval_T *rettv)
11474{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011475 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011476
11477 if (pos >= 0)
11478 rettv->vval.v_number = set_cmdline_pos(pos);
11479}
11480
11481/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011482 * "setenv()" function
11483 */
11484 static void
11485f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11486{
11487 char_u namebuf[NUMBUFLEN];
11488 char_u valbuf[NUMBUFLEN];
11489 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11490
11491 if (argvars[1].v_type == VAR_SPECIAL
11492 && argvars[1].vval.v_number == VVAL_NULL)
11493 vim_unsetenv(name);
11494 else
11495 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11496}
11497
11498/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499 * "setfperm({fname}, {mode})" function
11500 */
11501 static void
11502f_setfperm(typval_T *argvars, typval_T *rettv)
11503{
11504 char_u *fname;
11505 char_u modebuf[NUMBUFLEN];
11506 char_u *mode_str;
11507 int i;
11508 int mask;
11509 int mode = 0;
11510
11511 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011512 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513 if (fname == NULL)
11514 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011515 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 if (mode_str == NULL)
11517 return;
11518 if (STRLEN(mode_str) != 9)
11519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011520 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011521 return;
11522 }
11523
11524 mask = 1;
11525 for (i = 8; i >= 0; --i)
11526 {
11527 if (mode_str[i] != '-')
11528 mode |= mask;
11529 mask = mask << 1;
11530 }
11531 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11532}
11533
11534/*
11535 * "setline()" function
11536 */
11537 static void
11538f_setline(typval_T *argvars, typval_T *rettv)
11539{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011540 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541
Bram Moolenaarca851592018-06-06 21:04:07 +020011542 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543}
11544
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011545/*
11546 * Used by "setqflist()" and "setloclist()" functions
11547 */
11548 static void
11549set_qf_ll_list(
11550 win_T *wp UNUSED,
11551 typval_T *list_arg UNUSED,
11552 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011553 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011554 typval_T *rettv)
11555{
11556#ifdef FEAT_QUICKFIX
11557 static char *e_invact = N_("E927: Invalid action: '%s'");
11558 char_u *act;
11559 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011560 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011561#endif
11562
11563 rettv->vval.v_number = -1;
11564
11565#ifdef FEAT_QUICKFIX
11566 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011567 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011568 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011569 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 else
11571 {
11572 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011573 dict_T *d = NULL;
11574 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011575
11576 if (action_arg->v_type == VAR_STRING)
11577 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011578 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579 if (act == NULL)
11580 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011581 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11582 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 action = *act;
11584 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011585 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586 }
11587 else if (action_arg->v_type == VAR_UNKNOWN)
11588 action = ' ';
11589 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011590 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011591
Bram Moolenaard823fa92016-08-12 16:29:27 +020011592 if (action_arg->v_type != VAR_UNKNOWN
11593 && what_arg->v_type != VAR_UNKNOWN)
11594 {
11595 if (what_arg->v_type == VAR_DICT)
11596 d = what_arg->vval.v_dict;
11597 else
11598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011599 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011600 valid_dict = FALSE;
11601 }
11602 }
11603
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011604 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011605 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011606 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11607 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011608 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011609 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011610 }
11611#endif
11612}
11613
11614/*
11615 * "setloclist()" function
11616 */
11617 static void
11618f_setloclist(typval_T *argvars, typval_T *rettv)
11619{
11620 win_T *win;
11621
11622 rettv->vval.v_number = -1;
11623
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011624 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011625 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011626 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011627}
11628
11629/*
11630 * "setmatches()" function
11631 */
11632 static void
11633f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11634{
11635#ifdef FEAT_SEARCH_EXTRA
11636 list_T *l;
11637 listitem_T *li;
11638 dict_T *d;
11639 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011640 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641
11642 rettv->vval.v_number = -1;
11643 if (argvars[0].v_type != VAR_LIST)
11644 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011645 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011646 return;
11647 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011648 if (win == NULL)
11649 return;
11650
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011651 if ((l = argvars[0].vval.v_list) != NULL)
11652 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011653 /* To some extent make sure that we are dealing with a list from
11654 * "getmatches()". */
11655 li = l->lv_first;
11656 while (li != NULL)
11657 {
11658 if (li->li_tv.v_type != VAR_DICT
11659 || (d = li->li_tv.vval.v_dict) == NULL)
11660 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011661 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011662 return;
11663 }
11664 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11665 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11666 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11667 && dict_find(d, (char_u *)"priority", -1) != NULL
11668 && dict_find(d, (char_u *)"id", -1) != NULL))
11669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011670 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011671 return;
11672 }
11673 li = li->li_next;
11674 }
11675
Bram Moolenaaraff74912019-03-30 18:11:49 +010011676 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011677 li = l->lv_first;
11678 while (li != NULL)
11679 {
11680 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011681 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682 dictitem_T *di;
11683 char_u *group;
11684 int priority;
11685 int id;
11686 char_u *conceal;
11687
11688 d = li->li_tv.vval.v_dict;
11689 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11690 {
11691 if (s == NULL)
11692 {
11693 s = list_alloc();
11694 if (s == NULL)
11695 return;
11696 }
11697
11698 /* match from matchaddpos() */
11699 for (i = 1; i < 9; i++)
11700 {
11701 sprintf((char *)buf, (char *)"pos%d", i);
11702 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11703 {
11704 if (di->di_tv.v_type != VAR_LIST)
11705 return;
11706
11707 list_append_tv(s, &di->di_tv);
11708 s->lv_refcount++;
11709 }
11710 else
11711 break;
11712 }
11713 }
11714
Bram Moolenaar8f667172018-12-14 15:38:31 +010011715 group = dict_get_string(d, (char_u *)"group", TRUE);
11716 priority = (int)dict_get_number(d, (char_u *)"priority");
11717 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011719 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720 : NULL;
11721 if (i == 0)
11722 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011723 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011724 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 priority, id, NULL, conceal);
11726 }
11727 else
11728 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011729 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011730 list_unref(s);
11731 s = NULL;
11732 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011733 vim_free(group);
11734 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011735
11736 li = li->li_next;
11737 }
11738 rettv->vval.v_number = 0;
11739 }
11740#endif
11741}
11742
11743/*
11744 * "setpos()" function
11745 */
11746 static void
11747f_setpos(typval_T *argvars, typval_T *rettv)
11748{
11749 pos_T pos;
11750 int fnum;
11751 char_u *name;
11752 colnr_T curswant = -1;
11753
11754 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011755 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011756 if (name != NULL)
11757 {
11758 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11759 {
11760 if (--pos.col < 0)
11761 pos.col = 0;
11762 if (name[0] == '.' && name[1] == NUL)
11763 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011764 /* set cursor; "fnum" is ignored */
11765 curwin->w_cursor = pos;
11766 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011767 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011768 curwin->w_curswant = curswant - 1;
11769 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011771 check_cursor();
11772 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011773 }
11774 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11775 {
11776 /* set mark */
11777 if (setmark_pos(name[1], &pos, fnum) == OK)
11778 rettv->vval.v_number = 0;
11779 }
11780 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011781 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011782 }
11783 }
11784}
11785
11786/*
11787 * "setqflist()" function
11788 */
11789 static void
11790f_setqflist(typval_T *argvars, typval_T *rettv)
11791{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011792 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011793}
11794
11795/*
11796 * "setreg()" function
11797 */
11798 static void
11799f_setreg(typval_T *argvars, typval_T *rettv)
11800{
11801 int regname;
11802 char_u *strregname;
11803 char_u *stropt;
11804 char_u *strval;
11805 int append;
11806 char_u yank_type;
11807 long block_len;
11808
11809 block_len = -1;
11810 yank_type = MAUTO;
11811 append = FALSE;
11812
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011813 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011814 rettv->vval.v_number = 1; /* FAIL is default */
11815
11816 if (strregname == NULL)
11817 return; /* type error; errmsg already given */
11818 regname = *strregname;
11819 if (regname == 0 || regname == '@')
11820 regname = '"';
11821
11822 if (argvars[2].v_type != VAR_UNKNOWN)
11823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011824 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011825 if (stropt == NULL)
11826 return; /* type error */
11827 for (; *stropt != NUL; ++stropt)
11828 switch (*stropt)
11829 {
11830 case 'a': case 'A': /* append */
11831 append = TRUE;
11832 break;
11833 case 'v': case 'c': /* character-wise selection */
11834 yank_type = MCHAR;
11835 break;
11836 case 'V': case 'l': /* line-wise selection */
11837 yank_type = MLINE;
11838 break;
11839 case 'b': case Ctrl_V: /* block-wise selection */
11840 yank_type = MBLOCK;
11841 if (VIM_ISDIGIT(stropt[1]))
11842 {
11843 ++stropt;
11844 block_len = getdigits(&stropt) - 1;
11845 --stropt;
11846 }
11847 break;
11848 }
11849 }
11850
11851 if (argvars[1].v_type == VAR_LIST)
11852 {
11853 char_u **lstval;
11854 char_u **allocval;
11855 char_u buf[NUMBUFLEN];
11856 char_u **curval;
11857 char_u **curallocval;
11858 list_T *ll = argvars[1].vval.v_list;
11859 listitem_T *li;
11860 int len;
11861
11862 /* If the list is NULL handle like an empty list. */
11863 len = ll == NULL ? 0 : ll->lv_len;
11864
11865 /* First half: use for pointers to result lines; second half: use for
11866 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011867 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011868 if (lstval == NULL)
11869 return;
11870 curval = lstval;
11871 allocval = lstval + len + 2;
11872 curallocval = allocval;
11873
11874 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11875 li = li->li_next)
11876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011877 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 if (strval == NULL)
11879 goto free_lstval;
11880 if (strval == buf)
11881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011882 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883 * overwrite the string. */
11884 strval = vim_strsave(buf);
11885 if (strval == NULL)
11886 goto free_lstval;
11887 *curallocval++ = strval;
11888 }
11889 *curval++ = strval;
11890 }
11891 *curval++ = NULL;
11892
11893 write_reg_contents_lst(regname, lstval, -1,
11894 append, yank_type, block_len);
11895free_lstval:
11896 while (curallocval > allocval)
11897 vim_free(*--curallocval);
11898 vim_free(lstval);
11899 }
11900 else
11901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011902 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903 if (strval == NULL)
11904 return;
11905 write_reg_contents_ex(regname, strval, -1,
11906 append, yank_type, block_len);
11907 }
11908 rettv->vval.v_number = 0;
11909}
11910
11911/*
11912 * "settabvar()" function
11913 */
11914 static void
11915f_settabvar(typval_T *argvars, typval_T *rettv)
11916{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 tabpage_T *save_curtab;
11918 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011919 char_u *varname, *tabvarname;
11920 typval_T *varp;
11921
11922 rettv->vval.v_number = 0;
11923
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011924 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925 return;
11926
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011927 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11928 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011929 varp = &argvars[2];
11930
Bram Moolenaar4033c552017-09-16 20:54:51 +020011931 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011933 save_curtab = curtab;
11934 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935
Bram Moolenaar964b3742019-05-24 18:54:09 +020011936 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011937 if (tabvarname != NULL)
11938 {
11939 STRCPY(tabvarname, "t:");
11940 STRCPY(tabvarname + 2, varname);
11941 set_var(tabvarname, varp, TRUE);
11942 vim_free(tabvarname);
11943 }
11944
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011945 /* Restore current tabpage */
11946 if (valid_tabpage(save_curtab))
11947 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011948 }
11949}
11950
11951/*
11952 * "settabwinvar()" function
11953 */
11954 static void
11955f_settabwinvar(typval_T *argvars, typval_T *rettv)
11956{
11957 setwinvar(argvars, rettv, 1);
11958}
11959
11960/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011961 * "settagstack()" function
11962 */
11963 static void
11964f_settagstack(typval_T *argvars, typval_T *rettv)
11965{
11966 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11967 win_T *wp;
11968 dict_T *d;
11969 int action = 'r';
11970
11971 rettv->vval.v_number = -1;
11972
11973 // first argument: window number or id
11974 wp = find_win_by_nr_or_id(&argvars[0]);
11975 if (wp == NULL)
11976 return;
11977
11978 // second argument: dict with items to set in the tag stack
11979 if (argvars[1].v_type != VAR_DICT)
11980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011981 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011982 return;
11983 }
11984 d = argvars[1].vval.v_dict;
11985 if (d == NULL)
11986 return;
11987
11988 // third argument: action - 'a' for append and 'r' for replace.
11989 // default is to replace the stack.
11990 if (argvars[2].v_type == VAR_UNKNOWN)
11991 action = 'r';
11992 else if (argvars[2].v_type == VAR_STRING)
11993 {
11994 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011995 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011996 if (actstr == NULL)
11997 return;
11998 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11999 action = *actstr;
12000 else
12001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012002 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012003 return;
12004 }
12005 }
12006 else
12007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012008 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012009 return;
12010 }
12011
12012 if (set_tagstack(wp, d, action) == OK)
12013 rettv->vval.v_number = 0;
12014}
12015
12016/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012017 * "setwinvar()" function
12018 */
12019 static void
12020f_setwinvar(typval_T *argvars, typval_T *rettv)
12021{
12022 setwinvar(argvars, rettv, 0);
12023}
12024
12025#ifdef FEAT_CRYPT
12026/*
12027 * "sha256({string})" function
12028 */
12029 static void
12030f_sha256(typval_T *argvars, typval_T *rettv)
12031{
12032 char_u *p;
12033
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012034 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012035 rettv->vval.v_string = vim_strsave(
12036 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12037 rettv->v_type = VAR_STRING;
12038}
12039#endif /* FEAT_CRYPT */
12040
12041/*
12042 * "shellescape({string})" function
12043 */
12044 static void
12045f_shellescape(typval_T *argvars, typval_T *rettv)
12046{
Bram Moolenaar20615522017-06-05 18:46:26 +020012047 int do_special = non_zero_arg(&argvars[1]);
12048
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012049 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012050 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012051 rettv->v_type = VAR_STRING;
12052}
12053
12054/*
12055 * shiftwidth() function
12056 */
12057 static void
12058f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12059{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012060 rettv->vval.v_number = 0;
12061
12062 if (argvars[0].v_type != VAR_UNKNOWN)
12063 {
12064 long col;
12065
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012066 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012067 if (col < 0)
12068 return; // type error; errmsg already given
12069#ifdef FEAT_VARTABS
12070 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12071 return;
12072#endif
12073 }
12074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012075 rettv->vval.v_number = get_sw_value(curbuf);
12076}
12077
Bram Moolenaar162b7142018-12-21 15:17:36 +010012078#ifdef FEAT_SIGNS
12079/*
12080 * "sign_define()" function
12081 */
12082 static void
12083f_sign_define(typval_T *argvars, typval_T *rettv)
12084{
12085 char_u *name;
12086 dict_T *dict;
12087 char_u *icon = NULL;
12088 char_u *linehl = NULL;
12089 char_u *text = NULL;
12090 char_u *texthl = NULL;
12091
12092 rettv->vval.v_number = -1;
12093
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012094 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012095 if (name == NULL)
12096 return;
12097
12098 if (argvars[1].v_type != VAR_UNKNOWN)
12099 {
12100 if (argvars[1].v_type != VAR_DICT)
12101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012102 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012103 return;
12104 }
12105
12106 // sign attributes
12107 dict = argvars[1].vval.v_dict;
12108 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12109 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12110 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12111 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12112 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12113 text = dict_get_string(dict, (char_u *)"text", TRUE);
12114 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12115 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12116 }
12117
12118 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12119 rettv->vval.v_number = 0;
12120
12121 vim_free(icon);
12122 vim_free(linehl);
12123 vim_free(text);
12124 vim_free(texthl);
12125}
12126
12127/*
12128 * "sign_getdefined()" function
12129 */
12130 static void
12131f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12132{
12133 char_u *name = NULL;
12134
12135 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12136 return;
12137
12138 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012139 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012140
12141 sign_getlist(name, rettv->vval.v_list);
12142}
12143
12144/*
12145 * "sign_getplaced()" function
12146 */
12147 static void
12148f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12149{
12150 buf_T *buf = NULL;
12151 dict_T *dict;
12152 dictitem_T *di;
12153 linenr_T lnum = 0;
12154 int sign_id = 0;
12155 char_u *group = NULL;
12156 int notanum = FALSE;
12157
12158 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12159 return;
12160
12161 if (argvars[0].v_type != VAR_UNKNOWN)
12162 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012163 // get signs placed in the specified buffer
12164 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012165 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012166 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012167
12168 if (argvars[1].v_type != VAR_UNKNOWN)
12169 {
12170 if (argvars[1].v_type != VAR_DICT ||
12171 ((dict = argvars[1].vval.v_dict) == NULL))
12172 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012173 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012174 return;
12175 }
12176 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12177 {
12178 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012179 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012180 if (notanum)
12181 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012182 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012183 }
12184 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12185 {
12186 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012187 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012188 if (notanum)
12189 return;
12190 }
12191 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012193 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012194 if (group == NULL)
12195 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012196 if (*group == '\0') // empty string means global group
12197 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012198 }
12199 }
12200 }
12201
12202 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12203}
12204
12205/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012206 * "sign_jump()" function
12207 */
12208 static void
12209f_sign_jump(typval_T *argvars, typval_T *rettv)
12210{
12211 int sign_id;
12212 char_u *sign_group = NULL;
12213 buf_T *buf;
12214 int notanum = FALSE;
12215
12216 rettv->vval.v_number = -1;
12217
Bram Moolenaarbdace832019-03-02 10:13:42 +010012218 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012219 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12220 if (notanum)
12221 return;
12222 if (sign_id <= 0)
12223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012224 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012225 return;
12226 }
12227
12228 // Sign group
12229 sign_group = tv_get_string_chk(&argvars[1]);
12230 if (sign_group == NULL)
12231 return;
12232 if (sign_group[0] == '\0')
12233 sign_group = NULL; // global sign group
12234 else
12235 {
12236 sign_group = vim_strsave(sign_group);
12237 if (sign_group == NULL)
12238 return;
12239 }
12240
12241 // Buffer to place the sign
12242 buf = get_buf_arg(&argvars[2]);
12243 if (buf == NULL)
12244 goto cleanup;
12245
12246 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12247
12248cleanup:
12249 vim_free(sign_group);
12250}
12251
12252/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012253 * "sign_place()" function
12254 */
12255 static void
12256f_sign_place(typval_T *argvars, typval_T *rettv)
12257{
12258 int sign_id;
12259 char_u *group = NULL;
12260 char_u *sign_name;
12261 buf_T *buf;
12262 dict_T *dict;
12263 dictitem_T *di;
12264 linenr_T lnum = 0;
12265 int prio = SIGN_DEF_PRIO;
12266 int notanum = FALSE;
12267
12268 rettv->vval.v_number = -1;
12269
Bram Moolenaarbdace832019-03-02 10:13:42 +010012270 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012271 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012272 if (notanum)
12273 return;
12274 if (sign_id < 0)
12275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012276 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012277 return;
12278 }
12279
12280 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012281 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012282 if (group == NULL)
12283 return;
12284 if (group[0] == '\0')
12285 group = NULL; // global sign group
12286 else
12287 {
12288 group = vim_strsave(group);
12289 if (group == NULL)
12290 return;
12291 }
12292
12293 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012294 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012295 if (sign_name == NULL)
12296 goto cleanup;
12297
12298 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012299 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012300 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012301 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012302
12303 if (argvars[4].v_type != VAR_UNKNOWN)
12304 {
12305 if (argvars[4].v_type != VAR_DICT ||
12306 ((dict = argvars[4].vval.v_dict) == NULL))
12307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012308 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012309 goto cleanup;
12310 }
12311
12312 // Line number where the sign is to be placed
12313 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12314 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012315 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012316 if (notanum)
12317 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012318 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012319 }
12320 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12321 {
12322 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012323 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012324 if (notanum)
12325 goto cleanup;
12326 }
12327 }
12328
12329 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12330 rettv->vval.v_number = sign_id;
12331
12332cleanup:
12333 vim_free(group);
12334}
12335
12336/*
12337 * "sign_undefine()" function
12338 */
12339 static void
12340f_sign_undefine(typval_T *argvars, typval_T *rettv)
12341{
12342 char_u *name;
12343
12344 rettv->vval.v_number = -1;
12345
12346 if (argvars[0].v_type == VAR_UNKNOWN)
12347 {
12348 // Free all the signs
12349 free_signs();
12350 rettv->vval.v_number = 0;
12351 }
12352 else
12353 {
12354 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012355 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012356 if (name == NULL)
12357 return;
12358
12359 if (sign_undefine_by_name(name) == OK)
12360 rettv->vval.v_number = 0;
12361 }
12362}
12363
12364/*
12365 * "sign_unplace()" function
12366 */
12367 static void
12368f_sign_unplace(typval_T *argvars, typval_T *rettv)
12369{
12370 dict_T *dict;
12371 dictitem_T *di;
12372 int sign_id = 0;
12373 buf_T *buf = NULL;
12374 char_u *group = NULL;
12375
12376 rettv->vval.v_number = -1;
12377
12378 if (argvars[0].v_type != VAR_STRING)
12379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012380 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012381 return;
12382 }
12383
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012384 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012385 if (group[0] == '\0')
12386 group = NULL; // global sign group
12387 else
12388 {
12389 group = vim_strsave(group);
12390 if (group == NULL)
12391 return;
12392 }
12393
12394 if (argvars[1].v_type != VAR_UNKNOWN)
12395 {
12396 if (argvars[1].v_type != VAR_DICT)
12397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012398 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012399 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012400 }
12401 dict = argvars[1].vval.v_dict;
12402
12403 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12404 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012405 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012406 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012407 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012408 }
12409 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12410 sign_id = dict_get_number(dict, (char_u *)"id");
12411 }
12412
12413 if (buf == NULL)
12414 {
12415 // Delete the sign in all the buffers
12416 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012417 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012418 rettv->vval.v_number = 0;
12419 }
12420 else
12421 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012422 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012423 rettv->vval.v_number = 0;
12424 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012425
12426cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012427 vim_free(group);
12428}
12429#endif
12430
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431/*
12432 * "simplify()" function
12433 */
12434 static void
12435f_simplify(typval_T *argvars, typval_T *rettv)
12436{
12437 char_u *p;
12438
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012439 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012440 rettv->vval.v_string = vim_strsave(p);
12441 simplify_filename(rettv->vval.v_string); /* simplify in place */
12442 rettv->v_type = VAR_STRING;
12443}
12444
12445#ifdef FEAT_FLOAT
12446/*
12447 * "sin()" function
12448 */
12449 static void
12450f_sin(typval_T *argvars, typval_T *rettv)
12451{
12452 float_T f = 0.0;
12453
12454 rettv->v_type = VAR_FLOAT;
12455 if (get_float_arg(argvars, &f) == OK)
12456 rettv->vval.v_float = sin(f);
12457 else
12458 rettv->vval.v_float = 0.0;
12459}
12460
12461/*
12462 * "sinh()" function
12463 */
12464 static void
12465f_sinh(typval_T *argvars, typval_T *rettv)
12466{
12467 float_T f = 0.0;
12468
12469 rettv->v_type = VAR_FLOAT;
12470 if (get_float_arg(argvars, &f) == OK)
12471 rettv->vval.v_float = sinh(f);
12472 else
12473 rettv->vval.v_float = 0.0;
12474}
12475#endif
12476
Bram Moolenaareae1b912019-05-09 15:12:55 +020012477static int item_compare(const void *s1, const void *s2);
12478static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012479
12480/* struct used in the array that's given to qsort() */
12481typedef struct
12482{
12483 listitem_T *item;
12484 int idx;
12485} sortItem_T;
12486
12487/* struct storing information about current sort */
12488typedef struct
12489{
12490 int item_compare_ic;
12491 int item_compare_numeric;
12492 int item_compare_numbers;
12493#ifdef FEAT_FLOAT
12494 int item_compare_float;
12495#endif
12496 char_u *item_compare_func;
12497 partial_T *item_compare_partial;
12498 dict_T *item_compare_selfdict;
12499 int item_compare_func_err;
12500 int item_compare_keep_zero;
12501} sortinfo_T;
12502static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012503#define ITEM_COMPARE_FAIL 999
12504
12505/*
12506 * Compare functions for f_sort() and f_uniq() below.
12507 */
12508 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012509item_compare(const void *s1, const void *s2)
12510{
12511 sortItem_T *si1, *si2;
12512 typval_T *tv1, *tv2;
12513 char_u *p1, *p2;
12514 char_u *tofree1 = NULL, *tofree2 = NULL;
12515 int res;
12516 char_u numbuf1[NUMBUFLEN];
12517 char_u numbuf2[NUMBUFLEN];
12518
12519 si1 = (sortItem_T *)s1;
12520 si2 = (sortItem_T *)s2;
12521 tv1 = &si1->item->li_tv;
12522 tv2 = &si2->item->li_tv;
12523
12524 if (sortinfo->item_compare_numbers)
12525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012526 varnumber_T v1 = tv_get_number(tv1);
12527 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012528
12529 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12530 }
12531
12532#ifdef FEAT_FLOAT
12533 if (sortinfo->item_compare_float)
12534 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012535 float_T v1 = tv_get_float(tv1);
12536 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12539 }
12540#endif
12541
12542 /* tv2string() puts quotes around a string and allocates memory. Don't do
12543 * that for string variables. Use a single quote when comparing with a
12544 * non-string to do what the docs promise. */
12545 if (tv1->v_type == VAR_STRING)
12546 {
12547 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12548 p1 = (char_u *)"'";
12549 else
12550 p1 = tv1->vval.v_string;
12551 }
12552 else
12553 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12554 if (tv2->v_type == VAR_STRING)
12555 {
12556 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12557 p2 = (char_u *)"'";
12558 else
12559 p2 = tv2->vval.v_string;
12560 }
12561 else
12562 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12563 if (p1 == NULL)
12564 p1 = (char_u *)"";
12565 if (p2 == NULL)
12566 p2 = (char_u *)"";
12567 if (!sortinfo->item_compare_numeric)
12568 {
12569 if (sortinfo->item_compare_ic)
12570 res = STRICMP(p1, p2);
12571 else
12572 res = STRCMP(p1, p2);
12573 }
12574 else
12575 {
12576 double n1, n2;
12577 n1 = strtod((char *)p1, (char **)&p1);
12578 n2 = strtod((char *)p2, (char **)&p2);
12579 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12580 }
12581
12582 /* When the result would be zero, compare the item indexes. Makes the
12583 * sort stable. */
12584 if (res == 0 && !sortinfo->item_compare_keep_zero)
12585 res = si1->idx > si2->idx ? 1 : -1;
12586
12587 vim_free(tofree1);
12588 vim_free(tofree2);
12589 return res;
12590}
12591
12592 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012593item_compare2(const void *s1, const void *s2)
12594{
12595 sortItem_T *si1, *si2;
12596 int res;
12597 typval_T rettv;
12598 typval_T argv[3];
12599 int dummy;
12600 char_u *func_name;
12601 partial_T *partial = sortinfo->item_compare_partial;
12602
12603 /* shortcut after failure in previous call; compare all items equal */
12604 if (sortinfo->item_compare_func_err)
12605 return 0;
12606
12607 si1 = (sortItem_T *)s1;
12608 si2 = (sortItem_T *)s2;
12609
12610 if (partial == NULL)
12611 func_name = sortinfo->item_compare_func;
12612 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012613 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614
12615 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12616 * in the copy without changing the original list items. */
12617 copy_tv(&si1->item->li_tv, &argv[0]);
12618 copy_tv(&si2->item->li_tv, &argv[1]);
12619
12620 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012621 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012622 partial, sortinfo->item_compare_selfdict);
12623 clear_tv(&argv[0]);
12624 clear_tv(&argv[1]);
12625
12626 if (res == FAIL)
12627 res = ITEM_COMPARE_FAIL;
12628 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012629 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012630 if (sortinfo->item_compare_func_err)
12631 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12632 clear_tv(&rettv);
12633
12634 /* When the result would be zero, compare the pointers themselves. Makes
12635 * the sort stable. */
12636 if (res == 0 && !sortinfo->item_compare_keep_zero)
12637 res = si1->idx > si2->idx ? 1 : -1;
12638
12639 return res;
12640}
12641
12642/*
12643 * "sort({list})" function
12644 */
12645 static void
12646do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12647{
12648 list_T *l;
12649 listitem_T *li;
12650 sortItem_T *ptrs;
12651 sortinfo_T *old_sortinfo;
12652 sortinfo_T info;
12653 long len;
12654 long i;
12655
12656 /* Pointer to current info struct used in compare function. Save and
12657 * restore the current one for nested calls. */
12658 old_sortinfo = sortinfo;
12659 sortinfo = &info;
12660
12661 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012662 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 else
12664 {
12665 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012666 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012667 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12668 TRUE))
12669 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012670 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012671
12672 len = list_len(l);
12673 if (len <= 1)
12674 goto theend; /* short list sorts pretty quickly */
12675
12676 info.item_compare_ic = FALSE;
12677 info.item_compare_numeric = FALSE;
12678 info.item_compare_numbers = FALSE;
12679#ifdef FEAT_FLOAT
12680 info.item_compare_float = FALSE;
12681#endif
12682 info.item_compare_func = NULL;
12683 info.item_compare_partial = NULL;
12684 info.item_compare_selfdict = NULL;
12685 if (argvars[1].v_type != VAR_UNKNOWN)
12686 {
12687 /* optional second argument: {func} */
12688 if (argvars[1].v_type == VAR_FUNC)
12689 info.item_compare_func = argvars[1].vval.v_string;
12690 else if (argvars[1].v_type == VAR_PARTIAL)
12691 info.item_compare_partial = argvars[1].vval.v_partial;
12692 else
12693 {
12694 int error = FALSE;
12695
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012696 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012697 if (error)
12698 goto theend; /* type error; errmsg already given */
12699 if (i == 1)
12700 info.item_compare_ic = TRUE;
12701 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012702 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012703 else if (i != 0)
12704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012705 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012706 goto theend;
12707 }
12708 if (info.item_compare_func != NULL)
12709 {
12710 if (*info.item_compare_func == NUL)
12711 {
12712 /* empty string means default sort */
12713 info.item_compare_func = NULL;
12714 }
12715 else if (STRCMP(info.item_compare_func, "n") == 0)
12716 {
12717 info.item_compare_func = NULL;
12718 info.item_compare_numeric = TRUE;
12719 }
12720 else if (STRCMP(info.item_compare_func, "N") == 0)
12721 {
12722 info.item_compare_func = NULL;
12723 info.item_compare_numbers = TRUE;
12724 }
12725#ifdef FEAT_FLOAT
12726 else if (STRCMP(info.item_compare_func, "f") == 0)
12727 {
12728 info.item_compare_func = NULL;
12729 info.item_compare_float = TRUE;
12730 }
12731#endif
12732 else if (STRCMP(info.item_compare_func, "i") == 0)
12733 {
12734 info.item_compare_func = NULL;
12735 info.item_compare_ic = TRUE;
12736 }
12737 }
12738 }
12739
12740 if (argvars[2].v_type != VAR_UNKNOWN)
12741 {
12742 /* optional third argument: {dict} */
12743 if (argvars[2].v_type != VAR_DICT)
12744 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012745 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012746 goto theend;
12747 }
12748 info.item_compare_selfdict = argvars[2].vval.v_dict;
12749 }
12750 }
12751
12752 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012753 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012754 if (ptrs == NULL)
12755 goto theend;
12756
12757 i = 0;
12758 if (sort)
12759 {
12760 /* sort(): ptrs will be the list to sort */
12761 for (li = l->lv_first; li != NULL; li = li->li_next)
12762 {
12763 ptrs[i].item = li;
12764 ptrs[i].idx = i;
12765 ++i;
12766 }
12767
12768 info.item_compare_func_err = FALSE;
12769 info.item_compare_keep_zero = FALSE;
12770 /* test the compare function */
12771 if ((info.item_compare_func != NULL
12772 || info.item_compare_partial != NULL)
12773 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12774 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012775 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012776 else
12777 {
12778 /* Sort the array with item pointers. */
12779 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12780 info.item_compare_func == NULL
12781 && info.item_compare_partial == NULL
12782 ? item_compare : item_compare2);
12783
12784 if (!info.item_compare_func_err)
12785 {
12786 /* Clear the List and append the items in sorted order. */
12787 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12788 l->lv_len = 0;
12789 for (i = 0; i < len; ++i)
12790 list_append(l, ptrs[i].item);
12791 }
12792 }
12793 }
12794 else
12795 {
12796 int (*item_compare_func_ptr)(const void *, const void *);
12797
12798 /* f_uniq(): ptrs will be a stack of items to remove */
12799 info.item_compare_func_err = FALSE;
12800 info.item_compare_keep_zero = TRUE;
12801 item_compare_func_ptr = info.item_compare_func != NULL
12802 || info.item_compare_partial != NULL
12803 ? item_compare2 : item_compare;
12804
12805 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12806 li = li->li_next)
12807 {
12808 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12809 == 0)
12810 ptrs[i++].item = li;
12811 if (info.item_compare_func_err)
12812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012813 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012814 break;
12815 }
12816 }
12817
12818 if (!info.item_compare_func_err)
12819 {
12820 while (--i >= 0)
12821 {
12822 li = ptrs[i].item->li_next;
12823 ptrs[i].item->li_next = li->li_next;
12824 if (li->li_next != NULL)
12825 li->li_next->li_prev = ptrs[i].item;
12826 else
12827 l->lv_last = ptrs[i].item;
12828 list_fix_watch(l, li);
12829 listitem_free(li);
12830 l->lv_len--;
12831 }
12832 }
12833 }
12834
12835 vim_free(ptrs);
12836 }
12837theend:
12838 sortinfo = old_sortinfo;
12839}
12840
12841/*
12842 * "sort({list})" function
12843 */
12844 static void
12845f_sort(typval_T *argvars, typval_T *rettv)
12846{
12847 do_sort_uniq(argvars, rettv, TRUE);
12848}
12849
12850/*
12851 * "uniq({list})" function
12852 */
12853 static void
12854f_uniq(typval_T *argvars, typval_T *rettv)
12855{
12856 do_sort_uniq(argvars, rettv, FALSE);
12857}
12858
12859/*
12860 * "soundfold({word})" function
12861 */
12862 static void
12863f_soundfold(typval_T *argvars, typval_T *rettv)
12864{
12865 char_u *s;
12866
12867 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012868 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012869#ifdef FEAT_SPELL
12870 rettv->vval.v_string = eval_soundfold(s);
12871#else
12872 rettv->vval.v_string = vim_strsave(s);
12873#endif
12874}
12875
12876/*
12877 * "spellbadword()" function
12878 */
12879 static void
12880f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12881{
12882 char_u *word = (char_u *)"";
12883 hlf_T attr = HLF_COUNT;
12884 int len = 0;
12885
12886 if (rettv_list_alloc(rettv) == FAIL)
12887 return;
12888
12889#ifdef FEAT_SPELL
12890 if (argvars[0].v_type == VAR_UNKNOWN)
12891 {
12892 /* Find the start and length of the badly spelled word. */
12893 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12894 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012895 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012896 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012897 curwin->w_set_curswant = TRUE;
12898 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012899 }
12900 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012902 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012903 int capcol = -1;
12904
12905 if (str != NULL)
12906 {
12907 /* Check the argument for spelling. */
12908 while (*str != NUL)
12909 {
12910 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12911 if (attr != HLF_COUNT)
12912 {
12913 word = str;
12914 break;
12915 }
12916 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012917 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012918 }
12919 }
12920 }
12921#endif
12922
12923 list_append_string(rettv->vval.v_list, word, len);
12924 list_append_string(rettv->vval.v_list, (char_u *)(
12925 attr == HLF_SPB ? "bad" :
12926 attr == HLF_SPR ? "rare" :
12927 attr == HLF_SPL ? "local" :
12928 attr == HLF_SPC ? "caps" :
12929 ""), -1);
12930}
12931
12932/*
12933 * "spellsuggest()" function
12934 */
12935 static void
12936f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12937{
12938#ifdef FEAT_SPELL
12939 char_u *str;
12940 int typeerr = FALSE;
12941 int maxcount;
12942 garray_T ga;
12943 int i;
12944 listitem_T *li;
12945 int need_capital = FALSE;
12946#endif
12947
12948 if (rettv_list_alloc(rettv) == FAIL)
12949 return;
12950
12951#ifdef FEAT_SPELL
12952 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12953 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012954 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012955 if (argvars[1].v_type != VAR_UNKNOWN)
12956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012957 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 if (maxcount <= 0)
12959 return;
12960 if (argvars[2].v_type != VAR_UNKNOWN)
12961 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012962 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012963 if (typeerr)
12964 return;
12965 }
12966 }
12967 else
12968 maxcount = 25;
12969
12970 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12971
12972 for (i = 0; i < ga.ga_len; ++i)
12973 {
12974 str = ((char_u **)ga.ga_data)[i];
12975
12976 li = listitem_alloc();
12977 if (li == NULL)
12978 vim_free(str);
12979 else
12980 {
12981 li->li_tv.v_type = VAR_STRING;
12982 li->li_tv.v_lock = 0;
12983 li->li_tv.vval.v_string = str;
12984 list_append(rettv->vval.v_list, li);
12985 }
12986 }
12987 ga_clear(&ga);
12988 }
12989#endif
12990}
12991
12992 static void
12993f_split(typval_T *argvars, typval_T *rettv)
12994{
12995 char_u *str;
12996 char_u *end;
12997 char_u *pat = NULL;
12998 regmatch_T regmatch;
12999 char_u patbuf[NUMBUFLEN];
13000 char_u *save_cpo;
13001 int match;
13002 colnr_T col = 0;
13003 int keepempty = FALSE;
13004 int typeerr = FALSE;
13005
13006 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13007 save_cpo = p_cpo;
13008 p_cpo = (char_u *)"";
13009
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011 if (argvars[1].v_type != VAR_UNKNOWN)
13012 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013013 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 if (pat == NULL)
13015 typeerr = TRUE;
13016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013017 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018 }
13019 if (pat == NULL || *pat == NUL)
13020 pat = (char_u *)"[\\x01- ]\\+";
13021
13022 if (rettv_list_alloc(rettv) == FAIL)
13023 return;
13024 if (typeerr)
13025 return;
13026
13027 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13028 if (regmatch.regprog != NULL)
13029 {
13030 regmatch.rm_ic = FALSE;
13031 while (*str != NUL || keepempty)
13032 {
13033 if (*str == NUL)
13034 match = FALSE; /* empty item at the end */
13035 else
13036 match = vim_regexec_nl(&regmatch, str, col);
13037 if (match)
13038 end = regmatch.startp[0];
13039 else
13040 end = str + STRLEN(str);
13041 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13042 && *str != NUL && match && end < regmatch.endp[0]))
13043 {
13044 if (list_append_string(rettv->vval.v_list, str,
13045 (int)(end - str)) == FAIL)
13046 break;
13047 }
13048 if (!match)
13049 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013050 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013051 if (regmatch.endp[0] > str)
13052 col = 0;
13053 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013054 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013055 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013056 str = regmatch.endp[0];
13057 }
13058
13059 vim_regfree(regmatch.regprog);
13060 }
13061
13062 p_cpo = save_cpo;
13063}
13064
13065#ifdef FEAT_FLOAT
13066/*
13067 * "sqrt()" function
13068 */
13069 static void
13070f_sqrt(typval_T *argvars, typval_T *rettv)
13071{
13072 float_T f = 0.0;
13073
13074 rettv->v_type = VAR_FLOAT;
13075 if (get_float_arg(argvars, &f) == OK)
13076 rettv->vval.v_float = sqrt(f);
13077 else
13078 rettv->vval.v_float = 0.0;
13079}
13080
13081/*
13082 * "str2float()" function
13083 */
13084 static void
13085f_str2float(typval_T *argvars, typval_T *rettv)
13086{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013087 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013088 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089
Bram Moolenaar08243d22017-01-10 16:12:29 +010013090 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013091 p = skipwhite(p + 1);
13092 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013093 if (isneg)
13094 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013095 rettv->v_type = VAR_FLOAT;
13096}
13097#endif
13098
13099/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013100 * "str2list()" function
13101 */
13102 static void
13103f_str2list(typval_T *argvars, typval_T *rettv)
13104{
13105 char_u *p;
13106 int utf8 = FALSE;
13107
13108 if (rettv_list_alloc(rettv) == FAIL)
13109 return;
13110
13111 if (argvars[1].v_type != VAR_UNKNOWN)
13112 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13113
13114 p = tv_get_string(&argvars[0]);
13115
13116 if (has_mbyte || utf8)
13117 {
13118 int (*ptr2len)(char_u *);
13119 int (*ptr2char)(char_u *);
13120
13121 if (utf8 || enc_utf8)
13122 {
13123 ptr2len = utf_ptr2len;
13124 ptr2char = utf_ptr2char;
13125 }
13126 else
13127 {
13128 ptr2len = mb_ptr2len;
13129 ptr2char = mb_ptr2char;
13130 }
13131
13132 for ( ; *p != NUL; p += (*ptr2len)(p))
13133 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13134 }
13135 else
13136 for ( ; *p != NUL; ++p)
13137 list_append_number(rettv->vval.v_list, *p);
13138}
13139
13140/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013141 * "str2nr()" function
13142 */
13143 static void
13144f_str2nr(typval_T *argvars, typval_T *rettv)
13145{
13146 int base = 10;
13147 char_u *p;
13148 varnumber_T n;
13149 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013150 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151
13152 if (argvars[1].v_type != VAR_UNKNOWN)
13153 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013154 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013155 if (base != 2 && base != 8 && base != 10 && base != 16)
13156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013157 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 return;
13159 }
13160 }
13161
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013162 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013163 isneg = (*p == '-');
13164 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165 p = skipwhite(p + 1);
13166 switch (base)
13167 {
13168 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13169 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13170 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13171 default: what = 0;
13172 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013173 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13174 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013175 if (isneg)
13176 rettv->vval.v_number = -n;
13177 else
13178 rettv->vval.v_number = n;
13179
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180}
13181
13182#ifdef HAVE_STRFTIME
13183/*
13184 * "strftime({format}[, {time}])" function
13185 */
13186 static void
13187f_strftime(typval_T *argvars, typval_T *rettv)
13188{
13189 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013190# ifdef HAVE_LOCALTIME_R
13191 struct tm tmval;
13192# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193 struct tm *curtime;
13194 time_t seconds;
13195 char_u *p;
13196
13197 rettv->v_type = VAR_STRING;
13198
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013199 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013200 if (argvars[1].v_type == VAR_UNKNOWN)
13201 seconds = time(NULL);
13202 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013203 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013204# ifdef HAVE_LOCALTIME_R
13205 curtime = localtime_r(&seconds, &tmval);
13206# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013207 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013208# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 /* MSVC returns NULL for an invalid value of seconds. */
13210 if (curtime == NULL)
13211 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13212 else
13213 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 vimconv_T conv;
13215 char_u *enc;
13216
13217 conv.vc_type = CONV_NONE;
13218 enc = enc_locale();
13219 convert_setup(&conv, p_enc, enc);
13220 if (conv.vc_type != CONV_NONE)
13221 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222 if (p != NULL)
13223 (void)strftime((char *)result_buf, sizeof(result_buf),
13224 (char *)p, curtime);
13225 else
13226 result_buf[0] = NUL;
13227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 if (conv.vc_type != CONV_NONE)
13229 vim_free(p);
13230 convert_setup(&conv, enc, p_enc);
13231 if (conv.vc_type != CONV_NONE)
13232 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13233 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234 rettv->vval.v_string = vim_strsave(result_buf);
13235
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013236 /* Release conversion descriptors */
13237 convert_setup(&conv, NULL, NULL);
13238 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013239 }
13240}
13241#endif
13242
13243/*
13244 * "strgetchar()" function
13245 */
13246 static void
13247f_strgetchar(typval_T *argvars, typval_T *rettv)
13248{
13249 char_u *str;
13250 int len;
13251 int error = FALSE;
13252 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013253 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013254
13255 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013256 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013257 if (str == NULL)
13258 return;
13259 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013260 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261 if (error)
13262 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263
Bram Moolenaar13505972019-01-24 15:04:48 +010013264 while (charidx >= 0 && byteidx < len)
13265 {
13266 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013268 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13269 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013271 --charidx;
13272 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274}
13275
13276/*
13277 * "stridx()" function
13278 */
13279 static void
13280f_stridx(typval_T *argvars, typval_T *rettv)
13281{
13282 char_u buf[NUMBUFLEN];
13283 char_u *needle;
13284 char_u *haystack;
13285 char_u *save_haystack;
13286 char_u *pos;
13287 int start_idx;
13288
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013289 needle = tv_get_string_chk(&argvars[1]);
13290 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013291 rettv->vval.v_number = -1;
13292 if (needle == NULL || haystack == NULL)
13293 return; /* type error; errmsg already given */
13294
13295 if (argvars[2].v_type != VAR_UNKNOWN)
13296 {
13297 int error = FALSE;
13298
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013299 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 if (error || start_idx >= (int)STRLEN(haystack))
13301 return;
13302 if (start_idx >= 0)
13303 haystack += start_idx;
13304 }
13305
13306 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13307 if (pos != NULL)
13308 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13309}
13310
13311/*
13312 * "string()" function
13313 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013314 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013315f_string(typval_T *argvars, typval_T *rettv)
13316{
13317 char_u *tofree;
13318 char_u numbuf[NUMBUFLEN];
13319
13320 rettv->v_type = VAR_STRING;
13321 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13322 get_copyID());
13323 /* Make a copy if we have a value but it's not in allocated memory. */
13324 if (rettv->vval.v_string != NULL && tofree == NULL)
13325 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13326}
13327
13328/*
13329 * "strlen()" function
13330 */
13331 static void
13332f_strlen(typval_T *argvars, typval_T *rettv)
13333{
13334 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013335 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336}
13337
13338/*
13339 * "strchars()" function
13340 */
13341 static void
13342f_strchars(typval_T *argvars, typval_T *rettv)
13343{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346 varnumber_T len = 0;
13347 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348
13349 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013350 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013352 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 else
13354 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013355 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13356 while (*s != NUL)
13357 {
13358 func_mb_ptr2char_adv(&s);
13359 ++len;
13360 }
13361 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362 }
13363}
13364
13365/*
13366 * "strdisplaywidth()" function
13367 */
13368 static void
13369f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13370{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013371 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372 int col = 0;
13373
13374 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013375 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013376
13377 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13378}
13379
13380/*
13381 * "strwidth()" function
13382 */
13383 static void
13384f_strwidth(typval_T *argvars, typval_T *rettv)
13385{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013386 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387
Bram Moolenaar13505972019-01-24 15:04:48 +010013388 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013389}
13390
13391/*
13392 * "strcharpart()" function
13393 */
13394 static void
13395f_strcharpart(typval_T *argvars, typval_T *rettv)
13396{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013397 char_u *p;
13398 int nchar;
13399 int nbyte = 0;
13400 int charlen;
13401 int len = 0;
13402 int slen;
13403 int error = FALSE;
13404
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013405 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013406 slen = (int)STRLEN(p);
13407
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013408 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013409 if (!error)
13410 {
13411 if (nchar > 0)
13412 while (nchar > 0 && nbyte < slen)
13413 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013414 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013415 --nchar;
13416 }
13417 else
13418 nbyte = nchar;
13419 if (argvars[2].v_type != VAR_UNKNOWN)
13420 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013421 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013422 while (charlen > 0 && nbyte + len < slen)
13423 {
13424 int off = nbyte + len;
13425
13426 if (off < 0)
13427 len += 1;
13428 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013429 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013430 --charlen;
13431 }
13432 }
13433 else
13434 len = slen - nbyte; /* default: all bytes that are available. */
13435 }
13436
13437 /*
13438 * Only return the overlap between the specified part and the actual
13439 * string.
13440 */
13441 if (nbyte < 0)
13442 {
13443 len += nbyte;
13444 nbyte = 0;
13445 }
13446 else if (nbyte > slen)
13447 nbyte = slen;
13448 if (len < 0)
13449 len = 0;
13450 else if (nbyte + len > slen)
13451 len = slen - nbyte;
13452
13453 rettv->v_type = VAR_STRING;
13454 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013455}
13456
13457/*
13458 * "strpart()" function
13459 */
13460 static void
13461f_strpart(typval_T *argvars, typval_T *rettv)
13462{
13463 char_u *p;
13464 int n;
13465 int len;
13466 int slen;
13467 int error = FALSE;
13468
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013469 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013470 slen = (int)STRLEN(p);
13471
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013472 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013473 if (error)
13474 len = 0;
13475 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013476 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013477 else
13478 len = slen - n; /* default len: all bytes that are available. */
13479
13480 /*
13481 * Only return the overlap between the specified part and the actual
13482 * string.
13483 */
13484 if (n < 0)
13485 {
13486 len += n;
13487 n = 0;
13488 }
13489 else if (n > slen)
13490 n = slen;
13491 if (len < 0)
13492 len = 0;
13493 else if (n + len > slen)
13494 len = slen - n;
13495
13496 rettv->v_type = VAR_STRING;
13497 rettv->vval.v_string = vim_strnsave(p + n, len);
13498}
13499
13500/*
13501 * "strridx()" function
13502 */
13503 static void
13504f_strridx(typval_T *argvars, typval_T *rettv)
13505{
13506 char_u buf[NUMBUFLEN];
13507 char_u *needle;
13508 char_u *haystack;
13509 char_u *rest;
13510 char_u *lastmatch = NULL;
13511 int haystack_len, end_idx;
13512
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013513 needle = tv_get_string_chk(&argvars[1]);
13514 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013515
13516 rettv->vval.v_number = -1;
13517 if (needle == NULL || haystack == NULL)
13518 return; /* type error; errmsg already given */
13519
13520 haystack_len = (int)STRLEN(haystack);
13521 if (argvars[2].v_type != VAR_UNKNOWN)
13522 {
13523 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013524 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013525 if (end_idx < 0)
13526 return; /* can never find a match */
13527 }
13528 else
13529 end_idx = haystack_len;
13530
13531 if (*needle == NUL)
13532 {
13533 /* Empty string matches past the end. */
13534 lastmatch = haystack + end_idx;
13535 }
13536 else
13537 {
13538 for (rest = haystack; *rest != '\0'; ++rest)
13539 {
13540 rest = (char_u *)strstr((char *)rest, (char *)needle);
13541 if (rest == NULL || rest > haystack + end_idx)
13542 break;
13543 lastmatch = rest;
13544 }
13545 }
13546
13547 if (lastmatch == NULL)
13548 rettv->vval.v_number = -1;
13549 else
13550 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13551}
13552
13553/*
13554 * "strtrans()" function
13555 */
13556 static void
13557f_strtrans(typval_T *argvars, typval_T *rettv)
13558{
13559 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013560 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013561}
13562
13563/*
13564 * "submatch()" function
13565 */
13566 static void
13567f_submatch(typval_T *argvars, typval_T *rettv)
13568{
13569 int error = FALSE;
13570 int no;
13571 int retList = 0;
13572
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013573 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013574 if (error)
13575 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013576 if (no < 0 || no >= NSUBEXP)
13577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013578 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013579 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013580 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013582 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583 if (error)
13584 return;
13585
13586 if (retList == 0)
13587 {
13588 rettv->v_type = VAR_STRING;
13589 rettv->vval.v_string = reg_submatch(no);
13590 }
13591 else
13592 {
13593 rettv->v_type = VAR_LIST;
13594 rettv->vval.v_list = reg_submatch_list(no);
13595 }
13596}
13597
13598/*
13599 * "substitute()" function
13600 */
13601 static void
13602f_substitute(typval_T *argvars, typval_T *rettv)
13603{
13604 char_u patbuf[NUMBUFLEN];
13605 char_u subbuf[NUMBUFLEN];
13606 char_u flagsbuf[NUMBUFLEN];
13607
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013608 char_u *str = tv_get_string_chk(&argvars[0]);
13609 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013610 char_u *sub = NULL;
13611 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013612 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013614 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13615 expr = &argvars[2];
13616 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013617 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013619 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013620 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13621 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013622 rettv->vval.v_string = NULL;
13623 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013624 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013625}
13626
13627/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013628 * "swapinfo(swap_filename)" function
13629 */
13630 static void
13631f_swapinfo(typval_T *argvars, typval_T *rettv)
13632{
13633 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013634 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013635}
13636
13637/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013638 * "swapname(expr)" function
13639 */
13640 static void
13641f_swapname(typval_T *argvars, typval_T *rettv)
13642{
13643 buf_T *buf;
13644
13645 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013646 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013647 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13648 || buf->b_ml.ml_mfp->mf_fname == NULL)
13649 rettv->vval.v_string = NULL;
13650 else
13651 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13652}
13653
13654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013655 * "synID(lnum, col, trans)" function
13656 */
13657 static void
13658f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13659{
13660 int id = 0;
13661#ifdef FEAT_SYN_HL
13662 linenr_T lnum;
13663 colnr_T col;
13664 int trans;
13665 int transerr = FALSE;
13666
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013667 lnum = tv_get_lnum(argvars); /* -1 on type error */
13668 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13669 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013670
13671 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13672 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13673 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13674#endif
13675
13676 rettv->vval.v_number = id;
13677}
13678
13679/*
13680 * "synIDattr(id, what [, mode])" function
13681 */
13682 static void
13683f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13684{
13685 char_u *p = NULL;
13686#ifdef FEAT_SYN_HL
13687 int id;
13688 char_u *what;
13689 char_u *mode;
13690 char_u modebuf[NUMBUFLEN];
13691 int modec;
13692
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013693 id = (int)tv_get_number(&argvars[0]);
13694 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695 if (argvars[2].v_type != VAR_UNKNOWN)
13696 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013697 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698 modec = TOLOWER_ASC(mode[0]);
13699 if (modec != 't' && modec != 'c' && modec != 'g')
13700 modec = 0; /* replace invalid with current */
13701 }
13702 else
13703 {
13704#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13705 if (USE_24BIT)
13706 modec = 'g';
13707 else
13708#endif
13709 if (t_colors > 1)
13710 modec = 'c';
13711 else
13712 modec = 't';
13713 }
13714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013715 switch (TOLOWER_ASC(what[0]))
13716 {
13717 case 'b':
13718 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13719 p = highlight_color(id, what, modec);
13720 else /* bold */
13721 p = highlight_has_attr(id, HL_BOLD, modec);
13722 break;
13723
13724 case 'f': /* fg[#] or font */
13725 p = highlight_color(id, what, modec);
13726 break;
13727
13728 case 'i':
13729 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13730 p = highlight_has_attr(id, HL_INVERSE, modec);
13731 else /* italic */
13732 p = highlight_has_attr(id, HL_ITALIC, modec);
13733 break;
13734
13735 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013736 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013737 break;
13738
13739 case 'r': /* reverse */
13740 p = highlight_has_attr(id, HL_INVERSE, modec);
13741 break;
13742
13743 case 's':
13744 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13745 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013746 /* strikeout */
13747 else if (TOLOWER_ASC(what[1]) == 't' &&
13748 TOLOWER_ASC(what[2]) == 'r')
13749 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013750 else /* standout */
13751 p = highlight_has_attr(id, HL_STANDOUT, modec);
13752 break;
13753
13754 case 'u':
13755 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13756 /* underline */
13757 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13758 else
13759 /* undercurl */
13760 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13761 break;
13762 }
13763
13764 if (p != NULL)
13765 p = vim_strsave(p);
13766#endif
13767 rettv->v_type = VAR_STRING;
13768 rettv->vval.v_string = p;
13769}
13770
13771/*
13772 * "synIDtrans(id)" function
13773 */
13774 static void
13775f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13776{
13777 int id;
13778
13779#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013780 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013781
13782 if (id > 0)
13783 id = syn_get_final_id(id);
13784 else
13785#endif
13786 id = 0;
13787
13788 rettv->vval.v_number = id;
13789}
13790
13791/*
13792 * "synconcealed(lnum, col)" function
13793 */
13794 static void
13795f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13796{
13797#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13798 linenr_T lnum;
13799 colnr_T col;
13800 int syntax_flags = 0;
13801 int cchar;
13802 int matchid = 0;
13803 char_u str[NUMBUFLEN];
13804#endif
13805
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013806 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013807
13808#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013809 lnum = tv_get_lnum(argvars); /* -1 on type error */
13810 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013811
13812 vim_memset(str, NUL, sizeof(str));
13813
13814 if (rettv_list_alloc(rettv) != FAIL)
13815 {
13816 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13817 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13818 && curwin->w_p_cole > 0)
13819 {
13820 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13821 syntax_flags = get_syntax_info(&matchid);
13822
13823 /* get the conceal character */
13824 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13825 {
13826 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013827 if (cchar == NUL && curwin->w_p_cole == 1)
13828 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 if (cchar != NUL)
13830 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013831 if (has_mbyte)
13832 (*mb_char2bytes)(cchar, str);
13833 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013834 str[0] = cchar;
13835 }
13836 }
13837 }
13838
13839 list_append_number(rettv->vval.v_list,
13840 (syntax_flags & HL_CONCEAL) != 0);
13841 /* -1 to auto-determine strlen */
13842 list_append_string(rettv->vval.v_list, str, -1);
13843 list_append_number(rettv->vval.v_list, matchid);
13844 }
13845#endif
13846}
13847
13848/*
13849 * "synstack(lnum, col)" function
13850 */
13851 static void
13852f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13853{
13854#ifdef FEAT_SYN_HL
13855 linenr_T lnum;
13856 colnr_T col;
13857 int i;
13858 int id;
13859#endif
13860
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013861 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013862
13863#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013864 lnum = tv_get_lnum(argvars); /* -1 on type error */
13865 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866
13867 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13868 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13869 && rettv_list_alloc(rettv) != FAIL)
13870 {
13871 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13872 for (i = 0; ; ++i)
13873 {
13874 id = syn_get_stack_item(i);
13875 if (id < 0)
13876 break;
13877 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13878 break;
13879 }
13880 }
13881#endif
13882}
13883
13884 static void
13885get_cmd_output_as_rettv(
13886 typval_T *argvars,
13887 typval_T *rettv,
13888 int retlist)
13889{
13890 char_u *res = NULL;
13891 char_u *p;
13892 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013893 int err = FALSE;
13894 FILE *fd;
13895 list_T *list = NULL;
13896 int flags = SHELL_SILENT;
13897
13898 rettv->v_type = VAR_STRING;
13899 rettv->vval.v_string = NULL;
13900 if (check_restricted() || check_secure())
13901 goto errret;
13902
13903 if (argvars[1].v_type != VAR_UNKNOWN)
13904 {
13905 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013906 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013907 * command.
13908 */
13909 if ((infile = vim_tempname('i', TRUE)) == NULL)
13910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013911 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013912 goto errret;
13913 }
13914
13915 fd = mch_fopen((char *)infile, WRITEBIN);
13916 if (fd == NULL)
13917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013918 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013919 goto errret;
13920 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013921 if (argvars[1].v_type == VAR_NUMBER)
13922 {
13923 linenr_T lnum;
13924 buf_T *buf;
13925
13926 buf = buflist_findnr(argvars[1].vval.v_number);
13927 if (buf == NULL)
13928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013929 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013930 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013931 goto errret;
13932 }
13933
13934 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13935 {
13936 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13937 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13938 {
13939 err = TRUE;
13940 break;
13941 }
13942 if (putc(NL, fd) == EOF)
13943 {
13944 err = TRUE;
13945 break;
13946 }
13947 }
13948 }
13949 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013950 {
13951 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13952 err = TRUE;
13953 }
13954 else
13955 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013956 size_t len;
13957 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013958
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013959 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013960 if (p == NULL)
13961 {
13962 fclose(fd);
13963 goto errret; /* type error; errmsg already given */
13964 }
13965 len = STRLEN(p);
13966 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13967 err = TRUE;
13968 }
13969 if (fclose(fd) != 0)
13970 err = TRUE;
13971 if (err)
13972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013973 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013974 goto errret;
13975 }
13976 }
13977
13978 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13979 * echoes typeahead, that messes up the display. */
13980 if (!msg_silent)
13981 flags += SHELL_COOKED;
13982
13983 if (retlist)
13984 {
13985 int len;
13986 listitem_T *li;
13987 char_u *s = NULL;
13988 char_u *start;
13989 char_u *end;
13990 int i;
13991
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013992 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013993 if (res == NULL)
13994 goto errret;
13995
13996 list = list_alloc();
13997 if (list == NULL)
13998 goto errret;
13999
14000 for (i = 0; i < len; ++i)
14001 {
14002 start = res + i;
14003 while (i < len && res[i] != NL)
14004 ++i;
14005 end = res + i;
14006
Bram Moolenaar964b3742019-05-24 18:54:09 +020014007 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014008 if (s == NULL)
14009 goto errret;
14010
14011 for (p = s; start < end; ++p, ++start)
14012 *p = *start == NUL ? NL : *start;
14013 *p = NUL;
14014
14015 li = listitem_alloc();
14016 if (li == NULL)
14017 {
14018 vim_free(s);
14019 goto errret;
14020 }
14021 li->li_tv.v_type = VAR_STRING;
14022 li->li_tv.v_lock = 0;
14023 li->li_tv.vval.v_string = s;
14024 list_append(list, li);
14025 }
14026
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014027 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014028 list = NULL;
14029 }
14030 else
14031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014032 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014033#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014034 /* translate <CR><NL> into <NL> */
14035 if (res != NULL)
14036 {
14037 char_u *s, *d;
14038
14039 d = res;
14040 for (s = res; *s; ++s)
14041 {
14042 if (s[0] == CAR && s[1] == NL)
14043 ++s;
14044 *d++ = *s;
14045 }
14046 *d = NUL;
14047 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014048#endif
14049 rettv->vval.v_string = res;
14050 res = NULL;
14051 }
14052
14053errret:
14054 if (infile != NULL)
14055 {
14056 mch_remove(infile);
14057 vim_free(infile);
14058 }
14059 if (res != NULL)
14060 vim_free(res);
14061 if (list != NULL)
14062 list_free(list);
14063}
14064
14065/*
14066 * "system()" function
14067 */
14068 static void
14069f_system(typval_T *argvars, typval_T *rettv)
14070{
14071 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14072}
14073
14074/*
14075 * "systemlist()" function
14076 */
14077 static void
14078f_systemlist(typval_T *argvars, typval_T *rettv)
14079{
14080 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14081}
14082
14083/*
14084 * "tabpagebuflist()" function
14085 */
14086 static void
14087f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14088{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014089 tabpage_T *tp;
14090 win_T *wp = NULL;
14091
14092 if (argvars[0].v_type == VAR_UNKNOWN)
14093 wp = firstwin;
14094 else
14095 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014096 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014097 if (tp != NULL)
14098 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14099 }
14100 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14101 {
14102 for (; wp != NULL; wp = wp->w_next)
14103 if (list_append_number(rettv->vval.v_list,
14104 wp->w_buffer->b_fnum) == FAIL)
14105 break;
14106 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014107}
14108
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109/*
14110 * "tabpagenr()" function
14111 */
14112 static void
14113f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14114{
14115 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116 char_u *arg;
14117
14118 if (argvars[0].v_type != VAR_UNKNOWN)
14119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014120 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014121 nr = 0;
14122 if (arg != NULL)
14123 {
14124 if (STRCMP(arg, "$") == 0)
14125 nr = tabpage_index(NULL) - 1;
14126 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014127 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014128 }
14129 }
14130 else
14131 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014132 rettv->vval.v_number = nr;
14133}
14134
14135
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014136/*
14137 * Common code for tabpagewinnr() and winnr().
14138 */
14139 static int
14140get_winnr(tabpage_T *tp, typval_T *argvar)
14141{
14142 win_T *twin;
14143 int nr = 1;
14144 win_T *wp;
14145 char_u *arg;
14146
14147 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14148 if (argvar->v_type != VAR_UNKNOWN)
14149 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014150 int invalid_arg = FALSE;
14151
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014152 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014153 if (arg == NULL)
14154 nr = 0; /* type error; errmsg already given */
14155 else if (STRCMP(arg, "$") == 0)
14156 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14157 else if (STRCMP(arg, "#") == 0)
14158 {
14159 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14160 if (twin == NULL)
14161 nr = 0;
14162 }
14163 else
14164 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014165 long count;
14166 char_u *endp;
14167
14168 // Extract the window count (if specified). e.g. winnr('3j')
14169 count = strtol((char *)arg, (char **)&endp, 10);
14170 if (count <= 0)
14171 count = 1; // if count is not specified, default to 1
14172 if (endp != NULL && *endp != '\0')
14173 {
14174 if (STRCMP(endp, "j") == 0)
14175 twin = win_vert_neighbor(tp, twin, FALSE, count);
14176 else if (STRCMP(endp, "k") == 0)
14177 twin = win_vert_neighbor(tp, twin, TRUE, count);
14178 else if (STRCMP(endp, "h") == 0)
14179 twin = win_horz_neighbor(tp, twin, TRUE, count);
14180 else if (STRCMP(endp, "l") == 0)
14181 twin = win_horz_neighbor(tp, twin, FALSE, count);
14182 else
14183 invalid_arg = TRUE;
14184 }
14185 else
14186 invalid_arg = TRUE;
14187 }
14188
14189 if (invalid_arg)
14190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014191 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 nr = 0;
14193 }
14194 }
14195
14196 if (nr > 0)
14197 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14198 wp != twin; wp = wp->w_next)
14199 {
14200 if (wp == NULL)
14201 {
14202 /* didn't find it in this tabpage */
14203 nr = 0;
14204 break;
14205 }
14206 ++nr;
14207 }
14208 return nr;
14209}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014210
14211/*
14212 * "tabpagewinnr()" function
14213 */
14214 static void
14215f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14216{
14217 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218 tabpage_T *tp;
14219
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014220 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014221 if (tp == NULL)
14222 nr = 0;
14223 else
14224 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014225 rettv->vval.v_number = nr;
14226}
14227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014228/*
14229 * "tagfiles()" function
14230 */
14231 static void
14232f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14233{
14234 char_u *fname;
14235 tagname_T tn;
14236 int first;
14237
14238 if (rettv_list_alloc(rettv) == FAIL)
14239 return;
14240 fname = alloc(MAXPATHL);
14241 if (fname == NULL)
14242 return;
14243
14244 for (first = TRUE; ; first = FALSE)
14245 if (get_tagfname(&tn, first, fname) == FAIL
14246 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14247 break;
14248 tagname_free(&tn);
14249 vim_free(fname);
14250}
14251
14252/*
14253 * "taglist()" function
14254 */
14255 static void
14256f_taglist(typval_T *argvars, typval_T *rettv)
14257{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014258 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014259 char_u *tag_pattern;
14260
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014261 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014262
14263 rettv->vval.v_number = FALSE;
14264 if (*tag_pattern == NUL)
14265 return;
14266
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014267 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014268 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014269 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014270 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014271}
14272
14273/*
14274 * "tempname()" function
14275 */
14276 static void
14277f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14278{
14279 static int x = 'A';
14280
14281 rettv->v_type = VAR_STRING;
14282 rettv->vval.v_string = vim_tempname(x, FALSE);
14283
14284 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14285 * names. Skip 'I' and 'O', they are used for shell redirection. */
14286 do
14287 {
14288 if (x == 'Z')
14289 x = '0';
14290 else if (x == '9')
14291 x = 'A';
14292 else
14293 {
14294#ifdef EBCDIC
14295 if (x == 'I')
14296 x = 'J';
14297 else if (x == 'R')
14298 x = 'S';
14299 else
14300#endif
14301 ++x;
14302 }
14303 } while (x == 'I' || x == 'O');
14304}
14305
14306#ifdef FEAT_FLOAT
14307/*
14308 * "tan()" function
14309 */
14310 static void
14311f_tan(typval_T *argvars, typval_T *rettv)
14312{
14313 float_T f = 0.0;
14314
14315 rettv->v_type = VAR_FLOAT;
14316 if (get_float_arg(argvars, &f) == OK)
14317 rettv->vval.v_float = tan(f);
14318 else
14319 rettv->vval.v_float = 0.0;
14320}
14321
14322/*
14323 * "tanh()" function
14324 */
14325 static void
14326f_tanh(typval_T *argvars, typval_T *rettv)
14327{
14328 float_T f = 0.0;
14329
14330 rettv->v_type = VAR_FLOAT;
14331 if (get_float_arg(argvars, &f) == OK)
14332 rettv->vval.v_float = tanh(f);
14333 else
14334 rettv->vval.v_float = 0.0;
14335}
14336#endif
14337
14338/*
14339 * "test_alloc_fail(id, countdown, repeat)" function
14340 */
14341 static void
14342f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14343{
14344 if (argvars[0].v_type != VAR_NUMBER
14345 || argvars[0].vval.v_number <= 0
14346 || argvars[1].v_type != VAR_NUMBER
14347 || argvars[1].vval.v_number < 0
14348 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014350 else
14351 {
14352 alloc_fail_id = argvars[0].vval.v_number;
14353 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014354 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014355 alloc_fail_countdown = argvars[1].vval.v_number;
14356 alloc_fail_repeat = argvars[2].vval.v_number;
14357 did_outofmem_msg = FALSE;
14358 }
14359}
14360
14361/*
14362 * "test_autochdir()"
14363 */
14364 static void
14365f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14366{
14367#if defined(FEAT_AUTOCHDIR)
14368 test_autochdir = TRUE;
14369#endif
14370}
14371
14372/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014373 * "test_feedinput()"
14374 */
14375 static void
14376f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14377{
14378#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014379 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014380
14381 if (val != NULL)
14382 {
14383 trash_input_buf();
14384 add_to_input_buf_csi(val, (int)STRLEN(val));
14385 }
14386#endif
14387}
14388
14389/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014390 * "test_getvalue({name})" function
14391 */
14392 static void
14393f_test_getvalue(typval_T *argvars, typval_T *rettv)
14394{
14395 if (argvars[0].v_type != VAR_STRING)
14396 emsg(_(e_invarg));
14397 else
14398 {
14399 char_u *name = tv_get_string(&argvars[0]);
14400
14401 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14402 rettv->vval.v_number = need_fileinfo;
14403 else
14404 semsg(_(e_invarg2), name);
14405 }
14406}
14407
14408/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014409 * "test_option_not_set({name})" function
14410 */
14411 static void
14412f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14413{
14414 char_u *name = (char_u *)"";
14415
14416 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014417 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014418 else
14419 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014420 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014421 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014422 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014423 }
14424}
14425
14426/*
14427 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014428 */
14429 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014430f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014431{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014432 char_u *name = (char_u *)"";
14433 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014434 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014435
14436 if (argvars[0].v_type != VAR_STRING
14437 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014438 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014439 else
14440 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014441 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014442 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014443
14444 if (STRCMP(name, (char_u *)"redraw") == 0)
14445 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014446 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14447 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014448 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14449 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014450 else if (STRCMP(name, (char_u *)"starting") == 0)
14451 {
14452 if (val)
14453 {
14454 if (save_starting < 0)
14455 save_starting = starting;
14456 starting = 0;
14457 }
14458 else
14459 {
14460 starting = save_starting;
14461 save_starting = -1;
14462 }
14463 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014464 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14465 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014466 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14467 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014468 else if (STRCMP(name, (char_u *)"ALL") == 0)
14469 {
14470 disable_char_avail_for_testing = FALSE;
14471 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014472 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014473 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014474 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014475 if (save_starting >= 0)
14476 {
14477 starting = save_starting;
14478 save_starting = -1;
14479 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014480 }
14481 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014482 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014483 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014484}
14485
14486/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014487 * "test_refcount({expr})" function
14488 */
14489 static void
14490f_test_refcount(typval_T *argvars, typval_T *rettv)
14491{
14492 int retval = -1;
14493
14494 switch (argvars[0].v_type)
14495 {
14496 case VAR_UNKNOWN:
14497 case VAR_NUMBER:
14498 case VAR_FLOAT:
14499 case VAR_SPECIAL:
14500 case VAR_STRING:
14501 break;
14502 case VAR_JOB:
14503#ifdef FEAT_JOB_CHANNEL
14504 if (argvars[0].vval.v_job != NULL)
14505 retval = argvars[0].vval.v_job->jv_refcount - 1;
14506#endif
14507 break;
14508 case VAR_CHANNEL:
14509#ifdef FEAT_JOB_CHANNEL
14510 if (argvars[0].vval.v_channel != NULL)
14511 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14512#endif
14513 break;
14514 case VAR_FUNC:
14515 if (argvars[0].vval.v_string != NULL)
14516 {
14517 ufunc_T *fp;
14518
14519 fp = find_func(argvars[0].vval.v_string);
14520 if (fp != NULL)
14521 retval = fp->uf_refcount;
14522 }
14523 break;
14524 case VAR_PARTIAL:
14525 if (argvars[0].vval.v_partial != NULL)
14526 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14527 break;
14528 case VAR_BLOB:
14529 if (argvars[0].vval.v_blob != NULL)
14530 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14531 break;
14532 case VAR_LIST:
14533 if (argvars[0].vval.v_list != NULL)
14534 retval = argvars[0].vval.v_list->lv_refcount - 1;
14535 break;
14536 case VAR_DICT:
14537 if (argvars[0].vval.v_dict != NULL)
14538 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14539 break;
14540 }
14541
14542 rettv->v_type = VAR_NUMBER;
14543 rettv->vval.v_number = retval;
14544
14545}
14546
14547/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014548 * "test_garbagecollect_now()" function
14549 */
14550 static void
14551f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14552{
14553 /* This is dangerous, any Lists and Dicts used internally may be freed
14554 * while still in use. */
14555 garbage_collect(TRUE);
14556}
14557
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014558/*
14559 * "test_ignore_error()" function
14560 */
14561 static void
14562f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14563{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014564 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014565}
14566
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014567 static void
14568f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14569{
14570 rettv->v_type = VAR_BLOB;
14571 rettv->vval.v_blob = NULL;
14572}
14573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014574#ifdef FEAT_JOB_CHANNEL
14575 static void
14576f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14577{
14578 rettv->v_type = VAR_CHANNEL;
14579 rettv->vval.v_channel = NULL;
14580}
14581#endif
14582
14583 static void
14584f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14585{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014586 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014587}
14588
14589#ifdef FEAT_JOB_CHANNEL
14590 static void
14591f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14592{
14593 rettv->v_type = VAR_JOB;
14594 rettv->vval.v_job = NULL;
14595}
14596#endif
14597
14598 static void
14599f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14600{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014601 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602}
14603
14604 static void
14605f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14606{
14607 rettv->v_type = VAR_PARTIAL;
14608 rettv->vval.v_partial = NULL;
14609}
14610
14611 static void
14612f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14613{
14614 rettv->v_type = VAR_STRING;
14615 rettv->vval.v_string = NULL;
14616}
14617
Bram Moolenaarab186732018-09-14 21:27:06 +020014618#ifdef FEAT_GUI
14619 static void
14620f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14621{
14622 char_u *which;
14623 long value;
14624 int dragging;
14625 scrollbar_T *sb = NULL;
14626
14627 if (argvars[0].v_type != VAR_STRING
14628 || (argvars[1].v_type) != VAR_NUMBER
14629 || (argvars[2].v_type) != VAR_NUMBER)
14630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014631 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014632 return;
14633 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014634 which = tv_get_string(&argvars[0]);
14635 value = tv_get_number(&argvars[1]);
14636 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014637
14638 if (STRCMP(which, "left") == 0)
14639 sb = &curwin->w_scrollbars[SBAR_LEFT];
14640 else if (STRCMP(which, "right") == 0)
14641 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14642 else if (STRCMP(which, "hor") == 0)
14643 sb = &gui.bottom_sbar;
14644 if (sb == NULL)
14645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014646 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014647 return;
14648 }
14649 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014650# ifndef USE_ON_FLY_SCROLL
14651 // need to loop through normal_cmd() to handle the scroll events
14652 exec_normal(FALSE, TRUE, FALSE);
14653# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014654}
14655#endif
14656
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014657#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014659f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14660{
14661 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14662 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14663}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014664#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014665
14666 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14668{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670}
14671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672/*
14673 * Get a callback from "arg". It can be a Funcref or a function name.
14674 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014675 * "cb_name" is not allocated.
14676 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014678 callback_T
14679get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014680{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014681 callback_T res;
14682
14683 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014684 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14685 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014686 res.cb_partial = arg->vval.v_partial;
14687 ++res.cb_partial->pt_refcount;
14688 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014690 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014691 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014692 res.cb_partial = NULL;
14693 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14694 {
14695 // Note that we don't make a copy of the string.
14696 res.cb_name = arg->vval.v_string;
14697 func_ref(res.cb_name);
14698 }
14699 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14700 {
14701 res.cb_name = (char_u *)"";
14702 }
14703 else
14704 {
14705 emsg(_("E921: Invalid callback argument"));
14706 res.cb_name = NULL;
14707 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014709 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710}
14711
14712/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014713 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 */
14715 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014716put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014718 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014719 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014720 tv->v_type = VAR_PARTIAL;
14721 tv->vval.v_partial = cb->cb_partial;
14722 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014723 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014724 else
14725 {
14726 tv->v_type = VAR_FUNC;
14727 tv->vval.v_string = vim_strsave(cb->cb_name);
14728 func_ref(cb->cb_name);
14729 }
14730}
14731
14732/*
14733 * Make a copy of "src" into "dest", allocating the function name if needed,
14734 * without incrementing the refcount.
14735 */
14736 void
14737set_callback(callback_T *dest, callback_T *src)
14738{
14739 if (src->cb_partial == NULL)
14740 {
14741 // just a function name, make a copy
14742 dest->cb_name = vim_strsave(src->cb_name);
14743 dest->cb_free_name = TRUE;
14744 }
14745 else
14746 {
14747 // cb_name is a pointer into cb_partial
14748 dest->cb_name = src->cb_name;
14749 dest->cb_free_name = FALSE;
14750 }
14751 dest->cb_partial = src->cb_partial;
14752}
14753
14754/*
14755 * Unref/free "callback" returned by get_callback() or set_callback().
14756 */
14757 void
14758free_callback(callback_T *callback)
14759{
14760 if (callback->cb_partial != NULL)
14761 {
14762 partial_unref(callback->cb_partial);
14763 callback->cb_partial = NULL;
14764 }
14765 else if (callback->cb_name != NULL)
14766 func_unref(callback->cb_name);
14767 if (callback->cb_free_name)
14768 {
14769 vim_free(callback->cb_name);
14770 callback->cb_free_name = FALSE;
14771 }
14772 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014773}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014774
14775#ifdef FEAT_TIMERS
14776/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014777 * "timer_info([timer])" function
14778 */
14779 static void
14780f_timer_info(typval_T *argvars, typval_T *rettv)
14781{
14782 timer_T *timer = NULL;
14783
14784 if (rettv_list_alloc(rettv) != OK)
14785 return;
14786 if (argvars[0].v_type != VAR_UNKNOWN)
14787 {
14788 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014789 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014790 else
14791 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014792 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014793 if (timer != NULL)
14794 add_timer_info(rettv, timer);
14795 }
14796 }
14797 else
14798 add_timer_info_all(rettv);
14799}
14800
14801/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014802 * "timer_pause(timer, paused)" function
14803 */
14804 static void
14805f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14806{
14807 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014808 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014809
14810 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014811 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014812 else
14813 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014814 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014815 if (timer != NULL)
14816 timer->tr_paused = paused;
14817 }
14818}
14819
14820/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 * "timer_start(time, callback [, options])" function
14822 */
14823 static void
14824f_timer_start(typval_T *argvars, typval_T *rettv)
14825{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014826 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014827 timer_T *timer;
14828 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014829 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014830 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014831
Bram Moolenaar75537a92016-09-05 22:45:28 +020014832 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014833 if (check_secure())
14834 return;
14835 if (argvars[2].v_type != VAR_UNKNOWN)
14836 {
14837 if (argvars[2].v_type != VAR_DICT
14838 || (dict = argvars[2].vval.v_dict) == NULL)
14839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014840 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014841 return;
14842 }
14843 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014844 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014845 }
14846
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014847 callback = get_callback(&argvars[1]);
14848 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014849 return;
14850
14851 timer = create_timer(msec, repeat);
14852 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014853 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014854 else
14855 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014856 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014857 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014858 }
14859}
14860
14861/*
14862 * "timer_stop(timer)" function
14863 */
14864 static void
14865f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14866{
14867 timer_T *timer;
14868
14869 if (argvars[0].v_type != VAR_NUMBER)
14870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014871 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014872 return;
14873 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014874 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014875 if (timer != NULL)
14876 stop_timer(timer);
14877}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014878
14879/*
14880 * "timer_stopall()" function
14881 */
14882 static void
14883f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14884{
14885 stop_all_timers();
14886}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014887#endif
14888
14889/*
14890 * "tolower(string)" function
14891 */
14892 static void
14893f_tolower(typval_T *argvars, typval_T *rettv)
14894{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014895 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014896 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014897}
14898
14899/*
14900 * "toupper(string)" function
14901 */
14902 static void
14903f_toupper(typval_T *argvars, typval_T *rettv)
14904{
14905 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014906 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014907}
14908
14909/*
14910 * "tr(string, fromstr, tostr)" function
14911 */
14912 static void
14913f_tr(typval_T *argvars, typval_T *rettv)
14914{
14915 char_u *in_str;
14916 char_u *fromstr;
14917 char_u *tostr;
14918 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014919 int inlen;
14920 int fromlen;
14921 int tolen;
14922 int idx;
14923 char_u *cpstr;
14924 int cplen;
14925 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014926 char_u buf[NUMBUFLEN];
14927 char_u buf2[NUMBUFLEN];
14928 garray_T ga;
14929
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014930 in_str = tv_get_string(&argvars[0]);
14931 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14932 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014933
14934 /* Default return value: empty string. */
14935 rettv->v_type = VAR_STRING;
14936 rettv->vval.v_string = NULL;
14937 if (fromstr == NULL || tostr == NULL)
14938 return; /* type error; errmsg already given */
14939 ga_init2(&ga, (int)sizeof(char), 80);
14940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014941 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014942 /* not multi-byte: fromstr and tostr must be the same length */
14943 if (STRLEN(fromstr) != STRLEN(tostr))
14944 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014945error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014946 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014947 ga_clear(&ga);
14948 return;
14949 }
14950
14951 /* fromstr and tostr have to contain the same number of chars */
14952 while (*in_str != NUL)
14953 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014954 if (has_mbyte)
14955 {
14956 inlen = (*mb_ptr2len)(in_str);
14957 cpstr = in_str;
14958 cplen = inlen;
14959 idx = 0;
14960 for (p = fromstr; *p != NUL; p += fromlen)
14961 {
14962 fromlen = (*mb_ptr2len)(p);
14963 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14964 {
14965 for (p = tostr; *p != NUL; p += tolen)
14966 {
14967 tolen = (*mb_ptr2len)(p);
14968 if (idx-- == 0)
14969 {
14970 cplen = tolen;
14971 cpstr = p;
14972 break;
14973 }
14974 }
14975 if (*p == NUL) /* tostr is shorter than fromstr */
14976 goto error;
14977 break;
14978 }
14979 ++idx;
14980 }
14981
14982 if (first && cpstr == in_str)
14983 {
14984 /* Check that fromstr and tostr have the same number of
14985 * (multi-byte) characters. Done only once when a character
14986 * of in_str doesn't appear in fromstr. */
14987 first = FALSE;
14988 for (p = tostr; *p != NUL; p += tolen)
14989 {
14990 tolen = (*mb_ptr2len)(p);
14991 --idx;
14992 }
14993 if (idx != 0)
14994 goto error;
14995 }
14996
14997 (void)ga_grow(&ga, cplen);
14998 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14999 ga.ga_len += cplen;
15000
15001 in_str += inlen;
15002 }
15003 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015004 {
15005 /* When not using multi-byte chars we can do it faster. */
15006 p = vim_strchr(fromstr, *in_str);
15007 if (p != NULL)
15008 ga_append(&ga, tostr[p - fromstr]);
15009 else
15010 ga_append(&ga, *in_str);
15011 ++in_str;
15012 }
15013 }
15014
15015 /* add a terminating NUL */
15016 (void)ga_grow(&ga, 1);
15017 ga_append(&ga, NUL);
15018
15019 rettv->vval.v_string = ga.ga_data;
15020}
15021
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015022/*
15023 * "trim({expr})" function
15024 */
15025 static void
15026f_trim(typval_T *argvars, typval_T *rettv)
15027{
15028 char_u buf1[NUMBUFLEN];
15029 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015030 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015031 char_u *mask = NULL;
15032 char_u *tail;
15033 char_u *prev;
15034 char_u *p;
15035 int c1;
15036
15037 rettv->v_type = VAR_STRING;
15038 if (head == NULL)
15039 {
15040 rettv->vval.v_string = NULL;
15041 return;
15042 }
15043
15044 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015045 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015046
15047 while (*head != NUL)
15048 {
15049 c1 = PTR2CHAR(head);
15050 if (mask == NULL)
15051 {
15052 if (c1 > ' ' && c1 != 0xa0)
15053 break;
15054 }
15055 else
15056 {
15057 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15058 if (c1 == PTR2CHAR(p))
15059 break;
15060 if (*p == NUL)
15061 break;
15062 }
15063 MB_PTR_ADV(head);
15064 }
15065
15066 for (tail = head + STRLEN(head); tail > head; tail = prev)
15067 {
15068 prev = tail;
15069 MB_PTR_BACK(head, prev);
15070 c1 = PTR2CHAR(prev);
15071 if (mask == NULL)
15072 {
15073 if (c1 > ' ' && c1 != 0xa0)
15074 break;
15075 }
15076 else
15077 {
15078 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15079 if (c1 == PTR2CHAR(p))
15080 break;
15081 if (*p == NUL)
15082 break;
15083 }
15084 }
15085 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15086}
15087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015088#ifdef FEAT_FLOAT
15089/*
15090 * "trunc({float})" function
15091 */
15092 static void
15093f_trunc(typval_T *argvars, typval_T *rettv)
15094{
15095 float_T f = 0.0;
15096
15097 rettv->v_type = VAR_FLOAT;
15098 if (get_float_arg(argvars, &f) == OK)
15099 /* trunc() is not in C90, use floor() or ceil() instead. */
15100 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15101 else
15102 rettv->vval.v_float = 0.0;
15103}
15104#endif
15105
15106/*
15107 * "type(expr)" function
15108 */
15109 static void
15110f_type(typval_T *argvars, typval_T *rettv)
15111{
15112 int n = -1;
15113
15114 switch (argvars[0].v_type)
15115 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015116 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15117 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015118 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015119 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15120 case VAR_LIST: n = VAR_TYPE_LIST; break;
15121 case VAR_DICT: n = VAR_TYPE_DICT; break;
15122 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015123 case VAR_SPECIAL:
15124 if (argvars[0].vval.v_number == VVAL_FALSE
15125 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015126 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015127 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015128 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015129 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015130 case VAR_JOB: n = VAR_TYPE_JOB; break;
15131 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015132 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015133 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015134 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015135 n = -1;
15136 break;
15137 }
15138 rettv->vval.v_number = n;
15139}
15140
15141/*
15142 * "undofile(name)" function
15143 */
15144 static void
15145f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15146{
15147 rettv->v_type = VAR_STRING;
15148#ifdef FEAT_PERSISTENT_UNDO
15149 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015150 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015151
15152 if (*fname == NUL)
15153 {
15154 /* If there is no file name there will be no undo file. */
15155 rettv->vval.v_string = NULL;
15156 }
15157 else
15158 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015159 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015160
15161 if (ffname != NULL)
15162 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15163 vim_free(ffname);
15164 }
15165 }
15166#else
15167 rettv->vval.v_string = NULL;
15168#endif
15169}
15170
15171/*
15172 * "undotree()" function
15173 */
15174 static void
15175f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15176{
15177 if (rettv_dict_alloc(rettv) == OK)
15178 {
15179 dict_T *dict = rettv->vval.v_dict;
15180 list_T *list;
15181
Bram Moolenaare0be1672018-07-08 16:50:37 +020015182 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15183 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15184 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15185 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15186 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15187 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015188
15189 list = list_alloc();
15190 if (list != NULL)
15191 {
15192 u_eval_tree(curbuf->b_u_oldhead, list);
15193 dict_add_list(dict, "entries", list);
15194 }
15195 }
15196}
15197
15198/*
15199 * "values(dict)" function
15200 */
15201 static void
15202f_values(typval_T *argvars, typval_T *rettv)
15203{
15204 dict_list(argvars, rettv, 1);
15205}
15206
15207/*
15208 * "virtcol(string)" function
15209 */
15210 static void
15211f_virtcol(typval_T *argvars, typval_T *rettv)
15212{
15213 colnr_T vcol = 0;
15214 pos_T *fp;
15215 int fnum = curbuf->b_fnum;
15216
15217 fp = var2fpos(&argvars[0], FALSE, &fnum);
15218 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15219 && fnum == curbuf->b_fnum)
15220 {
15221 getvvcol(curwin, fp, NULL, NULL, &vcol);
15222 ++vcol;
15223 }
15224
15225 rettv->vval.v_number = vcol;
15226}
15227
15228/*
15229 * "visualmode()" function
15230 */
15231 static void
15232f_visualmode(typval_T *argvars, typval_T *rettv)
15233{
15234 char_u str[2];
15235
15236 rettv->v_type = VAR_STRING;
15237 str[0] = curbuf->b_visual_mode_eval;
15238 str[1] = NUL;
15239 rettv->vval.v_string = vim_strsave(str);
15240
15241 /* A non-zero number or non-empty string argument: reset mode. */
15242 if (non_zero_arg(&argvars[0]))
15243 curbuf->b_visual_mode_eval = NUL;
15244}
15245
15246/*
15247 * "wildmenumode()" function
15248 */
15249 static void
15250f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15251{
15252#ifdef FEAT_WILDMENU
15253 if (wild_menu_showing)
15254 rettv->vval.v_number = 1;
15255#endif
15256}
15257
15258/*
15259 * "winbufnr(nr)" function
15260 */
15261 static void
15262f_winbufnr(typval_T *argvars, typval_T *rettv)
15263{
15264 win_T *wp;
15265
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015266 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015267 if (wp == NULL)
15268 rettv->vval.v_number = -1;
15269 else
15270 rettv->vval.v_number = wp->w_buffer->b_fnum;
15271}
15272
15273/*
15274 * "wincol()" function
15275 */
15276 static void
15277f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15278{
15279 validate_cursor();
15280 rettv->vval.v_number = curwin->w_wcol + 1;
15281}
15282
15283/*
15284 * "winheight(nr)" function
15285 */
15286 static void
15287f_winheight(typval_T *argvars, typval_T *rettv)
15288{
15289 win_T *wp;
15290
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015291 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015292 if (wp == NULL)
15293 rettv->vval.v_number = -1;
15294 else
15295 rettv->vval.v_number = wp->w_height;
15296}
15297
15298/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015299 * "winlayout()" function
15300 */
15301 static void
15302f_winlayout(typval_T *argvars, typval_T *rettv)
15303{
15304 tabpage_T *tp;
15305
15306 if (rettv_list_alloc(rettv) != OK)
15307 return;
15308
15309 if (argvars[0].v_type == VAR_UNKNOWN)
15310 tp = curtab;
15311 else
15312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015313 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015314 if (tp == NULL)
15315 return;
15316 }
15317
15318 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15319}
15320
15321/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015322 * "winline()" function
15323 */
15324 static void
15325f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15326{
15327 validate_cursor();
15328 rettv->vval.v_number = curwin->w_wrow + 1;
15329}
15330
15331/*
15332 * "winnr()" function
15333 */
15334 static void
15335f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15336{
15337 int nr = 1;
15338
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015339 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015340 rettv->vval.v_number = nr;
15341}
15342
15343/*
15344 * "winrestcmd()" function
15345 */
15346 static void
15347f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15348{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015349 win_T *wp;
15350 int winnr = 1;
15351 garray_T ga;
15352 char_u buf[50];
15353
15354 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015355 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356 {
15357 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15358 ga_concat(&ga, buf);
15359 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15360 ga_concat(&ga, buf);
15361 ++winnr;
15362 }
15363 ga_append(&ga, NUL);
15364
15365 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015366 rettv->v_type = VAR_STRING;
15367}
15368
15369/*
15370 * "winrestview()" function
15371 */
15372 static void
15373f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15374{
15375 dict_T *dict;
15376
15377 if (argvars[0].v_type != VAR_DICT
15378 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015379 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015380 else
15381 {
15382 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015383 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015384 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015385 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015387 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015388 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15389 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015390 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391 curwin->w_set_curswant = FALSE;
15392 }
15393
15394 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015395 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015396#ifdef FEAT_DIFF
15397 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015398 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015399#endif
15400 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015401 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015402 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015403 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015404
15405 check_cursor();
15406 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015407 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015408 changed_window_setting();
15409
15410 if (curwin->w_topline <= 0)
15411 curwin->w_topline = 1;
15412 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15413 curwin->w_topline = curbuf->b_ml.ml_line_count;
15414#ifdef FEAT_DIFF
15415 check_topfill(curwin, TRUE);
15416#endif
15417 }
15418}
15419
15420/*
15421 * "winsaveview()" function
15422 */
15423 static void
15424f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15425{
15426 dict_T *dict;
15427
15428 if (rettv_dict_alloc(rettv) == FAIL)
15429 return;
15430 dict = rettv->vval.v_dict;
15431
Bram Moolenaare0be1672018-07-08 16:50:37 +020015432 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15433 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015434 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015435 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015436 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015437
Bram Moolenaare0be1672018-07-08 16:50:37 +020015438 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015439#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015440 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015441#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015442 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15443 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015444}
15445
15446/*
15447 * "winwidth(nr)" function
15448 */
15449 static void
15450f_winwidth(typval_T *argvars, typval_T *rettv)
15451{
15452 win_T *wp;
15453
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015454 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015455 if (wp == NULL)
15456 rettv->vval.v_number = -1;
15457 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015458 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015459}
15460
15461/*
15462 * "wordcount()" function
15463 */
15464 static void
15465f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15466{
15467 if (rettv_dict_alloc(rettv) == FAIL)
15468 return;
15469 cursor_pos_info(rettv->vval.v_dict);
15470}
15471
15472/*
15473 * "writefile()" function
15474 */
15475 static void
15476f_writefile(typval_T *argvars, typval_T *rettv)
15477{
15478 int binary = FALSE;
15479 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015480#ifdef HAVE_FSYNC
15481 int do_fsync = p_fs;
15482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015483 char_u *fname;
15484 FILE *fd;
15485 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015486 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015487 list_T *list = NULL;
15488 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015489
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015490 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015491 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015492 return;
15493
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015494 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015495 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015496 list = argvars[0].vval.v_list;
15497 if (list == NULL)
15498 return;
15499 for (li = list->lv_first; li != NULL; li = li->li_next)
15500 if (tv_get_string_chk(&li->li_tv) == NULL)
15501 return;
15502 }
15503 else if (argvars[0].v_type == VAR_BLOB)
15504 {
15505 blob = argvars[0].vval.v_blob;
15506 if (blob == NULL)
15507 return;
15508 }
15509 else
15510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015511 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015512 return;
15513 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015514
15515 if (argvars[2].v_type != VAR_UNKNOWN)
15516 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015517 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015518
15519 if (arg2 == NULL)
15520 return;
15521 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015522 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015523 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015524 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015525#ifdef HAVE_FSYNC
15526 if (vim_strchr(arg2, 's') != NULL)
15527 do_fsync = TRUE;
15528 else if (vim_strchr(arg2, 'S') != NULL)
15529 do_fsync = FALSE;
15530#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015531 }
15532
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015533 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015534 if (fname == NULL)
15535 return;
15536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015537 /* Always open the file in binary mode, library functions have a mind of
15538 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015539 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15540 append ? APPENDBIN : WRITEBIN)) == NULL)
15541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015542 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015543 ret = -1;
15544 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015545 else if (blob)
15546 {
15547 if (write_blob(fd, blob) == FAIL)
15548 ret = -1;
15549#ifdef HAVE_FSYNC
15550 else if (do_fsync)
15551 // Ignore the error, the user wouldn't know what to do about it.
15552 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015553 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015554#endif
15555 fclose(fd);
15556 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015557 else
15558 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015559 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015560 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015561#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015562 else if (do_fsync)
15563 /* Ignore the error, the user wouldn't know what to do about it.
15564 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015565 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015567 fclose(fd);
15568 }
15569
15570 rettv->vval.v_number = ret;
15571}
15572
15573/*
15574 * "xor(expr, expr)" function
15575 */
15576 static void
15577f_xor(typval_T *argvars, typval_T *rettv)
15578{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015579 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15580 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015581}
15582
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015583#endif /* FEAT_EVAL */