blob: 8c8bb4d6382e500f44fcc4d2b7d13f00c465ecae [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200152static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_extend(typval_T *argvars, typval_T *rettv);
154static void f_feedkeys(typval_T *argvars, typval_T *rettv);
155static void f_filereadable(typval_T *argvars, typval_T *rettv);
156static void f_filewritable(typval_T *argvars, typval_T *rettv);
157static void f_filter(typval_T *argvars, typval_T *rettv);
158static void f_finddir(typval_T *argvars, typval_T *rettv);
159static void f_findfile(typval_T *argvars, typval_T *rettv);
160#ifdef FEAT_FLOAT
161static void f_float2nr(typval_T *argvars, typval_T *rettv);
162static void f_floor(typval_T *argvars, typval_T *rettv);
163static void f_fmod(typval_T *argvars, typval_T *rettv);
164#endif
165static void f_fnameescape(typval_T *argvars, typval_T *rettv);
166static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
167static void f_foldclosed(typval_T *argvars, typval_T *rettv);
168static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
169static void f_foldlevel(typval_T *argvars, typval_T *rettv);
170static void f_foldtext(typval_T *argvars, typval_T *rettv);
171static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
172static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200173static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_function(typval_T *argvars, typval_T *rettv);
175static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
176static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200177static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getbufline(typval_T *argvars, typval_T *rettv);
179static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100180static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getchar(typval_T *argvars, typval_T *rettv);
182static void f_getcharmod(typval_T *argvars, typval_T *rettv);
183static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
184static void f_getcmdline(typval_T *argvars, typval_T *rettv);
185#if defined(FEAT_CMDL_COMPL)
186static void f_getcompletion(typval_T *argvars, typval_T *rettv);
187#endif
188static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
189static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
190static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
191static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getfontname(typval_T *argvars, typval_T *rettv);
194static void f_getfperm(typval_T *argvars, typval_T *rettv);
195static void f_getfsize(typval_T *argvars, typval_T *rettv);
196static void f_getftime(typval_T *argvars, typval_T *rettv);
197static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100198static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200200static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getmatches(typval_T *argvars, typval_T *rettv);
202static void f_getpid(typval_T *argvars, typval_T *rettv);
203static void f_getcurpos(typval_T *argvars, typval_T *rettv);
204static void f_getpos(typval_T *argvars, typval_T *rettv);
205static void f_getqflist(typval_T *argvars, typval_T *rettv);
206static void f_getreg(typval_T *argvars, typval_T *rettv);
207static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_gettabvar(typval_T *argvars, typval_T *rettv);
210static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100211static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200212static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100213static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_getwinposx(typval_T *argvars, typval_T *rettv);
215static void f_getwinposy(typval_T *argvars, typval_T *rettv);
216static void f_getwinvar(typval_T *argvars, typval_T *rettv);
217static void f_glob(typval_T *argvars, typval_T *rettv);
218static void f_globpath(typval_T *argvars, typval_T *rettv);
219static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
220static void f_has(typval_T *argvars, typval_T *rettv);
221static void f_has_key(typval_T *argvars, typval_T *rettv);
222static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
223static void f_hasmapto(typval_T *argvars, typval_T *rettv);
224static void f_histadd(typval_T *argvars, typval_T *rettv);
225static void f_histdel(typval_T *argvars, typval_T *rettv);
226static void f_histget(typval_T *argvars, typval_T *rettv);
227static void f_histnr(typval_T *argvars, typval_T *rettv);
228static void f_hlID(typval_T *argvars, typval_T *rettv);
229static void f_hlexists(typval_T *argvars, typval_T *rettv);
230static void f_hostname(typval_T *argvars, typval_T *rettv);
231static void f_iconv(typval_T *argvars, typval_T *rettv);
232static void f_indent(typval_T *argvars, typval_T *rettv);
233static void f_index(typval_T *argvars, typval_T *rettv);
234static void f_input(typval_T *argvars, typval_T *rettv);
235static void f_inputdialog(typval_T *argvars, typval_T *rettv);
236static void f_inputlist(typval_T *argvars, typval_T *rettv);
237static void f_inputrestore(typval_T *argvars, typval_T *rettv);
238static void f_inputsave(typval_T *argvars, typval_T *rettv);
239static void f_inputsecret(typval_T *argvars, typval_T *rettv);
240static void f_insert(typval_T *argvars, typval_T *rettv);
241static void f_invert(typval_T *argvars, typval_T *rettv);
242static void f_isdirectory(typval_T *argvars, typval_T *rettv);
243static void f_islocked(typval_T *argvars, typval_T *rettv);
244#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200245static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_isnan(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_items(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_JOB_CHANNEL
250static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
251static void f_job_info(typval_T *argvars, typval_T *rettv);
252static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
253static void f_job_start(typval_T *argvars, typval_T *rettv);
254static void f_job_stop(typval_T *argvars, typval_T *rettv);
255static void f_job_status(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_join(typval_T *argvars, typval_T *rettv);
258static void f_js_decode(typval_T *argvars, typval_T *rettv);
259static void f_js_encode(typval_T *argvars, typval_T *rettv);
260static void f_json_decode(typval_T *argvars, typval_T *rettv);
261static void f_json_encode(typval_T *argvars, typval_T *rettv);
262static void f_keys(typval_T *argvars, typval_T *rettv);
263static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
264static void f_len(typval_T *argvars, typval_T *rettv);
265static void f_libcall(typval_T *argvars, typval_T *rettv);
266static void f_libcallnr(typval_T *argvars, typval_T *rettv);
267static void f_line(typval_T *argvars, typval_T *rettv);
268static void f_line2byte(typval_T *argvars, typval_T *rettv);
269static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200270static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_localtime(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_log(typval_T *argvars, typval_T *rettv);
274static void f_log10(typval_T *argvars, typval_T *rettv);
275#endif
276#ifdef FEAT_LUA
277static void f_luaeval(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_map(typval_T *argvars, typval_T *rettv);
280static void f_maparg(typval_T *argvars, typval_T *rettv);
281static void f_mapcheck(typval_T *argvars, typval_T *rettv);
282static void f_match(typval_T *argvars, typval_T *rettv);
283static void f_matchadd(typval_T *argvars, typval_T *rettv);
284static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
285static void f_matcharg(typval_T *argvars, typval_T *rettv);
286static void f_matchdelete(typval_T *argvars, typval_T *rettv);
287static void f_matchend(typval_T *argvars, typval_T *rettv);
288static void f_matchlist(typval_T *argvars, typval_T *rettv);
289static void f_matchstr(typval_T *argvars, typval_T *rettv);
290static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
291static void f_max(typval_T *argvars, typval_T *rettv);
292static void f_min(typval_T *argvars, typval_T *rettv);
293#ifdef vim_mkdir
294static void f_mkdir(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_mode(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_MZSCHEME
298static void f_mzeval(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
301static void f_nr2char(typval_T *argvars, typval_T *rettv);
302static void f_or(typval_T *argvars, typval_T *rettv);
303static void f_pathshorten(typval_T *argvars, typval_T *rettv);
304#ifdef FEAT_PERL
305static void f_perleval(typval_T *argvars, typval_T *rettv);
306#endif
307#ifdef FEAT_FLOAT
308static void f_pow(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
311static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200312#ifdef FEAT_JOB_CHANNEL
313static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200314static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200315static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_pumvisible(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_PYTHON3
319static void f_py3eval(typval_T *argvars, typval_T *rettv);
320#endif
321#ifdef FEAT_PYTHON
322static void f_pyeval(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100324#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
325static void f_pyxeval(typval_T *argvars, typval_T *rettv);
326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200328static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200330static void f_reg_executing(typval_T *argvars, typval_T *rettv);
331static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_reltime(typval_T *argvars, typval_T *rettv);
333#ifdef FEAT_FLOAT
334static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
335#endif
336static void f_reltimestr(typval_T *argvars, typval_T *rettv);
337static void f_remote_expr(typval_T *argvars, typval_T *rettv);
338static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
339static void f_remote_peek(typval_T *argvars, typval_T *rettv);
340static void f_remote_read(typval_T *argvars, typval_T *rettv);
341static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100342static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_remove(typval_T *argvars, typval_T *rettv);
344static void f_rename(typval_T *argvars, typval_T *rettv);
345static void f_repeat(typval_T *argvars, typval_T *rettv);
346static void f_resolve(typval_T *argvars, typval_T *rettv);
347static void f_reverse(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_round(typval_T *argvars, typval_T *rettv);
350#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100351#ifdef FEAT_RUBY
352static void f_rubyeval(typval_T *argvars, typval_T *rettv);
353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_screenattr(typval_T *argvars, typval_T *rettv);
355static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100356static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_screencol(typval_T *argvars, typval_T *rettv);
358static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100359static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360static void f_search(typval_T *argvars, typval_T *rettv);
361static void f_searchdecl(typval_T *argvars, typval_T *rettv);
362static void f_searchpair(typval_T *argvars, typval_T *rettv);
363static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
364static void f_searchpos(typval_T *argvars, typval_T *rettv);
365static void f_server2client(typval_T *argvars, typval_T *rettv);
366static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200367static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_setbufvar(typval_T *argvars, typval_T *rettv);
369static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
370static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200371static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setfperm(typval_T *argvars, typval_T *rettv);
373static void f_setline(typval_T *argvars, typval_T *rettv);
374static void f_setloclist(typval_T *argvars, typval_T *rettv);
375static void f_setmatches(typval_T *argvars, typval_T *rettv);
376static void f_setpos(typval_T *argvars, typval_T *rettv);
377static void f_setqflist(typval_T *argvars, typval_T *rettv);
378static void f_setreg(typval_T *argvars, typval_T *rettv);
379static void f_settabvar(typval_T *argvars, typval_T *rettv);
380static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100381static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200382static void f_setwinvar(typval_T *argvars, typval_T *rettv);
383#ifdef FEAT_CRYPT
384static void f_sha256(typval_T *argvars, typval_T *rettv);
385#endif /* FEAT_CRYPT */
386static void f_shellescape(typval_T *argvars, typval_T *rettv);
387static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388#ifdef FEAT_SIGNS
389static void f_sign_define(typval_T *argvars, typval_T *rettv);
390static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
391static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100392static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100393static void f_sign_place(typval_T *argvars, typval_T *rettv);
394static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
395static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_simplify(typval_T *argvars, typval_T *rettv);
398#ifdef FEAT_FLOAT
399static void f_sin(typval_T *argvars, typval_T *rettv);
400static void f_sinh(typval_T *argvars, typval_T *rettv);
401#endif
402static void f_sort(typval_T *argvars, typval_T *rettv);
403static void f_soundfold(typval_T *argvars, typval_T *rettv);
404static void f_spellbadword(typval_T *argvars, typval_T *rettv);
405static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
406static void f_split(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_sqrt(typval_T *argvars, typval_T *rettv);
409static void f_str2float(typval_T *argvars, typval_T *rettv);
410#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200411static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_str2nr(typval_T *argvars, typval_T *rettv);
413static void f_strchars(typval_T *argvars, typval_T *rettv);
414#ifdef HAVE_STRFTIME
415static void f_strftime(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_strgetchar(typval_T *argvars, typval_T *rettv);
418static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_strlen(typval_T *argvars, typval_T *rettv);
420static void f_strcharpart(typval_T *argvars, typval_T *rettv);
421static void f_strpart(typval_T *argvars, typval_T *rettv);
422static void f_strridx(typval_T *argvars, typval_T *rettv);
423static void f_strtrans(typval_T *argvars, typval_T *rettv);
424static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
425static void f_strwidth(typval_T *argvars, typval_T *rettv);
426static void f_submatch(typval_T *argvars, typval_T *rettv);
427static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200428static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200429static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_synID(typval_T *argvars, typval_T *rettv);
431static void f_synIDattr(typval_T *argvars, typval_T *rettv);
432static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
433static void f_synstack(typval_T *argvars, typval_T *rettv);
434static void f_synconcealed(typval_T *argvars, typval_T *rettv);
435static void f_system(typval_T *argvars, typval_T *rettv);
436static void f_systemlist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
438static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
439static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
440static void f_taglist(typval_T *argvars, typval_T *rettv);
441static void f_tagfiles(typval_T *argvars, typval_T *rettv);
442static void f_tempname(typval_T *argvars, typval_T *rettv);
443static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
444static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200445static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200446static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200447static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100448static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100449static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100451static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100452static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200453#ifdef FEAT_JOB_CHANNEL
454static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
455#endif
456static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
457#ifdef FEAT_JOB_CHANNEL
458static void f_test_null_job(typval_T *argvars, typval_T *rettv);
459#endif
460static void f_test_null_list(typval_T *argvars, typval_T *rettv);
461static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
462static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200463#ifdef FEAT_GUI
464static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
465#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200467static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_test_settime(typval_T *argvars, typval_T *rettv);
470#ifdef FEAT_FLOAT
471static void f_tan(typval_T *argvars, typval_T *rettv);
472static void f_tanh(typval_T *argvars, typval_T *rettv);
473#endif
474#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200475static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200476static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477static void f_timer_start(typval_T *argvars, typval_T *rettv);
478static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200479static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481static void f_tolower(typval_T *argvars, typval_T *rettv);
482static void f_toupper(typval_T *argvars, typval_T *rettv);
483static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100484static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485#ifdef FEAT_FLOAT
486static void f_trunc(typval_T *argvars, typval_T *rettv);
487#endif
488static void f_type(typval_T *argvars, typval_T *rettv);
489static void f_undofile(typval_T *argvars, typval_T *rettv);
490static void f_undotree(typval_T *argvars, typval_T *rettv);
491static void f_uniq(typval_T *argvars, typval_T *rettv);
492static void f_values(typval_T *argvars, typval_T *rettv);
493static void f_virtcol(typval_T *argvars, typval_T *rettv);
494static void f_visualmode(typval_T *argvars, typval_T *rettv);
495static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200496static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
498static void f_win_getid(typval_T *argvars, typval_T *rettv);
499static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
500static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
501static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100502static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503static void f_winbufnr(typval_T *argvars, typval_T *rettv);
504static void f_wincol(typval_T *argvars, typval_T *rettv);
505static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200506static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507static void f_winline(typval_T *argvars, typval_T *rettv);
508static void f_winnr(typval_T *argvars, typval_T *rettv);
509static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
510static void f_winrestview(typval_T *argvars, typval_T *rettv);
511static void f_winsaveview(typval_T *argvars, typval_T *rettv);
512static void f_winwidth(typval_T *argvars, typval_T *rettv);
513static void f_writefile(typval_T *argvars, typval_T *rettv);
514static void f_wordcount(typval_T *argvars, typval_T *rettv);
515static void f_xor(typval_T *argvars, typval_T *rettv);
516
517/*
518 * Array with names and number of arguments of all internal functions
519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
520 */
521static struct fst
522{
523 char *f_name; /* function name */
524 char f_min_argc; /* minimal number of arguments */
525 char f_max_argc; /* maximal number of arguments */
526 void (*f_func)(typval_T *args, typval_T *rvar);
527 /* implementation of function */
528} functions[] =
529{
530#ifdef FEAT_FLOAT
531 {"abs", 1, 1, f_abs},
532 {"acos", 1, 1, f_acos}, /* WJMc */
533#endif
534 {"add", 2, 2, f_add},
535 {"and", 2, 2, f_and},
536 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200537 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"argidx", 0, 0, f_argidx},
540 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200541 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#ifdef FEAT_FLOAT
543 {"asin", 1, 1, f_asin}, /* WJMc */
544#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100545 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100547 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200549 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100551 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"assert_match", 2, 3, f_assert_match},
553 {"assert_notequal", 2, 3, f_assert_notequal},
554 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100555 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"assert_true", 1, 2, f_assert_true},
557#ifdef FEAT_FLOAT
558 {"atan", 1, 1, f_atan},
559 {"atan2", 2, 2, f_atan2},
560#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200562 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100563 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100565 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100566# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200568 {"browse", 4, 4, f_browse},
569 {"browsedir", 2, 2, f_browsedir},
570 {"bufexists", 1, 1, f_bufexists},
571 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
572 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
573 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
574 {"buflisted", 1, 1, f_buflisted},
575 {"bufloaded", 1, 1, f_bufloaded},
576 {"bufname", 1, 1, f_bufname},
577 {"bufnr", 1, 2, f_bufnr},
578 {"bufwinid", 1, 1, f_bufwinid},
579 {"bufwinnr", 1, 1, f_bufwinnr},
580 {"byte2line", 1, 1, f_byte2line},
581 {"byteidx", 2, 2, f_byteidx},
582 {"byteidxcomp", 2, 2, f_byteidxcomp},
583 {"call", 2, 3, f_call},
584#ifdef FEAT_FLOAT
585 {"ceil", 1, 1, f_ceil},
586#endif
587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200590 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
592 {"ch_evalraw", 2, 3, f_ch_evalraw},
593 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
594 {"ch_getjob", 1, 1, f_ch_getjob},
595 {"ch_info", 1, 1, f_ch_info},
596 {"ch_log", 1, 2, f_ch_log},
597 {"ch_logfile", 1, 2, f_ch_logfile},
598 {"ch_open", 1, 2, f_ch_open},
599 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"ch_readraw", 1, 2, f_ch_readraw},
602 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
603 {"ch_sendraw", 2, 3, f_ch_sendraw},
604 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200605 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606#endif
607 {"changenr", 0, 0, f_changenr},
608 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200609 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100611 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"col", 1, 1, f_col},
613#if defined(FEAT_INS_EXPAND)
614 {"complete", 2, 2, f_complete},
615 {"complete_add", 1, 1, f_complete_add},
616 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100617 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618#endif
619 {"confirm", 1, 4, f_confirm},
620 {"copy", 1, 1, f_copy},
621#ifdef FEAT_FLOAT
622 {"cos", 1, 1, f_cos},
623 {"cosh", 1, 1, f_cosh},
624#endif
625 {"count", 2, 4, f_count},
626 {"cscope_connection",0,3, f_cscope_connection},
627 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200629 {"debugbreak", 1, 1, f_debugbreak},
630#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"deepcopy", 1, 2, f_deepcopy},
632 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200633 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"did_filetype", 0, 0, f_did_filetype},
635 {"diff_filler", 1, 1, f_diff_filler},
636 {"diff_hlID", 2, 2, f_diff_hlID},
637 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200638 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"escape", 2, 2, f_escape},
640 {"eval", 1, 1, f_eval},
641 {"eventhandler", 0, 0, f_eventhandler},
642 {"executable", 1, 1, f_executable},
643 {"execute", 1, 2, f_execute},
644 {"exepath", 1, 1, f_exepath},
645 {"exists", 1, 1, f_exists},
646#ifdef FEAT_FLOAT
647 {"exp", 1, 1, f_exp},
648#endif
649 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200650 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"extend", 2, 3, f_extend},
652 {"feedkeys", 1, 2, f_feedkeys},
653 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
654 {"filereadable", 1, 1, f_filereadable},
655 {"filewritable", 1, 1, f_filewritable},
656 {"filter", 2, 2, f_filter},
657 {"finddir", 1, 3, f_finddir},
658 {"findfile", 1, 3, f_findfile},
659#ifdef FEAT_FLOAT
660 {"float2nr", 1, 1, f_float2nr},
661 {"floor", 1, 1, f_floor},
662 {"fmod", 2, 2, f_fmod},
663#endif
664 {"fnameescape", 1, 1, f_fnameescape},
665 {"fnamemodify", 2, 2, f_fnamemodify},
666 {"foldclosed", 1, 1, f_foldclosed},
667 {"foldclosedend", 1, 1, f_foldclosedend},
668 {"foldlevel", 1, 1, f_foldlevel},
669 {"foldtext", 0, 0, f_foldtext},
670 {"foldtextresult", 1, 1, f_foldtextresult},
671 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200672 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"function", 1, 3, f_function},
674 {"garbagecollect", 0, 1, f_garbagecollect},
675 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200676 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getbufline", 2, 3, f_getbufline},
678 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100679 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getchar", 0, 1, f_getchar},
681 {"getcharmod", 0, 0, f_getcharmod},
682 {"getcharsearch", 0, 0, f_getcharsearch},
683 {"getcmdline", 0, 0, f_getcmdline},
684 {"getcmdpos", 0, 0, f_getcmdpos},
685 {"getcmdtype", 0, 0, f_getcmdtype},
686 {"getcmdwintype", 0, 0, f_getcmdwintype},
687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200688 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689#endif
690 {"getcurpos", 0, 0, f_getcurpos},
691 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200692 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200693 {"getfontname", 0, 1, f_getfontname},
694 {"getfperm", 1, 1, f_getfperm},
695 {"getfsize", 1, 1, f_getfsize},
696 {"getftime", 1, 1, f_getftime},
697 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100698 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100701 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getpid", 0, 0, f_getpid},
703 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200704 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"getreg", 0, 3, f_getreg},
706 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200708 {"gettabvar", 2, 3, f_gettabvar},
709 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100710 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200711 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100712 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"getwinposx", 0, 0, f_getwinposx},
714 {"getwinposy", 0, 0, f_getwinposy},
715 {"getwinvar", 2, 3, f_getwinvar},
716 {"glob", 1, 4, f_glob},
717 {"glob2regpat", 1, 1, f_glob2regpat},
718 {"globpath", 2, 5, f_globpath},
719 {"has", 1, 1, f_has},
720 {"has_key", 2, 2, f_has_key},
721 {"haslocaldir", 0, 2, f_haslocaldir},
722 {"hasmapto", 1, 3, f_hasmapto},
723 {"highlightID", 1, 1, f_hlID}, /* obsolete */
724 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
725 {"histadd", 2, 2, f_histadd},
726 {"histdel", 1, 2, f_histdel},
727 {"histget", 1, 2, f_histget},
728 {"histnr", 1, 1, f_histnr},
729 {"hlID", 1, 1, f_hlID},
730 {"hlexists", 1, 1, f_hlexists},
731 {"hostname", 0, 0, f_hostname},
732 {"iconv", 3, 3, f_iconv},
733 {"indent", 1, 1, f_indent},
734 {"index", 2, 4, f_index},
735 {"input", 1, 3, f_input},
736 {"inputdialog", 1, 3, f_inputdialog},
737 {"inputlist", 1, 1, f_inputlist},
738 {"inputrestore", 0, 0, f_inputrestore},
739 {"inputsave", 0, 0, f_inputsave},
740 {"inputsecret", 1, 2, f_inputsecret},
741 {"insert", 2, 3, f_insert},
742 {"invert", 1, 1, f_invert},
743 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isinf", 1, 1, f_isinf},
746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747 {"islocked", 1, 1, f_islocked},
748#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
749 {"isnan", 1, 1, f_isnan},
750#endif
751 {"items", 1, 1, f_items},
752#ifdef FEAT_JOB_CHANNEL
753 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200754 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"job_setoptions", 2, 2, f_job_setoptions},
756 {"job_start", 1, 2, f_job_start},
757 {"job_status", 1, 1, f_job_status},
758 {"job_stop", 1, 2, f_job_stop},
759#endif
760 {"join", 1, 2, f_join},
761 {"js_decode", 1, 1, f_js_decode},
762 {"js_encode", 1, 1, f_js_encode},
763 {"json_decode", 1, 1, f_json_decode},
764 {"json_encode", 1, 1, f_json_encode},
765 {"keys", 1, 1, f_keys},
766 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
767 {"len", 1, 1, f_len},
768 {"libcall", 3, 3, f_libcall},
769 {"libcallnr", 3, 3, f_libcallnr},
770 {"line", 1, 1, f_line},
771 {"line2byte", 1, 1, f_line2byte},
772 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200773 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200775 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"localtime", 0, 0, f_localtime},
778#ifdef FEAT_FLOAT
779 {"log", 1, 1, f_log},
780 {"log10", 1, 1, f_log10},
781#endif
782#ifdef FEAT_LUA
783 {"luaeval", 1, 2, f_luaeval},
784#endif
785 {"map", 2, 2, f_map},
786 {"maparg", 1, 4, f_maparg},
787 {"mapcheck", 1, 3, f_mapcheck},
788 {"match", 2, 4, f_match},
789 {"matchadd", 2, 5, f_matchadd},
790 {"matchaddpos", 2, 5, f_matchaddpos},
791 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100792 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"matchend", 2, 4, f_matchend},
794 {"matchlist", 2, 4, f_matchlist},
795 {"matchstr", 2, 4, f_matchstr},
796 {"matchstrpos", 2, 4, f_matchstrpos},
797 {"max", 1, 1, f_max},
798 {"min", 1, 1, f_min},
799#ifdef vim_mkdir
800 {"mkdir", 1, 3, f_mkdir},
801#endif
802 {"mode", 0, 1, f_mode},
803#ifdef FEAT_MZSCHEME
804 {"mzeval", 1, 1, f_mzeval},
805#endif
806 {"nextnonblank", 1, 1, f_nextnonblank},
807 {"nr2char", 1, 2, f_nr2char},
808 {"or", 2, 2, f_or},
809 {"pathshorten", 1, 1, f_pathshorten},
810#ifdef FEAT_PERL
811 {"perleval", 1, 1, f_perleval},
812#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200814 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaara42d9452019-06-15 21:46:30 +0200818 {"popup_dialog", 2, 2, f_popup_dialog},
819 {"popup_filter_yesno", 2, 2, f_popup_filter_yesno},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200820 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200821 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200822 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200823 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200824 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaardc2ce582019-06-16 15:32:14 +0200825 {"popup_settext", 2, 2, f_popup_settext},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200826 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200827#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200828#ifdef FEAT_FLOAT
829 {"pow", 2, 2, f_pow},
830#endif
831 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100832 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200833#ifdef FEAT_JOB_CHANNEL
834 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200835 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200836 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
837#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100838#ifdef FEAT_TEXT_PROP
839 {"prop_add", 3, 3, f_prop_add},
840 {"prop_clear", 1, 3, f_prop_clear},
841 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100842 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100843 {"prop_type_add", 2, 2, f_prop_type_add},
844 {"prop_type_change", 2, 2, f_prop_type_change},
845 {"prop_type_delete", 1, 2, f_prop_type_delete},
846 {"prop_type_get", 1, 2, f_prop_type_get},
847 {"prop_type_list", 0, 1, f_prop_type_list},
848#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"pumvisible", 0, 0, f_pumvisible},
850#ifdef FEAT_PYTHON3
851 {"py3eval", 1, 1, f_py3eval},
852#endif
853#ifdef FEAT_PYTHON
854 {"pyeval", 1, 1, f_pyeval},
855#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100856#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
857 {"pyxeval", 1, 1, f_pyxeval},
858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200859 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200860 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200862 {"reg_executing", 0, 0, f_reg_executing},
863 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"reltime", 0, 2, f_reltime},
865#ifdef FEAT_FLOAT
866 {"reltimefloat", 1, 1, f_reltimefloat},
867#endif
868 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100869 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"remote_foreground", 1, 1, f_remote_foreground},
871 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100872 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200873 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100874 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"remove", 2, 3, f_remove},
876 {"rename", 2, 2, f_rename},
877 {"repeat", 2, 2, f_repeat},
878 {"resolve", 1, 1, f_resolve},
879 {"reverse", 1, 1, f_reverse},
880#ifdef FEAT_FLOAT
881 {"round", 1, 1, f_round},
882#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100883#ifdef FEAT_RUBY
884 {"rubyeval", 1, 1, f_rubyeval},
885#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"screenattr", 2, 2, f_screenattr},
887 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100888 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889 {"screencol", 0, 0, f_screencol},
890 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100891 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"search", 1, 4, f_search},
893 {"searchdecl", 1, 3, f_searchdecl},
894 {"searchpair", 3, 7, f_searchpair},
895 {"searchpairpos", 3, 7, f_searchpairpos},
896 {"searchpos", 1, 4, f_searchpos},
897 {"server2client", 2, 2, f_server2client},
898 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200899 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200900 {"setbufvar", 3, 3, f_setbufvar},
901 {"setcharsearch", 1, 1, f_setcharsearch},
902 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200903 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"setfperm", 2, 2, f_setfperm},
905 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200906 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100907 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200908 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200909 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200910 {"setreg", 2, 3, f_setreg},
911 {"settabvar", 3, 3, f_settabvar},
912 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100913 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200914 {"setwinvar", 3, 3, f_setwinvar},
915#ifdef FEAT_CRYPT
916 {"sha256", 1, 1, f_sha256},
917#endif
918 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100919 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100920#ifdef FEAT_SIGNS
921 {"sign_define", 1, 2, f_sign_define},
922 {"sign_getdefined", 0, 1, f_sign_getdefined},
923 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100924 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100925 {"sign_place", 4, 5, f_sign_place},
926 {"sign_undefine", 0, 1, f_sign_undefine},
927 {"sign_unplace", 1, 2, f_sign_unplace},
928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200929 {"simplify", 1, 1, f_simplify},
930#ifdef FEAT_FLOAT
931 {"sin", 1, 1, f_sin},
932 {"sinh", 1, 1, f_sinh},
933#endif
934 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200935#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200936 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200937 {"sound_playevent", 1, 2, f_sound_playevent},
938 {"sound_playfile", 1, 2, f_sound_playfile},
939 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200940#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200941 {"soundfold", 1, 1, f_soundfold},
942 {"spellbadword", 0, 1, f_spellbadword},
943 {"spellsuggest", 1, 3, f_spellsuggest},
944 {"split", 1, 3, f_split},
945#ifdef FEAT_FLOAT
946 {"sqrt", 1, 1, f_sqrt},
947 {"str2float", 1, 1, f_str2float},
948#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200949 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200950 {"str2nr", 1, 2, f_str2nr},
951 {"strcharpart", 2, 3, f_strcharpart},
952 {"strchars", 1, 2, f_strchars},
953 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
954#ifdef HAVE_STRFTIME
955 {"strftime", 1, 2, f_strftime},
956#endif
957 {"strgetchar", 2, 2, f_strgetchar},
958 {"stridx", 2, 3, f_stridx},
959 {"string", 1, 1, f_string},
960 {"strlen", 1, 1, f_strlen},
961 {"strpart", 2, 3, f_strpart},
962 {"strridx", 2, 3, f_strridx},
963 {"strtrans", 1, 1, f_strtrans},
964 {"strwidth", 1, 1, f_strwidth},
965 {"submatch", 1, 2, f_submatch},
966 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200967 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200968 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200969 {"synID", 3, 3, f_synID},
970 {"synIDattr", 2, 3, f_synIDattr},
971 {"synIDtrans", 1, 1, f_synIDtrans},
972 {"synconcealed", 2, 2, f_synconcealed},
973 {"synstack", 2, 2, f_synstack},
974 {"system", 1, 2, f_system},
975 {"systemlist", 1, 2, f_systemlist},
976 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
977 {"tabpagenr", 0, 1, f_tabpagenr},
978 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
979 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100980 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200981#ifdef FEAT_FLOAT
982 {"tan", 1, 1, f_tan},
983 {"tanh", 1, 1, f_tanh},
984#endif
985 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100987 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
988 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100989 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200990 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200991# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
992 {"term_getansicolors", 1, 1, f_term_getansicolors},
993# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200994 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200995 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200997 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200998 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +02001000 {"term_getstatus", 1, 1, f_term_getstatus},
1001 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +02001002 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001003 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001004 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001005 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001006# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1007 {"term_setansicolors", 2, 2, f_term_setansicolors},
1008# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001009 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001010 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001011 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001012 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001013 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001014#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001015 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1016 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001017 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001018 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001019 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001020 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001021 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001022#ifdef FEAT_JOB_CHANNEL
1023 {"test_null_channel", 0, 0, f_test_null_channel},
1024#endif
1025 {"test_null_dict", 0, 0, f_test_null_dict},
1026#ifdef FEAT_JOB_CHANNEL
1027 {"test_null_job", 0, 0, f_test_null_job},
1028#endif
1029 {"test_null_list", 0, 0, f_test_null_list},
1030 {"test_null_partial", 0, 0, f_test_null_partial},
1031 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001032 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001033 {"test_override", 2, 2, f_test_override},
1034 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001035#ifdef FEAT_GUI
1036 {"test_scrollbar", 3, 3, f_test_scrollbar},
1037#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001038#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001039 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001040#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001041 {"test_settime", 1, 1, f_test_settime},
1042#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001043 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001044 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001045 {"timer_start", 2, 3, f_timer_start},
1046 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001047 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001048#endif
1049 {"tolower", 1, 1, f_tolower},
1050 {"toupper", 1, 1, f_toupper},
1051 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001052 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001053#ifdef FEAT_FLOAT
1054 {"trunc", 1, 1, f_trunc},
1055#endif
1056 {"type", 1, 1, f_type},
1057 {"undofile", 1, 1, f_undofile},
1058 {"undotree", 0, 0, f_undotree},
1059 {"uniq", 1, 3, f_uniq},
1060 {"values", 1, 1, f_values},
1061 {"virtcol", 1, 1, f_virtcol},
1062 {"visualmode", 0, 1, f_visualmode},
1063 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001064 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001065 {"win_findbuf", 1, 1, f_win_findbuf},
1066 {"win_getid", 0, 2, f_win_getid},
1067 {"win_gotoid", 1, 1, f_win_gotoid},
1068 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1069 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001070 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001071 {"winbufnr", 1, 1, f_winbufnr},
1072 {"wincol", 0, 0, f_wincol},
1073 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001074 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001075 {"winline", 0, 0, f_winline},
1076 {"winnr", 0, 1, f_winnr},
1077 {"winrestcmd", 0, 0, f_winrestcmd},
1078 {"winrestview", 1, 1, f_winrestview},
1079 {"winsaveview", 0, 0, f_winsaveview},
1080 {"winwidth", 1, 1, f_winwidth},
1081 {"wordcount", 0, 0, f_wordcount},
1082 {"writefile", 2, 3, f_writefile},
1083 {"xor", 2, 2, f_xor},
1084};
1085
1086#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1087
1088/*
1089 * Function given to ExpandGeneric() to obtain the list of internal
1090 * or user defined function names.
1091 */
1092 char_u *
1093get_function_name(expand_T *xp, int idx)
1094{
1095 static int intidx = -1;
1096 char_u *name;
1097
1098 if (idx == 0)
1099 intidx = -1;
1100 if (intidx < 0)
1101 {
1102 name = get_user_func_name(xp, idx);
1103 if (name != NULL)
1104 return name;
1105 }
1106 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1107 {
1108 STRCPY(IObuff, functions[intidx].f_name);
1109 STRCAT(IObuff, "(");
1110 if (functions[intidx].f_max_argc == 0)
1111 STRCAT(IObuff, ")");
1112 return IObuff;
1113 }
1114
1115 return NULL;
1116}
1117
1118/*
1119 * Function given to ExpandGeneric() to obtain the list of internal or
1120 * user defined variable or function names.
1121 */
1122 char_u *
1123get_expr_name(expand_T *xp, int idx)
1124{
1125 static int intidx = -1;
1126 char_u *name;
1127
1128 if (idx == 0)
1129 intidx = -1;
1130 if (intidx < 0)
1131 {
1132 name = get_function_name(xp, idx);
1133 if (name != NULL)
1134 return name;
1135 }
1136 return get_user_var_name(xp, ++intidx);
1137}
1138
1139#endif /* FEAT_CMDL_COMPL */
1140
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001141/*
1142 * Find internal function in table above.
1143 * Return index, or -1 if not found
1144 */
1145 int
1146find_internal_func(
1147 char_u *name) /* name of the function */
1148{
1149 int first = 0;
1150 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1151 int cmp;
1152 int x;
1153
1154 /*
1155 * Find the function name in the table. Binary search.
1156 */
1157 while (first <= last)
1158 {
1159 x = first + ((unsigned)(last - first) >> 1);
1160 cmp = STRCMP(name, functions[x].f_name);
1161 if (cmp < 0)
1162 last = x - 1;
1163 else if (cmp > 0)
1164 first = x + 1;
1165 else
1166 return x;
1167 }
1168 return -1;
1169}
1170
1171 int
1172call_internal_func(
1173 char_u *name,
1174 int argcount,
1175 typval_T *argvars,
1176 typval_T *rettv)
1177{
1178 int i;
1179
1180 i = find_internal_func(name);
1181 if (i < 0)
1182 return ERROR_UNKNOWN;
1183 if (argcount < functions[i].f_min_argc)
1184 return ERROR_TOOFEW;
1185 if (argcount > functions[i].f_max_argc)
1186 return ERROR_TOOMANY;
1187 argvars[argcount].v_type = VAR_UNKNOWN;
1188 functions[i].f_func(argvars, rettv);
1189 return ERROR_NONE;
1190}
1191
1192/*
1193 * Return TRUE for a non-zero Number and a non-empty String.
1194 */
1195 static int
1196non_zero_arg(typval_T *argvars)
1197{
1198 return ((argvars[0].v_type == VAR_NUMBER
1199 && argvars[0].vval.v_number != 0)
1200 || (argvars[0].v_type == VAR_SPECIAL
1201 && argvars[0].vval.v_number == VVAL_TRUE)
1202 || (argvars[0].v_type == VAR_STRING
1203 && argvars[0].vval.v_string != NULL
1204 && *argvars[0].vval.v_string != NUL));
1205}
1206
1207/*
1208 * Get the lnum from the first argument.
1209 * Also accepts ".", "$", etc., but that only works for the current buffer.
1210 * Returns -1 on error.
1211 */
1212 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001213tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001214{
1215 typval_T rettv;
1216 linenr_T lnum;
1217
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001218 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001219 if (lnum == 0) /* no valid number, try using line() */
1220 {
1221 rettv.v_type = VAR_NUMBER;
1222 f_line(argvars, &rettv);
1223 lnum = (linenr_T)rettv.vval.v_number;
1224 clear_tv(&rettv);
1225 }
1226 return lnum;
1227}
1228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001229/*
1230 * Get the lnum from the first argument.
1231 * Also accepts "$", then "buf" is used.
1232 * Returns 0 on error.
1233 */
1234 static linenr_T
1235tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1236{
1237 if (argvars[0].v_type == VAR_STRING
1238 && argvars[0].vval.v_string != NULL
1239 && argvars[0].vval.v_string[0] == '$'
1240 && buf != NULL)
1241 return buf->b_ml.ml_line_count;
1242 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1243}
1244
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001245#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246/*
1247 * Get the float value of "argvars[0]" into "f".
1248 * Returns FAIL when the argument is not a Number or Float.
1249 */
1250 static int
1251get_float_arg(typval_T *argvars, float_T *f)
1252{
1253 if (argvars[0].v_type == VAR_FLOAT)
1254 {
1255 *f = argvars[0].vval.v_float;
1256 return OK;
1257 }
1258 if (argvars[0].v_type == VAR_NUMBER)
1259 {
1260 *f = (float_T)argvars[0].vval.v_number;
1261 return OK;
1262 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001263 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001264 return FAIL;
1265}
1266
1267/*
1268 * "abs(expr)" function
1269 */
1270 static void
1271f_abs(typval_T *argvars, typval_T *rettv)
1272{
1273 if (argvars[0].v_type == VAR_FLOAT)
1274 {
1275 rettv->v_type = VAR_FLOAT;
1276 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1277 }
1278 else
1279 {
1280 varnumber_T n;
1281 int error = FALSE;
1282
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001283 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001284 if (error)
1285 rettv->vval.v_number = -1;
1286 else if (n > 0)
1287 rettv->vval.v_number = n;
1288 else
1289 rettv->vval.v_number = -n;
1290 }
1291}
1292
1293/*
1294 * "acos()" function
1295 */
1296 static void
1297f_acos(typval_T *argvars, typval_T *rettv)
1298{
1299 float_T f = 0.0;
1300
1301 rettv->v_type = VAR_FLOAT;
1302 if (get_float_arg(argvars, &f) == OK)
1303 rettv->vval.v_float = acos(f);
1304 else
1305 rettv->vval.v_float = 0.0;
1306}
1307#endif
1308
1309/*
1310 * "add(list, item)" function
1311 */
1312 static void
1313f_add(typval_T *argvars, typval_T *rettv)
1314{
1315 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001317
1318 rettv->vval.v_number = 1; /* Default: Failed */
1319 if (argvars[0].v_type == VAR_LIST)
1320 {
1321 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001322 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001323 (char_u *)N_("add() argument"), TRUE)
1324 && list_append_tv(l, &argvars[1]) == OK)
1325 copy_tv(&argvars[0], rettv);
1326 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001327 else if (argvars[0].v_type == VAR_BLOB)
1328 {
1329 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001330 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001331 (char_u *)N_("add() argument"), TRUE))
1332 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001333 int error = FALSE;
1334 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1335
1336 if (!error)
1337 {
1338 ga_append(&b->bv_ga, (int)n);
1339 copy_tv(&argvars[0], rettv);
1340 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001341 }
1342 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001343 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001344 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001345}
1346
1347/*
1348 * "and(expr, expr)" function
1349 */
1350 static void
1351f_and(typval_T *argvars, typval_T *rettv)
1352{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001353 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1354 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001355}
1356
1357/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001358 * If there is a window for "curbuf", make it the current window.
1359 */
1360 static void
1361find_win_for_curbuf(void)
1362{
1363 wininfo_T *wip;
1364
1365 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1366 {
1367 if (wip->wi_win != NULL)
1368 {
1369 curwin = wip->wi_win;
1370 break;
1371 }
1372 }
1373}
1374
1375/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377 */
1378 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001379set_buffer_lines(
1380 buf_T *buf,
1381 linenr_T lnum_arg,
1382 int append,
1383 typval_T *lines,
1384 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385{
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1387 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 list_T *l = NULL;
1389 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001391 linenr_T append_lnum;
1392 buf_T *curbuf_save = NULL;
1393 win_T *curwin_save = NULL;
1394 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001395
Bram Moolenaarca851592018-06-06 21:04:07 +02001396 /* When using the current buffer ml_mfp will be set if needed. Useful when
1397 * setline() is used on startup. For other buffers the buffer must be
1398 * loaded. */
1399 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001401 rettv->vval.v_number = 1; /* FAIL */
1402 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 }
1404
Bram Moolenaarca851592018-06-06 21:04:07 +02001405 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001406 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001407 curbuf_save = curbuf;
1408 curwin_save = curwin;
1409 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001410 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001411 }
1412
1413 if (append)
1414 // appendbufline() uses the line number below which we insert
1415 append_lnum = lnum - 1;
1416 else
1417 // setbufline() uses the line number above which we insert, we only
1418 // append if it's below the last line
1419 append_lnum = curbuf->b_ml.ml_line_count;
1420
1421 if (lines->v_type == VAR_LIST)
1422 {
1423 l = lines->vval.v_list;
1424 li = l->lv_first;
1425 }
1426 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001427 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001428
1429 /* default result is zero == OK */
1430 for (;;)
1431 {
1432 if (l != NULL)
1433 {
1434 /* list argument, get next string */
1435 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001436 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001437 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001438 li = li->li_next;
1439 }
1440
Bram Moolenaarca851592018-06-06 21:04:07 +02001441 rettv->vval.v_number = 1; /* FAIL */
1442 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1443 break;
1444
1445 /* When coming here from Insert mode, sync undo, so that this can be
1446 * undone separately from what was previously inserted. */
1447 if (u_sync_once == 2)
1448 {
1449 u_sync_once = 1; /* notify that u_sync() was called */
1450 u_sync(TRUE);
1451 }
1452
1453 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1454 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001455 // Existing line, replace it.
1456 // Removes any existing text properties.
1457 if (u_savesub(lnum) == OK && ml_replace_len(
1458 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001459 {
1460 changed_bytes(lnum, 0);
1461 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1462 check_cursor_col();
1463 rettv->vval.v_number = 0; /* OK */
1464 }
1465 }
1466 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1467 {
1468 /* append the line */
1469 ++added;
1470 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1471 rettv->vval.v_number = 0; /* OK */
1472 }
1473
1474 if (l == NULL) /* only one string argument */
1475 break;
1476 ++lnum;
1477 }
1478
1479 if (added > 0)
1480 {
1481 win_T *wp;
1482 tabpage_T *tp;
1483
1484 appended_lines_mark(append_lnum, added);
1485 FOR_ALL_TAB_WINDOWS(tp, wp)
1486 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1487 wp->w_cursor.lnum += added;
1488 check_cursor_col();
1489
Bram Moolenaarf2732452018-06-03 14:47:35 +02001490#ifdef FEAT_JOB_CHANNEL
1491 if (bt_prompt(curbuf) && (State & INSERT))
1492 // show the line with the prompt
1493 update_topline();
1494#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001495 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001496
1497 if (!is_curbuf)
1498 {
1499 curbuf = curbuf_save;
1500 curwin = curwin_save;
1501 }
1502}
1503
1504/*
1505 * "append(lnum, string/list)" function
1506 */
1507 static void
1508f_append(typval_T *argvars, typval_T *rettv)
1509{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001510 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001511
1512 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1513}
1514
1515/*
1516 * "appendbufline(buf, lnum, string/list)" function
1517 */
1518 static void
1519f_appendbufline(typval_T *argvars, typval_T *rettv)
1520{
1521 linenr_T lnum;
1522 buf_T *buf;
1523
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001524 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001525 if (buf == NULL)
1526 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001527 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001528 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001529 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001530 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001532}
1533
1534/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001535 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001536 */
1537 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001538f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001539{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001540 win_T *wp;
1541
1542 if (argvars[0].v_type == VAR_UNKNOWN)
1543 // use the current window
1544 rettv->vval.v_number = ARGCOUNT;
1545 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001546 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001547 // use the global argument list
1548 rettv->vval.v_number = GARGCOUNT;
1549 else
1550 {
1551 // use the argument list of the specified window
1552 wp = find_win_by_nr_or_id(&argvars[0]);
1553 if (wp != NULL)
1554 rettv->vval.v_number = WARGCOUNT(wp);
1555 else
1556 rettv->vval.v_number = -1;
1557 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001558}
1559
1560/*
1561 * "argidx()" function
1562 */
1563 static void
1564f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1565{
1566 rettv->vval.v_number = curwin->w_arg_idx;
1567}
1568
1569/*
1570 * "arglistid()" function
1571 */
1572 static void
1573f_arglistid(typval_T *argvars, typval_T *rettv)
1574{
1575 win_T *wp;
1576
1577 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001578 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001579 if (wp != NULL)
1580 rettv->vval.v_number = wp->w_alist->id;
1581}
1582
1583/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 * Get the argument list for a given window
1585 */
1586 static void
1587get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1588{
1589 int idx;
1590
1591 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1592 for (idx = 0; idx < argcount; ++idx)
1593 list_append_string(rettv->vval.v_list,
1594 alist_name(&arglist[idx]), -1);
1595}
1596
1597/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001598 * "argv(nr)" function
1599 */
1600 static void
1601f_argv(typval_T *argvars, typval_T *rettv)
1602{
1603 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001604 aentry_T *arglist = NULL;
1605 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001606
1607 if (argvars[0].v_type != VAR_UNKNOWN)
1608 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001609 if (argvars[1].v_type == VAR_UNKNOWN)
1610 {
1611 arglist = ARGLIST;
1612 argcount = ARGCOUNT;
1613 }
1614 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001615 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001616 {
1617 arglist = GARGLIST;
1618 argcount = GARGCOUNT;
1619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001620 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001621 {
1622 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1623
1624 if (wp != NULL)
1625 {
1626 /* Use the argument list of the specified window */
1627 arglist = WARGLIST(wp);
1628 argcount = WARGCOUNT(wp);
1629 }
1630 }
1631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001632 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001633 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001634 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001635 if (arglist != NULL && idx >= 0 && idx < argcount)
1636 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1637 else if (idx == -1)
1638 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001640 else
1641 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642}
1643
1644/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001645 * "assert_beeps(cmd [, error])" function
1646 */
1647 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001649{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001651}
1652
1653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 * "assert_equal(expected, actual[, msg])" function
1655 */
1656 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660}
1661
1662/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001663 * "assert_equalfile(fname-one, fname-two)" function
1664 */
1665 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001667{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001669}
1670
1671/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672 * "assert_notequal(expected, actual[, msg])" function
1673 */
1674 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678}
1679
1680/*
1681 * "assert_exception(string[, msg])" function
1682 */
1683 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001684f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687}
1688
1689/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001690 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691 */
1692 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001693f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001694{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696}
1697
1698/*
1699 * "assert_false(actual[, msg])" function
1700 */
1701 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001702f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705}
1706
1707/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001708 * "assert_inrange(lower, upper[, msg])" function
1709 */
1710 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001711f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001712{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001714}
1715
1716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717 * "assert_match(pattern, actual[, msg])" function
1718 */
1719 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001720f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001721{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723}
1724
1725/*
1726 * "assert_notmatch(pattern, actual[, msg])" function
1727 */
1728 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001729f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001731 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732}
1733
1734/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001735 * "assert_report(msg)" function
1736 */
1737 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001738f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001739{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001740 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001741}
1742
1743/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001744 * "assert_true(actual[, msg])" function
1745 */
1746 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001747f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001748{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001749 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001750}
1751
1752#ifdef FEAT_FLOAT
1753/*
1754 * "asin()" function
1755 */
1756 static void
1757f_asin(typval_T *argvars, typval_T *rettv)
1758{
1759 float_T f = 0.0;
1760
1761 rettv->v_type = VAR_FLOAT;
1762 if (get_float_arg(argvars, &f) == OK)
1763 rettv->vval.v_float = asin(f);
1764 else
1765 rettv->vval.v_float = 0.0;
1766}
1767
1768/*
1769 * "atan()" function
1770 */
1771 static void
1772f_atan(typval_T *argvars, typval_T *rettv)
1773{
1774 float_T f = 0.0;
1775
1776 rettv->v_type = VAR_FLOAT;
1777 if (get_float_arg(argvars, &f) == OK)
1778 rettv->vval.v_float = atan(f);
1779 else
1780 rettv->vval.v_float = 0.0;
1781}
1782
1783/*
1784 * "atan2()" function
1785 */
1786 static void
1787f_atan2(typval_T *argvars, typval_T *rettv)
1788{
1789 float_T fx = 0.0, fy = 0.0;
1790
1791 rettv->v_type = VAR_FLOAT;
1792 if (get_float_arg(argvars, &fx) == OK
1793 && get_float_arg(&argvars[1], &fy) == OK)
1794 rettv->vval.v_float = atan2(fx, fy);
1795 else
1796 rettv->vval.v_float = 0.0;
1797}
1798#endif
1799
1800/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001801 * "balloon_show()" function
1802 */
1803#ifdef FEAT_BEVAL
1804 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001805f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1806{
1807 rettv->v_type = VAR_STRING;
1808 if (balloonEval != NULL)
1809 {
1810 if (balloonEval->msg == NULL)
1811 rettv->vval.v_string = NULL;
1812 else
1813 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1814 }
1815}
1816
1817 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001818f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1819{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001820 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001821 {
1822 if (argvars[0].v_type == VAR_LIST
1823# ifdef FEAT_GUI
1824 && !gui.in_use
1825# endif
1826 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001827 {
1828 list_T *l = argvars[0].vval.v_list;
1829
1830 // empty list removes the balloon
1831 post_balloon(balloonEval, NULL,
1832 l == NULL || l->lv_len == 0 ? NULL : l);
1833 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001834 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001835 {
1836 char_u *mesg = tv_get_string_chk(&argvars[0]);
1837
1838 if (mesg != NULL)
1839 // empty string removes the balloon
1840 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1841 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001842 }
1843}
1844
Bram Moolenaar669a8282017-11-19 20:13:05 +01001845# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001846 static void
1847f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1848{
1849 if (rettv_list_alloc(rettv) == OK)
1850 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001851 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001852
1853 if (msg != NULL)
1854 {
1855 pumitem_T *array;
1856 int size = split_message(msg, &array);
1857 int i;
1858
1859 /* Skip the first and last item, they are always empty. */
1860 for (i = 1; i < size - 1; ++i)
1861 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001862 while (size > 0)
1863 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001864 vim_free(array);
1865 }
1866 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001867}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001868# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001869#endif
1870
1871/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872 * "browse(save, title, initdir, default)" function
1873 */
1874 static void
1875f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1876{
1877#ifdef FEAT_BROWSE
1878 int save;
1879 char_u *title;
1880 char_u *initdir;
1881 char_u *defname;
1882 char_u buf[NUMBUFLEN];
1883 char_u buf2[NUMBUFLEN];
1884 int error = FALSE;
1885
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001886 save = (int)tv_get_number_chk(&argvars[0], &error);
1887 title = tv_get_string_chk(&argvars[1]);
1888 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1889 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001890
1891 if (error || title == NULL || initdir == NULL || defname == NULL)
1892 rettv->vval.v_string = NULL;
1893 else
1894 rettv->vval.v_string =
1895 do_browse(save ? BROWSE_SAVE : 0,
1896 title, defname, NULL, initdir, NULL, curbuf);
1897#else
1898 rettv->vval.v_string = NULL;
1899#endif
1900 rettv->v_type = VAR_STRING;
1901}
1902
1903/*
1904 * "browsedir(title, initdir)" function
1905 */
1906 static void
1907f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1908{
1909#ifdef FEAT_BROWSE
1910 char_u *title;
1911 char_u *initdir;
1912 char_u buf[NUMBUFLEN];
1913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001914 title = tv_get_string_chk(&argvars[0]);
1915 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001916
1917 if (title == NULL || initdir == NULL)
1918 rettv->vval.v_string = NULL;
1919 else
1920 rettv->vval.v_string = do_browse(BROWSE_DIR,
1921 title, NULL, NULL, initdir, NULL, curbuf);
1922#else
1923 rettv->vval.v_string = NULL;
1924#endif
1925 rettv->v_type = VAR_STRING;
1926}
1927
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928/*
1929 * Find a buffer by number or exact name.
1930 */
1931 static buf_T *
1932find_buffer(typval_T *avar)
1933{
1934 buf_T *buf = NULL;
1935
1936 if (avar->v_type == VAR_NUMBER)
1937 buf = buflist_findnr((int)avar->vval.v_number);
1938 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1939 {
1940 buf = buflist_findname_exp(avar->vval.v_string);
1941 if (buf == NULL)
1942 {
1943 /* No full path name match, try a match with a URL or a "nofile"
1944 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001945 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001946 if (buf->b_fname != NULL
1947 && (path_with_url(buf->b_fname)
1948#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02001949 || bt_nofilename(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001950#endif
1951 )
1952 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1953 break;
1954 }
1955 }
1956 return buf;
1957}
1958
1959/*
1960 * "bufexists(expr)" function
1961 */
1962 static void
1963f_bufexists(typval_T *argvars, typval_T *rettv)
1964{
1965 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1966}
1967
1968/*
1969 * "buflisted(expr)" function
1970 */
1971 static void
1972f_buflisted(typval_T *argvars, typval_T *rettv)
1973{
1974 buf_T *buf;
1975
1976 buf = find_buffer(&argvars[0]);
1977 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1978}
1979
1980/*
1981 * "bufloaded(expr)" function
1982 */
1983 static void
1984f_bufloaded(typval_T *argvars, typval_T *rettv)
1985{
1986 buf_T *buf;
1987
1988 buf = find_buffer(&argvars[0]);
1989 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1990}
1991
1992 buf_T *
1993buflist_find_by_name(char_u *name, int curtab_only)
1994{
1995 int save_magic;
1996 char_u *save_cpo;
1997 buf_T *buf;
1998
1999 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
2000 save_magic = p_magic;
2001 p_magic = TRUE;
2002 save_cpo = p_cpo;
2003 p_cpo = (char_u *)"";
2004
2005 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2006 TRUE, FALSE, curtab_only));
2007
2008 p_magic = save_magic;
2009 p_cpo = save_cpo;
2010 return buf;
2011}
2012
2013/*
2014 * Get buffer by number or pattern.
2015 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002016 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002017tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002018{
2019 char_u *name = tv->vval.v_string;
2020 buf_T *buf;
2021
2022 if (tv->v_type == VAR_NUMBER)
2023 return buflist_findnr((int)tv->vval.v_number);
2024 if (tv->v_type != VAR_STRING)
2025 return NULL;
2026 if (name == NULL || *name == NUL)
2027 return curbuf;
2028 if (name[0] == '$' && name[1] == NUL)
2029 return lastbuf;
2030
2031 buf = buflist_find_by_name(name, curtab_only);
2032
2033 /* If not found, try expanding the name, like done for bufexists(). */
2034 if (buf == NULL)
2035 buf = find_buffer(tv);
2036
2037 return buf;
2038}
2039
2040/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002041 * Get the buffer from "arg" and give an error and return NULL if it is not
2042 * valid.
2043 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002044 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002045get_buf_arg(typval_T *arg)
2046{
2047 buf_T *buf;
2048
2049 ++emsg_off;
2050 buf = tv_get_buf(arg, FALSE);
2051 --emsg_off;
2052 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002054 return buf;
2055}
2056
2057/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002058 * "bufname(expr)" function
2059 */
2060 static void
2061f_bufname(typval_T *argvars, typval_T *rettv)
2062{
2063 buf_T *buf;
2064
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002065 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002066 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002067 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002068 rettv->v_type = VAR_STRING;
2069 if (buf != NULL && buf->b_fname != NULL)
2070 rettv->vval.v_string = vim_strsave(buf->b_fname);
2071 else
2072 rettv->vval.v_string = NULL;
2073 --emsg_off;
2074}
2075
2076/*
2077 * "bufnr(expr)" function
2078 */
2079 static void
2080f_bufnr(typval_T *argvars, typval_T *rettv)
2081{
2082 buf_T *buf;
2083 int error = FALSE;
2084 char_u *name;
2085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002086 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002088 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002089 --emsg_off;
2090
2091 /* If the buffer isn't found and the second argument is not zero create a
2092 * new buffer. */
2093 if (buf == NULL
2094 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002095 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002096 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002097 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 && !error)
2099 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2100
2101 if (buf != NULL)
2102 rettv->vval.v_number = buf->b_fnum;
2103 else
2104 rettv->vval.v_number = -1;
2105}
2106
2107 static void
2108buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2109{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002110 win_T *wp;
2111 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 buf_T *buf;
2113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002114 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002116 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002117 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002118 {
2119 ++winnr;
2120 if (wp->w_buffer == buf)
2121 break;
2122 }
2123 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002124 --emsg_off;
2125}
2126
2127/*
2128 * "bufwinid(nr)" function
2129 */
2130 static void
2131f_bufwinid(typval_T *argvars, typval_T *rettv)
2132{
2133 buf_win_common(argvars, rettv, FALSE);
2134}
2135
2136/*
2137 * "bufwinnr(nr)" function
2138 */
2139 static void
2140f_bufwinnr(typval_T *argvars, typval_T *rettv)
2141{
2142 buf_win_common(argvars, rettv, TRUE);
2143}
2144
2145/*
2146 * "byte2line(byte)" function
2147 */
2148 static void
2149f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2150{
2151#ifndef FEAT_BYTEOFF
2152 rettv->vval.v_number = -1;
2153#else
2154 long boff = 0;
2155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002156 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 if (boff < 0)
2158 rettv->vval.v_number = -1;
2159 else
2160 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2161 (linenr_T)0, &boff);
2162#endif
2163}
2164
2165 static void
2166byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2167{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002168 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002169 char_u *str;
2170 varnumber_T idx;
2171
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002172 str = tv_get_string_chk(&argvars[0]);
2173 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174 rettv->vval.v_number = -1;
2175 if (str == NULL || idx < 0)
2176 return;
2177
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002178 t = str;
2179 for ( ; idx > 0; idx--)
2180 {
2181 if (*t == NUL) /* EOL reached */
2182 return;
2183 if (enc_utf8 && comp)
2184 t += utf_ptr2len(t);
2185 else
2186 t += (*mb_ptr2len)(t);
2187 }
2188 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002189}
2190
2191/*
2192 * "byteidx()" function
2193 */
2194 static void
2195f_byteidx(typval_T *argvars, typval_T *rettv)
2196{
2197 byteidx(argvars, rettv, FALSE);
2198}
2199
2200/*
2201 * "byteidxcomp()" function
2202 */
2203 static void
2204f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2205{
2206 byteidx(argvars, rettv, TRUE);
2207}
2208
2209/*
2210 * "call(func, arglist [, dict])" function
2211 */
2212 static void
2213f_call(typval_T *argvars, typval_T *rettv)
2214{
2215 char_u *func;
2216 partial_T *partial = NULL;
2217 dict_T *selfdict = NULL;
2218
2219 if (argvars[1].v_type != VAR_LIST)
2220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002221 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002222 return;
2223 }
2224 if (argvars[1].vval.v_list == NULL)
2225 return;
2226
2227 if (argvars[0].v_type == VAR_FUNC)
2228 func = argvars[0].vval.v_string;
2229 else if (argvars[0].v_type == VAR_PARTIAL)
2230 {
2231 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002232 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 }
2234 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002235 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002236 if (*func == NUL)
2237 return; /* type error or empty name */
2238
2239 if (argvars[2].v_type != VAR_UNKNOWN)
2240 {
2241 if (argvars[2].v_type != VAR_DICT)
2242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002244 return;
2245 }
2246 selfdict = argvars[2].vval.v_dict;
2247 }
2248
2249 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2250}
2251
2252#ifdef FEAT_FLOAT
2253/*
2254 * "ceil({float})" function
2255 */
2256 static void
2257f_ceil(typval_T *argvars, typval_T *rettv)
2258{
2259 float_T f = 0.0;
2260
2261 rettv->v_type = VAR_FLOAT;
2262 if (get_float_arg(argvars, &f) == OK)
2263 rettv->vval.v_float = ceil(f);
2264 else
2265 rettv->vval.v_float = 0.0;
2266}
2267#endif
2268
2269#ifdef FEAT_JOB_CHANNEL
2270/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002271 * "ch_canread()" function
2272 */
2273 static void
2274f_ch_canread(typval_T *argvars, typval_T *rettv)
2275{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002276 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002277
2278 rettv->vval.v_number = 0;
2279 if (channel != NULL)
2280 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2281 || channel_has_readahead(channel, PART_OUT)
2282 || channel_has_readahead(channel, PART_ERR);
2283}
2284
2285/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002286 * "ch_close()" function
2287 */
2288 static void
2289f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2290{
2291 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2292
2293 if (channel != NULL)
2294 {
2295 channel_close(channel, FALSE);
2296 channel_clear(channel);
2297 }
2298}
2299
2300/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002301 * "ch_close()" function
2302 */
2303 static void
2304f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2305{
2306 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2307
2308 if (channel != NULL)
2309 channel_close_in(channel);
2310}
2311
2312/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002313 * "ch_getbufnr()" function
2314 */
2315 static void
2316f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2317{
2318 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2319
2320 rettv->vval.v_number = -1;
2321 if (channel != NULL)
2322 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002323 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002324 int part;
2325
2326 if (STRCMP(what, "err") == 0)
2327 part = PART_ERR;
2328 else if (STRCMP(what, "out") == 0)
2329 part = PART_OUT;
2330 else if (STRCMP(what, "in") == 0)
2331 part = PART_IN;
2332 else
2333 part = PART_SOCK;
2334 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2335 rettv->vval.v_number =
2336 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2337 }
2338}
2339
2340/*
2341 * "ch_getjob()" function
2342 */
2343 static void
2344f_ch_getjob(typval_T *argvars, typval_T *rettv)
2345{
2346 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2347
2348 if (channel != NULL)
2349 {
2350 rettv->v_type = VAR_JOB;
2351 rettv->vval.v_job = channel->ch_job;
2352 if (channel->ch_job != NULL)
2353 ++channel->ch_job->jv_refcount;
2354 }
2355}
2356
2357/*
2358 * "ch_info()" function
2359 */
2360 static void
2361f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2362{
2363 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2364
2365 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2366 channel_info(channel, rettv->vval.v_dict);
2367}
2368
2369/*
2370 * "ch_log()" function
2371 */
2372 static void
2373f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2374{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002375 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002376 channel_T *channel = NULL;
2377
2378 if (argvars[1].v_type != VAR_UNKNOWN)
2379 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2380
Bram Moolenaard5359b22018-04-05 22:44:39 +02002381 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382}
2383
2384/*
2385 * "ch_logfile()" function
2386 */
2387 static void
2388f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2389{
2390 char_u *fname;
2391 char_u *opt = (char_u *)"";
2392 char_u buf[NUMBUFLEN];
2393
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002394 /* Don't open a file in restricted mode. */
2395 if (check_restricted() || check_secure())
2396 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002397 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002398 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002399 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002400 ch_logfile(fname, opt);
2401}
2402
2403/*
2404 * "ch_open()" function
2405 */
2406 static void
2407f_ch_open(typval_T *argvars, typval_T *rettv)
2408{
2409 rettv->v_type = VAR_CHANNEL;
2410 if (check_restricted() || check_secure())
2411 return;
2412 rettv->vval.v_channel = channel_open_func(argvars);
2413}
2414
2415/*
2416 * "ch_read()" function
2417 */
2418 static void
2419f_ch_read(typval_T *argvars, typval_T *rettv)
2420{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002421 common_channel_read(argvars, rettv, FALSE, FALSE);
2422}
2423
2424/*
2425 * "ch_readblob()" function
2426 */
2427 static void
2428f_ch_readblob(typval_T *argvars, typval_T *rettv)
2429{
2430 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002431}
2432
2433/*
2434 * "ch_readraw()" function
2435 */
2436 static void
2437f_ch_readraw(typval_T *argvars, typval_T *rettv)
2438{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002439 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002440}
2441
2442/*
2443 * "ch_evalexpr()" function
2444 */
2445 static void
2446f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2447{
2448 ch_expr_common(argvars, rettv, TRUE);
2449}
2450
2451/*
2452 * "ch_sendexpr()" function
2453 */
2454 static void
2455f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2456{
2457 ch_expr_common(argvars, rettv, FALSE);
2458}
2459
2460/*
2461 * "ch_evalraw()" function
2462 */
2463 static void
2464f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2465{
2466 ch_raw_common(argvars, rettv, TRUE);
2467}
2468
2469/*
2470 * "ch_sendraw()" function
2471 */
2472 static void
2473f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2474{
2475 ch_raw_common(argvars, rettv, FALSE);
2476}
2477
2478/*
2479 * "ch_setoptions()" function
2480 */
2481 static void
2482f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2483{
2484 channel_T *channel;
2485 jobopt_T opt;
2486
2487 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2488 if (channel == NULL)
2489 return;
2490 clear_job_options(&opt);
2491 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002492 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493 channel_set_options(channel, &opt);
2494 free_job_options(&opt);
2495}
2496
2497/*
2498 * "ch_status()" function
2499 */
2500 static void
2501f_ch_status(typval_T *argvars, typval_T *rettv)
2502{
2503 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002504 jobopt_T opt;
2505 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002506
2507 /* return an empty string by default */
2508 rettv->v_type = VAR_STRING;
2509 rettv->vval.v_string = NULL;
2510
2511 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002512
2513 if (argvars[1].v_type != VAR_UNKNOWN)
2514 {
2515 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002516 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002517 && (opt.jo_set & JO_PART))
2518 part = opt.jo_part;
2519 }
2520
2521 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522}
2523#endif
2524
2525/*
2526 * "changenr()" function
2527 */
2528 static void
2529f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2530{
2531 rettv->vval.v_number = curbuf->b_u_seq_cur;
2532}
2533
2534/*
2535 * "char2nr(string)" function
2536 */
2537 static void
2538f_char2nr(typval_T *argvars, typval_T *rettv)
2539{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002540 if (has_mbyte)
2541 {
2542 int utf8 = 0;
2543
2544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002545 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546
2547 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002548 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002549 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002550 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551 }
2552 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002553 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002554}
2555
2556/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002557 * "chdir(dir)" function
2558 */
2559 static void
2560f_chdir(typval_T *argvars, typval_T *rettv)
2561{
2562 char_u *cwd;
2563 cdscope_T scope = CDSCOPE_GLOBAL;
2564
2565 rettv->v_type = VAR_STRING;
2566 rettv->vval.v_string = NULL;
2567
2568 if (argvars[0].v_type != VAR_STRING)
2569 return;
2570
2571 // Return the current directory
2572 cwd = alloc(MAXPATHL);
2573 if (cwd != NULL)
2574 {
2575 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2576 {
2577#ifdef BACKSLASH_IN_FILENAME
2578 slash_adjust(cwd);
2579#endif
2580 rettv->vval.v_string = vim_strsave(cwd);
2581 }
2582 vim_free(cwd);
2583 }
2584
2585 if (curwin->w_localdir != NULL)
2586 scope = CDSCOPE_WINDOW;
2587 else if (curtab->tp_localdir != NULL)
2588 scope = CDSCOPE_TABPAGE;
2589
2590 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2591 // Directory change failed
2592 VIM_CLEAR(rettv->vval.v_string);
2593}
2594
2595/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002596 * "cindent(lnum)" function
2597 */
2598 static void
2599f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2600{
2601#ifdef FEAT_CINDENT
2602 pos_T pos;
2603 linenr_T lnum;
2604
2605 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002606 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002607 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2608 {
2609 curwin->w_cursor.lnum = lnum;
2610 rettv->vval.v_number = get_c_indent();
2611 curwin->w_cursor = pos;
2612 }
2613 else
2614#endif
2615 rettv->vval.v_number = -1;
2616}
2617
Bram Moolenaaraff74912019-03-30 18:11:49 +01002618 static win_T *
2619get_optional_window(typval_T *argvars, int idx)
2620{
2621 win_T *win = curwin;
2622
2623 if (argvars[idx].v_type != VAR_UNKNOWN)
2624 {
2625 win = find_win_by_nr_or_id(&argvars[idx]);
2626 if (win == NULL)
2627 {
2628 emsg(_(e_invalwindow));
2629 return NULL;
2630 }
2631 }
2632 return win;
2633}
2634
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002635/*
2636 * "clearmatches()" function
2637 */
2638 static void
2639f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2640{
2641#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002642 win_T *win = get_optional_window(argvars, 0);
2643
2644 if (win != NULL)
2645 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002646#endif
2647}
2648
2649/*
2650 * "col(string)" function
2651 */
2652 static void
2653f_col(typval_T *argvars, typval_T *rettv)
2654{
2655 colnr_T col = 0;
2656 pos_T *fp;
2657 int fnum = curbuf->b_fnum;
2658
2659 fp = var2fpos(&argvars[0], FALSE, &fnum);
2660 if (fp != NULL && fnum == curbuf->b_fnum)
2661 {
2662 if (fp->col == MAXCOL)
2663 {
2664 /* '> can be MAXCOL, get the length of the line then */
2665 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2666 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2667 else
2668 col = MAXCOL;
2669 }
2670 else
2671 {
2672 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 /* col(".") when the cursor is on the NUL at the end of the line
2674 * because of "coladd" can be seen as an extra column. */
2675 if (virtual_active() && fp == &curwin->w_cursor)
2676 {
2677 char_u *p = ml_get_cursor();
2678
2679 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2680 curwin->w_virtcol - curwin->w_cursor.coladd))
2681 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002682 int l;
2683
2684 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2685 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002686 }
2687 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 }
2689 }
2690 rettv->vval.v_number = col;
2691}
2692
2693#if defined(FEAT_INS_EXPAND)
2694/*
2695 * "complete()" function
2696 */
2697 static void
2698f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2699{
2700 int startcol;
2701
2702 if ((State & INSERT) == 0)
2703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002704 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 return;
2706 }
2707
2708 /* Check for undo allowed here, because if something was already inserted
2709 * the line was already saved for undo and this check isn't done. */
2710 if (!undo_allowed())
2711 return;
2712
2713 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002715 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002716 return;
2717 }
2718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002719 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002720 if (startcol <= 0)
2721 return;
2722
2723 set_completion(startcol - 1, argvars[1].vval.v_list);
2724}
2725
2726/*
2727 * "complete_add()" function
2728 */
2729 static void
2730f_complete_add(typval_T *argvars, typval_T *rettv)
2731{
2732 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2733}
2734
2735/*
2736 * "complete_check()" function
2737 */
2738 static void
2739f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2740{
2741 int saved = RedrawingDisabled;
2742
2743 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002744 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002745 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746 RedrawingDisabled = saved;
2747}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002748
2749/*
2750 * "complete_info()" function
2751 */
2752 static void
2753f_complete_info(typval_T *argvars, typval_T *rettv)
2754{
2755 list_T *what_list = NULL;
2756
2757 if (rettv_dict_alloc(rettv) != OK)
2758 return;
2759
2760 if (argvars[0].v_type != VAR_UNKNOWN)
2761 {
2762 if (argvars[0].v_type != VAR_LIST)
2763 {
2764 emsg(_(e_listreq));
2765 return;
2766 }
2767 what_list = argvars[0].vval.v_list;
2768 }
2769 get_complete_info(what_list, rettv->vval.v_dict);
2770}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002771#endif
2772
2773/*
2774 * "confirm(message, buttons[, default [, type]])" function
2775 */
2776 static void
2777f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2778{
2779#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2780 char_u *message;
2781 char_u *buttons = NULL;
2782 char_u buf[NUMBUFLEN];
2783 char_u buf2[NUMBUFLEN];
2784 int def = 1;
2785 int type = VIM_GENERIC;
2786 char_u *typestr;
2787 int error = FALSE;
2788
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002789 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 if (message == NULL)
2791 error = TRUE;
2792 if (argvars[1].v_type != VAR_UNKNOWN)
2793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002794 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002795 if (buttons == NULL)
2796 error = TRUE;
2797 if (argvars[2].v_type != VAR_UNKNOWN)
2798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 if (argvars[3].v_type != VAR_UNKNOWN)
2801 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002802 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002803 if (typestr == NULL)
2804 error = TRUE;
2805 else
2806 {
2807 switch (TOUPPER_ASC(*typestr))
2808 {
2809 case 'E': type = VIM_ERROR; break;
2810 case 'Q': type = VIM_QUESTION; break;
2811 case 'I': type = VIM_INFO; break;
2812 case 'W': type = VIM_WARNING; break;
2813 case 'G': type = VIM_GENERIC; break;
2814 }
2815 }
2816 }
2817 }
2818 }
2819
2820 if (buttons == NULL || *buttons == NUL)
2821 buttons = (char_u *)_("&Ok");
2822
2823 if (!error)
2824 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2825 def, NULL, FALSE);
2826#endif
2827}
2828
2829/*
2830 * "copy()" function
2831 */
2832 static void
2833f_copy(typval_T *argvars, typval_T *rettv)
2834{
2835 item_copy(&argvars[0], rettv, FALSE, 0);
2836}
2837
2838#ifdef FEAT_FLOAT
2839/*
2840 * "cos()" function
2841 */
2842 static void
2843f_cos(typval_T *argvars, typval_T *rettv)
2844{
2845 float_T f = 0.0;
2846
2847 rettv->v_type = VAR_FLOAT;
2848 if (get_float_arg(argvars, &f) == OK)
2849 rettv->vval.v_float = cos(f);
2850 else
2851 rettv->vval.v_float = 0.0;
2852}
2853
2854/*
2855 * "cosh()" function
2856 */
2857 static void
2858f_cosh(typval_T *argvars, typval_T *rettv)
2859{
2860 float_T f = 0.0;
2861
2862 rettv->v_type = VAR_FLOAT;
2863 if (get_float_arg(argvars, &f) == OK)
2864 rettv->vval.v_float = cosh(f);
2865 else
2866 rettv->vval.v_float = 0.0;
2867}
2868#endif
2869
2870/*
2871 * "count()" function
2872 */
2873 static void
2874f_count(typval_T *argvars, typval_T *rettv)
2875{
2876 long n = 0;
2877 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002878 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002879
Bram Moolenaar9966b212017-07-28 16:46:57 +02002880 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002881 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002882
2883 if (argvars[0].v_type == VAR_STRING)
2884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002885 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002886 char_u *p = argvars[0].vval.v_string;
2887 char_u *next;
2888
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002889 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002890 {
2891 if (ic)
2892 {
2893 size_t len = STRLEN(expr);
2894
2895 while (*p != NUL)
2896 {
2897 if (MB_STRNICMP(p, expr, len) == 0)
2898 {
2899 ++n;
2900 p += len;
2901 }
2902 else
2903 MB_PTR_ADV(p);
2904 }
2905 }
2906 else
2907 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2908 != NULL)
2909 {
2910 ++n;
2911 p = next + STRLEN(expr);
2912 }
2913 }
2914
2915 }
2916 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 {
2918 listitem_T *li;
2919 list_T *l;
2920 long idx;
2921
2922 if ((l = argvars[0].vval.v_list) != NULL)
2923 {
2924 li = l->lv_first;
2925 if (argvars[2].v_type != VAR_UNKNOWN)
2926 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 if (argvars[3].v_type != VAR_UNKNOWN)
2928 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002929 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002930 if (!error)
2931 {
2932 li = list_find(l, idx);
2933 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002934 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002935 }
2936 }
2937 if (error)
2938 li = NULL;
2939 }
2940
2941 for ( ; li != NULL; li = li->li_next)
2942 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2943 ++n;
2944 }
2945 }
2946 else if (argvars[0].v_type == VAR_DICT)
2947 {
2948 int todo;
2949 dict_T *d;
2950 hashitem_T *hi;
2951
2952 if ((d = argvars[0].vval.v_dict) != NULL)
2953 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002954 if (argvars[2].v_type != VAR_UNKNOWN)
2955 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002957 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002958 }
2959
2960 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2961 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2962 {
2963 if (!HASHITEM_EMPTY(hi))
2964 {
2965 --todo;
2966 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2967 ++n;
2968 }
2969 }
2970 }
2971 }
2972 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002973 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002974 rettv->vval.v_number = n;
2975}
2976
2977/*
2978 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2979 *
2980 * Checks the existence of a cscope connection.
2981 */
2982 static void
2983f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2984{
2985#ifdef FEAT_CSCOPE
2986 int num = 0;
2987 char_u *dbpath = NULL;
2988 char_u *prepend = NULL;
2989 char_u buf[NUMBUFLEN];
2990
2991 if (argvars[0].v_type != VAR_UNKNOWN
2992 && argvars[1].v_type != VAR_UNKNOWN)
2993 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002994 num = (int)tv_get_number(&argvars[0]);
2995 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002997 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 }
2999
3000 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
3001#endif
3002}
3003
3004/*
3005 * "cursor(lnum, col)" function, or
3006 * "cursor(list)"
3007 *
3008 * Moves the cursor to the specified line and column.
3009 * Returns 0 when the position could be set, -1 otherwise.
3010 */
3011 static void
3012f_cursor(typval_T *argvars, typval_T *rettv)
3013{
3014 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 int set_curswant = TRUE;
3017
3018 rettv->vval.v_number = -1;
3019 if (argvars[1].v_type == VAR_UNKNOWN)
3020 {
3021 pos_T pos;
3022 colnr_T curswant = -1;
3023
3024 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003026 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 return;
3028 }
3029 line = pos.lnum;
3030 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003032 if (curswant >= 0)
3033 {
3034 curwin->w_curswant = curswant - 1;
3035 set_curswant = FALSE;
3036 }
3037 }
3038 else
3039 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003040 line = tv_get_lnum(argvars);
3041 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003043 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003044 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003045 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003046 return; /* type error; errmsg already given */
3047 if (line > 0)
3048 curwin->w_cursor.lnum = line;
3049 if (col > 0)
3050 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003051 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052
3053 /* Make sure the cursor is in a valid position. */
3054 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055 /* Correct cursor for multi-byte character. */
3056 if (has_mbyte)
3057 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003058
3059 curwin->w_set_curswant = set_curswant;
3060 rettv->vval.v_number = 0;
3061}
3062
Bram Moolenaar4f974752019-02-17 17:44:42 +01003063#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003064/*
3065 * "debugbreak()" function
3066 */
3067 static void
3068f_debugbreak(typval_T *argvars, typval_T *rettv)
3069{
3070 int pid;
3071
3072 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003073 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003074 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003075 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003076 else
3077 {
3078 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3079
3080 if (hProcess != NULL)
3081 {
3082 DebugBreakProcess(hProcess);
3083 CloseHandle(hProcess);
3084 rettv->vval.v_number = OK;
3085 }
3086 }
3087}
3088#endif
3089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090/*
3091 * "deepcopy()" function
3092 */
3093 static void
3094f_deepcopy(typval_T *argvars, typval_T *rettv)
3095{
3096 int noref = 0;
3097 int copyID;
3098
3099 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003100 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003101 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 else
3104 {
3105 copyID = get_copyID();
3106 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3107 }
3108}
3109
3110/*
3111 * "delete()" function
3112 */
3113 static void
3114f_delete(typval_T *argvars, typval_T *rettv)
3115{
3116 char_u nbuf[NUMBUFLEN];
3117 char_u *name;
3118 char_u *flags;
3119
3120 rettv->vval.v_number = -1;
3121 if (check_restricted() || check_secure())
3122 return;
3123
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003124 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 if (name == NULL || *name == NUL)
3126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003127 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003128 return;
3129 }
3130
3131 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003132 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003133 else
3134 flags = (char_u *)"";
3135
3136 if (*flags == NUL)
3137 /* delete a file */
3138 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3139 else if (STRCMP(flags, "d") == 0)
3140 /* delete an empty directory */
3141 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3142 else if (STRCMP(flags, "rf") == 0)
3143 /* delete a directory recursively */
3144 rettv->vval.v_number = delete_recursive(name);
3145 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003146 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003147}
3148
3149/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003150 * "deletebufline()" function
3151 */
3152 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003153f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003154{
3155 buf_T *buf;
3156 linenr_T first, last;
3157 linenr_T lnum;
3158 long count;
3159 int is_curbuf;
3160 buf_T *curbuf_save = NULL;
3161 win_T *curwin_save = NULL;
3162 tabpage_T *tp;
3163 win_T *wp;
3164
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003165 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003166 if (buf == NULL)
3167 {
3168 rettv->vval.v_number = 1; /* FAIL */
3169 return;
3170 }
3171 is_curbuf = buf == curbuf;
3172
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003173 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003174 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003175 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003176 else
3177 last = first;
3178
3179 if (buf->b_ml.ml_mfp == NULL || first < 1
3180 || first > buf->b_ml.ml_line_count || last < first)
3181 {
3182 rettv->vval.v_number = 1; /* FAIL */
3183 return;
3184 }
3185
3186 if (!is_curbuf)
3187 {
3188 curbuf_save = curbuf;
3189 curwin_save = curwin;
3190 curbuf = buf;
3191 find_win_for_curbuf();
3192 }
3193 if (last > curbuf->b_ml.ml_line_count)
3194 last = curbuf->b_ml.ml_line_count;
3195 count = last - first + 1;
3196
3197 // When coming here from Insert mode, sync undo, so that this can be
3198 // undone separately from what was previously inserted.
3199 if (u_sync_once == 2)
3200 {
3201 u_sync_once = 1; // notify that u_sync() was called
3202 u_sync(TRUE);
3203 }
3204
3205 if (u_save(first - 1, last + 1) == FAIL)
3206 {
3207 rettv->vval.v_number = 1; /* FAIL */
3208 return;
3209 }
3210
3211 for (lnum = first; lnum <= last; ++lnum)
3212 ml_delete(first, TRUE);
3213
3214 FOR_ALL_TAB_WINDOWS(tp, wp)
3215 if (wp->w_buffer == buf)
3216 {
3217 if (wp->w_cursor.lnum > last)
3218 wp->w_cursor.lnum -= count;
3219 else if (wp->w_cursor.lnum> first)
3220 wp->w_cursor.lnum = first;
3221 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3222 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3223 }
3224 check_cursor_col();
3225 deleted_lines_mark(first, count);
3226
3227 if (!is_curbuf)
3228 {
3229 curbuf = curbuf_save;
3230 curwin = curwin_save;
3231 }
3232}
3233
3234/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003235 * "did_filetype()" function
3236 */
3237 static void
3238f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3239{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003240 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003241}
3242
3243/*
3244 * "diff_filler()" function
3245 */
3246 static void
3247f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3248{
3249#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003250 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251#endif
3252}
3253
3254/*
3255 * "diff_hlID()" function
3256 */
3257 static void
3258f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3259{
3260#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003261 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003263 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003264 static int fnum = 0;
3265 static int change_start = 0;
3266 static int change_end = 0;
3267 static hlf_T hlID = (hlf_T)0;
3268 int filler_lines;
3269 int col;
3270
3271 if (lnum < 0) /* ignore type error in {lnum} arg */
3272 lnum = 0;
3273 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003274 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003275 || fnum != curbuf->b_fnum)
3276 {
3277 /* New line, buffer, change: need to get the values. */
3278 filler_lines = diff_check(curwin, lnum);
3279 if (filler_lines < 0)
3280 {
3281 if (filler_lines == -1)
3282 {
3283 change_start = MAXCOL;
3284 change_end = -1;
3285 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3286 hlID = HLF_ADD; /* added line */
3287 else
3288 hlID = HLF_CHD; /* changed line */
3289 }
3290 else
3291 hlID = HLF_ADD; /* added line */
3292 }
3293 else
3294 hlID = (hlf_T)0;
3295 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003296 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003297 fnum = curbuf->b_fnum;
3298 }
3299
3300 if (hlID == HLF_CHD || hlID == HLF_TXD)
3301 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003302 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003303 if (col >= change_start && col <= change_end)
3304 hlID = HLF_TXD; /* changed text */
3305 else
3306 hlID = HLF_CHD; /* changed line */
3307 }
3308 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3309#endif
3310}
3311
3312/*
3313 * "empty({expr})" function
3314 */
3315 static void
3316f_empty(typval_T *argvars, typval_T *rettv)
3317{
3318 int n = FALSE;
3319
3320 switch (argvars[0].v_type)
3321 {
3322 case VAR_STRING:
3323 case VAR_FUNC:
3324 n = argvars[0].vval.v_string == NULL
3325 || *argvars[0].vval.v_string == NUL;
3326 break;
3327 case VAR_PARTIAL:
3328 n = FALSE;
3329 break;
3330 case VAR_NUMBER:
3331 n = argvars[0].vval.v_number == 0;
3332 break;
3333 case VAR_FLOAT:
3334#ifdef FEAT_FLOAT
3335 n = argvars[0].vval.v_float == 0.0;
3336 break;
3337#endif
3338 case VAR_LIST:
3339 n = argvars[0].vval.v_list == NULL
3340 || argvars[0].vval.v_list->lv_first == NULL;
3341 break;
3342 case VAR_DICT:
3343 n = argvars[0].vval.v_dict == NULL
3344 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3345 break;
3346 case VAR_SPECIAL:
3347 n = argvars[0].vval.v_number != VVAL_TRUE;
3348 break;
3349
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003350 case VAR_BLOB:
3351 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003352 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3353 break;
3354
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003355 case VAR_JOB:
3356#ifdef FEAT_JOB_CHANNEL
3357 n = argvars[0].vval.v_job == NULL
3358 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3359 break;
3360#endif
3361 case VAR_CHANNEL:
3362#ifdef FEAT_JOB_CHANNEL
3363 n = argvars[0].vval.v_channel == NULL
3364 || !channel_is_open(argvars[0].vval.v_channel);
3365 break;
3366#endif
3367 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003368 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003369 n = TRUE;
3370 break;
3371 }
3372
3373 rettv->vval.v_number = n;
3374}
3375
3376/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003377 * "environ()" function
3378 */
3379 static void
3380f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3381{
3382#if !defined(AMIGA)
3383 int i = 0;
3384 char_u *entry, *value;
3385# ifdef MSWIN
3386 extern wchar_t **_wenviron;
3387# else
3388 extern char **environ;
3389# endif
3390
3391 if (rettv_dict_alloc(rettv) != OK)
3392 return;
3393
3394# ifdef MSWIN
3395 if (*_wenviron == NULL)
3396 return;
3397# else
3398 if (*environ == NULL)
3399 return;
3400# endif
3401
3402 for (i = 0; ; ++i)
3403 {
3404# ifdef MSWIN
3405 short_u *p;
3406
3407 if ((p = (short_u *)_wenviron[i]) == NULL)
3408 return;
3409 entry = utf16_to_enc(p, NULL);
3410# else
3411 if ((entry = (char_u *)environ[i]) == NULL)
3412 return;
3413 entry = vim_strsave(entry);
3414# endif
3415 if (entry == NULL) // out of memory
3416 return;
3417 if ((value = vim_strchr(entry, '=')) == NULL)
3418 {
3419 vim_free(entry);
3420 continue;
3421 }
3422 *value++ = NUL;
3423 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3424 vim_free(entry);
3425 }
3426#endif
3427}
3428
3429/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003430 * "escape({string}, {chars})" function
3431 */
3432 static void
3433f_escape(typval_T *argvars, typval_T *rettv)
3434{
3435 char_u buf[NUMBUFLEN];
3436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003437 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3438 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003439 rettv->v_type = VAR_STRING;
3440}
3441
3442/*
3443 * "eval()" function
3444 */
3445 static void
3446f_eval(typval_T *argvars, typval_T *rettv)
3447{
3448 char_u *s, *p;
3449
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003450 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003451 if (s != NULL)
3452 s = skipwhite(s);
3453
3454 p = s;
3455 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3456 {
3457 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003458 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003459 need_clr_eos = FALSE;
3460 rettv->v_type = VAR_NUMBER;
3461 rettv->vval.v_number = 0;
3462 }
3463 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003464 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465}
3466
3467/*
3468 * "eventhandler()" function
3469 */
3470 static void
3471f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3472{
3473 rettv->vval.v_number = vgetc_busy;
3474}
3475
3476/*
3477 * "executable()" function
3478 */
3479 static void
3480f_executable(typval_T *argvars, typval_T *rettv)
3481{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003482 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003483
3484 /* Check in $PATH and also check directly if there is a directory name. */
3485 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3486 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3487}
3488
3489static garray_T redir_execute_ga;
3490
3491/*
3492 * Append "value[value_len]" to the execute() output.
3493 */
3494 void
3495execute_redir_str(char_u *value, int value_len)
3496{
3497 int len;
3498
3499 if (value_len == -1)
3500 len = (int)STRLEN(value); /* Append the entire string */
3501 else
3502 len = value_len; /* Append only "value_len" characters */
3503 if (ga_grow(&redir_execute_ga, len) == OK)
3504 {
3505 mch_memmove((char *)redir_execute_ga.ga_data
3506 + redir_execute_ga.ga_len, value, len);
3507 redir_execute_ga.ga_len += len;
3508 }
3509}
3510
3511/*
3512 * Get next line from a list.
3513 * Called by do_cmdline() to get the next line.
3514 * Returns allocated string, or NULL for end of function.
3515 */
3516
3517 static char_u *
3518get_list_line(
3519 int c UNUSED,
3520 void *cookie,
3521 int indent UNUSED)
3522{
3523 listitem_T **p = (listitem_T **)cookie;
3524 listitem_T *item = *p;
3525 char_u buf[NUMBUFLEN];
3526 char_u *s;
3527
3528 if (item == NULL)
3529 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003530 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531 *p = item->li_next;
3532 return s == NULL ? NULL : vim_strsave(s);
3533}
3534
3535/*
3536 * "execute()" function
3537 */
3538 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003539execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003540{
3541 char_u *cmd = NULL;
3542 list_T *list = NULL;
3543 int save_msg_silent = msg_silent;
3544 int save_emsg_silent = emsg_silent;
3545 int save_emsg_noredir = emsg_noredir;
3546 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003547 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003549 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003550 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551
3552 rettv->vval.v_string = NULL;
3553 rettv->v_type = VAR_STRING;
3554
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003555 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003557 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 if (list == NULL || list->lv_first == NULL)
3559 /* empty list, no commands, empty output */
3560 return;
3561 ++list->lv_refcount;
3562 }
3563 else
3564 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003565 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 if (cmd == NULL)
3567 return;
3568 }
3569
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003570 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571 {
3572 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003573 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003574
3575 if (s == NULL)
3576 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003577 if (*s == NUL)
3578 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579 if (STRNCMP(s, "silent", 6) == 0)
3580 ++msg_silent;
3581 if (STRCMP(s, "silent!") == 0)
3582 {
3583 emsg_silent = TRUE;
3584 emsg_noredir = TRUE;
3585 }
3586 }
3587 else
3588 ++msg_silent;
3589
3590 if (redir_execute)
3591 save_ga = redir_execute_ga;
3592 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3593 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003594 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003595 if (!echo_output)
3596 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003597
3598 if (cmd != NULL)
3599 do_cmdline_cmd(cmd);
3600 else
3601 {
3602 listitem_T *item = list->lv_first;
3603
3604 do_cmdline(NULL, get_list_line, (void *)&item,
3605 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3606 --list->lv_refcount;
3607 }
3608
Bram Moolenaard297f352017-01-29 20:31:21 +01003609 /* Need to append a NUL to the result. */
3610 if (ga_grow(&redir_execute_ga, 1) == OK)
3611 {
3612 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3613 rettv->vval.v_string = redir_execute_ga.ga_data;
3614 }
3615 else
3616 {
3617 ga_clear(&redir_execute_ga);
3618 rettv->vval.v_string = NULL;
3619 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620 msg_silent = save_msg_silent;
3621 emsg_silent = save_emsg_silent;
3622 emsg_noredir = save_emsg_noredir;
3623
3624 redir_execute = save_redir_execute;
3625 if (redir_execute)
3626 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003627 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003628
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003629 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003630 if (echo_output)
3631 // When not working silently: put it in column zero. A following
3632 // "echon" will overwrite the message, unavoidably.
3633 msg_col = 0;
3634 else
3635 // When working silently: Put it back where it was, since nothing
3636 // should have been written.
3637 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638}
3639
3640/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003641 * "execute()" function
3642 */
3643 static void
3644f_execute(typval_T *argvars, typval_T *rettv)
3645{
3646 execute_common(argvars, rettv, 0);
3647}
3648
3649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003650 * "exepath()" function
3651 */
3652 static void
3653f_exepath(typval_T *argvars, typval_T *rettv)
3654{
3655 char_u *p = NULL;
3656
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003657 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003658 rettv->v_type = VAR_STRING;
3659 rettv->vval.v_string = p;
3660}
3661
3662/*
3663 * "exists()" function
3664 */
3665 static void
3666f_exists(typval_T *argvars, typval_T *rettv)
3667{
3668 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003671 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 if (*p == '$') /* environment variable */
3673 {
3674 /* first try "normal" environment variables (fast) */
3675 if (mch_getenv(p + 1) != NULL)
3676 n = TRUE;
3677 else
3678 {
3679 /* try expanding things like $VIM and ${HOME} */
3680 p = expand_env_save(p);
3681 if (p != NULL && *p != '$')
3682 n = TRUE;
3683 vim_free(p);
3684 }
3685 }
3686 else if (*p == '&' || *p == '+') /* option */
3687 {
3688 n = (get_option_tv(&p, NULL, TRUE) == OK);
3689 if (*skipwhite(p) != NUL)
3690 n = FALSE; /* trailing garbage */
3691 }
3692 else if (*p == '*') /* internal or user defined function */
3693 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003694 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 }
3696 else if (*p == ':')
3697 {
3698 n = cmd_exists(p + 1);
3699 }
3700 else if (*p == '#')
3701 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003702 if (p[1] == '#')
3703 n = autocmd_supported(p + 2);
3704 else
3705 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003706 }
3707 else /* internal variable */
3708 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003709 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003710 }
3711
3712 rettv->vval.v_number = n;
3713}
3714
3715#ifdef FEAT_FLOAT
3716/*
3717 * "exp()" function
3718 */
3719 static void
3720f_exp(typval_T *argvars, typval_T *rettv)
3721{
3722 float_T f = 0.0;
3723
3724 rettv->v_type = VAR_FLOAT;
3725 if (get_float_arg(argvars, &f) == OK)
3726 rettv->vval.v_float = exp(f);
3727 else
3728 rettv->vval.v_float = 0.0;
3729}
3730#endif
3731
3732/*
3733 * "expand()" function
3734 */
3735 static void
3736f_expand(typval_T *argvars, typval_T *rettv)
3737{
3738 char_u *s;
3739 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003740 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3742 expand_T xpc;
3743 int error = FALSE;
3744 char_u *result;
3745
3746 rettv->v_type = VAR_STRING;
3747 if (argvars[1].v_type != VAR_UNKNOWN
3748 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003749 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003750 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003751 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003752
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003753 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003754 if (*s == '%' || *s == '#' || *s == '<')
3755 {
3756 ++emsg_off;
3757 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3758 --emsg_off;
3759 if (rettv->v_type == VAR_LIST)
3760 {
3761 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3762 list_append_string(rettv->vval.v_list, result, -1);
3763 else
3764 vim_free(result);
3765 }
3766 else
3767 rettv->vval.v_string = result;
3768 }
3769 else
3770 {
3771 /* When the optional second argument is non-zero, don't remove matches
3772 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3773 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003774 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003775 options |= WILD_KEEP_ALL;
3776 if (!error)
3777 {
3778 ExpandInit(&xpc);
3779 xpc.xp_context = EXPAND_FILES;
3780 if (p_wic)
3781 options += WILD_ICASE;
3782 if (rettv->v_type == VAR_STRING)
3783 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3784 options, WILD_ALL);
3785 else if (rettv_list_alloc(rettv) != FAIL)
3786 {
3787 int i;
3788
3789 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3790 for (i = 0; i < xpc.xp_numfiles; i++)
3791 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3792 ExpandCleanup(&xpc);
3793 }
3794 }
3795 else
3796 rettv->vval.v_string = NULL;
3797 }
3798}
3799
3800/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003801 * "expandcmd()" function
3802 * Expand all the special characters in a command string.
3803 */
3804 static void
3805f_expandcmd(typval_T *argvars, typval_T *rettv)
3806{
3807 exarg_T eap;
3808 char_u *cmdstr;
3809 char *errormsg = NULL;
3810
3811 rettv->v_type = VAR_STRING;
3812 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3813
3814 memset(&eap, 0, sizeof(eap));
3815 eap.cmd = cmdstr;
3816 eap.arg = cmdstr;
3817 eap.argt |= NOSPC;
3818 eap.usefilter = FALSE;
3819 eap.nextcmd = NULL;
3820 eap.cmdidx = CMD_USER;
3821
3822 expand_filename(&eap, &cmdstr, &errormsg);
3823 if (errormsg != NULL && *errormsg != NUL)
3824 emsg(errormsg);
3825
3826 rettv->vval.v_string = cmdstr;
3827}
3828
3829/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003830 * "extend(list, list [, idx])" function
3831 * "extend(dict, dict [, action])" function
3832 */
3833 static void
3834f_extend(typval_T *argvars, typval_T *rettv)
3835{
3836 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3837
3838 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3839 {
3840 list_T *l1, *l2;
3841 listitem_T *item;
3842 long before;
3843 int error = FALSE;
3844
3845 l1 = argvars[0].vval.v_list;
3846 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003847 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848 && l2 != NULL)
3849 {
3850 if (argvars[2].v_type != VAR_UNKNOWN)
3851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003852 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003853 if (error)
3854 return; /* type error; errmsg already given */
3855
3856 if (before == l1->lv_len)
3857 item = NULL;
3858 else
3859 {
3860 item = list_find(l1, before);
3861 if (item == NULL)
3862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003863 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003864 return;
3865 }
3866 }
3867 }
3868 else
3869 item = NULL;
3870 list_extend(l1, l2, item);
3871
3872 copy_tv(&argvars[0], rettv);
3873 }
3874 }
3875 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3876 {
3877 dict_T *d1, *d2;
3878 char_u *action;
3879 int i;
3880
3881 d1 = argvars[0].vval.v_dict;
3882 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003883 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003884 && d2 != NULL)
3885 {
3886 /* Check the third argument. */
3887 if (argvars[2].v_type != VAR_UNKNOWN)
3888 {
3889 static char *(av[]) = {"keep", "force", "error"};
3890
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003891 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003892 if (action == NULL)
3893 return; /* type error; errmsg already given */
3894 for (i = 0; i < 3; ++i)
3895 if (STRCMP(action, av[i]) == 0)
3896 break;
3897 if (i == 3)
3898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003899 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 return;
3901 }
3902 }
3903 else
3904 action = (char_u *)"force";
3905
3906 dict_extend(d1, d2, action);
3907
3908 copy_tv(&argvars[0], rettv);
3909 }
3910 }
3911 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003912 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003913}
3914
3915/*
3916 * "feedkeys()" function
3917 */
3918 static void
3919f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3920{
3921 int remap = TRUE;
3922 int insert = FALSE;
3923 char_u *keys, *flags;
3924 char_u nbuf[NUMBUFLEN];
3925 int typed = FALSE;
3926 int execute = FALSE;
3927 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003928 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 char_u *keys_esc;
3930
3931 /* This is not allowed in the sandbox. If the commands would still be
3932 * executed in the sandbox it would be OK, but it probably happens later,
3933 * when "sandbox" is no longer set. */
3934 if (check_secure())
3935 return;
3936
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003937 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003938
3939 if (argvars[1].v_type != VAR_UNKNOWN)
3940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003941 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 for ( ; *flags != NUL; ++flags)
3943 {
3944 switch (*flags)
3945 {
3946 case 'n': remap = FALSE; break;
3947 case 'm': remap = TRUE; break;
3948 case 't': typed = TRUE; break;
3949 case 'i': insert = TRUE; break;
3950 case 'x': execute = TRUE; break;
3951 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003952 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003953 }
3954 }
3955 }
3956
3957 if (*keys != NUL || execute)
3958 {
3959 /* Need to escape K_SPECIAL and CSI before putting the string in the
3960 * typeahead buffer. */
3961 keys_esc = vim_strsave_escape_csi(keys);
3962 if (keys_esc != NULL)
3963 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003964 if (lowlevel)
3965 {
3966#ifdef USE_INPUT_BUF
3967 add_to_input_buf(keys, (int)STRLEN(keys));
3968#else
3969 emsg(_("E980: lowlevel input not supported"));
3970#endif
3971 }
3972 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003973 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003974 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003976 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003977#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003978 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003979#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003980 )
3981 typebuf_was_filled = TRUE;
3982 }
3983 vim_free(keys_esc);
3984
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003985 if (execute)
3986 {
3987 int save_msg_scroll = msg_scroll;
3988
3989 /* Avoid a 1 second delay when the keys start Insert mode. */
3990 msg_scroll = FALSE;
3991
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003992 if (!dangerous)
3993 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003994 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003995 if (!dangerous)
3996 --ex_normal_busy;
3997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003998 msg_scroll |= save_msg_scroll;
3999 }
4000 }
4001 }
4002}
4003
4004/*
4005 * "filereadable()" function
4006 */
4007 static void
4008f_filereadable(typval_T *argvars, typval_T *rettv)
4009{
4010 int fd;
4011 char_u *p;
4012 int n;
4013
4014#ifndef O_NONBLOCK
4015# define O_NONBLOCK 0
4016#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004017 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4019 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4020 {
4021 n = TRUE;
4022 close(fd);
4023 }
4024 else
4025 n = FALSE;
4026
4027 rettv->vval.v_number = n;
4028}
4029
4030/*
4031 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4032 * rights to write into.
4033 */
4034 static void
4035f_filewritable(typval_T *argvars, typval_T *rettv)
4036{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004037 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004038}
4039
4040 static void
4041findfilendir(
4042 typval_T *argvars UNUSED,
4043 typval_T *rettv,
4044 int find_what UNUSED)
4045{
4046#ifdef FEAT_SEARCHPATH
4047 char_u *fname;
4048 char_u *fresult = NULL;
4049 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4050 char_u *p;
4051 char_u pathbuf[NUMBUFLEN];
4052 int count = 1;
4053 int first = TRUE;
4054 int error = FALSE;
4055#endif
4056
4057 rettv->vval.v_string = NULL;
4058 rettv->v_type = VAR_STRING;
4059
4060#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004061 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004062
4063 if (argvars[1].v_type != VAR_UNKNOWN)
4064 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004065 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004066 if (p == NULL)
4067 error = TRUE;
4068 else
4069 {
4070 if (*p != NUL)
4071 path = p;
4072
4073 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004074 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004075 }
4076 }
4077
4078 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4079 error = TRUE;
4080
4081 if (*fname != NUL && !error)
4082 {
4083 do
4084 {
4085 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4086 vim_free(fresult);
4087 fresult = find_file_in_path_option(first ? fname : NULL,
4088 first ? (int)STRLEN(fname) : 0,
4089 0, first, path,
4090 find_what,
4091 curbuf->b_ffname,
4092 find_what == FINDFILE_DIR
4093 ? (char_u *)"" : curbuf->b_p_sua);
4094 first = FALSE;
4095
4096 if (fresult != NULL && rettv->v_type == VAR_LIST)
4097 list_append_string(rettv->vval.v_list, fresult, -1);
4098
4099 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4100 }
4101
4102 if (rettv->v_type == VAR_STRING)
4103 rettv->vval.v_string = fresult;
4104#endif
4105}
4106
4107/*
4108 * "filter()" function
4109 */
4110 static void
4111f_filter(typval_T *argvars, typval_T *rettv)
4112{
4113 filter_map(argvars, rettv, FALSE);
4114}
4115
4116/*
4117 * "finddir({fname}[, {path}[, {count}]])" function
4118 */
4119 static void
4120f_finddir(typval_T *argvars, typval_T *rettv)
4121{
4122 findfilendir(argvars, rettv, FINDFILE_DIR);
4123}
4124
4125/*
4126 * "findfile({fname}[, {path}[, {count}]])" function
4127 */
4128 static void
4129f_findfile(typval_T *argvars, typval_T *rettv)
4130{
4131 findfilendir(argvars, rettv, FINDFILE_FILE);
4132}
4133
4134#ifdef FEAT_FLOAT
4135/*
4136 * "float2nr({float})" function
4137 */
4138 static void
4139f_float2nr(typval_T *argvars, typval_T *rettv)
4140{
4141 float_T f = 0.0;
4142
4143 if (get_float_arg(argvars, &f) == OK)
4144 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004145 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004146 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004147 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004148 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 else
4150 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004151 }
4152}
4153
4154/*
4155 * "floor({float})" function
4156 */
4157 static void
4158f_floor(typval_T *argvars, typval_T *rettv)
4159{
4160 float_T f = 0.0;
4161
4162 rettv->v_type = VAR_FLOAT;
4163 if (get_float_arg(argvars, &f) == OK)
4164 rettv->vval.v_float = floor(f);
4165 else
4166 rettv->vval.v_float = 0.0;
4167}
4168
4169/*
4170 * "fmod()" function
4171 */
4172 static void
4173f_fmod(typval_T *argvars, typval_T *rettv)
4174{
4175 float_T fx = 0.0, fy = 0.0;
4176
4177 rettv->v_type = VAR_FLOAT;
4178 if (get_float_arg(argvars, &fx) == OK
4179 && get_float_arg(&argvars[1], &fy) == OK)
4180 rettv->vval.v_float = fmod(fx, fy);
4181 else
4182 rettv->vval.v_float = 0.0;
4183}
4184#endif
4185
4186/*
4187 * "fnameescape({string})" function
4188 */
4189 static void
4190f_fnameescape(typval_T *argvars, typval_T *rettv)
4191{
4192 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004193 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004194 rettv->v_type = VAR_STRING;
4195}
4196
4197/*
4198 * "fnamemodify({fname}, {mods})" function
4199 */
4200 static void
4201f_fnamemodify(typval_T *argvars, typval_T *rettv)
4202{
4203 char_u *fname;
4204 char_u *mods;
4205 int usedlen = 0;
4206 int len;
4207 char_u *fbuf = NULL;
4208 char_u buf[NUMBUFLEN];
4209
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004210 fname = tv_get_string_chk(&argvars[0]);
4211 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004212 if (fname == NULL || mods == NULL)
4213 fname = NULL;
4214 else
4215 {
4216 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004217 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004218 }
4219
4220 rettv->v_type = VAR_STRING;
4221 if (fname == NULL)
4222 rettv->vval.v_string = NULL;
4223 else
4224 rettv->vval.v_string = vim_strnsave(fname, len);
4225 vim_free(fbuf);
4226}
4227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004228/*
4229 * "foldclosed()" function
4230 */
4231 static void
4232foldclosed_both(
4233 typval_T *argvars UNUSED,
4234 typval_T *rettv,
4235 int end UNUSED)
4236{
4237#ifdef FEAT_FOLDING
4238 linenr_T lnum;
4239 linenr_T first, last;
4240
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004241 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004242 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4243 {
4244 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4245 {
4246 if (end)
4247 rettv->vval.v_number = (varnumber_T)last;
4248 else
4249 rettv->vval.v_number = (varnumber_T)first;
4250 return;
4251 }
4252 }
4253#endif
4254 rettv->vval.v_number = -1;
4255}
4256
4257/*
4258 * "foldclosed()" function
4259 */
4260 static void
4261f_foldclosed(typval_T *argvars, typval_T *rettv)
4262{
4263 foldclosed_both(argvars, rettv, FALSE);
4264}
4265
4266/*
4267 * "foldclosedend()" function
4268 */
4269 static void
4270f_foldclosedend(typval_T *argvars, typval_T *rettv)
4271{
4272 foldclosed_both(argvars, rettv, TRUE);
4273}
4274
4275/*
4276 * "foldlevel()" function
4277 */
4278 static void
4279f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4280{
4281#ifdef FEAT_FOLDING
4282 linenr_T lnum;
4283
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004284 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004285 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4286 rettv->vval.v_number = foldLevel(lnum);
4287#endif
4288}
4289
4290/*
4291 * "foldtext()" function
4292 */
4293 static void
4294f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4295{
4296#ifdef FEAT_FOLDING
4297 linenr_T foldstart;
4298 linenr_T foldend;
4299 char_u *dashes;
4300 linenr_T lnum;
4301 char_u *s;
4302 char_u *r;
4303 int len;
4304 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004305 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004306#endif
4307
4308 rettv->v_type = VAR_STRING;
4309 rettv->vval.v_string = NULL;
4310#ifdef FEAT_FOLDING
4311 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4312 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4313 dashes = get_vim_var_str(VV_FOLDDASHES);
4314 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4315 && dashes != NULL)
4316 {
4317 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004318 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004319 if (!linewhite(lnum))
4320 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321
4322 /* Find interesting text in this line. */
4323 s = skipwhite(ml_get(lnum));
4324 /* skip C comment-start */
4325 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4326 {
4327 s = skipwhite(s + 2);
4328 if (*skipwhite(s) == NUL
4329 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4330 {
4331 s = skipwhite(ml_get(lnum + 1));
4332 if (*s == '*')
4333 s = skipwhite(s + 1);
4334 }
4335 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004336 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004337 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004338 r = alloc(STRLEN(txt)
4339 + STRLEN(dashes) // for %s
4340 + 20 // for %3ld
4341 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342 if (r != NULL)
4343 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004344 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004345 len = (int)STRLEN(r);
4346 STRCAT(r, s);
4347 /* remove 'foldmarker' and 'commentstring' */
4348 foldtext_cleanup(r + len);
4349 rettv->vval.v_string = r;
4350 }
4351 }
4352#endif
4353}
4354
4355/*
4356 * "foldtextresult(lnum)" function
4357 */
4358 static void
4359f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4360{
4361#ifdef FEAT_FOLDING
4362 linenr_T lnum;
4363 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004364 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365 foldinfo_T foldinfo;
4366 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004367 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368#endif
4369
4370 rettv->v_type = VAR_STRING;
4371 rettv->vval.v_string = NULL;
4372#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004373 if (entered)
4374 return; /* reject recursive use */
4375 entered = TRUE;
4376
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004377 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004378 /* treat illegal types and illegal string values for {lnum} the same */
4379 if (lnum < 0)
4380 lnum = 0;
4381 fold_count = foldedCount(curwin, lnum, &foldinfo);
4382 if (fold_count > 0)
4383 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004384 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4385 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004386 if (text == buf)
4387 text = vim_strsave(text);
4388 rettv->vval.v_string = text;
4389 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004390
4391 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004392#endif
4393}
4394
4395/*
4396 * "foreground()" function
4397 */
4398 static void
4399f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4400{
4401#ifdef FEAT_GUI
4402 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004403 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004404 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004405 return;
4406 }
4407#endif
4408#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410#endif
4411}
4412
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004413 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004414common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004415{
4416 char_u *s;
4417 char_u *name;
4418 int use_string = FALSE;
4419 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004420 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004421
4422 if (argvars[0].v_type == VAR_FUNC)
4423 {
4424 /* function(MyFunc, [arg], dict) */
4425 s = argvars[0].vval.v_string;
4426 }
4427 else if (argvars[0].v_type == VAR_PARTIAL
4428 && argvars[0].vval.v_partial != NULL)
4429 {
4430 /* function(dict.MyFunc, [arg]) */
4431 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004432 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 }
4434 else
4435 {
4436 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004437 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004438 use_string = TRUE;
4439 }
4440
Bram Moolenaar843b8842016-08-21 14:36:15 +02004441 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004442 {
4443 name = s;
4444 trans_name = trans_function_name(&name, FALSE,
4445 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4446 if (*name != NUL)
4447 s = NULL;
4448 }
4449
Bram Moolenaar843b8842016-08-21 14:36:15 +02004450 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4451 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004452 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004454 else if (trans_name != NULL && (is_funcref
4455 ? find_func(trans_name) == NULL
4456 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004457 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 else
4459 {
4460 int dict_idx = 0;
4461 int arg_idx = 0;
4462 list_T *list = NULL;
4463
4464 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4465 {
4466 char sid_buf[25];
4467 int off = *s == 's' ? 2 : 5;
4468
4469 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4470 * also be called from another script. Using trans_function_name()
4471 * would also work, but some plugins depend on the name being
4472 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004473 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004474 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004475 if (name != NULL)
4476 {
4477 STRCPY(name, sid_buf);
4478 STRCAT(name, s + off);
4479 }
4480 }
4481 else
4482 name = vim_strsave(s);
4483
4484 if (argvars[1].v_type != VAR_UNKNOWN)
4485 {
4486 if (argvars[2].v_type != VAR_UNKNOWN)
4487 {
4488 /* function(name, [args], dict) */
4489 arg_idx = 1;
4490 dict_idx = 2;
4491 }
4492 else if (argvars[1].v_type == VAR_DICT)
4493 /* function(name, dict) */
4494 dict_idx = 1;
4495 else
4496 /* function(name, [args]) */
4497 arg_idx = 1;
4498 if (dict_idx > 0)
4499 {
4500 if (argvars[dict_idx].v_type != VAR_DICT)
4501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004502 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004503 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004504 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004505 }
4506 if (argvars[dict_idx].vval.v_dict == NULL)
4507 dict_idx = 0;
4508 }
4509 if (arg_idx > 0)
4510 {
4511 if (argvars[arg_idx].v_type != VAR_LIST)
4512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004513 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004516 }
4517 list = argvars[arg_idx].vval.v_list;
4518 if (list == NULL || list->lv_len == 0)
4519 arg_idx = 0;
4520 }
4521 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004522 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004523 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004524 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004525
4526 /* result is a VAR_PARTIAL */
4527 if (pt == NULL)
4528 vim_free(name);
4529 else
4530 {
4531 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4532 {
4533 listitem_T *li;
4534 int i = 0;
4535 int arg_len = 0;
4536 int lv_len = 0;
4537
4538 if (arg_pt != NULL)
4539 arg_len = arg_pt->pt_argc;
4540 if (list != NULL)
4541 lv_len = list->lv_len;
4542 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004543 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004544 if (pt->pt_argv == NULL)
4545 {
4546 vim_free(pt);
4547 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004548 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004549 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004550 for (i = 0; i < arg_len; i++)
4551 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4552 if (lv_len > 0)
4553 for (li = list->lv_first; li != NULL;
4554 li = li->li_next)
4555 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004556 }
4557
4558 /* For "function(dict.func, [], dict)" and "func" is a partial
4559 * use "dict". That is backwards compatible. */
4560 if (dict_idx > 0)
4561 {
4562 /* The dict is bound explicitly, pt_auto is FALSE. */
4563 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4564 ++pt->pt_dict->dv_refcount;
4565 }
4566 else if (arg_pt != NULL)
4567 {
4568 /* If the dict was bound automatically the result is also
4569 * bound automatically. */
4570 pt->pt_dict = arg_pt->pt_dict;
4571 pt->pt_auto = arg_pt->pt_auto;
4572 if (pt->pt_dict != NULL)
4573 ++pt->pt_dict->dv_refcount;
4574 }
4575
4576 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004577 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4578 {
4579 pt->pt_func = arg_pt->pt_func;
4580 func_ptr_ref(pt->pt_func);
4581 vim_free(name);
4582 }
4583 else if (is_funcref)
4584 {
4585 pt->pt_func = find_func(trans_name);
4586 func_ptr_ref(pt->pt_func);
4587 vim_free(name);
4588 }
4589 else
4590 {
4591 pt->pt_name = name;
4592 func_ref(name);
4593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004594 }
4595 rettv->v_type = VAR_PARTIAL;
4596 rettv->vval.v_partial = pt;
4597 }
4598 else
4599 {
4600 /* result is a VAR_FUNC */
4601 rettv->v_type = VAR_FUNC;
4602 rettv->vval.v_string = name;
4603 func_ref(name);
4604 }
4605 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004606theend:
4607 vim_free(trans_name);
4608}
4609
4610/*
4611 * "funcref()" function
4612 */
4613 static void
4614f_funcref(typval_T *argvars, typval_T *rettv)
4615{
4616 common_function(argvars, rettv, TRUE);
4617}
4618
4619/*
4620 * "function()" function
4621 */
4622 static void
4623f_function(typval_T *argvars, typval_T *rettv)
4624{
4625 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004626}
4627
4628/*
4629 * "garbagecollect()" function
4630 */
4631 static void
4632f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4633{
4634 /* This is postponed until we are back at the toplevel, because we may be
4635 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4636 want_garbage_collect = TRUE;
4637
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004638 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004639 garbage_collect_at_exit = TRUE;
4640}
4641
4642/*
4643 * "get()" function
4644 */
4645 static void
4646f_get(typval_T *argvars, typval_T *rettv)
4647{
4648 listitem_T *li;
4649 list_T *l;
4650 dictitem_T *di;
4651 dict_T *d;
4652 typval_T *tv = NULL;
4653
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004654 if (argvars[0].v_type == VAR_BLOB)
4655 {
4656 int error = FALSE;
4657 int idx = tv_get_number_chk(&argvars[1], &error);
4658
4659 if (!error)
4660 {
4661 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004662 if (idx < 0)
4663 idx = blob_len(argvars[0].vval.v_blob) + idx;
4664 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4665 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004666 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004667 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004668 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004669 tv = rettv;
4670 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004671 }
4672 }
4673 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004674 {
4675 if ((l = argvars[0].vval.v_list) != NULL)
4676 {
4677 int error = FALSE;
4678
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004679 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004680 if (!error && li != NULL)
4681 tv = &li->li_tv;
4682 }
4683 }
4684 else if (argvars[0].v_type == VAR_DICT)
4685 {
4686 if ((d = argvars[0].vval.v_dict) != NULL)
4687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004688 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689 if (di != NULL)
4690 tv = &di->di_tv;
4691 }
4692 }
4693 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4694 {
4695 partial_T *pt;
4696 partial_T fref_pt;
4697
4698 if (argvars[0].v_type == VAR_PARTIAL)
4699 pt = argvars[0].vval.v_partial;
4700 else
4701 {
4702 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4703 fref_pt.pt_name = argvars[0].vval.v_string;
4704 pt = &fref_pt;
4705 }
4706
4707 if (pt != NULL)
4708 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004709 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004710 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004711
4712 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4713 {
4714 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004715 n = partial_name(pt);
4716 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004717 rettv->vval.v_string = NULL;
4718 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004719 {
4720 rettv->vval.v_string = vim_strsave(n);
4721 if (rettv->v_type == VAR_FUNC)
4722 func_ref(rettv->vval.v_string);
4723 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 }
4725 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004726 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004727 else if (STRCMP(what, "args") == 0)
4728 {
4729 rettv->v_type = VAR_LIST;
4730 if (rettv_list_alloc(rettv) == OK)
4731 {
4732 int i;
4733
4734 for (i = 0; i < pt->pt_argc; ++i)
4735 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4736 }
4737 }
4738 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004739 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004740 return;
4741 }
4742 }
4743 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004744 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004745
4746 if (tv == NULL)
4747 {
4748 if (argvars[2].v_type != VAR_UNKNOWN)
4749 copy_tv(&argvars[2], rettv);
4750 }
4751 else
4752 copy_tv(tv, rettv);
4753}
4754
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004755/*
4756 * Returns buffer options, variables and other attributes in a dictionary.
4757 */
4758 static dict_T *
4759get_buffer_info(buf_T *buf)
4760{
4761 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004762 tabpage_T *tp;
4763 win_T *wp;
4764 list_T *windows;
4765
4766 dict = dict_alloc();
4767 if (dict == NULL)
4768 return NULL;
4769
Bram Moolenaare0be1672018-07-08 16:50:37 +02004770 dict_add_number(dict, "bufnr", buf->b_fnum);
4771 dict_add_string(dict, "name", buf->b_ffname);
4772 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4773 : buflist_findlnum(buf));
4774 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4775 dict_add_number(dict, "listed", buf->b_p_bl);
4776 dict_add_number(dict, "changed", bufIsChanged(buf));
4777 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4778 dict_add_number(dict, "hidden",
4779 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004780
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004781 /* Get a reference to buffer variables */
4782 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004783
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004784 /* List of windows displaying this buffer */
4785 windows = list_alloc();
4786 if (windows != NULL)
4787 {
4788 FOR_ALL_TAB_WINDOWS(tp, wp)
4789 if (wp->w_buffer == buf)
4790 list_append_number(windows, (varnumber_T)wp->w_id);
4791 dict_add_list(dict, "windows", windows);
4792 }
4793
4794#ifdef FEAT_SIGNS
4795 if (buf->b_signlist != NULL)
4796 {
4797 /* List of signs placed in this buffer */
4798 list_T *signs = list_alloc();
4799 if (signs != NULL)
4800 {
4801 get_buffer_signs(buf, signs);
4802 dict_add_list(dict, "signs", signs);
4803 }
4804 }
4805#endif
4806
4807 return dict;
4808}
4809
4810/*
4811 * "getbufinfo()" function
4812 */
4813 static void
4814f_getbufinfo(typval_T *argvars, typval_T *rettv)
4815{
4816 buf_T *buf = NULL;
4817 buf_T *argbuf = NULL;
4818 dict_T *d;
4819 int filtered = FALSE;
4820 int sel_buflisted = FALSE;
4821 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004822 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004823
4824 if (rettv_list_alloc(rettv) != OK)
4825 return;
4826
4827 /* List of all the buffers or selected buffers */
4828 if (argvars[0].v_type == VAR_DICT)
4829 {
4830 dict_T *sel_d = argvars[0].vval.v_dict;
4831
4832 if (sel_d != NULL)
4833 {
4834 dictitem_T *di;
4835
4836 filtered = TRUE;
4837
4838 di = dict_find(sel_d, (char_u *)"buflisted", -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_buflisted = TRUE;
4841
4842 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004843 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004844 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004845
4846 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004847 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004848 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004849 }
4850 }
4851 else if (argvars[0].v_type != VAR_UNKNOWN)
4852 {
4853 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004854 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004855 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004856 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004857 --emsg_off;
4858 if (argbuf == NULL)
4859 return;
4860 }
4861
4862 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004863 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004864 {
4865 if (argbuf != NULL && argbuf != buf)
4866 continue;
4867 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004868 || (sel_buflisted && !buf->b_p_bl)
4869 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004870 continue;
4871
4872 d = get_buffer_info(buf);
4873 if (d != NULL)
4874 list_append_dict(rettv->vval.v_list, d);
4875 if (argbuf != NULL)
4876 return;
4877 }
4878}
4879
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004880/*
4881 * Get line or list of lines from buffer "buf" into "rettv".
4882 * Return a range (from start to end) of lines in rettv from the specified
4883 * buffer.
4884 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4885 */
4886 static void
4887get_buffer_lines(
4888 buf_T *buf,
4889 linenr_T start,
4890 linenr_T end,
4891 int retlist,
4892 typval_T *rettv)
4893{
4894 char_u *p;
4895
4896 rettv->v_type = VAR_STRING;
4897 rettv->vval.v_string = NULL;
4898 if (retlist && rettv_list_alloc(rettv) == FAIL)
4899 return;
4900
4901 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4902 return;
4903
4904 if (!retlist)
4905 {
4906 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4907 p = ml_get_buf(buf, start, FALSE);
4908 else
4909 p = (char_u *)"";
4910 rettv->vval.v_string = vim_strsave(p);
4911 }
4912 else
4913 {
4914 if (end < start)
4915 return;
4916
4917 if (start < 1)
4918 start = 1;
4919 if (end > buf->b_ml.ml_line_count)
4920 end = buf->b_ml.ml_line_count;
4921 while (start <= end)
4922 if (list_append_string(rettv->vval.v_list,
4923 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4924 break;
4925 }
4926}
4927
4928/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004929 * "getbufline()" function
4930 */
4931 static void
4932f_getbufline(typval_T *argvars, typval_T *rettv)
4933{
4934 linenr_T lnum;
4935 linenr_T end;
4936 buf_T *buf;
4937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004938 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004940 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 --emsg_off;
4942
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004943 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004944 if (argvars[2].v_type == VAR_UNKNOWN)
4945 end = lnum;
4946 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004947 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004948
4949 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4950}
4951
4952/*
4953 * "getbufvar()" function
4954 */
4955 static void
4956f_getbufvar(typval_T *argvars, typval_T *rettv)
4957{
4958 buf_T *buf;
4959 buf_T *save_curbuf;
4960 char_u *varname;
4961 dictitem_T *v;
4962 int done = FALSE;
4963
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004964 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4965 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004966 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004967 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004968
4969 rettv->v_type = VAR_STRING;
4970 rettv->vval.v_string = NULL;
4971
4972 if (buf != NULL && varname != NULL)
4973 {
4974 /* set curbuf to be our buf, temporarily */
4975 save_curbuf = curbuf;
4976 curbuf = buf;
4977
Bram Moolenaar30567352016-08-27 21:25:44 +02004978 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004979 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004980 if (varname[1] == NUL)
4981 {
4982 /* get all buffer-local options in a dict */
4983 dict_T *opts = get_winbuf_options(TRUE);
4984
4985 if (opts != NULL)
4986 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004987 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004988 done = TRUE;
4989 }
4990 }
4991 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4992 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004993 done = TRUE;
4994 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004995 else
4996 {
4997 /* Look up the variable. */
4998 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4999 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
5000 'b', varname, FALSE);
5001 if (v != NULL)
5002 {
5003 copy_tv(&v->di_tv, rettv);
5004 done = TRUE;
5005 }
5006 }
5007
5008 /* restore previous notion of curbuf */
5009 curbuf = save_curbuf;
5010 }
5011
5012 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5013 /* use the default value */
5014 copy_tv(&argvars[2], rettv);
5015
5016 --emsg_off;
5017}
5018
5019/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005020 * "getchangelist()" function
5021 */
5022 static void
5023f_getchangelist(typval_T *argvars, typval_T *rettv)
5024{
5025#ifdef FEAT_JUMPLIST
5026 buf_T *buf;
5027 int i;
5028 list_T *l;
5029 dict_T *d;
5030#endif
5031
5032 if (rettv_list_alloc(rettv) != OK)
5033 return;
5034
5035#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005036 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005037 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005038 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005039 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005040 if (buf == NULL)
5041 return;
5042
5043 l = list_alloc();
5044 if (l == NULL)
5045 return;
5046
5047 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5048 return;
5049 /*
5050 * The current window change list index tracks only the position in the
5051 * current buffer change list. For other buffers, use the change list
5052 * length as the current index.
5053 */
5054 list_append_number(rettv->vval.v_list,
5055 (varnumber_T)((buf == curwin->w_buffer)
5056 ? curwin->w_changelistidx : buf->b_changelistlen));
5057
5058 for (i = 0; i < buf->b_changelistlen; ++i)
5059 {
5060 if (buf->b_changelist[i].lnum == 0)
5061 continue;
5062 if ((d = dict_alloc()) == NULL)
5063 return;
5064 if (list_append_dict(l, d) == FAIL)
5065 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005066 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5067 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005068 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005069 }
5070#endif
5071}
5072/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005073 * "getchar()" function
5074 */
5075 static void
5076f_getchar(typval_T *argvars, typval_T *rettv)
5077{
5078 varnumber_T n;
5079 int error = FALSE;
5080
Bram Moolenaar84d93902018-09-11 20:10:20 +02005081#ifdef MESSAGE_QUEUE
5082 // vpeekc() used to check for messages, but that caused problems, invoking
5083 // a callback where it was not expected. Some plugins use getchar(1) in a
5084 // loop to await a message, therefore make sure we check for messages here.
5085 parse_queued_messages();
5086#endif
5087
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005088 /* Position the cursor. Needed after a message that ends in a space. */
5089 windgoto(msg_row, msg_col);
5090
5091 ++no_mapping;
5092 ++allow_keys;
5093 for (;;)
5094 {
5095 if (argvars[0].v_type == VAR_UNKNOWN)
5096 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005097 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005098 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 /* getchar(1): only check if char avail */
5100 n = vpeekc_any();
5101 else if (error || vpeekc_any() == NUL)
5102 /* illegal argument or getchar(0) and no char avail: return zero */
5103 n = 0;
5104 else
5105 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005106 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005107
5108 if (n == K_IGNORE)
5109 continue;
5110 break;
5111 }
5112 --no_mapping;
5113 --allow_keys;
5114
5115 set_vim_var_nr(VV_MOUSE_WIN, 0);
5116 set_vim_var_nr(VV_MOUSE_WINID, 0);
5117 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5118 set_vim_var_nr(VV_MOUSE_COL, 0);
5119
5120 rettv->vval.v_number = n;
5121 if (IS_SPECIAL(n) || mod_mask != 0)
5122 {
5123 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5124 int i = 0;
5125
5126 /* Turn a special key into three bytes, plus modifier. */
5127 if (mod_mask != 0)
5128 {
5129 temp[i++] = K_SPECIAL;
5130 temp[i++] = KS_MODIFIER;
5131 temp[i++] = mod_mask;
5132 }
5133 if (IS_SPECIAL(n))
5134 {
5135 temp[i++] = K_SPECIAL;
5136 temp[i++] = K_SECOND(n);
5137 temp[i++] = K_THIRD(n);
5138 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 else if (has_mbyte)
5140 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005141 else
5142 temp[i++] = n;
5143 temp[i++] = NUL;
5144 rettv->v_type = VAR_STRING;
5145 rettv->vval.v_string = vim_strsave(temp);
5146
5147#ifdef FEAT_MOUSE
5148 if (is_mouse_key(n))
5149 {
5150 int row = mouse_row;
5151 int col = mouse_col;
5152 win_T *win;
5153 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005154 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155 int winnr = 1;
5156
5157 if (row >= 0 && col >= 0)
5158 {
5159 /* Find the window at the mouse coordinates and compute the
5160 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005161 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005162 if (win == NULL)
5163 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005164 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005165# ifdef FEAT_TEXT_PROP
5166 if (bt_popup(win->w_buffer))
5167 winnr = 0;
5168 else
5169# endif
5170 for (wp = firstwin; wp != win && wp != NULL;
5171 wp = wp->w_next)
5172 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005173 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5174 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5175 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5176 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5177 }
5178 }
5179#endif
5180 }
5181}
5182
5183/*
5184 * "getcharmod()" function
5185 */
5186 static void
5187f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5188{
5189 rettv->vval.v_number = mod_mask;
5190}
5191
5192/*
5193 * "getcharsearch()" function
5194 */
5195 static void
5196f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5197{
5198 if (rettv_dict_alloc(rettv) != FAIL)
5199 {
5200 dict_T *dict = rettv->vval.v_dict;
5201
Bram Moolenaare0be1672018-07-08 16:50:37 +02005202 dict_add_string(dict, "char", last_csearch());
5203 dict_add_number(dict, "forward", last_csearch_forward());
5204 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005205 }
5206}
5207
5208/*
5209 * "getcmdline()" function
5210 */
5211 static void
5212f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5213{
5214 rettv->v_type = VAR_STRING;
5215 rettv->vval.v_string = get_cmdline_str();
5216}
5217
5218/*
5219 * "getcmdpos()" function
5220 */
5221 static void
5222f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5223{
5224 rettv->vval.v_number = get_cmdline_pos() + 1;
5225}
5226
5227/*
5228 * "getcmdtype()" function
5229 */
5230 static void
5231f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5232{
5233 rettv->v_type = VAR_STRING;
5234 rettv->vval.v_string = alloc(2);
5235 if (rettv->vval.v_string != NULL)
5236 {
5237 rettv->vval.v_string[0] = get_cmdline_type();
5238 rettv->vval.v_string[1] = NUL;
5239 }
5240}
5241
5242/*
5243 * "getcmdwintype()" function
5244 */
5245 static void
5246f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5247{
5248 rettv->v_type = VAR_STRING;
5249 rettv->vval.v_string = NULL;
5250#ifdef FEAT_CMDWIN
5251 rettv->vval.v_string = alloc(2);
5252 if (rettv->vval.v_string != NULL)
5253 {
5254 rettv->vval.v_string[0] = cmdwin_type;
5255 rettv->vval.v_string[1] = NUL;
5256 }
5257#endif
5258}
5259
5260#if defined(FEAT_CMDL_COMPL)
5261/*
5262 * "getcompletion()" function
5263 */
5264 static void
5265f_getcompletion(typval_T *argvars, typval_T *rettv)
5266{
5267 char_u *pat;
5268 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005269 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005270 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5271 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005274 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005275
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005276 if (p_wic)
5277 options |= WILD_ICASE;
5278
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005279 /* For filtered results, 'wildignore' is used */
5280 if (!filtered)
5281 options |= WILD_KEEP_ALL;
5282
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005283 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005284 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005285 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005286 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 if (xpc.xp_context == EXPAND_NOTHING)
5288 {
5289 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005290 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005291 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005292 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 return;
5294 }
5295
5296# if defined(FEAT_MENU)
5297 if (xpc.xp_context == EXPAND_MENUS)
5298 {
5299 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5300 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5301 }
5302# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005303#ifdef FEAT_CSCOPE
5304 if (xpc.xp_context == EXPAND_CSCOPE)
5305 {
5306 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5307 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5308 }
5309#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005310#ifdef FEAT_SIGNS
5311 if (xpc.xp_context == EXPAND_SIGN)
5312 {
5313 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5314 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5315 }
5316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005317
5318 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5319 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5320 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005321 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005322
5323 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5324
5325 for (i = 0; i < xpc.xp_numfiles; i++)
5326 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5327 }
5328 vim_free(pat);
5329 ExpandCleanup(&xpc);
5330}
5331#endif
5332
5333/*
5334 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005335 *
5336 * Return the current working directory of a window in a tab page.
5337 * First optional argument 'winnr' is the window number or -1 and the second
5338 * optional argument 'tabnr' is the tab page number.
5339 *
5340 * If no arguments are supplied, then return the directory of the current
5341 * window.
5342 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5343 * the specified window.
5344 * If 'winnr' is 0 then return the directory of the current window.
5345 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5346 * directory of the specified tab page. Otherwise return the directory of the
5347 * specified window in the specified tab page.
5348 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005349 */
5350 static void
5351f_getcwd(typval_T *argvars, typval_T *rettv)
5352{
5353 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005354 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005355 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005356 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005357
5358 rettv->v_type = VAR_STRING;
5359 rettv->vval.v_string = NULL;
5360
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005361 if (argvars[0].v_type == VAR_NUMBER
5362 && argvars[0].vval.v_number == -1
5363 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005364 global = TRUE;
5365 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005366 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005367
5368 if (wp != NULL && wp->w_localdir != NULL)
5369 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005370 else if (tp != NULL && tp->tp_localdir != NULL)
5371 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5372 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005373 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005374 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005375 rettv->vval.v_string = vim_strsave(globaldir);
5376 else
5377 {
5378 cwd = alloc(MAXPATHL);
5379 if (cwd != NULL)
5380 {
5381 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5382 rettv->vval.v_string = vim_strsave(cwd);
5383 vim_free(cwd);
5384 }
5385 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005386 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005387#ifdef BACKSLASH_IN_FILENAME
5388 if (rettv->vval.v_string != NULL)
5389 slash_adjust(rettv->vval.v_string);
5390#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005391}
5392
5393/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005394 * "getenv()" function
5395 */
5396 static void
5397f_getenv(typval_T *argvars, typval_T *rettv)
5398{
5399 int mustfree = FALSE;
5400 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5401
5402 if (p == NULL)
5403 {
5404 rettv->v_type = VAR_SPECIAL;
5405 rettv->vval.v_number = VVAL_NULL;
5406 return;
5407 }
5408 if (!mustfree)
5409 p = vim_strsave(p);
5410 rettv->vval.v_string = p;
5411 rettv->v_type = VAR_STRING;
5412}
5413
5414/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005415 * "getfontname()" function
5416 */
5417 static void
5418f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5419{
5420 rettv->v_type = VAR_STRING;
5421 rettv->vval.v_string = NULL;
5422#ifdef FEAT_GUI
5423 if (gui.in_use)
5424 {
5425 GuiFont font;
5426 char_u *name = NULL;
5427
5428 if (argvars[0].v_type == VAR_UNKNOWN)
5429 {
5430 /* Get the "Normal" font. Either the name saved by
5431 * hl_set_font_name() or from the font ID. */
5432 font = gui.norm_font;
5433 name = hl_get_font_name();
5434 }
5435 else
5436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005437 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005438 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5439 return;
5440 font = gui_mch_get_font(name, FALSE);
5441 if (font == NOFONT)
5442 return; /* Invalid font name, return empty string. */
5443 }
5444 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5445 if (argvars[0].v_type != VAR_UNKNOWN)
5446 gui_mch_free_font(font);
5447 }
5448#endif
5449}
5450
5451/*
5452 * "getfperm({fname})" function
5453 */
5454 static void
5455f_getfperm(typval_T *argvars, typval_T *rettv)
5456{
5457 char_u *fname;
5458 stat_T st;
5459 char_u *perm = NULL;
5460 char_u flags[] = "rwx";
5461 int i;
5462
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005463 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005464
5465 rettv->v_type = VAR_STRING;
5466 if (mch_stat((char *)fname, &st) >= 0)
5467 {
5468 perm = vim_strsave((char_u *)"---------");
5469 if (perm != NULL)
5470 {
5471 for (i = 0; i < 9; i++)
5472 {
5473 if (st.st_mode & (1 << (8 - i)))
5474 perm[i] = flags[i % 3];
5475 }
5476 }
5477 }
5478 rettv->vval.v_string = perm;
5479}
5480
5481/*
5482 * "getfsize({fname})" function
5483 */
5484 static void
5485f_getfsize(typval_T *argvars, typval_T *rettv)
5486{
5487 char_u *fname;
5488 stat_T st;
5489
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005490 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491
5492 rettv->v_type = VAR_NUMBER;
5493
5494 if (mch_stat((char *)fname, &st) >= 0)
5495 {
5496 if (mch_isdir(fname))
5497 rettv->vval.v_number = 0;
5498 else
5499 {
5500 rettv->vval.v_number = (varnumber_T)st.st_size;
5501
5502 /* non-perfect check for overflow */
5503 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5504 rettv->vval.v_number = -2;
5505 }
5506 }
5507 else
5508 rettv->vval.v_number = -1;
5509}
5510
5511/*
5512 * "getftime({fname})" function
5513 */
5514 static void
5515f_getftime(typval_T *argvars, typval_T *rettv)
5516{
5517 char_u *fname;
5518 stat_T st;
5519
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005520 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005521
5522 if (mch_stat((char *)fname, &st) >= 0)
5523 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5524 else
5525 rettv->vval.v_number = -1;
5526}
5527
5528/*
5529 * "getftype({fname})" function
5530 */
5531 static void
5532f_getftype(typval_T *argvars, typval_T *rettv)
5533{
5534 char_u *fname;
5535 stat_T st;
5536 char_u *type = NULL;
5537 char *t;
5538
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005539 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005540
5541 rettv->v_type = VAR_STRING;
5542 if (mch_lstat((char *)fname, &st) >= 0)
5543 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005544 if (S_ISREG(st.st_mode))
5545 t = "file";
5546 else if (S_ISDIR(st.st_mode))
5547 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005548 else if (S_ISLNK(st.st_mode))
5549 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005550 else if (S_ISBLK(st.st_mode))
5551 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005552 else if (S_ISCHR(st.st_mode))
5553 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 else if (S_ISFIFO(st.st_mode))
5555 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005556 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005557 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005558 else
5559 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005560 type = vim_strsave((char_u *)t);
5561 }
5562 rettv->vval.v_string = type;
5563}
5564
5565/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005566 * "getjumplist()" function
5567 */
5568 static void
5569f_getjumplist(typval_T *argvars, typval_T *rettv)
5570{
5571#ifdef FEAT_JUMPLIST
5572 win_T *wp;
5573 int i;
5574 list_T *l;
5575 dict_T *d;
5576#endif
5577
5578 if (rettv_list_alloc(rettv) != OK)
5579 return;
5580
5581#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005582 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005583 if (wp == NULL)
5584 return;
5585
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005586 cleanup_jumplist(wp, TRUE);
5587
Bram Moolenaar4f505882018-02-10 21:06:32 +01005588 l = list_alloc();
5589 if (l == NULL)
5590 return;
5591
5592 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5593 return;
5594 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5595
5596 for (i = 0; i < wp->w_jumplistlen; ++i)
5597 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005598 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5599 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005600 if ((d = dict_alloc()) == NULL)
5601 return;
5602 if (list_append_dict(l, d) == FAIL)
5603 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005604 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5605 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005606 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005607 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005608 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005609 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005610 }
5611#endif
5612}
5613
5614/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005615 * "getline(lnum, [end])" function
5616 */
5617 static void
5618f_getline(typval_T *argvars, typval_T *rettv)
5619{
5620 linenr_T lnum;
5621 linenr_T end;
5622 int retlist;
5623
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005624 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625 if (argvars[1].v_type == VAR_UNKNOWN)
5626 {
5627 end = 0;
5628 retlist = FALSE;
5629 }
5630 else
5631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005632 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005633 retlist = TRUE;
5634 }
5635
5636 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5637}
5638
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005639#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005640 static void
5641get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5642{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005643 if (what_arg->v_type == VAR_UNKNOWN)
5644 {
5645 if (rettv_list_alloc(rettv) == OK)
5646 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005647 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005648 }
5649 else
5650 {
5651 if (rettv_dict_alloc(rettv) == OK)
5652 if (is_qf || (wp != NULL))
5653 {
5654 if (what_arg->v_type == VAR_DICT)
5655 {
5656 dict_T *d = what_arg->vval.v_dict;
5657
5658 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005659 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005660 }
5661 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005662 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005663 }
5664 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005665}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005666#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005667
5668/*
5669 * "getloclist()" function
5670 */
5671 static void
5672f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5673{
5674#ifdef FEAT_QUICKFIX
5675 win_T *wp;
5676
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005677 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005678 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5679#endif
5680}
5681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005682/*
5683 * "getmatches()" function
5684 */
5685 static void
5686f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5687{
5688#ifdef FEAT_SEARCH_EXTRA
5689 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005690 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005691 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005692 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005693
Bram Moolenaaraff74912019-03-30 18:11:49 +01005694 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5695 return;
5696
5697 cur = win->w_match_head;
5698 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005699 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005700 dict = dict_alloc();
5701 if (dict == NULL)
5702 return;
5703 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005705 /* match added with matchaddpos() */
5706 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005707 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005708 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005709 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005710 list_T *l;
5711
5712 llpos = &cur->pos.pos[i];
5713 if (llpos->lnum == 0)
5714 break;
5715 l = list_alloc();
5716 if (l == NULL)
5717 break;
5718 list_append_number(l, (varnumber_T)llpos->lnum);
5719 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005720 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005721 list_append_number(l, (varnumber_T)llpos->col);
5722 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005724 sprintf(buf, "pos%d", i + 1);
5725 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005726 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005727 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005728 else
5729 {
5730 dict_add_string(dict, "pattern", cur->pattern);
5731 }
5732 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5733 dict_add_number(dict, "priority", (long)cur->priority);
5734 dict_add_number(dict, "id", (long)cur->id);
5735# if defined(FEAT_CONCEAL)
5736 if (cur->conceal_char)
5737 {
5738 char_u buf[MB_MAXBYTES + 1];
5739
5740 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5741 dict_add_string(dict, "conceal", (char_u *)&buf);
5742 }
5743# endif
5744 list_append_dict(rettv->vval.v_list, dict);
5745 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005746 }
5747#endif
5748}
5749
5750/*
5751 * "getpid()" function
5752 */
5753 static void
5754f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5755{
5756 rettv->vval.v_number = mch_get_pid();
5757}
5758
5759 static void
5760getpos_both(
5761 typval_T *argvars,
5762 typval_T *rettv,
5763 int getcurpos)
5764{
5765 pos_T *fp;
5766 list_T *l;
5767 int fnum = -1;
5768
5769 if (rettv_list_alloc(rettv) == OK)
5770 {
5771 l = rettv->vval.v_list;
5772 if (getcurpos)
5773 fp = &curwin->w_cursor;
5774 else
5775 fp = var2fpos(&argvars[0], TRUE, &fnum);
5776 if (fnum != -1)
5777 list_append_number(l, (varnumber_T)fnum);
5778 else
5779 list_append_number(l, (varnumber_T)0);
5780 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5781 : (varnumber_T)0);
5782 list_append_number(l, (fp != NULL)
5783 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5784 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005785 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005786 (varnumber_T)0);
5787 if (getcurpos)
5788 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005789 int save_set_curswant = curwin->w_set_curswant;
5790 colnr_T save_curswant = curwin->w_curswant;
5791 colnr_T save_virtcol = curwin->w_virtcol;
5792
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005793 update_curswant();
5794 list_append_number(l, curwin->w_curswant == MAXCOL ?
5795 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005796
5797 // Do not change "curswant", as it is unexpected that a get
5798 // function has a side effect.
5799 if (save_set_curswant)
5800 {
5801 curwin->w_set_curswant = save_set_curswant;
5802 curwin->w_curswant = save_curswant;
5803 curwin->w_virtcol = save_virtcol;
5804 curwin->w_valid &= ~VALID_VIRTCOL;
5805 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 }
5807 }
5808 else
5809 rettv->vval.v_number = FALSE;
5810}
5811
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005812/*
5813 * "getcurpos()" function
5814 */
5815 static void
5816f_getcurpos(typval_T *argvars, typval_T *rettv)
5817{
5818 getpos_both(argvars, rettv, TRUE);
5819}
5820
5821/*
5822 * "getpos(string)" function
5823 */
5824 static void
5825f_getpos(typval_T *argvars, typval_T *rettv)
5826{
5827 getpos_both(argvars, rettv, FALSE);
5828}
5829
5830/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005831 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005832 */
5833 static void
5834f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5835{
5836#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005837 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838#endif
5839}
5840
5841/*
5842 * "getreg()" function
5843 */
5844 static void
5845f_getreg(typval_T *argvars, typval_T *rettv)
5846{
5847 char_u *strregname;
5848 int regname;
5849 int arg2 = FALSE;
5850 int return_list = FALSE;
5851 int error = FALSE;
5852
5853 if (argvars[0].v_type != VAR_UNKNOWN)
5854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 error = strregname == NULL;
5857 if (argvars[1].v_type != VAR_UNKNOWN)
5858 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005859 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005860 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005861 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005862 }
5863 }
5864 else
5865 strregname = get_vim_var_str(VV_REG);
5866
5867 if (error)
5868 return;
5869
5870 regname = (strregname == NULL ? '"' : *strregname);
5871 if (regname == 0)
5872 regname = '"';
5873
5874 if (return_list)
5875 {
5876 rettv->v_type = VAR_LIST;
5877 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5878 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5879 if (rettv->vval.v_list == NULL)
5880 (void)rettv_list_alloc(rettv);
5881 else
5882 ++rettv->vval.v_list->lv_refcount;
5883 }
5884 else
5885 {
5886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = get_reg_contents(regname,
5888 arg2 ? GREG_EXPR_SRC : 0);
5889 }
5890}
5891
5892/*
5893 * "getregtype()" function
5894 */
5895 static void
5896f_getregtype(typval_T *argvars, typval_T *rettv)
5897{
5898 char_u *strregname;
5899 int regname;
5900 char_u buf[NUMBUFLEN + 2];
5901 long reglen = 0;
5902
5903 if (argvars[0].v_type != VAR_UNKNOWN)
5904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005905 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005906 if (strregname == NULL) /* type error; errmsg already given */
5907 {
5908 rettv->v_type = VAR_STRING;
5909 rettv->vval.v_string = NULL;
5910 return;
5911 }
5912 }
5913 else
5914 /* Default to v:register */
5915 strregname = get_vim_var_str(VV_REG);
5916
5917 regname = (strregname == NULL ? '"' : *strregname);
5918 if (regname == 0)
5919 regname = '"';
5920
5921 buf[0] = NUL;
5922 buf[1] = NUL;
5923 switch (get_reg_type(regname, &reglen))
5924 {
5925 case MLINE: buf[0] = 'V'; break;
5926 case MCHAR: buf[0] = 'v'; break;
5927 case MBLOCK:
5928 buf[0] = Ctrl_V;
5929 sprintf((char *)buf + 1, "%ld", reglen + 1);
5930 break;
5931 }
5932 rettv->v_type = VAR_STRING;
5933 rettv->vval.v_string = vim_strsave(buf);
5934}
5935
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005936/*
5937 * Returns information (variables, options, etc.) about a tab page
5938 * as a dictionary.
5939 */
5940 static dict_T *
5941get_tabpage_info(tabpage_T *tp, int tp_idx)
5942{
5943 win_T *wp;
5944 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005945 list_T *l;
5946
5947 dict = dict_alloc();
5948 if (dict == NULL)
5949 return NULL;
5950
Bram Moolenaare0be1672018-07-08 16:50:37 +02005951 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005952
5953 l = list_alloc();
5954 if (l != NULL)
5955 {
5956 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5957 wp; wp = wp->w_next)
5958 list_append_number(l, (varnumber_T)wp->w_id);
5959 dict_add_list(dict, "windows", l);
5960 }
5961
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005962 /* Make a reference to tabpage variables */
5963 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005964
5965 return dict;
5966}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005967
5968/*
5969 * "gettabinfo()" function
5970 */
5971 static void
5972f_gettabinfo(typval_T *argvars, typval_T *rettv)
5973{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005974 tabpage_T *tp, *tparg = NULL;
5975 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005976 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005977
5978 if (rettv_list_alloc(rettv) != OK)
5979 return;
5980
5981 if (argvars[0].v_type != VAR_UNKNOWN)
5982 {
5983 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005984 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005985 if (tparg == NULL)
5986 return;
5987 }
5988
5989 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005990 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005991 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005992 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005993 if (tparg != NULL && tp != tparg)
5994 continue;
5995 d = get_tabpage_info(tp, tpnr);
5996 if (d != NULL)
5997 list_append_dict(rettv->vval.v_list, d);
5998 if (tparg != NULL)
5999 return;
6000 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006001}
6002
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006003/*
6004 * "gettabvar()" function
6005 */
6006 static void
6007f_gettabvar(typval_T *argvars, typval_T *rettv)
6008{
6009 win_T *oldcurwin;
6010 tabpage_T *tp, *oldtabpage;
6011 dictitem_T *v;
6012 char_u *varname;
6013 int done = FALSE;
6014
6015 rettv->v_type = VAR_STRING;
6016 rettv->vval.v_string = NULL;
6017
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006018 varname = tv_get_string_chk(&argvars[1]);
6019 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006020 if (tp != NULL && varname != NULL)
6021 {
6022 /* Set tp to be our tabpage, temporarily. Also set the window to the
6023 * first window in the tabpage, otherwise the window is not valid. */
6024 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006025 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6026 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006027 {
6028 /* look up the variable */
6029 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6030 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6031 if (v != NULL)
6032 {
6033 copy_tv(&v->di_tv, rettv);
6034 done = TRUE;
6035 }
6036 }
6037
6038 /* restore previous notion of curwin */
6039 restore_win(oldcurwin, oldtabpage, TRUE);
6040 }
6041
6042 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6043 copy_tv(&argvars[2], rettv);
6044}
6045
6046/*
6047 * "gettabwinvar()" function
6048 */
6049 static void
6050f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6051{
6052 getwinvar(argvars, rettv, 1);
6053}
6054
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006055/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006056 * "gettagstack()" function
6057 */
6058 static void
6059f_gettagstack(typval_T *argvars, typval_T *rettv)
6060{
6061 win_T *wp = curwin; // default is current window
6062
6063 if (rettv_dict_alloc(rettv) != OK)
6064 return;
6065
6066 if (argvars[0].v_type != VAR_UNKNOWN)
6067 {
6068 wp = find_win_by_nr_or_id(&argvars[0]);
6069 if (wp == NULL)
6070 return;
6071 }
6072
6073 get_tagstack(wp, rettv->vval.v_dict);
6074}
6075
6076/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006077 * Returns information about a window as a dictionary.
6078 */
6079 static dict_T *
6080get_win_info(win_T *wp, short tpnr, short winnr)
6081{
6082 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006083
6084 dict = dict_alloc();
6085 if (dict == NULL)
6086 return NULL;
6087
Bram Moolenaare0be1672018-07-08 16:50:37 +02006088 dict_add_number(dict, "tabnr", tpnr);
6089 dict_add_number(dict, "winnr", winnr);
6090 dict_add_number(dict, "winid", wp->w_id);
6091 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006092 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006093 dict_add_number(dict, "topline", wp->w_topline);
6094 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006095#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006096 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006097#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006098 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006099 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006100 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006101
Bram Moolenaar69905d12017-08-13 18:14:47 +02006102#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006103 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006104#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006105#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006106 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6107 dict_add_number(dict, "loclist",
6108 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006109#endif
6110
Bram Moolenaar30567352016-08-27 21:25:44 +02006111 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006112 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006113
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006114 return dict;
6115}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006116
6117/*
6118 * "getwininfo()" function
6119 */
6120 static void
6121f_getwininfo(typval_T *argvars, typval_T *rettv)
6122{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006123 tabpage_T *tp;
6124 win_T *wp = NULL, *wparg = NULL;
6125 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006126 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006127
6128 if (rettv_list_alloc(rettv) != OK)
6129 return;
6130
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006131 if (argvars[0].v_type != VAR_UNKNOWN)
6132 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006133 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006134 if (wparg == NULL)
6135 return;
6136 }
6137
6138 /* Collect information about either all the windows across all the tab
6139 * pages or one particular window.
6140 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006141 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006142 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006143 tabnr++;
6144 winnr = 0;
6145 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006146 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006147 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006148 if (wparg != NULL && wp != wparg)
6149 continue;
6150 d = get_win_info(wp, tabnr, winnr);
6151 if (d != NULL)
6152 list_append_dict(rettv->vval.v_list, d);
6153 if (wparg != NULL)
6154 /* found information about a specific window */
6155 return;
6156 }
6157 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006158}
6159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006160/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006161 * "win_execute()" function
6162 */
6163 static void
6164f_win_execute(typval_T *argvars, typval_T *rettv)
6165{
6166 int id = (int)tv_get_number(argvars);
6167 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006168 win_T *save_curwin;
6169 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006170
6171 if (wp != NULL)
6172 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006173 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6174 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006175 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006176 check_cursor();
6177 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006178 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006179 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006180 }
6181}
6182
6183/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006184 * "win_findbuf()" function
6185 */
6186 static void
6187f_win_findbuf(typval_T *argvars, typval_T *rettv)
6188{
6189 if (rettv_list_alloc(rettv) != FAIL)
6190 win_findbuf(argvars, rettv->vval.v_list);
6191}
6192
6193/*
6194 * "win_getid()" function
6195 */
6196 static void
6197f_win_getid(typval_T *argvars, typval_T *rettv)
6198{
6199 rettv->vval.v_number = win_getid(argvars);
6200}
6201
6202/*
6203 * "win_gotoid()" function
6204 */
6205 static void
6206f_win_gotoid(typval_T *argvars, typval_T *rettv)
6207{
6208 rettv->vval.v_number = win_gotoid(argvars);
6209}
6210
6211/*
6212 * "win_id2tabwin()" function
6213 */
6214 static void
6215f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6216{
6217 if (rettv_list_alloc(rettv) != FAIL)
6218 win_id2tabwin(argvars, rettv->vval.v_list);
6219}
6220
6221/*
6222 * "win_id2win()" function
6223 */
6224 static void
6225f_win_id2win(typval_T *argvars, typval_T *rettv)
6226{
6227 rettv->vval.v_number = win_id2win(argvars);
6228}
6229
6230/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006231 * "win_screenpos()" function
6232 */
6233 static void
6234f_win_screenpos(typval_T *argvars, typval_T *rettv)
6235{
6236 win_T *wp;
6237
6238 if (rettv_list_alloc(rettv) == FAIL)
6239 return;
6240
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006241 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006242 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6243 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6244}
6245
6246/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006247 * "getwinpos({timeout})" function
6248 */
6249 static void
6250f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6251{
6252 int x = -1;
6253 int y = -1;
6254
6255 if (rettv_list_alloc(rettv) == FAIL)
6256 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006257#if defined(FEAT_GUI) \
6258 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6259 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006260 {
6261 varnumber_T timeout = 100;
6262
6263 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006264 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006265
6266 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006267 }
6268#endif
6269 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6270 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6271}
6272
6273
6274/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006275 * "getwinposx()" function
6276 */
6277 static void
6278f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6279{
6280 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006281#if defined(FEAT_GUI) \
6282 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6283 || defined(MSWIN)
6284
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006285 {
6286 int x, y;
6287
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006288 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006289 rettv->vval.v_number = x;
6290 }
6291#endif
6292}
6293
6294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 * "getwinposy()" function
6296 */
6297 static void
6298f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6299{
6300 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006301#if defined(FEAT_GUI) \
6302 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6303 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006304 {
6305 int x, y;
6306
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006307 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006308 rettv->vval.v_number = y;
6309 }
6310#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006311}
6312
6313/*
6314 * "getwinvar()" function
6315 */
6316 static void
6317f_getwinvar(typval_T *argvars, typval_T *rettv)
6318{
6319 getwinvar(argvars, rettv, 0);
6320}
6321
6322/*
6323 * "glob()" function
6324 */
6325 static void
6326f_glob(typval_T *argvars, typval_T *rettv)
6327{
6328 int options = WILD_SILENT|WILD_USE_NL;
6329 expand_T xpc;
6330 int error = FALSE;
6331
6332 /* When the optional second argument is non-zero, don't remove matches
6333 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6334 rettv->v_type = VAR_STRING;
6335 if (argvars[1].v_type != VAR_UNKNOWN)
6336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006337 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006338 options |= WILD_KEEP_ALL;
6339 if (argvars[2].v_type != VAR_UNKNOWN)
6340 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006341 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006342 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006344 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006345 options |= WILD_ALLLINKS;
6346 }
6347 }
6348 if (!error)
6349 {
6350 ExpandInit(&xpc);
6351 xpc.xp_context = EXPAND_FILES;
6352 if (p_wic)
6353 options += WILD_ICASE;
6354 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006355 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006356 NULL, options, WILD_ALL);
6357 else if (rettv_list_alloc(rettv) != FAIL)
6358 {
6359 int i;
6360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006361 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006362 NULL, options, WILD_ALL_KEEP);
6363 for (i = 0; i < xpc.xp_numfiles; i++)
6364 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6365
6366 ExpandCleanup(&xpc);
6367 }
6368 }
6369 else
6370 rettv->vval.v_string = NULL;
6371}
6372
6373/*
6374 * "globpath()" function
6375 */
6376 static void
6377f_globpath(typval_T *argvars, typval_T *rettv)
6378{
6379 int flags = 0;
6380 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006381 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006382 int error = FALSE;
6383 garray_T ga;
6384 int i;
6385
6386 /* When the optional second argument is non-zero, don't remove matches
6387 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6388 rettv->v_type = VAR_STRING;
6389 if (argvars[2].v_type != VAR_UNKNOWN)
6390 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006391 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006392 flags |= WILD_KEEP_ALL;
6393 if (argvars[3].v_type != VAR_UNKNOWN)
6394 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006395 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006396 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006397 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006398 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006399 flags |= WILD_ALLLINKS;
6400 }
6401 }
6402 if (file != NULL && !error)
6403 {
6404 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006405 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006406 if (rettv->v_type == VAR_STRING)
6407 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6408 else if (rettv_list_alloc(rettv) != FAIL)
6409 for (i = 0; i < ga.ga_len; ++i)
6410 list_append_string(rettv->vval.v_list,
6411 ((char_u **)(ga.ga_data))[i], -1);
6412 ga_clear_strings(&ga);
6413 }
6414 else
6415 rettv->vval.v_string = NULL;
6416}
6417
6418/*
6419 * "glob2regpat()" function
6420 */
6421 static void
6422f_glob2regpat(typval_T *argvars, typval_T *rettv)
6423{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006424 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425
6426 rettv->v_type = VAR_STRING;
6427 rettv->vval.v_string = (pat == NULL)
6428 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6429}
6430
6431/* for VIM_VERSION_ defines */
6432#include "version.h"
6433
6434/*
6435 * "has()" function
6436 */
6437 static void
6438f_has(typval_T *argvars, typval_T *rettv)
6439{
6440 int i;
6441 char_u *name;
6442 int n = FALSE;
6443 static char *(has_list[]) =
6444 {
6445#ifdef AMIGA
6446 "amiga",
6447# ifdef FEAT_ARP
6448 "arp",
6449# endif
6450#endif
6451#ifdef __BEOS__
6452 "beos",
6453#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006454#if defined(BSD) && !defined(MACOS_X)
6455 "bsd",
6456#endif
6457#ifdef hpux
6458 "hpux",
6459#endif
6460#ifdef __linux__
6461 "linux",
6462#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006463#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006464 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6465 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006466# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006467 "macunix", /* Mac OS X, with the darwin feature */
6468 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006469# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006470#endif
6471#ifdef __QNX__
6472 "qnx",
6473#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006474#ifdef SUN_SYSTEM
6475 "sun",
6476#else
6477 "moon",
6478#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006479#ifdef UNIX
6480 "unix",
6481#endif
6482#ifdef VMS
6483 "vms",
6484#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006485#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 "win32",
6487#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006488#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489 "win32unix",
6490#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006491#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006492 "win64",
6493#endif
6494#ifdef EBCDIC
6495 "ebcdic",
6496#endif
6497#ifndef CASE_INSENSITIVE_FILENAME
6498 "fname_case",
6499#endif
6500#ifdef HAVE_ACL
6501 "acl",
6502#endif
6503#ifdef FEAT_ARABIC
6504 "arabic",
6505#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006506 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006507#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006508 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006509#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006510#ifdef FEAT_AUTOSERVERNAME
6511 "autoservername",
6512#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006513#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006514 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006515# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516 "balloon_multiline",
6517# endif
6518#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006519#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006520 "balloon_eval_term",
6521#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006522#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6523 "builtin_terms",
6524# ifdef ALL_BUILTIN_TCAPS
6525 "all_builtin_terms",
6526# endif
6527#endif
6528#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006529 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006530 || defined(FEAT_GUI_MOTIF))
6531 "browsefilter",
6532#endif
6533#ifdef FEAT_BYTEOFF
6534 "byte_offset",
6535#endif
6536#ifdef FEAT_JOB_CHANNEL
6537 "channel",
6538#endif
6539#ifdef FEAT_CINDENT
6540 "cindent",
6541#endif
6542#ifdef FEAT_CLIENTSERVER
6543 "clientserver",
6544#endif
6545#ifdef FEAT_CLIPBOARD
6546 "clipboard",
6547#endif
6548#ifdef FEAT_CMDL_COMPL
6549 "cmdline_compl",
6550#endif
6551#ifdef FEAT_CMDHIST
6552 "cmdline_hist",
6553#endif
6554#ifdef FEAT_COMMENTS
6555 "comments",
6556#endif
6557#ifdef FEAT_CONCEAL
6558 "conceal",
6559#endif
6560#ifdef FEAT_CRYPT
6561 "cryptv",
6562 "crypt-blowfish",
6563 "crypt-blowfish2",
6564#endif
6565#ifdef FEAT_CSCOPE
6566 "cscope",
6567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006568 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006569#ifdef CURSOR_SHAPE
6570 "cursorshape",
6571#endif
6572#ifdef DEBUG
6573 "debug",
6574#endif
6575#ifdef FEAT_CON_DIALOG
6576 "dialog_con",
6577#endif
6578#ifdef FEAT_GUI_DIALOG
6579 "dialog_gui",
6580#endif
6581#ifdef FEAT_DIFF
6582 "diff",
6583#endif
6584#ifdef FEAT_DIGRAPHS
6585 "digraphs",
6586#endif
6587#ifdef FEAT_DIRECTX
6588 "directx",
6589#endif
6590#ifdef FEAT_DND
6591 "dnd",
6592#endif
6593#ifdef FEAT_EMACS_TAGS
6594 "emacs_tags",
6595#endif
6596 "eval", /* always present, of course! */
6597 "ex_extra", /* graduated feature */
6598#ifdef FEAT_SEARCH_EXTRA
6599 "extra_search",
6600#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006601#ifdef FEAT_SEARCHPATH
6602 "file_in_path",
6603#endif
6604#ifdef FEAT_FILTERPIPE
6605 "filterpipe",
6606#endif
6607#ifdef FEAT_FIND_ID
6608 "find_in_path",
6609#endif
6610#ifdef FEAT_FLOAT
6611 "float",
6612#endif
6613#ifdef FEAT_FOLDING
6614 "folding",
6615#endif
6616#ifdef FEAT_FOOTER
6617 "footer",
6618#endif
6619#if !defined(USE_SYSTEM) && defined(UNIX)
6620 "fork",
6621#endif
6622#ifdef FEAT_GETTEXT
6623 "gettext",
6624#endif
6625#ifdef FEAT_GUI
6626 "gui",
6627#endif
6628#ifdef FEAT_GUI_ATHENA
6629# ifdef FEAT_GUI_NEXTAW
6630 "gui_neXtaw",
6631# else
6632 "gui_athena",
6633# endif
6634#endif
6635#ifdef FEAT_GUI_GTK
6636 "gui_gtk",
6637# ifdef USE_GTK3
6638 "gui_gtk3",
6639# else
6640 "gui_gtk2",
6641# endif
6642#endif
6643#ifdef FEAT_GUI_GNOME
6644 "gui_gnome",
6645#endif
6646#ifdef FEAT_GUI_MAC
6647 "gui_mac",
6648#endif
6649#ifdef FEAT_GUI_MOTIF
6650 "gui_motif",
6651#endif
6652#ifdef FEAT_GUI_PHOTON
6653 "gui_photon",
6654#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006655#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006656 "gui_win32",
6657#endif
6658#ifdef FEAT_HANGULIN
6659 "hangul_input",
6660#endif
6661#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6662 "iconv",
6663#endif
6664#ifdef FEAT_INS_EXPAND
6665 "insert_expand",
6666#endif
6667#ifdef FEAT_JOB_CHANNEL
6668 "job",
6669#endif
6670#ifdef FEAT_JUMPLIST
6671 "jumplist",
6672#endif
6673#ifdef FEAT_KEYMAP
6674 "keymap",
6675#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006676 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677#ifdef FEAT_LANGMAP
6678 "langmap",
6679#endif
6680#ifdef FEAT_LIBCALL
6681 "libcall",
6682#endif
6683#ifdef FEAT_LINEBREAK
6684 "linebreak",
6685#endif
6686#ifdef FEAT_LISP
6687 "lispindent",
6688#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006689 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006690#ifdef FEAT_LOCALMAP
6691 "localmap",
6692#endif
6693#ifdef FEAT_LUA
6694# ifndef DYNAMIC_LUA
6695 "lua",
6696# endif
6697#endif
6698#ifdef FEAT_MENU
6699 "menu",
6700#endif
6701#ifdef FEAT_SESSION
6702 "mksession",
6703#endif
6704#ifdef FEAT_MODIFY_FNAME
6705 "modify_fname",
6706#endif
6707#ifdef FEAT_MOUSE
6708 "mouse",
6709#endif
6710#ifdef FEAT_MOUSESHAPE
6711 "mouseshape",
6712#endif
6713#if defined(UNIX) || defined(VMS)
6714# ifdef FEAT_MOUSE_DEC
6715 "mouse_dec",
6716# endif
6717# ifdef FEAT_MOUSE_GPM
6718 "mouse_gpm",
6719# endif
6720# ifdef FEAT_MOUSE_JSB
6721 "mouse_jsbterm",
6722# endif
6723# ifdef FEAT_MOUSE_NET
6724 "mouse_netterm",
6725# endif
6726# ifdef FEAT_MOUSE_PTERM
6727 "mouse_pterm",
6728# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006729# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 "mouse_sgr",
6731# endif
6732# ifdef FEAT_SYSMOUSE
6733 "mouse_sysmouse",
6734# endif
6735# ifdef FEAT_MOUSE_URXVT
6736 "mouse_urxvt",
6737# endif
6738# ifdef FEAT_MOUSE_XTERM
6739 "mouse_xterm",
6740# endif
6741#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743#ifdef FEAT_MBYTE_IME
6744 "multi_byte_ime",
6745#endif
6746#ifdef FEAT_MULTI_LANG
6747 "multi_lang",
6748#endif
6749#ifdef FEAT_MZSCHEME
6750#ifndef DYNAMIC_MZSCHEME
6751 "mzscheme",
6752#endif
6753#endif
6754#ifdef FEAT_NUM64
6755 "num64",
6756#endif
6757#ifdef FEAT_OLE
6758 "ole",
6759#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006760#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006762#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006763#ifdef FEAT_PATH_EXTRA
6764 "path_extra",
6765#endif
6766#ifdef FEAT_PERL
6767#ifndef DYNAMIC_PERL
6768 "perl",
6769#endif
6770#endif
6771#ifdef FEAT_PERSISTENT_UNDO
6772 "persistent_undo",
6773#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006774#if defined(FEAT_PYTHON)
6775 "python_compiled",
6776# if defined(DYNAMIC_PYTHON)
6777 "python_dynamic",
6778# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006780 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006781# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006783#if defined(FEAT_PYTHON3)
6784 "python3_compiled",
6785# if defined(DYNAMIC_PYTHON3)
6786 "python3_dynamic",
6787# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006789 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006790# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006791#endif
6792#ifdef FEAT_POSTSCRIPT
6793 "postscript",
6794#endif
6795#ifdef FEAT_PRINTER
6796 "printer",
6797#endif
6798#ifdef FEAT_PROFILE
6799 "profile",
6800#endif
6801#ifdef FEAT_RELTIME
6802 "reltime",
6803#endif
6804#ifdef FEAT_QUICKFIX
6805 "quickfix",
6806#endif
6807#ifdef FEAT_RIGHTLEFT
6808 "rightleft",
6809#endif
6810#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6811 "ruby",
6812#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814#ifdef FEAT_CMDL_INFO
6815 "showcmd",
6816 "cmdline_info",
6817#endif
6818#ifdef FEAT_SIGNS
6819 "signs",
6820#endif
6821#ifdef FEAT_SMARTINDENT
6822 "smartindent",
6823#endif
6824#ifdef STARTUPTIME
6825 "startuptime",
6826#endif
6827#ifdef FEAT_STL_OPT
6828 "statusline",
6829#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006830#ifdef FEAT_NETBEANS_INTG
6831 "netbeans_intg",
6832#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006833#ifdef FEAT_SOUND
6834 "sound",
6835#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006836#ifdef FEAT_SPELL
6837 "spell",
6838#endif
6839#ifdef FEAT_SYN_HL
6840 "syntax",
6841#endif
6842#if defined(USE_SYSTEM) || !defined(UNIX)
6843 "system",
6844#endif
6845#ifdef FEAT_TAG_BINS
6846 "tag_binary",
6847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848#ifdef FEAT_TCL
6849# ifndef DYNAMIC_TCL
6850 "tcl",
6851# endif
6852#endif
6853#ifdef FEAT_TERMGUICOLORS
6854 "termguicolors",
6855#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006856#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006857 "terminal",
6858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859#ifdef TERMINFO
6860 "terminfo",
6861#endif
6862#ifdef FEAT_TERMRESPONSE
6863 "termresponse",
6864#endif
6865#ifdef FEAT_TEXTOBJ
6866 "textobjects",
6867#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006868#ifdef FEAT_TEXT_PROP
6869 "textprop",
6870#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006871#ifdef HAVE_TGETENT
6872 "tgetent",
6873#endif
6874#ifdef FEAT_TIMERS
6875 "timers",
6876#endif
6877#ifdef FEAT_TITLE
6878 "title",
6879#endif
6880#ifdef FEAT_TOOLBAR
6881 "toolbar",
6882#endif
6883#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6884 "unnamedplus",
6885#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006886 "user-commands", /* was accidentally included in 5.4 */
6887 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006888#ifdef FEAT_VARTABS
6889 "vartabs",
6890#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006891 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006892#ifdef FEAT_VIMINFO
6893 "viminfo",
6894#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006895 "vimscript-1",
6896 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006897 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006901 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006902#ifdef FEAT_VTP
6903 "vtp",
6904#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006905#ifdef FEAT_WILDIGN
6906 "wildignore",
6907#endif
6908#ifdef FEAT_WILDMENU
6909 "wildmenu",
6910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006911 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006912#ifdef FEAT_WAK
6913 "winaltkeys",
6914#endif
6915#ifdef FEAT_WRITEBACKUP
6916 "writebackup",
6917#endif
6918#ifdef FEAT_XIM
6919 "xim",
6920#endif
6921#ifdef FEAT_XFONTSET
6922 "xfontset",
6923#endif
6924#ifdef FEAT_XPM_W32
6925 "xpm",
6926 "xpm_w32", /* for backward compatibility */
6927#else
6928# if defined(HAVE_XPM)
6929 "xpm",
6930# endif
6931#endif
6932#ifdef USE_XSMP
6933 "xsmp",
6934#endif
6935#ifdef USE_XSMP_INTERACT
6936 "xsmp_interact",
6937#endif
6938#ifdef FEAT_XCLIPBOARD
6939 "xterm_clipboard",
6940#endif
6941#ifdef FEAT_XTERM_SAVE
6942 "xterm_save",
6943#endif
6944#if defined(UNIX) && defined(FEAT_X11)
6945 "X11",
6946#endif
6947 NULL
6948 };
6949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006950 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951 for (i = 0; has_list[i] != NULL; ++i)
6952 if (STRICMP(name, has_list[i]) == 0)
6953 {
6954 n = TRUE;
6955 break;
6956 }
6957
6958 if (n == FALSE)
6959 {
6960 if (STRNICMP(name, "patch", 5) == 0)
6961 {
6962 if (name[5] == '-'
6963 && STRLEN(name) >= 11
6964 && vim_isdigit(name[6])
6965 && vim_isdigit(name[8])
6966 && vim_isdigit(name[10]))
6967 {
6968 int major = atoi((char *)name + 6);
6969 int minor = atoi((char *)name + 8);
6970
6971 /* Expect "patch-9.9.01234". */
6972 n = (major < VIM_VERSION_MAJOR
6973 || (major == VIM_VERSION_MAJOR
6974 && (minor < VIM_VERSION_MINOR
6975 || (minor == VIM_VERSION_MINOR
6976 && has_patch(atoi((char *)name + 10))))));
6977 }
6978 else
6979 n = has_patch(atoi((char *)name + 5));
6980 }
6981 else if (STRICMP(name, "vim_starting") == 0)
6982 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006983 else if (STRICMP(name, "ttyin") == 0)
6984 n = mch_input_isatty();
6985 else if (STRICMP(name, "ttyout") == 0)
6986 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 else if (STRICMP(name, "multi_byte_encoding") == 0)
6988 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006989#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006990 else if (STRICMP(name, "balloon_multiline") == 0)
6991 n = multiline_balloon_available();
6992#endif
6993#ifdef DYNAMIC_TCL
6994 else if (STRICMP(name, "tcl") == 0)
6995 n = tcl_enabled(FALSE);
6996#endif
6997#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6998 else if (STRICMP(name, "iconv") == 0)
6999 n = iconv_enabled(FALSE);
7000#endif
7001#ifdef DYNAMIC_LUA
7002 else if (STRICMP(name, "lua") == 0)
7003 n = lua_enabled(FALSE);
7004#endif
7005#ifdef DYNAMIC_MZSCHEME
7006 else if (STRICMP(name, "mzscheme") == 0)
7007 n = mzscheme_enabled(FALSE);
7008#endif
7009#ifdef DYNAMIC_RUBY
7010 else if (STRICMP(name, "ruby") == 0)
7011 n = ruby_enabled(FALSE);
7012#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007013#ifdef DYNAMIC_PYTHON
7014 else if (STRICMP(name, "python") == 0)
7015 n = python_enabled(FALSE);
7016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017#ifdef DYNAMIC_PYTHON3
7018 else if (STRICMP(name, "python3") == 0)
7019 n = python3_enabled(FALSE);
7020#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007021#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7022 else if (STRICMP(name, "pythonx") == 0)
7023 {
7024# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7025 if (p_pyx == 0)
7026 n = python3_enabled(FALSE) || python_enabled(FALSE);
7027 else if (p_pyx == 3)
7028 n = python3_enabled(FALSE);
7029 else if (p_pyx == 2)
7030 n = python_enabled(FALSE);
7031# elif defined(DYNAMIC_PYTHON)
7032 n = python_enabled(FALSE);
7033# elif defined(DYNAMIC_PYTHON3)
7034 n = python3_enabled(FALSE);
7035# endif
7036 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037#endif
7038#ifdef DYNAMIC_PERL
7039 else if (STRICMP(name, "perl") == 0)
7040 n = perl_enabled(FALSE);
7041#endif
7042#ifdef FEAT_GUI
7043 else if (STRICMP(name, "gui_running") == 0)
7044 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007045# ifdef FEAT_BROWSE
7046 else if (STRICMP(name, "browse") == 0)
7047 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7048# endif
7049#endif
7050#ifdef FEAT_SYN_HL
7051 else if (STRICMP(name, "syntax_items") == 0)
7052 n = syntax_present(curwin);
7053#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007054#ifdef FEAT_VTP
7055 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007056 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007057#endif
7058#ifdef FEAT_NETBEANS_INTG
7059 else if (STRICMP(name, "netbeans_enabled") == 0)
7060 n = netbeans_active();
7061#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007062#ifdef FEAT_MOUSE_GPM
7063 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7064 n = gpm_enabled();
7065#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007066#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007067 else if (STRICMP(name, "terminal") == 0)
7068 n = terminal_enabled();
7069#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007070#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007071 else if (STRICMP(name, "conpty") == 0)
7072 n = use_conpty();
7073#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 }
7075
7076 rettv->vval.v_number = n;
7077}
7078
7079/*
7080 * "has_key()" function
7081 */
7082 static void
7083f_has_key(typval_T *argvars, typval_T *rettv)
7084{
7085 if (argvars[0].v_type != VAR_DICT)
7086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007087 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007088 return;
7089 }
7090 if (argvars[0].vval.v_dict == NULL)
7091 return;
7092
7093 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007094 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007095}
7096
7097/*
7098 * "haslocaldir()" function
7099 */
7100 static void
7101f_haslocaldir(typval_T *argvars, typval_T *rettv)
7102{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007103 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007104 win_T *wp = NULL;
7105
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007106 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7107
7108 // Check for window-local and tab-local directories
7109 if (wp != NULL && wp->w_localdir != NULL)
7110 rettv->vval.v_number = 1;
7111 else if (tp != NULL && tp->tp_localdir != NULL)
7112 rettv->vval.v_number = 2;
7113 else
7114 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007115}
7116
7117/*
7118 * "hasmapto()" function
7119 */
7120 static void
7121f_hasmapto(typval_T *argvars, typval_T *rettv)
7122{
7123 char_u *name;
7124 char_u *mode;
7125 char_u buf[NUMBUFLEN];
7126 int abbr = FALSE;
7127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007128 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (argvars[1].v_type == VAR_UNKNOWN)
7130 mode = (char_u *)"nvo";
7131 else
7132 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007133 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007134 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007135 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 }
7137
7138 if (map_to_exists(name, mode, abbr))
7139 rettv->vval.v_number = TRUE;
7140 else
7141 rettv->vval.v_number = FALSE;
7142}
7143
7144/*
7145 * "histadd()" function
7146 */
7147 static void
7148f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7149{
7150#ifdef FEAT_CMDHIST
7151 int histype;
7152 char_u *str;
7153 char_u buf[NUMBUFLEN];
7154#endif
7155
7156 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007157 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158 return;
7159#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007160 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007161 histype = str != NULL ? get_histtype(str) : -1;
7162 if (histype >= 0)
7163 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007164 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 if (*str != NUL)
7166 {
7167 init_history();
7168 add_to_history(histype, str, FALSE, NUL);
7169 rettv->vval.v_number = TRUE;
7170 return;
7171 }
7172 }
7173#endif
7174}
7175
7176/*
7177 * "histdel()" function
7178 */
7179 static void
7180f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7181{
7182#ifdef FEAT_CMDHIST
7183 int n;
7184 char_u buf[NUMBUFLEN];
7185 char_u *str;
7186
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007187 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007188 if (str == NULL)
7189 n = 0;
7190 else if (argvars[1].v_type == VAR_UNKNOWN)
7191 /* only one argument: clear entire history */
7192 n = clr_history(get_histtype(str));
7193 else if (argvars[1].v_type == VAR_NUMBER)
7194 /* index given: remove that entry */
7195 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007196 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007197 else
7198 /* string given: remove all matching entries */
7199 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007200 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007201 rettv->vval.v_number = n;
7202#endif
7203}
7204
7205/*
7206 * "histget()" function
7207 */
7208 static void
7209f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7210{
7211#ifdef FEAT_CMDHIST
7212 int type;
7213 int idx;
7214 char_u *str;
7215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007216 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007217 if (str == NULL)
7218 rettv->vval.v_string = NULL;
7219 else
7220 {
7221 type = get_histtype(str);
7222 if (argvars[1].v_type == VAR_UNKNOWN)
7223 idx = get_history_idx(type);
7224 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007225 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007226 /* -1 on type error */
7227 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7228 }
7229#else
7230 rettv->vval.v_string = NULL;
7231#endif
7232 rettv->v_type = VAR_STRING;
7233}
7234
7235/*
7236 * "histnr()" function
7237 */
7238 static void
7239f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7240{
7241 int i;
7242
7243#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007244 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245
7246 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7247 if (i >= HIST_CMD && i < HIST_COUNT)
7248 i = get_history_idx(i);
7249 else
7250#endif
7251 i = -1;
7252 rettv->vval.v_number = i;
7253}
7254
7255/*
7256 * "highlightID(name)" function
7257 */
7258 static void
7259f_hlID(typval_T *argvars, typval_T *rettv)
7260{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007261 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007262}
7263
7264/*
7265 * "highlight_exists()" function
7266 */
7267 static void
7268f_hlexists(typval_T *argvars, typval_T *rettv)
7269{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007270 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271}
7272
7273/*
7274 * "hostname()" function
7275 */
7276 static void
7277f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7278{
7279 char_u hostname[256];
7280
7281 mch_get_host_name(hostname, 256);
7282 rettv->v_type = VAR_STRING;
7283 rettv->vval.v_string = vim_strsave(hostname);
7284}
7285
7286/*
7287 * iconv() function
7288 */
7289 static void
7290f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7291{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 char_u buf1[NUMBUFLEN];
7293 char_u buf2[NUMBUFLEN];
7294 char_u *from, *to, *str;
7295 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007296
7297 rettv->v_type = VAR_STRING;
7298 rettv->vval.v_string = NULL;
7299
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007300 str = tv_get_string(&argvars[0]);
7301 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7302 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 vimconv.vc_type = CONV_NONE;
7304 convert_setup(&vimconv, from, to);
7305
7306 /* If the encodings are equal, no conversion needed. */
7307 if (vimconv.vc_type == CONV_NONE)
7308 rettv->vval.v_string = vim_strsave(str);
7309 else
7310 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7311
7312 convert_setup(&vimconv, NULL, NULL);
7313 vim_free(from);
7314 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007315}
7316
7317/*
7318 * "indent()" function
7319 */
7320 static void
7321f_indent(typval_T *argvars, typval_T *rettv)
7322{
7323 linenr_T lnum;
7324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007325 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007326 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7327 rettv->vval.v_number = get_indent_lnum(lnum);
7328 else
7329 rettv->vval.v_number = -1;
7330}
7331
7332/*
7333 * "index()" function
7334 */
7335 static void
7336f_index(typval_T *argvars, typval_T *rettv)
7337{
7338 list_T *l;
7339 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007340 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341 long idx = 0;
7342 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007343 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007344
7345 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007346 if (argvars[0].v_type == VAR_BLOB)
7347 {
7348 typval_T tv;
7349 int start = 0;
7350
7351 if (argvars[2].v_type != VAR_UNKNOWN)
7352 {
7353 start = tv_get_number_chk(&argvars[2], &error);
7354 if (error)
7355 return;
7356 }
7357 b = argvars[0].vval.v_blob;
7358 if (b == NULL)
7359 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007360 if (start < 0)
7361 {
7362 start = blob_len(b) + start;
7363 if (start < 0)
7364 start = 0;
7365 }
7366
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007367 for (idx = start; idx < blob_len(b); ++idx)
7368 {
7369 tv.v_type = VAR_NUMBER;
7370 tv.vval.v_number = blob_get(b, idx);
7371 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7372 {
7373 rettv->vval.v_number = idx;
7374 return;
7375 }
7376 }
7377 return;
7378 }
7379 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007380 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007381 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 return;
7383 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007384
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385 l = argvars[0].vval.v_list;
7386 if (l != NULL)
7387 {
7388 item = l->lv_first;
7389 if (argvars[2].v_type != VAR_UNKNOWN)
7390 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007391 /* Start at specified item. Use the cached index that list_find()
7392 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007393 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394 idx = l->lv_idx;
7395 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007396 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007397 if (error)
7398 item = NULL;
7399 }
7400
7401 for ( ; item != NULL; item = item->li_next, ++idx)
7402 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7403 {
7404 rettv->vval.v_number = idx;
7405 break;
7406 }
7407 }
7408}
7409
7410static int inputsecret_flag = 0;
7411
7412/*
7413 * "input()" function
7414 * Also handles inputsecret() when inputsecret is set.
7415 */
7416 static void
7417f_input(typval_T *argvars, typval_T *rettv)
7418{
7419 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7420}
7421
7422/*
7423 * "inputdialog()" function
7424 */
7425 static void
7426f_inputdialog(typval_T *argvars, typval_T *rettv)
7427{
7428#if defined(FEAT_GUI_TEXTDIALOG)
7429 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7430 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7431 {
7432 char_u *message;
7433 char_u buf[NUMBUFLEN];
7434 char_u *defstr = (char_u *)"";
7435
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007436 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007437 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007438 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007439 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7440 else
7441 IObuff[0] = NUL;
7442 if (message != NULL && defstr != NULL
7443 && do_dialog(VIM_QUESTION, NULL, message,
7444 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7445 rettv->vval.v_string = vim_strsave(IObuff);
7446 else
7447 {
7448 if (message != NULL && defstr != NULL
7449 && argvars[1].v_type != VAR_UNKNOWN
7450 && argvars[2].v_type != VAR_UNKNOWN)
7451 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007452 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453 else
7454 rettv->vval.v_string = NULL;
7455 }
7456 rettv->v_type = VAR_STRING;
7457 }
7458 else
7459#endif
7460 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7461}
7462
7463/*
7464 * "inputlist()" function
7465 */
7466 static void
7467f_inputlist(typval_T *argvars, typval_T *rettv)
7468{
7469 listitem_T *li;
7470 int selected;
7471 int mouse_used;
7472
7473#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007474 /* While starting up, there is no place to enter text. When running tests
7475 * with --not-a-term we assume feedkeys() will be used. */
7476 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007477 return;
7478#endif
7479 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007481 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007482 return;
7483 }
7484
7485 msg_start();
7486 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7487 lines_left = Rows; /* avoid more prompt */
7488 msg_scroll = TRUE;
7489 msg_clr_eos();
7490
7491 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7492 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007493 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007494 msg_putchar('\n');
7495 }
7496
7497 /* Ask for choice. */
7498 selected = prompt_for_number(&mouse_used);
7499 if (mouse_used)
7500 selected -= lines_left;
7501
7502 rettv->vval.v_number = selected;
7503}
7504
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007505static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7506
7507/*
7508 * "inputrestore()" function
7509 */
7510 static void
7511f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7512{
7513 if (ga_userinput.ga_len > 0)
7514 {
7515 --ga_userinput.ga_len;
7516 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7517 + ga_userinput.ga_len);
7518 /* default return is zero == OK */
7519 }
7520 else if (p_verbose > 1)
7521 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007522 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007523 rettv->vval.v_number = 1; /* Failed */
7524 }
7525}
7526
7527/*
7528 * "inputsave()" function
7529 */
7530 static void
7531f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7532{
7533 /* Add an entry to the stack of typeahead storage. */
7534 if (ga_grow(&ga_userinput, 1) == OK)
7535 {
7536 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7537 + ga_userinput.ga_len);
7538 ++ga_userinput.ga_len;
7539 /* default return is zero == OK */
7540 }
7541 else
7542 rettv->vval.v_number = 1; /* Failed */
7543}
7544
7545/*
7546 * "inputsecret()" function
7547 */
7548 static void
7549f_inputsecret(typval_T *argvars, typval_T *rettv)
7550{
7551 ++cmdline_star;
7552 ++inputsecret_flag;
7553 f_input(argvars, rettv);
7554 --cmdline_star;
7555 --inputsecret_flag;
7556}
7557
7558/*
7559 * "insert()" function
7560 */
7561 static void
7562f_insert(typval_T *argvars, typval_T *rettv)
7563{
7564 long before = 0;
7565 listitem_T *item;
7566 list_T *l;
7567 int error = FALSE;
7568
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007569 if (argvars[0].v_type == VAR_BLOB)
7570 {
7571 int val, len;
7572 char_u *p;
7573
7574 len = blob_len(argvars[0].vval.v_blob);
7575 if (argvars[2].v_type != VAR_UNKNOWN)
7576 {
7577 before = (long)tv_get_number_chk(&argvars[2], &error);
7578 if (error)
7579 return; // type error; errmsg already given
7580 if (before < 0 || before > len)
7581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007582 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007583 return;
7584 }
7585 }
7586 val = tv_get_number_chk(&argvars[1], &error);
7587 if (error)
7588 return;
7589 if (val < 0 || val > 255)
7590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007592 return;
7593 }
7594
7595 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7596 return;
7597 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7598 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7599 *(p + before) = val;
7600 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7601
7602 copy_tv(&argvars[0], rettv);
7603 }
7604 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007605 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007606 else if ((l = argvars[0].vval.v_list) != NULL
7607 && !var_check_lock(l->lv_lock,
7608 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 {
7610 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007611 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 if (error)
7613 return; /* type error; errmsg already given */
7614
7615 if (before == l->lv_len)
7616 item = NULL;
7617 else
7618 {
7619 item = list_find(l, before);
7620 if (item == NULL)
7621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007622 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007623 l = NULL;
7624 }
7625 }
7626 if (l != NULL)
7627 {
7628 list_insert_tv(l, &argvars[1], item);
7629 copy_tv(&argvars[0], rettv);
7630 }
7631 }
7632}
7633
7634/*
7635 * "invert(expr)" function
7636 */
7637 static void
7638f_invert(typval_T *argvars, typval_T *rettv)
7639{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007640 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641}
7642
7643/*
7644 * "isdirectory()" function
7645 */
7646 static void
7647f_isdirectory(typval_T *argvars, typval_T *rettv)
7648{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007649 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007650}
7651
7652/*
7653 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7654 * or it refers to a List or Dictionary that is locked.
7655 */
7656 static int
7657tv_islocked(typval_T *tv)
7658{
7659 return (tv->v_lock & VAR_LOCKED)
7660 || (tv->v_type == VAR_LIST
7661 && tv->vval.v_list != NULL
7662 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7663 || (tv->v_type == VAR_DICT
7664 && tv->vval.v_dict != NULL
7665 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7666}
7667
7668/*
7669 * "islocked()" function
7670 */
7671 static void
7672f_islocked(typval_T *argvars, typval_T *rettv)
7673{
7674 lval_T lv;
7675 char_u *end;
7676 dictitem_T *di;
7677
7678 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007679 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007680 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007681 if (end != NULL && lv.ll_name != NULL)
7682 {
7683 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007684 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007685 else
7686 {
7687 if (lv.ll_tv == NULL)
7688 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007689 di = find_var(lv.ll_name, NULL, TRUE);
7690 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007691 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007692 /* Consider a variable locked when:
7693 * 1. the variable itself is locked
7694 * 2. the value of the variable is locked.
7695 * 3. the List or Dict value is locked.
7696 */
7697 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7698 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007699 }
7700 }
7701 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007702 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007703 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007704 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007705 else if (lv.ll_list != NULL)
7706 /* List item. */
7707 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7708 else
7709 /* Dictionary item. */
7710 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7711 }
7712 }
7713
7714 clear_lval(&lv);
7715}
7716
7717#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7718/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007719 * "isinf()" function
7720 */
7721 static void
7722f_isinf(typval_T *argvars, typval_T *rettv)
7723{
7724 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7725 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7726}
7727
7728/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 * "isnan()" function
7730 */
7731 static void
7732f_isnan(typval_T *argvars, typval_T *rettv)
7733{
7734 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7735 && isnan(argvars[0].vval.v_float);
7736}
7737#endif
7738
7739/*
7740 * "items(dict)" function
7741 */
7742 static void
7743f_items(typval_T *argvars, typval_T *rettv)
7744{
7745 dict_list(argvars, rettv, 2);
7746}
7747
7748#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7749/*
7750 * Get the job from the argument.
7751 * Returns NULL if the job is invalid.
7752 */
7753 static job_T *
7754get_job_arg(typval_T *tv)
7755{
7756 job_T *job;
7757
7758 if (tv->v_type != VAR_JOB)
7759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007760 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 return NULL;
7762 }
7763 job = tv->vval.v_job;
7764
7765 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007766 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007767 return job;
7768}
7769
7770/*
7771 * "job_getchannel()" function
7772 */
7773 static void
7774f_job_getchannel(typval_T *argvars, typval_T *rettv)
7775{
7776 job_T *job = get_job_arg(&argvars[0]);
7777
7778 if (job != NULL)
7779 {
7780 rettv->v_type = VAR_CHANNEL;
7781 rettv->vval.v_channel = job->jv_channel;
7782 if (job->jv_channel != NULL)
7783 ++job->jv_channel->ch_refcount;
7784 }
7785}
7786
7787/*
7788 * "job_info()" function
7789 */
7790 static void
7791f_job_info(typval_T *argvars, typval_T *rettv)
7792{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007793 if (argvars[0].v_type != VAR_UNKNOWN)
7794 {
7795 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007796
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007797 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7798 job_info(job, rettv->vval.v_dict);
7799 }
7800 else if (rettv_list_alloc(rettv) == OK)
7801 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007802}
7803
7804/*
7805 * "job_setoptions()" function
7806 */
7807 static void
7808f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7809{
7810 job_T *job = get_job_arg(&argvars[0]);
7811 jobopt_T opt;
7812
7813 if (job == NULL)
7814 return;
7815 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007816 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 job_set_options(job, &opt);
7818 free_job_options(&opt);
7819}
7820
7821/*
7822 * "job_start()" function
7823 */
7824 static void
7825f_job_start(typval_T *argvars, typval_T *rettv)
7826{
7827 rettv->v_type = VAR_JOB;
7828 if (check_restricted() || check_secure())
7829 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007830 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007831}
7832
7833/*
7834 * "job_status()" function
7835 */
7836 static void
7837f_job_status(typval_T *argvars, typval_T *rettv)
7838{
7839 job_T *job = get_job_arg(&argvars[0]);
7840
7841 if (job != NULL)
7842 {
7843 rettv->v_type = VAR_STRING;
7844 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7845 }
7846}
7847
7848/*
7849 * "job_stop()" function
7850 */
7851 static void
7852f_job_stop(typval_T *argvars, typval_T *rettv)
7853{
7854 job_T *job = get_job_arg(&argvars[0]);
7855
7856 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007857 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007858}
7859#endif
7860
7861/*
7862 * "join()" function
7863 */
7864 static void
7865f_join(typval_T *argvars, typval_T *rettv)
7866{
7867 garray_T ga;
7868 char_u *sep;
7869
7870 if (argvars[0].v_type != VAR_LIST)
7871 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007872 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007873 return;
7874 }
7875 if (argvars[0].vval.v_list == NULL)
7876 return;
7877 if (argvars[1].v_type == VAR_UNKNOWN)
7878 sep = (char_u *)" ";
7879 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007880 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881
7882 rettv->v_type = VAR_STRING;
7883
7884 if (sep != NULL)
7885 {
7886 ga_init2(&ga, (int)sizeof(char), 80);
7887 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7888 ga_append(&ga, NUL);
7889 rettv->vval.v_string = (char_u *)ga.ga_data;
7890 }
7891 else
7892 rettv->vval.v_string = NULL;
7893}
7894
7895/*
7896 * "js_decode()" function
7897 */
7898 static void
7899f_js_decode(typval_T *argvars, typval_T *rettv)
7900{
7901 js_read_T reader;
7902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007903 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007904 reader.js_fill = NULL;
7905 reader.js_used = 0;
7906 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007907 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007908}
7909
7910/*
7911 * "js_encode()" function
7912 */
7913 static void
7914f_js_encode(typval_T *argvars, typval_T *rettv)
7915{
7916 rettv->v_type = VAR_STRING;
7917 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7918}
7919
7920/*
7921 * "json_decode()" function
7922 */
7923 static void
7924f_json_decode(typval_T *argvars, typval_T *rettv)
7925{
7926 js_read_T reader;
7927
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007928 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 reader.js_fill = NULL;
7930 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007931 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007932}
7933
7934/*
7935 * "json_encode()" function
7936 */
7937 static void
7938f_json_encode(typval_T *argvars, typval_T *rettv)
7939{
7940 rettv->v_type = VAR_STRING;
7941 rettv->vval.v_string = json_encode(&argvars[0], 0);
7942}
7943
7944/*
7945 * "keys()" function
7946 */
7947 static void
7948f_keys(typval_T *argvars, typval_T *rettv)
7949{
7950 dict_list(argvars, rettv, 0);
7951}
7952
7953/*
7954 * "last_buffer_nr()" function.
7955 */
7956 static void
7957f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7958{
7959 int n = 0;
7960 buf_T *buf;
7961
Bram Moolenaar29323592016-07-24 22:04:11 +02007962 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007963 if (n < buf->b_fnum)
7964 n = buf->b_fnum;
7965
7966 rettv->vval.v_number = n;
7967}
7968
7969/*
7970 * "len()" function
7971 */
7972 static void
7973f_len(typval_T *argvars, typval_T *rettv)
7974{
7975 switch (argvars[0].v_type)
7976 {
7977 case VAR_STRING:
7978 case VAR_NUMBER:
7979 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007980 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007981 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007982 case VAR_BLOB:
7983 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7984 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007985 case VAR_LIST:
7986 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7987 break;
7988 case VAR_DICT:
7989 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7990 break;
7991 case VAR_UNKNOWN:
7992 case VAR_SPECIAL:
7993 case VAR_FLOAT:
7994 case VAR_FUNC:
7995 case VAR_PARTIAL:
7996 case VAR_JOB:
7997 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007998 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007999 break;
8000 }
8001}
8002
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008003 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01008004libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008005{
8006#ifdef FEAT_LIBCALL
8007 char_u *string_in;
8008 char_u **string_result;
8009 int nr_result;
8010#endif
8011
8012 rettv->v_type = type;
8013 if (type != VAR_NUMBER)
8014 rettv->vval.v_string = NULL;
8015
8016 if (check_restricted() || check_secure())
8017 return;
8018
8019#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008020 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8022 {
8023 string_in = NULL;
8024 if (argvars[2].v_type == VAR_STRING)
8025 string_in = argvars[2].vval.v_string;
8026 if (type == VAR_NUMBER)
8027 string_result = NULL;
8028 else
8029 string_result = &rettv->vval.v_string;
8030 if (mch_libcall(argvars[0].vval.v_string,
8031 argvars[1].vval.v_string,
8032 string_in,
8033 argvars[2].vval.v_number,
8034 string_result,
8035 &nr_result) == OK
8036 && type == VAR_NUMBER)
8037 rettv->vval.v_number = nr_result;
8038 }
8039#endif
8040}
8041
8042/*
8043 * "libcall()" function
8044 */
8045 static void
8046f_libcall(typval_T *argvars, typval_T *rettv)
8047{
8048 libcall_common(argvars, rettv, VAR_STRING);
8049}
8050
8051/*
8052 * "libcallnr()" function
8053 */
8054 static void
8055f_libcallnr(typval_T *argvars, typval_T *rettv)
8056{
8057 libcall_common(argvars, rettv, VAR_NUMBER);
8058}
8059
8060/*
8061 * "line(string)" function
8062 */
8063 static void
8064f_line(typval_T *argvars, typval_T *rettv)
8065{
8066 linenr_T lnum = 0;
8067 pos_T *fp;
8068 int fnum;
8069
8070 fp = var2fpos(&argvars[0], TRUE, &fnum);
8071 if (fp != NULL)
8072 lnum = fp->lnum;
8073 rettv->vval.v_number = lnum;
8074}
8075
8076/*
8077 * "line2byte(lnum)" function
8078 */
8079 static void
8080f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8081{
8082#ifndef FEAT_BYTEOFF
8083 rettv->vval.v_number = -1;
8084#else
8085 linenr_T lnum;
8086
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008087 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8089 rettv->vval.v_number = -1;
8090 else
8091 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8092 if (rettv->vval.v_number >= 0)
8093 ++rettv->vval.v_number;
8094#endif
8095}
8096
8097/*
8098 * "lispindent(lnum)" function
8099 */
8100 static void
8101f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8102{
8103#ifdef FEAT_LISP
8104 pos_T pos;
8105 linenr_T lnum;
8106
8107 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8110 {
8111 curwin->w_cursor.lnum = lnum;
8112 rettv->vval.v_number = get_lisp_indent();
8113 curwin->w_cursor = pos;
8114 }
8115 else
8116#endif
8117 rettv->vval.v_number = -1;
8118}
8119
8120/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008121 * "list2str()" function
8122 */
8123 static void
8124f_list2str(typval_T *argvars, typval_T *rettv)
8125{
8126 list_T *l;
8127 listitem_T *li;
8128 garray_T ga;
8129 int utf8 = FALSE;
8130
8131 rettv->v_type = VAR_STRING;
8132 rettv->vval.v_string = NULL;
8133 if (argvars[0].v_type != VAR_LIST)
8134 {
8135 emsg(_(e_invarg));
8136 return;
8137 }
8138
8139 l = argvars[0].vval.v_list;
8140 if (l == NULL)
8141 return; // empty list results in empty string
8142
8143 if (argvars[1].v_type != VAR_UNKNOWN)
8144 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8145
8146 ga_init2(&ga, 1, 80);
8147 if (has_mbyte || utf8)
8148 {
8149 char_u buf[MB_MAXBYTES + 1];
8150 int (*char2bytes)(int, char_u *);
8151
8152 if (utf8 || enc_utf8)
8153 char2bytes = utf_char2bytes;
8154 else
8155 char2bytes = mb_char2bytes;
8156
8157 for (li = l->lv_first; li != NULL; li = li->li_next)
8158 {
8159 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8160 ga_concat(&ga, buf);
8161 }
8162 ga_append(&ga, NUL);
8163 }
8164 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8165 {
8166 for (li = l->lv_first; li != NULL; li = li->li_next)
8167 ga_append(&ga, tv_get_number(&li->li_tv));
8168 ga_append(&ga, NUL);
8169 }
8170
8171 rettv->v_type = VAR_STRING;
8172 rettv->vval.v_string = ga.ga_data;
8173}
8174
8175/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008176 * "localtime()" function
8177 */
8178 static void
8179f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8180{
8181 rettv->vval.v_number = (varnumber_T)time(NULL);
8182}
8183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008184 static void
8185get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8186{
8187 char_u *keys;
8188 char_u *which;
8189 char_u buf[NUMBUFLEN];
8190 char_u *keys_buf = NULL;
8191 char_u *rhs;
8192 int mode;
8193 int abbr = FALSE;
8194 int get_dict = FALSE;
8195 mapblock_T *mp;
8196 int buffer_local;
8197
8198 /* return empty string for failure */
8199 rettv->v_type = VAR_STRING;
8200 rettv->vval.v_string = NULL;
8201
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008202 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008203 if (*keys == NUL)
8204 return;
8205
8206 if (argvars[1].v_type != VAR_UNKNOWN)
8207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008208 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (argvars[2].v_type != VAR_UNKNOWN)
8210 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008211 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008212 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008213 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 }
8215 }
8216 else
8217 which = (char_u *)"";
8218 if (which == NULL)
8219 return;
8220
8221 mode = get_map_mode(&which, 0);
8222
8223 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8224 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8225 vim_free(keys_buf);
8226
8227 if (!get_dict)
8228 {
8229 /* Return a string. */
8230 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008231 {
8232 if (*rhs == NUL)
8233 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8234 else
8235 rettv->vval.v_string = str2special_save(rhs, FALSE);
8236 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008237
8238 }
8239 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8240 {
8241 /* Return a dictionary. */
8242 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8243 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8244 dict_T *dict = rettv->vval.v_dict;
8245
Bram Moolenaare0be1672018-07-08 16:50:37 +02008246 dict_add_string(dict, "lhs", lhs);
8247 dict_add_string(dict, "rhs", mp->m_orig_str);
8248 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8249 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8250 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008251 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8252 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008253 dict_add_number(dict, "buffer", (long)buffer_local);
8254 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8255 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256
8257 vim_free(lhs);
8258 vim_free(mapmode);
8259 }
8260}
8261
8262#ifdef FEAT_FLOAT
8263/*
8264 * "log()" function
8265 */
8266 static void
8267f_log(typval_T *argvars, typval_T *rettv)
8268{
8269 float_T f = 0.0;
8270
8271 rettv->v_type = VAR_FLOAT;
8272 if (get_float_arg(argvars, &f) == OK)
8273 rettv->vval.v_float = log(f);
8274 else
8275 rettv->vval.v_float = 0.0;
8276}
8277
8278/*
8279 * "log10()" function
8280 */
8281 static void
8282f_log10(typval_T *argvars, typval_T *rettv)
8283{
8284 float_T f = 0.0;
8285
8286 rettv->v_type = VAR_FLOAT;
8287 if (get_float_arg(argvars, &f) == OK)
8288 rettv->vval.v_float = log10(f);
8289 else
8290 rettv->vval.v_float = 0.0;
8291}
8292#endif
8293
8294#ifdef FEAT_LUA
8295/*
8296 * "luaeval()" function
8297 */
8298 static void
8299f_luaeval(typval_T *argvars, typval_T *rettv)
8300{
8301 char_u *str;
8302 char_u buf[NUMBUFLEN];
8303
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008304 if (check_restricted() || check_secure())
8305 return;
8306
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008307 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 do_luaeval(str, argvars + 1, rettv);
8309}
8310#endif
8311
8312/*
8313 * "map()" function
8314 */
8315 static void
8316f_map(typval_T *argvars, typval_T *rettv)
8317{
8318 filter_map(argvars, rettv, TRUE);
8319}
8320
8321/*
8322 * "maparg()" function
8323 */
8324 static void
8325f_maparg(typval_T *argvars, typval_T *rettv)
8326{
8327 get_maparg(argvars, rettv, TRUE);
8328}
8329
8330/*
8331 * "mapcheck()" function
8332 */
8333 static void
8334f_mapcheck(typval_T *argvars, typval_T *rettv)
8335{
8336 get_maparg(argvars, rettv, FALSE);
8337}
8338
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008339typedef enum
8340{
8341 MATCH_END, /* matchend() */
8342 MATCH_MATCH, /* match() */
8343 MATCH_STR, /* matchstr() */
8344 MATCH_LIST, /* matchlist() */
8345 MATCH_POS /* matchstrpos() */
8346} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347
8348 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008349find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008350{
8351 char_u *str = NULL;
8352 long len = 0;
8353 char_u *expr = NULL;
8354 char_u *pat;
8355 regmatch_T regmatch;
8356 char_u patbuf[NUMBUFLEN];
8357 char_u strbuf[NUMBUFLEN];
8358 char_u *save_cpo;
8359 long start = 0;
8360 long nth = 1;
8361 colnr_T startcol = 0;
8362 int match = 0;
8363 list_T *l = NULL;
8364 listitem_T *li = NULL;
8365 long idx = 0;
8366 char_u *tofree = NULL;
8367
8368 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8369 save_cpo = p_cpo;
8370 p_cpo = (char_u *)"";
8371
8372 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008373 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008375 /* type MATCH_LIST: return empty list when there are no matches.
8376 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 if (rettv_list_alloc(rettv) == FAIL)
8378 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008379 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 && (list_append_string(rettv->vval.v_list,
8381 (char_u *)"", 0) == FAIL
8382 || list_append_number(rettv->vval.v_list,
8383 (varnumber_T)-1) == FAIL
8384 || list_append_number(rettv->vval.v_list,
8385 (varnumber_T)-1) == FAIL
8386 || list_append_number(rettv->vval.v_list,
8387 (varnumber_T)-1) == FAIL))
8388 {
8389 list_free(rettv->vval.v_list);
8390 rettv->vval.v_list = NULL;
8391 goto theend;
8392 }
8393 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008394 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 {
8396 rettv->v_type = VAR_STRING;
8397 rettv->vval.v_string = NULL;
8398 }
8399
8400 if (argvars[0].v_type == VAR_LIST)
8401 {
8402 if ((l = argvars[0].vval.v_list) == NULL)
8403 goto theend;
8404 li = l->lv_first;
8405 }
8406 else
8407 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008408 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 len = (long)STRLEN(str);
8410 }
8411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008412 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008413 if (pat == NULL)
8414 goto theend;
8415
8416 if (argvars[2].v_type != VAR_UNKNOWN)
8417 {
8418 int error = FALSE;
8419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008420 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008421 if (error)
8422 goto theend;
8423 if (l != NULL)
8424 {
8425 li = list_find(l, start);
8426 if (li == NULL)
8427 goto theend;
8428 idx = l->lv_idx; /* use the cached index */
8429 }
8430 else
8431 {
8432 if (start < 0)
8433 start = 0;
8434 if (start > len)
8435 goto theend;
8436 /* When "count" argument is there ignore matches before "start",
8437 * otherwise skip part of the string. Differs when pattern is "^"
8438 * or "\<". */
8439 if (argvars[3].v_type != VAR_UNKNOWN)
8440 startcol = start;
8441 else
8442 {
8443 str += start;
8444 len -= start;
8445 }
8446 }
8447
8448 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008449 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008450 if (error)
8451 goto theend;
8452 }
8453
8454 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8455 if (regmatch.regprog != NULL)
8456 {
8457 regmatch.rm_ic = p_ic;
8458
8459 for (;;)
8460 {
8461 if (l != NULL)
8462 {
8463 if (li == NULL)
8464 {
8465 match = FALSE;
8466 break;
8467 }
8468 vim_free(tofree);
8469 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8470 if (str == NULL)
8471 break;
8472 }
8473
8474 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8475
8476 if (match && --nth <= 0)
8477 break;
8478 if (l == NULL && !match)
8479 break;
8480
8481 /* Advance to just after the match. */
8482 if (l != NULL)
8483 {
8484 li = li->li_next;
8485 ++idx;
8486 }
8487 else
8488 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 startcol = (colnr_T)(regmatch.startp[0]
8490 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008491 if (startcol > (colnr_T)len
8492 || str + startcol <= regmatch.startp[0])
8493 {
8494 match = FALSE;
8495 break;
8496 }
8497 }
8498 }
8499
8500 if (match)
8501 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008502 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008503 {
8504 listitem_T *li1 = rettv->vval.v_list->lv_first;
8505 listitem_T *li2 = li1->li_next;
8506 listitem_T *li3 = li2->li_next;
8507 listitem_T *li4 = li3->li_next;
8508
8509 vim_free(li1->li_tv.vval.v_string);
8510 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8511 (int)(regmatch.endp[0] - regmatch.startp[0]));
8512 li3->li_tv.vval.v_number =
8513 (varnumber_T)(regmatch.startp[0] - expr);
8514 li4->li_tv.vval.v_number =
8515 (varnumber_T)(regmatch.endp[0] - expr);
8516 if (l != NULL)
8517 li2->li_tv.vval.v_number = (varnumber_T)idx;
8518 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008519 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008520 {
8521 int i;
8522
8523 /* return list with matched string and submatches */
8524 for (i = 0; i < NSUBEXP; ++i)
8525 {
8526 if (regmatch.endp[i] == NULL)
8527 {
8528 if (list_append_string(rettv->vval.v_list,
8529 (char_u *)"", 0) == FAIL)
8530 break;
8531 }
8532 else if (list_append_string(rettv->vval.v_list,
8533 regmatch.startp[i],
8534 (int)(regmatch.endp[i] - regmatch.startp[i]))
8535 == FAIL)
8536 break;
8537 }
8538 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008539 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540 {
8541 /* return matched string */
8542 if (l != NULL)
8543 copy_tv(&li->li_tv, rettv);
8544 else
8545 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8546 (int)(regmatch.endp[0] - regmatch.startp[0]));
8547 }
8548 else if (l != NULL)
8549 rettv->vval.v_number = idx;
8550 else
8551 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008552 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 rettv->vval.v_number =
8554 (varnumber_T)(regmatch.startp[0] - str);
8555 else
8556 rettv->vval.v_number =
8557 (varnumber_T)(regmatch.endp[0] - str);
8558 rettv->vval.v_number += (varnumber_T)(str - expr);
8559 }
8560 }
8561 vim_regfree(regmatch.regprog);
8562 }
8563
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008564theend:
8565 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 /* matchstrpos() without a list: drop the second item. */
8567 listitem_remove(rettv->vval.v_list,
8568 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008569 vim_free(tofree);
8570 p_cpo = save_cpo;
8571}
8572
8573/*
8574 * "match()" function
8575 */
8576 static void
8577f_match(typval_T *argvars, typval_T *rettv)
8578{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008579 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008580}
8581
Bram Moolenaar95e51472018-07-28 16:55:56 +02008582#ifdef FEAT_SEARCH_EXTRA
8583 static int
8584matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8585{
8586 dictitem_T *di;
8587
8588 if (tv->v_type != VAR_DICT)
8589 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008590 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008591 return FAIL;
8592 }
8593
8594 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008595 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008596 (char_u *)"conceal", FALSE);
8597
8598 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8599 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008600 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008601 if (*win == NULL)
8602 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008603 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008604 return FAIL;
8605 }
8606 }
8607
8608 return OK;
8609}
8610#endif
8611
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612/*
8613 * "matchadd()" function
8614 */
8615 static void
8616f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8617{
8618#ifdef FEAT_SEARCH_EXTRA
8619 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008620 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8621 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 int prio = 10; /* default priority */
8623 int id = -1;
8624 int error = FALSE;
8625 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008626 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627
8628 rettv->vval.v_number = -1;
8629
8630 if (grp == NULL || pat == NULL)
8631 return;
8632 if (argvars[2].v_type != VAR_UNKNOWN)
8633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008634 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008635 if (argvars[3].v_type != VAR_UNKNOWN)
8636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008638 if (argvars[4].v_type != VAR_UNKNOWN
8639 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8640 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008641 }
8642 }
8643 if (error == TRUE)
8644 return;
8645 if (id >= 1 && id <= 3)
8646 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008647 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008648 return;
8649 }
8650
Bram Moolenaar95e51472018-07-28 16:55:56 +02008651 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008652 conceal_char);
8653#endif
8654}
8655
8656/*
8657 * "matchaddpos()" function
8658 */
8659 static void
8660f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8661{
8662#ifdef FEAT_SEARCH_EXTRA
8663 char_u buf[NUMBUFLEN];
8664 char_u *group;
8665 int prio = 10;
8666 int id = -1;
8667 int error = FALSE;
8668 list_T *l;
8669 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008670 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671
8672 rettv->vval.v_number = -1;
8673
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008674 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008675 if (group == NULL)
8676 return;
8677
8678 if (argvars[1].v_type != VAR_LIST)
8679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008680 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681 return;
8682 }
8683 l = argvars[1].vval.v_list;
8684 if (l == NULL)
8685 return;
8686
8687 if (argvars[2].v_type != VAR_UNKNOWN)
8688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008689 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690 if (argvars[3].v_type != VAR_UNKNOWN)
8691 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008692 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008693
8694 if (argvars[4].v_type != VAR_UNKNOWN
8695 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8696 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008697 }
8698 }
8699 if (error == TRUE)
8700 return;
8701
8702 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8703 if (id == 1 || id == 2)
8704 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008705 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706 return;
8707 }
8708
Bram Moolenaar95e51472018-07-28 16:55:56 +02008709 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 conceal_char);
8711#endif
8712}
8713
8714/*
8715 * "matcharg()" function
8716 */
8717 static void
8718f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8719{
8720 if (rettv_list_alloc(rettv) == OK)
8721 {
8722#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008723 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008724 matchitem_T *m;
8725
8726 if (id >= 1 && id <= 3)
8727 {
8728 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8729 {
8730 list_append_string(rettv->vval.v_list,
8731 syn_id2name(m->hlg_id), -1);
8732 list_append_string(rettv->vval.v_list, m->pattern, -1);
8733 }
8734 else
8735 {
8736 list_append_string(rettv->vval.v_list, NULL, -1);
8737 list_append_string(rettv->vval.v_list, NULL, -1);
8738 }
8739 }
8740#endif
8741 }
8742}
8743
8744/*
8745 * "matchdelete()" function
8746 */
8747 static void
8748f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8749{
8750#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008751 win_T *win = get_optional_window(argvars, 1);
8752
8753 if (win == NULL)
8754 rettv->vval.v_number = -1;
8755 else
8756 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758#endif
8759}
8760
8761/*
8762 * "matchend()" function
8763 */
8764 static void
8765f_matchend(typval_T *argvars, typval_T *rettv)
8766{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008767 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008768}
8769
8770/*
8771 * "matchlist()" function
8772 */
8773 static void
8774f_matchlist(typval_T *argvars, typval_T *rettv)
8775{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008776 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008777}
8778
8779/*
8780 * "matchstr()" function
8781 */
8782 static void
8783f_matchstr(typval_T *argvars, typval_T *rettv)
8784{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008785 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008786}
8787
8788/*
8789 * "matchstrpos()" function
8790 */
8791 static void
8792f_matchstrpos(typval_T *argvars, typval_T *rettv)
8793{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008794 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795}
8796
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008797 static void
8798max_min(typval_T *argvars, typval_T *rettv, int domax)
8799{
8800 varnumber_T n = 0;
8801 varnumber_T i;
8802 int error = FALSE;
8803
8804 if (argvars[0].v_type == VAR_LIST)
8805 {
8806 list_T *l;
8807 listitem_T *li;
8808
8809 l = argvars[0].vval.v_list;
8810 if (l != NULL)
8811 {
8812 li = l->lv_first;
8813 if (li != NULL)
8814 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008815 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008816 for (;;)
8817 {
8818 li = li->li_next;
8819 if (li == NULL)
8820 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008821 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008822 if (domax ? i > n : i < n)
8823 n = i;
8824 }
8825 }
8826 }
8827 }
8828 else if (argvars[0].v_type == VAR_DICT)
8829 {
8830 dict_T *d;
8831 int first = TRUE;
8832 hashitem_T *hi;
8833 int todo;
8834
8835 d = argvars[0].vval.v_dict;
8836 if (d != NULL)
8837 {
8838 todo = (int)d->dv_hashtab.ht_used;
8839 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8840 {
8841 if (!HASHITEM_EMPTY(hi))
8842 {
8843 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008844 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 if (first)
8846 {
8847 n = i;
8848 first = FALSE;
8849 }
8850 else if (domax ? i > n : i < n)
8851 n = i;
8852 }
8853 }
8854 }
8855 }
8856 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008857 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858 rettv->vval.v_number = error ? 0 : n;
8859}
8860
8861/*
8862 * "max()" function
8863 */
8864 static void
8865f_max(typval_T *argvars, typval_T *rettv)
8866{
8867 max_min(argvars, rettv, TRUE);
8868}
8869
8870/*
8871 * "min()" function
8872 */
8873 static void
8874f_min(typval_T *argvars, typval_T *rettv)
8875{
8876 max_min(argvars, rettv, FALSE);
8877}
8878
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008879/*
8880 * Create the directory in which "dir" is located, and higher levels when
8881 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008882 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 */
8884 static int
8885mkdir_recurse(char_u *dir, int prot)
8886{
8887 char_u *p;
8888 char_u *updir;
8889 int r = FAIL;
8890
8891 /* Get end of directory name in "dir".
8892 * We're done when it's "/" or "c:/". */
8893 p = gettail_sep(dir);
8894 if (p <= get_past_head(dir))
8895 return OK;
8896
8897 /* If the directory exists we're done. Otherwise: create it.*/
8898 updir = vim_strnsave(dir, (int)(p - dir));
8899 if (updir == NULL)
8900 return FAIL;
8901 if (mch_isdir(updir))
8902 r = OK;
8903 else if (mkdir_recurse(updir, prot) == OK)
8904 r = vim_mkdir_emsg(updir, prot);
8905 vim_free(updir);
8906 return r;
8907}
8908
8909#ifdef vim_mkdir
8910/*
8911 * "mkdir()" function
8912 */
8913 static void
8914f_mkdir(typval_T *argvars, typval_T *rettv)
8915{
8916 char_u *dir;
8917 char_u buf[NUMBUFLEN];
8918 int prot = 0755;
8919
8920 rettv->vval.v_number = FAIL;
8921 if (check_restricted() || check_secure())
8922 return;
8923
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008924 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008926 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008928 if (*gettail(dir) == NUL)
8929 /* remove trailing slashes */
8930 *gettail_sep(dir) = NUL;
8931
8932 if (argvars[1].v_type != VAR_UNKNOWN)
8933 {
8934 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008936 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008937 if (prot == -1)
8938 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008940 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008941 {
8942 if (mch_isdir(dir))
8943 {
8944 /* With the "p" flag it's OK if the dir already exists. */
8945 rettv->vval.v_number = OK;
8946 return;
8947 }
8948 mkdir_recurse(dir, prot);
8949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008951 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008952}
8953#endif
8954
8955/*
8956 * "mode()" function
8957 */
8958 static void
8959f_mode(typval_T *argvars, typval_T *rettv)
8960{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008961 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962
Bram Moolenaar612cc382018-07-29 15:34:26 +02008963 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964
8965 if (time_for_testing == 93784)
8966 {
8967 /* Testing the two-character code. */
8968 buf[0] = 'x';
8969 buf[1] = '!';
8970 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008971#ifdef FEAT_TERMINAL
8972 else if (term_use_loop())
8973 buf[0] = 't';
8974#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 else if (VIsual_active)
8976 {
8977 if (VIsual_select)
8978 buf[0] = VIsual_mode + 's' - 'v';
8979 else
8980 buf[0] = VIsual_mode;
8981 }
8982 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8983 || State == CONFIRM)
8984 {
8985 buf[0] = 'r';
8986 if (State == ASKMORE)
8987 buf[1] = 'm';
8988 else if (State == CONFIRM)
8989 buf[1] = '?';
8990 }
8991 else if (State == EXTERNCMD)
8992 buf[0] = '!';
8993 else if (State & INSERT)
8994 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008995 if (State & VREPLACE_FLAG)
8996 {
8997 buf[0] = 'R';
8998 buf[1] = 'v';
8999 }
9000 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01009001 {
9002 if (State & REPLACE_FLAG)
9003 buf[0] = 'R';
9004 else
9005 buf[0] = 'i';
9006#ifdef FEAT_INS_EXPAND
9007 if (ins_compl_active())
9008 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01009009 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009010 buf[1] = 'x';
9011#endif
9012 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009014 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 {
9016 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009017 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009019 else if (exmode_active == EXMODE_NORMAL)
9020 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 }
9022 else
9023 {
9024 buf[0] = 'n';
9025 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009026 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009028 // to be able to detect force-linewise/blockwise/characterwise operations
9029 buf[2] = motion_force;
9030 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009031 else if (restart_edit == 'I' || restart_edit == 'R'
9032 || restart_edit == 'V')
9033 {
9034 buf[1] = 'i';
9035 buf[2] = restart_edit;
9036 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009037 }
9038
9039 /* Clear out the minor mode when the argument is not a non-zero number or
9040 * non-empty string. */
9041 if (!non_zero_arg(&argvars[0]))
9042 buf[1] = NUL;
9043
9044 rettv->vval.v_string = vim_strsave(buf);
9045 rettv->v_type = VAR_STRING;
9046}
9047
9048#if defined(FEAT_MZSCHEME) || defined(PROTO)
9049/*
9050 * "mzeval()" function
9051 */
9052 static void
9053f_mzeval(typval_T *argvars, typval_T *rettv)
9054{
9055 char_u *str;
9056 char_u buf[NUMBUFLEN];
9057
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009058 if (check_restricted() || check_secure())
9059 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009060 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061 do_mzeval(str, rettv);
9062}
9063
9064 void
9065mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9066{
9067 typval_T argvars[3];
9068
9069 argvars[0].v_type = VAR_STRING;
9070 argvars[0].vval.v_string = name;
9071 copy_tv(args, &argvars[1]);
9072 argvars[2].v_type = VAR_UNKNOWN;
9073 f_call(argvars, rettv);
9074 clear_tv(&argvars[1]);
9075}
9076#endif
9077
9078/*
9079 * "nextnonblank()" function
9080 */
9081 static void
9082f_nextnonblank(typval_T *argvars, typval_T *rettv)
9083{
9084 linenr_T lnum;
9085
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009086 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009087 {
9088 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9089 {
9090 lnum = 0;
9091 break;
9092 }
9093 if (*skipwhite(ml_get(lnum)) != NUL)
9094 break;
9095 }
9096 rettv->vval.v_number = lnum;
9097}
9098
9099/*
9100 * "nr2char()" function
9101 */
9102 static void
9103f_nr2char(typval_T *argvars, typval_T *rettv)
9104{
9105 char_u buf[NUMBUFLEN];
9106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009107 if (has_mbyte)
9108 {
9109 int utf8 = 0;
9110
9111 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009112 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009114 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009115 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 }
9118 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009120 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009121 buf[1] = NUL;
9122 }
9123 rettv->v_type = VAR_STRING;
9124 rettv->vval.v_string = vim_strsave(buf);
9125}
9126
9127/*
9128 * "or(expr, expr)" function
9129 */
9130 static void
9131f_or(typval_T *argvars, typval_T *rettv)
9132{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009133 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9134 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009135}
9136
9137/*
9138 * "pathshorten()" function
9139 */
9140 static void
9141f_pathshorten(typval_T *argvars, typval_T *rettv)
9142{
9143 char_u *p;
9144
9145 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009146 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 if (p == NULL)
9148 rettv->vval.v_string = NULL;
9149 else
9150 {
9151 p = vim_strsave(p);
9152 rettv->vval.v_string = p;
9153 if (p != NULL)
9154 shorten_dir(p);
9155 }
9156}
9157
9158#ifdef FEAT_PERL
9159/*
9160 * "perleval()" function
9161 */
9162 static void
9163f_perleval(typval_T *argvars, typval_T *rettv)
9164{
9165 char_u *str;
9166 char_u buf[NUMBUFLEN];
9167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009168 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009169 do_perleval(str, rettv);
9170}
9171#endif
9172
9173#ifdef FEAT_FLOAT
9174/*
9175 * "pow()" function
9176 */
9177 static void
9178f_pow(typval_T *argvars, typval_T *rettv)
9179{
9180 float_T fx = 0.0, fy = 0.0;
9181
9182 rettv->v_type = VAR_FLOAT;
9183 if (get_float_arg(argvars, &fx) == OK
9184 && get_float_arg(&argvars[1], &fy) == OK)
9185 rettv->vval.v_float = pow(fx, fy);
9186 else
9187 rettv->vval.v_float = 0.0;
9188}
9189#endif
9190
9191/*
9192 * "prevnonblank()" function
9193 */
9194 static void
9195f_prevnonblank(typval_T *argvars, typval_T *rettv)
9196{
9197 linenr_T lnum;
9198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009199 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009200 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9201 lnum = 0;
9202 else
9203 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9204 --lnum;
9205 rettv->vval.v_number = lnum;
9206}
9207
9208/* This dummy va_list is here because:
9209 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9210 * - locally in the function results in a "used before set" warning
9211 * - using va_start() to initialize it gives "function with fixed args" error */
9212static va_list ap;
9213
9214/*
9215 * "printf()" function
9216 */
9217 static void
9218f_printf(typval_T *argvars, typval_T *rettv)
9219{
9220 char_u buf[NUMBUFLEN];
9221 int len;
9222 char_u *s;
9223 int saved_did_emsg = did_emsg;
9224 char *fmt;
9225
9226 rettv->v_type = VAR_STRING;
9227 rettv->vval.v_string = NULL;
9228
9229 /* Get the required length, allocate the buffer and do it for real. */
9230 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009231 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009232 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233 if (!did_emsg)
9234 {
9235 s = alloc(len + 1);
9236 if (s != NULL)
9237 {
9238 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009239 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9240 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009241 }
9242 }
9243 did_emsg |= saved_did_emsg;
9244}
9245
Bram Moolenaarf2732452018-06-03 14:47:35 +02009246#ifdef FEAT_JOB_CHANNEL
9247/*
9248 * "prompt_setcallback({buffer}, {callback})" function
9249 */
9250 static void
9251f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9252{
9253 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009254 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009255
9256 if (check_secure())
9257 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009258 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009259 if (buf == NULL)
9260 return;
9261
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009262 callback = get_callback(&argvars[1]);
9263 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009264 return;
9265
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009266 free_callback(&buf->b_prompt_callback);
9267 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009268}
9269
9270/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009271 * "prompt_setinterrupt({buffer}, {callback})" function
9272 */
9273 static void
9274f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9275{
9276 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009277 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009278
9279 if (check_secure())
9280 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009281 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009282 if (buf == NULL)
9283 return;
9284
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009285 callback = get_callback(&argvars[1]);
9286 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009287 return;
9288
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009289 free_callback(&buf->b_prompt_interrupt);
9290 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009291}
9292
9293/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009294 * "prompt_setprompt({buffer}, {text})" function
9295 */
9296 static void
9297f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9298{
9299 buf_T *buf;
9300 char_u *text;
9301
9302 if (check_secure())
9303 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009304 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009305 if (buf == NULL)
9306 return;
9307
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009308 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009309 vim_free(buf->b_prompt_text);
9310 buf->b_prompt_text = vim_strsave(text);
9311}
9312#endif
9313
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009314/*
9315 * "pumvisible()" function
9316 */
9317 static void
9318f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9319{
9320#ifdef FEAT_INS_EXPAND
9321 if (pum_visible())
9322 rettv->vval.v_number = 1;
9323#endif
9324}
9325
9326#ifdef FEAT_PYTHON3
9327/*
9328 * "py3eval()" function
9329 */
9330 static void
9331f_py3eval(typval_T *argvars, typval_T *rettv)
9332{
9333 char_u *str;
9334 char_u buf[NUMBUFLEN];
9335
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009336 if (check_restricted() || check_secure())
9337 return;
9338
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009339 if (p_pyx == 0)
9340 p_pyx = 3;
9341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009342 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009343 do_py3eval(str, rettv);
9344}
9345#endif
9346
9347#ifdef FEAT_PYTHON
9348/*
9349 * "pyeval()" function
9350 */
9351 static void
9352f_pyeval(typval_T *argvars, typval_T *rettv)
9353{
9354 char_u *str;
9355 char_u buf[NUMBUFLEN];
9356
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009357 if (check_restricted() || check_secure())
9358 return;
9359
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009360 if (p_pyx == 0)
9361 p_pyx = 2;
9362
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 do_pyeval(str, rettv);
9365}
9366#endif
9367
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009368#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9369/*
9370 * "pyxeval()" function
9371 */
9372 static void
9373f_pyxeval(typval_T *argvars, typval_T *rettv)
9374{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009375 if (check_restricted() || check_secure())
9376 return;
9377
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009378# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9379 init_pyxversion();
9380 if (p_pyx == 2)
9381 f_pyeval(argvars, rettv);
9382 else
9383 f_py3eval(argvars, rettv);
9384# elif defined(FEAT_PYTHON)
9385 f_pyeval(argvars, rettv);
9386# elif defined(FEAT_PYTHON3)
9387 f_py3eval(argvars, rettv);
9388# endif
9389}
9390#endif
9391
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009392/*
9393 * "range()" function
9394 */
9395 static void
9396f_range(typval_T *argvars, typval_T *rettv)
9397{
9398 varnumber_T start;
9399 varnumber_T end;
9400 varnumber_T stride = 1;
9401 varnumber_T i;
9402 int error = FALSE;
9403
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009404 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009405 if (argvars[1].v_type == VAR_UNKNOWN)
9406 {
9407 end = start - 1;
9408 start = 0;
9409 }
9410 else
9411 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009412 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009414 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009415 }
9416
9417 if (error)
9418 return; /* type error; errmsg already given */
9419 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009420 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009421 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009422 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423 else
9424 {
9425 if (rettv_list_alloc(rettv) == OK)
9426 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9427 if (list_append_number(rettv->vval.v_list,
9428 (varnumber_T)i) == FAIL)
9429 break;
9430 }
9431}
9432
9433/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009434 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009435 */
9436 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009437readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009438{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009439 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009440 typval_T save_val;
9441 typval_T rettv;
9442 typval_T argv[2];
9443 int retval = 0;
9444 int error = FALSE;
9445
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009446 if (expr->v_type == VAR_UNKNOWN)
9447 return 1;
9448
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009449 prepare_vimvar(VV_VAL, &save_val);
9450 set_vim_var_string(VV_VAL, name, -1);
9451 argv[0].v_type = VAR_STRING;
9452 argv[0].vval.v_string = name;
9453
9454 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9455 goto theend;
9456
9457 retval = tv_get_number_chk(&rettv, &error);
9458 if (error)
9459 retval = -1;
9460 clear_tv(&rettv);
9461
9462theend:
9463 set_vim_var_string(VV_VAL, NULL, 0);
9464 restore_vimvar(VV_VAL, &save_val);
9465 return retval;
9466}
9467
9468/*
9469 * "readdir()" function
9470 */
9471 static void
9472f_readdir(typval_T *argvars, typval_T *rettv)
9473{
9474 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009475 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009476 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009477 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009478 garray_T ga;
9479 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009480
9481 if (rettv_list_alloc(rettv) == FAIL)
9482 return;
9483 path = tv_get_string(&argvars[0]);
9484 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009485
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009486 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9487 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009488 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009489 for (i = 0; i < ga.ga_len; i++)
9490 {
9491 p = ((char_u **)ga.ga_data)[i];
9492 list_append_string(rettv->vval.v_list, p, -1);
9493 }
9494 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009495 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009496}
9497
9498/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009499 * "readfile()" function
9500 */
9501 static void
9502f_readfile(typval_T *argvars, typval_T *rettv)
9503{
9504 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009505 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009506 int failed = FALSE;
9507 char_u *fname;
9508 FILE *fd;
9509 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9510 int io_size = sizeof(buf);
9511 int readlen; /* size of last fread() */
9512 char_u *prev = NULL; /* previously read bytes, if any */
9513 long prevlen = 0; /* length of data in prev */
9514 long prevsize = 0; /* size of prev buffer */
9515 long maxline = MAXLNUM;
9516 long cnt = 0;
9517 char_u *p; /* position in buf */
9518 char_u *start; /* start of current line */
9519
9520 if (argvars[1].v_type != VAR_UNKNOWN)
9521 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009522 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009523 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009524 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9525 blob = TRUE;
9526
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009527 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009528 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009529 }
9530
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009531 if (blob)
9532 {
9533 if (rettv_blob_alloc(rettv) == FAIL)
9534 return;
9535 }
9536 else
9537 {
9538 if (rettv_list_alloc(rettv) == FAIL)
9539 return;
9540 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009541
9542 /* Always open the file in binary mode, library functions have a mind of
9543 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009544 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009547 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 return;
9549 }
9550
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009551 if (blob)
9552 {
9553 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9554 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009555 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009556 blob_free(rettv->vval.v_blob);
9557 }
9558 fclose(fd);
9559 return;
9560 }
9561
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009562 while (cnt < maxline || maxline < 0)
9563 {
9564 readlen = (int)fread(buf, 1, io_size, fd);
9565
9566 /* This for loop processes what was read, but is also entered at end
9567 * of file so that either:
9568 * - an incomplete line gets written
9569 * - a "binary" file gets an empty line at the end if it ends in a
9570 * newline. */
9571 for (p = buf, start = buf;
9572 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9573 ++p)
9574 {
9575 if (*p == '\n' || readlen <= 0)
9576 {
9577 listitem_T *li;
9578 char_u *s = NULL;
9579 long_u len = p - start;
9580
9581 /* Finished a line. Remove CRs before NL. */
9582 if (readlen > 0 && !binary)
9583 {
9584 while (len > 0 && start[len - 1] == '\r')
9585 --len;
9586 /* removal may cross back to the "prev" string */
9587 if (len == 0)
9588 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9589 --prevlen;
9590 }
9591 if (prevlen == 0)
9592 s = vim_strnsave(start, (int)len);
9593 else
9594 {
9595 /* Change "prev" buffer to be the right size. This way
9596 * the bytes are only copied once, and very long lines are
9597 * allocated only once. */
9598 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9599 {
9600 mch_memmove(s + prevlen, start, len);
9601 s[prevlen + len] = NUL;
9602 prev = NULL; /* the list will own the string */
9603 prevlen = prevsize = 0;
9604 }
9605 }
9606 if (s == NULL)
9607 {
9608 do_outofmem_msg((long_u) prevlen + len + 1);
9609 failed = TRUE;
9610 break;
9611 }
9612
9613 if ((li = listitem_alloc()) == NULL)
9614 {
9615 vim_free(s);
9616 failed = TRUE;
9617 break;
9618 }
9619 li->li_tv.v_type = VAR_STRING;
9620 li->li_tv.v_lock = 0;
9621 li->li_tv.vval.v_string = s;
9622 list_append(rettv->vval.v_list, li);
9623
9624 start = p + 1; /* step over newline */
9625 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9626 break;
9627 }
9628 else if (*p == NUL)
9629 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009630 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9631 * when finding the BF and check the previous two bytes. */
9632 else if (*p == 0xbf && enc_utf8 && !binary)
9633 {
9634 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9635 * + 1, these may be in the "prev" string. */
9636 char_u back1 = p >= buf + 1 ? p[-1]
9637 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9638 char_u back2 = p >= buf + 2 ? p[-2]
9639 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9640 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9641
9642 if (back2 == 0xef && back1 == 0xbb)
9643 {
9644 char_u *dest = p - 2;
9645
9646 /* Usually a BOM is at the beginning of a file, and so at
9647 * the beginning of a line; then we can just step over it.
9648 */
9649 if (start == dest)
9650 start = p + 1;
9651 else
9652 {
9653 /* have to shuffle buf to close gap */
9654 int adjust_prevlen = 0;
9655
9656 if (dest < buf)
9657 {
9658 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9659 dest = buf;
9660 }
9661 if (readlen > p - buf + 1)
9662 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9663 readlen -= 3 - adjust_prevlen;
9664 prevlen -= adjust_prevlen;
9665 p = dest - 1;
9666 }
9667 }
9668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009669 } /* for */
9670
9671 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9672 break;
9673 if (start < p)
9674 {
9675 /* There's part of a line in buf, store it in "prev". */
9676 if (p - start + prevlen >= prevsize)
9677 {
9678 /* need bigger "prev" buffer */
9679 char_u *newprev;
9680
9681 /* A common use case is ordinary text files and "prev" gets a
9682 * fragment of a line, so the first allocation is made
9683 * small, to avoid repeatedly 'allocing' large and
9684 * 'reallocing' small. */
9685 if (prevsize == 0)
9686 prevsize = (long)(p - start);
9687 else
9688 {
9689 long grow50pc = (prevsize * 3) / 2;
9690 long growmin = (long)((p - start) * 2 + prevlen);
9691 prevsize = grow50pc > growmin ? grow50pc : growmin;
9692 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009693 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 if (newprev == NULL)
9695 {
9696 do_outofmem_msg((long_u)prevsize);
9697 failed = TRUE;
9698 break;
9699 }
9700 prev = newprev;
9701 }
9702 /* Add the line part to end of "prev". */
9703 mch_memmove(prev + prevlen, start, p - start);
9704 prevlen += (long)(p - start);
9705 }
9706 } /* while */
9707
9708 /*
9709 * For a negative line count use only the lines at the end of the file,
9710 * free the rest.
9711 */
9712 if (!failed && maxline < 0)
9713 while (cnt > -maxline)
9714 {
9715 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9716 --cnt;
9717 }
9718
9719 if (failed)
9720 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009721 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009722 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009723 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 }
9725
9726 vim_free(prev);
9727 fclose(fd);
9728}
9729
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009730 static void
9731return_register(int regname, typval_T *rettv)
9732{
9733 char_u buf[2] = {0, 0};
9734
9735 buf[0] = (char_u)regname;
9736 rettv->v_type = VAR_STRING;
9737 rettv->vval.v_string = vim_strsave(buf);
9738}
9739
9740/*
9741 * "reg_executing()" function
9742 */
9743 static void
9744f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9745{
9746 return_register(reg_executing, rettv);
9747}
9748
9749/*
9750 * "reg_recording()" function
9751 */
9752 static void
9753f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9754{
9755 return_register(reg_recording, rettv);
9756}
9757
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009758#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009759/*
9760 * Convert a List to proftime_T.
9761 * Return FAIL when there is something wrong.
9762 */
9763 static int
9764list2proftime(typval_T *arg, proftime_T *tm)
9765{
9766 long n1, n2;
9767 int error = FALSE;
9768
9769 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9770 || arg->vval.v_list->lv_len != 2)
9771 return FAIL;
9772 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9773 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009774# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009775 tm->HighPart = n1;
9776 tm->LowPart = n2;
9777# else
9778 tm->tv_sec = n1;
9779 tm->tv_usec = n2;
9780# endif
9781 return error ? FAIL : OK;
9782}
9783#endif /* FEAT_RELTIME */
9784
9785/*
9786 * "reltime()" function
9787 */
9788 static void
9789f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9790{
9791#ifdef FEAT_RELTIME
9792 proftime_T res;
9793 proftime_T start;
9794
9795 if (argvars[0].v_type == VAR_UNKNOWN)
9796 {
9797 /* No arguments: get current time. */
9798 profile_start(&res);
9799 }
9800 else if (argvars[1].v_type == VAR_UNKNOWN)
9801 {
9802 if (list2proftime(&argvars[0], &res) == FAIL)
9803 return;
9804 profile_end(&res);
9805 }
9806 else
9807 {
9808 /* Two arguments: compute the difference. */
9809 if (list2proftime(&argvars[0], &start) == FAIL
9810 || list2proftime(&argvars[1], &res) == FAIL)
9811 return;
9812 profile_sub(&res, &start);
9813 }
9814
9815 if (rettv_list_alloc(rettv) == OK)
9816 {
9817 long n1, n2;
9818
Bram Moolenaar4f974752019-02-17 17:44:42 +01009819# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 n1 = res.HighPart;
9821 n2 = res.LowPart;
9822# else
9823 n1 = res.tv_sec;
9824 n2 = res.tv_usec;
9825# endif
9826 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9827 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9828 }
9829#endif
9830}
9831
9832#ifdef FEAT_FLOAT
9833/*
9834 * "reltimefloat()" function
9835 */
9836 static void
9837f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9838{
9839# ifdef FEAT_RELTIME
9840 proftime_T tm;
9841# endif
9842
9843 rettv->v_type = VAR_FLOAT;
9844 rettv->vval.v_float = 0;
9845# ifdef FEAT_RELTIME
9846 if (list2proftime(&argvars[0], &tm) == OK)
9847 rettv->vval.v_float = profile_float(&tm);
9848# endif
9849}
9850#endif
9851
9852/*
9853 * "reltimestr()" function
9854 */
9855 static void
9856f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9857{
9858#ifdef FEAT_RELTIME
9859 proftime_T tm;
9860#endif
9861
9862 rettv->v_type = VAR_STRING;
9863 rettv->vval.v_string = NULL;
9864#ifdef FEAT_RELTIME
9865 if (list2proftime(&argvars[0], &tm) == OK)
9866 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9867#endif
9868}
9869
9870#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009871 static void
9872make_connection(void)
9873{
9874 if (X_DISPLAY == NULL
9875# ifdef FEAT_GUI
9876 && !gui.in_use
9877# endif
9878 )
9879 {
9880 x_force_connect = TRUE;
9881 setup_term_clip();
9882 x_force_connect = FALSE;
9883 }
9884}
9885
9886 static int
9887check_connection(void)
9888{
9889 make_connection();
9890 if (X_DISPLAY == NULL)
9891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009892 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009893 return FAIL;
9894 }
9895 return OK;
9896}
9897#endif
9898
9899#ifdef FEAT_CLIENTSERVER
9900 static void
9901remote_common(typval_T *argvars, typval_T *rettv, int expr)
9902{
9903 char_u *server_name;
9904 char_u *keys;
9905 char_u *r = NULL;
9906 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009907 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009908# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909 HWND w;
9910# else
9911 Window w;
9912# endif
9913
9914 if (check_restricted() || check_secure())
9915 return;
9916
9917# ifdef FEAT_X11
9918 if (check_connection() == FAIL)
9919 return;
9920# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009921 if (argvars[2].v_type != VAR_UNKNOWN
9922 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009923 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009924
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009925 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 if (server_name == NULL)
9927 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009928 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009929# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009930 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009932 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9933 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934# endif
9935 {
9936 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009938 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009939 vim_free(r);
9940 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009941 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009942 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009943 return;
9944 }
9945
9946 rettv->vval.v_string = r;
9947
9948 if (argvars[2].v_type != VAR_UNKNOWN)
9949 {
9950 dictitem_T v;
9951 char_u str[30];
9952 char_u *idvar;
9953
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009954 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009955 if (idvar != NULL && *idvar != NUL)
9956 {
9957 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9958 v.di_tv.v_type = VAR_STRING;
9959 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961 vim_free(v.di_tv.vval.v_string);
9962 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009963 }
9964}
9965#endif
9966
9967/*
9968 * "remote_expr()" function
9969 */
9970 static void
9971f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9972{
9973 rettv->v_type = VAR_STRING;
9974 rettv->vval.v_string = NULL;
9975#ifdef FEAT_CLIENTSERVER
9976 remote_common(argvars, rettv, TRUE);
9977#endif
9978}
9979
9980/*
9981 * "remote_foreground()" function
9982 */
9983 static void
9984f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9985{
9986#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009987# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009988 /* On Win32 it's done in this application. */
9989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009990 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991
9992 if (server_name != NULL)
9993 serverForeground(server_name);
9994 }
9995# else
9996 /* Send a foreground() expression to the server. */
9997 argvars[1].v_type = VAR_STRING;
9998 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9999 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +020010000 rettv->v_type = VAR_STRING;
10001 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 remote_common(argvars, rettv, TRUE);
10003 vim_free(argvars[1].vval.v_string);
10004# endif
10005#endif
10006}
10007
10008 static void
10009f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10010{
10011#ifdef FEAT_CLIENTSERVER
10012 dictitem_T v;
10013 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010014# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015 long_u n = 0;
10016# endif
10017 char_u *serverid;
10018
10019 if (check_restricted() || check_secure())
10020 {
10021 rettv->vval.v_number = -1;
10022 return;
10023 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010024 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010025 if (serverid == NULL)
10026 {
10027 rettv->vval.v_number = -1;
10028 return; /* type error; errmsg already given */
10029 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010030# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010031 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10032 if (n == 0)
10033 rettv->vval.v_number = -1;
10034 else
10035 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010036 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010037 rettv->vval.v_number = (s != NULL);
10038 }
10039# else
10040 if (check_connection() == FAIL)
10041 return;
10042
10043 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10044 serverStrToWin(serverid), &s);
10045# endif
10046
10047 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10048 {
10049 char_u *retvar;
10050
10051 v.di_tv.v_type = VAR_STRING;
10052 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010053 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010054 if (retvar != NULL)
10055 set_var(retvar, &v.di_tv, FALSE);
10056 vim_free(v.di_tv.vval.v_string);
10057 }
10058#else
10059 rettv->vval.v_number = -1;
10060#endif
10061}
10062
10063 static void
10064f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10065{
10066 char_u *r = NULL;
10067
10068#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010069 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010070
10071 if (serverid != NULL && !check_restricted() && !check_secure())
10072 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010073 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010074# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010075 /* The server's HWND is encoded in the 'id' parameter */
10076 long_u n = 0;
10077# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010078
10079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010080 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010081
Bram Moolenaar4f974752019-02-17 17:44:42 +010010082# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10084 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010085 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 if (r == NULL)
10087# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010088 if (check_connection() == FAIL
10089 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10090 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010091# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010092 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010093 }
10094#endif
10095 rettv->v_type = VAR_STRING;
10096 rettv->vval.v_string = r;
10097}
10098
10099/*
10100 * "remote_send()" function
10101 */
10102 static void
10103f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10104{
10105 rettv->v_type = VAR_STRING;
10106 rettv->vval.v_string = NULL;
10107#ifdef FEAT_CLIENTSERVER
10108 remote_common(argvars, rettv, FALSE);
10109#endif
10110}
10111
10112/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010113 * "remote_startserver()" function
10114 */
10115 static void
10116f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10117{
10118#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010119 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010120
10121 if (server == NULL)
10122 return; /* type error; errmsg already given */
10123 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010124 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010125 else
10126 {
10127# ifdef FEAT_X11
10128 if (check_connection() == OK)
10129 serverRegisterName(X_DISPLAY, server);
10130# else
10131 serverSetName(server);
10132# endif
10133 }
10134#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010135 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010136#endif
10137}
10138
10139/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010140 * "remove()" function
10141 */
10142 static void
10143f_remove(typval_T *argvars, typval_T *rettv)
10144{
10145 list_T *l;
10146 listitem_T *item, *item2;
10147 listitem_T *li;
10148 long idx;
10149 long end;
10150 char_u *key;
10151 dict_T *d;
10152 dictitem_T *di;
10153 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010154 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010155
10156 if (argvars[0].v_type == VAR_DICT)
10157 {
10158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010159 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010161 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010163 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 if (key != NULL)
10165 {
10166 di = dict_find(d, key, -1);
10167 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010168 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10170 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10171 {
10172 *rettv = di->di_tv;
10173 init_tv(&di->di_tv);
10174 dictitem_remove(d, di);
10175 }
10176 }
10177 }
10178 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010179 else if (argvars[0].v_type == VAR_BLOB)
10180 {
10181 idx = (long)tv_get_number_chk(&argvars[1], &error);
10182 if (!error)
10183 {
10184 blob_T *b = argvars[0].vval.v_blob;
10185 int len = blob_len(b);
10186 char_u *p;
10187
10188 if (idx < 0)
10189 // count from the end
10190 idx = len + idx;
10191 if (idx < 0 || idx >= len)
10192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010193 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010194 return;
10195 }
10196 if (argvars[2].v_type == VAR_UNKNOWN)
10197 {
10198 // Remove one item, return its value.
10199 p = (char_u *)b->bv_ga.ga_data;
10200 rettv->vval.v_number = (varnumber_T) *(p + idx);
10201 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10202 --b->bv_ga.ga_len;
10203 }
10204 else
10205 {
10206 blob_T *blob;
10207
10208 // Remove range of items, return list with values.
10209 end = (long)tv_get_number_chk(&argvars[2], &error);
10210 if (error)
10211 return;
10212 if (end < 0)
10213 // count from the end
10214 end = len + end;
10215 if (end >= len || idx > end)
10216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010217 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010218 return;
10219 }
10220 blob = blob_alloc();
10221 if (blob == NULL)
10222 return;
10223 blob->bv_ga.ga_len = end - idx + 1;
10224 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10225 {
10226 vim_free(blob);
10227 return;
10228 }
10229 p = (char_u *)b->bv_ga.ga_data;
10230 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10231 (size_t)(end - idx + 1));
10232 ++blob->bv_refcount;
10233 rettv->v_type = VAR_BLOB;
10234 rettv->vval.v_blob = blob;
10235
10236 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10237 b->bv_ga.ga_len -= end - idx + 1;
10238 }
10239 }
10240 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010241 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010242 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010243 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010244 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010246 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010248 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010249 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010250 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010251 else
10252 {
10253 if (argvars[2].v_type == VAR_UNKNOWN)
10254 {
10255 /* Remove one item, return its value. */
10256 vimlist_remove(l, item, item);
10257 *rettv = item->li_tv;
10258 vim_free(item);
10259 }
10260 else
10261 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010262 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010263 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010265 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010266 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010267 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010268 else
10269 {
10270 int cnt = 0;
10271
10272 for (li = item; li != NULL; li = li->li_next)
10273 {
10274 ++cnt;
10275 if (li == item2)
10276 break;
10277 }
10278 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010279 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010280 else
10281 {
10282 vimlist_remove(l, item, item2);
10283 if (rettv_list_alloc(rettv) == OK)
10284 {
10285 l = rettv->vval.v_list;
10286 l->lv_first = item;
10287 l->lv_last = item2;
10288 item->li_prev = NULL;
10289 item2->li_next = NULL;
10290 l->lv_len = cnt;
10291 }
10292 }
10293 }
10294 }
10295 }
10296 }
10297}
10298
10299/*
10300 * "rename({from}, {to})" function
10301 */
10302 static void
10303f_rename(typval_T *argvars, typval_T *rettv)
10304{
10305 char_u buf[NUMBUFLEN];
10306
10307 if (check_restricted() || check_secure())
10308 rettv->vval.v_number = -1;
10309 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010310 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10311 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010312}
10313
10314/*
10315 * "repeat()" function
10316 */
10317 static void
10318f_repeat(typval_T *argvars, typval_T *rettv)
10319{
10320 char_u *p;
10321 int n;
10322 int slen;
10323 int len;
10324 char_u *r;
10325 int i;
10326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010327 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010328 if (argvars[0].v_type == VAR_LIST)
10329 {
10330 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10331 while (n-- > 0)
10332 if (list_extend(rettv->vval.v_list,
10333 argvars[0].vval.v_list, NULL) == FAIL)
10334 break;
10335 }
10336 else
10337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010338 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010339 rettv->v_type = VAR_STRING;
10340 rettv->vval.v_string = NULL;
10341
10342 slen = (int)STRLEN(p);
10343 len = slen * n;
10344 if (len <= 0)
10345 return;
10346
10347 r = alloc(len + 1);
10348 if (r != NULL)
10349 {
10350 for (i = 0; i < n; i++)
10351 mch_memmove(r + i * slen, p, (size_t)slen);
10352 r[len] = NUL;
10353 }
10354
10355 rettv->vval.v_string = r;
10356 }
10357}
10358
10359/*
10360 * "resolve()" function
10361 */
10362 static void
10363f_resolve(typval_T *argvars, typval_T *rettv)
10364{
10365 char_u *p;
10366#ifdef HAVE_READLINK
10367 char_u *buf = NULL;
10368#endif
10369
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010370 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010371#ifdef FEAT_SHORTCUT
10372 {
10373 char_u *v = NULL;
10374
Bram Moolenaardce1e892019-02-10 23:18:53 +010010375 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010376 if (v != NULL)
10377 rettv->vval.v_string = v;
10378 else
10379 rettv->vval.v_string = vim_strsave(p);
10380 }
10381#else
10382# ifdef HAVE_READLINK
10383 {
10384 char_u *cpy;
10385 int len;
10386 char_u *remain = NULL;
10387 char_u *q;
10388 int is_relative_to_current = FALSE;
10389 int has_trailing_pathsep = FALSE;
10390 int limit = 100;
10391
10392 p = vim_strsave(p);
10393
10394 if (p[0] == '.' && (vim_ispathsep(p[1])
10395 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10396 is_relative_to_current = TRUE;
10397
10398 len = STRLEN(p);
10399 if (len > 0 && after_pathsep(p, p + len))
10400 {
10401 has_trailing_pathsep = TRUE;
10402 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10403 }
10404
10405 q = getnextcomp(p);
10406 if (*q != NUL)
10407 {
10408 /* Separate the first path component in "p", and keep the
10409 * remainder (beginning with the path separator). */
10410 remain = vim_strsave(q - 1);
10411 q[-1] = NUL;
10412 }
10413
10414 buf = alloc(MAXPATHL + 1);
10415 if (buf == NULL)
10416 goto fail;
10417
10418 for (;;)
10419 {
10420 for (;;)
10421 {
10422 len = readlink((char *)p, (char *)buf, MAXPATHL);
10423 if (len <= 0)
10424 break;
10425 buf[len] = NUL;
10426
10427 if (limit-- == 0)
10428 {
10429 vim_free(p);
10430 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010431 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010432 rettv->vval.v_string = NULL;
10433 goto fail;
10434 }
10435
10436 /* Ensure that the result will have a trailing path separator
10437 * if the argument has one. */
10438 if (remain == NULL && has_trailing_pathsep)
10439 add_pathsep(buf);
10440
10441 /* Separate the first path component in the link value and
10442 * concatenate the remainders. */
10443 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10444 if (*q != NUL)
10445 {
10446 if (remain == NULL)
10447 remain = vim_strsave(q - 1);
10448 else
10449 {
10450 cpy = concat_str(q - 1, remain);
10451 if (cpy != NULL)
10452 {
10453 vim_free(remain);
10454 remain = cpy;
10455 }
10456 }
10457 q[-1] = NUL;
10458 }
10459
10460 q = gettail(p);
10461 if (q > p && *q == NUL)
10462 {
10463 /* Ignore trailing path separator. */
10464 q[-1] = NUL;
10465 q = gettail(p);
10466 }
10467 if (q > p && !mch_isFullName(buf))
10468 {
10469 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010470 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010471 if (cpy != NULL)
10472 {
10473 STRCPY(cpy, p);
10474 STRCPY(gettail(cpy), buf);
10475 vim_free(p);
10476 p = cpy;
10477 }
10478 }
10479 else
10480 {
10481 vim_free(p);
10482 p = vim_strsave(buf);
10483 }
10484 }
10485
10486 if (remain == NULL)
10487 break;
10488
10489 /* Append the first path component of "remain" to "p". */
10490 q = getnextcomp(remain + 1);
10491 len = q - remain - (*q != NUL);
10492 cpy = vim_strnsave(p, STRLEN(p) + len);
10493 if (cpy != NULL)
10494 {
10495 STRNCAT(cpy, remain, len);
10496 vim_free(p);
10497 p = cpy;
10498 }
10499 /* Shorten "remain". */
10500 if (*q != NUL)
10501 STRMOVE(remain, q - 1);
10502 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010503 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010504 }
10505
10506 /* If the result is a relative path name, make it explicitly relative to
10507 * the current directory if and only if the argument had this form. */
10508 if (!vim_ispathsep(*p))
10509 {
10510 if (is_relative_to_current
10511 && *p != NUL
10512 && !(p[0] == '.'
10513 && (p[1] == NUL
10514 || vim_ispathsep(p[1])
10515 || (p[1] == '.'
10516 && (p[2] == NUL
10517 || vim_ispathsep(p[2]))))))
10518 {
10519 /* Prepend "./". */
10520 cpy = concat_str((char_u *)"./", p);
10521 if (cpy != NULL)
10522 {
10523 vim_free(p);
10524 p = cpy;
10525 }
10526 }
10527 else if (!is_relative_to_current)
10528 {
10529 /* Strip leading "./". */
10530 q = p;
10531 while (q[0] == '.' && vim_ispathsep(q[1]))
10532 q += 2;
10533 if (q > p)
10534 STRMOVE(p, p + 2);
10535 }
10536 }
10537
10538 /* Ensure that the result will have no trailing path separator
10539 * if the argument had none. But keep "/" or "//". */
10540 if (!has_trailing_pathsep)
10541 {
10542 q = p + STRLEN(p);
10543 if (after_pathsep(p, q))
10544 *gettail_sep(p) = NUL;
10545 }
10546
10547 rettv->vval.v_string = p;
10548 }
10549# else
10550 rettv->vval.v_string = vim_strsave(p);
10551# endif
10552#endif
10553
10554 simplify_filename(rettv->vval.v_string);
10555
10556#ifdef HAVE_READLINK
10557fail:
10558 vim_free(buf);
10559#endif
10560 rettv->v_type = VAR_STRING;
10561}
10562
10563/*
10564 * "reverse({list})" function
10565 */
10566 static void
10567f_reverse(typval_T *argvars, typval_T *rettv)
10568{
10569 list_T *l;
10570 listitem_T *li, *ni;
10571
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010572 if (argvars[0].v_type == VAR_BLOB)
10573 {
10574 blob_T *b = argvars[0].vval.v_blob;
10575 int i, len = blob_len(b);
10576
10577 for (i = 0; i < len / 2; i++)
10578 {
10579 int tmp = blob_get(b, i);
10580
10581 blob_set(b, i, blob_get(b, len - i - 1));
10582 blob_set(b, len - i - 1, tmp);
10583 }
10584 rettv_blob_set(rettv, b);
10585 return;
10586 }
10587
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010588 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010589 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010590 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010591 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010592 (char_u *)N_("reverse() argument"), TRUE))
10593 {
10594 li = l->lv_last;
10595 l->lv_first = l->lv_last = NULL;
10596 l->lv_len = 0;
10597 while (li != NULL)
10598 {
10599 ni = li->li_prev;
10600 list_append(l, li);
10601 li = ni;
10602 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010603 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010604 l->lv_idx = l->lv_len - l->lv_idx - 1;
10605 }
10606}
10607
10608#define SP_NOMOVE 0x01 /* don't move cursor */
10609#define SP_REPEAT 0x02 /* repeat to find outer pair */
10610#define SP_RETCOUNT 0x04 /* return matchcount */
10611#define SP_SETPCMARK 0x08 /* set previous context mark */
10612#define SP_START 0x10 /* accept match at start position */
10613#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10614#define SP_END 0x40 /* leave cursor at end of match */
10615#define SP_COLUMN 0x80 /* start at cursor column */
10616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617/*
10618 * Get flags for a search function.
10619 * Possibly sets "p_ws".
10620 * Returns BACKWARD, FORWARD or zero (for an error).
10621 */
10622 static int
10623get_search_arg(typval_T *varp, int *flagsp)
10624{
10625 int dir = FORWARD;
10626 char_u *flags;
10627 char_u nbuf[NUMBUFLEN];
10628 int mask;
10629
10630 if (varp->v_type != VAR_UNKNOWN)
10631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 if (flags == NULL)
10634 return 0; /* type error; errmsg already given */
10635 while (*flags != NUL)
10636 {
10637 switch (*flags)
10638 {
10639 case 'b': dir = BACKWARD; break;
10640 case 'w': p_ws = TRUE; break;
10641 case 'W': p_ws = FALSE; break;
10642 default: mask = 0;
10643 if (flagsp != NULL)
10644 switch (*flags)
10645 {
10646 case 'c': mask = SP_START; break;
10647 case 'e': mask = SP_END; break;
10648 case 'm': mask = SP_RETCOUNT; break;
10649 case 'n': mask = SP_NOMOVE; break;
10650 case 'p': mask = SP_SUBPAT; break;
10651 case 'r': mask = SP_REPEAT; break;
10652 case 's': mask = SP_SETPCMARK; break;
10653 case 'z': mask = SP_COLUMN; break;
10654 }
10655 if (mask == 0)
10656 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010657 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010658 dir = 0;
10659 }
10660 else
10661 *flagsp |= mask;
10662 }
10663 if (dir == 0)
10664 break;
10665 ++flags;
10666 }
10667 }
10668 return dir;
10669}
10670
10671/*
10672 * Shared by search() and searchpos() functions.
10673 */
10674 static int
10675search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10676{
10677 int flags;
10678 char_u *pat;
10679 pos_T pos;
10680 pos_T save_cursor;
10681 int save_p_ws = p_ws;
10682 int dir;
10683 int retval = 0; /* default: FAIL */
10684 long lnum_stop = 0;
10685 proftime_T tm;
10686#ifdef FEAT_RELTIME
10687 long time_limit = 0;
10688#endif
10689 int options = SEARCH_KEEP;
10690 int subpatnum;
10691
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010692 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010693 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10694 if (dir == 0)
10695 goto theend;
10696 flags = *flagsp;
10697 if (flags & SP_START)
10698 options |= SEARCH_START;
10699 if (flags & SP_END)
10700 options |= SEARCH_END;
10701 if (flags & SP_COLUMN)
10702 options |= SEARCH_COL;
10703
10704 /* Optional arguments: line number to stop searching and timeout. */
10705 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010707 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010708 if (lnum_stop < 0)
10709 goto theend;
10710#ifdef FEAT_RELTIME
10711 if (argvars[3].v_type != VAR_UNKNOWN)
10712 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010713 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010714 if (time_limit < 0)
10715 goto theend;
10716 }
10717#endif
10718 }
10719
10720#ifdef FEAT_RELTIME
10721 /* Set the time limit, if there is one. */
10722 profile_setlimit(time_limit, &tm);
10723#endif
10724
10725 /*
10726 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10727 * Check to make sure only those flags are set.
10728 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10729 * flags cannot be set. Check for that condition also.
10730 */
10731 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10732 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010734 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010735 goto theend;
10736 }
10737
10738 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010739 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010740 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010741 if (subpatnum != FAIL)
10742 {
10743 if (flags & SP_SUBPAT)
10744 retval = subpatnum;
10745 else
10746 retval = pos.lnum;
10747 if (flags & SP_SETPCMARK)
10748 setpcmark();
10749 curwin->w_cursor = pos;
10750 if (match_pos != NULL)
10751 {
10752 /* Store the match cursor position */
10753 match_pos->lnum = pos.lnum;
10754 match_pos->col = pos.col + 1;
10755 }
10756 /* "/$" will put the cursor after the end of the line, may need to
10757 * correct that here */
10758 check_cursor();
10759 }
10760
10761 /* If 'n' flag is used: restore cursor position. */
10762 if (flags & SP_NOMOVE)
10763 curwin->w_cursor = save_cursor;
10764 else
10765 curwin->w_set_curswant = TRUE;
10766theend:
10767 p_ws = save_p_ws;
10768
10769 return retval;
10770}
10771
10772#ifdef FEAT_FLOAT
10773
10774/*
10775 * round() is not in C90, use ceil() or floor() instead.
10776 */
10777 float_T
10778vim_round(float_T f)
10779{
10780 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10781}
10782
10783/*
10784 * "round({float})" function
10785 */
10786 static void
10787f_round(typval_T *argvars, typval_T *rettv)
10788{
10789 float_T f = 0.0;
10790
10791 rettv->v_type = VAR_FLOAT;
10792 if (get_float_arg(argvars, &f) == OK)
10793 rettv->vval.v_float = vim_round(f);
10794 else
10795 rettv->vval.v_float = 0.0;
10796}
10797#endif
10798
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010799#ifdef FEAT_RUBY
10800/*
10801 * "rubyeval()" function
10802 */
10803 static void
10804f_rubyeval(typval_T *argvars, typval_T *rettv)
10805{
10806 char_u *str;
10807 char_u buf[NUMBUFLEN];
10808
10809 str = tv_get_string_buf(&argvars[0], buf);
10810 do_rubyeval(str, rettv);
10811}
10812#endif
10813
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010814/*
10815 * "screenattr()" function
10816 */
10817 static void
10818f_screenattr(typval_T *argvars, typval_T *rettv)
10819{
10820 int row;
10821 int col;
10822 int c;
10823
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010824 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10825 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010826 if (row < 0 || row >= screen_Rows
10827 || col < 0 || col >= screen_Columns)
10828 c = -1;
10829 else
10830 c = ScreenAttrs[LineOffset[row] + col];
10831 rettv->vval.v_number = c;
10832}
10833
10834/*
10835 * "screenchar()" function
10836 */
10837 static void
10838f_screenchar(typval_T *argvars, typval_T *rettv)
10839{
10840 int row;
10841 int col;
10842 int off;
10843 int c;
10844
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010845 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10846 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010847 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010848 c = -1;
10849 else
10850 {
10851 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010852 if (enc_utf8 && ScreenLinesUC[off] != 0)
10853 c = ScreenLinesUC[off];
10854 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010855 c = ScreenLines[off];
10856 }
10857 rettv->vval.v_number = c;
10858}
10859
10860/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010861 * "screenchars()" function
10862 */
10863 static void
10864f_screenchars(typval_T *argvars, typval_T *rettv)
10865{
10866 int row;
10867 int col;
10868 int off;
10869 int c;
10870 int i;
10871
10872 if (rettv_list_alloc(rettv) == FAIL)
10873 return;
10874 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10875 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10876 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10877 return;
10878
10879 off = LineOffset[row] + col;
10880 if (enc_utf8 && ScreenLinesUC[off] != 0)
10881 c = ScreenLinesUC[off];
10882 else
10883 c = ScreenLines[off];
10884 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10885
10886 if (enc_utf8)
10887
10888 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10889 list_append_number(rettv->vval.v_list,
10890 (varnumber_T)ScreenLinesC[i][off]);
10891}
10892
10893/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894 * "screencol()" function
10895 *
10896 * First column is 1 to be consistent with virtcol().
10897 */
10898 static void
10899f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10900{
10901 rettv->vval.v_number = screen_screencol() + 1;
10902}
10903
10904/*
10905 * "screenrow()" function
10906 */
10907 static void
10908f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10909{
10910 rettv->vval.v_number = screen_screenrow() + 1;
10911}
10912
10913/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010914 * "screenstring()" function
10915 */
10916 static void
10917f_screenstring(typval_T *argvars, typval_T *rettv)
10918{
10919 int row;
10920 int col;
10921 int off;
10922 int c;
10923 int i;
10924 char_u buf[MB_MAXBYTES + 1];
10925 int buflen = 0;
10926
10927 rettv->vval.v_string = NULL;
10928 rettv->v_type = VAR_STRING;
10929
10930 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10931 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10932 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10933 return;
10934
10935 off = LineOffset[row] + col;
10936 if (enc_utf8 && ScreenLinesUC[off] != 0)
10937 c = ScreenLinesUC[off];
10938 else
10939 c = ScreenLines[off];
10940 buflen += mb_char2bytes(c, buf);
10941
10942 if (enc_utf8)
10943 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10944 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10945
10946 buf[buflen] = NUL;
10947 rettv->vval.v_string = vim_strsave(buf);
10948}
10949
10950/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010951 * "search()" function
10952 */
10953 static void
10954f_search(typval_T *argvars, typval_T *rettv)
10955{
10956 int flags = 0;
10957
10958 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10959}
10960
10961/*
10962 * "searchdecl()" function
10963 */
10964 static void
10965f_searchdecl(typval_T *argvars, typval_T *rettv)
10966{
10967 int locally = 1;
10968 int thisblock = 0;
10969 int error = FALSE;
10970 char_u *name;
10971
10972 rettv->vval.v_number = 1; /* default: FAIL */
10973
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 if (argvars[1].v_type != VAR_UNKNOWN)
10976 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010977 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 }
10981 if (!error && name != NULL)
10982 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10983 locally, thisblock, SEARCH_KEEP) == FAIL;
10984}
10985
10986/*
10987 * Used by searchpair() and searchpairpos()
10988 */
10989 static int
10990searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10991{
10992 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010993 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994 int save_p_ws = p_ws;
10995 int dir;
10996 int flags = 0;
10997 char_u nbuf1[NUMBUFLEN];
10998 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 int retval = 0; /* default: FAIL */
11000 long lnum_stop = 0;
11001 long time_limit = 0;
11002
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011003 /* Get the three pattern arguments: start, middle, end. Will result in an
11004 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011005 spat = tv_get_string_chk(&argvars[0]);
11006 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11007 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 if (spat == NULL || mpat == NULL || epat == NULL)
11009 goto theend; /* type error */
11010
11011 /* Handle the optional fourth argument: flags */
11012 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11013 if (dir == 0)
11014 goto theend;
11015
11016 /* Don't accept SP_END or SP_SUBPAT.
11017 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11018 */
11019 if ((flags & (SP_END | SP_SUBPAT)) != 0
11020 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011022 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011023 goto theend;
11024 }
11025
11026 /* Using 'r' implies 'W', otherwise it doesn't work. */
11027 if (flags & SP_REPEAT)
11028 p_ws = FALSE;
11029
11030 /* Optional fifth argument: skip expression */
11031 if (argvars[3].v_type == VAR_UNKNOWN
11032 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011033 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011034 else
11035 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011036 skip = &argvars[4];
11037 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11038 && skip->v_type != VAR_STRING)
11039 {
11040 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011041 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011042 goto theend;
11043 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 if (argvars[5].v_type != VAR_UNKNOWN)
11045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011046 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011049 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011050 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011051 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011052#ifdef FEAT_RELTIME
11053 if (argvars[6].v_type != VAR_UNKNOWN)
11054 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011055 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011058 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011059 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011060 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 }
11062#endif
11063 }
11064 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011065
11066 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11067 match_pos, lnum_stop, time_limit);
11068
11069theend:
11070 p_ws = save_p_ws;
11071
11072 return retval;
11073}
11074
11075/*
11076 * "searchpair()" function
11077 */
11078 static void
11079f_searchpair(typval_T *argvars, typval_T *rettv)
11080{
11081 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11082}
11083
11084/*
11085 * "searchpairpos()" function
11086 */
11087 static void
11088f_searchpairpos(typval_T *argvars, typval_T *rettv)
11089{
11090 pos_T match_pos;
11091 int lnum = 0;
11092 int col = 0;
11093
11094 if (rettv_list_alloc(rettv) == FAIL)
11095 return;
11096
11097 if (searchpair_cmn(argvars, &match_pos) > 0)
11098 {
11099 lnum = match_pos.lnum;
11100 col = match_pos.col;
11101 }
11102
11103 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11104 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11105}
11106
11107/*
11108 * Search for a start/middle/end thing.
11109 * Used by searchpair(), see its documentation for the details.
11110 * Returns 0 or -1 for no match,
11111 */
11112 long
11113do_searchpair(
11114 char_u *spat, /* start pattern */
11115 char_u *mpat, /* middle pattern */
11116 char_u *epat, /* end pattern */
11117 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011118 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011119 int flags, /* SP_SETPCMARK and other SP_ values */
11120 pos_T *match_pos,
11121 linenr_T lnum_stop, /* stop at this line if not zero */
11122 long time_limit UNUSED) /* stop after this many msec */
11123{
11124 char_u *save_cpo;
11125 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11126 long retval = 0;
11127 pos_T pos;
11128 pos_T firstpos;
11129 pos_T foundpos;
11130 pos_T save_cursor;
11131 pos_T save_pos;
11132 int n;
11133 int r;
11134 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011135 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 int err;
11137 int options = SEARCH_KEEP;
11138 proftime_T tm;
11139
11140 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11141 save_cpo = p_cpo;
11142 p_cpo = empty_option;
11143
11144#ifdef FEAT_RELTIME
11145 /* Set the time limit, if there is one. */
11146 profile_setlimit(time_limit, &tm);
11147#endif
11148
11149 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11150 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011151 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11152 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 if (pat2 == NULL || pat3 == NULL)
11154 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011155 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011156 if (*mpat == NUL)
11157 STRCPY(pat3, pat2);
11158 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011159 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011160 spat, epat, mpat);
11161 if (flags & SP_START)
11162 options |= SEARCH_START;
11163
Bram Moolenaar48570482017-10-30 21:48:41 +010011164 if (skip != NULL)
11165 {
11166 /* Empty string means to not use the skip expression. */
11167 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11168 use_skip = skip->vval.v_string != NULL
11169 && *skip->vval.v_string != NUL;
11170 }
11171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011172 save_cursor = curwin->w_cursor;
11173 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011174 CLEAR_POS(&firstpos);
11175 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011176 pat = pat3;
11177 for (;;)
11178 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011179 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011180 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011181 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011182 /* didn't find it or found the first match again: FAIL */
11183 break;
11184
11185 if (firstpos.lnum == 0)
11186 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011187 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011188 {
11189 /* Found the same position again. Can happen with a pattern that
11190 * has "\zs" at the end and searching backwards. Advance one
11191 * character and try again. */
11192 if (dir == BACKWARD)
11193 decl(&pos);
11194 else
11195 incl(&pos);
11196 }
11197 foundpos = pos;
11198
11199 /* clear the start flag to avoid getting stuck here */
11200 options &= ~SEARCH_START;
11201
11202 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011203 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011204 {
11205 save_pos = curwin->w_cursor;
11206 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011207 err = FALSE;
11208 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011209 curwin->w_cursor = save_pos;
11210 if (err)
11211 {
11212 /* Evaluating {skip} caused an error, break here. */
11213 curwin->w_cursor = save_cursor;
11214 retval = -1;
11215 break;
11216 }
11217 if (r)
11218 continue;
11219 }
11220
11221 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11222 {
11223 /* Found end when searching backwards or start when searching
11224 * forward: nested pair. */
11225 ++nest;
11226 pat = pat2; /* nested, don't search for middle */
11227 }
11228 else
11229 {
11230 /* Found end when searching forward or start when searching
11231 * backward: end of (nested) pair; or found middle in outer pair. */
11232 if (--nest == 1)
11233 pat = pat3; /* outer level, search for middle */
11234 }
11235
11236 if (nest == 0)
11237 {
11238 /* Found the match: return matchcount or line number. */
11239 if (flags & SP_RETCOUNT)
11240 ++retval;
11241 else
11242 retval = pos.lnum;
11243 if (flags & SP_SETPCMARK)
11244 setpcmark();
11245 curwin->w_cursor = pos;
11246 if (!(flags & SP_REPEAT))
11247 break;
11248 nest = 1; /* search for next unmatched */
11249 }
11250 }
11251
11252 if (match_pos != NULL)
11253 {
11254 /* Store the match cursor position */
11255 match_pos->lnum = curwin->w_cursor.lnum;
11256 match_pos->col = curwin->w_cursor.col + 1;
11257 }
11258
11259 /* If 'n' flag is used or search failed: restore cursor position. */
11260 if ((flags & SP_NOMOVE) || retval == 0)
11261 curwin->w_cursor = save_cursor;
11262
11263theend:
11264 vim_free(pat2);
11265 vim_free(pat3);
11266 if (p_cpo == empty_option)
11267 p_cpo = save_cpo;
11268 else
11269 /* Darn, evaluating the {skip} expression changed the value. */
11270 free_string_option(save_cpo);
11271
11272 return retval;
11273}
11274
11275/*
11276 * "searchpos()" function
11277 */
11278 static void
11279f_searchpos(typval_T *argvars, typval_T *rettv)
11280{
11281 pos_T match_pos;
11282 int lnum = 0;
11283 int col = 0;
11284 int n;
11285 int flags = 0;
11286
11287 if (rettv_list_alloc(rettv) == FAIL)
11288 return;
11289
11290 n = search_cmn(argvars, &match_pos, &flags);
11291 if (n > 0)
11292 {
11293 lnum = match_pos.lnum;
11294 col = match_pos.col;
11295 }
11296
11297 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11298 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11299 if (flags & SP_SUBPAT)
11300 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11301}
11302
11303 static void
11304f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11305{
11306#ifdef FEAT_CLIENTSERVER
11307 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011308 char_u *server = tv_get_string_chk(&argvars[0]);
11309 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011310
11311 rettv->vval.v_number = -1;
11312 if (server == NULL || reply == NULL)
11313 return;
11314 if (check_restricted() || check_secure())
11315 return;
11316# ifdef FEAT_X11
11317 if (check_connection() == FAIL)
11318 return;
11319# endif
11320
11321 if (serverSendReply(server, reply) < 0)
11322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011323 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011324 return;
11325 }
11326 rettv->vval.v_number = 0;
11327#else
11328 rettv->vval.v_number = -1;
11329#endif
11330}
11331
11332 static void
11333f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11334{
11335 char_u *r = NULL;
11336
11337#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011338# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011339 r = serverGetVimNames();
11340# else
11341 make_connection();
11342 if (X_DISPLAY != NULL)
11343 r = serverGetVimNames(X_DISPLAY);
11344# endif
11345#endif
11346 rettv->v_type = VAR_STRING;
11347 rettv->vval.v_string = r;
11348}
11349
11350/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011351 * "setbufline()" function
11352 */
11353 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011354f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011355{
11356 linenr_T lnum;
11357 buf_T *buf;
11358
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011359 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011360 if (buf == NULL)
11361 rettv->vval.v_number = 1; /* FAIL */
11362 else
11363 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011364 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011365 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011366 }
11367}
11368
11369/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011370 * "setbufvar()" function
11371 */
11372 static void
11373f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11374{
11375 buf_T *buf;
11376 char_u *varname, *bufvarname;
11377 typval_T *varp;
11378 char_u nbuf[NUMBUFLEN];
11379
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011380 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011381 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011382 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11383 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011384 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 varp = &argvars[2];
11386
11387 if (buf != NULL && varname != NULL && varp != NULL)
11388 {
11389 if (*varname == '&')
11390 {
11391 long numval;
11392 char_u *strval;
11393 int error = FALSE;
11394 aco_save_T aco;
11395
11396 /* set curbuf to be our buf, temporarily */
11397 aucmd_prepbuf(&aco, buf);
11398
11399 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011400 numval = (long)tv_get_number_chk(varp, &error);
11401 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402 if (!error && strval != NULL)
11403 set_option_value(varname, numval, strval, OPT_LOCAL);
11404
11405 /* reset notion of buffer */
11406 aucmd_restbuf(&aco);
11407 }
11408 else
11409 {
11410 buf_T *save_curbuf = curbuf;
11411
Bram Moolenaar964b3742019-05-24 18:54:09 +020011412 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011413 if (bufvarname != NULL)
11414 {
11415 curbuf = buf;
11416 STRCPY(bufvarname, "b:");
11417 STRCPY(bufvarname + 2, varname);
11418 set_var(bufvarname, varp, TRUE);
11419 vim_free(bufvarname);
11420 curbuf = save_curbuf;
11421 }
11422 }
11423 }
11424}
11425
11426 static void
11427f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11428{
11429 dict_T *d;
11430 dictitem_T *di;
11431 char_u *csearch;
11432
11433 if (argvars[0].v_type != VAR_DICT)
11434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011435 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436 return;
11437 }
11438
11439 if ((d = argvars[0].vval.v_dict) != NULL)
11440 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011441 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011442 if (csearch != NULL)
11443 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 if (enc_utf8)
11445 {
11446 int pcc[MAX_MCO];
11447 int c = utfc_ptr2char(csearch, pcc);
11448
11449 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11450 }
11451 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011452 set_last_csearch(PTR2CHAR(csearch),
11453 csearch, MB_PTR2LEN(csearch));
11454 }
11455
11456 di = dict_find(d, (char_u *)"forward", -1);
11457 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011458 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011459 ? FORWARD : BACKWARD);
11460
11461 di = dict_find(d, (char_u *)"until", -1);
11462 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011463 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011464 }
11465}
11466
11467/*
11468 * "setcmdpos()" function
11469 */
11470 static void
11471f_setcmdpos(typval_T *argvars, typval_T *rettv)
11472{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011473 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011474
11475 if (pos >= 0)
11476 rettv->vval.v_number = set_cmdline_pos(pos);
11477}
11478
11479/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011480 * "setenv()" function
11481 */
11482 static void
11483f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11484{
11485 char_u namebuf[NUMBUFLEN];
11486 char_u valbuf[NUMBUFLEN];
11487 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11488
11489 if (argvars[1].v_type == VAR_SPECIAL
11490 && argvars[1].vval.v_number == VVAL_NULL)
11491 vim_unsetenv(name);
11492 else
11493 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11494}
11495
11496/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497 * "setfperm({fname}, {mode})" function
11498 */
11499 static void
11500f_setfperm(typval_T *argvars, typval_T *rettv)
11501{
11502 char_u *fname;
11503 char_u modebuf[NUMBUFLEN];
11504 char_u *mode_str;
11505 int i;
11506 int mask;
11507 int mode = 0;
11508
11509 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011510 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011511 if (fname == NULL)
11512 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011513 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011514 if (mode_str == NULL)
11515 return;
11516 if (STRLEN(mode_str) != 9)
11517 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011518 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011519 return;
11520 }
11521
11522 mask = 1;
11523 for (i = 8; i >= 0; --i)
11524 {
11525 if (mode_str[i] != '-')
11526 mode |= mask;
11527 mask = mask << 1;
11528 }
11529 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11530}
11531
11532/*
11533 * "setline()" function
11534 */
11535 static void
11536f_setline(typval_T *argvars, typval_T *rettv)
11537{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011538 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011539
Bram Moolenaarca851592018-06-06 21:04:07 +020011540 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541}
11542
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011543/*
11544 * Used by "setqflist()" and "setloclist()" functions
11545 */
11546 static void
11547set_qf_ll_list(
11548 win_T *wp UNUSED,
11549 typval_T *list_arg UNUSED,
11550 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011551 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011552 typval_T *rettv)
11553{
11554#ifdef FEAT_QUICKFIX
11555 static char *e_invact = N_("E927: Invalid action: '%s'");
11556 char_u *act;
11557 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011558 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011559#endif
11560
11561 rettv->vval.v_number = -1;
11562
11563#ifdef FEAT_QUICKFIX
11564 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011565 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011566 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011567 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 else
11569 {
11570 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011571 dict_T *d = NULL;
11572 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573
11574 if (action_arg->v_type == VAR_STRING)
11575 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011576 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011577 if (act == NULL)
11578 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011579 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11580 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 action = *act;
11582 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011583 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 }
11585 else if (action_arg->v_type == VAR_UNKNOWN)
11586 action = ' ';
11587 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011588 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011589
Bram Moolenaard823fa92016-08-12 16:29:27 +020011590 if (action_arg->v_type != VAR_UNKNOWN
11591 && what_arg->v_type != VAR_UNKNOWN)
11592 {
11593 if (what_arg->v_type == VAR_DICT)
11594 d = what_arg->vval.v_dict;
11595 else
11596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011597 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011598 valid_dict = FALSE;
11599 }
11600 }
11601
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011602 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011603 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011604 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11605 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011606 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011607 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011608 }
11609#endif
11610}
11611
11612/*
11613 * "setloclist()" function
11614 */
11615 static void
11616f_setloclist(typval_T *argvars, typval_T *rettv)
11617{
11618 win_T *win;
11619
11620 rettv->vval.v_number = -1;
11621
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011622 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011623 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011624 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011625}
11626
11627/*
11628 * "setmatches()" function
11629 */
11630 static void
11631f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11632{
11633#ifdef FEAT_SEARCH_EXTRA
11634 list_T *l;
11635 listitem_T *li;
11636 dict_T *d;
11637 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011638 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011639
11640 rettv->vval.v_number = -1;
11641 if (argvars[0].v_type != VAR_LIST)
11642 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011643 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011644 return;
11645 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011646 if (win == NULL)
11647 return;
11648
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011649 if ((l = argvars[0].vval.v_list) != NULL)
11650 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011651 /* To some extent make sure that we are dealing with a list from
11652 * "getmatches()". */
11653 li = l->lv_first;
11654 while (li != NULL)
11655 {
11656 if (li->li_tv.v_type != VAR_DICT
11657 || (d = li->li_tv.vval.v_dict) == NULL)
11658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011659 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011660 return;
11661 }
11662 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11663 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11664 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11665 && dict_find(d, (char_u *)"priority", -1) != NULL
11666 && dict_find(d, (char_u *)"id", -1) != NULL))
11667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011668 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011669 return;
11670 }
11671 li = li->li_next;
11672 }
11673
Bram Moolenaaraff74912019-03-30 18:11:49 +010011674 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011675 li = l->lv_first;
11676 while (li != NULL)
11677 {
11678 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011679 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011680 dictitem_T *di;
11681 char_u *group;
11682 int priority;
11683 int id;
11684 char_u *conceal;
11685
11686 d = li->li_tv.vval.v_dict;
11687 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11688 {
11689 if (s == NULL)
11690 {
11691 s = list_alloc();
11692 if (s == NULL)
11693 return;
11694 }
11695
11696 /* match from matchaddpos() */
11697 for (i = 1; i < 9; i++)
11698 {
11699 sprintf((char *)buf, (char *)"pos%d", i);
11700 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11701 {
11702 if (di->di_tv.v_type != VAR_LIST)
11703 return;
11704
11705 list_append_tv(s, &di->di_tv);
11706 s->lv_refcount++;
11707 }
11708 else
11709 break;
11710 }
11711 }
11712
Bram Moolenaar8f667172018-12-14 15:38:31 +010011713 group = dict_get_string(d, (char_u *)"group", TRUE);
11714 priority = (int)dict_get_number(d, (char_u *)"priority");
11715 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011716 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011717 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011718 : NULL;
11719 if (i == 0)
11720 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011721 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011722 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011723 priority, id, NULL, conceal);
11724 }
11725 else
11726 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011727 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011728 list_unref(s);
11729 s = NULL;
11730 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011731 vim_free(group);
11732 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011733
11734 li = li->li_next;
11735 }
11736 rettv->vval.v_number = 0;
11737 }
11738#endif
11739}
11740
11741/*
11742 * "setpos()" function
11743 */
11744 static void
11745f_setpos(typval_T *argvars, typval_T *rettv)
11746{
11747 pos_T pos;
11748 int fnum;
11749 char_u *name;
11750 colnr_T curswant = -1;
11751
11752 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011753 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011754 if (name != NULL)
11755 {
11756 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11757 {
11758 if (--pos.col < 0)
11759 pos.col = 0;
11760 if (name[0] == '.' && name[1] == NUL)
11761 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011762 /* set cursor; "fnum" is ignored */
11763 curwin->w_cursor = pos;
11764 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011765 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011766 curwin->w_curswant = curswant - 1;
11767 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011768 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011769 check_cursor();
11770 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011771 }
11772 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11773 {
11774 /* set mark */
11775 if (setmark_pos(name[1], &pos, fnum) == OK)
11776 rettv->vval.v_number = 0;
11777 }
11778 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011779 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011780 }
11781 }
11782}
11783
11784/*
11785 * "setqflist()" function
11786 */
11787 static void
11788f_setqflist(typval_T *argvars, typval_T *rettv)
11789{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011790 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011791}
11792
11793/*
11794 * "setreg()" function
11795 */
11796 static void
11797f_setreg(typval_T *argvars, typval_T *rettv)
11798{
11799 int regname;
11800 char_u *strregname;
11801 char_u *stropt;
11802 char_u *strval;
11803 int append;
11804 char_u yank_type;
11805 long block_len;
11806
11807 block_len = -1;
11808 yank_type = MAUTO;
11809 append = FALSE;
11810
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011811 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011812 rettv->vval.v_number = 1; /* FAIL is default */
11813
11814 if (strregname == NULL)
11815 return; /* type error; errmsg already given */
11816 regname = *strregname;
11817 if (regname == 0 || regname == '@')
11818 regname = '"';
11819
11820 if (argvars[2].v_type != VAR_UNKNOWN)
11821 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011822 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011823 if (stropt == NULL)
11824 return; /* type error */
11825 for (; *stropt != NUL; ++stropt)
11826 switch (*stropt)
11827 {
11828 case 'a': case 'A': /* append */
11829 append = TRUE;
11830 break;
11831 case 'v': case 'c': /* character-wise selection */
11832 yank_type = MCHAR;
11833 break;
11834 case 'V': case 'l': /* line-wise selection */
11835 yank_type = MLINE;
11836 break;
11837 case 'b': case Ctrl_V: /* block-wise selection */
11838 yank_type = MBLOCK;
11839 if (VIM_ISDIGIT(stropt[1]))
11840 {
11841 ++stropt;
11842 block_len = getdigits(&stropt) - 1;
11843 --stropt;
11844 }
11845 break;
11846 }
11847 }
11848
11849 if (argvars[1].v_type == VAR_LIST)
11850 {
11851 char_u **lstval;
11852 char_u **allocval;
11853 char_u buf[NUMBUFLEN];
11854 char_u **curval;
11855 char_u **curallocval;
11856 list_T *ll = argvars[1].vval.v_list;
11857 listitem_T *li;
11858 int len;
11859
11860 /* If the list is NULL handle like an empty list. */
11861 len = ll == NULL ? 0 : ll->lv_len;
11862
11863 /* First half: use for pointers to result lines; second half: use for
11864 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011865 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866 if (lstval == NULL)
11867 return;
11868 curval = lstval;
11869 allocval = lstval + len + 2;
11870 curallocval = allocval;
11871
11872 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11873 li = li->li_next)
11874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011875 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011876 if (strval == NULL)
11877 goto free_lstval;
11878 if (strval == buf)
11879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011880 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011881 * overwrite the string. */
11882 strval = vim_strsave(buf);
11883 if (strval == NULL)
11884 goto free_lstval;
11885 *curallocval++ = strval;
11886 }
11887 *curval++ = strval;
11888 }
11889 *curval++ = NULL;
11890
11891 write_reg_contents_lst(regname, lstval, -1,
11892 append, yank_type, block_len);
11893free_lstval:
11894 while (curallocval > allocval)
11895 vim_free(*--curallocval);
11896 vim_free(lstval);
11897 }
11898 else
11899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011900 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011901 if (strval == NULL)
11902 return;
11903 write_reg_contents_ex(regname, strval, -1,
11904 append, yank_type, block_len);
11905 }
11906 rettv->vval.v_number = 0;
11907}
11908
11909/*
11910 * "settabvar()" function
11911 */
11912 static void
11913f_settabvar(typval_T *argvars, typval_T *rettv)
11914{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011915 tabpage_T *save_curtab;
11916 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011917 char_u *varname, *tabvarname;
11918 typval_T *varp;
11919
11920 rettv->vval.v_number = 0;
11921
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011922 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011923 return;
11924
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011925 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11926 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011927 varp = &argvars[2];
11928
Bram Moolenaar4033c552017-09-16 20:54:51 +020011929 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011931 save_curtab = curtab;
11932 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011933
Bram Moolenaar964b3742019-05-24 18:54:09 +020011934 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011935 if (tabvarname != NULL)
11936 {
11937 STRCPY(tabvarname, "t:");
11938 STRCPY(tabvarname + 2, varname);
11939 set_var(tabvarname, varp, TRUE);
11940 vim_free(tabvarname);
11941 }
11942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011943 /* Restore current tabpage */
11944 if (valid_tabpage(save_curtab))
11945 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011946 }
11947}
11948
11949/*
11950 * "settabwinvar()" function
11951 */
11952 static void
11953f_settabwinvar(typval_T *argvars, typval_T *rettv)
11954{
11955 setwinvar(argvars, rettv, 1);
11956}
11957
11958/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011959 * "settagstack()" function
11960 */
11961 static void
11962f_settagstack(typval_T *argvars, typval_T *rettv)
11963{
11964 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11965 win_T *wp;
11966 dict_T *d;
11967 int action = 'r';
11968
11969 rettv->vval.v_number = -1;
11970
11971 // first argument: window number or id
11972 wp = find_win_by_nr_or_id(&argvars[0]);
11973 if (wp == NULL)
11974 return;
11975
11976 // second argument: dict with items to set in the tag stack
11977 if (argvars[1].v_type != VAR_DICT)
11978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011979 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011980 return;
11981 }
11982 d = argvars[1].vval.v_dict;
11983 if (d == NULL)
11984 return;
11985
11986 // third argument: action - 'a' for append and 'r' for replace.
11987 // default is to replace the stack.
11988 if (argvars[2].v_type == VAR_UNKNOWN)
11989 action = 'r';
11990 else if (argvars[2].v_type == VAR_STRING)
11991 {
11992 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011993 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011994 if (actstr == NULL)
11995 return;
11996 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11997 action = *actstr;
11998 else
11999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012000 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012001 return;
12002 }
12003 }
12004 else
12005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012006 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012007 return;
12008 }
12009
12010 if (set_tagstack(wp, d, action) == OK)
12011 rettv->vval.v_number = 0;
12012}
12013
12014/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012015 * "setwinvar()" function
12016 */
12017 static void
12018f_setwinvar(typval_T *argvars, typval_T *rettv)
12019{
12020 setwinvar(argvars, rettv, 0);
12021}
12022
12023#ifdef FEAT_CRYPT
12024/*
12025 * "sha256({string})" function
12026 */
12027 static void
12028f_sha256(typval_T *argvars, typval_T *rettv)
12029{
12030 char_u *p;
12031
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012032 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012033 rettv->vval.v_string = vim_strsave(
12034 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12035 rettv->v_type = VAR_STRING;
12036}
12037#endif /* FEAT_CRYPT */
12038
12039/*
12040 * "shellescape({string})" function
12041 */
12042 static void
12043f_shellescape(typval_T *argvars, typval_T *rettv)
12044{
Bram Moolenaar20615522017-06-05 18:46:26 +020012045 int do_special = non_zero_arg(&argvars[1]);
12046
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012047 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012048 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012049 rettv->v_type = VAR_STRING;
12050}
12051
12052/*
12053 * shiftwidth() function
12054 */
12055 static void
12056f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12057{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012058 rettv->vval.v_number = 0;
12059
12060 if (argvars[0].v_type != VAR_UNKNOWN)
12061 {
12062 long col;
12063
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012064 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012065 if (col < 0)
12066 return; // type error; errmsg already given
12067#ifdef FEAT_VARTABS
12068 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12069 return;
12070#endif
12071 }
12072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012073 rettv->vval.v_number = get_sw_value(curbuf);
12074}
12075
Bram Moolenaar162b7142018-12-21 15:17:36 +010012076#ifdef FEAT_SIGNS
12077/*
12078 * "sign_define()" function
12079 */
12080 static void
12081f_sign_define(typval_T *argvars, typval_T *rettv)
12082{
12083 char_u *name;
12084 dict_T *dict;
12085 char_u *icon = NULL;
12086 char_u *linehl = NULL;
12087 char_u *text = NULL;
12088 char_u *texthl = NULL;
12089
12090 rettv->vval.v_number = -1;
12091
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012092 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012093 if (name == NULL)
12094 return;
12095
12096 if (argvars[1].v_type != VAR_UNKNOWN)
12097 {
12098 if (argvars[1].v_type != VAR_DICT)
12099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012100 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012101 return;
12102 }
12103
12104 // sign attributes
12105 dict = argvars[1].vval.v_dict;
12106 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12107 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12108 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12109 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12110 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12111 text = dict_get_string(dict, (char_u *)"text", TRUE);
12112 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12113 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12114 }
12115
12116 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12117 rettv->vval.v_number = 0;
12118
12119 vim_free(icon);
12120 vim_free(linehl);
12121 vim_free(text);
12122 vim_free(texthl);
12123}
12124
12125/*
12126 * "sign_getdefined()" function
12127 */
12128 static void
12129f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12130{
12131 char_u *name = NULL;
12132
12133 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12134 return;
12135
12136 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012137 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012138
12139 sign_getlist(name, rettv->vval.v_list);
12140}
12141
12142/*
12143 * "sign_getplaced()" function
12144 */
12145 static void
12146f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12147{
12148 buf_T *buf = NULL;
12149 dict_T *dict;
12150 dictitem_T *di;
12151 linenr_T lnum = 0;
12152 int sign_id = 0;
12153 char_u *group = NULL;
12154 int notanum = FALSE;
12155
12156 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12157 return;
12158
12159 if (argvars[0].v_type != VAR_UNKNOWN)
12160 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012161 // get signs placed in the specified buffer
12162 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012163 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012164 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012165
12166 if (argvars[1].v_type != VAR_UNKNOWN)
12167 {
12168 if (argvars[1].v_type != VAR_DICT ||
12169 ((dict = argvars[1].vval.v_dict) == NULL))
12170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012171 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012172 return;
12173 }
12174 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12175 {
12176 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012178 if (notanum)
12179 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012180 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012181 }
12182 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12183 {
12184 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012185 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012186 if (notanum)
12187 return;
12188 }
12189 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12190 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012191 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012192 if (group == NULL)
12193 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012194 if (*group == '\0') // empty string means global group
12195 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012196 }
12197 }
12198 }
12199
12200 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12201}
12202
12203/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012204 * "sign_jump()" function
12205 */
12206 static void
12207f_sign_jump(typval_T *argvars, typval_T *rettv)
12208{
12209 int sign_id;
12210 char_u *sign_group = NULL;
12211 buf_T *buf;
12212 int notanum = FALSE;
12213
12214 rettv->vval.v_number = -1;
12215
Bram Moolenaarbdace832019-03-02 10:13:42 +010012216 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012217 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12218 if (notanum)
12219 return;
12220 if (sign_id <= 0)
12221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012222 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012223 return;
12224 }
12225
12226 // Sign group
12227 sign_group = tv_get_string_chk(&argvars[1]);
12228 if (sign_group == NULL)
12229 return;
12230 if (sign_group[0] == '\0')
12231 sign_group = NULL; // global sign group
12232 else
12233 {
12234 sign_group = vim_strsave(sign_group);
12235 if (sign_group == NULL)
12236 return;
12237 }
12238
12239 // Buffer to place the sign
12240 buf = get_buf_arg(&argvars[2]);
12241 if (buf == NULL)
12242 goto cleanup;
12243
12244 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12245
12246cleanup:
12247 vim_free(sign_group);
12248}
12249
12250/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012251 * "sign_place()" function
12252 */
12253 static void
12254f_sign_place(typval_T *argvars, typval_T *rettv)
12255{
12256 int sign_id;
12257 char_u *group = NULL;
12258 char_u *sign_name;
12259 buf_T *buf;
12260 dict_T *dict;
12261 dictitem_T *di;
12262 linenr_T lnum = 0;
12263 int prio = SIGN_DEF_PRIO;
12264 int notanum = FALSE;
12265
12266 rettv->vval.v_number = -1;
12267
Bram Moolenaarbdace832019-03-02 10:13:42 +010012268 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012269 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012270 if (notanum)
12271 return;
12272 if (sign_id < 0)
12273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012274 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012275 return;
12276 }
12277
12278 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012279 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012280 if (group == NULL)
12281 return;
12282 if (group[0] == '\0')
12283 group = NULL; // global sign group
12284 else
12285 {
12286 group = vim_strsave(group);
12287 if (group == NULL)
12288 return;
12289 }
12290
12291 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012292 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012293 if (sign_name == NULL)
12294 goto cleanup;
12295
12296 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012297 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012298 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012299 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012300
12301 if (argvars[4].v_type != VAR_UNKNOWN)
12302 {
12303 if (argvars[4].v_type != VAR_DICT ||
12304 ((dict = argvars[4].vval.v_dict) == NULL))
12305 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012306 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012307 goto cleanup;
12308 }
12309
12310 // Line number where the sign is to be placed
12311 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012313 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012314 if (notanum)
12315 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012316 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012317 }
12318 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12319 {
12320 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012321 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012322 if (notanum)
12323 goto cleanup;
12324 }
12325 }
12326
12327 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12328 rettv->vval.v_number = sign_id;
12329
12330cleanup:
12331 vim_free(group);
12332}
12333
12334/*
12335 * "sign_undefine()" function
12336 */
12337 static void
12338f_sign_undefine(typval_T *argvars, typval_T *rettv)
12339{
12340 char_u *name;
12341
12342 rettv->vval.v_number = -1;
12343
12344 if (argvars[0].v_type == VAR_UNKNOWN)
12345 {
12346 // Free all the signs
12347 free_signs();
12348 rettv->vval.v_number = 0;
12349 }
12350 else
12351 {
12352 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012353 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012354 if (name == NULL)
12355 return;
12356
12357 if (sign_undefine_by_name(name) == OK)
12358 rettv->vval.v_number = 0;
12359 }
12360}
12361
12362/*
12363 * "sign_unplace()" function
12364 */
12365 static void
12366f_sign_unplace(typval_T *argvars, typval_T *rettv)
12367{
12368 dict_T *dict;
12369 dictitem_T *di;
12370 int sign_id = 0;
12371 buf_T *buf = NULL;
12372 char_u *group = NULL;
12373
12374 rettv->vval.v_number = -1;
12375
12376 if (argvars[0].v_type != VAR_STRING)
12377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012378 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012379 return;
12380 }
12381
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012382 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012383 if (group[0] == '\0')
12384 group = NULL; // global sign group
12385 else
12386 {
12387 group = vim_strsave(group);
12388 if (group == NULL)
12389 return;
12390 }
12391
12392 if (argvars[1].v_type != VAR_UNKNOWN)
12393 {
12394 if (argvars[1].v_type != VAR_DICT)
12395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012396 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012397 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012398 }
12399 dict = argvars[1].vval.v_dict;
12400
12401 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12402 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012403 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012404 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012405 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012406 }
12407 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12408 sign_id = dict_get_number(dict, (char_u *)"id");
12409 }
12410
12411 if (buf == NULL)
12412 {
12413 // Delete the sign in all the buffers
12414 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012415 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012416 rettv->vval.v_number = 0;
12417 }
12418 else
12419 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012420 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012421 rettv->vval.v_number = 0;
12422 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012423
12424cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012425 vim_free(group);
12426}
12427#endif
12428
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012429/*
12430 * "simplify()" function
12431 */
12432 static void
12433f_simplify(typval_T *argvars, typval_T *rettv)
12434{
12435 char_u *p;
12436
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012437 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012438 rettv->vval.v_string = vim_strsave(p);
12439 simplify_filename(rettv->vval.v_string); /* simplify in place */
12440 rettv->v_type = VAR_STRING;
12441}
12442
12443#ifdef FEAT_FLOAT
12444/*
12445 * "sin()" function
12446 */
12447 static void
12448f_sin(typval_T *argvars, typval_T *rettv)
12449{
12450 float_T f = 0.0;
12451
12452 rettv->v_type = VAR_FLOAT;
12453 if (get_float_arg(argvars, &f) == OK)
12454 rettv->vval.v_float = sin(f);
12455 else
12456 rettv->vval.v_float = 0.0;
12457}
12458
12459/*
12460 * "sinh()" function
12461 */
12462 static void
12463f_sinh(typval_T *argvars, typval_T *rettv)
12464{
12465 float_T f = 0.0;
12466
12467 rettv->v_type = VAR_FLOAT;
12468 if (get_float_arg(argvars, &f) == OK)
12469 rettv->vval.v_float = sinh(f);
12470 else
12471 rettv->vval.v_float = 0.0;
12472}
12473#endif
12474
Bram Moolenaareae1b912019-05-09 15:12:55 +020012475static int item_compare(const void *s1, const void *s2);
12476static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012477
12478/* struct used in the array that's given to qsort() */
12479typedef struct
12480{
12481 listitem_T *item;
12482 int idx;
12483} sortItem_T;
12484
12485/* struct storing information about current sort */
12486typedef struct
12487{
12488 int item_compare_ic;
12489 int item_compare_numeric;
12490 int item_compare_numbers;
12491#ifdef FEAT_FLOAT
12492 int item_compare_float;
12493#endif
12494 char_u *item_compare_func;
12495 partial_T *item_compare_partial;
12496 dict_T *item_compare_selfdict;
12497 int item_compare_func_err;
12498 int item_compare_keep_zero;
12499} sortinfo_T;
12500static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012501#define ITEM_COMPARE_FAIL 999
12502
12503/*
12504 * Compare functions for f_sort() and f_uniq() below.
12505 */
12506 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012507item_compare(const void *s1, const void *s2)
12508{
12509 sortItem_T *si1, *si2;
12510 typval_T *tv1, *tv2;
12511 char_u *p1, *p2;
12512 char_u *tofree1 = NULL, *tofree2 = NULL;
12513 int res;
12514 char_u numbuf1[NUMBUFLEN];
12515 char_u numbuf2[NUMBUFLEN];
12516
12517 si1 = (sortItem_T *)s1;
12518 si2 = (sortItem_T *)s2;
12519 tv1 = &si1->item->li_tv;
12520 tv2 = &si2->item->li_tv;
12521
12522 if (sortinfo->item_compare_numbers)
12523 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012524 varnumber_T v1 = tv_get_number(tv1);
12525 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012526
12527 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12528 }
12529
12530#ifdef FEAT_FLOAT
12531 if (sortinfo->item_compare_float)
12532 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012533 float_T v1 = tv_get_float(tv1);
12534 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012535
12536 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12537 }
12538#endif
12539
12540 /* tv2string() puts quotes around a string and allocates memory. Don't do
12541 * that for string variables. Use a single quote when comparing with a
12542 * non-string to do what the docs promise. */
12543 if (tv1->v_type == VAR_STRING)
12544 {
12545 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12546 p1 = (char_u *)"'";
12547 else
12548 p1 = tv1->vval.v_string;
12549 }
12550 else
12551 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12552 if (tv2->v_type == VAR_STRING)
12553 {
12554 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12555 p2 = (char_u *)"'";
12556 else
12557 p2 = tv2->vval.v_string;
12558 }
12559 else
12560 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12561 if (p1 == NULL)
12562 p1 = (char_u *)"";
12563 if (p2 == NULL)
12564 p2 = (char_u *)"";
12565 if (!sortinfo->item_compare_numeric)
12566 {
12567 if (sortinfo->item_compare_ic)
12568 res = STRICMP(p1, p2);
12569 else
12570 res = STRCMP(p1, p2);
12571 }
12572 else
12573 {
12574 double n1, n2;
12575 n1 = strtod((char *)p1, (char **)&p1);
12576 n2 = strtod((char *)p2, (char **)&p2);
12577 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12578 }
12579
12580 /* When the result would be zero, compare the item indexes. Makes the
12581 * sort stable. */
12582 if (res == 0 && !sortinfo->item_compare_keep_zero)
12583 res = si1->idx > si2->idx ? 1 : -1;
12584
12585 vim_free(tofree1);
12586 vim_free(tofree2);
12587 return res;
12588}
12589
12590 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012591item_compare2(const void *s1, const void *s2)
12592{
12593 sortItem_T *si1, *si2;
12594 int res;
12595 typval_T rettv;
12596 typval_T argv[3];
12597 int dummy;
12598 char_u *func_name;
12599 partial_T *partial = sortinfo->item_compare_partial;
12600
12601 /* shortcut after failure in previous call; compare all items equal */
12602 if (sortinfo->item_compare_func_err)
12603 return 0;
12604
12605 si1 = (sortItem_T *)s1;
12606 si2 = (sortItem_T *)s2;
12607
12608 if (partial == NULL)
12609 func_name = sortinfo->item_compare_func;
12610 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012611 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012612
12613 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12614 * in the copy without changing the original list items. */
12615 copy_tv(&si1->item->li_tv, &argv[0]);
12616 copy_tv(&si2->item->li_tv, &argv[1]);
12617
12618 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012619 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 partial, sortinfo->item_compare_selfdict);
12621 clear_tv(&argv[0]);
12622 clear_tv(&argv[1]);
12623
12624 if (res == FAIL)
12625 res = ITEM_COMPARE_FAIL;
12626 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012627 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 if (sortinfo->item_compare_func_err)
12629 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12630 clear_tv(&rettv);
12631
12632 /* When the result would be zero, compare the pointers themselves. Makes
12633 * the sort stable. */
12634 if (res == 0 && !sortinfo->item_compare_keep_zero)
12635 res = si1->idx > si2->idx ? 1 : -1;
12636
12637 return res;
12638}
12639
12640/*
12641 * "sort({list})" function
12642 */
12643 static void
12644do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12645{
12646 list_T *l;
12647 listitem_T *li;
12648 sortItem_T *ptrs;
12649 sortinfo_T *old_sortinfo;
12650 sortinfo_T info;
12651 long len;
12652 long i;
12653
12654 /* Pointer to current info struct used in compare function. Save and
12655 * restore the current one for nested calls. */
12656 old_sortinfo = sortinfo;
12657 sortinfo = &info;
12658
12659 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012660 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 else
12662 {
12663 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012664 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012665 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12666 TRUE))
12667 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012668 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012669
12670 len = list_len(l);
12671 if (len <= 1)
12672 goto theend; /* short list sorts pretty quickly */
12673
12674 info.item_compare_ic = FALSE;
12675 info.item_compare_numeric = FALSE;
12676 info.item_compare_numbers = FALSE;
12677#ifdef FEAT_FLOAT
12678 info.item_compare_float = FALSE;
12679#endif
12680 info.item_compare_func = NULL;
12681 info.item_compare_partial = NULL;
12682 info.item_compare_selfdict = NULL;
12683 if (argvars[1].v_type != VAR_UNKNOWN)
12684 {
12685 /* optional second argument: {func} */
12686 if (argvars[1].v_type == VAR_FUNC)
12687 info.item_compare_func = argvars[1].vval.v_string;
12688 else if (argvars[1].v_type == VAR_PARTIAL)
12689 info.item_compare_partial = argvars[1].vval.v_partial;
12690 else
12691 {
12692 int error = FALSE;
12693
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012694 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012695 if (error)
12696 goto theend; /* type error; errmsg already given */
12697 if (i == 1)
12698 info.item_compare_ic = TRUE;
12699 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012700 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 else if (i != 0)
12702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012703 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704 goto theend;
12705 }
12706 if (info.item_compare_func != NULL)
12707 {
12708 if (*info.item_compare_func == NUL)
12709 {
12710 /* empty string means default sort */
12711 info.item_compare_func = NULL;
12712 }
12713 else if (STRCMP(info.item_compare_func, "n") == 0)
12714 {
12715 info.item_compare_func = NULL;
12716 info.item_compare_numeric = TRUE;
12717 }
12718 else if (STRCMP(info.item_compare_func, "N") == 0)
12719 {
12720 info.item_compare_func = NULL;
12721 info.item_compare_numbers = TRUE;
12722 }
12723#ifdef FEAT_FLOAT
12724 else if (STRCMP(info.item_compare_func, "f") == 0)
12725 {
12726 info.item_compare_func = NULL;
12727 info.item_compare_float = TRUE;
12728 }
12729#endif
12730 else if (STRCMP(info.item_compare_func, "i") == 0)
12731 {
12732 info.item_compare_func = NULL;
12733 info.item_compare_ic = TRUE;
12734 }
12735 }
12736 }
12737
12738 if (argvars[2].v_type != VAR_UNKNOWN)
12739 {
12740 /* optional third argument: {dict} */
12741 if (argvars[2].v_type != VAR_DICT)
12742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012743 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012744 goto theend;
12745 }
12746 info.item_compare_selfdict = argvars[2].vval.v_dict;
12747 }
12748 }
12749
12750 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012751 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012752 if (ptrs == NULL)
12753 goto theend;
12754
12755 i = 0;
12756 if (sort)
12757 {
12758 /* sort(): ptrs will be the list to sort */
12759 for (li = l->lv_first; li != NULL; li = li->li_next)
12760 {
12761 ptrs[i].item = li;
12762 ptrs[i].idx = i;
12763 ++i;
12764 }
12765
12766 info.item_compare_func_err = FALSE;
12767 info.item_compare_keep_zero = FALSE;
12768 /* test the compare function */
12769 if ((info.item_compare_func != NULL
12770 || info.item_compare_partial != NULL)
12771 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12772 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012773 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012774 else
12775 {
12776 /* Sort the array with item pointers. */
12777 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12778 info.item_compare_func == NULL
12779 && info.item_compare_partial == NULL
12780 ? item_compare : item_compare2);
12781
12782 if (!info.item_compare_func_err)
12783 {
12784 /* Clear the List and append the items in sorted order. */
12785 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12786 l->lv_len = 0;
12787 for (i = 0; i < len; ++i)
12788 list_append(l, ptrs[i].item);
12789 }
12790 }
12791 }
12792 else
12793 {
12794 int (*item_compare_func_ptr)(const void *, const void *);
12795
12796 /* f_uniq(): ptrs will be a stack of items to remove */
12797 info.item_compare_func_err = FALSE;
12798 info.item_compare_keep_zero = TRUE;
12799 item_compare_func_ptr = info.item_compare_func != NULL
12800 || info.item_compare_partial != NULL
12801 ? item_compare2 : item_compare;
12802
12803 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12804 li = li->li_next)
12805 {
12806 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12807 == 0)
12808 ptrs[i++].item = li;
12809 if (info.item_compare_func_err)
12810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012811 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012812 break;
12813 }
12814 }
12815
12816 if (!info.item_compare_func_err)
12817 {
12818 while (--i >= 0)
12819 {
12820 li = ptrs[i].item->li_next;
12821 ptrs[i].item->li_next = li->li_next;
12822 if (li->li_next != NULL)
12823 li->li_next->li_prev = ptrs[i].item;
12824 else
12825 l->lv_last = ptrs[i].item;
12826 list_fix_watch(l, li);
12827 listitem_free(li);
12828 l->lv_len--;
12829 }
12830 }
12831 }
12832
12833 vim_free(ptrs);
12834 }
12835theend:
12836 sortinfo = old_sortinfo;
12837}
12838
12839/*
12840 * "sort({list})" function
12841 */
12842 static void
12843f_sort(typval_T *argvars, typval_T *rettv)
12844{
12845 do_sort_uniq(argvars, rettv, TRUE);
12846}
12847
12848/*
12849 * "uniq({list})" function
12850 */
12851 static void
12852f_uniq(typval_T *argvars, typval_T *rettv)
12853{
12854 do_sort_uniq(argvars, rettv, FALSE);
12855}
12856
12857/*
12858 * "soundfold({word})" function
12859 */
12860 static void
12861f_soundfold(typval_T *argvars, typval_T *rettv)
12862{
12863 char_u *s;
12864
12865 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012866 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012867#ifdef FEAT_SPELL
12868 rettv->vval.v_string = eval_soundfold(s);
12869#else
12870 rettv->vval.v_string = vim_strsave(s);
12871#endif
12872}
12873
12874/*
12875 * "spellbadword()" function
12876 */
12877 static void
12878f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12879{
12880 char_u *word = (char_u *)"";
12881 hlf_T attr = HLF_COUNT;
12882 int len = 0;
12883
12884 if (rettv_list_alloc(rettv) == FAIL)
12885 return;
12886
12887#ifdef FEAT_SPELL
12888 if (argvars[0].v_type == VAR_UNKNOWN)
12889 {
12890 /* Find the start and length of the badly spelled word. */
12891 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12892 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012893 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012895 curwin->w_set_curswant = TRUE;
12896 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012897 }
12898 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12899 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012900 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012901 int capcol = -1;
12902
12903 if (str != NULL)
12904 {
12905 /* Check the argument for spelling. */
12906 while (*str != NUL)
12907 {
12908 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12909 if (attr != HLF_COUNT)
12910 {
12911 word = str;
12912 break;
12913 }
12914 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012915 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 }
12917 }
12918 }
12919#endif
12920
12921 list_append_string(rettv->vval.v_list, word, len);
12922 list_append_string(rettv->vval.v_list, (char_u *)(
12923 attr == HLF_SPB ? "bad" :
12924 attr == HLF_SPR ? "rare" :
12925 attr == HLF_SPL ? "local" :
12926 attr == HLF_SPC ? "caps" :
12927 ""), -1);
12928}
12929
12930/*
12931 * "spellsuggest()" function
12932 */
12933 static void
12934f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12935{
12936#ifdef FEAT_SPELL
12937 char_u *str;
12938 int typeerr = FALSE;
12939 int maxcount;
12940 garray_T ga;
12941 int i;
12942 listitem_T *li;
12943 int need_capital = FALSE;
12944#endif
12945
12946 if (rettv_list_alloc(rettv) == FAIL)
12947 return;
12948
12949#ifdef FEAT_SPELL
12950 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012952 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012953 if (argvars[1].v_type != VAR_UNKNOWN)
12954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012955 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012956 if (maxcount <= 0)
12957 return;
12958 if (argvars[2].v_type != VAR_UNKNOWN)
12959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012960 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 if (typeerr)
12962 return;
12963 }
12964 }
12965 else
12966 maxcount = 25;
12967
12968 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12969
12970 for (i = 0; i < ga.ga_len; ++i)
12971 {
12972 str = ((char_u **)ga.ga_data)[i];
12973
12974 li = listitem_alloc();
12975 if (li == NULL)
12976 vim_free(str);
12977 else
12978 {
12979 li->li_tv.v_type = VAR_STRING;
12980 li->li_tv.v_lock = 0;
12981 li->li_tv.vval.v_string = str;
12982 list_append(rettv->vval.v_list, li);
12983 }
12984 }
12985 ga_clear(&ga);
12986 }
12987#endif
12988}
12989
12990 static void
12991f_split(typval_T *argvars, typval_T *rettv)
12992{
12993 char_u *str;
12994 char_u *end;
12995 char_u *pat = NULL;
12996 regmatch_T regmatch;
12997 char_u patbuf[NUMBUFLEN];
12998 char_u *save_cpo;
12999 int match;
13000 colnr_T col = 0;
13001 int keepempty = FALSE;
13002 int typeerr = FALSE;
13003
13004 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13005 save_cpo = p_cpo;
13006 p_cpo = (char_u *)"";
13007
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013008 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009 if (argvars[1].v_type != VAR_UNKNOWN)
13010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013011 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 if (pat == NULL)
13013 typeerr = TRUE;
13014 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013015 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013016 }
13017 if (pat == NULL || *pat == NUL)
13018 pat = (char_u *)"[\\x01- ]\\+";
13019
13020 if (rettv_list_alloc(rettv) == FAIL)
13021 return;
13022 if (typeerr)
13023 return;
13024
13025 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13026 if (regmatch.regprog != NULL)
13027 {
13028 regmatch.rm_ic = FALSE;
13029 while (*str != NUL || keepempty)
13030 {
13031 if (*str == NUL)
13032 match = FALSE; /* empty item at the end */
13033 else
13034 match = vim_regexec_nl(&regmatch, str, col);
13035 if (match)
13036 end = regmatch.startp[0];
13037 else
13038 end = str + STRLEN(str);
13039 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13040 && *str != NUL && match && end < regmatch.endp[0]))
13041 {
13042 if (list_append_string(rettv->vval.v_list, str,
13043 (int)(end - str)) == FAIL)
13044 break;
13045 }
13046 if (!match)
13047 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013048 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049 if (regmatch.endp[0] > str)
13050 col = 0;
13051 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013052 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013053 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013054 str = regmatch.endp[0];
13055 }
13056
13057 vim_regfree(regmatch.regprog);
13058 }
13059
13060 p_cpo = save_cpo;
13061}
13062
13063#ifdef FEAT_FLOAT
13064/*
13065 * "sqrt()" function
13066 */
13067 static void
13068f_sqrt(typval_T *argvars, typval_T *rettv)
13069{
13070 float_T f = 0.0;
13071
13072 rettv->v_type = VAR_FLOAT;
13073 if (get_float_arg(argvars, &f) == OK)
13074 rettv->vval.v_float = sqrt(f);
13075 else
13076 rettv->vval.v_float = 0.0;
13077}
13078
13079/*
13080 * "str2float()" function
13081 */
13082 static void
13083f_str2float(typval_T *argvars, typval_T *rettv)
13084{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013086 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087
Bram Moolenaar08243d22017-01-10 16:12:29 +010013088 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013089 p = skipwhite(p + 1);
13090 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013091 if (isneg)
13092 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013093 rettv->v_type = VAR_FLOAT;
13094}
13095#endif
13096
13097/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013098 * "str2list()" function
13099 */
13100 static void
13101f_str2list(typval_T *argvars, typval_T *rettv)
13102{
13103 char_u *p;
13104 int utf8 = FALSE;
13105
13106 if (rettv_list_alloc(rettv) == FAIL)
13107 return;
13108
13109 if (argvars[1].v_type != VAR_UNKNOWN)
13110 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13111
13112 p = tv_get_string(&argvars[0]);
13113
13114 if (has_mbyte || utf8)
13115 {
13116 int (*ptr2len)(char_u *);
13117 int (*ptr2char)(char_u *);
13118
13119 if (utf8 || enc_utf8)
13120 {
13121 ptr2len = utf_ptr2len;
13122 ptr2char = utf_ptr2char;
13123 }
13124 else
13125 {
13126 ptr2len = mb_ptr2len;
13127 ptr2char = mb_ptr2char;
13128 }
13129
13130 for ( ; *p != NUL; p += (*ptr2len)(p))
13131 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13132 }
13133 else
13134 for ( ; *p != NUL; ++p)
13135 list_append_number(rettv->vval.v_list, *p);
13136}
13137
13138/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139 * "str2nr()" function
13140 */
13141 static void
13142f_str2nr(typval_T *argvars, typval_T *rettv)
13143{
13144 int base = 10;
13145 char_u *p;
13146 varnumber_T n;
13147 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013148 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013149
13150 if (argvars[1].v_type != VAR_UNKNOWN)
13151 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013152 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 if (base != 2 && base != 8 && base != 10 && base != 16)
13154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013155 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 return;
13157 }
13158 }
13159
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013160 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013161 isneg = (*p == '-');
13162 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013163 p = skipwhite(p + 1);
13164 switch (base)
13165 {
13166 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13167 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13168 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13169 default: what = 0;
13170 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013171 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13172 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013173 if (isneg)
13174 rettv->vval.v_number = -n;
13175 else
13176 rettv->vval.v_number = n;
13177
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013178}
13179
13180#ifdef HAVE_STRFTIME
13181/*
13182 * "strftime({format}[, {time}])" function
13183 */
13184 static void
13185f_strftime(typval_T *argvars, typval_T *rettv)
13186{
13187 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013188# ifdef HAVE_LOCALTIME_R
13189 struct tm tmval;
13190# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191 struct tm *curtime;
13192 time_t seconds;
13193 char_u *p;
13194
13195 rettv->v_type = VAR_STRING;
13196
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013197 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013198 if (argvars[1].v_type == VAR_UNKNOWN)
13199 seconds = time(NULL);
13200 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013201 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013202# ifdef HAVE_LOCALTIME_R
13203 curtime = localtime_r(&seconds, &tmval);
13204# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013205 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013206# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013207 /* MSVC returns NULL for an invalid value of seconds. */
13208 if (curtime == NULL)
13209 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13210 else
13211 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212 vimconv_T conv;
13213 char_u *enc;
13214
13215 conv.vc_type = CONV_NONE;
13216 enc = enc_locale();
13217 convert_setup(&conv, p_enc, enc);
13218 if (conv.vc_type != CONV_NONE)
13219 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220 if (p != NULL)
13221 (void)strftime((char *)result_buf, sizeof(result_buf),
13222 (char *)p, curtime);
13223 else
13224 result_buf[0] = NUL;
13225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013226 if (conv.vc_type != CONV_NONE)
13227 vim_free(p);
13228 convert_setup(&conv, enc, p_enc);
13229 if (conv.vc_type != CONV_NONE)
13230 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13231 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232 rettv->vval.v_string = vim_strsave(result_buf);
13233
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234 /* Release conversion descriptors */
13235 convert_setup(&conv, NULL, NULL);
13236 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237 }
13238}
13239#endif
13240
13241/*
13242 * "strgetchar()" function
13243 */
13244 static void
13245f_strgetchar(typval_T *argvars, typval_T *rettv)
13246{
13247 char_u *str;
13248 int len;
13249 int error = FALSE;
13250 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013251 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013252
13253 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013254 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013255 if (str == NULL)
13256 return;
13257 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013258 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259 if (error)
13260 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013261
Bram Moolenaar13505972019-01-24 15:04:48 +010013262 while (charidx >= 0 && byteidx < len)
13263 {
13264 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013266 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13267 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013269 --charidx;
13270 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272}
13273
13274/*
13275 * "stridx()" function
13276 */
13277 static void
13278f_stridx(typval_T *argvars, typval_T *rettv)
13279{
13280 char_u buf[NUMBUFLEN];
13281 char_u *needle;
13282 char_u *haystack;
13283 char_u *save_haystack;
13284 char_u *pos;
13285 int start_idx;
13286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013287 needle = tv_get_string_chk(&argvars[1]);
13288 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 rettv->vval.v_number = -1;
13290 if (needle == NULL || haystack == NULL)
13291 return; /* type error; errmsg already given */
13292
13293 if (argvars[2].v_type != VAR_UNKNOWN)
13294 {
13295 int error = FALSE;
13296
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013297 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 if (error || start_idx >= (int)STRLEN(haystack))
13299 return;
13300 if (start_idx >= 0)
13301 haystack += start_idx;
13302 }
13303
13304 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13305 if (pos != NULL)
13306 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13307}
13308
13309/*
13310 * "string()" function
13311 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013312 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313f_string(typval_T *argvars, typval_T *rettv)
13314{
13315 char_u *tofree;
13316 char_u numbuf[NUMBUFLEN];
13317
13318 rettv->v_type = VAR_STRING;
13319 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13320 get_copyID());
13321 /* Make a copy if we have a value but it's not in allocated memory. */
13322 if (rettv->vval.v_string != NULL && tofree == NULL)
13323 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13324}
13325
13326/*
13327 * "strlen()" function
13328 */
13329 static void
13330f_strlen(typval_T *argvars, typval_T *rettv)
13331{
13332 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013333 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334}
13335
13336/*
13337 * "strchars()" function
13338 */
13339 static void
13340f_strchars(typval_T *argvars, typval_T *rettv)
13341{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013342 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 varnumber_T len = 0;
13345 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346
13347 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013348 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013349 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013350 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013351 else
13352 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13354 while (*s != NUL)
13355 {
13356 func_mb_ptr2char_adv(&s);
13357 ++len;
13358 }
13359 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013360 }
13361}
13362
13363/*
13364 * "strdisplaywidth()" function
13365 */
13366 static void
13367f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13368{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013369 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013370 int col = 0;
13371
13372 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013373 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013374
13375 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13376}
13377
13378/*
13379 * "strwidth()" function
13380 */
13381 static void
13382f_strwidth(typval_T *argvars, typval_T *rettv)
13383{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013384 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013385
Bram Moolenaar13505972019-01-24 15:04:48 +010013386 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387}
13388
13389/*
13390 * "strcharpart()" function
13391 */
13392 static void
13393f_strcharpart(typval_T *argvars, typval_T *rettv)
13394{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013395 char_u *p;
13396 int nchar;
13397 int nbyte = 0;
13398 int charlen;
13399 int len = 0;
13400 int slen;
13401 int error = FALSE;
13402
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013403 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013404 slen = (int)STRLEN(p);
13405
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013406 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013407 if (!error)
13408 {
13409 if (nchar > 0)
13410 while (nchar > 0 && nbyte < slen)
13411 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013412 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413 --nchar;
13414 }
13415 else
13416 nbyte = nchar;
13417 if (argvars[2].v_type != VAR_UNKNOWN)
13418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013419 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420 while (charlen > 0 && nbyte + len < slen)
13421 {
13422 int off = nbyte + len;
13423
13424 if (off < 0)
13425 len += 1;
13426 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013427 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428 --charlen;
13429 }
13430 }
13431 else
13432 len = slen - nbyte; /* default: all bytes that are available. */
13433 }
13434
13435 /*
13436 * Only return the overlap between the specified part and the actual
13437 * string.
13438 */
13439 if (nbyte < 0)
13440 {
13441 len += nbyte;
13442 nbyte = 0;
13443 }
13444 else if (nbyte > slen)
13445 nbyte = slen;
13446 if (len < 0)
13447 len = 0;
13448 else if (nbyte + len > slen)
13449 len = slen - nbyte;
13450
13451 rettv->v_type = VAR_STRING;
13452 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013453}
13454
13455/*
13456 * "strpart()" function
13457 */
13458 static void
13459f_strpart(typval_T *argvars, typval_T *rettv)
13460{
13461 char_u *p;
13462 int n;
13463 int len;
13464 int slen;
13465 int error = FALSE;
13466
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013467 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468 slen = (int)STRLEN(p);
13469
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013470 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013471 if (error)
13472 len = 0;
13473 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013474 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013475 else
13476 len = slen - n; /* default len: all bytes that are available. */
13477
13478 /*
13479 * Only return the overlap between the specified part and the actual
13480 * string.
13481 */
13482 if (n < 0)
13483 {
13484 len += n;
13485 n = 0;
13486 }
13487 else if (n > slen)
13488 n = slen;
13489 if (len < 0)
13490 len = 0;
13491 else if (n + len > slen)
13492 len = slen - n;
13493
13494 rettv->v_type = VAR_STRING;
13495 rettv->vval.v_string = vim_strnsave(p + n, len);
13496}
13497
13498/*
13499 * "strridx()" function
13500 */
13501 static void
13502f_strridx(typval_T *argvars, typval_T *rettv)
13503{
13504 char_u buf[NUMBUFLEN];
13505 char_u *needle;
13506 char_u *haystack;
13507 char_u *rest;
13508 char_u *lastmatch = NULL;
13509 int haystack_len, end_idx;
13510
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013511 needle = tv_get_string_chk(&argvars[1]);
13512 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013513
13514 rettv->vval.v_number = -1;
13515 if (needle == NULL || haystack == NULL)
13516 return; /* type error; errmsg already given */
13517
13518 haystack_len = (int)STRLEN(haystack);
13519 if (argvars[2].v_type != VAR_UNKNOWN)
13520 {
13521 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013522 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013523 if (end_idx < 0)
13524 return; /* can never find a match */
13525 }
13526 else
13527 end_idx = haystack_len;
13528
13529 if (*needle == NUL)
13530 {
13531 /* Empty string matches past the end. */
13532 lastmatch = haystack + end_idx;
13533 }
13534 else
13535 {
13536 for (rest = haystack; *rest != '\0'; ++rest)
13537 {
13538 rest = (char_u *)strstr((char *)rest, (char *)needle);
13539 if (rest == NULL || rest > haystack + end_idx)
13540 break;
13541 lastmatch = rest;
13542 }
13543 }
13544
13545 if (lastmatch == NULL)
13546 rettv->vval.v_number = -1;
13547 else
13548 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13549}
13550
13551/*
13552 * "strtrans()" function
13553 */
13554 static void
13555f_strtrans(typval_T *argvars, typval_T *rettv)
13556{
13557 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013558 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013559}
13560
13561/*
13562 * "submatch()" function
13563 */
13564 static void
13565f_submatch(typval_T *argvars, typval_T *rettv)
13566{
13567 int error = FALSE;
13568 int no;
13569 int retList = 0;
13570
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013571 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572 if (error)
13573 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013574 if (no < 0 || no >= NSUBEXP)
13575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013576 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013577 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013578 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013579 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013580 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013581 if (error)
13582 return;
13583
13584 if (retList == 0)
13585 {
13586 rettv->v_type = VAR_STRING;
13587 rettv->vval.v_string = reg_submatch(no);
13588 }
13589 else
13590 {
13591 rettv->v_type = VAR_LIST;
13592 rettv->vval.v_list = reg_submatch_list(no);
13593 }
13594}
13595
13596/*
13597 * "substitute()" function
13598 */
13599 static void
13600f_substitute(typval_T *argvars, typval_T *rettv)
13601{
13602 char_u patbuf[NUMBUFLEN];
13603 char_u subbuf[NUMBUFLEN];
13604 char_u flagsbuf[NUMBUFLEN];
13605
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013606 char_u *str = tv_get_string_chk(&argvars[0]);
13607 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013608 char_u *sub = NULL;
13609 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013610 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013611
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013612 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13613 expr = &argvars[2];
13614 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013615 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013618 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13619 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620 rettv->vval.v_string = NULL;
13621 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013622 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013623}
13624
13625/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013626 * "swapinfo(swap_filename)" function
13627 */
13628 static void
13629f_swapinfo(typval_T *argvars, typval_T *rettv)
13630{
13631 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013632 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013633}
13634
13635/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013636 * "swapname(expr)" function
13637 */
13638 static void
13639f_swapname(typval_T *argvars, typval_T *rettv)
13640{
13641 buf_T *buf;
13642
13643 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013644 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013645 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13646 || buf->b_ml.ml_mfp->mf_fname == NULL)
13647 rettv->vval.v_string = NULL;
13648 else
13649 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13650}
13651
13652/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013653 * "synID(lnum, col, trans)" function
13654 */
13655 static void
13656f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13657{
13658 int id = 0;
13659#ifdef FEAT_SYN_HL
13660 linenr_T lnum;
13661 colnr_T col;
13662 int trans;
13663 int transerr = FALSE;
13664
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013665 lnum = tv_get_lnum(argvars); /* -1 on type error */
13666 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13667 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013668
13669 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13670 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13671 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13672#endif
13673
13674 rettv->vval.v_number = id;
13675}
13676
13677/*
13678 * "synIDattr(id, what [, mode])" function
13679 */
13680 static void
13681f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13682{
13683 char_u *p = NULL;
13684#ifdef FEAT_SYN_HL
13685 int id;
13686 char_u *what;
13687 char_u *mode;
13688 char_u modebuf[NUMBUFLEN];
13689 int modec;
13690
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013691 id = (int)tv_get_number(&argvars[0]);
13692 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693 if (argvars[2].v_type != VAR_UNKNOWN)
13694 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013695 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013696 modec = TOLOWER_ASC(mode[0]);
13697 if (modec != 't' && modec != 'c' && modec != 'g')
13698 modec = 0; /* replace invalid with current */
13699 }
13700 else
13701 {
13702#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13703 if (USE_24BIT)
13704 modec = 'g';
13705 else
13706#endif
13707 if (t_colors > 1)
13708 modec = 'c';
13709 else
13710 modec = 't';
13711 }
13712
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013713 switch (TOLOWER_ASC(what[0]))
13714 {
13715 case 'b':
13716 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13717 p = highlight_color(id, what, modec);
13718 else /* bold */
13719 p = highlight_has_attr(id, HL_BOLD, modec);
13720 break;
13721
13722 case 'f': /* fg[#] or font */
13723 p = highlight_color(id, what, modec);
13724 break;
13725
13726 case 'i':
13727 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13728 p = highlight_has_attr(id, HL_INVERSE, modec);
13729 else /* italic */
13730 p = highlight_has_attr(id, HL_ITALIC, modec);
13731 break;
13732
13733 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013734 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013735 break;
13736
13737 case 'r': /* reverse */
13738 p = highlight_has_attr(id, HL_INVERSE, modec);
13739 break;
13740
13741 case 's':
13742 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13743 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013744 /* strikeout */
13745 else if (TOLOWER_ASC(what[1]) == 't' &&
13746 TOLOWER_ASC(what[2]) == 'r')
13747 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 else /* standout */
13749 p = highlight_has_attr(id, HL_STANDOUT, modec);
13750 break;
13751
13752 case 'u':
13753 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13754 /* underline */
13755 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13756 else
13757 /* undercurl */
13758 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13759 break;
13760 }
13761
13762 if (p != NULL)
13763 p = vim_strsave(p);
13764#endif
13765 rettv->v_type = VAR_STRING;
13766 rettv->vval.v_string = p;
13767}
13768
13769/*
13770 * "synIDtrans(id)" function
13771 */
13772 static void
13773f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13774{
13775 int id;
13776
13777#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013778 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779
13780 if (id > 0)
13781 id = syn_get_final_id(id);
13782 else
13783#endif
13784 id = 0;
13785
13786 rettv->vval.v_number = id;
13787}
13788
13789/*
13790 * "synconcealed(lnum, col)" function
13791 */
13792 static void
13793f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13794{
13795#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13796 linenr_T lnum;
13797 colnr_T col;
13798 int syntax_flags = 0;
13799 int cchar;
13800 int matchid = 0;
13801 char_u str[NUMBUFLEN];
13802#endif
13803
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013804 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013805
13806#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013807 lnum = tv_get_lnum(argvars); /* -1 on type error */
13808 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809
13810 vim_memset(str, NUL, sizeof(str));
13811
13812 if (rettv_list_alloc(rettv) != FAIL)
13813 {
13814 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13815 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13816 && curwin->w_p_cole > 0)
13817 {
13818 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13819 syntax_flags = get_syntax_info(&matchid);
13820
13821 /* get the conceal character */
13822 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13823 {
13824 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013825 if (cchar == NUL && curwin->w_p_cole == 1)
13826 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013827 if (cchar != NUL)
13828 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 if (has_mbyte)
13830 (*mb_char2bytes)(cchar, str);
13831 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013832 str[0] = cchar;
13833 }
13834 }
13835 }
13836
13837 list_append_number(rettv->vval.v_list,
13838 (syntax_flags & HL_CONCEAL) != 0);
13839 /* -1 to auto-determine strlen */
13840 list_append_string(rettv->vval.v_list, str, -1);
13841 list_append_number(rettv->vval.v_list, matchid);
13842 }
13843#endif
13844}
13845
13846/*
13847 * "synstack(lnum, col)" function
13848 */
13849 static void
13850f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13851{
13852#ifdef FEAT_SYN_HL
13853 linenr_T lnum;
13854 colnr_T col;
13855 int i;
13856 int id;
13857#endif
13858
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013859 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013860
13861#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013862 lnum = tv_get_lnum(argvars); /* -1 on type error */
13863 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013864
13865 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13866 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13867 && rettv_list_alloc(rettv) != FAIL)
13868 {
13869 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13870 for (i = 0; ; ++i)
13871 {
13872 id = syn_get_stack_item(i);
13873 if (id < 0)
13874 break;
13875 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13876 break;
13877 }
13878 }
13879#endif
13880}
13881
13882 static void
13883get_cmd_output_as_rettv(
13884 typval_T *argvars,
13885 typval_T *rettv,
13886 int retlist)
13887{
13888 char_u *res = NULL;
13889 char_u *p;
13890 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 int err = FALSE;
13892 FILE *fd;
13893 list_T *list = NULL;
13894 int flags = SHELL_SILENT;
13895
13896 rettv->v_type = VAR_STRING;
13897 rettv->vval.v_string = NULL;
13898 if (check_restricted() || check_secure())
13899 goto errret;
13900
13901 if (argvars[1].v_type != VAR_UNKNOWN)
13902 {
13903 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013904 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013905 * command.
13906 */
13907 if ((infile = vim_tempname('i', TRUE)) == NULL)
13908 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013909 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013910 goto errret;
13911 }
13912
13913 fd = mch_fopen((char *)infile, WRITEBIN);
13914 if (fd == NULL)
13915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013916 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013917 goto errret;
13918 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013919 if (argvars[1].v_type == VAR_NUMBER)
13920 {
13921 linenr_T lnum;
13922 buf_T *buf;
13923
13924 buf = buflist_findnr(argvars[1].vval.v_number);
13925 if (buf == NULL)
13926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013927 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013928 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013929 goto errret;
13930 }
13931
13932 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13933 {
13934 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13935 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13936 {
13937 err = TRUE;
13938 break;
13939 }
13940 if (putc(NL, fd) == EOF)
13941 {
13942 err = TRUE;
13943 break;
13944 }
13945 }
13946 }
13947 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013948 {
13949 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13950 err = TRUE;
13951 }
13952 else
13953 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013954 size_t len;
13955 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013956
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013957 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013958 if (p == NULL)
13959 {
13960 fclose(fd);
13961 goto errret; /* type error; errmsg already given */
13962 }
13963 len = STRLEN(p);
13964 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13965 err = TRUE;
13966 }
13967 if (fclose(fd) != 0)
13968 err = TRUE;
13969 if (err)
13970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013971 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013972 goto errret;
13973 }
13974 }
13975
13976 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13977 * echoes typeahead, that messes up the display. */
13978 if (!msg_silent)
13979 flags += SHELL_COOKED;
13980
13981 if (retlist)
13982 {
13983 int len;
13984 listitem_T *li;
13985 char_u *s = NULL;
13986 char_u *start;
13987 char_u *end;
13988 int i;
13989
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013990 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013991 if (res == NULL)
13992 goto errret;
13993
13994 list = list_alloc();
13995 if (list == NULL)
13996 goto errret;
13997
13998 for (i = 0; i < len; ++i)
13999 {
14000 start = res + i;
14001 while (i < len && res[i] != NL)
14002 ++i;
14003 end = res + i;
14004
Bram Moolenaar964b3742019-05-24 18:54:09 +020014005 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014006 if (s == NULL)
14007 goto errret;
14008
14009 for (p = s; start < end; ++p, ++start)
14010 *p = *start == NUL ? NL : *start;
14011 *p = NUL;
14012
14013 li = listitem_alloc();
14014 if (li == NULL)
14015 {
14016 vim_free(s);
14017 goto errret;
14018 }
14019 li->li_tv.v_type = VAR_STRING;
14020 li->li_tv.v_lock = 0;
14021 li->li_tv.vval.v_string = s;
14022 list_append(list, li);
14023 }
14024
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014025 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014026 list = NULL;
14027 }
14028 else
14029 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014030 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014031#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032 /* translate <CR><NL> into <NL> */
14033 if (res != NULL)
14034 {
14035 char_u *s, *d;
14036
14037 d = res;
14038 for (s = res; *s; ++s)
14039 {
14040 if (s[0] == CAR && s[1] == NL)
14041 ++s;
14042 *d++ = *s;
14043 }
14044 *d = NUL;
14045 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014046#endif
14047 rettv->vval.v_string = res;
14048 res = NULL;
14049 }
14050
14051errret:
14052 if (infile != NULL)
14053 {
14054 mch_remove(infile);
14055 vim_free(infile);
14056 }
14057 if (res != NULL)
14058 vim_free(res);
14059 if (list != NULL)
14060 list_free(list);
14061}
14062
14063/*
14064 * "system()" function
14065 */
14066 static void
14067f_system(typval_T *argvars, typval_T *rettv)
14068{
14069 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14070}
14071
14072/*
14073 * "systemlist()" function
14074 */
14075 static void
14076f_systemlist(typval_T *argvars, typval_T *rettv)
14077{
14078 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14079}
14080
14081/*
14082 * "tabpagebuflist()" function
14083 */
14084 static void
14085f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14086{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014087 tabpage_T *tp;
14088 win_T *wp = NULL;
14089
14090 if (argvars[0].v_type == VAR_UNKNOWN)
14091 wp = firstwin;
14092 else
14093 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014094 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014095 if (tp != NULL)
14096 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14097 }
14098 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14099 {
14100 for (; wp != NULL; wp = wp->w_next)
14101 if (list_append_number(rettv->vval.v_list,
14102 wp->w_buffer->b_fnum) == FAIL)
14103 break;
14104 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014105}
14106
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014107/*
14108 * "tabpagenr()" function
14109 */
14110 static void
14111f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14112{
14113 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114 char_u *arg;
14115
14116 if (argvars[0].v_type != VAR_UNKNOWN)
14117 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014118 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014119 nr = 0;
14120 if (arg != NULL)
14121 {
14122 if (STRCMP(arg, "$") == 0)
14123 nr = tabpage_index(NULL) - 1;
14124 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014125 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014126 }
14127 }
14128 else
14129 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014130 rettv->vval.v_number = nr;
14131}
14132
14133
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014134/*
14135 * Common code for tabpagewinnr() and winnr().
14136 */
14137 static int
14138get_winnr(tabpage_T *tp, typval_T *argvar)
14139{
14140 win_T *twin;
14141 int nr = 1;
14142 win_T *wp;
14143 char_u *arg;
14144
14145 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14146 if (argvar->v_type != VAR_UNKNOWN)
14147 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014148 int invalid_arg = FALSE;
14149
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014150 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151 if (arg == NULL)
14152 nr = 0; /* type error; errmsg already given */
14153 else if (STRCMP(arg, "$") == 0)
14154 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14155 else if (STRCMP(arg, "#") == 0)
14156 {
14157 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14158 if (twin == NULL)
14159 nr = 0;
14160 }
14161 else
14162 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014163 long count;
14164 char_u *endp;
14165
14166 // Extract the window count (if specified). e.g. winnr('3j')
14167 count = strtol((char *)arg, (char **)&endp, 10);
14168 if (count <= 0)
14169 count = 1; // if count is not specified, default to 1
14170 if (endp != NULL && *endp != '\0')
14171 {
14172 if (STRCMP(endp, "j") == 0)
14173 twin = win_vert_neighbor(tp, twin, FALSE, count);
14174 else if (STRCMP(endp, "k") == 0)
14175 twin = win_vert_neighbor(tp, twin, TRUE, count);
14176 else if (STRCMP(endp, "h") == 0)
14177 twin = win_horz_neighbor(tp, twin, TRUE, count);
14178 else if (STRCMP(endp, "l") == 0)
14179 twin = win_horz_neighbor(tp, twin, FALSE, count);
14180 else
14181 invalid_arg = TRUE;
14182 }
14183 else
14184 invalid_arg = TRUE;
14185 }
14186
14187 if (invalid_arg)
14188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014189 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014190 nr = 0;
14191 }
14192 }
14193
14194 if (nr > 0)
14195 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14196 wp != twin; wp = wp->w_next)
14197 {
14198 if (wp == NULL)
14199 {
14200 /* didn't find it in this tabpage */
14201 nr = 0;
14202 break;
14203 }
14204 ++nr;
14205 }
14206 return nr;
14207}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014208
14209/*
14210 * "tabpagewinnr()" function
14211 */
14212 static void
14213f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14214{
14215 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216 tabpage_T *tp;
14217
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014218 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014219 if (tp == NULL)
14220 nr = 0;
14221 else
14222 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223 rettv->vval.v_number = nr;
14224}
14225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014226/*
14227 * "tagfiles()" function
14228 */
14229 static void
14230f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14231{
14232 char_u *fname;
14233 tagname_T tn;
14234 int first;
14235
14236 if (rettv_list_alloc(rettv) == FAIL)
14237 return;
14238 fname = alloc(MAXPATHL);
14239 if (fname == NULL)
14240 return;
14241
14242 for (first = TRUE; ; first = FALSE)
14243 if (get_tagfname(&tn, first, fname) == FAIL
14244 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14245 break;
14246 tagname_free(&tn);
14247 vim_free(fname);
14248}
14249
14250/*
14251 * "taglist()" function
14252 */
14253 static void
14254f_taglist(typval_T *argvars, typval_T *rettv)
14255{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014256 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014257 char_u *tag_pattern;
14258
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014259 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014260
14261 rettv->vval.v_number = FALSE;
14262 if (*tag_pattern == NUL)
14263 return;
14264
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014265 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014266 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014267 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014268 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014269}
14270
14271/*
14272 * "tempname()" function
14273 */
14274 static void
14275f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14276{
14277 static int x = 'A';
14278
14279 rettv->v_type = VAR_STRING;
14280 rettv->vval.v_string = vim_tempname(x, FALSE);
14281
14282 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14283 * names. Skip 'I' and 'O', they are used for shell redirection. */
14284 do
14285 {
14286 if (x == 'Z')
14287 x = '0';
14288 else if (x == '9')
14289 x = 'A';
14290 else
14291 {
14292#ifdef EBCDIC
14293 if (x == 'I')
14294 x = 'J';
14295 else if (x == 'R')
14296 x = 'S';
14297 else
14298#endif
14299 ++x;
14300 }
14301 } while (x == 'I' || x == 'O');
14302}
14303
14304#ifdef FEAT_FLOAT
14305/*
14306 * "tan()" function
14307 */
14308 static void
14309f_tan(typval_T *argvars, typval_T *rettv)
14310{
14311 float_T f = 0.0;
14312
14313 rettv->v_type = VAR_FLOAT;
14314 if (get_float_arg(argvars, &f) == OK)
14315 rettv->vval.v_float = tan(f);
14316 else
14317 rettv->vval.v_float = 0.0;
14318}
14319
14320/*
14321 * "tanh()" function
14322 */
14323 static void
14324f_tanh(typval_T *argvars, typval_T *rettv)
14325{
14326 float_T f = 0.0;
14327
14328 rettv->v_type = VAR_FLOAT;
14329 if (get_float_arg(argvars, &f) == OK)
14330 rettv->vval.v_float = tanh(f);
14331 else
14332 rettv->vval.v_float = 0.0;
14333}
14334#endif
14335
14336/*
14337 * "test_alloc_fail(id, countdown, repeat)" function
14338 */
14339 static void
14340f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14341{
14342 if (argvars[0].v_type != VAR_NUMBER
14343 || argvars[0].vval.v_number <= 0
14344 || argvars[1].v_type != VAR_NUMBER
14345 || argvars[1].vval.v_number < 0
14346 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014347 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014348 else
14349 {
14350 alloc_fail_id = argvars[0].vval.v_number;
14351 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014352 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353 alloc_fail_countdown = argvars[1].vval.v_number;
14354 alloc_fail_repeat = argvars[2].vval.v_number;
14355 did_outofmem_msg = FALSE;
14356 }
14357}
14358
14359/*
14360 * "test_autochdir()"
14361 */
14362 static void
14363f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14364{
14365#if defined(FEAT_AUTOCHDIR)
14366 test_autochdir = TRUE;
14367#endif
14368}
14369
14370/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014371 * "test_feedinput()"
14372 */
14373 static void
14374f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14375{
14376#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014377 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014378
14379 if (val != NULL)
14380 {
14381 trash_input_buf();
14382 add_to_input_buf_csi(val, (int)STRLEN(val));
14383 }
14384#endif
14385}
14386
14387/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014388 * "test_getvalue({name})" function
14389 */
14390 static void
14391f_test_getvalue(typval_T *argvars, typval_T *rettv)
14392{
14393 if (argvars[0].v_type != VAR_STRING)
14394 emsg(_(e_invarg));
14395 else
14396 {
14397 char_u *name = tv_get_string(&argvars[0]);
14398
14399 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14400 rettv->vval.v_number = need_fileinfo;
14401 else
14402 semsg(_(e_invarg2), name);
14403 }
14404}
14405
14406/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014407 * "test_option_not_set({name})" function
14408 */
14409 static void
14410f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14411{
14412 char_u *name = (char_u *)"";
14413
14414 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014415 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014416 else
14417 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014418 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014419 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014420 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014421 }
14422}
14423
14424/*
14425 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014426 */
14427 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014428f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014429{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014430 char_u *name = (char_u *)"";
14431 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014432 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014433
14434 if (argvars[0].v_type != VAR_STRING
14435 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014436 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014437 else
14438 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014439 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014440 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014441
14442 if (STRCMP(name, (char_u *)"redraw") == 0)
14443 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014444 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14445 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014446 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14447 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014448 else if (STRCMP(name, (char_u *)"starting") == 0)
14449 {
14450 if (val)
14451 {
14452 if (save_starting < 0)
14453 save_starting = starting;
14454 starting = 0;
14455 }
14456 else
14457 {
14458 starting = save_starting;
14459 save_starting = -1;
14460 }
14461 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014462 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14463 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014464 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14465 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014466 else if (STRCMP(name, (char_u *)"ALL") == 0)
14467 {
14468 disable_char_avail_for_testing = FALSE;
14469 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014470 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014471 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014472 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014473 if (save_starting >= 0)
14474 {
14475 starting = save_starting;
14476 save_starting = -1;
14477 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014478 }
14479 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014480 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014481 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014482}
14483
14484/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014485 * "test_refcount({expr})" function
14486 */
14487 static void
14488f_test_refcount(typval_T *argvars, typval_T *rettv)
14489{
14490 int retval = -1;
14491
14492 switch (argvars[0].v_type)
14493 {
14494 case VAR_UNKNOWN:
14495 case VAR_NUMBER:
14496 case VAR_FLOAT:
14497 case VAR_SPECIAL:
14498 case VAR_STRING:
14499 break;
14500 case VAR_JOB:
14501#ifdef FEAT_JOB_CHANNEL
14502 if (argvars[0].vval.v_job != NULL)
14503 retval = argvars[0].vval.v_job->jv_refcount - 1;
14504#endif
14505 break;
14506 case VAR_CHANNEL:
14507#ifdef FEAT_JOB_CHANNEL
14508 if (argvars[0].vval.v_channel != NULL)
14509 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14510#endif
14511 break;
14512 case VAR_FUNC:
14513 if (argvars[0].vval.v_string != NULL)
14514 {
14515 ufunc_T *fp;
14516
14517 fp = find_func(argvars[0].vval.v_string);
14518 if (fp != NULL)
14519 retval = fp->uf_refcount;
14520 }
14521 break;
14522 case VAR_PARTIAL:
14523 if (argvars[0].vval.v_partial != NULL)
14524 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14525 break;
14526 case VAR_BLOB:
14527 if (argvars[0].vval.v_blob != NULL)
14528 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14529 break;
14530 case VAR_LIST:
14531 if (argvars[0].vval.v_list != NULL)
14532 retval = argvars[0].vval.v_list->lv_refcount - 1;
14533 break;
14534 case VAR_DICT:
14535 if (argvars[0].vval.v_dict != NULL)
14536 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14537 break;
14538 }
14539
14540 rettv->v_type = VAR_NUMBER;
14541 rettv->vval.v_number = retval;
14542
14543}
14544
14545/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014546 * "test_garbagecollect_now()" function
14547 */
14548 static void
14549f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14550{
14551 /* This is dangerous, any Lists and Dicts used internally may be freed
14552 * while still in use. */
14553 garbage_collect(TRUE);
14554}
14555
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014556/*
14557 * "test_ignore_error()" function
14558 */
14559 static void
14560f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14561{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014562 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014563}
14564
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014565 static void
14566f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14567{
14568 rettv->v_type = VAR_BLOB;
14569 rettv->vval.v_blob = NULL;
14570}
14571
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014572#ifdef FEAT_JOB_CHANNEL
14573 static void
14574f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14575{
14576 rettv->v_type = VAR_CHANNEL;
14577 rettv->vval.v_channel = NULL;
14578}
14579#endif
14580
14581 static void
14582f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14583{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014584 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014585}
14586
14587#ifdef FEAT_JOB_CHANNEL
14588 static void
14589f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14590{
14591 rettv->v_type = VAR_JOB;
14592 rettv->vval.v_job = NULL;
14593}
14594#endif
14595
14596 static void
14597f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14598{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014599 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014600}
14601
14602 static void
14603f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14604{
14605 rettv->v_type = VAR_PARTIAL;
14606 rettv->vval.v_partial = NULL;
14607}
14608
14609 static void
14610f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14611{
14612 rettv->v_type = VAR_STRING;
14613 rettv->vval.v_string = NULL;
14614}
14615
Bram Moolenaarab186732018-09-14 21:27:06 +020014616#ifdef FEAT_GUI
14617 static void
14618f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14619{
14620 char_u *which;
14621 long value;
14622 int dragging;
14623 scrollbar_T *sb = NULL;
14624
14625 if (argvars[0].v_type != VAR_STRING
14626 || (argvars[1].v_type) != VAR_NUMBER
14627 || (argvars[2].v_type) != VAR_NUMBER)
14628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014629 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014630 return;
14631 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014632 which = tv_get_string(&argvars[0]);
14633 value = tv_get_number(&argvars[1]);
14634 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014635
14636 if (STRCMP(which, "left") == 0)
14637 sb = &curwin->w_scrollbars[SBAR_LEFT];
14638 else if (STRCMP(which, "right") == 0)
14639 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14640 else if (STRCMP(which, "hor") == 0)
14641 sb = &gui.bottom_sbar;
14642 if (sb == NULL)
14643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014644 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014645 return;
14646 }
14647 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014648# ifndef USE_ON_FLY_SCROLL
14649 // need to loop through normal_cmd() to handle the scroll events
14650 exec_normal(FALSE, TRUE, FALSE);
14651# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014652}
14653#endif
14654
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014655#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014656 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014657f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14658{
14659 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14660 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14661}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014662#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014663
14664 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14666{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014667 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014668}
14669
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670/*
14671 * Get a callback from "arg". It can be a Funcref or a function name.
14672 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014673 * "cb_name" is not allocated.
14674 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014675 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014676 callback_T
14677get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014678{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014679 callback_T res;
14680
14681 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14683 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014684 res.cb_partial = arg->vval.v_partial;
14685 ++res.cb_partial->pt_refcount;
14686 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014688 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014690 res.cb_partial = NULL;
14691 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14692 {
14693 // Note that we don't make a copy of the string.
14694 res.cb_name = arg->vval.v_string;
14695 func_ref(res.cb_name);
14696 }
14697 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14698 {
14699 res.cb_name = (char_u *)"";
14700 }
14701 else
14702 {
14703 emsg(_("E921: Invalid callback argument"));
14704 res.cb_name = NULL;
14705 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014707 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014708}
14709
14710/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014711 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712 */
14713 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014714put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014715{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014716 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014718 tv->v_type = VAR_PARTIAL;
14719 tv->vval.v_partial = cb->cb_partial;
14720 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014721 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014722 else
14723 {
14724 tv->v_type = VAR_FUNC;
14725 tv->vval.v_string = vim_strsave(cb->cb_name);
14726 func_ref(cb->cb_name);
14727 }
14728}
14729
14730/*
14731 * Make a copy of "src" into "dest", allocating the function name if needed,
14732 * without incrementing the refcount.
14733 */
14734 void
14735set_callback(callback_T *dest, callback_T *src)
14736{
14737 if (src->cb_partial == NULL)
14738 {
14739 // just a function name, make a copy
14740 dest->cb_name = vim_strsave(src->cb_name);
14741 dest->cb_free_name = TRUE;
14742 }
14743 else
14744 {
14745 // cb_name is a pointer into cb_partial
14746 dest->cb_name = src->cb_name;
14747 dest->cb_free_name = FALSE;
14748 }
14749 dest->cb_partial = src->cb_partial;
14750}
14751
14752/*
14753 * Unref/free "callback" returned by get_callback() or set_callback().
14754 */
14755 void
14756free_callback(callback_T *callback)
14757{
14758 if (callback->cb_partial != NULL)
14759 {
14760 partial_unref(callback->cb_partial);
14761 callback->cb_partial = NULL;
14762 }
14763 else if (callback->cb_name != NULL)
14764 func_unref(callback->cb_name);
14765 if (callback->cb_free_name)
14766 {
14767 vim_free(callback->cb_name);
14768 callback->cb_free_name = FALSE;
14769 }
14770 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014771}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014772
14773#ifdef FEAT_TIMERS
14774/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014775 * "timer_info([timer])" function
14776 */
14777 static void
14778f_timer_info(typval_T *argvars, typval_T *rettv)
14779{
14780 timer_T *timer = NULL;
14781
14782 if (rettv_list_alloc(rettv) != OK)
14783 return;
14784 if (argvars[0].v_type != VAR_UNKNOWN)
14785 {
14786 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014787 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014788 else
14789 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014790 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014791 if (timer != NULL)
14792 add_timer_info(rettv, timer);
14793 }
14794 }
14795 else
14796 add_timer_info_all(rettv);
14797}
14798
14799/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014800 * "timer_pause(timer, paused)" function
14801 */
14802 static void
14803f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14804{
14805 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014806 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014807
14808 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014809 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014810 else
14811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014812 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014813 if (timer != NULL)
14814 timer->tr_paused = paused;
14815 }
14816}
14817
14818/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014819 * "timer_start(time, callback [, options])" function
14820 */
14821 static void
14822f_timer_start(typval_T *argvars, typval_T *rettv)
14823{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014824 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014825 timer_T *timer;
14826 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014827 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014828 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014829
Bram Moolenaar75537a92016-09-05 22:45:28 +020014830 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014831 if (check_secure())
14832 return;
14833 if (argvars[2].v_type != VAR_UNKNOWN)
14834 {
14835 if (argvars[2].v_type != VAR_DICT
14836 || (dict = argvars[2].vval.v_dict) == NULL)
14837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014838 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014839 return;
14840 }
14841 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014842 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014843 }
14844
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014845 callback = get_callback(&argvars[1]);
14846 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014847 return;
14848
14849 timer = create_timer(msec, repeat);
14850 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014851 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014852 else
14853 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014854 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014855 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014856 }
14857}
14858
14859/*
14860 * "timer_stop(timer)" function
14861 */
14862 static void
14863f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14864{
14865 timer_T *timer;
14866
14867 if (argvars[0].v_type != VAR_NUMBER)
14868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014869 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014870 return;
14871 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014872 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014873 if (timer != NULL)
14874 stop_timer(timer);
14875}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014876
14877/*
14878 * "timer_stopall()" function
14879 */
14880 static void
14881f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14882{
14883 stop_all_timers();
14884}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014885#endif
14886
14887/*
14888 * "tolower(string)" function
14889 */
14890 static void
14891f_tolower(typval_T *argvars, typval_T *rettv)
14892{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014893 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014894 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014895}
14896
14897/*
14898 * "toupper(string)" function
14899 */
14900 static void
14901f_toupper(typval_T *argvars, typval_T *rettv)
14902{
14903 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014904 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014905}
14906
14907/*
14908 * "tr(string, fromstr, tostr)" function
14909 */
14910 static void
14911f_tr(typval_T *argvars, typval_T *rettv)
14912{
14913 char_u *in_str;
14914 char_u *fromstr;
14915 char_u *tostr;
14916 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014917 int inlen;
14918 int fromlen;
14919 int tolen;
14920 int idx;
14921 char_u *cpstr;
14922 int cplen;
14923 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014924 char_u buf[NUMBUFLEN];
14925 char_u buf2[NUMBUFLEN];
14926 garray_T ga;
14927
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014928 in_str = tv_get_string(&argvars[0]);
14929 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14930 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014931
14932 /* Default return value: empty string. */
14933 rettv->v_type = VAR_STRING;
14934 rettv->vval.v_string = NULL;
14935 if (fromstr == NULL || tostr == NULL)
14936 return; /* type error; errmsg already given */
14937 ga_init2(&ga, (int)sizeof(char), 80);
14938
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014939 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014940 /* not multi-byte: fromstr and tostr must be the same length */
14941 if (STRLEN(fromstr) != STRLEN(tostr))
14942 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014943error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014944 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014945 ga_clear(&ga);
14946 return;
14947 }
14948
14949 /* fromstr and tostr have to contain the same number of chars */
14950 while (*in_str != NUL)
14951 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014952 if (has_mbyte)
14953 {
14954 inlen = (*mb_ptr2len)(in_str);
14955 cpstr = in_str;
14956 cplen = inlen;
14957 idx = 0;
14958 for (p = fromstr; *p != NUL; p += fromlen)
14959 {
14960 fromlen = (*mb_ptr2len)(p);
14961 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14962 {
14963 for (p = tostr; *p != NUL; p += tolen)
14964 {
14965 tolen = (*mb_ptr2len)(p);
14966 if (idx-- == 0)
14967 {
14968 cplen = tolen;
14969 cpstr = p;
14970 break;
14971 }
14972 }
14973 if (*p == NUL) /* tostr is shorter than fromstr */
14974 goto error;
14975 break;
14976 }
14977 ++idx;
14978 }
14979
14980 if (first && cpstr == in_str)
14981 {
14982 /* Check that fromstr and tostr have the same number of
14983 * (multi-byte) characters. Done only once when a character
14984 * of in_str doesn't appear in fromstr. */
14985 first = FALSE;
14986 for (p = tostr; *p != NUL; p += tolen)
14987 {
14988 tolen = (*mb_ptr2len)(p);
14989 --idx;
14990 }
14991 if (idx != 0)
14992 goto error;
14993 }
14994
14995 (void)ga_grow(&ga, cplen);
14996 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14997 ga.ga_len += cplen;
14998
14999 in_str += inlen;
15000 }
15001 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015002 {
15003 /* When not using multi-byte chars we can do it faster. */
15004 p = vim_strchr(fromstr, *in_str);
15005 if (p != NULL)
15006 ga_append(&ga, tostr[p - fromstr]);
15007 else
15008 ga_append(&ga, *in_str);
15009 ++in_str;
15010 }
15011 }
15012
15013 /* add a terminating NUL */
15014 (void)ga_grow(&ga, 1);
15015 ga_append(&ga, NUL);
15016
15017 rettv->vval.v_string = ga.ga_data;
15018}
15019
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015020/*
15021 * "trim({expr})" function
15022 */
15023 static void
15024f_trim(typval_T *argvars, typval_T *rettv)
15025{
15026 char_u buf1[NUMBUFLEN];
15027 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015028 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015029 char_u *mask = NULL;
15030 char_u *tail;
15031 char_u *prev;
15032 char_u *p;
15033 int c1;
15034
15035 rettv->v_type = VAR_STRING;
15036 if (head == NULL)
15037 {
15038 rettv->vval.v_string = NULL;
15039 return;
15040 }
15041
15042 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015043 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015044
15045 while (*head != NUL)
15046 {
15047 c1 = PTR2CHAR(head);
15048 if (mask == NULL)
15049 {
15050 if (c1 > ' ' && c1 != 0xa0)
15051 break;
15052 }
15053 else
15054 {
15055 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15056 if (c1 == PTR2CHAR(p))
15057 break;
15058 if (*p == NUL)
15059 break;
15060 }
15061 MB_PTR_ADV(head);
15062 }
15063
15064 for (tail = head + STRLEN(head); tail > head; tail = prev)
15065 {
15066 prev = tail;
15067 MB_PTR_BACK(head, prev);
15068 c1 = PTR2CHAR(prev);
15069 if (mask == NULL)
15070 {
15071 if (c1 > ' ' && c1 != 0xa0)
15072 break;
15073 }
15074 else
15075 {
15076 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15077 if (c1 == PTR2CHAR(p))
15078 break;
15079 if (*p == NUL)
15080 break;
15081 }
15082 }
15083 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15084}
15085
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015086#ifdef FEAT_FLOAT
15087/*
15088 * "trunc({float})" function
15089 */
15090 static void
15091f_trunc(typval_T *argvars, typval_T *rettv)
15092{
15093 float_T f = 0.0;
15094
15095 rettv->v_type = VAR_FLOAT;
15096 if (get_float_arg(argvars, &f) == OK)
15097 /* trunc() is not in C90, use floor() or ceil() instead. */
15098 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15099 else
15100 rettv->vval.v_float = 0.0;
15101}
15102#endif
15103
15104/*
15105 * "type(expr)" function
15106 */
15107 static void
15108f_type(typval_T *argvars, typval_T *rettv)
15109{
15110 int n = -1;
15111
15112 switch (argvars[0].v_type)
15113 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015114 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15115 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015116 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015117 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15118 case VAR_LIST: n = VAR_TYPE_LIST; break;
15119 case VAR_DICT: n = VAR_TYPE_DICT; break;
15120 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015121 case VAR_SPECIAL:
15122 if (argvars[0].vval.v_number == VVAL_FALSE
15123 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015124 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015125 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015126 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015127 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015128 case VAR_JOB: n = VAR_TYPE_JOB; break;
15129 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015130 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015131 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015132 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015133 n = -1;
15134 break;
15135 }
15136 rettv->vval.v_number = n;
15137}
15138
15139/*
15140 * "undofile(name)" function
15141 */
15142 static void
15143f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15144{
15145 rettv->v_type = VAR_STRING;
15146#ifdef FEAT_PERSISTENT_UNDO
15147 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015148 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015149
15150 if (*fname == NUL)
15151 {
15152 /* If there is no file name there will be no undo file. */
15153 rettv->vval.v_string = NULL;
15154 }
15155 else
15156 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015157 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015158
15159 if (ffname != NULL)
15160 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15161 vim_free(ffname);
15162 }
15163 }
15164#else
15165 rettv->vval.v_string = NULL;
15166#endif
15167}
15168
15169/*
15170 * "undotree()" function
15171 */
15172 static void
15173f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15174{
15175 if (rettv_dict_alloc(rettv) == OK)
15176 {
15177 dict_T *dict = rettv->vval.v_dict;
15178 list_T *list;
15179
Bram Moolenaare0be1672018-07-08 16:50:37 +020015180 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15181 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15182 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15183 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15184 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15185 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015186
15187 list = list_alloc();
15188 if (list != NULL)
15189 {
15190 u_eval_tree(curbuf->b_u_oldhead, list);
15191 dict_add_list(dict, "entries", list);
15192 }
15193 }
15194}
15195
15196/*
15197 * "values(dict)" function
15198 */
15199 static void
15200f_values(typval_T *argvars, typval_T *rettv)
15201{
15202 dict_list(argvars, rettv, 1);
15203}
15204
15205/*
15206 * "virtcol(string)" function
15207 */
15208 static void
15209f_virtcol(typval_T *argvars, typval_T *rettv)
15210{
15211 colnr_T vcol = 0;
15212 pos_T *fp;
15213 int fnum = curbuf->b_fnum;
15214
15215 fp = var2fpos(&argvars[0], FALSE, &fnum);
15216 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15217 && fnum == curbuf->b_fnum)
15218 {
15219 getvvcol(curwin, fp, NULL, NULL, &vcol);
15220 ++vcol;
15221 }
15222
15223 rettv->vval.v_number = vcol;
15224}
15225
15226/*
15227 * "visualmode()" function
15228 */
15229 static void
15230f_visualmode(typval_T *argvars, typval_T *rettv)
15231{
15232 char_u str[2];
15233
15234 rettv->v_type = VAR_STRING;
15235 str[0] = curbuf->b_visual_mode_eval;
15236 str[1] = NUL;
15237 rettv->vval.v_string = vim_strsave(str);
15238
15239 /* A non-zero number or non-empty string argument: reset mode. */
15240 if (non_zero_arg(&argvars[0]))
15241 curbuf->b_visual_mode_eval = NUL;
15242}
15243
15244/*
15245 * "wildmenumode()" function
15246 */
15247 static void
15248f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15249{
15250#ifdef FEAT_WILDMENU
15251 if (wild_menu_showing)
15252 rettv->vval.v_number = 1;
15253#endif
15254}
15255
15256/*
15257 * "winbufnr(nr)" function
15258 */
15259 static void
15260f_winbufnr(typval_T *argvars, typval_T *rettv)
15261{
15262 win_T *wp;
15263
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015264 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015265 if (wp == NULL)
15266 rettv->vval.v_number = -1;
15267 else
15268 rettv->vval.v_number = wp->w_buffer->b_fnum;
15269}
15270
15271/*
15272 * "wincol()" function
15273 */
15274 static void
15275f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15276{
15277 validate_cursor();
15278 rettv->vval.v_number = curwin->w_wcol + 1;
15279}
15280
15281/*
15282 * "winheight(nr)" function
15283 */
15284 static void
15285f_winheight(typval_T *argvars, typval_T *rettv)
15286{
15287 win_T *wp;
15288
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015289 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015290 if (wp == NULL)
15291 rettv->vval.v_number = -1;
15292 else
15293 rettv->vval.v_number = wp->w_height;
15294}
15295
15296/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015297 * "winlayout()" function
15298 */
15299 static void
15300f_winlayout(typval_T *argvars, typval_T *rettv)
15301{
15302 tabpage_T *tp;
15303
15304 if (rettv_list_alloc(rettv) != OK)
15305 return;
15306
15307 if (argvars[0].v_type == VAR_UNKNOWN)
15308 tp = curtab;
15309 else
15310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015311 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015312 if (tp == NULL)
15313 return;
15314 }
15315
15316 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15317}
15318
15319/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015320 * "winline()" function
15321 */
15322 static void
15323f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15324{
15325 validate_cursor();
15326 rettv->vval.v_number = curwin->w_wrow + 1;
15327}
15328
15329/*
15330 * "winnr()" function
15331 */
15332 static void
15333f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15334{
15335 int nr = 1;
15336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015337 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015338 rettv->vval.v_number = nr;
15339}
15340
15341/*
15342 * "winrestcmd()" function
15343 */
15344 static void
15345f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15346{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015347 win_T *wp;
15348 int winnr = 1;
15349 garray_T ga;
15350 char_u buf[50];
15351
15352 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015353 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015354 {
15355 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15356 ga_concat(&ga, buf);
15357 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15358 ga_concat(&ga, buf);
15359 ++winnr;
15360 }
15361 ga_append(&ga, NUL);
15362
15363 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015364 rettv->v_type = VAR_STRING;
15365}
15366
15367/*
15368 * "winrestview()" function
15369 */
15370 static void
15371f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15372{
15373 dict_T *dict;
15374
15375 if (argvars[0].v_type != VAR_DICT
15376 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015377 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015378 else
15379 {
15380 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015381 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015382 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015383 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015384 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015385 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15387 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015388 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015389 curwin->w_set_curswant = FALSE;
15390 }
15391
15392 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015393 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015394#ifdef FEAT_DIFF
15395 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015396 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015397#endif
15398 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015399 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015400 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015401 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015402
15403 check_cursor();
15404 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015405 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015406 changed_window_setting();
15407
15408 if (curwin->w_topline <= 0)
15409 curwin->w_topline = 1;
15410 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15411 curwin->w_topline = curbuf->b_ml.ml_line_count;
15412#ifdef FEAT_DIFF
15413 check_topfill(curwin, TRUE);
15414#endif
15415 }
15416}
15417
15418/*
15419 * "winsaveview()" function
15420 */
15421 static void
15422f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15423{
15424 dict_T *dict;
15425
15426 if (rettv_dict_alloc(rettv) == FAIL)
15427 return;
15428 dict = rettv->vval.v_dict;
15429
Bram Moolenaare0be1672018-07-08 16:50:37 +020015430 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15431 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015432 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015433 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015434 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015435
Bram Moolenaare0be1672018-07-08 16:50:37 +020015436 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015437#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015438 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015439#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015440 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15441 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015442}
15443
15444/*
15445 * "winwidth(nr)" function
15446 */
15447 static void
15448f_winwidth(typval_T *argvars, typval_T *rettv)
15449{
15450 win_T *wp;
15451
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015452 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015453 if (wp == NULL)
15454 rettv->vval.v_number = -1;
15455 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015456 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015457}
15458
15459/*
15460 * "wordcount()" function
15461 */
15462 static void
15463f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15464{
15465 if (rettv_dict_alloc(rettv) == FAIL)
15466 return;
15467 cursor_pos_info(rettv->vval.v_dict);
15468}
15469
15470/*
15471 * "writefile()" function
15472 */
15473 static void
15474f_writefile(typval_T *argvars, typval_T *rettv)
15475{
15476 int binary = FALSE;
15477 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015478#ifdef HAVE_FSYNC
15479 int do_fsync = p_fs;
15480#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015481 char_u *fname;
15482 FILE *fd;
15483 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015484 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015485 list_T *list = NULL;
15486 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015487
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015488 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015489 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015490 return;
15491
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015492 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015493 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015494 list = argvars[0].vval.v_list;
15495 if (list == NULL)
15496 return;
15497 for (li = list->lv_first; li != NULL; li = li->li_next)
15498 if (tv_get_string_chk(&li->li_tv) == NULL)
15499 return;
15500 }
15501 else if (argvars[0].v_type == VAR_BLOB)
15502 {
15503 blob = argvars[0].vval.v_blob;
15504 if (blob == NULL)
15505 return;
15506 }
15507 else
15508 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015509 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015510 return;
15511 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015512
15513 if (argvars[2].v_type != VAR_UNKNOWN)
15514 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015515 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015516
15517 if (arg2 == NULL)
15518 return;
15519 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015520 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015521 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015522 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015523#ifdef HAVE_FSYNC
15524 if (vim_strchr(arg2, 's') != NULL)
15525 do_fsync = TRUE;
15526 else if (vim_strchr(arg2, 'S') != NULL)
15527 do_fsync = FALSE;
15528#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015529 }
15530
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015531 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015532 if (fname == NULL)
15533 return;
15534
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015535 /* Always open the file in binary mode, library functions have a mind of
15536 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015537 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15538 append ? APPENDBIN : WRITEBIN)) == NULL)
15539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015540 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015541 ret = -1;
15542 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015543 else if (blob)
15544 {
15545 if (write_blob(fd, blob) == FAIL)
15546 ret = -1;
15547#ifdef HAVE_FSYNC
15548 else if (do_fsync)
15549 // Ignore the error, the user wouldn't know what to do about it.
15550 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015551 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015552#endif
15553 fclose(fd);
15554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015555 else
15556 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015557 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015558 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015559#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015560 else if (do_fsync)
15561 /* Ignore the error, the user wouldn't know what to do about it.
15562 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015563 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015565 fclose(fd);
15566 }
15567
15568 rettv->vval.v_number = ret;
15569}
15570
15571/*
15572 * "xor(expr, expr)" function
15573 */
15574 static void
15575f_xor(typval_T *argvars, typval_T *rettv)
15576{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015577 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15578 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015579}
15580
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015581#endif /* FEAT_EVAL */