blob: 1a0e113b16583e03cec18cd9e26bc50d14fdbbde [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);
152static void f_extend(typval_T *argvars, typval_T *rettv);
153static void f_feedkeys(typval_T *argvars, typval_T *rettv);
154static void f_filereadable(typval_T *argvars, typval_T *rettv);
155static void f_filewritable(typval_T *argvars, typval_T *rettv);
156static void f_filter(typval_T *argvars, typval_T *rettv);
157static void f_finddir(typval_T *argvars, typval_T *rettv);
158static void f_findfile(typval_T *argvars, typval_T *rettv);
159#ifdef FEAT_FLOAT
160static void f_float2nr(typval_T *argvars, typval_T *rettv);
161static void f_floor(typval_T *argvars, typval_T *rettv);
162static void f_fmod(typval_T *argvars, typval_T *rettv);
163#endif
164static void f_fnameescape(typval_T *argvars, typval_T *rettv);
165static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
166static void f_foldclosed(typval_T *argvars, typval_T *rettv);
167static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
168static void f_foldlevel(typval_T *argvars, typval_T *rettv);
169static void f_foldtext(typval_T *argvars, typval_T *rettv);
170static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
171static void f_foreground(typval_T *argvars, typval_T *rettv);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200172static void f_funcref(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200173static void f_function(typval_T *argvars, typval_T *rettv);
174static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
175static void f_get(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200176static void f_getbufinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200177static void f_getbufline(typval_T *argvars, typval_T *rettv);
178static void f_getbufvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100179static void f_getchangelist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200180static void f_getchar(typval_T *argvars, typval_T *rettv);
181static void f_getcharmod(typval_T *argvars, typval_T *rettv);
182static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
183static void f_getcmdline(typval_T *argvars, typval_T *rettv);
184#if defined(FEAT_CMDL_COMPL)
185static void f_getcompletion(typval_T *argvars, typval_T *rettv);
186#endif
187static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
188static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
189static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
190static void f_getcwd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200191static void f_getenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200192static void f_getfontname(typval_T *argvars, typval_T *rettv);
193static void f_getfperm(typval_T *argvars, typval_T *rettv);
194static void f_getfsize(typval_T *argvars, typval_T *rettv);
195static void f_getftime(typval_T *argvars, typval_T *rettv);
196static void f_getftype(typval_T *argvars, typval_T *rettv);
Bram Moolenaar4f505882018-02-10 21:06:32 +0100197static void f_getjumplist(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200198static void f_getline(typval_T *argvars, typval_T *rettv);
Bram Moolenaard823fa92016-08-12 16:29:27 +0200199static void f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200200static void f_getmatches(typval_T *argvars, typval_T *rettv);
201static void f_getpid(typval_T *argvars, typval_T *rettv);
202static void f_getcurpos(typval_T *argvars, typval_T *rettv);
203static void f_getpos(typval_T *argvars, typval_T *rettv);
204static void f_getqflist(typval_T *argvars, typval_T *rettv);
205static void f_getreg(typval_T *argvars, typval_T *rettv);
206static void f_getregtype(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200207static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200208static void f_gettabvar(typval_T *argvars, typval_T *rettv);
209static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100210static void f_gettagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200211static void f_getwininfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100212static void f_getwinpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200213static void f_getwinposx(typval_T *argvars, typval_T *rettv);
214static void f_getwinposy(typval_T *argvars, typval_T *rettv);
215static void f_getwinvar(typval_T *argvars, typval_T *rettv);
216static void f_glob(typval_T *argvars, typval_T *rettv);
217static void f_globpath(typval_T *argvars, typval_T *rettv);
218static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
219static void f_has(typval_T *argvars, typval_T *rettv);
220static void f_has_key(typval_T *argvars, typval_T *rettv);
221static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
222static void f_hasmapto(typval_T *argvars, typval_T *rettv);
223static void f_histadd(typval_T *argvars, typval_T *rettv);
224static void f_histdel(typval_T *argvars, typval_T *rettv);
225static void f_histget(typval_T *argvars, typval_T *rettv);
226static void f_histnr(typval_T *argvars, typval_T *rettv);
227static void f_hlID(typval_T *argvars, typval_T *rettv);
228static void f_hlexists(typval_T *argvars, typval_T *rettv);
229static void f_hostname(typval_T *argvars, typval_T *rettv);
230static void f_iconv(typval_T *argvars, typval_T *rettv);
231static void f_indent(typval_T *argvars, typval_T *rettv);
232static void f_index(typval_T *argvars, typval_T *rettv);
233static void f_input(typval_T *argvars, typval_T *rettv);
234static void f_inputdialog(typval_T *argvars, typval_T *rettv);
235static void f_inputlist(typval_T *argvars, typval_T *rettv);
236static void f_inputrestore(typval_T *argvars, typval_T *rettv);
237static void f_inputsave(typval_T *argvars, typval_T *rettv);
238static void f_inputsecret(typval_T *argvars, typval_T *rettv);
239static void f_insert(typval_T *argvars, typval_T *rettv);
240static void f_invert(typval_T *argvars, typval_T *rettv);
241static void f_isdirectory(typval_T *argvars, typval_T *rettv);
242static void f_islocked(typval_T *argvars, typval_T *rettv);
243#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200244static void f_isinf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200245static void f_isnan(typval_T *argvars, typval_T *rettv);
246#endif
247static void f_items(typval_T *argvars, typval_T *rettv);
248#ifdef FEAT_JOB_CHANNEL
249static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
250static void f_job_info(typval_T *argvars, typval_T *rettv);
251static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
252static void f_job_start(typval_T *argvars, typval_T *rettv);
253static void f_job_stop(typval_T *argvars, typval_T *rettv);
254static void f_job_status(typval_T *argvars, typval_T *rettv);
255#endif
256static void f_join(typval_T *argvars, typval_T *rettv);
257static void f_js_decode(typval_T *argvars, typval_T *rettv);
258static void f_js_encode(typval_T *argvars, typval_T *rettv);
259static void f_json_decode(typval_T *argvars, typval_T *rettv);
260static void f_json_encode(typval_T *argvars, typval_T *rettv);
261static void f_keys(typval_T *argvars, typval_T *rettv);
262static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
263static void f_len(typval_T *argvars, typval_T *rettv);
264static void f_libcall(typval_T *argvars, typval_T *rettv);
265static void f_libcallnr(typval_T *argvars, typval_T *rettv);
266static void f_line(typval_T *argvars, typval_T *rettv);
267static void f_line2byte(typval_T *argvars, typval_T *rettv);
268static void f_lispindent(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9d401282019-04-06 13:18:12 +0200269static void f_list2str(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200270static void f_localtime(typval_T *argvars, typval_T *rettv);
271#ifdef FEAT_FLOAT
272static void f_log(typval_T *argvars, typval_T *rettv);
273static void f_log10(typval_T *argvars, typval_T *rettv);
274#endif
275#ifdef FEAT_LUA
276static void f_luaeval(typval_T *argvars, typval_T *rettv);
277#endif
278static void f_map(typval_T *argvars, typval_T *rettv);
279static void f_maparg(typval_T *argvars, typval_T *rettv);
280static void f_mapcheck(typval_T *argvars, typval_T *rettv);
281static void f_match(typval_T *argvars, typval_T *rettv);
282static void f_matchadd(typval_T *argvars, typval_T *rettv);
283static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
284static void f_matcharg(typval_T *argvars, typval_T *rettv);
285static void f_matchdelete(typval_T *argvars, typval_T *rettv);
286static void f_matchend(typval_T *argvars, typval_T *rettv);
287static void f_matchlist(typval_T *argvars, typval_T *rettv);
288static void f_matchstr(typval_T *argvars, typval_T *rettv);
289static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
290static void f_max(typval_T *argvars, typval_T *rettv);
291static void f_min(typval_T *argvars, typval_T *rettv);
292#ifdef vim_mkdir
293static void f_mkdir(typval_T *argvars, typval_T *rettv);
294#endif
295static void f_mode(typval_T *argvars, typval_T *rettv);
296#ifdef FEAT_MZSCHEME
297static void f_mzeval(typval_T *argvars, typval_T *rettv);
298#endif
299static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
300static void f_nr2char(typval_T *argvars, typval_T *rettv);
301static void f_or(typval_T *argvars, typval_T *rettv);
302static void f_pathshorten(typval_T *argvars, typval_T *rettv);
303#ifdef FEAT_PERL
304static void f_perleval(typval_T *argvars, typval_T *rettv);
305#endif
306#ifdef FEAT_FLOAT
307static void f_pow(typval_T *argvars, typval_T *rettv);
308#endif
309static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
310static void f_printf(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200311#ifdef FEAT_JOB_CHANNEL
312static void f_prompt_setcallback(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200313static void f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf2732452018-06-03 14:47:35 +0200314static void f_prompt_setprompt(typval_T *argvars, typval_T *rettv);
315#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200316static void f_pumvisible(typval_T *argvars, typval_T *rettv);
317#ifdef FEAT_PYTHON3
318static void f_py3eval(typval_T *argvars, typval_T *rettv);
319#endif
320#ifdef FEAT_PYTHON
321static void f_pyeval(typval_T *argvars, typval_T *rettv);
322#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100323#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
324static void f_pyxeval(typval_T *argvars, typval_T *rettv);
325#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200326static void f_range(typval_T *argvars, typval_T *rettv);
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200327static void f_readdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200328static void f_readfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200329static void f_reg_executing(typval_T *argvars, typval_T *rettv);
330static void f_reg_recording(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200331static void f_reltime(typval_T *argvars, typval_T *rettv);
332#ifdef FEAT_FLOAT
333static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
334#endif
335static void f_reltimestr(typval_T *argvars, typval_T *rettv);
336static void f_remote_expr(typval_T *argvars, typval_T *rettv);
337static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
338static void f_remote_peek(typval_T *argvars, typval_T *rettv);
339static void f_remote_read(typval_T *argvars, typval_T *rettv);
340static void f_remote_send(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100341static void f_remote_startserver(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200342static void f_remove(typval_T *argvars, typval_T *rettv);
343static void f_rename(typval_T *argvars, typval_T *rettv);
344static void f_repeat(typval_T *argvars, typval_T *rettv);
345static void f_resolve(typval_T *argvars, typval_T *rettv);
346static void f_reverse(typval_T *argvars, typval_T *rettv);
347#ifdef FEAT_FLOAT
348static void f_round(typval_T *argvars, typval_T *rettv);
349#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100350#ifdef FEAT_RUBY
351static void f_rubyeval(typval_T *argvars, typval_T *rettv);
352#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200353static void f_screenattr(typval_T *argvars, typval_T *rettv);
354static void f_screenchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100355static void f_screenchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200356static void f_screencol(typval_T *argvars, typval_T *rettv);
357static void f_screenrow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100358static void f_screenstring(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200359static void f_search(typval_T *argvars, typval_T *rettv);
360static void f_searchdecl(typval_T *argvars, typval_T *rettv);
361static void f_searchpair(typval_T *argvars, typval_T *rettv);
362static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
363static void f_searchpos(typval_T *argvars, typval_T *rettv);
364static void f_server2client(typval_T *argvars, typval_T *rettv);
365static void f_serverlist(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200366static void f_setbufline(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200367static void f_setbufvar(typval_T *argvars, typval_T *rettv);
368static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
369static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200370static void f_setenv(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200371static void f_setfperm(typval_T *argvars, typval_T *rettv);
372static void f_setline(typval_T *argvars, typval_T *rettv);
373static void f_setloclist(typval_T *argvars, typval_T *rettv);
374static void f_setmatches(typval_T *argvars, typval_T *rettv);
375static void f_setpos(typval_T *argvars, typval_T *rettv);
376static void f_setqflist(typval_T *argvars, typval_T *rettv);
377static void f_setreg(typval_T *argvars, typval_T *rettv);
378static void f_settabvar(typval_T *argvars, typval_T *rettv);
379static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100380static void f_settagstack(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200381static void f_setwinvar(typval_T *argvars, typval_T *rettv);
382#ifdef FEAT_CRYPT
383static void f_sha256(typval_T *argvars, typval_T *rettv);
384#endif /* FEAT_CRYPT */
385static void f_shellescape(typval_T *argvars, typval_T *rettv);
386static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100387#ifdef FEAT_SIGNS
388static void f_sign_define(typval_T *argvars, typval_T *rettv);
389static void f_sign_getdefined(typval_T *argvars, typval_T *rettv);
390static void f_sign_getplaced(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100391static void f_sign_jump(typval_T *argvars, typval_T *rettv);
Bram Moolenaar162b7142018-12-21 15:17:36 +0100392static void f_sign_place(typval_T *argvars, typval_T *rettv);
393static void f_sign_undefine(typval_T *argvars, typval_T *rettv);
394static void f_sign_unplace(typval_T *argvars, typval_T *rettv);
395#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200396static void f_simplify(typval_T *argvars, typval_T *rettv);
397#ifdef FEAT_FLOAT
398static void f_sin(typval_T *argvars, typval_T *rettv);
399static void f_sinh(typval_T *argvars, typval_T *rettv);
400#endif
401static void f_sort(typval_T *argvars, typval_T *rettv);
402static void f_soundfold(typval_T *argvars, typval_T *rettv);
403static void f_spellbadword(typval_T *argvars, typval_T *rettv);
404static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
405static void f_split(typval_T *argvars, typval_T *rettv);
406#ifdef FEAT_FLOAT
407static void f_sqrt(typval_T *argvars, typval_T *rettv);
408static void f_str2float(typval_T *argvars, typval_T *rettv);
409#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200410static void f_str2list(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200411static void f_str2nr(typval_T *argvars, typval_T *rettv);
412static void f_strchars(typval_T *argvars, typval_T *rettv);
413#ifdef HAVE_STRFTIME
414static void f_strftime(typval_T *argvars, typval_T *rettv);
415#endif
416static void f_strgetchar(typval_T *argvars, typval_T *rettv);
417static void f_stridx(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200418static void f_strlen(typval_T *argvars, typval_T *rettv);
419static void f_strcharpart(typval_T *argvars, typval_T *rettv);
420static void f_strpart(typval_T *argvars, typval_T *rettv);
421static void f_strridx(typval_T *argvars, typval_T *rettv);
422static void f_strtrans(typval_T *argvars, typval_T *rettv);
423static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
424static void f_strwidth(typval_T *argvars, typval_T *rettv);
425static void f_submatch(typval_T *argvars, typval_T *rettv);
426static void f_substitute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200427static void f_swapinfo(typval_T *argvars, typval_T *rettv);
Bram Moolenaar110bd602018-09-16 18:46:59 +0200428static void f_swapname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200429static void f_synID(typval_T *argvars, typval_T *rettv);
430static void f_synIDattr(typval_T *argvars, typval_T *rettv);
431static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
432static void f_synstack(typval_T *argvars, typval_T *rettv);
433static void f_synconcealed(typval_T *argvars, typval_T *rettv);
434static void f_system(typval_T *argvars, typval_T *rettv);
435static void f_systemlist(typval_T *argvars, typval_T *rettv);
436static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
437static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
438static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
439static void f_taglist(typval_T *argvars, typval_T *rettv);
440static void f_tagfiles(typval_T *argvars, typval_T *rettv);
441static void f_tempname(typval_T *argvars, typval_T *rettv);
442static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
443static void f_test_autochdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200444static void f_test_feedinput(typval_T *argvars, typval_T *rettv);
Bram Moolenaareda65222019-05-16 20:29:44 +0200445static void f_test_getvalue(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +0200446static void f_test_option_not_set(typval_T *argvars, typval_T *rettv);
Bram Moolenaareb992cb2017-03-09 18:20:16 +0100447static void f_test_override(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100448static void f_test_refcount(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200449static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100450static void f_test_ignore_error(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100451static void f_test_null_blob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200452#ifdef FEAT_JOB_CHANNEL
453static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
454#endif
455static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
456#ifdef FEAT_JOB_CHANNEL
457static void f_test_null_job(typval_T *argvars, typval_T *rettv);
458#endif
459static void f_test_null_list(typval_T *argvars, typval_T *rettv);
460static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
461static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaarab186732018-09-14 21:27:06 +0200462#ifdef FEAT_GUI
463static void f_test_scrollbar(typval_T *argvars, typval_T *rettv);
464#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200465#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +0200466static void f_test_setmouse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +0200467#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200468static void f_test_settime(typval_T *argvars, typval_T *rettv);
469#ifdef FEAT_FLOAT
470static void f_tan(typval_T *argvars, typval_T *rettv);
471static void f_tanh(typval_T *argvars, typval_T *rettv);
472#endif
473#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +0200474static void f_timer_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200475static void f_timer_pause(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200476static void f_timer_start(typval_T *argvars, typval_T *rettv);
477static void f_timer_stop(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb73598e2016-08-07 18:22:53 +0200478static void f_timer_stopall(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200479#endif
480static void f_tolower(typval_T *argvars, typval_T *rettv);
481static void f_toupper(typval_T *argvars, typval_T *rettv);
482static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +0100483static void f_trim(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200484#ifdef FEAT_FLOAT
485static void f_trunc(typval_T *argvars, typval_T *rettv);
486#endif
487static void f_type(typval_T *argvars, typval_T *rettv);
488static void f_undofile(typval_T *argvars, typval_T *rettv);
489static void f_undotree(typval_T *argvars, typval_T *rettv);
490static void f_uniq(typval_T *argvars, typval_T *rettv);
491static void f_values(typval_T *argvars, typval_T *rettv);
492static void f_virtcol(typval_T *argvars, typval_T *rettv);
493static void f_visualmode(typval_T *argvars, typval_T *rettv);
494static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
495static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
496static void f_win_getid(typval_T *argvars, typval_T *rettv);
497static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
498static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
499static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100500static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200501static void f_winbufnr(typval_T *argvars, typval_T *rettv);
502static void f_wincol(typval_T *argvars, typval_T *rettv);
503static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200504static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200505static void f_winline(typval_T *argvars, typval_T *rettv);
506static void f_winnr(typval_T *argvars, typval_T *rettv);
507static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
508static void f_winrestview(typval_T *argvars, typval_T *rettv);
509static void f_winsaveview(typval_T *argvars, typval_T *rettv);
510static void f_winwidth(typval_T *argvars, typval_T *rettv);
511static void f_writefile(typval_T *argvars, typval_T *rettv);
512static void f_wordcount(typval_T *argvars, typval_T *rettv);
513static void f_xor(typval_T *argvars, typval_T *rettv);
514
515/*
516 * Array with names and number of arguments of all internal functions
517 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
518 */
519static struct fst
520{
521 char *f_name; /* function name */
522 char f_min_argc; /* minimal number of arguments */
523 char f_max_argc; /* maximal number of arguments */
524 void (*f_func)(typval_T *args, typval_T *rvar);
525 /* implementation of function */
526} functions[] =
527{
528#ifdef FEAT_FLOAT
529 {"abs", 1, 1, f_abs},
530 {"acos", 1, 1, f_acos}, /* WJMc */
531#endif
532 {"add", 2, 2, f_add},
533 {"and", 2, 2, f_and},
534 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200535 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200536 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200537 {"argidx", 0, 0, f_argidx},
538 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200539 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200540#ifdef FEAT_FLOAT
541 {"asin", 1, 1, f_asin}, /* WJMc */
542#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100543 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200544 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100545 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200546 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200547 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200548 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100549 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200550 {"assert_match", 2, 3, f_assert_match},
551 {"assert_notequal", 2, 3, f_assert_notequal},
552 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100553 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200554 {"assert_true", 1, 2, f_assert_true},
555#ifdef FEAT_FLOAT
556 {"atan", 1, 1, f_atan},
557 {"atan2", 2, 2, f_atan2},
558#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100559#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200560 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100561 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100562# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100563 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100564# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100565#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200566 {"browse", 4, 4, f_browse},
567 {"browsedir", 2, 2, f_browsedir},
568 {"bufexists", 1, 1, f_bufexists},
569 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
570 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
571 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
572 {"buflisted", 1, 1, f_buflisted},
573 {"bufloaded", 1, 1, f_bufloaded},
574 {"bufname", 1, 1, f_bufname},
575 {"bufnr", 1, 2, f_bufnr},
576 {"bufwinid", 1, 1, f_bufwinid},
577 {"bufwinnr", 1, 1, f_bufwinnr},
578 {"byte2line", 1, 1, f_byte2line},
579 {"byteidx", 2, 2, f_byteidx},
580 {"byteidxcomp", 2, 2, f_byteidxcomp},
581 {"call", 2, 3, f_call},
582#ifdef FEAT_FLOAT
583 {"ceil", 1, 1, f_ceil},
584#endif
585#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100586 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200587 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200588 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200589 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
590 {"ch_evalraw", 2, 3, f_ch_evalraw},
591 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
592 {"ch_getjob", 1, 1, f_ch_getjob},
593 {"ch_info", 1, 1, f_ch_info},
594 {"ch_log", 1, 2, f_ch_log},
595 {"ch_logfile", 1, 2, f_ch_logfile},
596 {"ch_open", 1, 2, f_ch_open},
597 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100598 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200599 {"ch_readraw", 1, 2, f_ch_readraw},
600 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
601 {"ch_sendraw", 2, 3, f_ch_sendraw},
602 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200603 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200604#endif
605 {"changenr", 0, 0, f_changenr},
606 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200607 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200608 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100609 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200610 {"col", 1, 1, f_col},
611#if defined(FEAT_INS_EXPAND)
612 {"complete", 2, 2, f_complete},
613 {"complete_add", 1, 1, f_complete_add},
614 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100615 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200616#endif
617 {"confirm", 1, 4, f_confirm},
618 {"copy", 1, 1, f_copy},
619#ifdef FEAT_FLOAT
620 {"cos", 1, 1, f_cos},
621 {"cosh", 1, 1, f_cosh},
622#endif
623 {"count", 2, 4, f_count},
624 {"cscope_connection",0,3, f_cscope_connection},
625 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100626#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200627 {"debugbreak", 1, 1, f_debugbreak},
628#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200629 {"deepcopy", 1, 2, f_deepcopy},
630 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200631 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200632 {"did_filetype", 0, 0, f_did_filetype},
633 {"diff_filler", 1, 1, f_diff_filler},
634 {"diff_hlID", 2, 2, f_diff_hlID},
635 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200636 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200637 {"escape", 2, 2, f_escape},
638 {"eval", 1, 1, f_eval},
639 {"eventhandler", 0, 0, f_eventhandler},
640 {"executable", 1, 1, f_executable},
641 {"execute", 1, 2, f_execute},
642 {"exepath", 1, 1, f_exepath},
643 {"exists", 1, 1, f_exists},
644#ifdef FEAT_FLOAT
645 {"exp", 1, 1, f_exp},
646#endif
647 {"expand", 1, 3, f_expand},
648 {"extend", 2, 3, f_extend},
649 {"feedkeys", 1, 2, f_feedkeys},
650 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
651 {"filereadable", 1, 1, f_filereadable},
652 {"filewritable", 1, 1, f_filewritable},
653 {"filter", 2, 2, f_filter},
654 {"finddir", 1, 3, f_finddir},
655 {"findfile", 1, 3, f_findfile},
656#ifdef FEAT_FLOAT
657 {"float2nr", 1, 1, f_float2nr},
658 {"floor", 1, 1, f_floor},
659 {"fmod", 2, 2, f_fmod},
660#endif
661 {"fnameescape", 1, 1, f_fnameescape},
662 {"fnamemodify", 2, 2, f_fnamemodify},
663 {"foldclosed", 1, 1, f_foldclosed},
664 {"foldclosedend", 1, 1, f_foldclosedend},
665 {"foldlevel", 1, 1, f_foldlevel},
666 {"foldtext", 0, 0, f_foldtext},
667 {"foldtextresult", 1, 1, f_foldtextresult},
668 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200669 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200670 {"function", 1, 3, f_function},
671 {"garbagecollect", 0, 1, f_garbagecollect},
672 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200673 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200674 {"getbufline", 2, 3, f_getbufline},
675 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100676 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200677 {"getchar", 0, 1, f_getchar},
678 {"getcharmod", 0, 0, f_getcharmod},
679 {"getcharsearch", 0, 0, f_getcharsearch},
680 {"getcmdline", 0, 0, f_getcmdline},
681 {"getcmdpos", 0, 0, f_getcmdpos},
682 {"getcmdtype", 0, 0, f_getcmdtype},
683 {"getcmdwintype", 0, 0, f_getcmdwintype},
684#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200685 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200686#endif
687 {"getcurpos", 0, 0, f_getcurpos},
688 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200689 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200690 {"getfontname", 0, 1, f_getfontname},
691 {"getfperm", 1, 1, f_getfperm},
692 {"getfsize", 1, 1, f_getfsize},
693 {"getftime", 1, 1, f_getftime},
694 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100695 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200696 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200697 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100698 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200699 {"getpid", 0, 0, f_getpid},
700 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200701 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200702 {"getreg", 0, 3, f_getreg},
703 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200704 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200705 {"gettabvar", 2, 3, f_gettabvar},
706 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100707 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200708 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100709 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200710 {"getwinposx", 0, 0, f_getwinposx},
711 {"getwinposy", 0, 0, f_getwinposy},
712 {"getwinvar", 2, 3, f_getwinvar},
713 {"glob", 1, 4, f_glob},
714 {"glob2regpat", 1, 1, f_glob2regpat},
715 {"globpath", 2, 5, f_globpath},
716 {"has", 1, 1, f_has},
717 {"has_key", 2, 2, f_has_key},
718 {"haslocaldir", 0, 2, f_haslocaldir},
719 {"hasmapto", 1, 3, f_hasmapto},
720 {"highlightID", 1, 1, f_hlID}, /* obsolete */
721 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
722 {"histadd", 2, 2, f_histadd},
723 {"histdel", 1, 2, f_histdel},
724 {"histget", 1, 2, f_histget},
725 {"histnr", 1, 1, f_histnr},
726 {"hlID", 1, 1, f_hlID},
727 {"hlexists", 1, 1, f_hlexists},
728 {"hostname", 0, 0, f_hostname},
729 {"iconv", 3, 3, f_iconv},
730 {"indent", 1, 1, f_indent},
731 {"index", 2, 4, f_index},
732 {"input", 1, 3, f_input},
733 {"inputdialog", 1, 3, f_inputdialog},
734 {"inputlist", 1, 1, f_inputlist},
735 {"inputrestore", 0, 0, f_inputrestore},
736 {"inputsave", 0, 0, f_inputsave},
737 {"inputsecret", 1, 2, f_inputsecret},
738 {"insert", 2, 3, f_insert},
739 {"invert", 1, 1, f_invert},
740 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200741#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
742 {"isinf", 1, 1, f_isinf},
743#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200744 {"islocked", 1, 1, f_islocked},
745#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
746 {"isnan", 1, 1, f_isnan},
747#endif
748 {"items", 1, 1, f_items},
749#ifdef FEAT_JOB_CHANNEL
750 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200751 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200752 {"job_setoptions", 2, 2, f_job_setoptions},
753 {"job_start", 1, 2, f_job_start},
754 {"job_status", 1, 1, f_job_status},
755 {"job_stop", 1, 2, f_job_stop},
756#endif
757 {"join", 1, 2, f_join},
758 {"js_decode", 1, 1, f_js_decode},
759 {"js_encode", 1, 1, f_js_encode},
760 {"json_decode", 1, 1, f_json_decode},
761 {"json_encode", 1, 1, f_json_encode},
762 {"keys", 1, 1, f_keys},
763 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
764 {"len", 1, 1, f_len},
765 {"libcall", 3, 3, f_libcall},
766 {"libcallnr", 3, 3, f_libcallnr},
767 {"line", 1, 1, f_line},
768 {"line2byte", 1, 1, f_line2byte},
769 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200770 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200771 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200772 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200773 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200774 {"localtime", 0, 0, f_localtime},
775#ifdef FEAT_FLOAT
776 {"log", 1, 1, f_log},
777 {"log10", 1, 1, f_log10},
778#endif
779#ifdef FEAT_LUA
780 {"luaeval", 1, 2, f_luaeval},
781#endif
782 {"map", 2, 2, f_map},
783 {"maparg", 1, 4, f_maparg},
784 {"mapcheck", 1, 3, f_mapcheck},
785 {"match", 2, 4, f_match},
786 {"matchadd", 2, 5, f_matchadd},
787 {"matchaddpos", 2, 5, f_matchaddpos},
788 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100789 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200790 {"matchend", 2, 4, f_matchend},
791 {"matchlist", 2, 4, f_matchlist},
792 {"matchstr", 2, 4, f_matchstr},
793 {"matchstrpos", 2, 4, f_matchstrpos},
794 {"max", 1, 1, f_max},
795 {"min", 1, 1, f_min},
796#ifdef vim_mkdir
797 {"mkdir", 1, 3, f_mkdir},
798#endif
799 {"mode", 0, 1, f_mode},
800#ifdef FEAT_MZSCHEME
801 {"mzeval", 1, 1, f_mzeval},
802#endif
803 {"nextnonblank", 1, 1, f_nextnonblank},
804 {"nr2char", 1, 2, f_nr2char},
805 {"or", 2, 2, f_or},
806 {"pathshorten", 1, 1, f_pathshorten},
807#ifdef FEAT_PERL
808 {"perleval", 1, 1, f_perleval},
809#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200810#ifdef FEAT_TEXT_PROP
811 {"popup_close", 1, 1, f_popup_close},
812 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaarbc133542019-05-29 20:26:46 +0200813 {"popup_getposition", 1, 1, f_popup_getposition},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200814 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200815 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200816 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200818#ifdef FEAT_FLOAT
819 {"pow", 2, 2, f_pow},
820#endif
821 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100822 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200823#ifdef FEAT_JOB_CHANNEL
824 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200825 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200826 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
827#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100828#ifdef FEAT_TEXT_PROP
829 {"prop_add", 3, 3, f_prop_add},
830 {"prop_clear", 1, 3, f_prop_clear},
831 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100832 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100833 {"prop_type_add", 2, 2, f_prop_type_add},
834 {"prop_type_change", 2, 2, f_prop_type_change},
835 {"prop_type_delete", 1, 2, f_prop_type_delete},
836 {"prop_type_get", 1, 2, f_prop_type_get},
837 {"prop_type_list", 0, 1, f_prop_type_list},
838#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200839 {"pumvisible", 0, 0, f_pumvisible},
840#ifdef FEAT_PYTHON3
841 {"py3eval", 1, 1, f_py3eval},
842#endif
843#ifdef FEAT_PYTHON
844 {"pyeval", 1, 1, f_pyeval},
845#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100846#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
847 {"pyxeval", 1, 1, f_pyxeval},
848#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200849 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200850 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200851 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200852 {"reg_executing", 0, 0, f_reg_executing},
853 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"reltime", 0, 2, f_reltime},
855#ifdef FEAT_FLOAT
856 {"reltimefloat", 1, 1, f_reltimefloat},
857#endif
858 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100859 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200860 {"remote_foreground", 1, 1, f_remote_foreground},
861 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100862 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100864 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200865 {"remove", 2, 3, f_remove},
866 {"rename", 2, 2, f_rename},
867 {"repeat", 2, 2, f_repeat},
868 {"resolve", 1, 1, f_resolve},
869 {"reverse", 1, 1, f_reverse},
870#ifdef FEAT_FLOAT
871 {"round", 1, 1, f_round},
872#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100873#ifdef FEAT_RUBY
874 {"rubyeval", 1, 1, f_rubyeval},
875#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200876 {"screenattr", 2, 2, f_screenattr},
877 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100878 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"screencol", 0, 0, f_screencol},
880 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100881 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"search", 1, 4, f_search},
883 {"searchdecl", 1, 3, f_searchdecl},
884 {"searchpair", 3, 7, f_searchpair},
885 {"searchpairpos", 3, 7, f_searchpairpos},
886 {"searchpos", 1, 4, f_searchpos},
887 {"server2client", 2, 2, f_server2client},
888 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200889 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890 {"setbufvar", 3, 3, f_setbufvar},
891 {"setcharsearch", 1, 1, f_setcharsearch},
892 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200893 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200894 {"setfperm", 2, 2, f_setfperm},
895 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200896 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100897 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200898 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200899 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200900 {"setreg", 2, 3, f_setreg},
901 {"settabvar", 3, 3, f_settabvar},
902 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100903 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200904 {"setwinvar", 3, 3, f_setwinvar},
905#ifdef FEAT_CRYPT
906 {"sha256", 1, 1, f_sha256},
907#endif
908 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100909 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100910#ifdef FEAT_SIGNS
911 {"sign_define", 1, 2, f_sign_define},
912 {"sign_getdefined", 0, 1, f_sign_getdefined},
913 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100914 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100915 {"sign_place", 4, 5, f_sign_place},
916 {"sign_undefine", 0, 1, f_sign_undefine},
917 {"sign_unplace", 1, 2, f_sign_unplace},
918#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200919 {"simplify", 1, 1, f_simplify},
920#ifdef FEAT_FLOAT
921 {"sin", 1, 1, f_sin},
922 {"sinh", 1, 1, f_sinh},
923#endif
924 {"sort", 1, 3, f_sort},
925 {"soundfold", 1, 1, f_soundfold},
926 {"spellbadword", 0, 1, f_spellbadword},
927 {"spellsuggest", 1, 3, f_spellsuggest},
928 {"split", 1, 3, f_split},
929#ifdef FEAT_FLOAT
930 {"sqrt", 1, 1, f_sqrt},
931 {"str2float", 1, 1, f_str2float},
932#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200933 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200934 {"str2nr", 1, 2, f_str2nr},
935 {"strcharpart", 2, 3, f_strcharpart},
936 {"strchars", 1, 2, f_strchars},
937 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
938#ifdef HAVE_STRFTIME
939 {"strftime", 1, 2, f_strftime},
940#endif
941 {"strgetchar", 2, 2, f_strgetchar},
942 {"stridx", 2, 3, f_stridx},
943 {"string", 1, 1, f_string},
944 {"strlen", 1, 1, f_strlen},
945 {"strpart", 2, 3, f_strpart},
946 {"strridx", 2, 3, f_strridx},
947 {"strtrans", 1, 1, f_strtrans},
948 {"strwidth", 1, 1, f_strwidth},
949 {"submatch", 1, 2, f_submatch},
950 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200951 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200952 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200953 {"synID", 3, 3, f_synID},
954 {"synIDattr", 2, 3, f_synIDattr},
955 {"synIDtrans", 1, 1, f_synIDtrans},
956 {"synconcealed", 2, 2, f_synconcealed},
957 {"synstack", 2, 2, f_synstack},
958 {"system", 1, 2, f_system},
959 {"systemlist", 1, 2, f_systemlist},
960 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
961 {"tabpagenr", 0, 1, f_tabpagenr},
962 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
963 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100964 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200965#ifdef FEAT_FLOAT
966 {"tan", 1, 1, f_tan},
967 {"tanh", 1, 1, f_tanh},
968#endif
969 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100971 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
972 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100973 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200974 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200975# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
976 {"term_getansicolors", 1, 1, f_term_getansicolors},
977# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200979 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200980 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200981 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200982 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200984 {"term_getstatus", 1, 1, f_term_getstatus},
985 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200986 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200987 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200988 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200989 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200990# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
991 {"term_setansicolors", 2, 2, f_term_setansicolors},
992# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100993 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100994 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200995 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200996 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200997 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200998#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200999 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1000 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001001 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001002 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001003 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001004 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001005 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001006#ifdef FEAT_JOB_CHANNEL
1007 {"test_null_channel", 0, 0, f_test_null_channel},
1008#endif
1009 {"test_null_dict", 0, 0, f_test_null_dict},
1010#ifdef FEAT_JOB_CHANNEL
1011 {"test_null_job", 0, 0, f_test_null_job},
1012#endif
1013 {"test_null_list", 0, 0, f_test_null_list},
1014 {"test_null_partial", 0, 0, f_test_null_partial},
1015 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001016 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001017 {"test_override", 2, 2, f_test_override},
1018 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001019#ifdef FEAT_GUI
1020 {"test_scrollbar", 3, 3, f_test_scrollbar},
1021#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001022#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001023 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001024#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001025 {"test_settime", 1, 1, f_test_settime},
1026#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001027 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001028 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001029 {"timer_start", 2, 3, f_timer_start},
1030 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001031 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001032#endif
1033 {"tolower", 1, 1, f_tolower},
1034 {"toupper", 1, 1, f_toupper},
1035 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001036 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001037#ifdef FEAT_FLOAT
1038 {"trunc", 1, 1, f_trunc},
1039#endif
1040 {"type", 1, 1, f_type},
1041 {"undofile", 1, 1, f_undofile},
1042 {"undotree", 0, 0, f_undotree},
1043 {"uniq", 1, 3, f_uniq},
1044 {"values", 1, 1, f_values},
1045 {"virtcol", 1, 1, f_virtcol},
1046 {"visualmode", 0, 1, f_visualmode},
1047 {"wildmenumode", 0, 0, f_wildmenumode},
1048 {"win_findbuf", 1, 1, f_win_findbuf},
1049 {"win_getid", 0, 2, f_win_getid},
1050 {"win_gotoid", 1, 1, f_win_gotoid},
1051 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1052 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001053 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001054 {"winbufnr", 1, 1, f_winbufnr},
1055 {"wincol", 0, 0, f_wincol},
1056 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001057 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058 {"winline", 0, 0, f_winline},
1059 {"winnr", 0, 1, f_winnr},
1060 {"winrestcmd", 0, 0, f_winrestcmd},
1061 {"winrestview", 1, 1, f_winrestview},
1062 {"winsaveview", 0, 0, f_winsaveview},
1063 {"winwidth", 1, 1, f_winwidth},
1064 {"wordcount", 0, 0, f_wordcount},
1065 {"writefile", 2, 3, f_writefile},
1066 {"xor", 2, 2, f_xor},
1067};
1068
1069#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1070
1071/*
1072 * Function given to ExpandGeneric() to obtain the list of internal
1073 * or user defined function names.
1074 */
1075 char_u *
1076get_function_name(expand_T *xp, int idx)
1077{
1078 static int intidx = -1;
1079 char_u *name;
1080
1081 if (idx == 0)
1082 intidx = -1;
1083 if (intidx < 0)
1084 {
1085 name = get_user_func_name(xp, idx);
1086 if (name != NULL)
1087 return name;
1088 }
1089 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1090 {
1091 STRCPY(IObuff, functions[intidx].f_name);
1092 STRCAT(IObuff, "(");
1093 if (functions[intidx].f_max_argc == 0)
1094 STRCAT(IObuff, ")");
1095 return IObuff;
1096 }
1097
1098 return NULL;
1099}
1100
1101/*
1102 * Function given to ExpandGeneric() to obtain the list of internal or
1103 * user defined variable or function names.
1104 */
1105 char_u *
1106get_expr_name(expand_T *xp, int idx)
1107{
1108 static int intidx = -1;
1109 char_u *name;
1110
1111 if (idx == 0)
1112 intidx = -1;
1113 if (intidx < 0)
1114 {
1115 name = get_function_name(xp, idx);
1116 if (name != NULL)
1117 return name;
1118 }
1119 return get_user_var_name(xp, ++intidx);
1120}
1121
1122#endif /* FEAT_CMDL_COMPL */
1123
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001124/*
1125 * Find internal function in table above.
1126 * Return index, or -1 if not found
1127 */
1128 int
1129find_internal_func(
1130 char_u *name) /* name of the function */
1131{
1132 int first = 0;
1133 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1134 int cmp;
1135 int x;
1136
1137 /*
1138 * Find the function name in the table. Binary search.
1139 */
1140 while (first <= last)
1141 {
1142 x = first + ((unsigned)(last - first) >> 1);
1143 cmp = STRCMP(name, functions[x].f_name);
1144 if (cmp < 0)
1145 last = x - 1;
1146 else if (cmp > 0)
1147 first = x + 1;
1148 else
1149 return x;
1150 }
1151 return -1;
1152}
1153
1154 int
1155call_internal_func(
1156 char_u *name,
1157 int argcount,
1158 typval_T *argvars,
1159 typval_T *rettv)
1160{
1161 int i;
1162
1163 i = find_internal_func(name);
1164 if (i < 0)
1165 return ERROR_UNKNOWN;
1166 if (argcount < functions[i].f_min_argc)
1167 return ERROR_TOOFEW;
1168 if (argcount > functions[i].f_max_argc)
1169 return ERROR_TOOMANY;
1170 argvars[argcount].v_type = VAR_UNKNOWN;
1171 functions[i].f_func(argvars, rettv);
1172 return ERROR_NONE;
1173}
1174
1175/*
1176 * Return TRUE for a non-zero Number and a non-empty String.
1177 */
1178 static int
1179non_zero_arg(typval_T *argvars)
1180{
1181 return ((argvars[0].v_type == VAR_NUMBER
1182 && argvars[0].vval.v_number != 0)
1183 || (argvars[0].v_type == VAR_SPECIAL
1184 && argvars[0].vval.v_number == VVAL_TRUE)
1185 || (argvars[0].v_type == VAR_STRING
1186 && argvars[0].vval.v_string != NULL
1187 && *argvars[0].vval.v_string != NUL));
1188}
1189
1190/*
1191 * Get the lnum from the first argument.
1192 * Also accepts ".", "$", etc., but that only works for the current buffer.
1193 * Returns -1 on error.
1194 */
1195 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001196tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001197{
1198 typval_T rettv;
1199 linenr_T lnum;
1200
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001201 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001202 if (lnum == 0) /* no valid number, try using line() */
1203 {
1204 rettv.v_type = VAR_NUMBER;
1205 f_line(argvars, &rettv);
1206 lnum = (linenr_T)rettv.vval.v_number;
1207 clear_tv(&rettv);
1208 }
1209 return lnum;
1210}
1211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001212/*
1213 * Get the lnum from the first argument.
1214 * Also accepts "$", then "buf" is used.
1215 * Returns 0 on error.
1216 */
1217 static linenr_T
1218tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1219{
1220 if (argvars[0].v_type == VAR_STRING
1221 && argvars[0].vval.v_string != NULL
1222 && argvars[0].vval.v_string[0] == '$'
1223 && buf != NULL)
1224 return buf->b_ml.ml_line_count;
1225 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1226}
1227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001228#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001229/*
1230 * Get the float value of "argvars[0]" into "f".
1231 * Returns FAIL when the argument is not a Number or Float.
1232 */
1233 static int
1234get_float_arg(typval_T *argvars, float_T *f)
1235{
1236 if (argvars[0].v_type == VAR_FLOAT)
1237 {
1238 *f = argvars[0].vval.v_float;
1239 return OK;
1240 }
1241 if (argvars[0].v_type == VAR_NUMBER)
1242 {
1243 *f = (float_T)argvars[0].vval.v_number;
1244 return OK;
1245 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001246 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001247 return FAIL;
1248}
1249
1250/*
1251 * "abs(expr)" function
1252 */
1253 static void
1254f_abs(typval_T *argvars, typval_T *rettv)
1255{
1256 if (argvars[0].v_type == VAR_FLOAT)
1257 {
1258 rettv->v_type = VAR_FLOAT;
1259 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1260 }
1261 else
1262 {
1263 varnumber_T n;
1264 int error = FALSE;
1265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001266 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001267 if (error)
1268 rettv->vval.v_number = -1;
1269 else if (n > 0)
1270 rettv->vval.v_number = n;
1271 else
1272 rettv->vval.v_number = -n;
1273 }
1274}
1275
1276/*
1277 * "acos()" function
1278 */
1279 static void
1280f_acos(typval_T *argvars, typval_T *rettv)
1281{
1282 float_T f = 0.0;
1283
1284 rettv->v_type = VAR_FLOAT;
1285 if (get_float_arg(argvars, &f) == OK)
1286 rettv->vval.v_float = acos(f);
1287 else
1288 rettv->vval.v_float = 0.0;
1289}
1290#endif
1291
1292/*
1293 * "add(list, item)" function
1294 */
1295 static void
1296f_add(typval_T *argvars, typval_T *rettv)
1297{
1298 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001299 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001300
1301 rettv->vval.v_number = 1; /* Default: Failed */
1302 if (argvars[0].v_type == VAR_LIST)
1303 {
1304 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001305 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001306 (char_u *)N_("add() argument"), TRUE)
1307 && list_append_tv(l, &argvars[1]) == OK)
1308 copy_tv(&argvars[0], rettv);
1309 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001310 else if (argvars[0].v_type == VAR_BLOB)
1311 {
1312 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001313 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 (char_u *)N_("add() argument"), TRUE))
1315 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001316 int error = FALSE;
1317 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1318
1319 if (!error)
1320 {
1321 ga_append(&b->bv_ga, (int)n);
1322 copy_tv(&argvars[0], rettv);
1323 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001324 }
1325 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001326 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001327 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328}
1329
1330/*
1331 * "and(expr, expr)" function
1332 */
1333 static void
1334f_and(typval_T *argvars, typval_T *rettv)
1335{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001336 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1337 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001338}
1339
1340/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001341 * If there is a window for "curbuf", make it the current window.
1342 */
1343 static void
1344find_win_for_curbuf(void)
1345{
1346 wininfo_T *wip;
1347
1348 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1349 {
1350 if (wip->wi_win != NULL)
1351 {
1352 curwin = wip->wi_win;
1353 break;
1354 }
1355 }
1356}
1357
1358/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001359 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360 */
1361 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001362set_buffer_lines(
1363 buf_T *buf,
1364 linenr_T lnum_arg,
1365 int append,
1366 typval_T *lines,
1367 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001368{
Bram Moolenaarca851592018-06-06 21:04:07 +02001369 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1370 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001371 list_T *l = NULL;
1372 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001374 linenr_T append_lnum;
1375 buf_T *curbuf_save = NULL;
1376 win_T *curwin_save = NULL;
1377 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001378
Bram Moolenaarca851592018-06-06 21:04:07 +02001379 /* When using the current buffer ml_mfp will be set if needed. Useful when
1380 * setline() is used on startup. For other buffers the buffer must be
1381 * loaded. */
1382 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001383 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001384 rettv->vval.v_number = 1; /* FAIL */
1385 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001386 }
1387
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001389 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001390 curbuf_save = curbuf;
1391 curwin_save = curwin;
1392 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001393 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001394 }
1395
1396 if (append)
1397 // appendbufline() uses the line number below which we insert
1398 append_lnum = lnum - 1;
1399 else
1400 // setbufline() uses the line number above which we insert, we only
1401 // append if it's below the last line
1402 append_lnum = curbuf->b_ml.ml_line_count;
1403
1404 if (lines->v_type == VAR_LIST)
1405 {
1406 l = lines->vval.v_list;
1407 li = l->lv_first;
1408 }
1409 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001410 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001411
1412 /* default result is zero == OK */
1413 for (;;)
1414 {
1415 if (l != NULL)
1416 {
1417 /* list argument, get next string */
1418 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001419 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001420 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001421 li = li->li_next;
1422 }
1423
Bram Moolenaarca851592018-06-06 21:04:07 +02001424 rettv->vval.v_number = 1; /* FAIL */
1425 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1426 break;
1427
1428 /* When coming here from Insert mode, sync undo, so that this can be
1429 * undone separately from what was previously inserted. */
1430 if (u_sync_once == 2)
1431 {
1432 u_sync_once = 1; /* notify that u_sync() was called */
1433 u_sync(TRUE);
1434 }
1435
1436 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1437 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001438 // Existing line, replace it.
1439 // Removes any existing text properties.
1440 if (u_savesub(lnum) == OK && ml_replace_len(
1441 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001442 {
1443 changed_bytes(lnum, 0);
1444 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1445 check_cursor_col();
1446 rettv->vval.v_number = 0; /* OK */
1447 }
1448 }
1449 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1450 {
1451 /* append the line */
1452 ++added;
1453 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1454 rettv->vval.v_number = 0; /* OK */
1455 }
1456
1457 if (l == NULL) /* only one string argument */
1458 break;
1459 ++lnum;
1460 }
1461
1462 if (added > 0)
1463 {
1464 win_T *wp;
1465 tabpage_T *tp;
1466
1467 appended_lines_mark(append_lnum, added);
1468 FOR_ALL_TAB_WINDOWS(tp, wp)
1469 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1470 wp->w_cursor.lnum += added;
1471 check_cursor_col();
1472
Bram Moolenaarf2732452018-06-03 14:47:35 +02001473#ifdef FEAT_JOB_CHANNEL
1474 if (bt_prompt(curbuf) && (State & INSERT))
1475 // show the line with the prompt
1476 update_topline();
1477#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001478 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001479
1480 if (!is_curbuf)
1481 {
1482 curbuf = curbuf_save;
1483 curwin = curwin_save;
1484 }
1485}
1486
1487/*
1488 * "append(lnum, string/list)" function
1489 */
1490 static void
1491f_append(typval_T *argvars, typval_T *rettv)
1492{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001493 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001494
1495 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1496}
1497
1498/*
1499 * "appendbufline(buf, lnum, string/list)" function
1500 */
1501 static void
1502f_appendbufline(typval_T *argvars, typval_T *rettv)
1503{
1504 linenr_T lnum;
1505 buf_T *buf;
1506
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001507 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001508 if (buf == NULL)
1509 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001510 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001511 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001512 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001513 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1514 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001515}
1516
1517/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001518 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519 */
1520 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001521f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001522{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001523 win_T *wp;
1524
1525 if (argvars[0].v_type == VAR_UNKNOWN)
1526 // use the current window
1527 rettv->vval.v_number = ARGCOUNT;
1528 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001529 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001530 // use the global argument list
1531 rettv->vval.v_number = GARGCOUNT;
1532 else
1533 {
1534 // use the argument list of the specified window
1535 wp = find_win_by_nr_or_id(&argvars[0]);
1536 if (wp != NULL)
1537 rettv->vval.v_number = WARGCOUNT(wp);
1538 else
1539 rettv->vval.v_number = -1;
1540 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001541}
1542
1543/*
1544 * "argidx()" function
1545 */
1546 static void
1547f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1548{
1549 rettv->vval.v_number = curwin->w_arg_idx;
1550}
1551
1552/*
1553 * "arglistid()" function
1554 */
1555 static void
1556f_arglistid(typval_T *argvars, typval_T *rettv)
1557{
1558 win_T *wp;
1559
1560 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001561 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001562 if (wp != NULL)
1563 rettv->vval.v_number = wp->w_alist->id;
1564}
1565
1566/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001567 * Get the argument list for a given window
1568 */
1569 static void
1570get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1571{
1572 int idx;
1573
1574 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1575 for (idx = 0; idx < argcount; ++idx)
1576 list_append_string(rettv->vval.v_list,
1577 alist_name(&arglist[idx]), -1);
1578}
1579
1580/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581 * "argv(nr)" function
1582 */
1583 static void
1584f_argv(typval_T *argvars, typval_T *rettv)
1585{
1586 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001587 aentry_T *arglist = NULL;
1588 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001589
1590 if (argvars[0].v_type != VAR_UNKNOWN)
1591 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001592 if (argvars[1].v_type == VAR_UNKNOWN)
1593 {
1594 arglist = ARGLIST;
1595 argcount = ARGCOUNT;
1596 }
1597 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001598 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001599 {
1600 arglist = GARGLIST;
1601 argcount = GARGCOUNT;
1602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001603 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001604 {
1605 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1606
1607 if (wp != NULL)
1608 {
1609 /* Use the argument list of the specified window */
1610 arglist = WARGLIST(wp);
1611 argcount = WARGCOUNT(wp);
1612 }
1613 }
1614
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001615 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001616 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001617 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001618 if (arglist != NULL && idx >= 0 && idx < argcount)
1619 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1620 else if (idx == -1)
1621 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001622 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001623 else
1624 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001625}
1626
1627/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001628 * "assert_beeps(cmd [, error])" function
1629 */
1630 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001631f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001632{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001634}
1635
1636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001637 * "assert_equal(expected, actual[, msg])" function
1638 */
1639 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001640f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643}
1644
1645/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001646 * "assert_equalfile(fname-one, fname-two)" function
1647 */
1648 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001649f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001650{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001652}
1653
1654/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001655 * "assert_notequal(expected, actual[, msg])" function
1656 */
1657 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001658f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661}
1662
1663/*
1664 * "assert_exception(string[, msg])" function
1665 */
1666 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001667f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001668{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670}
1671
1672/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001673 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674 */
1675 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001676f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001677{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679}
1680
1681/*
1682 * "assert_false(actual[, msg])" function
1683 */
1684 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001685f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001686{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001687 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688}
1689
1690/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001691 * "assert_inrange(lower, upper[, msg])" function
1692 */
1693 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001694f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001695{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001696 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001697}
1698
1699/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001700 * "assert_match(pattern, actual[, msg])" function
1701 */
1702 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001703f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001705 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001706}
1707
1708/*
1709 * "assert_notmatch(pattern, actual[, msg])" function
1710 */
1711 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001712f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001713{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001714 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715}
1716
1717/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001718 * "assert_report(msg)" function
1719 */
1720 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001721f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001722{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001723 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001724}
1725
1726/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001727 * "assert_true(actual[, msg])" function
1728 */
1729 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001730f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001732 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001733}
1734
1735#ifdef FEAT_FLOAT
1736/*
1737 * "asin()" function
1738 */
1739 static void
1740f_asin(typval_T *argvars, typval_T *rettv)
1741{
1742 float_T f = 0.0;
1743
1744 rettv->v_type = VAR_FLOAT;
1745 if (get_float_arg(argvars, &f) == OK)
1746 rettv->vval.v_float = asin(f);
1747 else
1748 rettv->vval.v_float = 0.0;
1749}
1750
1751/*
1752 * "atan()" function
1753 */
1754 static void
1755f_atan(typval_T *argvars, typval_T *rettv)
1756{
1757 float_T f = 0.0;
1758
1759 rettv->v_type = VAR_FLOAT;
1760 if (get_float_arg(argvars, &f) == OK)
1761 rettv->vval.v_float = atan(f);
1762 else
1763 rettv->vval.v_float = 0.0;
1764}
1765
1766/*
1767 * "atan2()" function
1768 */
1769 static void
1770f_atan2(typval_T *argvars, typval_T *rettv)
1771{
1772 float_T fx = 0.0, fy = 0.0;
1773
1774 rettv->v_type = VAR_FLOAT;
1775 if (get_float_arg(argvars, &fx) == OK
1776 && get_float_arg(&argvars[1], &fy) == OK)
1777 rettv->vval.v_float = atan2(fx, fy);
1778 else
1779 rettv->vval.v_float = 0.0;
1780}
1781#endif
1782
1783/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001784 * "balloon_show()" function
1785 */
1786#ifdef FEAT_BEVAL
1787 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001788f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1789{
1790 rettv->v_type = VAR_STRING;
1791 if (balloonEval != NULL)
1792 {
1793 if (balloonEval->msg == NULL)
1794 rettv->vval.v_string = NULL;
1795 else
1796 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1797 }
1798}
1799
1800 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001801f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1802{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001803 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001804 {
1805 if (argvars[0].v_type == VAR_LIST
1806# ifdef FEAT_GUI
1807 && !gui.in_use
1808# endif
1809 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001810 {
1811 list_T *l = argvars[0].vval.v_list;
1812
1813 // empty list removes the balloon
1814 post_balloon(balloonEval, NULL,
1815 l == NULL || l->lv_len == 0 ? NULL : l);
1816 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001818 {
1819 char_u *mesg = tv_get_string_chk(&argvars[0]);
1820
1821 if (mesg != NULL)
1822 // empty string removes the balloon
1823 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1824 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001825 }
1826}
1827
Bram Moolenaar669a8282017-11-19 20:13:05 +01001828# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001829 static void
1830f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1831{
1832 if (rettv_list_alloc(rettv) == OK)
1833 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001834 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001835
1836 if (msg != NULL)
1837 {
1838 pumitem_T *array;
1839 int size = split_message(msg, &array);
1840 int i;
1841
1842 /* Skip the first and last item, they are always empty. */
1843 for (i = 1; i < size - 1; ++i)
1844 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001845 while (size > 0)
1846 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001847 vim_free(array);
1848 }
1849 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001850}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001851# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001852#endif
1853
1854/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001855 * "browse(save, title, initdir, default)" function
1856 */
1857 static void
1858f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1859{
1860#ifdef FEAT_BROWSE
1861 int save;
1862 char_u *title;
1863 char_u *initdir;
1864 char_u *defname;
1865 char_u buf[NUMBUFLEN];
1866 char_u buf2[NUMBUFLEN];
1867 int error = FALSE;
1868
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001869 save = (int)tv_get_number_chk(&argvars[0], &error);
1870 title = tv_get_string_chk(&argvars[1]);
1871 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1872 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001873
1874 if (error || title == NULL || initdir == NULL || defname == NULL)
1875 rettv->vval.v_string = NULL;
1876 else
1877 rettv->vval.v_string =
1878 do_browse(save ? BROWSE_SAVE : 0,
1879 title, defname, NULL, initdir, NULL, curbuf);
1880#else
1881 rettv->vval.v_string = NULL;
1882#endif
1883 rettv->v_type = VAR_STRING;
1884}
1885
1886/*
1887 * "browsedir(title, initdir)" function
1888 */
1889 static void
1890f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1891{
1892#ifdef FEAT_BROWSE
1893 char_u *title;
1894 char_u *initdir;
1895 char_u buf[NUMBUFLEN];
1896
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001897 title = tv_get_string_chk(&argvars[0]);
1898 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001899
1900 if (title == NULL || initdir == NULL)
1901 rettv->vval.v_string = NULL;
1902 else
1903 rettv->vval.v_string = do_browse(BROWSE_DIR,
1904 title, NULL, NULL, initdir, NULL, curbuf);
1905#else
1906 rettv->vval.v_string = NULL;
1907#endif
1908 rettv->v_type = VAR_STRING;
1909}
1910
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001911/*
1912 * Find a buffer by number or exact name.
1913 */
1914 static buf_T *
1915find_buffer(typval_T *avar)
1916{
1917 buf_T *buf = NULL;
1918
1919 if (avar->v_type == VAR_NUMBER)
1920 buf = buflist_findnr((int)avar->vval.v_number);
1921 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1922 {
1923 buf = buflist_findname_exp(avar->vval.v_string);
1924 if (buf == NULL)
1925 {
1926 /* No full path name match, try a match with a URL or a "nofile"
1927 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001928 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001929 if (buf->b_fname != NULL
1930 && (path_with_url(buf->b_fname)
1931#ifdef FEAT_QUICKFIX
1932 || bt_nofile(buf)
1933#endif
1934 )
1935 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1936 break;
1937 }
1938 }
1939 return buf;
1940}
1941
1942/*
1943 * "bufexists(expr)" function
1944 */
1945 static void
1946f_bufexists(typval_T *argvars, typval_T *rettv)
1947{
1948 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1949}
1950
1951/*
1952 * "buflisted(expr)" function
1953 */
1954 static void
1955f_buflisted(typval_T *argvars, typval_T *rettv)
1956{
1957 buf_T *buf;
1958
1959 buf = find_buffer(&argvars[0]);
1960 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1961}
1962
1963/*
1964 * "bufloaded(expr)" function
1965 */
1966 static void
1967f_bufloaded(typval_T *argvars, typval_T *rettv)
1968{
1969 buf_T *buf;
1970
1971 buf = find_buffer(&argvars[0]);
1972 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1973}
1974
1975 buf_T *
1976buflist_find_by_name(char_u *name, int curtab_only)
1977{
1978 int save_magic;
1979 char_u *save_cpo;
1980 buf_T *buf;
1981
1982 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1983 save_magic = p_magic;
1984 p_magic = TRUE;
1985 save_cpo = p_cpo;
1986 p_cpo = (char_u *)"";
1987
1988 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1989 TRUE, FALSE, curtab_only));
1990
1991 p_magic = save_magic;
1992 p_cpo = save_cpo;
1993 return buf;
1994}
1995
1996/*
1997 * Get buffer by number or pattern.
1998 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001999 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002000tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002001{
2002 char_u *name = tv->vval.v_string;
2003 buf_T *buf;
2004
2005 if (tv->v_type == VAR_NUMBER)
2006 return buflist_findnr((int)tv->vval.v_number);
2007 if (tv->v_type != VAR_STRING)
2008 return NULL;
2009 if (name == NULL || *name == NUL)
2010 return curbuf;
2011 if (name[0] == '$' && name[1] == NUL)
2012 return lastbuf;
2013
2014 buf = buflist_find_by_name(name, curtab_only);
2015
2016 /* If not found, try expanding the name, like done for bufexists(). */
2017 if (buf == NULL)
2018 buf = find_buffer(tv);
2019
2020 return buf;
2021}
2022
2023/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002024 * Get the buffer from "arg" and give an error and return NULL if it is not
2025 * valid.
2026 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002027 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002028get_buf_arg(typval_T *arg)
2029{
2030 buf_T *buf;
2031
2032 ++emsg_off;
2033 buf = tv_get_buf(arg, FALSE);
2034 --emsg_off;
2035 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002036 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002037 return buf;
2038}
2039
2040/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 * "bufname(expr)" function
2042 */
2043 static void
2044f_bufname(typval_T *argvars, typval_T *rettv)
2045{
2046 buf_T *buf;
2047
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002048 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002049 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002050 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 rettv->v_type = VAR_STRING;
2052 if (buf != NULL && buf->b_fname != NULL)
2053 rettv->vval.v_string = vim_strsave(buf->b_fname);
2054 else
2055 rettv->vval.v_string = NULL;
2056 --emsg_off;
2057}
2058
2059/*
2060 * "bufnr(expr)" function
2061 */
2062 static void
2063f_bufnr(typval_T *argvars, typval_T *rettv)
2064{
2065 buf_T *buf;
2066 int error = FALSE;
2067 char_u *name;
2068
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002069 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002070 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002071 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002072 --emsg_off;
2073
2074 /* If the buffer isn't found and the second argument is not zero create a
2075 * new buffer. */
2076 if (buf == NULL
2077 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002078 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002079 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002080 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002081 && !error)
2082 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2083
2084 if (buf != NULL)
2085 rettv->vval.v_number = buf->b_fnum;
2086 else
2087 rettv->vval.v_number = -1;
2088}
2089
2090 static void
2091buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2092{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 win_T *wp;
2094 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 buf_T *buf;
2096
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002097 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002098 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002099 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002100 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002101 {
2102 ++winnr;
2103 if (wp->w_buffer == buf)
2104 break;
2105 }
2106 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002107 --emsg_off;
2108}
2109
2110/*
2111 * "bufwinid(nr)" function
2112 */
2113 static void
2114f_bufwinid(typval_T *argvars, typval_T *rettv)
2115{
2116 buf_win_common(argvars, rettv, FALSE);
2117}
2118
2119/*
2120 * "bufwinnr(nr)" function
2121 */
2122 static void
2123f_bufwinnr(typval_T *argvars, typval_T *rettv)
2124{
2125 buf_win_common(argvars, rettv, TRUE);
2126}
2127
2128/*
2129 * "byte2line(byte)" function
2130 */
2131 static void
2132f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2133{
2134#ifndef FEAT_BYTEOFF
2135 rettv->vval.v_number = -1;
2136#else
2137 long boff = 0;
2138
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002139 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002140 if (boff < 0)
2141 rettv->vval.v_number = -1;
2142 else
2143 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2144 (linenr_T)0, &boff);
2145#endif
2146}
2147
2148 static void
2149byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2150{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002152 char_u *str;
2153 varnumber_T idx;
2154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002155 str = tv_get_string_chk(&argvars[0]);
2156 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002157 rettv->vval.v_number = -1;
2158 if (str == NULL || idx < 0)
2159 return;
2160
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 t = str;
2162 for ( ; idx > 0; idx--)
2163 {
2164 if (*t == NUL) /* EOL reached */
2165 return;
2166 if (enc_utf8 && comp)
2167 t += utf_ptr2len(t);
2168 else
2169 t += (*mb_ptr2len)(t);
2170 }
2171 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002172}
2173
2174/*
2175 * "byteidx()" function
2176 */
2177 static void
2178f_byteidx(typval_T *argvars, typval_T *rettv)
2179{
2180 byteidx(argvars, rettv, FALSE);
2181}
2182
2183/*
2184 * "byteidxcomp()" function
2185 */
2186 static void
2187f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2188{
2189 byteidx(argvars, rettv, TRUE);
2190}
2191
2192/*
2193 * "call(func, arglist [, dict])" function
2194 */
2195 static void
2196f_call(typval_T *argvars, typval_T *rettv)
2197{
2198 char_u *func;
2199 partial_T *partial = NULL;
2200 dict_T *selfdict = NULL;
2201
2202 if (argvars[1].v_type != VAR_LIST)
2203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002205 return;
2206 }
2207 if (argvars[1].vval.v_list == NULL)
2208 return;
2209
2210 if (argvars[0].v_type == VAR_FUNC)
2211 func = argvars[0].vval.v_string;
2212 else if (argvars[0].v_type == VAR_PARTIAL)
2213 {
2214 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002215 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002216 }
2217 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002218 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 if (*func == NUL)
2220 return; /* type error or empty name */
2221
2222 if (argvars[2].v_type != VAR_UNKNOWN)
2223 {
2224 if (argvars[2].v_type != VAR_DICT)
2225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002226 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002227 return;
2228 }
2229 selfdict = argvars[2].vval.v_dict;
2230 }
2231
2232 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2233}
2234
2235#ifdef FEAT_FLOAT
2236/*
2237 * "ceil({float})" function
2238 */
2239 static void
2240f_ceil(typval_T *argvars, typval_T *rettv)
2241{
2242 float_T f = 0.0;
2243
2244 rettv->v_type = VAR_FLOAT;
2245 if (get_float_arg(argvars, &f) == OK)
2246 rettv->vval.v_float = ceil(f);
2247 else
2248 rettv->vval.v_float = 0.0;
2249}
2250#endif
2251
2252#ifdef FEAT_JOB_CHANNEL
2253/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002254 * "ch_canread()" function
2255 */
2256 static void
2257f_ch_canread(typval_T *argvars, typval_T *rettv)
2258{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002259 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002260
2261 rettv->vval.v_number = 0;
2262 if (channel != NULL)
2263 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2264 || channel_has_readahead(channel, PART_OUT)
2265 || channel_has_readahead(channel, PART_ERR);
2266}
2267
2268/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002269 * "ch_close()" function
2270 */
2271 static void
2272f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2273{
2274 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2275
2276 if (channel != NULL)
2277 {
2278 channel_close(channel, FALSE);
2279 channel_clear(channel);
2280 }
2281}
2282
2283/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002284 * "ch_close()" function
2285 */
2286 static void
2287f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2288{
2289 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2290
2291 if (channel != NULL)
2292 channel_close_in(channel);
2293}
2294
2295/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002296 * "ch_getbufnr()" function
2297 */
2298 static void
2299f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2300{
2301 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2302
2303 rettv->vval.v_number = -1;
2304 if (channel != NULL)
2305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002306 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002307 int part;
2308
2309 if (STRCMP(what, "err") == 0)
2310 part = PART_ERR;
2311 else if (STRCMP(what, "out") == 0)
2312 part = PART_OUT;
2313 else if (STRCMP(what, "in") == 0)
2314 part = PART_IN;
2315 else
2316 part = PART_SOCK;
2317 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2318 rettv->vval.v_number =
2319 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2320 }
2321}
2322
2323/*
2324 * "ch_getjob()" function
2325 */
2326 static void
2327f_ch_getjob(typval_T *argvars, typval_T *rettv)
2328{
2329 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2330
2331 if (channel != NULL)
2332 {
2333 rettv->v_type = VAR_JOB;
2334 rettv->vval.v_job = channel->ch_job;
2335 if (channel->ch_job != NULL)
2336 ++channel->ch_job->jv_refcount;
2337 }
2338}
2339
2340/*
2341 * "ch_info()" function
2342 */
2343 static void
2344f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2345{
2346 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2347
2348 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2349 channel_info(channel, rettv->vval.v_dict);
2350}
2351
2352/*
2353 * "ch_log()" function
2354 */
2355 static void
2356f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2357{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002358 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002359 channel_T *channel = NULL;
2360
2361 if (argvars[1].v_type != VAR_UNKNOWN)
2362 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2363
Bram Moolenaard5359b22018-04-05 22:44:39 +02002364 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002365}
2366
2367/*
2368 * "ch_logfile()" function
2369 */
2370 static void
2371f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2372{
2373 char_u *fname;
2374 char_u *opt = (char_u *)"";
2375 char_u buf[NUMBUFLEN];
2376
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002377 /* Don't open a file in restricted mode. */
2378 if (check_restricted() || check_secure())
2379 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002380 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002381 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002382 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383 ch_logfile(fname, opt);
2384}
2385
2386/*
2387 * "ch_open()" function
2388 */
2389 static void
2390f_ch_open(typval_T *argvars, typval_T *rettv)
2391{
2392 rettv->v_type = VAR_CHANNEL;
2393 if (check_restricted() || check_secure())
2394 return;
2395 rettv->vval.v_channel = channel_open_func(argvars);
2396}
2397
2398/*
2399 * "ch_read()" function
2400 */
2401 static void
2402f_ch_read(typval_T *argvars, typval_T *rettv)
2403{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002404 common_channel_read(argvars, rettv, FALSE, FALSE);
2405}
2406
2407/*
2408 * "ch_readblob()" function
2409 */
2410 static void
2411f_ch_readblob(typval_T *argvars, typval_T *rettv)
2412{
2413 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002414}
2415
2416/*
2417 * "ch_readraw()" function
2418 */
2419 static void
2420f_ch_readraw(typval_T *argvars, typval_T *rettv)
2421{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002422 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002423}
2424
2425/*
2426 * "ch_evalexpr()" function
2427 */
2428 static void
2429f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2430{
2431 ch_expr_common(argvars, rettv, TRUE);
2432}
2433
2434/*
2435 * "ch_sendexpr()" function
2436 */
2437 static void
2438f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2439{
2440 ch_expr_common(argvars, rettv, FALSE);
2441}
2442
2443/*
2444 * "ch_evalraw()" function
2445 */
2446 static void
2447f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2448{
2449 ch_raw_common(argvars, rettv, TRUE);
2450}
2451
2452/*
2453 * "ch_sendraw()" function
2454 */
2455 static void
2456f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2457{
2458 ch_raw_common(argvars, rettv, FALSE);
2459}
2460
2461/*
2462 * "ch_setoptions()" function
2463 */
2464 static void
2465f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2466{
2467 channel_T *channel;
2468 jobopt_T opt;
2469
2470 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2471 if (channel == NULL)
2472 return;
2473 clear_job_options(&opt);
2474 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002475 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002476 channel_set_options(channel, &opt);
2477 free_job_options(&opt);
2478}
2479
2480/*
2481 * "ch_status()" function
2482 */
2483 static void
2484f_ch_status(typval_T *argvars, typval_T *rettv)
2485{
2486 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002487 jobopt_T opt;
2488 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002489
2490 /* return an empty string by default */
2491 rettv->v_type = VAR_STRING;
2492 rettv->vval.v_string = NULL;
2493
2494 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002495
2496 if (argvars[1].v_type != VAR_UNKNOWN)
2497 {
2498 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002499 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002500 && (opt.jo_set & JO_PART))
2501 part = opt.jo_part;
2502 }
2503
2504 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002505}
2506#endif
2507
2508/*
2509 * "changenr()" function
2510 */
2511 static void
2512f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2513{
2514 rettv->vval.v_number = curbuf->b_u_seq_cur;
2515}
2516
2517/*
2518 * "char2nr(string)" function
2519 */
2520 static void
2521f_char2nr(typval_T *argvars, typval_T *rettv)
2522{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002523 if (has_mbyte)
2524 {
2525 int utf8 = 0;
2526
2527 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002528 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529
2530 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002531 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002532 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002533 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 }
2535 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002536 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002537}
2538
2539/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002540 * "chdir(dir)" function
2541 */
2542 static void
2543f_chdir(typval_T *argvars, typval_T *rettv)
2544{
2545 char_u *cwd;
2546 cdscope_T scope = CDSCOPE_GLOBAL;
2547
2548 rettv->v_type = VAR_STRING;
2549 rettv->vval.v_string = NULL;
2550
2551 if (argvars[0].v_type != VAR_STRING)
2552 return;
2553
2554 // Return the current directory
2555 cwd = alloc(MAXPATHL);
2556 if (cwd != NULL)
2557 {
2558 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2559 {
2560#ifdef BACKSLASH_IN_FILENAME
2561 slash_adjust(cwd);
2562#endif
2563 rettv->vval.v_string = vim_strsave(cwd);
2564 }
2565 vim_free(cwd);
2566 }
2567
2568 if (curwin->w_localdir != NULL)
2569 scope = CDSCOPE_WINDOW;
2570 else if (curtab->tp_localdir != NULL)
2571 scope = CDSCOPE_TABPAGE;
2572
2573 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2574 // Directory change failed
2575 VIM_CLEAR(rettv->vval.v_string);
2576}
2577
2578/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002579 * "cindent(lnum)" function
2580 */
2581 static void
2582f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2583{
2584#ifdef FEAT_CINDENT
2585 pos_T pos;
2586 linenr_T lnum;
2587
2588 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002589 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002590 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2591 {
2592 curwin->w_cursor.lnum = lnum;
2593 rettv->vval.v_number = get_c_indent();
2594 curwin->w_cursor = pos;
2595 }
2596 else
2597#endif
2598 rettv->vval.v_number = -1;
2599}
2600
Bram Moolenaaraff74912019-03-30 18:11:49 +01002601 static win_T *
2602get_optional_window(typval_T *argvars, int idx)
2603{
2604 win_T *win = curwin;
2605
2606 if (argvars[idx].v_type != VAR_UNKNOWN)
2607 {
2608 win = find_win_by_nr_or_id(&argvars[idx]);
2609 if (win == NULL)
2610 {
2611 emsg(_(e_invalwindow));
2612 return NULL;
2613 }
2614 }
2615 return win;
2616}
2617
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002618/*
2619 * "clearmatches()" function
2620 */
2621 static void
2622f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2623{
2624#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002625 win_T *win = get_optional_window(argvars, 0);
2626
2627 if (win != NULL)
2628 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002629#endif
2630}
2631
2632/*
2633 * "col(string)" function
2634 */
2635 static void
2636f_col(typval_T *argvars, typval_T *rettv)
2637{
2638 colnr_T col = 0;
2639 pos_T *fp;
2640 int fnum = curbuf->b_fnum;
2641
2642 fp = var2fpos(&argvars[0], FALSE, &fnum);
2643 if (fp != NULL && fnum == curbuf->b_fnum)
2644 {
2645 if (fp->col == MAXCOL)
2646 {
2647 /* '> can be MAXCOL, get the length of the line then */
2648 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2649 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2650 else
2651 col = MAXCOL;
2652 }
2653 else
2654 {
2655 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002656 /* col(".") when the cursor is on the NUL at the end of the line
2657 * because of "coladd" can be seen as an extra column. */
2658 if (virtual_active() && fp == &curwin->w_cursor)
2659 {
2660 char_u *p = ml_get_cursor();
2661
2662 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2663 curwin->w_virtcol - curwin->w_cursor.coladd))
2664 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002665 int l;
2666
2667 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2668 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 }
2670 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671 }
2672 }
2673 rettv->vval.v_number = col;
2674}
2675
2676#if defined(FEAT_INS_EXPAND)
2677/*
2678 * "complete()" function
2679 */
2680 static void
2681f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2682{
2683 int startcol;
2684
2685 if ((State & INSERT) == 0)
2686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002687 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002688 return;
2689 }
2690
2691 /* Check for undo allowed here, because if something was already inserted
2692 * the line was already saved for undo and this check isn't done. */
2693 if (!undo_allowed())
2694 return;
2695
2696 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002698 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002699 return;
2700 }
2701
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002702 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 if (startcol <= 0)
2704 return;
2705
2706 set_completion(startcol - 1, argvars[1].vval.v_list);
2707}
2708
2709/*
2710 * "complete_add()" function
2711 */
2712 static void
2713f_complete_add(typval_T *argvars, typval_T *rettv)
2714{
2715 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2716}
2717
2718/*
2719 * "complete_check()" function
2720 */
2721 static void
2722f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2723{
2724 int saved = RedrawingDisabled;
2725
2726 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002727 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002728 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002729 RedrawingDisabled = saved;
2730}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002731
2732/*
2733 * "complete_info()" function
2734 */
2735 static void
2736f_complete_info(typval_T *argvars, typval_T *rettv)
2737{
2738 list_T *what_list = NULL;
2739
2740 if (rettv_dict_alloc(rettv) != OK)
2741 return;
2742
2743 if (argvars[0].v_type != VAR_UNKNOWN)
2744 {
2745 if (argvars[0].v_type != VAR_LIST)
2746 {
2747 emsg(_(e_listreq));
2748 return;
2749 }
2750 what_list = argvars[0].vval.v_list;
2751 }
2752 get_complete_info(what_list, rettv->vval.v_dict);
2753}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002754#endif
2755
2756/*
2757 * "confirm(message, buttons[, default [, type]])" function
2758 */
2759 static void
2760f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2761{
2762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2763 char_u *message;
2764 char_u *buttons = NULL;
2765 char_u buf[NUMBUFLEN];
2766 char_u buf2[NUMBUFLEN];
2767 int def = 1;
2768 int type = VIM_GENERIC;
2769 char_u *typestr;
2770 int error = FALSE;
2771
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002772 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002773 if (message == NULL)
2774 error = TRUE;
2775 if (argvars[1].v_type != VAR_UNKNOWN)
2776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (buttons == NULL)
2779 error = TRUE;
2780 if (argvars[2].v_type != VAR_UNKNOWN)
2781 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002782 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002783 if (argvars[3].v_type != VAR_UNKNOWN)
2784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002785 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002786 if (typestr == NULL)
2787 error = TRUE;
2788 else
2789 {
2790 switch (TOUPPER_ASC(*typestr))
2791 {
2792 case 'E': type = VIM_ERROR; break;
2793 case 'Q': type = VIM_QUESTION; break;
2794 case 'I': type = VIM_INFO; break;
2795 case 'W': type = VIM_WARNING; break;
2796 case 'G': type = VIM_GENERIC; break;
2797 }
2798 }
2799 }
2800 }
2801 }
2802
2803 if (buttons == NULL || *buttons == NUL)
2804 buttons = (char_u *)_("&Ok");
2805
2806 if (!error)
2807 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2808 def, NULL, FALSE);
2809#endif
2810}
2811
2812/*
2813 * "copy()" function
2814 */
2815 static void
2816f_copy(typval_T *argvars, typval_T *rettv)
2817{
2818 item_copy(&argvars[0], rettv, FALSE, 0);
2819}
2820
2821#ifdef FEAT_FLOAT
2822/*
2823 * "cos()" function
2824 */
2825 static void
2826f_cos(typval_T *argvars, typval_T *rettv)
2827{
2828 float_T f = 0.0;
2829
2830 rettv->v_type = VAR_FLOAT;
2831 if (get_float_arg(argvars, &f) == OK)
2832 rettv->vval.v_float = cos(f);
2833 else
2834 rettv->vval.v_float = 0.0;
2835}
2836
2837/*
2838 * "cosh()" function
2839 */
2840 static void
2841f_cosh(typval_T *argvars, typval_T *rettv)
2842{
2843 float_T f = 0.0;
2844
2845 rettv->v_type = VAR_FLOAT;
2846 if (get_float_arg(argvars, &f) == OK)
2847 rettv->vval.v_float = cosh(f);
2848 else
2849 rettv->vval.v_float = 0.0;
2850}
2851#endif
2852
2853/*
2854 * "count()" function
2855 */
2856 static void
2857f_count(typval_T *argvars, typval_T *rettv)
2858{
2859 long n = 0;
2860 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002861 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002862
Bram Moolenaar9966b212017-07-28 16:46:57 +02002863 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002864 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865
2866 if (argvars[0].v_type == VAR_STRING)
2867 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002868 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002869 char_u *p = argvars[0].vval.v_string;
2870 char_u *next;
2871
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002872 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002873 {
2874 if (ic)
2875 {
2876 size_t len = STRLEN(expr);
2877
2878 while (*p != NUL)
2879 {
2880 if (MB_STRNICMP(p, expr, len) == 0)
2881 {
2882 ++n;
2883 p += len;
2884 }
2885 else
2886 MB_PTR_ADV(p);
2887 }
2888 }
2889 else
2890 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2891 != NULL)
2892 {
2893 ++n;
2894 p = next + STRLEN(expr);
2895 }
2896 }
2897
2898 }
2899 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002900 {
2901 listitem_T *li;
2902 list_T *l;
2903 long idx;
2904
2905 if ((l = argvars[0].vval.v_list) != NULL)
2906 {
2907 li = l->lv_first;
2908 if (argvars[2].v_type != VAR_UNKNOWN)
2909 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910 if (argvars[3].v_type != VAR_UNKNOWN)
2911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002912 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002913 if (!error)
2914 {
2915 li = list_find(l, idx);
2916 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002917 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002918 }
2919 }
2920 if (error)
2921 li = NULL;
2922 }
2923
2924 for ( ; li != NULL; li = li->li_next)
2925 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2926 ++n;
2927 }
2928 }
2929 else if (argvars[0].v_type == VAR_DICT)
2930 {
2931 int todo;
2932 dict_T *d;
2933 hashitem_T *hi;
2934
2935 if ((d = argvars[0].vval.v_dict) != NULL)
2936 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002937 if (argvars[2].v_type != VAR_UNKNOWN)
2938 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002940 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 }
2942
2943 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2944 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2945 {
2946 if (!HASHITEM_EMPTY(hi))
2947 {
2948 --todo;
2949 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2950 ++n;
2951 }
2952 }
2953 }
2954 }
2955 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002957 rettv->vval.v_number = n;
2958}
2959
2960/*
2961 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2962 *
2963 * Checks the existence of a cscope connection.
2964 */
2965 static void
2966f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2967{
2968#ifdef FEAT_CSCOPE
2969 int num = 0;
2970 char_u *dbpath = NULL;
2971 char_u *prepend = NULL;
2972 char_u buf[NUMBUFLEN];
2973
2974 if (argvars[0].v_type != VAR_UNKNOWN
2975 && argvars[1].v_type != VAR_UNKNOWN)
2976 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002977 num = (int)tv_get_number(&argvars[0]);
2978 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002979 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002980 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 }
2982
2983 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2984#endif
2985}
2986
2987/*
2988 * "cursor(lnum, col)" function, or
2989 * "cursor(list)"
2990 *
2991 * Moves the cursor to the specified line and column.
2992 * Returns 0 when the position could be set, -1 otherwise.
2993 */
2994 static void
2995f_cursor(typval_T *argvars, typval_T *rettv)
2996{
2997 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002999 int set_curswant = TRUE;
3000
3001 rettv->vval.v_number = -1;
3002 if (argvars[1].v_type == VAR_UNKNOWN)
3003 {
3004 pos_T pos;
3005 colnr_T curswant = -1;
3006
3007 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003009 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003010 return;
3011 }
3012 line = pos.lnum;
3013 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003015 if (curswant >= 0)
3016 {
3017 curwin->w_curswant = curswant - 1;
3018 set_curswant = FALSE;
3019 }
3020 }
3021 else
3022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003023 line = tv_get_lnum(argvars);
3024 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003025 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003026 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003028 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 return; /* type error; errmsg already given */
3030 if (line > 0)
3031 curwin->w_cursor.lnum = line;
3032 if (col > 0)
3033 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003035
3036 /* Make sure the cursor is in a valid position. */
3037 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 /* Correct cursor for multi-byte character. */
3039 if (has_mbyte)
3040 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003041
3042 curwin->w_set_curswant = set_curswant;
3043 rettv->vval.v_number = 0;
3044}
3045
Bram Moolenaar4f974752019-02-17 17:44:42 +01003046#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003047/*
3048 * "debugbreak()" function
3049 */
3050 static void
3051f_debugbreak(typval_T *argvars, typval_T *rettv)
3052{
3053 int pid;
3054
3055 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003056 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003057 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003058 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003059 else
3060 {
3061 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3062
3063 if (hProcess != NULL)
3064 {
3065 DebugBreakProcess(hProcess);
3066 CloseHandle(hProcess);
3067 rettv->vval.v_number = OK;
3068 }
3069 }
3070}
3071#endif
3072
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003073/*
3074 * "deepcopy()" function
3075 */
3076 static void
3077f_deepcopy(typval_T *argvars, typval_T *rettv)
3078{
3079 int noref = 0;
3080 int copyID;
3081
3082 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003083 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003084 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003085 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 else
3087 {
3088 copyID = get_copyID();
3089 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3090 }
3091}
3092
3093/*
3094 * "delete()" function
3095 */
3096 static void
3097f_delete(typval_T *argvars, typval_T *rettv)
3098{
3099 char_u nbuf[NUMBUFLEN];
3100 char_u *name;
3101 char_u *flags;
3102
3103 rettv->vval.v_number = -1;
3104 if (check_restricted() || check_secure())
3105 return;
3106
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003107 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 if (name == NULL || *name == NUL)
3109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003110 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003111 return;
3112 }
3113
3114 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003115 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003116 else
3117 flags = (char_u *)"";
3118
3119 if (*flags == NUL)
3120 /* delete a file */
3121 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3122 else if (STRCMP(flags, "d") == 0)
3123 /* delete an empty directory */
3124 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3125 else if (STRCMP(flags, "rf") == 0)
3126 /* delete a directory recursively */
3127 rettv->vval.v_number = delete_recursive(name);
3128 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003129 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003130}
3131
3132/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003133 * "deletebufline()" function
3134 */
3135 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003136f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003137{
3138 buf_T *buf;
3139 linenr_T first, last;
3140 linenr_T lnum;
3141 long count;
3142 int is_curbuf;
3143 buf_T *curbuf_save = NULL;
3144 win_T *curwin_save = NULL;
3145 tabpage_T *tp;
3146 win_T *wp;
3147
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003148 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149 if (buf == NULL)
3150 {
3151 rettv->vval.v_number = 1; /* FAIL */
3152 return;
3153 }
3154 is_curbuf = buf == curbuf;
3155
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003156 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003157 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003158 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003159 else
3160 last = first;
3161
3162 if (buf->b_ml.ml_mfp == NULL || first < 1
3163 || first > buf->b_ml.ml_line_count || last < first)
3164 {
3165 rettv->vval.v_number = 1; /* FAIL */
3166 return;
3167 }
3168
3169 if (!is_curbuf)
3170 {
3171 curbuf_save = curbuf;
3172 curwin_save = curwin;
3173 curbuf = buf;
3174 find_win_for_curbuf();
3175 }
3176 if (last > curbuf->b_ml.ml_line_count)
3177 last = curbuf->b_ml.ml_line_count;
3178 count = last - first + 1;
3179
3180 // When coming here from Insert mode, sync undo, so that this can be
3181 // undone separately from what was previously inserted.
3182 if (u_sync_once == 2)
3183 {
3184 u_sync_once = 1; // notify that u_sync() was called
3185 u_sync(TRUE);
3186 }
3187
3188 if (u_save(first - 1, last + 1) == FAIL)
3189 {
3190 rettv->vval.v_number = 1; /* FAIL */
3191 return;
3192 }
3193
3194 for (lnum = first; lnum <= last; ++lnum)
3195 ml_delete(first, TRUE);
3196
3197 FOR_ALL_TAB_WINDOWS(tp, wp)
3198 if (wp->w_buffer == buf)
3199 {
3200 if (wp->w_cursor.lnum > last)
3201 wp->w_cursor.lnum -= count;
3202 else if (wp->w_cursor.lnum> first)
3203 wp->w_cursor.lnum = first;
3204 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3205 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3206 }
3207 check_cursor_col();
3208 deleted_lines_mark(first, count);
3209
3210 if (!is_curbuf)
3211 {
3212 curbuf = curbuf_save;
3213 curwin = curwin_save;
3214 }
3215}
3216
3217/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003218 * "did_filetype()" function
3219 */
3220 static void
3221f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3222{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003223 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003224}
3225
3226/*
3227 * "diff_filler()" function
3228 */
3229 static void
3230f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3231{
3232#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003233 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003234#endif
3235}
3236
3237/*
3238 * "diff_hlID()" function
3239 */
3240 static void
3241f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3242{
3243#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003244 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003245 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003246 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 static int fnum = 0;
3248 static int change_start = 0;
3249 static int change_end = 0;
3250 static hlf_T hlID = (hlf_T)0;
3251 int filler_lines;
3252 int col;
3253
3254 if (lnum < 0) /* ignore type error in {lnum} arg */
3255 lnum = 0;
3256 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003257 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003258 || fnum != curbuf->b_fnum)
3259 {
3260 /* New line, buffer, change: need to get the values. */
3261 filler_lines = diff_check(curwin, lnum);
3262 if (filler_lines < 0)
3263 {
3264 if (filler_lines == -1)
3265 {
3266 change_start = MAXCOL;
3267 change_end = -1;
3268 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3269 hlID = HLF_ADD; /* added line */
3270 else
3271 hlID = HLF_CHD; /* changed line */
3272 }
3273 else
3274 hlID = HLF_ADD; /* added line */
3275 }
3276 else
3277 hlID = (hlf_T)0;
3278 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003279 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003280 fnum = curbuf->b_fnum;
3281 }
3282
3283 if (hlID == HLF_CHD || hlID == HLF_TXD)
3284 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003285 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003286 if (col >= change_start && col <= change_end)
3287 hlID = HLF_TXD; /* changed text */
3288 else
3289 hlID = HLF_CHD; /* changed line */
3290 }
3291 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3292#endif
3293}
3294
3295/*
3296 * "empty({expr})" function
3297 */
3298 static void
3299f_empty(typval_T *argvars, typval_T *rettv)
3300{
3301 int n = FALSE;
3302
3303 switch (argvars[0].v_type)
3304 {
3305 case VAR_STRING:
3306 case VAR_FUNC:
3307 n = argvars[0].vval.v_string == NULL
3308 || *argvars[0].vval.v_string == NUL;
3309 break;
3310 case VAR_PARTIAL:
3311 n = FALSE;
3312 break;
3313 case VAR_NUMBER:
3314 n = argvars[0].vval.v_number == 0;
3315 break;
3316 case VAR_FLOAT:
3317#ifdef FEAT_FLOAT
3318 n = argvars[0].vval.v_float == 0.0;
3319 break;
3320#endif
3321 case VAR_LIST:
3322 n = argvars[0].vval.v_list == NULL
3323 || argvars[0].vval.v_list->lv_first == NULL;
3324 break;
3325 case VAR_DICT:
3326 n = argvars[0].vval.v_dict == NULL
3327 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3328 break;
3329 case VAR_SPECIAL:
3330 n = argvars[0].vval.v_number != VVAL_TRUE;
3331 break;
3332
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003333 case VAR_BLOB:
3334 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003335 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3336 break;
3337
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003338 case VAR_JOB:
3339#ifdef FEAT_JOB_CHANNEL
3340 n = argvars[0].vval.v_job == NULL
3341 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3342 break;
3343#endif
3344 case VAR_CHANNEL:
3345#ifdef FEAT_JOB_CHANNEL
3346 n = argvars[0].vval.v_channel == NULL
3347 || !channel_is_open(argvars[0].vval.v_channel);
3348 break;
3349#endif
3350 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003351 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003352 n = TRUE;
3353 break;
3354 }
3355
3356 rettv->vval.v_number = n;
3357}
3358
3359/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003360 * "environ()" function
3361 */
3362 static void
3363f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3364{
3365#if !defined(AMIGA)
3366 int i = 0;
3367 char_u *entry, *value;
3368# ifdef MSWIN
3369 extern wchar_t **_wenviron;
3370# else
3371 extern char **environ;
3372# endif
3373
3374 if (rettv_dict_alloc(rettv) != OK)
3375 return;
3376
3377# ifdef MSWIN
3378 if (*_wenviron == NULL)
3379 return;
3380# else
3381 if (*environ == NULL)
3382 return;
3383# endif
3384
3385 for (i = 0; ; ++i)
3386 {
3387# ifdef MSWIN
3388 short_u *p;
3389
3390 if ((p = (short_u *)_wenviron[i]) == NULL)
3391 return;
3392 entry = utf16_to_enc(p, NULL);
3393# else
3394 if ((entry = (char_u *)environ[i]) == NULL)
3395 return;
3396 entry = vim_strsave(entry);
3397# endif
3398 if (entry == NULL) // out of memory
3399 return;
3400 if ((value = vim_strchr(entry, '=')) == NULL)
3401 {
3402 vim_free(entry);
3403 continue;
3404 }
3405 *value++ = NUL;
3406 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3407 vim_free(entry);
3408 }
3409#endif
3410}
3411
3412/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003413 * "escape({string}, {chars})" function
3414 */
3415 static void
3416f_escape(typval_T *argvars, typval_T *rettv)
3417{
3418 char_u buf[NUMBUFLEN];
3419
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003420 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3421 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003422 rettv->v_type = VAR_STRING;
3423}
3424
3425/*
3426 * "eval()" function
3427 */
3428 static void
3429f_eval(typval_T *argvars, typval_T *rettv)
3430{
3431 char_u *s, *p;
3432
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003433 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 if (s != NULL)
3435 s = skipwhite(s);
3436
3437 p = s;
3438 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3439 {
3440 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003441 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003442 need_clr_eos = FALSE;
3443 rettv->v_type = VAR_NUMBER;
3444 rettv->vval.v_number = 0;
3445 }
3446 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003447 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003448}
3449
3450/*
3451 * "eventhandler()" function
3452 */
3453 static void
3454f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3455{
3456 rettv->vval.v_number = vgetc_busy;
3457}
3458
3459/*
3460 * "executable()" function
3461 */
3462 static void
3463f_executable(typval_T *argvars, typval_T *rettv)
3464{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003465 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003466
3467 /* Check in $PATH and also check directly if there is a directory name. */
3468 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3469 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3470}
3471
3472static garray_T redir_execute_ga;
3473
3474/*
3475 * Append "value[value_len]" to the execute() output.
3476 */
3477 void
3478execute_redir_str(char_u *value, int value_len)
3479{
3480 int len;
3481
3482 if (value_len == -1)
3483 len = (int)STRLEN(value); /* Append the entire string */
3484 else
3485 len = value_len; /* Append only "value_len" characters */
3486 if (ga_grow(&redir_execute_ga, len) == OK)
3487 {
3488 mch_memmove((char *)redir_execute_ga.ga_data
3489 + redir_execute_ga.ga_len, value, len);
3490 redir_execute_ga.ga_len += len;
3491 }
3492}
3493
3494/*
3495 * Get next line from a list.
3496 * Called by do_cmdline() to get the next line.
3497 * Returns allocated string, or NULL for end of function.
3498 */
3499
3500 static char_u *
3501get_list_line(
3502 int c UNUSED,
3503 void *cookie,
3504 int indent UNUSED)
3505{
3506 listitem_T **p = (listitem_T **)cookie;
3507 listitem_T *item = *p;
3508 char_u buf[NUMBUFLEN];
3509 char_u *s;
3510
3511 if (item == NULL)
3512 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003513 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003514 *p = item->li_next;
3515 return s == NULL ? NULL : vim_strsave(s);
3516}
3517
3518/*
3519 * "execute()" function
3520 */
3521 static void
3522f_execute(typval_T *argvars, typval_T *rettv)
3523{
3524 char_u *cmd = NULL;
3525 list_T *list = NULL;
3526 int save_msg_silent = msg_silent;
3527 int save_emsg_silent = emsg_silent;
3528 int save_emsg_noredir = emsg_noredir;
3529 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003530 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003531 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003532 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003533 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003534
3535 rettv->vval.v_string = NULL;
3536 rettv->v_type = VAR_STRING;
3537
3538 if (argvars[0].v_type == VAR_LIST)
3539 {
3540 list = argvars[0].vval.v_list;
3541 if (list == NULL || list->lv_first == NULL)
3542 /* empty list, no commands, empty output */
3543 return;
3544 ++list->lv_refcount;
3545 }
3546 else
3547 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003548 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549 if (cmd == NULL)
3550 return;
3551 }
3552
3553 if (argvars[1].v_type != VAR_UNKNOWN)
3554 {
3555 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003556 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003557
3558 if (s == NULL)
3559 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003560 if (*s == NUL)
3561 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003562 if (STRNCMP(s, "silent", 6) == 0)
3563 ++msg_silent;
3564 if (STRCMP(s, "silent!") == 0)
3565 {
3566 emsg_silent = TRUE;
3567 emsg_noredir = TRUE;
3568 }
3569 }
3570 else
3571 ++msg_silent;
3572
3573 if (redir_execute)
3574 save_ga = redir_execute_ga;
3575 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3576 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003577 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003578 if (!echo_output)
3579 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003580
3581 if (cmd != NULL)
3582 do_cmdline_cmd(cmd);
3583 else
3584 {
3585 listitem_T *item = list->lv_first;
3586
3587 do_cmdline(NULL, get_list_line, (void *)&item,
3588 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3589 --list->lv_refcount;
3590 }
3591
Bram Moolenaard297f352017-01-29 20:31:21 +01003592 /* Need to append a NUL to the result. */
3593 if (ga_grow(&redir_execute_ga, 1) == OK)
3594 {
3595 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3596 rettv->vval.v_string = redir_execute_ga.ga_data;
3597 }
3598 else
3599 {
3600 ga_clear(&redir_execute_ga);
3601 rettv->vval.v_string = NULL;
3602 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603 msg_silent = save_msg_silent;
3604 emsg_silent = save_emsg_silent;
3605 emsg_noredir = save_emsg_noredir;
3606
3607 redir_execute = save_redir_execute;
3608 if (redir_execute)
3609 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003610 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003611
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003612 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003613 if (echo_output)
3614 // When not working silently: put it in column zero. A following
3615 // "echon" will overwrite the message, unavoidably.
3616 msg_col = 0;
3617 else
3618 // When working silently: Put it back where it was, since nothing
3619 // should have been written.
3620 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003621}
3622
3623/*
3624 * "exepath()" function
3625 */
3626 static void
3627f_exepath(typval_T *argvars, typval_T *rettv)
3628{
3629 char_u *p = NULL;
3630
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003631 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003632 rettv->v_type = VAR_STRING;
3633 rettv->vval.v_string = p;
3634}
3635
3636/*
3637 * "exists()" function
3638 */
3639 static void
3640f_exists(typval_T *argvars, typval_T *rettv)
3641{
3642 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003644
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003645 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003646 if (*p == '$') /* environment variable */
3647 {
3648 /* first try "normal" environment variables (fast) */
3649 if (mch_getenv(p + 1) != NULL)
3650 n = TRUE;
3651 else
3652 {
3653 /* try expanding things like $VIM and ${HOME} */
3654 p = expand_env_save(p);
3655 if (p != NULL && *p != '$')
3656 n = TRUE;
3657 vim_free(p);
3658 }
3659 }
3660 else if (*p == '&' || *p == '+') /* option */
3661 {
3662 n = (get_option_tv(&p, NULL, TRUE) == OK);
3663 if (*skipwhite(p) != NUL)
3664 n = FALSE; /* trailing garbage */
3665 }
3666 else if (*p == '*') /* internal or user defined function */
3667 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003668 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003669 }
3670 else if (*p == ':')
3671 {
3672 n = cmd_exists(p + 1);
3673 }
3674 else if (*p == '#')
3675 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 if (p[1] == '#')
3677 n = autocmd_supported(p + 2);
3678 else
3679 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680 }
3681 else /* internal variable */
3682 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003683 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003684 }
3685
3686 rettv->vval.v_number = n;
3687}
3688
3689#ifdef FEAT_FLOAT
3690/*
3691 * "exp()" function
3692 */
3693 static void
3694f_exp(typval_T *argvars, typval_T *rettv)
3695{
3696 float_T f = 0.0;
3697
3698 rettv->v_type = VAR_FLOAT;
3699 if (get_float_arg(argvars, &f) == OK)
3700 rettv->vval.v_float = exp(f);
3701 else
3702 rettv->vval.v_float = 0.0;
3703}
3704#endif
3705
3706/*
3707 * "expand()" function
3708 */
3709 static void
3710f_expand(typval_T *argvars, typval_T *rettv)
3711{
3712 char_u *s;
3713 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003714 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003715 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3716 expand_T xpc;
3717 int error = FALSE;
3718 char_u *result;
3719
3720 rettv->v_type = VAR_STRING;
3721 if (argvars[1].v_type != VAR_UNKNOWN
3722 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003723 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003724 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003725 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003727 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 if (*s == '%' || *s == '#' || *s == '<')
3729 {
3730 ++emsg_off;
3731 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3732 --emsg_off;
3733 if (rettv->v_type == VAR_LIST)
3734 {
3735 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3736 list_append_string(rettv->vval.v_list, result, -1);
3737 else
3738 vim_free(result);
3739 }
3740 else
3741 rettv->vval.v_string = result;
3742 }
3743 else
3744 {
3745 /* When the optional second argument is non-zero, don't remove matches
3746 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3747 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003748 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003749 options |= WILD_KEEP_ALL;
3750 if (!error)
3751 {
3752 ExpandInit(&xpc);
3753 xpc.xp_context = EXPAND_FILES;
3754 if (p_wic)
3755 options += WILD_ICASE;
3756 if (rettv->v_type == VAR_STRING)
3757 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3758 options, WILD_ALL);
3759 else if (rettv_list_alloc(rettv) != FAIL)
3760 {
3761 int i;
3762
3763 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3764 for (i = 0; i < xpc.xp_numfiles; i++)
3765 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3766 ExpandCleanup(&xpc);
3767 }
3768 }
3769 else
3770 rettv->vval.v_string = NULL;
3771 }
3772}
3773
3774/*
3775 * "extend(list, list [, idx])" function
3776 * "extend(dict, dict [, action])" function
3777 */
3778 static void
3779f_extend(typval_T *argvars, typval_T *rettv)
3780{
3781 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3782
3783 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3784 {
3785 list_T *l1, *l2;
3786 listitem_T *item;
3787 long before;
3788 int error = FALSE;
3789
3790 l1 = argvars[0].vval.v_list;
3791 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003792 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003793 && l2 != NULL)
3794 {
3795 if (argvars[2].v_type != VAR_UNKNOWN)
3796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003797 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003798 if (error)
3799 return; /* type error; errmsg already given */
3800
3801 if (before == l1->lv_len)
3802 item = NULL;
3803 else
3804 {
3805 item = list_find(l1, before);
3806 if (item == NULL)
3807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003808 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003809 return;
3810 }
3811 }
3812 }
3813 else
3814 item = NULL;
3815 list_extend(l1, l2, item);
3816
3817 copy_tv(&argvars[0], rettv);
3818 }
3819 }
3820 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3821 {
3822 dict_T *d1, *d2;
3823 char_u *action;
3824 int i;
3825
3826 d1 = argvars[0].vval.v_dict;
3827 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003828 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 && d2 != NULL)
3830 {
3831 /* Check the third argument. */
3832 if (argvars[2].v_type != VAR_UNKNOWN)
3833 {
3834 static char *(av[]) = {"keep", "force", "error"};
3835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003836 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 if (action == NULL)
3838 return; /* type error; errmsg already given */
3839 for (i = 0; i < 3; ++i)
3840 if (STRCMP(action, av[i]) == 0)
3841 break;
3842 if (i == 3)
3843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003844 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003845 return;
3846 }
3847 }
3848 else
3849 action = (char_u *)"force";
3850
3851 dict_extend(d1, d2, action);
3852
3853 copy_tv(&argvars[0], rettv);
3854 }
3855 }
3856 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003857 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003858}
3859
3860/*
3861 * "feedkeys()" function
3862 */
3863 static void
3864f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3865{
3866 int remap = TRUE;
3867 int insert = FALSE;
3868 char_u *keys, *flags;
3869 char_u nbuf[NUMBUFLEN];
3870 int typed = FALSE;
3871 int execute = FALSE;
3872 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003873 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003874 char_u *keys_esc;
3875
3876 /* This is not allowed in the sandbox. If the commands would still be
3877 * executed in the sandbox it would be OK, but it probably happens later,
3878 * when "sandbox" is no longer set. */
3879 if (check_secure())
3880 return;
3881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003882 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003883
3884 if (argvars[1].v_type != VAR_UNKNOWN)
3885 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003886 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 for ( ; *flags != NUL; ++flags)
3888 {
3889 switch (*flags)
3890 {
3891 case 'n': remap = FALSE; break;
3892 case 'm': remap = TRUE; break;
3893 case 't': typed = TRUE; break;
3894 case 'i': insert = TRUE; break;
3895 case 'x': execute = TRUE; break;
3896 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003897 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 }
3899 }
3900 }
3901
3902 if (*keys != NUL || execute)
3903 {
3904 /* Need to escape K_SPECIAL and CSI before putting the string in the
3905 * typeahead buffer. */
3906 keys_esc = vim_strsave_escape_csi(keys);
3907 if (keys_esc != NULL)
3908 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003909 if (lowlevel)
3910 {
3911#ifdef USE_INPUT_BUF
3912 add_to_input_buf(keys, (int)STRLEN(keys));
3913#else
3914 emsg(_("E980: lowlevel input not supported"));
3915#endif
3916 }
3917 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003918 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003919 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003920 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003921 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003922#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003923 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003924#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003925 )
3926 typebuf_was_filled = TRUE;
3927 }
3928 vim_free(keys_esc);
3929
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003930 if (execute)
3931 {
3932 int save_msg_scroll = msg_scroll;
3933
3934 /* Avoid a 1 second delay when the keys start Insert mode. */
3935 msg_scroll = FALSE;
3936
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003937 if (!dangerous)
3938 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003939 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003940 if (!dangerous)
3941 --ex_normal_busy;
3942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 msg_scroll |= save_msg_scroll;
3944 }
3945 }
3946 }
3947}
3948
3949/*
3950 * "filereadable()" function
3951 */
3952 static void
3953f_filereadable(typval_T *argvars, typval_T *rettv)
3954{
3955 int fd;
3956 char_u *p;
3957 int n;
3958
3959#ifndef O_NONBLOCK
3960# define O_NONBLOCK 0
3961#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003962 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003963 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3964 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3965 {
3966 n = TRUE;
3967 close(fd);
3968 }
3969 else
3970 n = FALSE;
3971
3972 rettv->vval.v_number = n;
3973}
3974
3975/*
3976 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3977 * rights to write into.
3978 */
3979 static void
3980f_filewritable(typval_T *argvars, typval_T *rettv)
3981{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003982 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003983}
3984
3985 static void
3986findfilendir(
3987 typval_T *argvars UNUSED,
3988 typval_T *rettv,
3989 int find_what UNUSED)
3990{
3991#ifdef FEAT_SEARCHPATH
3992 char_u *fname;
3993 char_u *fresult = NULL;
3994 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3995 char_u *p;
3996 char_u pathbuf[NUMBUFLEN];
3997 int count = 1;
3998 int first = TRUE;
3999 int error = FALSE;
4000#endif
4001
4002 rettv->vval.v_string = NULL;
4003 rettv->v_type = VAR_STRING;
4004
4005#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004006 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004007
4008 if (argvars[1].v_type != VAR_UNKNOWN)
4009 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004010 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004011 if (p == NULL)
4012 error = TRUE;
4013 else
4014 {
4015 if (*p != NUL)
4016 path = p;
4017
4018 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020 }
4021 }
4022
4023 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4024 error = TRUE;
4025
4026 if (*fname != NUL && !error)
4027 {
4028 do
4029 {
4030 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4031 vim_free(fresult);
4032 fresult = find_file_in_path_option(first ? fname : NULL,
4033 first ? (int)STRLEN(fname) : 0,
4034 0, first, path,
4035 find_what,
4036 curbuf->b_ffname,
4037 find_what == FINDFILE_DIR
4038 ? (char_u *)"" : curbuf->b_p_sua);
4039 first = FALSE;
4040
4041 if (fresult != NULL && rettv->v_type == VAR_LIST)
4042 list_append_string(rettv->vval.v_list, fresult, -1);
4043
4044 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4045 }
4046
4047 if (rettv->v_type == VAR_STRING)
4048 rettv->vval.v_string = fresult;
4049#endif
4050}
4051
4052/*
4053 * "filter()" function
4054 */
4055 static void
4056f_filter(typval_T *argvars, typval_T *rettv)
4057{
4058 filter_map(argvars, rettv, FALSE);
4059}
4060
4061/*
4062 * "finddir({fname}[, {path}[, {count}]])" function
4063 */
4064 static void
4065f_finddir(typval_T *argvars, typval_T *rettv)
4066{
4067 findfilendir(argvars, rettv, FINDFILE_DIR);
4068}
4069
4070/*
4071 * "findfile({fname}[, {path}[, {count}]])" function
4072 */
4073 static void
4074f_findfile(typval_T *argvars, typval_T *rettv)
4075{
4076 findfilendir(argvars, rettv, FINDFILE_FILE);
4077}
4078
4079#ifdef FEAT_FLOAT
4080/*
4081 * "float2nr({float})" function
4082 */
4083 static void
4084f_float2nr(typval_T *argvars, typval_T *rettv)
4085{
4086 float_T f = 0.0;
4087
4088 if (get_float_arg(argvars, &f) == OK)
4089 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004090 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004091 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004092 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004093 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004094 else
4095 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004096 }
4097}
4098
4099/*
4100 * "floor({float})" function
4101 */
4102 static void
4103f_floor(typval_T *argvars, typval_T *rettv)
4104{
4105 float_T f = 0.0;
4106
4107 rettv->v_type = VAR_FLOAT;
4108 if (get_float_arg(argvars, &f) == OK)
4109 rettv->vval.v_float = floor(f);
4110 else
4111 rettv->vval.v_float = 0.0;
4112}
4113
4114/*
4115 * "fmod()" function
4116 */
4117 static void
4118f_fmod(typval_T *argvars, typval_T *rettv)
4119{
4120 float_T fx = 0.0, fy = 0.0;
4121
4122 rettv->v_type = VAR_FLOAT;
4123 if (get_float_arg(argvars, &fx) == OK
4124 && get_float_arg(&argvars[1], &fy) == OK)
4125 rettv->vval.v_float = fmod(fx, fy);
4126 else
4127 rettv->vval.v_float = 0.0;
4128}
4129#endif
4130
4131/*
4132 * "fnameescape({string})" function
4133 */
4134 static void
4135f_fnameescape(typval_T *argvars, typval_T *rettv)
4136{
4137 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004138 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004139 rettv->v_type = VAR_STRING;
4140}
4141
4142/*
4143 * "fnamemodify({fname}, {mods})" function
4144 */
4145 static void
4146f_fnamemodify(typval_T *argvars, typval_T *rettv)
4147{
4148 char_u *fname;
4149 char_u *mods;
4150 int usedlen = 0;
4151 int len;
4152 char_u *fbuf = NULL;
4153 char_u buf[NUMBUFLEN];
4154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004155 fname = tv_get_string_chk(&argvars[0]);
4156 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004157 if (fname == NULL || mods == NULL)
4158 fname = NULL;
4159 else
4160 {
4161 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004162 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004163 }
4164
4165 rettv->v_type = VAR_STRING;
4166 if (fname == NULL)
4167 rettv->vval.v_string = NULL;
4168 else
4169 rettv->vval.v_string = vim_strnsave(fname, len);
4170 vim_free(fbuf);
4171}
4172
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004173/*
4174 * "foldclosed()" function
4175 */
4176 static void
4177foldclosed_both(
4178 typval_T *argvars UNUSED,
4179 typval_T *rettv,
4180 int end UNUSED)
4181{
4182#ifdef FEAT_FOLDING
4183 linenr_T lnum;
4184 linenr_T first, last;
4185
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004186 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004187 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4188 {
4189 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4190 {
4191 if (end)
4192 rettv->vval.v_number = (varnumber_T)last;
4193 else
4194 rettv->vval.v_number = (varnumber_T)first;
4195 return;
4196 }
4197 }
4198#endif
4199 rettv->vval.v_number = -1;
4200}
4201
4202/*
4203 * "foldclosed()" function
4204 */
4205 static void
4206f_foldclosed(typval_T *argvars, typval_T *rettv)
4207{
4208 foldclosed_both(argvars, rettv, FALSE);
4209}
4210
4211/*
4212 * "foldclosedend()" function
4213 */
4214 static void
4215f_foldclosedend(typval_T *argvars, typval_T *rettv)
4216{
4217 foldclosed_both(argvars, rettv, TRUE);
4218}
4219
4220/*
4221 * "foldlevel()" function
4222 */
4223 static void
4224f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4225{
4226#ifdef FEAT_FOLDING
4227 linenr_T lnum;
4228
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004229 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004230 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4231 rettv->vval.v_number = foldLevel(lnum);
4232#endif
4233}
4234
4235/*
4236 * "foldtext()" function
4237 */
4238 static void
4239f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4240{
4241#ifdef FEAT_FOLDING
4242 linenr_T foldstart;
4243 linenr_T foldend;
4244 char_u *dashes;
4245 linenr_T lnum;
4246 char_u *s;
4247 char_u *r;
4248 int len;
4249 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004250 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004251#endif
4252
4253 rettv->v_type = VAR_STRING;
4254 rettv->vval.v_string = NULL;
4255#ifdef FEAT_FOLDING
4256 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4257 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4258 dashes = get_vim_var_str(VV_FOLDDASHES);
4259 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4260 && dashes != NULL)
4261 {
4262 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004263 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264 if (!linewhite(lnum))
4265 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004266
4267 /* Find interesting text in this line. */
4268 s = skipwhite(ml_get(lnum));
4269 /* skip C comment-start */
4270 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4271 {
4272 s = skipwhite(s + 2);
4273 if (*skipwhite(s) == NUL
4274 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4275 {
4276 s = skipwhite(ml_get(lnum + 1));
4277 if (*s == '*')
4278 s = skipwhite(s + 1);
4279 }
4280 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004281 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004282 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004283 r = alloc(STRLEN(txt)
4284 + STRLEN(dashes) // for %s
4285 + 20 // for %3ld
4286 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004287 if (r != NULL)
4288 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004289 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004290 len = (int)STRLEN(r);
4291 STRCAT(r, s);
4292 /* remove 'foldmarker' and 'commentstring' */
4293 foldtext_cleanup(r + len);
4294 rettv->vval.v_string = r;
4295 }
4296 }
4297#endif
4298}
4299
4300/*
4301 * "foldtextresult(lnum)" function
4302 */
4303 static void
4304f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4305{
4306#ifdef FEAT_FOLDING
4307 linenr_T lnum;
4308 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004309 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004310 foldinfo_T foldinfo;
4311 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004312 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004313#endif
4314
4315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = NULL;
4317#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004318 if (entered)
4319 return; /* reject recursive use */
4320 entered = TRUE;
4321
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004322 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 /* treat illegal types and illegal string values for {lnum} the same */
4324 if (lnum < 0)
4325 lnum = 0;
4326 fold_count = foldedCount(curwin, lnum, &foldinfo);
4327 if (fold_count > 0)
4328 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004329 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4330 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004331 if (text == buf)
4332 text = vim_strsave(text);
4333 rettv->vval.v_string = text;
4334 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004335
4336 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004337#endif
4338}
4339
4340/*
4341 * "foreground()" function
4342 */
4343 static void
4344f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4345{
4346#ifdef FEAT_GUI
4347 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004348 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004349 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004350 return;
4351 }
4352#endif
4353#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004354 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004355#endif
4356}
4357
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004359common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360{
4361 char_u *s;
4362 char_u *name;
4363 int use_string = FALSE;
4364 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004365 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004366
4367 if (argvars[0].v_type == VAR_FUNC)
4368 {
4369 /* function(MyFunc, [arg], dict) */
4370 s = argvars[0].vval.v_string;
4371 }
4372 else if (argvars[0].v_type == VAR_PARTIAL
4373 && argvars[0].vval.v_partial != NULL)
4374 {
4375 /* function(dict.MyFunc, [arg]) */
4376 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004377 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004378 }
4379 else
4380 {
4381 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004382 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004383 use_string = TRUE;
4384 }
4385
Bram Moolenaar843b8842016-08-21 14:36:15 +02004386 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004387 {
4388 name = s;
4389 trans_name = trans_function_name(&name, FALSE,
4390 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4391 if (*name != NUL)
4392 s = NULL;
4393 }
4394
Bram Moolenaar843b8842016-08-21 14:36:15 +02004395 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4396 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004397 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004398 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004399 else if (trans_name != NULL && (is_funcref
4400 ? find_func(trans_name) == NULL
4401 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004402 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004403 else
4404 {
4405 int dict_idx = 0;
4406 int arg_idx = 0;
4407 list_T *list = NULL;
4408
4409 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4410 {
4411 char sid_buf[25];
4412 int off = *s == 's' ? 2 : 5;
4413
4414 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4415 * also be called from another script. Using trans_function_name()
4416 * would also work, but some plugins depend on the name being
4417 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004418 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004419 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004420 if (name != NULL)
4421 {
4422 STRCPY(name, sid_buf);
4423 STRCAT(name, s + off);
4424 }
4425 }
4426 else
4427 name = vim_strsave(s);
4428
4429 if (argvars[1].v_type != VAR_UNKNOWN)
4430 {
4431 if (argvars[2].v_type != VAR_UNKNOWN)
4432 {
4433 /* function(name, [args], dict) */
4434 arg_idx = 1;
4435 dict_idx = 2;
4436 }
4437 else if (argvars[1].v_type == VAR_DICT)
4438 /* function(name, dict) */
4439 dict_idx = 1;
4440 else
4441 /* function(name, [args]) */
4442 arg_idx = 1;
4443 if (dict_idx > 0)
4444 {
4445 if (argvars[dict_idx].v_type != VAR_DICT)
4446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004447 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004448 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004449 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004450 }
4451 if (argvars[dict_idx].vval.v_dict == NULL)
4452 dict_idx = 0;
4453 }
4454 if (arg_idx > 0)
4455 {
4456 if (argvars[arg_idx].v_type != VAR_LIST)
4457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004458 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004460 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 }
4462 list = argvars[arg_idx].vval.v_list;
4463 if (list == NULL || list->lv_len == 0)
4464 arg_idx = 0;
4465 }
4466 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004467 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004468 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004469 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004470
4471 /* result is a VAR_PARTIAL */
4472 if (pt == NULL)
4473 vim_free(name);
4474 else
4475 {
4476 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4477 {
4478 listitem_T *li;
4479 int i = 0;
4480 int arg_len = 0;
4481 int lv_len = 0;
4482
4483 if (arg_pt != NULL)
4484 arg_len = arg_pt->pt_argc;
4485 if (list != NULL)
4486 lv_len = list->lv_len;
4487 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004488 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004489 if (pt->pt_argv == NULL)
4490 {
4491 vim_free(pt);
4492 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004493 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004495 for (i = 0; i < arg_len; i++)
4496 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4497 if (lv_len > 0)
4498 for (li = list->lv_first; li != NULL;
4499 li = li->li_next)
4500 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004501 }
4502
4503 /* For "function(dict.func, [], dict)" and "func" is a partial
4504 * use "dict". That is backwards compatible. */
4505 if (dict_idx > 0)
4506 {
4507 /* The dict is bound explicitly, pt_auto is FALSE. */
4508 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4509 ++pt->pt_dict->dv_refcount;
4510 }
4511 else if (arg_pt != NULL)
4512 {
4513 /* If the dict was bound automatically the result is also
4514 * bound automatically. */
4515 pt->pt_dict = arg_pt->pt_dict;
4516 pt->pt_auto = arg_pt->pt_auto;
4517 if (pt->pt_dict != NULL)
4518 ++pt->pt_dict->dv_refcount;
4519 }
4520
4521 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004522 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4523 {
4524 pt->pt_func = arg_pt->pt_func;
4525 func_ptr_ref(pt->pt_func);
4526 vim_free(name);
4527 }
4528 else if (is_funcref)
4529 {
4530 pt->pt_func = find_func(trans_name);
4531 func_ptr_ref(pt->pt_func);
4532 vim_free(name);
4533 }
4534 else
4535 {
4536 pt->pt_name = name;
4537 func_ref(name);
4538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004539 }
4540 rettv->v_type = VAR_PARTIAL;
4541 rettv->vval.v_partial = pt;
4542 }
4543 else
4544 {
4545 /* result is a VAR_FUNC */
4546 rettv->v_type = VAR_FUNC;
4547 rettv->vval.v_string = name;
4548 func_ref(name);
4549 }
4550 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004551theend:
4552 vim_free(trans_name);
4553}
4554
4555/*
4556 * "funcref()" function
4557 */
4558 static void
4559f_funcref(typval_T *argvars, typval_T *rettv)
4560{
4561 common_function(argvars, rettv, TRUE);
4562}
4563
4564/*
4565 * "function()" function
4566 */
4567 static void
4568f_function(typval_T *argvars, typval_T *rettv)
4569{
4570 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004571}
4572
4573/*
4574 * "garbagecollect()" function
4575 */
4576 static void
4577f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4578{
4579 /* This is postponed until we are back at the toplevel, because we may be
4580 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4581 want_garbage_collect = TRUE;
4582
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004583 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584 garbage_collect_at_exit = TRUE;
4585}
4586
4587/*
4588 * "get()" function
4589 */
4590 static void
4591f_get(typval_T *argvars, typval_T *rettv)
4592{
4593 listitem_T *li;
4594 list_T *l;
4595 dictitem_T *di;
4596 dict_T *d;
4597 typval_T *tv = NULL;
4598
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004599 if (argvars[0].v_type == VAR_BLOB)
4600 {
4601 int error = FALSE;
4602 int idx = tv_get_number_chk(&argvars[1], &error);
4603
4604 if (!error)
4605 {
4606 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004607 if (idx < 0)
4608 idx = blob_len(argvars[0].vval.v_blob) + idx;
4609 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4610 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004611 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004612 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004613 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004614 tv = rettv;
4615 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004616 }
4617 }
4618 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004619 {
4620 if ((l = argvars[0].vval.v_list) != NULL)
4621 {
4622 int error = FALSE;
4623
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004624 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004625 if (!error && li != NULL)
4626 tv = &li->li_tv;
4627 }
4628 }
4629 else if (argvars[0].v_type == VAR_DICT)
4630 {
4631 if ((d = argvars[0].vval.v_dict) != NULL)
4632 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004633 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004634 if (di != NULL)
4635 tv = &di->di_tv;
4636 }
4637 }
4638 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4639 {
4640 partial_T *pt;
4641 partial_T fref_pt;
4642
4643 if (argvars[0].v_type == VAR_PARTIAL)
4644 pt = argvars[0].vval.v_partial;
4645 else
4646 {
4647 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4648 fref_pt.pt_name = argvars[0].vval.v_string;
4649 pt = &fref_pt;
4650 }
4651
4652 if (pt != NULL)
4653 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004654 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004655 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004656
4657 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4658 {
4659 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004660 n = partial_name(pt);
4661 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 rettv->vval.v_string = NULL;
4663 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004664 {
4665 rettv->vval.v_string = vim_strsave(n);
4666 if (rettv->v_type == VAR_FUNC)
4667 func_ref(rettv->vval.v_string);
4668 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669 }
4670 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004671 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004672 else if (STRCMP(what, "args") == 0)
4673 {
4674 rettv->v_type = VAR_LIST;
4675 if (rettv_list_alloc(rettv) == OK)
4676 {
4677 int i;
4678
4679 for (i = 0; i < pt->pt_argc; ++i)
4680 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4681 }
4682 }
4683 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 return;
4686 }
4687 }
4688 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004689 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004690
4691 if (tv == NULL)
4692 {
4693 if (argvars[2].v_type != VAR_UNKNOWN)
4694 copy_tv(&argvars[2], rettv);
4695 }
4696 else
4697 copy_tv(tv, rettv);
4698}
4699
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004700/*
4701 * Returns buffer options, variables and other attributes in a dictionary.
4702 */
4703 static dict_T *
4704get_buffer_info(buf_T *buf)
4705{
4706 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004707 tabpage_T *tp;
4708 win_T *wp;
4709 list_T *windows;
4710
4711 dict = dict_alloc();
4712 if (dict == NULL)
4713 return NULL;
4714
Bram Moolenaare0be1672018-07-08 16:50:37 +02004715 dict_add_number(dict, "bufnr", buf->b_fnum);
4716 dict_add_string(dict, "name", buf->b_ffname);
4717 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4718 : buflist_findlnum(buf));
4719 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4720 dict_add_number(dict, "listed", buf->b_p_bl);
4721 dict_add_number(dict, "changed", bufIsChanged(buf));
4722 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4723 dict_add_number(dict, "hidden",
4724 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004725
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004726 /* Get a reference to buffer variables */
4727 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004728
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004729 /* List of windows displaying this buffer */
4730 windows = list_alloc();
4731 if (windows != NULL)
4732 {
4733 FOR_ALL_TAB_WINDOWS(tp, wp)
4734 if (wp->w_buffer == buf)
4735 list_append_number(windows, (varnumber_T)wp->w_id);
4736 dict_add_list(dict, "windows", windows);
4737 }
4738
4739#ifdef FEAT_SIGNS
4740 if (buf->b_signlist != NULL)
4741 {
4742 /* List of signs placed in this buffer */
4743 list_T *signs = list_alloc();
4744 if (signs != NULL)
4745 {
4746 get_buffer_signs(buf, signs);
4747 dict_add_list(dict, "signs", signs);
4748 }
4749 }
4750#endif
4751
4752 return dict;
4753}
4754
4755/*
4756 * "getbufinfo()" function
4757 */
4758 static void
4759f_getbufinfo(typval_T *argvars, typval_T *rettv)
4760{
4761 buf_T *buf = NULL;
4762 buf_T *argbuf = NULL;
4763 dict_T *d;
4764 int filtered = FALSE;
4765 int sel_buflisted = FALSE;
4766 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004767 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004768
4769 if (rettv_list_alloc(rettv) != OK)
4770 return;
4771
4772 /* List of all the buffers or selected buffers */
4773 if (argvars[0].v_type == VAR_DICT)
4774 {
4775 dict_T *sel_d = argvars[0].vval.v_dict;
4776
4777 if (sel_d != NULL)
4778 {
4779 dictitem_T *di;
4780
4781 filtered = TRUE;
4782
4783 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004784 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004785 sel_buflisted = TRUE;
4786
4787 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004788 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004789 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004790
4791 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004793 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004794 }
4795 }
4796 else if (argvars[0].v_type != VAR_UNKNOWN)
4797 {
4798 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004799 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004800 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004801 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 --emsg_off;
4803 if (argbuf == NULL)
4804 return;
4805 }
4806
4807 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004808 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004809 {
4810 if (argbuf != NULL && argbuf != buf)
4811 continue;
4812 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004813 || (sel_buflisted && !buf->b_p_bl)
4814 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004815 continue;
4816
4817 d = get_buffer_info(buf);
4818 if (d != NULL)
4819 list_append_dict(rettv->vval.v_list, d);
4820 if (argbuf != NULL)
4821 return;
4822 }
4823}
4824
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004825/*
4826 * Get line or list of lines from buffer "buf" into "rettv".
4827 * Return a range (from start to end) of lines in rettv from the specified
4828 * buffer.
4829 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4830 */
4831 static void
4832get_buffer_lines(
4833 buf_T *buf,
4834 linenr_T start,
4835 linenr_T end,
4836 int retlist,
4837 typval_T *rettv)
4838{
4839 char_u *p;
4840
4841 rettv->v_type = VAR_STRING;
4842 rettv->vval.v_string = NULL;
4843 if (retlist && rettv_list_alloc(rettv) == FAIL)
4844 return;
4845
4846 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4847 return;
4848
4849 if (!retlist)
4850 {
4851 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4852 p = ml_get_buf(buf, start, FALSE);
4853 else
4854 p = (char_u *)"";
4855 rettv->vval.v_string = vim_strsave(p);
4856 }
4857 else
4858 {
4859 if (end < start)
4860 return;
4861
4862 if (start < 1)
4863 start = 1;
4864 if (end > buf->b_ml.ml_line_count)
4865 end = buf->b_ml.ml_line_count;
4866 while (start <= end)
4867 if (list_append_string(rettv->vval.v_list,
4868 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4869 break;
4870 }
4871}
4872
4873/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004874 * "getbufline()" function
4875 */
4876 static void
4877f_getbufline(typval_T *argvars, typval_T *rettv)
4878{
4879 linenr_T lnum;
4880 linenr_T end;
4881 buf_T *buf;
4882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004883 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004884 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004885 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886 --emsg_off;
4887
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004888 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004889 if (argvars[2].v_type == VAR_UNKNOWN)
4890 end = lnum;
4891 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004892 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004893
4894 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4895}
4896
4897/*
4898 * "getbufvar()" function
4899 */
4900 static void
4901f_getbufvar(typval_T *argvars, typval_T *rettv)
4902{
4903 buf_T *buf;
4904 buf_T *save_curbuf;
4905 char_u *varname;
4906 dictitem_T *v;
4907 int done = FALSE;
4908
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004909 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4910 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004911 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004912 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004913
4914 rettv->v_type = VAR_STRING;
4915 rettv->vval.v_string = NULL;
4916
4917 if (buf != NULL && varname != NULL)
4918 {
4919 /* set curbuf to be our buf, temporarily */
4920 save_curbuf = curbuf;
4921 curbuf = buf;
4922
Bram Moolenaar30567352016-08-27 21:25:44 +02004923 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004925 if (varname[1] == NUL)
4926 {
4927 /* get all buffer-local options in a dict */
4928 dict_T *opts = get_winbuf_options(TRUE);
4929
4930 if (opts != NULL)
4931 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004932 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004933 done = TRUE;
4934 }
4935 }
4936 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4937 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004938 done = TRUE;
4939 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004940 else
4941 {
4942 /* Look up the variable. */
4943 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4944 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4945 'b', varname, FALSE);
4946 if (v != NULL)
4947 {
4948 copy_tv(&v->di_tv, rettv);
4949 done = TRUE;
4950 }
4951 }
4952
4953 /* restore previous notion of curbuf */
4954 curbuf = save_curbuf;
4955 }
4956
4957 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4958 /* use the default value */
4959 copy_tv(&argvars[2], rettv);
4960
4961 --emsg_off;
4962}
4963
4964/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004965 * "getchangelist()" function
4966 */
4967 static void
4968f_getchangelist(typval_T *argvars, typval_T *rettv)
4969{
4970#ifdef FEAT_JUMPLIST
4971 buf_T *buf;
4972 int i;
4973 list_T *l;
4974 dict_T *d;
4975#endif
4976
4977 if (rettv_list_alloc(rettv) != OK)
4978 return;
4979
4980#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004981 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004982 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004983 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004984 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004985 if (buf == NULL)
4986 return;
4987
4988 l = list_alloc();
4989 if (l == NULL)
4990 return;
4991
4992 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4993 return;
4994 /*
4995 * The current window change list index tracks only the position in the
4996 * current buffer change list. For other buffers, use the change list
4997 * length as the current index.
4998 */
4999 list_append_number(rettv->vval.v_list,
5000 (varnumber_T)((buf == curwin->w_buffer)
5001 ? curwin->w_changelistidx : buf->b_changelistlen));
5002
5003 for (i = 0; i < buf->b_changelistlen; ++i)
5004 {
5005 if (buf->b_changelist[i].lnum == 0)
5006 continue;
5007 if ((d = dict_alloc()) == NULL)
5008 return;
5009 if (list_append_dict(l, d) == FAIL)
5010 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005011 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5012 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005013 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005014 }
5015#endif
5016}
5017/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005018 * "getchar()" function
5019 */
5020 static void
5021f_getchar(typval_T *argvars, typval_T *rettv)
5022{
5023 varnumber_T n;
5024 int error = FALSE;
5025
Bram Moolenaar84d93902018-09-11 20:10:20 +02005026#ifdef MESSAGE_QUEUE
5027 // vpeekc() used to check for messages, but that caused problems, invoking
5028 // a callback where it was not expected. Some plugins use getchar(1) in a
5029 // loop to await a message, therefore make sure we check for messages here.
5030 parse_queued_messages();
5031#endif
5032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005033 /* Position the cursor. Needed after a message that ends in a space. */
5034 windgoto(msg_row, msg_col);
5035
5036 ++no_mapping;
5037 ++allow_keys;
5038 for (;;)
5039 {
5040 if (argvars[0].v_type == VAR_UNKNOWN)
5041 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005042 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005043 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 /* getchar(1): only check if char avail */
5045 n = vpeekc_any();
5046 else if (error || vpeekc_any() == NUL)
5047 /* illegal argument or getchar(0) and no char avail: return zero */
5048 n = 0;
5049 else
5050 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005051 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005052
5053 if (n == K_IGNORE)
5054 continue;
5055 break;
5056 }
5057 --no_mapping;
5058 --allow_keys;
5059
5060 set_vim_var_nr(VV_MOUSE_WIN, 0);
5061 set_vim_var_nr(VV_MOUSE_WINID, 0);
5062 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5063 set_vim_var_nr(VV_MOUSE_COL, 0);
5064
5065 rettv->vval.v_number = n;
5066 if (IS_SPECIAL(n) || mod_mask != 0)
5067 {
5068 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5069 int i = 0;
5070
5071 /* Turn a special key into three bytes, plus modifier. */
5072 if (mod_mask != 0)
5073 {
5074 temp[i++] = K_SPECIAL;
5075 temp[i++] = KS_MODIFIER;
5076 temp[i++] = mod_mask;
5077 }
5078 if (IS_SPECIAL(n))
5079 {
5080 temp[i++] = K_SPECIAL;
5081 temp[i++] = K_SECOND(n);
5082 temp[i++] = K_THIRD(n);
5083 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005084 else if (has_mbyte)
5085 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005086 else
5087 temp[i++] = n;
5088 temp[i++] = NUL;
5089 rettv->v_type = VAR_STRING;
5090 rettv->vval.v_string = vim_strsave(temp);
5091
5092#ifdef FEAT_MOUSE
5093 if (is_mouse_key(n))
5094 {
5095 int row = mouse_row;
5096 int col = mouse_col;
5097 win_T *win;
5098 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005100 int winnr = 1;
5101
5102 if (row >= 0 && col >= 0)
5103 {
5104 /* Find the window at the mouse coordinates and compute the
5105 * text position. */
5106 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005107 if (win == NULL)
5108 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 for (wp = firstwin; wp != win; wp = wp->w_next)
5111 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5113 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5114 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5115 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5116 }
5117 }
5118#endif
5119 }
5120}
5121
5122/*
5123 * "getcharmod()" function
5124 */
5125 static void
5126f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5127{
5128 rettv->vval.v_number = mod_mask;
5129}
5130
5131/*
5132 * "getcharsearch()" function
5133 */
5134 static void
5135f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5136{
5137 if (rettv_dict_alloc(rettv) != FAIL)
5138 {
5139 dict_T *dict = rettv->vval.v_dict;
5140
Bram Moolenaare0be1672018-07-08 16:50:37 +02005141 dict_add_string(dict, "char", last_csearch());
5142 dict_add_number(dict, "forward", last_csearch_forward());
5143 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005144 }
5145}
5146
5147/*
5148 * "getcmdline()" function
5149 */
5150 static void
5151f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5152{
5153 rettv->v_type = VAR_STRING;
5154 rettv->vval.v_string = get_cmdline_str();
5155}
5156
5157/*
5158 * "getcmdpos()" function
5159 */
5160 static void
5161f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5162{
5163 rettv->vval.v_number = get_cmdline_pos() + 1;
5164}
5165
5166/*
5167 * "getcmdtype()" function
5168 */
5169 static void
5170f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5171{
5172 rettv->v_type = VAR_STRING;
5173 rettv->vval.v_string = alloc(2);
5174 if (rettv->vval.v_string != NULL)
5175 {
5176 rettv->vval.v_string[0] = get_cmdline_type();
5177 rettv->vval.v_string[1] = NUL;
5178 }
5179}
5180
5181/*
5182 * "getcmdwintype()" function
5183 */
5184 static void
5185f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5186{
5187 rettv->v_type = VAR_STRING;
5188 rettv->vval.v_string = NULL;
5189#ifdef FEAT_CMDWIN
5190 rettv->vval.v_string = alloc(2);
5191 if (rettv->vval.v_string != NULL)
5192 {
5193 rettv->vval.v_string[0] = cmdwin_type;
5194 rettv->vval.v_string[1] = NUL;
5195 }
5196#endif
5197}
5198
5199#if defined(FEAT_CMDL_COMPL)
5200/*
5201 * "getcompletion()" function
5202 */
5203 static void
5204f_getcompletion(typval_T *argvars, typval_T *rettv)
5205{
5206 char_u *pat;
5207 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005208 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005209 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5210 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005211
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005213 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 if (p_wic)
5216 options |= WILD_ICASE;
5217
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005218 /* For filtered results, 'wildignore' is used */
5219 if (!filtered)
5220 options |= WILD_KEEP_ALL;
5221
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005223 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005225 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005226 if (xpc.xp_context == EXPAND_NOTHING)
5227 {
5228 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005229 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005230 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005231 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005232 return;
5233 }
5234
5235# if defined(FEAT_MENU)
5236 if (xpc.xp_context == EXPAND_MENUS)
5237 {
5238 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5239 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5240 }
5241# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005242#ifdef FEAT_CSCOPE
5243 if (xpc.xp_context == EXPAND_CSCOPE)
5244 {
5245 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5246 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5247 }
5248#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005249#ifdef FEAT_SIGNS
5250 if (xpc.xp_context == EXPAND_SIGN)
5251 {
5252 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5253 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5254 }
5255#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005256
5257 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5258 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5259 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005260 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005261
5262 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5263
5264 for (i = 0; i < xpc.xp_numfiles; i++)
5265 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5266 }
5267 vim_free(pat);
5268 ExpandCleanup(&xpc);
5269}
5270#endif
5271
5272/*
5273 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005274 *
5275 * Return the current working directory of a window in a tab page.
5276 * First optional argument 'winnr' is the window number or -1 and the second
5277 * optional argument 'tabnr' is the tab page number.
5278 *
5279 * If no arguments are supplied, then return the directory of the current
5280 * window.
5281 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5282 * the specified window.
5283 * If 'winnr' is 0 then return the directory of the current window.
5284 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5285 * directory of the specified tab page. Otherwise return the directory of the
5286 * specified window in the specified tab page.
5287 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005288 */
5289 static void
5290f_getcwd(typval_T *argvars, typval_T *rettv)
5291{
5292 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005293 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005294 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005295 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005296
5297 rettv->v_type = VAR_STRING;
5298 rettv->vval.v_string = NULL;
5299
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005300 if (argvars[0].v_type == VAR_NUMBER
5301 && argvars[0].vval.v_number == -1
5302 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005303 global = TRUE;
5304 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005305 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005306
5307 if (wp != NULL && wp->w_localdir != NULL)
5308 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005309 else if (tp != NULL && tp->tp_localdir != NULL)
5310 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5311 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005312 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005313 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005314 rettv->vval.v_string = vim_strsave(globaldir);
5315 else
5316 {
5317 cwd = alloc(MAXPATHL);
5318 if (cwd != NULL)
5319 {
5320 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5321 rettv->vval.v_string = vim_strsave(cwd);
5322 vim_free(cwd);
5323 }
5324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005326#ifdef BACKSLASH_IN_FILENAME
5327 if (rettv->vval.v_string != NULL)
5328 slash_adjust(rettv->vval.v_string);
5329#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005330}
5331
5332/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005333 * "getenv()" function
5334 */
5335 static void
5336f_getenv(typval_T *argvars, typval_T *rettv)
5337{
5338 int mustfree = FALSE;
5339 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5340
5341 if (p == NULL)
5342 {
5343 rettv->v_type = VAR_SPECIAL;
5344 rettv->vval.v_number = VVAL_NULL;
5345 return;
5346 }
5347 if (!mustfree)
5348 p = vim_strsave(p);
5349 rettv->vval.v_string = p;
5350 rettv->v_type = VAR_STRING;
5351}
5352
5353/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005354 * "getfontname()" function
5355 */
5356 static void
5357f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5358{
5359 rettv->v_type = VAR_STRING;
5360 rettv->vval.v_string = NULL;
5361#ifdef FEAT_GUI
5362 if (gui.in_use)
5363 {
5364 GuiFont font;
5365 char_u *name = NULL;
5366
5367 if (argvars[0].v_type == VAR_UNKNOWN)
5368 {
5369 /* Get the "Normal" font. Either the name saved by
5370 * hl_set_font_name() or from the font ID. */
5371 font = gui.norm_font;
5372 name = hl_get_font_name();
5373 }
5374 else
5375 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005376 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005377 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5378 return;
5379 font = gui_mch_get_font(name, FALSE);
5380 if (font == NOFONT)
5381 return; /* Invalid font name, return empty string. */
5382 }
5383 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5384 if (argvars[0].v_type != VAR_UNKNOWN)
5385 gui_mch_free_font(font);
5386 }
5387#endif
5388}
5389
5390/*
5391 * "getfperm({fname})" function
5392 */
5393 static void
5394f_getfperm(typval_T *argvars, typval_T *rettv)
5395{
5396 char_u *fname;
5397 stat_T st;
5398 char_u *perm = NULL;
5399 char_u flags[] = "rwx";
5400 int i;
5401
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005402 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005403
5404 rettv->v_type = VAR_STRING;
5405 if (mch_stat((char *)fname, &st) >= 0)
5406 {
5407 perm = vim_strsave((char_u *)"---------");
5408 if (perm != NULL)
5409 {
5410 for (i = 0; i < 9; i++)
5411 {
5412 if (st.st_mode & (1 << (8 - i)))
5413 perm[i] = flags[i % 3];
5414 }
5415 }
5416 }
5417 rettv->vval.v_string = perm;
5418}
5419
5420/*
5421 * "getfsize({fname})" function
5422 */
5423 static void
5424f_getfsize(typval_T *argvars, typval_T *rettv)
5425{
5426 char_u *fname;
5427 stat_T st;
5428
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005429 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005430
5431 rettv->v_type = VAR_NUMBER;
5432
5433 if (mch_stat((char *)fname, &st) >= 0)
5434 {
5435 if (mch_isdir(fname))
5436 rettv->vval.v_number = 0;
5437 else
5438 {
5439 rettv->vval.v_number = (varnumber_T)st.st_size;
5440
5441 /* non-perfect check for overflow */
5442 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5443 rettv->vval.v_number = -2;
5444 }
5445 }
5446 else
5447 rettv->vval.v_number = -1;
5448}
5449
5450/*
5451 * "getftime({fname})" function
5452 */
5453 static void
5454f_getftime(typval_T *argvars, typval_T *rettv)
5455{
5456 char_u *fname;
5457 stat_T st;
5458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005459 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005460
5461 if (mch_stat((char *)fname, &st) >= 0)
5462 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5463 else
5464 rettv->vval.v_number = -1;
5465}
5466
5467/*
5468 * "getftype({fname})" function
5469 */
5470 static void
5471f_getftype(typval_T *argvars, typval_T *rettv)
5472{
5473 char_u *fname;
5474 stat_T st;
5475 char_u *type = NULL;
5476 char *t;
5477
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005478 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005479
5480 rettv->v_type = VAR_STRING;
5481 if (mch_lstat((char *)fname, &st) >= 0)
5482 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005483 if (S_ISREG(st.st_mode))
5484 t = "file";
5485 else if (S_ISDIR(st.st_mode))
5486 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005487 else if (S_ISLNK(st.st_mode))
5488 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005489 else if (S_ISBLK(st.st_mode))
5490 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005491 else if (S_ISCHR(st.st_mode))
5492 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005493 else if (S_ISFIFO(st.st_mode))
5494 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005495 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005496 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005497 else
5498 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005499 type = vim_strsave((char_u *)t);
5500 }
5501 rettv->vval.v_string = type;
5502}
5503
5504/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005505 * "getjumplist()" function
5506 */
5507 static void
5508f_getjumplist(typval_T *argvars, typval_T *rettv)
5509{
5510#ifdef FEAT_JUMPLIST
5511 win_T *wp;
5512 int i;
5513 list_T *l;
5514 dict_T *d;
5515#endif
5516
5517 if (rettv_list_alloc(rettv) != OK)
5518 return;
5519
5520#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005521 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005522 if (wp == NULL)
5523 return;
5524
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005525 cleanup_jumplist(wp, TRUE);
5526
Bram Moolenaar4f505882018-02-10 21:06:32 +01005527 l = list_alloc();
5528 if (l == NULL)
5529 return;
5530
5531 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5532 return;
5533 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5534
5535 for (i = 0; i < wp->w_jumplistlen; ++i)
5536 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005537 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5538 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005539 if ((d = dict_alloc()) == NULL)
5540 return;
5541 if (list_append_dict(l, d) == FAIL)
5542 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005543 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5544 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005545 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005546 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005547 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005548 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005549 }
5550#endif
5551}
5552
5553/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005554 * "getline(lnum, [end])" function
5555 */
5556 static void
5557f_getline(typval_T *argvars, typval_T *rettv)
5558{
5559 linenr_T lnum;
5560 linenr_T end;
5561 int retlist;
5562
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005563 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005564 if (argvars[1].v_type == VAR_UNKNOWN)
5565 {
5566 end = 0;
5567 retlist = FALSE;
5568 }
5569 else
5570 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005571 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005572 retlist = TRUE;
5573 }
5574
5575 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5576}
5577
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005578#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005579 static void
5580get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5581{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005582 if (what_arg->v_type == VAR_UNKNOWN)
5583 {
5584 if (rettv_list_alloc(rettv) == OK)
5585 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005586 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005587 }
5588 else
5589 {
5590 if (rettv_dict_alloc(rettv) == OK)
5591 if (is_qf || (wp != NULL))
5592 {
5593 if (what_arg->v_type == VAR_DICT)
5594 {
5595 dict_T *d = what_arg->vval.v_dict;
5596
5597 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005598 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005599 }
5600 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005601 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005602 }
5603 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005604}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005605#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005606
5607/*
5608 * "getloclist()" function
5609 */
5610 static void
5611f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5612{
5613#ifdef FEAT_QUICKFIX
5614 win_T *wp;
5615
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005616 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005617 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5618#endif
5619}
5620
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005621/*
5622 * "getmatches()" function
5623 */
5624 static void
5625f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5626{
5627#ifdef FEAT_SEARCH_EXTRA
5628 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005629 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005630 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005631 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632
Bram Moolenaaraff74912019-03-30 18:11:49 +01005633 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5634 return;
5635
5636 cur = win->w_match_head;
5637 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005638 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005639 dict = dict_alloc();
5640 if (dict == NULL)
5641 return;
5642 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005644 /* match added with matchaddpos() */
5645 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005646 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005647 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005648 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005649 list_T *l;
5650
5651 llpos = &cur->pos.pos[i];
5652 if (llpos->lnum == 0)
5653 break;
5654 l = list_alloc();
5655 if (l == NULL)
5656 break;
5657 list_append_number(l, (varnumber_T)llpos->lnum);
5658 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 list_append_number(l, (varnumber_T)llpos->col);
5661 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005662 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005663 sprintf(buf, "pos%d", i + 1);
5664 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005666 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005667 else
5668 {
5669 dict_add_string(dict, "pattern", cur->pattern);
5670 }
5671 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5672 dict_add_number(dict, "priority", (long)cur->priority);
5673 dict_add_number(dict, "id", (long)cur->id);
5674# if defined(FEAT_CONCEAL)
5675 if (cur->conceal_char)
5676 {
5677 char_u buf[MB_MAXBYTES + 1];
5678
5679 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5680 dict_add_string(dict, "conceal", (char_u *)&buf);
5681 }
5682# endif
5683 list_append_dict(rettv->vval.v_list, dict);
5684 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005685 }
5686#endif
5687}
5688
5689/*
5690 * "getpid()" function
5691 */
5692 static void
5693f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5694{
5695 rettv->vval.v_number = mch_get_pid();
5696}
5697
5698 static void
5699getpos_both(
5700 typval_T *argvars,
5701 typval_T *rettv,
5702 int getcurpos)
5703{
5704 pos_T *fp;
5705 list_T *l;
5706 int fnum = -1;
5707
5708 if (rettv_list_alloc(rettv) == OK)
5709 {
5710 l = rettv->vval.v_list;
5711 if (getcurpos)
5712 fp = &curwin->w_cursor;
5713 else
5714 fp = var2fpos(&argvars[0], TRUE, &fnum);
5715 if (fnum != -1)
5716 list_append_number(l, (varnumber_T)fnum);
5717 else
5718 list_append_number(l, (varnumber_T)0);
5719 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5720 : (varnumber_T)0);
5721 list_append_number(l, (fp != NULL)
5722 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5723 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005724 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 (varnumber_T)0);
5726 if (getcurpos)
5727 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005728 int save_set_curswant = curwin->w_set_curswant;
5729 colnr_T save_curswant = curwin->w_curswant;
5730 colnr_T save_virtcol = curwin->w_virtcol;
5731
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005732 update_curswant();
5733 list_append_number(l, curwin->w_curswant == MAXCOL ?
5734 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005735
5736 // Do not change "curswant", as it is unexpected that a get
5737 // function has a side effect.
5738 if (save_set_curswant)
5739 {
5740 curwin->w_set_curswant = save_set_curswant;
5741 curwin->w_curswant = save_curswant;
5742 curwin->w_virtcol = save_virtcol;
5743 curwin->w_valid &= ~VALID_VIRTCOL;
5744 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 }
5746 }
5747 else
5748 rettv->vval.v_number = FALSE;
5749}
5750
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005751/*
5752 * "getcurpos()" function
5753 */
5754 static void
5755f_getcurpos(typval_T *argvars, typval_T *rettv)
5756{
5757 getpos_both(argvars, rettv, TRUE);
5758}
5759
5760/*
5761 * "getpos(string)" function
5762 */
5763 static void
5764f_getpos(typval_T *argvars, typval_T *rettv)
5765{
5766 getpos_both(argvars, rettv, FALSE);
5767}
5768
5769/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005770 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005771 */
5772 static void
5773f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5774{
5775#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005776 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005777#endif
5778}
5779
5780/*
5781 * "getreg()" function
5782 */
5783 static void
5784f_getreg(typval_T *argvars, typval_T *rettv)
5785{
5786 char_u *strregname;
5787 int regname;
5788 int arg2 = FALSE;
5789 int return_list = FALSE;
5790 int error = FALSE;
5791
5792 if (argvars[0].v_type != VAR_UNKNOWN)
5793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005794 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005795 error = strregname == NULL;
5796 if (argvars[1].v_type != VAR_UNKNOWN)
5797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005798 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005799 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005800 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005801 }
5802 }
5803 else
5804 strregname = get_vim_var_str(VV_REG);
5805
5806 if (error)
5807 return;
5808
5809 regname = (strregname == NULL ? '"' : *strregname);
5810 if (regname == 0)
5811 regname = '"';
5812
5813 if (return_list)
5814 {
5815 rettv->v_type = VAR_LIST;
5816 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5817 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5818 if (rettv->vval.v_list == NULL)
5819 (void)rettv_list_alloc(rettv);
5820 else
5821 ++rettv->vval.v_list->lv_refcount;
5822 }
5823 else
5824 {
5825 rettv->v_type = VAR_STRING;
5826 rettv->vval.v_string = get_reg_contents(regname,
5827 arg2 ? GREG_EXPR_SRC : 0);
5828 }
5829}
5830
5831/*
5832 * "getregtype()" function
5833 */
5834 static void
5835f_getregtype(typval_T *argvars, typval_T *rettv)
5836{
5837 char_u *strregname;
5838 int regname;
5839 char_u buf[NUMBUFLEN + 2];
5840 long reglen = 0;
5841
5842 if (argvars[0].v_type != VAR_UNKNOWN)
5843 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005844 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005845 if (strregname == NULL) /* type error; errmsg already given */
5846 {
5847 rettv->v_type = VAR_STRING;
5848 rettv->vval.v_string = NULL;
5849 return;
5850 }
5851 }
5852 else
5853 /* Default to v:register */
5854 strregname = get_vim_var_str(VV_REG);
5855
5856 regname = (strregname == NULL ? '"' : *strregname);
5857 if (regname == 0)
5858 regname = '"';
5859
5860 buf[0] = NUL;
5861 buf[1] = NUL;
5862 switch (get_reg_type(regname, &reglen))
5863 {
5864 case MLINE: buf[0] = 'V'; break;
5865 case MCHAR: buf[0] = 'v'; break;
5866 case MBLOCK:
5867 buf[0] = Ctrl_V;
5868 sprintf((char *)buf + 1, "%ld", reglen + 1);
5869 break;
5870 }
5871 rettv->v_type = VAR_STRING;
5872 rettv->vval.v_string = vim_strsave(buf);
5873}
5874
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005875/*
5876 * Returns information (variables, options, etc.) about a tab page
5877 * as a dictionary.
5878 */
5879 static dict_T *
5880get_tabpage_info(tabpage_T *tp, int tp_idx)
5881{
5882 win_T *wp;
5883 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884 list_T *l;
5885
5886 dict = dict_alloc();
5887 if (dict == NULL)
5888 return NULL;
5889
Bram Moolenaare0be1672018-07-08 16:50:37 +02005890 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005891
5892 l = list_alloc();
5893 if (l != NULL)
5894 {
5895 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5896 wp; wp = wp->w_next)
5897 list_append_number(l, (varnumber_T)wp->w_id);
5898 dict_add_list(dict, "windows", l);
5899 }
5900
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005901 /* Make a reference to tabpage variables */
5902 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005903
5904 return dict;
5905}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906
5907/*
5908 * "gettabinfo()" function
5909 */
5910 static void
5911f_gettabinfo(typval_T *argvars, typval_T *rettv)
5912{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005913 tabpage_T *tp, *tparg = NULL;
5914 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005915 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005916
5917 if (rettv_list_alloc(rettv) != OK)
5918 return;
5919
5920 if (argvars[0].v_type != VAR_UNKNOWN)
5921 {
5922 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005923 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005924 if (tparg == NULL)
5925 return;
5926 }
5927
5928 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005929 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005930 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005931 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005932 if (tparg != NULL && tp != tparg)
5933 continue;
5934 d = get_tabpage_info(tp, tpnr);
5935 if (d != NULL)
5936 list_append_dict(rettv->vval.v_list, d);
5937 if (tparg != NULL)
5938 return;
5939 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005940}
5941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005942/*
5943 * "gettabvar()" function
5944 */
5945 static void
5946f_gettabvar(typval_T *argvars, typval_T *rettv)
5947{
5948 win_T *oldcurwin;
5949 tabpage_T *tp, *oldtabpage;
5950 dictitem_T *v;
5951 char_u *varname;
5952 int done = FALSE;
5953
5954 rettv->v_type = VAR_STRING;
5955 rettv->vval.v_string = NULL;
5956
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005957 varname = tv_get_string_chk(&argvars[1]);
5958 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 if (tp != NULL && varname != NULL)
5960 {
5961 /* Set tp to be our tabpage, temporarily. Also set the window to the
5962 * first window in the tabpage, otherwise the window is not valid. */
5963 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005964 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5965 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005966 {
5967 /* look up the variable */
5968 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5969 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5970 if (v != NULL)
5971 {
5972 copy_tv(&v->di_tv, rettv);
5973 done = TRUE;
5974 }
5975 }
5976
5977 /* restore previous notion of curwin */
5978 restore_win(oldcurwin, oldtabpage, TRUE);
5979 }
5980
5981 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5982 copy_tv(&argvars[2], rettv);
5983}
5984
5985/*
5986 * "gettabwinvar()" function
5987 */
5988 static void
5989f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5990{
5991 getwinvar(argvars, rettv, 1);
5992}
5993
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005994/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005995 * "gettagstack()" function
5996 */
5997 static void
5998f_gettagstack(typval_T *argvars, typval_T *rettv)
5999{
6000 win_T *wp = curwin; // default is current window
6001
6002 if (rettv_dict_alloc(rettv) != OK)
6003 return;
6004
6005 if (argvars[0].v_type != VAR_UNKNOWN)
6006 {
6007 wp = find_win_by_nr_or_id(&argvars[0]);
6008 if (wp == NULL)
6009 return;
6010 }
6011
6012 get_tagstack(wp, rettv->vval.v_dict);
6013}
6014
6015/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006016 * Returns information about a window as a dictionary.
6017 */
6018 static dict_T *
6019get_win_info(win_T *wp, short tpnr, short winnr)
6020{
6021 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006022
6023 dict = dict_alloc();
6024 if (dict == NULL)
6025 return NULL;
6026
Bram Moolenaare0be1672018-07-08 16:50:37 +02006027 dict_add_number(dict, "tabnr", tpnr);
6028 dict_add_number(dict, "winnr", winnr);
6029 dict_add_number(dict, "winid", wp->w_id);
6030 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006031 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006032 dict_add_number(dict, "topline", wp->w_topline);
6033 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006034#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006036#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006037 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006038 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006039 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006040
Bram Moolenaar69905d12017-08-13 18:14:47 +02006041#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006042 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006043#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006044#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006045 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6046 dict_add_number(dict, "loclist",
6047 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006048#endif
6049
Bram Moolenaar30567352016-08-27 21:25:44 +02006050 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006051 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006052
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006053 return dict;
6054}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006055
6056/*
6057 * "getwininfo()" function
6058 */
6059 static void
6060f_getwininfo(typval_T *argvars, typval_T *rettv)
6061{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006062 tabpage_T *tp;
6063 win_T *wp = NULL, *wparg = NULL;
6064 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006065 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066
6067 if (rettv_list_alloc(rettv) != OK)
6068 return;
6069
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006070 if (argvars[0].v_type != VAR_UNKNOWN)
6071 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006072 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006073 if (wparg == NULL)
6074 return;
6075 }
6076
6077 /* Collect information about either all the windows across all the tab
6078 * pages or one particular window.
6079 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006080 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006081 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006082 tabnr++;
6083 winnr = 0;
6084 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006085 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006086 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006087 if (wparg != NULL && wp != wparg)
6088 continue;
6089 d = get_win_info(wp, tabnr, winnr);
6090 if (d != NULL)
6091 list_append_dict(rettv->vval.v_list, d);
6092 if (wparg != NULL)
6093 /* found information about a specific window */
6094 return;
6095 }
6096 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006097}
6098
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006100 * "win_findbuf()" function
6101 */
6102 static void
6103f_win_findbuf(typval_T *argvars, typval_T *rettv)
6104{
6105 if (rettv_list_alloc(rettv) != FAIL)
6106 win_findbuf(argvars, rettv->vval.v_list);
6107}
6108
6109/*
6110 * "win_getid()" function
6111 */
6112 static void
6113f_win_getid(typval_T *argvars, typval_T *rettv)
6114{
6115 rettv->vval.v_number = win_getid(argvars);
6116}
6117
6118/*
6119 * "win_gotoid()" function
6120 */
6121 static void
6122f_win_gotoid(typval_T *argvars, typval_T *rettv)
6123{
6124 rettv->vval.v_number = win_gotoid(argvars);
6125}
6126
6127/*
6128 * "win_id2tabwin()" function
6129 */
6130 static void
6131f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6132{
6133 if (rettv_list_alloc(rettv) != FAIL)
6134 win_id2tabwin(argvars, rettv->vval.v_list);
6135}
6136
6137/*
6138 * "win_id2win()" function
6139 */
6140 static void
6141f_win_id2win(typval_T *argvars, typval_T *rettv)
6142{
6143 rettv->vval.v_number = win_id2win(argvars);
6144}
6145
6146/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006147 * "win_screenpos()" function
6148 */
6149 static void
6150f_win_screenpos(typval_T *argvars, typval_T *rettv)
6151{
6152 win_T *wp;
6153
6154 if (rettv_list_alloc(rettv) == FAIL)
6155 return;
6156
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006157 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006158 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6159 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6160}
6161
6162/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006163 * "getwinpos({timeout})" function
6164 */
6165 static void
6166f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6167{
6168 int x = -1;
6169 int y = -1;
6170
6171 if (rettv_list_alloc(rettv) == FAIL)
6172 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006173#if defined(FEAT_GUI) \
6174 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6175 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006176 {
6177 varnumber_T timeout = 100;
6178
6179 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006180 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006181
6182 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006183 }
6184#endif
6185 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6186 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6187}
6188
6189
6190/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006191 * "getwinposx()" function
6192 */
6193 static void
6194f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6195{
6196 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006197#if defined(FEAT_GUI) \
6198 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6199 || defined(MSWIN)
6200
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006201 {
6202 int x, y;
6203
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006204 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006205 rettv->vval.v_number = x;
6206 }
6207#endif
6208}
6209
6210/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006211 * "getwinposy()" function
6212 */
6213 static void
6214f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6215{
6216 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006217#if defined(FEAT_GUI) \
6218 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6219 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 {
6221 int x, y;
6222
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006223 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006224 rettv->vval.v_number = y;
6225 }
6226#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006227}
6228
6229/*
6230 * "getwinvar()" function
6231 */
6232 static void
6233f_getwinvar(typval_T *argvars, typval_T *rettv)
6234{
6235 getwinvar(argvars, rettv, 0);
6236}
6237
6238/*
6239 * "glob()" function
6240 */
6241 static void
6242f_glob(typval_T *argvars, typval_T *rettv)
6243{
6244 int options = WILD_SILENT|WILD_USE_NL;
6245 expand_T xpc;
6246 int error = FALSE;
6247
6248 /* When the optional second argument is non-zero, don't remove matches
6249 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6250 rettv->v_type = VAR_STRING;
6251 if (argvars[1].v_type != VAR_UNKNOWN)
6252 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 options |= WILD_KEEP_ALL;
6255 if (argvars[2].v_type != VAR_UNKNOWN)
6256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006257 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006258 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006259 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006260 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006261 options |= WILD_ALLLINKS;
6262 }
6263 }
6264 if (!error)
6265 {
6266 ExpandInit(&xpc);
6267 xpc.xp_context = EXPAND_FILES;
6268 if (p_wic)
6269 options += WILD_ICASE;
6270 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006271 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006272 NULL, options, WILD_ALL);
6273 else if (rettv_list_alloc(rettv) != FAIL)
6274 {
6275 int i;
6276
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006277 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006278 NULL, options, WILD_ALL_KEEP);
6279 for (i = 0; i < xpc.xp_numfiles; i++)
6280 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6281
6282 ExpandCleanup(&xpc);
6283 }
6284 }
6285 else
6286 rettv->vval.v_string = NULL;
6287}
6288
6289/*
6290 * "globpath()" function
6291 */
6292 static void
6293f_globpath(typval_T *argvars, typval_T *rettv)
6294{
6295 int flags = 0;
6296 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006297 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006298 int error = FALSE;
6299 garray_T ga;
6300 int i;
6301
6302 /* When the optional second argument is non-zero, don't remove matches
6303 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6304 rettv->v_type = VAR_STRING;
6305 if (argvars[2].v_type != VAR_UNKNOWN)
6306 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 flags |= WILD_KEEP_ALL;
6309 if (argvars[3].v_type != VAR_UNKNOWN)
6310 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006311 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006312 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 flags |= WILD_ALLLINKS;
6316 }
6317 }
6318 if (file != NULL && !error)
6319 {
6320 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006321 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006322 if (rettv->v_type == VAR_STRING)
6323 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6324 else if (rettv_list_alloc(rettv) != FAIL)
6325 for (i = 0; i < ga.ga_len; ++i)
6326 list_append_string(rettv->vval.v_list,
6327 ((char_u **)(ga.ga_data))[i], -1);
6328 ga_clear_strings(&ga);
6329 }
6330 else
6331 rettv->vval.v_string = NULL;
6332}
6333
6334/*
6335 * "glob2regpat()" function
6336 */
6337 static void
6338f_glob2regpat(typval_T *argvars, typval_T *rettv)
6339{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006340 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006341
6342 rettv->v_type = VAR_STRING;
6343 rettv->vval.v_string = (pat == NULL)
6344 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6345}
6346
6347/* for VIM_VERSION_ defines */
6348#include "version.h"
6349
6350/*
6351 * "has()" function
6352 */
6353 static void
6354f_has(typval_T *argvars, typval_T *rettv)
6355{
6356 int i;
6357 char_u *name;
6358 int n = FALSE;
6359 static char *(has_list[]) =
6360 {
6361#ifdef AMIGA
6362 "amiga",
6363# ifdef FEAT_ARP
6364 "arp",
6365# endif
6366#endif
6367#ifdef __BEOS__
6368 "beos",
6369#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006370#if defined(BSD) && !defined(MACOS_X)
6371 "bsd",
6372#endif
6373#ifdef hpux
6374 "hpux",
6375#endif
6376#ifdef __linux__
6377 "linux",
6378#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006379#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006380 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6381 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006382# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006383 "macunix", /* Mac OS X, with the darwin feature */
6384 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006385# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006386#endif
6387#ifdef __QNX__
6388 "qnx",
6389#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006390#ifdef SUN_SYSTEM
6391 "sun",
6392#else
6393 "moon",
6394#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395#ifdef UNIX
6396 "unix",
6397#endif
6398#ifdef VMS
6399 "vms",
6400#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006401#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006402 "win32",
6403#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006404#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006405 "win32unix",
6406#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006407#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006408 "win64",
6409#endif
6410#ifdef EBCDIC
6411 "ebcdic",
6412#endif
6413#ifndef CASE_INSENSITIVE_FILENAME
6414 "fname_case",
6415#endif
6416#ifdef HAVE_ACL
6417 "acl",
6418#endif
6419#ifdef FEAT_ARABIC
6420 "arabic",
6421#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006423#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006424 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006425#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006426#ifdef FEAT_AUTOSERVERNAME
6427 "autoservername",
6428#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006429#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006431# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006432 "balloon_multiline",
6433# endif
6434#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006435#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006436 "balloon_eval_term",
6437#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6439 "builtin_terms",
6440# ifdef ALL_BUILTIN_TCAPS
6441 "all_builtin_terms",
6442# endif
6443#endif
6444#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006445 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006446 || defined(FEAT_GUI_MOTIF))
6447 "browsefilter",
6448#endif
6449#ifdef FEAT_BYTEOFF
6450 "byte_offset",
6451#endif
6452#ifdef FEAT_JOB_CHANNEL
6453 "channel",
6454#endif
6455#ifdef FEAT_CINDENT
6456 "cindent",
6457#endif
6458#ifdef FEAT_CLIENTSERVER
6459 "clientserver",
6460#endif
6461#ifdef FEAT_CLIPBOARD
6462 "clipboard",
6463#endif
6464#ifdef FEAT_CMDL_COMPL
6465 "cmdline_compl",
6466#endif
6467#ifdef FEAT_CMDHIST
6468 "cmdline_hist",
6469#endif
6470#ifdef FEAT_COMMENTS
6471 "comments",
6472#endif
6473#ifdef FEAT_CONCEAL
6474 "conceal",
6475#endif
6476#ifdef FEAT_CRYPT
6477 "cryptv",
6478 "crypt-blowfish",
6479 "crypt-blowfish2",
6480#endif
6481#ifdef FEAT_CSCOPE
6482 "cscope",
6483#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006485#ifdef CURSOR_SHAPE
6486 "cursorshape",
6487#endif
6488#ifdef DEBUG
6489 "debug",
6490#endif
6491#ifdef FEAT_CON_DIALOG
6492 "dialog_con",
6493#endif
6494#ifdef FEAT_GUI_DIALOG
6495 "dialog_gui",
6496#endif
6497#ifdef FEAT_DIFF
6498 "diff",
6499#endif
6500#ifdef FEAT_DIGRAPHS
6501 "digraphs",
6502#endif
6503#ifdef FEAT_DIRECTX
6504 "directx",
6505#endif
6506#ifdef FEAT_DND
6507 "dnd",
6508#endif
6509#ifdef FEAT_EMACS_TAGS
6510 "emacs_tags",
6511#endif
6512 "eval", /* always present, of course! */
6513 "ex_extra", /* graduated feature */
6514#ifdef FEAT_SEARCH_EXTRA
6515 "extra_search",
6516#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006517#ifdef FEAT_SEARCHPATH
6518 "file_in_path",
6519#endif
6520#ifdef FEAT_FILTERPIPE
6521 "filterpipe",
6522#endif
6523#ifdef FEAT_FIND_ID
6524 "find_in_path",
6525#endif
6526#ifdef FEAT_FLOAT
6527 "float",
6528#endif
6529#ifdef FEAT_FOLDING
6530 "folding",
6531#endif
6532#ifdef FEAT_FOOTER
6533 "footer",
6534#endif
6535#if !defined(USE_SYSTEM) && defined(UNIX)
6536 "fork",
6537#endif
6538#ifdef FEAT_GETTEXT
6539 "gettext",
6540#endif
6541#ifdef FEAT_GUI
6542 "gui",
6543#endif
6544#ifdef FEAT_GUI_ATHENA
6545# ifdef FEAT_GUI_NEXTAW
6546 "gui_neXtaw",
6547# else
6548 "gui_athena",
6549# endif
6550#endif
6551#ifdef FEAT_GUI_GTK
6552 "gui_gtk",
6553# ifdef USE_GTK3
6554 "gui_gtk3",
6555# else
6556 "gui_gtk2",
6557# endif
6558#endif
6559#ifdef FEAT_GUI_GNOME
6560 "gui_gnome",
6561#endif
6562#ifdef FEAT_GUI_MAC
6563 "gui_mac",
6564#endif
6565#ifdef FEAT_GUI_MOTIF
6566 "gui_motif",
6567#endif
6568#ifdef FEAT_GUI_PHOTON
6569 "gui_photon",
6570#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006571#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006572 "gui_win32",
6573#endif
6574#ifdef FEAT_HANGULIN
6575 "hangul_input",
6576#endif
6577#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6578 "iconv",
6579#endif
6580#ifdef FEAT_INS_EXPAND
6581 "insert_expand",
6582#endif
6583#ifdef FEAT_JOB_CHANNEL
6584 "job",
6585#endif
6586#ifdef FEAT_JUMPLIST
6587 "jumplist",
6588#endif
6589#ifdef FEAT_KEYMAP
6590 "keymap",
6591#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006592 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006593#ifdef FEAT_LANGMAP
6594 "langmap",
6595#endif
6596#ifdef FEAT_LIBCALL
6597 "libcall",
6598#endif
6599#ifdef FEAT_LINEBREAK
6600 "linebreak",
6601#endif
6602#ifdef FEAT_LISP
6603 "lispindent",
6604#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006606#ifdef FEAT_LOCALMAP
6607 "localmap",
6608#endif
6609#ifdef FEAT_LUA
6610# ifndef DYNAMIC_LUA
6611 "lua",
6612# endif
6613#endif
6614#ifdef FEAT_MENU
6615 "menu",
6616#endif
6617#ifdef FEAT_SESSION
6618 "mksession",
6619#endif
6620#ifdef FEAT_MODIFY_FNAME
6621 "modify_fname",
6622#endif
6623#ifdef FEAT_MOUSE
6624 "mouse",
6625#endif
6626#ifdef FEAT_MOUSESHAPE
6627 "mouseshape",
6628#endif
6629#if defined(UNIX) || defined(VMS)
6630# ifdef FEAT_MOUSE_DEC
6631 "mouse_dec",
6632# endif
6633# ifdef FEAT_MOUSE_GPM
6634 "mouse_gpm",
6635# endif
6636# ifdef FEAT_MOUSE_JSB
6637 "mouse_jsbterm",
6638# endif
6639# ifdef FEAT_MOUSE_NET
6640 "mouse_netterm",
6641# endif
6642# ifdef FEAT_MOUSE_PTERM
6643 "mouse_pterm",
6644# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006645# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006646 "mouse_sgr",
6647# endif
6648# ifdef FEAT_SYSMOUSE
6649 "mouse_sysmouse",
6650# endif
6651# ifdef FEAT_MOUSE_URXVT
6652 "mouse_urxvt",
6653# endif
6654# ifdef FEAT_MOUSE_XTERM
6655 "mouse_xterm",
6656# endif
6657#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006659#ifdef FEAT_MBYTE_IME
6660 "multi_byte_ime",
6661#endif
6662#ifdef FEAT_MULTI_LANG
6663 "multi_lang",
6664#endif
6665#ifdef FEAT_MZSCHEME
6666#ifndef DYNAMIC_MZSCHEME
6667 "mzscheme",
6668#endif
6669#endif
6670#ifdef FEAT_NUM64
6671 "num64",
6672#endif
6673#ifdef FEAT_OLE
6674 "ole",
6675#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006676#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006677 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006678#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006679#ifdef FEAT_PATH_EXTRA
6680 "path_extra",
6681#endif
6682#ifdef FEAT_PERL
6683#ifndef DYNAMIC_PERL
6684 "perl",
6685#endif
6686#endif
6687#ifdef FEAT_PERSISTENT_UNDO
6688 "persistent_undo",
6689#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006690#if defined(FEAT_PYTHON)
6691 "python_compiled",
6692# if defined(DYNAMIC_PYTHON)
6693 "python_dynamic",
6694# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006696 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006697# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006698#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006699#if defined(FEAT_PYTHON3)
6700 "python3_compiled",
6701# if defined(DYNAMIC_PYTHON3)
6702 "python3_dynamic",
6703# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006704 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006705 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006706# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006707#endif
6708#ifdef FEAT_POSTSCRIPT
6709 "postscript",
6710#endif
6711#ifdef FEAT_PRINTER
6712 "printer",
6713#endif
6714#ifdef FEAT_PROFILE
6715 "profile",
6716#endif
6717#ifdef FEAT_RELTIME
6718 "reltime",
6719#endif
6720#ifdef FEAT_QUICKFIX
6721 "quickfix",
6722#endif
6723#ifdef FEAT_RIGHTLEFT
6724 "rightleft",
6725#endif
6726#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6727 "ruby",
6728#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730#ifdef FEAT_CMDL_INFO
6731 "showcmd",
6732 "cmdline_info",
6733#endif
6734#ifdef FEAT_SIGNS
6735 "signs",
6736#endif
6737#ifdef FEAT_SMARTINDENT
6738 "smartindent",
6739#endif
6740#ifdef STARTUPTIME
6741 "startuptime",
6742#endif
6743#ifdef FEAT_STL_OPT
6744 "statusline",
6745#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006746#ifdef FEAT_NETBEANS_INTG
6747 "netbeans_intg",
6748#endif
6749#ifdef FEAT_SPELL
6750 "spell",
6751#endif
6752#ifdef FEAT_SYN_HL
6753 "syntax",
6754#endif
6755#if defined(USE_SYSTEM) || !defined(UNIX)
6756 "system",
6757#endif
6758#ifdef FEAT_TAG_BINS
6759 "tag_binary",
6760#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006761#ifdef FEAT_TCL
6762# ifndef DYNAMIC_TCL
6763 "tcl",
6764# endif
6765#endif
6766#ifdef FEAT_TERMGUICOLORS
6767 "termguicolors",
6768#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006769#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006770 "terminal",
6771#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006772#ifdef TERMINFO
6773 "terminfo",
6774#endif
6775#ifdef FEAT_TERMRESPONSE
6776 "termresponse",
6777#endif
6778#ifdef FEAT_TEXTOBJ
6779 "textobjects",
6780#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006781#ifdef FEAT_TEXT_PROP
6782 "textprop",
6783#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006784#ifdef HAVE_TGETENT
6785 "tgetent",
6786#endif
6787#ifdef FEAT_TIMERS
6788 "timers",
6789#endif
6790#ifdef FEAT_TITLE
6791 "title",
6792#endif
6793#ifdef FEAT_TOOLBAR
6794 "toolbar",
6795#endif
6796#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6797 "unnamedplus",
6798#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006799 "user-commands", /* was accidentally included in 5.4 */
6800 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006801#ifdef FEAT_VARTABS
6802 "vartabs",
6803#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006804 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805#ifdef FEAT_VIMINFO
6806 "viminfo",
6807#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006808 "vimscript-1",
6809 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006810 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006814 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006815#ifdef FEAT_VTP
6816 "vtp",
6817#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818#ifdef FEAT_WILDIGN
6819 "wildignore",
6820#endif
6821#ifdef FEAT_WILDMENU
6822 "wildmenu",
6823#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006825#ifdef FEAT_WAK
6826 "winaltkeys",
6827#endif
6828#ifdef FEAT_WRITEBACKUP
6829 "writebackup",
6830#endif
6831#ifdef FEAT_XIM
6832 "xim",
6833#endif
6834#ifdef FEAT_XFONTSET
6835 "xfontset",
6836#endif
6837#ifdef FEAT_XPM_W32
6838 "xpm",
6839 "xpm_w32", /* for backward compatibility */
6840#else
6841# if defined(HAVE_XPM)
6842 "xpm",
6843# endif
6844#endif
6845#ifdef USE_XSMP
6846 "xsmp",
6847#endif
6848#ifdef USE_XSMP_INTERACT
6849 "xsmp_interact",
6850#endif
6851#ifdef FEAT_XCLIPBOARD
6852 "xterm_clipboard",
6853#endif
6854#ifdef FEAT_XTERM_SAVE
6855 "xterm_save",
6856#endif
6857#if defined(UNIX) && defined(FEAT_X11)
6858 "X11",
6859#endif
6860 NULL
6861 };
6862
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006863 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006864 for (i = 0; has_list[i] != NULL; ++i)
6865 if (STRICMP(name, has_list[i]) == 0)
6866 {
6867 n = TRUE;
6868 break;
6869 }
6870
6871 if (n == FALSE)
6872 {
6873 if (STRNICMP(name, "patch", 5) == 0)
6874 {
6875 if (name[5] == '-'
6876 && STRLEN(name) >= 11
6877 && vim_isdigit(name[6])
6878 && vim_isdigit(name[8])
6879 && vim_isdigit(name[10]))
6880 {
6881 int major = atoi((char *)name + 6);
6882 int minor = atoi((char *)name + 8);
6883
6884 /* Expect "patch-9.9.01234". */
6885 n = (major < VIM_VERSION_MAJOR
6886 || (major == VIM_VERSION_MAJOR
6887 && (minor < VIM_VERSION_MINOR
6888 || (minor == VIM_VERSION_MINOR
6889 && has_patch(atoi((char *)name + 10))))));
6890 }
6891 else
6892 n = has_patch(atoi((char *)name + 5));
6893 }
6894 else if (STRICMP(name, "vim_starting") == 0)
6895 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006896 else if (STRICMP(name, "ttyin") == 0)
6897 n = mch_input_isatty();
6898 else if (STRICMP(name, "ttyout") == 0)
6899 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 else if (STRICMP(name, "multi_byte_encoding") == 0)
6901 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006902#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006903 else if (STRICMP(name, "balloon_multiline") == 0)
6904 n = multiline_balloon_available();
6905#endif
6906#ifdef DYNAMIC_TCL
6907 else if (STRICMP(name, "tcl") == 0)
6908 n = tcl_enabled(FALSE);
6909#endif
6910#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6911 else if (STRICMP(name, "iconv") == 0)
6912 n = iconv_enabled(FALSE);
6913#endif
6914#ifdef DYNAMIC_LUA
6915 else if (STRICMP(name, "lua") == 0)
6916 n = lua_enabled(FALSE);
6917#endif
6918#ifdef DYNAMIC_MZSCHEME
6919 else if (STRICMP(name, "mzscheme") == 0)
6920 n = mzscheme_enabled(FALSE);
6921#endif
6922#ifdef DYNAMIC_RUBY
6923 else if (STRICMP(name, "ruby") == 0)
6924 n = ruby_enabled(FALSE);
6925#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006926#ifdef DYNAMIC_PYTHON
6927 else if (STRICMP(name, "python") == 0)
6928 n = python_enabled(FALSE);
6929#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006930#ifdef DYNAMIC_PYTHON3
6931 else if (STRICMP(name, "python3") == 0)
6932 n = python3_enabled(FALSE);
6933#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006934#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6935 else if (STRICMP(name, "pythonx") == 0)
6936 {
6937# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6938 if (p_pyx == 0)
6939 n = python3_enabled(FALSE) || python_enabled(FALSE);
6940 else if (p_pyx == 3)
6941 n = python3_enabled(FALSE);
6942 else if (p_pyx == 2)
6943 n = python_enabled(FALSE);
6944# elif defined(DYNAMIC_PYTHON)
6945 n = python_enabled(FALSE);
6946# elif defined(DYNAMIC_PYTHON3)
6947 n = python3_enabled(FALSE);
6948# endif
6949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006950#endif
6951#ifdef DYNAMIC_PERL
6952 else if (STRICMP(name, "perl") == 0)
6953 n = perl_enabled(FALSE);
6954#endif
6955#ifdef FEAT_GUI
6956 else if (STRICMP(name, "gui_running") == 0)
6957 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006958# ifdef FEAT_BROWSE
6959 else if (STRICMP(name, "browse") == 0)
6960 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6961# endif
6962#endif
6963#ifdef FEAT_SYN_HL
6964 else if (STRICMP(name, "syntax_items") == 0)
6965 n = syntax_present(curwin);
6966#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006967#ifdef FEAT_VTP
6968 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006969 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006970#endif
6971#ifdef FEAT_NETBEANS_INTG
6972 else if (STRICMP(name, "netbeans_enabled") == 0)
6973 n = netbeans_active();
6974#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006975#ifdef FEAT_MOUSE_GPM
6976 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6977 n = gpm_enabled();
6978#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006979#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006980 else if (STRICMP(name, "terminal") == 0)
6981 n = terminal_enabled();
6982#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006983#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006984 else if (STRICMP(name, "conpty") == 0)
6985 n = use_conpty();
6986#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006987 }
6988
6989 rettv->vval.v_number = n;
6990}
6991
6992/*
6993 * "has_key()" function
6994 */
6995 static void
6996f_has_key(typval_T *argvars, typval_T *rettv)
6997{
6998 if (argvars[0].v_type != VAR_DICT)
6999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007000 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001 return;
7002 }
7003 if (argvars[0].vval.v_dict == NULL)
7004 return;
7005
7006 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007007 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007008}
7009
7010/*
7011 * "haslocaldir()" function
7012 */
7013 static void
7014f_haslocaldir(typval_T *argvars, typval_T *rettv)
7015{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007016 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007017 win_T *wp = NULL;
7018
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007019 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7020
7021 // Check for window-local and tab-local directories
7022 if (wp != NULL && wp->w_localdir != NULL)
7023 rettv->vval.v_number = 1;
7024 else if (tp != NULL && tp->tp_localdir != NULL)
7025 rettv->vval.v_number = 2;
7026 else
7027 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007028}
7029
7030/*
7031 * "hasmapto()" function
7032 */
7033 static void
7034f_hasmapto(typval_T *argvars, typval_T *rettv)
7035{
7036 char_u *name;
7037 char_u *mode;
7038 char_u buf[NUMBUFLEN];
7039 int abbr = FALSE;
7040
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 if (argvars[1].v_type == VAR_UNKNOWN)
7043 mode = (char_u *)"nvo";
7044 else
7045 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007046 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007048 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007049 }
7050
7051 if (map_to_exists(name, mode, abbr))
7052 rettv->vval.v_number = TRUE;
7053 else
7054 rettv->vval.v_number = FALSE;
7055}
7056
7057/*
7058 * "histadd()" function
7059 */
7060 static void
7061f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7062{
7063#ifdef FEAT_CMDHIST
7064 int histype;
7065 char_u *str;
7066 char_u buf[NUMBUFLEN];
7067#endif
7068
7069 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007070 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 return;
7072#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007073 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007074 histype = str != NULL ? get_histtype(str) : -1;
7075 if (histype >= 0)
7076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007077 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 if (*str != NUL)
7079 {
7080 init_history();
7081 add_to_history(histype, str, FALSE, NUL);
7082 rettv->vval.v_number = TRUE;
7083 return;
7084 }
7085 }
7086#endif
7087}
7088
7089/*
7090 * "histdel()" function
7091 */
7092 static void
7093f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7094{
7095#ifdef FEAT_CMDHIST
7096 int n;
7097 char_u buf[NUMBUFLEN];
7098 char_u *str;
7099
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007100 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007101 if (str == NULL)
7102 n = 0;
7103 else if (argvars[1].v_type == VAR_UNKNOWN)
7104 /* only one argument: clear entire history */
7105 n = clr_history(get_histtype(str));
7106 else if (argvars[1].v_type == VAR_NUMBER)
7107 /* index given: remove that entry */
7108 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007109 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 else
7111 /* string given: remove all matching entries */
7112 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 rettv->vval.v_number = n;
7115#endif
7116}
7117
7118/*
7119 * "histget()" function
7120 */
7121 static void
7122f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7123{
7124#ifdef FEAT_CMDHIST
7125 int type;
7126 int idx;
7127 char_u *str;
7128
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007129 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007130 if (str == NULL)
7131 rettv->vval.v_string = NULL;
7132 else
7133 {
7134 type = get_histtype(str);
7135 if (argvars[1].v_type == VAR_UNKNOWN)
7136 idx = get_history_idx(type);
7137 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007138 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007139 /* -1 on type error */
7140 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7141 }
7142#else
7143 rettv->vval.v_string = NULL;
7144#endif
7145 rettv->v_type = VAR_STRING;
7146}
7147
7148/*
7149 * "histnr()" function
7150 */
7151 static void
7152f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7153{
7154 int i;
7155
7156#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007157 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007158
7159 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7160 if (i >= HIST_CMD && i < HIST_COUNT)
7161 i = get_history_idx(i);
7162 else
7163#endif
7164 i = -1;
7165 rettv->vval.v_number = i;
7166}
7167
7168/*
7169 * "highlightID(name)" function
7170 */
7171 static void
7172f_hlID(typval_T *argvars, typval_T *rettv)
7173{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175}
7176
7177/*
7178 * "highlight_exists()" function
7179 */
7180 static void
7181f_hlexists(typval_T *argvars, typval_T *rettv)
7182{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007183 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007184}
7185
7186/*
7187 * "hostname()" function
7188 */
7189 static void
7190f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7191{
7192 char_u hostname[256];
7193
7194 mch_get_host_name(hostname, 256);
7195 rettv->v_type = VAR_STRING;
7196 rettv->vval.v_string = vim_strsave(hostname);
7197}
7198
7199/*
7200 * iconv() function
7201 */
7202 static void
7203f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7204{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007205 char_u buf1[NUMBUFLEN];
7206 char_u buf2[NUMBUFLEN];
7207 char_u *from, *to, *str;
7208 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209
7210 rettv->v_type = VAR_STRING;
7211 rettv->vval.v_string = NULL;
7212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007213 str = tv_get_string(&argvars[0]);
7214 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7215 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007216 vimconv.vc_type = CONV_NONE;
7217 convert_setup(&vimconv, from, to);
7218
7219 /* If the encodings are equal, no conversion needed. */
7220 if (vimconv.vc_type == CONV_NONE)
7221 rettv->vval.v_string = vim_strsave(str);
7222 else
7223 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7224
7225 convert_setup(&vimconv, NULL, NULL);
7226 vim_free(from);
7227 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007228}
7229
7230/*
7231 * "indent()" function
7232 */
7233 static void
7234f_indent(typval_T *argvars, typval_T *rettv)
7235{
7236 linenr_T lnum;
7237
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007238 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007239 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7240 rettv->vval.v_number = get_indent_lnum(lnum);
7241 else
7242 rettv->vval.v_number = -1;
7243}
7244
7245/*
7246 * "index()" function
7247 */
7248 static void
7249f_index(typval_T *argvars, typval_T *rettv)
7250{
7251 list_T *l;
7252 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007253 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007254 long idx = 0;
7255 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007256 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007257
7258 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007259 if (argvars[0].v_type == VAR_BLOB)
7260 {
7261 typval_T tv;
7262 int start = 0;
7263
7264 if (argvars[2].v_type != VAR_UNKNOWN)
7265 {
7266 start = tv_get_number_chk(&argvars[2], &error);
7267 if (error)
7268 return;
7269 }
7270 b = argvars[0].vval.v_blob;
7271 if (b == NULL)
7272 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007273 if (start < 0)
7274 {
7275 start = blob_len(b) + start;
7276 if (start < 0)
7277 start = 0;
7278 }
7279
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007280 for (idx = start; idx < blob_len(b); ++idx)
7281 {
7282 tv.v_type = VAR_NUMBER;
7283 tv.vval.v_number = blob_get(b, idx);
7284 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7285 {
7286 rettv->vval.v_number = idx;
7287 return;
7288 }
7289 }
7290 return;
7291 }
7292 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007294 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007295 return;
7296 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007297
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007298 l = argvars[0].vval.v_list;
7299 if (l != NULL)
7300 {
7301 item = l->lv_first;
7302 if (argvars[2].v_type != VAR_UNKNOWN)
7303 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007304 /* Start at specified item. Use the cached index that list_find()
7305 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007306 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007307 idx = l->lv_idx;
7308 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007309 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007310 if (error)
7311 item = NULL;
7312 }
7313
7314 for ( ; item != NULL; item = item->li_next, ++idx)
7315 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7316 {
7317 rettv->vval.v_number = idx;
7318 break;
7319 }
7320 }
7321}
7322
7323static int inputsecret_flag = 0;
7324
7325/*
7326 * "input()" function
7327 * Also handles inputsecret() when inputsecret is set.
7328 */
7329 static void
7330f_input(typval_T *argvars, typval_T *rettv)
7331{
7332 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7333}
7334
7335/*
7336 * "inputdialog()" function
7337 */
7338 static void
7339f_inputdialog(typval_T *argvars, typval_T *rettv)
7340{
7341#if defined(FEAT_GUI_TEXTDIALOG)
7342 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7343 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7344 {
7345 char_u *message;
7346 char_u buf[NUMBUFLEN];
7347 char_u *defstr = (char_u *)"";
7348
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007349 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007350 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007351 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007352 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7353 else
7354 IObuff[0] = NUL;
7355 if (message != NULL && defstr != NULL
7356 && do_dialog(VIM_QUESTION, NULL, message,
7357 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7358 rettv->vval.v_string = vim_strsave(IObuff);
7359 else
7360 {
7361 if (message != NULL && defstr != NULL
7362 && argvars[1].v_type != VAR_UNKNOWN
7363 && argvars[2].v_type != VAR_UNKNOWN)
7364 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007365 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007366 else
7367 rettv->vval.v_string = NULL;
7368 }
7369 rettv->v_type = VAR_STRING;
7370 }
7371 else
7372#endif
7373 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7374}
7375
7376/*
7377 * "inputlist()" function
7378 */
7379 static void
7380f_inputlist(typval_T *argvars, typval_T *rettv)
7381{
7382 listitem_T *li;
7383 int selected;
7384 int mouse_used;
7385
7386#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007387 /* While starting up, there is no place to enter text. When running tests
7388 * with --not-a-term we assume feedkeys() will be used. */
7389 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007390 return;
7391#endif
7392 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007394 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007395 return;
7396 }
7397
7398 msg_start();
7399 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7400 lines_left = Rows; /* avoid more prompt */
7401 msg_scroll = TRUE;
7402 msg_clr_eos();
7403
7404 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7405 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007406 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007407 msg_putchar('\n');
7408 }
7409
7410 /* Ask for choice. */
7411 selected = prompt_for_number(&mouse_used);
7412 if (mouse_used)
7413 selected -= lines_left;
7414
7415 rettv->vval.v_number = selected;
7416}
7417
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007418static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7419
7420/*
7421 * "inputrestore()" function
7422 */
7423 static void
7424f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7425{
7426 if (ga_userinput.ga_len > 0)
7427 {
7428 --ga_userinput.ga_len;
7429 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7430 + ga_userinput.ga_len);
7431 /* default return is zero == OK */
7432 }
7433 else if (p_verbose > 1)
7434 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007435 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007436 rettv->vval.v_number = 1; /* Failed */
7437 }
7438}
7439
7440/*
7441 * "inputsave()" function
7442 */
7443 static void
7444f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7445{
7446 /* Add an entry to the stack of typeahead storage. */
7447 if (ga_grow(&ga_userinput, 1) == OK)
7448 {
7449 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7450 + ga_userinput.ga_len);
7451 ++ga_userinput.ga_len;
7452 /* default return is zero == OK */
7453 }
7454 else
7455 rettv->vval.v_number = 1; /* Failed */
7456}
7457
7458/*
7459 * "inputsecret()" function
7460 */
7461 static void
7462f_inputsecret(typval_T *argvars, typval_T *rettv)
7463{
7464 ++cmdline_star;
7465 ++inputsecret_flag;
7466 f_input(argvars, rettv);
7467 --cmdline_star;
7468 --inputsecret_flag;
7469}
7470
7471/*
7472 * "insert()" function
7473 */
7474 static void
7475f_insert(typval_T *argvars, typval_T *rettv)
7476{
7477 long before = 0;
7478 listitem_T *item;
7479 list_T *l;
7480 int error = FALSE;
7481
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007482 if (argvars[0].v_type == VAR_BLOB)
7483 {
7484 int val, len;
7485 char_u *p;
7486
7487 len = blob_len(argvars[0].vval.v_blob);
7488 if (argvars[2].v_type != VAR_UNKNOWN)
7489 {
7490 before = (long)tv_get_number_chk(&argvars[2], &error);
7491 if (error)
7492 return; // type error; errmsg already given
7493 if (before < 0 || before > len)
7494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007495 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007496 return;
7497 }
7498 }
7499 val = tv_get_number_chk(&argvars[1], &error);
7500 if (error)
7501 return;
7502 if (val < 0 || val > 255)
7503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007504 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007505 return;
7506 }
7507
7508 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7509 return;
7510 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7511 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7512 *(p + before) = val;
7513 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7514
7515 copy_tv(&argvars[0], rettv);
7516 }
7517 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007518 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007519 else if ((l = argvars[0].vval.v_list) != NULL
7520 && !var_check_lock(l->lv_lock,
7521 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007522 {
7523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007524 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007525 if (error)
7526 return; /* type error; errmsg already given */
7527
7528 if (before == l->lv_len)
7529 item = NULL;
7530 else
7531 {
7532 item = list_find(l, before);
7533 if (item == NULL)
7534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007536 l = NULL;
7537 }
7538 }
7539 if (l != NULL)
7540 {
7541 list_insert_tv(l, &argvars[1], item);
7542 copy_tv(&argvars[0], rettv);
7543 }
7544 }
7545}
7546
7547/*
7548 * "invert(expr)" function
7549 */
7550 static void
7551f_invert(typval_T *argvars, typval_T *rettv)
7552{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007553 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007554}
7555
7556/*
7557 * "isdirectory()" function
7558 */
7559 static void
7560f_isdirectory(typval_T *argvars, typval_T *rettv)
7561{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007562 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007563}
7564
7565/*
7566 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7567 * or it refers to a List or Dictionary that is locked.
7568 */
7569 static int
7570tv_islocked(typval_T *tv)
7571{
7572 return (tv->v_lock & VAR_LOCKED)
7573 || (tv->v_type == VAR_LIST
7574 && tv->vval.v_list != NULL
7575 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7576 || (tv->v_type == VAR_DICT
7577 && tv->vval.v_dict != NULL
7578 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7579}
7580
7581/*
7582 * "islocked()" function
7583 */
7584 static void
7585f_islocked(typval_T *argvars, typval_T *rettv)
7586{
7587 lval_T lv;
7588 char_u *end;
7589 dictitem_T *di;
7590
7591 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007592 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007593 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007594 if (end != NULL && lv.ll_name != NULL)
7595 {
7596 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598 else
7599 {
7600 if (lv.ll_tv == NULL)
7601 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007602 di = find_var(lv.ll_name, NULL, TRUE);
7603 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007604 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007605 /* Consider a variable locked when:
7606 * 1. the variable itself is locked
7607 * 2. the value of the variable is locked.
7608 * 3. the List or Dict value is locked.
7609 */
7610 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7611 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007612 }
7613 }
7614 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007616 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007617 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007618 else if (lv.ll_list != NULL)
7619 /* List item. */
7620 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7621 else
7622 /* Dictionary item. */
7623 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7624 }
7625 }
7626
7627 clear_lval(&lv);
7628}
7629
7630#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7631/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007632 * "isinf()" function
7633 */
7634 static void
7635f_isinf(typval_T *argvars, typval_T *rettv)
7636{
7637 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7638 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7639}
7640
7641/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 * "isnan()" function
7643 */
7644 static void
7645f_isnan(typval_T *argvars, typval_T *rettv)
7646{
7647 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7648 && isnan(argvars[0].vval.v_float);
7649}
7650#endif
7651
7652/*
7653 * "items(dict)" function
7654 */
7655 static void
7656f_items(typval_T *argvars, typval_T *rettv)
7657{
7658 dict_list(argvars, rettv, 2);
7659}
7660
7661#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7662/*
7663 * Get the job from the argument.
7664 * Returns NULL if the job is invalid.
7665 */
7666 static job_T *
7667get_job_arg(typval_T *tv)
7668{
7669 job_T *job;
7670
7671 if (tv->v_type != VAR_JOB)
7672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007674 return NULL;
7675 }
7676 job = tv->vval.v_job;
7677
7678 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007679 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007680 return job;
7681}
7682
7683/*
7684 * "job_getchannel()" function
7685 */
7686 static void
7687f_job_getchannel(typval_T *argvars, typval_T *rettv)
7688{
7689 job_T *job = get_job_arg(&argvars[0]);
7690
7691 if (job != NULL)
7692 {
7693 rettv->v_type = VAR_CHANNEL;
7694 rettv->vval.v_channel = job->jv_channel;
7695 if (job->jv_channel != NULL)
7696 ++job->jv_channel->ch_refcount;
7697 }
7698}
7699
7700/*
7701 * "job_info()" function
7702 */
7703 static void
7704f_job_info(typval_T *argvars, typval_T *rettv)
7705{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007706 if (argvars[0].v_type != VAR_UNKNOWN)
7707 {
7708 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007710 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7711 job_info(job, rettv->vval.v_dict);
7712 }
7713 else if (rettv_list_alloc(rettv) == OK)
7714 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715}
7716
7717/*
7718 * "job_setoptions()" function
7719 */
7720 static void
7721f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7722{
7723 job_T *job = get_job_arg(&argvars[0]);
7724 jobopt_T opt;
7725
7726 if (job == NULL)
7727 return;
7728 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007729 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007730 job_set_options(job, &opt);
7731 free_job_options(&opt);
7732}
7733
7734/*
7735 * "job_start()" function
7736 */
7737 static void
7738f_job_start(typval_T *argvars, typval_T *rettv)
7739{
7740 rettv->v_type = VAR_JOB;
7741 if (check_restricted() || check_secure())
7742 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007743 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744}
7745
7746/*
7747 * "job_status()" function
7748 */
7749 static void
7750f_job_status(typval_T *argvars, typval_T *rettv)
7751{
7752 job_T *job = get_job_arg(&argvars[0]);
7753
7754 if (job != NULL)
7755 {
7756 rettv->v_type = VAR_STRING;
7757 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7758 }
7759}
7760
7761/*
7762 * "job_stop()" function
7763 */
7764 static void
7765f_job_stop(typval_T *argvars, typval_T *rettv)
7766{
7767 job_T *job = get_job_arg(&argvars[0]);
7768
7769 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007770 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007771}
7772#endif
7773
7774/*
7775 * "join()" function
7776 */
7777 static void
7778f_join(typval_T *argvars, typval_T *rettv)
7779{
7780 garray_T ga;
7781 char_u *sep;
7782
7783 if (argvars[0].v_type != VAR_LIST)
7784 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007785 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007786 return;
7787 }
7788 if (argvars[0].vval.v_list == NULL)
7789 return;
7790 if (argvars[1].v_type == VAR_UNKNOWN)
7791 sep = (char_u *)" ";
7792 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007793 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007794
7795 rettv->v_type = VAR_STRING;
7796
7797 if (sep != NULL)
7798 {
7799 ga_init2(&ga, (int)sizeof(char), 80);
7800 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7801 ga_append(&ga, NUL);
7802 rettv->vval.v_string = (char_u *)ga.ga_data;
7803 }
7804 else
7805 rettv->vval.v_string = NULL;
7806}
7807
7808/*
7809 * "js_decode()" function
7810 */
7811 static void
7812f_js_decode(typval_T *argvars, typval_T *rettv)
7813{
7814 js_read_T reader;
7815
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007816 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007817 reader.js_fill = NULL;
7818 reader.js_used = 0;
7819 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007820 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821}
7822
7823/*
7824 * "js_encode()" function
7825 */
7826 static void
7827f_js_encode(typval_T *argvars, typval_T *rettv)
7828{
7829 rettv->v_type = VAR_STRING;
7830 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7831}
7832
7833/*
7834 * "json_decode()" function
7835 */
7836 static void
7837f_json_decode(typval_T *argvars, typval_T *rettv)
7838{
7839 js_read_T reader;
7840
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007841 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007842 reader.js_fill = NULL;
7843 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007844 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007845}
7846
7847/*
7848 * "json_encode()" function
7849 */
7850 static void
7851f_json_encode(typval_T *argvars, typval_T *rettv)
7852{
7853 rettv->v_type = VAR_STRING;
7854 rettv->vval.v_string = json_encode(&argvars[0], 0);
7855}
7856
7857/*
7858 * "keys()" function
7859 */
7860 static void
7861f_keys(typval_T *argvars, typval_T *rettv)
7862{
7863 dict_list(argvars, rettv, 0);
7864}
7865
7866/*
7867 * "last_buffer_nr()" function.
7868 */
7869 static void
7870f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7871{
7872 int n = 0;
7873 buf_T *buf;
7874
Bram Moolenaar29323592016-07-24 22:04:11 +02007875 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007876 if (n < buf->b_fnum)
7877 n = buf->b_fnum;
7878
7879 rettv->vval.v_number = n;
7880}
7881
7882/*
7883 * "len()" function
7884 */
7885 static void
7886f_len(typval_T *argvars, typval_T *rettv)
7887{
7888 switch (argvars[0].v_type)
7889 {
7890 case VAR_STRING:
7891 case VAR_NUMBER:
7892 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007893 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007894 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007895 case VAR_BLOB:
7896 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7897 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007898 case VAR_LIST:
7899 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7900 break;
7901 case VAR_DICT:
7902 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7903 break;
7904 case VAR_UNKNOWN:
7905 case VAR_SPECIAL:
7906 case VAR_FLOAT:
7907 case VAR_FUNC:
7908 case VAR_PARTIAL:
7909 case VAR_JOB:
7910 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007911 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 break;
7913 }
7914}
7915
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007916 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007917libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007918{
7919#ifdef FEAT_LIBCALL
7920 char_u *string_in;
7921 char_u **string_result;
7922 int nr_result;
7923#endif
7924
7925 rettv->v_type = type;
7926 if (type != VAR_NUMBER)
7927 rettv->vval.v_string = NULL;
7928
7929 if (check_restricted() || check_secure())
7930 return;
7931
7932#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007933 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7935 {
7936 string_in = NULL;
7937 if (argvars[2].v_type == VAR_STRING)
7938 string_in = argvars[2].vval.v_string;
7939 if (type == VAR_NUMBER)
7940 string_result = NULL;
7941 else
7942 string_result = &rettv->vval.v_string;
7943 if (mch_libcall(argvars[0].vval.v_string,
7944 argvars[1].vval.v_string,
7945 string_in,
7946 argvars[2].vval.v_number,
7947 string_result,
7948 &nr_result) == OK
7949 && type == VAR_NUMBER)
7950 rettv->vval.v_number = nr_result;
7951 }
7952#endif
7953}
7954
7955/*
7956 * "libcall()" function
7957 */
7958 static void
7959f_libcall(typval_T *argvars, typval_T *rettv)
7960{
7961 libcall_common(argvars, rettv, VAR_STRING);
7962}
7963
7964/*
7965 * "libcallnr()" function
7966 */
7967 static void
7968f_libcallnr(typval_T *argvars, typval_T *rettv)
7969{
7970 libcall_common(argvars, rettv, VAR_NUMBER);
7971}
7972
7973/*
7974 * "line(string)" function
7975 */
7976 static void
7977f_line(typval_T *argvars, typval_T *rettv)
7978{
7979 linenr_T lnum = 0;
7980 pos_T *fp;
7981 int fnum;
7982
7983 fp = var2fpos(&argvars[0], TRUE, &fnum);
7984 if (fp != NULL)
7985 lnum = fp->lnum;
7986 rettv->vval.v_number = lnum;
7987}
7988
7989/*
7990 * "line2byte(lnum)" function
7991 */
7992 static void
7993f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7994{
7995#ifndef FEAT_BYTEOFF
7996 rettv->vval.v_number = -1;
7997#else
7998 linenr_T lnum;
7999
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008000 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008001 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8002 rettv->vval.v_number = -1;
8003 else
8004 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8005 if (rettv->vval.v_number >= 0)
8006 ++rettv->vval.v_number;
8007#endif
8008}
8009
8010/*
8011 * "lispindent(lnum)" function
8012 */
8013 static void
8014f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8015{
8016#ifdef FEAT_LISP
8017 pos_T pos;
8018 linenr_T lnum;
8019
8020 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008021 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8023 {
8024 curwin->w_cursor.lnum = lnum;
8025 rettv->vval.v_number = get_lisp_indent();
8026 curwin->w_cursor = pos;
8027 }
8028 else
8029#endif
8030 rettv->vval.v_number = -1;
8031}
8032
8033/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008034 * "list2str()" function
8035 */
8036 static void
8037f_list2str(typval_T *argvars, typval_T *rettv)
8038{
8039 list_T *l;
8040 listitem_T *li;
8041 garray_T ga;
8042 int utf8 = FALSE;
8043
8044 rettv->v_type = VAR_STRING;
8045 rettv->vval.v_string = NULL;
8046 if (argvars[0].v_type != VAR_LIST)
8047 {
8048 emsg(_(e_invarg));
8049 return;
8050 }
8051
8052 l = argvars[0].vval.v_list;
8053 if (l == NULL)
8054 return; // empty list results in empty string
8055
8056 if (argvars[1].v_type != VAR_UNKNOWN)
8057 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8058
8059 ga_init2(&ga, 1, 80);
8060 if (has_mbyte || utf8)
8061 {
8062 char_u buf[MB_MAXBYTES + 1];
8063 int (*char2bytes)(int, char_u *);
8064
8065 if (utf8 || enc_utf8)
8066 char2bytes = utf_char2bytes;
8067 else
8068 char2bytes = mb_char2bytes;
8069
8070 for (li = l->lv_first; li != NULL; li = li->li_next)
8071 {
8072 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8073 ga_concat(&ga, buf);
8074 }
8075 ga_append(&ga, NUL);
8076 }
8077 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8078 {
8079 for (li = l->lv_first; li != NULL; li = li->li_next)
8080 ga_append(&ga, tv_get_number(&li->li_tv));
8081 ga_append(&ga, NUL);
8082 }
8083
8084 rettv->v_type = VAR_STRING;
8085 rettv->vval.v_string = ga.ga_data;
8086}
8087
8088/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008089 * "localtime()" function
8090 */
8091 static void
8092f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8093{
8094 rettv->vval.v_number = (varnumber_T)time(NULL);
8095}
8096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008097 static void
8098get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8099{
8100 char_u *keys;
8101 char_u *which;
8102 char_u buf[NUMBUFLEN];
8103 char_u *keys_buf = NULL;
8104 char_u *rhs;
8105 int mode;
8106 int abbr = FALSE;
8107 int get_dict = FALSE;
8108 mapblock_T *mp;
8109 int buffer_local;
8110
8111 /* return empty string for failure */
8112 rettv->v_type = VAR_STRING;
8113 rettv->vval.v_string = NULL;
8114
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008115 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008116 if (*keys == NUL)
8117 return;
8118
8119 if (argvars[1].v_type != VAR_UNKNOWN)
8120 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008121 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008122 if (argvars[2].v_type != VAR_UNKNOWN)
8123 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008124 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008126 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008127 }
8128 }
8129 else
8130 which = (char_u *)"";
8131 if (which == NULL)
8132 return;
8133
8134 mode = get_map_mode(&which, 0);
8135
8136 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8137 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8138 vim_free(keys_buf);
8139
8140 if (!get_dict)
8141 {
8142 /* Return a string. */
8143 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008144 {
8145 if (*rhs == NUL)
8146 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8147 else
8148 rettv->vval.v_string = str2special_save(rhs, FALSE);
8149 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008150
8151 }
8152 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8153 {
8154 /* Return a dictionary. */
8155 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8156 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8157 dict_T *dict = rettv->vval.v_dict;
8158
Bram Moolenaare0be1672018-07-08 16:50:37 +02008159 dict_add_string(dict, "lhs", lhs);
8160 dict_add_string(dict, "rhs", mp->m_orig_str);
8161 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8162 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8163 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008164 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8165 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008166 dict_add_number(dict, "buffer", (long)buffer_local);
8167 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8168 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008169
8170 vim_free(lhs);
8171 vim_free(mapmode);
8172 }
8173}
8174
8175#ifdef FEAT_FLOAT
8176/*
8177 * "log()" function
8178 */
8179 static void
8180f_log(typval_T *argvars, typval_T *rettv)
8181{
8182 float_T f = 0.0;
8183
8184 rettv->v_type = VAR_FLOAT;
8185 if (get_float_arg(argvars, &f) == OK)
8186 rettv->vval.v_float = log(f);
8187 else
8188 rettv->vval.v_float = 0.0;
8189}
8190
8191/*
8192 * "log10()" function
8193 */
8194 static void
8195f_log10(typval_T *argvars, typval_T *rettv)
8196{
8197 float_T f = 0.0;
8198
8199 rettv->v_type = VAR_FLOAT;
8200 if (get_float_arg(argvars, &f) == OK)
8201 rettv->vval.v_float = log10(f);
8202 else
8203 rettv->vval.v_float = 0.0;
8204}
8205#endif
8206
8207#ifdef FEAT_LUA
8208/*
8209 * "luaeval()" function
8210 */
8211 static void
8212f_luaeval(typval_T *argvars, typval_T *rettv)
8213{
8214 char_u *str;
8215 char_u buf[NUMBUFLEN];
8216
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008217 if (check_restricted() || check_secure())
8218 return;
8219
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008220 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008221 do_luaeval(str, argvars + 1, rettv);
8222}
8223#endif
8224
8225/*
8226 * "map()" function
8227 */
8228 static void
8229f_map(typval_T *argvars, typval_T *rettv)
8230{
8231 filter_map(argvars, rettv, TRUE);
8232}
8233
8234/*
8235 * "maparg()" function
8236 */
8237 static void
8238f_maparg(typval_T *argvars, typval_T *rettv)
8239{
8240 get_maparg(argvars, rettv, TRUE);
8241}
8242
8243/*
8244 * "mapcheck()" function
8245 */
8246 static void
8247f_mapcheck(typval_T *argvars, typval_T *rettv)
8248{
8249 get_maparg(argvars, rettv, FALSE);
8250}
8251
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008252typedef enum
8253{
8254 MATCH_END, /* matchend() */
8255 MATCH_MATCH, /* match() */
8256 MATCH_STR, /* matchstr() */
8257 MATCH_LIST, /* matchlist() */
8258 MATCH_POS /* matchstrpos() */
8259} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008260
8261 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008262find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008263{
8264 char_u *str = NULL;
8265 long len = 0;
8266 char_u *expr = NULL;
8267 char_u *pat;
8268 regmatch_T regmatch;
8269 char_u patbuf[NUMBUFLEN];
8270 char_u strbuf[NUMBUFLEN];
8271 char_u *save_cpo;
8272 long start = 0;
8273 long nth = 1;
8274 colnr_T startcol = 0;
8275 int match = 0;
8276 list_T *l = NULL;
8277 listitem_T *li = NULL;
8278 long idx = 0;
8279 char_u *tofree = NULL;
8280
8281 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8282 save_cpo = p_cpo;
8283 p_cpo = (char_u *)"";
8284
8285 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008286 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008287 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008288 /* type MATCH_LIST: return empty list when there are no matches.
8289 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008290 if (rettv_list_alloc(rettv) == FAIL)
8291 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008292 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008293 && (list_append_string(rettv->vval.v_list,
8294 (char_u *)"", 0) == FAIL
8295 || list_append_number(rettv->vval.v_list,
8296 (varnumber_T)-1) == FAIL
8297 || list_append_number(rettv->vval.v_list,
8298 (varnumber_T)-1) == FAIL
8299 || list_append_number(rettv->vval.v_list,
8300 (varnumber_T)-1) == FAIL))
8301 {
8302 list_free(rettv->vval.v_list);
8303 rettv->vval.v_list = NULL;
8304 goto theend;
8305 }
8306 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008307 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008308 {
8309 rettv->v_type = VAR_STRING;
8310 rettv->vval.v_string = NULL;
8311 }
8312
8313 if (argvars[0].v_type == VAR_LIST)
8314 {
8315 if ((l = argvars[0].vval.v_list) == NULL)
8316 goto theend;
8317 li = l->lv_first;
8318 }
8319 else
8320 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008321 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 len = (long)STRLEN(str);
8323 }
8324
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008325 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (pat == NULL)
8327 goto theend;
8328
8329 if (argvars[2].v_type != VAR_UNKNOWN)
8330 {
8331 int error = FALSE;
8332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008333 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008334 if (error)
8335 goto theend;
8336 if (l != NULL)
8337 {
8338 li = list_find(l, start);
8339 if (li == NULL)
8340 goto theend;
8341 idx = l->lv_idx; /* use the cached index */
8342 }
8343 else
8344 {
8345 if (start < 0)
8346 start = 0;
8347 if (start > len)
8348 goto theend;
8349 /* When "count" argument is there ignore matches before "start",
8350 * otherwise skip part of the string. Differs when pattern is "^"
8351 * or "\<". */
8352 if (argvars[3].v_type != VAR_UNKNOWN)
8353 startcol = start;
8354 else
8355 {
8356 str += start;
8357 len -= start;
8358 }
8359 }
8360
8361 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008362 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008363 if (error)
8364 goto theend;
8365 }
8366
8367 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8368 if (regmatch.regprog != NULL)
8369 {
8370 regmatch.rm_ic = p_ic;
8371
8372 for (;;)
8373 {
8374 if (l != NULL)
8375 {
8376 if (li == NULL)
8377 {
8378 match = FALSE;
8379 break;
8380 }
8381 vim_free(tofree);
8382 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8383 if (str == NULL)
8384 break;
8385 }
8386
8387 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8388
8389 if (match && --nth <= 0)
8390 break;
8391 if (l == NULL && !match)
8392 break;
8393
8394 /* Advance to just after the match. */
8395 if (l != NULL)
8396 {
8397 li = li->li_next;
8398 ++idx;
8399 }
8400 else
8401 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008402 startcol = (colnr_T)(regmatch.startp[0]
8403 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008404 if (startcol > (colnr_T)len
8405 || str + startcol <= regmatch.startp[0])
8406 {
8407 match = FALSE;
8408 break;
8409 }
8410 }
8411 }
8412
8413 if (match)
8414 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008415 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008416 {
8417 listitem_T *li1 = rettv->vval.v_list->lv_first;
8418 listitem_T *li2 = li1->li_next;
8419 listitem_T *li3 = li2->li_next;
8420 listitem_T *li4 = li3->li_next;
8421
8422 vim_free(li1->li_tv.vval.v_string);
8423 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8424 (int)(regmatch.endp[0] - regmatch.startp[0]));
8425 li3->li_tv.vval.v_number =
8426 (varnumber_T)(regmatch.startp[0] - expr);
8427 li4->li_tv.vval.v_number =
8428 (varnumber_T)(regmatch.endp[0] - expr);
8429 if (l != NULL)
8430 li2->li_tv.vval.v_number = (varnumber_T)idx;
8431 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008432 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008433 {
8434 int i;
8435
8436 /* return list with matched string and submatches */
8437 for (i = 0; i < NSUBEXP; ++i)
8438 {
8439 if (regmatch.endp[i] == NULL)
8440 {
8441 if (list_append_string(rettv->vval.v_list,
8442 (char_u *)"", 0) == FAIL)
8443 break;
8444 }
8445 else if (list_append_string(rettv->vval.v_list,
8446 regmatch.startp[i],
8447 (int)(regmatch.endp[i] - regmatch.startp[i]))
8448 == FAIL)
8449 break;
8450 }
8451 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008452 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008453 {
8454 /* return matched string */
8455 if (l != NULL)
8456 copy_tv(&li->li_tv, rettv);
8457 else
8458 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8459 (int)(regmatch.endp[0] - regmatch.startp[0]));
8460 }
8461 else if (l != NULL)
8462 rettv->vval.v_number = idx;
8463 else
8464 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008465 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008466 rettv->vval.v_number =
8467 (varnumber_T)(regmatch.startp[0] - str);
8468 else
8469 rettv->vval.v_number =
8470 (varnumber_T)(regmatch.endp[0] - str);
8471 rettv->vval.v_number += (varnumber_T)(str - expr);
8472 }
8473 }
8474 vim_regfree(regmatch.regprog);
8475 }
8476
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008477theend:
8478 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008479 /* matchstrpos() without a list: drop the second item. */
8480 listitem_remove(rettv->vval.v_list,
8481 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008482 vim_free(tofree);
8483 p_cpo = save_cpo;
8484}
8485
8486/*
8487 * "match()" function
8488 */
8489 static void
8490f_match(typval_T *argvars, typval_T *rettv)
8491{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008492 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008493}
8494
Bram Moolenaar95e51472018-07-28 16:55:56 +02008495#ifdef FEAT_SEARCH_EXTRA
8496 static int
8497matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8498{
8499 dictitem_T *di;
8500
8501 if (tv->v_type != VAR_DICT)
8502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008503 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008504 return FAIL;
8505 }
8506
8507 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008508 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008509 (char_u *)"conceal", FALSE);
8510
8511 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8512 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008513 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008514 if (*win == NULL)
8515 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008516 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008517 return FAIL;
8518 }
8519 }
8520
8521 return OK;
8522}
8523#endif
8524
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008525/*
8526 * "matchadd()" function
8527 */
8528 static void
8529f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8530{
8531#ifdef FEAT_SEARCH_EXTRA
8532 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008533 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8534 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008535 int prio = 10; /* default priority */
8536 int id = -1;
8537 int error = FALSE;
8538 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008539 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008540
8541 rettv->vval.v_number = -1;
8542
8543 if (grp == NULL || pat == NULL)
8544 return;
8545 if (argvars[2].v_type != VAR_UNKNOWN)
8546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008547 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008548 if (argvars[3].v_type != VAR_UNKNOWN)
8549 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008550 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008551 if (argvars[4].v_type != VAR_UNKNOWN
8552 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8553 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 }
8555 }
8556 if (error == TRUE)
8557 return;
8558 if (id >= 1 && id <= 3)
8559 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008560 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561 return;
8562 }
8563
Bram Moolenaar95e51472018-07-28 16:55:56 +02008564 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008565 conceal_char);
8566#endif
8567}
8568
8569/*
8570 * "matchaddpos()" function
8571 */
8572 static void
8573f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8574{
8575#ifdef FEAT_SEARCH_EXTRA
8576 char_u buf[NUMBUFLEN];
8577 char_u *group;
8578 int prio = 10;
8579 int id = -1;
8580 int error = FALSE;
8581 list_T *l;
8582 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008583 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584
8585 rettv->vval.v_number = -1;
8586
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008587 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008588 if (group == NULL)
8589 return;
8590
8591 if (argvars[1].v_type != VAR_LIST)
8592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008593 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008594 return;
8595 }
8596 l = argvars[1].vval.v_list;
8597 if (l == NULL)
8598 return;
8599
8600 if (argvars[2].v_type != VAR_UNKNOWN)
8601 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008602 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 if (argvars[3].v_type != VAR_UNKNOWN)
8604 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008605 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008606
8607 if (argvars[4].v_type != VAR_UNKNOWN
8608 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8609 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008610 }
8611 }
8612 if (error == TRUE)
8613 return;
8614
8615 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8616 if (id == 1 || id == 2)
8617 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008618 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619 return;
8620 }
8621
Bram Moolenaar95e51472018-07-28 16:55:56 +02008622 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 conceal_char);
8624#endif
8625}
8626
8627/*
8628 * "matcharg()" function
8629 */
8630 static void
8631f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8632{
8633 if (rettv_list_alloc(rettv) == OK)
8634 {
8635#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008636 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008637 matchitem_T *m;
8638
8639 if (id >= 1 && id <= 3)
8640 {
8641 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8642 {
8643 list_append_string(rettv->vval.v_list,
8644 syn_id2name(m->hlg_id), -1);
8645 list_append_string(rettv->vval.v_list, m->pattern, -1);
8646 }
8647 else
8648 {
8649 list_append_string(rettv->vval.v_list, NULL, -1);
8650 list_append_string(rettv->vval.v_list, NULL, -1);
8651 }
8652 }
8653#endif
8654 }
8655}
8656
8657/*
8658 * "matchdelete()" function
8659 */
8660 static void
8661f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8662{
8663#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008664 win_T *win = get_optional_window(argvars, 1);
8665
8666 if (win == NULL)
8667 rettv->vval.v_number = -1;
8668 else
8669 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008670 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008671#endif
8672}
8673
8674/*
8675 * "matchend()" function
8676 */
8677 static void
8678f_matchend(typval_T *argvars, typval_T *rettv)
8679{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008680 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008681}
8682
8683/*
8684 * "matchlist()" function
8685 */
8686 static void
8687f_matchlist(typval_T *argvars, typval_T *rettv)
8688{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008689 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008690}
8691
8692/*
8693 * "matchstr()" function
8694 */
8695 static void
8696f_matchstr(typval_T *argvars, typval_T *rettv)
8697{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008698 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008699}
8700
8701/*
8702 * "matchstrpos()" function
8703 */
8704 static void
8705f_matchstrpos(typval_T *argvars, typval_T *rettv)
8706{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008707 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008708}
8709
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008710 static void
8711max_min(typval_T *argvars, typval_T *rettv, int domax)
8712{
8713 varnumber_T n = 0;
8714 varnumber_T i;
8715 int error = FALSE;
8716
8717 if (argvars[0].v_type == VAR_LIST)
8718 {
8719 list_T *l;
8720 listitem_T *li;
8721
8722 l = argvars[0].vval.v_list;
8723 if (l != NULL)
8724 {
8725 li = l->lv_first;
8726 if (li != NULL)
8727 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008728 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008729 for (;;)
8730 {
8731 li = li->li_next;
8732 if (li == NULL)
8733 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008734 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735 if (domax ? i > n : i < n)
8736 n = i;
8737 }
8738 }
8739 }
8740 }
8741 else if (argvars[0].v_type == VAR_DICT)
8742 {
8743 dict_T *d;
8744 int first = TRUE;
8745 hashitem_T *hi;
8746 int todo;
8747
8748 d = argvars[0].vval.v_dict;
8749 if (d != NULL)
8750 {
8751 todo = (int)d->dv_hashtab.ht_used;
8752 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8753 {
8754 if (!HASHITEM_EMPTY(hi))
8755 {
8756 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008757 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008758 if (first)
8759 {
8760 n = i;
8761 first = FALSE;
8762 }
8763 else if (domax ? i > n : i < n)
8764 n = i;
8765 }
8766 }
8767 }
8768 }
8769 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008770 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 rettv->vval.v_number = error ? 0 : n;
8772}
8773
8774/*
8775 * "max()" function
8776 */
8777 static void
8778f_max(typval_T *argvars, typval_T *rettv)
8779{
8780 max_min(argvars, rettv, TRUE);
8781}
8782
8783/*
8784 * "min()" function
8785 */
8786 static void
8787f_min(typval_T *argvars, typval_T *rettv)
8788{
8789 max_min(argvars, rettv, FALSE);
8790}
8791
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008792/*
8793 * Create the directory in which "dir" is located, and higher levels when
8794 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008795 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 */
8797 static int
8798mkdir_recurse(char_u *dir, int prot)
8799{
8800 char_u *p;
8801 char_u *updir;
8802 int r = FAIL;
8803
8804 /* Get end of directory name in "dir".
8805 * We're done when it's "/" or "c:/". */
8806 p = gettail_sep(dir);
8807 if (p <= get_past_head(dir))
8808 return OK;
8809
8810 /* If the directory exists we're done. Otherwise: create it.*/
8811 updir = vim_strnsave(dir, (int)(p - dir));
8812 if (updir == NULL)
8813 return FAIL;
8814 if (mch_isdir(updir))
8815 r = OK;
8816 else if (mkdir_recurse(updir, prot) == OK)
8817 r = vim_mkdir_emsg(updir, prot);
8818 vim_free(updir);
8819 return r;
8820}
8821
8822#ifdef vim_mkdir
8823/*
8824 * "mkdir()" function
8825 */
8826 static void
8827f_mkdir(typval_T *argvars, typval_T *rettv)
8828{
8829 char_u *dir;
8830 char_u buf[NUMBUFLEN];
8831 int prot = 0755;
8832
8833 rettv->vval.v_number = FAIL;
8834 if (check_restricted() || check_secure())
8835 return;
8836
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008837 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008838 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008839 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008840
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008841 if (*gettail(dir) == NUL)
8842 /* remove trailing slashes */
8843 *gettail_sep(dir) = NUL;
8844
8845 if (argvars[1].v_type != VAR_UNKNOWN)
8846 {
8847 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008849 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008850 if (prot == -1)
8851 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008852 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008853 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008854 {
8855 if (mch_isdir(dir))
8856 {
8857 /* With the "p" flag it's OK if the dir already exists. */
8858 rettv->vval.v_number = OK;
8859 return;
8860 }
8861 mkdir_recurse(dir, prot);
8862 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008863 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008864 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008865}
8866#endif
8867
8868/*
8869 * "mode()" function
8870 */
8871 static void
8872f_mode(typval_T *argvars, typval_T *rettv)
8873{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008874 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875
Bram Moolenaar612cc382018-07-29 15:34:26 +02008876 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008877
8878 if (time_for_testing == 93784)
8879 {
8880 /* Testing the two-character code. */
8881 buf[0] = 'x';
8882 buf[1] = '!';
8883 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008884#ifdef FEAT_TERMINAL
8885 else if (term_use_loop())
8886 buf[0] = 't';
8887#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 else if (VIsual_active)
8889 {
8890 if (VIsual_select)
8891 buf[0] = VIsual_mode + 's' - 'v';
8892 else
8893 buf[0] = VIsual_mode;
8894 }
8895 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8896 || State == CONFIRM)
8897 {
8898 buf[0] = 'r';
8899 if (State == ASKMORE)
8900 buf[1] = 'm';
8901 else if (State == CONFIRM)
8902 buf[1] = '?';
8903 }
8904 else if (State == EXTERNCMD)
8905 buf[0] = '!';
8906 else if (State & INSERT)
8907 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008908 if (State & VREPLACE_FLAG)
8909 {
8910 buf[0] = 'R';
8911 buf[1] = 'v';
8912 }
8913 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008914 {
8915 if (State & REPLACE_FLAG)
8916 buf[0] = 'R';
8917 else
8918 buf[0] = 'i';
8919#ifdef FEAT_INS_EXPAND
8920 if (ins_compl_active())
8921 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008922 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008923 buf[1] = 'x';
8924#endif
8925 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008926 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008927 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008928 {
8929 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008930 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008931 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008932 else if (exmode_active == EXMODE_NORMAL)
8933 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008934 }
8935 else
8936 {
8937 buf[0] = 'n';
8938 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008939 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008940 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008941 // to be able to detect force-linewise/blockwise/characterwise operations
8942 buf[2] = motion_force;
8943 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008944 else if (restart_edit == 'I' || restart_edit == 'R'
8945 || restart_edit == 'V')
8946 {
8947 buf[1] = 'i';
8948 buf[2] = restart_edit;
8949 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008950 }
8951
8952 /* Clear out the minor mode when the argument is not a non-zero number or
8953 * non-empty string. */
8954 if (!non_zero_arg(&argvars[0]))
8955 buf[1] = NUL;
8956
8957 rettv->vval.v_string = vim_strsave(buf);
8958 rettv->v_type = VAR_STRING;
8959}
8960
8961#if defined(FEAT_MZSCHEME) || defined(PROTO)
8962/*
8963 * "mzeval()" function
8964 */
8965 static void
8966f_mzeval(typval_T *argvars, typval_T *rettv)
8967{
8968 char_u *str;
8969 char_u buf[NUMBUFLEN];
8970
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008971 if (check_restricted() || check_secure())
8972 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008973 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008974 do_mzeval(str, rettv);
8975}
8976
8977 void
8978mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8979{
8980 typval_T argvars[3];
8981
8982 argvars[0].v_type = VAR_STRING;
8983 argvars[0].vval.v_string = name;
8984 copy_tv(args, &argvars[1]);
8985 argvars[2].v_type = VAR_UNKNOWN;
8986 f_call(argvars, rettv);
8987 clear_tv(&argvars[1]);
8988}
8989#endif
8990
8991/*
8992 * "nextnonblank()" function
8993 */
8994 static void
8995f_nextnonblank(typval_T *argvars, typval_T *rettv)
8996{
8997 linenr_T lnum;
8998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008999 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009000 {
9001 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9002 {
9003 lnum = 0;
9004 break;
9005 }
9006 if (*skipwhite(ml_get(lnum)) != NUL)
9007 break;
9008 }
9009 rettv->vval.v_number = lnum;
9010}
9011
9012/*
9013 * "nr2char()" function
9014 */
9015 static void
9016f_nr2char(typval_T *argvars, typval_T *rettv)
9017{
9018 char_u buf[NUMBUFLEN];
9019
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009020 if (has_mbyte)
9021 {
9022 int utf8 = 0;
9023
9024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009025 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009026 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009027 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009028 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009029 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009030 }
9031 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009032 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009033 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009034 buf[1] = NUL;
9035 }
9036 rettv->v_type = VAR_STRING;
9037 rettv->vval.v_string = vim_strsave(buf);
9038}
9039
9040/*
9041 * "or(expr, expr)" function
9042 */
9043 static void
9044f_or(typval_T *argvars, typval_T *rettv)
9045{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009046 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9047 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009048}
9049
9050/*
9051 * "pathshorten()" function
9052 */
9053 static void
9054f_pathshorten(typval_T *argvars, typval_T *rettv)
9055{
9056 char_u *p;
9057
9058 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009059 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009060 if (p == NULL)
9061 rettv->vval.v_string = NULL;
9062 else
9063 {
9064 p = vim_strsave(p);
9065 rettv->vval.v_string = p;
9066 if (p != NULL)
9067 shorten_dir(p);
9068 }
9069}
9070
9071#ifdef FEAT_PERL
9072/*
9073 * "perleval()" function
9074 */
9075 static void
9076f_perleval(typval_T *argvars, typval_T *rettv)
9077{
9078 char_u *str;
9079 char_u buf[NUMBUFLEN];
9080
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009082 do_perleval(str, rettv);
9083}
9084#endif
9085
9086#ifdef FEAT_FLOAT
9087/*
9088 * "pow()" function
9089 */
9090 static void
9091f_pow(typval_T *argvars, typval_T *rettv)
9092{
9093 float_T fx = 0.0, fy = 0.0;
9094
9095 rettv->v_type = VAR_FLOAT;
9096 if (get_float_arg(argvars, &fx) == OK
9097 && get_float_arg(&argvars[1], &fy) == OK)
9098 rettv->vval.v_float = pow(fx, fy);
9099 else
9100 rettv->vval.v_float = 0.0;
9101}
9102#endif
9103
9104/*
9105 * "prevnonblank()" function
9106 */
9107 static void
9108f_prevnonblank(typval_T *argvars, typval_T *rettv)
9109{
9110 linenr_T lnum;
9111
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009112 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009113 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9114 lnum = 0;
9115 else
9116 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9117 --lnum;
9118 rettv->vval.v_number = lnum;
9119}
9120
9121/* This dummy va_list is here because:
9122 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9123 * - locally in the function results in a "used before set" warning
9124 * - using va_start() to initialize it gives "function with fixed args" error */
9125static va_list ap;
9126
9127/*
9128 * "printf()" function
9129 */
9130 static void
9131f_printf(typval_T *argvars, typval_T *rettv)
9132{
9133 char_u buf[NUMBUFLEN];
9134 int len;
9135 char_u *s;
9136 int saved_did_emsg = did_emsg;
9137 char *fmt;
9138
9139 rettv->v_type = VAR_STRING;
9140 rettv->vval.v_string = NULL;
9141
9142 /* Get the required length, allocate the buffer and do it for real. */
9143 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009144 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009145 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009146 if (!did_emsg)
9147 {
9148 s = alloc(len + 1);
9149 if (s != NULL)
9150 {
9151 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009152 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9153 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009154 }
9155 }
9156 did_emsg |= saved_did_emsg;
9157}
9158
Bram Moolenaarf2732452018-06-03 14:47:35 +02009159#ifdef FEAT_JOB_CHANNEL
9160/*
9161 * "prompt_setcallback({buffer}, {callback})" function
9162 */
9163 static void
9164f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9165{
9166 buf_T *buf;
9167 char_u *callback;
9168 partial_T *partial;
9169
9170 if (check_secure())
9171 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009172 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009173 if (buf == NULL)
9174 return;
9175
9176 callback = get_callback(&argvars[1], &partial);
9177 if (callback == NULL)
9178 return;
9179
9180 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9181 if (partial == NULL)
9182 buf->b_prompt_callback = vim_strsave(callback);
9183 else
9184 /* pointer into the partial */
9185 buf->b_prompt_callback = callback;
9186 buf->b_prompt_partial = partial;
9187}
9188
9189/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009190 * "prompt_setinterrupt({buffer}, {callback})" function
9191 */
9192 static void
9193f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9194{
9195 buf_T *buf;
9196 char_u *callback;
9197 partial_T *partial;
9198
9199 if (check_secure())
9200 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009201 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009202 if (buf == NULL)
9203 return;
9204
9205 callback = get_callback(&argvars[1], &partial);
9206 if (callback == NULL)
9207 return;
9208
9209 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9210 if (partial == NULL)
9211 buf->b_prompt_interrupt = vim_strsave(callback);
9212 else
9213 /* pointer into the partial */
9214 buf->b_prompt_interrupt = callback;
9215 buf->b_prompt_int_partial = partial;
9216}
9217
9218/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009219 * "prompt_setprompt({buffer}, {text})" function
9220 */
9221 static void
9222f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9223{
9224 buf_T *buf;
9225 char_u *text;
9226
9227 if (check_secure())
9228 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009229 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009230 if (buf == NULL)
9231 return;
9232
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009233 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009234 vim_free(buf->b_prompt_text);
9235 buf->b_prompt_text = vim_strsave(text);
9236}
9237#endif
9238
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009239/*
9240 * "pumvisible()" function
9241 */
9242 static void
9243f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9244{
9245#ifdef FEAT_INS_EXPAND
9246 if (pum_visible())
9247 rettv->vval.v_number = 1;
9248#endif
9249}
9250
9251#ifdef FEAT_PYTHON3
9252/*
9253 * "py3eval()" function
9254 */
9255 static void
9256f_py3eval(typval_T *argvars, typval_T *rettv)
9257{
9258 char_u *str;
9259 char_u buf[NUMBUFLEN];
9260
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009261 if (check_restricted() || check_secure())
9262 return;
9263
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009264 if (p_pyx == 0)
9265 p_pyx = 3;
9266
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009267 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009268 do_py3eval(str, rettv);
9269}
9270#endif
9271
9272#ifdef FEAT_PYTHON
9273/*
9274 * "pyeval()" function
9275 */
9276 static void
9277f_pyeval(typval_T *argvars, typval_T *rettv)
9278{
9279 char_u *str;
9280 char_u buf[NUMBUFLEN];
9281
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009282 if (check_restricted() || check_secure())
9283 return;
9284
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009285 if (p_pyx == 0)
9286 p_pyx = 2;
9287
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009288 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009289 do_pyeval(str, rettv);
9290}
9291#endif
9292
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009293#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9294/*
9295 * "pyxeval()" function
9296 */
9297 static void
9298f_pyxeval(typval_T *argvars, typval_T *rettv)
9299{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009300 if (check_restricted() || check_secure())
9301 return;
9302
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009303# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9304 init_pyxversion();
9305 if (p_pyx == 2)
9306 f_pyeval(argvars, rettv);
9307 else
9308 f_py3eval(argvars, rettv);
9309# elif defined(FEAT_PYTHON)
9310 f_pyeval(argvars, rettv);
9311# elif defined(FEAT_PYTHON3)
9312 f_py3eval(argvars, rettv);
9313# endif
9314}
9315#endif
9316
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009317/*
9318 * "range()" function
9319 */
9320 static void
9321f_range(typval_T *argvars, typval_T *rettv)
9322{
9323 varnumber_T start;
9324 varnumber_T end;
9325 varnumber_T stride = 1;
9326 varnumber_T i;
9327 int error = FALSE;
9328
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009329 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009330 if (argvars[1].v_type == VAR_UNKNOWN)
9331 {
9332 end = start - 1;
9333 start = 0;
9334 }
9335 else
9336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009337 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009339 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009340 }
9341
9342 if (error)
9343 return; /* type error; errmsg already given */
9344 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009345 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009346 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009347 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009348 else
9349 {
9350 if (rettv_list_alloc(rettv) == OK)
9351 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9352 if (list_append_number(rettv->vval.v_list,
9353 (varnumber_T)i) == FAIL)
9354 break;
9355 }
9356}
9357
9358/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009359 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009360 */
9361 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009362readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009363{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009364 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009365 typval_T save_val;
9366 typval_T rettv;
9367 typval_T argv[2];
9368 int retval = 0;
9369 int error = FALSE;
9370
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009371 if (expr->v_type == VAR_UNKNOWN)
9372 return 1;
9373
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009374 prepare_vimvar(VV_VAL, &save_val);
9375 set_vim_var_string(VV_VAL, name, -1);
9376 argv[0].v_type = VAR_STRING;
9377 argv[0].vval.v_string = name;
9378
9379 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9380 goto theend;
9381
9382 retval = tv_get_number_chk(&rettv, &error);
9383 if (error)
9384 retval = -1;
9385 clear_tv(&rettv);
9386
9387theend:
9388 set_vim_var_string(VV_VAL, NULL, 0);
9389 restore_vimvar(VV_VAL, &save_val);
9390 return retval;
9391}
9392
9393/*
9394 * "readdir()" function
9395 */
9396 static void
9397f_readdir(typval_T *argvars, typval_T *rettv)
9398{
9399 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009400 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009401 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009402 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009403 garray_T ga;
9404 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009405
9406 if (rettv_list_alloc(rettv) == FAIL)
9407 return;
9408 path = tv_get_string(&argvars[0]);
9409 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009410
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009411 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9412 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009413 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009414 for (i = 0; i < ga.ga_len; i++)
9415 {
9416 p = ((char_u **)ga.ga_data)[i];
9417 list_append_string(rettv->vval.v_list, p, -1);
9418 }
9419 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009420 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009421}
9422
9423/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 * "readfile()" function
9425 */
9426 static void
9427f_readfile(typval_T *argvars, typval_T *rettv)
9428{
9429 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009430 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009431 int failed = FALSE;
9432 char_u *fname;
9433 FILE *fd;
9434 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9435 int io_size = sizeof(buf);
9436 int readlen; /* size of last fread() */
9437 char_u *prev = NULL; /* previously read bytes, if any */
9438 long prevlen = 0; /* length of data in prev */
9439 long prevsize = 0; /* size of prev buffer */
9440 long maxline = MAXLNUM;
9441 long cnt = 0;
9442 char_u *p; /* position in buf */
9443 char_u *start; /* start of current line */
9444
9445 if (argvars[1].v_type != VAR_UNKNOWN)
9446 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009447 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009449 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9450 blob = TRUE;
9451
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009453 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454 }
9455
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009456 if (blob)
9457 {
9458 if (rettv_blob_alloc(rettv) == FAIL)
9459 return;
9460 }
9461 else
9462 {
9463 if (rettv_list_alloc(rettv) == FAIL)
9464 return;
9465 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466
9467 /* Always open the file in binary mode, library functions have a mind of
9468 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009469 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009470 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009472 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009473 return;
9474 }
9475
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009476 if (blob)
9477 {
9478 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009480 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009481 blob_free(rettv->vval.v_blob);
9482 }
9483 fclose(fd);
9484 return;
9485 }
9486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 while (cnt < maxline || maxline < 0)
9488 {
9489 readlen = (int)fread(buf, 1, io_size, fd);
9490
9491 /* This for loop processes what was read, but is also entered at end
9492 * of file so that either:
9493 * - an incomplete line gets written
9494 * - a "binary" file gets an empty line at the end if it ends in a
9495 * newline. */
9496 for (p = buf, start = buf;
9497 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9498 ++p)
9499 {
9500 if (*p == '\n' || readlen <= 0)
9501 {
9502 listitem_T *li;
9503 char_u *s = NULL;
9504 long_u len = p - start;
9505
9506 /* Finished a line. Remove CRs before NL. */
9507 if (readlen > 0 && !binary)
9508 {
9509 while (len > 0 && start[len - 1] == '\r')
9510 --len;
9511 /* removal may cross back to the "prev" string */
9512 if (len == 0)
9513 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9514 --prevlen;
9515 }
9516 if (prevlen == 0)
9517 s = vim_strnsave(start, (int)len);
9518 else
9519 {
9520 /* Change "prev" buffer to be the right size. This way
9521 * the bytes are only copied once, and very long lines are
9522 * allocated only once. */
9523 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9524 {
9525 mch_memmove(s + prevlen, start, len);
9526 s[prevlen + len] = NUL;
9527 prev = NULL; /* the list will own the string */
9528 prevlen = prevsize = 0;
9529 }
9530 }
9531 if (s == NULL)
9532 {
9533 do_outofmem_msg((long_u) prevlen + len + 1);
9534 failed = TRUE;
9535 break;
9536 }
9537
9538 if ((li = listitem_alloc()) == NULL)
9539 {
9540 vim_free(s);
9541 failed = TRUE;
9542 break;
9543 }
9544 li->li_tv.v_type = VAR_STRING;
9545 li->li_tv.v_lock = 0;
9546 li->li_tv.vval.v_string = s;
9547 list_append(rettv->vval.v_list, li);
9548
9549 start = p + 1; /* step over newline */
9550 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9551 break;
9552 }
9553 else if (*p == NUL)
9554 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009555 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9556 * when finding the BF and check the previous two bytes. */
9557 else if (*p == 0xbf && enc_utf8 && !binary)
9558 {
9559 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9560 * + 1, these may be in the "prev" string. */
9561 char_u back1 = p >= buf + 1 ? p[-1]
9562 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9563 char_u back2 = p >= buf + 2 ? p[-2]
9564 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9565 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9566
9567 if (back2 == 0xef && back1 == 0xbb)
9568 {
9569 char_u *dest = p - 2;
9570
9571 /* Usually a BOM is at the beginning of a file, and so at
9572 * the beginning of a line; then we can just step over it.
9573 */
9574 if (start == dest)
9575 start = p + 1;
9576 else
9577 {
9578 /* have to shuffle buf to close gap */
9579 int adjust_prevlen = 0;
9580
9581 if (dest < buf)
9582 {
9583 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9584 dest = buf;
9585 }
9586 if (readlen > p - buf + 1)
9587 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9588 readlen -= 3 - adjust_prevlen;
9589 prevlen -= adjust_prevlen;
9590 p = dest - 1;
9591 }
9592 }
9593 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009594 } /* for */
9595
9596 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9597 break;
9598 if (start < p)
9599 {
9600 /* There's part of a line in buf, store it in "prev". */
9601 if (p - start + prevlen >= prevsize)
9602 {
9603 /* need bigger "prev" buffer */
9604 char_u *newprev;
9605
9606 /* A common use case is ordinary text files and "prev" gets a
9607 * fragment of a line, so the first allocation is made
9608 * small, to avoid repeatedly 'allocing' large and
9609 * 'reallocing' small. */
9610 if (prevsize == 0)
9611 prevsize = (long)(p - start);
9612 else
9613 {
9614 long grow50pc = (prevsize * 3) / 2;
9615 long growmin = (long)((p - start) * 2 + prevlen);
9616 prevsize = grow50pc > growmin ? grow50pc : growmin;
9617 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009618 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009619 if (newprev == NULL)
9620 {
9621 do_outofmem_msg((long_u)prevsize);
9622 failed = TRUE;
9623 break;
9624 }
9625 prev = newprev;
9626 }
9627 /* Add the line part to end of "prev". */
9628 mch_memmove(prev + prevlen, start, p - start);
9629 prevlen += (long)(p - start);
9630 }
9631 } /* while */
9632
9633 /*
9634 * For a negative line count use only the lines at the end of the file,
9635 * free the rest.
9636 */
9637 if (!failed && maxline < 0)
9638 while (cnt > -maxline)
9639 {
9640 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9641 --cnt;
9642 }
9643
9644 if (failed)
9645 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009646 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009648 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009649 }
9650
9651 vim_free(prev);
9652 fclose(fd);
9653}
9654
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009655 static void
9656return_register(int regname, typval_T *rettv)
9657{
9658 char_u buf[2] = {0, 0};
9659
9660 buf[0] = (char_u)regname;
9661 rettv->v_type = VAR_STRING;
9662 rettv->vval.v_string = vim_strsave(buf);
9663}
9664
9665/*
9666 * "reg_executing()" function
9667 */
9668 static void
9669f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9670{
9671 return_register(reg_executing, rettv);
9672}
9673
9674/*
9675 * "reg_recording()" function
9676 */
9677 static void
9678f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9679{
9680 return_register(reg_recording, rettv);
9681}
9682
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684/*
9685 * Convert a List to proftime_T.
9686 * Return FAIL when there is something wrong.
9687 */
9688 static int
9689list2proftime(typval_T *arg, proftime_T *tm)
9690{
9691 long n1, n2;
9692 int error = FALSE;
9693
9694 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9695 || arg->vval.v_list->lv_len != 2)
9696 return FAIL;
9697 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9698 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009699# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009700 tm->HighPart = n1;
9701 tm->LowPart = n2;
9702# else
9703 tm->tv_sec = n1;
9704 tm->tv_usec = n2;
9705# endif
9706 return error ? FAIL : OK;
9707}
9708#endif /* FEAT_RELTIME */
9709
9710/*
9711 * "reltime()" function
9712 */
9713 static void
9714f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9715{
9716#ifdef FEAT_RELTIME
9717 proftime_T res;
9718 proftime_T start;
9719
9720 if (argvars[0].v_type == VAR_UNKNOWN)
9721 {
9722 /* No arguments: get current time. */
9723 profile_start(&res);
9724 }
9725 else if (argvars[1].v_type == VAR_UNKNOWN)
9726 {
9727 if (list2proftime(&argvars[0], &res) == FAIL)
9728 return;
9729 profile_end(&res);
9730 }
9731 else
9732 {
9733 /* Two arguments: compute the difference. */
9734 if (list2proftime(&argvars[0], &start) == FAIL
9735 || list2proftime(&argvars[1], &res) == FAIL)
9736 return;
9737 profile_sub(&res, &start);
9738 }
9739
9740 if (rettv_list_alloc(rettv) == OK)
9741 {
9742 long n1, n2;
9743
Bram Moolenaar4f974752019-02-17 17:44:42 +01009744# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009745 n1 = res.HighPart;
9746 n2 = res.LowPart;
9747# else
9748 n1 = res.tv_sec;
9749 n2 = res.tv_usec;
9750# endif
9751 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9752 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9753 }
9754#endif
9755}
9756
9757#ifdef FEAT_FLOAT
9758/*
9759 * "reltimefloat()" function
9760 */
9761 static void
9762f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9763{
9764# ifdef FEAT_RELTIME
9765 proftime_T tm;
9766# endif
9767
9768 rettv->v_type = VAR_FLOAT;
9769 rettv->vval.v_float = 0;
9770# ifdef FEAT_RELTIME
9771 if (list2proftime(&argvars[0], &tm) == OK)
9772 rettv->vval.v_float = profile_float(&tm);
9773# endif
9774}
9775#endif
9776
9777/*
9778 * "reltimestr()" function
9779 */
9780 static void
9781f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9782{
9783#ifdef FEAT_RELTIME
9784 proftime_T tm;
9785#endif
9786
9787 rettv->v_type = VAR_STRING;
9788 rettv->vval.v_string = NULL;
9789#ifdef FEAT_RELTIME
9790 if (list2proftime(&argvars[0], &tm) == OK)
9791 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9792#endif
9793}
9794
9795#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009796 static void
9797make_connection(void)
9798{
9799 if (X_DISPLAY == NULL
9800# ifdef FEAT_GUI
9801 && !gui.in_use
9802# endif
9803 )
9804 {
9805 x_force_connect = TRUE;
9806 setup_term_clip();
9807 x_force_connect = FALSE;
9808 }
9809}
9810
9811 static int
9812check_connection(void)
9813{
9814 make_connection();
9815 if (X_DISPLAY == NULL)
9816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009817 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009818 return FAIL;
9819 }
9820 return OK;
9821}
9822#endif
9823
9824#ifdef FEAT_CLIENTSERVER
9825 static void
9826remote_common(typval_T *argvars, typval_T *rettv, int expr)
9827{
9828 char_u *server_name;
9829 char_u *keys;
9830 char_u *r = NULL;
9831 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009832 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009833# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009834 HWND w;
9835# else
9836 Window w;
9837# endif
9838
9839 if (check_restricted() || check_secure())
9840 return;
9841
9842# ifdef FEAT_X11
9843 if (check_connection() == FAIL)
9844 return;
9845# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009846 if (argvars[2].v_type != VAR_UNKNOWN
9847 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009848 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009849
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009850 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009851 if (server_name == NULL)
9852 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009853 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009854# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009855 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009856# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009857 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9858 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009859# endif
9860 {
9861 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009862 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009863 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009864 vim_free(r);
9865 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009866 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009867 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009868 return;
9869 }
9870
9871 rettv->vval.v_string = r;
9872
9873 if (argvars[2].v_type != VAR_UNKNOWN)
9874 {
9875 dictitem_T v;
9876 char_u str[30];
9877 char_u *idvar;
9878
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009879 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009880 if (idvar != NULL && *idvar != NUL)
9881 {
9882 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9883 v.di_tv.v_type = VAR_STRING;
9884 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009885 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009886 vim_free(v.di_tv.vval.v_string);
9887 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009888 }
9889}
9890#endif
9891
9892/*
9893 * "remote_expr()" function
9894 */
9895 static void
9896f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9897{
9898 rettv->v_type = VAR_STRING;
9899 rettv->vval.v_string = NULL;
9900#ifdef FEAT_CLIENTSERVER
9901 remote_common(argvars, rettv, TRUE);
9902#endif
9903}
9904
9905/*
9906 * "remote_foreground()" function
9907 */
9908 static void
9909f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9910{
9911#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009912# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009913 /* On Win32 it's done in this application. */
9914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009915 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009916
9917 if (server_name != NULL)
9918 serverForeground(server_name);
9919 }
9920# else
9921 /* Send a foreground() expression to the server. */
9922 argvars[1].v_type = VAR_STRING;
9923 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9924 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009925 rettv->v_type = VAR_STRING;
9926 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009927 remote_common(argvars, rettv, TRUE);
9928 vim_free(argvars[1].vval.v_string);
9929# endif
9930#endif
9931}
9932
9933 static void
9934f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9935{
9936#ifdef FEAT_CLIENTSERVER
9937 dictitem_T v;
9938 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009939# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940 long_u n = 0;
9941# endif
9942 char_u *serverid;
9943
9944 if (check_restricted() || check_secure())
9945 {
9946 rettv->vval.v_number = -1;
9947 return;
9948 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009949 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 if (serverid == NULL)
9951 {
9952 rettv->vval.v_number = -1;
9953 return; /* type error; errmsg already given */
9954 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009955# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9957 if (n == 0)
9958 rettv->vval.v_number = -1;
9959 else
9960 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009961 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 rettv->vval.v_number = (s != NULL);
9963 }
9964# else
9965 if (check_connection() == FAIL)
9966 return;
9967
9968 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9969 serverStrToWin(serverid), &s);
9970# endif
9971
9972 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9973 {
9974 char_u *retvar;
9975
9976 v.di_tv.v_type = VAR_STRING;
9977 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009978 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009979 if (retvar != NULL)
9980 set_var(retvar, &v.di_tv, FALSE);
9981 vim_free(v.di_tv.vval.v_string);
9982 }
9983#else
9984 rettv->vval.v_number = -1;
9985#endif
9986}
9987
9988 static void
9989f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9990{
9991 char_u *r = NULL;
9992
9993#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009994 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009995
9996 if (serverid != NULL && !check_restricted() && !check_secure())
9997 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009998 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009999# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010000 /* The server's HWND is encoded in the 'id' parameter */
10001 long_u n = 0;
10002# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010003
10004 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010005 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010006
Bram Moolenaar4f974752019-02-17 17:44:42 +010010007# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010008 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10009 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010010 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010011 if (r == NULL)
10012# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010013 if (check_connection() == FAIL
10014 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10015 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010016# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010017 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010018 }
10019#endif
10020 rettv->v_type = VAR_STRING;
10021 rettv->vval.v_string = r;
10022}
10023
10024/*
10025 * "remote_send()" function
10026 */
10027 static void
10028f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10029{
10030 rettv->v_type = VAR_STRING;
10031 rettv->vval.v_string = NULL;
10032#ifdef FEAT_CLIENTSERVER
10033 remote_common(argvars, rettv, FALSE);
10034#endif
10035}
10036
10037/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010038 * "remote_startserver()" function
10039 */
10040 static void
10041f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10042{
10043#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010044 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010045
10046 if (server == NULL)
10047 return; /* type error; errmsg already given */
10048 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010049 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010050 else
10051 {
10052# ifdef FEAT_X11
10053 if (check_connection() == OK)
10054 serverRegisterName(X_DISPLAY, server);
10055# else
10056 serverSetName(server);
10057# endif
10058 }
10059#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010060 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010061#endif
10062}
10063
10064/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010065 * "remove()" function
10066 */
10067 static void
10068f_remove(typval_T *argvars, typval_T *rettv)
10069{
10070 list_T *l;
10071 listitem_T *item, *item2;
10072 listitem_T *li;
10073 long idx;
10074 long end;
10075 char_u *key;
10076 dict_T *d;
10077 dictitem_T *di;
10078 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010079 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010080
10081 if (argvars[0].v_type == VAR_DICT)
10082 {
10083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010084 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010085 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010086 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010087 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010088 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089 if (key != NULL)
10090 {
10091 di = dict_find(d, key, -1);
10092 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010093 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010094 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10095 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10096 {
10097 *rettv = di->di_tv;
10098 init_tv(&di->di_tv);
10099 dictitem_remove(d, di);
10100 }
10101 }
10102 }
10103 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010104 else if (argvars[0].v_type == VAR_BLOB)
10105 {
10106 idx = (long)tv_get_number_chk(&argvars[1], &error);
10107 if (!error)
10108 {
10109 blob_T *b = argvars[0].vval.v_blob;
10110 int len = blob_len(b);
10111 char_u *p;
10112
10113 if (idx < 0)
10114 // count from the end
10115 idx = len + idx;
10116 if (idx < 0 || idx >= len)
10117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010118 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010119 return;
10120 }
10121 if (argvars[2].v_type == VAR_UNKNOWN)
10122 {
10123 // Remove one item, return its value.
10124 p = (char_u *)b->bv_ga.ga_data;
10125 rettv->vval.v_number = (varnumber_T) *(p + idx);
10126 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10127 --b->bv_ga.ga_len;
10128 }
10129 else
10130 {
10131 blob_T *blob;
10132
10133 // Remove range of items, return list with values.
10134 end = (long)tv_get_number_chk(&argvars[2], &error);
10135 if (error)
10136 return;
10137 if (end < 0)
10138 // count from the end
10139 end = len + end;
10140 if (end >= len || idx > end)
10141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010142 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010143 return;
10144 }
10145 blob = blob_alloc();
10146 if (blob == NULL)
10147 return;
10148 blob->bv_ga.ga_len = end - idx + 1;
10149 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10150 {
10151 vim_free(blob);
10152 return;
10153 }
10154 p = (char_u *)b->bv_ga.ga_data;
10155 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10156 (size_t)(end - idx + 1));
10157 ++blob->bv_refcount;
10158 rettv->v_type = VAR_BLOB;
10159 rettv->vval.v_blob = blob;
10160
10161 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10162 b->bv_ga.ga_len -= end - idx + 1;
10163 }
10164 }
10165 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010167 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010169 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010171 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010172 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010173 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010174 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010175 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010176 else
10177 {
10178 if (argvars[2].v_type == VAR_UNKNOWN)
10179 {
10180 /* Remove one item, return its value. */
10181 vimlist_remove(l, item, item);
10182 *rettv = item->li_tv;
10183 vim_free(item);
10184 }
10185 else
10186 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010187 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010188 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010189 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010190 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010191 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010192 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010193 else
10194 {
10195 int cnt = 0;
10196
10197 for (li = item; li != NULL; li = li->li_next)
10198 {
10199 ++cnt;
10200 if (li == item2)
10201 break;
10202 }
10203 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010204 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 else
10206 {
10207 vimlist_remove(l, item, item2);
10208 if (rettv_list_alloc(rettv) == OK)
10209 {
10210 l = rettv->vval.v_list;
10211 l->lv_first = item;
10212 l->lv_last = item2;
10213 item->li_prev = NULL;
10214 item2->li_next = NULL;
10215 l->lv_len = cnt;
10216 }
10217 }
10218 }
10219 }
10220 }
10221 }
10222}
10223
10224/*
10225 * "rename({from}, {to})" function
10226 */
10227 static void
10228f_rename(typval_T *argvars, typval_T *rettv)
10229{
10230 char_u buf[NUMBUFLEN];
10231
10232 if (check_restricted() || check_secure())
10233 rettv->vval.v_number = -1;
10234 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010235 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10236 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010237}
10238
10239/*
10240 * "repeat()" function
10241 */
10242 static void
10243f_repeat(typval_T *argvars, typval_T *rettv)
10244{
10245 char_u *p;
10246 int n;
10247 int slen;
10248 int len;
10249 char_u *r;
10250 int i;
10251
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010252 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010253 if (argvars[0].v_type == VAR_LIST)
10254 {
10255 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10256 while (n-- > 0)
10257 if (list_extend(rettv->vval.v_list,
10258 argvars[0].vval.v_list, NULL) == FAIL)
10259 break;
10260 }
10261 else
10262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010263 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010264 rettv->v_type = VAR_STRING;
10265 rettv->vval.v_string = NULL;
10266
10267 slen = (int)STRLEN(p);
10268 len = slen * n;
10269 if (len <= 0)
10270 return;
10271
10272 r = alloc(len + 1);
10273 if (r != NULL)
10274 {
10275 for (i = 0; i < n; i++)
10276 mch_memmove(r + i * slen, p, (size_t)slen);
10277 r[len] = NUL;
10278 }
10279
10280 rettv->vval.v_string = r;
10281 }
10282}
10283
10284/*
10285 * "resolve()" function
10286 */
10287 static void
10288f_resolve(typval_T *argvars, typval_T *rettv)
10289{
10290 char_u *p;
10291#ifdef HAVE_READLINK
10292 char_u *buf = NULL;
10293#endif
10294
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010295 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010296#ifdef FEAT_SHORTCUT
10297 {
10298 char_u *v = NULL;
10299
Bram Moolenaardce1e892019-02-10 23:18:53 +010010300 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010301 if (v != NULL)
10302 rettv->vval.v_string = v;
10303 else
10304 rettv->vval.v_string = vim_strsave(p);
10305 }
10306#else
10307# ifdef HAVE_READLINK
10308 {
10309 char_u *cpy;
10310 int len;
10311 char_u *remain = NULL;
10312 char_u *q;
10313 int is_relative_to_current = FALSE;
10314 int has_trailing_pathsep = FALSE;
10315 int limit = 100;
10316
10317 p = vim_strsave(p);
10318
10319 if (p[0] == '.' && (vim_ispathsep(p[1])
10320 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10321 is_relative_to_current = TRUE;
10322
10323 len = STRLEN(p);
10324 if (len > 0 && after_pathsep(p, p + len))
10325 {
10326 has_trailing_pathsep = TRUE;
10327 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10328 }
10329
10330 q = getnextcomp(p);
10331 if (*q != NUL)
10332 {
10333 /* Separate the first path component in "p", and keep the
10334 * remainder (beginning with the path separator). */
10335 remain = vim_strsave(q - 1);
10336 q[-1] = NUL;
10337 }
10338
10339 buf = alloc(MAXPATHL + 1);
10340 if (buf == NULL)
10341 goto fail;
10342
10343 for (;;)
10344 {
10345 for (;;)
10346 {
10347 len = readlink((char *)p, (char *)buf, MAXPATHL);
10348 if (len <= 0)
10349 break;
10350 buf[len] = NUL;
10351
10352 if (limit-- == 0)
10353 {
10354 vim_free(p);
10355 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010356 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010357 rettv->vval.v_string = NULL;
10358 goto fail;
10359 }
10360
10361 /* Ensure that the result will have a trailing path separator
10362 * if the argument has one. */
10363 if (remain == NULL && has_trailing_pathsep)
10364 add_pathsep(buf);
10365
10366 /* Separate the first path component in the link value and
10367 * concatenate the remainders. */
10368 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10369 if (*q != NUL)
10370 {
10371 if (remain == NULL)
10372 remain = vim_strsave(q - 1);
10373 else
10374 {
10375 cpy = concat_str(q - 1, remain);
10376 if (cpy != NULL)
10377 {
10378 vim_free(remain);
10379 remain = cpy;
10380 }
10381 }
10382 q[-1] = NUL;
10383 }
10384
10385 q = gettail(p);
10386 if (q > p && *q == NUL)
10387 {
10388 /* Ignore trailing path separator. */
10389 q[-1] = NUL;
10390 q = gettail(p);
10391 }
10392 if (q > p && !mch_isFullName(buf))
10393 {
10394 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010395 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010396 if (cpy != NULL)
10397 {
10398 STRCPY(cpy, p);
10399 STRCPY(gettail(cpy), buf);
10400 vim_free(p);
10401 p = cpy;
10402 }
10403 }
10404 else
10405 {
10406 vim_free(p);
10407 p = vim_strsave(buf);
10408 }
10409 }
10410
10411 if (remain == NULL)
10412 break;
10413
10414 /* Append the first path component of "remain" to "p". */
10415 q = getnextcomp(remain + 1);
10416 len = q - remain - (*q != NUL);
10417 cpy = vim_strnsave(p, STRLEN(p) + len);
10418 if (cpy != NULL)
10419 {
10420 STRNCAT(cpy, remain, len);
10421 vim_free(p);
10422 p = cpy;
10423 }
10424 /* Shorten "remain". */
10425 if (*q != NUL)
10426 STRMOVE(remain, q - 1);
10427 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010428 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010429 }
10430
10431 /* If the result is a relative path name, make it explicitly relative to
10432 * the current directory if and only if the argument had this form. */
10433 if (!vim_ispathsep(*p))
10434 {
10435 if (is_relative_to_current
10436 && *p != NUL
10437 && !(p[0] == '.'
10438 && (p[1] == NUL
10439 || vim_ispathsep(p[1])
10440 || (p[1] == '.'
10441 && (p[2] == NUL
10442 || vim_ispathsep(p[2]))))))
10443 {
10444 /* Prepend "./". */
10445 cpy = concat_str((char_u *)"./", p);
10446 if (cpy != NULL)
10447 {
10448 vim_free(p);
10449 p = cpy;
10450 }
10451 }
10452 else if (!is_relative_to_current)
10453 {
10454 /* Strip leading "./". */
10455 q = p;
10456 while (q[0] == '.' && vim_ispathsep(q[1]))
10457 q += 2;
10458 if (q > p)
10459 STRMOVE(p, p + 2);
10460 }
10461 }
10462
10463 /* Ensure that the result will have no trailing path separator
10464 * if the argument had none. But keep "/" or "//". */
10465 if (!has_trailing_pathsep)
10466 {
10467 q = p + STRLEN(p);
10468 if (after_pathsep(p, q))
10469 *gettail_sep(p) = NUL;
10470 }
10471
10472 rettv->vval.v_string = p;
10473 }
10474# else
10475 rettv->vval.v_string = vim_strsave(p);
10476# endif
10477#endif
10478
10479 simplify_filename(rettv->vval.v_string);
10480
10481#ifdef HAVE_READLINK
10482fail:
10483 vim_free(buf);
10484#endif
10485 rettv->v_type = VAR_STRING;
10486}
10487
10488/*
10489 * "reverse({list})" function
10490 */
10491 static void
10492f_reverse(typval_T *argvars, typval_T *rettv)
10493{
10494 list_T *l;
10495 listitem_T *li, *ni;
10496
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010497 if (argvars[0].v_type == VAR_BLOB)
10498 {
10499 blob_T *b = argvars[0].vval.v_blob;
10500 int i, len = blob_len(b);
10501
10502 for (i = 0; i < len / 2; i++)
10503 {
10504 int tmp = blob_get(b, i);
10505
10506 blob_set(b, i, blob_get(b, len - i - 1));
10507 blob_set(b, len - i - 1, tmp);
10508 }
10509 rettv_blob_set(rettv, b);
10510 return;
10511 }
10512
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010513 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010514 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010515 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010516 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010517 (char_u *)N_("reverse() argument"), TRUE))
10518 {
10519 li = l->lv_last;
10520 l->lv_first = l->lv_last = NULL;
10521 l->lv_len = 0;
10522 while (li != NULL)
10523 {
10524 ni = li->li_prev;
10525 list_append(l, li);
10526 li = ni;
10527 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010528 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010529 l->lv_idx = l->lv_len - l->lv_idx - 1;
10530 }
10531}
10532
10533#define SP_NOMOVE 0x01 /* don't move cursor */
10534#define SP_REPEAT 0x02 /* repeat to find outer pair */
10535#define SP_RETCOUNT 0x04 /* return matchcount */
10536#define SP_SETPCMARK 0x08 /* set previous context mark */
10537#define SP_START 0x10 /* accept match at start position */
10538#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10539#define SP_END 0x40 /* leave cursor at end of match */
10540#define SP_COLUMN 0x80 /* start at cursor column */
10541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010542/*
10543 * Get flags for a search function.
10544 * Possibly sets "p_ws".
10545 * Returns BACKWARD, FORWARD or zero (for an error).
10546 */
10547 static int
10548get_search_arg(typval_T *varp, int *flagsp)
10549{
10550 int dir = FORWARD;
10551 char_u *flags;
10552 char_u nbuf[NUMBUFLEN];
10553 int mask;
10554
10555 if (varp->v_type != VAR_UNKNOWN)
10556 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010557 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010558 if (flags == NULL)
10559 return 0; /* type error; errmsg already given */
10560 while (*flags != NUL)
10561 {
10562 switch (*flags)
10563 {
10564 case 'b': dir = BACKWARD; break;
10565 case 'w': p_ws = TRUE; break;
10566 case 'W': p_ws = FALSE; break;
10567 default: mask = 0;
10568 if (flagsp != NULL)
10569 switch (*flags)
10570 {
10571 case 'c': mask = SP_START; break;
10572 case 'e': mask = SP_END; break;
10573 case 'm': mask = SP_RETCOUNT; break;
10574 case 'n': mask = SP_NOMOVE; break;
10575 case 'p': mask = SP_SUBPAT; break;
10576 case 'r': mask = SP_REPEAT; break;
10577 case 's': mask = SP_SETPCMARK; break;
10578 case 'z': mask = SP_COLUMN; break;
10579 }
10580 if (mask == 0)
10581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010582 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010583 dir = 0;
10584 }
10585 else
10586 *flagsp |= mask;
10587 }
10588 if (dir == 0)
10589 break;
10590 ++flags;
10591 }
10592 }
10593 return dir;
10594}
10595
10596/*
10597 * Shared by search() and searchpos() functions.
10598 */
10599 static int
10600search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10601{
10602 int flags;
10603 char_u *pat;
10604 pos_T pos;
10605 pos_T save_cursor;
10606 int save_p_ws = p_ws;
10607 int dir;
10608 int retval = 0; /* default: FAIL */
10609 long lnum_stop = 0;
10610 proftime_T tm;
10611#ifdef FEAT_RELTIME
10612 long time_limit = 0;
10613#endif
10614 int options = SEARCH_KEEP;
10615 int subpatnum;
10616
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010617 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10619 if (dir == 0)
10620 goto theend;
10621 flags = *flagsp;
10622 if (flags & SP_START)
10623 options |= SEARCH_START;
10624 if (flags & SP_END)
10625 options |= SEARCH_END;
10626 if (flags & SP_COLUMN)
10627 options |= SEARCH_COL;
10628
10629 /* Optional arguments: line number to stop searching and timeout. */
10630 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 if (lnum_stop < 0)
10634 goto theend;
10635#ifdef FEAT_RELTIME
10636 if (argvars[3].v_type != VAR_UNKNOWN)
10637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010638 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010639 if (time_limit < 0)
10640 goto theend;
10641 }
10642#endif
10643 }
10644
10645#ifdef FEAT_RELTIME
10646 /* Set the time limit, if there is one. */
10647 profile_setlimit(time_limit, &tm);
10648#endif
10649
10650 /*
10651 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10652 * Check to make sure only those flags are set.
10653 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10654 * flags cannot be set. Check for that condition also.
10655 */
10656 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10657 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010659 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 goto theend;
10661 }
10662
10663 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010664 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010665 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010666 if (subpatnum != FAIL)
10667 {
10668 if (flags & SP_SUBPAT)
10669 retval = subpatnum;
10670 else
10671 retval = pos.lnum;
10672 if (flags & SP_SETPCMARK)
10673 setpcmark();
10674 curwin->w_cursor = pos;
10675 if (match_pos != NULL)
10676 {
10677 /* Store the match cursor position */
10678 match_pos->lnum = pos.lnum;
10679 match_pos->col = pos.col + 1;
10680 }
10681 /* "/$" will put the cursor after the end of the line, may need to
10682 * correct that here */
10683 check_cursor();
10684 }
10685
10686 /* If 'n' flag is used: restore cursor position. */
10687 if (flags & SP_NOMOVE)
10688 curwin->w_cursor = save_cursor;
10689 else
10690 curwin->w_set_curswant = TRUE;
10691theend:
10692 p_ws = save_p_ws;
10693
10694 return retval;
10695}
10696
10697#ifdef FEAT_FLOAT
10698
10699/*
10700 * round() is not in C90, use ceil() or floor() instead.
10701 */
10702 float_T
10703vim_round(float_T f)
10704{
10705 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10706}
10707
10708/*
10709 * "round({float})" function
10710 */
10711 static void
10712f_round(typval_T *argvars, typval_T *rettv)
10713{
10714 float_T f = 0.0;
10715
10716 rettv->v_type = VAR_FLOAT;
10717 if (get_float_arg(argvars, &f) == OK)
10718 rettv->vval.v_float = vim_round(f);
10719 else
10720 rettv->vval.v_float = 0.0;
10721}
10722#endif
10723
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010724#ifdef FEAT_RUBY
10725/*
10726 * "rubyeval()" function
10727 */
10728 static void
10729f_rubyeval(typval_T *argvars, typval_T *rettv)
10730{
10731 char_u *str;
10732 char_u buf[NUMBUFLEN];
10733
10734 str = tv_get_string_buf(&argvars[0], buf);
10735 do_rubyeval(str, rettv);
10736}
10737#endif
10738
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010739/*
10740 * "screenattr()" function
10741 */
10742 static void
10743f_screenattr(typval_T *argvars, typval_T *rettv)
10744{
10745 int row;
10746 int col;
10747 int c;
10748
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010749 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10750 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010751 if (row < 0 || row >= screen_Rows
10752 || col < 0 || col >= screen_Columns)
10753 c = -1;
10754 else
10755 c = ScreenAttrs[LineOffset[row] + col];
10756 rettv->vval.v_number = c;
10757}
10758
10759/*
10760 * "screenchar()" function
10761 */
10762 static void
10763f_screenchar(typval_T *argvars, typval_T *rettv)
10764{
10765 int row;
10766 int col;
10767 int off;
10768 int c;
10769
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010770 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10771 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010772 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010773 c = -1;
10774 else
10775 {
10776 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010777 if (enc_utf8 && ScreenLinesUC[off] != 0)
10778 c = ScreenLinesUC[off];
10779 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010780 c = ScreenLines[off];
10781 }
10782 rettv->vval.v_number = c;
10783}
10784
10785/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010786 * "screenchars()" function
10787 */
10788 static void
10789f_screenchars(typval_T *argvars, typval_T *rettv)
10790{
10791 int row;
10792 int col;
10793 int off;
10794 int c;
10795 int i;
10796
10797 if (rettv_list_alloc(rettv) == FAIL)
10798 return;
10799 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10800 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10801 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10802 return;
10803
10804 off = LineOffset[row] + col;
10805 if (enc_utf8 && ScreenLinesUC[off] != 0)
10806 c = ScreenLinesUC[off];
10807 else
10808 c = ScreenLines[off];
10809 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10810
10811 if (enc_utf8)
10812
10813 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10814 list_append_number(rettv->vval.v_list,
10815 (varnumber_T)ScreenLinesC[i][off]);
10816}
10817
10818/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010819 * "screencol()" function
10820 *
10821 * First column is 1 to be consistent with virtcol().
10822 */
10823 static void
10824f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10825{
10826 rettv->vval.v_number = screen_screencol() + 1;
10827}
10828
10829/*
10830 * "screenrow()" function
10831 */
10832 static void
10833f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10834{
10835 rettv->vval.v_number = screen_screenrow() + 1;
10836}
10837
10838/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010839 * "screenstring()" function
10840 */
10841 static void
10842f_screenstring(typval_T *argvars, typval_T *rettv)
10843{
10844 int row;
10845 int col;
10846 int off;
10847 int c;
10848 int i;
10849 char_u buf[MB_MAXBYTES + 1];
10850 int buflen = 0;
10851
10852 rettv->vval.v_string = NULL;
10853 rettv->v_type = VAR_STRING;
10854
10855 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10856 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10857 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10858 return;
10859
10860 off = LineOffset[row] + col;
10861 if (enc_utf8 && ScreenLinesUC[off] != 0)
10862 c = ScreenLinesUC[off];
10863 else
10864 c = ScreenLines[off];
10865 buflen += mb_char2bytes(c, buf);
10866
10867 if (enc_utf8)
10868 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10869 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10870
10871 buf[buflen] = NUL;
10872 rettv->vval.v_string = vim_strsave(buf);
10873}
10874
10875/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010876 * "search()" function
10877 */
10878 static void
10879f_search(typval_T *argvars, typval_T *rettv)
10880{
10881 int flags = 0;
10882
10883 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10884}
10885
10886/*
10887 * "searchdecl()" function
10888 */
10889 static void
10890f_searchdecl(typval_T *argvars, typval_T *rettv)
10891{
10892 int locally = 1;
10893 int thisblock = 0;
10894 int error = FALSE;
10895 char_u *name;
10896
10897 rettv->vval.v_number = 1; /* default: FAIL */
10898
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010899 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 if (argvars[1].v_type != VAR_UNKNOWN)
10901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010902 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010903 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010904 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010905 }
10906 if (!error && name != NULL)
10907 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10908 locally, thisblock, SEARCH_KEEP) == FAIL;
10909}
10910
10911/*
10912 * Used by searchpair() and searchpairpos()
10913 */
10914 static int
10915searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10916{
10917 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010918 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919 int save_p_ws = p_ws;
10920 int dir;
10921 int flags = 0;
10922 char_u nbuf1[NUMBUFLEN];
10923 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 int retval = 0; /* default: FAIL */
10925 long lnum_stop = 0;
10926 long time_limit = 0;
10927
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010928 /* Get the three pattern arguments: start, middle, end. Will result in an
10929 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010930 spat = tv_get_string_chk(&argvars[0]);
10931 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10932 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010933 if (spat == NULL || mpat == NULL || epat == NULL)
10934 goto theend; /* type error */
10935
10936 /* Handle the optional fourth argument: flags */
10937 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10938 if (dir == 0)
10939 goto theend;
10940
10941 /* Don't accept SP_END or SP_SUBPAT.
10942 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10943 */
10944 if ((flags & (SP_END | SP_SUBPAT)) != 0
10945 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010947 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 goto theend;
10949 }
10950
10951 /* Using 'r' implies 'W', otherwise it doesn't work. */
10952 if (flags & SP_REPEAT)
10953 p_ws = FALSE;
10954
10955 /* Optional fifth argument: skip expression */
10956 if (argvars[3].v_type == VAR_UNKNOWN
10957 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010958 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 else
10960 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010961 skip = &argvars[4];
10962 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10963 && skip->v_type != VAR_STRING)
10964 {
10965 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010966 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010967 goto theend;
10968 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 if (argvars[5].v_type != VAR_UNKNOWN)
10970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010971 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010974 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010976 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010977#ifdef FEAT_RELTIME
10978 if (argvars[6].v_type != VAR_UNKNOWN)
10979 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010980 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010981 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010983 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010986 }
10987#endif
10988 }
10989 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010990
10991 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10992 match_pos, lnum_stop, time_limit);
10993
10994theend:
10995 p_ws = save_p_ws;
10996
10997 return retval;
10998}
10999
11000/*
11001 * "searchpair()" function
11002 */
11003 static void
11004f_searchpair(typval_T *argvars, typval_T *rettv)
11005{
11006 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11007}
11008
11009/*
11010 * "searchpairpos()" function
11011 */
11012 static void
11013f_searchpairpos(typval_T *argvars, typval_T *rettv)
11014{
11015 pos_T match_pos;
11016 int lnum = 0;
11017 int col = 0;
11018
11019 if (rettv_list_alloc(rettv) == FAIL)
11020 return;
11021
11022 if (searchpair_cmn(argvars, &match_pos) > 0)
11023 {
11024 lnum = match_pos.lnum;
11025 col = match_pos.col;
11026 }
11027
11028 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11029 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11030}
11031
11032/*
11033 * Search for a start/middle/end thing.
11034 * Used by searchpair(), see its documentation for the details.
11035 * Returns 0 or -1 for no match,
11036 */
11037 long
11038do_searchpair(
11039 char_u *spat, /* start pattern */
11040 char_u *mpat, /* middle pattern */
11041 char_u *epat, /* end pattern */
11042 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011043 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011044 int flags, /* SP_SETPCMARK and other SP_ values */
11045 pos_T *match_pos,
11046 linenr_T lnum_stop, /* stop at this line if not zero */
11047 long time_limit UNUSED) /* stop after this many msec */
11048{
11049 char_u *save_cpo;
11050 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11051 long retval = 0;
11052 pos_T pos;
11053 pos_T firstpos;
11054 pos_T foundpos;
11055 pos_T save_cursor;
11056 pos_T save_pos;
11057 int n;
11058 int r;
11059 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011060 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011061 int err;
11062 int options = SEARCH_KEEP;
11063 proftime_T tm;
11064
11065 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11066 save_cpo = p_cpo;
11067 p_cpo = empty_option;
11068
11069#ifdef FEAT_RELTIME
11070 /* Set the time limit, if there is one. */
11071 profile_setlimit(time_limit, &tm);
11072#endif
11073
11074 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11075 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011076 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11077 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011078 if (pat2 == NULL || pat3 == NULL)
11079 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011080 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011081 if (*mpat == NUL)
11082 STRCPY(pat3, pat2);
11083 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011084 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011085 spat, epat, mpat);
11086 if (flags & SP_START)
11087 options |= SEARCH_START;
11088
Bram Moolenaar48570482017-10-30 21:48:41 +010011089 if (skip != NULL)
11090 {
11091 /* Empty string means to not use the skip expression. */
11092 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11093 use_skip = skip->vval.v_string != NULL
11094 && *skip->vval.v_string != NUL;
11095 }
11096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011097 save_cursor = curwin->w_cursor;
11098 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011099 CLEAR_POS(&firstpos);
11100 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 pat = pat3;
11102 for (;;)
11103 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011104 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011105 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011106 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011107 /* didn't find it or found the first match again: FAIL */
11108 break;
11109
11110 if (firstpos.lnum == 0)
11111 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011112 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 {
11114 /* Found the same position again. Can happen with a pattern that
11115 * has "\zs" at the end and searching backwards. Advance one
11116 * character and try again. */
11117 if (dir == BACKWARD)
11118 decl(&pos);
11119 else
11120 incl(&pos);
11121 }
11122 foundpos = pos;
11123
11124 /* clear the start flag to avoid getting stuck here */
11125 options &= ~SEARCH_START;
11126
11127 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011128 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011129 {
11130 save_pos = curwin->w_cursor;
11131 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011132 err = FALSE;
11133 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011134 curwin->w_cursor = save_pos;
11135 if (err)
11136 {
11137 /* Evaluating {skip} caused an error, break here. */
11138 curwin->w_cursor = save_cursor;
11139 retval = -1;
11140 break;
11141 }
11142 if (r)
11143 continue;
11144 }
11145
11146 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11147 {
11148 /* Found end when searching backwards or start when searching
11149 * forward: nested pair. */
11150 ++nest;
11151 pat = pat2; /* nested, don't search for middle */
11152 }
11153 else
11154 {
11155 /* Found end when searching forward or start when searching
11156 * backward: end of (nested) pair; or found middle in outer pair. */
11157 if (--nest == 1)
11158 pat = pat3; /* outer level, search for middle */
11159 }
11160
11161 if (nest == 0)
11162 {
11163 /* Found the match: return matchcount or line number. */
11164 if (flags & SP_RETCOUNT)
11165 ++retval;
11166 else
11167 retval = pos.lnum;
11168 if (flags & SP_SETPCMARK)
11169 setpcmark();
11170 curwin->w_cursor = pos;
11171 if (!(flags & SP_REPEAT))
11172 break;
11173 nest = 1; /* search for next unmatched */
11174 }
11175 }
11176
11177 if (match_pos != NULL)
11178 {
11179 /* Store the match cursor position */
11180 match_pos->lnum = curwin->w_cursor.lnum;
11181 match_pos->col = curwin->w_cursor.col + 1;
11182 }
11183
11184 /* If 'n' flag is used or search failed: restore cursor position. */
11185 if ((flags & SP_NOMOVE) || retval == 0)
11186 curwin->w_cursor = save_cursor;
11187
11188theend:
11189 vim_free(pat2);
11190 vim_free(pat3);
11191 if (p_cpo == empty_option)
11192 p_cpo = save_cpo;
11193 else
11194 /* Darn, evaluating the {skip} expression changed the value. */
11195 free_string_option(save_cpo);
11196
11197 return retval;
11198}
11199
11200/*
11201 * "searchpos()" function
11202 */
11203 static void
11204f_searchpos(typval_T *argvars, typval_T *rettv)
11205{
11206 pos_T match_pos;
11207 int lnum = 0;
11208 int col = 0;
11209 int n;
11210 int flags = 0;
11211
11212 if (rettv_list_alloc(rettv) == FAIL)
11213 return;
11214
11215 n = search_cmn(argvars, &match_pos, &flags);
11216 if (n > 0)
11217 {
11218 lnum = match_pos.lnum;
11219 col = match_pos.col;
11220 }
11221
11222 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11223 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11224 if (flags & SP_SUBPAT)
11225 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11226}
11227
11228 static void
11229f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11230{
11231#ifdef FEAT_CLIENTSERVER
11232 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011233 char_u *server = tv_get_string_chk(&argvars[0]);
11234 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011235
11236 rettv->vval.v_number = -1;
11237 if (server == NULL || reply == NULL)
11238 return;
11239 if (check_restricted() || check_secure())
11240 return;
11241# ifdef FEAT_X11
11242 if (check_connection() == FAIL)
11243 return;
11244# endif
11245
11246 if (serverSendReply(server, reply) < 0)
11247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011248 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011249 return;
11250 }
11251 rettv->vval.v_number = 0;
11252#else
11253 rettv->vval.v_number = -1;
11254#endif
11255}
11256
11257 static void
11258f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11259{
11260 char_u *r = NULL;
11261
11262#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011263# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011264 r = serverGetVimNames();
11265# else
11266 make_connection();
11267 if (X_DISPLAY != NULL)
11268 r = serverGetVimNames(X_DISPLAY);
11269# endif
11270#endif
11271 rettv->v_type = VAR_STRING;
11272 rettv->vval.v_string = r;
11273}
11274
11275/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011276 * "setbufline()" function
11277 */
11278 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011279f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011280{
11281 linenr_T lnum;
11282 buf_T *buf;
11283
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011284 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011285 if (buf == NULL)
11286 rettv->vval.v_number = 1; /* FAIL */
11287 else
11288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011289 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011290 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011291 }
11292}
11293
11294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011295 * "setbufvar()" function
11296 */
11297 static void
11298f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11299{
11300 buf_T *buf;
11301 char_u *varname, *bufvarname;
11302 typval_T *varp;
11303 char_u nbuf[NUMBUFLEN];
11304
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011305 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011306 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011307 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11308 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011309 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011310 varp = &argvars[2];
11311
11312 if (buf != NULL && varname != NULL && varp != NULL)
11313 {
11314 if (*varname == '&')
11315 {
11316 long numval;
11317 char_u *strval;
11318 int error = FALSE;
11319 aco_save_T aco;
11320
11321 /* set curbuf to be our buf, temporarily */
11322 aucmd_prepbuf(&aco, buf);
11323
11324 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011325 numval = (long)tv_get_number_chk(varp, &error);
11326 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011327 if (!error && strval != NULL)
11328 set_option_value(varname, numval, strval, OPT_LOCAL);
11329
11330 /* reset notion of buffer */
11331 aucmd_restbuf(&aco);
11332 }
11333 else
11334 {
11335 buf_T *save_curbuf = curbuf;
11336
Bram Moolenaar964b3742019-05-24 18:54:09 +020011337 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011338 if (bufvarname != NULL)
11339 {
11340 curbuf = buf;
11341 STRCPY(bufvarname, "b:");
11342 STRCPY(bufvarname + 2, varname);
11343 set_var(bufvarname, varp, TRUE);
11344 vim_free(bufvarname);
11345 curbuf = save_curbuf;
11346 }
11347 }
11348 }
11349}
11350
11351 static void
11352f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11353{
11354 dict_T *d;
11355 dictitem_T *di;
11356 char_u *csearch;
11357
11358 if (argvars[0].v_type != VAR_DICT)
11359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011360 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 return;
11362 }
11363
11364 if ((d = argvars[0].vval.v_dict) != NULL)
11365 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011366 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011367 if (csearch != NULL)
11368 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011369 if (enc_utf8)
11370 {
11371 int pcc[MAX_MCO];
11372 int c = utfc_ptr2char(csearch, pcc);
11373
11374 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11375 }
11376 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011377 set_last_csearch(PTR2CHAR(csearch),
11378 csearch, MB_PTR2LEN(csearch));
11379 }
11380
11381 di = dict_find(d, (char_u *)"forward", -1);
11382 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011383 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011384 ? FORWARD : BACKWARD);
11385
11386 di = dict_find(d, (char_u *)"until", -1);
11387 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011388 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011389 }
11390}
11391
11392/*
11393 * "setcmdpos()" function
11394 */
11395 static void
11396f_setcmdpos(typval_T *argvars, typval_T *rettv)
11397{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011398 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011399
11400 if (pos >= 0)
11401 rettv->vval.v_number = set_cmdline_pos(pos);
11402}
11403
11404/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011405 * "setenv()" function
11406 */
11407 static void
11408f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11409{
11410 char_u namebuf[NUMBUFLEN];
11411 char_u valbuf[NUMBUFLEN];
11412 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11413
11414 if (argvars[1].v_type == VAR_SPECIAL
11415 && argvars[1].vval.v_number == VVAL_NULL)
11416 vim_unsetenv(name);
11417 else
11418 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11419}
11420
11421/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011422 * "setfperm({fname}, {mode})" function
11423 */
11424 static void
11425f_setfperm(typval_T *argvars, typval_T *rettv)
11426{
11427 char_u *fname;
11428 char_u modebuf[NUMBUFLEN];
11429 char_u *mode_str;
11430 int i;
11431 int mask;
11432 int mode = 0;
11433
11434 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011435 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011436 if (fname == NULL)
11437 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011438 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011439 if (mode_str == NULL)
11440 return;
11441 if (STRLEN(mode_str) != 9)
11442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011443 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011444 return;
11445 }
11446
11447 mask = 1;
11448 for (i = 8; i >= 0; --i)
11449 {
11450 if (mode_str[i] != '-')
11451 mode |= mask;
11452 mask = mask << 1;
11453 }
11454 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11455}
11456
11457/*
11458 * "setline()" function
11459 */
11460 static void
11461f_setline(typval_T *argvars, typval_T *rettv)
11462{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011463 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011464
Bram Moolenaarca851592018-06-06 21:04:07 +020011465 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011466}
11467
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468/*
11469 * Used by "setqflist()" and "setloclist()" functions
11470 */
11471 static void
11472set_qf_ll_list(
11473 win_T *wp UNUSED,
11474 typval_T *list_arg UNUSED,
11475 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011476 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011477 typval_T *rettv)
11478{
11479#ifdef FEAT_QUICKFIX
11480 static char *e_invact = N_("E927: Invalid action: '%s'");
11481 char_u *act;
11482 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011483 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011484#endif
11485
11486 rettv->vval.v_number = -1;
11487
11488#ifdef FEAT_QUICKFIX
11489 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011490 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011491 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011492 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011493 else
11494 {
11495 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011496 dict_T *d = NULL;
11497 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011498
11499 if (action_arg->v_type == VAR_STRING)
11500 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011501 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011502 if (act == NULL)
11503 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011504 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11505 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011506 action = *act;
11507 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011508 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011509 }
11510 else if (action_arg->v_type == VAR_UNKNOWN)
11511 action = ' ';
11512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011513 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011514
Bram Moolenaard823fa92016-08-12 16:29:27 +020011515 if (action_arg->v_type != VAR_UNKNOWN
11516 && what_arg->v_type != VAR_UNKNOWN)
11517 {
11518 if (what_arg->v_type == VAR_DICT)
11519 d = what_arg->vval.v_dict;
11520 else
11521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011522 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011523 valid_dict = FALSE;
11524 }
11525 }
11526
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011527 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011528 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011529 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11530 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011531 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011532 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533 }
11534#endif
11535}
11536
11537/*
11538 * "setloclist()" function
11539 */
11540 static void
11541f_setloclist(typval_T *argvars, typval_T *rettv)
11542{
11543 win_T *win;
11544
11545 rettv->vval.v_number = -1;
11546
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011547 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011548 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011549 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011550}
11551
11552/*
11553 * "setmatches()" function
11554 */
11555 static void
11556f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11557{
11558#ifdef FEAT_SEARCH_EXTRA
11559 list_T *l;
11560 listitem_T *li;
11561 dict_T *d;
11562 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011563 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011564
11565 rettv->vval.v_number = -1;
11566 if (argvars[0].v_type != VAR_LIST)
11567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011568 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011569 return;
11570 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011571 if (win == NULL)
11572 return;
11573
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574 if ((l = argvars[0].vval.v_list) != NULL)
11575 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011576 /* To some extent make sure that we are dealing with a list from
11577 * "getmatches()". */
11578 li = l->lv_first;
11579 while (li != NULL)
11580 {
11581 if (li->li_tv.v_type != VAR_DICT
11582 || (d = li->li_tv.vval.v_dict) == NULL)
11583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011584 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585 return;
11586 }
11587 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11588 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11589 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11590 && dict_find(d, (char_u *)"priority", -1) != NULL
11591 && dict_find(d, (char_u *)"id", -1) != NULL))
11592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011593 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011594 return;
11595 }
11596 li = li->li_next;
11597 }
11598
Bram Moolenaaraff74912019-03-30 18:11:49 +010011599 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 li = l->lv_first;
11601 while (li != NULL)
11602 {
11603 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011604 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011605 dictitem_T *di;
11606 char_u *group;
11607 int priority;
11608 int id;
11609 char_u *conceal;
11610
11611 d = li->li_tv.vval.v_dict;
11612 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11613 {
11614 if (s == NULL)
11615 {
11616 s = list_alloc();
11617 if (s == NULL)
11618 return;
11619 }
11620
11621 /* match from matchaddpos() */
11622 for (i = 1; i < 9; i++)
11623 {
11624 sprintf((char *)buf, (char *)"pos%d", i);
11625 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11626 {
11627 if (di->di_tv.v_type != VAR_LIST)
11628 return;
11629
11630 list_append_tv(s, &di->di_tv);
11631 s->lv_refcount++;
11632 }
11633 else
11634 break;
11635 }
11636 }
11637
Bram Moolenaar8f667172018-12-14 15:38:31 +010011638 group = dict_get_string(d, (char_u *)"group", TRUE);
11639 priority = (int)dict_get_number(d, (char_u *)"priority");
11640 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011641 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011642 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011643 : NULL;
11644 if (i == 0)
11645 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011646 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011647 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011648 priority, id, NULL, conceal);
11649 }
11650 else
11651 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011652 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011653 list_unref(s);
11654 s = NULL;
11655 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011656 vim_free(group);
11657 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011658
11659 li = li->li_next;
11660 }
11661 rettv->vval.v_number = 0;
11662 }
11663#endif
11664}
11665
11666/*
11667 * "setpos()" function
11668 */
11669 static void
11670f_setpos(typval_T *argvars, typval_T *rettv)
11671{
11672 pos_T pos;
11673 int fnum;
11674 char_u *name;
11675 colnr_T curswant = -1;
11676
11677 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011678 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011679 if (name != NULL)
11680 {
11681 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11682 {
11683 if (--pos.col < 0)
11684 pos.col = 0;
11685 if (name[0] == '.' && name[1] == NUL)
11686 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011687 /* set cursor; "fnum" is ignored */
11688 curwin->w_cursor = pos;
11689 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011690 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011691 curwin->w_curswant = curswant - 1;
11692 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011693 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011694 check_cursor();
11695 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011696 }
11697 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11698 {
11699 /* set mark */
11700 if (setmark_pos(name[1], &pos, fnum) == OK)
11701 rettv->vval.v_number = 0;
11702 }
11703 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011704 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011705 }
11706 }
11707}
11708
11709/*
11710 * "setqflist()" function
11711 */
11712 static void
11713f_setqflist(typval_T *argvars, typval_T *rettv)
11714{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011715 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011716}
11717
11718/*
11719 * "setreg()" function
11720 */
11721 static void
11722f_setreg(typval_T *argvars, typval_T *rettv)
11723{
11724 int regname;
11725 char_u *strregname;
11726 char_u *stropt;
11727 char_u *strval;
11728 int append;
11729 char_u yank_type;
11730 long block_len;
11731
11732 block_len = -1;
11733 yank_type = MAUTO;
11734 append = FALSE;
11735
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011736 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011737 rettv->vval.v_number = 1; /* FAIL is default */
11738
11739 if (strregname == NULL)
11740 return; /* type error; errmsg already given */
11741 regname = *strregname;
11742 if (regname == 0 || regname == '@')
11743 regname = '"';
11744
11745 if (argvars[2].v_type != VAR_UNKNOWN)
11746 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011747 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011748 if (stropt == NULL)
11749 return; /* type error */
11750 for (; *stropt != NUL; ++stropt)
11751 switch (*stropt)
11752 {
11753 case 'a': case 'A': /* append */
11754 append = TRUE;
11755 break;
11756 case 'v': case 'c': /* character-wise selection */
11757 yank_type = MCHAR;
11758 break;
11759 case 'V': case 'l': /* line-wise selection */
11760 yank_type = MLINE;
11761 break;
11762 case 'b': case Ctrl_V: /* block-wise selection */
11763 yank_type = MBLOCK;
11764 if (VIM_ISDIGIT(stropt[1]))
11765 {
11766 ++stropt;
11767 block_len = getdigits(&stropt) - 1;
11768 --stropt;
11769 }
11770 break;
11771 }
11772 }
11773
11774 if (argvars[1].v_type == VAR_LIST)
11775 {
11776 char_u **lstval;
11777 char_u **allocval;
11778 char_u buf[NUMBUFLEN];
11779 char_u **curval;
11780 char_u **curallocval;
11781 list_T *ll = argvars[1].vval.v_list;
11782 listitem_T *li;
11783 int len;
11784
11785 /* If the list is NULL handle like an empty list. */
11786 len = ll == NULL ? 0 : ll->lv_len;
11787
11788 /* First half: use for pointers to result lines; second half: use for
11789 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011790 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011791 if (lstval == NULL)
11792 return;
11793 curval = lstval;
11794 allocval = lstval + len + 2;
11795 curallocval = allocval;
11796
11797 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11798 li = li->li_next)
11799 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011800 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011801 if (strval == NULL)
11802 goto free_lstval;
11803 if (strval == buf)
11804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011805 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011806 * overwrite the string. */
11807 strval = vim_strsave(buf);
11808 if (strval == NULL)
11809 goto free_lstval;
11810 *curallocval++ = strval;
11811 }
11812 *curval++ = strval;
11813 }
11814 *curval++ = NULL;
11815
11816 write_reg_contents_lst(regname, lstval, -1,
11817 append, yank_type, block_len);
11818free_lstval:
11819 while (curallocval > allocval)
11820 vim_free(*--curallocval);
11821 vim_free(lstval);
11822 }
11823 else
11824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011825 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011826 if (strval == NULL)
11827 return;
11828 write_reg_contents_ex(regname, strval, -1,
11829 append, yank_type, block_len);
11830 }
11831 rettv->vval.v_number = 0;
11832}
11833
11834/*
11835 * "settabvar()" function
11836 */
11837 static void
11838f_settabvar(typval_T *argvars, typval_T *rettv)
11839{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011840 tabpage_T *save_curtab;
11841 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 char_u *varname, *tabvarname;
11843 typval_T *varp;
11844
11845 rettv->vval.v_number = 0;
11846
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011847 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011848 return;
11849
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011850 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11851 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852 varp = &argvars[2];
11853
Bram Moolenaar4033c552017-09-16 20:54:51 +020011854 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011855 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011856 save_curtab = curtab;
11857 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011858
Bram Moolenaar964b3742019-05-24 18:54:09 +020011859 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011860 if (tabvarname != NULL)
11861 {
11862 STRCPY(tabvarname, "t:");
11863 STRCPY(tabvarname + 2, varname);
11864 set_var(tabvarname, varp, TRUE);
11865 vim_free(tabvarname);
11866 }
11867
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011868 /* Restore current tabpage */
11869 if (valid_tabpage(save_curtab))
11870 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011871 }
11872}
11873
11874/*
11875 * "settabwinvar()" function
11876 */
11877 static void
11878f_settabwinvar(typval_T *argvars, typval_T *rettv)
11879{
11880 setwinvar(argvars, rettv, 1);
11881}
11882
11883/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011884 * "settagstack()" function
11885 */
11886 static void
11887f_settagstack(typval_T *argvars, typval_T *rettv)
11888{
11889 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11890 win_T *wp;
11891 dict_T *d;
11892 int action = 'r';
11893
11894 rettv->vval.v_number = -1;
11895
11896 // first argument: window number or id
11897 wp = find_win_by_nr_or_id(&argvars[0]);
11898 if (wp == NULL)
11899 return;
11900
11901 // second argument: dict with items to set in the tag stack
11902 if (argvars[1].v_type != VAR_DICT)
11903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011904 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011905 return;
11906 }
11907 d = argvars[1].vval.v_dict;
11908 if (d == NULL)
11909 return;
11910
11911 // third argument: action - 'a' for append and 'r' for replace.
11912 // default is to replace the stack.
11913 if (argvars[2].v_type == VAR_UNKNOWN)
11914 action = 'r';
11915 else if (argvars[2].v_type == VAR_STRING)
11916 {
11917 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011918 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011919 if (actstr == NULL)
11920 return;
11921 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11922 action = *actstr;
11923 else
11924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011925 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011926 return;
11927 }
11928 }
11929 else
11930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011931 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011932 return;
11933 }
11934
11935 if (set_tagstack(wp, d, action) == OK)
11936 rettv->vval.v_number = 0;
11937}
11938
11939/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011940 * "setwinvar()" function
11941 */
11942 static void
11943f_setwinvar(typval_T *argvars, typval_T *rettv)
11944{
11945 setwinvar(argvars, rettv, 0);
11946}
11947
11948#ifdef FEAT_CRYPT
11949/*
11950 * "sha256({string})" function
11951 */
11952 static void
11953f_sha256(typval_T *argvars, typval_T *rettv)
11954{
11955 char_u *p;
11956
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011957 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011958 rettv->vval.v_string = vim_strsave(
11959 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11960 rettv->v_type = VAR_STRING;
11961}
11962#endif /* FEAT_CRYPT */
11963
11964/*
11965 * "shellescape({string})" function
11966 */
11967 static void
11968f_shellescape(typval_T *argvars, typval_T *rettv)
11969{
Bram Moolenaar20615522017-06-05 18:46:26 +020011970 int do_special = non_zero_arg(&argvars[1]);
11971
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011972 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011973 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011974 rettv->v_type = VAR_STRING;
11975}
11976
11977/*
11978 * shiftwidth() function
11979 */
11980 static void
11981f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11982{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011983 rettv->vval.v_number = 0;
11984
11985 if (argvars[0].v_type != VAR_UNKNOWN)
11986 {
11987 long col;
11988
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011989 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011990 if (col < 0)
11991 return; // type error; errmsg already given
11992#ifdef FEAT_VARTABS
11993 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11994 return;
11995#endif
11996 }
11997
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011998 rettv->vval.v_number = get_sw_value(curbuf);
11999}
12000
Bram Moolenaar162b7142018-12-21 15:17:36 +010012001#ifdef FEAT_SIGNS
12002/*
12003 * "sign_define()" function
12004 */
12005 static void
12006f_sign_define(typval_T *argvars, typval_T *rettv)
12007{
12008 char_u *name;
12009 dict_T *dict;
12010 char_u *icon = NULL;
12011 char_u *linehl = NULL;
12012 char_u *text = NULL;
12013 char_u *texthl = NULL;
12014
12015 rettv->vval.v_number = -1;
12016
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012017 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012018 if (name == NULL)
12019 return;
12020
12021 if (argvars[1].v_type != VAR_UNKNOWN)
12022 {
12023 if (argvars[1].v_type != VAR_DICT)
12024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012025 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012026 return;
12027 }
12028
12029 // sign attributes
12030 dict = argvars[1].vval.v_dict;
12031 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12032 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12033 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12034 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12035 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12036 text = dict_get_string(dict, (char_u *)"text", TRUE);
12037 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12038 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12039 }
12040
12041 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12042 rettv->vval.v_number = 0;
12043
12044 vim_free(icon);
12045 vim_free(linehl);
12046 vim_free(text);
12047 vim_free(texthl);
12048}
12049
12050/*
12051 * "sign_getdefined()" function
12052 */
12053 static void
12054f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12055{
12056 char_u *name = NULL;
12057
12058 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12059 return;
12060
12061 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012062 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012063
12064 sign_getlist(name, rettv->vval.v_list);
12065}
12066
12067/*
12068 * "sign_getplaced()" function
12069 */
12070 static void
12071f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12072{
12073 buf_T *buf = NULL;
12074 dict_T *dict;
12075 dictitem_T *di;
12076 linenr_T lnum = 0;
12077 int sign_id = 0;
12078 char_u *group = NULL;
12079 int notanum = FALSE;
12080
12081 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12082 return;
12083
12084 if (argvars[0].v_type != VAR_UNKNOWN)
12085 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012086 // get signs placed in the specified buffer
12087 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012088 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012090
12091 if (argvars[1].v_type != VAR_UNKNOWN)
12092 {
12093 if (argvars[1].v_type != VAR_DICT ||
12094 ((dict = argvars[1].vval.v_dict) == NULL))
12095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012096 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012097 return;
12098 }
12099 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12100 {
12101 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012102 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012103 if (notanum)
12104 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012105 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012106 }
12107 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12108 {
12109 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012110 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012111 if (notanum)
12112 return;
12113 }
12114 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12115 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012116 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012117 if (group == NULL)
12118 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012119 if (*group == '\0') // empty string means global group
12120 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012121 }
12122 }
12123 }
12124
12125 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12126}
12127
12128/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012129 * "sign_jump()" function
12130 */
12131 static void
12132f_sign_jump(typval_T *argvars, typval_T *rettv)
12133{
12134 int sign_id;
12135 char_u *sign_group = NULL;
12136 buf_T *buf;
12137 int notanum = FALSE;
12138
12139 rettv->vval.v_number = -1;
12140
Bram Moolenaarbdace832019-03-02 10:13:42 +010012141 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012142 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12143 if (notanum)
12144 return;
12145 if (sign_id <= 0)
12146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012147 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012148 return;
12149 }
12150
12151 // Sign group
12152 sign_group = tv_get_string_chk(&argvars[1]);
12153 if (sign_group == NULL)
12154 return;
12155 if (sign_group[0] == '\0')
12156 sign_group = NULL; // global sign group
12157 else
12158 {
12159 sign_group = vim_strsave(sign_group);
12160 if (sign_group == NULL)
12161 return;
12162 }
12163
12164 // Buffer to place the sign
12165 buf = get_buf_arg(&argvars[2]);
12166 if (buf == NULL)
12167 goto cleanup;
12168
12169 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12170
12171cleanup:
12172 vim_free(sign_group);
12173}
12174
12175/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012176 * "sign_place()" function
12177 */
12178 static void
12179f_sign_place(typval_T *argvars, typval_T *rettv)
12180{
12181 int sign_id;
12182 char_u *group = NULL;
12183 char_u *sign_name;
12184 buf_T *buf;
12185 dict_T *dict;
12186 dictitem_T *di;
12187 linenr_T lnum = 0;
12188 int prio = SIGN_DEF_PRIO;
12189 int notanum = FALSE;
12190
12191 rettv->vval.v_number = -1;
12192
Bram Moolenaarbdace832019-03-02 10:13:42 +010012193 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012194 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012195 if (notanum)
12196 return;
12197 if (sign_id < 0)
12198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012199 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012200 return;
12201 }
12202
12203 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012204 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012205 if (group == NULL)
12206 return;
12207 if (group[0] == '\0')
12208 group = NULL; // global sign group
12209 else
12210 {
12211 group = vim_strsave(group);
12212 if (group == NULL)
12213 return;
12214 }
12215
12216 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012217 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012218 if (sign_name == NULL)
12219 goto cleanup;
12220
12221 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012222 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012223 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012225
12226 if (argvars[4].v_type != VAR_UNKNOWN)
12227 {
12228 if (argvars[4].v_type != VAR_DICT ||
12229 ((dict = argvars[4].vval.v_dict) == NULL))
12230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012231 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012232 goto cleanup;
12233 }
12234
12235 // Line number where the sign is to be placed
12236 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12237 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012238 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012239 if (notanum)
12240 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012241 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012242 }
12243 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12244 {
12245 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012246 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012247 if (notanum)
12248 goto cleanup;
12249 }
12250 }
12251
12252 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12253 rettv->vval.v_number = sign_id;
12254
12255cleanup:
12256 vim_free(group);
12257}
12258
12259/*
12260 * "sign_undefine()" function
12261 */
12262 static void
12263f_sign_undefine(typval_T *argvars, typval_T *rettv)
12264{
12265 char_u *name;
12266
12267 rettv->vval.v_number = -1;
12268
12269 if (argvars[0].v_type == VAR_UNKNOWN)
12270 {
12271 // Free all the signs
12272 free_signs();
12273 rettv->vval.v_number = 0;
12274 }
12275 else
12276 {
12277 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012278 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012279 if (name == NULL)
12280 return;
12281
12282 if (sign_undefine_by_name(name) == OK)
12283 rettv->vval.v_number = 0;
12284 }
12285}
12286
12287/*
12288 * "sign_unplace()" function
12289 */
12290 static void
12291f_sign_unplace(typval_T *argvars, typval_T *rettv)
12292{
12293 dict_T *dict;
12294 dictitem_T *di;
12295 int sign_id = 0;
12296 buf_T *buf = NULL;
12297 char_u *group = NULL;
12298
12299 rettv->vval.v_number = -1;
12300
12301 if (argvars[0].v_type != VAR_STRING)
12302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012303 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012304 return;
12305 }
12306
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012307 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012308 if (group[0] == '\0')
12309 group = NULL; // global sign group
12310 else
12311 {
12312 group = vim_strsave(group);
12313 if (group == NULL)
12314 return;
12315 }
12316
12317 if (argvars[1].v_type != VAR_UNKNOWN)
12318 {
12319 if (argvars[1].v_type != VAR_DICT)
12320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012321 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012322 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012323 }
12324 dict = argvars[1].vval.v_dict;
12325
12326 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12327 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012328 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012329 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012330 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012331 }
12332 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12333 sign_id = dict_get_number(dict, (char_u *)"id");
12334 }
12335
12336 if (buf == NULL)
12337 {
12338 // Delete the sign in all the buffers
12339 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012340 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012341 rettv->vval.v_number = 0;
12342 }
12343 else
12344 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012345 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012346 rettv->vval.v_number = 0;
12347 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012348
12349cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012350 vim_free(group);
12351}
12352#endif
12353
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012354/*
12355 * "simplify()" function
12356 */
12357 static void
12358f_simplify(typval_T *argvars, typval_T *rettv)
12359{
12360 char_u *p;
12361
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012362 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012363 rettv->vval.v_string = vim_strsave(p);
12364 simplify_filename(rettv->vval.v_string); /* simplify in place */
12365 rettv->v_type = VAR_STRING;
12366}
12367
12368#ifdef FEAT_FLOAT
12369/*
12370 * "sin()" function
12371 */
12372 static void
12373f_sin(typval_T *argvars, typval_T *rettv)
12374{
12375 float_T f = 0.0;
12376
12377 rettv->v_type = VAR_FLOAT;
12378 if (get_float_arg(argvars, &f) == OK)
12379 rettv->vval.v_float = sin(f);
12380 else
12381 rettv->vval.v_float = 0.0;
12382}
12383
12384/*
12385 * "sinh()" function
12386 */
12387 static void
12388f_sinh(typval_T *argvars, typval_T *rettv)
12389{
12390 float_T f = 0.0;
12391
12392 rettv->v_type = VAR_FLOAT;
12393 if (get_float_arg(argvars, &f) == OK)
12394 rettv->vval.v_float = sinh(f);
12395 else
12396 rettv->vval.v_float = 0.0;
12397}
12398#endif
12399
Bram Moolenaareae1b912019-05-09 15:12:55 +020012400static int item_compare(const void *s1, const void *s2);
12401static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012402
12403/* struct used in the array that's given to qsort() */
12404typedef struct
12405{
12406 listitem_T *item;
12407 int idx;
12408} sortItem_T;
12409
12410/* struct storing information about current sort */
12411typedef struct
12412{
12413 int item_compare_ic;
12414 int item_compare_numeric;
12415 int item_compare_numbers;
12416#ifdef FEAT_FLOAT
12417 int item_compare_float;
12418#endif
12419 char_u *item_compare_func;
12420 partial_T *item_compare_partial;
12421 dict_T *item_compare_selfdict;
12422 int item_compare_func_err;
12423 int item_compare_keep_zero;
12424} sortinfo_T;
12425static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426#define ITEM_COMPARE_FAIL 999
12427
12428/*
12429 * Compare functions for f_sort() and f_uniq() below.
12430 */
12431 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012432item_compare(const void *s1, const void *s2)
12433{
12434 sortItem_T *si1, *si2;
12435 typval_T *tv1, *tv2;
12436 char_u *p1, *p2;
12437 char_u *tofree1 = NULL, *tofree2 = NULL;
12438 int res;
12439 char_u numbuf1[NUMBUFLEN];
12440 char_u numbuf2[NUMBUFLEN];
12441
12442 si1 = (sortItem_T *)s1;
12443 si2 = (sortItem_T *)s2;
12444 tv1 = &si1->item->li_tv;
12445 tv2 = &si2->item->li_tv;
12446
12447 if (sortinfo->item_compare_numbers)
12448 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012449 varnumber_T v1 = tv_get_number(tv1);
12450 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012451
12452 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12453 }
12454
12455#ifdef FEAT_FLOAT
12456 if (sortinfo->item_compare_float)
12457 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012458 float_T v1 = tv_get_float(tv1);
12459 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012460
12461 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12462 }
12463#endif
12464
12465 /* tv2string() puts quotes around a string and allocates memory. Don't do
12466 * that for string variables. Use a single quote when comparing with a
12467 * non-string to do what the docs promise. */
12468 if (tv1->v_type == VAR_STRING)
12469 {
12470 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12471 p1 = (char_u *)"'";
12472 else
12473 p1 = tv1->vval.v_string;
12474 }
12475 else
12476 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12477 if (tv2->v_type == VAR_STRING)
12478 {
12479 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12480 p2 = (char_u *)"'";
12481 else
12482 p2 = tv2->vval.v_string;
12483 }
12484 else
12485 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12486 if (p1 == NULL)
12487 p1 = (char_u *)"";
12488 if (p2 == NULL)
12489 p2 = (char_u *)"";
12490 if (!sortinfo->item_compare_numeric)
12491 {
12492 if (sortinfo->item_compare_ic)
12493 res = STRICMP(p1, p2);
12494 else
12495 res = STRCMP(p1, p2);
12496 }
12497 else
12498 {
12499 double n1, n2;
12500 n1 = strtod((char *)p1, (char **)&p1);
12501 n2 = strtod((char *)p2, (char **)&p2);
12502 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12503 }
12504
12505 /* When the result would be zero, compare the item indexes. Makes the
12506 * sort stable. */
12507 if (res == 0 && !sortinfo->item_compare_keep_zero)
12508 res = si1->idx > si2->idx ? 1 : -1;
12509
12510 vim_free(tofree1);
12511 vim_free(tofree2);
12512 return res;
12513}
12514
12515 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012516item_compare2(const void *s1, const void *s2)
12517{
12518 sortItem_T *si1, *si2;
12519 int res;
12520 typval_T rettv;
12521 typval_T argv[3];
12522 int dummy;
12523 char_u *func_name;
12524 partial_T *partial = sortinfo->item_compare_partial;
12525
12526 /* shortcut after failure in previous call; compare all items equal */
12527 if (sortinfo->item_compare_func_err)
12528 return 0;
12529
12530 si1 = (sortItem_T *)s1;
12531 si2 = (sortItem_T *)s2;
12532
12533 if (partial == NULL)
12534 func_name = sortinfo->item_compare_func;
12535 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012536 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012537
12538 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12539 * in the copy without changing the original list items. */
12540 copy_tv(&si1->item->li_tv, &argv[0]);
12541 copy_tv(&si2->item->li_tv, &argv[1]);
12542
12543 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012544 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012545 partial, sortinfo->item_compare_selfdict);
12546 clear_tv(&argv[0]);
12547 clear_tv(&argv[1]);
12548
12549 if (res == FAIL)
12550 res = ITEM_COMPARE_FAIL;
12551 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012552 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012553 if (sortinfo->item_compare_func_err)
12554 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12555 clear_tv(&rettv);
12556
12557 /* When the result would be zero, compare the pointers themselves. Makes
12558 * the sort stable. */
12559 if (res == 0 && !sortinfo->item_compare_keep_zero)
12560 res = si1->idx > si2->idx ? 1 : -1;
12561
12562 return res;
12563}
12564
12565/*
12566 * "sort({list})" function
12567 */
12568 static void
12569do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12570{
12571 list_T *l;
12572 listitem_T *li;
12573 sortItem_T *ptrs;
12574 sortinfo_T *old_sortinfo;
12575 sortinfo_T info;
12576 long len;
12577 long i;
12578
12579 /* Pointer to current info struct used in compare function. Save and
12580 * restore the current one for nested calls. */
12581 old_sortinfo = sortinfo;
12582 sortinfo = &info;
12583
12584 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012585 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012586 else
12587 {
12588 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012589 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012590 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12591 TRUE))
12592 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012593 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012594
12595 len = list_len(l);
12596 if (len <= 1)
12597 goto theend; /* short list sorts pretty quickly */
12598
12599 info.item_compare_ic = FALSE;
12600 info.item_compare_numeric = FALSE;
12601 info.item_compare_numbers = FALSE;
12602#ifdef FEAT_FLOAT
12603 info.item_compare_float = FALSE;
12604#endif
12605 info.item_compare_func = NULL;
12606 info.item_compare_partial = NULL;
12607 info.item_compare_selfdict = NULL;
12608 if (argvars[1].v_type != VAR_UNKNOWN)
12609 {
12610 /* optional second argument: {func} */
12611 if (argvars[1].v_type == VAR_FUNC)
12612 info.item_compare_func = argvars[1].vval.v_string;
12613 else if (argvars[1].v_type == VAR_PARTIAL)
12614 info.item_compare_partial = argvars[1].vval.v_partial;
12615 else
12616 {
12617 int error = FALSE;
12618
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 if (error)
12621 goto theend; /* type error; errmsg already given */
12622 if (i == 1)
12623 info.item_compare_ic = TRUE;
12624 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012625 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012626 else if (i != 0)
12627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012628 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012629 goto theend;
12630 }
12631 if (info.item_compare_func != NULL)
12632 {
12633 if (*info.item_compare_func == NUL)
12634 {
12635 /* empty string means default sort */
12636 info.item_compare_func = NULL;
12637 }
12638 else if (STRCMP(info.item_compare_func, "n") == 0)
12639 {
12640 info.item_compare_func = NULL;
12641 info.item_compare_numeric = TRUE;
12642 }
12643 else if (STRCMP(info.item_compare_func, "N") == 0)
12644 {
12645 info.item_compare_func = NULL;
12646 info.item_compare_numbers = TRUE;
12647 }
12648#ifdef FEAT_FLOAT
12649 else if (STRCMP(info.item_compare_func, "f") == 0)
12650 {
12651 info.item_compare_func = NULL;
12652 info.item_compare_float = TRUE;
12653 }
12654#endif
12655 else if (STRCMP(info.item_compare_func, "i") == 0)
12656 {
12657 info.item_compare_func = NULL;
12658 info.item_compare_ic = TRUE;
12659 }
12660 }
12661 }
12662
12663 if (argvars[2].v_type != VAR_UNKNOWN)
12664 {
12665 /* optional third argument: {dict} */
12666 if (argvars[2].v_type != VAR_DICT)
12667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012668 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012669 goto theend;
12670 }
12671 info.item_compare_selfdict = argvars[2].vval.v_dict;
12672 }
12673 }
12674
12675 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012676 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012677 if (ptrs == NULL)
12678 goto theend;
12679
12680 i = 0;
12681 if (sort)
12682 {
12683 /* sort(): ptrs will be the list to sort */
12684 for (li = l->lv_first; li != NULL; li = li->li_next)
12685 {
12686 ptrs[i].item = li;
12687 ptrs[i].idx = i;
12688 ++i;
12689 }
12690
12691 info.item_compare_func_err = FALSE;
12692 info.item_compare_keep_zero = FALSE;
12693 /* test the compare function */
12694 if ((info.item_compare_func != NULL
12695 || info.item_compare_partial != NULL)
12696 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12697 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012698 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012699 else
12700 {
12701 /* Sort the array with item pointers. */
12702 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12703 info.item_compare_func == NULL
12704 && info.item_compare_partial == NULL
12705 ? item_compare : item_compare2);
12706
12707 if (!info.item_compare_func_err)
12708 {
12709 /* Clear the List and append the items in sorted order. */
12710 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12711 l->lv_len = 0;
12712 for (i = 0; i < len; ++i)
12713 list_append(l, ptrs[i].item);
12714 }
12715 }
12716 }
12717 else
12718 {
12719 int (*item_compare_func_ptr)(const void *, const void *);
12720
12721 /* f_uniq(): ptrs will be a stack of items to remove */
12722 info.item_compare_func_err = FALSE;
12723 info.item_compare_keep_zero = TRUE;
12724 item_compare_func_ptr = info.item_compare_func != NULL
12725 || info.item_compare_partial != NULL
12726 ? item_compare2 : item_compare;
12727
12728 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12729 li = li->li_next)
12730 {
12731 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12732 == 0)
12733 ptrs[i++].item = li;
12734 if (info.item_compare_func_err)
12735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012736 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012737 break;
12738 }
12739 }
12740
12741 if (!info.item_compare_func_err)
12742 {
12743 while (--i >= 0)
12744 {
12745 li = ptrs[i].item->li_next;
12746 ptrs[i].item->li_next = li->li_next;
12747 if (li->li_next != NULL)
12748 li->li_next->li_prev = ptrs[i].item;
12749 else
12750 l->lv_last = ptrs[i].item;
12751 list_fix_watch(l, li);
12752 listitem_free(li);
12753 l->lv_len--;
12754 }
12755 }
12756 }
12757
12758 vim_free(ptrs);
12759 }
12760theend:
12761 sortinfo = old_sortinfo;
12762}
12763
12764/*
12765 * "sort({list})" function
12766 */
12767 static void
12768f_sort(typval_T *argvars, typval_T *rettv)
12769{
12770 do_sort_uniq(argvars, rettv, TRUE);
12771}
12772
12773/*
12774 * "uniq({list})" function
12775 */
12776 static void
12777f_uniq(typval_T *argvars, typval_T *rettv)
12778{
12779 do_sort_uniq(argvars, rettv, FALSE);
12780}
12781
12782/*
12783 * "soundfold({word})" function
12784 */
12785 static void
12786f_soundfold(typval_T *argvars, typval_T *rettv)
12787{
12788 char_u *s;
12789
12790 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012791 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012792#ifdef FEAT_SPELL
12793 rettv->vval.v_string = eval_soundfold(s);
12794#else
12795 rettv->vval.v_string = vim_strsave(s);
12796#endif
12797}
12798
12799/*
12800 * "spellbadword()" function
12801 */
12802 static void
12803f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12804{
12805 char_u *word = (char_u *)"";
12806 hlf_T attr = HLF_COUNT;
12807 int len = 0;
12808
12809 if (rettv_list_alloc(rettv) == FAIL)
12810 return;
12811
12812#ifdef FEAT_SPELL
12813 if (argvars[0].v_type == VAR_UNKNOWN)
12814 {
12815 /* Find the start and length of the badly spelled word. */
12816 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12817 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012818 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012819 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012820 curwin->w_set_curswant = TRUE;
12821 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012822 }
12823 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12824 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012825 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012826 int capcol = -1;
12827
12828 if (str != NULL)
12829 {
12830 /* Check the argument for spelling. */
12831 while (*str != NUL)
12832 {
12833 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12834 if (attr != HLF_COUNT)
12835 {
12836 word = str;
12837 break;
12838 }
12839 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012840 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012841 }
12842 }
12843 }
12844#endif
12845
12846 list_append_string(rettv->vval.v_list, word, len);
12847 list_append_string(rettv->vval.v_list, (char_u *)(
12848 attr == HLF_SPB ? "bad" :
12849 attr == HLF_SPR ? "rare" :
12850 attr == HLF_SPL ? "local" :
12851 attr == HLF_SPC ? "caps" :
12852 ""), -1);
12853}
12854
12855/*
12856 * "spellsuggest()" function
12857 */
12858 static void
12859f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12860{
12861#ifdef FEAT_SPELL
12862 char_u *str;
12863 int typeerr = FALSE;
12864 int maxcount;
12865 garray_T ga;
12866 int i;
12867 listitem_T *li;
12868 int need_capital = FALSE;
12869#endif
12870
12871 if (rettv_list_alloc(rettv) == FAIL)
12872 return;
12873
12874#ifdef FEAT_SPELL
12875 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12876 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012877 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012878 if (argvars[1].v_type != VAR_UNKNOWN)
12879 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012880 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012881 if (maxcount <= 0)
12882 return;
12883 if (argvars[2].v_type != VAR_UNKNOWN)
12884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012885 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012886 if (typeerr)
12887 return;
12888 }
12889 }
12890 else
12891 maxcount = 25;
12892
12893 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12894
12895 for (i = 0; i < ga.ga_len; ++i)
12896 {
12897 str = ((char_u **)ga.ga_data)[i];
12898
12899 li = listitem_alloc();
12900 if (li == NULL)
12901 vim_free(str);
12902 else
12903 {
12904 li->li_tv.v_type = VAR_STRING;
12905 li->li_tv.v_lock = 0;
12906 li->li_tv.vval.v_string = str;
12907 list_append(rettv->vval.v_list, li);
12908 }
12909 }
12910 ga_clear(&ga);
12911 }
12912#endif
12913}
12914
12915 static void
12916f_split(typval_T *argvars, typval_T *rettv)
12917{
12918 char_u *str;
12919 char_u *end;
12920 char_u *pat = NULL;
12921 regmatch_T regmatch;
12922 char_u patbuf[NUMBUFLEN];
12923 char_u *save_cpo;
12924 int match;
12925 colnr_T col = 0;
12926 int keepempty = FALSE;
12927 int typeerr = FALSE;
12928
12929 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12930 save_cpo = p_cpo;
12931 p_cpo = (char_u *)"";
12932
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012933 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012934 if (argvars[1].v_type != VAR_UNKNOWN)
12935 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012936 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012937 if (pat == NULL)
12938 typeerr = TRUE;
12939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012940 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012941 }
12942 if (pat == NULL || *pat == NUL)
12943 pat = (char_u *)"[\\x01- ]\\+";
12944
12945 if (rettv_list_alloc(rettv) == FAIL)
12946 return;
12947 if (typeerr)
12948 return;
12949
12950 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12951 if (regmatch.regprog != NULL)
12952 {
12953 regmatch.rm_ic = FALSE;
12954 while (*str != NUL || keepempty)
12955 {
12956 if (*str == NUL)
12957 match = FALSE; /* empty item at the end */
12958 else
12959 match = vim_regexec_nl(&regmatch, str, col);
12960 if (match)
12961 end = regmatch.startp[0];
12962 else
12963 end = str + STRLEN(str);
12964 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12965 && *str != NUL && match && end < regmatch.endp[0]))
12966 {
12967 if (list_append_string(rettv->vval.v_list, str,
12968 (int)(end - str)) == FAIL)
12969 break;
12970 }
12971 if (!match)
12972 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012973 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012974 if (regmatch.endp[0] > str)
12975 col = 0;
12976 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012977 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012979 str = regmatch.endp[0];
12980 }
12981
12982 vim_regfree(regmatch.regprog);
12983 }
12984
12985 p_cpo = save_cpo;
12986}
12987
12988#ifdef FEAT_FLOAT
12989/*
12990 * "sqrt()" function
12991 */
12992 static void
12993f_sqrt(typval_T *argvars, typval_T *rettv)
12994{
12995 float_T f = 0.0;
12996
12997 rettv->v_type = VAR_FLOAT;
12998 if (get_float_arg(argvars, &f) == OK)
12999 rettv->vval.v_float = sqrt(f);
13000 else
13001 rettv->vval.v_float = 0.0;
13002}
13003
13004/*
13005 * "str2float()" function
13006 */
13007 static void
13008f_str2float(typval_T *argvars, typval_T *rettv)
13009{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013010 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013011 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012
Bram Moolenaar08243d22017-01-10 16:12:29 +010013013 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 p = skipwhite(p + 1);
13015 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013016 if (isneg)
13017 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013018 rettv->v_type = VAR_FLOAT;
13019}
13020#endif
13021
13022/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013023 * "str2list()" function
13024 */
13025 static void
13026f_str2list(typval_T *argvars, typval_T *rettv)
13027{
13028 char_u *p;
13029 int utf8 = FALSE;
13030
13031 if (rettv_list_alloc(rettv) == FAIL)
13032 return;
13033
13034 if (argvars[1].v_type != VAR_UNKNOWN)
13035 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13036
13037 p = tv_get_string(&argvars[0]);
13038
13039 if (has_mbyte || utf8)
13040 {
13041 int (*ptr2len)(char_u *);
13042 int (*ptr2char)(char_u *);
13043
13044 if (utf8 || enc_utf8)
13045 {
13046 ptr2len = utf_ptr2len;
13047 ptr2char = utf_ptr2char;
13048 }
13049 else
13050 {
13051 ptr2len = mb_ptr2len;
13052 ptr2char = mb_ptr2char;
13053 }
13054
13055 for ( ; *p != NUL; p += (*ptr2len)(p))
13056 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13057 }
13058 else
13059 for ( ; *p != NUL; ++p)
13060 list_append_number(rettv->vval.v_list, *p);
13061}
13062
13063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013064 * "str2nr()" function
13065 */
13066 static void
13067f_str2nr(typval_T *argvars, typval_T *rettv)
13068{
13069 int base = 10;
13070 char_u *p;
13071 varnumber_T n;
13072 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013073 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013074
13075 if (argvars[1].v_type != VAR_UNKNOWN)
13076 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013077 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013078 if (base != 2 && base != 8 && base != 10 && base != 16)
13079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013080 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013081 return;
13082 }
13083 }
13084
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013085 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013086 isneg = (*p == '-');
13087 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 p = skipwhite(p + 1);
13089 switch (base)
13090 {
13091 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13092 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13093 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13094 default: what = 0;
13095 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013096 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13097 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013098 if (isneg)
13099 rettv->vval.v_number = -n;
13100 else
13101 rettv->vval.v_number = n;
13102
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013103}
13104
13105#ifdef HAVE_STRFTIME
13106/*
13107 * "strftime({format}[, {time}])" function
13108 */
13109 static void
13110f_strftime(typval_T *argvars, typval_T *rettv)
13111{
13112 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013113# ifdef HAVE_LOCALTIME_R
13114 struct tm tmval;
13115# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 struct tm *curtime;
13117 time_t seconds;
13118 char_u *p;
13119
13120 rettv->v_type = VAR_STRING;
13121
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013122 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 if (argvars[1].v_type == VAR_UNKNOWN)
13124 seconds = time(NULL);
13125 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013126 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013127# ifdef HAVE_LOCALTIME_R
13128 curtime = localtime_r(&seconds, &tmval);
13129# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013130 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013131# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013132 /* MSVC returns NULL for an invalid value of seconds. */
13133 if (curtime == NULL)
13134 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13135 else
13136 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013137 vimconv_T conv;
13138 char_u *enc;
13139
13140 conv.vc_type = CONV_NONE;
13141 enc = enc_locale();
13142 convert_setup(&conv, p_enc, enc);
13143 if (conv.vc_type != CONV_NONE)
13144 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 if (p != NULL)
13146 (void)strftime((char *)result_buf, sizeof(result_buf),
13147 (char *)p, curtime);
13148 else
13149 result_buf[0] = NUL;
13150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 if (conv.vc_type != CONV_NONE)
13152 vim_free(p);
13153 convert_setup(&conv, enc, p_enc);
13154 if (conv.vc_type != CONV_NONE)
13155 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13156 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013157 rettv->vval.v_string = vim_strsave(result_buf);
13158
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013159 /* Release conversion descriptors */
13160 convert_setup(&conv, NULL, NULL);
13161 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013162 }
13163}
13164#endif
13165
13166/*
13167 * "strgetchar()" function
13168 */
13169 static void
13170f_strgetchar(typval_T *argvars, typval_T *rettv)
13171{
13172 char_u *str;
13173 int len;
13174 int error = FALSE;
13175 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013176 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013177
13178 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013179 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (str == NULL)
13181 return;
13182 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013183 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 if (error)
13185 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186
Bram Moolenaar13505972019-01-24 15:04:48 +010013187 while (charidx >= 0 && byteidx < len)
13188 {
13189 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013191 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13192 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013193 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013194 --charidx;
13195 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197}
13198
13199/*
13200 * "stridx()" function
13201 */
13202 static void
13203f_stridx(typval_T *argvars, typval_T *rettv)
13204{
13205 char_u buf[NUMBUFLEN];
13206 char_u *needle;
13207 char_u *haystack;
13208 char_u *save_haystack;
13209 char_u *pos;
13210 int start_idx;
13211
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013212 needle = tv_get_string_chk(&argvars[1]);
13213 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 rettv->vval.v_number = -1;
13215 if (needle == NULL || haystack == NULL)
13216 return; /* type error; errmsg already given */
13217
13218 if (argvars[2].v_type != VAR_UNKNOWN)
13219 {
13220 int error = FALSE;
13221
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013222 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013223 if (error || start_idx >= (int)STRLEN(haystack))
13224 return;
13225 if (start_idx >= 0)
13226 haystack += start_idx;
13227 }
13228
13229 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13230 if (pos != NULL)
13231 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13232}
13233
13234/*
13235 * "string()" function
13236 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013237 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238f_string(typval_T *argvars, typval_T *rettv)
13239{
13240 char_u *tofree;
13241 char_u numbuf[NUMBUFLEN];
13242
13243 rettv->v_type = VAR_STRING;
13244 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13245 get_copyID());
13246 /* Make a copy if we have a value but it's not in allocated memory. */
13247 if (rettv->vval.v_string != NULL && tofree == NULL)
13248 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13249}
13250
13251/*
13252 * "strlen()" function
13253 */
13254 static void
13255f_strlen(typval_T *argvars, typval_T *rettv)
13256{
13257 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013258 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013259}
13260
13261/*
13262 * "strchars()" function
13263 */
13264 static void
13265f_strchars(typval_T *argvars, typval_T *rettv)
13266{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013267 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013269 varnumber_T len = 0;
13270 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013271
13272 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013273 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013274 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013275 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013276 else
13277 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013278 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13279 while (*s != NUL)
13280 {
13281 func_mb_ptr2char_adv(&s);
13282 ++len;
13283 }
13284 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013285 }
13286}
13287
13288/*
13289 * "strdisplaywidth()" function
13290 */
13291 static void
13292f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13293{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013294 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295 int col = 0;
13296
13297 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013298 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013299
13300 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13301}
13302
13303/*
13304 * "strwidth()" function
13305 */
13306 static void
13307f_strwidth(typval_T *argvars, typval_T *rettv)
13308{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013309 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013310
Bram Moolenaar13505972019-01-24 15:04:48 +010013311 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013312}
13313
13314/*
13315 * "strcharpart()" function
13316 */
13317 static void
13318f_strcharpart(typval_T *argvars, typval_T *rettv)
13319{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320 char_u *p;
13321 int nchar;
13322 int nbyte = 0;
13323 int charlen;
13324 int len = 0;
13325 int slen;
13326 int error = FALSE;
13327
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013328 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013329 slen = (int)STRLEN(p);
13330
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013331 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 if (!error)
13333 {
13334 if (nchar > 0)
13335 while (nchar > 0 && nbyte < slen)
13336 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013337 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013338 --nchar;
13339 }
13340 else
13341 nbyte = nchar;
13342 if (argvars[2].v_type != VAR_UNKNOWN)
13343 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345 while (charlen > 0 && nbyte + len < slen)
13346 {
13347 int off = nbyte + len;
13348
13349 if (off < 0)
13350 len += 1;
13351 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013352 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 --charlen;
13354 }
13355 }
13356 else
13357 len = slen - nbyte; /* default: all bytes that are available. */
13358 }
13359
13360 /*
13361 * Only return the overlap between the specified part and the actual
13362 * string.
13363 */
13364 if (nbyte < 0)
13365 {
13366 len += nbyte;
13367 nbyte = 0;
13368 }
13369 else if (nbyte > slen)
13370 nbyte = slen;
13371 if (len < 0)
13372 len = 0;
13373 else if (nbyte + len > slen)
13374 len = slen - nbyte;
13375
13376 rettv->v_type = VAR_STRING;
13377 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013378}
13379
13380/*
13381 * "strpart()" function
13382 */
13383 static void
13384f_strpart(typval_T *argvars, typval_T *rettv)
13385{
13386 char_u *p;
13387 int n;
13388 int len;
13389 int slen;
13390 int error = FALSE;
13391
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013392 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013393 slen = (int)STRLEN(p);
13394
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013395 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013396 if (error)
13397 len = 0;
13398 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013399 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013400 else
13401 len = slen - n; /* default len: all bytes that are available. */
13402
13403 /*
13404 * Only return the overlap between the specified part and the actual
13405 * string.
13406 */
13407 if (n < 0)
13408 {
13409 len += n;
13410 n = 0;
13411 }
13412 else if (n > slen)
13413 n = slen;
13414 if (len < 0)
13415 len = 0;
13416 else if (n + len > slen)
13417 len = slen - n;
13418
13419 rettv->v_type = VAR_STRING;
13420 rettv->vval.v_string = vim_strnsave(p + n, len);
13421}
13422
13423/*
13424 * "strridx()" function
13425 */
13426 static void
13427f_strridx(typval_T *argvars, typval_T *rettv)
13428{
13429 char_u buf[NUMBUFLEN];
13430 char_u *needle;
13431 char_u *haystack;
13432 char_u *rest;
13433 char_u *lastmatch = NULL;
13434 int haystack_len, end_idx;
13435
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013436 needle = tv_get_string_chk(&argvars[1]);
13437 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013438
13439 rettv->vval.v_number = -1;
13440 if (needle == NULL || haystack == NULL)
13441 return; /* type error; errmsg already given */
13442
13443 haystack_len = (int)STRLEN(haystack);
13444 if (argvars[2].v_type != VAR_UNKNOWN)
13445 {
13446 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013447 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013448 if (end_idx < 0)
13449 return; /* can never find a match */
13450 }
13451 else
13452 end_idx = haystack_len;
13453
13454 if (*needle == NUL)
13455 {
13456 /* Empty string matches past the end. */
13457 lastmatch = haystack + end_idx;
13458 }
13459 else
13460 {
13461 for (rest = haystack; *rest != '\0'; ++rest)
13462 {
13463 rest = (char_u *)strstr((char *)rest, (char *)needle);
13464 if (rest == NULL || rest > haystack + end_idx)
13465 break;
13466 lastmatch = rest;
13467 }
13468 }
13469
13470 if (lastmatch == NULL)
13471 rettv->vval.v_number = -1;
13472 else
13473 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13474}
13475
13476/*
13477 * "strtrans()" function
13478 */
13479 static void
13480f_strtrans(typval_T *argvars, typval_T *rettv)
13481{
13482 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013483 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013484}
13485
13486/*
13487 * "submatch()" function
13488 */
13489 static void
13490f_submatch(typval_T *argvars, typval_T *rettv)
13491{
13492 int error = FALSE;
13493 int no;
13494 int retList = 0;
13495
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013496 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013497 if (error)
13498 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013499 if (no < 0 || no >= NSUBEXP)
13500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013501 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013502 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013504 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013505 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013506 if (error)
13507 return;
13508
13509 if (retList == 0)
13510 {
13511 rettv->v_type = VAR_STRING;
13512 rettv->vval.v_string = reg_submatch(no);
13513 }
13514 else
13515 {
13516 rettv->v_type = VAR_LIST;
13517 rettv->vval.v_list = reg_submatch_list(no);
13518 }
13519}
13520
13521/*
13522 * "substitute()" function
13523 */
13524 static void
13525f_substitute(typval_T *argvars, typval_T *rettv)
13526{
13527 char_u patbuf[NUMBUFLEN];
13528 char_u subbuf[NUMBUFLEN];
13529 char_u flagsbuf[NUMBUFLEN];
13530
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013531 char_u *str = tv_get_string_chk(&argvars[0]);
13532 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013533 char_u *sub = NULL;
13534 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013535 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013537 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13538 expr = &argvars[2];
13539 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013540 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013541
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013543 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13544 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013545 rettv->vval.v_string = NULL;
13546 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013547 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013548}
13549
13550/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013551 * "swapinfo(swap_filename)" function
13552 */
13553 static void
13554f_swapinfo(typval_T *argvars, typval_T *rettv)
13555{
13556 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013557 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013558}
13559
13560/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013561 * "swapname(expr)" function
13562 */
13563 static void
13564f_swapname(typval_T *argvars, typval_T *rettv)
13565{
13566 buf_T *buf;
13567
13568 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013569 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013570 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13571 || buf->b_ml.ml_mfp->mf_fname == NULL)
13572 rettv->vval.v_string = NULL;
13573 else
13574 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13575}
13576
13577/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013578 * "synID(lnum, col, trans)" function
13579 */
13580 static void
13581f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13582{
13583 int id = 0;
13584#ifdef FEAT_SYN_HL
13585 linenr_T lnum;
13586 colnr_T col;
13587 int trans;
13588 int transerr = FALSE;
13589
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013590 lnum = tv_get_lnum(argvars); /* -1 on type error */
13591 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13592 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013593
13594 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13595 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13596 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13597#endif
13598
13599 rettv->vval.v_number = id;
13600}
13601
13602/*
13603 * "synIDattr(id, what [, mode])" function
13604 */
13605 static void
13606f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13607{
13608 char_u *p = NULL;
13609#ifdef FEAT_SYN_HL
13610 int id;
13611 char_u *what;
13612 char_u *mode;
13613 char_u modebuf[NUMBUFLEN];
13614 int modec;
13615
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013616 id = (int)tv_get_number(&argvars[0]);
13617 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013618 if (argvars[2].v_type != VAR_UNKNOWN)
13619 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013620 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013621 modec = TOLOWER_ASC(mode[0]);
13622 if (modec != 't' && modec != 'c' && modec != 'g')
13623 modec = 0; /* replace invalid with current */
13624 }
13625 else
13626 {
13627#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13628 if (USE_24BIT)
13629 modec = 'g';
13630 else
13631#endif
13632 if (t_colors > 1)
13633 modec = 'c';
13634 else
13635 modec = 't';
13636 }
13637
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013638 switch (TOLOWER_ASC(what[0]))
13639 {
13640 case 'b':
13641 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13642 p = highlight_color(id, what, modec);
13643 else /* bold */
13644 p = highlight_has_attr(id, HL_BOLD, modec);
13645 break;
13646
13647 case 'f': /* fg[#] or font */
13648 p = highlight_color(id, what, modec);
13649 break;
13650
13651 case 'i':
13652 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13653 p = highlight_has_attr(id, HL_INVERSE, modec);
13654 else /* italic */
13655 p = highlight_has_attr(id, HL_ITALIC, modec);
13656 break;
13657
13658 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013659 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013660 break;
13661
13662 case 'r': /* reverse */
13663 p = highlight_has_attr(id, HL_INVERSE, modec);
13664 break;
13665
13666 case 's':
13667 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13668 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013669 /* strikeout */
13670 else if (TOLOWER_ASC(what[1]) == 't' &&
13671 TOLOWER_ASC(what[2]) == 'r')
13672 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013673 else /* standout */
13674 p = highlight_has_attr(id, HL_STANDOUT, modec);
13675 break;
13676
13677 case 'u':
13678 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13679 /* underline */
13680 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13681 else
13682 /* undercurl */
13683 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13684 break;
13685 }
13686
13687 if (p != NULL)
13688 p = vim_strsave(p);
13689#endif
13690 rettv->v_type = VAR_STRING;
13691 rettv->vval.v_string = p;
13692}
13693
13694/*
13695 * "synIDtrans(id)" function
13696 */
13697 static void
13698f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13699{
13700 int id;
13701
13702#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013703 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013704
13705 if (id > 0)
13706 id = syn_get_final_id(id);
13707 else
13708#endif
13709 id = 0;
13710
13711 rettv->vval.v_number = id;
13712}
13713
13714/*
13715 * "synconcealed(lnum, col)" function
13716 */
13717 static void
13718f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13719{
13720#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13721 linenr_T lnum;
13722 colnr_T col;
13723 int syntax_flags = 0;
13724 int cchar;
13725 int matchid = 0;
13726 char_u str[NUMBUFLEN];
13727#endif
13728
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013729 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013730
13731#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013732 lnum = tv_get_lnum(argvars); /* -1 on type error */
13733 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013734
13735 vim_memset(str, NUL, sizeof(str));
13736
13737 if (rettv_list_alloc(rettv) != FAIL)
13738 {
13739 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13740 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13741 && curwin->w_p_cole > 0)
13742 {
13743 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13744 syntax_flags = get_syntax_info(&matchid);
13745
13746 /* get the conceal character */
13747 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13748 {
13749 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013750 if (cchar == NUL && curwin->w_p_cole == 1)
13751 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013752 if (cchar != NUL)
13753 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013754 if (has_mbyte)
13755 (*mb_char2bytes)(cchar, str);
13756 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013757 str[0] = cchar;
13758 }
13759 }
13760 }
13761
13762 list_append_number(rettv->vval.v_list,
13763 (syntax_flags & HL_CONCEAL) != 0);
13764 /* -1 to auto-determine strlen */
13765 list_append_string(rettv->vval.v_list, str, -1);
13766 list_append_number(rettv->vval.v_list, matchid);
13767 }
13768#endif
13769}
13770
13771/*
13772 * "synstack(lnum, col)" function
13773 */
13774 static void
13775f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13776{
13777#ifdef FEAT_SYN_HL
13778 linenr_T lnum;
13779 colnr_T col;
13780 int i;
13781 int id;
13782#endif
13783
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013784 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013785
13786#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013787 lnum = tv_get_lnum(argvars); /* -1 on type error */
13788 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789
13790 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13791 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13792 && rettv_list_alloc(rettv) != FAIL)
13793 {
13794 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13795 for (i = 0; ; ++i)
13796 {
13797 id = syn_get_stack_item(i);
13798 if (id < 0)
13799 break;
13800 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13801 break;
13802 }
13803 }
13804#endif
13805}
13806
13807 static void
13808get_cmd_output_as_rettv(
13809 typval_T *argvars,
13810 typval_T *rettv,
13811 int retlist)
13812{
13813 char_u *res = NULL;
13814 char_u *p;
13815 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013816 int err = FALSE;
13817 FILE *fd;
13818 list_T *list = NULL;
13819 int flags = SHELL_SILENT;
13820
13821 rettv->v_type = VAR_STRING;
13822 rettv->vval.v_string = NULL;
13823 if (check_restricted() || check_secure())
13824 goto errret;
13825
13826 if (argvars[1].v_type != VAR_UNKNOWN)
13827 {
13828 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013829 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013830 * command.
13831 */
13832 if ((infile = vim_tempname('i', TRUE)) == NULL)
13833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013834 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013835 goto errret;
13836 }
13837
13838 fd = mch_fopen((char *)infile, WRITEBIN);
13839 if (fd == NULL)
13840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013841 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013842 goto errret;
13843 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013844 if (argvars[1].v_type == VAR_NUMBER)
13845 {
13846 linenr_T lnum;
13847 buf_T *buf;
13848
13849 buf = buflist_findnr(argvars[1].vval.v_number);
13850 if (buf == NULL)
13851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013852 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013853 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013854 goto errret;
13855 }
13856
13857 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13858 {
13859 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13860 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13861 {
13862 err = TRUE;
13863 break;
13864 }
13865 if (putc(NL, fd) == EOF)
13866 {
13867 err = TRUE;
13868 break;
13869 }
13870 }
13871 }
13872 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013873 {
13874 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13875 err = TRUE;
13876 }
13877 else
13878 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013879 size_t len;
13880 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013881
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013882 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013883 if (p == NULL)
13884 {
13885 fclose(fd);
13886 goto errret; /* type error; errmsg already given */
13887 }
13888 len = STRLEN(p);
13889 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13890 err = TRUE;
13891 }
13892 if (fclose(fd) != 0)
13893 err = TRUE;
13894 if (err)
13895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013896 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897 goto errret;
13898 }
13899 }
13900
13901 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13902 * echoes typeahead, that messes up the display. */
13903 if (!msg_silent)
13904 flags += SHELL_COOKED;
13905
13906 if (retlist)
13907 {
13908 int len;
13909 listitem_T *li;
13910 char_u *s = NULL;
13911 char_u *start;
13912 char_u *end;
13913 int i;
13914
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013915 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916 if (res == NULL)
13917 goto errret;
13918
13919 list = list_alloc();
13920 if (list == NULL)
13921 goto errret;
13922
13923 for (i = 0; i < len; ++i)
13924 {
13925 start = res + i;
13926 while (i < len && res[i] != NL)
13927 ++i;
13928 end = res + i;
13929
Bram Moolenaar964b3742019-05-24 18:54:09 +020013930 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013931 if (s == NULL)
13932 goto errret;
13933
13934 for (p = s; start < end; ++p, ++start)
13935 *p = *start == NUL ? NL : *start;
13936 *p = NUL;
13937
13938 li = listitem_alloc();
13939 if (li == NULL)
13940 {
13941 vim_free(s);
13942 goto errret;
13943 }
13944 li->li_tv.v_type = VAR_STRING;
13945 li->li_tv.v_lock = 0;
13946 li->li_tv.vval.v_string = s;
13947 list_append(list, li);
13948 }
13949
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013950 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013951 list = NULL;
13952 }
13953 else
13954 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013955 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013956#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013957 /* translate <CR><NL> into <NL> */
13958 if (res != NULL)
13959 {
13960 char_u *s, *d;
13961
13962 d = res;
13963 for (s = res; *s; ++s)
13964 {
13965 if (s[0] == CAR && s[1] == NL)
13966 ++s;
13967 *d++ = *s;
13968 }
13969 *d = NUL;
13970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013971#endif
13972 rettv->vval.v_string = res;
13973 res = NULL;
13974 }
13975
13976errret:
13977 if (infile != NULL)
13978 {
13979 mch_remove(infile);
13980 vim_free(infile);
13981 }
13982 if (res != NULL)
13983 vim_free(res);
13984 if (list != NULL)
13985 list_free(list);
13986}
13987
13988/*
13989 * "system()" function
13990 */
13991 static void
13992f_system(typval_T *argvars, typval_T *rettv)
13993{
13994 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13995}
13996
13997/*
13998 * "systemlist()" function
13999 */
14000 static void
14001f_systemlist(typval_T *argvars, typval_T *rettv)
14002{
14003 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14004}
14005
14006/*
14007 * "tabpagebuflist()" function
14008 */
14009 static void
14010f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14011{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014012 tabpage_T *tp;
14013 win_T *wp = NULL;
14014
14015 if (argvars[0].v_type == VAR_UNKNOWN)
14016 wp = firstwin;
14017 else
14018 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014019 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014020 if (tp != NULL)
14021 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14022 }
14023 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14024 {
14025 for (; wp != NULL; wp = wp->w_next)
14026 if (list_append_number(rettv->vval.v_list,
14027 wp->w_buffer->b_fnum) == FAIL)
14028 break;
14029 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014030}
14031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014032/*
14033 * "tabpagenr()" function
14034 */
14035 static void
14036f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14037{
14038 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014039 char_u *arg;
14040
14041 if (argvars[0].v_type != VAR_UNKNOWN)
14042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014043 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044 nr = 0;
14045 if (arg != NULL)
14046 {
14047 if (STRCMP(arg, "$") == 0)
14048 nr = tabpage_index(NULL) - 1;
14049 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014050 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014051 }
14052 }
14053 else
14054 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 rettv->vval.v_number = nr;
14056}
14057
14058
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014059/*
14060 * Common code for tabpagewinnr() and winnr().
14061 */
14062 static int
14063get_winnr(tabpage_T *tp, typval_T *argvar)
14064{
14065 win_T *twin;
14066 int nr = 1;
14067 win_T *wp;
14068 char_u *arg;
14069
14070 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14071 if (argvar->v_type != VAR_UNKNOWN)
14072 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014073 int invalid_arg = FALSE;
14074
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014075 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014076 if (arg == NULL)
14077 nr = 0; /* type error; errmsg already given */
14078 else if (STRCMP(arg, "$") == 0)
14079 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14080 else if (STRCMP(arg, "#") == 0)
14081 {
14082 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14083 if (twin == NULL)
14084 nr = 0;
14085 }
14086 else
14087 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014088 long count;
14089 char_u *endp;
14090
14091 // Extract the window count (if specified). e.g. winnr('3j')
14092 count = strtol((char *)arg, (char **)&endp, 10);
14093 if (count <= 0)
14094 count = 1; // if count is not specified, default to 1
14095 if (endp != NULL && *endp != '\0')
14096 {
14097 if (STRCMP(endp, "j") == 0)
14098 twin = win_vert_neighbor(tp, twin, FALSE, count);
14099 else if (STRCMP(endp, "k") == 0)
14100 twin = win_vert_neighbor(tp, twin, TRUE, count);
14101 else if (STRCMP(endp, "h") == 0)
14102 twin = win_horz_neighbor(tp, twin, TRUE, count);
14103 else if (STRCMP(endp, "l") == 0)
14104 twin = win_horz_neighbor(tp, twin, FALSE, count);
14105 else
14106 invalid_arg = TRUE;
14107 }
14108 else
14109 invalid_arg = TRUE;
14110 }
14111
14112 if (invalid_arg)
14113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014114 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014115 nr = 0;
14116 }
14117 }
14118
14119 if (nr > 0)
14120 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14121 wp != twin; wp = wp->w_next)
14122 {
14123 if (wp == NULL)
14124 {
14125 /* didn't find it in this tabpage */
14126 nr = 0;
14127 break;
14128 }
14129 ++nr;
14130 }
14131 return nr;
14132}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014133
14134/*
14135 * "tabpagewinnr()" function
14136 */
14137 static void
14138f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14139{
14140 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014141 tabpage_T *tp;
14142
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014143 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014144 if (tp == NULL)
14145 nr = 0;
14146 else
14147 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014148 rettv->vval.v_number = nr;
14149}
14150
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014151/*
14152 * "tagfiles()" function
14153 */
14154 static void
14155f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14156{
14157 char_u *fname;
14158 tagname_T tn;
14159 int first;
14160
14161 if (rettv_list_alloc(rettv) == FAIL)
14162 return;
14163 fname = alloc(MAXPATHL);
14164 if (fname == NULL)
14165 return;
14166
14167 for (first = TRUE; ; first = FALSE)
14168 if (get_tagfname(&tn, first, fname) == FAIL
14169 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14170 break;
14171 tagname_free(&tn);
14172 vim_free(fname);
14173}
14174
14175/*
14176 * "taglist()" function
14177 */
14178 static void
14179f_taglist(typval_T *argvars, typval_T *rettv)
14180{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014181 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014182 char_u *tag_pattern;
14183
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014184 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014185
14186 rettv->vval.v_number = FALSE;
14187 if (*tag_pattern == NUL)
14188 return;
14189
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014190 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014191 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014192 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014193 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014194}
14195
14196/*
14197 * "tempname()" function
14198 */
14199 static void
14200f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14201{
14202 static int x = 'A';
14203
14204 rettv->v_type = VAR_STRING;
14205 rettv->vval.v_string = vim_tempname(x, FALSE);
14206
14207 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14208 * names. Skip 'I' and 'O', they are used for shell redirection. */
14209 do
14210 {
14211 if (x == 'Z')
14212 x = '0';
14213 else if (x == '9')
14214 x = 'A';
14215 else
14216 {
14217#ifdef EBCDIC
14218 if (x == 'I')
14219 x = 'J';
14220 else if (x == 'R')
14221 x = 'S';
14222 else
14223#endif
14224 ++x;
14225 }
14226 } while (x == 'I' || x == 'O');
14227}
14228
14229#ifdef FEAT_FLOAT
14230/*
14231 * "tan()" function
14232 */
14233 static void
14234f_tan(typval_T *argvars, typval_T *rettv)
14235{
14236 float_T f = 0.0;
14237
14238 rettv->v_type = VAR_FLOAT;
14239 if (get_float_arg(argvars, &f) == OK)
14240 rettv->vval.v_float = tan(f);
14241 else
14242 rettv->vval.v_float = 0.0;
14243}
14244
14245/*
14246 * "tanh()" function
14247 */
14248 static void
14249f_tanh(typval_T *argvars, typval_T *rettv)
14250{
14251 float_T f = 0.0;
14252
14253 rettv->v_type = VAR_FLOAT;
14254 if (get_float_arg(argvars, &f) == OK)
14255 rettv->vval.v_float = tanh(f);
14256 else
14257 rettv->vval.v_float = 0.0;
14258}
14259#endif
14260
14261/*
14262 * "test_alloc_fail(id, countdown, repeat)" function
14263 */
14264 static void
14265f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14266{
14267 if (argvars[0].v_type != VAR_NUMBER
14268 || argvars[0].vval.v_number <= 0
14269 || argvars[1].v_type != VAR_NUMBER
14270 || argvars[1].vval.v_number < 0
14271 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014272 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014273 else
14274 {
14275 alloc_fail_id = argvars[0].vval.v_number;
14276 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014277 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014278 alloc_fail_countdown = argvars[1].vval.v_number;
14279 alloc_fail_repeat = argvars[2].vval.v_number;
14280 did_outofmem_msg = FALSE;
14281 }
14282}
14283
14284/*
14285 * "test_autochdir()"
14286 */
14287 static void
14288f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14289{
14290#if defined(FEAT_AUTOCHDIR)
14291 test_autochdir = TRUE;
14292#endif
14293}
14294
14295/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014296 * "test_feedinput()"
14297 */
14298 static void
14299f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14300{
14301#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014302 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014303
14304 if (val != NULL)
14305 {
14306 trash_input_buf();
14307 add_to_input_buf_csi(val, (int)STRLEN(val));
14308 }
14309#endif
14310}
14311
14312/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014313 * "test_getvalue({name})" function
14314 */
14315 static void
14316f_test_getvalue(typval_T *argvars, typval_T *rettv)
14317{
14318 if (argvars[0].v_type != VAR_STRING)
14319 emsg(_(e_invarg));
14320 else
14321 {
14322 char_u *name = tv_get_string(&argvars[0]);
14323
14324 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14325 rettv->vval.v_number = need_fileinfo;
14326 else
14327 semsg(_(e_invarg2), name);
14328 }
14329}
14330
14331/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014332 * "test_option_not_set({name})" function
14333 */
14334 static void
14335f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14336{
14337 char_u *name = (char_u *)"";
14338
14339 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014340 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014341 else
14342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014343 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014344 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014345 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014346 }
14347}
14348
14349/*
14350 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014351 */
14352 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014353f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014354{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014355 char_u *name = (char_u *)"";
14356 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014357 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014358
14359 if (argvars[0].v_type != VAR_STRING
14360 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014361 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014362 else
14363 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014364 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014365 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014366
14367 if (STRCMP(name, (char_u *)"redraw") == 0)
14368 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014369 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14370 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014371 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14372 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014373 else if (STRCMP(name, (char_u *)"starting") == 0)
14374 {
14375 if (val)
14376 {
14377 if (save_starting < 0)
14378 save_starting = starting;
14379 starting = 0;
14380 }
14381 else
14382 {
14383 starting = save_starting;
14384 save_starting = -1;
14385 }
14386 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014387 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14388 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014389 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14390 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014391 else if (STRCMP(name, (char_u *)"ALL") == 0)
14392 {
14393 disable_char_avail_for_testing = FALSE;
14394 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014395 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014396 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014397 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014398 if (save_starting >= 0)
14399 {
14400 starting = save_starting;
14401 save_starting = -1;
14402 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014403 }
14404 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014405 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014406 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014407}
14408
14409/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014410 * "test_refcount({expr})" function
14411 */
14412 static void
14413f_test_refcount(typval_T *argvars, typval_T *rettv)
14414{
14415 int retval = -1;
14416
14417 switch (argvars[0].v_type)
14418 {
14419 case VAR_UNKNOWN:
14420 case VAR_NUMBER:
14421 case VAR_FLOAT:
14422 case VAR_SPECIAL:
14423 case VAR_STRING:
14424 break;
14425 case VAR_JOB:
14426#ifdef FEAT_JOB_CHANNEL
14427 if (argvars[0].vval.v_job != NULL)
14428 retval = argvars[0].vval.v_job->jv_refcount - 1;
14429#endif
14430 break;
14431 case VAR_CHANNEL:
14432#ifdef FEAT_JOB_CHANNEL
14433 if (argvars[0].vval.v_channel != NULL)
14434 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14435#endif
14436 break;
14437 case VAR_FUNC:
14438 if (argvars[0].vval.v_string != NULL)
14439 {
14440 ufunc_T *fp;
14441
14442 fp = find_func(argvars[0].vval.v_string);
14443 if (fp != NULL)
14444 retval = fp->uf_refcount;
14445 }
14446 break;
14447 case VAR_PARTIAL:
14448 if (argvars[0].vval.v_partial != NULL)
14449 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14450 break;
14451 case VAR_BLOB:
14452 if (argvars[0].vval.v_blob != NULL)
14453 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14454 break;
14455 case VAR_LIST:
14456 if (argvars[0].vval.v_list != NULL)
14457 retval = argvars[0].vval.v_list->lv_refcount - 1;
14458 break;
14459 case VAR_DICT:
14460 if (argvars[0].vval.v_dict != NULL)
14461 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14462 break;
14463 }
14464
14465 rettv->v_type = VAR_NUMBER;
14466 rettv->vval.v_number = retval;
14467
14468}
14469
14470/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014471 * "test_garbagecollect_now()" function
14472 */
14473 static void
14474f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14475{
14476 /* This is dangerous, any Lists and Dicts used internally may be freed
14477 * while still in use. */
14478 garbage_collect(TRUE);
14479}
14480
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014481/*
14482 * "test_ignore_error()" function
14483 */
14484 static void
14485f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14486{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014487 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014488}
14489
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014490 static void
14491f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14492{
14493 rettv->v_type = VAR_BLOB;
14494 rettv->vval.v_blob = NULL;
14495}
14496
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014497#ifdef FEAT_JOB_CHANNEL
14498 static void
14499f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14500{
14501 rettv->v_type = VAR_CHANNEL;
14502 rettv->vval.v_channel = NULL;
14503}
14504#endif
14505
14506 static void
14507f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14508{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014509 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014510}
14511
14512#ifdef FEAT_JOB_CHANNEL
14513 static void
14514f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14515{
14516 rettv->v_type = VAR_JOB;
14517 rettv->vval.v_job = NULL;
14518}
14519#endif
14520
14521 static void
14522f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14523{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014524 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014525}
14526
14527 static void
14528f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14529{
14530 rettv->v_type = VAR_PARTIAL;
14531 rettv->vval.v_partial = NULL;
14532}
14533
14534 static void
14535f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14536{
14537 rettv->v_type = VAR_STRING;
14538 rettv->vval.v_string = NULL;
14539}
14540
Bram Moolenaarab186732018-09-14 21:27:06 +020014541#ifdef FEAT_GUI
14542 static void
14543f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14544{
14545 char_u *which;
14546 long value;
14547 int dragging;
14548 scrollbar_T *sb = NULL;
14549
14550 if (argvars[0].v_type != VAR_STRING
14551 || (argvars[1].v_type) != VAR_NUMBER
14552 || (argvars[2].v_type) != VAR_NUMBER)
14553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014554 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014555 return;
14556 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014557 which = tv_get_string(&argvars[0]);
14558 value = tv_get_number(&argvars[1]);
14559 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014560
14561 if (STRCMP(which, "left") == 0)
14562 sb = &curwin->w_scrollbars[SBAR_LEFT];
14563 else if (STRCMP(which, "right") == 0)
14564 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14565 else if (STRCMP(which, "hor") == 0)
14566 sb = &gui.bottom_sbar;
14567 if (sb == NULL)
14568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014569 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014570 return;
14571 }
14572 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014573# ifndef USE_ON_FLY_SCROLL
14574 // need to loop through normal_cmd() to handle the scroll events
14575 exec_normal(FALSE, TRUE, FALSE);
14576# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014577}
14578#endif
14579
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014580#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014581 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014582f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14583{
14584 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14585 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14586}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014587#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014588
14589 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014590f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14591{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014592 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014593}
14594
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014595/*
14596 * Get a callback from "arg". It can be a Funcref or a function name.
14597 * When "arg" is zero return an empty string.
14598 * Return NULL for an invalid argument.
14599 */
14600 char_u *
14601get_callback(typval_T *arg, partial_T **pp)
14602{
14603 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14604 {
14605 *pp = arg->vval.v_partial;
14606 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014607 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014608 }
14609 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014610 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014611 {
14612 func_ref(arg->vval.v_string);
14613 return arg->vval.v_string;
14614 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014615 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14616 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014617 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014618 return NULL;
14619}
14620
14621/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014622 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014623 */
14624 void
14625free_callback(char_u *callback, partial_T *partial)
14626{
14627 if (partial != NULL)
14628 partial_unref(partial);
14629 else if (callback != NULL)
14630 {
14631 func_unref(callback);
14632 vim_free(callback);
14633 }
14634}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014635
14636#ifdef FEAT_TIMERS
14637/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014638 * "timer_info([timer])" function
14639 */
14640 static void
14641f_timer_info(typval_T *argvars, typval_T *rettv)
14642{
14643 timer_T *timer = NULL;
14644
14645 if (rettv_list_alloc(rettv) != OK)
14646 return;
14647 if (argvars[0].v_type != VAR_UNKNOWN)
14648 {
14649 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014650 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014651 else
14652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014653 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014654 if (timer != NULL)
14655 add_timer_info(rettv, timer);
14656 }
14657 }
14658 else
14659 add_timer_info_all(rettv);
14660}
14661
14662/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014663 * "timer_pause(timer, paused)" function
14664 */
14665 static void
14666f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14667{
14668 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014670
14671 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014672 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014673 else
14674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014675 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014676 if (timer != NULL)
14677 timer->tr_paused = paused;
14678 }
14679}
14680
14681/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014682 * "timer_start(time, callback [, options])" function
14683 */
14684 static void
14685f_timer_start(typval_T *argvars, typval_T *rettv)
14686{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014687 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014688 timer_T *timer;
14689 int repeat = 0;
14690 char_u *callback;
14691 dict_T *dict;
14692 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014693
Bram Moolenaar75537a92016-09-05 22:45:28 +020014694 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014695 if (check_secure())
14696 return;
14697 if (argvars[2].v_type != VAR_UNKNOWN)
14698 {
14699 if (argvars[2].v_type != VAR_DICT
14700 || (dict = argvars[2].vval.v_dict) == NULL)
14701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014702 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014703 return;
14704 }
14705 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014706 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014707 }
14708
Bram Moolenaar75537a92016-09-05 22:45:28 +020014709 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014711 return;
14712
14713 timer = create_timer(msec, repeat);
14714 if (timer == NULL)
14715 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014716 else
14717 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014718 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014719 timer->tr_callback = vim_strsave(callback);
14720 else
14721 /* pointer into the partial */
14722 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014723 timer->tr_partial = partial;
14724 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014725 }
14726}
14727
14728/*
14729 * "timer_stop(timer)" function
14730 */
14731 static void
14732f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14733{
14734 timer_T *timer;
14735
14736 if (argvars[0].v_type != VAR_NUMBER)
14737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014738 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014739 return;
14740 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014741 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742 if (timer != NULL)
14743 stop_timer(timer);
14744}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014745
14746/*
14747 * "timer_stopall()" function
14748 */
14749 static void
14750f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14751{
14752 stop_all_timers();
14753}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014754#endif
14755
14756/*
14757 * "tolower(string)" function
14758 */
14759 static void
14760f_tolower(typval_T *argvars, typval_T *rettv)
14761{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014762 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014763 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014764}
14765
14766/*
14767 * "toupper(string)" function
14768 */
14769 static void
14770f_toupper(typval_T *argvars, typval_T *rettv)
14771{
14772 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014773 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014774}
14775
14776/*
14777 * "tr(string, fromstr, tostr)" function
14778 */
14779 static void
14780f_tr(typval_T *argvars, typval_T *rettv)
14781{
14782 char_u *in_str;
14783 char_u *fromstr;
14784 char_u *tostr;
14785 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014786 int inlen;
14787 int fromlen;
14788 int tolen;
14789 int idx;
14790 char_u *cpstr;
14791 int cplen;
14792 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014793 char_u buf[NUMBUFLEN];
14794 char_u buf2[NUMBUFLEN];
14795 garray_T ga;
14796
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014797 in_str = tv_get_string(&argvars[0]);
14798 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14799 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014800
14801 /* Default return value: empty string. */
14802 rettv->v_type = VAR_STRING;
14803 rettv->vval.v_string = NULL;
14804 if (fromstr == NULL || tostr == NULL)
14805 return; /* type error; errmsg already given */
14806 ga_init2(&ga, (int)sizeof(char), 80);
14807
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809 /* not multi-byte: fromstr and tostr must be the same length */
14810 if (STRLEN(fromstr) != STRLEN(tostr))
14811 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014812error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014813 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014814 ga_clear(&ga);
14815 return;
14816 }
14817
14818 /* fromstr and tostr have to contain the same number of chars */
14819 while (*in_str != NUL)
14820 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 if (has_mbyte)
14822 {
14823 inlen = (*mb_ptr2len)(in_str);
14824 cpstr = in_str;
14825 cplen = inlen;
14826 idx = 0;
14827 for (p = fromstr; *p != NUL; p += fromlen)
14828 {
14829 fromlen = (*mb_ptr2len)(p);
14830 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14831 {
14832 for (p = tostr; *p != NUL; p += tolen)
14833 {
14834 tolen = (*mb_ptr2len)(p);
14835 if (idx-- == 0)
14836 {
14837 cplen = tolen;
14838 cpstr = p;
14839 break;
14840 }
14841 }
14842 if (*p == NUL) /* tostr is shorter than fromstr */
14843 goto error;
14844 break;
14845 }
14846 ++idx;
14847 }
14848
14849 if (first && cpstr == in_str)
14850 {
14851 /* Check that fromstr and tostr have the same number of
14852 * (multi-byte) characters. Done only once when a character
14853 * of in_str doesn't appear in fromstr. */
14854 first = FALSE;
14855 for (p = tostr; *p != NUL; p += tolen)
14856 {
14857 tolen = (*mb_ptr2len)(p);
14858 --idx;
14859 }
14860 if (idx != 0)
14861 goto error;
14862 }
14863
14864 (void)ga_grow(&ga, cplen);
14865 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14866 ga.ga_len += cplen;
14867
14868 in_str += inlen;
14869 }
14870 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014871 {
14872 /* When not using multi-byte chars we can do it faster. */
14873 p = vim_strchr(fromstr, *in_str);
14874 if (p != NULL)
14875 ga_append(&ga, tostr[p - fromstr]);
14876 else
14877 ga_append(&ga, *in_str);
14878 ++in_str;
14879 }
14880 }
14881
14882 /* add a terminating NUL */
14883 (void)ga_grow(&ga, 1);
14884 ga_append(&ga, NUL);
14885
14886 rettv->vval.v_string = ga.ga_data;
14887}
14888
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014889/*
14890 * "trim({expr})" function
14891 */
14892 static void
14893f_trim(typval_T *argvars, typval_T *rettv)
14894{
14895 char_u buf1[NUMBUFLEN];
14896 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014897 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014898 char_u *mask = NULL;
14899 char_u *tail;
14900 char_u *prev;
14901 char_u *p;
14902 int c1;
14903
14904 rettv->v_type = VAR_STRING;
14905 if (head == NULL)
14906 {
14907 rettv->vval.v_string = NULL;
14908 return;
14909 }
14910
14911 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014912 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014913
14914 while (*head != NUL)
14915 {
14916 c1 = PTR2CHAR(head);
14917 if (mask == NULL)
14918 {
14919 if (c1 > ' ' && c1 != 0xa0)
14920 break;
14921 }
14922 else
14923 {
14924 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14925 if (c1 == PTR2CHAR(p))
14926 break;
14927 if (*p == NUL)
14928 break;
14929 }
14930 MB_PTR_ADV(head);
14931 }
14932
14933 for (tail = head + STRLEN(head); tail > head; tail = prev)
14934 {
14935 prev = tail;
14936 MB_PTR_BACK(head, prev);
14937 c1 = PTR2CHAR(prev);
14938 if (mask == NULL)
14939 {
14940 if (c1 > ' ' && c1 != 0xa0)
14941 break;
14942 }
14943 else
14944 {
14945 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14946 if (c1 == PTR2CHAR(p))
14947 break;
14948 if (*p == NUL)
14949 break;
14950 }
14951 }
14952 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14953}
14954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014955#ifdef FEAT_FLOAT
14956/*
14957 * "trunc({float})" function
14958 */
14959 static void
14960f_trunc(typval_T *argvars, typval_T *rettv)
14961{
14962 float_T f = 0.0;
14963
14964 rettv->v_type = VAR_FLOAT;
14965 if (get_float_arg(argvars, &f) == OK)
14966 /* trunc() is not in C90, use floor() or ceil() instead. */
14967 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14968 else
14969 rettv->vval.v_float = 0.0;
14970}
14971#endif
14972
14973/*
14974 * "type(expr)" function
14975 */
14976 static void
14977f_type(typval_T *argvars, typval_T *rettv)
14978{
14979 int n = -1;
14980
14981 switch (argvars[0].v_type)
14982 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014983 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14984 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014985 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014986 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14987 case VAR_LIST: n = VAR_TYPE_LIST; break;
14988 case VAR_DICT: n = VAR_TYPE_DICT; break;
14989 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014990 case VAR_SPECIAL:
14991 if (argvars[0].vval.v_number == VVAL_FALSE
14992 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014993 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014994 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014995 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014996 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014997 case VAR_JOB: n = VAR_TYPE_JOB; break;
14998 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014999 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015000 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015001 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015002 n = -1;
15003 break;
15004 }
15005 rettv->vval.v_number = n;
15006}
15007
15008/*
15009 * "undofile(name)" function
15010 */
15011 static void
15012f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15013{
15014 rettv->v_type = VAR_STRING;
15015#ifdef FEAT_PERSISTENT_UNDO
15016 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015017 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015018
15019 if (*fname == NUL)
15020 {
15021 /* If there is no file name there will be no undo file. */
15022 rettv->vval.v_string = NULL;
15023 }
15024 else
15025 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015026 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015027
15028 if (ffname != NULL)
15029 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15030 vim_free(ffname);
15031 }
15032 }
15033#else
15034 rettv->vval.v_string = NULL;
15035#endif
15036}
15037
15038/*
15039 * "undotree()" function
15040 */
15041 static void
15042f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15043{
15044 if (rettv_dict_alloc(rettv) == OK)
15045 {
15046 dict_T *dict = rettv->vval.v_dict;
15047 list_T *list;
15048
Bram Moolenaare0be1672018-07-08 16:50:37 +020015049 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15050 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15051 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15052 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15053 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15054 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015055
15056 list = list_alloc();
15057 if (list != NULL)
15058 {
15059 u_eval_tree(curbuf->b_u_oldhead, list);
15060 dict_add_list(dict, "entries", list);
15061 }
15062 }
15063}
15064
15065/*
15066 * "values(dict)" function
15067 */
15068 static void
15069f_values(typval_T *argvars, typval_T *rettv)
15070{
15071 dict_list(argvars, rettv, 1);
15072}
15073
15074/*
15075 * "virtcol(string)" function
15076 */
15077 static void
15078f_virtcol(typval_T *argvars, typval_T *rettv)
15079{
15080 colnr_T vcol = 0;
15081 pos_T *fp;
15082 int fnum = curbuf->b_fnum;
15083
15084 fp = var2fpos(&argvars[0], FALSE, &fnum);
15085 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15086 && fnum == curbuf->b_fnum)
15087 {
15088 getvvcol(curwin, fp, NULL, NULL, &vcol);
15089 ++vcol;
15090 }
15091
15092 rettv->vval.v_number = vcol;
15093}
15094
15095/*
15096 * "visualmode()" function
15097 */
15098 static void
15099f_visualmode(typval_T *argvars, typval_T *rettv)
15100{
15101 char_u str[2];
15102
15103 rettv->v_type = VAR_STRING;
15104 str[0] = curbuf->b_visual_mode_eval;
15105 str[1] = NUL;
15106 rettv->vval.v_string = vim_strsave(str);
15107
15108 /* A non-zero number or non-empty string argument: reset mode. */
15109 if (non_zero_arg(&argvars[0]))
15110 curbuf->b_visual_mode_eval = NUL;
15111}
15112
15113/*
15114 * "wildmenumode()" function
15115 */
15116 static void
15117f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15118{
15119#ifdef FEAT_WILDMENU
15120 if (wild_menu_showing)
15121 rettv->vval.v_number = 1;
15122#endif
15123}
15124
15125/*
15126 * "winbufnr(nr)" function
15127 */
15128 static void
15129f_winbufnr(typval_T *argvars, typval_T *rettv)
15130{
15131 win_T *wp;
15132
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015133 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015134 if (wp == NULL)
15135 rettv->vval.v_number = -1;
15136 else
15137 rettv->vval.v_number = wp->w_buffer->b_fnum;
15138}
15139
15140/*
15141 * "wincol()" function
15142 */
15143 static void
15144f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15145{
15146 validate_cursor();
15147 rettv->vval.v_number = curwin->w_wcol + 1;
15148}
15149
15150/*
15151 * "winheight(nr)" function
15152 */
15153 static void
15154f_winheight(typval_T *argvars, typval_T *rettv)
15155{
15156 win_T *wp;
15157
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015158 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015159 if (wp == NULL)
15160 rettv->vval.v_number = -1;
15161 else
15162 rettv->vval.v_number = wp->w_height;
15163}
15164
15165/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015166 * "winlayout()" function
15167 */
15168 static void
15169f_winlayout(typval_T *argvars, typval_T *rettv)
15170{
15171 tabpage_T *tp;
15172
15173 if (rettv_list_alloc(rettv) != OK)
15174 return;
15175
15176 if (argvars[0].v_type == VAR_UNKNOWN)
15177 tp = curtab;
15178 else
15179 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015180 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015181 if (tp == NULL)
15182 return;
15183 }
15184
15185 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15186}
15187
15188/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015189 * "winline()" function
15190 */
15191 static void
15192f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15193{
15194 validate_cursor();
15195 rettv->vval.v_number = curwin->w_wrow + 1;
15196}
15197
15198/*
15199 * "winnr()" function
15200 */
15201 static void
15202f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15203{
15204 int nr = 1;
15205
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015206 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015207 rettv->vval.v_number = nr;
15208}
15209
15210/*
15211 * "winrestcmd()" function
15212 */
15213 static void
15214f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15215{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015216 win_T *wp;
15217 int winnr = 1;
15218 garray_T ga;
15219 char_u buf[50];
15220
15221 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015222 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015223 {
15224 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15225 ga_concat(&ga, buf);
15226 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15227 ga_concat(&ga, buf);
15228 ++winnr;
15229 }
15230 ga_append(&ga, NUL);
15231
15232 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015233 rettv->v_type = VAR_STRING;
15234}
15235
15236/*
15237 * "winrestview()" function
15238 */
15239 static void
15240f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15241{
15242 dict_T *dict;
15243
15244 if (argvars[0].v_type != VAR_DICT
15245 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015246 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015247 else
15248 {
15249 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015250 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015251 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015252 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015253 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015254 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015255 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15256 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015257 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015258 curwin->w_set_curswant = FALSE;
15259 }
15260
15261 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015262 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015263#ifdef FEAT_DIFF
15264 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015265 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015266#endif
15267 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015268 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015269 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015270 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015271
15272 check_cursor();
15273 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015274 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015275 changed_window_setting();
15276
15277 if (curwin->w_topline <= 0)
15278 curwin->w_topline = 1;
15279 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15280 curwin->w_topline = curbuf->b_ml.ml_line_count;
15281#ifdef FEAT_DIFF
15282 check_topfill(curwin, TRUE);
15283#endif
15284 }
15285}
15286
15287/*
15288 * "winsaveview()" function
15289 */
15290 static void
15291f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15292{
15293 dict_T *dict;
15294
15295 if (rettv_dict_alloc(rettv) == FAIL)
15296 return;
15297 dict = rettv->vval.v_dict;
15298
Bram Moolenaare0be1672018-07-08 16:50:37 +020015299 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15300 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015301 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015302 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015303 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015304
Bram Moolenaare0be1672018-07-08 16:50:37 +020015305 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015306#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015307 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015308#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015309 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15310 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015311}
15312
15313/*
15314 * "winwidth(nr)" function
15315 */
15316 static void
15317f_winwidth(typval_T *argvars, typval_T *rettv)
15318{
15319 win_T *wp;
15320
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015321 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015322 if (wp == NULL)
15323 rettv->vval.v_number = -1;
15324 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015326}
15327
15328/*
15329 * "wordcount()" function
15330 */
15331 static void
15332f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15333{
15334 if (rettv_dict_alloc(rettv) == FAIL)
15335 return;
15336 cursor_pos_info(rettv->vval.v_dict);
15337}
15338
15339/*
15340 * "writefile()" function
15341 */
15342 static void
15343f_writefile(typval_T *argvars, typval_T *rettv)
15344{
15345 int binary = FALSE;
15346 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015347#ifdef HAVE_FSYNC
15348 int do_fsync = p_fs;
15349#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350 char_u *fname;
15351 FILE *fd;
15352 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015353 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015354 list_T *list = NULL;
15355 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015357 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015358 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015359 return;
15360
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015361 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015362 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015363 list = argvars[0].vval.v_list;
15364 if (list == NULL)
15365 return;
15366 for (li = list->lv_first; li != NULL; li = li->li_next)
15367 if (tv_get_string_chk(&li->li_tv) == NULL)
15368 return;
15369 }
15370 else if (argvars[0].v_type == VAR_BLOB)
15371 {
15372 blob = argvars[0].vval.v_blob;
15373 if (blob == NULL)
15374 return;
15375 }
15376 else
15377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015378 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015379 return;
15380 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015381
15382 if (argvars[2].v_type != VAR_UNKNOWN)
15383 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015384 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015385
15386 if (arg2 == NULL)
15387 return;
15388 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015389 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015390 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015392#ifdef HAVE_FSYNC
15393 if (vim_strchr(arg2, 's') != NULL)
15394 do_fsync = TRUE;
15395 else if (vim_strchr(arg2, 'S') != NULL)
15396 do_fsync = FALSE;
15397#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015398 }
15399
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015400 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015401 if (fname == NULL)
15402 return;
15403
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015404 /* Always open the file in binary mode, library functions have a mind of
15405 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015406 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15407 append ? APPENDBIN : WRITEBIN)) == NULL)
15408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015409 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015410 ret = -1;
15411 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015412 else if (blob)
15413 {
15414 if (write_blob(fd, blob) == FAIL)
15415 ret = -1;
15416#ifdef HAVE_FSYNC
15417 else if (do_fsync)
15418 // Ignore the error, the user wouldn't know what to do about it.
15419 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015420 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015421#endif
15422 fclose(fd);
15423 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015424 else
15425 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015426 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015427 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015428#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015429 else if (do_fsync)
15430 /* Ignore the error, the user wouldn't know what to do about it.
15431 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015432 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015433#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015434 fclose(fd);
15435 }
15436
15437 rettv->vval.v_number = ret;
15438}
15439
15440/*
15441 * "xor(expr, expr)" function
15442 */
15443 static void
15444f_xor(typval_T *argvars, typval_T *rettv)
15445{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015446 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15447 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015448}
15449
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015450#endif /* FEAT_EVAL */