blob: 48b848d01283592063a1f7b6c0c06e8bf9f22905 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200152static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_extend(typval_T *argvars, typval_T *rettv);
154static void f_feedkeys(typval_T *argvars, typval_T *rettv);
155static void f_filereadable(typval_T *argvars, typval_T *rettv);
156static void f_filewritable(typval_T *argvars, typval_T *rettv);
157static void f_filter(typval_T *argvars, typval_T *rettv);
158static void f_finddir(typval_T *argvars, typval_T *rettv);
159static void f_findfile(typval_T *argvars, typval_T *rettv);
160#ifdef FEAT_FLOAT
161static void f_float2nr(typval_T *argvars, typval_T *rettv);
162static void f_floor(typval_T *argvars, typval_T *rettv);
163static void f_fmod(typval_T *argvars, typval_T *rettv);
164#endif
165static void f_fnameescape(typval_T *argvars, typval_T *rettv);
166static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
167static void f_foldclosed(typval_T *argvars, typval_T *rettv);
168static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
169static void f_foldlevel(typval_T *argvars, typval_T *rettv);
170static void f_foldtext(typval_T *argvars, typval_T *rettv);
171static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
172static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200173static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_function(typval_T *argvars, typval_T *rettv);
175static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
176static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200177static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getbufline(typval_T *argvars, typval_T *rettv);
179static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100180static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getchar(typval_T *argvars, typval_T *rettv);
182static void f_getcharmod(typval_T *argvars, typval_T *rettv);
183static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
184static void f_getcmdline(typval_T *argvars, typval_T *rettv);
185#if defined(FEAT_CMDL_COMPL)
186static void f_getcompletion(typval_T *argvars, typval_T *rettv);
187#endif
188static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
189static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
190static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
191static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getfontname(typval_T *argvars, typval_T *rettv);
194static void f_getfperm(typval_T *argvars, typval_T *rettv);
195static void f_getfsize(typval_T *argvars, typval_T *rettv);
196static void f_getftime(typval_T *argvars, typval_T *rettv);
197static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100198static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200200static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getmatches(typval_T *argvars, typval_T *rettv);
202static void f_getpid(typval_T *argvars, typval_T *rettv);
203static void f_getcurpos(typval_T *argvars, typval_T *rettv);
204static void f_getpos(typval_T *argvars, typval_T *rettv);
205static void f_getqflist(typval_T *argvars, typval_T *rettv);
206static void f_getreg(typval_T *argvars, typval_T *rettv);
207static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_gettabvar(typval_T *argvars, typval_T *rettv);
210static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100211static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200212static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100213static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_getwinposx(typval_T *argvars, typval_T *rettv);
215static void f_getwinposy(typval_T *argvars, typval_T *rettv);
216static void f_getwinvar(typval_T *argvars, typval_T *rettv);
217static void f_glob(typval_T *argvars, typval_T *rettv);
218static void f_globpath(typval_T *argvars, typval_T *rettv);
219static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
220static void f_has(typval_T *argvars, typval_T *rettv);
221static void f_has_key(typval_T *argvars, typval_T *rettv);
222static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
223static void f_hasmapto(typval_T *argvars, typval_T *rettv);
224static void f_histadd(typval_T *argvars, typval_T *rettv);
225static void f_histdel(typval_T *argvars, typval_T *rettv);
226static void f_histget(typval_T *argvars, typval_T *rettv);
227static void f_histnr(typval_T *argvars, typval_T *rettv);
228static void f_hlID(typval_T *argvars, typval_T *rettv);
229static void f_hlexists(typval_T *argvars, typval_T *rettv);
230static void f_hostname(typval_T *argvars, typval_T *rettv);
231static void f_iconv(typval_T *argvars, typval_T *rettv);
232static void f_indent(typval_T *argvars, typval_T *rettv);
233static void f_index(typval_T *argvars, typval_T *rettv);
234static void f_input(typval_T *argvars, typval_T *rettv);
235static void f_inputdialog(typval_T *argvars, typval_T *rettv);
236static void f_inputlist(typval_T *argvars, typval_T *rettv);
237static void f_inputrestore(typval_T *argvars, typval_T *rettv);
238static void f_inputsave(typval_T *argvars, typval_T *rettv);
239static void f_inputsecret(typval_T *argvars, typval_T *rettv);
240static void f_insert(typval_T *argvars, typval_T *rettv);
241static void f_invert(typval_T *argvars, typval_T *rettv);
242static void f_isdirectory(typval_T *argvars, typval_T *rettv);
243static void f_islocked(typval_T *argvars, typval_T *rettv);
244#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200245static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_isnan(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_items(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_JOB_CHANNEL
250static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
251static void f_job_info(typval_T *argvars, typval_T *rettv);
252static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
253static void f_job_start(typval_T *argvars, typval_T *rettv);
254static void f_job_stop(typval_T *argvars, typval_T *rettv);
255static void f_job_status(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_join(typval_T *argvars, typval_T *rettv);
258static void f_js_decode(typval_T *argvars, typval_T *rettv);
259static void f_js_encode(typval_T *argvars, typval_T *rettv);
260static void f_json_decode(typval_T *argvars, typval_T *rettv);
261static void f_json_encode(typval_T *argvars, typval_T *rettv);
262static void f_keys(typval_T *argvars, typval_T *rettv);
263static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
264static void f_len(typval_T *argvars, typval_T *rettv);
265static void f_libcall(typval_T *argvars, typval_T *rettv);
266static void f_libcallnr(typval_T *argvars, typval_T *rettv);
267static void f_line(typval_T *argvars, typval_T *rettv);
268static void f_line2byte(typval_T *argvars, typval_T *rettv);
269static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200270static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_localtime(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_log(typval_T *argvars, typval_T *rettv);
274static void f_log10(typval_T *argvars, typval_T *rettv);
275#endif
276#ifdef FEAT_LUA
277static void f_luaeval(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_map(typval_T *argvars, typval_T *rettv);
280static void f_maparg(typval_T *argvars, typval_T *rettv);
281static void f_mapcheck(typval_T *argvars, typval_T *rettv);
282static void f_match(typval_T *argvars, typval_T *rettv);
283static void f_matchadd(typval_T *argvars, typval_T *rettv);
284static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
285static void f_matcharg(typval_T *argvars, typval_T *rettv);
286static void f_matchdelete(typval_T *argvars, typval_T *rettv);
287static void f_matchend(typval_T *argvars, typval_T *rettv);
288static void f_matchlist(typval_T *argvars, typval_T *rettv);
289static void f_matchstr(typval_T *argvars, typval_T *rettv);
290static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
291static void f_max(typval_T *argvars, typval_T *rettv);
292static void f_min(typval_T *argvars, typval_T *rettv);
293#ifdef vim_mkdir
294static void f_mkdir(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_mode(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_MZSCHEME
298static void f_mzeval(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
301static void f_nr2char(typval_T *argvars, typval_T *rettv);
302static void f_or(typval_T *argvars, typval_T *rettv);
303static void f_pathshorten(typval_T *argvars, typval_T *rettv);
304#ifdef FEAT_PERL
305static void f_perleval(typval_T *argvars, typval_T *rettv);
306#endif
307#ifdef FEAT_FLOAT
308static void f_pow(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
311static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200312#ifdef FEAT_JOB_CHANNEL
313static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200314static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200315static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_pumvisible(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_PYTHON3
319static void f_py3eval(typval_T *argvars, typval_T *rettv);
320#endif
321#ifdef FEAT_PYTHON
322static void f_pyeval(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100324#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
325static void f_pyxeval(typval_T *argvars, typval_T *rettv);
326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200328static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200330static void f_reg_executing(typval_T *argvars, typval_T *rettv);
331static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_reltime(typval_T *argvars, typval_T *rettv);
333#ifdef FEAT_FLOAT
334static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
335#endif
336static void f_reltimestr(typval_T *argvars, typval_T *rettv);
337static void f_remote_expr(typval_T *argvars, typval_T *rettv);
338static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
339static void f_remote_peek(typval_T *argvars, typval_T *rettv);
340static void f_remote_read(typval_T *argvars, typval_T *rettv);
341static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100342static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_remove(typval_T *argvars, typval_T *rettv);
344static void f_rename(typval_T *argvars, typval_T *rettv);
345static void f_repeat(typval_T *argvars, typval_T *rettv);
346static void f_resolve(typval_T *argvars, typval_T *rettv);
347static void f_reverse(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_round(typval_T *argvars, typval_T *rettv);
350#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100351#ifdef FEAT_RUBY
352static void f_rubyeval(typval_T *argvars, typval_T *rettv);
353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_screenattr(typval_T *argvars, typval_T *rettv);
355static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100356static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_screencol(typval_T *argvars, typval_T *rettv);
358static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100359static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360static void f_search(typval_T *argvars, typval_T *rettv);
361static void f_searchdecl(typval_T *argvars, typval_T *rettv);
362static void f_searchpair(typval_T *argvars, typval_T *rettv);
363static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
364static void f_searchpos(typval_T *argvars, typval_T *rettv);
365static void f_server2client(typval_T *argvars, typval_T *rettv);
366static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200367static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_setbufvar(typval_T *argvars, typval_T *rettv);
369static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
370static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200371static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setfperm(typval_T *argvars, typval_T *rettv);
373static void f_setline(typval_T *argvars, typval_T *rettv);
374static void f_setloclist(typval_T *argvars, typval_T *rettv);
375static void f_setmatches(typval_T *argvars, typval_T *rettv);
376static void f_setpos(typval_T *argvars, typval_T *rettv);
377static void f_setqflist(typval_T *argvars, typval_T *rettv);
378static void f_setreg(typval_T *argvars, typval_T *rettv);
379static void f_settabvar(typval_T *argvars, typval_T *rettv);
380static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100381static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200382static void f_setwinvar(typval_T *argvars, typval_T *rettv);
383#ifdef FEAT_CRYPT
384static void f_sha256(typval_T *argvars, typval_T *rettv);
385#endif /* FEAT_CRYPT */
386static void f_shellescape(typval_T *argvars, typval_T *rettv);
387static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388#ifdef FEAT_SIGNS
389static void f_sign_define(typval_T *argvars, typval_T *rettv);
390static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
391static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100392static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100393static void f_sign_place(typval_T *argvars, typval_T *rettv);
394static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
395static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_simplify(typval_T *argvars, typval_T *rettv);
398#ifdef FEAT_FLOAT
399static void f_sin(typval_T *argvars, typval_T *rettv);
400static void f_sinh(typval_T *argvars, typval_T *rettv);
401#endif
402static void f_sort(typval_T *argvars, typval_T *rettv);
403static void f_soundfold(typval_T *argvars, typval_T *rettv);
404static void f_spellbadword(typval_T *argvars, typval_T *rettv);
405static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
406static void f_split(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_sqrt(typval_T *argvars, typval_T *rettv);
409static void f_str2float(typval_T *argvars, typval_T *rettv);
410#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200411static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_str2nr(typval_T *argvars, typval_T *rettv);
413static void f_strchars(typval_T *argvars, typval_T *rettv);
414#ifdef HAVE_STRFTIME
415static void f_strftime(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_strgetchar(typval_T *argvars, typval_T *rettv);
418static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_strlen(typval_T *argvars, typval_T *rettv);
420static void f_strcharpart(typval_T *argvars, typval_T *rettv);
421static void f_strpart(typval_T *argvars, typval_T *rettv);
422static void f_strridx(typval_T *argvars, typval_T *rettv);
423static void f_strtrans(typval_T *argvars, typval_T *rettv);
424static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
425static void f_strwidth(typval_T *argvars, typval_T *rettv);
426static void f_submatch(typval_T *argvars, typval_T *rettv);
427static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200428static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200429static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_synID(typval_T *argvars, typval_T *rettv);
431static void f_synIDattr(typval_T *argvars, typval_T *rettv);
432static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
433static void f_synstack(typval_T *argvars, typval_T *rettv);
434static void f_synconcealed(typval_T *argvars, typval_T *rettv);
435static void f_system(typval_T *argvars, typval_T *rettv);
436static void f_systemlist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
438static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
439static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
440static void f_taglist(typval_T *argvars, typval_T *rettv);
441static void f_tagfiles(typval_T *argvars, typval_T *rettv);
442static void f_tempname(typval_T *argvars, typval_T *rettv);
443static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
444static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200445static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200446static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200447static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100448static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100449static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100451static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100452static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200453#ifdef FEAT_JOB_CHANNEL
454static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
455#endif
456static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
457#ifdef FEAT_JOB_CHANNEL
458static void f_test_null_job(typval_T *argvars, typval_T *rettv);
459#endif
460static void f_test_null_list(typval_T *argvars, typval_T *rettv);
461static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
462static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200463#ifdef FEAT_GUI
464static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
465#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200467static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_test_settime(typval_T *argvars, typval_T *rettv);
470#ifdef FEAT_FLOAT
471static void f_tan(typval_T *argvars, typval_T *rettv);
472static void f_tanh(typval_T *argvars, typval_T *rettv);
473#endif
474#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200475static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200476static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477static void f_timer_start(typval_T *argvars, typval_T *rettv);
478static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200479static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481static void f_tolower(typval_T *argvars, typval_T *rettv);
482static void f_toupper(typval_T *argvars, typval_T *rettv);
483static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100484static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485#ifdef FEAT_FLOAT
486static void f_trunc(typval_T *argvars, typval_T *rettv);
487#endif
488static void f_type(typval_T *argvars, typval_T *rettv);
489static void f_undofile(typval_T *argvars, typval_T *rettv);
490static void f_undotree(typval_T *argvars, typval_T *rettv);
491static void f_uniq(typval_T *argvars, typval_T *rettv);
492static void f_values(typval_T *argvars, typval_T *rettv);
493static void f_virtcol(typval_T *argvars, typval_T *rettv);
494static void f_visualmode(typval_T *argvars, typval_T *rettv);
495static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200496static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
498static void f_win_getid(typval_T *argvars, typval_T *rettv);
499static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
500static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
501static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100502static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503static void f_winbufnr(typval_T *argvars, typval_T *rettv);
504static void f_wincol(typval_T *argvars, typval_T *rettv);
505static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200506static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507static void f_winline(typval_T *argvars, typval_T *rettv);
508static void f_winnr(typval_T *argvars, typval_T *rettv);
509static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
510static void f_winrestview(typval_T *argvars, typval_T *rettv);
511static void f_winsaveview(typval_T *argvars, typval_T *rettv);
512static void f_winwidth(typval_T *argvars, typval_T *rettv);
513static void f_writefile(typval_T *argvars, typval_T *rettv);
514static void f_wordcount(typval_T *argvars, typval_T *rettv);
515static void f_xor(typval_T *argvars, typval_T *rettv);
516
517/*
518 * Array with names and number of arguments of all internal functions
519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
520 */
521static struct fst
522{
523 char *f_name; /* function name */
524 char f_min_argc; /* minimal number of arguments */
525 char f_max_argc; /* maximal number of arguments */
526 void (*f_func)(typval_T *args, typval_T *rvar);
527 /* implementation of function */
528} functions[] =
529{
530#ifdef FEAT_FLOAT
531 {"abs", 1, 1, f_abs},
532 {"acos", 1, 1, f_acos}, /* WJMc */
533#endif
534 {"add", 2, 2, f_add},
535 {"and", 2, 2, f_and},
536 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200537 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"argidx", 0, 0, f_argidx},
540 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200541 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#ifdef FEAT_FLOAT
543 {"asin", 1, 1, f_asin}, /* WJMc */
544#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100545 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100547 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200549 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100551 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"assert_match", 2, 3, f_assert_match},
553 {"assert_notequal", 2, 3, f_assert_notequal},
554 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100555 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"assert_true", 1, 2, f_assert_true},
557#ifdef FEAT_FLOAT
558 {"atan", 1, 1, f_atan},
559 {"atan2", 2, 2, f_atan2},
560#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200562 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100563 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100565 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100566# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200568 {"browse", 4, 4, f_browse},
569 {"browsedir", 2, 2, f_browsedir},
570 {"bufexists", 1, 1, f_bufexists},
571 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
572 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
573 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
574 {"buflisted", 1, 1, f_buflisted},
575 {"bufloaded", 1, 1, f_bufloaded},
576 {"bufname", 1, 1, f_bufname},
577 {"bufnr", 1, 2, f_bufnr},
578 {"bufwinid", 1, 1, f_bufwinid},
579 {"bufwinnr", 1, 1, f_bufwinnr},
580 {"byte2line", 1, 1, f_byte2line},
581 {"byteidx", 2, 2, f_byteidx},
582 {"byteidxcomp", 2, 2, f_byteidxcomp},
583 {"call", 2, 3, f_call},
584#ifdef FEAT_FLOAT
585 {"ceil", 1, 1, f_ceil},
586#endif
587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200590 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
592 {"ch_evalraw", 2, 3, f_ch_evalraw},
593 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
594 {"ch_getjob", 1, 1, f_ch_getjob},
595 {"ch_info", 1, 1, f_ch_info},
596 {"ch_log", 1, 2, f_ch_log},
597 {"ch_logfile", 1, 2, f_ch_logfile},
598 {"ch_open", 1, 2, f_ch_open},
599 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"ch_readraw", 1, 2, f_ch_readraw},
602 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
603 {"ch_sendraw", 2, 3, f_ch_sendraw},
604 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200605 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606#endif
607 {"changenr", 0, 0, f_changenr},
608 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200609 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100611 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"col", 1, 1, f_col},
613#if defined(FEAT_INS_EXPAND)
614 {"complete", 2, 2, f_complete},
615 {"complete_add", 1, 1, f_complete_add},
616 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100617 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618#endif
619 {"confirm", 1, 4, f_confirm},
620 {"copy", 1, 1, f_copy},
621#ifdef FEAT_FLOAT
622 {"cos", 1, 1, f_cos},
623 {"cosh", 1, 1, f_cosh},
624#endif
625 {"count", 2, 4, f_count},
626 {"cscope_connection",0,3, f_cscope_connection},
627 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200629 {"debugbreak", 1, 1, f_debugbreak},
630#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"deepcopy", 1, 2, f_deepcopy},
632 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200633 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"did_filetype", 0, 0, f_did_filetype},
635 {"diff_filler", 1, 1, f_diff_filler},
636 {"diff_hlID", 2, 2, f_diff_hlID},
637 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200638 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"escape", 2, 2, f_escape},
640 {"eval", 1, 1, f_eval},
641 {"eventhandler", 0, 0, f_eventhandler},
642 {"executable", 1, 1, f_executable},
643 {"execute", 1, 2, f_execute},
644 {"exepath", 1, 1, f_exepath},
645 {"exists", 1, 1, f_exists},
646#ifdef FEAT_FLOAT
647 {"exp", 1, 1, f_exp},
648#endif
649 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200650 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"extend", 2, 3, f_extend},
652 {"feedkeys", 1, 2, f_feedkeys},
653 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
654 {"filereadable", 1, 1, f_filereadable},
655 {"filewritable", 1, 1, f_filewritable},
656 {"filter", 2, 2, f_filter},
657 {"finddir", 1, 3, f_finddir},
658 {"findfile", 1, 3, f_findfile},
659#ifdef FEAT_FLOAT
660 {"float2nr", 1, 1, f_float2nr},
661 {"floor", 1, 1, f_floor},
662 {"fmod", 2, 2, f_fmod},
663#endif
664 {"fnameescape", 1, 1, f_fnameescape},
665 {"fnamemodify", 2, 2, f_fnamemodify},
666 {"foldclosed", 1, 1, f_foldclosed},
667 {"foldclosedend", 1, 1, f_foldclosedend},
668 {"foldlevel", 1, 1, f_foldlevel},
669 {"foldtext", 0, 0, f_foldtext},
670 {"foldtextresult", 1, 1, f_foldtextresult},
671 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200672 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"function", 1, 3, f_function},
674 {"garbagecollect", 0, 1, f_garbagecollect},
675 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200676 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getbufline", 2, 3, f_getbufline},
678 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100679 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getchar", 0, 1, f_getchar},
681 {"getcharmod", 0, 0, f_getcharmod},
682 {"getcharsearch", 0, 0, f_getcharsearch},
683 {"getcmdline", 0, 0, f_getcmdline},
684 {"getcmdpos", 0, 0, f_getcmdpos},
685 {"getcmdtype", 0, 0, f_getcmdtype},
686 {"getcmdwintype", 0, 0, f_getcmdwintype},
687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200688 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689#endif
690 {"getcurpos", 0, 0, f_getcurpos},
691 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200692 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200693 {"getfontname", 0, 1, f_getfontname},
694 {"getfperm", 1, 1, f_getfperm},
695 {"getfsize", 1, 1, f_getfsize},
696 {"getftime", 1, 1, f_getftime},
697 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100698 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100701 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getpid", 0, 0, f_getpid},
703 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200704 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"getreg", 0, 3, f_getreg},
706 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200708 {"gettabvar", 2, 3, f_gettabvar},
709 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100710 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200711 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100712 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"getwinposx", 0, 0, f_getwinposx},
714 {"getwinposy", 0, 0, f_getwinposy},
715 {"getwinvar", 2, 3, f_getwinvar},
716 {"glob", 1, 4, f_glob},
717 {"glob2regpat", 1, 1, f_glob2regpat},
718 {"globpath", 2, 5, f_globpath},
719 {"has", 1, 1, f_has},
720 {"has_key", 2, 2, f_has_key},
721 {"haslocaldir", 0, 2, f_haslocaldir},
722 {"hasmapto", 1, 3, f_hasmapto},
723 {"highlightID", 1, 1, f_hlID}, /* obsolete */
724 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
725 {"histadd", 2, 2, f_histadd},
726 {"histdel", 1, 2, f_histdel},
727 {"histget", 1, 2, f_histget},
728 {"histnr", 1, 1, f_histnr},
729 {"hlID", 1, 1, f_hlID},
730 {"hlexists", 1, 1, f_hlexists},
731 {"hostname", 0, 0, f_hostname},
732 {"iconv", 3, 3, f_iconv},
733 {"indent", 1, 1, f_indent},
734 {"index", 2, 4, f_index},
735 {"input", 1, 3, f_input},
736 {"inputdialog", 1, 3, f_inputdialog},
737 {"inputlist", 1, 1, f_inputlist},
738 {"inputrestore", 0, 0, f_inputrestore},
739 {"inputsave", 0, 0, f_inputsave},
740 {"inputsecret", 1, 2, f_inputsecret},
741 {"insert", 2, 3, f_insert},
742 {"invert", 1, 1, f_invert},
743 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isinf", 1, 1, f_isinf},
746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747 {"islocked", 1, 1, f_islocked},
748#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
749 {"isnan", 1, 1, f_isnan},
750#endif
751 {"items", 1, 1, f_items},
752#ifdef FEAT_JOB_CHANNEL
753 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200754 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"job_setoptions", 2, 2, f_job_setoptions},
756 {"job_start", 1, 2, f_job_start},
757 {"job_status", 1, 1, f_job_status},
758 {"job_stop", 1, 2, f_job_stop},
759#endif
760 {"join", 1, 2, f_join},
761 {"js_decode", 1, 1, f_js_decode},
762 {"js_encode", 1, 1, f_js_encode},
763 {"json_decode", 1, 1, f_json_decode},
764 {"json_encode", 1, 1, f_json_encode},
765 {"keys", 1, 1, f_keys},
766 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
767 {"len", 1, 1, f_len},
768 {"libcall", 3, 3, f_libcall},
769 {"libcallnr", 3, 3, f_libcallnr},
770 {"line", 1, 1, f_line},
771 {"line2byte", 1, 1, f_line2byte},
772 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200773 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200775 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"localtime", 0, 0, f_localtime},
778#ifdef FEAT_FLOAT
779 {"log", 1, 1, f_log},
780 {"log10", 1, 1, f_log10},
781#endif
782#ifdef FEAT_LUA
783 {"luaeval", 1, 2, f_luaeval},
784#endif
785 {"map", 2, 2, f_map},
786 {"maparg", 1, 4, f_maparg},
787 {"mapcheck", 1, 3, f_mapcheck},
788 {"match", 2, 4, f_match},
789 {"matchadd", 2, 5, f_matchadd},
790 {"matchaddpos", 2, 5, f_matchaddpos},
791 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100792 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"matchend", 2, 4, f_matchend},
794 {"matchlist", 2, 4, f_matchlist},
795 {"matchstr", 2, 4, f_matchstr},
796 {"matchstrpos", 2, 4, f_matchstrpos},
797 {"max", 1, 1, f_max},
798 {"min", 1, 1, f_min},
799#ifdef vim_mkdir
800 {"mkdir", 1, 3, f_mkdir},
801#endif
802 {"mode", 0, 1, f_mode},
803#ifdef FEAT_MZSCHEME
804 {"mzeval", 1, 1, f_mzeval},
805#endif
806 {"nextnonblank", 1, 1, f_nextnonblank},
807 {"nr2char", 1, 2, f_nr2char},
808 {"or", 2, 2, f_or},
809 {"pathshorten", 1, 1, f_pathshorten},
810#ifdef FEAT_PERL
811 {"perleval", 1, 1, f_perleval},
812#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200814 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200818 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200819 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200820 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200821 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200822 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200824#ifdef FEAT_FLOAT
825 {"pow", 2, 2, f_pow},
826#endif
827 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100828 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200829#ifdef FEAT_JOB_CHANNEL
830 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200831 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200832 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
833#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100834#ifdef FEAT_TEXT_PROP
835 {"prop_add", 3, 3, f_prop_add},
836 {"prop_clear", 1, 3, f_prop_clear},
837 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100838 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100839 {"prop_type_add", 2, 2, f_prop_type_add},
840 {"prop_type_change", 2, 2, f_prop_type_change},
841 {"prop_type_delete", 1, 2, f_prop_type_delete},
842 {"prop_type_get", 1, 2, f_prop_type_get},
843 {"prop_type_list", 0, 1, f_prop_type_list},
844#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200845 {"pumvisible", 0, 0, f_pumvisible},
846#ifdef FEAT_PYTHON3
847 {"py3eval", 1, 1, f_py3eval},
848#endif
849#ifdef FEAT_PYTHON
850 {"pyeval", 1, 1, f_pyeval},
851#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100852#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
853 {"pyxeval", 1, 1, f_pyxeval},
854#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200856 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200858 {"reg_executing", 0, 0, f_reg_executing},
859 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"reltime", 0, 2, f_reltime},
861#ifdef FEAT_FLOAT
862 {"reltimefloat", 1, 1, f_reltimefloat},
863#endif
864 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100865 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"remote_foreground", 1, 1, f_remote_foreground},
867 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100868 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200869 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100870 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"remove", 2, 3, f_remove},
872 {"rename", 2, 2, f_rename},
873 {"repeat", 2, 2, f_repeat},
874 {"resolve", 1, 1, f_resolve},
875 {"reverse", 1, 1, f_reverse},
876#ifdef FEAT_FLOAT
877 {"round", 1, 1, f_round},
878#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100879#ifdef FEAT_RUBY
880 {"rubyeval", 1, 1, f_rubyeval},
881#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"screenattr", 2, 2, f_screenattr},
883 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100884 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"screencol", 0, 0, f_screencol},
886 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100887 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200888 {"search", 1, 4, f_search},
889 {"searchdecl", 1, 3, f_searchdecl},
890 {"searchpair", 3, 7, f_searchpair},
891 {"searchpairpos", 3, 7, f_searchpairpos},
892 {"searchpos", 1, 4, f_searchpos},
893 {"server2client", 2, 2, f_server2client},
894 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200895 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"setbufvar", 3, 3, f_setbufvar},
897 {"setcharsearch", 1, 1, f_setcharsearch},
898 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200899 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200900 {"setfperm", 2, 2, f_setfperm},
901 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200902 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100903 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200905 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200906 {"setreg", 2, 3, f_setreg},
907 {"settabvar", 3, 3, f_settabvar},
908 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100909 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"setwinvar", 3, 3, f_setwinvar},
911#ifdef FEAT_CRYPT
912 {"sha256", 1, 1, f_sha256},
913#endif
914 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100915 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100916#ifdef FEAT_SIGNS
917 {"sign_define", 1, 2, f_sign_define},
918 {"sign_getdefined", 0, 1, f_sign_getdefined},
919 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100920 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100921 {"sign_place", 4, 5, f_sign_place},
922 {"sign_undefine", 0, 1, f_sign_undefine},
923 {"sign_unplace", 1, 2, f_sign_unplace},
924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200925 {"simplify", 1, 1, f_simplify},
926#ifdef FEAT_FLOAT
927 {"sin", 1, 1, f_sin},
928 {"sinh", 1, 1, f_sinh},
929#endif
930 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200931#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200932 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200933 {"sound_playevent", 1, 2, f_sound_playevent},
934 {"sound_playfile", 1, 2, f_sound_playfile},
935 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200936#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200937 {"soundfold", 1, 1, f_soundfold},
938 {"spellbadword", 0, 1, f_spellbadword},
939 {"spellsuggest", 1, 3, f_spellsuggest},
940 {"split", 1, 3, f_split},
941#ifdef FEAT_FLOAT
942 {"sqrt", 1, 1, f_sqrt},
943 {"str2float", 1, 1, f_str2float},
944#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200945 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200946 {"str2nr", 1, 2, f_str2nr},
947 {"strcharpart", 2, 3, f_strcharpart},
948 {"strchars", 1, 2, f_strchars},
949 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
950#ifdef HAVE_STRFTIME
951 {"strftime", 1, 2, f_strftime},
952#endif
953 {"strgetchar", 2, 2, f_strgetchar},
954 {"stridx", 2, 3, f_stridx},
955 {"string", 1, 1, f_string},
956 {"strlen", 1, 1, f_strlen},
957 {"strpart", 2, 3, f_strpart},
958 {"strridx", 2, 3, f_strridx},
959 {"strtrans", 1, 1, f_strtrans},
960 {"strwidth", 1, 1, f_strwidth},
961 {"submatch", 1, 2, f_submatch},
962 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200963 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200964 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965 {"synID", 3, 3, f_synID},
966 {"synIDattr", 2, 3, f_synIDattr},
967 {"synIDtrans", 1, 1, f_synIDtrans},
968 {"synconcealed", 2, 2, f_synconcealed},
969 {"synstack", 2, 2, f_synstack},
970 {"system", 1, 2, f_system},
971 {"systemlist", 1, 2, f_systemlist},
972 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
973 {"tabpagenr", 0, 1, f_tabpagenr},
974 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
975 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100976 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200977#ifdef FEAT_FLOAT
978 {"tan", 1, 1, f_tan},
979 {"tanh", 1, 1, f_tanh},
980#endif
981 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200982#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100983 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
984 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100985 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200986 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200987# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
988 {"term_getansicolors", 1, 1, f_term_getansicolors},
989# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200991 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200993 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200994 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200995 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200996 {"term_getstatus", 1, 1, f_term_getstatus},
997 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200998 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001000 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001001 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001002# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1003 {"term_setansicolors", 2, 2, f_term_setansicolors},
1004# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001005 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001006 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001007 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001008 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001009 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001010#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001011 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1012 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001013 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001014 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001015 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001016 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001017 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001018#ifdef FEAT_JOB_CHANNEL
1019 {"test_null_channel", 0, 0, f_test_null_channel},
1020#endif
1021 {"test_null_dict", 0, 0, f_test_null_dict},
1022#ifdef FEAT_JOB_CHANNEL
1023 {"test_null_job", 0, 0, f_test_null_job},
1024#endif
1025 {"test_null_list", 0, 0, f_test_null_list},
1026 {"test_null_partial", 0, 0, f_test_null_partial},
1027 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001028 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001029 {"test_override", 2, 2, f_test_override},
1030 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001031#ifdef FEAT_GUI
1032 {"test_scrollbar", 3, 3, f_test_scrollbar},
1033#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001034#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001035 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001036#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037 {"test_settime", 1, 1, f_test_settime},
1038#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001039 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001040 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001041 {"timer_start", 2, 3, f_timer_start},
1042 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001043 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001044#endif
1045 {"tolower", 1, 1, f_tolower},
1046 {"toupper", 1, 1, f_toupper},
1047 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001048 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001049#ifdef FEAT_FLOAT
1050 {"trunc", 1, 1, f_trunc},
1051#endif
1052 {"type", 1, 1, f_type},
1053 {"undofile", 1, 1, f_undofile},
1054 {"undotree", 0, 0, f_undotree},
1055 {"uniq", 1, 3, f_uniq},
1056 {"values", 1, 1, f_values},
1057 {"virtcol", 1, 1, f_virtcol},
1058 {"visualmode", 0, 1, f_visualmode},
1059 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001060 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001061 {"win_findbuf", 1, 1, f_win_findbuf},
1062 {"win_getid", 0, 2, f_win_getid},
1063 {"win_gotoid", 1, 1, f_win_gotoid},
1064 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1065 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001066 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001067 {"winbufnr", 1, 1, f_winbufnr},
1068 {"wincol", 0, 0, f_wincol},
1069 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001070 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001071 {"winline", 0, 0, f_winline},
1072 {"winnr", 0, 1, f_winnr},
1073 {"winrestcmd", 0, 0, f_winrestcmd},
1074 {"winrestview", 1, 1, f_winrestview},
1075 {"winsaveview", 0, 0, f_winsaveview},
1076 {"winwidth", 1, 1, f_winwidth},
1077 {"wordcount", 0, 0, f_wordcount},
1078 {"writefile", 2, 3, f_writefile},
1079 {"xor", 2, 2, f_xor},
1080};
1081
1082#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1083
1084/*
1085 * Function given to ExpandGeneric() to obtain the list of internal
1086 * or user defined function names.
1087 */
1088 char_u *
1089get_function_name(expand_T *xp, int idx)
1090{
1091 static int intidx = -1;
1092 char_u *name;
1093
1094 if (idx == 0)
1095 intidx = -1;
1096 if (intidx < 0)
1097 {
1098 name = get_user_func_name(xp, idx);
1099 if (name != NULL)
1100 return name;
1101 }
1102 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1103 {
1104 STRCPY(IObuff, functions[intidx].f_name);
1105 STRCAT(IObuff, "(");
1106 if (functions[intidx].f_max_argc == 0)
1107 STRCAT(IObuff, ")");
1108 return IObuff;
1109 }
1110
1111 return NULL;
1112}
1113
1114/*
1115 * Function given to ExpandGeneric() to obtain the list of internal or
1116 * user defined variable or function names.
1117 */
1118 char_u *
1119get_expr_name(expand_T *xp, int idx)
1120{
1121 static int intidx = -1;
1122 char_u *name;
1123
1124 if (idx == 0)
1125 intidx = -1;
1126 if (intidx < 0)
1127 {
1128 name = get_function_name(xp, idx);
1129 if (name != NULL)
1130 return name;
1131 }
1132 return get_user_var_name(xp, ++intidx);
1133}
1134
1135#endif /* FEAT_CMDL_COMPL */
1136
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001137/*
1138 * Find internal function in table above.
1139 * Return index, or -1 if not found
1140 */
1141 int
1142find_internal_func(
1143 char_u *name) /* name of the function */
1144{
1145 int first = 0;
1146 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1147 int cmp;
1148 int x;
1149
1150 /*
1151 * Find the function name in the table. Binary search.
1152 */
1153 while (first <= last)
1154 {
1155 x = first + ((unsigned)(last - first) >> 1);
1156 cmp = STRCMP(name, functions[x].f_name);
1157 if (cmp < 0)
1158 last = x - 1;
1159 else if (cmp > 0)
1160 first = x + 1;
1161 else
1162 return x;
1163 }
1164 return -1;
1165}
1166
1167 int
1168call_internal_func(
1169 char_u *name,
1170 int argcount,
1171 typval_T *argvars,
1172 typval_T *rettv)
1173{
1174 int i;
1175
1176 i = find_internal_func(name);
1177 if (i < 0)
1178 return ERROR_UNKNOWN;
1179 if (argcount < functions[i].f_min_argc)
1180 return ERROR_TOOFEW;
1181 if (argcount > functions[i].f_max_argc)
1182 return ERROR_TOOMANY;
1183 argvars[argcount].v_type = VAR_UNKNOWN;
1184 functions[i].f_func(argvars, rettv);
1185 return ERROR_NONE;
1186}
1187
1188/*
1189 * Return TRUE for a non-zero Number and a non-empty String.
1190 */
1191 static int
1192non_zero_arg(typval_T *argvars)
1193{
1194 return ((argvars[0].v_type == VAR_NUMBER
1195 && argvars[0].vval.v_number != 0)
1196 || (argvars[0].v_type == VAR_SPECIAL
1197 && argvars[0].vval.v_number == VVAL_TRUE)
1198 || (argvars[0].v_type == VAR_STRING
1199 && argvars[0].vval.v_string != NULL
1200 && *argvars[0].vval.v_string != NUL));
1201}
1202
1203/*
1204 * Get the lnum from the first argument.
1205 * Also accepts ".", "$", etc., but that only works for the current buffer.
1206 * Returns -1 on error.
1207 */
1208 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001209tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001210{
1211 typval_T rettv;
1212 linenr_T lnum;
1213
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001215 if (lnum == 0) /* no valid number, try using line() */
1216 {
1217 rettv.v_type = VAR_NUMBER;
1218 f_line(argvars, &rettv);
1219 lnum = (linenr_T)rettv.vval.v_number;
1220 clear_tv(&rettv);
1221 }
1222 return lnum;
1223}
1224
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001225/*
1226 * Get the lnum from the first argument.
1227 * Also accepts "$", then "buf" is used.
1228 * Returns 0 on error.
1229 */
1230 static linenr_T
1231tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1232{
1233 if (argvars[0].v_type == VAR_STRING
1234 && argvars[0].vval.v_string != NULL
1235 && argvars[0].vval.v_string[0] == '$'
1236 && buf != NULL)
1237 return buf->b_ml.ml_line_count;
1238 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1239}
1240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001241#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242/*
1243 * Get the float value of "argvars[0]" into "f".
1244 * Returns FAIL when the argument is not a Number or Float.
1245 */
1246 static int
1247get_float_arg(typval_T *argvars, float_T *f)
1248{
1249 if (argvars[0].v_type == VAR_FLOAT)
1250 {
1251 *f = argvars[0].vval.v_float;
1252 return OK;
1253 }
1254 if (argvars[0].v_type == VAR_NUMBER)
1255 {
1256 *f = (float_T)argvars[0].vval.v_number;
1257 return OK;
1258 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001260 return FAIL;
1261}
1262
1263/*
1264 * "abs(expr)" function
1265 */
1266 static void
1267f_abs(typval_T *argvars, typval_T *rettv)
1268{
1269 if (argvars[0].v_type == VAR_FLOAT)
1270 {
1271 rettv->v_type = VAR_FLOAT;
1272 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1273 }
1274 else
1275 {
1276 varnumber_T n;
1277 int error = FALSE;
1278
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001279 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001280 if (error)
1281 rettv->vval.v_number = -1;
1282 else if (n > 0)
1283 rettv->vval.v_number = n;
1284 else
1285 rettv->vval.v_number = -n;
1286 }
1287}
1288
1289/*
1290 * "acos()" function
1291 */
1292 static void
1293f_acos(typval_T *argvars, typval_T *rettv)
1294{
1295 float_T f = 0.0;
1296
1297 rettv->v_type = VAR_FLOAT;
1298 if (get_float_arg(argvars, &f) == OK)
1299 rettv->vval.v_float = acos(f);
1300 else
1301 rettv->vval.v_float = 0.0;
1302}
1303#endif
1304
1305/*
1306 * "add(list, item)" function
1307 */
1308 static void
1309f_add(typval_T *argvars, typval_T *rettv)
1310{
1311 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001313
1314 rettv->vval.v_number = 1; /* Default: Failed */
1315 if (argvars[0].v_type == VAR_LIST)
1316 {
1317 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001318 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001319 (char_u *)N_("add() argument"), TRUE)
1320 && list_append_tv(l, &argvars[1]) == OK)
1321 copy_tv(&argvars[0], rettv);
1322 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 else if (argvars[0].v_type == VAR_BLOB)
1324 {
1325 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001326 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327 (char_u *)N_("add() argument"), TRUE))
1328 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001329 int error = FALSE;
1330 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1331
1332 if (!error)
1333 {
1334 ga_append(&b->bv_ga, (int)n);
1335 copy_tv(&argvars[0], rettv);
1336 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001337 }
1338 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001339 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001340 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001341}
1342
1343/*
1344 * "and(expr, expr)" function
1345 */
1346 static void
1347f_and(typval_T *argvars, typval_T *rettv)
1348{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001349 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1350 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001351}
1352
1353/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001354 * If there is a window for "curbuf", make it the current window.
1355 */
1356 static void
1357find_win_for_curbuf(void)
1358{
1359 wininfo_T *wip;
1360
1361 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1362 {
1363 if (wip->wi_win != NULL)
1364 {
1365 curwin = wip->wi_win;
1366 break;
1367 }
1368 }
1369}
1370
1371/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001372 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 */
1374 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001375set_buffer_lines(
1376 buf_T *buf,
1377 linenr_T lnum_arg,
1378 int append,
1379 typval_T *lines,
1380 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381{
Bram Moolenaarca851592018-06-06 21:04:07 +02001382 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1383 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001384 list_T *l = NULL;
1385 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 linenr_T append_lnum;
1388 buf_T *curbuf_save = NULL;
1389 win_T *curwin_save = NULL;
1390 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001391
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 /* When using the current buffer ml_mfp will be set if needed. Useful when
1393 * setline() is used on startup. For other buffers the buffer must be
1394 * loaded. */
1395 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001396 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001397 rettv->vval.v_number = 1; /* FAIL */
1398 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001399 }
1400
Bram Moolenaarca851592018-06-06 21:04:07 +02001401 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001402 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001403 curbuf_save = curbuf;
1404 curwin_save = curwin;
1405 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001406 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 }
1408
1409 if (append)
1410 // appendbufline() uses the line number below which we insert
1411 append_lnum = lnum - 1;
1412 else
1413 // setbufline() uses the line number above which we insert, we only
1414 // append if it's below the last line
1415 append_lnum = curbuf->b_ml.ml_line_count;
1416
1417 if (lines->v_type == VAR_LIST)
1418 {
1419 l = lines->vval.v_list;
1420 li = l->lv_first;
1421 }
1422 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001423 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001424
1425 /* default result is zero == OK */
1426 for (;;)
1427 {
1428 if (l != NULL)
1429 {
1430 /* list argument, get next string */
1431 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001432 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001433 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001434 li = li->li_next;
1435 }
1436
Bram Moolenaarca851592018-06-06 21:04:07 +02001437 rettv->vval.v_number = 1; /* FAIL */
1438 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1439 break;
1440
1441 /* When coming here from Insert mode, sync undo, so that this can be
1442 * undone separately from what was previously inserted. */
1443 if (u_sync_once == 2)
1444 {
1445 u_sync_once = 1; /* notify that u_sync() was called */
1446 u_sync(TRUE);
1447 }
1448
1449 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1450 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001451 // Existing line, replace it.
1452 // Removes any existing text properties.
1453 if (u_savesub(lnum) == OK && ml_replace_len(
1454 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001455 {
1456 changed_bytes(lnum, 0);
1457 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1458 check_cursor_col();
1459 rettv->vval.v_number = 0; /* OK */
1460 }
1461 }
1462 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1463 {
1464 /* append the line */
1465 ++added;
1466 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1467 rettv->vval.v_number = 0; /* OK */
1468 }
1469
1470 if (l == NULL) /* only one string argument */
1471 break;
1472 ++lnum;
1473 }
1474
1475 if (added > 0)
1476 {
1477 win_T *wp;
1478 tabpage_T *tp;
1479
1480 appended_lines_mark(append_lnum, added);
1481 FOR_ALL_TAB_WINDOWS(tp, wp)
1482 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1483 wp->w_cursor.lnum += added;
1484 check_cursor_col();
1485
Bram Moolenaarf2732452018-06-03 14:47:35 +02001486#ifdef FEAT_JOB_CHANNEL
1487 if (bt_prompt(curbuf) && (State & INSERT))
1488 // show the line with the prompt
1489 update_topline();
1490#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001491 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001492
1493 if (!is_curbuf)
1494 {
1495 curbuf = curbuf_save;
1496 curwin = curwin_save;
1497 }
1498}
1499
1500/*
1501 * "append(lnum, string/list)" function
1502 */
1503 static void
1504f_append(typval_T *argvars, typval_T *rettv)
1505{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001506 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001507
1508 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1509}
1510
1511/*
1512 * "appendbufline(buf, lnum, string/list)" function
1513 */
1514 static void
1515f_appendbufline(typval_T *argvars, typval_T *rettv)
1516{
1517 linenr_T lnum;
1518 buf_T *buf;
1519
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001520 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001521 if (buf == NULL)
1522 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001525 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001526 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1527 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001528}
1529
1530/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001531 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001532 */
1533 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001535{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001536 win_T *wp;
1537
1538 if (argvars[0].v_type == VAR_UNKNOWN)
1539 // use the current window
1540 rettv->vval.v_number = ARGCOUNT;
1541 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001542 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001543 // use the global argument list
1544 rettv->vval.v_number = GARGCOUNT;
1545 else
1546 {
1547 // use the argument list of the specified window
1548 wp = find_win_by_nr_or_id(&argvars[0]);
1549 if (wp != NULL)
1550 rettv->vval.v_number = WARGCOUNT(wp);
1551 else
1552 rettv->vval.v_number = -1;
1553 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554}
1555
1556/*
1557 * "argidx()" function
1558 */
1559 static void
1560f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1561{
1562 rettv->vval.v_number = curwin->w_arg_idx;
1563}
1564
1565/*
1566 * "arglistid()" function
1567 */
1568 static void
1569f_arglistid(typval_T *argvars, typval_T *rettv)
1570{
1571 win_T *wp;
1572
1573 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001574 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001575 if (wp != NULL)
1576 rettv->vval.v_number = wp->w_alist->id;
1577}
1578
1579/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001580 * Get the argument list for a given window
1581 */
1582 static void
1583get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1584{
1585 int idx;
1586
1587 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1588 for (idx = 0; idx < argcount; ++idx)
1589 list_append_string(rettv->vval.v_list,
1590 alist_name(&arglist[idx]), -1);
1591}
1592
1593/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001594 * "argv(nr)" function
1595 */
1596 static void
1597f_argv(typval_T *argvars, typval_T *rettv)
1598{
1599 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001600 aentry_T *arglist = NULL;
1601 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602
1603 if (argvars[0].v_type != VAR_UNKNOWN)
1604 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001605 if (argvars[1].v_type == VAR_UNKNOWN)
1606 {
1607 arglist = ARGLIST;
1608 argcount = ARGCOUNT;
1609 }
1610 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001611 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001612 {
1613 arglist = GARGLIST;
1614 argcount = GARGCOUNT;
1615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001616 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001617 {
1618 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1619
1620 if (wp != NULL)
1621 {
1622 /* Use the argument list of the specified window */
1623 arglist = WARGLIST(wp);
1624 argcount = WARGCOUNT(wp);
1625 }
1626 }
1627
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001628 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001629 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001630 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001631 if (arglist != NULL && idx >= 0 && idx < argcount)
1632 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1633 else if (idx == -1)
1634 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001636 else
1637 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001638}
1639
1640/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001641 * "assert_beeps(cmd [, error])" function
1642 */
1643 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001645{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001647}
1648
1649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001650 * "assert_equal(expected, actual[, msg])" function
1651 */
1652 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001656}
1657
1658/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001659 * "assert_equalfile(fname-one, fname-two)" function
1660 */
1661 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001663{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001665}
1666
1667/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668 * "assert_notequal(expected, actual[, msg])" function
1669 */
1670 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674}
1675
1676/*
1677 * "assert_exception(string[, msg])" function
1678 */
1679 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683}
1684
1685/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001686 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687 */
1688 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692}
1693
1694/*
1695 * "assert_false(actual[, msg])" function
1696 */
1697 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001701}
1702
1703/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001704 * "assert_inrange(lower, upper[, msg])" function
1705 */
1706 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001708{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001710}
1711
1712/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713 * "assert_match(pattern, actual[, msg])" function
1714 */
1715 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001718 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719}
1720
1721/*
1722 * "assert_notmatch(pattern, actual[, msg])" function
1723 */
1724 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001725f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001727 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001728}
1729
1730/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001731 * "assert_report(msg)" function
1732 */
1733 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001734f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001735{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001736 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001737}
1738
1739/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001740 * "assert_true(actual[, msg])" function
1741 */
1742 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001743f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001745 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001746}
1747
1748#ifdef FEAT_FLOAT
1749/*
1750 * "asin()" function
1751 */
1752 static void
1753f_asin(typval_T *argvars, typval_T *rettv)
1754{
1755 float_T f = 0.0;
1756
1757 rettv->v_type = VAR_FLOAT;
1758 if (get_float_arg(argvars, &f) == OK)
1759 rettv->vval.v_float = asin(f);
1760 else
1761 rettv->vval.v_float = 0.0;
1762}
1763
1764/*
1765 * "atan()" function
1766 */
1767 static void
1768f_atan(typval_T *argvars, typval_T *rettv)
1769{
1770 float_T f = 0.0;
1771
1772 rettv->v_type = VAR_FLOAT;
1773 if (get_float_arg(argvars, &f) == OK)
1774 rettv->vval.v_float = atan(f);
1775 else
1776 rettv->vval.v_float = 0.0;
1777}
1778
1779/*
1780 * "atan2()" function
1781 */
1782 static void
1783f_atan2(typval_T *argvars, typval_T *rettv)
1784{
1785 float_T fx = 0.0, fy = 0.0;
1786
1787 rettv->v_type = VAR_FLOAT;
1788 if (get_float_arg(argvars, &fx) == OK
1789 && get_float_arg(&argvars[1], &fy) == OK)
1790 rettv->vval.v_float = atan2(fx, fy);
1791 else
1792 rettv->vval.v_float = 0.0;
1793}
1794#endif
1795
1796/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001797 * "balloon_show()" function
1798 */
1799#ifdef FEAT_BEVAL
1800 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001801f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1802{
1803 rettv->v_type = VAR_STRING;
1804 if (balloonEval != NULL)
1805 {
1806 if (balloonEval->msg == NULL)
1807 rettv->vval.v_string = NULL;
1808 else
1809 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1810 }
1811}
1812
1813 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001814f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1815{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001816 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817 {
1818 if (argvars[0].v_type == VAR_LIST
1819# ifdef FEAT_GUI
1820 && !gui.in_use
1821# endif
1822 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001823 {
1824 list_T *l = argvars[0].vval.v_list;
1825
1826 // empty list removes the balloon
1827 post_balloon(balloonEval, NULL,
1828 l == NULL || l->lv_len == 0 ? NULL : l);
1829 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001830 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001831 {
1832 char_u *mesg = tv_get_string_chk(&argvars[0]);
1833
1834 if (mesg != NULL)
1835 // empty string removes the balloon
1836 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1837 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001838 }
1839}
1840
Bram Moolenaar669a8282017-11-19 20:13:05 +01001841# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001842 static void
1843f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1844{
1845 if (rettv_list_alloc(rettv) == OK)
1846 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001847 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001848
1849 if (msg != NULL)
1850 {
1851 pumitem_T *array;
1852 int size = split_message(msg, &array);
1853 int i;
1854
1855 /* Skip the first and last item, they are always empty. */
1856 for (i = 1; i < size - 1; ++i)
1857 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001858 while (size > 0)
1859 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001860 vim_free(array);
1861 }
1862 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001863}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001864# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001865#endif
1866
1867/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001868 * "browse(save, title, initdir, default)" function
1869 */
1870 static void
1871f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1872{
1873#ifdef FEAT_BROWSE
1874 int save;
1875 char_u *title;
1876 char_u *initdir;
1877 char_u *defname;
1878 char_u buf[NUMBUFLEN];
1879 char_u buf2[NUMBUFLEN];
1880 int error = FALSE;
1881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 save = (int)tv_get_number_chk(&argvars[0], &error);
1883 title = tv_get_string_chk(&argvars[1]);
1884 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1885 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001886
1887 if (error || title == NULL || initdir == NULL || defname == NULL)
1888 rettv->vval.v_string = NULL;
1889 else
1890 rettv->vval.v_string =
1891 do_browse(save ? BROWSE_SAVE : 0,
1892 title, defname, NULL, initdir, NULL, curbuf);
1893#else
1894 rettv->vval.v_string = NULL;
1895#endif
1896 rettv->v_type = VAR_STRING;
1897}
1898
1899/*
1900 * "browsedir(title, initdir)" function
1901 */
1902 static void
1903f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1904{
1905#ifdef FEAT_BROWSE
1906 char_u *title;
1907 char_u *initdir;
1908 char_u buf[NUMBUFLEN];
1909
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001910 title = tv_get_string_chk(&argvars[0]);
1911 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001912
1913 if (title == NULL || initdir == NULL)
1914 rettv->vval.v_string = NULL;
1915 else
1916 rettv->vval.v_string = do_browse(BROWSE_DIR,
1917 title, NULL, NULL, initdir, NULL, curbuf);
1918#else
1919 rettv->vval.v_string = NULL;
1920#endif
1921 rettv->v_type = VAR_STRING;
1922}
1923
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001924/*
1925 * Find a buffer by number or exact name.
1926 */
1927 static buf_T *
1928find_buffer(typval_T *avar)
1929{
1930 buf_T *buf = NULL;
1931
1932 if (avar->v_type == VAR_NUMBER)
1933 buf = buflist_findnr((int)avar->vval.v_number);
1934 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1935 {
1936 buf = buflist_findname_exp(avar->vval.v_string);
1937 if (buf == NULL)
1938 {
1939 /* No full path name match, try a match with a URL or a "nofile"
1940 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001941 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001942 if (buf->b_fname != NULL
1943 && (path_with_url(buf->b_fname)
1944#ifdef FEAT_QUICKFIX
1945 || bt_nofile(buf)
1946#endif
1947 )
1948 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1949 break;
1950 }
1951 }
1952 return buf;
1953}
1954
1955/*
1956 * "bufexists(expr)" function
1957 */
1958 static void
1959f_bufexists(typval_T *argvars, typval_T *rettv)
1960{
1961 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1962}
1963
1964/*
1965 * "buflisted(expr)" function
1966 */
1967 static void
1968f_buflisted(typval_T *argvars, typval_T *rettv)
1969{
1970 buf_T *buf;
1971
1972 buf = find_buffer(&argvars[0]);
1973 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1974}
1975
1976/*
1977 * "bufloaded(expr)" function
1978 */
1979 static void
1980f_bufloaded(typval_T *argvars, typval_T *rettv)
1981{
1982 buf_T *buf;
1983
1984 buf = find_buffer(&argvars[0]);
1985 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1986}
1987
1988 buf_T *
1989buflist_find_by_name(char_u *name, int curtab_only)
1990{
1991 int save_magic;
1992 char_u *save_cpo;
1993 buf_T *buf;
1994
1995 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1996 save_magic = p_magic;
1997 p_magic = TRUE;
1998 save_cpo = p_cpo;
1999 p_cpo = (char_u *)"";
2000
2001 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2002 TRUE, FALSE, curtab_only));
2003
2004 p_magic = save_magic;
2005 p_cpo = save_cpo;
2006 return buf;
2007}
2008
2009/*
2010 * Get buffer by number or pattern.
2011 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002012 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002013tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002014{
2015 char_u *name = tv->vval.v_string;
2016 buf_T *buf;
2017
2018 if (tv->v_type == VAR_NUMBER)
2019 return buflist_findnr((int)tv->vval.v_number);
2020 if (tv->v_type != VAR_STRING)
2021 return NULL;
2022 if (name == NULL || *name == NUL)
2023 return curbuf;
2024 if (name[0] == '$' && name[1] == NUL)
2025 return lastbuf;
2026
2027 buf = buflist_find_by_name(name, curtab_only);
2028
2029 /* If not found, try expanding the name, like done for bufexists(). */
2030 if (buf == NULL)
2031 buf = find_buffer(tv);
2032
2033 return buf;
2034}
2035
2036/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002037 * Get the buffer from "arg" and give an error and return NULL if it is not
2038 * valid.
2039 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002040 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002041get_buf_arg(typval_T *arg)
2042{
2043 buf_T *buf;
2044
2045 ++emsg_off;
2046 buf = tv_get_buf(arg, FALSE);
2047 --emsg_off;
2048 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002049 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002050 return buf;
2051}
2052
2053/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002054 * "bufname(expr)" function
2055 */
2056 static void
2057f_bufname(typval_T *argvars, typval_T *rettv)
2058{
2059 buf_T *buf;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002063 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 rettv->v_type = VAR_STRING;
2065 if (buf != NULL && buf->b_fname != NULL)
2066 rettv->vval.v_string = vim_strsave(buf->b_fname);
2067 else
2068 rettv->vval.v_string = NULL;
2069 --emsg_off;
2070}
2071
2072/*
2073 * "bufnr(expr)" function
2074 */
2075 static void
2076f_bufnr(typval_T *argvars, typval_T *rettv)
2077{
2078 buf_T *buf;
2079 int error = FALSE;
2080 char_u *name;
2081
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002082 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002084 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 --emsg_off;
2086
2087 /* If the buffer isn't found and the second argument is not zero create a
2088 * new buffer. */
2089 if (buf == NULL
2090 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002091 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002092 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002093 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 && !error)
2095 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2096
2097 if (buf != NULL)
2098 rettv->vval.v_number = buf->b_fnum;
2099 else
2100 rettv->vval.v_number = -1;
2101}
2102
2103 static void
2104buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2105{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 win_T *wp;
2107 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002108 buf_T *buf;
2109
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002110 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002112 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002113 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002114 {
2115 ++winnr;
2116 if (wp->w_buffer == buf)
2117 break;
2118 }
2119 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002120 --emsg_off;
2121}
2122
2123/*
2124 * "bufwinid(nr)" function
2125 */
2126 static void
2127f_bufwinid(typval_T *argvars, typval_T *rettv)
2128{
2129 buf_win_common(argvars, rettv, FALSE);
2130}
2131
2132/*
2133 * "bufwinnr(nr)" function
2134 */
2135 static void
2136f_bufwinnr(typval_T *argvars, typval_T *rettv)
2137{
2138 buf_win_common(argvars, rettv, TRUE);
2139}
2140
2141/*
2142 * "byte2line(byte)" function
2143 */
2144 static void
2145f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2146{
2147#ifndef FEAT_BYTEOFF
2148 rettv->vval.v_number = -1;
2149#else
2150 long boff = 0;
2151
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002152 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 if (boff < 0)
2154 rettv->vval.v_number = -1;
2155 else
2156 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2157 (linenr_T)0, &boff);
2158#endif
2159}
2160
2161 static void
2162byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2163{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 char_u *str;
2166 varnumber_T idx;
2167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002168 str = tv_get_string_chk(&argvars[0]);
2169 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002170 rettv->vval.v_number = -1;
2171 if (str == NULL || idx < 0)
2172 return;
2173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174 t = str;
2175 for ( ; idx > 0; idx--)
2176 {
2177 if (*t == NUL) /* EOL reached */
2178 return;
2179 if (enc_utf8 && comp)
2180 t += utf_ptr2len(t);
2181 else
2182 t += (*mb_ptr2len)(t);
2183 }
2184 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002185}
2186
2187/*
2188 * "byteidx()" function
2189 */
2190 static void
2191f_byteidx(typval_T *argvars, typval_T *rettv)
2192{
2193 byteidx(argvars, rettv, FALSE);
2194}
2195
2196/*
2197 * "byteidxcomp()" function
2198 */
2199 static void
2200f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2201{
2202 byteidx(argvars, rettv, TRUE);
2203}
2204
2205/*
2206 * "call(func, arglist [, dict])" function
2207 */
2208 static void
2209f_call(typval_T *argvars, typval_T *rettv)
2210{
2211 char_u *func;
2212 partial_T *partial = NULL;
2213 dict_T *selfdict = NULL;
2214
2215 if (argvars[1].v_type != VAR_LIST)
2216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002217 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 return;
2219 }
2220 if (argvars[1].vval.v_list == NULL)
2221 return;
2222
2223 if (argvars[0].v_type == VAR_FUNC)
2224 func = argvars[0].vval.v_string;
2225 else if (argvars[0].v_type == VAR_PARTIAL)
2226 {
2227 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002228 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 }
2230 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002231 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002232 if (*func == NUL)
2233 return; /* type error or empty name */
2234
2235 if (argvars[2].v_type != VAR_UNKNOWN)
2236 {
2237 if (argvars[2].v_type != VAR_DICT)
2238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002240 return;
2241 }
2242 selfdict = argvars[2].vval.v_dict;
2243 }
2244
2245 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2246}
2247
2248#ifdef FEAT_FLOAT
2249/*
2250 * "ceil({float})" function
2251 */
2252 static void
2253f_ceil(typval_T *argvars, typval_T *rettv)
2254{
2255 float_T f = 0.0;
2256
2257 rettv->v_type = VAR_FLOAT;
2258 if (get_float_arg(argvars, &f) == OK)
2259 rettv->vval.v_float = ceil(f);
2260 else
2261 rettv->vval.v_float = 0.0;
2262}
2263#endif
2264
2265#ifdef FEAT_JOB_CHANNEL
2266/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002267 * "ch_canread()" function
2268 */
2269 static void
2270f_ch_canread(typval_T *argvars, typval_T *rettv)
2271{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002272 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002273
2274 rettv->vval.v_number = 0;
2275 if (channel != NULL)
2276 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2277 || channel_has_readahead(channel, PART_OUT)
2278 || channel_has_readahead(channel, PART_ERR);
2279}
2280
2281/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002282 * "ch_close()" function
2283 */
2284 static void
2285f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2286{
2287 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2288
2289 if (channel != NULL)
2290 {
2291 channel_close(channel, FALSE);
2292 channel_clear(channel);
2293 }
2294}
2295
2296/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002297 * "ch_close()" function
2298 */
2299 static void
2300f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2301{
2302 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2303
2304 if (channel != NULL)
2305 channel_close_in(channel);
2306}
2307
2308/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002309 * "ch_getbufnr()" function
2310 */
2311 static void
2312f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2313{
2314 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2315
2316 rettv->vval.v_number = -1;
2317 if (channel != NULL)
2318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002319 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002320 int part;
2321
2322 if (STRCMP(what, "err") == 0)
2323 part = PART_ERR;
2324 else if (STRCMP(what, "out") == 0)
2325 part = PART_OUT;
2326 else if (STRCMP(what, "in") == 0)
2327 part = PART_IN;
2328 else
2329 part = PART_SOCK;
2330 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2331 rettv->vval.v_number =
2332 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2333 }
2334}
2335
2336/*
2337 * "ch_getjob()" function
2338 */
2339 static void
2340f_ch_getjob(typval_T *argvars, typval_T *rettv)
2341{
2342 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2343
2344 if (channel != NULL)
2345 {
2346 rettv->v_type = VAR_JOB;
2347 rettv->vval.v_job = channel->ch_job;
2348 if (channel->ch_job != NULL)
2349 ++channel->ch_job->jv_refcount;
2350 }
2351}
2352
2353/*
2354 * "ch_info()" function
2355 */
2356 static void
2357f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2358{
2359 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2360
2361 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2362 channel_info(channel, rettv->vval.v_dict);
2363}
2364
2365/*
2366 * "ch_log()" function
2367 */
2368 static void
2369f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2370{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002371 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002372 channel_T *channel = NULL;
2373
2374 if (argvars[1].v_type != VAR_UNKNOWN)
2375 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2376
Bram Moolenaard5359b22018-04-05 22:44:39 +02002377 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002378}
2379
2380/*
2381 * "ch_logfile()" function
2382 */
2383 static void
2384f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2385{
2386 char_u *fname;
2387 char_u *opt = (char_u *)"";
2388 char_u buf[NUMBUFLEN];
2389
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002390 /* Don't open a file in restricted mode. */
2391 if (check_restricted() || check_secure())
2392 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002393 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002394 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002395 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002396 ch_logfile(fname, opt);
2397}
2398
2399/*
2400 * "ch_open()" function
2401 */
2402 static void
2403f_ch_open(typval_T *argvars, typval_T *rettv)
2404{
2405 rettv->v_type = VAR_CHANNEL;
2406 if (check_restricted() || check_secure())
2407 return;
2408 rettv->vval.v_channel = channel_open_func(argvars);
2409}
2410
2411/*
2412 * "ch_read()" function
2413 */
2414 static void
2415f_ch_read(typval_T *argvars, typval_T *rettv)
2416{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002417 common_channel_read(argvars, rettv, FALSE, FALSE);
2418}
2419
2420/*
2421 * "ch_readblob()" function
2422 */
2423 static void
2424f_ch_readblob(typval_T *argvars, typval_T *rettv)
2425{
2426 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427}
2428
2429/*
2430 * "ch_readraw()" function
2431 */
2432 static void
2433f_ch_readraw(typval_T *argvars, typval_T *rettv)
2434{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002435 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002436}
2437
2438/*
2439 * "ch_evalexpr()" function
2440 */
2441 static void
2442f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2443{
2444 ch_expr_common(argvars, rettv, TRUE);
2445}
2446
2447/*
2448 * "ch_sendexpr()" function
2449 */
2450 static void
2451f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2452{
2453 ch_expr_common(argvars, rettv, FALSE);
2454}
2455
2456/*
2457 * "ch_evalraw()" function
2458 */
2459 static void
2460f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2461{
2462 ch_raw_common(argvars, rettv, TRUE);
2463}
2464
2465/*
2466 * "ch_sendraw()" function
2467 */
2468 static void
2469f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2470{
2471 ch_raw_common(argvars, rettv, FALSE);
2472}
2473
2474/*
2475 * "ch_setoptions()" function
2476 */
2477 static void
2478f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2479{
2480 channel_T *channel;
2481 jobopt_T opt;
2482
2483 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2484 if (channel == NULL)
2485 return;
2486 clear_job_options(&opt);
2487 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002488 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002489 channel_set_options(channel, &opt);
2490 free_job_options(&opt);
2491}
2492
2493/*
2494 * "ch_status()" function
2495 */
2496 static void
2497f_ch_status(typval_T *argvars, typval_T *rettv)
2498{
2499 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002500 jobopt_T opt;
2501 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002502
2503 /* return an empty string by default */
2504 rettv->v_type = VAR_STRING;
2505 rettv->vval.v_string = NULL;
2506
2507 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002508
2509 if (argvars[1].v_type != VAR_UNKNOWN)
2510 {
2511 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002512 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002513 && (opt.jo_set & JO_PART))
2514 part = opt.jo_part;
2515 }
2516
2517 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002518}
2519#endif
2520
2521/*
2522 * "changenr()" function
2523 */
2524 static void
2525f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2526{
2527 rettv->vval.v_number = curbuf->b_u_seq_cur;
2528}
2529
2530/*
2531 * "char2nr(string)" function
2532 */
2533 static void
2534f_char2nr(typval_T *argvars, typval_T *rettv)
2535{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 if (has_mbyte)
2537 {
2538 int utf8 = 0;
2539
2540 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002541 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002542
2543 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002544 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002545 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002546 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002547 }
2548 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002549 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002550}
2551
2552/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002553 * "chdir(dir)" function
2554 */
2555 static void
2556f_chdir(typval_T *argvars, typval_T *rettv)
2557{
2558 char_u *cwd;
2559 cdscope_T scope = CDSCOPE_GLOBAL;
2560
2561 rettv->v_type = VAR_STRING;
2562 rettv->vval.v_string = NULL;
2563
2564 if (argvars[0].v_type != VAR_STRING)
2565 return;
2566
2567 // Return the current directory
2568 cwd = alloc(MAXPATHL);
2569 if (cwd != NULL)
2570 {
2571 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2572 {
2573#ifdef BACKSLASH_IN_FILENAME
2574 slash_adjust(cwd);
2575#endif
2576 rettv->vval.v_string = vim_strsave(cwd);
2577 }
2578 vim_free(cwd);
2579 }
2580
2581 if (curwin->w_localdir != NULL)
2582 scope = CDSCOPE_WINDOW;
2583 else if (curtab->tp_localdir != NULL)
2584 scope = CDSCOPE_TABPAGE;
2585
2586 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2587 // Directory change failed
2588 VIM_CLEAR(rettv->vval.v_string);
2589}
2590
2591/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 * "cindent(lnum)" function
2593 */
2594 static void
2595f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2596{
2597#ifdef FEAT_CINDENT
2598 pos_T pos;
2599 linenr_T lnum;
2600
2601 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002602 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002603 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2604 {
2605 curwin->w_cursor.lnum = lnum;
2606 rettv->vval.v_number = get_c_indent();
2607 curwin->w_cursor = pos;
2608 }
2609 else
2610#endif
2611 rettv->vval.v_number = -1;
2612}
2613
Bram Moolenaaraff74912019-03-30 18:11:49 +01002614 static win_T *
2615get_optional_window(typval_T *argvars, int idx)
2616{
2617 win_T *win = curwin;
2618
2619 if (argvars[idx].v_type != VAR_UNKNOWN)
2620 {
2621 win = find_win_by_nr_or_id(&argvars[idx]);
2622 if (win == NULL)
2623 {
2624 emsg(_(e_invalwindow));
2625 return NULL;
2626 }
2627 }
2628 return win;
2629}
2630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631/*
2632 * "clearmatches()" function
2633 */
2634 static void
2635f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2636{
2637#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002638 win_T *win = get_optional_window(argvars, 0);
2639
2640 if (win != NULL)
2641 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002642#endif
2643}
2644
2645/*
2646 * "col(string)" function
2647 */
2648 static void
2649f_col(typval_T *argvars, typval_T *rettv)
2650{
2651 colnr_T col = 0;
2652 pos_T *fp;
2653 int fnum = curbuf->b_fnum;
2654
2655 fp = var2fpos(&argvars[0], FALSE, &fnum);
2656 if (fp != NULL && fnum == curbuf->b_fnum)
2657 {
2658 if (fp->col == MAXCOL)
2659 {
2660 /* '> can be MAXCOL, get the length of the line then */
2661 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2662 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2663 else
2664 col = MAXCOL;
2665 }
2666 else
2667 {
2668 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 /* col(".") when the cursor is on the NUL at the end of the line
2670 * because of "coladd" can be seen as an extra column. */
2671 if (virtual_active() && fp == &curwin->w_cursor)
2672 {
2673 char_u *p = ml_get_cursor();
2674
2675 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2676 curwin->w_virtcol - curwin->w_cursor.coladd))
2677 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002678 int l;
2679
2680 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2681 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 }
2683 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002684 }
2685 }
2686 rettv->vval.v_number = col;
2687}
2688
2689#if defined(FEAT_INS_EXPAND)
2690/*
2691 * "complete()" function
2692 */
2693 static void
2694f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2695{
2696 int startcol;
2697
2698 if ((State & INSERT) == 0)
2699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002700 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 return;
2702 }
2703
2704 /* Check for undo allowed here, because if something was already inserted
2705 * the line was already saved for undo and this check isn't done. */
2706 if (!undo_allowed())
2707 return;
2708
2709 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002711 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002712 return;
2713 }
2714
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002715 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 if (startcol <= 0)
2717 return;
2718
2719 set_completion(startcol - 1, argvars[1].vval.v_list);
2720}
2721
2722/*
2723 * "complete_add()" function
2724 */
2725 static void
2726f_complete_add(typval_T *argvars, typval_T *rettv)
2727{
2728 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2729}
2730
2731/*
2732 * "complete_check()" function
2733 */
2734 static void
2735f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2736{
2737 int saved = RedrawingDisabled;
2738
2739 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002740 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002741 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002742 RedrawingDisabled = saved;
2743}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002744
2745/*
2746 * "complete_info()" function
2747 */
2748 static void
2749f_complete_info(typval_T *argvars, typval_T *rettv)
2750{
2751 list_T *what_list = NULL;
2752
2753 if (rettv_dict_alloc(rettv) != OK)
2754 return;
2755
2756 if (argvars[0].v_type != VAR_UNKNOWN)
2757 {
2758 if (argvars[0].v_type != VAR_LIST)
2759 {
2760 emsg(_(e_listreq));
2761 return;
2762 }
2763 what_list = argvars[0].vval.v_list;
2764 }
2765 get_complete_info(what_list, rettv->vval.v_dict);
2766}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002767#endif
2768
2769/*
2770 * "confirm(message, buttons[, default [, type]])" function
2771 */
2772 static void
2773f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2774{
2775#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2776 char_u *message;
2777 char_u *buttons = NULL;
2778 char_u buf[NUMBUFLEN];
2779 char_u buf2[NUMBUFLEN];
2780 int def = 1;
2781 int type = VIM_GENERIC;
2782 char_u *typestr;
2783 int error = FALSE;
2784
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 if (message == NULL)
2787 error = TRUE;
2788 if (argvars[1].v_type != VAR_UNKNOWN)
2789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002790 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002791 if (buttons == NULL)
2792 error = TRUE;
2793 if (argvars[2].v_type != VAR_UNKNOWN)
2794 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002795 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002796 if (argvars[3].v_type != VAR_UNKNOWN)
2797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002798 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002799 if (typestr == NULL)
2800 error = TRUE;
2801 else
2802 {
2803 switch (TOUPPER_ASC(*typestr))
2804 {
2805 case 'E': type = VIM_ERROR; break;
2806 case 'Q': type = VIM_QUESTION; break;
2807 case 'I': type = VIM_INFO; break;
2808 case 'W': type = VIM_WARNING; break;
2809 case 'G': type = VIM_GENERIC; break;
2810 }
2811 }
2812 }
2813 }
2814 }
2815
2816 if (buttons == NULL || *buttons == NUL)
2817 buttons = (char_u *)_("&Ok");
2818
2819 if (!error)
2820 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2821 def, NULL, FALSE);
2822#endif
2823}
2824
2825/*
2826 * "copy()" function
2827 */
2828 static void
2829f_copy(typval_T *argvars, typval_T *rettv)
2830{
2831 item_copy(&argvars[0], rettv, FALSE, 0);
2832}
2833
2834#ifdef FEAT_FLOAT
2835/*
2836 * "cos()" function
2837 */
2838 static void
2839f_cos(typval_T *argvars, typval_T *rettv)
2840{
2841 float_T f = 0.0;
2842
2843 rettv->v_type = VAR_FLOAT;
2844 if (get_float_arg(argvars, &f) == OK)
2845 rettv->vval.v_float = cos(f);
2846 else
2847 rettv->vval.v_float = 0.0;
2848}
2849
2850/*
2851 * "cosh()" function
2852 */
2853 static void
2854f_cosh(typval_T *argvars, typval_T *rettv)
2855{
2856 float_T f = 0.0;
2857
2858 rettv->v_type = VAR_FLOAT;
2859 if (get_float_arg(argvars, &f) == OK)
2860 rettv->vval.v_float = cosh(f);
2861 else
2862 rettv->vval.v_float = 0.0;
2863}
2864#endif
2865
2866/*
2867 * "count()" function
2868 */
2869 static void
2870f_count(typval_T *argvars, typval_T *rettv)
2871{
2872 long n = 0;
2873 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002874 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002875
Bram Moolenaar9966b212017-07-28 16:46:57 +02002876 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002877 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002878
2879 if (argvars[0].v_type == VAR_STRING)
2880 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002881 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002882 char_u *p = argvars[0].vval.v_string;
2883 char_u *next;
2884
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002885 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002886 {
2887 if (ic)
2888 {
2889 size_t len = STRLEN(expr);
2890
2891 while (*p != NUL)
2892 {
2893 if (MB_STRNICMP(p, expr, len) == 0)
2894 {
2895 ++n;
2896 p += len;
2897 }
2898 else
2899 MB_PTR_ADV(p);
2900 }
2901 }
2902 else
2903 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2904 != NULL)
2905 {
2906 ++n;
2907 p = next + STRLEN(expr);
2908 }
2909 }
2910
2911 }
2912 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 {
2914 listitem_T *li;
2915 list_T *l;
2916 long idx;
2917
2918 if ((l = argvars[0].vval.v_list) != NULL)
2919 {
2920 li = l->lv_first;
2921 if (argvars[2].v_type != VAR_UNKNOWN)
2922 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002923 if (argvars[3].v_type != VAR_UNKNOWN)
2924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002925 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002926 if (!error)
2927 {
2928 li = list_find(l, idx);
2929 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002930 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 }
2932 }
2933 if (error)
2934 li = NULL;
2935 }
2936
2937 for ( ; li != NULL; li = li->li_next)
2938 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2939 ++n;
2940 }
2941 }
2942 else if (argvars[0].v_type == VAR_DICT)
2943 {
2944 int todo;
2945 dict_T *d;
2946 hashitem_T *hi;
2947
2948 if ((d = argvars[0].vval.v_dict) != NULL)
2949 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002950 if (argvars[2].v_type != VAR_UNKNOWN)
2951 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002952 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 }
2955
2956 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2957 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2958 {
2959 if (!HASHITEM_EMPTY(hi))
2960 {
2961 --todo;
2962 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2963 ++n;
2964 }
2965 }
2966 }
2967 }
2968 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002970 rettv->vval.v_number = n;
2971}
2972
2973/*
2974 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2975 *
2976 * Checks the existence of a cscope connection.
2977 */
2978 static void
2979f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2980{
2981#ifdef FEAT_CSCOPE
2982 int num = 0;
2983 char_u *dbpath = NULL;
2984 char_u *prepend = NULL;
2985 char_u buf[NUMBUFLEN];
2986
2987 if (argvars[0].v_type != VAR_UNKNOWN
2988 && argvars[1].v_type != VAR_UNKNOWN)
2989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002990 num = (int)tv_get_number(&argvars[0]);
2991 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002992 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002993 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002994 }
2995
2996 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2997#endif
2998}
2999
3000/*
3001 * "cursor(lnum, col)" function, or
3002 * "cursor(list)"
3003 *
3004 * Moves the cursor to the specified line and column.
3005 * Returns 0 when the position could be set, -1 otherwise.
3006 */
3007 static void
3008f_cursor(typval_T *argvars, typval_T *rettv)
3009{
3010 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003011 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 int set_curswant = TRUE;
3013
3014 rettv->vval.v_number = -1;
3015 if (argvars[1].v_type == VAR_UNKNOWN)
3016 {
3017 pos_T pos;
3018 colnr_T curswant = -1;
3019
3020 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003023 return;
3024 }
3025 line = pos.lnum;
3026 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 if (curswant >= 0)
3029 {
3030 curwin->w_curswant = curswant - 1;
3031 set_curswant = FALSE;
3032 }
3033 }
3034 else
3035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003036 line = tv_get_lnum(argvars);
3037 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003039 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003041 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 return; /* type error; errmsg already given */
3043 if (line > 0)
3044 curwin->w_cursor.lnum = line;
3045 if (col > 0)
3046 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003047 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048
3049 /* Make sure the cursor is in a valid position. */
3050 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 /* Correct cursor for multi-byte character. */
3052 if (has_mbyte)
3053 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003054
3055 curwin->w_set_curswant = set_curswant;
3056 rettv->vval.v_number = 0;
3057}
3058
Bram Moolenaar4f974752019-02-17 17:44:42 +01003059#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003060/*
3061 * "debugbreak()" function
3062 */
3063 static void
3064f_debugbreak(typval_T *argvars, typval_T *rettv)
3065{
3066 int pid;
3067
3068 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003069 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003070 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003071 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003072 else
3073 {
3074 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3075
3076 if (hProcess != NULL)
3077 {
3078 DebugBreakProcess(hProcess);
3079 CloseHandle(hProcess);
3080 rettv->vval.v_number = OK;
3081 }
3082 }
3083}
3084#endif
3085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086/*
3087 * "deepcopy()" function
3088 */
3089 static void
3090f_deepcopy(typval_T *argvars, typval_T *rettv)
3091{
3092 int noref = 0;
3093 int copyID;
3094
3095 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003096 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003097 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003098 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003099 else
3100 {
3101 copyID = get_copyID();
3102 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3103 }
3104}
3105
3106/*
3107 * "delete()" function
3108 */
3109 static void
3110f_delete(typval_T *argvars, typval_T *rettv)
3111{
3112 char_u nbuf[NUMBUFLEN];
3113 char_u *name;
3114 char_u *flags;
3115
3116 rettv->vval.v_number = -1;
3117 if (check_restricted() || check_secure())
3118 return;
3119
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003120 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003121 if (name == NULL || *name == NUL)
3122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003123 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003124 return;
3125 }
3126
3127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003128 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129 else
3130 flags = (char_u *)"";
3131
3132 if (*flags == NUL)
3133 /* delete a file */
3134 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3135 else if (STRCMP(flags, "d") == 0)
3136 /* delete an empty directory */
3137 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3138 else if (STRCMP(flags, "rf") == 0)
3139 /* delete a directory recursively */
3140 rettv->vval.v_number = delete_recursive(name);
3141 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003142 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003143}
3144
3145/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003146 * "deletebufline()" function
3147 */
3148 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003149f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003150{
3151 buf_T *buf;
3152 linenr_T first, last;
3153 linenr_T lnum;
3154 long count;
3155 int is_curbuf;
3156 buf_T *curbuf_save = NULL;
3157 win_T *curwin_save = NULL;
3158 tabpage_T *tp;
3159 win_T *wp;
3160
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003161 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003162 if (buf == NULL)
3163 {
3164 rettv->vval.v_number = 1; /* FAIL */
3165 return;
3166 }
3167 is_curbuf = buf == curbuf;
3168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003169 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003171 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003172 else
3173 last = first;
3174
3175 if (buf->b_ml.ml_mfp == NULL || first < 1
3176 || first > buf->b_ml.ml_line_count || last < first)
3177 {
3178 rettv->vval.v_number = 1; /* FAIL */
3179 return;
3180 }
3181
3182 if (!is_curbuf)
3183 {
3184 curbuf_save = curbuf;
3185 curwin_save = curwin;
3186 curbuf = buf;
3187 find_win_for_curbuf();
3188 }
3189 if (last > curbuf->b_ml.ml_line_count)
3190 last = curbuf->b_ml.ml_line_count;
3191 count = last - first + 1;
3192
3193 // When coming here from Insert mode, sync undo, so that this can be
3194 // undone separately from what was previously inserted.
3195 if (u_sync_once == 2)
3196 {
3197 u_sync_once = 1; // notify that u_sync() was called
3198 u_sync(TRUE);
3199 }
3200
3201 if (u_save(first - 1, last + 1) == FAIL)
3202 {
3203 rettv->vval.v_number = 1; /* FAIL */
3204 return;
3205 }
3206
3207 for (lnum = first; lnum <= last; ++lnum)
3208 ml_delete(first, TRUE);
3209
3210 FOR_ALL_TAB_WINDOWS(tp, wp)
3211 if (wp->w_buffer == buf)
3212 {
3213 if (wp->w_cursor.lnum > last)
3214 wp->w_cursor.lnum -= count;
3215 else if (wp->w_cursor.lnum> first)
3216 wp->w_cursor.lnum = first;
3217 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3218 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3219 }
3220 check_cursor_col();
3221 deleted_lines_mark(first, count);
3222
3223 if (!is_curbuf)
3224 {
3225 curbuf = curbuf_save;
3226 curwin = curwin_save;
3227 }
3228}
3229
3230/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003231 * "did_filetype()" function
3232 */
3233 static void
3234f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3235{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237}
3238
3239/*
3240 * "diff_filler()" function
3241 */
3242 static void
3243f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3244{
3245#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003246 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247#endif
3248}
3249
3250/*
3251 * "diff_hlID()" function
3252 */
3253 static void
3254f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3255{
3256#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003257 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003259 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 static int fnum = 0;
3261 static int change_start = 0;
3262 static int change_end = 0;
3263 static hlf_T hlID = (hlf_T)0;
3264 int filler_lines;
3265 int col;
3266
3267 if (lnum < 0) /* ignore type error in {lnum} arg */
3268 lnum = 0;
3269 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003270 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003271 || fnum != curbuf->b_fnum)
3272 {
3273 /* New line, buffer, change: need to get the values. */
3274 filler_lines = diff_check(curwin, lnum);
3275 if (filler_lines < 0)
3276 {
3277 if (filler_lines == -1)
3278 {
3279 change_start = MAXCOL;
3280 change_end = -1;
3281 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3282 hlID = HLF_ADD; /* added line */
3283 else
3284 hlID = HLF_CHD; /* changed line */
3285 }
3286 else
3287 hlID = HLF_ADD; /* added line */
3288 }
3289 else
3290 hlID = (hlf_T)0;
3291 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003292 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003293 fnum = curbuf->b_fnum;
3294 }
3295
3296 if (hlID == HLF_CHD || hlID == HLF_TXD)
3297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003298 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003299 if (col >= change_start && col <= change_end)
3300 hlID = HLF_TXD; /* changed text */
3301 else
3302 hlID = HLF_CHD; /* changed line */
3303 }
3304 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3305#endif
3306}
3307
3308/*
3309 * "empty({expr})" function
3310 */
3311 static void
3312f_empty(typval_T *argvars, typval_T *rettv)
3313{
3314 int n = FALSE;
3315
3316 switch (argvars[0].v_type)
3317 {
3318 case VAR_STRING:
3319 case VAR_FUNC:
3320 n = argvars[0].vval.v_string == NULL
3321 || *argvars[0].vval.v_string == NUL;
3322 break;
3323 case VAR_PARTIAL:
3324 n = FALSE;
3325 break;
3326 case VAR_NUMBER:
3327 n = argvars[0].vval.v_number == 0;
3328 break;
3329 case VAR_FLOAT:
3330#ifdef FEAT_FLOAT
3331 n = argvars[0].vval.v_float == 0.0;
3332 break;
3333#endif
3334 case VAR_LIST:
3335 n = argvars[0].vval.v_list == NULL
3336 || argvars[0].vval.v_list->lv_first == NULL;
3337 break;
3338 case VAR_DICT:
3339 n = argvars[0].vval.v_dict == NULL
3340 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3341 break;
3342 case VAR_SPECIAL:
3343 n = argvars[0].vval.v_number != VVAL_TRUE;
3344 break;
3345
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003346 case VAR_BLOB:
3347 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003348 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3349 break;
3350
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 case VAR_JOB:
3352#ifdef FEAT_JOB_CHANNEL
3353 n = argvars[0].vval.v_job == NULL
3354 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3355 break;
3356#endif
3357 case VAR_CHANNEL:
3358#ifdef FEAT_JOB_CHANNEL
3359 n = argvars[0].vval.v_channel == NULL
3360 || !channel_is_open(argvars[0].vval.v_channel);
3361 break;
3362#endif
3363 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003364 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003365 n = TRUE;
3366 break;
3367 }
3368
3369 rettv->vval.v_number = n;
3370}
3371
3372/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003373 * "environ()" function
3374 */
3375 static void
3376f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3377{
3378#if !defined(AMIGA)
3379 int i = 0;
3380 char_u *entry, *value;
3381# ifdef MSWIN
3382 extern wchar_t **_wenviron;
3383# else
3384 extern char **environ;
3385# endif
3386
3387 if (rettv_dict_alloc(rettv) != OK)
3388 return;
3389
3390# ifdef MSWIN
3391 if (*_wenviron == NULL)
3392 return;
3393# else
3394 if (*environ == NULL)
3395 return;
3396# endif
3397
3398 for (i = 0; ; ++i)
3399 {
3400# ifdef MSWIN
3401 short_u *p;
3402
3403 if ((p = (short_u *)_wenviron[i]) == NULL)
3404 return;
3405 entry = utf16_to_enc(p, NULL);
3406# else
3407 if ((entry = (char_u *)environ[i]) == NULL)
3408 return;
3409 entry = vim_strsave(entry);
3410# endif
3411 if (entry == NULL) // out of memory
3412 return;
3413 if ((value = vim_strchr(entry, '=')) == NULL)
3414 {
3415 vim_free(entry);
3416 continue;
3417 }
3418 *value++ = NUL;
3419 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3420 vim_free(entry);
3421 }
3422#endif
3423}
3424
3425/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 * "escape({string}, {chars})" function
3427 */
3428 static void
3429f_escape(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u buf[NUMBUFLEN];
3432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3434 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003435 rettv->v_type = VAR_STRING;
3436}
3437
3438/*
3439 * "eval()" function
3440 */
3441 static void
3442f_eval(typval_T *argvars, typval_T *rettv)
3443{
3444 char_u *s, *p;
3445
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003446 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447 if (s != NULL)
3448 s = skipwhite(s);
3449
3450 p = s;
3451 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3452 {
3453 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003454 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003455 need_clr_eos = FALSE;
3456 rettv->v_type = VAR_NUMBER;
3457 rettv->vval.v_number = 0;
3458 }
3459 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003460 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003461}
3462
3463/*
3464 * "eventhandler()" function
3465 */
3466 static void
3467f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3468{
3469 rettv->vval.v_number = vgetc_busy;
3470}
3471
3472/*
3473 * "executable()" function
3474 */
3475 static void
3476f_executable(typval_T *argvars, typval_T *rettv)
3477{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003478 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003479
3480 /* Check in $PATH and also check directly if there is a directory name. */
3481 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3482 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3483}
3484
3485static garray_T redir_execute_ga;
3486
3487/*
3488 * Append "value[value_len]" to the execute() output.
3489 */
3490 void
3491execute_redir_str(char_u *value, int value_len)
3492{
3493 int len;
3494
3495 if (value_len == -1)
3496 len = (int)STRLEN(value); /* Append the entire string */
3497 else
3498 len = value_len; /* Append only "value_len" characters */
3499 if (ga_grow(&redir_execute_ga, len) == OK)
3500 {
3501 mch_memmove((char *)redir_execute_ga.ga_data
3502 + redir_execute_ga.ga_len, value, len);
3503 redir_execute_ga.ga_len += len;
3504 }
3505}
3506
3507/*
3508 * Get next line from a list.
3509 * Called by do_cmdline() to get the next line.
3510 * Returns allocated string, or NULL for end of function.
3511 */
3512
3513 static char_u *
3514get_list_line(
3515 int c UNUSED,
3516 void *cookie,
3517 int indent UNUSED)
3518{
3519 listitem_T **p = (listitem_T **)cookie;
3520 listitem_T *item = *p;
3521 char_u buf[NUMBUFLEN];
3522 char_u *s;
3523
3524 if (item == NULL)
3525 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003526 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527 *p = item->li_next;
3528 return s == NULL ? NULL : vim_strsave(s);
3529}
3530
3531/*
3532 * "execute()" function
3533 */
3534 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003535execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536{
3537 char_u *cmd = NULL;
3538 list_T *list = NULL;
3539 int save_msg_silent = msg_silent;
3540 int save_emsg_silent = emsg_silent;
3541 int save_emsg_noredir = emsg_noredir;
3542 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003543 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003544 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003545 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003546 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003547
3548 rettv->vval.v_string = NULL;
3549 rettv->v_type = VAR_STRING;
3550
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003551 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003552 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003553 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 if (list == NULL || list->lv_first == NULL)
3555 /* empty list, no commands, empty output */
3556 return;
3557 ++list->lv_refcount;
3558 }
3559 else
3560 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003561 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 if (cmd == NULL)
3563 return;
3564 }
3565
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003566 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003567 {
3568 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003569 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003570
3571 if (s == NULL)
3572 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003573 if (*s == NUL)
3574 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003575 if (STRNCMP(s, "silent", 6) == 0)
3576 ++msg_silent;
3577 if (STRCMP(s, "silent!") == 0)
3578 {
3579 emsg_silent = TRUE;
3580 emsg_noredir = TRUE;
3581 }
3582 }
3583 else
3584 ++msg_silent;
3585
3586 if (redir_execute)
3587 save_ga = redir_execute_ga;
3588 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3589 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003590 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003591 if (!echo_output)
3592 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003593
3594 if (cmd != NULL)
3595 do_cmdline_cmd(cmd);
3596 else
3597 {
3598 listitem_T *item = list->lv_first;
3599
3600 do_cmdline(NULL, get_list_line, (void *)&item,
3601 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3602 --list->lv_refcount;
3603 }
3604
Bram Moolenaard297f352017-01-29 20:31:21 +01003605 /* Need to append a NUL to the result. */
3606 if (ga_grow(&redir_execute_ga, 1) == OK)
3607 {
3608 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3609 rettv->vval.v_string = redir_execute_ga.ga_data;
3610 }
3611 else
3612 {
3613 ga_clear(&redir_execute_ga);
3614 rettv->vval.v_string = NULL;
3615 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003616 msg_silent = save_msg_silent;
3617 emsg_silent = save_emsg_silent;
3618 emsg_noredir = save_emsg_noredir;
3619
3620 redir_execute = save_redir_execute;
3621 if (redir_execute)
3622 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003623 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003625 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003626 if (echo_output)
3627 // When not working silently: put it in column zero. A following
3628 // "echon" will overwrite the message, unavoidably.
3629 msg_col = 0;
3630 else
3631 // When working silently: Put it back where it was, since nothing
3632 // should have been written.
3633 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003634}
3635
3636/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003637 * "execute()" function
3638 */
3639 static void
3640f_execute(typval_T *argvars, typval_T *rettv)
3641{
3642 execute_common(argvars, rettv, 0);
3643}
3644
3645/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646 * "exepath()" function
3647 */
3648 static void
3649f_exepath(typval_T *argvars, typval_T *rettv)
3650{
3651 char_u *p = NULL;
3652
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003653 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 rettv->v_type = VAR_STRING;
3655 rettv->vval.v_string = p;
3656}
3657
3658/*
3659 * "exists()" function
3660 */
3661 static void
3662f_exists(typval_T *argvars, typval_T *rettv)
3663{
3664 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003665 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003667 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 if (*p == '$') /* environment variable */
3669 {
3670 /* first try "normal" environment variables (fast) */
3671 if (mch_getenv(p + 1) != NULL)
3672 n = TRUE;
3673 else
3674 {
3675 /* try expanding things like $VIM and ${HOME} */
3676 p = expand_env_save(p);
3677 if (p != NULL && *p != '$')
3678 n = TRUE;
3679 vim_free(p);
3680 }
3681 }
3682 else if (*p == '&' || *p == '+') /* option */
3683 {
3684 n = (get_option_tv(&p, NULL, TRUE) == OK);
3685 if (*skipwhite(p) != NUL)
3686 n = FALSE; /* trailing garbage */
3687 }
3688 else if (*p == '*') /* internal or user defined function */
3689 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003690 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 }
3692 else if (*p == ':')
3693 {
3694 n = cmd_exists(p + 1);
3695 }
3696 else if (*p == '#')
3697 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003698 if (p[1] == '#')
3699 n = autocmd_supported(p + 2);
3700 else
3701 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 }
3703 else /* internal variable */
3704 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003705 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707
3708 rettv->vval.v_number = n;
3709}
3710
3711#ifdef FEAT_FLOAT
3712/*
3713 * "exp()" function
3714 */
3715 static void
3716f_exp(typval_T *argvars, typval_T *rettv)
3717{
3718 float_T f = 0.0;
3719
3720 rettv->v_type = VAR_FLOAT;
3721 if (get_float_arg(argvars, &f) == OK)
3722 rettv->vval.v_float = exp(f);
3723 else
3724 rettv->vval.v_float = 0.0;
3725}
3726#endif
3727
3728/*
3729 * "expand()" function
3730 */
3731 static void
3732f_expand(typval_T *argvars, typval_T *rettv)
3733{
3734 char_u *s;
3735 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003736 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3738 expand_T xpc;
3739 int error = FALSE;
3740 char_u *result;
3741
3742 rettv->v_type = VAR_STRING;
3743 if (argvars[1].v_type != VAR_UNKNOWN
3744 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003745 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003746 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003747 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003749 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 if (*s == '%' || *s == '#' || *s == '<')
3751 {
3752 ++emsg_off;
3753 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3754 --emsg_off;
3755 if (rettv->v_type == VAR_LIST)
3756 {
3757 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3758 list_append_string(rettv->vval.v_list, result, -1);
3759 else
3760 vim_free(result);
3761 }
3762 else
3763 rettv->vval.v_string = result;
3764 }
3765 else
3766 {
3767 /* When the optional second argument is non-zero, don't remove matches
3768 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3769 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003770 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003771 options |= WILD_KEEP_ALL;
3772 if (!error)
3773 {
3774 ExpandInit(&xpc);
3775 xpc.xp_context = EXPAND_FILES;
3776 if (p_wic)
3777 options += WILD_ICASE;
3778 if (rettv->v_type == VAR_STRING)
3779 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3780 options, WILD_ALL);
3781 else if (rettv_list_alloc(rettv) != FAIL)
3782 {
3783 int i;
3784
3785 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3786 for (i = 0; i < xpc.xp_numfiles; i++)
3787 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3788 ExpandCleanup(&xpc);
3789 }
3790 }
3791 else
3792 rettv->vval.v_string = NULL;
3793 }
3794}
3795
3796/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003797 * "expandcmd()" function
3798 * Expand all the special characters in a command string.
3799 */
3800 static void
3801f_expandcmd(typval_T *argvars, typval_T *rettv)
3802{
3803 exarg_T eap;
3804 char_u *cmdstr;
3805 char *errormsg = NULL;
3806
3807 rettv->v_type = VAR_STRING;
3808 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3809
3810 memset(&eap, 0, sizeof(eap));
3811 eap.cmd = cmdstr;
3812 eap.arg = cmdstr;
3813 eap.argt |= NOSPC;
3814 eap.usefilter = FALSE;
3815 eap.nextcmd = NULL;
3816 eap.cmdidx = CMD_USER;
3817
3818 expand_filename(&eap, &cmdstr, &errormsg);
3819 if (errormsg != NULL && *errormsg != NUL)
3820 emsg(errormsg);
3821
3822 rettv->vval.v_string = cmdstr;
3823}
3824
3825/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003826 * "extend(list, list [, idx])" function
3827 * "extend(dict, dict [, action])" function
3828 */
3829 static void
3830f_extend(typval_T *argvars, typval_T *rettv)
3831{
3832 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3833
3834 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3835 {
3836 list_T *l1, *l2;
3837 listitem_T *item;
3838 long before;
3839 int error = FALSE;
3840
3841 l1 = argvars[0].vval.v_list;
3842 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003843 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 && l2 != NULL)
3845 {
3846 if (argvars[2].v_type != VAR_UNKNOWN)
3847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003848 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003849 if (error)
3850 return; /* type error; errmsg already given */
3851
3852 if (before == l1->lv_len)
3853 item = NULL;
3854 else
3855 {
3856 item = list_find(l1, before);
3857 if (item == NULL)
3858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003859 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003860 return;
3861 }
3862 }
3863 }
3864 else
3865 item = NULL;
3866 list_extend(l1, l2, item);
3867
3868 copy_tv(&argvars[0], rettv);
3869 }
3870 }
3871 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3872 {
3873 dict_T *d1, *d2;
3874 char_u *action;
3875 int i;
3876
3877 d1 = argvars[0].vval.v_dict;
3878 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003879 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003880 && d2 != NULL)
3881 {
3882 /* Check the third argument. */
3883 if (argvars[2].v_type != VAR_UNKNOWN)
3884 {
3885 static char *(av[]) = {"keep", "force", "error"};
3886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003887 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003888 if (action == NULL)
3889 return; /* type error; errmsg already given */
3890 for (i = 0; i < 3; ++i)
3891 if (STRCMP(action, av[i]) == 0)
3892 break;
3893 if (i == 3)
3894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896 return;
3897 }
3898 }
3899 else
3900 action = (char_u *)"force";
3901
3902 dict_extend(d1, d2, action);
3903
3904 copy_tv(&argvars[0], rettv);
3905 }
3906 }
3907 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003908 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909}
3910
3911/*
3912 * "feedkeys()" function
3913 */
3914 static void
3915f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3916{
3917 int remap = TRUE;
3918 int insert = FALSE;
3919 char_u *keys, *flags;
3920 char_u nbuf[NUMBUFLEN];
3921 int typed = FALSE;
3922 int execute = FALSE;
3923 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003924 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003925 char_u *keys_esc;
3926
3927 /* This is not allowed in the sandbox. If the commands would still be
3928 * executed in the sandbox it would be OK, but it probably happens later,
3929 * when "sandbox" is no longer set. */
3930 if (check_secure())
3931 return;
3932
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003933 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003934
3935 if (argvars[1].v_type != VAR_UNKNOWN)
3936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003937 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938 for ( ; *flags != NUL; ++flags)
3939 {
3940 switch (*flags)
3941 {
3942 case 'n': remap = FALSE; break;
3943 case 'm': remap = TRUE; break;
3944 case 't': typed = TRUE; break;
3945 case 'i': insert = TRUE; break;
3946 case 'x': execute = TRUE; break;
3947 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003948 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003949 }
3950 }
3951 }
3952
3953 if (*keys != NUL || execute)
3954 {
3955 /* Need to escape K_SPECIAL and CSI before putting the string in the
3956 * typeahead buffer. */
3957 keys_esc = vim_strsave_escape_csi(keys);
3958 if (keys_esc != NULL)
3959 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003960 if (lowlevel)
3961 {
3962#ifdef USE_INPUT_BUF
3963 add_to_input_buf(keys, (int)STRLEN(keys));
3964#else
3965 emsg(_("E980: lowlevel input not supported"));
3966#endif
3967 }
3968 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003969 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003970 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003971 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003972 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003973#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003974 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003975#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003976 )
3977 typebuf_was_filled = TRUE;
3978 }
3979 vim_free(keys_esc);
3980
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003981 if (execute)
3982 {
3983 int save_msg_scroll = msg_scroll;
3984
3985 /* Avoid a 1 second delay when the keys start Insert mode. */
3986 msg_scroll = FALSE;
3987
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003988 if (!dangerous)
3989 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003990 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003991 if (!dangerous)
3992 --ex_normal_busy;
3993
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994 msg_scroll |= save_msg_scroll;
3995 }
3996 }
3997 }
3998}
3999
4000/*
4001 * "filereadable()" function
4002 */
4003 static void
4004f_filereadable(typval_T *argvars, typval_T *rettv)
4005{
4006 int fd;
4007 char_u *p;
4008 int n;
4009
4010#ifndef O_NONBLOCK
4011# define O_NONBLOCK 0
4012#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004013 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004014 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4015 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4016 {
4017 n = TRUE;
4018 close(fd);
4019 }
4020 else
4021 n = FALSE;
4022
4023 rettv->vval.v_number = n;
4024}
4025
4026/*
4027 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4028 * rights to write into.
4029 */
4030 static void
4031f_filewritable(typval_T *argvars, typval_T *rettv)
4032{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004033 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004034}
4035
4036 static void
4037findfilendir(
4038 typval_T *argvars UNUSED,
4039 typval_T *rettv,
4040 int find_what UNUSED)
4041{
4042#ifdef FEAT_SEARCHPATH
4043 char_u *fname;
4044 char_u *fresult = NULL;
4045 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4046 char_u *p;
4047 char_u pathbuf[NUMBUFLEN];
4048 int count = 1;
4049 int first = TRUE;
4050 int error = FALSE;
4051#endif
4052
4053 rettv->vval.v_string = NULL;
4054 rettv->v_type = VAR_STRING;
4055
4056#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004057 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004058
4059 if (argvars[1].v_type != VAR_UNKNOWN)
4060 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004061 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004062 if (p == NULL)
4063 error = TRUE;
4064 else
4065 {
4066 if (*p != NUL)
4067 path = p;
4068
4069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004070 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004071 }
4072 }
4073
4074 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4075 error = TRUE;
4076
4077 if (*fname != NUL && !error)
4078 {
4079 do
4080 {
4081 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4082 vim_free(fresult);
4083 fresult = find_file_in_path_option(first ? fname : NULL,
4084 first ? (int)STRLEN(fname) : 0,
4085 0, first, path,
4086 find_what,
4087 curbuf->b_ffname,
4088 find_what == FINDFILE_DIR
4089 ? (char_u *)"" : curbuf->b_p_sua);
4090 first = FALSE;
4091
4092 if (fresult != NULL && rettv->v_type == VAR_LIST)
4093 list_append_string(rettv->vval.v_list, fresult, -1);
4094
4095 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4096 }
4097
4098 if (rettv->v_type == VAR_STRING)
4099 rettv->vval.v_string = fresult;
4100#endif
4101}
4102
4103/*
4104 * "filter()" function
4105 */
4106 static void
4107f_filter(typval_T *argvars, typval_T *rettv)
4108{
4109 filter_map(argvars, rettv, FALSE);
4110}
4111
4112/*
4113 * "finddir({fname}[, {path}[, {count}]])" function
4114 */
4115 static void
4116f_finddir(typval_T *argvars, typval_T *rettv)
4117{
4118 findfilendir(argvars, rettv, FINDFILE_DIR);
4119}
4120
4121/*
4122 * "findfile({fname}[, {path}[, {count}]])" function
4123 */
4124 static void
4125f_findfile(typval_T *argvars, typval_T *rettv)
4126{
4127 findfilendir(argvars, rettv, FINDFILE_FILE);
4128}
4129
4130#ifdef FEAT_FLOAT
4131/*
4132 * "float2nr({float})" function
4133 */
4134 static void
4135f_float2nr(typval_T *argvars, typval_T *rettv)
4136{
4137 float_T f = 0.0;
4138
4139 if (get_float_arg(argvars, &f) == OK)
4140 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004141 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004142 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004143 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004144 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004145 else
4146 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004147 }
4148}
4149
4150/*
4151 * "floor({float})" function
4152 */
4153 static void
4154f_floor(typval_T *argvars, typval_T *rettv)
4155{
4156 float_T f = 0.0;
4157
4158 rettv->v_type = VAR_FLOAT;
4159 if (get_float_arg(argvars, &f) == OK)
4160 rettv->vval.v_float = floor(f);
4161 else
4162 rettv->vval.v_float = 0.0;
4163}
4164
4165/*
4166 * "fmod()" function
4167 */
4168 static void
4169f_fmod(typval_T *argvars, typval_T *rettv)
4170{
4171 float_T fx = 0.0, fy = 0.0;
4172
4173 rettv->v_type = VAR_FLOAT;
4174 if (get_float_arg(argvars, &fx) == OK
4175 && get_float_arg(&argvars[1], &fy) == OK)
4176 rettv->vval.v_float = fmod(fx, fy);
4177 else
4178 rettv->vval.v_float = 0.0;
4179}
4180#endif
4181
4182/*
4183 * "fnameescape({string})" function
4184 */
4185 static void
4186f_fnameescape(typval_T *argvars, typval_T *rettv)
4187{
4188 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004189 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004190 rettv->v_type = VAR_STRING;
4191}
4192
4193/*
4194 * "fnamemodify({fname}, {mods})" function
4195 */
4196 static void
4197f_fnamemodify(typval_T *argvars, typval_T *rettv)
4198{
4199 char_u *fname;
4200 char_u *mods;
4201 int usedlen = 0;
4202 int len;
4203 char_u *fbuf = NULL;
4204 char_u buf[NUMBUFLEN];
4205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004206 fname = tv_get_string_chk(&argvars[0]);
4207 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004208 if (fname == NULL || mods == NULL)
4209 fname = NULL;
4210 else
4211 {
4212 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004213 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004214 }
4215
4216 rettv->v_type = VAR_STRING;
4217 if (fname == NULL)
4218 rettv->vval.v_string = NULL;
4219 else
4220 rettv->vval.v_string = vim_strnsave(fname, len);
4221 vim_free(fbuf);
4222}
4223
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004224/*
4225 * "foldclosed()" function
4226 */
4227 static void
4228foldclosed_both(
4229 typval_T *argvars UNUSED,
4230 typval_T *rettv,
4231 int end UNUSED)
4232{
4233#ifdef FEAT_FOLDING
4234 linenr_T lnum;
4235 linenr_T first, last;
4236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004237 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4239 {
4240 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4241 {
4242 if (end)
4243 rettv->vval.v_number = (varnumber_T)last;
4244 else
4245 rettv->vval.v_number = (varnumber_T)first;
4246 return;
4247 }
4248 }
4249#endif
4250 rettv->vval.v_number = -1;
4251}
4252
4253/*
4254 * "foldclosed()" function
4255 */
4256 static void
4257f_foldclosed(typval_T *argvars, typval_T *rettv)
4258{
4259 foldclosed_both(argvars, rettv, FALSE);
4260}
4261
4262/*
4263 * "foldclosedend()" function
4264 */
4265 static void
4266f_foldclosedend(typval_T *argvars, typval_T *rettv)
4267{
4268 foldclosed_both(argvars, rettv, TRUE);
4269}
4270
4271/*
4272 * "foldlevel()" function
4273 */
4274 static void
4275f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4276{
4277#ifdef FEAT_FOLDING
4278 linenr_T lnum;
4279
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004280 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004281 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4282 rettv->vval.v_number = foldLevel(lnum);
4283#endif
4284}
4285
4286/*
4287 * "foldtext()" function
4288 */
4289 static void
4290f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4291{
4292#ifdef FEAT_FOLDING
4293 linenr_T foldstart;
4294 linenr_T foldend;
4295 char_u *dashes;
4296 linenr_T lnum;
4297 char_u *s;
4298 char_u *r;
4299 int len;
4300 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004301 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302#endif
4303
4304 rettv->v_type = VAR_STRING;
4305 rettv->vval.v_string = NULL;
4306#ifdef FEAT_FOLDING
4307 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4308 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4309 dashes = get_vim_var_str(VV_FOLDDASHES);
4310 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4311 && dashes != NULL)
4312 {
4313 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004314 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 if (!linewhite(lnum))
4316 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004317
4318 /* Find interesting text in this line. */
4319 s = skipwhite(ml_get(lnum));
4320 /* skip C comment-start */
4321 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4322 {
4323 s = skipwhite(s + 2);
4324 if (*skipwhite(s) == NUL
4325 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4326 {
4327 s = skipwhite(ml_get(lnum + 1));
4328 if (*s == '*')
4329 s = skipwhite(s + 1);
4330 }
4331 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004332 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004333 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004334 r = alloc(STRLEN(txt)
4335 + STRLEN(dashes) // for %s
4336 + 20 // for %3ld
4337 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004338 if (r != NULL)
4339 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004340 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 len = (int)STRLEN(r);
4342 STRCAT(r, s);
4343 /* remove 'foldmarker' and 'commentstring' */
4344 foldtext_cleanup(r + len);
4345 rettv->vval.v_string = r;
4346 }
4347 }
4348#endif
4349}
4350
4351/*
4352 * "foldtextresult(lnum)" function
4353 */
4354 static void
4355f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4356{
4357#ifdef FEAT_FOLDING
4358 linenr_T lnum;
4359 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004360 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004361 foldinfo_T foldinfo;
4362 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004363 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004364#endif
4365
4366 rettv->v_type = VAR_STRING;
4367 rettv->vval.v_string = NULL;
4368#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004369 if (entered)
4370 return; /* reject recursive use */
4371 entered = TRUE;
4372
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004373 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004374 /* treat illegal types and illegal string values for {lnum} the same */
4375 if (lnum < 0)
4376 lnum = 0;
4377 fold_count = foldedCount(curwin, lnum, &foldinfo);
4378 if (fold_count > 0)
4379 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004380 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4381 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 if (text == buf)
4383 text = vim_strsave(text);
4384 rettv->vval.v_string = text;
4385 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004386
4387 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004388#endif
4389}
4390
4391/*
4392 * "foreground()" function
4393 */
4394 static void
4395f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4396{
4397#ifdef FEAT_GUI
4398 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004399 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004400 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004401 return;
4402 }
4403#endif
4404#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004405 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406#endif
4407}
4408
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004410common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411{
4412 char_u *s;
4413 char_u *name;
4414 int use_string = FALSE;
4415 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004416 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004417
4418 if (argvars[0].v_type == VAR_FUNC)
4419 {
4420 /* function(MyFunc, [arg], dict) */
4421 s = argvars[0].vval.v_string;
4422 }
4423 else if (argvars[0].v_type == VAR_PARTIAL
4424 && argvars[0].vval.v_partial != NULL)
4425 {
4426 /* function(dict.MyFunc, [arg]) */
4427 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004428 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004429 }
4430 else
4431 {
4432 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004433 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004434 use_string = TRUE;
4435 }
4436
Bram Moolenaar843b8842016-08-21 14:36:15 +02004437 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004438 {
4439 name = s;
4440 trans_name = trans_function_name(&name, FALSE,
4441 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4442 if (*name != NUL)
4443 s = NULL;
4444 }
4445
Bram Moolenaar843b8842016-08-21 14:36:15 +02004446 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4447 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004448 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004450 else if (trans_name != NULL && (is_funcref
4451 ? find_func(trans_name) == NULL
4452 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004453 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004454 else
4455 {
4456 int dict_idx = 0;
4457 int arg_idx = 0;
4458 list_T *list = NULL;
4459
4460 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4461 {
4462 char sid_buf[25];
4463 int off = *s == 's' ? 2 : 5;
4464
4465 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4466 * also be called from another script. Using trans_function_name()
4467 * would also work, but some plugins depend on the name being
4468 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004469 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004470 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004471 if (name != NULL)
4472 {
4473 STRCPY(name, sid_buf);
4474 STRCAT(name, s + off);
4475 }
4476 }
4477 else
4478 name = vim_strsave(s);
4479
4480 if (argvars[1].v_type != VAR_UNKNOWN)
4481 {
4482 if (argvars[2].v_type != VAR_UNKNOWN)
4483 {
4484 /* function(name, [args], dict) */
4485 arg_idx = 1;
4486 dict_idx = 2;
4487 }
4488 else if (argvars[1].v_type == VAR_DICT)
4489 /* function(name, dict) */
4490 dict_idx = 1;
4491 else
4492 /* function(name, [args]) */
4493 arg_idx = 1;
4494 if (dict_idx > 0)
4495 {
4496 if (argvars[dict_idx].v_type != VAR_DICT)
4497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004498 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004499 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004500 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 }
4502 if (argvars[dict_idx].vval.v_dict == NULL)
4503 dict_idx = 0;
4504 }
4505 if (arg_idx > 0)
4506 {
4507 if (argvars[arg_idx].v_type != VAR_LIST)
4508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004509 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004510 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004511 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512 }
4513 list = argvars[arg_idx].vval.v_list;
4514 if (list == NULL || list->lv_len == 0)
4515 arg_idx = 0;
4516 }
4517 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004518 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004519 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004520 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004521
4522 /* result is a VAR_PARTIAL */
4523 if (pt == NULL)
4524 vim_free(name);
4525 else
4526 {
4527 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4528 {
4529 listitem_T *li;
4530 int i = 0;
4531 int arg_len = 0;
4532 int lv_len = 0;
4533
4534 if (arg_pt != NULL)
4535 arg_len = arg_pt->pt_argc;
4536 if (list != NULL)
4537 lv_len = list->lv_len;
4538 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004539 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004540 if (pt->pt_argv == NULL)
4541 {
4542 vim_free(pt);
4543 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004545 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004546 for (i = 0; i < arg_len; i++)
4547 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4548 if (lv_len > 0)
4549 for (li = list->lv_first; li != NULL;
4550 li = li->li_next)
4551 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 }
4553
4554 /* For "function(dict.func, [], dict)" and "func" is a partial
4555 * use "dict". That is backwards compatible. */
4556 if (dict_idx > 0)
4557 {
4558 /* The dict is bound explicitly, pt_auto is FALSE. */
4559 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4560 ++pt->pt_dict->dv_refcount;
4561 }
4562 else if (arg_pt != NULL)
4563 {
4564 /* If the dict was bound automatically the result is also
4565 * bound automatically. */
4566 pt->pt_dict = arg_pt->pt_dict;
4567 pt->pt_auto = arg_pt->pt_auto;
4568 if (pt->pt_dict != NULL)
4569 ++pt->pt_dict->dv_refcount;
4570 }
4571
4572 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004573 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4574 {
4575 pt->pt_func = arg_pt->pt_func;
4576 func_ptr_ref(pt->pt_func);
4577 vim_free(name);
4578 }
4579 else if (is_funcref)
4580 {
4581 pt->pt_func = find_func(trans_name);
4582 func_ptr_ref(pt->pt_func);
4583 vim_free(name);
4584 }
4585 else
4586 {
4587 pt->pt_name = name;
4588 func_ref(name);
4589 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004590 }
4591 rettv->v_type = VAR_PARTIAL;
4592 rettv->vval.v_partial = pt;
4593 }
4594 else
4595 {
4596 /* result is a VAR_FUNC */
4597 rettv->v_type = VAR_FUNC;
4598 rettv->vval.v_string = name;
4599 func_ref(name);
4600 }
4601 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004602theend:
4603 vim_free(trans_name);
4604}
4605
4606/*
4607 * "funcref()" function
4608 */
4609 static void
4610f_funcref(typval_T *argvars, typval_T *rettv)
4611{
4612 common_function(argvars, rettv, TRUE);
4613}
4614
4615/*
4616 * "function()" function
4617 */
4618 static void
4619f_function(typval_T *argvars, typval_T *rettv)
4620{
4621 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004622}
4623
4624/*
4625 * "garbagecollect()" function
4626 */
4627 static void
4628f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4629{
4630 /* This is postponed until we are back at the toplevel, because we may be
4631 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4632 want_garbage_collect = TRUE;
4633
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004634 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004635 garbage_collect_at_exit = TRUE;
4636}
4637
4638/*
4639 * "get()" function
4640 */
4641 static void
4642f_get(typval_T *argvars, typval_T *rettv)
4643{
4644 listitem_T *li;
4645 list_T *l;
4646 dictitem_T *di;
4647 dict_T *d;
4648 typval_T *tv = NULL;
4649
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004650 if (argvars[0].v_type == VAR_BLOB)
4651 {
4652 int error = FALSE;
4653 int idx = tv_get_number_chk(&argvars[1], &error);
4654
4655 if (!error)
4656 {
4657 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004658 if (idx < 0)
4659 idx = blob_len(argvars[0].vval.v_blob) + idx;
4660 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4661 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004662 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004663 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004664 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004665 tv = rettv;
4666 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004667 }
4668 }
4669 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004670 {
4671 if ((l = argvars[0].vval.v_list) != NULL)
4672 {
4673 int error = FALSE;
4674
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004675 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004676 if (!error && li != NULL)
4677 tv = &li->li_tv;
4678 }
4679 }
4680 else if (argvars[0].v_type == VAR_DICT)
4681 {
4682 if ((d = argvars[0].vval.v_dict) != NULL)
4683 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004684 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 if (di != NULL)
4686 tv = &di->di_tv;
4687 }
4688 }
4689 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4690 {
4691 partial_T *pt;
4692 partial_T fref_pt;
4693
4694 if (argvars[0].v_type == VAR_PARTIAL)
4695 pt = argvars[0].vval.v_partial;
4696 else
4697 {
4698 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4699 fref_pt.pt_name = argvars[0].vval.v_string;
4700 pt = &fref_pt;
4701 }
4702
4703 if (pt != NULL)
4704 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004705 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004706 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004707
4708 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4709 {
4710 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004711 n = partial_name(pt);
4712 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004713 rettv->vval.v_string = NULL;
4714 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004715 {
4716 rettv->vval.v_string = vim_strsave(n);
4717 if (rettv->v_type == VAR_FUNC)
4718 func_ref(rettv->vval.v_string);
4719 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004720 }
4721 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004722 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004723 else if (STRCMP(what, "args") == 0)
4724 {
4725 rettv->v_type = VAR_LIST;
4726 if (rettv_list_alloc(rettv) == OK)
4727 {
4728 int i;
4729
4730 for (i = 0; i < pt->pt_argc; ++i)
4731 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4732 }
4733 }
4734 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004735 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004736 return;
4737 }
4738 }
4739 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004740 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004741
4742 if (tv == NULL)
4743 {
4744 if (argvars[2].v_type != VAR_UNKNOWN)
4745 copy_tv(&argvars[2], rettv);
4746 }
4747 else
4748 copy_tv(tv, rettv);
4749}
4750
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004751/*
4752 * Returns buffer options, variables and other attributes in a dictionary.
4753 */
4754 static dict_T *
4755get_buffer_info(buf_T *buf)
4756{
4757 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004758 tabpage_T *tp;
4759 win_T *wp;
4760 list_T *windows;
4761
4762 dict = dict_alloc();
4763 if (dict == NULL)
4764 return NULL;
4765
Bram Moolenaare0be1672018-07-08 16:50:37 +02004766 dict_add_number(dict, "bufnr", buf->b_fnum);
4767 dict_add_string(dict, "name", buf->b_ffname);
4768 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4769 : buflist_findlnum(buf));
4770 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4771 dict_add_number(dict, "listed", buf->b_p_bl);
4772 dict_add_number(dict, "changed", bufIsChanged(buf));
4773 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4774 dict_add_number(dict, "hidden",
4775 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004776
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004777 /* Get a reference to buffer variables */
4778 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004779
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004780 /* List of windows displaying this buffer */
4781 windows = list_alloc();
4782 if (windows != NULL)
4783 {
4784 FOR_ALL_TAB_WINDOWS(tp, wp)
4785 if (wp->w_buffer == buf)
4786 list_append_number(windows, (varnumber_T)wp->w_id);
4787 dict_add_list(dict, "windows", windows);
4788 }
4789
4790#ifdef FEAT_SIGNS
4791 if (buf->b_signlist != NULL)
4792 {
4793 /* List of signs placed in this buffer */
4794 list_T *signs = list_alloc();
4795 if (signs != NULL)
4796 {
4797 get_buffer_signs(buf, signs);
4798 dict_add_list(dict, "signs", signs);
4799 }
4800 }
4801#endif
4802
4803 return dict;
4804}
4805
4806/*
4807 * "getbufinfo()" function
4808 */
4809 static void
4810f_getbufinfo(typval_T *argvars, typval_T *rettv)
4811{
4812 buf_T *buf = NULL;
4813 buf_T *argbuf = NULL;
4814 dict_T *d;
4815 int filtered = FALSE;
4816 int sel_buflisted = FALSE;
4817 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004818 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004819
4820 if (rettv_list_alloc(rettv) != OK)
4821 return;
4822
4823 /* List of all the buffers or selected buffers */
4824 if (argvars[0].v_type == VAR_DICT)
4825 {
4826 dict_T *sel_d = argvars[0].vval.v_dict;
4827
4828 if (sel_d != NULL)
4829 {
4830 dictitem_T *di;
4831
4832 filtered = TRUE;
4833
4834 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004835 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004836 sel_buflisted = TRUE;
4837
4838 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004839 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004840 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004841
4842 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004843 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004844 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004845 }
4846 }
4847 else if (argvars[0].v_type != VAR_UNKNOWN)
4848 {
4849 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004850 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004851 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004852 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004853 --emsg_off;
4854 if (argbuf == NULL)
4855 return;
4856 }
4857
4858 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004859 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004860 {
4861 if (argbuf != NULL && argbuf != buf)
4862 continue;
4863 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004864 || (sel_buflisted && !buf->b_p_bl)
4865 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004866 continue;
4867
4868 d = get_buffer_info(buf);
4869 if (d != NULL)
4870 list_append_dict(rettv->vval.v_list, d);
4871 if (argbuf != NULL)
4872 return;
4873 }
4874}
4875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004876/*
4877 * Get line or list of lines from buffer "buf" into "rettv".
4878 * Return a range (from start to end) of lines in rettv from the specified
4879 * buffer.
4880 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4881 */
4882 static void
4883get_buffer_lines(
4884 buf_T *buf,
4885 linenr_T start,
4886 linenr_T end,
4887 int retlist,
4888 typval_T *rettv)
4889{
4890 char_u *p;
4891
4892 rettv->v_type = VAR_STRING;
4893 rettv->vval.v_string = NULL;
4894 if (retlist && rettv_list_alloc(rettv) == FAIL)
4895 return;
4896
4897 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4898 return;
4899
4900 if (!retlist)
4901 {
4902 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4903 p = ml_get_buf(buf, start, FALSE);
4904 else
4905 p = (char_u *)"";
4906 rettv->vval.v_string = vim_strsave(p);
4907 }
4908 else
4909 {
4910 if (end < start)
4911 return;
4912
4913 if (start < 1)
4914 start = 1;
4915 if (end > buf->b_ml.ml_line_count)
4916 end = buf->b_ml.ml_line_count;
4917 while (start <= end)
4918 if (list_append_string(rettv->vval.v_list,
4919 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4920 break;
4921 }
4922}
4923
4924/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004925 * "getbufline()" function
4926 */
4927 static void
4928f_getbufline(typval_T *argvars, typval_T *rettv)
4929{
4930 linenr_T lnum;
4931 linenr_T end;
4932 buf_T *buf;
4933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004934 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004936 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 --emsg_off;
4938
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 if (argvars[2].v_type == VAR_UNKNOWN)
4941 end = lnum;
4942 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004943 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944
4945 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4946}
4947
4948/*
4949 * "getbufvar()" function
4950 */
4951 static void
4952f_getbufvar(typval_T *argvars, typval_T *rettv)
4953{
4954 buf_T *buf;
4955 buf_T *save_curbuf;
4956 char_u *varname;
4957 dictitem_T *v;
4958 int done = FALSE;
4959
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004960 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4961 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004962 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004963 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004964
4965 rettv->v_type = VAR_STRING;
4966 rettv->vval.v_string = NULL;
4967
4968 if (buf != NULL && varname != NULL)
4969 {
4970 /* set curbuf to be our buf, temporarily */
4971 save_curbuf = curbuf;
4972 curbuf = buf;
4973
Bram Moolenaar30567352016-08-27 21:25:44 +02004974 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004975 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004976 if (varname[1] == NUL)
4977 {
4978 /* get all buffer-local options in a dict */
4979 dict_T *opts = get_winbuf_options(TRUE);
4980
4981 if (opts != NULL)
4982 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004983 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004984 done = TRUE;
4985 }
4986 }
4987 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4988 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004989 done = TRUE;
4990 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004991 else
4992 {
4993 /* Look up the variable. */
4994 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4995 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4996 'b', varname, FALSE);
4997 if (v != NULL)
4998 {
4999 copy_tv(&v->di_tv, rettv);
5000 done = TRUE;
5001 }
5002 }
5003
5004 /* restore previous notion of curbuf */
5005 curbuf = save_curbuf;
5006 }
5007
5008 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5009 /* use the default value */
5010 copy_tv(&argvars[2], rettv);
5011
5012 --emsg_off;
5013}
5014
5015/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005016 * "getchangelist()" function
5017 */
5018 static void
5019f_getchangelist(typval_T *argvars, typval_T *rettv)
5020{
5021#ifdef FEAT_JUMPLIST
5022 buf_T *buf;
5023 int i;
5024 list_T *l;
5025 dict_T *d;
5026#endif
5027
5028 if (rettv_list_alloc(rettv) != OK)
5029 return;
5030
5031#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005032 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005033 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005034 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005035 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005036 if (buf == NULL)
5037 return;
5038
5039 l = list_alloc();
5040 if (l == NULL)
5041 return;
5042
5043 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5044 return;
5045 /*
5046 * The current window change list index tracks only the position in the
5047 * current buffer change list. For other buffers, use the change list
5048 * length as the current index.
5049 */
5050 list_append_number(rettv->vval.v_list,
5051 (varnumber_T)((buf == curwin->w_buffer)
5052 ? curwin->w_changelistidx : buf->b_changelistlen));
5053
5054 for (i = 0; i < buf->b_changelistlen; ++i)
5055 {
5056 if (buf->b_changelist[i].lnum == 0)
5057 continue;
5058 if ((d = dict_alloc()) == NULL)
5059 return;
5060 if (list_append_dict(l, d) == FAIL)
5061 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005062 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5063 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005064 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005065 }
5066#endif
5067}
5068/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005069 * "getchar()" function
5070 */
5071 static void
5072f_getchar(typval_T *argvars, typval_T *rettv)
5073{
5074 varnumber_T n;
5075 int error = FALSE;
5076
Bram Moolenaar84d93902018-09-11 20:10:20 +02005077#ifdef MESSAGE_QUEUE
5078 // vpeekc() used to check for messages, but that caused problems, invoking
5079 // a callback where it was not expected. Some plugins use getchar(1) in a
5080 // loop to await a message, therefore make sure we check for messages here.
5081 parse_queued_messages();
5082#endif
5083
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 /* Position the cursor. Needed after a message that ends in a space. */
5085 windgoto(msg_row, msg_col);
5086
5087 ++no_mapping;
5088 ++allow_keys;
5089 for (;;)
5090 {
5091 if (argvars[0].v_type == VAR_UNKNOWN)
5092 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005093 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005094 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 /* getchar(1): only check if char avail */
5096 n = vpeekc_any();
5097 else if (error || vpeekc_any() == NUL)
5098 /* illegal argument or getchar(0) and no char avail: return zero */
5099 n = 0;
5100 else
5101 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005102 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103
5104 if (n == K_IGNORE)
5105 continue;
5106 break;
5107 }
5108 --no_mapping;
5109 --allow_keys;
5110
5111 set_vim_var_nr(VV_MOUSE_WIN, 0);
5112 set_vim_var_nr(VV_MOUSE_WINID, 0);
5113 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5114 set_vim_var_nr(VV_MOUSE_COL, 0);
5115
5116 rettv->vval.v_number = n;
5117 if (IS_SPECIAL(n) || mod_mask != 0)
5118 {
5119 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5120 int i = 0;
5121
5122 /* Turn a special key into three bytes, plus modifier. */
5123 if (mod_mask != 0)
5124 {
5125 temp[i++] = K_SPECIAL;
5126 temp[i++] = KS_MODIFIER;
5127 temp[i++] = mod_mask;
5128 }
5129 if (IS_SPECIAL(n))
5130 {
5131 temp[i++] = K_SPECIAL;
5132 temp[i++] = K_SECOND(n);
5133 temp[i++] = K_THIRD(n);
5134 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005135 else if (has_mbyte)
5136 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 else
5138 temp[i++] = n;
5139 temp[i++] = NUL;
5140 rettv->v_type = VAR_STRING;
5141 rettv->vval.v_string = vim_strsave(temp);
5142
5143#ifdef FEAT_MOUSE
5144 if (is_mouse_key(n))
5145 {
5146 int row = mouse_row;
5147 int col = mouse_col;
5148 win_T *win;
5149 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005150 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 int winnr = 1;
5152
5153 if (row >= 0 && col >= 0)
5154 {
5155 /* Find the window at the mouse coordinates and compute the
5156 * text position. */
5157 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005158 if (win == NULL)
5159 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005160 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 for (wp = firstwin; wp != win; wp = wp->w_next)
5162 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005163 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5164 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5165 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5166 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5167 }
5168 }
5169#endif
5170 }
5171}
5172
5173/*
5174 * "getcharmod()" function
5175 */
5176 static void
5177f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5178{
5179 rettv->vval.v_number = mod_mask;
5180}
5181
5182/*
5183 * "getcharsearch()" function
5184 */
5185 static void
5186f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5187{
5188 if (rettv_dict_alloc(rettv) != FAIL)
5189 {
5190 dict_T *dict = rettv->vval.v_dict;
5191
Bram Moolenaare0be1672018-07-08 16:50:37 +02005192 dict_add_string(dict, "char", last_csearch());
5193 dict_add_number(dict, "forward", last_csearch_forward());
5194 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005195 }
5196}
5197
5198/*
5199 * "getcmdline()" function
5200 */
5201 static void
5202f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5203{
5204 rettv->v_type = VAR_STRING;
5205 rettv->vval.v_string = get_cmdline_str();
5206}
5207
5208/*
5209 * "getcmdpos()" function
5210 */
5211 static void
5212f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5213{
5214 rettv->vval.v_number = get_cmdline_pos() + 1;
5215}
5216
5217/*
5218 * "getcmdtype()" function
5219 */
5220 static void
5221f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5222{
5223 rettv->v_type = VAR_STRING;
5224 rettv->vval.v_string = alloc(2);
5225 if (rettv->vval.v_string != NULL)
5226 {
5227 rettv->vval.v_string[0] = get_cmdline_type();
5228 rettv->vval.v_string[1] = NUL;
5229 }
5230}
5231
5232/*
5233 * "getcmdwintype()" function
5234 */
5235 static void
5236f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5237{
5238 rettv->v_type = VAR_STRING;
5239 rettv->vval.v_string = NULL;
5240#ifdef FEAT_CMDWIN
5241 rettv->vval.v_string = alloc(2);
5242 if (rettv->vval.v_string != NULL)
5243 {
5244 rettv->vval.v_string[0] = cmdwin_type;
5245 rettv->vval.v_string[1] = NUL;
5246 }
5247#endif
5248}
5249
5250#if defined(FEAT_CMDL_COMPL)
5251/*
5252 * "getcompletion()" function
5253 */
5254 static void
5255f_getcompletion(typval_T *argvars, typval_T *rettv)
5256{
5257 char_u *pat;
5258 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005259 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005260 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5261 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005262
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005263 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005264 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005265
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005266 if (p_wic)
5267 options |= WILD_ICASE;
5268
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005269 /* For filtered results, 'wildignore' is used */
5270 if (!filtered)
5271 options |= WILD_KEEP_ALL;
5272
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005273 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005275 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005276 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005277 if (xpc.xp_context == EXPAND_NOTHING)
5278 {
5279 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005280 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005282 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 return;
5284 }
5285
5286# if defined(FEAT_MENU)
5287 if (xpc.xp_context == EXPAND_MENUS)
5288 {
5289 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5290 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5291 }
5292# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005293#ifdef FEAT_CSCOPE
5294 if (xpc.xp_context == EXPAND_CSCOPE)
5295 {
5296 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5297 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5298 }
5299#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005300#ifdef FEAT_SIGNS
5301 if (xpc.xp_context == EXPAND_SIGN)
5302 {
5303 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5304 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5305 }
5306#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307
5308 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5309 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5310 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005311 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312
5313 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5314
5315 for (i = 0; i < xpc.xp_numfiles; i++)
5316 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5317 }
5318 vim_free(pat);
5319 ExpandCleanup(&xpc);
5320}
5321#endif
5322
5323/*
5324 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005325 *
5326 * Return the current working directory of a window in a tab page.
5327 * First optional argument 'winnr' is the window number or -1 and the second
5328 * optional argument 'tabnr' is the tab page number.
5329 *
5330 * If no arguments are supplied, then return the directory of the current
5331 * window.
5332 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5333 * the specified window.
5334 * If 'winnr' is 0 then return the directory of the current window.
5335 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5336 * directory of the specified tab page. Otherwise return the directory of the
5337 * specified window in the specified tab page.
5338 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005339 */
5340 static void
5341f_getcwd(typval_T *argvars, typval_T *rettv)
5342{
5343 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005344 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005345 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005346 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347
5348 rettv->v_type = VAR_STRING;
5349 rettv->vval.v_string = NULL;
5350
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005351 if (argvars[0].v_type == VAR_NUMBER
5352 && argvars[0].vval.v_number == -1
5353 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005354 global = TRUE;
5355 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005356 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005357
5358 if (wp != NULL && wp->w_localdir != NULL)
5359 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005360 else if (tp != NULL && tp->tp_localdir != NULL)
5361 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5362 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005363 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005364 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 rettv->vval.v_string = vim_strsave(globaldir);
5366 else
5367 {
5368 cwd = alloc(MAXPATHL);
5369 if (cwd != NULL)
5370 {
5371 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5372 rettv->vval.v_string = vim_strsave(cwd);
5373 vim_free(cwd);
5374 }
5375 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005377#ifdef BACKSLASH_IN_FILENAME
5378 if (rettv->vval.v_string != NULL)
5379 slash_adjust(rettv->vval.v_string);
5380#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005381}
5382
5383/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005384 * "getenv()" function
5385 */
5386 static void
5387f_getenv(typval_T *argvars, typval_T *rettv)
5388{
5389 int mustfree = FALSE;
5390 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5391
5392 if (p == NULL)
5393 {
5394 rettv->v_type = VAR_SPECIAL;
5395 rettv->vval.v_number = VVAL_NULL;
5396 return;
5397 }
5398 if (!mustfree)
5399 p = vim_strsave(p);
5400 rettv->vval.v_string = p;
5401 rettv->v_type = VAR_STRING;
5402}
5403
5404/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005405 * "getfontname()" function
5406 */
5407 static void
5408f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5409{
5410 rettv->v_type = VAR_STRING;
5411 rettv->vval.v_string = NULL;
5412#ifdef FEAT_GUI
5413 if (gui.in_use)
5414 {
5415 GuiFont font;
5416 char_u *name = NULL;
5417
5418 if (argvars[0].v_type == VAR_UNKNOWN)
5419 {
5420 /* Get the "Normal" font. Either the name saved by
5421 * hl_set_font_name() or from the font ID. */
5422 font = gui.norm_font;
5423 name = hl_get_font_name();
5424 }
5425 else
5426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005427 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005428 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5429 return;
5430 font = gui_mch_get_font(name, FALSE);
5431 if (font == NOFONT)
5432 return; /* Invalid font name, return empty string. */
5433 }
5434 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5435 if (argvars[0].v_type != VAR_UNKNOWN)
5436 gui_mch_free_font(font);
5437 }
5438#endif
5439}
5440
5441/*
5442 * "getfperm({fname})" function
5443 */
5444 static void
5445f_getfperm(typval_T *argvars, typval_T *rettv)
5446{
5447 char_u *fname;
5448 stat_T st;
5449 char_u *perm = NULL;
5450 char_u flags[] = "rwx";
5451 int i;
5452
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005453 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005454
5455 rettv->v_type = VAR_STRING;
5456 if (mch_stat((char *)fname, &st) >= 0)
5457 {
5458 perm = vim_strsave((char_u *)"---------");
5459 if (perm != NULL)
5460 {
5461 for (i = 0; i < 9; i++)
5462 {
5463 if (st.st_mode & (1 << (8 - i)))
5464 perm[i] = flags[i % 3];
5465 }
5466 }
5467 }
5468 rettv->vval.v_string = perm;
5469}
5470
5471/*
5472 * "getfsize({fname})" function
5473 */
5474 static void
5475f_getfsize(typval_T *argvars, typval_T *rettv)
5476{
5477 char_u *fname;
5478 stat_T st;
5479
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005480 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005481
5482 rettv->v_type = VAR_NUMBER;
5483
5484 if (mch_stat((char *)fname, &st) >= 0)
5485 {
5486 if (mch_isdir(fname))
5487 rettv->vval.v_number = 0;
5488 else
5489 {
5490 rettv->vval.v_number = (varnumber_T)st.st_size;
5491
5492 /* non-perfect check for overflow */
5493 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5494 rettv->vval.v_number = -2;
5495 }
5496 }
5497 else
5498 rettv->vval.v_number = -1;
5499}
5500
5501/*
5502 * "getftime({fname})" function
5503 */
5504 static void
5505f_getftime(typval_T *argvars, typval_T *rettv)
5506{
5507 char_u *fname;
5508 stat_T st;
5509
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005510 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005511
5512 if (mch_stat((char *)fname, &st) >= 0)
5513 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5514 else
5515 rettv->vval.v_number = -1;
5516}
5517
5518/*
5519 * "getftype({fname})" function
5520 */
5521 static void
5522f_getftype(typval_T *argvars, typval_T *rettv)
5523{
5524 char_u *fname;
5525 stat_T st;
5526 char_u *type = NULL;
5527 char *t;
5528
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005529 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005530
5531 rettv->v_type = VAR_STRING;
5532 if (mch_lstat((char *)fname, &st) >= 0)
5533 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005534 if (S_ISREG(st.st_mode))
5535 t = "file";
5536 else if (S_ISDIR(st.st_mode))
5537 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005538 else if (S_ISLNK(st.st_mode))
5539 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540 else if (S_ISBLK(st.st_mode))
5541 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005542 else if (S_ISCHR(st.st_mode))
5543 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 else if (S_ISFIFO(st.st_mode))
5545 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005546 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005547 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 else
5549 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 type = vim_strsave((char_u *)t);
5551 }
5552 rettv->vval.v_string = type;
5553}
5554
5555/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005556 * "getjumplist()" function
5557 */
5558 static void
5559f_getjumplist(typval_T *argvars, typval_T *rettv)
5560{
5561#ifdef FEAT_JUMPLIST
5562 win_T *wp;
5563 int i;
5564 list_T *l;
5565 dict_T *d;
5566#endif
5567
5568 if (rettv_list_alloc(rettv) != OK)
5569 return;
5570
5571#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005572 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005573 if (wp == NULL)
5574 return;
5575
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005576 cleanup_jumplist(wp, TRUE);
5577
Bram Moolenaar4f505882018-02-10 21:06:32 +01005578 l = list_alloc();
5579 if (l == NULL)
5580 return;
5581
5582 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5583 return;
5584 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5585
5586 for (i = 0; i < wp->w_jumplistlen; ++i)
5587 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005588 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5589 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005590 if ((d = dict_alloc()) == NULL)
5591 return;
5592 if (list_append_dict(l, d) == FAIL)
5593 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005594 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5595 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005596 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005597 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005598 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005599 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005600 }
5601#endif
5602}
5603
5604/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005605 * "getline(lnum, [end])" function
5606 */
5607 static void
5608f_getline(typval_T *argvars, typval_T *rettv)
5609{
5610 linenr_T lnum;
5611 linenr_T end;
5612 int retlist;
5613
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005614 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 if (argvars[1].v_type == VAR_UNKNOWN)
5616 {
5617 end = 0;
5618 retlist = FALSE;
5619 }
5620 else
5621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005622 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 retlist = TRUE;
5624 }
5625
5626 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5627}
5628
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005629#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005630 static void
5631get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5632{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005633 if (what_arg->v_type == VAR_UNKNOWN)
5634 {
5635 if (rettv_list_alloc(rettv) == OK)
5636 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005637 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005638 }
5639 else
5640 {
5641 if (rettv_dict_alloc(rettv) == OK)
5642 if (is_qf || (wp != NULL))
5643 {
5644 if (what_arg->v_type == VAR_DICT)
5645 {
5646 dict_T *d = what_arg->vval.v_dict;
5647
5648 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005649 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005650 }
5651 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005652 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005653 }
5654 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005655}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005656#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005657
5658/*
5659 * "getloclist()" function
5660 */
5661 static void
5662f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5663{
5664#ifdef FEAT_QUICKFIX
5665 win_T *wp;
5666
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005667 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005668 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5669#endif
5670}
5671
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672/*
5673 * "getmatches()" function
5674 */
5675 static void
5676f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5677{
5678#ifdef FEAT_SEARCH_EXTRA
5679 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005680 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005681 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005682 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005683
Bram Moolenaaraff74912019-03-30 18:11:49 +01005684 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5685 return;
5686
5687 cur = win->w_match_head;
5688 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005689 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005690 dict = dict_alloc();
5691 if (dict == NULL)
5692 return;
5693 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005694 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005695 /* match added with matchaddpos() */
5696 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005697 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005698 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005699 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005700 list_T *l;
5701
5702 llpos = &cur->pos.pos[i];
5703 if (llpos->lnum == 0)
5704 break;
5705 l = list_alloc();
5706 if (l == NULL)
5707 break;
5708 list_append_number(l, (varnumber_T)llpos->lnum);
5709 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005710 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005711 list_append_number(l, (varnumber_T)llpos->col);
5712 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005713 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005714 sprintf(buf, "pos%d", i + 1);
5715 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005716 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005718 else
5719 {
5720 dict_add_string(dict, "pattern", cur->pattern);
5721 }
5722 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5723 dict_add_number(dict, "priority", (long)cur->priority);
5724 dict_add_number(dict, "id", (long)cur->id);
5725# if defined(FEAT_CONCEAL)
5726 if (cur->conceal_char)
5727 {
5728 char_u buf[MB_MAXBYTES + 1];
5729
5730 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5731 dict_add_string(dict, "conceal", (char_u *)&buf);
5732 }
5733# endif
5734 list_append_dict(rettv->vval.v_list, dict);
5735 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 }
5737#endif
5738}
5739
5740/*
5741 * "getpid()" function
5742 */
5743 static void
5744f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5745{
5746 rettv->vval.v_number = mch_get_pid();
5747}
5748
5749 static void
5750getpos_both(
5751 typval_T *argvars,
5752 typval_T *rettv,
5753 int getcurpos)
5754{
5755 pos_T *fp;
5756 list_T *l;
5757 int fnum = -1;
5758
5759 if (rettv_list_alloc(rettv) == OK)
5760 {
5761 l = rettv->vval.v_list;
5762 if (getcurpos)
5763 fp = &curwin->w_cursor;
5764 else
5765 fp = var2fpos(&argvars[0], TRUE, &fnum);
5766 if (fnum != -1)
5767 list_append_number(l, (varnumber_T)fnum);
5768 else
5769 list_append_number(l, (varnumber_T)0);
5770 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5771 : (varnumber_T)0);
5772 list_append_number(l, (fp != NULL)
5773 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5774 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005775 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005776 (varnumber_T)0);
5777 if (getcurpos)
5778 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005779 int save_set_curswant = curwin->w_set_curswant;
5780 colnr_T save_curswant = curwin->w_curswant;
5781 colnr_T save_virtcol = curwin->w_virtcol;
5782
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005783 update_curswant();
5784 list_append_number(l, curwin->w_curswant == MAXCOL ?
5785 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005786
5787 // Do not change "curswant", as it is unexpected that a get
5788 // function has a side effect.
5789 if (save_set_curswant)
5790 {
5791 curwin->w_set_curswant = save_set_curswant;
5792 curwin->w_curswant = save_curswant;
5793 curwin->w_virtcol = save_virtcol;
5794 curwin->w_valid &= ~VALID_VIRTCOL;
5795 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005796 }
5797 }
5798 else
5799 rettv->vval.v_number = FALSE;
5800}
5801
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005802/*
5803 * "getcurpos()" function
5804 */
5805 static void
5806f_getcurpos(typval_T *argvars, typval_T *rettv)
5807{
5808 getpos_both(argvars, rettv, TRUE);
5809}
5810
5811/*
5812 * "getpos(string)" function
5813 */
5814 static void
5815f_getpos(typval_T *argvars, typval_T *rettv)
5816{
5817 getpos_both(argvars, rettv, FALSE);
5818}
5819
5820/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005821 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005822 */
5823 static void
5824f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5825{
5826#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005827 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005828#endif
5829}
5830
5831/*
5832 * "getreg()" function
5833 */
5834 static void
5835f_getreg(typval_T *argvars, typval_T *rettv)
5836{
5837 char_u *strregname;
5838 int regname;
5839 int arg2 = FALSE;
5840 int return_list = FALSE;
5841 int error = FALSE;
5842
5843 if (argvars[0].v_type != VAR_UNKNOWN)
5844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005845 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005846 error = strregname == NULL;
5847 if (argvars[1].v_type != VAR_UNKNOWN)
5848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005849 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005850 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005851 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005852 }
5853 }
5854 else
5855 strregname = get_vim_var_str(VV_REG);
5856
5857 if (error)
5858 return;
5859
5860 regname = (strregname == NULL ? '"' : *strregname);
5861 if (regname == 0)
5862 regname = '"';
5863
5864 if (return_list)
5865 {
5866 rettv->v_type = VAR_LIST;
5867 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5868 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5869 if (rettv->vval.v_list == NULL)
5870 (void)rettv_list_alloc(rettv);
5871 else
5872 ++rettv->vval.v_list->lv_refcount;
5873 }
5874 else
5875 {
5876 rettv->v_type = VAR_STRING;
5877 rettv->vval.v_string = get_reg_contents(regname,
5878 arg2 ? GREG_EXPR_SRC : 0);
5879 }
5880}
5881
5882/*
5883 * "getregtype()" function
5884 */
5885 static void
5886f_getregtype(typval_T *argvars, typval_T *rettv)
5887{
5888 char_u *strregname;
5889 int regname;
5890 char_u buf[NUMBUFLEN + 2];
5891 long reglen = 0;
5892
5893 if (argvars[0].v_type != VAR_UNKNOWN)
5894 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005895 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005896 if (strregname == NULL) /* type error; errmsg already given */
5897 {
5898 rettv->v_type = VAR_STRING;
5899 rettv->vval.v_string = NULL;
5900 return;
5901 }
5902 }
5903 else
5904 /* Default to v:register */
5905 strregname = get_vim_var_str(VV_REG);
5906
5907 regname = (strregname == NULL ? '"' : *strregname);
5908 if (regname == 0)
5909 regname = '"';
5910
5911 buf[0] = NUL;
5912 buf[1] = NUL;
5913 switch (get_reg_type(regname, &reglen))
5914 {
5915 case MLINE: buf[0] = 'V'; break;
5916 case MCHAR: buf[0] = 'v'; break;
5917 case MBLOCK:
5918 buf[0] = Ctrl_V;
5919 sprintf((char *)buf + 1, "%ld", reglen + 1);
5920 break;
5921 }
5922 rettv->v_type = VAR_STRING;
5923 rettv->vval.v_string = vim_strsave(buf);
5924}
5925
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005926/*
5927 * Returns information (variables, options, etc.) about a tab page
5928 * as a dictionary.
5929 */
5930 static dict_T *
5931get_tabpage_info(tabpage_T *tp, int tp_idx)
5932{
5933 win_T *wp;
5934 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005935 list_T *l;
5936
5937 dict = dict_alloc();
5938 if (dict == NULL)
5939 return NULL;
5940
Bram Moolenaare0be1672018-07-08 16:50:37 +02005941 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005942
5943 l = list_alloc();
5944 if (l != NULL)
5945 {
5946 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5947 wp; wp = wp->w_next)
5948 list_append_number(l, (varnumber_T)wp->w_id);
5949 dict_add_list(dict, "windows", l);
5950 }
5951
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005952 /* Make a reference to tabpage variables */
5953 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005954
5955 return dict;
5956}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005957
5958/*
5959 * "gettabinfo()" function
5960 */
5961 static void
5962f_gettabinfo(typval_T *argvars, typval_T *rettv)
5963{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005964 tabpage_T *tp, *tparg = NULL;
5965 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005966 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005967
5968 if (rettv_list_alloc(rettv) != OK)
5969 return;
5970
5971 if (argvars[0].v_type != VAR_UNKNOWN)
5972 {
5973 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005974 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005975 if (tparg == NULL)
5976 return;
5977 }
5978
5979 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005980 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005981 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005982 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005983 if (tparg != NULL && tp != tparg)
5984 continue;
5985 d = get_tabpage_info(tp, tpnr);
5986 if (d != NULL)
5987 list_append_dict(rettv->vval.v_list, d);
5988 if (tparg != NULL)
5989 return;
5990 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005991}
5992
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005993/*
5994 * "gettabvar()" function
5995 */
5996 static void
5997f_gettabvar(typval_T *argvars, typval_T *rettv)
5998{
5999 win_T *oldcurwin;
6000 tabpage_T *tp, *oldtabpage;
6001 dictitem_T *v;
6002 char_u *varname;
6003 int done = FALSE;
6004
6005 rettv->v_type = VAR_STRING;
6006 rettv->vval.v_string = NULL;
6007
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006008 varname = tv_get_string_chk(&argvars[1]);
6009 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006010 if (tp != NULL && varname != NULL)
6011 {
6012 /* Set tp to be our tabpage, temporarily. Also set the window to the
6013 * first window in the tabpage, otherwise the window is not valid. */
6014 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006015 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6016 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017 {
6018 /* look up the variable */
6019 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6020 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6021 if (v != NULL)
6022 {
6023 copy_tv(&v->di_tv, rettv);
6024 done = TRUE;
6025 }
6026 }
6027
6028 /* restore previous notion of curwin */
6029 restore_win(oldcurwin, oldtabpage, TRUE);
6030 }
6031
6032 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6033 copy_tv(&argvars[2], rettv);
6034}
6035
6036/*
6037 * "gettabwinvar()" function
6038 */
6039 static void
6040f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6041{
6042 getwinvar(argvars, rettv, 1);
6043}
6044
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006045/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006046 * "gettagstack()" function
6047 */
6048 static void
6049f_gettagstack(typval_T *argvars, typval_T *rettv)
6050{
6051 win_T *wp = curwin; // default is current window
6052
6053 if (rettv_dict_alloc(rettv) != OK)
6054 return;
6055
6056 if (argvars[0].v_type != VAR_UNKNOWN)
6057 {
6058 wp = find_win_by_nr_or_id(&argvars[0]);
6059 if (wp == NULL)
6060 return;
6061 }
6062
6063 get_tagstack(wp, rettv->vval.v_dict);
6064}
6065
6066/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006067 * Returns information about a window as a dictionary.
6068 */
6069 static dict_T *
6070get_win_info(win_T *wp, short tpnr, short winnr)
6071{
6072 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006073
6074 dict = dict_alloc();
6075 if (dict == NULL)
6076 return NULL;
6077
Bram Moolenaare0be1672018-07-08 16:50:37 +02006078 dict_add_number(dict, "tabnr", tpnr);
6079 dict_add_number(dict, "winnr", winnr);
6080 dict_add_number(dict, "winid", wp->w_id);
6081 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006082 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006083 dict_add_number(dict, "topline", wp->w_topline);
6084 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006085#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006086 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006087#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006088 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006089 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006090 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006091
Bram Moolenaar69905d12017-08-13 18:14:47 +02006092#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006093 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006094#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006095#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006096 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6097 dict_add_number(dict, "loclist",
6098 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006099#endif
6100
Bram Moolenaar30567352016-08-27 21:25:44 +02006101 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006102 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006103
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006104 return dict;
6105}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006106
6107/*
6108 * "getwininfo()" function
6109 */
6110 static void
6111f_getwininfo(typval_T *argvars, typval_T *rettv)
6112{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006113 tabpage_T *tp;
6114 win_T *wp = NULL, *wparg = NULL;
6115 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006116 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006117
6118 if (rettv_list_alloc(rettv) != OK)
6119 return;
6120
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006121 if (argvars[0].v_type != VAR_UNKNOWN)
6122 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006123 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006124 if (wparg == NULL)
6125 return;
6126 }
6127
6128 /* Collect information about either all the windows across all the tab
6129 * pages or one particular window.
6130 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006131 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006132 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006133 tabnr++;
6134 winnr = 0;
6135 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006136 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006137 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006138 if (wparg != NULL && wp != wparg)
6139 continue;
6140 d = get_win_info(wp, tabnr, winnr);
6141 if (d != NULL)
6142 list_append_dict(rettv->vval.v_list, d);
6143 if (wparg != NULL)
6144 /* found information about a specific window */
6145 return;
6146 }
6147 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006148}
6149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006150/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006151 * "win_execute()" function
6152 */
6153 static void
6154f_win_execute(typval_T *argvars, typval_T *rettv)
6155{
6156 int id = (int)tv_get_number(argvars);
6157 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006158 win_T *save_curwin;
6159 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006160
6161 if (wp != NULL)
6162 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006163 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6164 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006165 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006166 check_cursor();
6167 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006168 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006169 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006170 }
6171}
6172
6173/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006174 * "win_findbuf()" function
6175 */
6176 static void
6177f_win_findbuf(typval_T *argvars, typval_T *rettv)
6178{
6179 if (rettv_list_alloc(rettv) != FAIL)
6180 win_findbuf(argvars, rettv->vval.v_list);
6181}
6182
6183/*
6184 * "win_getid()" function
6185 */
6186 static void
6187f_win_getid(typval_T *argvars, typval_T *rettv)
6188{
6189 rettv->vval.v_number = win_getid(argvars);
6190}
6191
6192/*
6193 * "win_gotoid()" function
6194 */
6195 static void
6196f_win_gotoid(typval_T *argvars, typval_T *rettv)
6197{
6198 rettv->vval.v_number = win_gotoid(argvars);
6199}
6200
6201/*
6202 * "win_id2tabwin()" function
6203 */
6204 static void
6205f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6206{
6207 if (rettv_list_alloc(rettv) != FAIL)
6208 win_id2tabwin(argvars, rettv->vval.v_list);
6209}
6210
6211/*
6212 * "win_id2win()" function
6213 */
6214 static void
6215f_win_id2win(typval_T *argvars, typval_T *rettv)
6216{
6217 rettv->vval.v_number = win_id2win(argvars);
6218}
6219
6220/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006221 * "win_screenpos()" function
6222 */
6223 static void
6224f_win_screenpos(typval_T *argvars, typval_T *rettv)
6225{
6226 win_T *wp;
6227
6228 if (rettv_list_alloc(rettv) == FAIL)
6229 return;
6230
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006231 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006232 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6233 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6234}
6235
6236/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006237 * "getwinpos({timeout})" function
6238 */
6239 static void
6240f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6241{
6242 int x = -1;
6243 int y = -1;
6244
6245 if (rettv_list_alloc(rettv) == FAIL)
6246 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006247#if defined(FEAT_GUI) \
6248 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6249 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006250 {
6251 varnumber_T timeout = 100;
6252
6253 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006254 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006255
6256 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006257 }
6258#endif
6259 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6260 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6261}
6262
6263
6264/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006265 * "getwinposx()" function
6266 */
6267 static void
6268f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6269{
6270 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006271#if defined(FEAT_GUI) \
6272 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6273 || defined(MSWIN)
6274
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006275 {
6276 int x, y;
6277
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006278 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006279 rettv->vval.v_number = x;
6280 }
6281#endif
6282}
6283
6284/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006285 * "getwinposy()" function
6286 */
6287 static void
6288f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6289{
6290 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006291#if defined(FEAT_GUI) \
6292 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6293 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 {
6295 int x, y;
6296
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006297 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006298 rettv->vval.v_number = y;
6299 }
6300#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301}
6302
6303/*
6304 * "getwinvar()" function
6305 */
6306 static void
6307f_getwinvar(typval_T *argvars, typval_T *rettv)
6308{
6309 getwinvar(argvars, rettv, 0);
6310}
6311
6312/*
6313 * "glob()" function
6314 */
6315 static void
6316f_glob(typval_T *argvars, typval_T *rettv)
6317{
6318 int options = WILD_SILENT|WILD_USE_NL;
6319 expand_T xpc;
6320 int error = FALSE;
6321
6322 /* When the optional second argument is non-zero, don't remove matches
6323 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6324 rettv->v_type = VAR_STRING;
6325 if (argvars[1].v_type != VAR_UNKNOWN)
6326 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006327 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006328 options |= WILD_KEEP_ALL;
6329 if (argvars[2].v_type != VAR_UNKNOWN)
6330 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006331 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006332 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006334 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006335 options |= WILD_ALLLINKS;
6336 }
6337 }
6338 if (!error)
6339 {
6340 ExpandInit(&xpc);
6341 xpc.xp_context = EXPAND_FILES;
6342 if (p_wic)
6343 options += WILD_ICASE;
6344 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006345 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006346 NULL, options, WILD_ALL);
6347 else if (rettv_list_alloc(rettv) != FAIL)
6348 {
6349 int i;
6350
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006351 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006352 NULL, options, WILD_ALL_KEEP);
6353 for (i = 0; i < xpc.xp_numfiles; i++)
6354 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6355
6356 ExpandCleanup(&xpc);
6357 }
6358 }
6359 else
6360 rettv->vval.v_string = NULL;
6361}
6362
6363/*
6364 * "globpath()" function
6365 */
6366 static void
6367f_globpath(typval_T *argvars, typval_T *rettv)
6368{
6369 int flags = 0;
6370 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006371 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006372 int error = FALSE;
6373 garray_T ga;
6374 int i;
6375
6376 /* When the optional second argument is non-zero, don't remove matches
6377 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6378 rettv->v_type = VAR_STRING;
6379 if (argvars[2].v_type != VAR_UNKNOWN)
6380 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006381 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 flags |= WILD_KEEP_ALL;
6383 if (argvars[3].v_type != VAR_UNKNOWN)
6384 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006385 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006386 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006387 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006388 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389 flags |= WILD_ALLLINKS;
6390 }
6391 }
6392 if (file != NULL && !error)
6393 {
6394 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006395 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 if (rettv->v_type == VAR_STRING)
6397 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6398 else if (rettv_list_alloc(rettv) != FAIL)
6399 for (i = 0; i < ga.ga_len; ++i)
6400 list_append_string(rettv->vval.v_list,
6401 ((char_u **)(ga.ga_data))[i], -1);
6402 ga_clear_strings(&ga);
6403 }
6404 else
6405 rettv->vval.v_string = NULL;
6406}
6407
6408/*
6409 * "glob2regpat()" function
6410 */
6411 static void
6412f_glob2regpat(typval_T *argvars, typval_T *rettv)
6413{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006414 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415
6416 rettv->v_type = VAR_STRING;
6417 rettv->vval.v_string = (pat == NULL)
6418 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6419}
6420
6421/* for VIM_VERSION_ defines */
6422#include "version.h"
6423
6424/*
6425 * "has()" function
6426 */
6427 static void
6428f_has(typval_T *argvars, typval_T *rettv)
6429{
6430 int i;
6431 char_u *name;
6432 int n = FALSE;
6433 static char *(has_list[]) =
6434 {
6435#ifdef AMIGA
6436 "amiga",
6437# ifdef FEAT_ARP
6438 "arp",
6439# endif
6440#endif
6441#ifdef __BEOS__
6442 "beos",
6443#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006444#if defined(BSD) && !defined(MACOS_X)
6445 "bsd",
6446#endif
6447#ifdef hpux
6448 "hpux",
6449#endif
6450#ifdef __linux__
6451 "linux",
6452#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006453#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006454 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6455 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006456# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006457 "macunix", /* Mac OS X, with the darwin feature */
6458 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006459# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006460#endif
6461#ifdef __QNX__
6462 "qnx",
6463#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006464#ifdef SUN_SYSTEM
6465 "sun",
6466#else
6467 "moon",
6468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006469#ifdef UNIX
6470 "unix",
6471#endif
6472#ifdef VMS
6473 "vms",
6474#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006475#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476 "win32",
6477#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006478#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006479 "win32unix",
6480#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006481#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 "win64",
6483#endif
6484#ifdef EBCDIC
6485 "ebcdic",
6486#endif
6487#ifndef CASE_INSENSITIVE_FILENAME
6488 "fname_case",
6489#endif
6490#ifdef HAVE_ACL
6491 "acl",
6492#endif
6493#ifdef FEAT_ARABIC
6494 "arabic",
6495#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006496 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006497#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006498 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006499#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006500#ifdef FEAT_AUTOSERVERNAME
6501 "autoservername",
6502#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006503#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006504 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006505# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 "balloon_multiline",
6507# endif
6508#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006509#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006510 "balloon_eval_term",
6511#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006512#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6513 "builtin_terms",
6514# ifdef ALL_BUILTIN_TCAPS
6515 "all_builtin_terms",
6516# endif
6517#endif
6518#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006519 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 || defined(FEAT_GUI_MOTIF))
6521 "browsefilter",
6522#endif
6523#ifdef FEAT_BYTEOFF
6524 "byte_offset",
6525#endif
6526#ifdef FEAT_JOB_CHANNEL
6527 "channel",
6528#endif
6529#ifdef FEAT_CINDENT
6530 "cindent",
6531#endif
6532#ifdef FEAT_CLIENTSERVER
6533 "clientserver",
6534#endif
6535#ifdef FEAT_CLIPBOARD
6536 "clipboard",
6537#endif
6538#ifdef FEAT_CMDL_COMPL
6539 "cmdline_compl",
6540#endif
6541#ifdef FEAT_CMDHIST
6542 "cmdline_hist",
6543#endif
6544#ifdef FEAT_COMMENTS
6545 "comments",
6546#endif
6547#ifdef FEAT_CONCEAL
6548 "conceal",
6549#endif
6550#ifdef FEAT_CRYPT
6551 "cryptv",
6552 "crypt-blowfish",
6553 "crypt-blowfish2",
6554#endif
6555#ifdef FEAT_CSCOPE
6556 "cscope",
6557#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006558 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006559#ifdef CURSOR_SHAPE
6560 "cursorshape",
6561#endif
6562#ifdef DEBUG
6563 "debug",
6564#endif
6565#ifdef FEAT_CON_DIALOG
6566 "dialog_con",
6567#endif
6568#ifdef FEAT_GUI_DIALOG
6569 "dialog_gui",
6570#endif
6571#ifdef FEAT_DIFF
6572 "diff",
6573#endif
6574#ifdef FEAT_DIGRAPHS
6575 "digraphs",
6576#endif
6577#ifdef FEAT_DIRECTX
6578 "directx",
6579#endif
6580#ifdef FEAT_DND
6581 "dnd",
6582#endif
6583#ifdef FEAT_EMACS_TAGS
6584 "emacs_tags",
6585#endif
6586 "eval", /* always present, of course! */
6587 "ex_extra", /* graduated feature */
6588#ifdef FEAT_SEARCH_EXTRA
6589 "extra_search",
6590#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006591#ifdef FEAT_SEARCHPATH
6592 "file_in_path",
6593#endif
6594#ifdef FEAT_FILTERPIPE
6595 "filterpipe",
6596#endif
6597#ifdef FEAT_FIND_ID
6598 "find_in_path",
6599#endif
6600#ifdef FEAT_FLOAT
6601 "float",
6602#endif
6603#ifdef FEAT_FOLDING
6604 "folding",
6605#endif
6606#ifdef FEAT_FOOTER
6607 "footer",
6608#endif
6609#if !defined(USE_SYSTEM) && defined(UNIX)
6610 "fork",
6611#endif
6612#ifdef FEAT_GETTEXT
6613 "gettext",
6614#endif
6615#ifdef FEAT_GUI
6616 "gui",
6617#endif
6618#ifdef FEAT_GUI_ATHENA
6619# ifdef FEAT_GUI_NEXTAW
6620 "gui_neXtaw",
6621# else
6622 "gui_athena",
6623# endif
6624#endif
6625#ifdef FEAT_GUI_GTK
6626 "gui_gtk",
6627# ifdef USE_GTK3
6628 "gui_gtk3",
6629# else
6630 "gui_gtk2",
6631# endif
6632#endif
6633#ifdef FEAT_GUI_GNOME
6634 "gui_gnome",
6635#endif
6636#ifdef FEAT_GUI_MAC
6637 "gui_mac",
6638#endif
6639#ifdef FEAT_GUI_MOTIF
6640 "gui_motif",
6641#endif
6642#ifdef FEAT_GUI_PHOTON
6643 "gui_photon",
6644#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006645#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "gui_win32",
6647#endif
6648#ifdef FEAT_HANGULIN
6649 "hangul_input",
6650#endif
6651#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6652 "iconv",
6653#endif
6654#ifdef FEAT_INS_EXPAND
6655 "insert_expand",
6656#endif
6657#ifdef FEAT_JOB_CHANNEL
6658 "job",
6659#endif
6660#ifdef FEAT_JUMPLIST
6661 "jumplist",
6662#endif
6663#ifdef FEAT_KEYMAP
6664 "keymap",
6665#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006666 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006667#ifdef FEAT_LANGMAP
6668 "langmap",
6669#endif
6670#ifdef FEAT_LIBCALL
6671 "libcall",
6672#endif
6673#ifdef FEAT_LINEBREAK
6674 "linebreak",
6675#endif
6676#ifdef FEAT_LISP
6677 "lispindent",
6678#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006680#ifdef FEAT_LOCALMAP
6681 "localmap",
6682#endif
6683#ifdef FEAT_LUA
6684# ifndef DYNAMIC_LUA
6685 "lua",
6686# endif
6687#endif
6688#ifdef FEAT_MENU
6689 "menu",
6690#endif
6691#ifdef FEAT_SESSION
6692 "mksession",
6693#endif
6694#ifdef FEAT_MODIFY_FNAME
6695 "modify_fname",
6696#endif
6697#ifdef FEAT_MOUSE
6698 "mouse",
6699#endif
6700#ifdef FEAT_MOUSESHAPE
6701 "mouseshape",
6702#endif
6703#if defined(UNIX) || defined(VMS)
6704# ifdef FEAT_MOUSE_DEC
6705 "mouse_dec",
6706# endif
6707# ifdef FEAT_MOUSE_GPM
6708 "mouse_gpm",
6709# endif
6710# ifdef FEAT_MOUSE_JSB
6711 "mouse_jsbterm",
6712# endif
6713# ifdef FEAT_MOUSE_NET
6714 "mouse_netterm",
6715# endif
6716# ifdef FEAT_MOUSE_PTERM
6717 "mouse_pterm",
6718# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006719# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006720 "mouse_sgr",
6721# endif
6722# ifdef FEAT_SYSMOUSE
6723 "mouse_sysmouse",
6724# endif
6725# ifdef FEAT_MOUSE_URXVT
6726 "mouse_urxvt",
6727# endif
6728# ifdef FEAT_MOUSE_XTERM
6729 "mouse_xterm",
6730# endif
6731#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006732 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006733#ifdef FEAT_MBYTE_IME
6734 "multi_byte_ime",
6735#endif
6736#ifdef FEAT_MULTI_LANG
6737 "multi_lang",
6738#endif
6739#ifdef FEAT_MZSCHEME
6740#ifndef DYNAMIC_MZSCHEME
6741 "mzscheme",
6742#endif
6743#endif
6744#ifdef FEAT_NUM64
6745 "num64",
6746#endif
6747#ifdef FEAT_OLE
6748 "ole",
6749#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006750#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006751 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006752#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006753#ifdef FEAT_PATH_EXTRA
6754 "path_extra",
6755#endif
6756#ifdef FEAT_PERL
6757#ifndef DYNAMIC_PERL
6758 "perl",
6759#endif
6760#endif
6761#ifdef FEAT_PERSISTENT_UNDO
6762 "persistent_undo",
6763#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006764#if defined(FEAT_PYTHON)
6765 "python_compiled",
6766# if defined(DYNAMIC_PYTHON)
6767 "python_dynamic",
6768# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006769 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006770 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006771# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006773#if defined(FEAT_PYTHON3)
6774 "python3_compiled",
6775# if defined(DYNAMIC_PYTHON3)
6776 "python3_dynamic",
6777# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006778 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006779 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006780# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781#endif
6782#ifdef FEAT_POSTSCRIPT
6783 "postscript",
6784#endif
6785#ifdef FEAT_PRINTER
6786 "printer",
6787#endif
6788#ifdef FEAT_PROFILE
6789 "profile",
6790#endif
6791#ifdef FEAT_RELTIME
6792 "reltime",
6793#endif
6794#ifdef FEAT_QUICKFIX
6795 "quickfix",
6796#endif
6797#ifdef FEAT_RIGHTLEFT
6798 "rightleft",
6799#endif
6800#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6801 "ruby",
6802#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006803 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804#ifdef FEAT_CMDL_INFO
6805 "showcmd",
6806 "cmdline_info",
6807#endif
6808#ifdef FEAT_SIGNS
6809 "signs",
6810#endif
6811#ifdef FEAT_SMARTINDENT
6812 "smartindent",
6813#endif
6814#ifdef STARTUPTIME
6815 "startuptime",
6816#endif
6817#ifdef FEAT_STL_OPT
6818 "statusline",
6819#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820#ifdef FEAT_NETBEANS_INTG
6821 "netbeans_intg",
6822#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006823#ifdef FEAT_SOUND
6824 "sound",
6825#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006826#ifdef FEAT_SPELL
6827 "spell",
6828#endif
6829#ifdef FEAT_SYN_HL
6830 "syntax",
6831#endif
6832#if defined(USE_SYSTEM) || !defined(UNIX)
6833 "system",
6834#endif
6835#ifdef FEAT_TAG_BINS
6836 "tag_binary",
6837#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006838#ifdef FEAT_TCL
6839# ifndef DYNAMIC_TCL
6840 "tcl",
6841# endif
6842#endif
6843#ifdef FEAT_TERMGUICOLORS
6844 "termguicolors",
6845#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006846#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006847 "terminal",
6848#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849#ifdef TERMINFO
6850 "terminfo",
6851#endif
6852#ifdef FEAT_TERMRESPONSE
6853 "termresponse",
6854#endif
6855#ifdef FEAT_TEXTOBJ
6856 "textobjects",
6857#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006858#ifdef FEAT_TEXT_PROP
6859 "textprop",
6860#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861#ifdef HAVE_TGETENT
6862 "tgetent",
6863#endif
6864#ifdef FEAT_TIMERS
6865 "timers",
6866#endif
6867#ifdef FEAT_TITLE
6868 "title",
6869#endif
6870#ifdef FEAT_TOOLBAR
6871 "toolbar",
6872#endif
6873#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6874 "unnamedplus",
6875#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006876 "user-commands", /* was accidentally included in 5.4 */
6877 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006878#ifdef FEAT_VARTABS
6879 "vartabs",
6880#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006881 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006882#ifdef FEAT_VIMINFO
6883 "viminfo",
6884#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006885 "vimscript-1",
6886 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006887 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006888 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006890 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006891 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006892#ifdef FEAT_VTP
6893 "vtp",
6894#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895#ifdef FEAT_WILDIGN
6896 "wildignore",
6897#endif
6898#ifdef FEAT_WILDMENU
6899 "wildmenu",
6900#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902#ifdef FEAT_WAK
6903 "winaltkeys",
6904#endif
6905#ifdef FEAT_WRITEBACKUP
6906 "writebackup",
6907#endif
6908#ifdef FEAT_XIM
6909 "xim",
6910#endif
6911#ifdef FEAT_XFONTSET
6912 "xfontset",
6913#endif
6914#ifdef FEAT_XPM_W32
6915 "xpm",
6916 "xpm_w32", /* for backward compatibility */
6917#else
6918# if defined(HAVE_XPM)
6919 "xpm",
6920# endif
6921#endif
6922#ifdef USE_XSMP
6923 "xsmp",
6924#endif
6925#ifdef USE_XSMP_INTERACT
6926 "xsmp_interact",
6927#endif
6928#ifdef FEAT_XCLIPBOARD
6929 "xterm_clipboard",
6930#endif
6931#ifdef FEAT_XTERM_SAVE
6932 "xterm_save",
6933#endif
6934#if defined(UNIX) && defined(FEAT_X11)
6935 "X11",
6936#endif
6937 NULL
6938 };
6939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006940 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006941 for (i = 0; has_list[i] != NULL; ++i)
6942 if (STRICMP(name, has_list[i]) == 0)
6943 {
6944 n = TRUE;
6945 break;
6946 }
6947
6948 if (n == FALSE)
6949 {
6950 if (STRNICMP(name, "patch", 5) == 0)
6951 {
6952 if (name[5] == '-'
6953 && STRLEN(name) >= 11
6954 && vim_isdigit(name[6])
6955 && vim_isdigit(name[8])
6956 && vim_isdigit(name[10]))
6957 {
6958 int major = atoi((char *)name + 6);
6959 int minor = atoi((char *)name + 8);
6960
6961 /* Expect "patch-9.9.01234". */
6962 n = (major < VIM_VERSION_MAJOR
6963 || (major == VIM_VERSION_MAJOR
6964 && (minor < VIM_VERSION_MINOR
6965 || (minor == VIM_VERSION_MINOR
6966 && has_patch(atoi((char *)name + 10))))));
6967 }
6968 else
6969 n = has_patch(atoi((char *)name + 5));
6970 }
6971 else if (STRICMP(name, "vim_starting") == 0)
6972 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006973 else if (STRICMP(name, "ttyin") == 0)
6974 n = mch_input_isatty();
6975 else if (STRICMP(name, "ttyout") == 0)
6976 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006977 else if (STRICMP(name, "multi_byte_encoding") == 0)
6978 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006979#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 else if (STRICMP(name, "balloon_multiline") == 0)
6981 n = multiline_balloon_available();
6982#endif
6983#ifdef DYNAMIC_TCL
6984 else if (STRICMP(name, "tcl") == 0)
6985 n = tcl_enabled(FALSE);
6986#endif
6987#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6988 else if (STRICMP(name, "iconv") == 0)
6989 n = iconv_enabled(FALSE);
6990#endif
6991#ifdef DYNAMIC_LUA
6992 else if (STRICMP(name, "lua") == 0)
6993 n = lua_enabled(FALSE);
6994#endif
6995#ifdef DYNAMIC_MZSCHEME
6996 else if (STRICMP(name, "mzscheme") == 0)
6997 n = mzscheme_enabled(FALSE);
6998#endif
6999#ifdef DYNAMIC_RUBY
7000 else if (STRICMP(name, "ruby") == 0)
7001 n = ruby_enabled(FALSE);
7002#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007003#ifdef DYNAMIC_PYTHON
7004 else if (STRICMP(name, "python") == 0)
7005 n = python_enabled(FALSE);
7006#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007#ifdef DYNAMIC_PYTHON3
7008 else if (STRICMP(name, "python3") == 0)
7009 n = python3_enabled(FALSE);
7010#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007011#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7012 else if (STRICMP(name, "pythonx") == 0)
7013 {
7014# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7015 if (p_pyx == 0)
7016 n = python3_enabled(FALSE) || python_enabled(FALSE);
7017 else if (p_pyx == 3)
7018 n = python3_enabled(FALSE);
7019 else if (p_pyx == 2)
7020 n = python_enabled(FALSE);
7021# elif defined(DYNAMIC_PYTHON)
7022 n = python_enabled(FALSE);
7023# elif defined(DYNAMIC_PYTHON3)
7024 n = python3_enabled(FALSE);
7025# endif
7026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027#endif
7028#ifdef DYNAMIC_PERL
7029 else if (STRICMP(name, "perl") == 0)
7030 n = perl_enabled(FALSE);
7031#endif
7032#ifdef FEAT_GUI
7033 else if (STRICMP(name, "gui_running") == 0)
7034 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035# ifdef FEAT_BROWSE
7036 else if (STRICMP(name, "browse") == 0)
7037 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7038# endif
7039#endif
7040#ifdef FEAT_SYN_HL
7041 else if (STRICMP(name, "syntax_items") == 0)
7042 n = syntax_present(curwin);
7043#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007044#ifdef FEAT_VTP
7045 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007046 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047#endif
7048#ifdef FEAT_NETBEANS_INTG
7049 else if (STRICMP(name, "netbeans_enabled") == 0)
7050 n = netbeans_active();
7051#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007052#ifdef FEAT_MOUSE_GPM
7053 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7054 n = gpm_enabled();
7055#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007056#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007057 else if (STRICMP(name, "terminal") == 0)
7058 n = terminal_enabled();
7059#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007060#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007061 else if (STRICMP(name, "conpty") == 0)
7062 n = use_conpty();
7063#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 }
7065
7066 rettv->vval.v_number = n;
7067}
7068
7069/*
7070 * "has_key()" function
7071 */
7072 static void
7073f_has_key(typval_T *argvars, typval_T *rettv)
7074{
7075 if (argvars[0].v_type != VAR_DICT)
7076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007077 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 return;
7079 }
7080 if (argvars[0].vval.v_dict == NULL)
7081 return;
7082
7083 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007084 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085}
7086
7087/*
7088 * "haslocaldir()" function
7089 */
7090 static void
7091f_haslocaldir(typval_T *argvars, typval_T *rettv)
7092{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007093 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 win_T *wp = NULL;
7095
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007096 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7097
7098 // Check for window-local and tab-local directories
7099 if (wp != NULL && wp->w_localdir != NULL)
7100 rettv->vval.v_number = 1;
7101 else if (tp != NULL && tp->tp_localdir != NULL)
7102 rettv->vval.v_number = 2;
7103 else
7104 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007105}
7106
7107/*
7108 * "hasmapto()" function
7109 */
7110 static void
7111f_hasmapto(typval_T *argvars, typval_T *rettv)
7112{
7113 char_u *name;
7114 char_u *mode;
7115 char_u buf[NUMBUFLEN];
7116 int abbr = FALSE;
7117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007118 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007119 if (argvars[1].v_type == VAR_UNKNOWN)
7120 mode = (char_u *)"nvo";
7121 else
7122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007123 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007125 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 }
7127
7128 if (map_to_exists(name, mode, abbr))
7129 rettv->vval.v_number = TRUE;
7130 else
7131 rettv->vval.v_number = FALSE;
7132}
7133
7134/*
7135 * "histadd()" function
7136 */
7137 static void
7138f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7139{
7140#ifdef FEAT_CMDHIST
7141 int histype;
7142 char_u *str;
7143 char_u buf[NUMBUFLEN];
7144#endif
7145
7146 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007147 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007148 return;
7149#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007150 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151 histype = str != NULL ? get_histtype(str) : -1;
7152 if (histype >= 0)
7153 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007154 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 if (*str != NUL)
7156 {
7157 init_history();
7158 add_to_history(histype, str, FALSE, NUL);
7159 rettv->vval.v_number = TRUE;
7160 return;
7161 }
7162 }
7163#endif
7164}
7165
7166/*
7167 * "histdel()" function
7168 */
7169 static void
7170f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7171{
7172#ifdef FEAT_CMDHIST
7173 int n;
7174 char_u buf[NUMBUFLEN];
7175 char_u *str;
7176
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007177 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007178 if (str == NULL)
7179 n = 0;
7180 else if (argvars[1].v_type == VAR_UNKNOWN)
7181 /* only one argument: clear entire history */
7182 n = clr_history(get_histtype(str));
7183 else if (argvars[1].v_type == VAR_NUMBER)
7184 /* index given: remove that entry */
7185 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007186 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007187 else
7188 /* string given: remove all matching entries */
7189 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007190 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007191 rettv->vval.v_number = n;
7192#endif
7193}
7194
7195/*
7196 * "histget()" function
7197 */
7198 static void
7199f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7200{
7201#ifdef FEAT_CMDHIST
7202 int type;
7203 int idx;
7204 char_u *str;
7205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007207 if (str == NULL)
7208 rettv->vval.v_string = NULL;
7209 else
7210 {
7211 type = get_histtype(str);
7212 if (argvars[1].v_type == VAR_UNKNOWN)
7213 idx = get_history_idx(type);
7214 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007215 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216 /* -1 on type error */
7217 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7218 }
7219#else
7220 rettv->vval.v_string = NULL;
7221#endif
7222 rettv->v_type = VAR_STRING;
7223}
7224
7225/*
7226 * "histnr()" function
7227 */
7228 static void
7229f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7230{
7231 int i;
7232
7233#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007234 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007235
7236 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7237 if (i >= HIST_CMD && i < HIST_COUNT)
7238 i = get_history_idx(i);
7239 else
7240#endif
7241 i = -1;
7242 rettv->vval.v_number = i;
7243}
7244
7245/*
7246 * "highlightID(name)" function
7247 */
7248 static void
7249f_hlID(typval_T *argvars, typval_T *rettv)
7250{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007251 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252}
7253
7254/*
7255 * "highlight_exists()" function
7256 */
7257 static void
7258f_hlexists(typval_T *argvars, typval_T *rettv)
7259{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007260 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007261}
7262
7263/*
7264 * "hostname()" function
7265 */
7266 static void
7267f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7268{
7269 char_u hostname[256];
7270
7271 mch_get_host_name(hostname, 256);
7272 rettv->v_type = VAR_STRING;
7273 rettv->vval.v_string = vim_strsave(hostname);
7274}
7275
7276/*
7277 * iconv() function
7278 */
7279 static void
7280f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7281{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007282 char_u buf1[NUMBUFLEN];
7283 char_u buf2[NUMBUFLEN];
7284 char_u *from, *to, *str;
7285 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286
7287 rettv->v_type = VAR_STRING;
7288 rettv->vval.v_string = NULL;
7289
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007290 str = tv_get_string(&argvars[0]);
7291 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7292 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 vimconv.vc_type = CONV_NONE;
7294 convert_setup(&vimconv, from, to);
7295
7296 /* If the encodings are equal, no conversion needed. */
7297 if (vimconv.vc_type == CONV_NONE)
7298 rettv->vval.v_string = vim_strsave(str);
7299 else
7300 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7301
7302 convert_setup(&vimconv, NULL, NULL);
7303 vim_free(from);
7304 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007305}
7306
7307/*
7308 * "indent()" function
7309 */
7310 static void
7311f_indent(typval_T *argvars, typval_T *rettv)
7312{
7313 linenr_T lnum;
7314
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007315 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007316 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7317 rettv->vval.v_number = get_indent_lnum(lnum);
7318 else
7319 rettv->vval.v_number = -1;
7320}
7321
7322/*
7323 * "index()" function
7324 */
7325 static void
7326f_index(typval_T *argvars, typval_T *rettv)
7327{
7328 list_T *l;
7329 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007330 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 long idx = 0;
7332 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007333 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334
7335 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007336 if (argvars[0].v_type == VAR_BLOB)
7337 {
7338 typval_T tv;
7339 int start = 0;
7340
7341 if (argvars[2].v_type != VAR_UNKNOWN)
7342 {
7343 start = tv_get_number_chk(&argvars[2], &error);
7344 if (error)
7345 return;
7346 }
7347 b = argvars[0].vval.v_blob;
7348 if (b == NULL)
7349 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007350 if (start < 0)
7351 {
7352 start = blob_len(b) + start;
7353 if (start < 0)
7354 start = 0;
7355 }
7356
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007357 for (idx = start; idx < blob_len(b); ++idx)
7358 {
7359 tv.v_type = VAR_NUMBER;
7360 tv.vval.v_number = blob_get(b, idx);
7361 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7362 {
7363 rettv->vval.v_number = idx;
7364 return;
7365 }
7366 }
7367 return;
7368 }
7369 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007370 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007371 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007372 return;
7373 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007374
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007375 l = argvars[0].vval.v_list;
7376 if (l != NULL)
7377 {
7378 item = l->lv_first;
7379 if (argvars[2].v_type != VAR_UNKNOWN)
7380 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007381 /* Start at specified item. Use the cached index that list_find()
7382 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007383 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007384 idx = l->lv_idx;
7385 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007386 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 if (error)
7388 item = NULL;
7389 }
7390
7391 for ( ; item != NULL; item = item->li_next, ++idx)
7392 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7393 {
7394 rettv->vval.v_number = idx;
7395 break;
7396 }
7397 }
7398}
7399
7400static int inputsecret_flag = 0;
7401
7402/*
7403 * "input()" function
7404 * Also handles inputsecret() when inputsecret is set.
7405 */
7406 static void
7407f_input(typval_T *argvars, typval_T *rettv)
7408{
7409 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7410}
7411
7412/*
7413 * "inputdialog()" function
7414 */
7415 static void
7416f_inputdialog(typval_T *argvars, typval_T *rettv)
7417{
7418#if defined(FEAT_GUI_TEXTDIALOG)
7419 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7420 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7421 {
7422 char_u *message;
7423 char_u buf[NUMBUFLEN];
7424 char_u *defstr = (char_u *)"";
7425
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007426 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007427 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007428 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7430 else
7431 IObuff[0] = NUL;
7432 if (message != NULL && defstr != NULL
7433 && do_dialog(VIM_QUESTION, NULL, message,
7434 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7435 rettv->vval.v_string = vim_strsave(IObuff);
7436 else
7437 {
7438 if (message != NULL && defstr != NULL
7439 && argvars[1].v_type != VAR_UNKNOWN
7440 && argvars[2].v_type != VAR_UNKNOWN)
7441 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007442 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 else
7444 rettv->vval.v_string = NULL;
7445 }
7446 rettv->v_type = VAR_STRING;
7447 }
7448 else
7449#endif
7450 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7451}
7452
7453/*
7454 * "inputlist()" function
7455 */
7456 static void
7457f_inputlist(typval_T *argvars, typval_T *rettv)
7458{
7459 listitem_T *li;
7460 int selected;
7461 int mouse_used;
7462
7463#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007464 /* While starting up, there is no place to enter text. When running tests
7465 * with --not-a-term we assume feedkeys() will be used. */
7466 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007467 return;
7468#endif
7469 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007471 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 return;
7473 }
7474
7475 msg_start();
7476 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7477 lines_left = Rows; /* avoid more prompt */
7478 msg_scroll = TRUE;
7479 msg_clr_eos();
7480
7481 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7482 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007483 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007484 msg_putchar('\n');
7485 }
7486
7487 /* Ask for choice. */
7488 selected = prompt_for_number(&mouse_used);
7489 if (mouse_used)
7490 selected -= lines_left;
7491
7492 rettv->vval.v_number = selected;
7493}
7494
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007495static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7496
7497/*
7498 * "inputrestore()" function
7499 */
7500 static void
7501f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7502{
7503 if (ga_userinput.ga_len > 0)
7504 {
7505 --ga_userinput.ga_len;
7506 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7507 + ga_userinput.ga_len);
7508 /* default return is zero == OK */
7509 }
7510 else if (p_verbose > 1)
7511 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007512 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007513 rettv->vval.v_number = 1; /* Failed */
7514 }
7515}
7516
7517/*
7518 * "inputsave()" function
7519 */
7520 static void
7521f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7522{
7523 /* Add an entry to the stack of typeahead storage. */
7524 if (ga_grow(&ga_userinput, 1) == OK)
7525 {
7526 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7527 + ga_userinput.ga_len);
7528 ++ga_userinput.ga_len;
7529 /* default return is zero == OK */
7530 }
7531 else
7532 rettv->vval.v_number = 1; /* Failed */
7533}
7534
7535/*
7536 * "inputsecret()" function
7537 */
7538 static void
7539f_inputsecret(typval_T *argvars, typval_T *rettv)
7540{
7541 ++cmdline_star;
7542 ++inputsecret_flag;
7543 f_input(argvars, rettv);
7544 --cmdline_star;
7545 --inputsecret_flag;
7546}
7547
7548/*
7549 * "insert()" function
7550 */
7551 static void
7552f_insert(typval_T *argvars, typval_T *rettv)
7553{
7554 long before = 0;
7555 listitem_T *item;
7556 list_T *l;
7557 int error = FALSE;
7558
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007559 if (argvars[0].v_type == VAR_BLOB)
7560 {
7561 int val, len;
7562 char_u *p;
7563
7564 len = blob_len(argvars[0].vval.v_blob);
7565 if (argvars[2].v_type != VAR_UNKNOWN)
7566 {
7567 before = (long)tv_get_number_chk(&argvars[2], &error);
7568 if (error)
7569 return; // type error; errmsg already given
7570 if (before < 0 || before > len)
7571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007572 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007573 return;
7574 }
7575 }
7576 val = tv_get_number_chk(&argvars[1], &error);
7577 if (error)
7578 return;
7579 if (val < 0 || val > 255)
7580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007581 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007582 return;
7583 }
7584
7585 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7586 return;
7587 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7588 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7589 *(p + before) = val;
7590 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7591
7592 copy_tv(&argvars[0], rettv);
7593 }
7594 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007595 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007596 else if ((l = argvars[0].vval.v_list) != NULL
7597 && !var_check_lock(l->lv_lock,
7598 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599 {
7600 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007601 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007602 if (error)
7603 return; /* type error; errmsg already given */
7604
7605 if (before == l->lv_len)
7606 item = NULL;
7607 else
7608 {
7609 item = list_find(l, before);
7610 if (item == NULL)
7611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007612 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007613 l = NULL;
7614 }
7615 }
7616 if (l != NULL)
7617 {
7618 list_insert_tv(l, &argvars[1], item);
7619 copy_tv(&argvars[0], rettv);
7620 }
7621 }
7622}
7623
7624/*
7625 * "invert(expr)" function
7626 */
7627 static void
7628f_invert(typval_T *argvars, typval_T *rettv)
7629{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007630 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007631}
7632
7633/*
7634 * "isdirectory()" function
7635 */
7636 static void
7637f_isdirectory(typval_T *argvars, typval_T *rettv)
7638{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007639 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640}
7641
7642/*
7643 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7644 * or it refers to a List or Dictionary that is locked.
7645 */
7646 static int
7647tv_islocked(typval_T *tv)
7648{
7649 return (tv->v_lock & VAR_LOCKED)
7650 || (tv->v_type == VAR_LIST
7651 && tv->vval.v_list != NULL
7652 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7653 || (tv->v_type == VAR_DICT
7654 && tv->vval.v_dict != NULL
7655 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7656}
7657
7658/*
7659 * "islocked()" function
7660 */
7661 static void
7662f_islocked(typval_T *argvars, typval_T *rettv)
7663{
7664 lval_T lv;
7665 char_u *end;
7666 dictitem_T *di;
7667
7668 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007669 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007670 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007671 if (end != NULL && lv.ll_name != NULL)
7672 {
7673 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007674 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007675 else
7676 {
7677 if (lv.ll_tv == NULL)
7678 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007679 di = find_var(lv.ll_name, NULL, TRUE);
7680 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007682 /* Consider a variable locked when:
7683 * 1. the variable itself is locked
7684 * 2. the value of the variable is locked.
7685 * 3. the List or Dict value is locked.
7686 */
7687 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7688 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007689 }
7690 }
7691 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007692 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007693 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007694 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 else if (lv.ll_list != NULL)
7696 /* List item. */
7697 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7698 else
7699 /* Dictionary item. */
7700 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7701 }
7702 }
7703
7704 clear_lval(&lv);
7705}
7706
7707#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7708/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007709 * "isinf()" function
7710 */
7711 static void
7712f_isinf(typval_T *argvars, typval_T *rettv)
7713{
7714 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7715 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7716}
7717
7718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007719 * "isnan()" function
7720 */
7721 static void
7722f_isnan(typval_T *argvars, typval_T *rettv)
7723{
7724 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7725 && isnan(argvars[0].vval.v_float);
7726}
7727#endif
7728
7729/*
7730 * "items(dict)" function
7731 */
7732 static void
7733f_items(typval_T *argvars, typval_T *rettv)
7734{
7735 dict_list(argvars, rettv, 2);
7736}
7737
7738#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7739/*
7740 * Get the job from the argument.
7741 * Returns NULL if the job is invalid.
7742 */
7743 static job_T *
7744get_job_arg(typval_T *tv)
7745{
7746 job_T *job;
7747
7748 if (tv->v_type != VAR_JOB)
7749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007750 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751 return NULL;
7752 }
7753 job = tv->vval.v_job;
7754
7755 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007756 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 return job;
7758}
7759
7760/*
7761 * "job_getchannel()" function
7762 */
7763 static void
7764f_job_getchannel(typval_T *argvars, typval_T *rettv)
7765{
7766 job_T *job = get_job_arg(&argvars[0]);
7767
7768 if (job != NULL)
7769 {
7770 rettv->v_type = VAR_CHANNEL;
7771 rettv->vval.v_channel = job->jv_channel;
7772 if (job->jv_channel != NULL)
7773 ++job->jv_channel->ch_refcount;
7774 }
7775}
7776
7777/*
7778 * "job_info()" function
7779 */
7780 static void
7781f_job_info(typval_T *argvars, typval_T *rettv)
7782{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007783 if (argvars[0].v_type != VAR_UNKNOWN)
7784 {
7785 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007787 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7788 job_info(job, rettv->vval.v_dict);
7789 }
7790 else if (rettv_list_alloc(rettv) == OK)
7791 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007792}
7793
7794/*
7795 * "job_setoptions()" function
7796 */
7797 static void
7798f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7799{
7800 job_T *job = get_job_arg(&argvars[0]);
7801 jobopt_T opt;
7802
7803 if (job == NULL)
7804 return;
7805 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007806 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807 job_set_options(job, &opt);
7808 free_job_options(&opt);
7809}
7810
7811/*
7812 * "job_start()" function
7813 */
7814 static void
7815f_job_start(typval_T *argvars, typval_T *rettv)
7816{
7817 rettv->v_type = VAR_JOB;
7818 if (check_restricted() || check_secure())
7819 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007820 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821}
7822
7823/*
7824 * "job_status()" function
7825 */
7826 static void
7827f_job_status(typval_T *argvars, typval_T *rettv)
7828{
7829 job_T *job = get_job_arg(&argvars[0]);
7830
7831 if (job != NULL)
7832 {
7833 rettv->v_type = VAR_STRING;
7834 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7835 }
7836}
7837
7838/*
7839 * "job_stop()" function
7840 */
7841 static void
7842f_job_stop(typval_T *argvars, typval_T *rettv)
7843{
7844 job_T *job = get_job_arg(&argvars[0]);
7845
7846 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007847 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007848}
7849#endif
7850
7851/*
7852 * "join()" function
7853 */
7854 static void
7855f_join(typval_T *argvars, typval_T *rettv)
7856{
7857 garray_T ga;
7858 char_u *sep;
7859
7860 if (argvars[0].v_type != VAR_LIST)
7861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007862 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007863 return;
7864 }
7865 if (argvars[0].vval.v_list == NULL)
7866 return;
7867 if (argvars[1].v_type == VAR_UNKNOWN)
7868 sep = (char_u *)" ";
7869 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007870 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007871
7872 rettv->v_type = VAR_STRING;
7873
7874 if (sep != NULL)
7875 {
7876 ga_init2(&ga, (int)sizeof(char), 80);
7877 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7878 ga_append(&ga, NUL);
7879 rettv->vval.v_string = (char_u *)ga.ga_data;
7880 }
7881 else
7882 rettv->vval.v_string = NULL;
7883}
7884
7885/*
7886 * "js_decode()" function
7887 */
7888 static void
7889f_js_decode(typval_T *argvars, typval_T *rettv)
7890{
7891 js_read_T reader;
7892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007893 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 reader.js_fill = NULL;
7895 reader.js_used = 0;
7896 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007897 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898}
7899
7900/*
7901 * "js_encode()" function
7902 */
7903 static void
7904f_js_encode(typval_T *argvars, typval_T *rettv)
7905{
7906 rettv->v_type = VAR_STRING;
7907 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7908}
7909
7910/*
7911 * "json_decode()" function
7912 */
7913 static void
7914f_json_decode(typval_T *argvars, typval_T *rettv)
7915{
7916 js_read_T reader;
7917
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007918 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007919 reader.js_fill = NULL;
7920 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007921 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007922}
7923
7924/*
7925 * "json_encode()" function
7926 */
7927 static void
7928f_json_encode(typval_T *argvars, typval_T *rettv)
7929{
7930 rettv->v_type = VAR_STRING;
7931 rettv->vval.v_string = json_encode(&argvars[0], 0);
7932}
7933
7934/*
7935 * "keys()" function
7936 */
7937 static void
7938f_keys(typval_T *argvars, typval_T *rettv)
7939{
7940 dict_list(argvars, rettv, 0);
7941}
7942
7943/*
7944 * "last_buffer_nr()" function.
7945 */
7946 static void
7947f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7948{
7949 int n = 0;
7950 buf_T *buf;
7951
Bram Moolenaar29323592016-07-24 22:04:11 +02007952 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953 if (n < buf->b_fnum)
7954 n = buf->b_fnum;
7955
7956 rettv->vval.v_number = n;
7957}
7958
7959/*
7960 * "len()" function
7961 */
7962 static void
7963f_len(typval_T *argvars, typval_T *rettv)
7964{
7965 switch (argvars[0].v_type)
7966 {
7967 case VAR_STRING:
7968 case VAR_NUMBER:
7969 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007970 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007971 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007972 case VAR_BLOB:
7973 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7974 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007975 case VAR_LIST:
7976 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7977 break;
7978 case VAR_DICT:
7979 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7980 break;
7981 case VAR_UNKNOWN:
7982 case VAR_SPECIAL:
7983 case VAR_FLOAT:
7984 case VAR_FUNC:
7985 case VAR_PARTIAL:
7986 case VAR_JOB:
7987 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007988 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007989 break;
7990 }
7991}
7992
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007993 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007994libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007995{
7996#ifdef FEAT_LIBCALL
7997 char_u *string_in;
7998 char_u **string_result;
7999 int nr_result;
8000#endif
8001
8002 rettv->v_type = type;
8003 if (type != VAR_NUMBER)
8004 rettv->vval.v_string = NULL;
8005
8006 if (check_restricted() || check_secure())
8007 return;
8008
8009#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008010 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008011 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8012 {
8013 string_in = NULL;
8014 if (argvars[2].v_type == VAR_STRING)
8015 string_in = argvars[2].vval.v_string;
8016 if (type == VAR_NUMBER)
8017 string_result = NULL;
8018 else
8019 string_result = &rettv->vval.v_string;
8020 if (mch_libcall(argvars[0].vval.v_string,
8021 argvars[1].vval.v_string,
8022 string_in,
8023 argvars[2].vval.v_number,
8024 string_result,
8025 &nr_result) == OK
8026 && type == VAR_NUMBER)
8027 rettv->vval.v_number = nr_result;
8028 }
8029#endif
8030}
8031
8032/*
8033 * "libcall()" function
8034 */
8035 static void
8036f_libcall(typval_T *argvars, typval_T *rettv)
8037{
8038 libcall_common(argvars, rettv, VAR_STRING);
8039}
8040
8041/*
8042 * "libcallnr()" function
8043 */
8044 static void
8045f_libcallnr(typval_T *argvars, typval_T *rettv)
8046{
8047 libcall_common(argvars, rettv, VAR_NUMBER);
8048}
8049
8050/*
8051 * "line(string)" function
8052 */
8053 static void
8054f_line(typval_T *argvars, typval_T *rettv)
8055{
8056 linenr_T lnum = 0;
8057 pos_T *fp;
8058 int fnum;
8059
8060 fp = var2fpos(&argvars[0], TRUE, &fnum);
8061 if (fp != NULL)
8062 lnum = fp->lnum;
8063 rettv->vval.v_number = lnum;
8064}
8065
8066/*
8067 * "line2byte(lnum)" function
8068 */
8069 static void
8070f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8071{
8072#ifndef FEAT_BYTEOFF
8073 rettv->vval.v_number = -1;
8074#else
8075 linenr_T lnum;
8076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008077 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008078 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8079 rettv->vval.v_number = -1;
8080 else
8081 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8082 if (rettv->vval.v_number >= 0)
8083 ++rettv->vval.v_number;
8084#endif
8085}
8086
8087/*
8088 * "lispindent(lnum)" function
8089 */
8090 static void
8091f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8092{
8093#ifdef FEAT_LISP
8094 pos_T pos;
8095 linenr_T lnum;
8096
8097 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008098 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008099 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8100 {
8101 curwin->w_cursor.lnum = lnum;
8102 rettv->vval.v_number = get_lisp_indent();
8103 curwin->w_cursor = pos;
8104 }
8105 else
8106#endif
8107 rettv->vval.v_number = -1;
8108}
8109
8110/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008111 * "list2str()" function
8112 */
8113 static void
8114f_list2str(typval_T *argvars, typval_T *rettv)
8115{
8116 list_T *l;
8117 listitem_T *li;
8118 garray_T ga;
8119 int utf8 = FALSE;
8120
8121 rettv->v_type = VAR_STRING;
8122 rettv->vval.v_string = NULL;
8123 if (argvars[0].v_type != VAR_LIST)
8124 {
8125 emsg(_(e_invarg));
8126 return;
8127 }
8128
8129 l = argvars[0].vval.v_list;
8130 if (l == NULL)
8131 return; // empty list results in empty string
8132
8133 if (argvars[1].v_type != VAR_UNKNOWN)
8134 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8135
8136 ga_init2(&ga, 1, 80);
8137 if (has_mbyte || utf8)
8138 {
8139 char_u buf[MB_MAXBYTES + 1];
8140 int (*char2bytes)(int, char_u *);
8141
8142 if (utf8 || enc_utf8)
8143 char2bytes = utf_char2bytes;
8144 else
8145 char2bytes = mb_char2bytes;
8146
8147 for (li = l->lv_first; li != NULL; li = li->li_next)
8148 {
8149 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8150 ga_concat(&ga, buf);
8151 }
8152 ga_append(&ga, NUL);
8153 }
8154 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8155 {
8156 for (li = l->lv_first; li != NULL; li = li->li_next)
8157 ga_append(&ga, tv_get_number(&li->li_tv));
8158 ga_append(&ga, NUL);
8159 }
8160
8161 rettv->v_type = VAR_STRING;
8162 rettv->vval.v_string = ga.ga_data;
8163}
8164
8165/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008166 * "localtime()" function
8167 */
8168 static void
8169f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8170{
8171 rettv->vval.v_number = (varnumber_T)time(NULL);
8172}
8173
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008174 static void
8175get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8176{
8177 char_u *keys;
8178 char_u *which;
8179 char_u buf[NUMBUFLEN];
8180 char_u *keys_buf = NULL;
8181 char_u *rhs;
8182 int mode;
8183 int abbr = FALSE;
8184 int get_dict = FALSE;
8185 mapblock_T *mp;
8186 int buffer_local;
8187
8188 /* return empty string for failure */
8189 rettv->v_type = VAR_STRING;
8190 rettv->vval.v_string = NULL;
8191
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008192 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008193 if (*keys == NUL)
8194 return;
8195
8196 if (argvars[1].v_type != VAR_UNKNOWN)
8197 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008198 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008199 if (argvars[2].v_type != VAR_UNKNOWN)
8200 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008201 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008202 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008203 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204 }
8205 }
8206 else
8207 which = (char_u *)"";
8208 if (which == NULL)
8209 return;
8210
8211 mode = get_map_mode(&which, 0);
8212
8213 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8214 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8215 vim_free(keys_buf);
8216
8217 if (!get_dict)
8218 {
8219 /* Return a string. */
8220 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008221 {
8222 if (*rhs == NUL)
8223 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8224 else
8225 rettv->vval.v_string = str2special_save(rhs, FALSE);
8226 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008227
8228 }
8229 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8230 {
8231 /* Return a dictionary. */
8232 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8233 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8234 dict_T *dict = rettv->vval.v_dict;
8235
Bram Moolenaare0be1672018-07-08 16:50:37 +02008236 dict_add_string(dict, "lhs", lhs);
8237 dict_add_string(dict, "rhs", mp->m_orig_str);
8238 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8239 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8240 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008241 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8242 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008243 dict_add_number(dict, "buffer", (long)buffer_local);
8244 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8245 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008246
8247 vim_free(lhs);
8248 vim_free(mapmode);
8249 }
8250}
8251
8252#ifdef FEAT_FLOAT
8253/*
8254 * "log()" function
8255 */
8256 static void
8257f_log(typval_T *argvars, typval_T *rettv)
8258{
8259 float_T f = 0.0;
8260
8261 rettv->v_type = VAR_FLOAT;
8262 if (get_float_arg(argvars, &f) == OK)
8263 rettv->vval.v_float = log(f);
8264 else
8265 rettv->vval.v_float = 0.0;
8266}
8267
8268/*
8269 * "log10()" function
8270 */
8271 static void
8272f_log10(typval_T *argvars, typval_T *rettv)
8273{
8274 float_T f = 0.0;
8275
8276 rettv->v_type = VAR_FLOAT;
8277 if (get_float_arg(argvars, &f) == OK)
8278 rettv->vval.v_float = log10(f);
8279 else
8280 rettv->vval.v_float = 0.0;
8281}
8282#endif
8283
8284#ifdef FEAT_LUA
8285/*
8286 * "luaeval()" function
8287 */
8288 static void
8289f_luaeval(typval_T *argvars, typval_T *rettv)
8290{
8291 char_u *str;
8292 char_u buf[NUMBUFLEN];
8293
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008294 if (check_restricted() || check_secure())
8295 return;
8296
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008297 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298 do_luaeval(str, argvars + 1, rettv);
8299}
8300#endif
8301
8302/*
8303 * "map()" function
8304 */
8305 static void
8306f_map(typval_T *argvars, typval_T *rettv)
8307{
8308 filter_map(argvars, rettv, TRUE);
8309}
8310
8311/*
8312 * "maparg()" function
8313 */
8314 static void
8315f_maparg(typval_T *argvars, typval_T *rettv)
8316{
8317 get_maparg(argvars, rettv, TRUE);
8318}
8319
8320/*
8321 * "mapcheck()" function
8322 */
8323 static void
8324f_mapcheck(typval_T *argvars, typval_T *rettv)
8325{
8326 get_maparg(argvars, rettv, FALSE);
8327}
8328
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008329typedef enum
8330{
8331 MATCH_END, /* matchend() */
8332 MATCH_MATCH, /* match() */
8333 MATCH_STR, /* matchstr() */
8334 MATCH_LIST, /* matchlist() */
8335 MATCH_POS /* matchstrpos() */
8336} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008337
8338 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008339find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008340{
8341 char_u *str = NULL;
8342 long len = 0;
8343 char_u *expr = NULL;
8344 char_u *pat;
8345 regmatch_T regmatch;
8346 char_u patbuf[NUMBUFLEN];
8347 char_u strbuf[NUMBUFLEN];
8348 char_u *save_cpo;
8349 long start = 0;
8350 long nth = 1;
8351 colnr_T startcol = 0;
8352 int match = 0;
8353 list_T *l = NULL;
8354 listitem_T *li = NULL;
8355 long idx = 0;
8356 char_u *tofree = NULL;
8357
8358 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8359 save_cpo = p_cpo;
8360 p_cpo = (char_u *)"";
8361
8362 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008363 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008364 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008365 /* type MATCH_LIST: return empty list when there are no matches.
8366 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 if (rettv_list_alloc(rettv) == FAIL)
8368 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008369 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 && (list_append_string(rettv->vval.v_list,
8371 (char_u *)"", 0) == FAIL
8372 || list_append_number(rettv->vval.v_list,
8373 (varnumber_T)-1) == FAIL
8374 || list_append_number(rettv->vval.v_list,
8375 (varnumber_T)-1) == FAIL
8376 || list_append_number(rettv->vval.v_list,
8377 (varnumber_T)-1) == FAIL))
8378 {
8379 list_free(rettv->vval.v_list);
8380 rettv->vval.v_list = NULL;
8381 goto theend;
8382 }
8383 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008384 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008385 {
8386 rettv->v_type = VAR_STRING;
8387 rettv->vval.v_string = NULL;
8388 }
8389
8390 if (argvars[0].v_type == VAR_LIST)
8391 {
8392 if ((l = argvars[0].vval.v_list) == NULL)
8393 goto theend;
8394 li = l->lv_first;
8395 }
8396 else
8397 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 len = (long)STRLEN(str);
8400 }
8401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008402 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 if (pat == NULL)
8404 goto theend;
8405
8406 if (argvars[2].v_type != VAR_UNKNOWN)
8407 {
8408 int error = FALSE;
8409
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008410 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008411 if (error)
8412 goto theend;
8413 if (l != NULL)
8414 {
8415 li = list_find(l, start);
8416 if (li == NULL)
8417 goto theend;
8418 idx = l->lv_idx; /* use the cached index */
8419 }
8420 else
8421 {
8422 if (start < 0)
8423 start = 0;
8424 if (start > len)
8425 goto theend;
8426 /* When "count" argument is there ignore matches before "start",
8427 * otherwise skip part of the string. Differs when pattern is "^"
8428 * or "\<". */
8429 if (argvars[3].v_type != VAR_UNKNOWN)
8430 startcol = start;
8431 else
8432 {
8433 str += start;
8434 len -= start;
8435 }
8436 }
8437
8438 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008439 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (error)
8441 goto theend;
8442 }
8443
8444 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8445 if (regmatch.regprog != NULL)
8446 {
8447 regmatch.rm_ic = p_ic;
8448
8449 for (;;)
8450 {
8451 if (l != NULL)
8452 {
8453 if (li == NULL)
8454 {
8455 match = FALSE;
8456 break;
8457 }
8458 vim_free(tofree);
8459 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8460 if (str == NULL)
8461 break;
8462 }
8463
8464 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8465
8466 if (match && --nth <= 0)
8467 break;
8468 if (l == NULL && !match)
8469 break;
8470
8471 /* Advance to just after the match. */
8472 if (l != NULL)
8473 {
8474 li = li->li_next;
8475 ++idx;
8476 }
8477 else
8478 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 startcol = (colnr_T)(regmatch.startp[0]
8480 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 if (startcol > (colnr_T)len
8482 || str + startcol <= regmatch.startp[0])
8483 {
8484 match = FALSE;
8485 break;
8486 }
8487 }
8488 }
8489
8490 if (match)
8491 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008492 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493 {
8494 listitem_T *li1 = rettv->vval.v_list->lv_first;
8495 listitem_T *li2 = li1->li_next;
8496 listitem_T *li3 = li2->li_next;
8497 listitem_T *li4 = li3->li_next;
8498
8499 vim_free(li1->li_tv.vval.v_string);
8500 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8501 (int)(regmatch.endp[0] - regmatch.startp[0]));
8502 li3->li_tv.vval.v_number =
8503 (varnumber_T)(regmatch.startp[0] - expr);
8504 li4->li_tv.vval.v_number =
8505 (varnumber_T)(regmatch.endp[0] - expr);
8506 if (l != NULL)
8507 li2->li_tv.vval.v_number = (varnumber_T)idx;
8508 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008509 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008510 {
8511 int i;
8512
8513 /* return list with matched string and submatches */
8514 for (i = 0; i < NSUBEXP; ++i)
8515 {
8516 if (regmatch.endp[i] == NULL)
8517 {
8518 if (list_append_string(rettv->vval.v_list,
8519 (char_u *)"", 0) == FAIL)
8520 break;
8521 }
8522 else if (list_append_string(rettv->vval.v_list,
8523 regmatch.startp[i],
8524 (int)(regmatch.endp[i] - regmatch.startp[i]))
8525 == FAIL)
8526 break;
8527 }
8528 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008529 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008530 {
8531 /* return matched string */
8532 if (l != NULL)
8533 copy_tv(&li->li_tv, rettv);
8534 else
8535 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8536 (int)(regmatch.endp[0] - regmatch.startp[0]));
8537 }
8538 else if (l != NULL)
8539 rettv->vval.v_number = idx;
8540 else
8541 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008542 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008543 rettv->vval.v_number =
8544 (varnumber_T)(regmatch.startp[0] - str);
8545 else
8546 rettv->vval.v_number =
8547 (varnumber_T)(regmatch.endp[0] - str);
8548 rettv->vval.v_number += (varnumber_T)(str - expr);
8549 }
8550 }
8551 vim_regfree(regmatch.regprog);
8552 }
8553
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008554theend:
8555 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008556 /* matchstrpos() without a list: drop the second item. */
8557 listitem_remove(rettv->vval.v_list,
8558 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008559 vim_free(tofree);
8560 p_cpo = save_cpo;
8561}
8562
8563/*
8564 * "match()" function
8565 */
8566 static void
8567f_match(typval_T *argvars, typval_T *rettv)
8568{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008569 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570}
8571
Bram Moolenaar95e51472018-07-28 16:55:56 +02008572#ifdef FEAT_SEARCH_EXTRA
8573 static int
8574matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8575{
8576 dictitem_T *di;
8577
8578 if (tv->v_type != VAR_DICT)
8579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008580 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008581 return FAIL;
8582 }
8583
8584 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008585 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008586 (char_u *)"conceal", FALSE);
8587
8588 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8589 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008590 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008591 if (*win == NULL)
8592 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008593 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008594 return FAIL;
8595 }
8596 }
8597
8598 return OK;
8599}
8600#endif
8601
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602/*
8603 * "matchadd()" function
8604 */
8605 static void
8606f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8607{
8608#ifdef FEAT_SEARCH_EXTRA
8609 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008610 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8611 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 int prio = 10; /* default priority */
8613 int id = -1;
8614 int error = FALSE;
8615 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008616 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008617
8618 rettv->vval.v_number = -1;
8619
8620 if (grp == NULL || pat == NULL)
8621 return;
8622 if (argvars[2].v_type != VAR_UNKNOWN)
8623 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008624 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008625 if (argvars[3].v_type != VAR_UNKNOWN)
8626 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008627 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008628 if (argvars[4].v_type != VAR_UNKNOWN
8629 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8630 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 }
8632 }
8633 if (error == TRUE)
8634 return;
8635 if (id >= 1 && id <= 3)
8636 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008637 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 return;
8639 }
8640
Bram Moolenaar95e51472018-07-28 16:55:56 +02008641 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008642 conceal_char);
8643#endif
8644}
8645
8646/*
8647 * "matchaddpos()" function
8648 */
8649 static void
8650f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8651{
8652#ifdef FEAT_SEARCH_EXTRA
8653 char_u buf[NUMBUFLEN];
8654 char_u *group;
8655 int prio = 10;
8656 int id = -1;
8657 int error = FALSE;
8658 list_T *l;
8659 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008660 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008661
8662 rettv->vval.v_number = -1;
8663
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008664 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008665 if (group == NULL)
8666 return;
8667
8668 if (argvars[1].v_type != VAR_LIST)
8669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008670 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671 return;
8672 }
8673 l = argvars[1].vval.v_list;
8674 if (l == NULL)
8675 return;
8676
8677 if (argvars[2].v_type != VAR_UNKNOWN)
8678 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008679 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680 if (argvars[3].v_type != VAR_UNKNOWN)
8681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008682 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008683
8684 if (argvars[4].v_type != VAR_UNKNOWN
8685 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8686 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 }
8688 }
8689 if (error == TRUE)
8690 return;
8691
8692 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8693 if (id == 1 || id == 2)
8694 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008695 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008696 return;
8697 }
8698
Bram Moolenaar95e51472018-07-28 16:55:56 +02008699 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008700 conceal_char);
8701#endif
8702}
8703
8704/*
8705 * "matcharg()" function
8706 */
8707 static void
8708f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8709{
8710 if (rettv_list_alloc(rettv) == OK)
8711 {
8712#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008713 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714 matchitem_T *m;
8715
8716 if (id >= 1 && id <= 3)
8717 {
8718 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8719 {
8720 list_append_string(rettv->vval.v_list,
8721 syn_id2name(m->hlg_id), -1);
8722 list_append_string(rettv->vval.v_list, m->pattern, -1);
8723 }
8724 else
8725 {
8726 list_append_string(rettv->vval.v_list, NULL, -1);
8727 list_append_string(rettv->vval.v_list, NULL, -1);
8728 }
8729 }
8730#endif
8731 }
8732}
8733
8734/*
8735 * "matchdelete()" function
8736 */
8737 static void
8738f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8739{
8740#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008741 win_T *win = get_optional_window(argvars, 1);
8742
8743 if (win == NULL)
8744 rettv->vval.v_number = -1;
8745 else
8746 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008747 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008748#endif
8749}
8750
8751/*
8752 * "matchend()" function
8753 */
8754 static void
8755f_matchend(typval_T *argvars, typval_T *rettv)
8756{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008757 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758}
8759
8760/*
8761 * "matchlist()" function
8762 */
8763 static void
8764f_matchlist(typval_T *argvars, typval_T *rettv)
8765{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008766 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008767}
8768
8769/*
8770 * "matchstr()" function
8771 */
8772 static void
8773f_matchstr(typval_T *argvars, typval_T *rettv)
8774{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008775 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008776}
8777
8778/*
8779 * "matchstrpos()" function
8780 */
8781 static void
8782f_matchstrpos(typval_T *argvars, typval_T *rettv)
8783{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008784 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785}
8786
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008787 static void
8788max_min(typval_T *argvars, typval_T *rettv, int domax)
8789{
8790 varnumber_T n = 0;
8791 varnumber_T i;
8792 int error = FALSE;
8793
8794 if (argvars[0].v_type == VAR_LIST)
8795 {
8796 list_T *l;
8797 listitem_T *li;
8798
8799 l = argvars[0].vval.v_list;
8800 if (l != NULL)
8801 {
8802 li = l->lv_first;
8803 if (li != NULL)
8804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008805 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 for (;;)
8807 {
8808 li = li->li_next;
8809 if (li == NULL)
8810 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008811 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008812 if (domax ? i > n : i < n)
8813 n = i;
8814 }
8815 }
8816 }
8817 }
8818 else if (argvars[0].v_type == VAR_DICT)
8819 {
8820 dict_T *d;
8821 int first = TRUE;
8822 hashitem_T *hi;
8823 int todo;
8824
8825 d = argvars[0].vval.v_dict;
8826 if (d != NULL)
8827 {
8828 todo = (int)d->dv_hashtab.ht_used;
8829 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8830 {
8831 if (!HASHITEM_EMPTY(hi))
8832 {
8833 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008834 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008835 if (first)
8836 {
8837 n = i;
8838 first = FALSE;
8839 }
8840 else if (domax ? i > n : i < n)
8841 n = i;
8842 }
8843 }
8844 }
8845 }
8846 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008847 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 rettv->vval.v_number = error ? 0 : n;
8849}
8850
8851/*
8852 * "max()" function
8853 */
8854 static void
8855f_max(typval_T *argvars, typval_T *rettv)
8856{
8857 max_min(argvars, rettv, TRUE);
8858}
8859
8860/*
8861 * "min()" function
8862 */
8863 static void
8864f_min(typval_T *argvars, typval_T *rettv)
8865{
8866 max_min(argvars, rettv, FALSE);
8867}
8868
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008869/*
8870 * Create the directory in which "dir" is located, and higher levels when
8871 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008872 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 */
8874 static int
8875mkdir_recurse(char_u *dir, int prot)
8876{
8877 char_u *p;
8878 char_u *updir;
8879 int r = FAIL;
8880
8881 /* Get end of directory name in "dir".
8882 * We're done when it's "/" or "c:/". */
8883 p = gettail_sep(dir);
8884 if (p <= get_past_head(dir))
8885 return OK;
8886
8887 /* If the directory exists we're done. Otherwise: create it.*/
8888 updir = vim_strnsave(dir, (int)(p - dir));
8889 if (updir == NULL)
8890 return FAIL;
8891 if (mch_isdir(updir))
8892 r = OK;
8893 else if (mkdir_recurse(updir, prot) == OK)
8894 r = vim_mkdir_emsg(updir, prot);
8895 vim_free(updir);
8896 return r;
8897}
8898
8899#ifdef vim_mkdir
8900/*
8901 * "mkdir()" function
8902 */
8903 static void
8904f_mkdir(typval_T *argvars, typval_T *rettv)
8905{
8906 char_u *dir;
8907 char_u buf[NUMBUFLEN];
8908 int prot = 0755;
8909
8910 rettv->vval.v_number = FAIL;
8911 if (check_restricted() || check_secure())
8912 return;
8913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008914 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008915 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008916 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008917
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008918 if (*gettail(dir) == NUL)
8919 /* remove trailing slashes */
8920 *gettail_sep(dir) = NUL;
8921
8922 if (argvars[1].v_type != VAR_UNKNOWN)
8923 {
8924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008926 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008927 if (prot == -1)
8928 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008929 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008930 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008931 {
8932 if (mch_isdir(dir))
8933 {
8934 /* With the "p" flag it's OK if the dir already exists. */
8935 rettv->vval.v_number = OK;
8936 return;
8937 }
8938 mkdir_recurse(dir, prot);
8939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008941 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008942}
8943#endif
8944
8945/*
8946 * "mode()" function
8947 */
8948 static void
8949f_mode(typval_T *argvars, typval_T *rettv)
8950{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008951 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952
Bram Moolenaar612cc382018-07-29 15:34:26 +02008953 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008954
8955 if (time_for_testing == 93784)
8956 {
8957 /* Testing the two-character code. */
8958 buf[0] = 'x';
8959 buf[1] = '!';
8960 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008961#ifdef FEAT_TERMINAL
8962 else if (term_use_loop())
8963 buf[0] = 't';
8964#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008965 else if (VIsual_active)
8966 {
8967 if (VIsual_select)
8968 buf[0] = VIsual_mode + 's' - 'v';
8969 else
8970 buf[0] = VIsual_mode;
8971 }
8972 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8973 || State == CONFIRM)
8974 {
8975 buf[0] = 'r';
8976 if (State == ASKMORE)
8977 buf[1] = 'm';
8978 else if (State == CONFIRM)
8979 buf[1] = '?';
8980 }
8981 else if (State == EXTERNCMD)
8982 buf[0] = '!';
8983 else if (State & INSERT)
8984 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 if (State & VREPLACE_FLAG)
8986 {
8987 buf[0] = 'R';
8988 buf[1] = 'v';
8989 }
8990 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008991 {
8992 if (State & REPLACE_FLAG)
8993 buf[0] = 'R';
8994 else
8995 buf[0] = 'i';
8996#ifdef FEAT_INS_EXPAND
8997 if (ins_compl_active())
8998 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008999 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009000 buf[1] = 'x';
9001#endif
9002 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009003 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009004 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009005 {
9006 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009007 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009008 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009009 else if (exmode_active == EXMODE_NORMAL)
9010 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009011 }
9012 else
9013 {
9014 buf[0] = 'n';
9015 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009016 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009017 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009018 // to be able to detect force-linewise/blockwise/characterwise operations
9019 buf[2] = motion_force;
9020 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009021 else if (restart_edit == 'I' || restart_edit == 'R'
9022 || restart_edit == 'V')
9023 {
9024 buf[1] = 'i';
9025 buf[2] = restart_edit;
9026 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 }
9028
9029 /* Clear out the minor mode when the argument is not a non-zero number or
9030 * non-empty string. */
9031 if (!non_zero_arg(&argvars[0]))
9032 buf[1] = NUL;
9033
9034 rettv->vval.v_string = vim_strsave(buf);
9035 rettv->v_type = VAR_STRING;
9036}
9037
9038#if defined(FEAT_MZSCHEME) || defined(PROTO)
9039/*
9040 * "mzeval()" function
9041 */
9042 static void
9043f_mzeval(typval_T *argvars, typval_T *rettv)
9044{
9045 char_u *str;
9046 char_u buf[NUMBUFLEN];
9047
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009048 if (check_restricted() || check_secure())
9049 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009050 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009051 do_mzeval(str, rettv);
9052}
9053
9054 void
9055mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9056{
9057 typval_T argvars[3];
9058
9059 argvars[0].v_type = VAR_STRING;
9060 argvars[0].vval.v_string = name;
9061 copy_tv(args, &argvars[1]);
9062 argvars[2].v_type = VAR_UNKNOWN;
9063 f_call(argvars, rettv);
9064 clear_tv(&argvars[1]);
9065}
9066#endif
9067
9068/*
9069 * "nextnonblank()" function
9070 */
9071 static void
9072f_nextnonblank(typval_T *argvars, typval_T *rettv)
9073{
9074 linenr_T lnum;
9075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009076 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009077 {
9078 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9079 {
9080 lnum = 0;
9081 break;
9082 }
9083 if (*skipwhite(ml_get(lnum)) != NUL)
9084 break;
9085 }
9086 rettv->vval.v_number = lnum;
9087}
9088
9089/*
9090 * "nr2char()" function
9091 */
9092 static void
9093f_nr2char(typval_T *argvars, typval_T *rettv)
9094{
9095 char_u buf[NUMBUFLEN];
9096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009097 if (has_mbyte)
9098 {
9099 int utf8 = 0;
9100
9101 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009102 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009103 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009104 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009105 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009106 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 }
9108 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009110 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009111 buf[1] = NUL;
9112 }
9113 rettv->v_type = VAR_STRING;
9114 rettv->vval.v_string = vim_strsave(buf);
9115}
9116
9117/*
9118 * "or(expr, expr)" function
9119 */
9120 static void
9121f_or(typval_T *argvars, typval_T *rettv)
9122{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009123 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9124 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009125}
9126
9127/*
9128 * "pathshorten()" function
9129 */
9130 static void
9131f_pathshorten(typval_T *argvars, typval_T *rettv)
9132{
9133 char_u *p;
9134
9135 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009136 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009137 if (p == NULL)
9138 rettv->vval.v_string = NULL;
9139 else
9140 {
9141 p = vim_strsave(p);
9142 rettv->vval.v_string = p;
9143 if (p != NULL)
9144 shorten_dir(p);
9145 }
9146}
9147
9148#ifdef FEAT_PERL
9149/*
9150 * "perleval()" function
9151 */
9152 static void
9153f_perleval(typval_T *argvars, typval_T *rettv)
9154{
9155 char_u *str;
9156 char_u buf[NUMBUFLEN];
9157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009158 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009159 do_perleval(str, rettv);
9160}
9161#endif
9162
9163#ifdef FEAT_FLOAT
9164/*
9165 * "pow()" function
9166 */
9167 static void
9168f_pow(typval_T *argvars, typval_T *rettv)
9169{
9170 float_T fx = 0.0, fy = 0.0;
9171
9172 rettv->v_type = VAR_FLOAT;
9173 if (get_float_arg(argvars, &fx) == OK
9174 && get_float_arg(&argvars[1], &fy) == OK)
9175 rettv->vval.v_float = pow(fx, fy);
9176 else
9177 rettv->vval.v_float = 0.0;
9178}
9179#endif
9180
9181/*
9182 * "prevnonblank()" function
9183 */
9184 static void
9185f_prevnonblank(typval_T *argvars, typval_T *rettv)
9186{
9187 linenr_T lnum;
9188
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009189 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9191 lnum = 0;
9192 else
9193 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9194 --lnum;
9195 rettv->vval.v_number = lnum;
9196}
9197
9198/* This dummy va_list is here because:
9199 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9200 * - locally in the function results in a "used before set" warning
9201 * - using va_start() to initialize it gives "function with fixed args" error */
9202static va_list ap;
9203
9204/*
9205 * "printf()" function
9206 */
9207 static void
9208f_printf(typval_T *argvars, typval_T *rettv)
9209{
9210 char_u buf[NUMBUFLEN];
9211 int len;
9212 char_u *s;
9213 int saved_did_emsg = did_emsg;
9214 char *fmt;
9215
9216 rettv->v_type = VAR_STRING;
9217 rettv->vval.v_string = NULL;
9218
9219 /* Get the required length, allocate the buffer and do it for real. */
9220 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009221 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009222 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009223 if (!did_emsg)
9224 {
9225 s = alloc(len + 1);
9226 if (s != NULL)
9227 {
9228 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009229 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9230 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009231 }
9232 }
9233 did_emsg |= saved_did_emsg;
9234}
9235
Bram Moolenaarf2732452018-06-03 14:47:35 +02009236#ifdef FEAT_JOB_CHANNEL
9237/*
9238 * "prompt_setcallback({buffer}, {callback})" function
9239 */
9240 static void
9241f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9242{
9243 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009244 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009245
9246 if (check_secure())
9247 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009248 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009249 if (buf == NULL)
9250 return;
9251
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009252 callback = get_callback(&argvars[1]);
9253 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009254 return;
9255
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009256 free_callback(&buf->b_prompt_callback);
9257 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009258}
9259
9260/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009261 * "prompt_setinterrupt({buffer}, {callback})" function
9262 */
9263 static void
9264f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9265{
9266 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009267 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009268
9269 if (check_secure())
9270 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009271 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009272 if (buf == NULL)
9273 return;
9274
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009275 callback = get_callback(&argvars[1]);
9276 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009277 return;
9278
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009279 free_callback(&buf->b_prompt_interrupt);
9280 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009281}
9282
9283/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009284 * "prompt_setprompt({buffer}, {text})" function
9285 */
9286 static void
9287f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9288{
9289 buf_T *buf;
9290 char_u *text;
9291
9292 if (check_secure())
9293 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009294 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009295 if (buf == NULL)
9296 return;
9297
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009298 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009299 vim_free(buf->b_prompt_text);
9300 buf->b_prompt_text = vim_strsave(text);
9301}
9302#endif
9303
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009304/*
9305 * "pumvisible()" function
9306 */
9307 static void
9308f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9309{
9310#ifdef FEAT_INS_EXPAND
9311 if (pum_visible())
9312 rettv->vval.v_number = 1;
9313#endif
9314}
9315
9316#ifdef FEAT_PYTHON3
9317/*
9318 * "py3eval()" function
9319 */
9320 static void
9321f_py3eval(typval_T *argvars, typval_T *rettv)
9322{
9323 char_u *str;
9324 char_u buf[NUMBUFLEN];
9325
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009326 if (check_restricted() || check_secure())
9327 return;
9328
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009329 if (p_pyx == 0)
9330 p_pyx = 3;
9331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009332 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 do_py3eval(str, rettv);
9334}
9335#endif
9336
9337#ifdef FEAT_PYTHON
9338/*
9339 * "pyeval()" function
9340 */
9341 static void
9342f_pyeval(typval_T *argvars, typval_T *rettv)
9343{
9344 char_u *str;
9345 char_u buf[NUMBUFLEN];
9346
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009347 if (check_restricted() || check_secure())
9348 return;
9349
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009350 if (p_pyx == 0)
9351 p_pyx = 2;
9352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009353 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 do_pyeval(str, rettv);
9355}
9356#endif
9357
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009358#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9359/*
9360 * "pyxeval()" function
9361 */
9362 static void
9363f_pyxeval(typval_T *argvars, typval_T *rettv)
9364{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009365 if (check_restricted() || check_secure())
9366 return;
9367
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009368# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9369 init_pyxversion();
9370 if (p_pyx == 2)
9371 f_pyeval(argvars, rettv);
9372 else
9373 f_py3eval(argvars, rettv);
9374# elif defined(FEAT_PYTHON)
9375 f_pyeval(argvars, rettv);
9376# elif defined(FEAT_PYTHON3)
9377 f_py3eval(argvars, rettv);
9378# endif
9379}
9380#endif
9381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009382/*
9383 * "range()" function
9384 */
9385 static void
9386f_range(typval_T *argvars, typval_T *rettv)
9387{
9388 varnumber_T start;
9389 varnumber_T end;
9390 varnumber_T stride = 1;
9391 varnumber_T i;
9392 int error = FALSE;
9393
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009394 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009395 if (argvars[1].v_type == VAR_UNKNOWN)
9396 {
9397 end = start - 1;
9398 start = 0;
9399 }
9400 else
9401 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009402 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009403 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009404 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405 }
9406
9407 if (error)
9408 return; /* type error; errmsg already given */
9409 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009410 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009411 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009412 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 else
9414 {
9415 if (rettv_list_alloc(rettv) == OK)
9416 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9417 if (list_append_number(rettv->vval.v_list,
9418 (varnumber_T)i) == FAIL)
9419 break;
9420 }
9421}
9422
9423/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009424 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009425 */
9426 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009427readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009428{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009429 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009430 typval_T save_val;
9431 typval_T rettv;
9432 typval_T argv[2];
9433 int retval = 0;
9434 int error = FALSE;
9435
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009436 if (expr->v_type == VAR_UNKNOWN)
9437 return 1;
9438
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009439 prepare_vimvar(VV_VAL, &save_val);
9440 set_vim_var_string(VV_VAL, name, -1);
9441 argv[0].v_type = VAR_STRING;
9442 argv[0].vval.v_string = name;
9443
9444 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9445 goto theend;
9446
9447 retval = tv_get_number_chk(&rettv, &error);
9448 if (error)
9449 retval = -1;
9450 clear_tv(&rettv);
9451
9452theend:
9453 set_vim_var_string(VV_VAL, NULL, 0);
9454 restore_vimvar(VV_VAL, &save_val);
9455 return retval;
9456}
9457
9458/*
9459 * "readdir()" function
9460 */
9461 static void
9462f_readdir(typval_T *argvars, typval_T *rettv)
9463{
9464 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009465 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009466 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009467 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009468 garray_T ga;
9469 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009470
9471 if (rettv_list_alloc(rettv) == FAIL)
9472 return;
9473 path = tv_get_string(&argvars[0]);
9474 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009475
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009476 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9477 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009478 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009479 for (i = 0; i < ga.ga_len; i++)
9480 {
9481 p = ((char_u **)ga.ga_data)[i];
9482 list_append_string(rettv->vval.v_list, p, -1);
9483 }
9484 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009485 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009486}
9487
9488/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489 * "readfile()" function
9490 */
9491 static void
9492f_readfile(typval_T *argvars, typval_T *rettv)
9493{
9494 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009495 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 int failed = FALSE;
9497 char_u *fname;
9498 FILE *fd;
9499 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9500 int io_size = sizeof(buf);
9501 int readlen; /* size of last fread() */
9502 char_u *prev = NULL; /* previously read bytes, if any */
9503 long prevlen = 0; /* length of data in prev */
9504 long prevsize = 0; /* size of prev buffer */
9505 long maxline = MAXLNUM;
9506 long cnt = 0;
9507 char_u *p; /* position in buf */
9508 char_u *start; /* start of current line */
9509
9510 if (argvars[1].v_type != VAR_UNKNOWN)
9511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009512 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009513 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009514 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9515 blob = TRUE;
9516
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009517 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009518 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009519 }
9520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009521 if (blob)
9522 {
9523 if (rettv_blob_alloc(rettv) == FAIL)
9524 return;
9525 }
9526 else
9527 {
9528 if (rettv_list_alloc(rettv) == FAIL)
9529 return;
9530 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009531
9532 /* Always open the file in binary mode, library functions have a mind of
9533 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009534 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009535 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009537 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538 return;
9539 }
9540
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009541 if (blob)
9542 {
9543 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9544 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009545 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009546 blob_free(rettv->vval.v_blob);
9547 }
9548 fclose(fd);
9549 return;
9550 }
9551
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552 while (cnt < maxline || maxline < 0)
9553 {
9554 readlen = (int)fread(buf, 1, io_size, fd);
9555
9556 /* This for loop processes what was read, but is also entered at end
9557 * of file so that either:
9558 * - an incomplete line gets written
9559 * - a "binary" file gets an empty line at the end if it ends in a
9560 * newline. */
9561 for (p = buf, start = buf;
9562 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9563 ++p)
9564 {
9565 if (*p == '\n' || readlen <= 0)
9566 {
9567 listitem_T *li;
9568 char_u *s = NULL;
9569 long_u len = p - start;
9570
9571 /* Finished a line. Remove CRs before NL. */
9572 if (readlen > 0 && !binary)
9573 {
9574 while (len > 0 && start[len - 1] == '\r')
9575 --len;
9576 /* removal may cross back to the "prev" string */
9577 if (len == 0)
9578 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9579 --prevlen;
9580 }
9581 if (prevlen == 0)
9582 s = vim_strnsave(start, (int)len);
9583 else
9584 {
9585 /* Change "prev" buffer to be the right size. This way
9586 * the bytes are only copied once, and very long lines are
9587 * allocated only once. */
9588 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9589 {
9590 mch_memmove(s + prevlen, start, len);
9591 s[prevlen + len] = NUL;
9592 prev = NULL; /* the list will own the string */
9593 prevlen = prevsize = 0;
9594 }
9595 }
9596 if (s == NULL)
9597 {
9598 do_outofmem_msg((long_u) prevlen + len + 1);
9599 failed = TRUE;
9600 break;
9601 }
9602
9603 if ((li = listitem_alloc()) == NULL)
9604 {
9605 vim_free(s);
9606 failed = TRUE;
9607 break;
9608 }
9609 li->li_tv.v_type = VAR_STRING;
9610 li->li_tv.v_lock = 0;
9611 li->li_tv.vval.v_string = s;
9612 list_append(rettv->vval.v_list, li);
9613
9614 start = p + 1; /* step over newline */
9615 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9616 break;
9617 }
9618 else if (*p == NUL)
9619 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009620 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9621 * when finding the BF and check the previous two bytes. */
9622 else if (*p == 0xbf && enc_utf8 && !binary)
9623 {
9624 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9625 * + 1, these may be in the "prev" string. */
9626 char_u back1 = p >= buf + 1 ? p[-1]
9627 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9628 char_u back2 = p >= buf + 2 ? p[-2]
9629 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9630 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9631
9632 if (back2 == 0xef && back1 == 0xbb)
9633 {
9634 char_u *dest = p - 2;
9635
9636 /* Usually a BOM is at the beginning of a file, and so at
9637 * the beginning of a line; then we can just step over it.
9638 */
9639 if (start == dest)
9640 start = p + 1;
9641 else
9642 {
9643 /* have to shuffle buf to close gap */
9644 int adjust_prevlen = 0;
9645
9646 if (dest < buf)
9647 {
9648 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9649 dest = buf;
9650 }
9651 if (readlen > p - buf + 1)
9652 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9653 readlen -= 3 - adjust_prevlen;
9654 prevlen -= adjust_prevlen;
9655 p = dest - 1;
9656 }
9657 }
9658 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009659 } /* for */
9660
9661 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9662 break;
9663 if (start < p)
9664 {
9665 /* There's part of a line in buf, store it in "prev". */
9666 if (p - start + prevlen >= prevsize)
9667 {
9668 /* need bigger "prev" buffer */
9669 char_u *newprev;
9670
9671 /* A common use case is ordinary text files and "prev" gets a
9672 * fragment of a line, so the first allocation is made
9673 * small, to avoid repeatedly 'allocing' large and
9674 * 'reallocing' small. */
9675 if (prevsize == 0)
9676 prevsize = (long)(p - start);
9677 else
9678 {
9679 long grow50pc = (prevsize * 3) / 2;
9680 long growmin = (long)((p - start) * 2 + prevlen);
9681 prevsize = grow50pc > growmin ? grow50pc : growmin;
9682 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009683 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684 if (newprev == NULL)
9685 {
9686 do_outofmem_msg((long_u)prevsize);
9687 failed = TRUE;
9688 break;
9689 }
9690 prev = newprev;
9691 }
9692 /* Add the line part to end of "prev". */
9693 mch_memmove(prev + prevlen, start, p - start);
9694 prevlen += (long)(p - start);
9695 }
9696 } /* while */
9697
9698 /*
9699 * For a negative line count use only the lines at the end of the file,
9700 * free the rest.
9701 */
9702 if (!failed && maxline < 0)
9703 while (cnt > -maxline)
9704 {
9705 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9706 --cnt;
9707 }
9708
9709 if (failed)
9710 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009711 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009712 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009713 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009714 }
9715
9716 vim_free(prev);
9717 fclose(fd);
9718}
9719
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009720 static void
9721return_register(int regname, typval_T *rettv)
9722{
9723 char_u buf[2] = {0, 0};
9724
9725 buf[0] = (char_u)regname;
9726 rettv->v_type = VAR_STRING;
9727 rettv->vval.v_string = vim_strsave(buf);
9728}
9729
9730/*
9731 * "reg_executing()" function
9732 */
9733 static void
9734f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9735{
9736 return_register(reg_executing, rettv);
9737}
9738
9739/*
9740 * "reg_recording()" function
9741 */
9742 static void
9743f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9744{
9745 return_register(reg_recording, rettv);
9746}
9747
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009748#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009749/*
9750 * Convert a List to proftime_T.
9751 * Return FAIL when there is something wrong.
9752 */
9753 static int
9754list2proftime(typval_T *arg, proftime_T *tm)
9755{
9756 long n1, n2;
9757 int error = FALSE;
9758
9759 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9760 || arg->vval.v_list->lv_len != 2)
9761 return FAIL;
9762 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9763 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009764# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009765 tm->HighPart = n1;
9766 tm->LowPart = n2;
9767# else
9768 tm->tv_sec = n1;
9769 tm->tv_usec = n2;
9770# endif
9771 return error ? FAIL : OK;
9772}
9773#endif /* FEAT_RELTIME */
9774
9775/*
9776 * "reltime()" function
9777 */
9778 static void
9779f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9780{
9781#ifdef FEAT_RELTIME
9782 proftime_T res;
9783 proftime_T start;
9784
9785 if (argvars[0].v_type == VAR_UNKNOWN)
9786 {
9787 /* No arguments: get current time. */
9788 profile_start(&res);
9789 }
9790 else if (argvars[1].v_type == VAR_UNKNOWN)
9791 {
9792 if (list2proftime(&argvars[0], &res) == FAIL)
9793 return;
9794 profile_end(&res);
9795 }
9796 else
9797 {
9798 /* Two arguments: compute the difference. */
9799 if (list2proftime(&argvars[0], &start) == FAIL
9800 || list2proftime(&argvars[1], &res) == FAIL)
9801 return;
9802 profile_sub(&res, &start);
9803 }
9804
9805 if (rettv_list_alloc(rettv) == OK)
9806 {
9807 long n1, n2;
9808
Bram Moolenaar4f974752019-02-17 17:44:42 +01009809# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009810 n1 = res.HighPart;
9811 n2 = res.LowPart;
9812# else
9813 n1 = res.tv_sec;
9814 n2 = res.tv_usec;
9815# endif
9816 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9817 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9818 }
9819#endif
9820}
9821
9822#ifdef FEAT_FLOAT
9823/*
9824 * "reltimefloat()" function
9825 */
9826 static void
9827f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9828{
9829# ifdef FEAT_RELTIME
9830 proftime_T tm;
9831# endif
9832
9833 rettv->v_type = VAR_FLOAT;
9834 rettv->vval.v_float = 0;
9835# ifdef FEAT_RELTIME
9836 if (list2proftime(&argvars[0], &tm) == OK)
9837 rettv->vval.v_float = profile_float(&tm);
9838# endif
9839}
9840#endif
9841
9842/*
9843 * "reltimestr()" function
9844 */
9845 static void
9846f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9847{
9848#ifdef FEAT_RELTIME
9849 proftime_T tm;
9850#endif
9851
9852 rettv->v_type = VAR_STRING;
9853 rettv->vval.v_string = NULL;
9854#ifdef FEAT_RELTIME
9855 if (list2proftime(&argvars[0], &tm) == OK)
9856 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9857#endif
9858}
9859
9860#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009861 static void
9862make_connection(void)
9863{
9864 if (X_DISPLAY == NULL
9865# ifdef FEAT_GUI
9866 && !gui.in_use
9867# endif
9868 )
9869 {
9870 x_force_connect = TRUE;
9871 setup_term_clip();
9872 x_force_connect = FALSE;
9873 }
9874}
9875
9876 static int
9877check_connection(void)
9878{
9879 make_connection();
9880 if (X_DISPLAY == NULL)
9881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009882 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883 return FAIL;
9884 }
9885 return OK;
9886}
9887#endif
9888
9889#ifdef FEAT_CLIENTSERVER
9890 static void
9891remote_common(typval_T *argvars, typval_T *rettv, int expr)
9892{
9893 char_u *server_name;
9894 char_u *keys;
9895 char_u *r = NULL;
9896 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009897 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009898# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009899 HWND w;
9900# else
9901 Window w;
9902# endif
9903
9904 if (check_restricted() || check_secure())
9905 return;
9906
9907# ifdef FEAT_X11
9908 if (check_connection() == FAIL)
9909 return;
9910# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009911 if (argvars[2].v_type != VAR_UNKNOWN
9912 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009913 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009914
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009915 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916 if (server_name == NULL)
9917 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009918 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009919# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009920 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009922 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9923 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924# endif
9925 {
9926 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009928 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009929 vim_free(r);
9930 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009932 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009933 return;
9934 }
9935
9936 rettv->vval.v_string = r;
9937
9938 if (argvars[2].v_type != VAR_UNKNOWN)
9939 {
9940 dictitem_T v;
9941 char_u str[30];
9942 char_u *idvar;
9943
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009944 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009945 if (idvar != NULL && *idvar != NUL)
9946 {
9947 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9948 v.di_tv.v_type = VAR_STRING;
9949 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009951 vim_free(v.di_tv.vval.v_string);
9952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009953 }
9954}
9955#endif
9956
9957/*
9958 * "remote_expr()" function
9959 */
9960 static void
9961f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9962{
9963 rettv->v_type = VAR_STRING;
9964 rettv->vval.v_string = NULL;
9965#ifdef FEAT_CLIENTSERVER
9966 remote_common(argvars, rettv, TRUE);
9967#endif
9968}
9969
9970/*
9971 * "remote_foreground()" function
9972 */
9973 static void
9974f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9975{
9976#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009977# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009978 /* On Win32 it's done in this application. */
9979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009980 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009981
9982 if (server_name != NULL)
9983 serverForeground(server_name);
9984 }
9985# else
9986 /* Send a foreground() expression to the server. */
9987 argvars[1].v_type = VAR_STRING;
9988 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9989 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009990 rettv->v_type = VAR_STRING;
9991 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009992 remote_common(argvars, rettv, TRUE);
9993 vim_free(argvars[1].vval.v_string);
9994# endif
9995#endif
9996}
9997
9998 static void
9999f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10000{
10001#ifdef FEAT_CLIENTSERVER
10002 dictitem_T v;
10003 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010004# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 long_u n = 0;
10006# endif
10007 char_u *serverid;
10008
10009 if (check_restricted() || check_secure())
10010 {
10011 rettv->vval.v_number = -1;
10012 return;
10013 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010014 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015 if (serverid == NULL)
10016 {
10017 rettv->vval.v_number = -1;
10018 return; /* type error; errmsg already given */
10019 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010020# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010021 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10022 if (n == 0)
10023 rettv->vval.v_number = -1;
10024 else
10025 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010026 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010027 rettv->vval.v_number = (s != NULL);
10028 }
10029# else
10030 if (check_connection() == FAIL)
10031 return;
10032
10033 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10034 serverStrToWin(serverid), &s);
10035# endif
10036
10037 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10038 {
10039 char_u *retvar;
10040
10041 v.di_tv.v_type = VAR_STRING;
10042 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010043 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010044 if (retvar != NULL)
10045 set_var(retvar, &v.di_tv, FALSE);
10046 vim_free(v.di_tv.vval.v_string);
10047 }
10048#else
10049 rettv->vval.v_number = -1;
10050#endif
10051}
10052
10053 static void
10054f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10055{
10056 char_u *r = NULL;
10057
10058#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010059 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010060
10061 if (serverid != NULL && !check_restricted() && !check_secure())
10062 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010063 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010064# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010065 /* The server's HWND is encoded in the 'id' parameter */
10066 long_u n = 0;
10067# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010068
10069 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010070 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010071
Bram Moolenaar4f974752019-02-17 17:44:42 +010010072# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010073 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10074 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010075 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010076 if (r == NULL)
10077# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010078 if (check_connection() == FAIL
10079 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10080 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010081# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010082 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 }
10084#endif
10085 rettv->v_type = VAR_STRING;
10086 rettv->vval.v_string = r;
10087}
10088
10089/*
10090 * "remote_send()" function
10091 */
10092 static void
10093f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10094{
10095 rettv->v_type = VAR_STRING;
10096 rettv->vval.v_string = NULL;
10097#ifdef FEAT_CLIENTSERVER
10098 remote_common(argvars, rettv, FALSE);
10099#endif
10100}
10101
10102/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010103 * "remote_startserver()" function
10104 */
10105 static void
10106f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10107{
10108#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010109 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010110
10111 if (server == NULL)
10112 return; /* type error; errmsg already given */
10113 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010114 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010115 else
10116 {
10117# ifdef FEAT_X11
10118 if (check_connection() == OK)
10119 serverRegisterName(X_DISPLAY, server);
10120# else
10121 serverSetName(server);
10122# endif
10123 }
10124#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010125 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010126#endif
10127}
10128
10129/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010130 * "remove()" function
10131 */
10132 static void
10133f_remove(typval_T *argvars, typval_T *rettv)
10134{
10135 list_T *l;
10136 listitem_T *item, *item2;
10137 listitem_T *li;
10138 long idx;
10139 long end;
10140 char_u *key;
10141 dict_T *d;
10142 dictitem_T *di;
10143 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010144 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010145
10146 if (argvars[0].v_type == VAR_DICT)
10147 {
10148 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010149 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010150 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010151 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010153 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010154 if (key != NULL)
10155 {
10156 di = dict_find(d, key, -1);
10157 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010158 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10160 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10161 {
10162 *rettv = di->di_tv;
10163 init_tv(&di->di_tv);
10164 dictitem_remove(d, di);
10165 }
10166 }
10167 }
10168 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010169 else if (argvars[0].v_type == VAR_BLOB)
10170 {
10171 idx = (long)tv_get_number_chk(&argvars[1], &error);
10172 if (!error)
10173 {
10174 blob_T *b = argvars[0].vval.v_blob;
10175 int len = blob_len(b);
10176 char_u *p;
10177
10178 if (idx < 0)
10179 // count from the end
10180 idx = len + idx;
10181 if (idx < 0 || idx >= len)
10182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010183 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010184 return;
10185 }
10186 if (argvars[2].v_type == VAR_UNKNOWN)
10187 {
10188 // Remove one item, return its value.
10189 p = (char_u *)b->bv_ga.ga_data;
10190 rettv->vval.v_number = (varnumber_T) *(p + idx);
10191 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10192 --b->bv_ga.ga_len;
10193 }
10194 else
10195 {
10196 blob_T *blob;
10197
10198 // Remove range of items, return list with values.
10199 end = (long)tv_get_number_chk(&argvars[2], &error);
10200 if (error)
10201 return;
10202 if (end < 0)
10203 // count from the end
10204 end = len + end;
10205 if (end >= len || idx > end)
10206 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010207 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010208 return;
10209 }
10210 blob = blob_alloc();
10211 if (blob == NULL)
10212 return;
10213 blob->bv_ga.ga_len = end - idx + 1;
10214 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10215 {
10216 vim_free(blob);
10217 return;
10218 }
10219 p = (char_u *)b->bv_ga.ga_data;
10220 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10221 (size_t)(end - idx + 1));
10222 ++blob->bv_refcount;
10223 rettv->v_type = VAR_BLOB;
10224 rettv->vval.v_blob = blob;
10225
10226 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10227 b->bv_ga.ga_len -= end - idx + 1;
10228 }
10229 }
10230 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010232 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010233 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010234 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010235 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010236 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010238 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010239 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010240 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 else
10242 {
10243 if (argvars[2].v_type == VAR_UNKNOWN)
10244 {
10245 /* Remove one item, return its value. */
10246 vimlist_remove(l, item, item);
10247 *rettv = item->li_tv;
10248 vim_free(item);
10249 }
10250 else
10251 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010252 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010253 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010254 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010255 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010256 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010257 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 else
10259 {
10260 int cnt = 0;
10261
10262 for (li = item; li != NULL; li = li->li_next)
10263 {
10264 ++cnt;
10265 if (li == item2)
10266 break;
10267 }
10268 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010269 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010270 else
10271 {
10272 vimlist_remove(l, item, item2);
10273 if (rettv_list_alloc(rettv) == OK)
10274 {
10275 l = rettv->vval.v_list;
10276 l->lv_first = item;
10277 l->lv_last = item2;
10278 item->li_prev = NULL;
10279 item2->li_next = NULL;
10280 l->lv_len = cnt;
10281 }
10282 }
10283 }
10284 }
10285 }
10286 }
10287}
10288
10289/*
10290 * "rename({from}, {to})" function
10291 */
10292 static void
10293f_rename(typval_T *argvars, typval_T *rettv)
10294{
10295 char_u buf[NUMBUFLEN];
10296
10297 if (check_restricted() || check_secure())
10298 rettv->vval.v_number = -1;
10299 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010300 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10301 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010302}
10303
10304/*
10305 * "repeat()" function
10306 */
10307 static void
10308f_repeat(typval_T *argvars, typval_T *rettv)
10309{
10310 char_u *p;
10311 int n;
10312 int slen;
10313 int len;
10314 char_u *r;
10315 int i;
10316
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010317 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010318 if (argvars[0].v_type == VAR_LIST)
10319 {
10320 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10321 while (n-- > 0)
10322 if (list_extend(rettv->vval.v_list,
10323 argvars[0].vval.v_list, NULL) == FAIL)
10324 break;
10325 }
10326 else
10327 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010328 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010329 rettv->v_type = VAR_STRING;
10330 rettv->vval.v_string = NULL;
10331
10332 slen = (int)STRLEN(p);
10333 len = slen * n;
10334 if (len <= 0)
10335 return;
10336
10337 r = alloc(len + 1);
10338 if (r != NULL)
10339 {
10340 for (i = 0; i < n; i++)
10341 mch_memmove(r + i * slen, p, (size_t)slen);
10342 r[len] = NUL;
10343 }
10344
10345 rettv->vval.v_string = r;
10346 }
10347}
10348
10349/*
10350 * "resolve()" function
10351 */
10352 static void
10353f_resolve(typval_T *argvars, typval_T *rettv)
10354{
10355 char_u *p;
10356#ifdef HAVE_READLINK
10357 char_u *buf = NULL;
10358#endif
10359
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010360 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010361#ifdef FEAT_SHORTCUT
10362 {
10363 char_u *v = NULL;
10364
Bram Moolenaardce1e892019-02-10 23:18:53 +010010365 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010366 if (v != NULL)
10367 rettv->vval.v_string = v;
10368 else
10369 rettv->vval.v_string = vim_strsave(p);
10370 }
10371#else
10372# ifdef HAVE_READLINK
10373 {
10374 char_u *cpy;
10375 int len;
10376 char_u *remain = NULL;
10377 char_u *q;
10378 int is_relative_to_current = FALSE;
10379 int has_trailing_pathsep = FALSE;
10380 int limit = 100;
10381
10382 p = vim_strsave(p);
10383
10384 if (p[0] == '.' && (vim_ispathsep(p[1])
10385 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10386 is_relative_to_current = TRUE;
10387
10388 len = STRLEN(p);
10389 if (len > 0 && after_pathsep(p, p + len))
10390 {
10391 has_trailing_pathsep = TRUE;
10392 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10393 }
10394
10395 q = getnextcomp(p);
10396 if (*q != NUL)
10397 {
10398 /* Separate the first path component in "p", and keep the
10399 * remainder (beginning with the path separator). */
10400 remain = vim_strsave(q - 1);
10401 q[-1] = NUL;
10402 }
10403
10404 buf = alloc(MAXPATHL + 1);
10405 if (buf == NULL)
10406 goto fail;
10407
10408 for (;;)
10409 {
10410 for (;;)
10411 {
10412 len = readlink((char *)p, (char *)buf, MAXPATHL);
10413 if (len <= 0)
10414 break;
10415 buf[len] = NUL;
10416
10417 if (limit-- == 0)
10418 {
10419 vim_free(p);
10420 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010421 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010422 rettv->vval.v_string = NULL;
10423 goto fail;
10424 }
10425
10426 /* Ensure that the result will have a trailing path separator
10427 * if the argument has one. */
10428 if (remain == NULL && has_trailing_pathsep)
10429 add_pathsep(buf);
10430
10431 /* Separate the first path component in the link value and
10432 * concatenate the remainders. */
10433 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10434 if (*q != NUL)
10435 {
10436 if (remain == NULL)
10437 remain = vim_strsave(q - 1);
10438 else
10439 {
10440 cpy = concat_str(q - 1, remain);
10441 if (cpy != NULL)
10442 {
10443 vim_free(remain);
10444 remain = cpy;
10445 }
10446 }
10447 q[-1] = NUL;
10448 }
10449
10450 q = gettail(p);
10451 if (q > p && *q == NUL)
10452 {
10453 /* Ignore trailing path separator. */
10454 q[-1] = NUL;
10455 q = gettail(p);
10456 }
10457 if (q > p && !mch_isFullName(buf))
10458 {
10459 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010460 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010461 if (cpy != NULL)
10462 {
10463 STRCPY(cpy, p);
10464 STRCPY(gettail(cpy), buf);
10465 vim_free(p);
10466 p = cpy;
10467 }
10468 }
10469 else
10470 {
10471 vim_free(p);
10472 p = vim_strsave(buf);
10473 }
10474 }
10475
10476 if (remain == NULL)
10477 break;
10478
10479 /* Append the first path component of "remain" to "p". */
10480 q = getnextcomp(remain + 1);
10481 len = q - remain - (*q != NUL);
10482 cpy = vim_strnsave(p, STRLEN(p) + len);
10483 if (cpy != NULL)
10484 {
10485 STRNCAT(cpy, remain, len);
10486 vim_free(p);
10487 p = cpy;
10488 }
10489 /* Shorten "remain". */
10490 if (*q != NUL)
10491 STRMOVE(remain, q - 1);
10492 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010493 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010494 }
10495
10496 /* If the result is a relative path name, make it explicitly relative to
10497 * the current directory if and only if the argument had this form. */
10498 if (!vim_ispathsep(*p))
10499 {
10500 if (is_relative_to_current
10501 && *p != NUL
10502 && !(p[0] == '.'
10503 && (p[1] == NUL
10504 || vim_ispathsep(p[1])
10505 || (p[1] == '.'
10506 && (p[2] == NUL
10507 || vim_ispathsep(p[2]))))))
10508 {
10509 /* Prepend "./". */
10510 cpy = concat_str((char_u *)"./", p);
10511 if (cpy != NULL)
10512 {
10513 vim_free(p);
10514 p = cpy;
10515 }
10516 }
10517 else if (!is_relative_to_current)
10518 {
10519 /* Strip leading "./". */
10520 q = p;
10521 while (q[0] == '.' && vim_ispathsep(q[1]))
10522 q += 2;
10523 if (q > p)
10524 STRMOVE(p, p + 2);
10525 }
10526 }
10527
10528 /* Ensure that the result will have no trailing path separator
10529 * if the argument had none. But keep "/" or "//". */
10530 if (!has_trailing_pathsep)
10531 {
10532 q = p + STRLEN(p);
10533 if (after_pathsep(p, q))
10534 *gettail_sep(p) = NUL;
10535 }
10536
10537 rettv->vval.v_string = p;
10538 }
10539# else
10540 rettv->vval.v_string = vim_strsave(p);
10541# endif
10542#endif
10543
10544 simplify_filename(rettv->vval.v_string);
10545
10546#ifdef HAVE_READLINK
10547fail:
10548 vim_free(buf);
10549#endif
10550 rettv->v_type = VAR_STRING;
10551}
10552
10553/*
10554 * "reverse({list})" function
10555 */
10556 static void
10557f_reverse(typval_T *argvars, typval_T *rettv)
10558{
10559 list_T *l;
10560 listitem_T *li, *ni;
10561
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010562 if (argvars[0].v_type == VAR_BLOB)
10563 {
10564 blob_T *b = argvars[0].vval.v_blob;
10565 int i, len = blob_len(b);
10566
10567 for (i = 0; i < len / 2; i++)
10568 {
10569 int tmp = blob_get(b, i);
10570
10571 blob_set(b, i, blob_get(b, len - i - 1));
10572 blob_set(b, len - i - 1, tmp);
10573 }
10574 rettv_blob_set(rettv, b);
10575 return;
10576 }
10577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010578 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010579 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010580 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010581 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 (char_u *)N_("reverse() argument"), TRUE))
10583 {
10584 li = l->lv_last;
10585 l->lv_first = l->lv_last = NULL;
10586 l->lv_len = 0;
10587 while (li != NULL)
10588 {
10589 ni = li->li_prev;
10590 list_append(l, li);
10591 li = ni;
10592 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010593 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010594 l->lv_idx = l->lv_len - l->lv_idx - 1;
10595 }
10596}
10597
10598#define SP_NOMOVE 0x01 /* don't move cursor */
10599#define SP_REPEAT 0x02 /* repeat to find outer pair */
10600#define SP_RETCOUNT 0x04 /* return matchcount */
10601#define SP_SETPCMARK 0x08 /* set previous context mark */
10602#define SP_START 0x10 /* accept match at start position */
10603#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10604#define SP_END 0x40 /* leave cursor at end of match */
10605#define SP_COLUMN 0x80 /* start at cursor column */
10606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607/*
10608 * Get flags for a search function.
10609 * Possibly sets "p_ws".
10610 * Returns BACKWARD, FORWARD or zero (for an error).
10611 */
10612 static int
10613get_search_arg(typval_T *varp, int *flagsp)
10614{
10615 int dir = FORWARD;
10616 char_u *flags;
10617 char_u nbuf[NUMBUFLEN];
10618 int mask;
10619
10620 if (varp->v_type != VAR_UNKNOWN)
10621 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010622 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010623 if (flags == NULL)
10624 return 0; /* type error; errmsg already given */
10625 while (*flags != NUL)
10626 {
10627 switch (*flags)
10628 {
10629 case 'b': dir = BACKWARD; break;
10630 case 'w': p_ws = TRUE; break;
10631 case 'W': p_ws = FALSE; break;
10632 default: mask = 0;
10633 if (flagsp != NULL)
10634 switch (*flags)
10635 {
10636 case 'c': mask = SP_START; break;
10637 case 'e': mask = SP_END; break;
10638 case 'm': mask = SP_RETCOUNT; break;
10639 case 'n': mask = SP_NOMOVE; break;
10640 case 'p': mask = SP_SUBPAT; break;
10641 case 'r': mask = SP_REPEAT; break;
10642 case 's': mask = SP_SETPCMARK; break;
10643 case 'z': mask = SP_COLUMN; break;
10644 }
10645 if (mask == 0)
10646 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010647 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010648 dir = 0;
10649 }
10650 else
10651 *flagsp |= mask;
10652 }
10653 if (dir == 0)
10654 break;
10655 ++flags;
10656 }
10657 }
10658 return dir;
10659}
10660
10661/*
10662 * Shared by search() and searchpos() functions.
10663 */
10664 static int
10665search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10666{
10667 int flags;
10668 char_u *pat;
10669 pos_T pos;
10670 pos_T save_cursor;
10671 int save_p_ws = p_ws;
10672 int dir;
10673 int retval = 0; /* default: FAIL */
10674 long lnum_stop = 0;
10675 proftime_T tm;
10676#ifdef FEAT_RELTIME
10677 long time_limit = 0;
10678#endif
10679 int options = SEARCH_KEEP;
10680 int subpatnum;
10681
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010682 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010683 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10684 if (dir == 0)
10685 goto theend;
10686 flags = *flagsp;
10687 if (flags & SP_START)
10688 options |= SEARCH_START;
10689 if (flags & SP_END)
10690 options |= SEARCH_END;
10691 if (flags & SP_COLUMN)
10692 options |= SEARCH_COL;
10693
10694 /* Optional arguments: line number to stop searching and timeout. */
10695 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10696 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010697 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010698 if (lnum_stop < 0)
10699 goto theend;
10700#ifdef FEAT_RELTIME
10701 if (argvars[3].v_type != VAR_UNKNOWN)
10702 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010703 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010704 if (time_limit < 0)
10705 goto theend;
10706 }
10707#endif
10708 }
10709
10710#ifdef FEAT_RELTIME
10711 /* Set the time limit, if there is one. */
10712 profile_setlimit(time_limit, &tm);
10713#endif
10714
10715 /*
10716 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10717 * Check to make sure only those flags are set.
10718 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10719 * flags cannot be set. Check for that condition also.
10720 */
10721 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10722 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10723 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010724 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010725 goto theend;
10726 }
10727
10728 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010729 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010730 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010731 if (subpatnum != FAIL)
10732 {
10733 if (flags & SP_SUBPAT)
10734 retval = subpatnum;
10735 else
10736 retval = pos.lnum;
10737 if (flags & SP_SETPCMARK)
10738 setpcmark();
10739 curwin->w_cursor = pos;
10740 if (match_pos != NULL)
10741 {
10742 /* Store the match cursor position */
10743 match_pos->lnum = pos.lnum;
10744 match_pos->col = pos.col + 1;
10745 }
10746 /* "/$" will put the cursor after the end of the line, may need to
10747 * correct that here */
10748 check_cursor();
10749 }
10750
10751 /* If 'n' flag is used: restore cursor position. */
10752 if (flags & SP_NOMOVE)
10753 curwin->w_cursor = save_cursor;
10754 else
10755 curwin->w_set_curswant = TRUE;
10756theend:
10757 p_ws = save_p_ws;
10758
10759 return retval;
10760}
10761
10762#ifdef FEAT_FLOAT
10763
10764/*
10765 * round() is not in C90, use ceil() or floor() instead.
10766 */
10767 float_T
10768vim_round(float_T f)
10769{
10770 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10771}
10772
10773/*
10774 * "round({float})" function
10775 */
10776 static void
10777f_round(typval_T *argvars, typval_T *rettv)
10778{
10779 float_T f = 0.0;
10780
10781 rettv->v_type = VAR_FLOAT;
10782 if (get_float_arg(argvars, &f) == OK)
10783 rettv->vval.v_float = vim_round(f);
10784 else
10785 rettv->vval.v_float = 0.0;
10786}
10787#endif
10788
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010789#ifdef FEAT_RUBY
10790/*
10791 * "rubyeval()" function
10792 */
10793 static void
10794f_rubyeval(typval_T *argvars, typval_T *rettv)
10795{
10796 char_u *str;
10797 char_u buf[NUMBUFLEN];
10798
10799 str = tv_get_string_buf(&argvars[0], buf);
10800 do_rubyeval(str, rettv);
10801}
10802#endif
10803
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804/*
10805 * "screenattr()" function
10806 */
10807 static void
10808f_screenattr(typval_T *argvars, typval_T *rettv)
10809{
10810 int row;
10811 int col;
10812 int c;
10813
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010814 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10815 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010816 if (row < 0 || row >= screen_Rows
10817 || col < 0 || col >= screen_Columns)
10818 c = -1;
10819 else
10820 c = ScreenAttrs[LineOffset[row] + col];
10821 rettv->vval.v_number = c;
10822}
10823
10824/*
10825 * "screenchar()" function
10826 */
10827 static void
10828f_screenchar(typval_T *argvars, typval_T *rettv)
10829{
10830 int row;
10831 int col;
10832 int off;
10833 int c;
10834
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010835 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10836 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010837 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010838 c = -1;
10839 else
10840 {
10841 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010842 if (enc_utf8 && ScreenLinesUC[off] != 0)
10843 c = ScreenLinesUC[off];
10844 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 c = ScreenLines[off];
10846 }
10847 rettv->vval.v_number = c;
10848}
10849
10850/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010851 * "screenchars()" function
10852 */
10853 static void
10854f_screenchars(typval_T *argvars, typval_T *rettv)
10855{
10856 int row;
10857 int col;
10858 int off;
10859 int c;
10860 int i;
10861
10862 if (rettv_list_alloc(rettv) == FAIL)
10863 return;
10864 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10865 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10866 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10867 return;
10868
10869 off = LineOffset[row] + col;
10870 if (enc_utf8 && ScreenLinesUC[off] != 0)
10871 c = ScreenLinesUC[off];
10872 else
10873 c = ScreenLines[off];
10874 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10875
10876 if (enc_utf8)
10877
10878 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10879 list_append_number(rettv->vval.v_list,
10880 (varnumber_T)ScreenLinesC[i][off]);
10881}
10882
10883/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010884 * "screencol()" function
10885 *
10886 * First column is 1 to be consistent with virtcol().
10887 */
10888 static void
10889f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10890{
10891 rettv->vval.v_number = screen_screencol() + 1;
10892}
10893
10894/*
10895 * "screenrow()" function
10896 */
10897 static void
10898f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10899{
10900 rettv->vval.v_number = screen_screenrow() + 1;
10901}
10902
10903/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010904 * "screenstring()" function
10905 */
10906 static void
10907f_screenstring(typval_T *argvars, typval_T *rettv)
10908{
10909 int row;
10910 int col;
10911 int off;
10912 int c;
10913 int i;
10914 char_u buf[MB_MAXBYTES + 1];
10915 int buflen = 0;
10916
10917 rettv->vval.v_string = NULL;
10918 rettv->v_type = VAR_STRING;
10919
10920 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10921 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10922 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10923 return;
10924
10925 off = LineOffset[row] + col;
10926 if (enc_utf8 && ScreenLinesUC[off] != 0)
10927 c = ScreenLinesUC[off];
10928 else
10929 c = ScreenLines[off];
10930 buflen += mb_char2bytes(c, buf);
10931
10932 if (enc_utf8)
10933 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10934 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10935
10936 buf[buflen] = NUL;
10937 rettv->vval.v_string = vim_strsave(buf);
10938}
10939
10940/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010941 * "search()" function
10942 */
10943 static void
10944f_search(typval_T *argvars, typval_T *rettv)
10945{
10946 int flags = 0;
10947
10948 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10949}
10950
10951/*
10952 * "searchdecl()" function
10953 */
10954 static void
10955f_searchdecl(typval_T *argvars, typval_T *rettv)
10956{
10957 int locally = 1;
10958 int thisblock = 0;
10959 int error = FALSE;
10960 char_u *name;
10961
10962 rettv->vval.v_number = 1; /* default: FAIL */
10963
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010964 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010965 if (argvars[1].v_type != VAR_UNKNOWN)
10966 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010967 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010969 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010970 }
10971 if (!error && name != NULL)
10972 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10973 locally, thisblock, SEARCH_KEEP) == FAIL;
10974}
10975
10976/*
10977 * Used by searchpair() and searchpairpos()
10978 */
10979 static int
10980searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10981{
10982 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010983 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 int save_p_ws = p_ws;
10985 int dir;
10986 int flags = 0;
10987 char_u nbuf1[NUMBUFLEN];
10988 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989 int retval = 0; /* default: FAIL */
10990 long lnum_stop = 0;
10991 long time_limit = 0;
10992
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010993 /* Get the three pattern arguments: start, middle, end. Will result in an
10994 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010995 spat = tv_get_string_chk(&argvars[0]);
10996 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10997 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010998 if (spat == NULL || mpat == NULL || epat == NULL)
10999 goto theend; /* type error */
11000
11001 /* Handle the optional fourth argument: flags */
11002 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11003 if (dir == 0)
11004 goto theend;
11005
11006 /* Don't accept SP_END or SP_SUBPAT.
11007 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11008 */
11009 if ((flags & (SP_END | SP_SUBPAT)) != 0
11010 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011012 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011013 goto theend;
11014 }
11015
11016 /* Using 'r' implies 'W', otherwise it doesn't work. */
11017 if (flags & SP_REPEAT)
11018 p_ws = FALSE;
11019
11020 /* Optional fifth argument: skip expression */
11021 if (argvars[3].v_type == VAR_UNKNOWN
11022 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011023 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011024 else
11025 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011026 skip = &argvars[4];
11027 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11028 && skip->v_type != VAR_STRING)
11029 {
11030 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011031 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011032 goto theend;
11033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 if (argvars[5].v_type != VAR_UNKNOWN)
11035 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011036 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011037 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011039 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011040 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011041 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011042#ifdef FEAT_RELTIME
11043 if (argvars[6].v_type != VAR_UNKNOWN)
11044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011045 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011046 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011048 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011050 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011051 }
11052#endif
11053 }
11054 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055
11056 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11057 match_pos, lnum_stop, time_limit);
11058
11059theend:
11060 p_ws = save_p_ws;
11061
11062 return retval;
11063}
11064
11065/*
11066 * "searchpair()" function
11067 */
11068 static void
11069f_searchpair(typval_T *argvars, typval_T *rettv)
11070{
11071 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11072}
11073
11074/*
11075 * "searchpairpos()" function
11076 */
11077 static void
11078f_searchpairpos(typval_T *argvars, typval_T *rettv)
11079{
11080 pos_T match_pos;
11081 int lnum = 0;
11082 int col = 0;
11083
11084 if (rettv_list_alloc(rettv) == FAIL)
11085 return;
11086
11087 if (searchpair_cmn(argvars, &match_pos) > 0)
11088 {
11089 lnum = match_pos.lnum;
11090 col = match_pos.col;
11091 }
11092
11093 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11094 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11095}
11096
11097/*
11098 * Search for a start/middle/end thing.
11099 * Used by searchpair(), see its documentation for the details.
11100 * Returns 0 or -1 for no match,
11101 */
11102 long
11103do_searchpair(
11104 char_u *spat, /* start pattern */
11105 char_u *mpat, /* middle pattern */
11106 char_u *epat, /* end pattern */
11107 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011108 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 int flags, /* SP_SETPCMARK and other SP_ values */
11110 pos_T *match_pos,
11111 linenr_T lnum_stop, /* stop at this line if not zero */
11112 long time_limit UNUSED) /* stop after this many msec */
11113{
11114 char_u *save_cpo;
11115 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11116 long retval = 0;
11117 pos_T pos;
11118 pos_T firstpos;
11119 pos_T foundpos;
11120 pos_T save_cursor;
11121 pos_T save_pos;
11122 int n;
11123 int r;
11124 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011125 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011126 int err;
11127 int options = SEARCH_KEEP;
11128 proftime_T tm;
11129
11130 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11131 save_cpo = p_cpo;
11132 p_cpo = empty_option;
11133
11134#ifdef FEAT_RELTIME
11135 /* Set the time limit, if there is one. */
11136 profile_setlimit(time_limit, &tm);
11137#endif
11138
11139 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11140 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011141 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11142 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011143 if (pat2 == NULL || pat3 == NULL)
11144 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011145 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011146 if (*mpat == NUL)
11147 STRCPY(pat3, pat2);
11148 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011149 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 spat, epat, mpat);
11151 if (flags & SP_START)
11152 options |= SEARCH_START;
11153
Bram Moolenaar48570482017-10-30 21:48:41 +010011154 if (skip != NULL)
11155 {
11156 /* Empty string means to not use the skip expression. */
11157 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11158 use_skip = skip->vval.v_string != NULL
11159 && *skip->vval.v_string != NUL;
11160 }
11161
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011162 save_cursor = curwin->w_cursor;
11163 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011164 CLEAR_POS(&firstpos);
11165 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011166 pat = pat3;
11167 for (;;)
11168 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011169 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011170 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011171 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011172 /* didn't find it or found the first match again: FAIL */
11173 break;
11174
11175 if (firstpos.lnum == 0)
11176 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011177 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011178 {
11179 /* Found the same position again. Can happen with a pattern that
11180 * has "\zs" at the end and searching backwards. Advance one
11181 * character and try again. */
11182 if (dir == BACKWARD)
11183 decl(&pos);
11184 else
11185 incl(&pos);
11186 }
11187 foundpos = pos;
11188
11189 /* clear the start flag to avoid getting stuck here */
11190 options &= ~SEARCH_START;
11191
11192 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011193 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011194 {
11195 save_pos = curwin->w_cursor;
11196 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011197 err = FALSE;
11198 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011199 curwin->w_cursor = save_pos;
11200 if (err)
11201 {
11202 /* Evaluating {skip} caused an error, break here. */
11203 curwin->w_cursor = save_cursor;
11204 retval = -1;
11205 break;
11206 }
11207 if (r)
11208 continue;
11209 }
11210
11211 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11212 {
11213 /* Found end when searching backwards or start when searching
11214 * forward: nested pair. */
11215 ++nest;
11216 pat = pat2; /* nested, don't search for middle */
11217 }
11218 else
11219 {
11220 /* Found end when searching forward or start when searching
11221 * backward: end of (nested) pair; or found middle in outer pair. */
11222 if (--nest == 1)
11223 pat = pat3; /* outer level, search for middle */
11224 }
11225
11226 if (nest == 0)
11227 {
11228 /* Found the match: return matchcount or line number. */
11229 if (flags & SP_RETCOUNT)
11230 ++retval;
11231 else
11232 retval = pos.lnum;
11233 if (flags & SP_SETPCMARK)
11234 setpcmark();
11235 curwin->w_cursor = pos;
11236 if (!(flags & SP_REPEAT))
11237 break;
11238 nest = 1; /* search for next unmatched */
11239 }
11240 }
11241
11242 if (match_pos != NULL)
11243 {
11244 /* Store the match cursor position */
11245 match_pos->lnum = curwin->w_cursor.lnum;
11246 match_pos->col = curwin->w_cursor.col + 1;
11247 }
11248
11249 /* If 'n' flag is used or search failed: restore cursor position. */
11250 if ((flags & SP_NOMOVE) || retval == 0)
11251 curwin->w_cursor = save_cursor;
11252
11253theend:
11254 vim_free(pat2);
11255 vim_free(pat3);
11256 if (p_cpo == empty_option)
11257 p_cpo = save_cpo;
11258 else
11259 /* Darn, evaluating the {skip} expression changed the value. */
11260 free_string_option(save_cpo);
11261
11262 return retval;
11263}
11264
11265/*
11266 * "searchpos()" function
11267 */
11268 static void
11269f_searchpos(typval_T *argvars, typval_T *rettv)
11270{
11271 pos_T match_pos;
11272 int lnum = 0;
11273 int col = 0;
11274 int n;
11275 int flags = 0;
11276
11277 if (rettv_list_alloc(rettv) == FAIL)
11278 return;
11279
11280 n = search_cmn(argvars, &match_pos, &flags);
11281 if (n > 0)
11282 {
11283 lnum = match_pos.lnum;
11284 col = match_pos.col;
11285 }
11286
11287 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11288 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11289 if (flags & SP_SUBPAT)
11290 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11291}
11292
11293 static void
11294f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11295{
11296#ifdef FEAT_CLIENTSERVER
11297 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011298 char_u *server = tv_get_string_chk(&argvars[0]);
11299 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300
11301 rettv->vval.v_number = -1;
11302 if (server == NULL || reply == NULL)
11303 return;
11304 if (check_restricted() || check_secure())
11305 return;
11306# ifdef FEAT_X11
11307 if (check_connection() == FAIL)
11308 return;
11309# endif
11310
11311 if (serverSendReply(server, reply) < 0)
11312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011313 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011314 return;
11315 }
11316 rettv->vval.v_number = 0;
11317#else
11318 rettv->vval.v_number = -1;
11319#endif
11320}
11321
11322 static void
11323f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11324{
11325 char_u *r = NULL;
11326
11327#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011328# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011329 r = serverGetVimNames();
11330# else
11331 make_connection();
11332 if (X_DISPLAY != NULL)
11333 r = serverGetVimNames(X_DISPLAY);
11334# endif
11335#endif
11336 rettv->v_type = VAR_STRING;
11337 rettv->vval.v_string = r;
11338}
11339
11340/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011341 * "setbufline()" function
11342 */
11343 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011344f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011345{
11346 linenr_T lnum;
11347 buf_T *buf;
11348
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011349 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011350 if (buf == NULL)
11351 rettv->vval.v_number = 1; /* FAIL */
11352 else
11353 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011354 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011355 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011356 }
11357}
11358
11359/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 * "setbufvar()" function
11361 */
11362 static void
11363f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11364{
11365 buf_T *buf;
11366 char_u *varname, *bufvarname;
11367 typval_T *varp;
11368 char_u nbuf[NUMBUFLEN];
11369
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011370 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011371 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011372 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11373 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011374 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011375 varp = &argvars[2];
11376
11377 if (buf != NULL && varname != NULL && varp != NULL)
11378 {
11379 if (*varname == '&')
11380 {
11381 long numval;
11382 char_u *strval;
11383 int error = FALSE;
11384 aco_save_T aco;
11385
11386 /* set curbuf to be our buf, temporarily */
11387 aucmd_prepbuf(&aco, buf);
11388
11389 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011390 numval = (long)tv_get_number_chk(varp, &error);
11391 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011392 if (!error && strval != NULL)
11393 set_option_value(varname, numval, strval, OPT_LOCAL);
11394
11395 /* reset notion of buffer */
11396 aucmd_restbuf(&aco);
11397 }
11398 else
11399 {
11400 buf_T *save_curbuf = curbuf;
11401
Bram Moolenaar964b3742019-05-24 18:54:09 +020011402 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011403 if (bufvarname != NULL)
11404 {
11405 curbuf = buf;
11406 STRCPY(bufvarname, "b:");
11407 STRCPY(bufvarname + 2, varname);
11408 set_var(bufvarname, varp, TRUE);
11409 vim_free(bufvarname);
11410 curbuf = save_curbuf;
11411 }
11412 }
11413 }
11414}
11415
11416 static void
11417f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11418{
11419 dict_T *d;
11420 dictitem_T *di;
11421 char_u *csearch;
11422
11423 if (argvars[0].v_type != VAR_DICT)
11424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011425 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011426 return;
11427 }
11428
11429 if ((d = argvars[0].vval.v_dict) != NULL)
11430 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011431 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011432 if (csearch != NULL)
11433 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011434 if (enc_utf8)
11435 {
11436 int pcc[MAX_MCO];
11437 int c = utfc_ptr2char(csearch, pcc);
11438
11439 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11440 }
11441 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442 set_last_csearch(PTR2CHAR(csearch),
11443 csearch, MB_PTR2LEN(csearch));
11444 }
11445
11446 di = dict_find(d, (char_u *)"forward", -1);
11447 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011448 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449 ? FORWARD : BACKWARD);
11450
11451 di = dict_find(d, (char_u *)"until", -1);
11452 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011453 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011454 }
11455}
11456
11457/*
11458 * "setcmdpos()" function
11459 */
11460 static void
11461f_setcmdpos(typval_T *argvars, typval_T *rettv)
11462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011463 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011464
11465 if (pos >= 0)
11466 rettv->vval.v_number = set_cmdline_pos(pos);
11467}
11468
11469/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011470 * "setenv()" function
11471 */
11472 static void
11473f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11474{
11475 char_u namebuf[NUMBUFLEN];
11476 char_u valbuf[NUMBUFLEN];
11477 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11478
11479 if (argvars[1].v_type == VAR_SPECIAL
11480 && argvars[1].vval.v_number == VVAL_NULL)
11481 vim_unsetenv(name);
11482 else
11483 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11484}
11485
11486/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487 * "setfperm({fname}, {mode})" function
11488 */
11489 static void
11490f_setfperm(typval_T *argvars, typval_T *rettv)
11491{
11492 char_u *fname;
11493 char_u modebuf[NUMBUFLEN];
11494 char_u *mode_str;
11495 int i;
11496 int mask;
11497 int mode = 0;
11498
11499 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011500 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 if (fname == NULL)
11502 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011503 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011504 if (mode_str == NULL)
11505 return;
11506 if (STRLEN(mode_str) != 9)
11507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011508 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 return;
11510 }
11511
11512 mask = 1;
11513 for (i = 8; i >= 0; --i)
11514 {
11515 if (mode_str[i] != '-')
11516 mode |= mask;
11517 mask = mask << 1;
11518 }
11519 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11520}
11521
11522/*
11523 * "setline()" function
11524 */
11525 static void
11526f_setline(typval_T *argvars, typval_T *rettv)
11527{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011528 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011529
Bram Moolenaarca851592018-06-06 21:04:07 +020011530 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011531}
11532
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533/*
11534 * Used by "setqflist()" and "setloclist()" functions
11535 */
11536 static void
11537set_qf_ll_list(
11538 win_T *wp UNUSED,
11539 typval_T *list_arg UNUSED,
11540 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011541 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 typval_T *rettv)
11543{
11544#ifdef FEAT_QUICKFIX
11545 static char *e_invact = N_("E927: Invalid action: '%s'");
11546 char_u *act;
11547 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011548 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549#endif
11550
11551 rettv->vval.v_number = -1;
11552
11553#ifdef FEAT_QUICKFIX
11554 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011555 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011556 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011557 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558 else
11559 {
11560 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011561 dict_T *d = NULL;
11562 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011563
11564 if (action_arg->v_type == VAR_STRING)
11565 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011566 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011567 if (act == NULL)
11568 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011569 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11570 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011571 action = *act;
11572 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011573 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 }
11575 else if (action_arg->v_type == VAR_UNKNOWN)
11576 action = ' ';
11577 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011578 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579
Bram Moolenaard823fa92016-08-12 16:29:27 +020011580 if (action_arg->v_type != VAR_UNKNOWN
11581 && what_arg->v_type != VAR_UNKNOWN)
11582 {
11583 if (what_arg->v_type == VAR_DICT)
11584 d = what_arg->vval.v_dict;
11585 else
11586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011587 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011588 valid_dict = FALSE;
11589 }
11590 }
11591
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011592 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011593 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011594 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11595 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011596 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011597 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011598 }
11599#endif
11600}
11601
11602/*
11603 * "setloclist()" function
11604 */
11605 static void
11606f_setloclist(typval_T *argvars, typval_T *rettv)
11607{
11608 win_T *win;
11609
11610 rettv->vval.v_number = -1;
11611
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011612 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011613 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011614 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011615}
11616
11617/*
11618 * "setmatches()" function
11619 */
11620 static void
11621f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11622{
11623#ifdef FEAT_SEARCH_EXTRA
11624 list_T *l;
11625 listitem_T *li;
11626 dict_T *d;
11627 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011628 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011629
11630 rettv->vval.v_number = -1;
11631 if (argvars[0].v_type != VAR_LIST)
11632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011633 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011634 return;
11635 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011636 if (win == NULL)
11637 return;
11638
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011639 if ((l = argvars[0].vval.v_list) != NULL)
11640 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 /* To some extent make sure that we are dealing with a list from
11642 * "getmatches()". */
11643 li = l->lv_first;
11644 while (li != NULL)
11645 {
11646 if (li->li_tv.v_type != VAR_DICT
11647 || (d = li->li_tv.vval.v_dict) == NULL)
11648 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011649 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011650 return;
11651 }
11652 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11653 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11654 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11655 && dict_find(d, (char_u *)"priority", -1) != NULL
11656 && dict_find(d, (char_u *)"id", -1) != NULL))
11657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011658 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011659 return;
11660 }
11661 li = li->li_next;
11662 }
11663
Bram Moolenaaraff74912019-03-30 18:11:49 +010011664 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 li = l->lv_first;
11666 while (li != NULL)
11667 {
11668 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011669 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011670 dictitem_T *di;
11671 char_u *group;
11672 int priority;
11673 int id;
11674 char_u *conceal;
11675
11676 d = li->li_tv.vval.v_dict;
11677 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11678 {
11679 if (s == NULL)
11680 {
11681 s = list_alloc();
11682 if (s == NULL)
11683 return;
11684 }
11685
11686 /* match from matchaddpos() */
11687 for (i = 1; i < 9; i++)
11688 {
11689 sprintf((char *)buf, (char *)"pos%d", i);
11690 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11691 {
11692 if (di->di_tv.v_type != VAR_LIST)
11693 return;
11694
11695 list_append_tv(s, &di->di_tv);
11696 s->lv_refcount++;
11697 }
11698 else
11699 break;
11700 }
11701 }
11702
Bram Moolenaar8f667172018-12-14 15:38:31 +010011703 group = dict_get_string(d, (char_u *)"group", TRUE);
11704 priority = (int)dict_get_number(d, (char_u *)"priority");
11705 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011706 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011707 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011708 : NULL;
11709 if (i == 0)
11710 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011711 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011712 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011713 priority, id, NULL, conceal);
11714 }
11715 else
11716 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011717 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 list_unref(s);
11719 s = NULL;
11720 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011721 vim_free(group);
11722 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011723
11724 li = li->li_next;
11725 }
11726 rettv->vval.v_number = 0;
11727 }
11728#endif
11729}
11730
11731/*
11732 * "setpos()" function
11733 */
11734 static void
11735f_setpos(typval_T *argvars, typval_T *rettv)
11736{
11737 pos_T pos;
11738 int fnum;
11739 char_u *name;
11740 colnr_T curswant = -1;
11741
11742 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011743 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011744 if (name != NULL)
11745 {
11746 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11747 {
11748 if (--pos.col < 0)
11749 pos.col = 0;
11750 if (name[0] == '.' && name[1] == NUL)
11751 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011752 /* set cursor; "fnum" is ignored */
11753 curwin->w_cursor = pos;
11754 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011755 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011756 curwin->w_curswant = curswant - 1;
11757 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011758 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011759 check_cursor();
11760 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011761 }
11762 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11763 {
11764 /* set mark */
11765 if (setmark_pos(name[1], &pos, fnum) == OK)
11766 rettv->vval.v_number = 0;
11767 }
11768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011769 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011770 }
11771 }
11772}
11773
11774/*
11775 * "setqflist()" function
11776 */
11777 static void
11778f_setqflist(typval_T *argvars, typval_T *rettv)
11779{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011780 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011781}
11782
11783/*
11784 * "setreg()" function
11785 */
11786 static void
11787f_setreg(typval_T *argvars, typval_T *rettv)
11788{
11789 int regname;
11790 char_u *strregname;
11791 char_u *stropt;
11792 char_u *strval;
11793 int append;
11794 char_u yank_type;
11795 long block_len;
11796
11797 block_len = -1;
11798 yank_type = MAUTO;
11799 append = FALSE;
11800
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011801 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011802 rettv->vval.v_number = 1; /* FAIL is default */
11803
11804 if (strregname == NULL)
11805 return; /* type error; errmsg already given */
11806 regname = *strregname;
11807 if (regname == 0 || regname == '@')
11808 regname = '"';
11809
11810 if (argvars[2].v_type != VAR_UNKNOWN)
11811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011812 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011813 if (stropt == NULL)
11814 return; /* type error */
11815 for (; *stropt != NUL; ++stropt)
11816 switch (*stropt)
11817 {
11818 case 'a': case 'A': /* append */
11819 append = TRUE;
11820 break;
11821 case 'v': case 'c': /* character-wise selection */
11822 yank_type = MCHAR;
11823 break;
11824 case 'V': case 'l': /* line-wise selection */
11825 yank_type = MLINE;
11826 break;
11827 case 'b': case Ctrl_V: /* block-wise selection */
11828 yank_type = MBLOCK;
11829 if (VIM_ISDIGIT(stropt[1]))
11830 {
11831 ++stropt;
11832 block_len = getdigits(&stropt) - 1;
11833 --stropt;
11834 }
11835 break;
11836 }
11837 }
11838
11839 if (argvars[1].v_type == VAR_LIST)
11840 {
11841 char_u **lstval;
11842 char_u **allocval;
11843 char_u buf[NUMBUFLEN];
11844 char_u **curval;
11845 char_u **curallocval;
11846 list_T *ll = argvars[1].vval.v_list;
11847 listitem_T *li;
11848 int len;
11849
11850 /* If the list is NULL handle like an empty list. */
11851 len = ll == NULL ? 0 : ll->lv_len;
11852
11853 /* First half: use for pointers to result lines; second half: use for
11854 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011855 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 if (lstval == NULL)
11857 return;
11858 curval = lstval;
11859 allocval = lstval + len + 2;
11860 curallocval = allocval;
11861
11862 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11863 li = li->li_next)
11864 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011865 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866 if (strval == NULL)
11867 goto free_lstval;
11868 if (strval == buf)
11869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011870 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 * overwrite the string. */
11872 strval = vim_strsave(buf);
11873 if (strval == NULL)
11874 goto free_lstval;
11875 *curallocval++ = strval;
11876 }
11877 *curval++ = strval;
11878 }
11879 *curval++ = NULL;
11880
11881 write_reg_contents_lst(regname, lstval, -1,
11882 append, yank_type, block_len);
11883free_lstval:
11884 while (curallocval > allocval)
11885 vim_free(*--curallocval);
11886 vim_free(lstval);
11887 }
11888 else
11889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011890 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011891 if (strval == NULL)
11892 return;
11893 write_reg_contents_ex(regname, strval, -1,
11894 append, yank_type, block_len);
11895 }
11896 rettv->vval.v_number = 0;
11897}
11898
11899/*
11900 * "settabvar()" function
11901 */
11902 static void
11903f_settabvar(typval_T *argvars, typval_T *rettv)
11904{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011905 tabpage_T *save_curtab;
11906 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011907 char_u *varname, *tabvarname;
11908 typval_T *varp;
11909
11910 rettv->vval.v_number = 0;
11911
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011912 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011913 return;
11914
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011915 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11916 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 varp = &argvars[2];
11918
Bram Moolenaar4033c552017-09-16 20:54:51 +020011919 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011921 save_curtab = curtab;
11922 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923
Bram Moolenaar964b3742019-05-24 18:54:09 +020011924 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011925 if (tabvarname != NULL)
11926 {
11927 STRCPY(tabvarname, "t:");
11928 STRCPY(tabvarname + 2, varname);
11929 set_var(tabvarname, varp, TRUE);
11930 vim_free(tabvarname);
11931 }
11932
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011933 /* Restore current tabpage */
11934 if (valid_tabpage(save_curtab))
11935 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011936 }
11937}
11938
11939/*
11940 * "settabwinvar()" function
11941 */
11942 static void
11943f_settabwinvar(typval_T *argvars, typval_T *rettv)
11944{
11945 setwinvar(argvars, rettv, 1);
11946}
11947
11948/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011949 * "settagstack()" function
11950 */
11951 static void
11952f_settagstack(typval_T *argvars, typval_T *rettv)
11953{
11954 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11955 win_T *wp;
11956 dict_T *d;
11957 int action = 'r';
11958
11959 rettv->vval.v_number = -1;
11960
11961 // first argument: window number or id
11962 wp = find_win_by_nr_or_id(&argvars[0]);
11963 if (wp == NULL)
11964 return;
11965
11966 // second argument: dict with items to set in the tag stack
11967 if (argvars[1].v_type != VAR_DICT)
11968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011969 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011970 return;
11971 }
11972 d = argvars[1].vval.v_dict;
11973 if (d == NULL)
11974 return;
11975
11976 // third argument: action - 'a' for append and 'r' for replace.
11977 // default is to replace the stack.
11978 if (argvars[2].v_type == VAR_UNKNOWN)
11979 action = 'r';
11980 else if (argvars[2].v_type == VAR_STRING)
11981 {
11982 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011983 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011984 if (actstr == NULL)
11985 return;
11986 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11987 action = *actstr;
11988 else
11989 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011990 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011991 return;
11992 }
11993 }
11994 else
11995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011996 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011997 return;
11998 }
11999
12000 if (set_tagstack(wp, d, action) == OK)
12001 rettv->vval.v_number = 0;
12002}
12003
12004/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012005 * "setwinvar()" function
12006 */
12007 static void
12008f_setwinvar(typval_T *argvars, typval_T *rettv)
12009{
12010 setwinvar(argvars, rettv, 0);
12011}
12012
12013#ifdef FEAT_CRYPT
12014/*
12015 * "sha256({string})" function
12016 */
12017 static void
12018f_sha256(typval_T *argvars, typval_T *rettv)
12019{
12020 char_u *p;
12021
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012022 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012023 rettv->vval.v_string = vim_strsave(
12024 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12025 rettv->v_type = VAR_STRING;
12026}
12027#endif /* FEAT_CRYPT */
12028
12029/*
12030 * "shellescape({string})" function
12031 */
12032 static void
12033f_shellescape(typval_T *argvars, typval_T *rettv)
12034{
Bram Moolenaar20615522017-06-05 18:46:26 +020012035 int do_special = non_zero_arg(&argvars[1]);
12036
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012037 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012038 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012039 rettv->v_type = VAR_STRING;
12040}
12041
12042/*
12043 * shiftwidth() function
12044 */
12045 static void
12046f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12047{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012048 rettv->vval.v_number = 0;
12049
12050 if (argvars[0].v_type != VAR_UNKNOWN)
12051 {
12052 long col;
12053
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012054 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012055 if (col < 0)
12056 return; // type error; errmsg already given
12057#ifdef FEAT_VARTABS
12058 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12059 return;
12060#endif
12061 }
12062
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012063 rettv->vval.v_number = get_sw_value(curbuf);
12064}
12065
Bram Moolenaar162b7142018-12-21 15:17:36 +010012066#ifdef FEAT_SIGNS
12067/*
12068 * "sign_define()" function
12069 */
12070 static void
12071f_sign_define(typval_T *argvars, typval_T *rettv)
12072{
12073 char_u *name;
12074 dict_T *dict;
12075 char_u *icon = NULL;
12076 char_u *linehl = NULL;
12077 char_u *text = NULL;
12078 char_u *texthl = NULL;
12079
12080 rettv->vval.v_number = -1;
12081
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012082 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012083 if (name == NULL)
12084 return;
12085
12086 if (argvars[1].v_type != VAR_UNKNOWN)
12087 {
12088 if (argvars[1].v_type != VAR_DICT)
12089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012090 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012091 return;
12092 }
12093
12094 // sign attributes
12095 dict = argvars[1].vval.v_dict;
12096 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12097 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12098 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12099 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12100 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12101 text = dict_get_string(dict, (char_u *)"text", TRUE);
12102 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12103 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12104 }
12105
12106 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12107 rettv->vval.v_number = 0;
12108
12109 vim_free(icon);
12110 vim_free(linehl);
12111 vim_free(text);
12112 vim_free(texthl);
12113}
12114
12115/*
12116 * "sign_getdefined()" function
12117 */
12118 static void
12119f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12120{
12121 char_u *name = NULL;
12122
12123 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12124 return;
12125
12126 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012127 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012128
12129 sign_getlist(name, rettv->vval.v_list);
12130}
12131
12132/*
12133 * "sign_getplaced()" function
12134 */
12135 static void
12136f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12137{
12138 buf_T *buf = NULL;
12139 dict_T *dict;
12140 dictitem_T *di;
12141 linenr_T lnum = 0;
12142 int sign_id = 0;
12143 char_u *group = NULL;
12144 int notanum = FALSE;
12145
12146 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12147 return;
12148
12149 if (argvars[0].v_type != VAR_UNKNOWN)
12150 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012151 // get signs placed in the specified buffer
12152 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012153 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012154 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012155
12156 if (argvars[1].v_type != VAR_UNKNOWN)
12157 {
12158 if (argvars[1].v_type != VAR_DICT ||
12159 ((dict = argvars[1].vval.v_dict) == NULL))
12160 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012161 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012162 return;
12163 }
12164 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12165 {
12166 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012167 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012168 if (notanum)
12169 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012170 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012171 }
12172 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12173 {
12174 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012175 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012176 if (notanum)
12177 return;
12178 }
12179 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12180 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012181 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012182 if (group == NULL)
12183 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012184 if (*group == '\0') // empty string means global group
12185 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012186 }
12187 }
12188 }
12189
12190 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12191}
12192
12193/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012194 * "sign_jump()" function
12195 */
12196 static void
12197f_sign_jump(typval_T *argvars, typval_T *rettv)
12198{
12199 int sign_id;
12200 char_u *sign_group = NULL;
12201 buf_T *buf;
12202 int notanum = FALSE;
12203
12204 rettv->vval.v_number = -1;
12205
Bram Moolenaarbdace832019-03-02 10:13:42 +010012206 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012207 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12208 if (notanum)
12209 return;
12210 if (sign_id <= 0)
12211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012212 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012213 return;
12214 }
12215
12216 // Sign group
12217 sign_group = tv_get_string_chk(&argvars[1]);
12218 if (sign_group == NULL)
12219 return;
12220 if (sign_group[0] == '\0')
12221 sign_group = NULL; // global sign group
12222 else
12223 {
12224 sign_group = vim_strsave(sign_group);
12225 if (sign_group == NULL)
12226 return;
12227 }
12228
12229 // Buffer to place the sign
12230 buf = get_buf_arg(&argvars[2]);
12231 if (buf == NULL)
12232 goto cleanup;
12233
12234 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12235
12236cleanup:
12237 vim_free(sign_group);
12238}
12239
12240/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012241 * "sign_place()" function
12242 */
12243 static void
12244f_sign_place(typval_T *argvars, typval_T *rettv)
12245{
12246 int sign_id;
12247 char_u *group = NULL;
12248 char_u *sign_name;
12249 buf_T *buf;
12250 dict_T *dict;
12251 dictitem_T *di;
12252 linenr_T lnum = 0;
12253 int prio = SIGN_DEF_PRIO;
12254 int notanum = FALSE;
12255
12256 rettv->vval.v_number = -1;
12257
Bram Moolenaarbdace832019-03-02 10:13:42 +010012258 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012259 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012260 if (notanum)
12261 return;
12262 if (sign_id < 0)
12263 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012264 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012265 return;
12266 }
12267
12268 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012269 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012270 if (group == NULL)
12271 return;
12272 if (group[0] == '\0')
12273 group = NULL; // global sign group
12274 else
12275 {
12276 group = vim_strsave(group);
12277 if (group == NULL)
12278 return;
12279 }
12280
12281 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012282 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012283 if (sign_name == NULL)
12284 goto cleanup;
12285
12286 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012287 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012288 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012289 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012290
12291 if (argvars[4].v_type != VAR_UNKNOWN)
12292 {
12293 if (argvars[4].v_type != VAR_DICT ||
12294 ((dict = argvars[4].vval.v_dict) == NULL))
12295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012296 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012297 goto cleanup;
12298 }
12299
12300 // Line number where the sign is to be placed
12301 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12302 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012303 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012304 if (notanum)
12305 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012306 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012307 }
12308 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12309 {
12310 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012311 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012312 if (notanum)
12313 goto cleanup;
12314 }
12315 }
12316
12317 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12318 rettv->vval.v_number = sign_id;
12319
12320cleanup:
12321 vim_free(group);
12322}
12323
12324/*
12325 * "sign_undefine()" function
12326 */
12327 static void
12328f_sign_undefine(typval_T *argvars, typval_T *rettv)
12329{
12330 char_u *name;
12331
12332 rettv->vval.v_number = -1;
12333
12334 if (argvars[0].v_type == VAR_UNKNOWN)
12335 {
12336 // Free all the signs
12337 free_signs();
12338 rettv->vval.v_number = 0;
12339 }
12340 else
12341 {
12342 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012343 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012344 if (name == NULL)
12345 return;
12346
12347 if (sign_undefine_by_name(name) == OK)
12348 rettv->vval.v_number = 0;
12349 }
12350}
12351
12352/*
12353 * "sign_unplace()" function
12354 */
12355 static void
12356f_sign_unplace(typval_T *argvars, typval_T *rettv)
12357{
12358 dict_T *dict;
12359 dictitem_T *di;
12360 int sign_id = 0;
12361 buf_T *buf = NULL;
12362 char_u *group = NULL;
12363
12364 rettv->vval.v_number = -1;
12365
12366 if (argvars[0].v_type != VAR_STRING)
12367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012368 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012369 return;
12370 }
12371
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012372 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012373 if (group[0] == '\0')
12374 group = NULL; // global sign group
12375 else
12376 {
12377 group = vim_strsave(group);
12378 if (group == NULL)
12379 return;
12380 }
12381
12382 if (argvars[1].v_type != VAR_UNKNOWN)
12383 {
12384 if (argvars[1].v_type != VAR_DICT)
12385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012386 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012387 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012388 }
12389 dict = argvars[1].vval.v_dict;
12390
12391 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12392 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012393 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012394 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012395 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012396 }
12397 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12398 sign_id = dict_get_number(dict, (char_u *)"id");
12399 }
12400
12401 if (buf == NULL)
12402 {
12403 // Delete the sign in all the buffers
12404 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012405 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012406 rettv->vval.v_number = 0;
12407 }
12408 else
12409 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012410 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012411 rettv->vval.v_number = 0;
12412 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012413
12414cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012415 vim_free(group);
12416}
12417#endif
12418
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012419/*
12420 * "simplify()" function
12421 */
12422 static void
12423f_simplify(typval_T *argvars, typval_T *rettv)
12424{
12425 char_u *p;
12426
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012427 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012428 rettv->vval.v_string = vim_strsave(p);
12429 simplify_filename(rettv->vval.v_string); /* simplify in place */
12430 rettv->v_type = VAR_STRING;
12431}
12432
12433#ifdef FEAT_FLOAT
12434/*
12435 * "sin()" function
12436 */
12437 static void
12438f_sin(typval_T *argvars, typval_T *rettv)
12439{
12440 float_T f = 0.0;
12441
12442 rettv->v_type = VAR_FLOAT;
12443 if (get_float_arg(argvars, &f) == OK)
12444 rettv->vval.v_float = sin(f);
12445 else
12446 rettv->vval.v_float = 0.0;
12447}
12448
12449/*
12450 * "sinh()" function
12451 */
12452 static void
12453f_sinh(typval_T *argvars, typval_T *rettv)
12454{
12455 float_T f = 0.0;
12456
12457 rettv->v_type = VAR_FLOAT;
12458 if (get_float_arg(argvars, &f) == OK)
12459 rettv->vval.v_float = sinh(f);
12460 else
12461 rettv->vval.v_float = 0.0;
12462}
12463#endif
12464
Bram Moolenaareae1b912019-05-09 15:12:55 +020012465static int item_compare(const void *s1, const void *s2);
12466static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467
12468/* struct used in the array that's given to qsort() */
12469typedef struct
12470{
12471 listitem_T *item;
12472 int idx;
12473} sortItem_T;
12474
12475/* struct storing information about current sort */
12476typedef struct
12477{
12478 int item_compare_ic;
12479 int item_compare_numeric;
12480 int item_compare_numbers;
12481#ifdef FEAT_FLOAT
12482 int item_compare_float;
12483#endif
12484 char_u *item_compare_func;
12485 partial_T *item_compare_partial;
12486 dict_T *item_compare_selfdict;
12487 int item_compare_func_err;
12488 int item_compare_keep_zero;
12489} sortinfo_T;
12490static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012491#define ITEM_COMPARE_FAIL 999
12492
12493/*
12494 * Compare functions for f_sort() and f_uniq() below.
12495 */
12496 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012497item_compare(const void *s1, const void *s2)
12498{
12499 sortItem_T *si1, *si2;
12500 typval_T *tv1, *tv2;
12501 char_u *p1, *p2;
12502 char_u *tofree1 = NULL, *tofree2 = NULL;
12503 int res;
12504 char_u numbuf1[NUMBUFLEN];
12505 char_u numbuf2[NUMBUFLEN];
12506
12507 si1 = (sortItem_T *)s1;
12508 si2 = (sortItem_T *)s2;
12509 tv1 = &si1->item->li_tv;
12510 tv2 = &si2->item->li_tv;
12511
12512 if (sortinfo->item_compare_numbers)
12513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012514 varnumber_T v1 = tv_get_number(tv1);
12515 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516
12517 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12518 }
12519
12520#ifdef FEAT_FLOAT
12521 if (sortinfo->item_compare_float)
12522 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012523 float_T v1 = tv_get_float(tv1);
12524 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012525
12526 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12527 }
12528#endif
12529
12530 /* tv2string() puts quotes around a string and allocates memory. Don't do
12531 * that for string variables. Use a single quote when comparing with a
12532 * non-string to do what the docs promise. */
12533 if (tv1->v_type == VAR_STRING)
12534 {
12535 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12536 p1 = (char_u *)"'";
12537 else
12538 p1 = tv1->vval.v_string;
12539 }
12540 else
12541 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12542 if (tv2->v_type == VAR_STRING)
12543 {
12544 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12545 p2 = (char_u *)"'";
12546 else
12547 p2 = tv2->vval.v_string;
12548 }
12549 else
12550 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12551 if (p1 == NULL)
12552 p1 = (char_u *)"";
12553 if (p2 == NULL)
12554 p2 = (char_u *)"";
12555 if (!sortinfo->item_compare_numeric)
12556 {
12557 if (sortinfo->item_compare_ic)
12558 res = STRICMP(p1, p2);
12559 else
12560 res = STRCMP(p1, p2);
12561 }
12562 else
12563 {
12564 double n1, n2;
12565 n1 = strtod((char *)p1, (char **)&p1);
12566 n2 = strtod((char *)p2, (char **)&p2);
12567 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12568 }
12569
12570 /* When the result would be zero, compare the item indexes. Makes the
12571 * sort stable. */
12572 if (res == 0 && !sortinfo->item_compare_keep_zero)
12573 res = si1->idx > si2->idx ? 1 : -1;
12574
12575 vim_free(tofree1);
12576 vim_free(tofree2);
12577 return res;
12578}
12579
12580 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012581item_compare2(const void *s1, const void *s2)
12582{
12583 sortItem_T *si1, *si2;
12584 int res;
12585 typval_T rettv;
12586 typval_T argv[3];
12587 int dummy;
12588 char_u *func_name;
12589 partial_T *partial = sortinfo->item_compare_partial;
12590
12591 /* shortcut after failure in previous call; compare all items equal */
12592 if (sortinfo->item_compare_func_err)
12593 return 0;
12594
12595 si1 = (sortItem_T *)s1;
12596 si2 = (sortItem_T *)s2;
12597
12598 if (partial == NULL)
12599 func_name = sortinfo->item_compare_func;
12600 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012601 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012602
12603 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12604 * in the copy without changing the original list items. */
12605 copy_tv(&si1->item->li_tv, &argv[0]);
12606 copy_tv(&si2->item->li_tv, &argv[1]);
12607
12608 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012609 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012610 partial, sortinfo->item_compare_selfdict);
12611 clear_tv(&argv[0]);
12612 clear_tv(&argv[1]);
12613
12614 if (res == FAIL)
12615 res = ITEM_COMPARE_FAIL;
12616 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012617 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618 if (sortinfo->item_compare_func_err)
12619 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12620 clear_tv(&rettv);
12621
12622 /* When the result would be zero, compare the pointers themselves. Makes
12623 * the sort stable. */
12624 if (res == 0 && !sortinfo->item_compare_keep_zero)
12625 res = si1->idx > si2->idx ? 1 : -1;
12626
12627 return res;
12628}
12629
12630/*
12631 * "sort({list})" function
12632 */
12633 static void
12634do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12635{
12636 list_T *l;
12637 listitem_T *li;
12638 sortItem_T *ptrs;
12639 sortinfo_T *old_sortinfo;
12640 sortinfo_T info;
12641 long len;
12642 long i;
12643
12644 /* Pointer to current info struct used in compare function. Save and
12645 * restore the current one for nested calls. */
12646 old_sortinfo = sortinfo;
12647 sortinfo = &info;
12648
12649 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012650 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012651 else
12652 {
12653 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012654 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12656 TRUE))
12657 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012658 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012659
12660 len = list_len(l);
12661 if (len <= 1)
12662 goto theend; /* short list sorts pretty quickly */
12663
12664 info.item_compare_ic = FALSE;
12665 info.item_compare_numeric = FALSE;
12666 info.item_compare_numbers = FALSE;
12667#ifdef FEAT_FLOAT
12668 info.item_compare_float = FALSE;
12669#endif
12670 info.item_compare_func = NULL;
12671 info.item_compare_partial = NULL;
12672 info.item_compare_selfdict = NULL;
12673 if (argvars[1].v_type != VAR_UNKNOWN)
12674 {
12675 /* optional second argument: {func} */
12676 if (argvars[1].v_type == VAR_FUNC)
12677 info.item_compare_func = argvars[1].vval.v_string;
12678 else if (argvars[1].v_type == VAR_PARTIAL)
12679 info.item_compare_partial = argvars[1].vval.v_partial;
12680 else
12681 {
12682 int error = FALSE;
12683
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012684 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012685 if (error)
12686 goto theend; /* type error; errmsg already given */
12687 if (i == 1)
12688 info.item_compare_ic = TRUE;
12689 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012690 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012691 else if (i != 0)
12692 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012693 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012694 goto theend;
12695 }
12696 if (info.item_compare_func != NULL)
12697 {
12698 if (*info.item_compare_func == NUL)
12699 {
12700 /* empty string means default sort */
12701 info.item_compare_func = NULL;
12702 }
12703 else if (STRCMP(info.item_compare_func, "n") == 0)
12704 {
12705 info.item_compare_func = NULL;
12706 info.item_compare_numeric = TRUE;
12707 }
12708 else if (STRCMP(info.item_compare_func, "N") == 0)
12709 {
12710 info.item_compare_func = NULL;
12711 info.item_compare_numbers = TRUE;
12712 }
12713#ifdef FEAT_FLOAT
12714 else if (STRCMP(info.item_compare_func, "f") == 0)
12715 {
12716 info.item_compare_func = NULL;
12717 info.item_compare_float = TRUE;
12718 }
12719#endif
12720 else if (STRCMP(info.item_compare_func, "i") == 0)
12721 {
12722 info.item_compare_func = NULL;
12723 info.item_compare_ic = TRUE;
12724 }
12725 }
12726 }
12727
12728 if (argvars[2].v_type != VAR_UNKNOWN)
12729 {
12730 /* optional third argument: {dict} */
12731 if (argvars[2].v_type != VAR_DICT)
12732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012733 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 goto theend;
12735 }
12736 info.item_compare_selfdict = argvars[2].vval.v_dict;
12737 }
12738 }
12739
12740 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012741 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012742 if (ptrs == NULL)
12743 goto theend;
12744
12745 i = 0;
12746 if (sort)
12747 {
12748 /* sort(): ptrs will be the list to sort */
12749 for (li = l->lv_first; li != NULL; li = li->li_next)
12750 {
12751 ptrs[i].item = li;
12752 ptrs[i].idx = i;
12753 ++i;
12754 }
12755
12756 info.item_compare_func_err = FALSE;
12757 info.item_compare_keep_zero = FALSE;
12758 /* test the compare function */
12759 if ((info.item_compare_func != NULL
12760 || info.item_compare_partial != NULL)
12761 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12762 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012763 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012764 else
12765 {
12766 /* Sort the array with item pointers. */
12767 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12768 info.item_compare_func == NULL
12769 && info.item_compare_partial == NULL
12770 ? item_compare : item_compare2);
12771
12772 if (!info.item_compare_func_err)
12773 {
12774 /* Clear the List and append the items in sorted order. */
12775 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12776 l->lv_len = 0;
12777 for (i = 0; i < len; ++i)
12778 list_append(l, ptrs[i].item);
12779 }
12780 }
12781 }
12782 else
12783 {
12784 int (*item_compare_func_ptr)(const void *, const void *);
12785
12786 /* f_uniq(): ptrs will be a stack of items to remove */
12787 info.item_compare_func_err = FALSE;
12788 info.item_compare_keep_zero = TRUE;
12789 item_compare_func_ptr = info.item_compare_func != NULL
12790 || info.item_compare_partial != NULL
12791 ? item_compare2 : item_compare;
12792
12793 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12794 li = li->li_next)
12795 {
12796 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12797 == 0)
12798 ptrs[i++].item = li;
12799 if (info.item_compare_func_err)
12800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012801 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012802 break;
12803 }
12804 }
12805
12806 if (!info.item_compare_func_err)
12807 {
12808 while (--i >= 0)
12809 {
12810 li = ptrs[i].item->li_next;
12811 ptrs[i].item->li_next = li->li_next;
12812 if (li->li_next != NULL)
12813 li->li_next->li_prev = ptrs[i].item;
12814 else
12815 l->lv_last = ptrs[i].item;
12816 list_fix_watch(l, li);
12817 listitem_free(li);
12818 l->lv_len--;
12819 }
12820 }
12821 }
12822
12823 vim_free(ptrs);
12824 }
12825theend:
12826 sortinfo = old_sortinfo;
12827}
12828
12829/*
12830 * "sort({list})" function
12831 */
12832 static void
12833f_sort(typval_T *argvars, typval_T *rettv)
12834{
12835 do_sort_uniq(argvars, rettv, TRUE);
12836}
12837
12838/*
12839 * "uniq({list})" function
12840 */
12841 static void
12842f_uniq(typval_T *argvars, typval_T *rettv)
12843{
12844 do_sort_uniq(argvars, rettv, FALSE);
12845}
12846
12847/*
12848 * "soundfold({word})" function
12849 */
12850 static void
12851f_soundfold(typval_T *argvars, typval_T *rettv)
12852{
12853 char_u *s;
12854
12855 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012856 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012857#ifdef FEAT_SPELL
12858 rettv->vval.v_string = eval_soundfold(s);
12859#else
12860 rettv->vval.v_string = vim_strsave(s);
12861#endif
12862}
12863
12864/*
12865 * "spellbadword()" function
12866 */
12867 static void
12868f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12869{
12870 char_u *word = (char_u *)"";
12871 hlf_T attr = HLF_COUNT;
12872 int len = 0;
12873
12874 if (rettv_list_alloc(rettv) == FAIL)
12875 return;
12876
12877#ifdef FEAT_SPELL
12878 if (argvars[0].v_type == VAR_UNKNOWN)
12879 {
12880 /* Find the start and length of the badly spelled word. */
12881 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12882 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012883 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012884 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012885 curwin->w_set_curswant = TRUE;
12886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012887 }
12888 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12889 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012890 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891 int capcol = -1;
12892
12893 if (str != NULL)
12894 {
12895 /* Check the argument for spelling. */
12896 while (*str != NUL)
12897 {
12898 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12899 if (attr != HLF_COUNT)
12900 {
12901 word = str;
12902 break;
12903 }
12904 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012905 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012906 }
12907 }
12908 }
12909#endif
12910
12911 list_append_string(rettv->vval.v_list, word, len);
12912 list_append_string(rettv->vval.v_list, (char_u *)(
12913 attr == HLF_SPB ? "bad" :
12914 attr == HLF_SPR ? "rare" :
12915 attr == HLF_SPL ? "local" :
12916 attr == HLF_SPC ? "caps" :
12917 ""), -1);
12918}
12919
12920/*
12921 * "spellsuggest()" function
12922 */
12923 static void
12924f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12925{
12926#ifdef FEAT_SPELL
12927 char_u *str;
12928 int typeerr = FALSE;
12929 int maxcount;
12930 garray_T ga;
12931 int i;
12932 listitem_T *li;
12933 int need_capital = FALSE;
12934#endif
12935
12936 if (rettv_list_alloc(rettv) == FAIL)
12937 return;
12938
12939#ifdef FEAT_SPELL
12940 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12941 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012942 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012943 if (argvars[1].v_type != VAR_UNKNOWN)
12944 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012945 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012946 if (maxcount <= 0)
12947 return;
12948 if (argvars[2].v_type != VAR_UNKNOWN)
12949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012950 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012951 if (typeerr)
12952 return;
12953 }
12954 }
12955 else
12956 maxcount = 25;
12957
12958 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12959
12960 for (i = 0; i < ga.ga_len; ++i)
12961 {
12962 str = ((char_u **)ga.ga_data)[i];
12963
12964 li = listitem_alloc();
12965 if (li == NULL)
12966 vim_free(str);
12967 else
12968 {
12969 li->li_tv.v_type = VAR_STRING;
12970 li->li_tv.v_lock = 0;
12971 li->li_tv.vval.v_string = str;
12972 list_append(rettv->vval.v_list, li);
12973 }
12974 }
12975 ga_clear(&ga);
12976 }
12977#endif
12978}
12979
12980 static void
12981f_split(typval_T *argvars, typval_T *rettv)
12982{
12983 char_u *str;
12984 char_u *end;
12985 char_u *pat = NULL;
12986 regmatch_T regmatch;
12987 char_u patbuf[NUMBUFLEN];
12988 char_u *save_cpo;
12989 int match;
12990 colnr_T col = 0;
12991 int keepempty = FALSE;
12992 int typeerr = FALSE;
12993
12994 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12995 save_cpo = p_cpo;
12996 p_cpo = (char_u *)"";
12997
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012998 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012999 if (argvars[1].v_type != VAR_UNKNOWN)
13000 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013001 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002 if (pat == NULL)
13003 typeerr = TRUE;
13004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013005 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006 }
13007 if (pat == NULL || *pat == NUL)
13008 pat = (char_u *)"[\\x01- ]\\+";
13009
13010 if (rettv_list_alloc(rettv) == FAIL)
13011 return;
13012 if (typeerr)
13013 return;
13014
13015 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13016 if (regmatch.regprog != NULL)
13017 {
13018 regmatch.rm_ic = FALSE;
13019 while (*str != NUL || keepempty)
13020 {
13021 if (*str == NUL)
13022 match = FALSE; /* empty item at the end */
13023 else
13024 match = vim_regexec_nl(&regmatch, str, col);
13025 if (match)
13026 end = regmatch.startp[0];
13027 else
13028 end = str + STRLEN(str);
13029 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13030 && *str != NUL && match && end < regmatch.endp[0]))
13031 {
13032 if (list_append_string(rettv->vval.v_list, str,
13033 (int)(end - str)) == FAIL)
13034 break;
13035 }
13036 if (!match)
13037 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013038 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013039 if (regmatch.endp[0] > str)
13040 col = 0;
13041 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013042 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013043 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013044 str = regmatch.endp[0];
13045 }
13046
13047 vim_regfree(regmatch.regprog);
13048 }
13049
13050 p_cpo = save_cpo;
13051}
13052
13053#ifdef FEAT_FLOAT
13054/*
13055 * "sqrt()" function
13056 */
13057 static void
13058f_sqrt(typval_T *argvars, typval_T *rettv)
13059{
13060 float_T f = 0.0;
13061
13062 rettv->v_type = VAR_FLOAT;
13063 if (get_float_arg(argvars, &f) == OK)
13064 rettv->vval.v_float = sqrt(f);
13065 else
13066 rettv->vval.v_float = 0.0;
13067}
13068
13069/*
13070 * "str2float()" function
13071 */
13072 static void
13073f_str2float(typval_T *argvars, typval_T *rettv)
13074{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013075 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013076 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077
Bram Moolenaar08243d22017-01-10 16:12:29 +010013078 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013079 p = skipwhite(p + 1);
13080 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013081 if (isneg)
13082 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013083 rettv->v_type = VAR_FLOAT;
13084}
13085#endif
13086
13087/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013088 * "str2list()" function
13089 */
13090 static void
13091f_str2list(typval_T *argvars, typval_T *rettv)
13092{
13093 char_u *p;
13094 int utf8 = FALSE;
13095
13096 if (rettv_list_alloc(rettv) == FAIL)
13097 return;
13098
13099 if (argvars[1].v_type != VAR_UNKNOWN)
13100 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13101
13102 p = tv_get_string(&argvars[0]);
13103
13104 if (has_mbyte || utf8)
13105 {
13106 int (*ptr2len)(char_u *);
13107 int (*ptr2char)(char_u *);
13108
13109 if (utf8 || enc_utf8)
13110 {
13111 ptr2len = utf_ptr2len;
13112 ptr2char = utf_ptr2char;
13113 }
13114 else
13115 {
13116 ptr2len = mb_ptr2len;
13117 ptr2char = mb_ptr2char;
13118 }
13119
13120 for ( ; *p != NUL; p += (*ptr2len)(p))
13121 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13122 }
13123 else
13124 for ( ; *p != NUL; ++p)
13125 list_append_number(rettv->vval.v_list, *p);
13126}
13127
13128/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 * "str2nr()" function
13130 */
13131 static void
13132f_str2nr(typval_T *argvars, typval_T *rettv)
13133{
13134 int base = 10;
13135 char_u *p;
13136 varnumber_T n;
13137 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013138 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139
13140 if (argvars[1].v_type != VAR_UNKNOWN)
13141 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013142 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013143 if (base != 2 && base != 8 && base != 10 && base != 16)
13144 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013145 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146 return;
13147 }
13148 }
13149
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013150 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013151 isneg = (*p == '-');
13152 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 p = skipwhite(p + 1);
13154 switch (base)
13155 {
13156 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13157 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13158 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13159 default: what = 0;
13160 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013161 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13162 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013163 if (isneg)
13164 rettv->vval.v_number = -n;
13165 else
13166 rettv->vval.v_number = n;
13167
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013168}
13169
13170#ifdef HAVE_STRFTIME
13171/*
13172 * "strftime({format}[, {time}])" function
13173 */
13174 static void
13175f_strftime(typval_T *argvars, typval_T *rettv)
13176{
13177 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013178# ifdef HAVE_LOCALTIME_R
13179 struct tm tmval;
13180# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 struct tm *curtime;
13182 time_t seconds;
13183 char_u *p;
13184
13185 rettv->v_type = VAR_STRING;
13186
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013187 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188 if (argvars[1].v_type == VAR_UNKNOWN)
13189 seconds = time(NULL);
13190 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013191 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013192# ifdef HAVE_LOCALTIME_R
13193 curtime = localtime_r(&seconds, &tmval);
13194# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013196# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 /* MSVC returns NULL for an invalid value of seconds. */
13198 if (curtime == NULL)
13199 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13200 else
13201 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013202 vimconv_T conv;
13203 char_u *enc;
13204
13205 conv.vc_type = CONV_NONE;
13206 enc = enc_locale();
13207 convert_setup(&conv, p_enc, enc);
13208 if (conv.vc_type != CONV_NONE)
13209 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210 if (p != NULL)
13211 (void)strftime((char *)result_buf, sizeof(result_buf),
13212 (char *)p, curtime);
13213 else
13214 result_buf[0] = NUL;
13215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013216 if (conv.vc_type != CONV_NONE)
13217 vim_free(p);
13218 convert_setup(&conv, enc, p_enc);
13219 if (conv.vc_type != CONV_NONE)
13220 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13221 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222 rettv->vval.v_string = vim_strsave(result_buf);
13223
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013224 /* Release conversion descriptors */
13225 convert_setup(&conv, NULL, NULL);
13226 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013227 }
13228}
13229#endif
13230
13231/*
13232 * "strgetchar()" function
13233 */
13234 static void
13235f_strgetchar(typval_T *argvars, typval_T *rettv)
13236{
13237 char_u *str;
13238 int len;
13239 int error = FALSE;
13240 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013241 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013242
13243 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013244 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013245 if (str == NULL)
13246 return;
13247 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013248 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013249 if (error)
13250 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013251
Bram Moolenaar13505972019-01-24 15:04:48 +010013252 while (charidx >= 0 && byteidx < len)
13253 {
13254 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013256 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13257 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013259 --charidx;
13260 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262}
13263
13264/*
13265 * "stridx()" function
13266 */
13267 static void
13268f_stridx(typval_T *argvars, typval_T *rettv)
13269{
13270 char_u buf[NUMBUFLEN];
13271 char_u *needle;
13272 char_u *haystack;
13273 char_u *save_haystack;
13274 char_u *pos;
13275 int start_idx;
13276
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013277 needle = tv_get_string_chk(&argvars[1]);
13278 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013279 rettv->vval.v_number = -1;
13280 if (needle == NULL || haystack == NULL)
13281 return; /* type error; errmsg already given */
13282
13283 if (argvars[2].v_type != VAR_UNKNOWN)
13284 {
13285 int error = FALSE;
13286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013287 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013288 if (error || start_idx >= (int)STRLEN(haystack))
13289 return;
13290 if (start_idx >= 0)
13291 haystack += start_idx;
13292 }
13293
13294 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13295 if (pos != NULL)
13296 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13297}
13298
13299/*
13300 * "string()" function
13301 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013302 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013303f_string(typval_T *argvars, typval_T *rettv)
13304{
13305 char_u *tofree;
13306 char_u numbuf[NUMBUFLEN];
13307
13308 rettv->v_type = VAR_STRING;
13309 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13310 get_copyID());
13311 /* Make a copy if we have a value but it's not in allocated memory. */
13312 if (rettv->vval.v_string != NULL && tofree == NULL)
13313 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13314}
13315
13316/*
13317 * "strlen()" function
13318 */
13319 static void
13320f_strlen(typval_T *argvars, typval_T *rettv)
13321{
13322 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013323 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013324}
13325
13326/*
13327 * "strchars()" function
13328 */
13329 static void
13330f_strchars(typval_T *argvars, typval_T *rettv)
13331{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013332 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013333 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334 varnumber_T len = 0;
13335 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336
13337 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013338 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013340 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 else
13342 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13344 while (*s != NUL)
13345 {
13346 func_mb_ptr2char_adv(&s);
13347 ++len;
13348 }
13349 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013350 }
13351}
13352
13353/*
13354 * "strdisplaywidth()" function
13355 */
13356 static void
13357f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13358{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013359 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013360 int col = 0;
13361
13362 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013363 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364
13365 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13366}
13367
13368/*
13369 * "strwidth()" function
13370 */
13371 static void
13372f_strwidth(typval_T *argvars, typval_T *rettv)
13373{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013374 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013375
Bram Moolenaar13505972019-01-24 15:04:48 +010013376 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377}
13378
13379/*
13380 * "strcharpart()" function
13381 */
13382 static void
13383f_strcharpart(typval_T *argvars, typval_T *rettv)
13384{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013385 char_u *p;
13386 int nchar;
13387 int nbyte = 0;
13388 int charlen;
13389 int len = 0;
13390 int slen;
13391 int error = FALSE;
13392
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013393 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 slen = (int)STRLEN(p);
13395
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013396 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013397 if (!error)
13398 {
13399 if (nchar > 0)
13400 while (nchar > 0 && nbyte < slen)
13401 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013402 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013403 --nchar;
13404 }
13405 else
13406 nbyte = nchar;
13407 if (argvars[2].v_type != VAR_UNKNOWN)
13408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013409 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 while (charlen > 0 && nbyte + len < slen)
13411 {
13412 int off = nbyte + len;
13413
13414 if (off < 0)
13415 len += 1;
13416 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013417 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013418 --charlen;
13419 }
13420 }
13421 else
13422 len = slen - nbyte; /* default: all bytes that are available. */
13423 }
13424
13425 /*
13426 * Only return the overlap between the specified part and the actual
13427 * string.
13428 */
13429 if (nbyte < 0)
13430 {
13431 len += nbyte;
13432 nbyte = 0;
13433 }
13434 else if (nbyte > slen)
13435 nbyte = slen;
13436 if (len < 0)
13437 len = 0;
13438 else if (nbyte + len > slen)
13439 len = slen - nbyte;
13440
13441 rettv->v_type = VAR_STRING;
13442 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013443}
13444
13445/*
13446 * "strpart()" function
13447 */
13448 static void
13449f_strpart(typval_T *argvars, typval_T *rettv)
13450{
13451 char_u *p;
13452 int n;
13453 int len;
13454 int slen;
13455 int error = FALSE;
13456
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013457 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013458 slen = (int)STRLEN(p);
13459
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013460 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013461 if (error)
13462 len = 0;
13463 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013464 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013465 else
13466 len = slen - n; /* default len: all bytes that are available. */
13467
13468 /*
13469 * Only return the overlap between the specified part and the actual
13470 * string.
13471 */
13472 if (n < 0)
13473 {
13474 len += n;
13475 n = 0;
13476 }
13477 else if (n > slen)
13478 n = slen;
13479 if (len < 0)
13480 len = 0;
13481 else if (n + len > slen)
13482 len = slen - n;
13483
13484 rettv->v_type = VAR_STRING;
13485 rettv->vval.v_string = vim_strnsave(p + n, len);
13486}
13487
13488/*
13489 * "strridx()" function
13490 */
13491 static void
13492f_strridx(typval_T *argvars, typval_T *rettv)
13493{
13494 char_u buf[NUMBUFLEN];
13495 char_u *needle;
13496 char_u *haystack;
13497 char_u *rest;
13498 char_u *lastmatch = NULL;
13499 int haystack_len, end_idx;
13500
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013501 needle = tv_get_string_chk(&argvars[1]);
13502 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503
13504 rettv->vval.v_number = -1;
13505 if (needle == NULL || haystack == NULL)
13506 return; /* type error; errmsg already given */
13507
13508 haystack_len = (int)STRLEN(haystack);
13509 if (argvars[2].v_type != VAR_UNKNOWN)
13510 {
13511 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013512 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013513 if (end_idx < 0)
13514 return; /* can never find a match */
13515 }
13516 else
13517 end_idx = haystack_len;
13518
13519 if (*needle == NUL)
13520 {
13521 /* Empty string matches past the end. */
13522 lastmatch = haystack + end_idx;
13523 }
13524 else
13525 {
13526 for (rest = haystack; *rest != '\0'; ++rest)
13527 {
13528 rest = (char_u *)strstr((char *)rest, (char *)needle);
13529 if (rest == NULL || rest > haystack + end_idx)
13530 break;
13531 lastmatch = rest;
13532 }
13533 }
13534
13535 if (lastmatch == NULL)
13536 rettv->vval.v_number = -1;
13537 else
13538 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13539}
13540
13541/*
13542 * "strtrans()" function
13543 */
13544 static void
13545f_strtrans(typval_T *argvars, typval_T *rettv)
13546{
13547 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013548 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013549}
13550
13551/*
13552 * "submatch()" function
13553 */
13554 static void
13555f_submatch(typval_T *argvars, typval_T *rettv)
13556{
13557 int error = FALSE;
13558 int no;
13559 int retList = 0;
13560
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013561 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013562 if (error)
13563 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013564 if (no < 0 || no >= NSUBEXP)
13565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013566 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013567 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013568 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013569 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013570 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571 if (error)
13572 return;
13573
13574 if (retList == 0)
13575 {
13576 rettv->v_type = VAR_STRING;
13577 rettv->vval.v_string = reg_submatch(no);
13578 }
13579 else
13580 {
13581 rettv->v_type = VAR_LIST;
13582 rettv->vval.v_list = reg_submatch_list(no);
13583 }
13584}
13585
13586/*
13587 * "substitute()" function
13588 */
13589 static void
13590f_substitute(typval_T *argvars, typval_T *rettv)
13591{
13592 char_u patbuf[NUMBUFLEN];
13593 char_u subbuf[NUMBUFLEN];
13594 char_u flagsbuf[NUMBUFLEN];
13595
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013596 char_u *str = tv_get_string_chk(&argvars[0]);
13597 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013598 char_u *sub = NULL;
13599 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013600 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013601
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013602 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13603 expr = &argvars[2];
13604 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013605 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013607 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013608 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13609 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013610 rettv->vval.v_string = NULL;
13611 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013612 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613}
13614
13615/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013616 * "swapinfo(swap_filename)" function
13617 */
13618 static void
13619f_swapinfo(typval_T *argvars, typval_T *rettv)
13620{
13621 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013622 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013623}
13624
13625/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013626 * "swapname(expr)" function
13627 */
13628 static void
13629f_swapname(typval_T *argvars, typval_T *rettv)
13630{
13631 buf_T *buf;
13632
13633 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013634 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013635 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13636 || buf->b_ml.ml_mfp->mf_fname == NULL)
13637 rettv->vval.v_string = NULL;
13638 else
13639 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13640}
13641
13642/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013643 * "synID(lnum, col, trans)" function
13644 */
13645 static void
13646f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13647{
13648 int id = 0;
13649#ifdef FEAT_SYN_HL
13650 linenr_T lnum;
13651 colnr_T col;
13652 int trans;
13653 int transerr = FALSE;
13654
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013655 lnum = tv_get_lnum(argvars); /* -1 on type error */
13656 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13657 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013658
13659 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13660 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13661 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13662#endif
13663
13664 rettv->vval.v_number = id;
13665}
13666
13667/*
13668 * "synIDattr(id, what [, mode])" function
13669 */
13670 static void
13671f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13672{
13673 char_u *p = NULL;
13674#ifdef FEAT_SYN_HL
13675 int id;
13676 char_u *what;
13677 char_u *mode;
13678 char_u modebuf[NUMBUFLEN];
13679 int modec;
13680
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013681 id = (int)tv_get_number(&argvars[0]);
13682 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013683 if (argvars[2].v_type != VAR_UNKNOWN)
13684 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013685 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013686 modec = TOLOWER_ASC(mode[0]);
13687 if (modec != 't' && modec != 'c' && modec != 'g')
13688 modec = 0; /* replace invalid with current */
13689 }
13690 else
13691 {
13692#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13693 if (USE_24BIT)
13694 modec = 'g';
13695 else
13696#endif
13697 if (t_colors > 1)
13698 modec = 'c';
13699 else
13700 modec = 't';
13701 }
13702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013703 switch (TOLOWER_ASC(what[0]))
13704 {
13705 case 'b':
13706 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13707 p = highlight_color(id, what, modec);
13708 else /* bold */
13709 p = highlight_has_attr(id, HL_BOLD, modec);
13710 break;
13711
13712 case 'f': /* fg[#] or font */
13713 p = highlight_color(id, what, modec);
13714 break;
13715
13716 case 'i':
13717 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13718 p = highlight_has_attr(id, HL_INVERSE, modec);
13719 else /* italic */
13720 p = highlight_has_attr(id, HL_ITALIC, modec);
13721 break;
13722
13723 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013724 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013725 break;
13726
13727 case 'r': /* reverse */
13728 p = highlight_has_attr(id, HL_INVERSE, modec);
13729 break;
13730
13731 case 's':
13732 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13733 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013734 /* strikeout */
13735 else if (TOLOWER_ASC(what[1]) == 't' &&
13736 TOLOWER_ASC(what[2]) == 'r')
13737 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013738 else /* standout */
13739 p = highlight_has_attr(id, HL_STANDOUT, modec);
13740 break;
13741
13742 case 'u':
13743 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13744 /* underline */
13745 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13746 else
13747 /* undercurl */
13748 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13749 break;
13750 }
13751
13752 if (p != NULL)
13753 p = vim_strsave(p);
13754#endif
13755 rettv->v_type = VAR_STRING;
13756 rettv->vval.v_string = p;
13757}
13758
13759/*
13760 * "synIDtrans(id)" function
13761 */
13762 static void
13763f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13764{
13765 int id;
13766
13767#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013768 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013769
13770 if (id > 0)
13771 id = syn_get_final_id(id);
13772 else
13773#endif
13774 id = 0;
13775
13776 rettv->vval.v_number = id;
13777}
13778
13779/*
13780 * "synconcealed(lnum, col)" function
13781 */
13782 static void
13783f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13784{
13785#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13786 linenr_T lnum;
13787 colnr_T col;
13788 int syntax_flags = 0;
13789 int cchar;
13790 int matchid = 0;
13791 char_u str[NUMBUFLEN];
13792#endif
13793
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013794 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013795
13796#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013797 lnum = tv_get_lnum(argvars); /* -1 on type error */
13798 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013799
13800 vim_memset(str, NUL, sizeof(str));
13801
13802 if (rettv_list_alloc(rettv) != FAIL)
13803 {
13804 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13805 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13806 && curwin->w_p_cole > 0)
13807 {
13808 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13809 syntax_flags = get_syntax_info(&matchid);
13810
13811 /* get the conceal character */
13812 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13813 {
13814 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013815 if (cchar == NUL && curwin->w_p_cole == 1)
13816 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013817 if (cchar != NUL)
13818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013819 if (has_mbyte)
13820 (*mb_char2bytes)(cchar, str);
13821 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013822 str[0] = cchar;
13823 }
13824 }
13825 }
13826
13827 list_append_number(rettv->vval.v_list,
13828 (syntax_flags & HL_CONCEAL) != 0);
13829 /* -1 to auto-determine strlen */
13830 list_append_string(rettv->vval.v_list, str, -1);
13831 list_append_number(rettv->vval.v_list, matchid);
13832 }
13833#endif
13834}
13835
13836/*
13837 * "synstack(lnum, col)" function
13838 */
13839 static void
13840f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13841{
13842#ifdef FEAT_SYN_HL
13843 linenr_T lnum;
13844 colnr_T col;
13845 int i;
13846 int id;
13847#endif
13848
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013849 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013850
13851#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013852 lnum = tv_get_lnum(argvars); /* -1 on type error */
13853 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013854
13855 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13856 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13857 && rettv_list_alloc(rettv) != FAIL)
13858 {
13859 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13860 for (i = 0; ; ++i)
13861 {
13862 id = syn_get_stack_item(i);
13863 if (id < 0)
13864 break;
13865 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13866 break;
13867 }
13868 }
13869#endif
13870}
13871
13872 static void
13873get_cmd_output_as_rettv(
13874 typval_T *argvars,
13875 typval_T *rettv,
13876 int retlist)
13877{
13878 char_u *res = NULL;
13879 char_u *p;
13880 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881 int err = FALSE;
13882 FILE *fd;
13883 list_T *list = NULL;
13884 int flags = SHELL_SILENT;
13885
13886 rettv->v_type = VAR_STRING;
13887 rettv->vval.v_string = NULL;
13888 if (check_restricted() || check_secure())
13889 goto errret;
13890
13891 if (argvars[1].v_type != VAR_UNKNOWN)
13892 {
13893 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013894 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013895 * command.
13896 */
13897 if ((infile = vim_tempname('i', TRUE)) == NULL)
13898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013899 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013900 goto errret;
13901 }
13902
13903 fd = mch_fopen((char *)infile, WRITEBIN);
13904 if (fd == NULL)
13905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013906 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013907 goto errret;
13908 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013909 if (argvars[1].v_type == VAR_NUMBER)
13910 {
13911 linenr_T lnum;
13912 buf_T *buf;
13913
13914 buf = buflist_findnr(argvars[1].vval.v_number);
13915 if (buf == NULL)
13916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013917 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013918 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013919 goto errret;
13920 }
13921
13922 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13923 {
13924 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13925 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13926 {
13927 err = TRUE;
13928 break;
13929 }
13930 if (putc(NL, fd) == EOF)
13931 {
13932 err = TRUE;
13933 break;
13934 }
13935 }
13936 }
13937 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013938 {
13939 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13940 err = TRUE;
13941 }
13942 else
13943 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013944 size_t len;
13945 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013946
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013947 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013948 if (p == NULL)
13949 {
13950 fclose(fd);
13951 goto errret; /* type error; errmsg already given */
13952 }
13953 len = STRLEN(p);
13954 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13955 err = TRUE;
13956 }
13957 if (fclose(fd) != 0)
13958 err = TRUE;
13959 if (err)
13960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013961 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013962 goto errret;
13963 }
13964 }
13965
13966 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13967 * echoes typeahead, that messes up the display. */
13968 if (!msg_silent)
13969 flags += SHELL_COOKED;
13970
13971 if (retlist)
13972 {
13973 int len;
13974 listitem_T *li;
13975 char_u *s = NULL;
13976 char_u *start;
13977 char_u *end;
13978 int i;
13979
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013980 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013981 if (res == NULL)
13982 goto errret;
13983
13984 list = list_alloc();
13985 if (list == NULL)
13986 goto errret;
13987
13988 for (i = 0; i < len; ++i)
13989 {
13990 start = res + i;
13991 while (i < len && res[i] != NL)
13992 ++i;
13993 end = res + i;
13994
Bram Moolenaar964b3742019-05-24 18:54:09 +020013995 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013996 if (s == NULL)
13997 goto errret;
13998
13999 for (p = s; start < end; ++p, ++start)
14000 *p = *start == NUL ? NL : *start;
14001 *p = NUL;
14002
14003 li = listitem_alloc();
14004 if (li == NULL)
14005 {
14006 vim_free(s);
14007 goto errret;
14008 }
14009 li->li_tv.v_type = VAR_STRING;
14010 li->li_tv.v_lock = 0;
14011 li->li_tv.vval.v_string = s;
14012 list_append(list, li);
14013 }
14014
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014015 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014016 list = NULL;
14017 }
14018 else
14019 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014020 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014021#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014022 /* translate <CR><NL> into <NL> */
14023 if (res != NULL)
14024 {
14025 char_u *s, *d;
14026
14027 d = res;
14028 for (s = res; *s; ++s)
14029 {
14030 if (s[0] == CAR && s[1] == NL)
14031 ++s;
14032 *d++ = *s;
14033 }
14034 *d = NUL;
14035 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014036#endif
14037 rettv->vval.v_string = res;
14038 res = NULL;
14039 }
14040
14041errret:
14042 if (infile != NULL)
14043 {
14044 mch_remove(infile);
14045 vim_free(infile);
14046 }
14047 if (res != NULL)
14048 vim_free(res);
14049 if (list != NULL)
14050 list_free(list);
14051}
14052
14053/*
14054 * "system()" function
14055 */
14056 static void
14057f_system(typval_T *argvars, typval_T *rettv)
14058{
14059 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14060}
14061
14062/*
14063 * "systemlist()" function
14064 */
14065 static void
14066f_systemlist(typval_T *argvars, typval_T *rettv)
14067{
14068 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14069}
14070
14071/*
14072 * "tabpagebuflist()" function
14073 */
14074 static void
14075f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14076{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014077 tabpage_T *tp;
14078 win_T *wp = NULL;
14079
14080 if (argvars[0].v_type == VAR_UNKNOWN)
14081 wp = firstwin;
14082 else
14083 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014084 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014085 if (tp != NULL)
14086 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14087 }
14088 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14089 {
14090 for (; wp != NULL; wp = wp->w_next)
14091 if (list_append_number(rettv->vval.v_list,
14092 wp->w_buffer->b_fnum) == FAIL)
14093 break;
14094 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095}
14096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014097/*
14098 * "tabpagenr()" function
14099 */
14100 static void
14101f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14102{
14103 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104 char_u *arg;
14105
14106 if (argvars[0].v_type != VAR_UNKNOWN)
14107 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014108 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109 nr = 0;
14110 if (arg != NULL)
14111 {
14112 if (STRCMP(arg, "$") == 0)
14113 nr = tabpage_index(NULL) - 1;
14114 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014115 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116 }
14117 }
14118 else
14119 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014120 rettv->vval.v_number = nr;
14121}
14122
14123
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014124/*
14125 * Common code for tabpagewinnr() and winnr().
14126 */
14127 static int
14128get_winnr(tabpage_T *tp, typval_T *argvar)
14129{
14130 win_T *twin;
14131 int nr = 1;
14132 win_T *wp;
14133 char_u *arg;
14134
14135 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14136 if (argvar->v_type != VAR_UNKNOWN)
14137 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014138 int invalid_arg = FALSE;
14139
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014140 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 if (arg == NULL)
14142 nr = 0; /* type error; errmsg already given */
14143 else if (STRCMP(arg, "$") == 0)
14144 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14145 else if (STRCMP(arg, "#") == 0)
14146 {
14147 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14148 if (twin == NULL)
14149 nr = 0;
14150 }
14151 else
14152 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014153 long count;
14154 char_u *endp;
14155
14156 // Extract the window count (if specified). e.g. winnr('3j')
14157 count = strtol((char *)arg, (char **)&endp, 10);
14158 if (count <= 0)
14159 count = 1; // if count is not specified, default to 1
14160 if (endp != NULL && *endp != '\0')
14161 {
14162 if (STRCMP(endp, "j") == 0)
14163 twin = win_vert_neighbor(tp, twin, FALSE, count);
14164 else if (STRCMP(endp, "k") == 0)
14165 twin = win_vert_neighbor(tp, twin, TRUE, count);
14166 else if (STRCMP(endp, "h") == 0)
14167 twin = win_horz_neighbor(tp, twin, TRUE, count);
14168 else if (STRCMP(endp, "l") == 0)
14169 twin = win_horz_neighbor(tp, twin, FALSE, count);
14170 else
14171 invalid_arg = TRUE;
14172 }
14173 else
14174 invalid_arg = TRUE;
14175 }
14176
14177 if (invalid_arg)
14178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014179 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014180 nr = 0;
14181 }
14182 }
14183
14184 if (nr > 0)
14185 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14186 wp != twin; wp = wp->w_next)
14187 {
14188 if (wp == NULL)
14189 {
14190 /* didn't find it in this tabpage */
14191 nr = 0;
14192 break;
14193 }
14194 ++nr;
14195 }
14196 return nr;
14197}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014198
14199/*
14200 * "tabpagewinnr()" function
14201 */
14202 static void
14203f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14204{
14205 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014206 tabpage_T *tp;
14207
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014208 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014209 if (tp == NULL)
14210 nr = 0;
14211 else
14212 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014213 rettv->vval.v_number = nr;
14214}
14215
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216/*
14217 * "tagfiles()" function
14218 */
14219 static void
14220f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14221{
14222 char_u *fname;
14223 tagname_T tn;
14224 int first;
14225
14226 if (rettv_list_alloc(rettv) == FAIL)
14227 return;
14228 fname = alloc(MAXPATHL);
14229 if (fname == NULL)
14230 return;
14231
14232 for (first = TRUE; ; first = FALSE)
14233 if (get_tagfname(&tn, first, fname) == FAIL
14234 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14235 break;
14236 tagname_free(&tn);
14237 vim_free(fname);
14238}
14239
14240/*
14241 * "taglist()" function
14242 */
14243 static void
14244f_taglist(typval_T *argvars, typval_T *rettv)
14245{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014246 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014247 char_u *tag_pattern;
14248
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014249 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014250
14251 rettv->vval.v_number = FALSE;
14252 if (*tag_pattern == NUL)
14253 return;
14254
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014255 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014256 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014257 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014258 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014259}
14260
14261/*
14262 * "tempname()" function
14263 */
14264 static void
14265f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14266{
14267 static int x = 'A';
14268
14269 rettv->v_type = VAR_STRING;
14270 rettv->vval.v_string = vim_tempname(x, FALSE);
14271
14272 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14273 * names. Skip 'I' and 'O', they are used for shell redirection. */
14274 do
14275 {
14276 if (x == 'Z')
14277 x = '0';
14278 else if (x == '9')
14279 x = 'A';
14280 else
14281 {
14282#ifdef EBCDIC
14283 if (x == 'I')
14284 x = 'J';
14285 else if (x == 'R')
14286 x = 'S';
14287 else
14288#endif
14289 ++x;
14290 }
14291 } while (x == 'I' || x == 'O');
14292}
14293
14294#ifdef FEAT_FLOAT
14295/*
14296 * "tan()" function
14297 */
14298 static void
14299f_tan(typval_T *argvars, typval_T *rettv)
14300{
14301 float_T f = 0.0;
14302
14303 rettv->v_type = VAR_FLOAT;
14304 if (get_float_arg(argvars, &f) == OK)
14305 rettv->vval.v_float = tan(f);
14306 else
14307 rettv->vval.v_float = 0.0;
14308}
14309
14310/*
14311 * "tanh()" function
14312 */
14313 static void
14314f_tanh(typval_T *argvars, typval_T *rettv)
14315{
14316 float_T f = 0.0;
14317
14318 rettv->v_type = VAR_FLOAT;
14319 if (get_float_arg(argvars, &f) == OK)
14320 rettv->vval.v_float = tanh(f);
14321 else
14322 rettv->vval.v_float = 0.0;
14323}
14324#endif
14325
14326/*
14327 * "test_alloc_fail(id, countdown, repeat)" function
14328 */
14329 static void
14330f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14331{
14332 if (argvars[0].v_type != VAR_NUMBER
14333 || argvars[0].vval.v_number <= 0
14334 || argvars[1].v_type != VAR_NUMBER
14335 || argvars[1].vval.v_number < 0
14336 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014337 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014338 else
14339 {
14340 alloc_fail_id = argvars[0].vval.v_number;
14341 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014342 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014343 alloc_fail_countdown = argvars[1].vval.v_number;
14344 alloc_fail_repeat = argvars[2].vval.v_number;
14345 did_outofmem_msg = FALSE;
14346 }
14347}
14348
14349/*
14350 * "test_autochdir()"
14351 */
14352 static void
14353f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14354{
14355#if defined(FEAT_AUTOCHDIR)
14356 test_autochdir = TRUE;
14357#endif
14358}
14359
14360/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014361 * "test_feedinput()"
14362 */
14363 static void
14364f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14365{
14366#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014367 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014368
14369 if (val != NULL)
14370 {
14371 trash_input_buf();
14372 add_to_input_buf_csi(val, (int)STRLEN(val));
14373 }
14374#endif
14375}
14376
14377/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014378 * "test_getvalue({name})" function
14379 */
14380 static void
14381f_test_getvalue(typval_T *argvars, typval_T *rettv)
14382{
14383 if (argvars[0].v_type != VAR_STRING)
14384 emsg(_(e_invarg));
14385 else
14386 {
14387 char_u *name = tv_get_string(&argvars[0]);
14388
14389 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14390 rettv->vval.v_number = need_fileinfo;
14391 else
14392 semsg(_(e_invarg2), name);
14393 }
14394}
14395
14396/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014397 * "test_option_not_set({name})" function
14398 */
14399 static void
14400f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14401{
14402 char_u *name = (char_u *)"";
14403
14404 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014405 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014406 else
14407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014408 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014409 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014410 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014411 }
14412}
14413
14414/*
14415 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014416 */
14417 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014418f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014419{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014420 char_u *name = (char_u *)"";
14421 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014422 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014423
14424 if (argvars[0].v_type != VAR_STRING
14425 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014426 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014427 else
14428 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014429 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014430 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014431
14432 if (STRCMP(name, (char_u *)"redraw") == 0)
14433 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014434 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14435 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014436 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14437 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014438 else if (STRCMP(name, (char_u *)"starting") == 0)
14439 {
14440 if (val)
14441 {
14442 if (save_starting < 0)
14443 save_starting = starting;
14444 starting = 0;
14445 }
14446 else
14447 {
14448 starting = save_starting;
14449 save_starting = -1;
14450 }
14451 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014452 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14453 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014454 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14455 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014456 else if (STRCMP(name, (char_u *)"ALL") == 0)
14457 {
14458 disable_char_avail_for_testing = FALSE;
14459 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014460 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014461 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014462 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014463 if (save_starting >= 0)
14464 {
14465 starting = save_starting;
14466 save_starting = -1;
14467 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014468 }
14469 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014470 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014471 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014472}
14473
14474/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014475 * "test_refcount({expr})" function
14476 */
14477 static void
14478f_test_refcount(typval_T *argvars, typval_T *rettv)
14479{
14480 int retval = -1;
14481
14482 switch (argvars[0].v_type)
14483 {
14484 case VAR_UNKNOWN:
14485 case VAR_NUMBER:
14486 case VAR_FLOAT:
14487 case VAR_SPECIAL:
14488 case VAR_STRING:
14489 break;
14490 case VAR_JOB:
14491#ifdef FEAT_JOB_CHANNEL
14492 if (argvars[0].vval.v_job != NULL)
14493 retval = argvars[0].vval.v_job->jv_refcount - 1;
14494#endif
14495 break;
14496 case VAR_CHANNEL:
14497#ifdef FEAT_JOB_CHANNEL
14498 if (argvars[0].vval.v_channel != NULL)
14499 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14500#endif
14501 break;
14502 case VAR_FUNC:
14503 if (argvars[0].vval.v_string != NULL)
14504 {
14505 ufunc_T *fp;
14506
14507 fp = find_func(argvars[0].vval.v_string);
14508 if (fp != NULL)
14509 retval = fp->uf_refcount;
14510 }
14511 break;
14512 case VAR_PARTIAL:
14513 if (argvars[0].vval.v_partial != NULL)
14514 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14515 break;
14516 case VAR_BLOB:
14517 if (argvars[0].vval.v_blob != NULL)
14518 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14519 break;
14520 case VAR_LIST:
14521 if (argvars[0].vval.v_list != NULL)
14522 retval = argvars[0].vval.v_list->lv_refcount - 1;
14523 break;
14524 case VAR_DICT:
14525 if (argvars[0].vval.v_dict != NULL)
14526 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14527 break;
14528 }
14529
14530 rettv->v_type = VAR_NUMBER;
14531 rettv->vval.v_number = retval;
14532
14533}
14534
14535/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014536 * "test_garbagecollect_now()" function
14537 */
14538 static void
14539f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14540{
14541 /* This is dangerous, any Lists and Dicts used internally may be freed
14542 * while still in use. */
14543 garbage_collect(TRUE);
14544}
14545
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014546/*
14547 * "test_ignore_error()" function
14548 */
14549 static void
14550f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14551{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014552 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014553}
14554
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014555 static void
14556f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14557{
14558 rettv->v_type = VAR_BLOB;
14559 rettv->vval.v_blob = NULL;
14560}
14561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014562#ifdef FEAT_JOB_CHANNEL
14563 static void
14564f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14565{
14566 rettv->v_type = VAR_CHANNEL;
14567 rettv->vval.v_channel = NULL;
14568}
14569#endif
14570
14571 static void
14572f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14573{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014574 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575}
14576
14577#ifdef FEAT_JOB_CHANNEL
14578 static void
14579f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14580{
14581 rettv->v_type = VAR_JOB;
14582 rettv->vval.v_job = NULL;
14583}
14584#endif
14585
14586 static void
14587f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14588{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014589 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590}
14591
14592 static void
14593f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14594{
14595 rettv->v_type = VAR_PARTIAL;
14596 rettv->vval.v_partial = NULL;
14597}
14598
14599 static void
14600f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14601{
14602 rettv->v_type = VAR_STRING;
14603 rettv->vval.v_string = NULL;
14604}
14605
Bram Moolenaarab186732018-09-14 21:27:06 +020014606#ifdef FEAT_GUI
14607 static void
14608f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14609{
14610 char_u *which;
14611 long value;
14612 int dragging;
14613 scrollbar_T *sb = NULL;
14614
14615 if (argvars[0].v_type != VAR_STRING
14616 || (argvars[1].v_type) != VAR_NUMBER
14617 || (argvars[2].v_type) != VAR_NUMBER)
14618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014619 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014620 return;
14621 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014622 which = tv_get_string(&argvars[0]);
14623 value = tv_get_number(&argvars[1]);
14624 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014625
14626 if (STRCMP(which, "left") == 0)
14627 sb = &curwin->w_scrollbars[SBAR_LEFT];
14628 else if (STRCMP(which, "right") == 0)
14629 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14630 else if (STRCMP(which, "hor") == 0)
14631 sb = &gui.bottom_sbar;
14632 if (sb == NULL)
14633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014634 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014635 return;
14636 }
14637 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014638# ifndef USE_ON_FLY_SCROLL
14639 // need to loop through normal_cmd() to handle the scroll events
14640 exec_normal(FALSE, TRUE, FALSE);
14641# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014642}
14643#endif
14644
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014645#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014646 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014647f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14648{
14649 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14650 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14651}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014652#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014653
14654 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14656{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014657 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658}
14659
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014660/*
14661 * Get a callback from "arg". It can be a Funcref or a function name.
14662 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014663 * "cb_name" is not allocated.
14664 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014666 callback_T
14667get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014669 callback_T res;
14670
14671 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14673 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014674 res.cb_partial = arg->vval.v_partial;
14675 ++res.cb_partial->pt_refcount;
14676 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014677 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014678 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014680 res.cb_partial = NULL;
14681 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14682 {
14683 // Note that we don't make a copy of the string.
14684 res.cb_name = arg->vval.v_string;
14685 func_ref(res.cb_name);
14686 }
14687 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14688 {
14689 res.cb_name = (char_u *)"";
14690 }
14691 else
14692 {
14693 emsg(_("E921: Invalid callback argument"));
14694 res.cb_name = NULL;
14695 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014696 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014697 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014698}
14699
14700/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014701 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 */
14703 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014704put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014705{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014706 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014708 tv->v_type = VAR_PARTIAL;
14709 tv->vval.v_partial = cb->cb_partial;
14710 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014711 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014712 else
14713 {
14714 tv->v_type = VAR_FUNC;
14715 tv->vval.v_string = vim_strsave(cb->cb_name);
14716 func_ref(cb->cb_name);
14717 }
14718}
14719
14720/*
14721 * Make a copy of "src" into "dest", allocating the function name if needed,
14722 * without incrementing the refcount.
14723 */
14724 void
14725set_callback(callback_T *dest, callback_T *src)
14726{
14727 if (src->cb_partial == NULL)
14728 {
14729 // just a function name, make a copy
14730 dest->cb_name = vim_strsave(src->cb_name);
14731 dest->cb_free_name = TRUE;
14732 }
14733 else
14734 {
14735 // cb_name is a pointer into cb_partial
14736 dest->cb_name = src->cb_name;
14737 dest->cb_free_name = FALSE;
14738 }
14739 dest->cb_partial = src->cb_partial;
14740}
14741
14742/*
14743 * Unref/free "callback" returned by get_callback() or set_callback().
14744 */
14745 void
14746free_callback(callback_T *callback)
14747{
14748 if (callback->cb_partial != NULL)
14749 {
14750 partial_unref(callback->cb_partial);
14751 callback->cb_partial = NULL;
14752 }
14753 else if (callback->cb_name != NULL)
14754 func_unref(callback->cb_name);
14755 if (callback->cb_free_name)
14756 {
14757 vim_free(callback->cb_name);
14758 callback->cb_free_name = FALSE;
14759 }
14760 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762
14763#ifdef FEAT_TIMERS
14764/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014765 * "timer_info([timer])" function
14766 */
14767 static void
14768f_timer_info(typval_T *argvars, typval_T *rettv)
14769{
14770 timer_T *timer = NULL;
14771
14772 if (rettv_list_alloc(rettv) != OK)
14773 return;
14774 if (argvars[0].v_type != VAR_UNKNOWN)
14775 {
14776 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014777 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014778 else
14779 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014780 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014781 if (timer != NULL)
14782 add_timer_info(rettv, timer);
14783 }
14784 }
14785 else
14786 add_timer_info_all(rettv);
14787}
14788
14789/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014790 * "timer_pause(timer, paused)" function
14791 */
14792 static void
14793f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14794{
14795 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014796 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014797
14798 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014799 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014800 else
14801 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014802 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014803 if (timer != NULL)
14804 timer->tr_paused = paused;
14805 }
14806}
14807
14808/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 * "timer_start(time, callback [, options])" function
14810 */
14811 static void
14812f_timer_start(typval_T *argvars, typval_T *rettv)
14813{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014814 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014815 timer_T *timer;
14816 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014817 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014818 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014819
Bram Moolenaar75537a92016-09-05 22:45:28 +020014820 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 if (check_secure())
14822 return;
14823 if (argvars[2].v_type != VAR_UNKNOWN)
14824 {
14825 if (argvars[2].v_type != VAR_DICT
14826 || (dict = argvars[2].vval.v_dict) == NULL)
14827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014828 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014829 return;
14830 }
14831 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014832 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014833 }
14834
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014835 callback = get_callback(&argvars[1]);
14836 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014837 return;
14838
14839 timer = create_timer(msec, repeat);
14840 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014841 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014842 else
14843 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014844 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014845 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014846 }
14847}
14848
14849/*
14850 * "timer_stop(timer)" function
14851 */
14852 static void
14853f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14854{
14855 timer_T *timer;
14856
14857 if (argvars[0].v_type != VAR_NUMBER)
14858 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014859 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014860 return;
14861 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014862 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014863 if (timer != NULL)
14864 stop_timer(timer);
14865}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014866
14867/*
14868 * "timer_stopall()" function
14869 */
14870 static void
14871f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14872{
14873 stop_all_timers();
14874}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014875#endif
14876
14877/*
14878 * "tolower(string)" function
14879 */
14880 static void
14881f_tolower(typval_T *argvars, typval_T *rettv)
14882{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014883 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014884 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014885}
14886
14887/*
14888 * "toupper(string)" function
14889 */
14890 static void
14891f_toupper(typval_T *argvars, typval_T *rettv)
14892{
14893 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014894 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014895}
14896
14897/*
14898 * "tr(string, fromstr, tostr)" function
14899 */
14900 static void
14901f_tr(typval_T *argvars, typval_T *rettv)
14902{
14903 char_u *in_str;
14904 char_u *fromstr;
14905 char_u *tostr;
14906 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014907 int inlen;
14908 int fromlen;
14909 int tolen;
14910 int idx;
14911 char_u *cpstr;
14912 int cplen;
14913 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014914 char_u buf[NUMBUFLEN];
14915 char_u buf2[NUMBUFLEN];
14916 garray_T ga;
14917
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014918 in_str = tv_get_string(&argvars[0]);
14919 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14920 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014921
14922 /* Default return value: empty string. */
14923 rettv->v_type = VAR_STRING;
14924 rettv->vval.v_string = NULL;
14925 if (fromstr == NULL || tostr == NULL)
14926 return; /* type error; errmsg already given */
14927 ga_init2(&ga, (int)sizeof(char), 80);
14928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014929 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014930 /* not multi-byte: fromstr and tostr must be the same length */
14931 if (STRLEN(fromstr) != STRLEN(tostr))
14932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014933error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014934 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014935 ga_clear(&ga);
14936 return;
14937 }
14938
14939 /* fromstr and tostr have to contain the same number of chars */
14940 while (*in_str != NUL)
14941 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014942 if (has_mbyte)
14943 {
14944 inlen = (*mb_ptr2len)(in_str);
14945 cpstr = in_str;
14946 cplen = inlen;
14947 idx = 0;
14948 for (p = fromstr; *p != NUL; p += fromlen)
14949 {
14950 fromlen = (*mb_ptr2len)(p);
14951 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14952 {
14953 for (p = tostr; *p != NUL; p += tolen)
14954 {
14955 tolen = (*mb_ptr2len)(p);
14956 if (idx-- == 0)
14957 {
14958 cplen = tolen;
14959 cpstr = p;
14960 break;
14961 }
14962 }
14963 if (*p == NUL) /* tostr is shorter than fromstr */
14964 goto error;
14965 break;
14966 }
14967 ++idx;
14968 }
14969
14970 if (first && cpstr == in_str)
14971 {
14972 /* Check that fromstr and tostr have the same number of
14973 * (multi-byte) characters. Done only once when a character
14974 * of in_str doesn't appear in fromstr. */
14975 first = FALSE;
14976 for (p = tostr; *p != NUL; p += tolen)
14977 {
14978 tolen = (*mb_ptr2len)(p);
14979 --idx;
14980 }
14981 if (idx != 0)
14982 goto error;
14983 }
14984
14985 (void)ga_grow(&ga, cplen);
14986 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14987 ga.ga_len += cplen;
14988
14989 in_str += inlen;
14990 }
14991 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014992 {
14993 /* When not using multi-byte chars we can do it faster. */
14994 p = vim_strchr(fromstr, *in_str);
14995 if (p != NULL)
14996 ga_append(&ga, tostr[p - fromstr]);
14997 else
14998 ga_append(&ga, *in_str);
14999 ++in_str;
15000 }
15001 }
15002
15003 /* add a terminating NUL */
15004 (void)ga_grow(&ga, 1);
15005 ga_append(&ga, NUL);
15006
15007 rettv->vval.v_string = ga.ga_data;
15008}
15009
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015010/*
15011 * "trim({expr})" function
15012 */
15013 static void
15014f_trim(typval_T *argvars, typval_T *rettv)
15015{
15016 char_u buf1[NUMBUFLEN];
15017 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015018 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015019 char_u *mask = NULL;
15020 char_u *tail;
15021 char_u *prev;
15022 char_u *p;
15023 int c1;
15024
15025 rettv->v_type = VAR_STRING;
15026 if (head == NULL)
15027 {
15028 rettv->vval.v_string = NULL;
15029 return;
15030 }
15031
15032 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015033 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015034
15035 while (*head != NUL)
15036 {
15037 c1 = PTR2CHAR(head);
15038 if (mask == NULL)
15039 {
15040 if (c1 > ' ' && c1 != 0xa0)
15041 break;
15042 }
15043 else
15044 {
15045 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15046 if (c1 == PTR2CHAR(p))
15047 break;
15048 if (*p == NUL)
15049 break;
15050 }
15051 MB_PTR_ADV(head);
15052 }
15053
15054 for (tail = head + STRLEN(head); tail > head; tail = prev)
15055 {
15056 prev = tail;
15057 MB_PTR_BACK(head, prev);
15058 c1 = PTR2CHAR(prev);
15059 if (mask == NULL)
15060 {
15061 if (c1 > ' ' && c1 != 0xa0)
15062 break;
15063 }
15064 else
15065 {
15066 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15067 if (c1 == PTR2CHAR(p))
15068 break;
15069 if (*p == NUL)
15070 break;
15071 }
15072 }
15073 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15074}
15075
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015076#ifdef FEAT_FLOAT
15077/*
15078 * "trunc({float})" function
15079 */
15080 static void
15081f_trunc(typval_T *argvars, typval_T *rettv)
15082{
15083 float_T f = 0.0;
15084
15085 rettv->v_type = VAR_FLOAT;
15086 if (get_float_arg(argvars, &f) == OK)
15087 /* trunc() is not in C90, use floor() or ceil() instead. */
15088 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15089 else
15090 rettv->vval.v_float = 0.0;
15091}
15092#endif
15093
15094/*
15095 * "type(expr)" function
15096 */
15097 static void
15098f_type(typval_T *argvars, typval_T *rettv)
15099{
15100 int n = -1;
15101
15102 switch (argvars[0].v_type)
15103 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015104 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15105 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015106 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015107 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15108 case VAR_LIST: n = VAR_TYPE_LIST; break;
15109 case VAR_DICT: n = VAR_TYPE_DICT; break;
15110 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015111 case VAR_SPECIAL:
15112 if (argvars[0].vval.v_number == VVAL_FALSE
15113 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015114 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015115 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015116 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015117 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015118 case VAR_JOB: n = VAR_TYPE_JOB; break;
15119 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015120 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015121 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015122 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015123 n = -1;
15124 break;
15125 }
15126 rettv->vval.v_number = n;
15127}
15128
15129/*
15130 * "undofile(name)" function
15131 */
15132 static void
15133f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15134{
15135 rettv->v_type = VAR_STRING;
15136#ifdef FEAT_PERSISTENT_UNDO
15137 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015138 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015139
15140 if (*fname == NUL)
15141 {
15142 /* If there is no file name there will be no undo file. */
15143 rettv->vval.v_string = NULL;
15144 }
15145 else
15146 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015147 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015148
15149 if (ffname != NULL)
15150 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15151 vim_free(ffname);
15152 }
15153 }
15154#else
15155 rettv->vval.v_string = NULL;
15156#endif
15157}
15158
15159/*
15160 * "undotree()" function
15161 */
15162 static void
15163f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15164{
15165 if (rettv_dict_alloc(rettv) == OK)
15166 {
15167 dict_T *dict = rettv->vval.v_dict;
15168 list_T *list;
15169
Bram Moolenaare0be1672018-07-08 16:50:37 +020015170 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15171 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15172 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15173 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15174 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15175 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015176
15177 list = list_alloc();
15178 if (list != NULL)
15179 {
15180 u_eval_tree(curbuf->b_u_oldhead, list);
15181 dict_add_list(dict, "entries", list);
15182 }
15183 }
15184}
15185
15186/*
15187 * "values(dict)" function
15188 */
15189 static void
15190f_values(typval_T *argvars, typval_T *rettv)
15191{
15192 dict_list(argvars, rettv, 1);
15193}
15194
15195/*
15196 * "virtcol(string)" function
15197 */
15198 static void
15199f_virtcol(typval_T *argvars, typval_T *rettv)
15200{
15201 colnr_T vcol = 0;
15202 pos_T *fp;
15203 int fnum = curbuf->b_fnum;
15204
15205 fp = var2fpos(&argvars[0], FALSE, &fnum);
15206 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15207 && fnum == curbuf->b_fnum)
15208 {
15209 getvvcol(curwin, fp, NULL, NULL, &vcol);
15210 ++vcol;
15211 }
15212
15213 rettv->vval.v_number = vcol;
15214}
15215
15216/*
15217 * "visualmode()" function
15218 */
15219 static void
15220f_visualmode(typval_T *argvars, typval_T *rettv)
15221{
15222 char_u str[2];
15223
15224 rettv->v_type = VAR_STRING;
15225 str[0] = curbuf->b_visual_mode_eval;
15226 str[1] = NUL;
15227 rettv->vval.v_string = vim_strsave(str);
15228
15229 /* A non-zero number or non-empty string argument: reset mode. */
15230 if (non_zero_arg(&argvars[0]))
15231 curbuf->b_visual_mode_eval = NUL;
15232}
15233
15234/*
15235 * "wildmenumode()" function
15236 */
15237 static void
15238f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15239{
15240#ifdef FEAT_WILDMENU
15241 if (wild_menu_showing)
15242 rettv->vval.v_number = 1;
15243#endif
15244}
15245
15246/*
15247 * "winbufnr(nr)" function
15248 */
15249 static void
15250f_winbufnr(typval_T *argvars, typval_T *rettv)
15251{
15252 win_T *wp;
15253
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015254 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015255 if (wp == NULL)
15256 rettv->vval.v_number = -1;
15257 else
15258 rettv->vval.v_number = wp->w_buffer->b_fnum;
15259}
15260
15261/*
15262 * "wincol()" function
15263 */
15264 static void
15265f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15266{
15267 validate_cursor();
15268 rettv->vval.v_number = curwin->w_wcol + 1;
15269}
15270
15271/*
15272 * "winheight(nr)" function
15273 */
15274 static void
15275f_winheight(typval_T *argvars, typval_T *rettv)
15276{
15277 win_T *wp;
15278
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015279 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015280 if (wp == NULL)
15281 rettv->vval.v_number = -1;
15282 else
15283 rettv->vval.v_number = wp->w_height;
15284}
15285
15286/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015287 * "winlayout()" function
15288 */
15289 static void
15290f_winlayout(typval_T *argvars, typval_T *rettv)
15291{
15292 tabpage_T *tp;
15293
15294 if (rettv_list_alloc(rettv) != OK)
15295 return;
15296
15297 if (argvars[0].v_type == VAR_UNKNOWN)
15298 tp = curtab;
15299 else
15300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015301 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015302 if (tp == NULL)
15303 return;
15304 }
15305
15306 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15307}
15308
15309/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015310 * "winline()" function
15311 */
15312 static void
15313f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15314{
15315 validate_cursor();
15316 rettv->vval.v_number = curwin->w_wrow + 1;
15317}
15318
15319/*
15320 * "winnr()" function
15321 */
15322 static void
15323f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15324{
15325 int nr = 1;
15326
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015328 rettv->vval.v_number = nr;
15329}
15330
15331/*
15332 * "winrestcmd()" function
15333 */
15334 static void
15335f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15336{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015337 win_T *wp;
15338 int winnr = 1;
15339 garray_T ga;
15340 char_u buf[50];
15341
15342 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015343 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015344 {
15345 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15346 ga_concat(&ga, buf);
15347 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15348 ga_concat(&ga, buf);
15349 ++winnr;
15350 }
15351 ga_append(&ga, NUL);
15352
15353 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015354 rettv->v_type = VAR_STRING;
15355}
15356
15357/*
15358 * "winrestview()" function
15359 */
15360 static void
15361f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15362{
15363 dict_T *dict;
15364
15365 if (argvars[0].v_type != VAR_DICT
15366 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015367 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015368 else
15369 {
15370 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015371 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015372 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015373 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015374 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015375 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015376 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15377 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015378 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015379 curwin->w_set_curswant = FALSE;
15380 }
15381
15382 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015383 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015384#ifdef FEAT_DIFF
15385 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015386 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015387#endif
15388 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015389 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015390 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015391 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015392
15393 check_cursor();
15394 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015395 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015396 changed_window_setting();
15397
15398 if (curwin->w_topline <= 0)
15399 curwin->w_topline = 1;
15400 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15401 curwin->w_topline = curbuf->b_ml.ml_line_count;
15402#ifdef FEAT_DIFF
15403 check_topfill(curwin, TRUE);
15404#endif
15405 }
15406}
15407
15408/*
15409 * "winsaveview()" function
15410 */
15411 static void
15412f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15413{
15414 dict_T *dict;
15415
15416 if (rettv_dict_alloc(rettv) == FAIL)
15417 return;
15418 dict = rettv->vval.v_dict;
15419
Bram Moolenaare0be1672018-07-08 16:50:37 +020015420 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15421 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015422 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015423 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015424 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015425
Bram Moolenaare0be1672018-07-08 16:50:37 +020015426 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015427#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015428 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015429#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015430 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15431 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015432}
15433
15434/*
15435 * "winwidth(nr)" function
15436 */
15437 static void
15438f_winwidth(typval_T *argvars, typval_T *rettv)
15439{
15440 win_T *wp;
15441
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015442 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015443 if (wp == NULL)
15444 rettv->vval.v_number = -1;
15445 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015446 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015447}
15448
15449/*
15450 * "wordcount()" function
15451 */
15452 static void
15453f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15454{
15455 if (rettv_dict_alloc(rettv) == FAIL)
15456 return;
15457 cursor_pos_info(rettv->vval.v_dict);
15458}
15459
15460/*
15461 * "writefile()" function
15462 */
15463 static void
15464f_writefile(typval_T *argvars, typval_T *rettv)
15465{
15466 int binary = FALSE;
15467 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015468#ifdef HAVE_FSYNC
15469 int do_fsync = p_fs;
15470#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015471 char_u *fname;
15472 FILE *fd;
15473 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015474 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015475 list_T *list = NULL;
15476 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015477
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015478 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015479 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015480 return;
15481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015482 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015483 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015484 list = argvars[0].vval.v_list;
15485 if (list == NULL)
15486 return;
15487 for (li = list->lv_first; li != NULL; li = li->li_next)
15488 if (tv_get_string_chk(&li->li_tv) == NULL)
15489 return;
15490 }
15491 else if (argvars[0].v_type == VAR_BLOB)
15492 {
15493 blob = argvars[0].vval.v_blob;
15494 if (blob == NULL)
15495 return;
15496 }
15497 else
15498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015499 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015500 return;
15501 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015502
15503 if (argvars[2].v_type != VAR_UNKNOWN)
15504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015505 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015506
15507 if (arg2 == NULL)
15508 return;
15509 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015510 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015511 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015512 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015513#ifdef HAVE_FSYNC
15514 if (vim_strchr(arg2, 's') != NULL)
15515 do_fsync = TRUE;
15516 else if (vim_strchr(arg2, 'S') != NULL)
15517 do_fsync = FALSE;
15518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015519 }
15520
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015521 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015522 if (fname == NULL)
15523 return;
15524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015525 /* Always open the file in binary mode, library functions have a mind of
15526 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015527 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15528 append ? APPENDBIN : WRITEBIN)) == NULL)
15529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015530 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015531 ret = -1;
15532 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015533 else if (blob)
15534 {
15535 if (write_blob(fd, blob) == FAIL)
15536 ret = -1;
15537#ifdef HAVE_FSYNC
15538 else if (do_fsync)
15539 // Ignore the error, the user wouldn't know what to do about it.
15540 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015541 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015542#endif
15543 fclose(fd);
15544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015545 else
15546 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015547 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015548 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015549#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015550 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 Moolenaar7567d0b2017-11-16 23:04:15 +010015554#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015555 fclose(fd);
15556 }
15557
15558 rettv->vval.v_number = ret;
15559}
15560
15561/*
15562 * "xor(expr, expr)" function
15563 */
15564 static void
15565f_xor(typval_T *argvars, typval_T *rettv)
15566{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015567 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15568 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015569}
15570
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015571#endif /* FEAT_EVAL */