blob: 0ef76095f404bd2d7ab580e3dd7d8537e47962e9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalfunc.c: Builtin functions
12 */
13#define USING_FLOAT_STUFF
14
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaard0573012017-10-28 21:11:06 +020027#ifdef MACOS_X
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020028# include <time.h> /* for time_t */
29#endif
30
31static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaarbf821bc2019-01-23 21:15:02 +010032static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020033static char *e_stringreq = N_("E928: String required");
Bram Moolenaaraff74912019-03-30 18:11:49 +010034static char *e_invalwindow = N_("E957: Invalid window number");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020035
36#ifdef FEAT_FLOAT
37static void f_abs(typval_T *argvars, typval_T *rettv);
38static void f_acos(typval_T *argvars, typval_T *rettv);
39#endif
40static void f_add(typval_T *argvars, typval_T *rettv);
41static void f_and(typval_T *argvars, typval_T *rettv);
42static void f_append(typval_T *argvars, typval_T *rettv);
Bram Moolenaarca851592018-06-06 21:04:07 +020043static void f_appendbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020044static void f_argc(typval_T *argvars, typval_T *rettv);
45static void f_argidx(typval_T *argvars, typval_T *rettv);
46static void f_arglistid(typval_T *argvars, typval_T *rettv);
47static void f_argv(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +010048static void f_assert_beeps(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020049static void f_assert_equal(typval_T *argvars, typval_T *rettv);
Bram Moolenaard96ff162018-02-18 22:13:29 +010050static void f_assert_equalfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020051static void f_assert_exception(typval_T *argvars, typval_T *rettv);
52static void f_assert_fails(typval_T *argvars, typval_T *rettv);
53static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaar61c04492016-07-23 15:35:35 +020054static void f_assert_inrange(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020055static void f_assert_match(typval_T *argvars, typval_T *rettv);
56static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
57static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar42205552017-03-18 19:42:22 +010058static void f_assert_report(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020059static void f_assert_true(typval_T *argvars, typval_T *rettv);
60#ifdef FEAT_FLOAT
61static void f_asin(typval_T *argvars, typval_T *rettv);
62static void f_atan(typval_T *argvars, typval_T *rettv);
63static void f_atan2(typval_T *argvars, typval_T *rettv);
64#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010065#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +020066static void f_balloon_gettext(typval_T *argvars, typval_T *rettv);
Bram Moolenaar59716a22017-03-01 20:32:44 +010067static void f_balloon_show(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010068# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +010069static void f_balloon_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar669a8282017-11-19 20:13:05 +010070# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +010071#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020072static void f_browse(typval_T *argvars, typval_T *rettv);
73static void f_browsedir(typval_T *argvars, typval_T *rettv);
74static void f_bufexists(typval_T *argvars, typval_T *rettv);
75static void f_buflisted(typval_T *argvars, typval_T *rettv);
76static void f_bufloaded(typval_T *argvars, typval_T *rettv);
77static void f_bufname(typval_T *argvars, typval_T *rettv);
78static void f_bufnr(typval_T *argvars, typval_T *rettv);
79static void f_bufwinid(typval_T *argvars, typval_T *rettv);
80static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
81static void f_byte2line(typval_T *argvars, typval_T *rettv);
82static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
83static void f_byteidx(typval_T *argvars, typval_T *rettv);
84static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
85static void f_call(typval_T *argvars, typval_T *rettv);
86#ifdef FEAT_FLOAT
87static void f_ceil(typval_T *argvars, typval_T *rettv);
88#endif
89#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +010090static void f_ch_canread(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020091static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0874a832016-09-01 15:11:51 +020092static void f_ch_close_in(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020093static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
94static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
95static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
96static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
97static void f_ch_info(typval_T *argvars, typval_T *rettv);
98static void f_ch_log(typval_T *argvars, typval_T *rettv);
99static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
100static void f_ch_open(typval_T *argvars, typval_T *rettv);
101static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100102static void f_ch_readblob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200103static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
104static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
105static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
106static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
107static void f_ch_status(typval_T *argvars, typval_T *rettv);
108#endif
109static void f_changenr(typval_T *argvars, typval_T *rettv);
110static void f_char2nr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200111static void f_chdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200112static void f_cindent(typval_T *argvars, typval_T *rettv);
113static void f_clearmatches(typval_T *argvars, typval_T *rettv);
114static void f_col(typval_T *argvars, typval_T *rettv);
115#if defined(FEAT_INS_EXPAND)
116static void f_complete(typval_T *argvars, typval_T *rettv);
117static void f_complete_add(typval_T *argvars, typval_T *rettv);
118static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfd133322019-03-29 12:20:27 +0100119static void f_complete_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200120#endif
121static void f_confirm(typval_T *argvars, typval_T *rettv);
122static void f_copy(typval_T *argvars, typval_T *rettv);
123#ifdef FEAT_FLOAT
124static void f_cos(typval_T *argvars, typval_T *rettv);
125static void f_cosh(typval_T *argvars, typval_T *rettv);
126#endif
127static void f_count(typval_T *argvars, typval_T *rettv);
128static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
129static void f_cursor(typval_T *argsvars, typval_T *rettv);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100130#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200131static void f_debugbreak(typval_T *argvars, typval_T *rettv);
132#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200133static void f_deepcopy(typval_T *argvars, typval_T *rettv);
134static void f_delete(typval_T *argvars, typval_T *rettv);
Bram Moolenaard79a2622018-06-07 18:17:46 +0200135static void f_deletebufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200136static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139static void f_empty(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200140static void f_environ(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200141static void f_escape(typval_T *argvars, typval_T *rettv);
142static void f_eval(typval_T *argvars, typval_T *rettv);
143static void f_eventhandler(typval_T *argvars, typval_T *rettv);
144static void f_executable(typval_T *argvars, typval_T *rettv);
145static void f_execute(typval_T *argvars, typval_T *rettv);
146static void f_exepath(typval_T *argvars, typval_T *rettv);
147static void f_exists(typval_T *argvars, typval_T *rettv);
148#ifdef FEAT_FLOAT
149static void f_exp(typval_T *argvars, typval_T *rettv);
150#endif
151static void f_expand(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80dad482019-06-09 17:22:31 +0200152static void f_expandcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200153static void f_extend(typval_T *argvars, typval_T *rettv);
154static void f_feedkeys(typval_T *argvars, typval_T *rettv);
155static void f_filereadable(typval_T *argvars, typval_T *rettv);
156static void f_filewritable(typval_T *argvars, typval_T *rettv);
157static void f_filter(typval_T *argvars, typval_T *rettv);
158static void f_finddir(typval_T *argvars, typval_T *rettv);
159static void f_findfile(typval_T *argvars, typval_T *rettv);
160#ifdef FEAT_FLOAT
161static void f_float2nr(typval_T *argvars, typval_T *rettv);
162static void f_floor(typval_T *argvars, typval_T *rettv);
163static void f_fmod(typval_T *argvars, typval_T *rettv);
164#endif
165static void f_fnameescape(typval_T *argvars, typval_T *rettv);
166static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
167static void f_foldclosed(typval_T *argvars, typval_T *rettv);
168static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
169static void f_foldlevel(typval_T *argvars, typval_T *rettv);
170static void f_foldtext(typval_T *argvars, typval_T *rettv);
171static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
172static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200173static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200174static void f_function(typval_T *argvars, typval_T *rettv);
175static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
176static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200177static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200178static void f_getbufline(typval_T *argvars, typval_T *rettv);
179static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100180static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200181static void f_getchar(typval_T *argvars, typval_T *rettv);
182static void f_getcharmod(typval_T *argvars, typval_T *rettv);
183static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
184static void f_getcmdline(typval_T *argvars, typval_T *rettv);
185#if defined(FEAT_CMDL_COMPL)
186static void f_getcompletion(typval_T *argvars, typval_T *rettv);
187#endif
188static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
189static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
190static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
191static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200192static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200193static void f_getfontname(typval_T *argvars, typval_T *rettv);
194static void f_getfperm(typval_T *argvars, typval_T *rettv);
195static void f_getfsize(typval_T *argvars, typval_T *rettv);
196static void f_getftime(typval_T *argvars, typval_T *rettv);
197static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100198static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200199static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200200static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200201static void f_getmatches(typval_T *argvars, typval_T *rettv);
202static void f_getpid(typval_T *argvars, typval_T *rettv);
203static void f_getcurpos(typval_T *argvars, typval_T *rettv);
204static void f_getpos(typval_T *argvars, typval_T *rettv);
205static void f_getqflist(typval_T *argvars, typval_T *rettv);
206static void f_getreg(typval_T *argvars, typval_T *rettv);
207static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200208static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200209static void f_gettabvar(typval_T *argvars, typval_T *rettv);
210static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100211static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200212static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100213static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200214static void f_getwinposx(typval_T *argvars, typval_T *rettv);
215static void f_getwinposy(typval_T *argvars, typval_T *rettv);
216static void f_getwinvar(typval_T *argvars, typval_T *rettv);
217static void f_glob(typval_T *argvars, typval_T *rettv);
218static void f_globpath(typval_T *argvars, typval_T *rettv);
219static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
220static void f_has(typval_T *argvars, typval_T *rettv);
221static void f_has_key(typval_T *argvars, typval_T *rettv);
222static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
223static void f_hasmapto(typval_T *argvars, typval_T *rettv);
224static void f_histadd(typval_T *argvars, typval_T *rettv);
225static void f_histdel(typval_T *argvars, typval_T *rettv);
226static void f_histget(typval_T *argvars, typval_T *rettv);
227static void f_histnr(typval_T *argvars, typval_T *rettv);
228static void f_hlID(typval_T *argvars, typval_T *rettv);
229static void f_hlexists(typval_T *argvars, typval_T *rettv);
230static void f_hostname(typval_T *argvars, typval_T *rettv);
231static void f_iconv(typval_T *argvars, typval_T *rettv);
232static void f_indent(typval_T *argvars, typval_T *rettv);
233static void f_index(typval_T *argvars, typval_T *rettv);
234static void f_input(typval_T *argvars, typval_T *rettv);
235static void f_inputdialog(typval_T *argvars, typval_T *rettv);
236static void f_inputlist(typval_T *argvars, typval_T *rettv);
237static void f_inputrestore(typval_T *argvars, typval_T *rettv);
238static void f_inputsave(typval_T *argvars, typval_T *rettv);
239static void f_inputsecret(typval_T *argvars, typval_T *rettv);
240static void f_insert(typval_T *argvars, typval_T *rettv);
241static void f_invert(typval_T *argvars, typval_T *rettv);
242static void f_isdirectory(typval_T *argvars, typval_T *rettv);
243static void f_islocked(typval_T *argvars, typval_T *rettv);
244#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200245static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200246static void f_isnan(typval_T *argvars, typval_T *rettv);
247#endif
248static void f_items(typval_T *argvars, typval_T *rettv);
249#ifdef FEAT_JOB_CHANNEL
250static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
251static void f_job_info(typval_T *argvars, typval_T *rettv);
252static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
253static void f_job_start(typval_T *argvars, typval_T *rettv);
254static void f_job_stop(typval_T *argvars, typval_T *rettv);
255static void f_job_status(typval_T *argvars, typval_T *rettv);
256#endif
257static void f_join(typval_T *argvars, typval_T *rettv);
258static void f_js_decode(typval_T *argvars, typval_T *rettv);
259static void f_js_encode(typval_T *argvars, typval_T *rettv);
260static void f_json_decode(typval_T *argvars, typval_T *rettv);
261static void f_json_encode(typval_T *argvars, typval_T *rettv);
262static void f_keys(typval_T *argvars, typval_T *rettv);
263static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
264static void f_len(typval_T *argvars, typval_T *rettv);
265static void f_libcall(typval_T *argvars, typval_T *rettv);
266static void f_libcallnr(typval_T *argvars, typval_T *rettv);
267static void f_line(typval_T *argvars, typval_T *rettv);
268static void f_line2byte(typval_T *argvars, typval_T *rettv);
269static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200270static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200271static void f_localtime(typval_T *argvars, typval_T *rettv);
272#ifdef FEAT_FLOAT
273static void f_log(typval_T *argvars, typval_T *rettv);
274static void f_log10(typval_T *argvars, typval_T *rettv);
275#endif
276#ifdef FEAT_LUA
277static void f_luaeval(typval_T *argvars, typval_T *rettv);
278#endif
279static void f_map(typval_T *argvars, typval_T *rettv);
280static void f_maparg(typval_T *argvars, typval_T *rettv);
281static void f_mapcheck(typval_T *argvars, typval_T *rettv);
282static void f_match(typval_T *argvars, typval_T *rettv);
283static void f_matchadd(typval_T *argvars, typval_T *rettv);
284static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
285static void f_matcharg(typval_T *argvars, typval_T *rettv);
286static void f_matchdelete(typval_T *argvars, typval_T *rettv);
287static void f_matchend(typval_T *argvars, typval_T *rettv);
288static void f_matchlist(typval_T *argvars, typval_T *rettv);
289static void f_matchstr(typval_T *argvars, typval_T *rettv);
290static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
291static void f_max(typval_T *argvars, typval_T *rettv);
292static void f_min(typval_T *argvars, typval_T *rettv);
293#ifdef vim_mkdir
294static void f_mkdir(typval_T *argvars, typval_T *rettv);
295#endif
296static void f_mode(typval_T *argvars, typval_T *rettv);
297#ifdef FEAT_MZSCHEME
298static void f_mzeval(typval_T *argvars, typval_T *rettv);
299#endif
300static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
301static void f_nr2char(typval_T *argvars, typval_T *rettv);
302static void f_or(typval_T *argvars, typval_T *rettv);
303static void f_pathshorten(typval_T *argvars, typval_T *rettv);
304#ifdef FEAT_PERL
305static void f_perleval(typval_T *argvars, typval_T *rettv);
306#endif
307#ifdef FEAT_FLOAT
308static void f_pow(typval_T *argvars, typval_T *rettv);
309#endif
310static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
311static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200312#ifdef FEAT_JOB_CHANNEL
313static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200314static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200315static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
316#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200317static void f_pumvisible(typval_T *argvars, typval_T *rettv);
318#ifdef FEAT_PYTHON3
319static void f_py3eval(typval_T *argvars, typval_T *rettv);
320#endif
321#ifdef FEAT_PYTHON
322static void f_pyeval(typval_T *argvars, typval_T *rettv);
323#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100324#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
325static void f_pyxeval(typval_T *argvars, typval_T *rettv);
326#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200327static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200328static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200329static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200330static void f_reg_executing(typval_T *argvars, typval_T *rettv);
331static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200332static void f_reltime(typval_T *argvars, typval_T *rettv);
333#ifdef FEAT_FLOAT
334static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
335#endif
336static void f_reltimestr(typval_T *argvars, typval_T *rettv);
337static void f_remote_expr(typval_T *argvars, typval_T *rettv);
338static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
339static void f_remote_peek(typval_T *argvars, typval_T *rettv);
340static void f_remote_read(typval_T *argvars, typval_T *rettv);
341static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100342static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200343static void f_remove(typval_T *argvars, typval_T *rettv);
344static void f_rename(typval_T *argvars, typval_T *rettv);
345static void f_repeat(typval_T *argvars, typval_T *rettv);
346static void f_resolve(typval_T *argvars, typval_T *rettv);
347static void f_reverse(typval_T *argvars, typval_T *rettv);
348#ifdef FEAT_FLOAT
349static void f_round(typval_T *argvars, typval_T *rettv);
350#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100351#ifdef FEAT_RUBY
352static void f_rubyeval(typval_T *argvars, typval_T *rettv);
353#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200354static void f_screenattr(typval_T *argvars, typval_T *rettv);
355static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100356static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200357static void f_screencol(typval_T *argvars, typval_T *rettv);
358static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100359static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200360static void f_search(typval_T *argvars, typval_T *rettv);
361static void f_searchdecl(typval_T *argvars, typval_T *rettv);
362static void f_searchpair(typval_T *argvars, typval_T *rettv);
363static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
364static void f_searchpos(typval_T *argvars, typval_T *rettv);
365static void f_server2client(typval_T *argvars, typval_T *rettv);
366static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200367static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200368static void f_setbufvar(typval_T *argvars, typval_T *rettv);
369static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
370static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200371static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200372static void f_setfperm(typval_T *argvars, typval_T *rettv);
373static void f_setline(typval_T *argvars, typval_T *rettv);
374static void f_setloclist(typval_T *argvars, typval_T *rettv);
375static void f_setmatches(typval_T *argvars, typval_T *rettv);
376static void f_setpos(typval_T *argvars, typval_T *rettv);
377static void f_setqflist(typval_T *argvars, typval_T *rettv);
378static void f_setreg(typval_T *argvars, typval_T *rettv);
379static void f_settabvar(typval_T *argvars, typval_T *rettv);
380static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100381static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200382static void f_setwinvar(typval_T *argvars, typval_T *rettv);
383#ifdef FEAT_CRYPT
384static void f_sha256(typval_T *argvars, typval_T *rettv);
385#endif /* FEAT_CRYPT */
386static void f_shellescape(typval_T *argvars, typval_T *rettv);
387static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100388#ifdef FEAT_SIGNS
389static void f_sign_define(typval_T *argvars, typval_T *rettv);
390static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
391static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100392static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100393static void f_sign_place(typval_T *argvars, typval_T *rettv);
394static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
395static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200397static void f_simplify(typval_T *argvars, typval_T *rettv);
398#ifdef FEAT_FLOAT
399static void f_sin(typval_T *argvars, typval_T *rettv);
400static void f_sinh(typval_T *argvars, typval_T *rettv);
401#endif
402static void f_sort(typval_T *argvars, typval_T *rettv);
403static void f_soundfold(typval_T *argvars, typval_T *rettv);
404static void f_spellbadword(typval_T *argvars, typval_T *rettv);
405static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
406static void f_split(typval_T *argvars, typval_T *rettv);
407#ifdef FEAT_FLOAT
408static void f_sqrt(typval_T *argvars, typval_T *rettv);
409static void f_str2float(typval_T *argvars, typval_T *rettv);
410#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200411static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200412static void f_str2nr(typval_T *argvars, typval_T *rettv);
413static void f_strchars(typval_T *argvars, typval_T *rettv);
414#ifdef HAVE_STRFTIME
415static void f_strftime(typval_T *argvars, typval_T *rettv);
416#endif
417static void f_strgetchar(typval_T *argvars, typval_T *rettv);
418static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200419static void f_strlen(typval_T *argvars, typval_T *rettv);
420static void f_strcharpart(typval_T *argvars, typval_T *rettv);
421static void f_strpart(typval_T *argvars, typval_T *rettv);
422static void f_strridx(typval_T *argvars, typval_T *rettv);
423static void f_strtrans(typval_T *argvars, typval_T *rettv);
424static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
425static void f_strwidth(typval_T *argvars, typval_T *rettv);
426static void f_submatch(typval_T *argvars, typval_T *rettv);
427static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200428static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200429static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200430static void f_synID(typval_T *argvars, typval_T *rettv);
431static void f_synIDattr(typval_T *argvars, typval_T *rettv);
432static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
433static void f_synstack(typval_T *argvars, typval_T *rettv);
434static void f_synconcealed(typval_T *argvars, typval_T *rettv);
435static void f_system(typval_T *argvars, typval_T *rettv);
436static void f_systemlist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
438static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
439static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
440static void f_taglist(typval_T *argvars, typval_T *rettv);
441static void f_tagfiles(typval_T *argvars, typval_T *rettv);
442static void f_tempname(typval_T *argvars, typval_T *rettv);
443static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
444static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200445static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200446static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200447static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100448static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100449static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200450static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100451static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100452static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200453#ifdef FEAT_JOB_CHANNEL
454static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
455#endif
456static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
457#ifdef FEAT_JOB_CHANNEL
458static void f_test_null_job(typval_T *argvars, typval_T *rettv);
459#endif
460static void f_test_null_list(typval_T *argvars, typval_T *rettv);
461static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
462static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200463#ifdef FEAT_GUI
464static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
465#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200466#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200467static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200469static void f_test_settime(typval_T *argvars, typval_T *rettv);
470#ifdef FEAT_FLOAT
471static void f_tan(typval_T *argvars, typval_T *rettv);
472static void f_tanh(typval_T *argvars, typval_T *rettv);
473#endif
474#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200475static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200476static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200477static void f_timer_start(typval_T *argvars, typval_T *rettv);
478static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200479static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200480#endif
481static void f_tolower(typval_T *argvars, typval_T *rettv);
482static void f_toupper(typval_T *argvars, typval_T *rettv);
483static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100484static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200485#ifdef FEAT_FLOAT
486static void f_trunc(typval_T *argvars, typval_T *rettv);
487#endif
488static void f_type(typval_T *argvars, typval_T *rettv);
489static void f_undofile(typval_T *argvars, typval_T *rettv);
490static void f_undotree(typval_T *argvars, typval_T *rettv);
491static void f_uniq(typval_T *argvars, typval_T *rettv);
492static void f_values(typval_T *argvars, typval_T *rettv);
493static void f_virtcol(typval_T *argvars, typval_T *rettv);
494static void f_visualmode(typval_T *argvars, typval_T *rettv);
495static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200496static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200497static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
498static void f_win_getid(typval_T *argvars, typval_T *rettv);
499static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
500static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
501static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100502static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200503static void f_winbufnr(typval_T *argvars, typval_T *rettv);
504static void f_wincol(typval_T *argvars, typval_T *rettv);
505static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200506static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200507static void f_winline(typval_T *argvars, typval_T *rettv);
508static void f_winnr(typval_T *argvars, typval_T *rettv);
509static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
510static void f_winrestview(typval_T *argvars, typval_T *rettv);
511static void f_winsaveview(typval_T *argvars, typval_T *rettv);
512static void f_winwidth(typval_T *argvars, typval_T *rettv);
513static void f_writefile(typval_T *argvars, typval_T *rettv);
514static void f_wordcount(typval_T *argvars, typval_T *rettv);
515static void f_xor(typval_T *argvars, typval_T *rettv);
516
517/*
518 * Array with names and number of arguments of all internal functions
519 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
520 */
521static struct fst
522{
523 char *f_name; /* function name */
524 char f_min_argc; /* minimal number of arguments */
525 char f_max_argc; /* maximal number of arguments */
526 void (*f_func)(typval_T *args, typval_T *rvar);
527 /* implementation of function */
528} functions[] =
529{
530#ifdef FEAT_FLOAT
531 {"abs", 1, 1, f_abs},
532 {"acos", 1, 1, f_acos}, /* WJMc */
533#endif
534 {"add", 2, 2, f_add},
535 {"and", 2, 2, f_and},
536 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200537 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200538 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200539 {"argidx", 0, 0, f_argidx},
540 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200541 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200542#ifdef FEAT_FLOAT
543 {"asin", 1, 1, f_asin}, /* WJMc */
544#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100545 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100547 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200549 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100551 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200552 {"assert_match", 2, 3, f_assert_match},
553 {"assert_notequal", 2, 3, f_assert_notequal},
554 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100555 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200556 {"assert_true", 1, 2, f_assert_true},
557#ifdef FEAT_FLOAT
558 {"atan", 1, 1, f_atan},
559 {"atan2", 2, 2, f_atan2},
560#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200562 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100563 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100565 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100566# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100567#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200568 {"browse", 4, 4, f_browse},
569 {"browsedir", 2, 2, f_browsedir},
570 {"bufexists", 1, 1, f_bufexists},
571 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
572 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
573 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
574 {"buflisted", 1, 1, f_buflisted},
575 {"bufloaded", 1, 1, f_bufloaded},
576 {"bufname", 1, 1, f_bufname},
577 {"bufnr", 1, 2, f_bufnr},
578 {"bufwinid", 1, 1, f_bufwinid},
579 {"bufwinnr", 1, 1, f_bufwinnr},
580 {"byte2line", 1, 1, f_byte2line},
581 {"byteidx", 2, 2, f_byteidx},
582 {"byteidxcomp", 2, 2, f_byteidxcomp},
583 {"call", 2, 3, f_call},
584#ifdef FEAT_FLOAT
585 {"ceil", 1, 1, f_ceil},
586#endif
587#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100588 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200590 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200591 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
592 {"ch_evalraw", 2, 3, f_ch_evalraw},
593 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
594 {"ch_getjob", 1, 1, f_ch_getjob},
595 {"ch_info", 1, 1, f_ch_info},
596 {"ch_log", 1, 2, f_ch_log},
597 {"ch_logfile", 1, 2, f_ch_logfile},
598 {"ch_open", 1, 2, f_ch_open},
599 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100600 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200601 {"ch_readraw", 1, 2, f_ch_readraw},
602 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
603 {"ch_sendraw", 2, 3, f_ch_sendraw},
604 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200605 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200606#endif
607 {"changenr", 0, 0, f_changenr},
608 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200609 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100611 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200612 {"col", 1, 1, f_col},
613#if defined(FEAT_INS_EXPAND)
614 {"complete", 2, 2, f_complete},
615 {"complete_add", 1, 1, f_complete_add},
616 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100617 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200618#endif
619 {"confirm", 1, 4, f_confirm},
620 {"copy", 1, 1, f_copy},
621#ifdef FEAT_FLOAT
622 {"cos", 1, 1, f_cos},
623 {"cosh", 1, 1, f_cosh},
624#endif
625 {"count", 2, 4, f_count},
626 {"cscope_connection",0,3, f_cscope_connection},
627 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100628#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200629 {"debugbreak", 1, 1, f_debugbreak},
630#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200631 {"deepcopy", 1, 2, f_deepcopy},
632 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200633 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200634 {"did_filetype", 0, 0, f_did_filetype},
635 {"diff_filler", 1, 1, f_diff_filler},
636 {"diff_hlID", 2, 2, f_diff_hlID},
637 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200638 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200639 {"escape", 2, 2, f_escape},
640 {"eval", 1, 1, f_eval},
641 {"eventhandler", 0, 0, f_eventhandler},
642 {"executable", 1, 1, f_executable},
643 {"execute", 1, 2, f_execute},
644 {"exepath", 1, 1, f_exepath},
645 {"exists", 1, 1, f_exists},
646#ifdef FEAT_FLOAT
647 {"exp", 1, 1, f_exp},
648#endif
649 {"expand", 1, 3, f_expand},
Bram Moolenaar80dad482019-06-09 17:22:31 +0200650 {"expandcmd", 1, 1, f_expandcmd},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200651 {"extend", 2, 3, f_extend},
652 {"feedkeys", 1, 2, f_feedkeys},
653 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
654 {"filereadable", 1, 1, f_filereadable},
655 {"filewritable", 1, 1, f_filewritable},
656 {"filter", 2, 2, f_filter},
657 {"finddir", 1, 3, f_finddir},
658 {"findfile", 1, 3, f_findfile},
659#ifdef FEAT_FLOAT
660 {"float2nr", 1, 1, f_float2nr},
661 {"floor", 1, 1, f_floor},
662 {"fmod", 2, 2, f_fmod},
663#endif
664 {"fnameescape", 1, 1, f_fnameescape},
665 {"fnamemodify", 2, 2, f_fnamemodify},
666 {"foldclosed", 1, 1, f_foldclosed},
667 {"foldclosedend", 1, 1, f_foldclosedend},
668 {"foldlevel", 1, 1, f_foldlevel},
669 {"foldtext", 0, 0, f_foldtext},
670 {"foldtextresult", 1, 1, f_foldtextresult},
671 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200672 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200673 {"function", 1, 3, f_function},
674 {"garbagecollect", 0, 1, f_garbagecollect},
675 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200676 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getbufline", 2, 3, f_getbufline},
678 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100679 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200680 {"getchar", 0, 1, f_getchar},
681 {"getcharmod", 0, 0, f_getcharmod},
682 {"getcharsearch", 0, 0, f_getcharsearch},
683 {"getcmdline", 0, 0, f_getcmdline},
684 {"getcmdpos", 0, 0, f_getcmdpos},
685 {"getcmdtype", 0, 0, f_getcmdtype},
686 {"getcmdwintype", 0, 0, f_getcmdwintype},
687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200688 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200689#endif
690 {"getcurpos", 0, 0, f_getcurpos},
691 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200692 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200693 {"getfontname", 0, 1, f_getfontname},
694 {"getfperm", 1, 1, f_getfperm},
695 {"getfsize", 1, 1, f_getfsize},
696 {"getftime", 1, 1, f_getftime},
697 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100698 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200700 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100701 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getpid", 0, 0, f_getpid},
703 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200704 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"getreg", 0, 3, f_getreg},
706 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200707 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200708 {"gettabvar", 2, 3, f_gettabvar},
709 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100710 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200711 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100712 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200713 {"getwinposx", 0, 0, f_getwinposx},
714 {"getwinposy", 0, 0, f_getwinposy},
715 {"getwinvar", 2, 3, f_getwinvar},
716 {"glob", 1, 4, f_glob},
717 {"glob2regpat", 1, 1, f_glob2regpat},
718 {"globpath", 2, 5, f_globpath},
719 {"has", 1, 1, f_has},
720 {"has_key", 2, 2, f_has_key},
721 {"haslocaldir", 0, 2, f_haslocaldir},
722 {"hasmapto", 1, 3, f_hasmapto},
723 {"highlightID", 1, 1, f_hlID}, /* obsolete */
724 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
725 {"histadd", 2, 2, f_histadd},
726 {"histdel", 1, 2, f_histdel},
727 {"histget", 1, 2, f_histget},
728 {"histnr", 1, 1, f_histnr},
729 {"hlID", 1, 1, f_hlID},
730 {"hlexists", 1, 1, f_hlexists},
731 {"hostname", 0, 0, f_hostname},
732 {"iconv", 3, 3, f_iconv},
733 {"indent", 1, 1, f_indent},
734 {"index", 2, 4, f_index},
735 {"input", 1, 3, f_input},
736 {"inputdialog", 1, 3, f_inputdialog},
737 {"inputlist", 1, 1, f_inputlist},
738 {"inputrestore", 0, 0, f_inputrestore},
739 {"inputsave", 0, 0, f_inputsave},
740 {"inputsecret", 1, 2, f_inputsecret},
741 {"insert", 2, 3, f_insert},
742 {"invert", 1, 1, f_invert},
743 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200744#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
745 {"isinf", 1, 1, f_isinf},
746#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200747 {"islocked", 1, 1, f_islocked},
748#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
749 {"isnan", 1, 1, f_isnan},
750#endif
751 {"items", 1, 1, f_items},
752#ifdef FEAT_JOB_CHANNEL
753 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200754 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200755 {"job_setoptions", 2, 2, f_job_setoptions},
756 {"job_start", 1, 2, f_job_start},
757 {"job_status", 1, 1, f_job_status},
758 {"job_stop", 1, 2, f_job_stop},
759#endif
760 {"join", 1, 2, f_join},
761 {"js_decode", 1, 1, f_js_decode},
762 {"js_encode", 1, 1, f_js_encode},
763 {"json_decode", 1, 1, f_json_decode},
764 {"json_encode", 1, 1, f_json_encode},
765 {"keys", 1, 1, f_keys},
766 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
767 {"len", 1, 1, f_len},
768 {"libcall", 3, 3, f_libcall},
769 {"libcallnr", 3, 3, f_libcallnr},
770 {"line", 1, 1, f_line},
771 {"line2byte", 1, 1, f_line2byte},
772 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200773 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200775 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200776 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200777 {"localtime", 0, 0, f_localtime},
778#ifdef FEAT_FLOAT
779 {"log", 1, 1, f_log},
780 {"log10", 1, 1, f_log10},
781#endif
782#ifdef FEAT_LUA
783 {"luaeval", 1, 2, f_luaeval},
784#endif
785 {"map", 2, 2, f_map},
786 {"maparg", 1, 4, f_maparg},
787 {"mapcheck", 1, 3, f_mapcheck},
788 {"match", 2, 4, f_match},
789 {"matchadd", 2, 5, f_matchadd},
790 {"matchaddpos", 2, 5, f_matchaddpos},
791 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100792 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200793 {"matchend", 2, 4, f_matchend},
794 {"matchlist", 2, 4, f_matchlist},
795 {"matchstr", 2, 4, f_matchstr},
796 {"matchstrpos", 2, 4, f_matchstrpos},
797 {"max", 1, 1, f_max},
798 {"min", 1, 1, f_min},
799#ifdef vim_mkdir
800 {"mkdir", 1, 3, f_mkdir},
801#endif
802 {"mode", 0, 1, f_mode},
803#ifdef FEAT_MZSCHEME
804 {"mzeval", 1, 1, f_mzeval},
805#endif
806 {"nextnonblank", 1, 1, f_nextnonblank},
807 {"nr2char", 1, 2, f_nr2char},
808 {"or", 2, 2, f_or},
809 {"pathshorten", 1, 1, f_pathshorten},
810#ifdef FEAT_PERL
811 {"perleval", 1, 1, f_perleval},
812#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813#ifdef FEAT_TEXT_PROP
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200814 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200815 {"popup_clear", 0, 0, f_popup_clear},
Bram Moolenaar9eaac892019-06-01 22:49:29 +0200816 {"popup_close", 1, 2, f_popup_close},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200818 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200819 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200820 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200821 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar68d48f42019-06-12 22:42:41 +0200822 {"popup_notification", 2, 2, f_popup_notification},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200823 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200824#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200825#ifdef FEAT_FLOAT
826 {"pow", 2, 2, f_pow},
827#endif
828 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100829 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200830#ifdef FEAT_JOB_CHANNEL
831 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200832 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200833 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
834#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100835#ifdef FEAT_TEXT_PROP
836 {"prop_add", 3, 3, f_prop_add},
837 {"prop_clear", 1, 3, f_prop_clear},
838 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100839 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100840 {"prop_type_add", 2, 2, f_prop_type_add},
841 {"prop_type_change", 2, 2, f_prop_type_change},
842 {"prop_type_delete", 1, 2, f_prop_type_delete},
843 {"prop_type_get", 1, 2, f_prop_type_get},
844 {"prop_type_list", 0, 1, f_prop_type_list},
845#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846 {"pumvisible", 0, 0, f_pumvisible},
847#ifdef FEAT_PYTHON3
848 {"py3eval", 1, 1, f_py3eval},
849#endif
850#ifdef FEAT_PYTHON
851 {"pyeval", 1, 1, f_pyeval},
852#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100853#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
854 {"pyxeval", 1, 1, f_pyxeval},
855#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200856 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200857 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200858 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200859 {"reg_executing", 0, 0, f_reg_executing},
860 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"reltime", 0, 2, f_reltime},
862#ifdef FEAT_FLOAT
863 {"reltimefloat", 1, 1, f_reltimefloat},
864#endif
865 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100866 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200867 {"remote_foreground", 1, 1, f_remote_foreground},
868 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100869 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200870 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100871 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200872 {"remove", 2, 3, f_remove},
873 {"rename", 2, 2, f_rename},
874 {"repeat", 2, 2, f_repeat},
875 {"resolve", 1, 1, f_resolve},
876 {"reverse", 1, 1, f_reverse},
877#ifdef FEAT_FLOAT
878 {"round", 1, 1, f_round},
879#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100880#ifdef FEAT_RUBY
881 {"rubyeval", 1, 1, f_rubyeval},
882#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883 {"screenattr", 2, 2, f_screenattr},
884 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100885 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"screencol", 0, 0, f_screencol},
887 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100888 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889 {"search", 1, 4, f_search},
890 {"searchdecl", 1, 3, f_searchdecl},
891 {"searchpair", 3, 7, f_searchpair},
892 {"searchpairpos", 3, 7, f_searchpairpos},
893 {"searchpos", 1, 4, f_searchpos},
894 {"server2client", 2, 2, f_server2client},
895 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200896 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"setbufvar", 3, 3, f_setbufvar},
898 {"setcharsearch", 1, 1, f_setcharsearch},
899 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200900 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200901 {"setfperm", 2, 2, f_setfperm},
902 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200903 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100904 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200905 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200906 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"setreg", 2, 3, f_setreg},
908 {"settabvar", 3, 3, f_settabvar},
909 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100910 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911 {"setwinvar", 3, 3, f_setwinvar},
912#ifdef FEAT_CRYPT
913 {"sha256", 1, 1, f_sha256},
914#endif
915 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100916 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100917#ifdef FEAT_SIGNS
918 {"sign_define", 1, 2, f_sign_define},
919 {"sign_getdefined", 0, 1, f_sign_getdefined},
920 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100921 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100922 {"sign_place", 4, 5, f_sign_place},
923 {"sign_undefine", 0, 1, f_sign_undefine},
924 {"sign_unplace", 1, 2, f_sign_unplace},
925#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926 {"simplify", 1, 1, f_simplify},
927#ifdef FEAT_FLOAT
928 {"sin", 1, 1, f_sin},
929 {"sinh", 1, 1, f_sinh},
930#endif
931 {"sort", 1, 3, f_sort},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200932#ifdef FEAT_SOUND
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200933 {"sound_clear", 0, 0, f_sound_clear},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200934 {"sound_playevent", 1, 2, f_sound_playevent},
935 {"sound_playfile", 1, 2, f_sound_playfile},
936 {"sound_stop", 1, 1, f_sound_stop},
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200937#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200938 {"soundfold", 1, 1, f_soundfold},
939 {"spellbadword", 0, 1, f_spellbadword},
940 {"spellsuggest", 1, 3, f_spellsuggest},
941 {"split", 1, 3, f_split},
942#ifdef FEAT_FLOAT
943 {"sqrt", 1, 1, f_sqrt},
944 {"str2float", 1, 1, f_str2float},
945#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200946 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200947 {"str2nr", 1, 2, f_str2nr},
948 {"strcharpart", 2, 3, f_strcharpart},
949 {"strchars", 1, 2, f_strchars},
950 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
951#ifdef HAVE_STRFTIME
952 {"strftime", 1, 2, f_strftime},
953#endif
954 {"strgetchar", 2, 2, f_strgetchar},
955 {"stridx", 2, 3, f_stridx},
956 {"string", 1, 1, f_string},
957 {"strlen", 1, 1, f_strlen},
958 {"strpart", 2, 3, f_strpart},
959 {"strridx", 2, 3, f_strridx},
960 {"strtrans", 1, 1, f_strtrans},
961 {"strwidth", 1, 1, f_strwidth},
962 {"submatch", 1, 2, f_submatch},
963 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200964 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200965 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200966 {"synID", 3, 3, f_synID},
967 {"synIDattr", 2, 3, f_synIDattr},
968 {"synIDtrans", 1, 1, f_synIDtrans},
969 {"synconcealed", 2, 2, f_synconcealed},
970 {"synstack", 2, 2, f_synstack},
971 {"system", 1, 2, f_system},
972 {"systemlist", 1, 2, f_systemlist},
973 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
974 {"tabpagenr", 0, 1, f_tabpagenr},
975 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
976 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100977 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200978#ifdef FEAT_FLOAT
979 {"tan", 1, 1, f_tan},
980 {"tanh", 1, 1, f_tanh},
981#endif
982 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100984 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
985 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100986 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200987 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200988# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
989 {"term_getansicolors", 1, 1, f_term_getansicolors},
990# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200991 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200992 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200993 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200994 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200995 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200997 {"term_getstatus", 1, 1, f_term_getstatus},
998 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200999 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001000 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +02001001 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001002 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001003# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1004 {"term_setansicolors", 2, 2, f_term_setansicolors},
1005# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001006 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01001007 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +02001008 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001009 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001010 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001011#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001012 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1013 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001014 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001015 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001016 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001017 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001018 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001019#ifdef FEAT_JOB_CHANNEL
1020 {"test_null_channel", 0, 0, f_test_null_channel},
1021#endif
1022 {"test_null_dict", 0, 0, f_test_null_dict},
1023#ifdef FEAT_JOB_CHANNEL
1024 {"test_null_job", 0, 0, f_test_null_job},
1025#endif
1026 {"test_null_list", 0, 0, f_test_null_list},
1027 {"test_null_partial", 0, 0, f_test_null_partial},
1028 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001029 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001030 {"test_override", 2, 2, f_test_override},
1031 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001032#ifdef FEAT_GUI
1033 {"test_scrollbar", 3, 3, f_test_scrollbar},
1034#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001035#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001036 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001037#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038 {"test_settime", 1, 1, f_test_settime},
1039#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001040 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001041 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001042 {"timer_start", 2, 3, f_timer_start},
1043 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001044 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001045#endif
1046 {"tolower", 1, 1, f_tolower},
1047 {"toupper", 1, 1, f_toupper},
1048 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001049 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001050#ifdef FEAT_FLOAT
1051 {"trunc", 1, 1, f_trunc},
1052#endif
1053 {"type", 1, 1, f_type},
1054 {"undofile", 1, 1, f_undofile},
1055 {"undotree", 0, 0, f_undotree},
1056 {"uniq", 1, 3, f_uniq},
1057 {"values", 1, 1, f_values},
1058 {"virtcol", 1, 1, f_virtcol},
1059 {"visualmode", 0, 1, f_visualmode},
1060 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001061 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001062 {"win_findbuf", 1, 1, f_win_findbuf},
1063 {"win_getid", 0, 2, f_win_getid},
1064 {"win_gotoid", 1, 1, f_win_gotoid},
1065 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1066 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001067 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001068 {"winbufnr", 1, 1, f_winbufnr},
1069 {"wincol", 0, 0, f_wincol},
1070 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001071 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001072 {"winline", 0, 0, f_winline},
1073 {"winnr", 0, 1, f_winnr},
1074 {"winrestcmd", 0, 0, f_winrestcmd},
1075 {"winrestview", 1, 1, f_winrestview},
1076 {"winsaveview", 0, 0, f_winsaveview},
1077 {"winwidth", 1, 1, f_winwidth},
1078 {"wordcount", 0, 0, f_wordcount},
1079 {"writefile", 2, 3, f_writefile},
1080 {"xor", 2, 2, f_xor},
1081};
1082
1083#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1084
1085/*
1086 * Function given to ExpandGeneric() to obtain the list of internal
1087 * or user defined function names.
1088 */
1089 char_u *
1090get_function_name(expand_T *xp, int idx)
1091{
1092 static int intidx = -1;
1093 char_u *name;
1094
1095 if (idx == 0)
1096 intidx = -1;
1097 if (intidx < 0)
1098 {
1099 name = get_user_func_name(xp, idx);
1100 if (name != NULL)
1101 return name;
1102 }
1103 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1104 {
1105 STRCPY(IObuff, functions[intidx].f_name);
1106 STRCAT(IObuff, "(");
1107 if (functions[intidx].f_max_argc == 0)
1108 STRCAT(IObuff, ")");
1109 return IObuff;
1110 }
1111
1112 return NULL;
1113}
1114
1115/*
1116 * Function given to ExpandGeneric() to obtain the list of internal or
1117 * user defined variable or function names.
1118 */
1119 char_u *
1120get_expr_name(expand_T *xp, int idx)
1121{
1122 static int intidx = -1;
1123 char_u *name;
1124
1125 if (idx == 0)
1126 intidx = -1;
1127 if (intidx < 0)
1128 {
1129 name = get_function_name(xp, idx);
1130 if (name != NULL)
1131 return name;
1132 }
1133 return get_user_var_name(xp, ++intidx);
1134}
1135
1136#endif /* FEAT_CMDL_COMPL */
1137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001138/*
1139 * Find internal function in table above.
1140 * Return index, or -1 if not found
1141 */
1142 int
1143find_internal_func(
1144 char_u *name) /* name of the function */
1145{
1146 int first = 0;
1147 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1148 int cmp;
1149 int x;
1150
1151 /*
1152 * Find the function name in the table. Binary search.
1153 */
1154 while (first <= last)
1155 {
1156 x = first + ((unsigned)(last - first) >> 1);
1157 cmp = STRCMP(name, functions[x].f_name);
1158 if (cmp < 0)
1159 last = x - 1;
1160 else if (cmp > 0)
1161 first = x + 1;
1162 else
1163 return x;
1164 }
1165 return -1;
1166}
1167
1168 int
1169call_internal_func(
1170 char_u *name,
1171 int argcount,
1172 typval_T *argvars,
1173 typval_T *rettv)
1174{
1175 int i;
1176
1177 i = find_internal_func(name);
1178 if (i < 0)
1179 return ERROR_UNKNOWN;
1180 if (argcount < functions[i].f_min_argc)
1181 return ERROR_TOOFEW;
1182 if (argcount > functions[i].f_max_argc)
1183 return ERROR_TOOMANY;
1184 argvars[argcount].v_type = VAR_UNKNOWN;
1185 functions[i].f_func(argvars, rettv);
1186 return ERROR_NONE;
1187}
1188
1189/*
1190 * Return TRUE for a non-zero Number and a non-empty String.
1191 */
1192 static int
1193non_zero_arg(typval_T *argvars)
1194{
1195 return ((argvars[0].v_type == VAR_NUMBER
1196 && argvars[0].vval.v_number != 0)
1197 || (argvars[0].v_type == VAR_SPECIAL
1198 && argvars[0].vval.v_number == VVAL_TRUE)
1199 || (argvars[0].v_type == VAR_STRING
1200 && argvars[0].vval.v_string != NULL
1201 && *argvars[0].vval.v_string != NUL));
1202}
1203
1204/*
1205 * Get the lnum from the first argument.
1206 * Also accepts ".", "$", etc., but that only works for the current buffer.
1207 * Returns -1 on error.
1208 */
1209 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001210tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001211{
1212 typval_T rettv;
1213 linenr_T lnum;
1214
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001215 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001216 if (lnum == 0) /* no valid number, try using line() */
1217 {
1218 rettv.v_type = VAR_NUMBER;
1219 f_line(argvars, &rettv);
1220 lnum = (linenr_T)rettv.vval.v_number;
1221 clear_tv(&rettv);
1222 }
1223 return lnum;
1224}
1225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001226/*
1227 * Get the lnum from the first argument.
1228 * Also accepts "$", then "buf" is used.
1229 * Returns 0 on error.
1230 */
1231 static linenr_T
1232tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1233{
1234 if (argvars[0].v_type == VAR_STRING
1235 && argvars[0].vval.v_string != NULL
1236 && argvars[0].vval.v_string[0] == '$'
1237 && buf != NULL)
1238 return buf->b_ml.ml_line_count;
1239 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1240}
1241
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001242#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001243/*
1244 * Get the float value of "argvars[0]" into "f".
1245 * Returns FAIL when the argument is not a Number or Float.
1246 */
1247 static int
1248get_float_arg(typval_T *argvars, float_T *f)
1249{
1250 if (argvars[0].v_type == VAR_FLOAT)
1251 {
1252 *f = argvars[0].vval.v_float;
1253 return OK;
1254 }
1255 if (argvars[0].v_type == VAR_NUMBER)
1256 {
1257 *f = (float_T)argvars[0].vval.v_number;
1258 return OK;
1259 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001260 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001261 return FAIL;
1262}
1263
1264/*
1265 * "abs(expr)" function
1266 */
1267 static void
1268f_abs(typval_T *argvars, typval_T *rettv)
1269{
1270 if (argvars[0].v_type == VAR_FLOAT)
1271 {
1272 rettv->v_type = VAR_FLOAT;
1273 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1274 }
1275 else
1276 {
1277 varnumber_T n;
1278 int error = FALSE;
1279
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001280 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001281 if (error)
1282 rettv->vval.v_number = -1;
1283 else if (n > 0)
1284 rettv->vval.v_number = n;
1285 else
1286 rettv->vval.v_number = -n;
1287 }
1288}
1289
1290/*
1291 * "acos()" function
1292 */
1293 static void
1294f_acos(typval_T *argvars, typval_T *rettv)
1295{
1296 float_T f = 0.0;
1297
1298 rettv->v_type = VAR_FLOAT;
1299 if (get_float_arg(argvars, &f) == OK)
1300 rettv->vval.v_float = acos(f);
1301 else
1302 rettv->vval.v_float = 0.0;
1303}
1304#endif
1305
1306/*
1307 * "add(list, item)" function
1308 */
1309 static void
1310f_add(typval_T *argvars, typval_T *rettv)
1311{
1312 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001314
1315 rettv->vval.v_number = 1; /* Default: Failed */
1316 if (argvars[0].v_type == VAR_LIST)
1317 {
1318 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001319 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320 (char_u *)N_("add() argument"), TRUE)
1321 && list_append_tv(l, &argvars[1]) == OK)
1322 copy_tv(&argvars[0], rettv);
1323 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001324 else if (argvars[0].v_type == VAR_BLOB)
1325 {
1326 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001327 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 (char_u *)N_("add() argument"), TRUE))
1329 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001330 int error = FALSE;
1331 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1332
1333 if (!error)
1334 {
1335 ga_append(&b->bv_ga, (int)n);
1336 copy_tv(&argvars[0], rettv);
1337 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 }
1339 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001340 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001341 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001342}
1343
1344/*
1345 * "and(expr, expr)" function
1346 */
1347 static void
1348f_and(typval_T *argvars, typval_T *rettv)
1349{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001350 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1351 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001352}
1353
1354/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001355 * If there is a window for "curbuf", make it the current window.
1356 */
1357 static void
1358find_win_for_curbuf(void)
1359{
1360 wininfo_T *wip;
1361
1362 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1363 {
1364 if (wip->wi_win != NULL)
1365 {
1366 curwin = wip->wi_win;
1367 break;
1368 }
1369 }
1370}
1371
1372/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001374 */
1375 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001376set_buffer_lines(
1377 buf_T *buf,
1378 linenr_T lnum_arg,
1379 int append,
1380 typval_T *lines,
1381 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001382{
Bram Moolenaarca851592018-06-06 21:04:07 +02001383 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1384 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385 list_T *l = NULL;
1386 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 linenr_T append_lnum;
1389 buf_T *curbuf_save = NULL;
1390 win_T *curwin_save = NULL;
1391 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001392
Bram Moolenaarca851592018-06-06 21:04:07 +02001393 /* When using the current buffer ml_mfp will be set if needed. Useful when
1394 * setline() is used on startup. For other buffers the buffer must be
1395 * loaded. */
1396 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001397 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001398 rettv->vval.v_number = 1; /* FAIL */
1399 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001400 }
1401
Bram Moolenaarca851592018-06-06 21:04:07 +02001402 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001403 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001404 curbuf_save = curbuf;
1405 curwin_save = curwin;
1406 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001407 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001408 }
1409
1410 if (append)
1411 // appendbufline() uses the line number below which we insert
1412 append_lnum = lnum - 1;
1413 else
1414 // setbufline() uses the line number above which we insert, we only
1415 // append if it's below the last line
1416 append_lnum = curbuf->b_ml.ml_line_count;
1417
1418 if (lines->v_type == VAR_LIST)
1419 {
1420 l = lines->vval.v_list;
1421 li = l->lv_first;
1422 }
1423 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001425
1426 /* default result is zero == OK */
1427 for (;;)
1428 {
1429 if (l != NULL)
1430 {
1431 /* list argument, get next string */
1432 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001433 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001434 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001435 li = li->li_next;
1436 }
1437
Bram Moolenaarca851592018-06-06 21:04:07 +02001438 rettv->vval.v_number = 1; /* FAIL */
1439 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1440 break;
1441
1442 /* When coming here from Insert mode, sync undo, so that this can be
1443 * undone separately from what was previously inserted. */
1444 if (u_sync_once == 2)
1445 {
1446 u_sync_once = 1; /* notify that u_sync() was called */
1447 u_sync(TRUE);
1448 }
1449
1450 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1451 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001452 // Existing line, replace it.
1453 // Removes any existing text properties.
1454 if (u_savesub(lnum) == OK && ml_replace_len(
1455 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001456 {
1457 changed_bytes(lnum, 0);
1458 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1459 check_cursor_col();
1460 rettv->vval.v_number = 0; /* OK */
1461 }
1462 }
1463 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1464 {
1465 /* append the line */
1466 ++added;
1467 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1468 rettv->vval.v_number = 0; /* OK */
1469 }
1470
1471 if (l == NULL) /* only one string argument */
1472 break;
1473 ++lnum;
1474 }
1475
1476 if (added > 0)
1477 {
1478 win_T *wp;
1479 tabpage_T *tp;
1480
1481 appended_lines_mark(append_lnum, added);
1482 FOR_ALL_TAB_WINDOWS(tp, wp)
1483 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1484 wp->w_cursor.lnum += added;
1485 check_cursor_col();
1486
Bram Moolenaarf2732452018-06-03 14:47:35 +02001487#ifdef FEAT_JOB_CHANNEL
1488 if (bt_prompt(curbuf) && (State & INSERT))
1489 // show the line with the prompt
1490 update_topline();
1491#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001492 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001493
1494 if (!is_curbuf)
1495 {
1496 curbuf = curbuf_save;
1497 curwin = curwin_save;
1498 }
1499}
1500
1501/*
1502 * "append(lnum, string/list)" function
1503 */
1504 static void
1505f_append(typval_T *argvars, typval_T *rettv)
1506{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001507 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001508
1509 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1510}
1511
1512/*
1513 * "appendbufline(buf, lnum, string/list)" function
1514 */
1515 static void
1516f_appendbufline(typval_T *argvars, typval_T *rettv)
1517{
1518 linenr_T lnum;
1519 buf_T *buf;
1520
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001521 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001522 if (buf == NULL)
1523 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001524 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001525 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001526 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001527 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1528 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001529}
1530
1531/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001532 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533 */
1534 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001535f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001536{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001537 win_T *wp;
1538
1539 if (argvars[0].v_type == VAR_UNKNOWN)
1540 // use the current window
1541 rettv->vval.v_number = ARGCOUNT;
1542 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001543 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001544 // use the global argument list
1545 rettv->vval.v_number = GARGCOUNT;
1546 else
1547 {
1548 // use the argument list of the specified window
1549 wp = find_win_by_nr_or_id(&argvars[0]);
1550 if (wp != NULL)
1551 rettv->vval.v_number = WARGCOUNT(wp);
1552 else
1553 rettv->vval.v_number = -1;
1554 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001555}
1556
1557/*
1558 * "argidx()" function
1559 */
1560 static void
1561f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1562{
1563 rettv->vval.v_number = curwin->w_arg_idx;
1564}
1565
1566/*
1567 * "arglistid()" function
1568 */
1569 static void
1570f_arglistid(typval_T *argvars, typval_T *rettv)
1571{
1572 win_T *wp;
1573
1574 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001575 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001576 if (wp != NULL)
1577 rettv->vval.v_number = wp->w_alist->id;
1578}
1579
1580/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001581 * Get the argument list for a given window
1582 */
1583 static void
1584get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1585{
1586 int idx;
1587
1588 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1589 for (idx = 0; idx < argcount; ++idx)
1590 list_append_string(rettv->vval.v_list,
1591 alist_name(&arglist[idx]), -1);
1592}
1593
1594/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 * "argv(nr)" function
1596 */
1597 static void
1598f_argv(typval_T *argvars, typval_T *rettv)
1599{
1600 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001601 aentry_T *arglist = NULL;
1602 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603
1604 if (argvars[0].v_type != VAR_UNKNOWN)
1605 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001606 if (argvars[1].v_type == VAR_UNKNOWN)
1607 {
1608 arglist = ARGLIST;
1609 argcount = ARGCOUNT;
1610 }
1611 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001612 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001613 {
1614 arglist = GARGLIST;
1615 argcount = GARGCOUNT;
1616 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001618 {
1619 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1620
1621 if (wp != NULL)
1622 {
1623 /* Use the argument list of the specified window */
1624 arglist = WARGLIST(wp);
1625 argcount = WARGCOUNT(wp);
1626 }
1627 }
1628
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001630 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001631 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001632 if (arglist != NULL && idx >= 0 && idx < argcount)
1633 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1634 else if (idx == -1)
1635 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001637 else
1638 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639}
1640
1641/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001642 * "assert_beeps(cmd [, error])" function
1643 */
1644 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001645f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001646{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001647 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001648}
1649
1650/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651 * "assert_equal(expected, actual[, msg])" function
1652 */
1653 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001654f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001656 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657}
1658
1659/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001660 * "assert_equalfile(fname-one, fname-two)" function
1661 */
1662 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001663f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001664{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001665 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001666}
1667
1668/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669 * "assert_notequal(expected, actual[, msg])" function
1670 */
1671 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001672f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001674 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001675}
1676
1677/*
1678 * "assert_exception(string[, msg])" function
1679 */
1680 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001681f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001682{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001683 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001684}
1685
1686/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001687 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688 */
1689 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001690f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001691{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001692 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001693}
1694
1695/*
1696 * "assert_false(actual[, msg])" function
1697 */
1698 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001699f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001701 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702}
1703
1704/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001705 * "assert_inrange(lower, upper[, msg])" function
1706 */
1707 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001708f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001709{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001710 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001711}
1712
1713/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714 * "assert_match(pattern, actual[, msg])" function
1715 */
1716 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001717f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001718{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001719 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001720}
1721
1722/*
1723 * "assert_notmatch(pattern, actual[, msg])" function
1724 */
1725 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001726f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001728 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001729}
1730
1731/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001732 * "assert_report(msg)" function
1733 */
1734 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001735f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001736{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001737 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001738}
1739
1740/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001741 * "assert_true(actual[, msg])" function
1742 */
1743 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001744f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001745{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001746 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001747}
1748
1749#ifdef FEAT_FLOAT
1750/*
1751 * "asin()" function
1752 */
1753 static void
1754f_asin(typval_T *argvars, typval_T *rettv)
1755{
1756 float_T f = 0.0;
1757
1758 rettv->v_type = VAR_FLOAT;
1759 if (get_float_arg(argvars, &f) == OK)
1760 rettv->vval.v_float = asin(f);
1761 else
1762 rettv->vval.v_float = 0.0;
1763}
1764
1765/*
1766 * "atan()" function
1767 */
1768 static void
1769f_atan(typval_T *argvars, typval_T *rettv)
1770{
1771 float_T f = 0.0;
1772
1773 rettv->v_type = VAR_FLOAT;
1774 if (get_float_arg(argvars, &f) == OK)
1775 rettv->vval.v_float = atan(f);
1776 else
1777 rettv->vval.v_float = 0.0;
1778}
1779
1780/*
1781 * "atan2()" function
1782 */
1783 static void
1784f_atan2(typval_T *argvars, typval_T *rettv)
1785{
1786 float_T fx = 0.0, fy = 0.0;
1787
1788 rettv->v_type = VAR_FLOAT;
1789 if (get_float_arg(argvars, &fx) == OK
1790 && get_float_arg(&argvars[1], &fy) == OK)
1791 rettv->vval.v_float = atan2(fx, fy);
1792 else
1793 rettv->vval.v_float = 0.0;
1794}
1795#endif
1796
1797/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001798 * "balloon_show()" function
1799 */
1800#ifdef FEAT_BEVAL
1801 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001802f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1803{
1804 rettv->v_type = VAR_STRING;
1805 if (balloonEval != NULL)
1806 {
1807 if (balloonEval->msg == NULL)
1808 rettv->vval.v_string = NULL;
1809 else
1810 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1811 }
1812}
1813
1814 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001815f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1816{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001817 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001818 {
1819 if (argvars[0].v_type == VAR_LIST
1820# ifdef FEAT_GUI
1821 && !gui.in_use
1822# endif
1823 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001824 {
1825 list_T *l = argvars[0].vval.v_list;
1826
1827 // empty list removes the balloon
1828 post_balloon(balloonEval, NULL,
1829 l == NULL || l->lv_len == 0 ? NULL : l);
1830 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001831 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001832 {
1833 char_u *mesg = tv_get_string_chk(&argvars[0]);
1834
1835 if (mesg != NULL)
1836 // empty string removes the balloon
1837 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1838 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001839 }
1840}
1841
Bram Moolenaar669a8282017-11-19 20:13:05 +01001842# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001843 static void
1844f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1845{
1846 if (rettv_list_alloc(rettv) == OK)
1847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001848 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001849
1850 if (msg != NULL)
1851 {
1852 pumitem_T *array;
1853 int size = split_message(msg, &array);
1854 int i;
1855
1856 /* Skip the first and last item, they are always empty. */
1857 for (i = 1; i < size - 1; ++i)
1858 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001859 while (size > 0)
1860 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001861 vim_free(array);
1862 }
1863 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001864}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001865# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001866#endif
1867
1868/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001869 * "browse(save, title, initdir, default)" function
1870 */
1871 static void
1872f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1873{
1874#ifdef FEAT_BROWSE
1875 int save;
1876 char_u *title;
1877 char_u *initdir;
1878 char_u *defname;
1879 char_u buf[NUMBUFLEN];
1880 char_u buf2[NUMBUFLEN];
1881 int error = FALSE;
1882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001883 save = (int)tv_get_number_chk(&argvars[0], &error);
1884 title = tv_get_string_chk(&argvars[1]);
1885 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1886 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001887
1888 if (error || title == NULL || initdir == NULL || defname == NULL)
1889 rettv->vval.v_string = NULL;
1890 else
1891 rettv->vval.v_string =
1892 do_browse(save ? BROWSE_SAVE : 0,
1893 title, defname, NULL, initdir, NULL, curbuf);
1894#else
1895 rettv->vval.v_string = NULL;
1896#endif
1897 rettv->v_type = VAR_STRING;
1898}
1899
1900/*
1901 * "browsedir(title, initdir)" function
1902 */
1903 static void
1904f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1905{
1906#ifdef FEAT_BROWSE
1907 char_u *title;
1908 char_u *initdir;
1909 char_u buf[NUMBUFLEN];
1910
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001911 title = tv_get_string_chk(&argvars[0]);
1912 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913
1914 if (title == NULL || initdir == NULL)
1915 rettv->vval.v_string = NULL;
1916 else
1917 rettv->vval.v_string = do_browse(BROWSE_DIR,
1918 title, NULL, NULL, initdir, NULL, curbuf);
1919#else
1920 rettv->vval.v_string = NULL;
1921#endif
1922 rettv->v_type = VAR_STRING;
1923}
1924
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001925/*
1926 * Find a buffer by number or exact name.
1927 */
1928 static buf_T *
1929find_buffer(typval_T *avar)
1930{
1931 buf_T *buf = NULL;
1932
1933 if (avar->v_type == VAR_NUMBER)
1934 buf = buflist_findnr((int)avar->vval.v_number);
1935 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1936 {
1937 buf = buflist_findname_exp(avar->vval.v_string);
1938 if (buf == NULL)
1939 {
1940 /* No full path name match, try a match with a URL or a "nofile"
1941 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001942 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001943 if (buf->b_fname != NULL
1944 && (path_with_url(buf->b_fname)
1945#ifdef FEAT_QUICKFIX
1946 || bt_nofile(buf)
1947#endif
1948 )
1949 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1950 break;
1951 }
1952 }
1953 return buf;
1954}
1955
1956/*
1957 * "bufexists(expr)" function
1958 */
1959 static void
1960f_bufexists(typval_T *argvars, typval_T *rettv)
1961{
1962 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1963}
1964
1965/*
1966 * "buflisted(expr)" function
1967 */
1968 static void
1969f_buflisted(typval_T *argvars, typval_T *rettv)
1970{
1971 buf_T *buf;
1972
1973 buf = find_buffer(&argvars[0]);
1974 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1975}
1976
1977/*
1978 * "bufloaded(expr)" function
1979 */
1980 static void
1981f_bufloaded(typval_T *argvars, typval_T *rettv)
1982{
1983 buf_T *buf;
1984
1985 buf = find_buffer(&argvars[0]);
1986 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1987}
1988
1989 buf_T *
1990buflist_find_by_name(char_u *name, int curtab_only)
1991{
1992 int save_magic;
1993 char_u *save_cpo;
1994 buf_T *buf;
1995
1996 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1997 save_magic = p_magic;
1998 p_magic = TRUE;
1999 save_cpo = p_cpo;
2000 p_cpo = (char_u *)"";
2001
2002 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
2003 TRUE, FALSE, curtab_only));
2004
2005 p_magic = save_magic;
2006 p_cpo = save_cpo;
2007 return buf;
2008}
2009
2010/*
2011 * Get buffer by number or pattern.
2012 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002013 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002014tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002015{
2016 char_u *name = tv->vval.v_string;
2017 buf_T *buf;
2018
2019 if (tv->v_type == VAR_NUMBER)
2020 return buflist_findnr((int)tv->vval.v_number);
2021 if (tv->v_type != VAR_STRING)
2022 return NULL;
2023 if (name == NULL || *name == NUL)
2024 return curbuf;
2025 if (name[0] == '$' && name[1] == NUL)
2026 return lastbuf;
2027
2028 buf = buflist_find_by_name(name, curtab_only);
2029
2030 /* If not found, try expanding the name, like done for bufexists(). */
2031 if (buf == NULL)
2032 buf = find_buffer(tv);
2033
2034 return buf;
2035}
2036
2037/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002038 * Get the buffer from "arg" and give an error and return NULL if it is not
2039 * valid.
2040 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002041 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002042get_buf_arg(typval_T *arg)
2043{
2044 buf_T *buf;
2045
2046 ++emsg_off;
2047 buf = tv_get_buf(arg, FALSE);
2048 --emsg_off;
2049 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002050 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002051 return buf;
2052}
2053
2054/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 * "bufname(expr)" function
2056 */
2057 static void
2058f_bufname(typval_T *argvars, typval_T *rettv)
2059{
2060 buf_T *buf;
2061
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002062 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002063 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002064 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002065 rettv->v_type = VAR_STRING;
2066 if (buf != NULL && buf->b_fname != NULL)
2067 rettv->vval.v_string = vim_strsave(buf->b_fname);
2068 else
2069 rettv->vval.v_string = NULL;
2070 --emsg_off;
2071}
2072
2073/*
2074 * "bufnr(expr)" function
2075 */
2076 static void
2077f_bufnr(typval_T *argvars, typval_T *rettv)
2078{
2079 buf_T *buf;
2080 int error = FALSE;
2081 char_u *name;
2082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002083 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002084 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002085 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002086 --emsg_off;
2087
2088 /* If the buffer isn't found and the second argument is not zero create a
2089 * new buffer. */
2090 if (buf == NULL
2091 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002092 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002094 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 && !error)
2096 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2097
2098 if (buf != NULL)
2099 rettv->vval.v_number = buf->b_fnum;
2100 else
2101 rettv->vval.v_number = -1;
2102}
2103
2104 static void
2105buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2106{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 win_T *wp;
2108 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 buf_T *buf;
2110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002111 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002112 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002113 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002114 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002115 {
2116 ++winnr;
2117 if (wp->w_buffer == buf)
2118 break;
2119 }
2120 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002121 --emsg_off;
2122}
2123
2124/*
2125 * "bufwinid(nr)" function
2126 */
2127 static void
2128f_bufwinid(typval_T *argvars, typval_T *rettv)
2129{
2130 buf_win_common(argvars, rettv, FALSE);
2131}
2132
2133/*
2134 * "bufwinnr(nr)" function
2135 */
2136 static void
2137f_bufwinnr(typval_T *argvars, typval_T *rettv)
2138{
2139 buf_win_common(argvars, rettv, TRUE);
2140}
2141
2142/*
2143 * "byte2line(byte)" function
2144 */
2145 static void
2146f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2147{
2148#ifndef FEAT_BYTEOFF
2149 rettv->vval.v_number = -1;
2150#else
2151 long boff = 0;
2152
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002153 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 if (boff < 0)
2155 rettv->vval.v_number = -1;
2156 else
2157 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2158 (linenr_T)0, &boff);
2159#endif
2160}
2161
2162 static void
2163byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2164{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002166 char_u *str;
2167 varnumber_T idx;
2168
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002169 str = tv_get_string_chk(&argvars[0]);
2170 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171 rettv->vval.v_number = -1;
2172 if (str == NULL || idx < 0)
2173 return;
2174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002175 t = str;
2176 for ( ; idx > 0; idx--)
2177 {
2178 if (*t == NUL) /* EOL reached */
2179 return;
2180 if (enc_utf8 && comp)
2181 t += utf_ptr2len(t);
2182 else
2183 t += (*mb_ptr2len)(t);
2184 }
2185 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002186}
2187
2188/*
2189 * "byteidx()" function
2190 */
2191 static void
2192f_byteidx(typval_T *argvars, typval_T *rettv)
2193{
2194 byteidx(argvars, rettv, FALSE);
2195}
2196
2197/*
2198 * "byteidxcomp()" function
2199 */
2200 static void
2201f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2202{
2203 byteidx(argvars, rettv, TRUE);
2204}
2205
2206/*
2207 * "call(func, arglist [, dict])" function
2208 */
2209 static void
2210f_call(typval_T *argvars, typval_T *rettv)
2211{
2212 char_u *func;
2213 partial_T *partial = NULL;
2214 dict_T *selfdict = NULL;
2215
2216 if (argvars[1].v_type != VAR_LIST)
2217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 return;
2220 }
2221 if (argvars[1].vval.v_list == NULL)
2222 return;
2223
2224 if (argvars[0].v_type == VAR_FUNC)
2225 func = argvars[0].vval.v_string;
2226 else if (argvars[0].v_type == VAR_PARTIAL)
2227 {
2228 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002229 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002230 }
2231 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002232 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002233 if (*func == NUL)
2234 return; /* type error or empty name */
2235
2236 if (argvars[2].v_type != VAR_UNKNOWN)
2237 {
2238 if (argvars[2].v_type != VAR_DICT)
2239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002241 return;
2242 }
2243 selfdict = argvars[2].vval.v_dict;
2244 }
2245
2246 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2247}
2248
2249#ifdef FEAT_FLOAT
2250/*
2251 * "ceil({float})" function
2252 */
2253 static void
2254f_ceil(typval_T *argvars, typval_T *rettv)
2255{
2256 float_T f = 0.0;
2257
2258 rettv->v_type = VAR_FLOAT;
2259 if (get_float_arg(argvars, &f) == OK)
2260 rettv->vval.v_float = ceil(f);
2261 else
2262 rettv->vval.v_float = 0.0;
2263}
2264#endif
2265
2266#ifdef FEAT_JOB_CHANNEL
2267/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002268 * "ch_canread()" function
2269 */
2270 static void
2271f_ch_canread(typval_T *argvars, typval_T *rettv)
2272{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002273 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002274
2275 rettv->vval.v_number = 0;
2276 if (channel != NULL)
2277 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2278 || channel_has_readahead(channel, PART_OUT)
2279 || channel_has_readahead(channel, PART_ERR);
2280}
2281
2282/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002283 * "ch_close()" function
2284 */
2285 static void
2286f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2287{
2288 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2289
2290 if (channel != NULL)
2291 {
2292 channel_close(channel, FALSE);
2293 channel_clear(channel);
2294 }
2295}
2296
2297/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002298 * "ch_close()" function
2299 */
2300 static void
2301f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2302{
2303 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2304
2305 if (channel != NULL)
2306 channel_close_in(channel);
2307}
2308
2309/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002310 * "ch_getbufnr()" function
2311 */
2312 static void
2313f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2314{
2315 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2316
2317 rettv->vval.v_number = -1;
2318 if (channel != NULL)
2319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002320 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002321 int part;
2322
2323 if (STRCMP(what, "err") == 0)
2324 part = PART_ERR;
2325 else if (STRCMP(what, "out") == 0)
2326 part = PART_OUT;
2327 else if (STRCMP(what, "in") == 0)
2328 part = PART_IN;
2329 else
2330 part = PART_SOCK;
2331 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2332 rettv->vval.v_number =
2333 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2334 }
2335}
2336
2337/*
2338 * "ch_getjob()" function
2339 */
2340 static void
2341f_ch_getjob(typval_T *argvars, typval_T *rettv)
2342{
2343 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2344
2345 if (channel != NULL)
2346 {
2347 rettv->v_type = VAR_JOB;
2348 rettv->vval.v_job = channel->ch_job;
2349 if (channel->ch_job != NULL)
2350 ++channel->ch_job->jv_refcount;
2351 }
2352}
2353
2354/*
2355 * "ch_info()" function
2356 */
2357 static void
2358f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2359{
2360 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2361
2362 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2363 channel_info(channel, rettv->vval.v_dict);
2364}
2365
2366/*
2367 * "ch_log()" function
2368 */
2369 static void
2370f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2371{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002372 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 channel_T *channel = NULL;
2374
2375 if (argvars[1].v_type != VAR_UNKNOWN)
2376 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2377
Bram Moolenaard5359b22018-04-05 22:44:39 +02002378 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002379}
2380
2381/*
2382 * "ch_logfile()" function
2383 */
2384 static void
2385f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2386{
2387 char_u *fname;
2388 char_u *opt = (char_u *)"";
2389 char_u buf[NUMBUFLEN];
2390
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002391 /* Don't open a file in restricted mode. */
2392 if (check_restricted() || check_secure())
2393 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002394 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002395 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002396 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002397 ch_logfile(fname, opt);
2398}
2399
2400/*
2401 * "ch_open()" function
2402 */
2403 static void
2404f_ch_open(typval_T *argvars, typval_T *rettv)
2405{
2406 rettv->v_type = VAR_CHANNEL;
2407 if (check_restricted() || check_secure())
2408 return;
2409 rettv->vval.v_channel = channel_open_func(argvars);
2410}
2411
2412/*
2413 * "ch_read()" function
2414 */
2415 static void
2416f_ch_read(typval_T *argvars, typval_T *rettv)
2417{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002418 common_channel_read(argvars, rettv, FALSE, FALSE);
2419}
2420
2421/*
2422 * "ch_readblob()" function
2423 */
2424 static void
2425f_ch_readblob(typval_T *argvars, typval_T *rettv)
2426{
2427 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002428}
2429
2430/*
2431 * "ch_readraw()" function
2432 */
2433 static void
2434f_ch_readraw(typval_T *argvars, typval_T *rettv)
2435{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002436 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002437}
2438
2439/*
2440 * "ch_evalexpr()" function
2441 */
2442 static void
2443f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2444{
2445 ch_expr_common(argvars, rettv, TRUE);
2446}
2447
2448/*
2449 * "ch_sendexpr()" function
2450 */
2451 static void
2452f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2453{
2454 ch_expr_common(argvars, rettv, FALSE);
2455}
2456
2457/*
2458 * "ch_evalraw()" function
2459 */
2460 static void
2461f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2462{
2463 ch_raw_common(argvars, rettv, TRUE);
2464}
2465
2466/*
2467 * "ch_sendraw()" function
2468 */
2469 static void
2470f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2471{
2472 ch_raw_common(argvars, rettv, FALSE);
2473}
2474
2475/*
2476 * "ch_setoptions()" function
2477 */
2478 static void
2479f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2480{
2481 channel_T *channel;
2482 jobopt_T opt;
2483
2484 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2485 if (channel == NULL)
2486 return;
2487 clear_job_options(&opt);
2488 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002489 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002490 channel_set_options(channel, &opt);
2491 free_job_options(&opt);
2492}
2493
2494/*
2495 * "ch_status()" function
2496 */
2497 static void
2498f_ch_status(typval_T *argvars, typval_T *rettv)
2499{
2500 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002501 jobopt_T opt;
2502 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002503
2504 /* return an empty string by default */
2505 rettv->v_type = VAR_STRING;
2506 rettv->vval.v_string = NULL;
2507
2508 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002509
2510 if (argvars[1].v_type != VAR_UNKNOWN)
2511 {
2512 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002513 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002514 && (opt.jo_set & JO_PART))
2515 part = opt.jo_part;
2516 }
2517
2518 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002519}
2520#endif
2521
2522/*
2523 * "changenr()" function
2524 */
2525 static void
2526f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2527{
2528 rettv->vval.v_number = curbuf->b_u_seq_cur;
2529}
2530
2531/*
2532 * "char2nr(string)" function
2533 */
2534 static void
2535f_char2nr(typval_T *argvars, typval_T *rettv)
2536{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537 if (has_mbyte)
2538 {
2539 int utf8 = 0;
2540
2541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002542 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002543
2544 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002545 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002546 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002547 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002548 }
2549 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002550 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002551}
2552
2553/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002554 * "chdir(dir)" function
2555 */
2556 static void
2557f_chdir(typval_T *argvars, typval_T *rettv)
2558{
2559 char_u *cwd;
2560 cdscope_T scope = CDSCOPE_GLOBAL;
2561
2562 rettv->v_type = VAR_STRING;
2563 rettv->vval.v_string = NULL;
2564
2565 if (argvars[0].v_type != VAR_STRING)
2566 return;
2567
2568 // Return the current directory
2569 cwd = alloc(MAXPATHL);
2570 if (cwd != NULL)
2571 {
2572 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2573 {
2574#ifdef BACKSLASH_IN_FILENAME
2575 slash_adjust(cwd);
2576#endif
2577 rettv->vval.v_string = vim_strsave(cwd);
2578 }
2579 vim_free(cwd);
2580 }
2581
2582 if (curwin->w_localdir != NULL)
2583 scope = CDSCOPE_WINDOW;
2584 else if (curtab->tp_localdir != NULL)
2585 scope = CDSCOPE_TABPAGE;
2586
2587 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2588 // Directory change failed
2589 VIM_CLEAR(rettv->vval.v_string);
2590}
2591
2592/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002593 * "cindent(lnum)" function
2594 */
2595 static void
2596f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2597{
2598#ifdef FEAT_CINDENT
2599 pos_T pos;
2600 linenr_T lnum;
2601
2602 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002603 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002604 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2605 {
2606 curwin->w_cursor.lnum = lnum;
2607 rettv->vval.v_number = get_c_indent();
2608 curwin->w_cursor = pos;
2609 }
2610 else
2611#endif
2612 rettv->vval.v_number = -1;
2613}
2614
Bram Moolenaaraff74912019-03-30 18:11:49 +01002615 static win_T *
2616get_optional_window(typval_T *argvars, int idx)
2617{
2618 win_T *win = curwin;
2619
2620 if (argvars[idx].v_type != VAR_UNKNOWN)
2621 {
2622 win = find_win_by_nr_or_id(&argvars[idx]);
2623 if (win == NULL)
2624 {
2625 emsg(_(e_invalwindow));
2626 return NULL;
2627 }
2628 }
2629 return win;
2630}
2631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002632/*
2633 * "clearmatches()" function
2634 */
2635 static void
2636f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2637{
2638#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002639 win_T *win = get_optional_window(argvars, 0);
2640
2641 if (win != NULL)
2642 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002643#endif
2644}
2645
2646/*
2647 * "col(string)" function
2648 */
2649 static void
2650f_col(typval_T *argvars, typval_T *rettv)
2651{
2652 colnr_T col = 0;
2653 pos_T *fp;
2654 int fnum = curbuf->b_fnum;
2655
2656 fp = var2fpos(&argvars[0], FALSE, &fnum);
2657 if (fp != NULL && fnum == curbuf->b_fnum)
2658 {
2659 if (fp->col == MAXCOL)
2660 {
2661 /* '> can be MAXCOL, get the length of the line then */
2662 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2663 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2664 else
2665 col = MAXCOL;
2666 }
2667 else
2668 {
2669 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 /* col(".") when the cursor is on the NUL at the end of the line
2671 * because of "coladd" can be seen as an extra column. */
2672 if (virtual_active() && fp == &curwin->w_cursor)
2673 {
2674 char_u *p = ml_get_cursor();
2675
2676 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2677 curwin->w_virtcol - curwin->w_cursor.coladd))
2678 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002679 int l;
2680
2681 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2682 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002683 }
2684 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002685 }
2686 }
2687 rettv->vval.v_number = col;
2688}
2689
2690#if defined(FEAT_INS_EXPAND)
2691/*
2692 * "complete()" function
2693 */
2694 static void
2695f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2696{
2697 int startcol;
2698
2699 if ((State & INSERT) == 0)
2700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002701 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702 return;
2703 }
2704
2705 /* Check for undo allowed here, because if something was already inserted
2706 * the line was already saved for undo and this check isn't done. */
2707 if (!undo_allowed())
2708 return;
2709
2710 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002712 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002713 return;
2714 }
2715
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002716 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002717 if (startcol <= 0)
2718 return;
2719
2720 set_completion(startcol - 1, argvars[1].vval.v_list);
2721}
2722
2723/*
2724 * "complete_add()" function
2725 */
2726 static void
2727f_complete_add(typval_T *argvars, typval_T *rettv)
2728{
2729 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2730}
2731
2732/*
2733 * "complete_check()" function
2734 */
2735 static void
2736f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2737{
2738 int saved = RedrawingDisabled;
2739
2740 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002741 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002742 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002743 RedrawingDisabled = saved;
2744}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002745
2746/*
2747 * "complete_info()" function
2748 */
2749 static void
2750f_complete_info(typval_T *argvars, typval_T *rettv)
2751{
2752 list_T *what_list = NULL;
2753
2754 if (rettv_dict_alloc(rettv) != OK)
2755 return;
2756
2757 if (argvars[0].v_type != VAR_UNKNOWN)
2758 {
2759 if (argvars[0].v_type != VAR_LIST)
2760 {
2761 emsg(_(e_listreq));
2762 return;
2763 }
2764 what_list = argvars[0].vval.v_list;
2765 }
2766 get_complete_info(what_list, rettv->vval.v_dict);
2767}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002768#endif
2769
2770/*
2771 * "confirm(message, buttons[, default [, type]])" function
2772 */
2773 static void
2774f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2775{
2776#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2777 char_u *message;
2778 char_u *buttons = NULL;
2779 char_u buf[NUMBUFLEN];
2780 char_u buf2[NUMBUFLEN];
2781 int def = 1;
2782 int type = VIM_GENERIC;
2783 char_u *typestr;
2784 int error = FALSE;
2785
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002786 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 if (message == NULL)
2788 error = TRUE;
2789 if (argvars[1].v_type != VAR_UNKNOWN)
2790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002791 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002792 if (buttons == NULL)
2793 error = TRUE;
2794 if (argvars[2].v_type != VAR_UNKNOWN)
2795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002796 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002797 if (argvars[3].v_type != VAR_UNKNOWN)
2798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002799 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002800 if (typestr == NULL)
2801 error = TRUE;
2802 else
2803 {
2804 switch (TOUPPER_ASC(*typestr))
2805 {
2806 case 'E': type = VIM_ERROR; break;
2807 case 'Q': type = VIM_QUESTION; break;
2808 case 'I': type = VIM_INFO; break;
2809 case 'W': type = VIM_WARNING; break;
2810 case 'G': type = VIM_GENERIC; break;
2811 }
2812 }
2813 }
2814 }
2815 }
2816
2817 if (buttons == NULL || *buttons == NUL)
2818 buttons = (char_u *)_("&Ok");
2819
2820 if (!error)
2821 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2822 def, NULL, FALSE);
2823#endif
2824}
2825
2826/*
2827 * "copy()" function
2828 */
2829 static void
2830f_copy(typval_T *argvars, typval_T *rettv)
2831{
2832 item_copy(&argvars[0], rettv, FALSE, 0);
2833}
2834
2835#ifdef FEAT_FLOAT
2836/*
2837 * "cos()" function
2838 */
2839 static void
2840f_cos(typval_T *argvars, typval_T *rettv)
2841{
2842 float_T f = 0.0;
2843
2844 rettv->v_type = VAR_FLOAT;
2845 if (get_float_arg(argvars, &f) == OK)
2846 rettv->vval.v_float = cos(f);
2847 else
2848 rettv->vval.v_float = 0.0;
2849}
2850
2851/*
2852 * "cosh()" function
2853 */
2854 static void
2855f_cosh(typval_T *argvars, typval_T *rettv)
2856{
2857 float_T f = 0.0;
2858
2859 rettv->v_type = VAR_FLOAT;
2860 if (get_float_arg(argvars, &f) == OK)
2861 rettv->vval.v_float = cosh(f);
2862 else
2863 rettv->vval.v_float = 0.0;
2864}
2865#endif
2866
2867/*
2868 * "count()" function
2869 */
2870 static void
2871f_count(typval_T *argvars, typval_T *rettv)
2872{
2873 long n = 0;
2874 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002875 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002876
Bram Moolenaar9966b212017-07-28 16:46:57 +02002877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002878 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002879
2880 if (argvars[0].v_type == VAR_STRING)
2881 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002882 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002883 char_u *p = argvars[0].vval.v_string;
2884 char_u *next;
2885
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002886 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002887 {
2888 if (ic)
2889 {
2890 size_t len = STRLEN(expr);
2891
2892 while (*p != NUL)
2893 {
2894 if (MB_STRNICMP(p, expr, len) == 0)
2895 {
2896 ++n;
2897 p += len;
2898 }
2899 else
2900 MB_PTR_ADV(p);
2901 }
2902 }
2903 else
2904 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2905 != NULL)
2906 {
2907 ++n;
2908 p = next + STRLEN(expr);
2909 }
2910 }
2911
2912 }
2913 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914 {
2915 listitem_T *li;
2916 list_T *l;
2917 long idx;
2918
2919 if ((l = argvars[0].vval.v_list) != NULL)
2920 {
2921 li = l->lv_first;
2922 if (argvars[2].v_type != VAR_UNKNOWN)
2923 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002924 if (argvars[3].v_type != VAR_UNKNOWN)
2925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002926 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002927 if (!error)
2928 {
2929 li = list_find(l, idx);
2930 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002931 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002932 }
2933 }
2934 if (error)
2935 li = NULL;
2936 }
2937
2938 for ( ; li != NULL; li = li->li_next)
2939 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2940 ++n;
2941 }
2942 }
2943 else if (argvars[0].v_type == VAR_DICT)
2944 {
2945 int todo;
2946 dict_T *d;
2947 hashitem_T *hi;
2948
2949 if ((d = argvars[0].vval.v_dict) != NULL)
2950 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002951 if (argvars[2].v_type != VAR_UNKNOWN)
2952 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002953 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002954 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002955 }
2956
2957 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2958 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2959 {
2960 if (!HASHITEM_EMPTY(hi))
2961 {
2962 --todo;
2963 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2964 ++n;
2965 }
2966 }
2967 }
2968 }
2969 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 rettv->vval.v_number = n;
2972}
2973
2974/*
2975 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2976 *
2977 * Checks the existence of a cscope connection.
2978 */
2979 static void
2980f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2981{
2982#ifdef FEAT_CSCOPE
2983 int num = 0;
2984 char_u *dbpath = NULL;
2985 char_u *prepend = NULL;
2986 char_u buf[NUMBUFLEN];
2987
2988 if (argvars[0].v_type != VAR_UNKNOWN
2989 && argvars[1].v_type != VAR_UNKNOWN)
2990 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002991 num = (int)tv_get_number(&argvars[0]);
2992 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002993 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002994 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002995 }
2996
2997 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2998#endif
2999}
3000
3001/*
3002 * "cursor(lnum, col)" function, or
3003 * "cursor(list)"
3004 *
3005 * Moves the cursor to the specified line and column.
3006 * Returns 0 when the position could be set, -1 otherwise.
3007 */
3008 static void
3009f_cursor(typval_T *argvars, typval_T *rettv)
3010{
3011 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 int set_curswant = TRUE;
3014
3015 rettv->vval.v_number = -1;
3016 if (argvars[1].v_type == VAR_UNKNOWN)
3017 {
3018 pos_T pos;
3019 colnr_T curswant = -1;
3020
3021 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 return;
3025 }
3026 line = pos.lnum;
3027 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 if (curswant >= 0)
3030 {
3031 curwin->w_curswant = curswant - 1;
3032 set_curswant = FALSE;
3033 }
3034 }
3035 else
3036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 line = tv_get_lnum(argvars);
3038 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003040 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003042 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043 return; /* type error; errmsg already given */
3044 if (line > 0)
3045 curwin->w_cursor.lnum = line;
3046 if (col > 0)
3047 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003048 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003049
3050 /* Make sure the cursor is in a valid position. */
3051 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003052 /* Correct cursor for multi-byte character. */
3053 if (has_mbyte)
3054 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003055
3056 curwin->w_set_curswant = set_curswant;
3057 rettv->vval.v_number = 0;
3058}
3059
Bram Moolenaar4f974752019-02-17 17:44:42 +01003060#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003061/*
3062 * "debugbreak()" function
3063 */
3064 static void
3065f_debugbreak(typval_T *argvars, typval_T *rettv)
3066{
3067 int pid;
3068
3069 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003070 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003071 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003072 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003073 else
3074 {
3075 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3076
3077 if (hProcess != NULL)
3078 {
3079 DebugBreakProcess(hProcess);
3080 CloseHandle(hProcess);
3081 rettv->vval.v_number = OK;
3082 }
3083 }
3084}
3085#endif
3086
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003087/*
3088 * "deepcopy()" function
3089 */
3090 static void
3091f_deepcopy(typval_T *argvars, typval_T *rettv)
3092{
3093 int noref = 0;
3094 int copyID;
3095
3096 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003097 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003098 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003099 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 else
3101 {
3102 copyID = get_copyID();
3103 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3104 }
3105}
3106
3107/*
3108 * "delete()" function
3109 */
3110 static void
3111f_delete(typval_T *argvars, typval_T *rettv)
3112{
3113 char_u nbuf[NUMBUFLEN];
3114 char_u *name;
3115 char_u *flags;
3116
3117 rettv->vval.v_number = -1;
3118 if (check_restricted() || check_secure())
3119 return;
3120
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003121 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122 if (name == NULL || *name == NUL)
3123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003124 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003125 return;
3126 }
3127
3128 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003129 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130 else
3131 flags = (char_u *)"";
3132
3133 if (*flags == NUL)
3134 /* delete a file */
3135 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3136 else if (STRCMP(flags, "d") == 0)
3137 /* delete an empty directory */
3138 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3139 else if (STRCMP(flags, "rf") == 0)
3140 /* delete a directory recursively */
3141 rettv->vval.v_number = delete_recursive(name);
3142 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003143 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003144}
3145
3146/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003147 * "deletebufline()" function
3148 */
3149 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003150f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003151{
3152 buf_T *buf;
3153 linenr_T first, last;
3154 linenr_T lnum;
3155 long count;
3156 int is_curbuf;
3157 buf_T *curbuf_save = NULL;
3158 win_T *curwin_save = NULL;
3159 tabpage_T *tp;
3160 win_T *wp;
3161
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003162 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003163 if (buf == NULL)
3164 {
3165 rettv->vval.v_number = 1; /* FAIL */
3166 return;
3167 }
3168 is_curbuf = buf == curbuf;
3169
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003170 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003171 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003172 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003173 else
3174 last = first;
3175
3176 if (buf->b_ml.ml_mfp == NULL || first < 1
3177 || first > buf->b_ml.ml_line_count || last < first)
3178 {
3179 rettv->vval.v_number = 1; /* FAIL */
3180 return;
3181 }
3182
3183 if (!is_curbuf)
3184 {
3185 curbuf_save = curbuf;
3186 curwin_save = curwin;
3187 curbuf = buf;
3188 find_win_for_curbuf();
3189 }
3190 if (last > curbuf->b_ml.ml_line_count)
3191 last = curbuf->b_ml.ml_line_count;
3192 count = last - first + 1;
3193
3194 // When coming here from Insert mode, sync undo, so that this can be
3195 // undone separately from what was previously inserted.
3196 if (u_sync_once == 2)
3197 {
3198 u_sync_once = 1; // notify that u_sync() was called
3199 u_sync(TRUE);
3200 }
3201
3202 if (u_save(first - 1, last + 1) == FAIL)
3203 {
3204 rettv->vval.v_number = 1; /* FAIL */
3205 return;
3206 }
3207
3208 for (lnum = first; lnum <= last; ++lnum)
3209 ml_delete(first, TRUE);
3210
3211 FOR_ALL_TAB_WINDOWS(tp, wp)
3212 if (wp->w_buffer == buf)
3213 {
3214 if (wp->w_cursor.lnum > last)
3215 wp->w_cursor.lnum -= count;
3216 else if (wp->w_cursor.lnum> first)
3217 wp->w_cursor.lnum = first;
3218 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3219 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3220 }
3221 check_cursor_col();
3222 deleted_lines_mark(first, count);
3223
3224 if (!is_curbuf)
3225 {
3226 curbuf = curbuf_save;
3227 curwin = curwin_save;
3228 }
3229}
3230
3231/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003232 * "did_filetype()" function
3233 */
3234 static void
3235f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3236{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003238}
3239
3240/*
3241 * "diff_filler()" function
3242 */
3243 static void
3244f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3245{
3246#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003247 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003248#endif
3249}
3250
3251/*
3252 * "diff_hlID()" function
3253 */
3254 static void
3255f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3256{
3257#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003258 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003259 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003260 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003261 static int fnum = 0;
3262 static int change_start = 0;
3263 static int change_end = 0;
3264 static hlf_T hlID = (hlf_T)0;
3265 int filler_lines;
3266 int col;
3267
3268 if (lnum < 0) /* ignore type error in {lnum} arg */
3269 lnum = 0;
3270 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003271 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 || fnum != curbuf->b_fnum)
3273 {
3274 /* New line, buffer, change: need to get the values. */
3275 filler_lines = diff_check(curwin, lnum);
3276 if (filler_lines < 0)
3277 {
3278 if (filler_lines == -1)
3279 {
3280 change_start = MAXCOL;
3281 change_end = -1;
3282 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3283 hlID = HLF_ADD; /* added line */
3284 else
3285 hlID = HLF_CHD; /* changed line */
3286 }
3287 else
3288 hlID = HLF_ADD; /* added line */
3289 }
3290 else
3291 hlID = (hlf_T)0;
3292 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003293 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003294 fnum = curbuf->b_fnum;
3295 }
3296
3297 if (hlID == HLF_CHD || hlID == HLF_TXD)
3298 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003299 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003300 if (col >= change_start && col <= change_end)
3301 hlID = HLF_TXD; /* changed text */
3302 else
3303 hlID = HLF_CHD; /* changed line */
3304 }
3305 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3306#endif
3307}
3308
3309/*
3310 * "empty({expr})" function
3311 */
3312 static void
3313f_empty(typval_T *argvars, typval_T *rettv)
3314{
3315 int n = FALSE;
3316
3317 switch (argvars[0].v_type)
3318 {
3319 case VAR_STRING:
3320 case VAR_FUNC:
3321 n = argvars[0].vval.v_string == NULL
3322 || *argvars[0].vval.v_string == NUL;
3323 break;
3324 case VAR_PARTIAL:
3325 n = FALSE;
3326 break;
3327 case VAR_NUMBER:
3328 n = argvars[0].vval.v_number == 0;
3329 break;
3330 case VAR_FLOAT:
3331#ifdef FEAT_FLOAT
3332 n = argvars[0].vval.v_float == 0.0;
3333 break;
3334#endif
3335 case VAR_LIST:
3336 n = argvars[0].vval.v_list == NULL
3337 || argvars[0].vval.v_list->lv_first == NULL;
3338 break;
3339 case VAR_DICT:
3340 n = argvars[0].vval.v_dict == NULL
3341 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3342 break;
3343 case VAR_SPECIAL:
3344 n = argvars[0].vval.v_number != VVAL_TRUE;
3345 break;
3346
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003347 case VAR_BLOB:
3348 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003349 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3350 break;
3351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 case VAR_JOB:
3353#ifdef FEAT_JOB_CHANNEL
3354 n = argvars[0].vval.v_job == NULL
3355 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3356 break;
3357#endif
3358 case VAR_CHANNEL:
3359#ifdef FEAT_JOB_CHANNEL
3360 n = argvars[0].vval.v_channel == NULL
3361 || !channel_is_open(argvars[0].vval.v_channel);
3362 break;
3363#endif
3364 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003365 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003366 n = TRUE;
3367 break;
3368 }
3369
3370 rettv->vval.v_number = n;
3371}
3372
3373/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003374 * "environ()" function
3375 */
3376 static void
3377f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3378{
3379#if !defined(AMIGA)
3380 int i = 0;
3381 char_u *entry, *value;
3382# ifdef MSWIN
3383 extern wchar_t **_wenviron;
3384# else
3385 extern char **environ;
3386# endif
3387
3388 if (rettv_dict_alloc(rettv) != OK)
3389 return;
3390
3391# ifdef MSWIN
3392 if (*_wenviron == NULL)
3393 return;
3394# else
3395 if (*environ == NULL)
3396 return;
3397# endif
3398
3399 for (i = 0; ; ++i)
3400 {
3401# ifdef MSWIN
3402 short_u *p;
3403
3404 if ((p = (short_u *)_wenviron[i]) == NULL)
3405 return;
3406 entry = utf16_to_enc(p, NULL);
3407# else
3408 if ((entry = (char_u *)environ[i]) == NULL)
3409 return;
3410 entry = vim_strsave(entry);
3411# endif
3412 if (entry == NULL) // out of memory
3413 return;
3414 if ((value = vim_strchr(entry, '=')) == NULL)
3415 {
3416 vim_free(entry);
3417 continue;
3418 }
3419 *value++ = NUL;
3420 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3421 vim_free(entry);
3422 }
3423#endif
3424}
3425
3426/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003427 * "escape({string}, {chars})" function
3428 */
3429 static void
3430f_escape(typval_T *argvars, typval_T *rettv)
3431{
3432 char_u buf[NUMBUFLEN];
3433
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003434 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3435 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 rettv->v_type = VAR_STRING;
3437}
3438
3439/*
3440 * "eval()" function
3441 */
3442 static void
3443f_eval(typval_T *argvars, typval_T *rettv)
3444{
3445 char_u *s, *p;
3446
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003447 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448 if (s != NULL)
3449 s = skipwhite(s);
3450
3451 p = s;
3452 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3453 {
3454 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003455 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003456 need_clr_eos = FALSE;
3457 rettv->v_type = VAR_NUMBER;
3458 rettv->vval.v_number = 0;
3459 }
3460 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003461 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003462}
3463
3464/*
3465 * "eventhandler()" function
3466 */
3467 static void
3468f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3469{
3470 rettv->vval.v_number = vgetc_busy;
3471}
3472
3473/*
3474 * "executable()" function
3475 */
3476 static void
3477f_executable(typval_T *argvars, typval_T *rettv)
3478{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003479 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003480
3481 /* Check in $PATH and also check directly if there is a directory name. */
3482 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3483 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3484}
3485
3486static garray_T redir_execute_ga;
3487
3488/*
3489 * Append "value[value_len]" to the execute() output.
3490 */
3491 void
3492execute_redir_str(char_u *value, int value_len)
3493{
3494 int len;
3495
3496 if (value_len == -1)
3497 len = (int)STRLEN(value); /* Append the entire string */
3498 else
3499 len = value_len; /* Append only "value_len" characters */
3500 if (ga_grow(&redir_execute_ga, len) == OK)
3501 {
3502 mch_memmove((char *)redir_execute_ga.ga_data
3503 + redir_execute_ga.ga_len, value, len);
3504 redir_execute_ga.ga_len += len;
3505 }
3506}
3507
3508/*
3509 * Get next line from a list.
3510 * Called by do_cmdline() to get the next line.
3511 * Returns allocated string, or NULL for end of function.
3512 */
3513
3514 static char_u *
3515get_list_line(
3516 int c UNUSED,
3517 void *cookie,
3518 int indent UNUSED)
3519{
3520 listitem_T **p = (listitem_T **)cookie;
3521 listitem_T *item = *p;
3522 char_u buf[NUMBUFLEN];
3523 char_u *s;
3524
3525 if (item == NULL)
3526 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003527 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003528 *p = item->li_next;
3529 return s == NULL ? NULL : vim_strsave(s);
3530}
3531
3532/*
3533 * "execute()" function
3534 */
3535 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003536execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003537{
3538 char_u *cmd = NULL;
3539 list_T *list = NULL;
3540 int save_msg_silent = msg_silent;
3541 int save_emsg_silent = emsg_silent;
3542 int save_emsg_noredir = emsg_noredir;
3543 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003544 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003546 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003547 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548
3549 rettv->vval.v_string = NULL;
3550 rettv->v_type = VAR_STRING;
3551
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003552 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003554 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003555 if (list == NULL || list->lv_first == NULL)
3556 /* empty list, no commands, empty output */
3557 return;
3558 ++list->lv_refcount;
3559 }
3560 else
3561 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003562 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003563 if (cmd == NULL)
3564 return;
3565 }
3566
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003567 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003568 {
3569 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003570 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003571
3572 if (s == NULL)
3573 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003574 if (*s == NUL)
3575 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003576 if (STRNCMP(s, "silent", 6) == 0)
3577 ++msg_silent;
3578 if (STRCMP(s, "silent!") == 0)
3579 {
3580 emsg_silent = TRUE;
3581 emsg_noredir = TRUE;
3582 }
3583 }
3584 else
3585 ++msg_silent;
3586
3587 if (redir_execute)
3588 save_ga = redir_execute_ga;
3589 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3590 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003591 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003592 if (!echo_output)
3593 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003594
3595 if (cmd != NULL)
3596 do_cmdline_cmd(cmd);
3597 else
3598 {
3599 listitem_T *item = list->lv_first;
3600
3601 do_cmdline(NULL, get_list_line, (void *)&item,
3602 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3603 --list->lv_refcount;
3604 }
3605
Bram Moolenaard297f352017-01-29 20:31:21 +01003606 /* Need to append a NUL to the result. */
3607 if (ga_grow(&redir_execute_ga, 1) == OK)
3608 {
3609 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3610 rettv->vval.v_string = redir_execute_ga.ga_data;
3611 }
3612 else
3613 {
3614 ga_clear(&redir_execute_ga);
3615 rettv->vval.v_string = NULL;
3616 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003617 msg_silent = save_msg_silent;
3618 emsg_silent = save_emsg_silent;
3619 emsg_noredir = save_emsg_noredir;
3620
3621 redir_execute = save_redir_execute;
3622 if (redir_execute)
3623 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003624 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003626 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003627 if (echo_output)
3628 // When not working silently: put it in column zero. A following
3629 // "echon" will overwrite the message, unavoidably.
3630 msg_col = 0;
3631 else
3632 // When working silently: Put it back where it was, since nothing
3633 // should have been written.
3634 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635}
3636
3637/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003638 * "execute()" function
3639 */
3640 static void
3641f_execute(typval_T *argvars, typval_T *rettv)
3642{
3643 execute_common(argvars, rettv, 0);
3644}
3645
3646/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003647 * "exepath()" function
3648 */
3649 static void
3650f_exepath(typval_T *argvars, typval_T *rettv)
3651{
3652 char_u *p = NULL;
3653
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003654 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003655 rettv->v_type = VAR_STRING;
3656 rettv->vval.v_string = p;
3657}
3658
3659/*
3660 * "exists()" function
3661 */
3662 static void
3663f_exists(typval_T *argvars, typval_T *rettv)
3664{
3665 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003666 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003667
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003668 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 if (*p == '$') /* environment variable */
3670 {
3671 /* first try "normal" environment variables (fast) */
3672 if (mch_getenv(p + 1) != NULL)
3673 n = TRUE;
3674 else
3675 {
3676 /* try expanding things like $VIM and ${HOME} */
3677 p = expand_env_save(p);
3678 if (p != NULL && *p != '$')
3679 n = TRUE;
3680 vim_free(p);
3681 }
3682 }
3683 else if (*p == '&' || *p == '+') /* option */
3684 {
3685 n = (get_option_tv(&p, NULL, TRUE) == OK);
3686 if (*skipwhite(p) != NUL)
3687 n = FALSE; /* trailing garbage */
3688 }
3689 else if (*p == '*') /* internal or user defined function */
3690 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003691 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003692 }
3693 else if (*p == ':')
3694 {
3695 n = cmd_exists(p + 1);
3696 }
3697 else if (*p == '#')
3698 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003699 if (p[1] == '#')
3700 n = autocmd_supported(p + 2);
3701 else
3702 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003703 }
3704 else /* internal variable */
3705 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003706 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 }
3708
3709 rettv->vval.v_number = n;
3710}
3711
3712#ifdef FEAT_FLOAT
3713/*
3714 * "exp()" function
3715 */
3716 static void
3717f_exp(typval_T *argvars, typval_T *rettv)
3718{
3719 float_T f = 0.0;
3720
3721 rettv->v_type = VAR_FLOAT;
3722 if (get_float_arg(argvars, &f) == OK)
3723 rettv->vval.v_float = exp(f);
3724 else
3725 rettv->vval.v_float = 0.0;
3726}
3727#endif
3728
3729/*
3730 * "expand()" function
3731 */
3732 static void
3733f_expand(typval_T *argvars, typval_T *rettv)
3734{
3735 char_u *s;
3736 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003737 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003738 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3739 expand_T xpc;
3740 int error = FALSE;
3741 char_u *result;
3742
3743 rettv->v_type = VAR_STRING;
3744 if (argvars[1].v_type != VAR_UNKNOWN
3745 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003746 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003747 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003748 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003750 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003751 if (*s == '%' || *s == '#' || *s == '<')
3752 {
3753 ++emsg_off;
3754 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3755 --emsg_off;
3756 if (rettv->v_type == VAR_LIST)
3757 {
3758 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3759 list_append_string(rettv->vval.v_list, result, -1);
3760 else
3761 vim_free(result);
3762 }
3763 else
3764 rettv->vval.v_string = result;
3765 }
3766 else
3767 {
3768 /* When the optional second argument is non-zero, don't remove matches
3769 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3770 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003771 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003772 options |= WILD_KEEP_ALL;
3773 if (!error)
3774 {
3775 ExpandInit(&xpc);
3776 xpc.xp_context = EXPAND_FILES;
3777 if (p_wic)
3778 options += WILD_ICASE;
3779 if (rettv->v_type == VAR_STRING)
3780 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3781 options, WILD_ALL);
3782 else if (rettv_list_alloc(rettv) != FAIL)
3783 {
3784 int i;
3785
3786 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3787 for (i = 0; i < xpc.xp_numfiles; i++)
3788 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3789 ExpandCleanup(&xpc);
3790 }
3791 }
3792 else
3793 rettv->vval.v_string = NULL;
3794 }
3795}
3796
3797/*
Bram Moolenaar80dad482019-06-09 17:22:31 +02003798 * "expandcmd()" function
3799 * Expand all the special characters in a command string.
3800 */
3801 static void
3802f_expandcmd(typval_T *argvars, typval_T *rettv)
3803{
3804 exarg_T eap;
3805 char_u *cmdstr;
3806 char *errormsg = NULL;
3807
3808 rettv->v_type = VAR_STRING;
3809 cmdstr = vim_strsave(tv_get_string(&argvars[0]));
3810
3811 memset(&eap, 0, sizeof(eap));
3812 eap.cmd = cmdstr;
3813 eap.arg = cmdstr;
3814 eap.argt |= NOSPC;
3815 eap.usefilter = FALSE;
3816 eap.nextcmd = NULL;
3817 eap.cmdidx = CMD_USER;
3818
3819 expand_filename(&eap, &cmdstr, &errormsg);
3820 if (errormsg != NULL && *errormsg != NUL)
3821 emsg(errormsg);
3822
3823 rettv->vval.v_string = cmdstr;
3824}
3825
3826/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003827 * "extend(list, list [, idx])" function
3828 * "extend(dict, dict [, action])" function
3829 */
3830 static void
3831f_extend(typval_T *argvars, typval_T *rettv)
3832{
3833 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3834
3835 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3836 {
3837 list_T *l1, *l2;
3838 listitem_T *item;
3839 long before;
3840 int error = FALSE;
3841
3842 l1 = argvars[0].vval.v_list;
3843 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003844 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845 && l2 != NULL)
3846 {
3847 if (argvars[2].v_type != VAR_UNKNOWN)
3848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003849 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 if (error)
3851 return; /* type error; errmsg already given */
3852
3853 if (before == l1->lv_len)
3854 item = NULL;
3855 else
3856 {
3857 item = list_find(l1, before);
3858 if (item == NULL)
3859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003860 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003861 return;
3862 }
3863 }
3864 }
3865 else
3866 item = NULL;
3867 list_extend(l1, l2, item);
3868
3869 copy_tv(&argvars[0], rettv);
3870 }
3871 }
3872 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3873 {
3874 dict_T *d1, *d2;
3875 char_u *action;
3876 int i;
3877
3878 d1 = argvars[0].vval.v_dict;
3879 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003880 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003881 && d2 != NULL)
3882 {
3883 /* Check the third argument. */
3884 if (argvars[2].v_type != VAR_UNKNOWN)
3885 {
3886 static char *(av[]) = {"keep", "force", "error"};
3887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003888 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003889 if (action == NULL)
3890 return; /* type error; errmsg already given */
3891 for (i = 0; i < 3; ++i)
3892 if (STRCMP(action, av[i]) == 0)
3893 break;
3894 if (i == 3)
3895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003896 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 return;
3898 }
3899 }
3900 else
3901 action = (char_u *)"force";
3902
3903 dict_extend(d1, d2, action);
3904
3905 copy_tv(&argvars[0], rettv);
3906 }
3907 }
3908 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003909 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003910}
3911
3912/*
3913 * "feedkeys()" function
3914 */
3915 static void
3916f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3917{
3918 int remap = TRUE;
3919 int insert = FALSE;
3920 char_u *keys, *flags;
3921 char_u nbuf[NUMBUFLEN];
3922 int typed = FALSE;
3923 int execute = FALSE;
3924 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003925 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003926 char_u *keys_esc;
3927
3928 /* This is not allowed in the sandbox. If the commands would still be
3929 * executed in the sandbox it would be OK, but it probably happens later,
3930 * when "sandbox" is no longer set. */
3931 if (check_secure())
3932 return;
3933
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003934 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935
3936 if (argvars[1].v_type != VAR_UNKNOWN)
3937 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003938 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003939 for ( ; *flags != NUL; ++flags)
3940 {
3941 switch (*flags)
3942 {
3943 case 'n': remap = FALSE; break;
3944 case 'm': remap = TRUE; break;
3945 case 't': typed = TRUE; break;
3946 case 'i': insert = TRUE; break;
3947 case 'x': execute = TRUE; break;
3948 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003949 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003950 }
3951 }
3952 }
3953
3954 if (*keys != NUL || execute)
3955 {
3956 /* Need to escape K_SPECIAL and CSI before putting the string in the
3957 * typeahead buffer. */
3958 keys_esc = vim_strsave_escape_csi(keys);
3959 if (keys_esc != NULL)
3960 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003961 if (lowlevel)
3962 {
3963#ifdef USE_INPUT_BUF
3964 add_to_input_buf(keys, (int)STRLEN(keys));
3965#else
3966 emsg(_("E980: lowlevel input not supported"));
3967#endif
3968 }
3969 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003970 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003971 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003972 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003973 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003974#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003975 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003976#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003977 )
3978 typebuf_was_filled = TRUE;
3979 }
3980 vim_free(keys_esc);
3981
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003982 if (execute)
3983 {
3984 int save_msg_scroll = msg_scroll;
3985
3986 /* Avoid a 1 second delay when the keys start Insert mode. */
3987 msg_scroll = FALSE;
3988
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003989 if (!dangerous)
3990 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003991 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003992 if (!dangerous)
3993 --ex_normal_busy;
3994
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003995 msg_scroll |= save_msg_scroll;
3996 }
3997 }
3998 }
3999}
4000
4001/*
4002 * "filereadable()" function
4003 */
4004 static void
4005f_filereadable(typval_T *argvars, typval_T *rettv)
4006{
4007 int fd;
4008 char_u *p;
4009 int n;
4010
4011#ifndef O_NONBLOCK
4012# define O_NONBLOCK 0
4013#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004014 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004015 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
4016 O_RDONLY | O_NONBLOCK, 0)) >= 0)
4017 {
4018 n = TRUE;
4019 close(fd);
4020 }
4021 else
4022 n = FALSE;
4023
4024 rettv->vval.v_number = n;
4025}
4026
4027/*
4028 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
4029 * rights to write into.
4030 */
4031 static void
4032f_filewritable(typval_T *argvars, typval_T *rettv)
4033{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004034 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004035}
4036
4037 static void
4038findfilendir(
4039 typval_T *argvars UNUSED,
4040 typval_T *rettv,
4041 int find_what UNUSED)
4042{
4043#ifdef FEAT_SEARCHPATH
4044 char_u *fname;
4045 char_u *fresult = NULL;
4046 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4047 char_u *p;
4048 char_u pathbuf[NUMBUFLEN];
4049 int count = 1;
4050 int first = TRUE;
4051 int error = FALSE;
4052#endif
4053
4054 rettv->vval.v_string = NULL;
4055 rettv->v_type = VAR_STRING;
4056
4057#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004058 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004059
4060 if (argvars[1].v_type != VAR_UNKNOWN)
4061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004062 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004063 if (p == NULL)
4064 error = TRUE;
4065 else
4066 {
4067 if (*p != NUL)
4068 path = p;
4069
4070 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004071 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004072 }
4073 }
4074
4075 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4076 error = TRUE;
4077
4078 if (*fname != NUL && !error)
4079 {
4080 do
4081 {
4082 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4083 vim_free(fresult);
4084 fresult = find_file_in_path_option(first ? fname : NULL,
4085 first ? (int)STRLEN(fname) : 0,
4086 0, first, path,
4087 find_what,
4088 curbuf->b_ffname,
4089 find_what == FINDFILE_DIR
4090 ? (char_u *)"" : curbuf->b_p_sua);
4091 first = FALSE;
4092
4093 if (fresult != NULL && rettv->v_type == VAR_LIST)
4094 list_append_string(rettv->vval.v_list, fresult, -1);
4095
4096 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4097 }
4098
4099 if (rettv->v_type == VAR_STRING)
4100 rettv->vval.v_string = fresult;
4101#endif
4102}
4103
4104/*
4105 * "filter()" function
4106 */
4107 static void
4108f_filter(typval_T *argvars, typval_T *rettv)
4109{
4110 filter_map(argvars, rettv, FALSE);
4111}
4112
4113/*
4114 * "finddir({fname}[, {path}[, {count}]])" function
4115 */
4116 static void
4117f_finddir(typval_T *argvars, typval_T *rettv)
4118{
4119 findfilendir(argvars, rettv, FINDFILE_DIR);
4120}
4121
4122/*
4123 * "findfile({fname}[, {path}[, {count}]])" function
4124 */
4125 static void
4126f_findfile(typval_T *argvars, typval_T *rettv)
4127{
4128 findfilendir(argvars, rettv, FINDFILE_FILE);
4129}
4130
4131#ifdef FEAT_FLOAT
4132/*
4133 * "float2nr({float})" function
4134 */
4135 static void
4136f_float2nr(typval_T *argvars, typval_T *rettv)
4137{
4138 float_T f = 0.0;
4139
4140 if (get_float_arg(argvars, &f) == OK)
4141 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004142 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004143 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004144 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004145 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004146 else
4147 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004148 }
4149}
4150
4151/*
4152 * "floor({float})" function
4153 */
4154 static void
4155f_floor(typval_T *argvars, typval_T *rettv)
4156{
4157 float_T f = 0.0;
4158
4159 rettv->v_type = VAR_FLOAT;
4160 if (get_float_arg(argvars, &f) == OK)
4161 rettv->vval.v_float = floor(f);
4162 else
4163 rettv->vval.v_float = 0.0;
4164}
4165
4166/*
4167 * "fmod()" function
4168 */
4169 static void
4170f_fmod(typval_T *argvars, typval_T *rettv)
4171{
4172 float_T fx = 0.0, fy = 0.0;
4173
4174 rettv->v_type = VAR_FLOAT;
4175 if (get_float_arg(argvars, &fx) == OK
4176 && get_float_arg(&argvars[1], &fy) == OK)
4177 rettv->vval.v_float = fmod(fx, fy);
4178 else
4179 rettv->vval.v_float = 0.0;
4180}
4181#endif
4182
4183/*
4184 * "fnameescape({string})" function
4185 */
4186 static void
4187f_fnameescape(typval_T *argvars, typval_T *rettv)
4188{
4189 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004190 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004191 rettv->v_type = VAR_STRING;
4192}
4193
4194/*
4195 * "fnamemodify({fname}, {mods})" function
4196 */
4197 static void
4198f_fnamemodify(typval_T *argvars, typval_T *rettv)
4199{
4200 char_u *fname;
4201 char_u *mods;
4202 int usedlen = 0;
4203 int len;
4204 char_u *fbuf = NULL;
4205 char_u buf[NUMBUFLEN];
4206
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004207 fname = tv_get_string_chk(&argvars[0]);
4208 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004209 if (fname == NULL || mods == NULL)
4210 fname = NULL;
4211 else
4212 {
4213 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004214 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004215 }
4216
4217 rettv->v_type = VAR_STRING;
4218 if (fname == NULL)
4219 rettv->vval.v_string = NULL;
4220 else
4221 rettv->vval.v_string = vim_strnsave(fname, len);
4222 vim_free(fbuf);
4223}
4224
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004225/*
4226 * "foldclosed()" function
4227 */
4228 static void
4229foldclosed_both(
4230 typval_T *argvars UNUSED,
4231 typval_T *rettv,
4232 int end UNUSED)
4233{
4234#ifdef FEAT_FOLDING
4235 linenr_T lnum;
4236 linenr_T first, last;
4237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004238 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4240 {
4241 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4242 {
4243 if (end)
4244 rettv->vval.v_number = (varnumber_T)last;
4245 else
4246 rettv->vval.v_number = (varnumber_T)first;
4247 return;
4248 }
4249 }
4250#endif
4251 rettv->vval.v_number = -1;
4252}
4253
4254/*
4255 * "foldclosed()" function
4256 */
4257 static void
4258f_foldclosed(typval_T *argvars, typval_T *rettv)
4259{
4260 foldclosed_both(argvars, rettv, FALSE);
4261}
4262
4263/*
4264 * "foldclosedend()" function
4265 */
4266 static void
4267f_foldclosedend(typval_T *argvars, typval_T *rettv)
4268{
4269 foldclosed_both(argvars, rettv, TRUE);
4270}
4271
4272/*
4273 * "foldlevel()" function
4274 */
4275 static void
4276f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4277{
4278#ifdef FEAT_FOLDING
4279 linenr_T lnum;
4280
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004281 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4283 rettv->vval.v_number = foldLevel(lnum);
4284#endif
4285}
4286
4287/*
4288 * "foldtext()" function
4289 */
4290 static void
4291f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4292{
4293#ifdef FEAT_FOLDING
4294 linenr_T foldstart;
4295 linenr_T foldend;
4296 char_u *dashes;
4297 linenr_T lnum;
4298 char_u *s;
4299 char_u *r;
4300 int len;
4301 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004302 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303#endif
4304
4305 rettv->v_type = VAR_STRING;
4306 rettv->vval.v_string = NULL;
4307#ifdef FEAT_FOLDING
4308 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4309 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4310 dashes = get_vim_var_str(VV_FOLDDASHES);
4311 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4312 && dashes != NULL)
4313 {
4314 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004315 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004316 if (!linewhite(lnum))
4317 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004318
4319 /* Find interesting text in this line. */
4320 s = skipwhite(ml_get(lnum));
4321 /* skip C comment-start */
4322 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4323 {
4324 s = skipwhite(s + 2);
4325 if (*skipwhite(s) == NUL
4326 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4327 {
4328 s = skipwhite(ml_get(lnum + 1));
4329 if (*s == '*')
4330 s = skipwhite(s + 1);
4331 }
4332 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004333 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004334 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004335 r = alloc(STRLEN(txt)
4336 + STRLEN(dashes) // for %s
4337 + 20 // for %3ld
4338 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004339 if (r != NULL)
4340 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004341 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342 len = (int)STRLEN(r);
4343 STRCAT(r, s);
4344 /* remove 'foldmarker' and 'commentstring' */
4345 foldtext_cleanup(r + len);
4346 rettv->vval.v_string = r;
4347 }
4348 }
4349#endif
4350}
4351
4352/*
4353 * "foldtextresult(lnum)" function
4354 */
4355 static void
4356f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4357{
4358#ifdef FEAT_FOLDING
4359 linenr_T lnum;
4360 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004361 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362 foldinfo_T foldinfo;
4363 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004364 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365#endif
4366
4367 rettv->v_type = VAR_STRING;
4368 rettv->vval.v_string = NULL;
4369#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004370 if (entered)
4371 return; /* reject recursive use */
4372 entered = TRUE;
4373
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004374 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375 /* treat illegal types and illegal string values for {lnum} the same */
4376 if (lnum < 0)
4377 lnum = 0;
4378 fold_count = foldedCount(curwin, lnum, &foldinfo);
4379 if (fold_count > 0)
4380 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004381 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4382 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383 if (text == buf)
4384 text = vim_strsave(text);
4385 rettv->vval.v_string = text;
4386 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004387
4388 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389#endif
4390}
4391
4392/*
4393 * "foreground()" function
4394 */
4395 static void
4396f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4397{
4398#ifdef FEAT_GUI
4399 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004400 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004401 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004402 return;
4403 }
4404#endif
4405#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004406 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004407#endif
4408}
4409
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004410 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004411common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004412{
4413 char_u *s;
4414 char_u *name;
4415 int use_string = FALSE;
4416 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004417 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004418
4419 if (argvars[0].v_type == VAR_FUNC)
4420 {
4421 /* function(MyFunc, [arg], dict) */
4422 s = argvars[0].vval.v_string;
4423 }
4424 else if (argvars[0].v_type == VAR_PARTIAL
4425 && argvars[0].vval.v_partial != NULL)
4426 {
4427 /* function(dict.MyFunc, [arg]) */
4428 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004429 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004430 }
4431 else
4432 {
4433 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004434 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004435 use_string = TRUE;
4436 }
4437
Bram Moolenaar843b8842016-08-21 14:36:15 +02004438 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004439 {
4440 name = s;
4441 trans_name = trans_function_name(&name, FALSE,
4442 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4443 if (*name != NUL)
4444 s = NULL;
4445 }
4446
Bram Moolenaar843b8842016-08-21 14:36:15 +02004447 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4448 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004449 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004451 else if (trans_name != NULL && (is_funcref
4452 ? find_func(trans_name) == NULL
4453 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004454 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004455 else
4456 {
4457 int dict_idx = 0;
4458 int arg_idx = 0;
4459 list_T *list = NULL;
4460
4461 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4462 {
4463 char sid_buf[25];
4464 int off = *s == 's' ? 2 : 5;
4465
4466 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4467 * also be called from another script. Using trans_function_name()
4468 * would also work, but some plugins depend on the name being
4469 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004470 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004471 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472 if (name != NULL)
4473 {
4474 STRCPY(name, sid_buf);
4475 STRCAT(name, s + off);
4476 }
4477 }
4478 else
4479 name = vim_strsave(s);
4480
4481 if (argvars[1].v_type != VAR_UNKNOWN)
4482 {
4483 if (argvars[2].v_type != VAR_UNKNOWN)
4484 {
4485 /* function(name, [args], dict) */
4486 arg_idx = 1;
4487 dict_idx = 2;
4488 }
4489 else if (argvars[1].v_type == VAR_DICT)
4490 /* function(name, dict) */
4491 dict_idx = 1;
4492 else
4493 /* function(name, [args]) */
4494 arg_idx = 1;
4495 if (dict_idx > 0)
4496 {
4497 if (argvars[dict_idx].v_type != VAR_DICT)
4498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004499 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004501 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 }
4503 if (argvars[dict_idx].vval.v_dict == NULL)
4504 dict_idx = 0;
4505 }
4506 if (arg_idx > 0)
4507 {
4508 if (argvars[arg_idx].v_type != VAR_LIST)
4509 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004510 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004511 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004512 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004513 }
4514 list = argvars[arg_idx].vval.v_list;
4515 if (list == NULL || list->lv_len == 0)
4516 arg_idx = 0;
4517 }
4518 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004519 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004520 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004521 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004522
4523 /* result is a VAR_PARTIAL */
4524 if (pt == NULL)
4525 vim_free(name);
4526 else
4527 {
4528 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4529 {
4530 listitem_T *li;
4531 int i = 0;
4532 int arg_len = 0;
4533 int lv_len = 0;
4534
4535 if (arg_pt != NULL)
4536 arg_len = arg_pt->pt_argc;
4537 if (list != NULL)
4538 lv_len = list->lv_len;
4539 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004540 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004541 if (pt->pt_argv == NULL)
4542 {
4543 vim_free(pt);
4544 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004545 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004546 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004547 for (i = 0; i < arg_len; i++)
4548 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4549 if (lv_len > 0)
4550 for (li = list->lv_first; li != NULL;
4551 li = li->li_next)
4552 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004553 }
4554
4555 /* For "function(dict.func, [], dict)" and "func" is a partial
4556 * use "dict". That is backwards compatible. */
4557 if (dict_idx > 0)
4558 {
4559 /* The dict is bound explicitly, pt_auto is FALSE. */
4560 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4561 ++pt->pt_dict->dv_refcount;
4562 }
4563 else if (arg_pt != NULL)
4564 {
4565 /* If the dict was bound automatically the result is also
4566 * bound automatically. */
4567 pt->pt_dict = arg_pt->pt_dict;
4568 pt->pt_auto = arg_pt->pt_auto;
4569 if (pt->pt_dict != NULL)
4570 ++pt->pt_dict->dv_refcount;
4571 }
4572
4573 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004574 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4575 {
4576 pt->pt_func = arg_pt->pt_func;
4577 func_ptr_ref(pt->pt_func);
4578 vim_free(name);
4579 }
4580 else if (is_funcref)
4581 {
4582 pt->pt_func = find_func(trans_name);
4583 func_ptr_ref(pt->pt_func);
4584 vim_free(name);
4585 }
4586 else
4587 {
4588 pt->pt_name = name;
4589 func_ref(name);
4590 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004591 }
4592 rettv->v_type = VAR_PARTIAL;
4593 rettv->vval.v_partial = pt;
4594 }
4595 else
4596 {
4597 /* result is a VAR_FUNC */
4598 rettv->v_type = VAR_FUNC;
4599 rettv->vval.v_string = name;
4600 func_ref(name);
4601 }
4602 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004603theend:
4604 vim_free(trans_name);
4605}
4606
4607/*
4608 * "funcref()" function
4609 */
4610 static void
4611f_funcref(typval_T *argvars, typval_T *rettv)
4612{
4613 common_function(argvars, rettv, TRUE);
4614}
4615
4616/*
4617 * "function()" function
4618 */
4619 static void
4620f_function(typval_T *argvars, typval_T *rettv)
4621{
4622 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004623}
4624
4625/*
4626 * "garbagecollect()" function
4627 */
4628 static void
4629f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4630{
4631 /* This is postponed until we are back at the toplevel, because we may be
4632 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4633 want_garbage_collect = TRUE;
4634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004635 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004636 garbage_collect_at_exit = TRUE;
4637}
4638
4639/*
4640 * "get()" function
4641 */
4642 static void
4643f_get(typval_T *argvars, typval_T *rettv)
4644{
4645 listitem_T *li;
4646 list_T *l;
4647 dictitem_T *di;
4648 dict_T *d;
4649 typval_T *tv = NULL;
4650
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004651 if (argvars[0].v_type == VAR_BLOB)
4652 {
4653 int error = FALSE;
4654 int idx = tv_get_number_chk(&argvars[1], &error);
4655
4656 if (!error)
4657 {
4658 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004659 if (idx < 0)
4660 idx = blob_len(argvars[0].vval.v_blob) + idx;
4661 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4662 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004663 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004664 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004665 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004666 tv = rettv;
4667 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004668 }
4669 }
4670 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 {
4672 if ((l = argvars[0].vval.v_list) != NULL)
4673 {
4674 int error = FALSE;
4675
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004676 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004677 if (!error && li != NULL)
4678 tv = &li->li_tv;
4679 }
4680 }
4681 else if (argvars[0].v_type == VAR_DICT)
4682 {
4683 if ((d = argvars[0].vval.v_dict) != NULL)
4684 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004685 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004686 if (di != NULL)
4687 tv = &di->di_tv;
4688 }
4689 }
4690 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4691 {
4692 partial_T *pt;
4693 partial_T fref_pt;
4694
4695 if (argvars[0].v_type == VAR_PARTIAL)
4696 pt = argvars[0].vval.v_partial;
4697 else
4698 {
4699 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4700 fref_pt.pt_name = argvars[0].vval.v_string;
4701 pt = &fref_pt;
4702 }
4703
4704 if (pt != NULL)
4705 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004706 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004707 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004708
4709 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4710 {
4711 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004712 n = partial_name(pt);
4713 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004714 rettv->vval.v_string = NULL;
4715 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004716 {
4717 rettv->vval.v_string = vim_strsave(n);
4718 if (rettv->v_type == VAR_FUNC)
4719 func_ref(rettv->vval.v_string);
4720 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004721 }
4722 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004723 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004724 else if (STRCMP(what, "args") == 0)
4725 {
4726 rettv->v_type = VAR_LIST;
4727 if (rettv_list_alloc(rettv) == OK)
4728 {
4729 int i;
4730
4731 for (i = 0; i < pt->pt_argc; ++i)
4732 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4733 }
4734 }
4735 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004736 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004737 return;
4738 }
4739 }
4740 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004741 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004742
4743 if (tv == NULL)
4744 {
4745 if (argvars[2].v_type != VAR_UNKNOWN)
4746 copy_tv(&argvars[2], rettv);
4747 }
4748 else
4749 copy_tv(tv, rettv);
4750}
4751
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004752/*
4753 * Returns buffer options, variables and other attributes in a dictionary.
4754 */
4755 static dict_T *
4756get_buffer_info(buf_T *buf)
4757{
4758 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004759 tabpage_T *tp;
4760 win_T *wp;
4761 list_T *windows;
4762
4763 dict = dict_alloc();
4764 if (dict == NULL)
4765 return NULL;
4766
Bram Moolenaare0be1672018-07-08 16:50:37 +02004767 dict_add_number(dict, "bufnr", buf->b_fnum);
4768 dict_add_string(dict, "name", buf->b_ffname);
4769 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4770 : buflist_findlnum(buf));
4771 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4772 dict_add_number(dict, "listed", buf->b_p_bl);
4773 dict_add_number(dict, "changed", bufIsChanged(buf));
4774 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4775 dict_add_number(dict, "hidden",
4776 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004777
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004778 /* Get a reference to buffer variables */
4779 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004780
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004781 /* List of windows displaying this buffer */
4782 windows = list_alloc();
4783 if (windows != NULL)
4784 {
4785 FOR_ALL_TAB_WINDOWS(tp, wp)
4786 if (wp->w_buffer == buf)
4787 list_append_number(windows, (varnumber_T)wp->w_id);
4788 dict_add_list(dict, "windows", windows);
4789 }
4790
4791#ifdef FEAT_SIGNS
4792 if (buf->b_signlist != NULL)
4793 {
4794 /* List of signs placed in this buffer */
4795 list_T *signs = list_alloc();
4796 if (signs != NULL)
4797 {
4798 get_buffer_signs(buf, signs);
4799 dict_add_list(dict, "signs", signs);
4800 }
4801 }
4802#endif
4803
4804 return dict;
4805}
4806
4807/*
4808 * "getbufinfo()" function
4809 */
4810 static void
4811f_getbufinfo(typval_T *argvars, typval_T *rettv)
4812{
4813 buf_T *buf = NULL;
4814 buf_T *argbuf = NULL;
4815 dict_T *d;
4816 int filtered = FALSE;
4817 int sel_buflisted = FALSE;
4818 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004819 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004820
4821 if (rettv_list_alloc(rettv) != OK)
4822 return;
4823
4824 /* List of all the buffers or selected buffers */
4825 if (argvars[0].v_type == VAR_DICT)
4826 {
4827 dict_T *sel_d = argvars[0].vval.v_dict;
4828
4829 if (sel_d != NULL)
4830 {
4831 dictitem_T *di;
4832
4833 filtered = TRUE;
4834
4835 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004836 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004837 sel_buflisted = TRUE;
4838
4839 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004840 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004841 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004842
4843 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004844 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004845 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004846 }
4847 }
4848 else if (argvars[0].v_type != VAR_UNKNOWN)
4849 {
4850 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004851 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004852 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004853 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004854 --emsg_off;
4855 if (argbuf == NULL)
4856 return;
4857 }
4858
4859 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004860 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004861 {
4862 if (argbuf != NULL && argbuf != buf)
4863 continue;
4864 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004865 || (sel_buflisted && !buf->b_p_bl)
4866 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004867 continue;
4868
4869 d = get_buffer_info(buf);
4870 if (d != NULL)
4871 list_append_dict(rettv->vval.v_list, d);
4872 if (argbuf != NULL)
4873 return;
4874 }
4875}
4876
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877/*
4878 * Get line or list of lines from buffer "buf" into "rettv".
4879 * Return a range (from start to end) of lines in rettv from the specified
4880 * buffer.
4881 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4882 */
4883 static void
4884get_buffer_lines(
4885 buf_T *buf,
4886 linenr_T start,
4887 linenr_T end,
4888 int retlist,
4889 typval_T *rettv)
4890{
4891 char_u *p;
4892
4893 rettv->v_type = VAR_STRING;
4894 rettv->vval.v_string = NULL;
4895 if (retlist && rettv_list_alloc(rettv) == FAIL)
4896 return;
4897
4898 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4899 return;
4900
4901 if (!retlist)
4902 {
4903 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4904 p = ml_get_buf(buf, start, FALSE);
4905 else
4906 p = (char_u *)"";
4907 rettv->vval.v_string = vim_strsave(p);
4908 }
4909 else
4910 {
4911 if (end < start)
4912 return;
4913
4914 if (start < 1)
4915 start = 1;
4916 if (end > buf->b_ml.ml_line_count)
4917 end = buf->b_ml.ml_line_count;
4918 while (start <= end)
4919 if (list_append_string(rettv->vval.v_list,
4920 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4921 break;
4922 }
4923}
4924
4925/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926 * "getbufline()" function
4927 */
4928 static void
4929f_getbufline(typval_T *argvars, typval_T *rettv)
4930{
4931 linenr_T lnum;
4932 linenr_T end;
4933 buf_T *buf;
4934
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004935 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004936 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004937 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 --emsg_off;
4939
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004940 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004941 if (argvars[2].v_type == VAR_UNKNOWN)
4942 end = lnum;
4943 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004944 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004945
4946 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4947}
4948
4949/*
4950 * "getbufvar()" function
4951 */
4952 static void
4953f_getbufvar(typval_T *argvars, typval_T *rettv)
4954{
4955 buf_T *buf;
4956 buf_T *save_curbuf;
4957 char_u *varname;
4958 dictitem_T *v;
4959 int done = FALSE;
4960
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004961 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4962 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004963 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004964 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004965
4966 rettv->v_type = VAR_STRING;
4967 rettv->vval.v_string = NULL;
4968
4969 if (buf != NULL && varname != NULL)
4970 {
4971 /* set curbuf to be our buf, temporarily */
4972 save_curbuf = curbuf;
4973 curbuf = buf;
4974
Bram Moolenaar30567352016-08-27 21:25:44 +02004975 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004976 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004977 if (varname[1] == NUL)
4978 {
4979 /* get all buffer-local options in a dict */
4980 dict_T *opts = get_winbuf_options(TRUE);
4981
4982 if (opts != NULL)
4983 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004984 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004985 done = TRUE;
4986 }
4987 }
4988 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4989 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004990 done = TRUE;
4991 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004992 else
4993 {
4994 /* Look up the variable. */
4995 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4996 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4997 'b', varname, FALSE);
4998 if (v != NULL)
4999 {
5000 copy_tv(&v->di_tv, rettv);
5001 done = TRUE;
5002 }
5003 }
5004
5005 /* restore previous notion of curbuf */
5006 curbuf = save_curbuf;
5007 }
5008
5009 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5010 /* use the default value */
5011 copy_tv(&argvars[2], rettv);
5012
5013 --emsg_off;
5014}
5015
5016/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005017 * "getchangelist()" function
5018 */
5019 static void
5020f_getchangelist(typval_T *argvars, typval_T *rettv)
5021{
5022#ifdef FEAT_JUMPLIST
5023 buf_T *buf;
5024 int i;
5025 list_T *l;
5026 dict_T *d;
5027#endif
5028
5029 if (rettv_list_alloc(rettv) != OK)
5030 return;
5031
5032#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005033 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005034 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01005035 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01005036 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005037 if (buf == NULL)
5038 return;
5039
5040 l = list_alloc();
5041 if (l == NULL)
5042 return;
5043
5044 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5045 return;
5046 /*
5047 * The current window change list index tracks only the position in the
5048 * current buffer change list. For other buffers, use the change list
5049 * length as the current index.
5050 */
5051 list_append_number(rettv->vval.v_list,
5052 (varnumber_T)((buf == curwin->w_buffer)
5053 ? curwin->w_changelistidx : buf->b_changelistlen));
5054
5055 for (i = 0; i < buf->b_changelistlen; ++i)
5056 {
5057 if (buf->b_changelist[i].lnum == 0)
5058 continue;
5059 if ((d = dict_alloc()) == NULL)
5060 return;
5061 if (list_append_dict(l, d) == FAIL)
5062 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005063 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5064 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005065 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005066 }
5067#endif
5068}
5069/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005070 * "getchar()" function
5071 */
5072 static void
5073f_getchar(typval_T *argvars, typval_T *rettv)
5074{
5075 varnumber_T n;
5076 int error = FALSE;
5077
Bram Moolenaar84d93902018-09-11 20:10:20 +02005078#ifdef MESSAGE_QUEUE
5079 // vpeekc() used to check for messages, but that caused problems, invoking
5080 // a callback where it was not expected. Some plugins use getchar(1) in a
5081 // loop to await a message, therefore make sure we check for messages here.
5082 parse_queued_messages();
5083#endif
5084
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 /* Position the cursor. Needed after a message that ends in a space. */
5086 windgoto(msg_row, msg_col);
5087
5088 ++no_mapping;
5089 ++allow_keys;
5090 for (;;)
5091 {
5092 if (argvars[0].v_type == VAR_UNKNOWN)
5093 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005094 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005095 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005096 /* getchar(1): only check if char avail */
5097 n = vpeekc_any();
5098 else if (error || vpeekc_any() == NUL)
5099 /* illegal argument or getchar(0) and no char avail: return zero */
5100 n = 0;
5101 else
5102 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005103 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005104
5105 if (n == K_IGNORE)
5106 continue;
5107 break;
5108 }
5109 --no_mapping;
5110 --allow_keys;
5111
5112 set_vim_var_nr(VV_MOUSE_WIN, 0);
5113 set_vim_var_nr(VV_MOUSE_WINID, 0);
5114 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5115 set_vim_var_nr(VV_MOUSE_COL, 0);
5116
5117 rettv->vval.v_number = n;
5118 if (IS_SPECIAL(n) || mod_mask != 0)
5119 {
5120 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5121 int i = 0;
5122
5123 /* Turn a special key into three bytes, plus modifier. */
5124 if (mod_mask != 0)
5125 {
5126 temp[i++] = K_SPECIAL;
5127 temp[i++] = KS_MODIFIER;
5128 temp[i++] = mod_mask;
5129 }
5130 if (IS_SPECIAL(n))
5131 {
5132 temp[i++] = K_SPECIAL;
5133 temp[i++] = K_SECOND(n);
5134 temp[i++] = K_THIRD(n);
5135 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005136 else if (has_mbyte)
5137 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005138 else
5139 temp[i++] = n;
5140 temp[i++] = NUL;
5141 rettv->v_type = VAR_STRING;
5142 rettv->vval.v_string = vim_strsave(temp);
5143
5144#ifdef FEAT_MOUSE
5145 if (is_mouse_key(n))
5146 {
5147 int row = mouse_row;
5148 int col = mouse_col;
5149 win_T *win;
5150 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005151 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005152 int winnr = 1;
5153
5154 if (row >= 0 && col >= 0)
5155 {
5156 /* Find the window at the mouse coordinates and compute the
5157 * text position. */
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005158 win = mouse_find_win(&row, &col, FIND_POPUP);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005159 if (win == NULL)
5160 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005161 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar451d4b52019-06-12 20:22:27 +02005162# ifdef FEAT_TEXT_PROP
5163 if (bt_popup(win->w_buffer))
5164 winnr = 0;
5165 else
5166# endif
5167 for (wp = firstwin; wp != win && wp != NULL;
5168 wp = wp->w_next)
5169 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005170 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5171 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5172 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5173 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5174 }
5175 }
5176#endif
5177 }
5178}
5179
5180/*
5181 * "getcharmod()" function
5182 */
5183 static void
5184f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5185{
5186 rettv->vval.v_number = mod_mask;
5187}
5188
5189/*
5190 * "getcharsearch()" function
5191 */
5192 static void
5193f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5194{
5195 if (rettv_dict_alloc(rettv) != FAIL)
5196 {
5197 dict_T *dict = rettv->vval.v_dict;
5198
Bram Moolenaare0be1672018-07-08 16:50:37 +02005199 dict_add_string(dict, "char", last_csearch());
5200 dict_add_number(dict, "forward", last_csearch_forward());
5201 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005202 }
5203}
5204
5205/*
5206 * "getcmdline()" function
5207 */
5208 static void
5209f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5210{
5211 rettv->v_type = VAR_STRING;
5212 rettv->vval.v_string = get_cmdline_str();
5213}
5214
5215/*
5216 * "getcmdpos()" function
5217 */
5218 static void
5219f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5220{
5221 rettv->vval.v_number = get_cmdline_pos() + 1;
5222}
5223
5224/*
5225 * "getcmdtype()" function
5226 */
5227 static void
5228f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5229{
5230 rettv->v_type = VAR_STRING;
5231 rettv->vval.v_string = alloc(2);
5232 if (rettv->vval.v_string != NULL)
5233 {
5234 rettv->vval.v_string[0] = get_cmdline_type();
5235 rettv->vval.v_string[1] = NUL;
5236 }
5237}
5238
5239/*
5240 * "getcmdwintype()" function
5241 */
5242 static void
5243f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5244{
5245 rettv->v_type = VAR_STRING;
5246 rettv->vval.v_string = NULL;
5247#ifdef FEAT_CMDWIN
5248 rettv->vval.v_string = alloc(2);
5249 if (rettv->vval.v_string != NULL)
5250 {
5251 rettv->vval.v_string[0] = cmdwin_type;
5252 rettv->vval.v_string[1] = NUL;
5253 }
5254#endif
5255}
5256
5257#if defined(FEAT_CMDL_COMPL)
5258/*
5259 * "getcompletion()" function
5260 */
5261 static void
5262f_getcompletion(typval_T *argvars, typval_T *rettv)
5263{
5264 char_u *pat;
5265 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005266 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005267 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5268 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005270 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005271 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005272
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005273 if (p_wic)
5274 options |= WILD_ICASE;
5275
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005276 /* For filtered results, 'wildignore' is used */
5277 if (!filtered)
5278 options |= WILD_KEEP_ALL;
5279
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005280 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005281 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005282 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005283 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005284 if (xpc.xp_context == EXPAND_NOTHING)
5285 {
5286 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005287 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005289 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005290 return;
5291 }
5292
5293# if defined(FEAT_MENU)
5294 if (xpc.xp_context == EXPAND_MENUS)
5295 {
5296 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5297 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5298 }
5299# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005300#ifdef FEAT_CSCOPE
5301 if (xpc.xp_context == EXPAND_CSCOPE)
5302 {
5303 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5304 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5305 }
5306#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005307#ifdef FEAT_SIGNS
5308 if (xpc.xp_context == EXPAND_SIGN)
5309 {
5310 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5311 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5312 }
5313#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314
5315 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5316 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5317 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005318 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005319
5320 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5321
5322 for (i = 0; i < xpc.xp_numfiles; i++)
5323 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5324 }
5325 vim_free(pat);
5326 ExpandCleanup(&xpc);
5327}
5328#endif
5329
5330/*
5331 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005332 *
5333 * Return the current working directory of a window in a tab page.
5334 * First optional argument 'winnr' is the window number or -1 and the second
5335 * optional argument 'tabnr' is the tab page number.
5336 *
5337 * If no arguments are supplied, then return the directory of the current
5338 * window.
5339 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5340 * the specified window.
5341 * If 'winnr' is 0 then return the directory of the current window.
5342 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5343 * directory of the specified tab page. Otherwise return the directory of the
5344 * specified window in the specified tab page.
5345 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005346 */
5347 static void
5348f_getcwd(typval_T *argvars, typval_T *rettv)
5349{
5350 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005351 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005352 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005353 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354
5355 rettv->v_type = VAR_STRING;
5356 rettv->vval.v_string = NULL;
5357
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005358 if (argvars[0].v_type == VAR_NUMBER
5359 && argvars[0].vval.v_number == -1
5360 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005361 global = TRUE;
5362 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005363 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005364
5365 if (wp != NULL && wp->w_localdir != NULL)
5366 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005367 else if (tp != NULL && tp->tp_localdir != NULL)
5368 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5369 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005371 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005372 rettv->vval.v_string = vim_strsave(globaldir);
5373 else
5374 {
5375 cwd = alloc(MAXPATHL);
5376 if (cwd != NULL)
5377 {
5378 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5379 rettv->vval.v_string = vim_strsave(cwd);
5380 vim_free(cwd);
5381 }
5382 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005383 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005384#ifdef BACKSLASH_IN_FILENAME
5385 if (rettv->vval.v_string != NULL)
5386 slash_adjust(rettv->vval.v_string);
5387#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388}
5389
5390/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005391 * "getenv()" function
5392 */
5393 static void
5394f_getenv(typval_T *argvars, typval_T *rettv)
5395{
5396 int mustfree = FALSE;
5397 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5398
5399 if (p == NULL)
5400 {
5401 rettv->v_type = VAR_SPECIAL;
5402 rettv->vval.v_number = VVAL_NULL;
5403 return;
5404 }
5405 if (!mustfree)
5406 p = vim_strsave(p);
5407 rettv->vval.v_string = p;
5408 rettv->v_type = VAR_STRING;
5409}
5410
5411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005412 * "getfontname()" function
5413 */
5414 static void
5415f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5416{
5417 rettv->v_type = VAR_STRING;
5418 rettv->vval.v_string = NULL;
5419#ifdef FEAT_GUI
5420 if (gui.in_use)
5421 {
5422 GuiFont font;
5423 char_u *name = NULL;
5424
5425 if (argvars[0].v_type == VAR_UNKNOWN)
5426 {
5427 /* Get the "Normal" font. Either the name saved by
5428 * hl_set_font_name() or from the font ID. */
5429 font = gui.norm_font;
5430 name = hl_get_font_name();
5431 }
5432 else
5433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005434 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005435 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5436 return;
5437 font = gui_mch_get_font(name, FALSE);
5438 if (font == NOFONT)
5439 return; /* Invalid font name, return empty string. */
5440 }
5441 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5442 if (argvars[0].v_type != VAR_UNKNOWN)
5443 gui_mch_free_font(font);
5444 }
5445#endif
5446}
5447
5448/*
5449 * "getfperm({fname})" function
5450 */
5451 static void
5452f_getfperm(typval_T *argvars, typval_T *rettv)
5453{
5454 char_u *fname;
5455 stat_T st;
5456 char_u *perm = NULL;
5457 char_u flags[] = "rwx";
5458 int i;
5459
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005460 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005461
5462 rettv->v_type = VAR_STRING;
5463 if (mch_stat((char *)fname, &st) >= 0)
5464 {
5465 perm = vim_strsave((char_u *)"---------");
5466 if (perm != NULL)
5467 {
5468 for (i = 0; i < 9; i++)
5469 {
5470 if (st.st_mode & (1 << (8 - i)))
5471 perm[i] = flags[i % 3];
5472 }
5473 }
5474 }
5475 rettv->vval.v_string = perm;
5476}
5477
5478/*
5479 * "getfsize({fname})" function
5480 */
5481 static void
5482f_getfsize(typval_T *argvars, typval_T *rettv)
5483{
5484 char_u *fname;
5485 stat_T st;
5486
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005487 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488
5489 rettv->v_type = VAR_NUMBER;
5490
5491 if (mch_stat((char *)fname, &st) >= 0)
5492 {
5493 if (mch_isdir(fname))
5494 rettv->vval.v_number = 0;
5495 else
5496 {
5497 rettv->vval.v_number = (varnumber_T)st.st_size;
5498
5499 /* non-perfect check for overflow */
5500 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5501 rettv->vval.v_number = -2;
5502 }
5503 }
5504 else
5505 rettv->vval.v_number = -1;
5506}
5507
5508/*
5509 * "getftime({fname})" function
5510 */
5511 static void
5512f_getftime(typval_T *argvars, typval_T *rettv)
5513{
5514 char_u *fname;
5515 stat_T st;
5516
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005517 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005518
5519 if (mch_stat((char *)fname, &st) >= 0)
5520 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5521 else
5522 rettv->vval.v_number = -1;
5523}
5524
5525/*
5526 * "getftype({fname})" function
5527 */
5528 static void
5529f_getftype(typval_T *argvars, typval_T *rettv)
5530{
5531 char_u *fname;
5532 stat_T st;
5533 char_u *type = NULL;
5534 char *t;
5535
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005536 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005537
5538 rettv->v_type = VAR_STRING;
5539 if (mch_lstat((char *)fname, &st) >= 0)
5540 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005541 if (S_ISREG(st.st_mode))
5542 t = "file";
5543 else if (S_ISDIR(st.st_mode))
5544 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005545 else if (S_ISLNK(st.st_mode))
5546 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 else if (S_ISBLK(st.st_mode))
5548 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005549 else if (S_ISCHR(st.st_mode))
5550 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005551 else if (S_ISFIFO(st.st_mode))
5552 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005554 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005555 else
5556 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 type = vim_strsave((char_u *)t);
5558 }
5559 rettv->vval.v_string = type;
5560}
5561
5562/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005563 * "getjumplist()" function
5564 */
5565 static void
5566f_getjumplist(typval_T *argvars, typval_T *rettv)
5567{
5568#ifdef FEAT_JUMPLIST
5569 win_T *wp;
5570 int i;
5571 list_T *l;
5572 dict_T *d;
5573#endif
5574
5575 if (rettv_list_alloc(rettv) != OK)
5576 return;
5577
5578#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005579 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005580 if (wp == NULL)
5581 return;
5582
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005583 cleanup_jumplist(wp, TRUE);
5584
Bram Moolenaar4f505882018-02-10 21:06:32 +01005585 l = list_alloc();
5586 if (l == NULL)
5587 return;
5588
5589 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5590 return;
5591 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5592
5593 for (i = 0; i < wp->w_jumplistlen; ++i)
5594 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005595 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5596 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005597 if ((d = dict_alloc()) == NULL)
5598 return;
5599 if (list_append_dict(l, d) == FAIL)
5600 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005601 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5602 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005603 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005604 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005605 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005606 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005607 }
5608#endif
5609}
5610
5611/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005612 * "getline(lnum, [end])" function
5613 */
5614 static void
5615f_getline(typval_T *argvars, typval_T *rettv)
5616{
5617 linenr_T lnum;
5618 linenr_T end;
5619 int retlist;
5620
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005621 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005622 if (argvars[1].v_type == VAR_UNKNOWN)
5623 {
5624 end = 0;
5625 retlist = FALSE;
5626 }
5627 else
5628 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005629 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 retlist = TRUE;
5631 }
5632
5633 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5634}
5635
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005636#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005637 static void
5638get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5639{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005640 if (what_arg->v_type == VAR_UNKNOWN)
5641 {
5642 if (rettv_list_alloc(rettv) == OK)
5643 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005644 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005645 }
5646 else
5647 {
5648 if (rettv_dict_alloc(rettv) == OK)
5649 if (is_qf || (wp != NULL))
5650 {
5651 if (what_arg->v_type == VAR_DICT)
5652 {
5653 dict_T *d = what_arg->vval.v_dict;
5654
5655 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005656 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005657 }
5658 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005659 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005660 }
5661 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005662}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005663#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005664
5665/*
5666 * "getloclist()" function
5667 */
5668 static void
5669f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5670{
5671#ifdef FEAT_QUICKFIX
5672 win_T *wp;
5673
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005674 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005675 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5676#endif
5677}
5678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005679/*
5680 * "getmatches()" function
5681 */
5682 static void
5683f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5684{
5685#ifdef FEAT_SEARCH_EXTRA
5686 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005687 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005688 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005689 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005690
Bram Moolenaaraff74912019-03-30 18:11:49 +01005691 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5692 return;
5693
5694 cur = win->w_match_head;
5695 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005697 dict = dict_alloc();
5698 if (dict == NULL)
5699 return;
5700 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005701 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005702 /* match added with matchaddpos() */
5703 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005704 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005705 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005706 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005707 list_T *l;
5708
5709 llpos = &cur->pos.pos[i];
5710 if (llpos->lnum == 0)
5711 break;
5712 l = list_alloc();
5713 if (l == NULL)
5714 break;
5715 list_append_number(l, (varnumber_T)llpos->lnum);
5716 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005717 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005718 list_append_number(l, (varnumber_T)llpos->col);
5719 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005720 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005721 sprintf(buf, "pos%d", i + 1);
5722 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005723 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005725 else
5726 {
5727 dict_add_string(dict, "pattern", cur->pattern);
5728 }
5729 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5730 dict_add_number(dict, "priority", (long)cur->priority);
5731 dict_add_number(dict, "id", (long)cur->id);
5732# if defined(FEAT_CONCEAL)
5733 if (cur->conceal_char)
5734 {
5735 char_u buf[MB_MAXBYTES + 1];
5736
5737 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5738 dict_add_string(dict, "conceal", (char_u *)&buf);
5739 }
5740# endif
5741 list_append_dict(rettv->vval.v_list, dict);
5742 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005743 }
5744#endif
5745}
5746
5747/*
5748 * "getpid()" function
5749 */
5750 static void
5751f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5752{
5753 rettv->vval.v_number = mch_get_pid();
5754}
5755
5756 static void
5757getpos_both(
5758 typval_T *argvars,
5759 typval_T *rettv,
5760 int getcurpos)
5761{
5762 pos_T *fp;
5763 list_T *l;
5764 int fnum = -1;
5765
5766 if (rettv_list_alloc(rettv) == OK)
5767 {
5768 l = rettv->vval.v_list;
5769 if (getcurpos)
5770 fp = &curwin->w_cursor;
5771 else
5772 fp = var2fpos(&argvars[0], TRUE, &fnum);
5773 if (fnum != -1)
5774 list_append_number(l, (varnumber_T)fnum);
5775 else
5776 list_append_number(l, (varnumber_T)0);
5777 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5778 : (varnumber_T)0);
5779 list_append_number(l, (fp != NULL)
5780 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5781 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005782 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005783 (varnumber_T)0);
5784 if (getcurpos)
5785 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005786 int save_set_curswant = curwin->w_set_curswant;
5787 colnr_T save_curswant = curwin->w_curswant;
5788 colnr_T save_virtcol = curwin->w_virtcol;
5789
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790 update_curswant();
5791 list_append_number(l, curwin->w_curswant == MAXCOL ?
5792 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005793
5794 // Do not change "curswant", as it is unexpected that a get
5795 // function has a side effect.
5796 if (save_set_curswant)
5797 {
5798 curwin->w_set_curswant = save_set_curswant;
5799 curwin->w_curswant = save_curswant;
5800 curwin->w_virtcol = save_virtcol;
5801 curwin->w_valid &= ~VALID_VIRTCOL;
5802 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005803 }
5804 }
5805 else
5806 rettv->vval.v_number = FALSE;
5807}
5808
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005809/*
5810 * "getcurpos()" function
5811 */
5812 static void
5813f_getcurpos(typval_T *argvars, typval_T *rettv)
5814{
5815 getpos_both(argvars, rettv, TRUE);
5816}
5817
5818/*
5819 * "getpos(string)" function
5820 */
5821 static void
5822f_getpos(typval_T *argvars, typval_T *rettv)
5823{
5824 getpos_both(argvars, rettv, FALSE);
5825}
5826
5827/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005828 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005829 */
5830 static void
5831f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5832{
5833#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005834 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005835#endif
5836}
5837
5838/*
5839 * "getreg()" function
5840 */
5841 static void
5842f_getreg(typval_T *argvars, typval_T *rettv)
5843{
5844 char_u *strregname;
5845 int regname;
5846 int arg2 = FALSE;
5847 int return_list = FALSE;
5848 int error = FALSE;
5849
5850 if (argvars[0].v_type != VAR_UNKNOWN)
5851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005852 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005853 error = strregname == NULL;
5854 if (argvars[1].v_type != VAR_UNKNOWN)
5855 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005856 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005857 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005858 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005859 }
5860 }
5861 else
5862 strregname = get_vim_var_str(VV_REG);
5863
5864 if (error)
5865 return;
5866
5867 regname = (strregname == NULL ? '"' : *strregname);
5868 if (regname == 0)
5869 regname = '"';
5870
5871 if (return_list)
5872 {
5873 rettv->v_type = VAR_LIST;
5874 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5875 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5876 if (rettv->vval.v_list == NULL)
5877 (void)rettv_list_alloc(rettv);
5878 else
5879 ++rettv->vval.v_list->lv_refcount;
5880 }
5881 else
5882 {
5883 rettv->v_type = VAR_STRING;
5884 rettv->vval.v_string = get_reg_contents(regname,
5885 arg2 ? GREG_EXPR_SRC : 0);
5886 }
5887}
5888
5889/*
5890 * "getregtype()" function
5891 */
5892 static void
5893f_getregtype(typval_T *argvars, typval_T *rettv)
5894{
5895 char_u *strregname;
5896 int regname;
5897 char_u buf[NUMBUFLEN + 2];
5898 long reglen = 0;
5899
5900 if (argvars[0].v_type != VAR_UNKNOWN)
5901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005902 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005903 if (strregname == NULL) /* type error; errmsg already given */
5904 {
5905 rettv->v_type = VAR_STRING;
5906 rettv->vval.v_string = NULL;
5907 return;
5908 }
5909 }
5910 else
5911 /* Default to v:register */
5912 strregname = get_vim_var_str(VV_REG);
5913
5914 regname = (strregname == NULL ? '"' : *strregname);
5915 if (regname == 0)
5916 regname = '"';
5917
5918 buf[0] = NUL;
5919 buf[1] = NUL;
5920 switch (get_reg_type(regname, &reglen))
5921 {
5922 case MLINE: buf[0] = 'V'; break;
5923 case MCHAR: buf[0] = 'v'; break;
5924 case MBLOCK:
5925 buf[0] = Ctrl_V;
5926 sprintf((char *)buf + 1, "%ld", reglen + 1);
5927 break;
5928 }
5929 rettv->v_type = VAR_STRING;
5930 rettv->vval.v_string = vim_strsave(buf);
5931}
5932
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005933/*
5934 * Returns information (variables, options, etc.) about a tab page
5935 * as a dictionary.
5936 */
5937 static dict_T *
5938get_tabpage_info(tabpage_T *tp, int tp_idx)
5939{
5940 win_T *wp;
5941 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005942 list_T *l;
5943
5944 dict = dict_alloc();
5945 if (dict == NULL)
5946 return NULL;
5947
Bram Moolenaare0be1672018-07-08 16:50:37 +02005948 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005949
5950 l = list_alloc();
5951 if (l != NULL)
5952 {
5953 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5954 wp; wp = wp->w_next)
5955 list_append_number(l, (varnumber_T)wp->w_id);
5956 dict_add_list(dict, "windows", l);
5957 }
5958
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005959 /* Make a reference to tabpage variables */
5960 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005961
5962 return dict;
5963}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005964
5965/*
5966 * "gettabinfo()" function
5967 */
5968 static void
5969f_gettabinfo(typval_T *argvars, typval_T *rettv)
5970{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005971 tabpage_T *tp, *tparg = NULL;
5972 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005973 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005974
5975 if (rettv_list_alloc(rettv) != OK)
5976 return;
5977
5978 if (argvars[0].v_type != VAR_UNKNOWN)
5979 {
5980 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005981 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005982 if (tparg == NULL)
5983 return;
5984 }
5985
5986 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005987 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005988 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005989 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005990 if (tparg != NULL && tp != tparg)
5991 continue;
5992 d = get_tabpage_info(tp, tpnr);
5993 if (d != NULL)
5994 list_append_dict(rettv->vval.v_list, d);
5995 if (tparg != NULL)
5996 return;
5997 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005998}
5999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006000/*
6001 * "gettabvar()" function
6002 */
6003 static void
6004f_gettabvar(typval_T *argvars, typval_T *rettv)
6005{
6006 win_T *oldcurwin;
6007 tabpage_T *tp, *oldtabpage;
6008 dictitem_T *v;
6009 char_u *varname;
6010 int done = FALSE;
6011
6012 rettv->v_type = VAR_STRING;
6013 rettv->vval.v_string = NULL;
6014
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006015 varname = tv_get_string_chk(&argvars[1]);
6016 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006017 if (tp != NULL && varname != NULL)
6018 {
6019 /* Set tp to be our tabpage, temporarily. Also set the window to the
6020 * first window in the tabpage, otherwise the window is not valid. */
6021 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02006022 tp == curtab || tp->tp_firstwin == NULL ? firstwin
6023 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006024 {
6025 /* look up the variable */
6026 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
6027 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
6028 if (v != NULL)
6029 {
6030 copy_tv(&v->di_tv, rettv);
6031 done = TRUE;
6032 }
6033 }
6034
6035 /* restore previous notion of curwin */
6036 restore_win(oldcurwin, oldtabpage, TRUE);
6037 }
6038
6039 if (!done && argvars[2].v_type != VAR_UNKNOWN)
6040 copy_tv(&argvars[2], rettv);
6041}
6042
6043/*
6044 * "gettabwinvar()" function
6045 */
6046 static void
6047f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6048{
6049 getwinvar(argvars, rettv, 1);
6050}
6051
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006052/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006053 * "gettagstack()" function
6054 */
6055 static void
6056f_gettagstack(typval_T *argvars, typval_T *rettv)
6057{
6058 win_T *wp = curwin; // default is current window
6059
6060 if (rettv_dict_alloc(rettv) != OK)
6061 return;
6062
6063 if (argvars[0].v_type != VAR_UNKNOWN)
6064 {
6065 wp = find_win_by_nr_or_id(&argvars[0]);
6066 if (wp == NULL)
6067 return;
6068 }
6069
6070 get_tagstack(wp, rettv->vval.v_dict);
6071}
6072
6073/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006074 * Returns information about a window as a dictionary.
6075 */
6076 static dict_T *
6077get_win_info(win_T *wp, short tpnr, short winnr)
6078{
6079 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006080
6081 dict = dict_alloc();
6082 if (dict == NULL)
6083 return NULL;
6084
Bram Moolenaare0be1672018-07-08 16:50:37 +02006085 dict_add_number(dict, "tabnr", tpnr);
6086 dict_add_number(dict, "winnr", winnr);
6087 dict_add_number(dict, "winid", wp->w_id);
6088 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006089 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006090 dict_add_number(dict, "topline", wp->w_topline);
6091 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006092#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006093 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006094#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006095 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006096 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006097 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006098
Bram Moolenaar69905d12017-08-13 18:14:47 +02006099#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006100 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006101#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006102#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006103 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6104 dict_add_number(dict, "loclist",
6105 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006106#endif
6107
Bram Moolenaar30567352016-08-27 21:25:44 +02006108 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006109 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006110
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006111 return dict;
6112}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006113
6114/*
6115 * "getwininfo()" function
6116 */
6117 static void
6118f_getwininfo(typval_T *argvars, typval_T *rettv)
6119{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006120 tabpage_T *tp;
6121 win_T *wp = NULL, *wparg = NULL;
6122 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006123 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006124
6125 if (rettv_list_alloc(rettv) != OK)
6126 return;
6127
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006128 if (argvars[0].v_type != VAR_UNKNOWN)
6129 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006130 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006131 if (wparg == NULL)
6132 return;
6133 }
6134
6135 /* Collect information about either all the windows across all the tab
6136 * pages or one particular window.
6137 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006138 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006139 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006140 tabnr++;
6141 winnr = 0;
6142 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006143 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006144 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006145 if (wparg != NULL && wp != wparg)
6146 continue;
6147 d = get_win_info(wp, tabnr, winnr);
6148 if (d != NULL)
6149 list_append_dict(rettv->vval.v_list, d);
6150 if (wparg != NULL)
6151 /* found information about a specific window */
6152 return;
6153 }
6154 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006155}
6156
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006157/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006158 * "win_execute()" function
6159 */
6160 static void
6161f_win_execute(typval_T *argvars, typval_T *rettv)
6162{
6163 int id = (int)tv_get_number(argvars);
6164 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006165 win_T *save_curwin;
6166 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006167
6168 if (wp != NULL)
6169 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006170 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6171 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006172 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006173 check_cursor();
6174 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006175 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006176 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006177 }
6178}
6179
6180/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006181 * "win_findbuf()" function
6182 */
6183 static void
6184f_win_findbuf(typval_T *argvars, typval_T *rettv)
6185{
6186 if (rettv_list_alloc(rettv) != FAIL)
6187 win_findbuf(argvars, rettv->vval.v_list);
6188}
6189
6190/*
6191 * "win_getid()" function
6192 */
6193 static void
6194f_win_getid(typval_T *argvars, typval_T *rettv)
6195{
6196 rettv->vval.v_number = win_getid(argvars);
6197}
6198
6199/*
6200 * "win_gotoid()" function
6201 */
6202 static void
6203f_win_gotoid(typval_T *argvars, typval_T *rettv)
6204{
6205 rettv->vval.v_number = win_gotoid(argvars);
6206}
6207
6208/*
6209 * "win_id2tabwin()" function
6210 */
6211 static void
6212f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6213{
6214 if (rettv_list_alloc(rettv) != FAIL)
6215 win_id2tabwin(argvars, rettv->vval.v_list);
6216}
6217
6218/*
6219 * "win_id2win()" function
6220 */
6221 static void
6222f_win_id2win(typval_T *argvars, typval_T *rettv)
6223{
6224 rettv->vval.v_number = win_id2win(argvars);
6225}
6226
6227/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006228 * "win_screenpos()" function
6229 */
6230 static void
6231f_win_screenpos(typval_T *argvars, typval_T *rettv)
6232{
6233 win_T *wp;
6234
6235 if (rettv_list_alloc(rettv) == FAIL)
6236 return;
6237
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006238 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006239 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6240 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6241}
6242
6243/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006244 * "getwinpos({timeout})" function
6245 */
6246 static void
6247f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6248{
6249 int x = -1;
6250 int y = -1;
6251
6252 if (rettv_list_alloc(rettv) == FAIL)
6253 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006254#if defined(FEAT_GUI) \
6255 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6256 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006257 {
6258 varnumber_T timeout = 100;
6259
6260 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006261 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006262
6263 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006264 }
6265#endif
6266 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6267 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6268}
6269
6270
6271/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006272 * "getwinposx()" function
6273 */
6274 static void
6275f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6276{
6277 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006278#if defined(FEAT_GUI) \
6279 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6280 || defined(MSWIN)
6281
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006282 {
6283 int x, y;
6284
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006285 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006286 rettv->vval.v_number = x;
6287 }
6288#endif
6289}
6290
6291/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006292 * "getwinposy()" function
6293 */
6294 static void
6295f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6296{
6297 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006298#if defined(FEAT_GUI) \
6299 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6300 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 {
6302 int x, y;
6303
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006304 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006305 rettv->vval.v_number = y;
6306 }
6307#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308}
6309
6310/*
6311 * "getwinvar()" function
6312 */
6313 static void
6314f_getwinvar(typval_T *argvars, typval_T *rettv)
6315{
6316 getwinvar(argvars, rettv, 0);
6317}
6318
6319/*
6320 * "glob()" function
6321 */
6322 static void
6323f_glob(typval_T *argvars, typval_T *rettv)
6324{
6325 int options = WILD_SILENT|WILD_USE_NL;
6326 expand_T xpc;
6327 int error = FALSE;
6328
6329 /* When the optional second argument is non-zero, don't remove matches
6330 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6331 rettv->v_type = VAR_STRING;
6332 if (argvars[1].v_type != VAR_UNKNOWN)
6333 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006334 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006335 options |= WILD_KEEP_ALL;
6336 if (argvars[2].v_type != VAR_UNKNOWN)
6337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006338 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006339 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006341 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006342 options |= WILD_ALLLINKS;
6343 }
6344 }
6345 if (!error)
6346 {
6347 ExpandInit(&xpc);
6348 xpc.xp_context = EXPAND_FILES;
6349 if (p_wic)
6350 options += WILD_ICASE;
6351 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006352 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006353 NULL, options, WILD_ALL);
6354 else if (rettv_list_alloc(rettv) != FAIL)
6355 {
6356 int i;
6357
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006358 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006359 NULL, options, WILD_ALL_KEEP);
6360 for (i = 0; i < xpc.xp_numfiles; i++)
6361 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6362
6363 ExpandCleanup(&xpc);
6364 }
6365 }
6366 else
6367 rettv->vval.v_string = NULL;
6368}
6369
6370/*
6371 * "globpath()" function
6372 */
6373 static void
6374f_globpath(typval_T *argvars, typval_T *rettv)
6375{
6376 int flags = 0;
6377 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006378 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379 int error = FALSE;
6380 garray_T ga;
6381 int i;
6382
6383 /* When the optional second argument is non-zero, don't remove matches
6384 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6385 rettv->v_type = VAR_STRING;
6386 if (argvars[2].v_type != VAR_UNKNOWN)
6387 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006388 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006389 flags |= WILD_KEEP_ALL;
6390 if (argvars[3].v_type != VAR_UNKNOWN)
6391 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006392 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006393 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006395 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006396 flags |= WILD_ALLLINKS;
6397 }
6398 }
6399 if (file != NULL && !error)
6400 {
6401 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006402 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006403 if (rettv->v_type == VAR_STRING)
6404 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6405 else if (rettv_list_alloc(rettv) != FAIL)
6406 for (i = 0; i < ga.ga_len; ++i)
6407 list_append_string(rettv->vval.v_list,
6408 ((char_u **)(ga.ga_data))[i], -1);
6409 ga_clear_strings(&ga);
6410 }
6411 else
6412 rettv->vval.v_string = NULL;
6413}
6414
6415/*
6416 * "glob2regpat()" function
6417 */
6418 static void
6419f_glob2regpat(typval_T *argvars, typval_T *rettv)
6420{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006421 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422
6423 rettv->v_type = VAR_STRING;
6424 rettv->vval.v_string = (pat == NULL)
6425 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6426}
6427
6428/* for VIM_VERSION_ defines */
6429#include "version.h"
6430
6431/*
6432 * "has()" function
6433 */
6434 static void
6435f_has(typval_T *argvars, typval_T *rettv)
6436{
6437 int i;
6438 char_u *name;
6439 int n = FALSE;
6440 static char *(has_list[]) =
6441 {
6442#ifdef AMIGA
6443 "amiga",
6444# ifdef FEAT_ARP
6445 "arp",
6446# endif
6447#endif
6448#ifdef __BEOS__
6449 "beos",
6450#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006451#if defined(BSD) && !defined(MACOS_X)
6452 "bsd",
6453#endif
6454#ifdef hpux
6455 "hpux",
6456#endif
6457#ifdef __linux__
6458 "linux",
6459#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006460#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006461 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6462 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006463# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006464 "macunix", /* Mac OS X, with the darwin feature */
6465 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006466# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467#endif
6468#ifdef __QNX__
6469 "qnx",
6470#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006471#ifdef SUN_SYSTEM
6472 "sun",
6473#else
6474 "moon",
6475#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006476#ifdef UNIX
6477 "unix",
6478#endif
6479#ifdef VMS
6480 "vms",
6481#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006482#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 "win32",
6484#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006485#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006486 "win32unix",
6487#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006488#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006489 "win64",
6490#endif
6491#ifdef EBCDIC
6492 "ebcdic",
6493#endif
6494#ifndef CASE_INSENSITIVE_FILENAME
6495 "fname_case",
6496#endif
6497#ifdef HAVE_ACL
6498 "acl",
6499#endif
6500#ifdef FEAT_ARABIC
6501 "arabic",
6502#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006503 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006504#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006505 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006506#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006507#ifdef FEAT_AUTOSERVERNAME
6508 "autoservername",
6509#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006510#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006511 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006512# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006513 "balloon_multiline",
6514# endif
6515#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006516#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006517 "balloon_eval_term",
6518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6520 "builtin_terms",
6521# ifdef ALL_BUILTIN_TCAPS
6522 "all_builtin_terms",
6523# endif
6524#endif
6525#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006526 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006527 || defined(FEAT_GUI_MOTIF))
6528 "browsefilter",
6529#endif
6530#ifdef FEAT_BYTEOFF
6531 "byte_offset",
6532#endif
6533#ifdef FEAT_JOB_CHANNEL
6534 "channel",
6535#endif
6536#ifdef FEAT_CINDENT
6537 "cindent",
6538#endif
6539#ifdef FEAT_CLIENTSERVER
6540 "clientserver",
6541#endif
6542#ifdef FEAT_CLIPBOARD
6543 "clipboard",
6544#endif
6545#ifdef FEAT_CMDL_COMPL
6546 "cmdline_compl",
6547#endif
6548#ifdef FEAT_CMDHIST
6549 "cmdline_hist",
6550#endif
6551#ifdef FEAT_COMMENTS
6552 "comments",
6553#endif
6554#ifdef FEAT_CONCEAL
6555 "conceal",
6556#endif
6557#ifdef FEAT_CRYPT
6558 "cryptv",
6559 "crypt-blowfish",
6560 "crypt-blowfish2",
6561#endif
6562#ifdef FEAT_CSCOPE
6563 "cscope",
6564#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006566#ifdef CURSOR_SHAPE
6567 "cursorshape",
6568#endif
6569#ifdef DEBUG
6570 "debug",
6571#endif
6572#ifdef FEAT_CON_DIALOG
6573 "dialog_con",
6574#endif
6575#ifdef FEAT_GUI_DIALOG
6576 "dialog_gui",
6577#endif
6578#ifdef FEAT_DIFF
6579 "diff",
6580#endif
6581#ifdef FEAT_DIGRAPHS
6582 "digraphs",
6583#endif
6584#ifdef FEAT_DIRECTX
6585 "directx",
6586#endif
6587#ifdef FEAT_DND
6588 "dnd",
6589#endif
6590#ifdef FEAT_EMACS_TAGS
6591 "emacs_tags",
6592#endif
6593 "eval", /* always present, of course! */
6594 "ex_extra", /* graduated feature */
6595#ifdef FEAT_SEARCH_EXTRA
6596 "extra_search",
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598#ifdef FEAT_SEARCHPATH
6599 "file_in_path",
6600#endif
6601#ifdef FEAT_FILTERPIPE
6602 "filterpipe",
6603#endif
6604#ifdef FEAT_FIND_ID
6605 "find_in_path",
6606#endif
6607#ifdef FEAT_FLOAT
6608 "float",
6609#endif
6610#ifdef FEAT_FOLDING
6611 "folding",
6612#endif
6613#ifdef FEAT_FOOTER
6614 "footer",
6615#endif
6616#if !defined(USE_SYSTEM) && defined(UNIX)
6617 "fork",
6618#endif
6619#ifdef FEAT_GETTEXT
6620 "gettext",
6621#endif
6622#ifdef FEAT_GUI
6623 "gui",
6624#endif
6625#ifdef FEAT_GUI_ATHENA
6626# ifdef FEAT_GUI_NEXTAW
6627 "gui_neXtaw",
6628# else
6629 "gui_athena",
6630# endif
6631#endif
6632#ifdef FEAT_GUI_GTK
6633 "gui_gtk",
6634# ifdef USE_GTK3
6635 "gui_gtk3",
6636# else
6637 "gui_gtk2",
6638# endif
6639#endif
6640#ifdef FEAT_GUI_GNOME
6641 "gui_gnome",
6642#endif
6643#ifdef FEAT_GUI_MAC
6644 "gui_mac",
6645#endif
6646#ifdef FEAT_GUI_MOTIF
6647 "gui_motif",
6648#endif
6649#ifdef FEAT_GUI_PHOTON
6650 "gui_photon",
6651#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006652#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006653 "gui_win32",
6654#endif
6655#ifdef FEAT_HANGULIN
6656 "hangul_input",
6657#endif
6658#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6659 "iconv",
6660#endif
6661#ifdef FEAT_INS_EXPAND
6662 "insert_expand",
6663#endif
6664#ifdef FEAT_JOB_CHANNEL
6665 "job",
6666#endif
6667#ifdef FEAT_JUMPLIST
6668 "jumplist",
6669#endif
6670#ifdef FEAT_KEYMAP
6671 "keymap",
6672#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006673 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006674#ifdef FEAT_LANGMAP
6675 "langmap",
6676#endif
6677#ifdef FEAT_LIBCALL
6678 "libcall",
6679#endif
6680#ifdef FEAT_LINEBREAK
6681 "linebreak",
6682#endif
6683#ifdef FEAT_LISP
6684 "lispindent",
6685#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006686 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006687#ifdef FEAT_LOCALMAP
6688 "localmap",
6689#endif
6690#ifdef FEAT_LUA
6691# ifndef DYNAMIC_LUA
6692 "lua",
6693# endif
6694#endif
6695#ifdef FEAT_MENU
6696 "menu",
6697#endif
6698#ifdef FEAT_SESSION
6699 "mksession",
6700#endif
6701#ifdef FEAT_MODIFY_FNAME
6702 "modify_fname",
6703#endif
6704#ifdef FEAT_MOUSE
6705 "mouse",
6706#endif
6707#ifdef FEAT_MOUSESHAPE
6708 "mouseshape",
6709#endif
6710#if defined(UNIX) || defined(VMS)
6711# ifdef FEAT_MOUSE_DEC
6712 "mouse_dec",
6713# endif
6714# ifdef FEAT_MOUSE_GPM
6715 "mouse_gpm",
6716# endif
6717# ifdef FEAT_MOUSE_JSB
6718 "mouse_jsbterm",
6719# endif
6720# ifdef FEAT_MOUSE_NET
6721 "mouse_netterm",
6722# endif
6723# ifdef FEAT_MOUSE_PTERM
6724 "mouse_pterm",
6725# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006726# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006727 "mouse_sgr",
6728# endif
6729# ifdef FEAT_SYSMOUSE
6730 "mouse_sysmouse",
6731# endif
6732# ifdef FEAT_MOUSE_URXVT
6733 "mouse_urxvt",
6734# endif
6735# ifdef FEAT_MOUSE_XTERM
6736 "mouse_xterm",
6737# endif
6738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740#ifdef FEAT_MBYTE_IME
6741 "multi_byte_ime",
6742#endif
6743#ifdef FEAT_MULTI_LANG
6744 "multi_lang",
6745#endif
6746#ifdef FEAT_MZSCHEME
6747#ifndef DYNAMIC_MZSCHEME
6748 "mzscheme",
6749#endif
6750#endif
6751#ifdef FEAT_NUM64
6752 "num64",
6753#endif
6754#ifdef FEAT_OLE
6755 "ole",
6756#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006757#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006758 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006759#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760#ifdef FEAT_PATH_EXTRA
6761 "path_extra",
6762#endif
6763#ifdef FEAT_PERL
6764#ifndef DYNAMIC_PERL
6765 "perl",
6766#endif
6767#endif
6768#ifdef FEAT_PERSISTENT_UNDO
6769 "persistent_undo",
6770#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006771#if defined(FEAT_PYTHON)
6772 "python_compiled",
6773# if defined(DYNAMIC_PYTHON)
6774 "python_dynamic",
6775# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006776 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006777 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006778# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006779#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006780#if defined(FEAT_PYTHON3)
6781 "python3_compiled",
6782# if defined(DYNAMIC_PYTHON3)
6783 "python3_dynamic",
6784# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006785 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006786 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006787# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006788#endif
6789#ifdef FEAT_POSTSCRIPT
6790 "postscript",
6791#endif
6792#ifdef FEAT_PRINTER
6793 "printer",
6794#endif
6795#ifdef FEAT_PROFILE
6796 "profile",
6797#endif
6798#ifdef FEAT_RELTIME
6799 "reltime",
6800#endif
6801#ifdef FEAT_QUICKFIX
6802 "quickfix",
6803#endif
6804#ifdef FEAT_RIGHTLEFT
6805 "rightleft",
6806#endif
6807#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6808 "ruby",
6809#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811#ifdef FEAT_CMDL_INFO
6812 "showcmd",
6813 "cmdline_info",
6814#endif
6815#ifdef FEAT_SIGNS
6816 "signs",
6817#endif
6818#ifdef FEAT_SMARTINDENT
6819 "smartindent",
6820#endif
6821#ifdef STARTUPTIME
6822 "startuptime",
6823#endif
6824#ifdef FEAT_STL_OPT
6825 "statusline",
6826#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006827#ifdef FEAT_NETBEANS_INTG
6828 "netbeans_intg",
6829#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +02006830#ifdef FEAT_SOUND
6831 "sound",
6832#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006833#ifdef FEAT_SPELL
6834 "spell",
6835#endif
6836#ifdef FEAT_SYN_HL
6837 "syntax",
6838#endif
6839#if defined(USE_SYSTEM) || !defined(UNIX)
6840 "system",
6841#endif
6842#ifdef FEAT_TAG_BINS
6843 "tag_binary",
6844#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006845#ifdef FEAT_TCL
6846# ifndef DYNAMIC_TCL
6847 "tcl",
6848# endif
6849#endif
6850#ifdef FEAT_TERMGUICOLORS
6851 "termguicolors",
6852#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006853#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006854 "terminal",
6855#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006856#ifdef TERMINFO
6857 "terminfo",
6858#endif
6859#ifdef FEAT_TERMRESPONSE
6860 "termresponse",
6861#endif
6862#ifdef FEAT_TEXTOBJ
6863 "textobjects",
6864#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006865#ifdef FEAT_TEXT_PROP
6866 "textprop",
6867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006868#ifdef HAVE_TGETENT
6869 "tgetent",
6870#endif
6871#ifdef FEAT_TIMERS
6872 "timers",
6873#endif
6874#ifdef FEAT_TITLE
6875 "title",
6876#endif
6877#ifdef FEAT_TOOLBAR
6878 "toolbar",
6879#endif
6880#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6881 "unnamedplus",
6882#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006883 "user-commands", /* was accidentally included in 5.4 */
6884 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006885#ifdef FEAT_VARTABS
6886 "vartabs",
6887#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006888 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006889#ifdef FEAT_VIMINFO
6890 "viminfo",
6891#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006892 "vimscript-1",
6893 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006894 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006895 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006897 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006898 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006899#ifdef FEAT_VTP
6900 "vtp",
6901#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902#ifdef FEAT_WILDIGN
6903 "wildignore",
6904#endif
6905#ifdef FEAT_WILDMENU
6906 "wildmenu",
6907#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006908 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006909#ifdef FEAT_WAK
6910 "winaltkeys",
6911#endif
6912#ifdef FEAT_WRITEBACKUP
6913 "writebackup",
6914#endif
6915#ifdef FEAT_XIM
6916 "xim",
6917#endif
6918#ifdef FEAT_XFONTSET
6919 "xfontset",
6920#endif
6921#ifdef FEAT_XPM_W32
6922 "xpm",
6923 "xpm_w32", /* for backward compatibility */
6924#else
6925# if defined(HAVE_XPM)
6926 "xpm",
6927# endif
6928#endif
6929#ifdef USE_XSMP
6930 "xsmp",
6931#endif
6932#ifdef USE_XSMP_INTERACT
6933 "xsmp_interact",
6934#endif
6935#ifdef FEAT_XCLIPBOARD
6936 "xterm_clipboard",
6937#endif
6938#ifdef FEAT_XTERM_SAVE
6939 "xterm_save",
6940#endif
6941#if defined(UNIX) && defined(FEAT_X11)
6942 "X11",
6943#endif
6944 NULL
6945 };
6946
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006947 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006948 for (i = 0; has_list[i] != NULL; ++i)
6949 if (STRICMP(name, has_list[i]) == 0)
6950 {
6951 n = TRUE;
6952 break;
6953 }
6954
6955 if (n == FALSE)
6956 {
6957 if (STRNICMP(name, "patch", 5) == 0)
6958 {
6959 if (name[5] == '-'
6960 && STRLEN(name) >= 11
6961 && vim_isdigit(name[6])
6962 && vim_isdigit(name[8])
6963 && vim_isdigit(name[10]))
6964 {
6965 int major = atoi((char *)name + 6);
6966 int minor = atoi((char *)name + 8);
6967
6968 /* Expect "patch-9.9.01234". */
6969 n = (major < VIM_VERSION_MAJOR
6970 || (major == VIM_VERSION_MAJOR
6971 && (minor < VIM_VERSION_MINOR
6972 || (minor == VIM_VERSION_MINOR
6973 && has_patch(atoi((char *)name + 10))))));
6974 }
6975 else
6976 n = has_patch(atoi((char *)name + 5));
6977 }
6978 else if (STRICMP(name, "vim_starting") == 0)
6979 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006980 else if (STRICMP(name, "ttyin") == 0)
6981 n = mch_input_isatty();
6982 else if (STRICMP(name, "ttyout") == 0)
6983 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006984 else if (STRICMP(name, "multi_byte_encoding") == 0)
6985 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006986#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 else if (STRICMP(name, "balloon_multiline") == 0)
6988 n = multiline_balloon_available();
6989#endif
6990#ifdef DYNAMIC_TCL
6991 else if (STRICMP(name, "tcl") == 0)
6992 n = tcl_enabled(FALSE);
6993#endif
6994#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6995 else if (STRICMP(name, "iconv") == 0)
6996 n = iconv_enabled(FALSE);
6997#endif
6998#ifdef DYNAMIC_LUA
6999 else if (STRICMP(name, "lua") == 0)
7000 n = lua_enabled(FALSE);
7001#endif
7002#ifdef DYNAMIC_MZSCHEME
7003 else if (STRICMP(name, "mzscheme") == 0)
7004 n = mzscheme_enabled(FALSE);
7005#endif
7006#ifdef DYNAMIC_RUBY
7007 else if (STRICMP(name, "ruby") == 0)
7008 n = ruby_enabled(FALSE);
7009#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010#ifdef DYNAMIC_PYTHON
7011 else if (STRICMP(name, "python") == 0)
7012 n = python_enabled(FALSE);
7013#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007014#ifdef DYNAMIC_PYTHON3
7015 else if (STRICMP(name, "python3") == 0)
7016 n = python3_enabled(FALSE);
7017#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01007018#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
7019 else if (STRICMP(name, "pythonx") == 0)
7020 {
7021# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
7022 if (p_pyx == 0)
7023 n = python3_enabled(FALSE) || python_enabled(FALSE);
7024 else if (p_pyx == 3)
7025 n = python3_enabled(FALSE);
7026 else if (p_pyx == 2)
7027 n = python_enabled(FALSE);
7028# elif defined(DYNAMIC_PYTHON)
7029 n = python_enabled(FALSE);
7030# elif defined(DYNAMIC_PYTHON3)
7031 n = python3_enabled(FALSE);
7032# endif
7033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007034#endif
7035#ifdef DYNAMIC_PERL
7036 else if (STRICMP(name, "perl") == 0)
7037 n = perl_enabled(FALSE);
7038#endif
7039#ifdef FEAT_GUI
7040 else if (STRICMP(name, "gui_running") == 0)
7041 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042# ifdef FEAT_BROWSE
7043 else if (STRICMP(name, "browse") == 0)
7044 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7045# endif
7046#endif
7047#ifdef FEAT_SYN_HL
7048 else if (STRICMP(name, "syntax_items") == 0)
7049 n = syntax_present(curwin);
7050#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007051#ifdef FEAT_VTP
7052 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007053 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007054#endif
7055#ifdef FEAT_NETBEANS_INTG
7056 else if (STRICMP(name, "netbeans_enabled") == 0)
7057 n = netbeans_active();
7058#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007059#ifdef FEAT_MOUSE_GPM
7060 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7061 n = gpm_enabled();
7062#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007063#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007064 else if (STRICMP(name, "terminal") == 0)
7065 n = terminal_enabled();
7066#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007067#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007068 else if (STRICMP(name, "conpty") == 0)
7069 n = use_conpty();
7070#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 }
7072
7073 rettv->vval.v_number = n;
7074}
7075
7076/*
7077 * "has_key()" function
7078 */
7079 static void
7080f_has_key(typval_T *argvars, typval_T *rettv)
7081{
7082 if (argvars[0].v_type != VAR_DICT)
7083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007084 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 return;
7086 }
7087 if (argvars[0].vval.v_dict == NULL)
7088 return;
7089
7090 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007091 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007092}
7093
7094/*
7095 * "haslocaldir()" function
7096 */
7097 static void
7098f_haslocaldir(typval_T *argvars, typval_T *rettv)
7099{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007100 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 win_T *wp = NULL;
7102
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007103 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7104
7105 // Check for window-local and tab-local directories
7106 if (wp != NULL && wp->w_localdir != NULL)
7107 rettv->vval.v_number = 1;
7108 else if (tp != NULL && tp->tp_localdir != NULL)
7109 rettv->vval.v_number = 2;
7110 else
7111 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007112}
7113
7114/*
7115 * "hasmapto()" function
7116 */
7117 static void
7118f_hasmapto(typval_T *argvars, typval_T *rettv)
7119{
7120 char_u *name;
7121 char_u *mode;
7122 char_u buf[NUMBUFLEN];
7123 int abbr = FALSE;
7124
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007125 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007126 if (argvars[1].v_type == VAR_UNKNOWN)
7127 mode = (char_u *)"nvo";
7128 else
7129 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007130 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007132 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007133 }
7134
7135 if (map_to_exists(name, mode, abbr))
7136 rettv->vval.v_number = TRUE;
7137 else
7138 rettv->vval.v_number = FALSE;
7139}
7140
7141/*
7142 * "histadd()" function
7143 */
7144 static void
7145f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7146{
7147#ifdef FEAT_CMDHIST
7148 int histype;
7149 char_u *str;
7150 char_u buf[NUMBUFLEN];
7151#endif
7152
7153 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007154 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007155 return;
7156#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007157 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158 histype = str != NULL ? get_histtype(str) : -1;
7159 if (histype >= 0)
7160 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007161 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007162 if (*str != NUL)
7163 {
7164 init_history();
7165 add_to_history(histype, str, FALSE, NUL);
7166 rettv->vval.v_number = TRUE;
7167 return;
7168 }
7169 }
7170#endif
7171}
7172
7173/*
7174 * "histdel()" function
7175 */
7176 static void
7177f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7178{
7179#ifdef FEAT_CMDHIST
7180 int n;
7181 char_u buf[NUMBUFLEN];
7182 char_u *str;
7183
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007184 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007185 if (str == NULL)
7186 n = 0;
7187 else if (argvars[1].v_type == VAR_UNKNOWN)
7188 /* only one argument: clear entire history */
7189 n = clr_history(get_histtype(str));
7190 else if (argvars[1].v_type == VAR_NUMBER)
7191 /* index given: remove that entry */
7192 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007193 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194 else
7195 /* string given: remove all matching entries */
7196 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007197 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 rettv->vval.v_number = n;
7199#endif
7200}
7201
7202/*
7203 * "histget()" function
7204 */
7205 static void
7206f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7207{
7208#ifdef FEAT_CMDHIST
7209 int type;
7210 int idx;
7211 char_u *str;
7212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007213 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007214 if (str == NULL)
7215 rettv->vval.v_string = NULL;
7216 else
7217 {
7218 type = get_histtype(str);
7219 if (argvars[1].v_type == VAR_UNKNOWN)
7220 idx = get_history_idx(type);
7221 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007222 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007223 /* -1 on type error */
7224 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7225 }
7226#else
7227 rettv->vval.v_string = NULL;
7228#endif
7229 rettv->v_type = VAR_STRING;
7230}
7231
7232/*
7233 * "histnr()" function
7234 */
7235 static void
7236f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7237{
7238 int i;
7239
7240#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007241 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007242
7243 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7244 if (i >= HIST_CMD && i < HIST_COUNT)
7245 i = get_history_idx(i);
7246 else
7247#endif
7248 i = -1;
7249 rettv->vval.v_number = i;
7250}
7251
7252/*
7253 * "highlightID(name)" function
7254 */
7255 static void
7256f_hlID(typval_T *argvars, typval_T *rettv)
7257{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007258 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007259}
7260
7261/*
7262 * "highlight_exists()" function
7263 */
7264 static void
7265f_hlexists(typval_T *argvars, typval_T *rettv)
7266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007267 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007268}
7269
7270/*
7271 * "hostname()" function
7272 */
7273 static void
7274f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7275{
7276 char_u hostname[256];
7277
7278 mch_get_host_name(hostname, 256);
7279 rettv->v_type = VAR_STRING;
7280 rettv->vval.v_string = vim_strsave(hostname);
7281}
7282
7283/*
7284 * iconv() function
7285 */
7286 static void
7287f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7288{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 char_u buf1[NUMBUFLEN];
7290 char_u buf2[NUMBUFLEN];
7291 char_u *from, *to, *str;
7292 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293
7294 rettv->v_type = VAR_STRING;
7295 rettv->vval.v_string = NULL;
7296
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007297 str = tv_get_string(&argvars[0]);
7298 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7299 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 vimconv.vc_type = CONV_NONE;
7301 convert_setup(&vimconv, from, to);
7302
7303 /* If the encodings are equal, no conversion needed. */
7304 if (vimconv.vc_type == CONV_NONE)
7305 rettv->vval.v_string = vim_strsave(str);
7306 else
7307 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7308
7309 convert_setup(&vimconv, NULL, NULL);
7310 vim_free(from);
7311 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007312}
7313
7314/*
7315 * "indent()" function
7316 */
7317 static void
7318f_indent(typval_T *argvars, typval_T *rettv)
7319{
7320 linenr_T lnum;
7321
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007322 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007323 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7324 rettv->vval.v_number = get_indent_lnum(lnum);
7325 else
7326 rettv->vval.v_number = -1;
7327}
7328
7329/*
7330 * "index()" function
7331 */
7332 static void
7333f_index(typval_T *argvars, typval_T *rettv)
7334{
7335 list_T *l;
7336 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007337 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007338 long idx = 0;
7339 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007340 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007341
7342 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007343 if (argvars[0].v_type == VAR_BLOB)
7344 {
7345 typval_T tv;
7346 int start = 0;
7347
7348 if (argvars[2].v_type != VAR_UNKNOWN)
7349 {
7350 start = tv_get_number_chk(&argvars[2], &error);
7351 if (error)
7352 return;
7353 }
7354 b = argvars[0].vval.v_blob;
7355 if (b == NULL)
7356 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007357 if (start < 0)
7358 {
7359 start = blob_len(b) + start;
7360 if (start < 0)
7361 start = 0;
7362 }
7363
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007364 for (idx = start; idx < blob_len(b); ++idx)
7365 {
7366 tv.v_type = VAR_NUMBER;
7367 tv.vval.v_number = blob_get(b, idx);
7368 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7369 {
7370 rettv->vval.v_number = idx;
7371 return;
7372 }
7373 }
7374 return;
7375 }
7376 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007377 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007378 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007379 return;
7380 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007381
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007382 l = argvars[0].vval.v_list;
7383 if (l != NULL)
7384 {
7385 item = l->lv_first;
7386 if (argvars[2].v_type != VAR_UNKNOWN)
7387 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 /* Start at specified item. Use the cached index that list_find()
7389 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007391 idx = l->lv_idx;
7392 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007393 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394 if (error)
7395 item = NULL;
7396 }
7397
7398 for ( ; item != NULL; item = item->li_next, ++idx)
7399 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7400 {
7401 rettv->vval.v_number = idx;
7402 break;
7403 }
7404 }
7405}
7406
7407static int inputsecret_flag = 0;
7408
7409/*
7410 * "input()" function
7411 * Also handles inputsecret() when inputsecret is set.
7412 */
7413 static void
7414f_input(typval_T *argvars, typval_T *rettv)
7415{
7416 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7417}
7418
7419/*
7420 * "inputdialog()" function
7421 */
7422 static void
7423f_inputdialog(typval_T *argvars, typval_T *rettv)
7424{
7425#if defined(FEAT_GUI_TEXTDIALOG)
7426 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7427 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7428 {
7429 char_u *message;
7430 char_u buf[NUMBUFLEN];
7431 char_u *defstr = (char_u *)"";
7432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007433 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007434 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007435 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7437 else
7438 IObuff[0] = NUL;
7439 if (message != NULL && defstr != NULL
7440 && do_dialog(VIM_QUESTION, NULL, message,
7441 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7442 rettv->vval.v_string = vim_strsave(IObuff);
7443 else
7444 {
7445 if (message != NULL && defstr != NULL
7446 && argvars[1].v_type != VAR_UNKNOWN
7447 && argvars[2].v_type != VAR_UNKNOWN)
7448 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007449 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007450 else
7451 rettv->vval.v_string = NULL;
7452 }
7453 rettv->v_type = VAR_STRING;
7454 }
7455 else
7456#endif
7457 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7458}
7459
7460/*
7461 * "inputlist()" function
7462 */
7463 static void
7464f_inputlist(typval_T *argvars, typval_T *rettv)
7465{
7466 listitem_T *li;
7467 int selected;
7468 int mouse_used;
7469
7470#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007471 /* While starting up, there is no place to enter text. When running tests
7472 * with --not-a-term we assume feedkeys() will be used. */
7473 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007474 return;
7475#endif
7476 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007478 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007479 return;
7480 }
7481
7482 msg_start();
7483 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7484 lines_left = Rows; /* avoid more prompt */
7485 msg_scroll = TRUE;
7486 msg_clr_eos();
7487
7488 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7489 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007490 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007491 msg_putchar('\n');
7492 }
7493
7494 /* Ask for choice. */
7495 selected = prompt_for_number(&mouse_used);
7496 if (mouse_used)
7497 selected -= lines_left;
7498
7499 rettv->vval.v_number = selected;
7500}
7501
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007502static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7503
7504/*
7505 * "inputrestore()" function
7506 */
7507 static void
7508f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7509{
7510 if (ga_userinput.ga_len > 0)
7511 {
7512 --ga_userinput.ga_len;
7513 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7514 + ga_userinput.ga_len);
7515 /* default return is zero == OK */
7516 }
7517 else if (p_verbose > 1)
7518 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007519 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007520 rettv->vval.v_number = 1; /* Failed */
7521 }
7522}
7523
7524/*
7525 * "inputsave()" function
7526 */
7527 static void
7528f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7529{
7530 /* Add an entry to the stack of typeahead storage. */
7531 if (ga_grow(&ga_userinput, 1) == OK)
7532 {
7533 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7534 + ga_userinput.ga_len);
7535 ++ga_userinput.ga_len;
7536 /* default return is zero == OK */
7537 }
7538 else
7539 rettv->vval.v_number = 1; /* Failed */
7540}
7541
7542/*
7543 * "inputsecret()" function
7544 */
7545 static void
7546f_inputsecret(typval_T *argvars, typval_T *rettv)
7547{
7548 ++cmdline_star;
7549 ++inputsecret_flag;
7550 f_input(argvars, rettv);
7551 --cmdline_star;
7552 --inputsecret_flag;
7553}
7554
7555/*
7556 * "insert()" function
7557 */
7558 static void
7559f_insert(typval_T *argvars, typval_T *rettv)
7560{
7561 long before = 0;
7562 listitem_T *item;
7563 list_T *l;
7564 int error = FALSE;
7565
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007566 if (argvars[0].v_type == VAR_BLOB)
7567 {
7568 int val, len;
7569 char_u *p;
7570
7571 len = blob_len(argvars[0].vval.v_blob);
7572 if (argvars[2].v_type != VAR_UNKNOWN)
7573 {
7574 before = (long)tv_get_number_chk(&argvars[2], &error);
7575 if (error)
7576 return; // type error; errmsg already given
7577 if (before < 0 || before > len)
7578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007579 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007580 return;
7581 }
7582 }
7583 val = tv_get_number_chk(&argvars[1], &error);
7584 if (error)
7585 return;
7586 if (val < 0 || val > 255)
7587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007588 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007589 return;
7590 }
7591
7592 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7593 return;
7594 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7595 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7596 *(p + before) = val;
7597 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7598
7599 copy_tv(&argvars[0], rettv);
7600 }
7601 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007602 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007603 else if ((l = argvars[0].vval.v_list) != NULL
7604 && !var_check_lock(l->lv_lock,
7605 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007606 {
7607 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007608 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 if (error)
7610 return; /* type error; errmsg already given */
7611
7612 if (before == l->lv_len)
7613 item = NULL;
7614 else
7615 {
7616 item = list_find(l, before);
7617 if (item == NULL)
7618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007619 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007620 l = NULL;
7621 }
7622 }
7623 if (l != NULL)
7624 {
7625 list_insert_tv(l, &argvars[1], item);
7626 copy_tv(&argvars[0], rettv);
7627 }
7628 }
7629}
7630
7631/*
7632 * "invert(expr)" function
7633 */
7634 static void
7635f_invert(typval_T *argvars, typval_T *rettv)
7636{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007637 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007638}
7639
7640/*
7641 * "isdirectory()" function
7642 */
7643 static void
7644f_isdirectory(typval_T *argvars, typval_T *rettv)
7645{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007646 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647}
7648
7649/*
7650 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7651 * or it refers to a List or Dictionary that is locked.
7652 */
7653 static int
7654tv_islocked(typval_T *tv)
7655{
7656 return (tv->v_lock & VAR_LOCKED)
7657 || (tv->v_type == VAR_LIST
7658 && tv->vval.v_list != NULL
7659 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7660 || (tv->v_type == VAR_DICT
7661 && tv->vval.v_dict != NULL
7662 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7663}
7664
7665/*
7666 * "islocked()" function
7667 */
7668 static void
7669f_islocked(typval_T *argvars, typval_T *rettv)
7670{
7671 lval_T lv;
7672 char_u *end;
7673 dictitem_T *di;
7674
7675 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007676 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007677 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 if (end != NULL && lv.ll_name != NULL)
7679 {
7680 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007681 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007682 else
7683 {
7684 if (lv.ll_tv == NULL)
7685 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007686 di = find_var(lv.ll_name, NULL, TRUE);
7687 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007688 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007689 /* Consider a variable locked when:
7690 * 1. the variable itself is locked
7691 * 2. the value of the variable is locked.
7692 * 3. the List or Dict value is locked.
7693 */
7694 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7695 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007696 }
7697 }
7698 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007699 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007700 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702 else if (lv.ll_list != NULL)
7703 /* List item. */
7704 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7705 else
7706 /* Dictionary item. */
7707 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7708 }
7709 }
7710
7711 clear_lval(&lv);
7712}
7713
7714#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7715/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007716 * "isinf()" function
7717 */
7718 static void
7719f_isinf(typval_T *argvars, typval_T *rettv)
7720{
7721 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7722 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7723}
7724
7725/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007726 * "isnan()" function
7727 */
7728 static void
7729f_isnan(typval_T *argvars, typval_T *rettv)
7730{
7731 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7732 && isnan(argvars[0].vval.v_float);
7733}
7734#endif
7735
7736/*
7737 * "items(dict)" function
7738 */
7739 static void
7740f_items(typval_T *argvars, typval_T *rettv)
7741{
7742 dict_list(argvars, rettv, 2);
7743}
7744
7745#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7746/*
7747 * Get the job from the argument.
7748 * Returns NULL if the job is invalid.
7749 */
7750 static job_T *
7751get_job_arg(typval_T *tv)
7752{
7753 job_T *job;
7754
7755 if (tv->v_type != VAR_JOB)
7756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007757 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007758 return NULL;
7759 }
7760 job = tv->vval.v_job;
7761
7762 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007763 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764 return job;
7765}
7766
7767/*
7768 * "job_getchannel()" function
7769 */
7770 static void
7771f_job_getchannel(typval_T *argvars, typval_T *rettv)
7772{
7773 job_T *job = get_job_arg(&argvars[0]);
7774
7775 if (job != NULL)
7776 {
7777 rettv->v_type = VAR_CHANNEL;
7778 rettv->vval.v_channel = job->jv_channel;
7779 if (job->jv_channel != NULL)
7780 ++job->jv_channel->ch_refcount;
7781 }
7782}
7783
7784/*
7785 * "job_info()" function
7786 */
7787 static void
7788f_job_info(typval_T *argvars, typval_T *rettv)
7789{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007790 if (argvars[0].v_type != VAR_UNKNOWN)
7791 {
7792 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007794 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7795 job_info(job, rettv->vval.v_dict);
7796 }
7797 else if (rettv_list_alloc(rettv) == OK)
7798 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007799}
7800
7801/*
7802 * "job_setoptions()" function
7803 */
7804 static void
7805f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7806{
7807 job_T *job = get_job_arg(&argvars[0]);
7808 jobopt_T opt;
7809
7810 if (job == NULL)
7811 return;
7812 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007813 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814 job_set_options(job, &opt);
7815 free_job_options(&opt);
7816}
7817
7818/*
7819 * "job_start()" function
7820 */
7821 static void
7822f_job_start(typval_T *argvars, typval_T *rettv)
7823{
7824 rettv->v_type = VAR_JOB;
7825 if (check_restricted() || check_secure())
7826 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007827 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007828}
7829
7830/*
7831 * "job_status()" function
7832 */
7833 static void
7834f_job_status(typval_T *argvars, typval_T *rettv)
7835{
7836 job_T *job = get_job_arg(&argvars[0]);
7837
7838 if (job != NULL)
7839 {
7840 rettv->v_type = VAR_STRING;
7841 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7842 }
7843}
7844
7845/*
7846 * "job_stop()" function
7847 */
7848 static void
7849f_job_stop(typval_T *argvars, typval_T *rettv)
7850{
7851 job_T *job = get_job_arg(&argvars[0]);
7852
7853 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007854 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007855}
7856#endif
7857
7858/*
7859 * "join()" function
7860 */
7861 static void
7862f_join(typval_T *argvars, typval_T *rettv)
7863{
7864 garray_T ga;
7865 char_u *sep;
7866
7867 if (argvars[0].v_type != VAR_LIST)
7868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007869 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007870 return;
7871 }
7872 if (argvars[0].vval.v_list == NULL)
7873 return;
7874 if (argvars[1].v_type == VAR_UNKNOWN)
7875 sep = (char_u *)" ";
7876 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007877 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878
7879 rettv->v_type = VAR_STRING;
7880
7881 if (sep != NULL)
7882 {
7883 ga_init2(&ga, (int)sizeof(char), 80);
7884 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7885 ga_append(&ga, NUL);
7886 rettv->vval.v_string = (char_u *)ga.ga_data;
7887 }
7888 else
7889 rettv->vval.v_string = NULL;
7890}
7891
7892/*
7893 * "js_decode()" function
7894 */
7895 static void
7896f_js_decode(typval_T *argvars, typval_T *rettv)
7897{
7898 js_read_T reader;
7899
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007900 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007901 reader.js_fill = NULL;
7902 reader.js_used = 0;
7903 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007904 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905}
7906
7907/*
7908 * "js_encode()" function
7909 */
7910 static void
7911f_js_encode(typval_T *argvars, typval_T *rettv)
7912{
7913 rettv->v_type = VAR_STRING;
7914 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7915}
7916
7917/*
7918 * "json_decode()" function
7919 */
7920 static void
7921f_json_decode(typval_T *argvars, typval_T *rettv)
7922{
7923 js_read_T reader;
7924
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007925 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007926 reader.js_fill = NULL;
7927 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007928 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929}
7930
7931/*
7932 * "json_encode()" function
7933 */
7934 static void
7935f_json_encode(typval_T *argvars, typval_T *rettv)
7936{
7937 rettv->v_type = VAR_STRING;
7938 rettv->vval.v_string = json_encode(&argvars[0], 0);
7939}
7940
7941/*
7942 * "keys()" function
7943 */
7944 static void
7945f_keys(typval_T *argvars, typval_T *rettv)
7946{
7947 dict_list(argvars, rettv, 0);
7948}
7949
7950/*
7951 * "last_buffer_nr()" function.
7952 */
7953 static void
7954f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7955{
7956 int n = 0;
7957 buf_T *buf;
7958
Bram Moolenaar29323592016-07-24 22:04:11 +02007959 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007960 if (n < buf->b_fnum)
7961 n = buf->b_fnum;
7962
7963 rettv->vval.v_number = n;
7964}
7965
7966/*
7967 * "len()" function
7968 */
7969 static void
7970f_len(typval_T *argvars, typval_T *rettv)
7971{
7972 switch (argvars[0].v_type)
7973 {
7974 case VAR_STRING:
7975 case VAR_NUMBER:
7976 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007977 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007978 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007979 case VAR_BLOB:
7980 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7981 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007982 case VAR_LIST:
7983 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7984 break;
7985 case VAR_DICT:
7986 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7987 break;
7988 case VAR_UNKNOWN:
7989 case VAR_SPECIAL:
7990 case VAR_FLOAT:
7991 case VAR_FUNC:
7992 case VAR_PARTIAL:
7993 case VAR_JOB:
7994 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007995 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007996 break;
7997 }
7998}
7999
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01008001libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008002{
8003#ifdef FEAT_LIBCALL
8004 char_u *string_in;
8005 char_u **string_result;
8006 int nr_result;
8007#endif
8008
8009 rettv->v_type = type;
8010 if (type != VAR_NUMBER)
8011 rettv->vval.v_string = NULL;
8012
8013 if (check_restricted() || check_secure())
8014 return;
8015
8016#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02008017 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008018 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8019 {
8020 string_in = NULL;
8021 if (argvars[2].v_type == VAR_STRING)
8022 string_in = argvars[2].vval.v_string;
8023 if (type == VAR_NUMBER)
8024 string_result = NULL;
8025 else
8026 string_result = &rettv->vval.v_string;
8027 if (mch_libcall(argvars[0].vval.v_string,
8028 argvars[1].vval.v_string,
8029 string_in,
8030 argvars[2].vval.v_number,
8031 string_result,
8032 &nr_result) == OK
8033 && type == VAR_NUMBER)
8034 rettv->vval.v_number = nr_result;
8035 }
8036#endif
8037}
8038
8039/*
8040 * "libcall()" function
8041 */
8042 static void
8043f_libcall(typval_T *argvars, typval_T *rettv)
8044{
8045 libcall_common(argvars, rettv, VAR_STRING);
8046}
8047
8048/*
8049 * "libcallnr()" function
8050 */
8051 static void
8052f_libcallnr(typval_T *argvars, typval_T *rettv)
8053{
8054 libcall_common(argvars, rettv, VAR_NUMBER);
8055}
8056
8057/*
8058 * "line(string)" function
8059 */
8060 static void
8061f_line(typval_T *argvars, typval_T *rettv)
8062{
8063 linenr_T lnum = 0;
8064 pos_T *fp;
8065 int fnum;
8066
8067 fp = var2fpos(&argvars[0], TRUE, &fnum);
8068 if (fp != NULL)
8069 lnum = fp->lnum;
8070 rettv->vval.v_number = lnum;
8071}
8072
8073/*
8074 * "line2byte(lnum)" function
8075 */
8076 static void
8077f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8078{
8079#ifndef FEAT_BYTEOFF
8080 rettv->vval.v_number = -1;
8081#else
8082 linenr_T lnum;
8083
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008084 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008085 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8086 rettv->vval.v_number = -1;
8087 else
8088 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8089 if (rettv->vval.v_number >= 0)
8090 ++rettv->vval.v_number;
8091#endif
8092}
8093
8094/*
8095 * "lispindent(lnum)" function
8096 */
8097 static void
8098f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8099{
8100#ifdef FEAT_LISP
8101 pos_T pos;
8102 linenr_T lnum;
8103
8104 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008105 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008106 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8107 {
8108 curwin->w_cursor.lnum = lnum;
8109 rettv->vval.v_number = get_lisp_indent();
8110 curwin->w_cursor = pos;
8111 }
8112 else
8113#endif
8114 rettv->vval.v_number = -1;
8115}
8116
8117/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008118 * "list2str()" function
8119 */
8120 static void
8121f_list2str(typval_T *argvars, typval_T *rettv)
8122{
8123 list_T *l;
8124 listitem_T *li;
8125 garray_T ga;
8126 int utf8 = FALSE;
8127
8128 rettv->v_type = VAR_STRING;
8129 rettv->vval.v_string = NULL;
8130 if (argvars[0].v_type != VAR_LIST)
8131 {
8132 emsg(_(e_invarg));
8133 return;
8134 }
8135
8136 l = argvars[0].vval.v_list;
8137 if (l == NULL)
8138 return; // empty list results in empty string
8139
8140 if (argvars[1].v_type != VAR_UNKNOWN)
8141 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8142
8143 ga_init2(&ga, 1, 80);
8144 if (has_mbyte || utf8)
8145 {
8146 char_u buf[MB_MAXBYTES + 1];
8147 int (*char2bytes)(int, char_u *);
8148
8149 if (utf8 || enc_utf8)
8150 char2bytes = utf_char2bytes;
8151 else
8152 char2bytes = mb_char2bytes;
8153
8154 for (li = l->lv_first; li != NULL; li = li->li_next)
8155 {
8156 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8157 ga_concat(&ga, buf);
8158 }
8159 ga_append(&ga, NUL);
8160 }
8161 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8162 {
8163 for (li = l->lv_first; li != NULL; li = li->li_next)
8164 ga_append(&ga, tv_get_number(&li->li_tv));
8165 ga_append(&ga, NUL);
8166 }
8167
8168 rettv->v_type = VAR_STRING;
8169 rettv->vval.v_string = ga.ga_data;
8170}
8171
8172/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008173 * "localtime()" function
8174 */
8175 static void
8176f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8177{
8178 rettv->vval.v_number = (varnumber_T)time(NULL);
8179}
8180
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008181 static void
8182get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8183{
8184 char_u *keys;
8185 char_u *which;
8186 char_u buf[NUMBUFLEN];
8187 char_u *keys_buf = NULL;
8188 char_u *rhs;
8189 int mode;
8190 int abbr = FALSE;
8191 int get_dict = FALSE;
8192 mapblock_T *mp;
8193 int buffer_local;
8194
8195 /* return empty string for failure */
8196 rettv->v_type = VAR_STRING;
8197 rettv->vval.v_string = NULL;
8198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008199 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008200 if (*keys == NUL)
8201 return;
8202
8203 if (argvars[1].v_type != VAR_UNKNOWN)
8204 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008205 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008206 if (argvars[2].v_type != VAR_UNKNOWN)
8207 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008208 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008209 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008210 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008211 }
8212 }
8213 else
8214 which = (char_u *)"";
8215 if (which == NULL)
8216 return;
8217
8218 mode = get_map_mode(&which, 0);
8219
8220 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8221 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8222 vim_free(keys_buf);
8223
8224 if (!get_dict)
8225 {
8226 /* Return a string. */
8227 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008228 {
8229 if (*rhs == NUL)
8230 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8231 else
8232 rettv->vval.v_string = str2special_save(rhs, FALSE);
8233 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008234
8235 }
8236 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8237 {
8238 /* Return a dictionary. */
8239 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8240 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8241 dict_T *dict = rettv->vval.v_dict;
8242
Bram Moolenaare0be1672018-07-08 16:50:37 +02008243 dict_add_string(dict, "lhs", lhs);
8244 dict_add_string(dict, "rhs", mp->m_orig_str);
8245 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8246 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8247 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008248 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8249 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008250 dict_add_number(dict, "buffer", (long)buffer_local);
8251 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8252 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253
8254 vim_free(lhs);
8255 vim_free(mapmode);
8256 }
8257}
8258
8259#ifdef FEAT_FLOAT
8260/*
8261 * "log()" function
8262 */
8263 static void
8264f_log(typval_T *argvars, typval_T *rettv)
8265{
8266 float_T f = 0.0;
8267
8268 rettv->v_type = VAR_FLOAT;
8269 if (get_float_arg(argvars, &f) == OK)
8270 rettv->vval.v_float = log(f);
8271 else
8272 rettv->vval.v_float = 0.0;
8273}
8274
8275/*
8276 * "log10()" function
8277 */
8278 static void
8279f_log10(typval_T *argvars, typval_T *rettv)
8280{
8281 float_T f = 0.0;
8282
8283 rettv->v_type = VAR_FLOAT;
8284 if (get_float_arg(argvars, &f) == OK)
8285 rettv->vval.v_float = log10(f);
8286 else
8287 rettv->vval.v_float = 0.0;
8288}
8289#endif
8290
8291#ifdef FEAT_LUA
8292/*
8293 * "luaeval()" function
8294 */
8295 static void
8296f_luaeval(typval_T *argvars, typval_T *rettv)
8297{
8298 char_u *str;
8299 char_u buf[NUMBUFLEN];
8300
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008301 if (check_restricted() || check_secure())
8302 return;
8303
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008304 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008305 do_luaeval(str, argvars + 1, rettv);
8306}
8307#endif
8308
8309/*
8310 * "map()" function
8311 */
8312 static void
8313f_map(typval_T *argvars, typval_T *rettv)
8314{
8315 filter_map(argvars, rettv, TRUE);
8316}
8317
8318/*
8319 * "maparg()" function
8320 */
8321 static void
8322f_maparg(typval_T *argvars, typval_T *rettv)
8323{
8324 get_maparg(argvars, rettv, TRUE);
8325}
8326
8327/*
8328 * "mapcheck()" function
8329 */
8330 static void
8331f_mapcheck(typval_T *argvars, typval_T *rettv)
8332{
8333 get_maparg(argvars, rettv, FALSE);
8334}
8335
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008336typedef enum
8337{
8338 MATCH_END, /* matchend() */
8339 MATCH_MATCH, /* match() */
8340 MATCH_STR, /* matchstr() */
8341 MATCH_LIST, /* matchlist() */
8342 MATCH_POS /* matchstrpos() */
8343} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344
8345 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008346find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008347{
8348 char_u *str = NULL;
8349 long len = 0;
8350 char_u *expr = NULL;
8351 char_u *pat;
8352 regmatch_T regmatch;
8353 char_u patbuf[NUMBUFLEN];
8354 char_u strbuf[NUMBUFLEN];
8355 char_u *save_cpo;
8356 long start = 0;
8357 long nth = 1;
8358 colnr_T startcol = 0;
8359 int match = 0;
8360 list_T *l = NULL;
8361 listitem_T *li = NULL;
8362 long idx = 0;
8363 char_u *tofree = NULL;
8364
8365 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8366 save_cpo = p_cpo;
8367 p_cpo = (char_u *)"";
8368
8369 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008370 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008371 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008372 /* type MATCH_LIST: return empty list when there are no matches.
8373 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008374 if (rettv_list_alloc(rettv) == FAIL)
8375 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008376 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008377 && (list_append_string(rettv->vval.v_list,
8378 (char_u *)"", 0) == FAIL
8379 || list_append_number(rettv->vval.v_list,
8380 (varnumber_T)-1) == FAIL
8381 || list_append_number(rettv->vval.v_list,
8382 (varnumber_T)-1) == FAIL
8383 || list_append_number(rettv->vval.v_list,
8384 (varnumber_T)-1) == FAIL))
8385 {
8386 list_free(rettv->vval.v_list);
8387 rettv->vval.v_list = NULL;
8388 goto theend;
8389 }
8390 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008391 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008392 {
8393 rettv->v_type = VAR_STRING;
8394 rettv->vval.v_string = NULL;
8395 }
8396
8397 if (argvars[0].v_type == VAR_LIST)
8398 {
8399 if ((l = argvars[0].vval.v_list) == NULL)
8400 goto theend;
8401 li = l->lv_first;
8402 }
8403 else
8404 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008405 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008406 len = (long)STRLEN(str);
8407 }
8408
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008409 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008410 if (pat == NULL)
8411 goto theend;
8412
8413 if (argvars[2].v_type != VAR_UNKNOWN)
8414 {
8415 int error = FALSE;
8416
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008417 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008418 if (error)
8419 goto theend;
8420 if (l != NULL)
8421 {
8422 li = list_find(l, start);
8423 if (li == NULL)
8424 goto theend;
8425 idx = l->lv_idx; /* use the cached index */
8426 }
8427 else
8428 {
8429 if (start < 0)
8430 start = 0;
8431 if (start > len)
8432 goto theend;
8433 /* When "count" argument is there ignore matches before "start",
8434 * otherwise skip part of the string. Differs when pattern is "^"
8435 * or "\<". */
8436 if (argvars[3].v_type != VAR_UNKNOWN)
8437 startcol = start;
8438 else
8439 {
8440 str += start;
8441 len -= start;
8442 }
8443 }
8444
8445 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008446 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008447 if (error)
8448 goto theend;
8449 }
8450
8451 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8452 if (regmatch.regprog != NULL)
8453 {
8454 regmatch.rm_ic = p_ic;
8455
8456 for (;;)
8457 {
8458 if (l != NULL)
8459 {
8460 if (li == NULL)
8461 {
8462 match = FALSE;
8463 break;
8464 }
8465 vim_free(tofree);
8466 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8467 if (str == NULL)
8468 break;
8469 }
8470
8471 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8472
8473 if (match && --nth <= 0)
8474 break;
8475 if (l == NULL && !match)
8476 break;
8477
8478 /* Advance to just after the match. */
8479 if (l != NULL)
8480 {
8481 li = li->li_next;
8482 ++idx;
8483 }
8484 else
8485 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486 startcol = (colnr_T)(regmatch.startp[0]
8487 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 if (startcol > (colnr_T)len
8489 || str + startcol <= regmatch.startp[0])
8490 {
8491 match = FALSE;
8492 break;
8493 }
8494 }
8495 }
8496
8497 if (match)
8498 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008499 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008500 {
8501 listitem_T *li1 = rettv->vval.v_list->lv_first;
8502 listitem_T *li2 = li1->li_next;
8503 listitem_T *li3 = li2->li_next;
8504 listitem_T *li4 = li3->li_next;
8505
8506 vim_free(li1->li_tv.vval.v_string);
8507 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8508 (int)(regmatch.endp[0] - regmatch.startp[0]));
8509 li3->li_tv.vval.v_number =
8510 (varnumber_T)(regmatch.startp[0] - expr);
8511 li4->li_tv.vval.v_number =
8512 (varnumber_T)(regmatch.endp[0] - expr);
8513 if (l != NULL)
8514 li2->li_tv.vval.v_number = (varnumber_T)idx;
8515 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008516 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 {
8518 int i;
8519
8520 /* return list with matched string and submatches */
8521 for (i = 0; i < NSUBEXP; ++i)
8522 {
8523 if (regmatch.endp[i] == NULL)
8524 {
8525 if (list_append_string(rettv->vval.v_list,
8526 (char_u *)"", 0) == FAIL)
8527 break;
8528 }
8529 else if (list_append_string(rettv->vval.v_list,
8530 regmatch.startp[i],
8531 (int)(regmatch.endp[i] - regmatch.startp[i]))
8532 == FAIL)
8533 break;
8534 }
8535 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008536 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008537 {
8538 /* return matched string */
8539 if (l != NULL)
8540 copy_tv(&li->li_tv, rettv);
8541 else
8542 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8543 (int)(regmatch.endp[0] - regmatch.startp[0]));
8544 }
8545 else if (l != NULL)
8546 rettv->vval.v_number = idx;
8547 else
8548 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008549 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008550 rettv->vval.v_number =
8551 (varnumber_T)(regmatch.startp[0] - str);
8552 else
8553 rettv->vval.v_number =
8554 (varnumber_T)(regmatch.endp[0] - str);
8555 rettv->vval.v_number += (varnumber_T)(str - expr);
8556 }
8557 }
8558 vim_regfree(regmatch.regprog);
8559 }
8560
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008561theend:
8562 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008563 /* matchstrpos() without a list: drop the second item. */
8564 listitem_remove(rettv->vval.v_list,
8565 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008566 vim_free(tofree);
8567 p_cpo = save_cpo;
8568}
8569
8570/*
8571 * "match()" function
8572 */
8573 static void
8574f_match(typval_T *argvars, typval_T *rettv)
8575{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008576 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577}
8578
Bram Moolenaar95e51472018-07-28 16:55:56 +02008579#ifdef FEAT_SEARCH_EXTRA
8580 static int
8581matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8582{
8583 dictitem_T *di;
8584
8585 if (tv->v_type != VAR_DICT)
8586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008588 return FAIL;
8589 }
8590
8591 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008592 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008593 (char_u *)"conceal", FALSE);
8594
8595 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8596 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008597 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008598 if (*win == NULL)
8599 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008600 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008601 return FAIL;
8602 }
8603 }
8604
8605 return OK;
8606}
8607#endif
8608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609/*
8610 * "matchadd()" function
8611 */
8612 static void
8613f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8614{
8615#ifdef FEAT_SEARCH_EXTRA
8616 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008617 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8618 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 int prio = 10; /* default priority */
8620 int id = -1;
8621 int error = FALSE;
8622 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008623 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624
8625 rettv->vval.v_number = -1;
8626
8627 if (grp == NULL || pat == NULL)
8628 return;
8629 if (argvars[2].v_type != VAR_UNKNOWN)
8630 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008631 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008632 if (argvars[3].v_type != VAR_UNKNOWN)
8633 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008634 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008635 if (argvars[4].v_type != VAR_UNKNOWN
8636 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8637 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 }
8639 }
8640 if (error == TRUE)
8641 return;
8642 if (id >= 1 && id <= 3)
8643 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008644 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 return;
8646 }
8647
Bram Moolenaar95e51472018-07-28 16:55:56 +02008648 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649 conceal_char);
8650#endif
8651}
8652
8653/*
8654 * "matchaddpos()" function
8655 */
8656 static void
8657f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8658{
8659#ifdef FEAT_SEARCH_EXTRA
8660 char_u buf[NUMBUFLEN];
8661 char_u *group;
8662 int prio = 10;
8663 int id = -1;
8664 int error = FALSE;
8665 list_T *l;
8666 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008667 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008668
8669 rettv->vval.v_number = -1;
8670
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008671 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 if (group == NULL)
8673 return;
8674
8675 if (argvars[1].v_type != VAR_LIST)
8676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008677 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008678 return;
8679 }
8680 l = argvars[1].vval.v_list;
8681 if (l == NULL)
8682 return;
8683
8684 if (argvars[2].v_type != VAR_UNKNOWN)
8685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008686 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008687 if (argvars[3].v_type != VAR_UNKNOWN)
8688 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008689 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008690
8691 if (argvars[4].v_type != VAR_UNKNOWN
8692 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8693 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008694 }
8695 }
8696 if (error == TRUE)
8697 return;
8698
8699 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8700 if (id == 1 || id == 2)
8701 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008702 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 return;
8704 }
8705
Bram Moolenaar95e51472018-07-28 16:55:56 +02008706 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707 conceal_char);
8708#endif
8709}
8710
8711/*
8712 * "matcharg()" function
8713 */
8714 static void
8715f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8716{
8717 if (rettv_list_alloc(rettv) == OK)
8718 {
8719#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008720 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 matchitem_T *m;
8722
8723 if (id >= 1 && id <= 3)
8724 {
8725 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8726 {
8727 list_append_string(rettv->vval.v_list,
8728 syn_id2name(m->hlg_id), -1);
8729 list_append_string(rettv->vval.v_list, m->pattern, -1);
8730 }
8731 else
8732 {
8733 list_append_string(rettv->vval.v_list, NULL, -1);
8734 list_append_string(rettv->vval.v_list, NULL, -1);
8735 }
8736 }
8737#endif
8738 }
8739}
8740
8741/*
8742 * "matchdelete()" function
8743 */
8744 static void
8745f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8746{
8747#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008748 win_T *win = get_optional_window(argvars, 1);
8749
8750 if (win == NULL)
8751 rettv->vval.v_number = -1;
8752 else
8753 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008754 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008755#endif
8756}
8757
8758/*
8759 * "matchend()" function
8760 */
8761 static void
8762f_matchend(typval_T *argvars, typval_T *rettv)
8763{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008764 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765}
8766
8767/*
8768 * "matchlist()" function
8769 */
8770 static void
8771f_matchlist(typval_T *argvars, typval_T *rettv)
8772{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008773 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008774}
8775
8776/*
8777 * "matchstr()" function
8778 */
8779 static void
8780f_matchstr(typval_T *argvars, typval_T *rettv)
8781{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008782 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008783}
8784
8785/*
8786 * "matchstrpos()" function
8787 */
8788 static void
8789f_matchstrpos(typval_T *argvars, typval_T *rettv)
8790{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008791 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792}
8793
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 static void
8795max_min(typval_T *argvars, typval_T *rettv, int domax)
8796{
8797 varnumber_T n = 0;
8798 varnumber_T i;
8799 int error = FALSE;
8800
8801 if (argvars[0].v_type == VAR_LIST)
8802 {
8803 list_T *l;
8804 listitem_T *li;
8805
8806 l = argvars[0].vval.v_list;
8807 if (l != NULL)
8808 {
8809 li = l->lv_first;
8810 if (li != NULL)
8811 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008812 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008813 for (;;)
8814 {
8815 li = li->li_next;
8816 if (li == NULL)
8817 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008818 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 if (domax ? i > n : i < n)
8820 n = i;
8821 }
8822 }
8823 }
8824 }
8825 else if (argvars[0].v_type == VAR_DICT)
8826 {
8827 dict_T *d;
8828 int first = TRUE;
8829 hashitem_T *hi;
8830 int todo;
8831
8832 d = argvars[0].vval.v_dict;
8833 if (d != NULL)
8834 {
8835 todo = (int)d->dv_hashtab.ht_used;
8836 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8837 {
8838 if (!HASHITEM_EMPTY(hi))
8839 {
8840 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008841 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008842 if (first)
8843 {
8844 n = i;
8845 first = FALSE;
8846 }
8847 else if (domax ? i > n : i < n)
8848 n = i;
8849 }
8850 }
8851 }
8852 }
8853 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008854 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008855 rettv->vval.v_number = error ? 0 : n;
8856}
8857
8858/*
8859 * "max()" function
8860 */
8861 static void
8862f_max(typval_T *argvars, typval_T *rettv)
8863{
8864 max_min(argvars, rettv, TRUE);
8865}
8866
8867/*
8868 * "min()" function
8869 */
8870 static void
8871f_min(typval_T *argvars, typval_T *rettv)
8872{
8873 max_min(argvars, rettv, FALSE);
8874}
8875
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876/*
8877 * Create the directory in which "dir" is located, and higher levels when
8878 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008879 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008880 */
8881 static int
8882mkdir_recurse(char_u *dir, int prot)
8883{
8884 char_u *p;
8885 char_u *updir;
8886 int r = FAIL;
8887
8888 /* Get end of directory name in "dir".
8889 * We're done when it's "/" or "c:/". */
8890 p = gettail_sep(dir);
8891 if (p <= get_past_head(dir))
8892 return OK;
8893
8894 /* If the directory exists we're done. Otherwise: create it.*/
8895 updir = vim_strnsave(dir, (int)(p - dir));
8896 if (updir == NULL)
8897 return FAIL;
8898 if (mch_isdir(updir))
8899 r = OK;
8900 else if (mkdir_recurse(updir, prot) == OK)
8901 r = vim_mkdir_emsg(updir, prot);
8902 vim_free(updir);
8903 return r;
8904}
8905
8906#ifdef vim_mkdir
8907/*
8908 * "mkdir()" function
8909 */
8910 static void
8911f_mkdir(typval_T *argvars, typval_T *rettv)
8912{
8913 char_u *dir;
8914 char_u buf[NUMBUFLEN];
8915 int prot = 0755;
8916
8917 rettv->vval.v_number = FAIL;
8918 if (check_restricted() || check_secure())
8919 return;
8920
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008921 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008922 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008923 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008925 if (*gettail(dir) == NUL)
8926 /* remove trailing slashes */
8927 *gettail_sep(dir) = NUL;
8928
8929 if (argvars[1].v_type != VAR_UNKNOWN)
8930 {
8931 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008932 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008933 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008934 if (prot == -1)
8935 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008936 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008937 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008938 {
8939 if (mch_isdir(dir))
8940 {
8941 /* With the "p" flag it's OK if the dir already exists. */
8942 rettv->vval.v_number = OK;
8943 return;
8944 }
8945 mkdir_recurse(dir, prot);
8946 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008947 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008948 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949}
8950#endif
8951
8952/*
8953 * "mode()" function
8954 */
8955 static void
8956f_mode(typval_T *argvars, typval_T *rettv)
8957{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008958 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008959
Bram Moolenaar612cc382018-07-29 15:34:26 +02008960 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961
8962 if (time_for_testing == 93784)
8963 {
8964 /* Testing the two-character code. */
8965 buf[0] = 'x';
8966 buf[1] = '!';
8967 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008968#ifdef FEAT_TERMINAL
8969 else if (term_use_loop())
8970 buf[0] = 't';
8971#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008972 else if (VIsual_active)
8973 {
8974 if (VIsual_select)
8975 buf[0] = VIsual_mode + 's' - 'v';
8976 else
8977 buf[0] = VIsual_mode;
8978 }
8979 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8980 || State == CONFIRM)
8981 {
8982 buf[0] = 'r';
8983 if (State == ASKMORE)
8984 buf[1] = 'm';
8985 else if (State == CONFIRM)
8986 buf[1] = '?';
8987 }
8988 else if (State == EXTERNCMD)
8989 buf[0] = '!';
8990 else if (State & INSERT)
8991 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008992 if (State & VREPLACE_FLAG)
8993 {
8994 buf[0] = 'R';
8995 buf[1] = 'v';
8996 }
8997 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008998 {
8999 if (State & REPLACE_FLAG)
9000 buf[0] = 'R';
9001 else
9002 buf[0] = 'i';
9003#ifdef FEAT_INS_EXPAND
9004 if (ins_compl_active())
9005 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01009006 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01009007 buf[1] = 'x';
9008#endif
9009 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01009011 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009012 {
9013 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009014 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009015 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01009016 else if (exmode_active == EXMODE_NORMAL)
9017 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009018 }
9019 else
9020 {
9021 buf[0] = 'n';
9022 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009023 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009024 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01009025 // to be able to detect force-linewise/blockwise/characterwise operations
9026 buf[2] = motion_force;
9027 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02009028 else if (restart_edit == 'I' || restart_edit == 'R'
9029 || restart_edit == 'V')
9030 {
9031 buf[1] = 'i';
9032 buf[2] = restart_edit;
9033 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 }
9035
9036 /* Clear out the minor mode when the argument is not a non-zero number or
9037 * non-empty string. */
9038 if (!non_zero_arg(&argvars[0]))
9039 buf[1] = NUL;
9040
9041 rettv->vval.v_string = vim_strsave(buf);
9042 rettv->v_type = VAR_STRING;
9043}
9044
9045#if defined(FEAT_MZSCHEME) || defined(PROTO)
9046/*
9047 * "mzeval()" function
9048 */
9049 static void
9050f_mzeval(typval_T *argvars, typval_T *rettv)
9051{
9052 char_u *str;
9053 char_u buf[NUMBUFLEN];
9054
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009055 if (check_restricted() || check_secure())
9056 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009057 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009058 do_mzeval(str, rettv);
9059}
9060
9061 void
9062mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9063{
9064 typval_T argvars[3];
9065
9066 argvars[0].v_type = VAR_STRING;
9067 argvars[0].vval.v_string = name;
9068 copy_tv(args, &argvars[1]);
9069 argvars[2].v_type = VAR_UNKNOWN;
9070 f_call(argvars, rettv);
9071 clear_tv(&argvars[1]);
9072}
9073#endif
9074
9075/*
9076 * "nextnonblank()" function
9077 */
9078 static void
9079f_nextnonblank(typval_T *argvars, typval_T *rettv)
9080{
9081 linenr_T lnum;
9082
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009083 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084 {
9085 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9086 {
9087 lnum = 0;
9088 break;
9089 }
9090 if (*skipwhite(ml_get(lnum)) != NUL)
9091 break;
9092 }
9093 rettv->vval.v_number = lnum;
9094}
9095
9096/*
9097 * "nr2char()" function
9098 */
9099 static void
9100f_nr2char(typval_T *argvars, typval_T *rettv)
9101{
9102 char_u buf[NUMBUFLEN];
9103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009104 if (has_mbyte)
9105 {
9106 int utf8 = 0;
9107
9108 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009109 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009110 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009111 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009113 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009114 }
9115 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 buf[1] = NUL;
9119 }
9120 rettv->v_type = VAR_STRING;
9121 rettv->vval.v_string = vim_strsave(buf);
9122}
9123
9124/*
9125 * "or(expr, expr)" function
9126 */
9127 static void
9128f_or(typval_T *argvars, typval_T *rettv)
9129{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009130 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9131 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009132}
9133
9134/*
9135 * "pathshorten()" function
9136 */
9137 static void
9138f_pathshorten(typval_T *argvars, typval_T *rettv)
9139{
9140 char_u *p;
9141
9142 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009143 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009144 if (p == NULL)
9145 rettv->vval.v_string = NULL;
9146 else
9147 {
9148 p = vim_strsave(p);
9149 rettv->vval.v_string = p;
9150 if (p != NULL)
9151 shorten_dir(p);
9152 }
9153}
9154
9155#ifdef FEAT_PERL
9156/*
9157 * "perleval()" function
9158 */
9159 static void
9160f_perleval(typval_T *argvars, typval_T *rettv)
9161{
9162 char_u *str;
9163 char_u buf[NUMBUFLEN];
9164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009165 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009166 do_perleval(str, rettv);
9167}
9168#endif
9169
9170#ifdef FEAT_FLOAT
9171/*
9172 * "pow()" function
9173 */
9174 static void
9175f_pow(typval_T *argvars, typval_T *rettv)
9176{
9177 float_T fx = 0.0, fy = 0.0;
9178
9179 rettv->v_type = VAR_FLOAT;
9180 if (get_float_arg(argvars, &fx) == OK
9181 && get_float_arg(&argvars[1], &fy) == OK)
9182 rettv->vval.v_float = pow(fx, fy);
9183 else
9184 rettv->vval.v_float = 0.0;
9185}
9186#endif
9187
9188/*
9189 * "prevnonblank()" function
9190 */
9191 static void
9192f_prevnonblank(typval_T *argvars, typval_T *rettv)
9193{
9194 linenr_T lnum;
9195
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009196 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009197 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9198 lnum = 0;
9199 else
9200 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9201 --lnum;
9202 rettv->vval.v_number = lnum;
9203}
9204
9205/* This dummy va_list is here because:
9206 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9207 * - locally in the function results in a "used before set" warning
9208 * - using va_start() to initialize it gives "function with fixed args" error */
9209static va_list ap;
9210
9211/*
9212 * "printf()" function
9213 */
9214 static void
9215f_printf(typval_T *argvars, typval_T *rettv)
9216{
9217 char_u buf[NUMBUFLEN];
9218 int len;
9219 char_u *s;
9220 int saved_did_emsg = did_emsg;
9221 char *fmt;
9222
9223 rettv->v_type = VAR_STRING;
9224 rettv->vval.v_string = NULL;
9225
9226 /* Get the required length, allocate the buffer and do it for real. */
9227 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009228 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009229 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009230 if (!did_emsg)
9231 {
9232 s = alloc(len + 1);
9233 if (s != NULL)
9234 {
9235 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009236 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9237 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238 }
9239 }
9240 did_emsg |= saved_did_emsg;
9241}
9242
Bram Moolenaarf2732452018-06-03 14:47:35 +02009243#ifdef FEAT_JOB_CHANNEL
9244/*
9245 * "prompt_setcallback({buffer}, {callback})" function
9246 */
9247 static void
9248f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9249{
9250 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009251 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009252
9253 if (check_secure())
9254 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009255 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009256 if (buf == NULL)
9257 return;
9258
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009259 callback = get_callback(&argvars[1]);
9260 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009261 return;
9262
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009263 free_callback(&buf->b_prompt_callback);
9264 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009265}
9266
9267/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009268 * "prompt_setinterrupt({buffer}, {callback})" function
9269 */
9270 static void
9271f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9272{
9273 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009274 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009275
9276 if (check_secure())
9277 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009278 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009279 if (buf == NULL)
9280 return;
9281
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009282 callback = get_callback(&argvars[1]);
9283 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009284 return;
9285
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009286 free_callback(&buf->b_prompt_interrupt);
9287 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009288}
9289
9290/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009291 * "prompt_setprompt({buffer}, {text})" function
9292 */
9293 static void
9294f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9295{
9296 buf_T *buf;
9297 char_u *text;
9298
9299 if (check_secure())
9300 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009301 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009302 if (buf == NULL)
9303 return;
9304
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009305 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009306 vim_free(buf->b_prompt_text);
9307 buf->b_prompt_text = vim_strsave(text);
9308}
9309#endif
9310
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009311/*
9312 * "pumvisible()" function
9313 */
9314 static void
9315f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9316{
9317#ifdef FEAT_INS_EXPAND
9318 if (pum_visible())
9319 rettv->vval.v_number = 1;
9320#endif
9321}
9322
9323#ifdef FEAT_PYTHON3
9324/*
9325 * "py3eval()" function
9326 */
9327 static void
9328f_py3eval(typval_T *argvars, typval_T *rettv)
9329{
9330 char_u *str;
9331 char_u buf[NUMBUFLEN];
9332
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009333 if (check_restricted() || check_secure())
9334 return;
9335
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009336 if (p_pyx == 0)
9337 p_pyx = 3;
9338
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 do_py3eval(str, rettv);
9341}
9342#endif
9343
9344#ifdef FEAT_PYTHON
9345/*
9346 * "pyeval()" function
9347 */
9348 static void
9349f_pyeval(typval_T *argvars, typval_T *rettv)
9350{
9351 char_u *str;
9352 char_u buf[NUMBUFLEN];
9353
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009354 if (check_restricted() || check_secure())
9355 return;
9356
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009357 if (p_pyx == 0)
9358 p_pyx = 2;
9359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009360 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361 do_pyeval(str, rettv);
9362}
9363#endif
9364
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009365#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9366/*
9367 * "pyxeval()" function
9368 */
9369 static void
9370f_pyxeval(typval_T *argvars, typval_T *rettv)
9371{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009372 if (check_restricted() || check_secure())
9373 return;
9374
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009375# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9376 init_pyxversion();
9377 if (p_pyx == 2)
9378 f_pyeval(argvars, rettv);
9379 else
9380 f_py3eval(argvars, rettv);
9381# elif defined(FEAT_PYTHON)
9382 f_pyeval(argvars, rettv);
9383# elif defined(FEAT_PYTHON3)
9384 f_py3eval(argvars, rettv);
9385# endif
9386}
9387#endif
9388
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009389/*
9390 * "range()" function
9391 */
9392 static void
9393f_range(typval_T *argvars, typval_T *rettv)
9394{
9395 varnumber_T start;
9396 varnumber_T end;
9397 varnumber_T stride = 1;
9398 varnumber_T i;
9399 int error = FALSE;
9400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009401 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009402 if (argvars[1].v_type == VAR_UNKNOWN)
9403 {
9404 end = start - 1;
9405 start = 0;
9406 }
9407 else
9408 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009409 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009410 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009411 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009412 }
9413
9414 if (error)
9415 return; /* type error; errmsg already given */
9416 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009417 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009418 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009420 else
9421 {
9422 if (rettv_list_alloc(rettv) == OK)
9423 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9424 if (list_append_number(rettv->vval.v_list,
9425 (varnumber_T)i) == FAIL)
9426 break;
9427 }
9428}
9429
9430/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009431 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009432 */
9433 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009434readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009435{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009436 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009437 typval_T save_val;
9438 typval_T rettv;
9439 typval_T argv[2];
9440 int retval = 0;
9441 int error = FALSE;
9442
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009443 if (expr->v_type == VAR_UNKNOWN)
9444 return 1;
9445
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009446 prepare_vimvar(VV_VAL, &save_val);
9447 set_vim_var_string(VV_VAL, name, -1);
9448 argv[0].v_type = VAR_STRING;
9449 argv[0].vval.v_string = name;
9450
9451 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9452 goto theend;
9453
9454 retval = tv_get_number_chk(&rettv, &error);
9455 if (error)
9456 retval = -1;
9457 clear_tv(&rettv);
9458
9459theend:
9460 set_vim_var_string(VV_VAL, NULL, 0);
9461 restore_vimvar(VV_VAL, &save_val);
9462 return retval;
9463}
9464
9465/*
9466 * "readdir()" function
9467 */
9468 static void
9469f_readdir(typval_T *argvars, typval_T *rettv)
9470{
9471 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009472 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009473 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009474 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009475 garray_T ga;
9476 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009477
9478 if (rettv_list_alloc(rettv) == FAIL)
9479 return;
9480 path = tv_get_string(&argvars[0]);
9481 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009482
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009483 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9484 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009485 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009486 for (i = 0; i < ga.ga_len; i++)
9487 {
9488 p = ((char_u **)ga.ga_data)[i];
9489 list_append_string(rettv->vval.v_list, p, -1);
9490 }
9491 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009492 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009493}
9494
9495/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496 * "readfile()" function
9497 */
9498 static void
9499f_readfile(typval_T *argvars, typval_T *rettv)
9500{
9501 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009502 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009503 int failed = FALSE;
9504 char_u *fname;
9505 FILE *fd;
9506 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9507 int io_size = sizeof(buf);
9508 int readlen; /* size of last fread() */
9509 char_u *prev = NULL; /* previously read bytes, if any */
9510 long prevlen = 0; /* length of data in prev */
9511 long prevsize = 0; /* size of prev buffer */
9512 long maxline = MAXLNUM;
9513 long cnt = 0;
9514 char_u *p; /* position in buf */
9515 char_u *start; /* start of current line */
9516
9517 if (argvars[1].v_type != VAR_UNKNOWN)
9518 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009519 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009520 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009521 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9522 blob = TRUE;
9523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009525 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009526 }
9527
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009528 if (blob)
9529 {
9530 if (rettv_blob_alloc(rettv) == FAIL)
9531 return;
9532 }
9533 else
9534 {
9535 if (rettv_list_alloc(rettv) == FAIL)
9536 return;
9537 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009538
9539 /* Always open the file in binary mode, library functions have a mind of
9540 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009541 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009542 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009544 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545 return;
9546 }
9547
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009548 if (blob)
9549 {
9550 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009552 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009553 blob_free(rettv->vval.v_blob);
9554 }
9555 fclose(fd);
9556 return;
9557 }
9558
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009559 while (cnt < maxline || maxline < 0)
9560 {
9561 readlen = (int)fread(buf, 1, io_size, fd);
9562
9563 /* This for loop processes what was read, but is also entered at end
9564 * of file so that either:
9565 * - an incomplete line gets written
9566 * - a "binary" file gets an empty line at the end if it ends in a
9567 * newline. */
9568 for (p = buf, start = buf;
9569 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9570 ++p)
9571 {
9572 if (*p == '\n' || readlen <= 0)
9573 {
9574 listitem_T *li;
9575 char_u *s = NULL;
9576 long_u len = p - start;
9577
9578 /* Finished a line. Remove CRs before NL. */
9579 if (readlen > 0 && !binary)
9580 {
9581 while (len > 0 && start[len - 1] == '\r')
9582 --len;
9583 /* removal may cross back to the "prev" string */
9584 if (len == 0)
9585 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9586 --prevlen;
9587 }
9588 if (prevlen == 0)
9589 s = vim_strnsave(start, (int)len);
9590 else
9591 {
9592 /* Change "prev" buffer to be the right size. This way
9593 * the bytes are only copied once, and very long lines are
9594 * allocated only once. */
9595 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9596 {
9597 mch_memmove(s + prevlen, start, len);
9598 s[prevlen + len] = NUL;
9599 prev = NULL; /* the list will own the string */
9600 prevlen = prevsize = 0;
9601 }
9602 }
9603 if (s == NULL)
9604 {
9605 do_outofmem_msg((long_u) prevlen + len + 1);
9606 failed = TRUE;
9607 break;
9608 }
9609
9610 if ((li = listitem_alloc()) == NULL)
9611 {
9612 vim_free(s);
9613 failed = TRUE;
9614 break;
9615 }
9616 li->li_tv.v_type = VAR_STRING;
9617 li->li_tv.v_lock = 0;
9618 li->li_tv.vval.v_string = s;
9619 list_append(rettv->vval.v_list, li);
9620
9621 start = p + 1; /* step over newline */
9622 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9623 break;
9624 }
9625 else if (*p == NUL)
9626 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009627 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9628 * when finding the BF and check the previous two bytes. */
9629 else if (*p == 0xbf && enc_utf8 && !binary)
9630 {
9631 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9632 * + 1, these may be in the "prev" string. */
9633 char_u back1 = p >= buf + 1 ? p[-1]
9634 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9635 char_u back2 = p >= buf + 2 ? p[-2]
9636 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9637 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9638
9639 if (back2 == 0xef && back1 == 0xbb)
9640 {
9641 char_u *dest = p - 2;
9642
9643 /* Usually a BOM is at the beginning of a file, and so at
9644 * the beginning of a line; then we can just step over it.
9645 */
9646 if (start == dest)
9647 start = p + 1;
9648 else
9649 {
9650 /* have to shuffle buf to close gap */
9651 int adjust_prevlen = 0;
9652
9653 if (dest < buf)
9654 {
9655 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9656 dest = buf;
9657 }
9658 if (readlen > p - buf + 1)
9659 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9660 readlen -= 3 - adjust_prevlen;
9661 prevlen -= adjust_prevlen;
9662 p = dest - 1;
9663 }
9664 }
9665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009666 } /* for */
9667
9668 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9669 break;
9670 if (start < p)
9671 {
9672 /* There's part of a line in buf, store it in "prev". */
9673 if (p - start + prevlen >= prevsize)
9674 {
9675 /* need bigger "prev" buffer */
9676 char_u *newprev;
9677
9678 /* A common use case is ordinary text files and "prev" gets a
9679 * fragment of a line, so the first allocation is made
9680 * small, to avoid repeatedly 'allocing' large and
9681 * 'reallocing' small. */
9682 if (prevsize == 0)
9683 prevsize = (long)(p - start);
9684 else
9685 {
9686 long grow50pc = (prevsize * 3) / 2;
9687 long growmin = (long)((p - start) * 2 + prevlen);
9688 prevsize = grow50pc > growmin ? grow50pc : growmin;
9689 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009690 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009691 if (newprev == NULL)
9692 {
9693 do_outofmem_msg((long_u)prevsize);
9694 failed = TRUE;
9695 break;
9696 }
9697 prev = newprev;
9698 }
9699 /* Add the line part to end of "prev". */
9700 mch_memmove(prev + prevlen, start, p - start);
9701 prevlen += (long)(p - start);
9702 }
9703 } /* while */
9704
9705 /*
9706 * For a negative line count use only the lines at the end of the file,
9707 * free the rest.
9708 */
9709 if (!failed && maxline < 0)
9710 while (cnt > -maxline)
9711 {
9712 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9713 --cnt;
9714 }
9715
9716 if (failed)
9717 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009718 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009720 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 }
9722
9723 vim_free(prev);
9724 fclose(fd);
9725}
9726
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009727 static void
9728return_register(int regname, typval_T *rettv)
9729{
9730 char_u buf[2] = {0, 0};
9731
9732 buf[0] = (char_u)regname;
9733 rettv->v_type = VAR_STRING;
9734 rettv->vval.v_string = vim_strsave(buf);
9735}
9736
9737/*
9738 * "reg_executing()" function
9739 */
9740 static void
9741f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9742{
9743 return_register(reg_executing, rettv);
9744}
9745
9746/*
9747 * "reg_recording()" function
9748 */
9749 static void
9750f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9751{
9752 return_register(reg_recording, rettv);
9753}
9754
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009755#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009756/*
9757 * Convert a List to proftime_T.
9758 * Return FAIL when there is something wrong.
9759 */
9760 static int
9761list2proftime(typval_T *arg, proftime_T *tm)
9762{
9763 long n1, n2;
9764 int error = FALSE;
9765
9766 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9767 || arg->vval.v_list->lv_len != 2)
9768 return FAIL;
9769 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9770 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009771# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009772 tm->HighPart = n1;
9773 tm->LowPart = n2;
9774# else
9775 tm->tv_sec = n1;
9776 tm->tv_usec = n2;
9777# endif
9778 return error ? FAIL : OK;
9779}
9780#endif /* FEAT_RELTIME */
9781
9782/*
9783 * "reltime()" function
9784 */
9785 static void
9786f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9787{
9788#ifdef FEAT_RELTIME
9789 proftime_T res;
9790 proftime_T start;
9791
9792 if (argvars[0].v_type == VAR_UNKNOWN)
9793 {
9794 /* No arguments: get current time. */
9795 profile_start(&res);
9796 }
9797 else if (argvars[1].v_type == VAR_UNKNOWN)
9798 {
9799 if (list2proftime(&argvars[0], &res) == FAIL)
9800 return;
9801 profile_end(&res);
9802 }
9803 else
9804 {
9805 /* Two arguments: compute the difference. */
9806 if (list2proftime(&argvars[0], &start) == FAIL
9807 || list2proftime(&argvars[1], &res) == FAIL)
9808 return;
9809 profile_sub(&res, &start);
9810 }
9811
9812 if (rettv_list_alloc(rettv) == OK)
9813 {
9814 long n1, n2;
9815
Bram Moolenaar4f974752019-02-17 17:44:42 +01009816# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 n1 = res.HighPart;
9818 n2 = res.LowPart;
9819# else
9820 n1 = res.tv_sec;
9821 n2 = res.tv_usec;
9822# endif
9823 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9824 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9825 }
9826#endif
9827}
9828
9829#ifdef FEAT_FLOAT
9830/*
9831 * "reltimefloat()" function
9832 */
9833 static void
9834f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9835{
9836# ifdef FEAT_RELTIME
9837 proftime_T tm;
9838# endif
9839
9840 rettv->v_type = VAR_FLOAT;
9841 rettv->vval.v_float = 0;
9842# ifdef FEAT_RELTIME
9843 if (list2proftime(&argvars[0], &tm) == OK)
9844 rettv->vval.v_float = profile_float(&tm);
9845# endif
9846}
9847#endif
9848
9849/*
9850 * "reltimestr()" function
9851 */
9852 static void
9853f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9854{
9855#ifdef FEAT_RELTIME
9856 proftime_T tm;
9857#endif
9858
9859 rettv->v_type = VAR_STRING;
9860 rettv->vval.v_string = NULL;
9861#ifdef FEAT_RELTIME
9862 if (list2proftime(&argvars[0], &tm) == OK)
9863 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9864#endif
9865}
9866
9867#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 static void
9869make_connection(void)
9870{
9871 if (X_DISPLAY == NULL
9872# ifdef FEAT_GUI
9873 && !gui.in_use
9874# endif
9875 )
9876 {
9877 x_force_connect = TRUE;
9878 setup_term_clip();
9879 x_force_connect = FALSE;
9880 }
9881}
9882
9883 static int
9884check_connection(void)
9885{
9886 make_connection();
9887 if (X_DISPLAY == NULL)
9888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009889 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 return FAIL;
9891 }
9892 return OK;
9893}
9894#endif
9895
9896#ifdef FEAT_CLIENTSERVER
9897 static void
9898remote_common(typval_T *argvars, typval_T *rettv, int expr)
9899{
9900 char_u *server_name;
9901 char_u *keys;
9902 char_u *r = NULL;
9903 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009904 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009905# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009906 HWND w;
9907# else
9908 Window w;
9909# endif
9910
9911 if (check_restricted() || check_secure())
9912 return;
9913
9914# ifdef FEAT_X11
9915 if (check_connection() == FAIL)
9916 return;
9917# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009918 if (argvars[2].v_type != VAR_UNKNOWN
9919 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009922 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 if (server_name == NULL)
9924 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009925 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009926# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009927 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009928# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009929 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9930 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009931# endif
9932 {
9933 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009935 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009936 vim_free(r);
9937 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009938 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009939 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 return;
9941 }
9942
9943 rettv->vval.v_string = r;
9944
9945 if (argvars[2].v_type != VAR_UNKNOWN)
9946 {
9947 dictitem_T v;
9948 char_u str[30];
9949 char_u *idvar;
9950
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009951 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009952 if (idvar != NULL && *idvar != NUL)
9953 {
9954 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9955 v.di_tv.v_type = VAR_STRING;
9956 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009957 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009958 vim_free(v.di_tv.vval.v_string);
9959 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009960 }
9961}
9962#endif
9963
9964/*
9965 * "remote_expr()" function
9966 */
9967 static void
9968f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9969{
9970 rettv->v_type = VAR_STRING;
9971 rettv->vval.v_string = NULL;
9972#ifdef FEAT_CLIENTSERVER
9973 remote_common(argvars, rettv, TRUE);
9974#endif
9975}
9976
9977/*
9978 * "remote_foreground()" function
9979 */
9980 static void
9981f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9982{
9983#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009984# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985 /* On Win32 it's done in this application. */
9986 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009987 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009988
9989 if (server_name != NULL)
9990 serverForeground(server_name);
9991 }
9992# else
9993 /* Send a foreground() expression to the server. */
9994 argvars[1].v_type = VAR_STRING;
9995 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9996 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009997 rettv->v_type = VAR_STRING;
9998 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009999 remote_common(argvars, rettv, TRUE);
10000 vim_free(argvars[1].vval.v_string);
10001# endif
10002#endif
10003}
10004
10005 static void
10006f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
10007{
10008#ifdef FEAT_CLIENTSERVER
10009 dictitem_T v;
10010 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010011# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 long_u n = 0;
10013# endif
10014 char_u *serverid;
10015
10016 if (check_restricted() || check_secure())
10017 {
10018 rettv->vval.v_number = -1;
10019 return;
10020 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010021 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010022 if (serverid == NULL)
10023 {
10024 rettv->vval.v_number = -1;
10025 return; /* type error; errmsg already given */
10026 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010027# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010028 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
10029 if (n == 0)
10030 rettv->vval.v_number = -1;
10031 else
10032 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010033 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010034 rettv->vval.v_number = (s != NULL);
10035 }
10036# else
10037 if (check_connection() == FAIL)
10038 return;
10039
10040 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10041 serverStrToWin(serverid), &s);
10042# endif
10043
10044 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10045 {
10046 char_u *retvar;
10047
10048 v.di_tv.v_type = VAR_STRING;
10049 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010050 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051 if (retvar != NULL)
10052 set_var(retvar, &v.di_tv, FALSE);
10053 vim_free(v.di_tv.vval.v_string);
10054 }
10055#else
10056 rettv->vval.v_number = -1;
10057#endif
10058}
10059
10060 static void
10061f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10062{
10063 char_u *r = NULL;
10064
10065#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010066 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010067
10068 if (serverid != NULL && !check_restricted() && !check_secure())
10069 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010070 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010071# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010072 /* The server's HWND is encoded in the 'id' parameter */
10073 long_u n = 0;
10074# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010075
10076 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010077 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010078
Bram Moolenaar4f974752019-02-17 17:44:42 +010010079# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10081 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010082 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 if (r == NULL)
10084# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010085 if (check_connection() == FAIL
10086 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10087 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010089 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010090 }
10091#endif
10092 rettv->v_type = VAR_STRING;
10093 rettv->vval.v_string = r;
10094}
10095
10096/*
10097 * "remote_send()" function
10098 */
10099 static void
10100f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10101{
10102 rettv->v_type = VAR_STRING;
10103 rettv->vval.v_string = NULL;
10104#ifdef FEAT_CLIENTSERVER
10105 remote_common(argvars, rettv, FALSE);
10106#endif
10107}
10108
10109/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010110 * "remote_startserver()" function
10111 */
10112 static void
10113f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10114{
10115#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010116 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010117
10118 if (server == NULL)
10119 return; /* type error; errmsg already given */
10120 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010121 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010122 else
10123 {
10124# ifdef FEAT_X11
10125 if (check_connection() == OK)
10126 serverRegisterName(X_DISPLAY, server);
10127# else
10128 serverSetName(server);
10129# endif
10130 }
10131#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010132 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010133#endif
10134}
10135
10136/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010137 * "remove()" function
10138 */
10139 static void
10140f_remove(typval_T *argvars, typval_T *rettv)
10141{
10142 list_T *l;
10143 listitem_T *item, *item2;
10144 listitem_T *li;
10145 long idx;
10146 long end;
10147 char_u *key;
10148 dict_T *d;
10149 dictitem_T *di;
10150 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010151 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010152
10153 if (argvars[0].v_type == VAR_DICT)
10154 {
10155 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010156 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010157 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010158 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010160 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010161 if (key != NULL)
10162 {
10163 di = dict_find(d, key, -1);
10164 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010165 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10167 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10168 {
10169 *rettv = di->di_tv;
10170 init_tv(&di->di_tv);
10171 dictitem_remove(d, di);
10172 }
10173 }
10174 }
10175 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010176 else if (argvars[0].v_type == VAR_BLOB)
10177 {
10178 idx = (long)tv_get_number_chk(&argvars[1], &error);
10179 if (!error)
10180 {
10181 blob_T *b = argvars[0].vval.v_blob;
10182 int len = blob_len(b);
10183 char_u *p;
10184
10185 if (idx < 0)
10186 // count from the end
10187 idx = len + idx;
10188 if (idx < 0 || idx >= len)
10189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010190 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010191 return;
10192 }
10193 if (argvars[2].v_type == VAR_UNKNOWN)
10194 {
10195 // Remove one item, return its value.
10196 p = (char_u *)b->bv_ga.ga_data;
10197 rettv->vval.v_number = (varnumber_T) *(p + idx);
10198 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10199 --b->bv_ga.ga_len;
10200 }
10201 else
10202 {
10203 blob_T *blob;
10204
10205 // Remove range of items, return list with values.
10206 end = (long)tv_get_number_chk(&argvars[2], &error);
10207 if (error)
10208 return;
10209 if (end < 0)
10210 // count from the end
10211 end = len + end;
10212 if (end >= len || idx > end)
10213 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010214 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010215 return;
10216 }
10217 blob = blob_alloc();
10218 if (blob == NULL)
10219 return;
10220 blob->bv_ga.ga_len = end - idx + 1;
10221 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10222 {
10223 vim_free(blob);
10224 return;
10225 }
10226 p = (char_u *)b->bv_ga.ga_data;
10227 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10228 (size_t)(end - idx + 1));
10229 ++blob->bv_refcount;
10230 rettv->v_type = VAR_BLOB;
10231 rettv->vval.v_blob = blob;
10232
10233 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10234 b->bv_ga.ga_len -= end - idx + 1;
10235 }
10236 }
10237 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010238 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010239 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010241 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010242 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010243 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010244 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010245 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010246 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010247 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010248 else
10249 {
10250 if (argvars[2].v_type == VAR_UNKNOWN)
10251 {
10252 /* Remove one item, return its value. */
10253 vimlist_remove(l, item, item);
10254 *rettv = item->li_tv;
10255 vim_free(item);
10256 }
10257 else
10258 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010259 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010260 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010261 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010262 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010264 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010265 else
10266 {
10267 int cnt = 0;
10268
10269 for (li = item; li != NULL; li = li->li_next)
10270 {
10271 ++cnt;
10272 if (li == item2)
10273 break;
10274 }
10275 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010276 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 else
10278 {
10279 vimlist_remove(l, item, item2);
10280 if (rettv_list_alloc(rettv) == OK)
10281 {
10282 l = rettv->vval.v_list;
10283 l->lv_first = item;
10284 l->lv_last = item2;
10285 item->li_prev = NULL;
10286 item2->li_next = NULL;
10287 l->lv_len = cnt;
10288 }
10289 }
10290 }
10291 }
10292 }
10293 }
10294}
10295
10296/*
10297 * "rename({from}, {to})" function
10298 */
10299 static void
10300f_rename(typval_T *argvars, typval_T *rettv)
10301{
10302 char_u buf[NUMBUFLEN];
10303
10304 if (check_restricted() || check_secure())
10305 rettv->vval.v_number = -1;
10306 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010307 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10308 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010309}
10310
10311/*
10312 * "repeat()" function
10313 */
10314 static void
10315f_repeat(typval_T *argvars, typval_T *rettv)
10316{
10317 char_u *p;
10318 int n;
10319 int slen;
10320 int len;
10321 char_u *r;
10322 int i;
10323
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010324 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 if (argvars[0].v_type == VAR_LIST)
10326 {
10327 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10328 while (n-- > 0)
10329 if (list_extend(rettv->vval.v_list,
10330 argvars[0].vval.v_list, NULL) == FAIL)
10331 break;
10332 }
10333 else
10334 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010335 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010336 rettv->v_type = VAR_STRING;
10337 rettv->vval.v_string = NULL;
10338
10339 slen = (int)STRLEN(p);
10340 len = slen * n;
10341 if (len <= 0)
10342 return;
10343
10344 r = alloc(len + 1);
10345 if (r != NULL)
10346 {
10347 for (i = 0; i < n; i++)
10348 mch_memmove(r + i * slen, p, (size_t)slen);
10349 r[len] = NUL;
10350 }
10351
10352 rettv->vval.v_string = r;
10353 }
10354}
10355
10356/*
10357 * "resolve()" function
10358 */
10359 static void
10360f_resolve(typval_T *argvars, typval_T *rettv)
10361{
10362 char_u *p;
10363#ifdef HAVE_READLINK
10364 char_u *buf = NULL;
10365#endif
10366
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010367 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010368#ifdef FEAT_SHORTCUT
10369 {
10370 char_u *v = NULL;
10371
Bram Moolenaardce1e892019-02-10 23:18:53 +010010372 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010373 if (v != NULL)
10374 rettv->vval.v_string = v;
10375 else
10376 rettv->vval.v_string = vim_strsave(p);
10377 }
10378#else
10379# ifdef HAVE_READLINK
10380 {
10381 char_u *cpy;
10382 int len;
10383 char_u *remain = NULL;
10384 char_u *q;
10385 int is_relative_to_current = FALSE;
10386 int has_trailing_pathsep = FALSE;
10387 int limit = 100;
10388
10389 p = vim_strsave(p);
10390
10391 if (p[0] == '.' && (vim_ispathsep(p[1])
10392 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10393 is_relative_to_current = TRUE;
10394
10395 len = STRLEN(p);
10396 if (len > 0 && after_pathsep(p, p + len))
10397 {
10398 has_trailing_pathsep = TRUE;
10399 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10400 }
10401
10402 q = getnextcomp(p);
10403 if (*q != NUL)
10404 {
10405 /* Separate the first path component in "p", and keep the
10406 * remainder (beginning with the path separator). */
10407 remain = vim_strsave(q - 1);
10408 q[-1] = NUL;
10409 }
10410
10411 buf = alloc(MAXPATHL + 1);
10412 if (buf == NULL)
10413 goto fail;
10414
10415 for (;;)
10416 {
10417 for (;;)
10418 {
10419 len = readlink((char *)p, (char *)buf, MAXPATHL);
10420 if (len <= 0)
10421 break;
10422 buf[len] = NUL;
10423
10424 if (limit-- == 0)
10425 {
10426 vim_free(p);
10427 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010428 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 rettv->vval.v_string = NULL;
10430 goto fail;
10431 }
10432
10433 /* Ensure that the result will have a trailing path separator
10434 * if the argument has one. */
10435 if (remain == NULL && has_trailing_pathsep)
10436 add_pathsep(buf);
10437
10438 /* Separate the first path component in the link value and
10439 * concatenate the remainders. */
10440 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10441 if (*q != NUL)
10442 {
10443 if (remain == NULL)
10444 remain = vim_strsave(q - 1);
10445 else
10446 {
10447 cpy = concat_str(q - 1, remain);
10448 if (cpy != NULL)
10449 {
10450 vim_free(remain);
10451 remain = cpy;
10452 }
10453 }
10454 q[-1] = NUL;
10455 }
10456
10457 q = gettail(p);
10458 if (q > p && *q == NUL)
10459 {
10460 /* Ignore trailing path separator. */
10461 q[-1] = NUL;
10462 q = gettail(p);
10463 }
10464 if (q > p && !mch_isFullName(buf))
10465 {
10466 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010467 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010468 if (cpy != NULL)
10469 {
10470 STRCPY(cpy, p);
10471 STRCPY(gettail(cpy), buf);
10472 vim_free(p);
10473 p = cpy;
10474 }
10475 }
10476 else
10477 {
10478 vim_free(p);
10479 p = vim_strsave(buf);
10480 }
10481 }
10482
10483 if (remain == NULL)
10484 break;
10485
10486 /* Append the first path component of "remain" to "p". */
10487 q = getnextcomp(remain + 1);
10488 len = q - remain - (*q != NUL);
10489 cpy = vim_strnsave(p, STRLEN(p) + len);
10490 if (cpy != NULL)
10491 {
10492 STRNCAT(cpy, remain, len);
10493 vim_free(p);
10494 p = cpy;
10495 }
10496 /* Shorten "remain". */
10497 if (*q != NUL)
10498 STRMOVE(remain, q - 1);
10499 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010500 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010501 }
10502
10503 /* If the result is a relative path name, make it explicitly relative to
10504 * the current directory if and only if the argument had this form. */
10505 if (!vim_ispathsep(*p))
10506 {
10507 if (is_relative_to_current
10508 && *p != NUL
10509 && !(p[0] == '.'
10510 && (p[1] == NUL
10511 || vim_ispathsep(p[1])
10512 || (p[1] == '.'
10513 && (p[2] == NUL
10514 || vim_ispathsep(p[2]))))))
10515 {
10516 /* Prepend "./". */
10517 cpy = concat_str((char_u *)"./", p);
10518 if (cpy != NULL)
10519 {
10520 vim_free(p);
10521 p = cpy;
10522 }
10523 }
10524 else if (!is_relative_to_current)
10525 {
10526 /* Strip leading "./". */
10527 q = p;
10528 while (q[0] == '.' && vim_ispathsep(q[1]))
10529 q += 2;
10530 if (q > p)
10531 STRMOVE(p, p + 2);
10532 }
10533 }
10534
10535 /* Ensure that the result will have no trailing path separator
10536 * if the argument had none. But keep "/" or "//". */
10537 if (!has_trailing_pathsep)
10538 {
10539 q = p + STRLEN(p);
10540 if (after_pathsep(p, q))
10541 *gettail_sep(p) = NUL;
10542 }
10543
10544 rettv->vval.v_string = p;
10545 }
10546# else
10547 rettv->vval.v_string = vim_strsave(p);
10548# endif
10549#endif
10550
10551 simplify_filename(rettv->vval.v_string);
10552
10553#ifdef HAVE_READLINK
10554fail:
10555 vim_free(buf);
10556#endif
10557 rettv->v_type = VAR_STRING;
10558}
10559
10560/*
10561 * "reverse({list})" function
10562 */
10563 static void
10564f_reverse(typval_T *argvars, typval_T *rettv)
10565{
10566 list_T *l;
10567 listitem_T *li, *ni;
10568
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010569 if (argvars[0].v_type == VAR_BLOB)
10570 {
10571 blob_T *b = argvars[0].vval.v_blob;
10572 int i, len = blob_len(b);
10573
10574 for (i = 0; i < len / 2; i++)
10575 {
10576 int tmp = blob_get(b, i);
10577
10578 blob_set(b, i, blob_get(b, len - i - 1));
10579 blob_set(b, len - i - 1, tmp);
10580 }
10581 rettv_blob_set(rettv, b);
10582 return;
10583 }
10584
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010585 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010586 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010587 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010588 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010589 (char_u *)N_("reverse() argument"), TRUE))
10590 {
10591 li = l->lv_last;
10592 l->lv_first = l->lv_last = NULL;
10593 l->lv_len = 0;
10594 while (li != NULL)
10595 {
10596 ni = li->li_prev;
10597 list_append(l, li);
10598 li = ni;
10599 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010600 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010601 l->lv_idx = l->lv_len - l->lv_idx - 1;
10602 }
10603}
10604
10605#define SP_NOMOVE 0x01 /* don't move cursor */
10606#define SP_REPEAT 0x02 /* repeat to find outer pair */
10607#define SP_RETCOUNT 0x04 /* return matchcount */
10608#define SP_SETPCMARK 0x08 /* set previous context mark */
10609#define SP_START 0x10 /* accept match at start position */
10610#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10611#define SP_END 0x40 /* leave cursor at end of match */
10612#define SP_COLUMN 0x80 /* start at cursor column */
10613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010614/*
10615 * Get flags for a search function.
10616 * Possibly sets "p_ws".
10617 * Returns BACKWARD, FORWARD or zero (for an error).
10618 */
10619 static int
10620get_search_arg(typval_T *varp, int *flagsp)
10621{
10622 int dir = FORWARD;
10623 char_u *flags;
10624 char_u nbuf[NUMBUFLEN];
10625 int mask;
10626
10627 if (varp->v_type != VAR_UNKNOWN)
10628 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010629 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010630 if (flags == NULL)
10631 return 0; /* type error; errmsg already given */
10632 while (*flags != NUL)
10633 {
10634 switch (*flags)
10635 {
10636 case 'b': dir = BACKWARD; break;
10637 case 'w': p_ws = TRUE; break;
10638 case 'W': p_ws = FALSE; break;
10639 default: mask = 0;
10640 if (flagsp != NULL)
10641 switch (*flags)
10642 {
10643 case 'c': mask = SP_START; break;
10644 case 'e': mask = SP_END; break;
10645 case 'm': mask = SP_RETCOUNT; break;
10646 case 'n': mask = SP_NOMOVE; break;
10647 case 'p': mask = SP_SUBPAT; break;
10648 case 'r': mask = SP_REPEAT; break;
10649 case 's': mask = SP_SETPCMARK; break;
10650 case 'z': mask = SP_COLUMN; break;
10651 }
10652 if (mask == 0)
10653 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010654 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010655 dir = 0;
10656 }
10657 else
10658 *flagsp |= mask;
10659 }
10660 if (dir == 0)
10661 break;
10662 ++flags;
10663 }
10664 }
10665 return dir;
10666}
10667
10668/*
10669 * Shared by search() and searchpos() functions.
10670 */
10671 static int
10672search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10673{
10674 int flags;
10675 char_u *pat;
10676 pos_T pos;
10677 pos_T save_cursor;
10678 int save_p_ws = p_ws;
10679 int dir;
10680 int retval = 0; /* default: FAIL */
10681 long lnum_stop = 0;
10682 proftime_T tm;
10683#ifdef FEAT_RELTIME
10684 long time_limit = 0;
10685#endif
10686 int options = SEARCH_KEEP;
10687 int subpatnum;
10688
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010689 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10691 if (dir == 0)
10692 goto theend;
10693 flags = *flagsp;
10694 if (flags & SP_START)
10695 options |= SEARCH_START;
10696 if (flags & SP_END)
10697 options |= SEARCH_END;
10698 if (flags & SP_COLUMN)
10699 options |= SEARCH_COL;
10700
10701 /* Optional arguments: line number to stop searching and timeout. */
10702 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10703 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010704 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010705 if (lnum_stop < 0)
10706 goto theend;
10707#ifdef FEAT_RELTIME
10708 if (argvars[3].v_type != VAR_UNKNOWN)
10709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010710 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010711 if (time_limit < 0)
10712 goto theend;
10713 }
10714#endif
10715 }
10716
10717#ifdef FEAT_RELTIME
10718 /* Set the time limit, if there is one. */
10719 profile_setlimit(time_limit, &tm);
10720#endif
10721
10722 /*
10723 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10724 * Check to make sure only those flags are set.
10725 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10726 * flags cannot be set. Check for that condition also.
10727 */
10728 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10729 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010731 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010732 goto theend;
10733 }
10734
10735 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010736 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010737 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738 if (subpatnum != FAIL)
10739 {
10740 if (flags & SP_SUBPAT)
10741 retval = subpatnum;
10742 else
10743 retval = pos.lnum;
10744 if (flags & SP_SETPCMARK)
10745 setpcmark();
10746 curwin->w_cursor = pos;
10747 if (match_pos != NULL)
10748 {
10749 /* Store the match cursor position */
10750 match_pos->lnum = pos.lnum;
10751 match_pos->col = pos.col + 1;
10752 }
10753 /* "/$" will put the cursor after the end of the line, may need to
10754 * correct that here */
10755 check_cursor();
10756 }
10757
10758 /* If 'n' flag is used: restore cursor position. */
10759 if (flags & SP_NOMOVE)
10760 curwin->w_cursor = save_cursor;
10761 else
10762 curwin->w_set_curswant = TRUE;
10763theend:
10764 p_ws = save_p_ws;
10765
10766 return retval;
10767}
10768
10769#ifdef FEAT_FLOAT
10770
10771/*
10772 * round() is not in C90, use ceil() or floor() instead.
10773 */
10774 float_T
10775vim_round(float_T f)
10776{
10777 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10778}
10779
10780/*
10781 * "round({float})" function
10782 */
10783 static void
10784f_round(typval_T *argvars, typval_T *rettv)
10785{
10786 float_T f = 0.0;
10787
10788 rettv->v_type = VAR_FLOAT;
10789 if (get_float_arg(argvars, &f) == OK)
10790 rettv->vval.v_float = vim_round(f);
10791 else
10792 rettv->vval.v_float = 0.0;
10793}
10794#endif
10795
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010796#ifdef FEAT_RUBY
10797/*
10798 * "rubyeval()" function
10799 */
10800 static void
10801f_rubyeval(typval_T *argvars, typval_T *rettv)
10802{
10803 char_u *str;
10804 char_u buf[NUMBUFLEN];
10805
10806 str = tv_get_string_buf(&argvars[0], buf);
10807 do_rubyeval(str, rettv);
10808}
10809#endif
10810
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010811/*
10812 * "screenattr()" function
10813 */
10814 static void
10815f_screenattr(typval_T *argvars, typval_T *rettv)
10816{
10817 int row;
10818 int col;
10819 int c;
10820
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010821 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10822 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010823 if (row < 0 || row >= screen_Rows
10824 || col < 0 || col >= screen_Columns)
10825 c = -1;
10826 else
10827 c = ScreenAttrs[LineOffset[row] + col];
10828 rettv->vval.v_number = c;
10829}
10830
10831/*
10832 * "screenchar()" function
10833 */
10834 static void
10835f_screenchar(typval_T *argvars, typval_T *rettv)
10836{
10837 int row;
10838 int col;
10839 int off;
10840 int c;
10841
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010842 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10843 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010844 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010845 c = -1;
10846 else
10847 {
10848 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010849 if (enc_utf8 && ScreenLinesUC[off] != 0)
10850 c = ScreenLinesUC[off];
10851 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010852 c = ScreenLines[off];
10853 }
10854 rettv->vval.v_number = c;
10855}
10856
10857/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010858 * "screenchars()" function
10859 */
10860 static void
10861f_screenchars(typval_T *argvars, typval_T *rettv)
10862{
10863 int row;
10864 int col;
10865 int off;
10866 int c;
10867 int i;
10868
10869 if (rettv_list_alloc(rettv) == FAIL)
10870 return;
10871 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10872 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10873 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10874 return;
10875
10876 off = LineOffset[row] + col;
10877 if (enc_utf8 && ScreenLinesUC[off] != 0)
10878 c = ScreenLinesUC[off];
10879 else
10880 c = ScreenLines[off];
10881 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10882
10883 if (enc_utf8)
10884
10885 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10886 list_append_number(rettv->vval.v_list,
10887 (varnumber_T)ScreenLinesC[i][off]);
10888}
10889
10890/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010891 * "screencol()" function
10892 *
10893 * First column is 1 to be consistent with virtcol().
10894 */
10895 static void
10896f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10897{
10898 rettv->vval.v_number = screen_screencol() + 1;
10899}
10900
10901/*
10902 * "screenrow()" function
10903 */
10904 static void
10905f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10906{
10907 rettv->vval.v_number = screen_screenrow() + 1;
10908}
10909
10910/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010911 * "screenstring()" function
10912 */
10913 static void
10914f_screenstring(typval_T *argvars, typval_T *rettv)
10915{
10916 int row;
10917 int col;
10918 int off;
10919 int c;
10920 int i;
10921 char_u buf[MB_MAXBYTES + 1];
10922 int buflen = 0;
10923
10924 rettv->vval.v_string = NULL;
10925 rettv->v_type = VAR_STRING;
10926
10927 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10928 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10929 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10930 return;
10931
10932 off = LineOffset[row] + col;
10933 if (enc_utf8 && ScreenLinesUC[off] != 0)
10934 c = ScreenLinesUC[off];
10935 else
10936 c = ScreenLines[off];
10937 buflen += mb_char2bytes(c, buf);
10938
10939 if (enc_utf8)
10940 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10941 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10942
10943 buf[buflen] = NUL;
10944 rettv->vval.v_string = vim_strsave(buf);
10945}
10946
10947/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 * "search()" function
10949 */
10950 static void
10951f_search(typval_T *argvars, typval_T *rettv)
10952{
10953 int flags = 0;
10954
10955 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10956}
10957
10958/*
10959 * "searchdecl()" function
10960 */
10961 static void
10962f_searchdecl(typval_T *argvars, typval_T *rettv)
10963{
10964 int locally = 1;
10965 int thisblock = 0;
10966 int error = FALSE;
10967 char_u *name;
10968
10969 rettv->vval.v_number = 1; /* default: FAIL */
10970
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010971 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 if (argvars[1].v_type != VAR_UNKNOWN)
10973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010976 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977 }
10978 if (!error && name != NULL)
10979 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10980 locally, thisblock, SEARCH_KEEP) == FAIL;
10981}
10982
10983/*
10984 * Used by searchpair() and searchpairpos()
10985 */
10986 static int
10987searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10988{
10989 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010990 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010991 int save_p_ws = p_ws;
10992 int dir;
10993 int flags = 0;
10994 char_u nbuf1[NUMBUFLEN];
10995 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 int retval = 0; /* default: FAIL */
10997 long lnum_stop = 0;
10998 long time_limit = 0;
10999
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011000 /* Get the three pattern arguments: start, middle, end. Will result in an
11001 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011002 spat = tv_get_string_chk(&argvars[0]);
11003 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
11004 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 if (spat == NULL || mpat == NULL || epat == NULL)
11006 goto theend; /* type error */
11007
11008 /* Handle the optional fourth argument: flags */
11009 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
11010 if (dir == 0)
11011 goto theend;
11012
11013 /* Don't accept SP_END or SP_SUBPAT.
11014 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
11015 */
11016 if ((flags & (SP_END | SP_SUBPAT)) != 0
11017 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
11018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011019 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011020 goto theend;
11021 }
11022
11023 /* Using 'r' implies 'W', otherwise it doesn't work. */
11024 if (flags & SP_REPEAT)
11025 p_ws = FALSE;
11026
11027 /* Optional fifth argument: skip expression */
11028 if (argvars[3].v_type == VAR_UNKNOWN
11029 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010011030 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011031 else
11032 {
Bram Moolenaar48570482017-10-30 21:48:41 +010011033 skip = &argvars[4];
11034 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
11035 && skip->v_type != VAR_STRING)
11036 {
11037 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011038 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011039 goto theend;
11040 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011041 if (argvars[5].v_type != VAR_UNKNOWN)
11042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011043 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011046 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011047 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011048 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011049#ifdef FEAT_RELTIME
11050 if (argvars[6].v_type != VAR_UNKNOWN)
11051 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011052 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011053 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011055 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011056 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011057 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011058 }
11059#endif
11060 }
11061 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011062
11063 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11064 match_pos, lnum_stop, time_limit);
11065
11066theend:
11067 p_ws = save_p_ws;
11068
11069 return retval;
11070}
11071
11072/*
11073 * "searchpair()" function
11074 */
11075 static void
11076f_searchpair(typval_T *argvars, typval_T *rettv)
11077{
11078 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11079}
11080
11081/*
11082 * "searchpairpos()" function
11083 */
11084 static void
11085f_searchpairpos(typval_T *argvars, typval_T *rettv)
11086{
11087 pos_T match_pos;
11088 int lnum = 0;
11089 int col = 0;
11090
11091 if (rettv_list_alloc(rettv) == FAIL)
11092 return;
11093
11094 if (searchpair_cmn(argvars, &match_pos) > 0)
11095 {
11096 lnum = match_pos.lnum;
11097 col = match_pos.col;
11098 }
11099
11100 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11101 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11102}
11103
11104/*
11105 * Search for a start/middle/end thing.
11106 * Used by searchpair(), see its documentation for the details.
11107 * Returns 0 or -1 for no match,
11108 */
11109 long
11110do_searchpair(
11111 char_u *spat, /* start pattern */
11112 char_u *mpat, /* middle pattern */
11113 char_u *epat, /* end pattern */
11114 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011115 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 int flags, /* SP_SETPCMARK and other SP_ values */
11117 pos_T *match_pos,
11118 linenr_T lnum_stop, /* stop at this line if not zero */
11119 long time_limit UNUSED) /* stop after this many msec */
11120{
11121 char_u *save_cpo;
11122 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11123 long retval = 0;
11124 pos_T pos;
11125 pos_T firstpos;
11126 pos_T foundpos;
11127 pos_T save_cursor;
11128 pos_T save_pos;
11129 int n;
11130 int r;
11131 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011132 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133 int err;
11134 int options = SEARCH_KEEP;
11135 proftime_T tm;
11136
11137 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11138 save_cpo = p_cpo;
11139 p_cpo = empty_option;
11140
11141#ifdef FEAT_RELTIME
11142 /* Set the time limit, if there is one. */
11143 profile_setlimit(time_limit, &tm);
11144#endif
11145
11146 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11147 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011148 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11149 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011150 if (pat2 == NULL || pat3 == NULL)
11151 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011152 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 if (*mpat == NUL)
11154 STRCPY(pat3, pat2);
11155 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011156 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011157 spat, epat, mpat);
11158 if (flags & SP_START)
11159 options |= SEARCH_START;
11160
Bram Moolenaar48570482017-10-30 21:48:41 +010011161 if (skip != NULL)
11162 {
11163 /* Empty string means to not use the skip expression. */
11164 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11165 use_skip = skip->vval.v_string != NULL
11166 && *skip->vval.v_string != NUL;
11167 }
11168
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011169 save_cursor = curwin->w_cursor;
11170 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011171 CLEAR_POS(&firstpos);
11172 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011173 pat = pat3;
11174 for (;;)
11175 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011176 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011177 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011178 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011179 /* didn't find it or found the first match again: FAIL */
11180 break;
11181
11182 if (firstpos.lnum == 0)
11183 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011184 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011185 {
11186 /* Found the same position again. Can happen with a pattern that
11187 * has "\zs" at the end and searching backwards. Advance one
11188 * character and try again. */
11189 if (dir == BACKWARD)
11190 decl(&pos);
11191 else
11192 incl(&pos);
11193 }
11194 foundpos = pos;
11195
11196 /* clear the start flag to avoid getting stuck here */
11197 options &= ~SEARCH_START;
11198
11199 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011200 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011201 {
11202 save_pos = curwin->w_cursor;
11203 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011204 err = FALSE;
11205 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011206 curwin->w_cursor = save_pos;
11207 if (err)
11208 {
11209 /* Evaluating {skip} caused an error, break here. */
11210 curwin->w_cursor = save_cursor;
11211 retval = -1;
11212 break;
11213 }
11214 if (r)
11215 continue;
11216 }
11217
11218 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11219 {
11220 /* Found end when searching backwards or start when searching
11221 * forward: nested pair. */
11222 ++nest;
11223 pat = pat2; /* nested, don't search for middle */
11224 }
11225 else
11226 {
11227 /* Found end when searching forward or start when searching
11228 * backward: end of (nested) pair; or found middle in outer pair. */
11229 if (--nest == 1)
11230 pat = pat3; /* outer level, search for middle */
11231 }
11232
11233 if (nest == 0)
11234 {
11235 /* Found the match: return matchcount or line number. */
11236 if (flags & SP_RETCOUNT)
11237 ++retval;
11238 else
11239 retval = pos.lnum;
11240 if (flags & SP_SETPCMARK)
11241 setpcmark();
11242 curwin->w_cursor = pos;
11243 if (!(flags & SP_REPEAT))
11244 break;
11245 nest = 1; /* search for next unmatched */
11246 }
11247 }
11248
11249 if (match_pos != NULL)
11250 {
11251 /* Store the match cursor position */
11252 match_pos->lnum = curwin->w_cursor.lnum;
11253 match_pos->col = curwin->w_cursor.col + 1;
11254 }
11255
11256 /* If 'n' flag is used or search failed: restore cursor position. */
11257 if ((flags & SP_NOMOVE) || retval == 0)
11258 curwin->w_cursor = save_cursor;
11259
11260theend:
11261 vim_free(pat2);
11262 vim_free(pat3);
11263 if (p_cpo == empty_option)
11264 p_cpo = save_cpo;
11265 else
11266 /* Darn, evaluating the {skip} expression changed the value. */
11267 free_string_option(save_cpo);
11268
11269 return retval;
11270}
11271
11272/*
11273 * "searchpos()" function
11274 */
11275 static void
11276f_searchpos(typval_T *argvars, typval_T *rettv)
11277{
11278 pos_T match_pos;
11279 int lnum = 0;
11280 int col = 0;
11281 int n;
11282 int flags = 0;
11283
11284 if (rettv_list_alloc(rettv) == FAIL)
11285 return;
11286
11287 n = search_cmn(argvars, &match_pos, &flags);
11288 if (n > 0)
11289 {
11290 lnum = match_pos.lnum;
11291 col = match_pos.col;
11292 }
11293
11294 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11295 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11296 if (flags & SP_SUBPAT)
11297 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11298}
11299
11300 static void
11301f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11302{
11303#ifdef FEAT_CLIENTSERVER
11304 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011305 char_u *server = tv_get_string_chk(&argvars[0]);
11306 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011307
11308 rettv->vval.v_number = -1;
11309 if (server == NULL || reply == NULL)
11310 return;
11311 if (check_restricted() || check_secure())
11312 return;
11313# ifdef FEAT_X11
11314 if (check_connection() == FAIL)
11315 return;
11316# endif
11317
11318 if (serverSendReply(server, reply) < 0)
11319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011320 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321 return;
11322 }
11323 rettv->vval.v_number = 0;
11324#else
11325 rettv->vval.v_number = -1;
11326#endif
11327}
11328
11329 static void
11330f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11331{
11332 char_u *r = NULL;
11333
11334#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011335# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011336 r = serverGetVimNames();
11337# else
11338 make_connection();
11339 if (X_DISPLAY != NULL)
11340 r = serverGetVimNames(X_DISPLAY);
11341# endif
11342#endif
11343 rettv->v_type = VAR_STRING;
11344 rettv->vval.v_string = r;
11345}
11346
11347/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011348 * "setbufline()" function
11349 */
11350 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011351f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011352{
11353 linenr_T lnum;
11354 buf_T *buf;
11355
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011356 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011357 if (buf == NULL)
11358 rettv->vval.v_number = 1; /* FAIL */
11359 else
11360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011361 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011362 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011363 }
11364}
11365
11366/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 * "setbufvar()" function
11368 */
11369 static void
11370f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11371{
11372 buf_T *buf;
11373 char_u *varname, *bufvarname;
11374 typval_T *varp;
11375 char_u nbuf[NUMBUFLEN];
11376
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011377 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011379 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11380 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011381 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011382 varp = &argvars[2];
11383
11384 if (buf != NULL && varname != NULL && varp != NULL)
11385 {
11386 if (*varname == '&')
11387 {
11388 long numval;
11389 char_u *strval;
11390 int error = FALSE;
11391 aco_save_T aco;
11392
11393 /* set curbuf to be our buf, temporarily */
11394 aucmd_prepbuf(&aco, buf);
11395
11396 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011397 numval = (long)tv_get_number_chk(varp, &error);
11398 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399 if (!error && strval != NULL)
11400 set_option_value(varname, numval, strval, OPT_LOCAL);
11401
11402 /* reset notion of buffer */
11403 aucmd_restbuf(&aco);
11404 }
11405 else
11406 {
11407 buf_T *save_curbuf = curbuf;
11408
Bram Moolenaar964b3742019-05-24 18:54:09 +020011409 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011410 if (bufvarname != NULL)
11411 {
11412 curbuf = buf;
11413 STRCPY(bufvarname, "b:");
11414 STRCPY(bufvarname + 2, varname);
11415 set_var(bufvarname, varp, TRUE);
11416 vim_free(bufvarname);
11417 curbuf = save_curbuf;
11418 }
11419 }
11420 }
11421}
11422
11423 static void
11424f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11425{
11426 dict_T *d;
11427 dictitem_T *di;
11428 char_u *csearch;
11429
11430 if (argvars[0].v_type != VAR_DICT)
11431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011432 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 return;
11434 }
11435
11436 if ((d = argvars[0].vval.v_dict) != NULL)
11437 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011438 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439 if (csearch != NULL)
11440 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011441 if (enc_utf8)
11442 {
11443 int pcc[MAX_MCO];
11444 int c = utfc_ptr2char(csearch, pcc);
11445
11446 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11447 }
11448 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011449 set_last_csearch(PTR2CHAR(csearch),
11450 csearch, MB_PTR2LEN(csearch));
11451 }
11452
11453 di = dict_find(d, (char_u *)"forward", -1);
11454 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011455 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011456 ? FORWARD : BACKWARD);
11457
11458 di = dict_find(d, (char_u *)"until", -1);
11459 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011460 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011461 }
11462}
11463
11464/*
11465 * "setcmdpos()" function
11466 */
11467 static void
11468f_setcmdpos(typval_T *argvars, typval_T *rettv)
11469{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011470 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471
11472 if (pos >= 0)
11473 rettv->vval.v_number = set_cmdline_pos(pos);
11474}
11475
11476/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011477 * "setenv()" function
11478 */
11479 static void
11480f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11481{
11482 char_u namebuf[NUMBUFLEN];
11483 char_u valbuf[NUMBUFLEN];
11484 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11485
11486 if (argvars[1].v_type == VAR_SPECIAL
11487 && argvars[1].vval.v_number == VVAL_NULL)
11488 vim_unsetenv(name);
11489 else
11490 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11491}
11492
11493/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011494 * "setfperm({fname}, {mode})" function
11495 */
11496 static void
11497f_setfperm(typval_T *argvars, typval_T *rettv)
11498{
11499 char_u *fname;
11500 char_u modebuf[NUMBUFLEN];
11501 char_u *mode_str;
11502 int i;
11503 int mask;
11504 int mode = 0;
11505
11506 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011507 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508 if (fname == NULL)
11509 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011510 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011511 if (mode_str == NULL)
11512 return;
11513 if (STRLEN(mode_str) != 9)
11514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011515 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011516 return;
11517 }
11518
11519 mask = 1;
11520 for (i = 8; i >= 0; --i)
11521 {
11522 if (mode_str[i] != '-')
11523 mode |= mask;
11524 mask = mask << 1;
11525 }
11526 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11527}
11528
11529/*
11530 * "setline()" function
11531 */
11532 static void
11533f_setline(typval_T *argvars, typval_T *rettv)
11534{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011535 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011536
Bram Moolenaarca851592018-06-06 21:04:07 +020011537 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538}
11539
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011540/*
11541 * Used by "setqflist()" and "setloclist()" functions
11542 */
11543 static void
11544set_qf_ll_list(
11545 win_T *wp UNUSED,
11546 typval_T *list_arg UNUSED,
11547 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011548 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549 typval_T *rettv)
11550{
11551#ifdef FEAT_QUICKFIX
11552 static char *e_invact = N_("E927: Invalid action: '%s'");
11553 char_u *act;
11554 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011555 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011556#endif
11557
11558 rettv->vval.v_number = -1;
11559
11560#ifdef FEAT_QUICKFIX
11561 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011562 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011563 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011564 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011565 else
11566 {
11567 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011568 dict_T *d = NULL;
11569 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570
11571 if (action_arg->v_type == VAR_STRING)
11572 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011573 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 if (act == NULL)
11575 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011576 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11577 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011578 action = *act;
11579 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011580 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011581 }
11582 else if (action_arg->v_type == VAR_UNKNOWN)
11583 action = ' ';
11584 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011585 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011586
Bram Moolenaard823fa92016-08-12 16:29:27 +020011587 if (action_arg->v_type != VAR_UNKNOWN
11588 && what_arg->v_type != VAR_UNKNOWN)
11589 {
11590 if (what_arg->v_type == VAR_DICT)
11591 d = what_arg->vval.v_dict;
11592 else
11593 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011594 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011595 valid_dict = FALSE;
11596 }
11597 }
11598
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011599 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011600 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011601 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11602 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011603 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011604 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 }
11606#endif
11607}
11608
11609/*
11610 * "setloclist()" function
11611 */
11612 static void
11613f_setloclist(typval_T *argvars, typval_T *rettv)
11614{
11615 win_T *win;
11616
11617 rettv->vval.v_number = -1;
11618
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011619 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011620 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011621 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011622}
11623
11624/*
11625 * "setmatches()" function
11626 */
11627 static void
11628f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11629{
11630#ifdef FEAT_SEARCH_EXTRA
11631 list_T *l;
11632 listitem_T *li;
11633 dict_T *d;
11634 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011635 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011636
11637 rettv->vval.v_number = -1;
11638 if (argvars[0].v_type != VAR_LIST)
11639 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011640 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 return;
11642 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011643 if (win == NULL)
11644 return;
11645
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011646 if ((l = argvars[0].vval.v_list) != NULL)
11647 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011648 /* To some extent make sure that we are dealing with a list from
11649 * "getmatches()". */
11650 li = l->lv_first;
11651 while (li != NULL)
11652 {
11653 if (li->li_tv.v_type != VAR_DICT
11654 || (d = li->li_tv.vval.v_dict) == NULL)
11655 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011656 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011657 return;
11658 }
11659 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11660 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11661 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11662 && dict_find(d, (char_u *)"priority", -1) != NULL
11663 && dict_find(d, (char_u *)"id", -1) != NULL))
11664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011665 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011666 return;
11667 }
11668 li = li->li_next;
11669 }
11670
Bram Moolenaaraff74912019-03-30 18:11:49 +010011671 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672 li = l->lv_first;
11673 while (li != NULL)
11674 {
11675 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011676 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011677 dictitem_T *di;
11678 char_u *group;
11679 int priority;
11680 int id;
11681 char_u *conceal;
11682
11683 d = li->li_tv.vval.v_dict;
11684 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11685 {
11686 if (s == NULL)
11687 {
11688 s = list_alloc();
11689 if (s == NULL)
11690 return;
11691 }
11692
11693 /* match from matchaddpos() */
11694 for (i = 1; i < 9; i++)
11695 {
11696 sprintf((char *)buf, (char *)"pos%d", i);
11697 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11698 {
11699 if (di->di_tv.v_type != VAR_LIST)
11700 return;
11701
11702 list_append_tv(s, &di->di_tv);
11703 s->lv_refcount++;
11704 }
11705 else
11706 break;
11707 }
11708 }
11709
Bram Moolenaar8f667172018-12-14 15:38:31 +010011710 group = dict_get_string(d, (char_u *)"group", TRUE);
11711 priority = (int)dict_get_number(d, (char_u *)"priority");
11712 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011713 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011714 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715 : NULL;
11716 if (i == 0)
11717 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011718 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011719 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720 priority, id, NULL, conceal);
11721 }
11722 else
11723 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011724 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 list_unref(s);
11726 s = NULL;
11727 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011728 vim_free(group);
11729 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011730
11731 li = li->li_next;
11732 }
11733 rettv->vval.v_number = 0;
11734 }
11735#endif
11736}
11737
11738/*
11739 * "setpos()" function
11740 */
11741 static void
11742f_setpos(typval_T *argvars, typval_T *rettv)
11743{
11744 pos_T pos;
11745 int fnum;
11746 char_u *name;
11747 colnr_T curswant = -1;
11748
11749 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011750 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751 if (name != NULL)
11752 {
11753 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11754 {
11755 if (--pos.col < 0)
11756 pos.col = 0;
11757 if (name[0] == '.' && name[1] == NUL)
11758 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011759 /* set cursor; "fnum" is ignored */
11760 curwin->w_cursor = pos;
11761 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011762 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011763 curwin->w_curswant = curswant - 1;
11764 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011765 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011766 check_cursor();
11767 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011768 }
11769 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11770 {
11771 /* set mark */
11772 if (setmark_pos(name[1], &pos, fnum) == OK)
11773 rettv->vval.v_number = 0;
11774 }
11775 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011776 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011777 }
11778 }
11779}
11780
11781/*
11782 * "setqflist()" function
11783 */
11784 static void
11785f_setqflist(typval_T *argvars, typval_T *rettv)
11786{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011787 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011788}
11789
11790/*
11791 * "setreg()" function
11792 */
11793 static void
11794f_setreg(typval_T *argvars, typval_T *rettv)
11795{
11796 int regname;
11797 char_u *strregname;
11798 char_u *stropt;
11799 char_u *strval;
11800 int append;
11801 char_u yank_type;
11802 long block_len;
11803
11804 block_len = -1;
11805 yank_type = MAUTO;
11806 append = FALSE;
11807
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011808 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011809 rettv->vval.v_number = 1; /* FAIL is default */
11810
11811 if (strregname == NULL)
11812 return; /* type error; errmsg already given */
11813 regname = *strregname;
11814 if (regname == 0 || regname == '@')
11815 regname = '"';
11816
11817 if (argvars[2].v_type != VAR_UNKNOWN)
11818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011819 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011820 if (stropt == NULL)
11821 return; /* type error */
11822 for (; *stropt != NUL; ++stropt)
11823 switch (*stropt)
11824 {
11825 case 'a': case 'A': /* append */
11826 append = TRUE;
11827 break;
11828 case 'v': case 'c': /* character-wise selection */
11829 yank_type = MCHAR;
11830 break;
11831 case 'V': case 'l': /* line-wise selection */
11832 yank_type = MLINE;
11833 break;
11834 case 'b': case Ctrl_V: /* block-wise selection */
11835 yank_type = MBLOCK;
11836 if (VIM_ISDIGIT(stropt[1]))
11837 {
11838 ++stropt;
11839 block_len = getdigits(&stropt) - 1;
11840 --stropt;
11841 }
11842 break;
11843 }
11844 }
11845
11846 if (argvars[1].v_type == VAR_LIST)
11847 {
11848 char_u **lstval;
11849 char_u **allocval;
11850 char_u buf[NUMBUFLEN];
11851 char_u **curval;
11852 char_u **curallocval;
11853 list_T *ll = argvars[1].vval.v_list;
11854 listitem_T *li;
11855 int len;
11856
11857 /* If the list is NULL handle like an empty list. */
11858 len = ll == NULL ? 0 : ll->lv_len;
11859
11860 /* First half: use for pointers to result lines; second half: use for
11861 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011862 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011863 if (lstval == NULL)
11864 return;
11865 curval = lstval;
11866 allocval = lstval + len + 2;
11867 curallocval = allocval;
11868
11869 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11870 li = li->li_next)
11871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011872 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011873 if (strval == NULL)
11874 goto free_lstval;
11875 if (strval == buf)
11876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011877 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011878 * overwrite the string. */
11879 strval = vim_strsave(buf);
11880 if (strval == NULL)
11881 goto free_lstval;
11882 *curallocval++ = strval;
11883 }
11884 *curval++ = strval;
11885 }
11886 *curval++ = NULL;
11887
11888 write_reg_contents_lst(regname, lstval, -1,
11889 append, yank_type, block_len);
11890free_lstval:
11891 while (curallocval > allocval)
11892 vim_free(*--curallocval);
11893 vim_free(lstval);
11894 }
11895 else
11896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011897 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011898 if (strval == NULL)
11899 return;
11900 write_reg_contents_ex(regname, strval, -1,
11901 append, yank_type, block_len);
11902 }
11903 rettv->vval.v_number = 0;
11904}
11905
11906/*
11907 * "settabvar()" function
11908 */
11909 static void
11910f_settabvar(typval_T *argvars, typval_T *rettv)
11911{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011912 tabpage_T *save_curtab;
11913 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011914 char_u *varname, *tabvarname;
11915 typval_T *varp;
11916
11917 rettv->vval.v_number = 0;
11918
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011919 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011920 return;
11921
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011922 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11923 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011924 varp = &argvars[2];
11925
Bram Moolenaar4033c552017-09-16 20:54:51 +020011926 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011927 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011928 save_curtab = curtab;
11929 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011930
Bram Moolenaar964b3742019-05-24 18:54:09 +020011931 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011932 if (tabvarname != NULL)
11933 {
11934 STRCPY(tabvarname, "t:");
11935 STRCPY(tabvarname + 2, varname);
11936 set_var(tabvarname, varp, TRUE);
11937 vim_free(tabvarname);
11938 }
11939
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011940 /* Restore current tabpage */
11941 if (valid_tabpage(save_curtab))
11942 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011943 }
11944}
11945
11946/*
11947 * "settabwinvar()" function
11948 */
11949 static void
11950f_settabwinvar(typval_T *argvars, typval_T *rettv)
11951{
11952 setwinvar(argvars, rettv, 1);
11953}
11954
11955/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011956 * "settagstack()" function
11957 */
11958 static void
11959f_settagstack(typval_T *argvars, typval_T *rettv)
11960{
11961 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11962 win_T *wp;
11963 dict_T *d;
11964 int action = 'r';
11965
11966 rettv->vval.v_number = -1;
11967
11968 // first argument: window number or id
11969 wp = find_win_by_nr_or_id(&argvars[0]);
11970 if (wp == NULL)
11971 return;
11972
11973 // second argument: dict with items to set in the tag stack
11974 if (argvars[1].v_type != VAR_DICT)
11975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011976 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011977 return;
11978 }
11979 d = argvars[1].vval.v_dict;
11980 if (d == NULL)
11981 return;
11982
11983 // third argument: action - 'a' for append and 'r' for replace.
11984 // default is to replace the stack.
11985 if (argvars[2].v_type == VAR_UNKNOWN)
11986 action = 'r';
11987 else if (argvars[2].v_type == VAR_STRING)
11988 {
11989 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011990 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011991 if (actstr == NULL)
11992 return;
11993 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11994 action = *actstr;
11995 else
11996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011997 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011998 return;
11999 }
12000 }
12001 else
12002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012003 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010012004 return;
12005 }
12006
12007 if (set_tagstack(wp, d, action) == OK)
12008 rettv->vval.v_number = 0;
12009}
12010
12011/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012012 * "setwinvar()" function
12013 */
12014 static void
12015f_setwinvar(typval_T *argvars, typval_T *rettv)
12016{
12017 setwinvar(argvars, rettv, 0);
12018}
12019
12020#ifdef FEAT_CRYPT
12021/*
12022 * "sha256({string})" function
12023 */
12024 static void
12025f_sha256(typval_T *argvars, typval_T *rettv)
12026{
12027 char_u *p;
12028
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012029 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012030 rettv->vval.v_string = vim_strsave(
12031 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
12032 rettv->v_type = VAR_STRING;
12033}
12034#endif /* FEAT_CRYPT */
12035
12036/*
12037 * "shellescape({string})" function
12038 */
12039 static void
12040f_shellescape(typval_T *argvars, typval_T *rettv)
12041{
Bram Moolenaar20615522017-06-05 18:46:26 +020012042 int do_special = non_zero_arg(&argvars[1]);
12043
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012044 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012045 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012046 rettv->v_type = VAR_STRING;
12047}
12048
12049/*
12050 * shiftwidth() function
12051 */
12052 static void
12053f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12054{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012055 rettv->vval.v_number = 0;
12056
12057 if (argvars[0].v_type != VAR_UNKNOWN)
12058 {
12059 long col;
12060
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012061 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012062 if (col < 0)
12063 return; // type error; errmsg already given
12064#ifdef FEAT_VARTABS
12065 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12066 return;
12067#endif
12068 }
12069
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012070 rettv->vval.v_number = get_sw_value(curbuf);
12071}
12072
Bram Moolenaar162b7142018-12-21 15:17:36 +010012073#ifdef FEAT_SIGNS
12074/*
12075 * "sign_define()" function
12076 */
12077 static void
12078f_sign_define(typval_T *argvars, typval_T *rettv)
12079{
12080 char_u *name;
12081 dict_T *dict;
12082 char_u *icon = NULL;
12083 char_u *linehl = NULL;
12084 char_u *text = NULL;
12085 char_u *texthl = NULL;
12086
12087 rettv->vval.v_number = -1;
12088
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012089 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090 if (name == NULL)
12091 return;
12092
12093 if (argvars[1].v_type != VAR_UNKNOWN)
12094 {
12095 if (argvars[1].v_type != VAR_DICT)
12096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012097 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012098 return;
12099 }
12100
12101 // sign attributes
12102 dict = argvars[1].vval.v_dict;
12103 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12104 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12105 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12106 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12107 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12108 text = dict_get_string(dict, (char_u *)"text", TRUE);
12109 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12110 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12111 }
12112
12113 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12114 rettv->vval.v_number = 0;
12115
12116 vim_free(icon);
12117 vim_free(linehl);
12118 vim_free(text);
12119 vim_free(texthl);
12120}
12121
12122/*
12123 * "sign_getdefined()" function
12124 */
12125 static void
12126f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12127{
12128 char_u *name = NULL;
12129
12130 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12131 return;
12132
12133 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012134 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012135
12136 sign_getlist(name, rettv->vval.v_list);
12137}
12138
12139/*
12140 * "sign_getplaced()" function
12141 */
12142 static void
12143f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12144{
12145 buf_T *buf = NULL;
12146 dict_T *dict;
12147 dictitem_T *di;
12148 linenr_T lnum = 0;
12149 int sign_id = 0;
12150 char_u *group = NULL;
12151 int notanum = FALSE;
12152
12153 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12154 return;
12155
12156 if (argvars[0].v_type != VAR_UNKNOWN)
12157 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012158 // get signs placed in the specified buffer
12159 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012160 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012161 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012162
12163 if (argvars[1].v_type != VAR_UNKNOWN)
12164 {
12165 if (argvars[1].v_type != VAR_DICT ||
12166 ((dict = argvars[1].vval.v_dict) == NULL))
12167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012168 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012169 return;
12170 }
12171 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12172 {
12173 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012174 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012175 if (notanum)
12176 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012177 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012178 }
12179 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12180 {
12181 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012182 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012183 if (notanum)
12184 return;
12185 }
12186 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12187 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012188 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012189 if (group == NULL)
12190 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012191 if (*group == '\0') // empty string means global group
12192 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012193 }
12194 }
12195 }
12196
12197 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12198}
12199
12200/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012201 * "sign_jump()" function
12202 */
12203 static void
12204f_sign_jump(typval_T *argvars, typval_T *rettv)
12205{
12206 int sign_id;
12207 char_u *sign_group = NULL;
12208 buf_T *buf;
12209 int notanum = FALSE;
12210
12211 rettv->vval.v_number = -1;
12212
Bram Moolenaarbdace832019-03-02 10:13:42 +010012213 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012214 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12215 if (notanum)
12216 return;
12217 if (sign_id <= 0)
12218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012219 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012220 return;
12221 }
12222
12223 // Sign group
12224 sign_group = tv_get_string_chk(&argvars[1]);
12225 if (sign_group == NULL)
12226 return;
12227 if (sign_group[0] == '\0')
12228 sign_group = NULL; // global sign group
12229 else
12230 {
12231 sign_group = vim_strsave(sign_group);
12232 if (sign_group == NULL)
12233 return;
12234 }
12235
12236 // Buffer to place the sign
12237 buf = get_buf_arg(&argvars[2]);
12238 if (buf == NULL)
12239 goto cleanup;
12240
12241 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12242
12243cleanup:
12244 vim_free(sign_group);
12245}
12246
12247/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012248 * "sign_place()" function
12249 */
12250 static void
12251f_sign_place(typval_T *argvars, typval_T *rettv)
12252{
12253 int sign_id;
12254 char_u *group = NULL;
12255 char_u *sign_name;
12256 buf_T *buf;
12257 dict_T *dict;
12258 dictitem_T *di;
12259 linenr_T lnum = 0;
12260 int prio = SIGN_DEF_PRIO;
12261 int notanum = FALSE;
12262
12263 rettv->vval.v_number = -1;
12264
Bram Moolenaarbdace832019-03-02 10:13:42 +010012265 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012266 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012267 if (notanum)
12268 return;
12269 if (sign_id < 0)
12270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012271 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012272 return;
12273 }
12274
12275 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012276 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012277 if (group == NULL)
12278 return;
12279 if (group[0] == '\0')
12280 group = NULL; // global sign group
12281 else
12282 {
12283 group = vim_strsave(group);
12284 if (group == NULL)
12285 return;
12286 }
12287
12288 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012289 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012290 if (sign_name == NULL)
12291 goto cleanup;
12292
12293 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012294 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012295 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012296 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012297
12298 if (argvars[4].v_type != VAR_UNKNOWN)
12299 {
12300 if (argvars[4].v_type != VAR_DICT ||
12301 ((dict = argvars[4].vval.v_dict) == NULL))
12302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012303 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012304 goto cleanup;
12305 }
12306
12307 // Line number where the sign is to be placed
12308 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012310 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012311 if (notanum)
12312 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012313 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012314 }
12315 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12316 {
12317 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012318 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012319 if (notanum)
12320 goto cleanup;
12321 }
12322 }
12323
12324 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12325 rettv->vval.v_number = sign_id;
12326
12327cleanup:
12328 vim_free(group);
12329}
12330
12331/*
12332 * "sign_undefine()" function
12333 */
12334 static void
12335f_sign_undefine(typval_T *argvars, typval_T *rettv)
12336{
12337 char_u *name;
12338
12339 rettv->vval.v_number = -1;
12340
12341 if (argvars[0].v_type == VAR_UNKNOWN)
12342 {
12343 // Free all the signs
12344 free_signs();
12345 rettv->vval.v_number = 0;
12346 }
12347 else
12348 {
12349 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012350 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012351 if (name == NULL)
12352 return;
12353
12354 if (sign_undefine_by_name(name) == OK)
12355 rettv->vval.v_number = 0;
12356 }
12357}
12358
12359/*
12360 * "sign_unplace()" function
12361 */
12362 static void
12363f_sign_unplace(typval_T *argvars, typval_T *rettv)
12364{
12365 dict_T *dict;
12366 dictitem_T *di;
12367 int sign_id = 0;
12368 buf_T *buf = NULL;
12369 char_u *group = NULL;
12370
12371 rettv->vval.v_number = -1;
12372
12373 if (argvars[0].v_type != VAR_STRING)
12374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012375 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012376 return;
12377 }
12378
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012379 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012380 if (group[0] == '\0')
12381 group = NULL; // global sign group
12382 else
12383 {
12384 group = vim_strsave(group);
12385 if (group == NULL)
12386 return;
12387 }
12388
12389 if (argvars[1].v_type != VAR_UNKNOWN)
12390 {
12391 if (argvars[1].v_type != VAR_DICT)
12392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012393 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012394 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012395 }
12396 dict = argvars[1].vval.v_dict;
12397
12398 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12399 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012400 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012401 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012402 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012403 }
12404 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12405 sign_id = dict_get_number(dict, (char_u *)"id");
12406 }
12407
12408 if (buf == NULL)
12409 {
12410 // Delete the sign in all the buffers
12411 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012412 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012413 rettv->vval.v_number = 0;
12414 }
12415 else
12416 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012417 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012418 rettv->vval.v_number = 0;
12419 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012420
12421cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012422 vim_free(group);
12423}
12424#endif
12425
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426/*
12427 * "simplify()" function
12428 */
12429 static void
12430f_simplify(typval_T *argvars, typval_T *rettv)
12431{
12432 char_u *p;
12433
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012434 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012435 rettv->vval.v_string = vim_strsave(p);
12436 simplify_filename(rettv->vval.v_string); /* simplify in place */
12437 rettv->v_type = VAR_STRING;
12438}
12439
12440#ifdef FEAT_FLOAT
12441/*
12442 * "sin()" function
12443 */
12444 static void
12445f_sin(typval_T *argvars, typval_T *rettv)
12446{
12447 float_T f = 0.0;
12448
12449 rettv->v_type = VAR_FLOAT;
12450 if (get_float_arg(argvars, &f) == OK)
12451 rettv->vval.v_float = sin(f);
12452 else
12453 rettv->vval.v_float = 0.0;
12454}
12455
12456/*
12457 * "sinh()" function
12458 */
12459 static void
12460f_sinh(typval_T *argvars, typval_T *rettv)
12461{
12462 float_T f = 0.0;
12463
12464 rettv->v_type = VAR_FLOAT;
12465 if (get_float_arg(argvars, &f) == OK)
12466 rettv->vval.v_float = sinh(f);
12467 else
12468 rettv->vval.v_float = 0.0;
12469}
12470#endif
12471
Bram Moolenaareae1b912019-05-09 15:12:55 +020012472static int item_compare(const void *s1, const void *s2);
12473static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012474
12475/* struct used in the array that's given to qsort() */
12476typedef struct
12477{
12478 listitem_T *item;
12479 int idx;
12480} sortItem_T;
12481
12482/* struct storing information about current sort */
12483typedef struct
12484{
12485 int item_compare_ic;
12486 int item_compare_numeric;
12487 int item_compare_numbers;
12488#ifdef FEAT_FLOAT
12489 int item_compare_float;
12490#endif
12491 char_u *item_compare_func;
12492 partial_T *item_compare_partial;
12493 dict_T *item_compare_selfdict;
12494 int item_compare_func_err;
12495 int item_compare_keep_zero;
12496} sortinfo_T;
12497static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012498#define ITEM_COMPARE_FAIL 999
12499
12500/*
12501 * Compare functions for f_sort() and f_uniq() below.
12502 */
12503 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012504item_compare(const void *s1, const void *s2)
12505{
12506 sortItem_T *si1, *si2;
12507 typval_T *tv1, *tv2;
12508 char_u *p1, *p2;
12509 char_u *tofree1 = NULL, *tofree2 = NULL;
12510 int res;
12511 char_u numbuf1[NUMBUFLEN];
12512 char_u numbuf2[NUMBUFLEN];
12513
12514 si1 = (sortItem_T *)s1;
12515 si2 = (sortItem_T *)s2;
12516 tv1 = &si1->item->li_tv;
12517 tv2 = &si2->item->li_tv;
12518
12519 if (sortinfo->item_compare_numbers)
12520 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012521 varnumber_T v1 = tv_get_number(tv1);
12522 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012523
12524 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12525 }
12526
12527#ifdef FEAT_FLOAT
12528 if (sortinfo->item_compare_float)
12529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012530 float_T v1 = tv_get_float(tv1);
12531 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012532
12533 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12534 }
12535#endif
12536
12537 /* tv2string() puts quotes around a string and allocates memory. Don't do
12538 * that for string variables. Use a single quote when comparing with a
12539 * non-string to do what the docs promise. */
12540 if (tv1->v_type == VAR_STRING)
12541 {
12542 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12543 p1 = (char_u *)"'";
12544 else
12545 p1 = tv1->vval.v_string;
12546 }
12547 else
12548 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12549 if (tv2->v_type == VAR_STRING)
12550 {
12551 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12552 p2 = (char_u *)"'";
12553 else
12554 p2 = tv2->vval.v_string;
12555 }
12556 else
12557 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12558 if (p1 == NULL)
12559 p1 = (char_u *)"";
12560 if (p2 == NULL)
12561 p2 = (char_u *)"";
12562 if (!sortinfo->item_compare_numeric)
12563 {
12564 if (sortinfo->item_compare_ic)
12565 res = STRICMP(p1, p2);
12566 else
12567 res = STRCMP(p1, p2);
12568 }
12569 else
12570 {
12571 double n1, n2;
12572 n1 = strtod((char *)p1, (char **)&p1);
12573 n2 = strtod((char *)p2, (char **)&p2);
12574 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12575 }
12576
12577 /* When the result would be zero, compare the item indexes. Makes the
12578 * sort stable. */
12579 if (res == 0 && !sortinfo->item_compare_keep_zero)
12580 res = si1->idx > si2->idx ? 1 : -1;
12581
12582 vim_free(tofree1);
12583 vim_free(tofree2);
12584 return res;
12585}
12586
12587 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588item_compare2(const void *s1, const void *s2)
12589{
12590 sortItem_T *si1, *si2;
12591 int res;
12592 typval_T rettv;
12593 typval_T argv[3];
12594 int dummy;
12595 char_u *func_name;
12596 partial_T *partial = sortinfo->item_compare_partial;
12597
12598 /* shortcut after failure in previous call; compare all items equal */
12599 if (sortinfo->item_compare_func_err)
12600 return 0;
12601
12602 si1 = (sortItem_T *)s1;
12603 si2 = (sortItem_T *)s2;
12604
12605 if (partial == NULL)
12606 func_name = sortinfo->item_compare_func;
12607 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012608 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012609
12610 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12611 * in the copy without changing the original list items. */
12612 copy_tv(&si1->item->li_tv, &argv[0]);
12613 copy_tv(&si2->item->li_tv, &argv[1]);
12614
12615 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012616 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012617 partial, sortinfo->item_compare_selfdict);
12618 clear_tv(&argv[0]);
12619 clear_tv(&argv[1]);
12620
12621 if (res == FAIL)
12622 res = ITEM_COMPARE_FAIL;
12623 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012624 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 if (sortinfo->item_compare_func_err)
12626 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12627 clear_tv(&rettv);
12628
12629 /* When the result would be zero, compare the pointers themselves. Makes
12630 * the sort stable. */
12631 if (res == 0 && !sortinfo->item_compare_keep_zero)
12632 res = si1->idx > si2->idx ? 1 : -1;
12633
12634 return res;
12635}
12636
12637/*
12638 * "sort({list})" function
12639 */
12640 static void
12641do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12642{
12643 list_T *l;
12644 listitem_T *li;
12645 sortItem_T *ptrs;
12646 sortinfo_T *old_sortinfo;
12647 sortinfo_T info;
12648 long len;
12649 long i;
12650
12651 /* Pointer to current info struct used in compare function. Save and
12652 * restore the current one for nested calls. */
12653 old_sortinfo = sortinfo;
12654 sortinfo = &info;
12655
12656 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012657 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012658 else
12659 {
12660 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012661 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012662 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12663 TRUE))
12664 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012665 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012666
12667 len = list_len(l);
12668 if (len <= 1)
12669 goto theend; /* short list sorts pretty quickly */
12670
12671 info.item_compare_ic = FALSE;
12672 info.item_compare_numeric = FALSE;
12673 info.item_compare_numbers = FALSE;
12674#ifdef FEAT_FLOAT
12675 info.item_compare_float = FALSE;
12676#endif
12677 info.item_compare_func = NULL;
12678 info.item_compare_partial = NULL;
12679 info.item_compare_selfdict = NULL;
12680 if (argvars[1].v_type != VAR_UNKNOWN)
12681 {
12682 /* optional second argument: {func} */
12683 if (argvars[1].v_type == VAR_FUNC)
12684 info.item_compare_func = argvars[1].vval.v_string;
12685 else if (argvars[1].v_type == VAR_PARTIAL)
12686 info.item_compare_partial = argvars[1].vval.v_partial;
12687 else
12688 {
12689 int error = FALSE;
12690
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012691 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012692 if (error)
12693 goto theend; /* type error; errmsg already given */
12694 if (i == 1)
12695 info.item_compare_ic = TRUE;
12696 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012697 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012698 else if (i != 0)
12699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012700 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 goto theend;
12702 }
12703 if (info.item_compare_func != NULL)
12704 {
12705 if (*info.item_compare_func == NUL)
12706 {
12707 /* empty string means default sort */
12708 info.item_compare_func = NULL;
12709 }
12710 else if (STRCMP(info.item_compare_func, "n") == 0)
12711 {
12712 info.item_compare_func = NULL;
12713 info.item_compare_numeric = TRUE;
12714 }
12715 else if (STRCMP(info.item_compare_func, "N") == 0)
12716 {
12717 info.item_compare_func = NULL;
12718 info.item_compare_numbers = TRUE;
12719 }
12720#ifdef FEAT_FLOAT
12721 else if (STRCMP(info.item_compare_func, "f") == 0)
12722 {
12723 info.item_compare_func = NULL;
12724 info.item_compare_float = TRUE;
12725 }
12726#endif
12727 else if (STRCMP(info.item_compare_func, "i") == 0)
12728 {
12729 info.item_compare_func = NULL;
12730 info.item_compare_ic = TRUE;
12731 }
12732 }
12733 }
12734
12735 if (argvars[2].v_type != VAR_UNKNOWN)
12736 {
12737 /* optional third argument: {dict} */
12738 if (argvars[2].v_type != VAR_DICT)
12739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012740 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012741 goto theend;
12742 }
12743 info.item_compare_selfdict = argvars[2].vval.v_dict;
12744 }
12745 }
12746
12747 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012748 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012749 if (ptrs == NULL)
12750 goto theend;
12751
12752 i = 0;
12753 if (sort)
12754 {
12755 /* sort(): ptrs will be the list to sort */
12756 for (li = l->lv_first; li != NULL; li = li->li_next)
12757 {
12758 ptrs[i].item = li;
12759 ptrs[i].idx = i;
12760 ++i;
12761 }
12762
12763 info.item_compare_func_err = FALSE;
12764 info.item_compare_keep_zero = FALSE;
12765 /* test the compare function */
12766 if ((info.item_compare_func != NULL
12767 || info.item_compare_partial != NULL)
12768 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12769 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012770 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012771 else
12772 {
12773 /* Sort the array with item pointers. */
12774 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12775 info.item_compare_func == NULL
12776 && info.item_compare_partial == NULL
12777 ? item_compare : item_compare2);
12778
12779 if (!info.item_compare_func_err)
12780 {
12781 /* Clear the List and append the items in sorted order. */
12782 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12783 l->lv_len = 0;
12784 for (i = 0; i < len; ++i)
12785 list_append(l, ptrs[i].item);
12786 }
12787 }
12788 }
12789 else
12790 {
12791 int (*item_compare_func_ptr)(const void *, const void *);
12792
12793 /* f_uniq(): ptrs will be a stack of items to remove */
12794 info.item_compare_func_err = FALSE;
12795 info.item_compare_keep_zero = TRUE;
12796 item_compare_func_ptr = info.item_compare_func != NULL
12797 || info.item_compare_partial != NULL
12798 ? item_compare2 : item_compare;
12799
12800 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12801 li = li->li_next)
12802 {
12803 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12804 == 0)
12805 ptrs[i++].item = li;
12806 if (info.item_compare_func_err)
12807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012808 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012809 break;
12810 }
12811 }
12812
12813 if (!info.item_compare_func_err)
12814 {
12815 while (--i >= 0)
12816 {
12817 li = ptrs[i].item->li_next;
12818 ptrs[i].item->li_next = li->li_next;
12819 if (li->li_next != NULL)
12820 li->li_next->li_prev = ptrs[i].item;
12821 else
12822 l->lv_last = ptrs[i].item;
12823 list_fix_watch(l, li);
12824 listitem_free(li);
12825 l->lv_len--;
12826 }
12827 }
12828 }
12829
12830 vim_free(ptrs);
12831 }
12832theend:
12833 sortinfo = old_sortinfo;
12834}
12835
12836/*
12837 * "sort({list})" function
12838 */
12839 static void
12840f_sort(typval_T *argvars, typval_T *rettv)
12841{
12842 do_sort_uniq(argvars, rettv, TRUE);
12843}
12844
12845/*
12846 * "uniq({list})" function
12847 */
12848 static void
12849f_uniq(typval_T *argvars, typval_T *rettv)
12850{
12851 do_sort_uniq(argvars, rettv, FALSE);
12852}
12853
12854/*
12855 * "soundfold({word})" function
12856 */
12857 static void
12858f_soundfold(typval_T *argvars, typval_T *rettv)
12859{
12860 char_u *s;
12861
12862 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012863 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012864#ifdef FEAT_SPELL
12865 rettv->vval.v_string = eval_soundfold(s);
12866#else
12867 rettv->vval.v_string = vim_strsave(s);
12868#endif
12869}
12870
12871/*
12872 * "spellbadword()" function
12873 */
12874 static void
12875f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12876{
12877 char_u *word = (char_u *)"";
12878 hlf_T attr = HLF_COUNT;
12879 int len = 0;
12880
12881 if (rettv_list_alloc(rettv) == FAIL)
12882 return;
12883
12884#ifdef FEAT_SPELL
12885 if (argvars[0].v_type == VAR_UNKNOWN)
12886 {
12887 /* Find the start and length of the badly spelled word. */
12888 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12889 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012890 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012891 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012892 curwin->w_set_curswant = TRUE;
12893 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012894 }
12895 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012897 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012898 int capcol = -1;
12899
12900 if (str != NULL)
12901 {
12902 /* Check the argument for spelling. */
12903 while (*str != NUL)
12904 {
12905 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12906 if (attr != HLF_COUNT)
12907 {
12908 word = str;
12909 break;
12910 }
12911 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012912 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 }
12914 }
12915 }
12916#endif
12917
12918 list_append_string(rettv->vval.v_list, word, len);
12919 list_append_string(rettv->vval.v_list, (char_u *)(
12920 attr == HLF_SPB ? "bad" :
12921 attr == HLF_SPR ? "rare" :
12922 attr == HLF_SPL ? "local" :
12923 attr == HLF_SPC ? "caps" :
12924 ""), -1);
12925}
12926
12927/*
12928 * "spellsuggest()" function
12929 */
12930 static void
12931f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12932{
12933#ifdef FEAT_SPELL
12934 char_u *str;
12935 int typeerr = FALSE;
12936 int maxcount;
12937 garray_T ga;
12938 int i;
12939 listitem_T *li;
12940 int need_capital = FALSE;
12941#endif
12942
12943 if (rettv_list_alloc(rettv) == FAIL)
12944 return;
12945
12946#ifdef FEAT_SPELL
12947 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012949 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012950 if (argvars[1].v_type != VAR_UNKNOWN)
12951 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012952 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012953 if (maxcount <= 0)
12954 return;
12955 if (argvars[2].v_type != VAR_UNKNOWN)
12956 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012957 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 if (typeerr)
12959 return;
12960 }
12961 }
12962 else
12963 maxcount = 25;
12964
12965 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12966
12967 for (i = 0; i < ga.ga_len; ++i)
12968 {
12969 str = ((char_u **)ga.ga_data)[i];
12970
12971 li = listitem_alloc();
12972 if (li == NULL)
12973 vim_free(str);
12974 else
12975 {
12976 li->li_tv.v_type = VAR_STRING;
12977 li->li_tv.v_lock = 0;
12978 li->li_tv.vval.v_string = str;
12979 list_append(rettv->vval.v_list, li);
12980 }
12981 }
12982 ga_clear(&ga);
12983 }
12984#endif
12985}
12986
12987 static void
12988f_split(typval_T *argvars, typval_T *rettv)
12989{
12990 char_u *str;
12991 char_u *end;
12992 char_u *pat = NULL;
12993 regmatch_T regmatch;
12994 char_u patbuf[NUMBUFLEN];
12995 char_u *save_cpo;
12996 int match;
12997 colnr_T col = 0;
12998 int keepempty = FALSE;
12999 int typeerr = FALSE;
13000
13001 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13002 save_cpo = p_cpo;
13003 p_cpo = (char_u *)"";
13004
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013005 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006 if (argvars[1].v_type != VAR_UNKNOWN)
13007 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013008 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009 if (pat == NULL)
13010 typeerr = TRUE;
13011 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013012 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013 }
13014 if (pat == NULL || *pat == NUL)
13015 pat = (char_u *)"[\\x01- ]\\+";
13016
13017 if (rettv_list_alloc(rettv) == FAIL)
13018 return;
13019 if (typeerr)
13020 return;
13021
13022 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13023 if (regmatch.regprog != NULL)
13024 {
13025 regmatch.rm_ic = FALSE;
13026 while (*str != NUL || keepempty)
13027 {
13028 if (*str == NUL)
13029 match = FALSE; /* empty item at the end */
13030 else
13031 match = vim_regexec_nl(&regmatch, str, col);
13032 if (match)
13033 end = regmatch.startp[0];
13034 else
13035 end = str + STRLEN(str);
13036 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13037 && *str != NUL && match && end < regmatch.endp[0]))
13038 {
13039 if (list_append_string(rettv->vval.v_list, str,
13040 (int)(end - str)) == FAIL)
13041 break;
13042 }
13043 if (!match)
13044 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013045 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013046 if (regmatch.endp[0] > str)
13047 col = 0;
13048 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013049 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013050 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013051 str = regmatch.endp[0];
13052 }
13053
13054 vim_regfree(regmatch.regprog);
13055 }
13056
13057 p_cpo = save_cpo;
13058}
13059
13060#ifdef FEAT_FLOAT
13061/*
13062 * "sqrt()" function
13063 */
13064 static void
13065f_sqrt(typval_T *argvars, typval_T *rettv)
13066{
13067 float_T f = 0.0;
13068
13069 rettv->v_type = VAR_FLOAT;
13070 if (get_float_arg(argvars, &f) == OK)
13071 rettv->vval.v_float = sqrt(f);
13072 else
13073 rettv->vval.v_float = 0.0;
13074}
13075
13076/*
13077 * "str2float()" function
13078 */
13079 static void
13080f_str2float(typval_T *argvars, typval_T *rettv)
13081{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013082 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013083 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013084
Bram Moolenaar08243d22017-01-10 16:12:29 +010013085 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013086 p = skipwhite(p + 1);
13087 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013088 if (isneg)
13089 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013090 rettv->v_type = VAR_FLOAT;
13091}
13092#endif
13093
13094/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013095 * "str2list()" function
13096 */
13097 static void
13098f_str2list(typval_T *argvars, typval_T *rettv)
13099{
13100 char_u *p;
13101 int utf8 = FALSE;
13102
13103 if (rettv_list_alloc(rettv) == FAIL)
13104 return;
13105
13106 if (argvars[1].v_type != VAR_UNKNOWN)
13107 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13108
13109 p = tv_get_string(&argvars[0]);
13110
13111 if (has_mbyte || utf8)
13112 {
13113 int (*ptr2len)(char_u *);
13114 int (*ptr2char)(char_u *);
13115
13116 if (utf8 || enc_utf8)
13117 {
13118 ptr2len = utf_ptr2len;
13119 ptr2char = utf_ptr2char;
13120 }
13121 else
13122 {
13123 ptr2len = mb_ptr2len;
13124 ptr2char = mb_ptr2char;
13125 }
13126
13127 for ( ; *p != NUL; p += (*ptr2len)(p))
13128 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13129 }
13130 else
13131 for ( ; *p != NUL; ++p)
13132 list_append_number(rettv->vval.v_list, *p);
13133}
13134
13135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 * "str2nr()" function
13137 */
13138 static void
13139f_str2nr(typval_T *argvars, typval_T *rettv)
13140{
13141 int base = 10;
13142 char_u *p;
13143 varnumber_T n;
13144 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013145 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013146
13147 if (argvars[1].v_type != VAR_UNKNOWN)
13148 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013149 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 if (base != 2 && base != 8 && base != 10 && base != 16)
13151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013152 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 return;
13154 }
13155 }
13156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013158 isneg = (*p == '-');
13159 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013160 p = skipwhite(p + 1);
13161 switch (base)
13162 {
13163 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13164 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13165 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13166 default: what = 0;
13167 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013168 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13169 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013170 if (isneg)
13171 rettv->vval.v_number = -n;
13172 else
13173 rettv->vval.v_number = n;
13174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013175}
13176
13177#ifdef HAVE_STRFTIME
13178/*
13179 * "strftime({format}[, {time}])" function
13180 */
13181 static void
13182f_strftime(typval_T *argvars, typval_T *rettv)
13183{
13184 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013185# ifdef HAVE_LOCALTIME_R
13186 struct tm tmval;
13187# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013188 struct tm *curtime;
13189 time_t seconds;
13190 char_u *p;
13191
13192 rettv->v_type = VAR_STRING;
13193
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013194 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 if (argvars[1].v_type == VAR_UNKNOWN)
13196 seconds = time(NULL);
13197 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013198 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013199# ifdef HAVE_LOCALTIME_R
13200 curtime = localtime_r(&seconds, &tmval);
13201# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013202 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013203# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 /* MSVC returns NULL for an invalid value of seconds. */
13205 if (curtime == NULL)
13206 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13207 else
13208 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013209 vimconv_T conv;
13210 char_u *enc;
13211
13212 conv.vc_type = CONV_NONE;
13213 enc = enc_locale();
13214 convert_setup(&conv, p_enc, enc);
13215 if (conv.vc_type != CONV_NONE)
13216 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 if (p != NULL)
13218 (void)strftime((char *)result_buf, sizeof(result_buf),
13219 (char *)p, curtime);
13220 else
13221 result_buf[0] = NUL;
13222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 if (conv.vc_type != CONV_NONE)
13224 vim_free(p);
13225 convert_setup(&conv, enc, p_enc);
13226 if (conv.vc_type != CONV_NONE)
13227 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13228 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013229 rettv->vval.v_string = vim_strsave(result_buf);
13230
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231 /* Release conversion descriptors */
13232 convert_setup(&conv, NULL, NULL);
13233 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013234 }
13235}
13236#endif
13237
13238/*
13239 * "strgetchar()" function
13240 */
13241 static void
13242f_strgetchar(typval_T *argvars, typval_T *rettv)
13243{
13244 char_u *str;
13245 int len;
13246 int error = FALSE;
13247 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013248 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013249
13250 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013251 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013252 if (str == NULL)
13253 return;
13254 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013255 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013256 if (error)
13257 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258
Bram Moolenaar13505972019-01-24 15:04:48 +010013259 while (charidx >= 0 && byteidx < len)
13260 {
13261 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013263 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13264 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013266 --charidx;
13267 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269}
13270
13271/*
13272 * "stridx()" function
13273 */
13274 static void
13275f_stridx(typval_T *argvars, typval_T *rettv)
13276{
13277 char_u buf[NUMBUFLEN];
13278 char_u *needle;
13279 char_u *haystack;
13280 char_u *save_haystack;
13281 char_u *pos;
13282 int start_idx;
13283
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013284 needle = tv_get_string_chk(&argvars[1]);
13285 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013286 rettv->vval.v_number = -1;
13287 if (needle == NULL || haystack == NULL)
13288 return; /* type error; errmsg already given */
13289
13290 if (argvars[2].v_type != VAR_UNKNOWN)
13291 {
13292 int error = FALSE;
13293
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013294 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 if (error || start_idx >= (int)STRLEN(haystack))
13296 return;
13297 if (start_idx >= 0)
13298 haystack += start_idx;
13299 }
13300
13301 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13302 if (pos != NULL)
13303 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13304}
13305
13306/*
13307 * "string()" function
13308 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013309 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310f_string(typval_T *argvars, typval_T *rettv)
13311{
13312 char_u *tofree;
13313 char_u numbuf[NUMBUFLEN];
13314
13315 rettv->v_type = VAR_STRING;
13316 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13317 get_copyID());
13318 /* Make a copy if we have a value but it's not in allocated memory. */
13319 if (rettv->vval.v_string != NULL && tofree == NULL)
13320 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13321}
13322
13323/*
13324 * "strlen()" function
13325 */
13326 static void
13327f_strlen(typval_T *argvars, typval_T *rettv)
13328{
13329 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013330 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331}
13332
13333/*
13334 * "strchars()" function
13335 */
13336 static void
13337f_strchars(typval_T *argvars, typval_T *rettv)
13338{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013339 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013340 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013341 varnumber_T len = 0;
13342 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013343
13344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013345 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013346 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013347 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013348 else
13349 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013350 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13351 while (*s != NUL)
13352 {
13353 func_mb_ptr2char_adv(&s);
13354 ++len;
13355 }
13356 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013357 }
13358}
13359
13360/*
13361 * "strdisplaywidth()" function
13362 */
13363 static void
13364f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13365{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013366 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013367 int col = 0;
13368
13369 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013370 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013371
13372 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13373}
13374
13375/*
13376 * "strwidth()" function
13377 */
13378 static void
13379f_strwidth(typval_T *argvars, typval_T *rettv)
13380{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013381 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013382
Bram Moolenaar13505972019-01-24 15:04:48 +010013383 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013384}
13385
13386/*
13387 * "strcharpart()" function
13388 */
13389 static void
13390f_strcharpart(typval_T *argvars, typval_T *rettv)
13391{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013392 char_u *p;
13393 int nchar;
13394 int nbyte = 0;
13395 int charlen;
13396 int len = 0;
13397 int slen;
13398 int error = FALSE;
13399
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013400 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013401 slen = (int)STRLEN(p);
13402
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013403 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013404 if (!error)
13405 {
13406 if (nchar > 0)
13407 while (nchar > 0 && nbyte < slen)
13408 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013409 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013410 --nchar;
13411 }
13412 else
13413 nbyte = nchar;
13414 if (argvars[2].v_type != VAR_UNKNOWN)
13415 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013416 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013417 while (charlen > 0 && nbyte + len < slen)
13418 {
13419 int off = nbyte + len;
13420
13421 if (off < 0)
13422 len += 1;
13423 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013424 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013425 --charlen;
13426 }
13427 }
13428 else
13429 len = slen - nbyte; /* default: all bytes that are available. */
13430 }
13431
13432 /*
13433 * Only return the overlap between the specified part and the actual
13434 * string.
13435 */
13436 if (nbyte < 0)
13437 {
13438 len += nbyte;
13439 nbyte = 0;
13440 }
13441 else if (nbyte > slen)
13442 nbyte = slen;
13443 if (len < 0)
13444 len = 0;
13445 else if (nbyte + len > slen)
13446 len = slen - nbyte;
13447
13448 rettv->v_type = VAR_STRING;
13449 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013450}
13451
13452/*
13453 * "strpart()" function
13454 */
13455 static void
13456f_strpart(typval_T *argvars, typval_T *rettv)
13457{
13458 char_u *p;
13459 int n;
13460 int len;
13461 int slen;
13462 int error = FALSE;
13463
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013464 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013465 slen = (int)STRLEN(p);
13466
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013467 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013468 if (error)
13469 len = 0;
13470 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013471 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013472 else
13473 len = slen - n; /* default len: all bytes that are available. */
13474
13475 /*
13476 * Only return the overlap between the specified part and the actual
13477 * string.
13478 */
13479 if (n < 0)
13480 {
13481 len += n;
13482 n = 0;
13483 }
13484 else if (n > slen)
13485 n = slen;
13486 if (len < 0)
13487 len = 0;
13488 else if (n + len > slen)
13489 len = slen - n;
13490
13491 rettv->v_type = VAR_STRING;
13492 rettv->vval.v_string = vim_strnsave(p + n, len);
13493}
13494
13495/*
13496 * "strridx()" function
13497 */
13498 static void
13499f_strridx(typval_T *argvars, typval_T *rettv)
13500{
13501 char_u buf[NUMBUFLEN];
13502 char_u *needle;
13503 char_u *haystack;
13504 char_u *rest;
13505 char_u *lastmatch = NULL;
13506 int haystack_len, end_idx;
13507
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013508 needle = tv_get_string_chk(&argvars[1]);
13509 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013510
13511 rettv->vval.v_number = -1;
13512 if (needle == NULL || haystack == NULL)
13513 return; /* type error; errmsg already given */
13514
13515 haystack_len = (int)STRLEN(haystack);
13516 if (argvars[2].v_type != VAR_UNKNOWN)
13517 {
13518 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013519 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013520 if (end_idx < 0)
13521 return; /* can never find a match */
13522 }
13523 else
13524 end_idx = haystack_len;
13525
13526 if (*needle == NUL)
13527 {
13528 /* Empty string matches past the end. */
13529 lastmatch = haystack + end_idx;
13530 }
13531 else
13532 {
13533 for (rest = haystack; *rest != '\0'; ++rest)
13534 {
13535 rest = (char_u *)strstr((char *)rest, (char *)needle);
13536 if (rest == NULL || rest > haystack + end_idx)
13537 break;
13538 lastmatch = rest;
13539 }
13540 }
13541
13542 if (lastmatch == NULL)
13543 rettv->vval.v_number = -1;
13544 else
13545 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13546}
13547
13548/*
13549 * "strtrans()" function
13550 */
13551 static void
13552f_strtrans(typval_T *argvars, typval_T *rettv)
13553{
13554 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013555 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013556}
13557
13558/*
13559 * "submatch()" function
13560 */
13561 static void
13562f_submatch(typval_T *argvars, typval_T *rettv)
13563{
13564 int error = FALSE;
13565 int no;
13566 int retList = 0;
13567
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013568 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013569 if (error)
13570 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013571 if (no < 0 || no >= NSUBEXP)
13572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013573 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013574 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013575 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013576 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013577 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 if (error)
13579 return;
13580
13581 if (retList == 0)
13582 {
13583 rettv->v_type = VAR_STRING;
13584 rettv->vval.v_string = reg_submatch(no);
13585 }
13586 else
13587 {
13588 rettv->v_type = VAR_LIST;
13589 rettv->vval.v_list = reg_submatch_list(no);
13590 }
13591}
13592
13593/*
13594 * "substitute()" function
13595 */
13596 static void
13597f_substitute(typval_T *argvars, typval_T *rettv)
13598{
13599 char_u patbuf[NUMBUFLEN];
13600 char_u subbuf[NUMBUFLEN];
13601 char_u flagsbuf[NUMBUFLEN];
13602
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013603 char_u *str = tv_get_string_chk(&argvars[0]);
13604 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013605 char_u *sub = NULL;
13606 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013607 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013608
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013609 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13610 expr = &argvars[2];
13611 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013612 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013614 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013615 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13616 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 rettv->vval.v_string = NULL;
13618 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013619 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620}
13621
13622/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013623 * "swapinfo(swap_filename)" function
13624 */
13625 static void
13626f_swapinfo(typval_T *argvars, typval_T *rettv)
13627{
13628 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013629 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013630}
13631
13632/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013633 * "swapname(expr)" function
13634 */
13635 static void
13636f_swapname(typval_T *argvars, typval_T *rettv)
13637{
13638 buf_T *buf;
13639
13640 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013641 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013642 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13643 || buf->b_ml.ml_mfp->mf_fname == NULL)
13644 rettv->vval.v_string = NULL;
13645 else
13646 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13647}
13648
13649/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013650 * "synID(lnum, col, trans)" function
13651 */
13652 static void
13653f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13654{
13655 int id = 0;
13656#ifdef FEAT_SYN_HL
13657 linenr_T lnum;
13658 colnr_T col;
13659 int trans;
13660 int transerr = FALSE;
13661
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013662 lnum = tv_get_lnum(argvars); /* -1 on type error */
13663 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13664 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013665
13666 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13667 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13668 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13669#endif
13670
13671 rettv->vval.v_number = id;
13672}
13673
13674/*
13675 * "synIDattr(id, what [, mode])" function
13676 */
13677 static void
13678f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13679{
13680 char_u *p = NULL;
13681#ifdef FEAT_SYN_HL
13682 int id;
13683 char_u *what;
13684 char_u *mode;
13685 char_u modebuf[NUMBUFLEN];
13686 int modec;
13687
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013688 id = (int)tv_get_number(&argvars[0]);
13689 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013690 if (argvars[2].v_type != VAR_UNKNOWN)
13691 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013692 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013693 modec = TOLOWER_ASC(mode[0]);
13694 if (modec != 't' && modec != 'c' && modec != 'g')
13695 modec = 0; /* replace invalid with current */
13696 }
13697 else
13698 {
13699#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13700 if (USE_24BIT)
13701 modec = 'g';
13702 else
13703#endif
13704 if (t_colors > 1)
13705 modec = 'c';
13706 else
13707 modec = 't';
13708 }
13709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013710 switch (TOLOWER_ASC(what[0]))
13711 {
13712 case 'b':
13713 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13714 p = highlight_color(id, what, modec);
13715 else /* bold */
13716 p = highlight_has_attr(id, HL_BOLD, modec);
13717 break;
13718
13719 case 'f': /* fg[#] or font */
13720 p = highlight_color(id, what, modec);
13721 break;
13722
13723 case 'i':
13724 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13725 p = highlight_has_attr(id, HL_INVERSE, modec);
13726 else /* italic */
13727 p = highlight_has_attr(id, HL_ITALIC, modec);
13728 break;
13729
13730 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013731 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013732 break;
13733
13734 case 'r': /* reverse */
13735 p = highlight_has_attr(id, HL_INVERSE, modec);
13736 break;
13737
13738 case 's':
13739 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13740 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013741 /* strikeout */
13742 else if (TOLOWER_ASC(what[1]) == 't' &&
13743 TOLOWER_ASC(what[2]) == 'r')
13744 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013745 else /* standout */
13746 p = highlight_has_attr(id, HL_STANDOUT, modec);
13747 break;
13748
13749 case 'u':
13750 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13751 /* underline */
13752 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13753 else
13754 /* undercurl */
13755 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13756 break;
13757 }
13758
13759 if (p != NULL)
13760 p = vim_strsave(p);
13761#endif
13762 rettv->v_type = VAR_STRING;
13763 rettv->vval.v_string = p;
13764}
13765
13766/*
13767 * "synIDtrans(id)" function
13768 */
13769 static void
13770f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13771{
13772 int id;
13773
13774#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013775 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013776
13777 if (id > 0)
13778 id = syn_get_final_id(id);
13779 else
13780#endif
13781 id = 0;
13782
13783 rettv->vval.v_number = id;
13784}
13785
13786/*
13787 * "synconcealed(lnum, col)" function
13788 */
13789 static void
13790f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13791{
13792#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13793 linenr_T lnum;
13794 colnr_T col;
13795 int syntax_flags = 0;
13796 int cchar;
13797 int matchid = 0;
13798 char_u str[NUMBUFLEN];
13799#endif
13800
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013801 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013802
13803#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013804 lnum = tv_get_lnum(argvars); /* -1 on type error */
13805 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013806
13807 vim_memset(str, NUL, sizeof(str));
13808
13809 if (rettv_list_alloc(rettv) != FAIL)
13810 {
13811 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13812 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13813 && curwin->w_p_cole > 0)
13814 {
13815 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13816 syntax_flags = get_syntax_info(&matchid);
13817
13818 /* get the conceal character */
13819 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13820 {
13821 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013822 if (cchar == NUL && curwin->w_p_cole == 1)
13823 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824 if (cchar != NUL)
13825 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013826 if (has_mbyte)
13827 (*mb_char2bytes)(cchar, str);
13828 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 str[0] = cchar;
13830 }
13831 }
13832 }
13833
13834 list_append_number(rettv->vval.v_list,
13835 (syntax_flags & HL_CONCEAL) != 0);
13836 /* -1 to auto-determine strlen */
13837 list_append_string(rettv->vval.v_list, str, -1);
13838 list_append_number(rettv->vval.v_list, matchid);
13839 }
13840#endif
13841}
13842
13843/*
13844 * "synstack(lnum, col)" function
13845 */
13846 static void
13847f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13848{
13849#ifdef FEAT_SYN_HL
13850 linenr_T lnum;
13851 colnr_T col;
13852 int i;
13853 int id;
13854#endif
13855
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013856 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013857
13858#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013859 lnum = tv_get_lnum(argvars); /* -1 on type error */
13860 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013861
13862 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13863 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13864 && rettv_list_alloc(rettv) != FAIL)
13865 {
13866 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13867 for (i = 0; ; ++i)
13868 {
13869 id = syn_get_stack_item(i);
13870 if (id < 0)
13871 break;
13872 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13873 break;
13874 }
13875 }
13876#endif
13877}
13878
13879 static void
13880get_cmd_output_as_rettv(
13881 typval_T *argvars,
13882 typval_T *rettv,
13883 int retlist)
13884{
13885 char_u *res = NULL;
13886 char_u *p;
13887 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013888 int err = FALSE;
13889 FILE *fd;
13890 list_T *list = NULL;
13891 int flags = SHELL_SILENT;
13892
13893 rettv->v_type = VAR_STRING;
13894 rettv->vval.v_string = NULL;
13895 if (check_restricted() || check_secure())
13896 goto errret;
13897
13898 if (argvars[1].v_type != VAR_UNKNOWN)
13899 {
13900 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013901 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013902 * command.
13903 */
13904 if ((infile = vim_tempname('i', TRUE)) == NULL)
13905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013906 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013907 goto errret;
13908 }
13909
13910 fd = mch_fopen((char *)infile, WRITEBIN);
13911 if (fd == NULL)
13912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013913 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013914 goto errret;
13915 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013916 if (argvars[1].v_type == VAR_NUMBER)
13917 {
13918 linenr_T lnum;
13919 buf_T *buf;
13920
13921 buf = buflist_findnr(argvars[1].vval.v_number);
13922 if (buf == NULL)
13923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013924 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013925 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013926 goto errret;
13927 }
13928
13929 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13930 {
13931 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13932 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13933 {
13934 err = TRUE;
13935 break;
13936 }
13937 if (putc(NL, fd) == EOF)
13938 {
13939 err = TRUE;
13940 break;
13941 }
13942 }
13943 }
13944 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 {
13946 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13947 err = TRUE;
13948 }
13949 else
13950 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013951 size_t len;
13952 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013953
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013954 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955 if (p == NULL)
13956 {
13957 fclose(fd);
13958 goto errret; /* type error; errmsg already given */
13959 }
13960 len = STRLEN(p);
13961 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13962 err = TRUE;
13963 }
13964 if (fclose(fd) != 0)
13965 err = TRUE;
13966 if (err)
13967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013968 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013969 goto errret;
13970 }
13971 }
13972
13973 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13974 * echoes typeahead, that messes up the display. */
13975 if (!msg_silent)
13976 flags += SHELL_COOKED;
13977
13978 if (retlist)
13979 {
13980 int len;
13981 listitem_T *li;
13982 char_u *s = NULL;
13983 char_u *start;
13984 char_u *end;
13985 int i;
13986
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013987 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013988 if (res == NULL)
13989 goto errret;
13990
13991 list = list_alloc();
13992 if (list == NULL)
13993 goto errret;
13994
13995 for (i = 0; i < len; ++i)
13996 {
13997 start = res + i;
13998 while (i < len && res[i] != NL)
13999 ++i;
14000 end = res + i;
14001
Bram Moolenaar964b3742019-05-24 18:54:09 +020014002 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014003 if (s == NULL)
14004 goto errret;
14005
14006 for (p = s; start < end; ++p, ++start)
14007 *p = *start == NUL ? NL : *start;
14008 *p = NUL;
14009
14010 li = listitem_alloc();
14011 if (li == NULL)
14012 {
14013 vim_free(s);
14014 goto errret;
14015 }
14016 li->li_tv.v_type = VAR_STRING;
14017 li->li_tv.v_lock = 0;
14018 li->li_tv.vval.v_string = s;
14019 list_append(list, li);
14020 }
14021
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014022 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014023 list = NULL;
14024 }
14025 else
14026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014027 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010014028#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014029 /* translate <CR><NL> into <NL> */
14030 if (res != NULL)
14031 {
14032 char_u *s, *d;
14033
14034 d = res;
14035 for (s = res; *s; ++s)
14036 {
14037 if (s[0] == CAR && s[1] == NL)
14038 ++s;
14039 *d++ = *s;
14040 }
14041 *d = NUL;
14042 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014043#endif
14044 rettv->vval.v_string = res;
14045 res = NULL;
14046 }
14047
14048errret:
14049 if (infile != NULL)
14050 {
14051 mch_remove(infile);
14052 vim_free(infile);
14053 }
14054 if (res != NULL)
14055 vim_free(res);
14056 if (list != NULL)
14057 list_free(list);
14058}
14059
14060/*
14061 * "system()" function
14062 */
14063 static void
14064f_system(typval_T *argvars, typval_T *rettv)
14065{
14066 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14067}
14068
14069/*
14070 * "systemlist()" function
14071 */
14072 static void
14073f_systemlist(typval_T *argvars, typval_T *rettv)
14074{
14075 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14076}
14077
14078/*
14079 * "tabpagebuflist()" function
14080 */
14081 static void
14082f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14083{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014084 tabpage_T *tp;
14085 win_T *wp = NULL;
14086
14087 if (argvars[0].v_type == VAR_UNKNOWN)
14088 wp = firstwin;
14089 else
14090 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014091 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014092 if (tp != NULL)
14093 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14094 }
14095 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14096 {
14097 for (; wp != NULL; wp = wp->w_next)
14098 if (list_append_number(rettv->vval.v_list,
14099 wp->w_buffer->b_fnum) == FAIL)
14100 break;
14101 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014102}
14103
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014104/*
14105 * "tabpagenr()" function
14106 */
14107 static void
14108f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14109{
14110 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014111 char_u *arg;
14112
14113 if (argvars[0].v_type != VAR_UNKNOWN)
14114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014115 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014116 nr = 0;
14117 if (arg != NULL)
14118 {
14119 if (STRCMP(arg, "$") == 0)
14120 nr = tabpage_index(NULL) - 1;
14121 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014122 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014123 }
14124 }
14125 else
14126 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127 rettv->vval.v_number = nr;
14128}
14129
14130
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014131/*
14132 * Common code for tabpagewinnr() and winnr().
14133 */
14134 static int
14135get_winnr(tabpage_T *tp, typval_T *argvar)
14136{
14137 win_T *twin;
14138 int nr = 1;
14139 win_T *wp;
14140 char_u *arg;
14141
14142 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14143 if (argvar->v_type != VAR_UNKNOWN)
14144 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014145 int invalid_arg = FALSE;
14146
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014147 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 if (arg == NULL)
14149 nr = 0; /* type error; errmsg already given */
14150 else if (STRCMP(arg, "$") == 0)
14151 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14152 else if (STRCMP(arg, "#") == 0)
14153 {
14154 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14155 if (twin == NULL)
14156 nr = 0;
14157 }
14158 else
14159 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014160 long count;
14161 char_u *endp;
14162
14163 // Extract the window count (if specified). e.g. winnr('3j')
14164 count = strtol((char *)arg, (char **)&endp, 10);
14165 if (count <= 0)
14166 count = 1; // if count is not specified, default to 1
14167 if (endp != NULL && *endp != '\0')
14168 {
14169 if (STRCMP(endp, "j") == 0)
14170 twin = win_vert_neighbor(tp, twin, FALSE, count);
14171 else if (STRCMP(endp, "k") == 0)
14172 twin = win_vert_neighbor(tp, twin, TRUE, count);
14173 else if (STRCMP(endp, "h") == 0)
14174 twin = win_horz_neighbor(tp, twin, TRUE, count);
14175 else if (STRCMP(endp, "l") == 0)
14176 twin = win_horz_neighbor(tp, twin, FALSE, count);
14177 else
14178 invalid_arg = TRUE;
14179 }
14180 else
14181 invalid_arg = TRUE;
14182 }
14183
14184 if (invalid_arg)
14185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014186 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014187 nr = 0;
14188 }
14189 }
14190
14191 if (nr > 0)
14192 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14193 wp != twin; wp = wp->w_next)
14194 {
14195 if (wp == NULL)
14196 {
14197 /* didn't find it in this tabpage */
14198 nr = 0;
14199 break;
14200 }
14201 ++nr;
14202 }
14203 return nr;
14204}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014205
14206/*
14207 * "tabpagewinnr()" function
14208 */
14209 static void
14210f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14211{
14212 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014213 tabpage_T *tp;
14214
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014215 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216 if (tp == NULL)
14217 nr = 0;
14218 else
14219 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014220 rettv->vval.v_number = nr;
14221}
14222
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014223/*
14224 * "tagfiles()" function
14225 */
14226 static void
14227f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14228{
14229 char_u *fname;
14230 tagname_T tn;
14231 int first;
14232
14233 if (rettv_list_alloc(rettv) == FAIL)
14234 return;
14235 fname = alloc(MAXPATHL);
14236 if (fname == NULL)
14237 return;
14238
14239 for (first = TRUE; ; first = FALSE)
14240 if (get_tagfname(&tn, first, fname) == FAIL
14241 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14242 break;
14243 tagname_free(&tn);
14244 vim_free(fname);
14245}
14246
14247/*
14248 * "taglist()" function
14249 */
14250 static void
14251f_taglist(typval_T *argvars, typval_T *rettv)
14252{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014253 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014254 char_u *tag_pattern;
14255
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014256 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014257
14258 rettv->vval.v_number = FALSE;
14259 if (*tag_pattern == NUL)
14260 return;
14261
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014262 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014263 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014264 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014265 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014266}
14267
14268/*
14269 * "tempname()" function
14270 */
14271 static void
14272f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14273{
14274 static int x = 'A';
14275
14276 rettv->v_type = VAR_STRING;
14277 rettv->vval.v_string = vim_tempname(x, FALSE);
14278
14279 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14280 * names. Skip 'I' and 'O', they are used for shell redirection. */
14281 do
14282 {
14283 if (x == 'Z')
14284 x = '0';
14285 else if (x == '9')
14286 x = 'A';
14287 else
14288 {
14289#ifdef EBCDIC
14290 if (x == 'I')
14291 x = 'J';
14292 else if (x == 'R')
14293 x = 'S';
14294 else
14295#endif
14296 ++x;
14297 }
14298 } while (x == 'I' || x == 'O');
14299}
14300
14301#ifdef FEAT_FLOAT
14302/*
14303 * "tan()" function
14304 */
14305 static void
14306f_tan(typval_T *argvars, typval_T *rettv)
14307{
14308 float_T f = 0.0;
14309
14310 rettv->v_type = VAR_FLOAT;
14311 if (get_float_arg(argvars, &f) == OK)
14312 rettv->vval.v_float = tan(f);
14313 else
14314 rettv->vval.v_float = 0.0;
14315}
14316
14317/*
14318 * "tanh()" function
14319 */
14320 static void
14321f_tanh(typval_T *argvars, typval_T *rettv)
14322{
14323 float_T f = 0.0;
14324
14325 rettv->v_type = VAR_FLOAT;
14326 if (get_float_arg(argvars, &f) == OK)
14327 rettv->vval.v_float = tanh(f);
14328 else
14329 rettv->vval.v_float = 0.0;
14330}
14331#endif
14332
14333/*
14334 * "test_alloc_fail(id, countdown, repeat)" function
14335 */
14336 static void
14337f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14338{
14339 if (argvars[0].v_type != VAR_NUMBER
14340 || argvars[0].vval.v_number <= 0
14341 || argvars[1].v_type != VAR_NUMBER
14342 || argvars[1].vval.v_number < 0
14343 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014344 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014345 else
14346 {
14347 alloc_fail_id = argvars[0].vval.v_number;
14348 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014349 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014350 alloc_fail_countdown = argvars[1].vval.v_number;
14351 alloc_fail_repeat = argvars[2].vval.v_number;
14352 did_outofmem_msg = FALSE;
14353 }
14354}
14355
14356/*
14357 * "test_autochdir()"
14358 */
14359 static void
14360f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14361{
14362#if defined(FEAT_AUTOCHDIR)
14363 test_autochdir = TRUE;
14364#endif
14365}
14366
14367/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014368 * "test_feedinput()"
14369 */
14370 static void
14371f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14372{
14373#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014374 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014375
14376 if (val != NULL)
14377 {
14378 trash_input_buf();
14379 add_to_input_buf_csi(val, (int)STRLEN(val));
14380 }
14381#endif
14382}
14383
14384/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014385 * "test_getvalue({name})" function
14386 */
14387 static void
14388f_test_getvalue(typval_T *argvars, typval_T *rettv)
14389{
14390 if (argvars[0].v_type != VAR_STRING)
14391 emsg(_(e_invarg));
14392 else
14393 {
14394 char_u *name = tv_get_string(&argvars[0]);
14395
14396 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14397 rettv->vval.v_number = need_fileinfo;
14398 else
14399 semsg(_(e_invarg2), name);
14400 }
14401}
14402
14403/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014404 * "test_option_not_set({name})" function
14405 */
14406 static void
14407f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14408{
14409 char_u *name = (char_u *)"";
14410
14411 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014412 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014413 else
14414 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014415 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014416 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014417 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014418 }
14419}
14420
14421/*
14422 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014423 */
14424 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014425f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014426{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014427 char_u *name = (char_u *)"";
14428 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014429 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014430
14431 if (argvars[0].v_type != VAR_STRING
14432 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014433 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014434 else
14435 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014436 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014437 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014438
14439 if (STRCMP(name, (char_u *)"redraw") == 0)
14440 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014441 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14442 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014443 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14444 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014445 else if (STRCMP(name, (char_u *)"starting") == 0)
14446 {
14447 if (val)
14448 {
14449 if (save_starting < 0)
14450 save_starting = starting;
14451 starting = 0;
14452 }
14453 else
14454 {
14455 starting = save_starting;
14456 save_starting = -1;
14457 }
14458 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014459 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14460 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014461 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14462 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014463 else if (STRCMP(name, (char_u *)"ALL") == 0)
14464 {
14465 disable_char_avail_for_testing = FALSE;
14466 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014467 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014468 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014469 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014470 if (save_starting >= 0)
14471 {
14472 starting = save_starting;
14473 save_starting = -1;
14474 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014475 }
14476 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014477 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014478 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014479}
14480
14481/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014482 * "test_refcount({expr})" function
14483 */
14484 static void
14485f_test_refcount(typval_T *argvars, typval_T *rettv)
14486{
14487 int retval = -1;
14488
14489 switch (argvars[0].v_type)
14490 {
14491 case VAR_UNKNOWN:
14492 case VAR_NUMBER:
14493 case VAR_FLOAT:
14494 case VAR_SPECIAL:
14495 case VAR_STRING:
14496 break;
14497 case VAR_JOB:
14498#ifdef FEAT_JOB_CHANNEL
14499 if (argvars[0].vval.v_job != NULL)
14500 retval = argvars[0].vval.v_job->jv_refcount - 1;
14501#endif
14502 break;
14503 case VAR_CHANNEL:
14504#ifdef FEAT_JOB_CHANNEL
14505 if (argvars[0].vval.v_channel != NULL)
14506 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14507#endif
14508 break;
14509 case VAR_FUNC:
14510 if (argvars[0].vval.v_string != NULL)
14511 {
14512 ufunc_T *fp;
14513
14514 fp = find_func(argvars[0].vval.v_string);
14515 if (fp != NULL)
14516 retval = fp->uf_refcount;
14517 }
14518 break;
14519 case VAR_PARTIAL:
14520 if (argvars[0].vval.v_partial != NULL)
14521 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14522 break;
14523 case VAR_BLOB:
14524 if (argvars[0].vval.v_blob != NULL)
14525 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14526 break;
14527 case VAR_LIST:
14528 if (argvars[0].vval.v_list != NULL)
14529 retval = argvars[0].vval.v_list->lv_refcount - 1;
14530 break;
14531 case VAR_DICT:
14532 if (argvars[0].vval.v_dict != NULL)
14533 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14534 break;
14535 }
14536
14537 rettv->v_type = VAR_NUMBER;
14538 rettv->vval.v_number = retval;
14539
14540}
14541
14542/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014543 * "test_garbagecollect_now()" function
14544 */
14545 static void
14546f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14547{
14548 /* This is dangerous, any Lists and Dicts used internally may be freed
14549 * while still in use. */
14550 garbage_collect(TRUE);
14551}
14552
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014553/*
14554 * "test_ignore_error()" function
14555 */
14556 static void
14557f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14558{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014559 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014560}
14561
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014562 static void
14563f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14564{
14565 rettv->v_type = VAR_BLOB;
14566 rettv->vval.v_blob = NULL;
14567}
14568
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014569#ifdef FEAT_JOB_CHANNEL
14570 static void
14571f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14572{
14573 rettv->v_type = VAR_CHANNEL;
14574 rettv->vval.v_channel = NULL;
14575}
14576#endif
14577
14578 static void
14579f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14580{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014581 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014582}
14583
14584#ifdef FEAT_JOB_CHANNEL
14585 static void
14586f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14587{
14588 rettv->v_type = VAR_JOB;
14589 rettv->vval.v_job = NULL;
14590}
14591#endif
14592
14593 static void
14594f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14595{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014596 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014597}
14598
14599 static void
14600f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14601{
14602 rettv->v_type = VAR_PARTIAL;
14603 rettv->vval.v_partial = NULL;
14604}
14605
14606 static void
14607f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14608{
14609 rettv->v_type = VAR_STRING;
14610 rettv->vval.v_string = NULL;
14611}
14612
Bram Moolenaarab186732018-09-14 21:27:06 +020014613#ifdef FEAT_GUI
14614 static void
14615f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14616{
14617 char_u *which;
14618 long value;
14619 int dragging;
14620 scrollbar_T *sb = NULL;
14621
14622 if (argvars[0].v_type != VAR_STRING
14623 || (argvars[1].v_type) != VAR_NUMBER
14624 || (argvars[2].v_type) != VAR_NUMBER)
14625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014626 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014627 return;
14628 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014629 which = tv_get_string(&argvars[0]);
14630 value = tv_get_number(&argvars[1]);
14631 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014632
14633 if (STRCMP(which, "left") == 0)
14634 sb = &curwin->w_scrollbars[SBAR_LEFT];
14635 else if (STRCMP(which, "right") == 0)
14636 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14637 else if (STRCMP(which, "hor") == 0)
14638 sb = &gui.bottom_sbar;
14639 if (sb == NULL)
14640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014641 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014642 return;
14643 }
14644 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014645# ifndef USE_ON_FLY_SCROLL
14646 // need to loop through normal_cmd() to handle the scroll events
14647 exec_normal(FALSE, TRUE, FALSE);
14648# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014649}
14650#endif
14651
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014652#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014654f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14655{
14656 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14657 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14658}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014659#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014660
14661 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014662f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14663{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014664 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014665}
14666
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014667/*
14668 * Get a callback from "arg". It can be a Funcref or a function name.
14669 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014670 * "cb_name" is not allocated.
14671 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014672 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014673 callback_T
14674get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014675{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014676 callback_T res;
14677
14678 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014679 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14680 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014681 res.cb_partial = arg->vval.v_partial;
14682 ++res.cb_partial->pt_refcount;
14683 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014684 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014685 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014686 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014687 res.cb_partial = NULL;
14688 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14689 {
14690 // Note that we don't make a copy of the string.
14691 res.cb_name = arg->vval.v_string;
14692 func_ref(res.cb_name);
14693 }
14694 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14695 {
14696 res.cb_name = (char_u *)"";
14697 }
14698 else
14699 {
14700 emsg(_("E921: Invalid callback argument"));
14701 res.cb_name = NULL;
14702 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014704 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014705}
14706
14707/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014708 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014709 */
14710 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014711put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014712{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014713 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014714 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014715 tv->v_type = VAR_PARTIAL;
14716 tv->vval.v_partial = cb->cb_partial;
14717 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014718 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014719 else
14720 {
14721 tv->v_type = VAR_FUNC;
14722 tv->vval.v_string = vim_strsave(cb->cb_name);
14723 func_ref(cb->cb_name);
14724 }
14725}
14726
14727/*
14728 * Make a copy of "src" into "dest", allocating the function name if needed,
14729 * without incrementing the refcount.
14730 */
14731 void
14732set_callback(callback_T *dest, callback_T *src)
14733{
14734 if (src->cb_partial == NULL)
14735 {
14736 // just a function name, make a copy
14737 dest->cb_name = vim_strsave(src->cb_name);
14738 dest->cb_free_name = TRUE;
14739 }
14740 else
14741 {
14742 // cb_name is a pointer into cb_partial
14743 dest->cb_name = src->cb_name;
14744 dest->cb_free_name = FALSE;
14745 }
14746 dest->cb_partial = src->cb_partial;
14747}
14748
14749/*
14750 * Unref/free "callback" returned by get_callback() or set_callback().
14751 */
14752 void
14753free_callback(callback_T *callback)
14754{
14755 if (callback->cb_partial != NULL)
14756 {
14757 partial_unref(callback->cb_partial);
14758 callback->cb_partial = NULL;
14759 }
14760 else if (callback->cb_name != NULL)
14761 func_unref(callback->cb_name);
14762 if (callback->cb_free_name)
14763 {
14764 vim_free(callback->cb_name);
14765 callback->cb_free_name = FALSE;
14766 }
14767 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014768}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014769
14770#ifdef FEAT_TIMERS
14771/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014772 * "timer_info([timer])" function
14773 */
14774 static void
14775f_timer_info(typval_T *argvars, typval_T *rettv)
14776{
14777 timer_T *timer = NULL;
14778
14779 if (rettv_list_alloc(rettv) != OK)
14780 return;
14781 if (argvars[0].v_type != VAR_UNKNOWN)
14782 {
14783 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014784 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014785 else
14786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014787 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014788 if (timer != NULL)
14789 add_timer_info(rettv, timer);
14790 }
14791 }
14792 else
14793 add_timer_info_all(rettv);
14794}
14795
14796/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014797 * "timer_pause(timer, paused)" function
14798 */
14799 static void
14800f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14801{
14802 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014803 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014804
14805 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014806 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014807 else
14808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014809 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014810 if (timer != NULL)
14811 timer->tr_paused = paused;
14812 }
14813}
14814
14815/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014816 * "timer_start(time, callback [, options])" function
14817 */
14818 static void
14819f_timer_start(typval_T *argvars, typval_T *rettv)
14820{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014821 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014822 timer_T *timer;
14823 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014824 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014825 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014826
Bram Moolenaar75537a92016-09-05 22:45:28 +020014827 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828 if (check_secure())
14829 return;
14830 if (argvars[2].v_type != VAR_UNKNOWN)
14831 {
14832 if (argvars[2].v_type != VAR_DICT
14833 || (dict = argvars[2].vval.v_dict) == NULL)
14834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014835 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014836 return;
14837 }
14838 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014839 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014840 }
14841
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014842 callback = get_callback(&argvars[1]);
14843 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014844 return;
14845
14846 timer = create_timer(msec, repeat);
14847 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014848 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014849 else
14850 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014851 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014852 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014853 }
14854}
14855
14856/*
14857 * "timer_stop(timer)" function
14858 */
14859 static void
14860f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14861{
14862 timer_T *timer;
14863
14864 if (argvars[0].v_type != VAR_NUMBER)
14865 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014866 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014867 return;
14868 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014869 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014870 if (timer != NULL)
14871 stop_timer(timer);
14872}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014873
14874/*
14875 * "timer_stopall()" function
14876 */
14877 static void
14878f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14879{
14880 stop_all_timers();
14881}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014882#endif
14883
14884/*
14885 * "tolower(string)" function
14886 */
14887 static void
14888f_tolower(typval_T *argvars, typval_T *rettv)
14889{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014890 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014891 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014892}
14893
14894/*
14895 * "toupper(string)" function
14896 */
14897 static void
14898f_toupper(typval_T *argvars, typval_T *rettv)
14899{
14900 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014901 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014902}
14903
14904/*
14905 * "tr(string, fromstr, tostr)" function
14906 */
14907 static void
14908f_tr(typval_T *argvars, typval_T *rettv)
14909{
14910 char_u *in_str;
14911 char_u *fromstr;
14912 char_u *tostr;
14913 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014914 int inlen;
14915 int fromlen;
14916 int tolen;
14917 int idx;
14918 char_u *cpstr;
14919 int cplen;
14920 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014921 char_u buf[NUMBUFLEN];
14922 char_u buf2[NUMBUFLEN];
14923 garray_T ga;
14924
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014925 in_str = tv_get_string(&argvars[0]);
14926 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14927 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014928
14929 /* Default return value: empty string. */
14930 rettv->v_type = VAR_STRING;
14931 rettv->vval.v_string = NULL;
14932 if (fromstr == NULL || tostr == NULL)
14933 return; /* type error; errmsg already given */
14934 ga_init2(&ga, (int)sizeof(char), 80);
14935
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014936 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014937 /* not multi-byte: fromstr and tostr must be the same length */
14938 if (STRLEN(fromstr) != STRLEN(tostr))
14939 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014940error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014941 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014942 ga_clear(&ga);
14943 return;
14944 }
14945
14946 /* fromstr and tostr have to contain the same number of chars */
14947 while (*in_str != NUL)
14948 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014949 if (has_mbyte)
14950 {
14951 inlen = (*mb_ptr2len)(in_str);
14952 cpstr = in_str;
14953 cplen = inlen;
14954 idx = 0;
14955 for (p = fromstr; *p != NUL; p += fromlen)
14956 {
14957 fromlen = (*mb_ptr2len)(p);
14958 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14959 {
14960 for (p = tostr; *p != NUL; p += tolen)
14961 {
14962 tolen = (*mb_ptr2len)(p);
14963 if (idx-- == 0)
14964 {
14965 cplen = tolen;
14966 cpstr = p;
14967 break;
14968 }
14969 }
14970 if (*p == NUL) /* tostr is shorter than fromstr */
14971 goto error;
14972 break;
14973 }
14974 ++idx;
14975 }
14976
14977 if (first && cpstr == in_str)
14978 {
14979 /* Check that fromstr and tostr have the same number of
14980 * (multi-byte) characters. Done only once when a character
14981 * of in_str doesn't appear in fromstr. */
14982 first = FALSE;
14983 for (p = tostr; *p != NUL; p += tolen)
14984 {
14985 tolen = (*mb_ptr2len)(p);
14986 --idx;
14987 }
14988 if (idx != 0)
14989 goto error;
14990 }
14991
14992 (void)ga_grow(&ga, cplen);
14993 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14994 ga.ga_len += cplen;
14995
14996 in_str += inlen;
14997 }
14998 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014999 {
15000 /* When not using multi-byte chars we can do it faster. */
15001 p = vim_strchr(fromstr, *in_str);
15002 if (p != NULL)
15003 ga_append(&ga, tostr[p - fromstr]);
15004 else
15005 ga_append(&ga, *in_str);
15006 ++in_str;
15007 }
15008 }
15009
15010 /* add a terminating NUL */
15011 (void)ga_grow(&ga, 1);
15012 ga_append(&ga, NUL);
15013
15014 rettv->vval.v_string = ga.ga_data;
15015}
15016
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015017/*
15018 * "trim({expr})" function
15019 */
15020 static void
15021f_trim(typval_T *argvars, typval_T *rettv)
15022{
15023 char_u buf1[NUMBUFLEN];
15024 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015025 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015026 char_u *mask = NULL;
15027 char_u *tail;
15028 char_u *prev;
15029 char_u *p;
15030 int c1;
15031
15032 rettv->v_type = VAR_STRING;
15033 if (head == NULL)
15034 {
15035 rettv->vval.v_string = NULL;
15036 return;
15037 }
15038
15039 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015040 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010015041
15042 while (*head != NUL)
15043 {
15044 c1 = PTR2CHAR(head);
15045 if (mask == NULL)
15046 {
15047 if (c1 > ' ' && c1 != 0xa0)
15048 break;
15049 }
15050 else
15051 {
15052 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15053 if (c1 == PTR2CHAR(p))
15054 break;
15055 if (*p == NUL)
15056 break;
15057 }
15058 MB_PTR_ADV(head);
15059 }
15060
15061 for (tail = head + STRLEN(head); tail > head; tail = prev)
15062 {
15063 prev = tail;
15064 MB_PTR_BACK(head, prev);
15065 c1 = PTR2CHAR(prev);
15066 if (mask == NULL)
15067 {
15068 if (c1 > ' ' && c1 != 0xa0)
15069 break;
15070 }
15071 else
15072 {
15073 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15074 if (c1 == PTR2CHAR(p))
15075 break;
15076 if (*p == NUL)
15077 break;
15078 }
15079 }
15080 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15081}
15082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015083#ifdef FEAT_FLOAT
15084/*
15085 * "trunc({float})" function
15086 */
15087 static void
15088f_trunc(typval_T *argvars, typval_T *rettv)
15089{
15090 float_T f = 0.0;
15091
15092 rettv->v_type = VAR_FLOAT;
15093 if (get_float_arg(argvars, &f) == OK)
15094 /* trunc() is not in C90, use floor() or ceil() instead. */
15095 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15096 else
15097 rettv->vval.v_float = 0.0;
15098}
15099#endif
15100
15101/*
15102 * "type(expr)" function
15103 */
15104 static void
15105f_type(typval_T *argvars, typval_T *rettv)
15106{
15107 int n = -1;
15108
15109 switch (argvars[0].v_type)
15110 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015111 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15112 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015113 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015114 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15115 case VAR_LIST: n = VAR_TYPE_LIST; break;
15116 case VAR_DICT: n = VAR_TYPE_DICT; break;
15117 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015118 case VAR_SPECIAL:
15119 if (argvars[0].vval.v_number == VVAL_FALSE
15120 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015121 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015122 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015123 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015124 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015125 case VAR_JOB: n = VAR_TYPE_JOB; break;
15126 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015127 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015128 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015129 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015130 n = -1;
15131 break;
15132 }
15133 rettv->vval.v_number = n;
15134}
15135
15136/*
15137 * "undofile(name)" function
15138 */
15139 static void
15140f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15141{
15142 rettv->v_type = VAR_STRING;
15143#ifdef FEAT_PERSISTENT_UNDO
15144 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015145 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015146
15147 if (*fname == NUL)
15148 {
15149 /* If there is no file name there will be no undo file. */
15150 rettv->vval.v_string = NULL;
15151 }
15152 else
15153 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015154 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015155
15156 if (ffname != NULL)
15157 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15158 vim_free(ffname);
15159 }
15160 }
15161#else
15162 rettv->vval.v_string = NULL;
15163#endif
15164}
15165
15166/*
15167 * "undotree()" function
15168 */
15169 static void
15170f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15171{
15172 if (rettv_dict_alloc(rettv) == OK)
15173 {
15174 dict_T *dict = rettv->vval.v_dict;
15175 list_T *list;
15176
Bram Moolenaare0be1672018-07-08 16:50:37 +020015177 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15178 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15179 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15180 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15181 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15182 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183
15184 list = list_alloc();
15185 if (list != NULL)
15186 {
15187 u_eval_tree(curbuf->b_u_oldhead, list);
15188 dict_add_list(dict, "entries", list);
15189 }
15190 }
15191}
15192
15193/*
15194 * "values(dict)" function
15195 */
15196 static void
15197f_values(typval_T *argvars, typval_T *rettv)
15198{
15199 dict_list(argvars, rettv, 1);
15200}
15201
15202/*
15203 * "virtcol(string)" function
15204 */
15205 static void
15206f_virtcol(typval_T *argvars, typval_T *rettv)
15207{
15208 colnr_T vcol = 0;
15209 pos_T *fp;
15210 int fnum = curbuf->b_fnum;
15211
15212 fp = var2fpos(&argvars[0], FALSE, &fnum);
15213 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15214 && fnum == curbuf->b_fnum)
15215 {
15216 getvvcol(curwin, fp, NULL, NULL, &vcol);
15217 ++vcol;
15218 }
15219
15220 rettv->vval.v_number = vcol;
15221}
15222
15223/*
15224 * "visualmode()" function
15225 */
15226 static void
15227f_visualmode(typval_T *argvars, typval_T *rettv)
15228{
15229 char_u str[2];
15230
15231 rettv->v_type = VAR_STRING;
15232 str[0] = curbuf->b_visual_mode_eval;
15233 str[1] = NUL;
15234 rettv->vval.v_string = vim_strsave(str);
15235
15236 /* A non-zero number or non-empty string argument: reset mode. */
15237 if (non_zero_arg(&argvars[0]))
15238 curbuf->b_visual_mode_eval = NUL;
15239}
15240
15241/*
15242 * "wildmenumode()" function
15243 */
15244 static void
15245f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15246{
15247#ifdef FEAT_WILDMENU
15248 if (wild_menu_showing)
15249 rettv->vval.v_number = 1;
15250#endif
15251}
15252
15253/*
15254 * "winbufnr(nr)" function
15255 */
15256 static void
15257f_winbufnr(typval_T *argvars, typval_T *rettv)
15258{
15259 win_T *wp;
15260
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015261 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015262 if (wp == NULL)
15263 rettv->vval.v_number = -1;
15264 else
15265 rettv->vval.v_number = wp->w_buffer->b_fnum;
15266}
15267
15268/*
15269 * "wincol()" function
15270 */
15271 static void
15272f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15273{
15274 validate_cursor();
15275 rettv->vval.v_number = curwin->w_wcol + 1;
15276}
15277
15278/*
15279 * "winheight(nr)" function
15280 */
15281 static void
15282f_winheight(typval_T *argvars, typval_T *rettv)
15283{
15284 win_T *wp;
15285
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015286 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287 if (wp == NULL)
15288 rettv->vval.v_number = -1;
15289 else
15290 rettv->vval.v_number = wp->w_height;
15291}
15292
15293/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015294 * "winlayout()" function
15295 */
15296 static void
15297f_winlayout(typval_T *argvars, typval_T *rettv)
15298{
15299 tabpage_T *tp;
15300
15301 if (rettv_list_alloc(rettv) != OK)
15302 return;
15303
15304 if (argvars[0].v_type == VAR_UNKNOWN)
15305 tp = curtab;
15306 else
15307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015308 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015309 if (tp == NULL)
15310 return;
15311 }
15312
15313 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15314}
15315
15316/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015317 * "winline()" function
15318 */
15319 static void
15320f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15321{
15322 validate_cursor();
15323 rettv->vval.v_number = curwin->w_wrow + 1;
15324}
15325
15326/*
15327 * "winnr()" function
15328 */
15329 static void
15330f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15331{
15332 int nr = 1;
15333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015334 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015335 rettv->vval.v_number = nr;
15336}
15337
15338/*
15339 * "winrestcmd()" function
15340 */
15341 static void
15342f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15343{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015344 win_T *wp;
15345 int winnr = 1;
15346 garray_T ga;
15347 char_u buf[50];
15348
15349 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015350 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015351 {
15352 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15353 ga_concat(&ga, buf);
15354 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15355 ga_concat(&ga, buf);
15356 ++winnr;
15357 }
15358 ga_append(&ga, NUL);
15359
15360 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015361 rettv->v_type = VAR_STRING;
15362}
15363
15364/*
15365 * "winrestview()" function
15366 */
15367 static void
15368f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15369{
15370 dict_T *dict;
15371
15372 if (argvars[0].v_type != VAR_DICT
15373 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015374 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015375 else
15376 {
15377 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015378 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015379 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015380 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015381 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015382 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015383 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15384 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015385 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386 curwin->w_set_curswant = FALSE;
15387 }
15388
15389 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015390 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391#ifdef FEAT_DIFF
15392 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015393 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015394#endif
15395 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015396 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015397 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015398 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015399
15400 check_cursor();
15401 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015402 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015403 changed_window_setting();
15404
15405 if (curwin->w_topline <= 0)
15406 curwin->w_topline = 1;
15407 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15408 curwin->w_topline = curbuf->b_ml.ml_line_count;
15409#ifdef FEAT_DIFF
15410 check_topfill(curwin, TRUE);
15411#endif
15412 }
15413}
15414
15415/*
15416 * "winsaveview()" function
15417 */
15418 static void
15419f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15420{
15421 dict_T *dict;
15422
15423 if (rettv_dict_alloc(rettv) == FAIL)
15424 return;
15425 dict = rettv->vval.v_dict;
15426
Bram Moolenaare0be1672018-07-08 16:50:37 +020015427 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15428 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015429 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015430 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015431 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015432
Bram Moolenaare0be1672018-07-08 16:50:37 +020015433 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015435 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015436#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015437 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15438 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015439}
15440
15441/*
15442 * "winwidth(nr)" function
15443 */
15444 static void
15445f_winwidth(typval_T *argvars, typval_T *rettv)
15446{
15447 win_T *wp;
15448
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015449 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015450 if (wp == NULL)
15451 rettv->vval.v_number = -1;
15452 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015453 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015454}
15455
15456/*
15457 * "wordcount()" function
15458 */
15459 static void
15460f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15461{
15462 if (rettv_dict_alloc(rettv) == FAIL)
15463 return;
15464 cursor_pos_info(rettv->vval.v_dict);
15465}
15466
15467/*
15468 * "writefile()" function
15469 */
15470 static void
15471f_writefile(typval_T *argvars, typval_T *rettv)
15472{
15473 int binary = FALSE;
15474 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015475#ifdef HAVE_FSYNC
15476 int do_fsync = p_fs;
15477#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015478 char_u *fname;
15479 FILE *fd;
15480 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015481 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015482 list_T *list = NULL;
15483 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015484
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015485 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015486 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015487 return;
15488
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015489 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015490 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015491 list = argvars[0].vval.v_list;
15492 if (list == NULL)
15493 return;
15494 for (li = list->lv_first; li != NULL; li = li->li_next)
15495 if (tv_get_string_chk(&li->li_tv) == NULL)
15496 return;
15497 }
15498 else if (argvars[0].v_type == VAR_BLOB)
15499 {
15500 blob = argvars[0].vval.v_blob;
15501 if (blob == NULL)
15502 return;
15503 }
15504 else
15505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015506 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015507 return;
15508 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015509
15510 if (argvars[2].v_type != VAR_UNKNOWN)
15511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015512 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015513
15514 if (arg2 == NULL)
15515 return;
15516 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015517 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015518 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015519 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015520#ifdef HAVE_FSYNC
15521 if (vim_strchr(arg2, 's') != NULL)
15522 do_fsync = TRUE;
15523 else if (vim_strchr(arg2, 'S') != NULL)
15524 do_fsync = FALSE;
15525#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015526 }
15527
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015528 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015529 if (fname == NULL)
15530 return;
15531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015532 /* Always open the file in binary mode, library functions have a mind of
15533 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015534 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15535 append ? APPENDBIN : WRITEBIN)) == NULL)
15536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015537 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015538 ret = -1;
15539 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015540 else if (blob)
15541 {
15542 if (write_blob(fd, blob) == FAIL)
15543 ret = -1;
15544#ifdef HAVE_FSYNC
15545 else if (do_fsync)
15546 // Ignore the error, the user wouldn't know what to do about it.
15547 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015548 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015549#endif
15550 fclose(fd);
15551 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015552 else
15553 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015554 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015555 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015556#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015557 else if (do_fsync)
15558 /* Ignore the error, the user wouldn't know what to do about it.
15559 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015560 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015561#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015562 fclose(fd);
15563 }
15564
15565 rettv->vval.v_number = ret;
15566}
15567
15568/*
15569 * "xor(expr, expr)" function
15570 */
15571 static void
15572f_xor(typval_T *argvars, typval_T *rettv)
15573{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015574 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15575 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015576}
15577
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015578#endif /* FEAT_EVAL */