blob: ccced484eef3c9c6454d9f980f276f7cfb14de40 [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 Moolenaar2cd0dce2019-05-26 22:17:52 +0200813 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200814 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200815 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200817#ifdef FEAT_FLOAT
818 {"pow", 2, 2, f_pow},
819#endif
820 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100821 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200822#ifdef FEAT_JOB_CHANNEL
823 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200824 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200825 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
826#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100827#ifdef FEAT_TEXT_PROP
828 {"prop_add", 3, 3, f_prop_add},
829 {"prop_clear", 1, 3, f_prop_clear},
830 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100831 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100832 {"prop_type_add", 2, 2, f_prop_type_add},
833 {"prop_type_change", 2, 2, f_prop_type_change},
834 {"prop_type_delete", 1, 2, f_prop_type_delete},
835 {"prop_type_get", 1, 2, f_prop_type_get},
836 {"prop_type_list", 0, 1, f_prop_type_list},
837#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200838 {"pumvisible", 0, 0, f_pumvisible},
839#ifdef FEAT_PYTHON3
840 {"py3eval", 1, 1, f_py3eval},
841#endif
842#ifdef FEAT_PYTHON
843 {"pyeval", 1, 1, f_pyeval},
844#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100845#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
846 {"pyxeval", 1, 1, f_pyxeval},
847#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200848 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200849 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200851 {"reg_executing", 0, 0, f_reg_executing},
852 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200853 {"reltime", 0, 2, f_reltime},
854#ifdef FEAT_FLOAT
855 {"reltimefloat", 1, 1, f_reltimefloat},
856#endif
857 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100858 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200859 {"remote_foreground", 1, 1, f_remote_foreground},
860 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100861 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200862 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100863 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"remove", 2, 3, f_remove},
865 {"rename", 2, 2, f_rename},
866 {"repeat", 2, 2, f_repeat},
867 {"resolve", 1, 1, f_resolve},
868 {"reverse", 1, 1, f_reverse},
869#ifdef FEAT_FLOAT
870 {"round", 1, 1, f_round},
871#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100872#ifdef FEAT_RUBY
873 {"rubyeval", 1, 1, f_rubyeval},
874#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200875 {"screenattr", 2, 2, f_screenattr},
876 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100877 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200878 {"screencol", 0, 0, f_screencol},
879 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100880 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200881 {"search", 1, 4, f_search},
882 {"searchdecl", 1, 3, f_searchdecl},
883 {"searchpair", 3, 7, f_searchpair},
884 {"searchpairpos", 3, 7, f_searchpairpos},
885 {"searchpos", 1, 4, f_searchpos},
886 {"server2client", 2, 2, f_server2client},
887 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200888 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200889 {"setbufvar", 3, 3, f_setbufvar},
890 {"setcharsearch", 1, 1, f_setcharsearch},
891 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200892 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200893 {"setfperm", 2, 2, f_setfperm},
894 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200895 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100896 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200898 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"setreg", 2, 3, f_setreg},
900 {"settabvar", 3, 3, f_settabvar},
901 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100902 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setwinvar", 3, 3, f_setwinvar},
904#ifdef FEAT_CRYPT
905 {"sha256", 1, 1, f_sha256},
906#endif
907 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100908 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100909#ifdef FEAT_SIGNS
910 {"sign_define", 1, 2, f_sign_define},
911 {"sign_getdefined", 0, 1, f_sign_getdefined},
912 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100913 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100914 {"sign_place", 4, 5, f_sign_place},
915 {"sign_undefine", 0, 1, f_sign_undefine},
916 {"sign_unplace", 1, 2, f_sign_unplace},
917#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200918 {"simplify", 1, 1, f_simplify},
919#ifdef FEAT_FLOAT
920 {"sin", 1, 1, f_sin},
921 {"sinh", 1, 1, f_sinh},
922#endif
923 {"sort", 1, 3, f_sort},
924 {"soundfold", 1, 1, f_soundfold},
925 {"spellbadword", 0, 1, f_spellbadword},
926 {"spellsuggest", 1, 3, f_spellsuggest},
927 {"split", 1, 3, f_split},
928#ifdef FEAT_FLOAT
929 {"sqrt", 1, 1, f_sqrt},
930 {"str2float", 1, 1, f_str2float},
931#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200932 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200933 {"str2nr", 1, 2, f_str2nr},
934 {"strcharpart", 2, 3, f_strcharpart},
935 {"strchars", 1, 2, f_strchars},
936 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
937#ifdef HAVE_STRFTIME
938 {"strftime", 1, 2, f_strftime},
939#endif
940 {"strgetchar", 2, 2, f_strgetchar},
941 {"stridx", 2, 3, f_stridx},
942 {"string", 1, 1, f_string},
943 {"strlen", 1, 1, f_strlen},
944 {"strpart", 2, 3, f_strpart},
945 {"strridx", 2, 3, f_strridx},
946 {"strtrans", 1, 1, f_strtrans},
947 {"strwidth", 1, 1, f_strwidth},
948 {"submatch", 1, 2, f_submatch},
949 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200950 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200951 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200952 {"synID", 3, 3, f_synID},
953 {"synIDattr", 2, 3, f_synIDattr},
954 {"synIDtrans", 1, 1, f_synIDtrans},
955 {"synconcealed", 2, 2, f_synconcealed},
956 {"synstack", 2, 2, f_synstack},
957 {"system", 1, 2, f_system},
958 {"systemlist", 1, 2, f_systemlist},
959 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
960 {"tabpagenr", 0, 1, f_tabpagenr},
961 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
962 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100963 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200964#ifdef FEAT_FLOAT
965 {"tan", 1, 1, f_tan},
966 {"tanh", 1, 1, f_tanh},
967#endif
968 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200969#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100970 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
971 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100972 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200973 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200974# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
975 {"term_getansicolors", 1, 1, f_term_getansicolors},
976# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200977 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200978 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200980 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200981 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200982 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200983 {"term_getstatus", 1, 1, f_term_getstatus},
984 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200985 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200987 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200989# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
990 {"term_setansicolors", 2, 2, f_term_setansicolors},
991# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100993 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200994 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200995 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200996 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200997#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200998 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
999 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001000 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001001 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001002 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001003 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001004 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001005#ifdef FEAT_JOB_CHANNEL
1006 {"test_null_channel", 0, 0, f_test_null_channel},
1007#endif
1008 {"test_null_dict", 0, 0, f_test_null_dict},
1009#ifdef FEAT_JOB_CHANNEL
1010 {"test_null_job", 0, 0, f_test_null_job},
1011#endif
1012 {"test_null_list", 0, 0, f_test_null_list},
1013 {"test_null_partial", 0, 0, f_test_null_partial},
1014 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001015 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001016 {"test_override", 2, 2, f_test_override},
1017 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001018#ifdef FEAT_GUI
1019 {"test_scrollbar", 3, 3, f_test_scrollbar},
1020#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001021#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001022 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001023#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001024 {"test_settime", 1, 1, f_test_settime},
1025#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001026 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001027 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"timer_start", 2, 3, f_timer_start},
1029 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001030 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001031#endif
1032 {"tolower", 1, 1, f_tolower},
1033 {"toupper", 1, 1, f_toupper},
1034 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001035 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001036#ifdef FEAT_FLOAT
1037 {"trunc", 1, 1, f_trunc},
1038#endif
1039 {"type", 1, 1, f_type},
1040 {"undofile", 1, 1, f_undofile},
1041 {"undotree", 0, 0, f_undotree},
1042 {"uniq", 1, 3, f_uniq},
1043 {"values", 1, 1, f_values},
1044 {"virtcol", 1, 1, f_virtcol},
1045 {"visualmode", 0, 1, f_visualmode},
1046 {"wildmenumode", 0, 0, f_wildmenumode},
1047 {"win_findbuf", 1, 1, f_win_findbuf},
1048 {"win_getid", 0, 2, f_win_getid},
1049 {"win_gotoid", 1, 1, f_win_gotoid},
1050 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1051 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001052 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001053 {"winbufnr", 1, 1, f_winbufnr},
1054 {"wincol", 0, 0, f_wincol},
1055 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001056 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001057 {"winline", 0, 0, f_winline},
1058 {"winnr", 0, 1, f_winnr},
1059 {"winrestcmd", 0, 0, f_winrestcmd},
1060 {"winrestview", 1, 1, f_winrestview},
1061 {"winsaveview", 0, 0, f_winsaveview},
1062 {"winwidth", 1, 1, f_winwidth},
1063 {"wordcount", 0, 0, f_wordcount},
1064 {"writefile", 2, 3, f_writefile},
1065 {"xor", 2, 2, f_xor},
1066};
1067
1068#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1069
1070/*
1071 * Function given to ExpandGeneric() to obtain the list of internal
1072 * or user defined function names.
1073 */
1074 char_u *
1075get_function_name(expand_T *xp, int idx)
1076{
1077 static int intidx = -1;
1078 char_u *name;
1079
1080 if (idx == 0)
1081 intidx = -1;
1082 if (intidx < 0)
1083 {
1084 name = get_user_func_name(xp, idx);
1085 if (name != NULL)
1086 return name;
1087 }
1088 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1089 {
1090 STRCPY(IObuff, functions[intidx].f_name);
1091 STRCAT(IObuff, "(");
1092 if (functions[intidx].f_max_argc == 0)
1093 STRCAT(IObuff, ")");
1094 return IObuff;
1095 }
1096
1097 return NULL;
1098}
1099
1100/*
1101 * Function given to ExpandGeneric() to obtain the list of internal or
1102 * user defined variable or function names.
1103 */
1104 char_u *
1105get_expr_name(expand_T *xp, int idx)
1106{
1107 static int intidx = -1;
1108 char_u *name;
1109
1110 if (idx == 0)
1111 intidx = -1;
1112 if (intidx < 0)
1113 {
1114 name = get_function_name(xp, idx);
1115 if (name != NULL)
1116 return name;
1117 }
1118 return get_user_var_name(xp, ++intidx);
1119}
1120
1121#endif /* FEAT_CMDL_COMPL */
1122
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001123/*
1124 * Find internal function in table above.
1125 * Return index, or -1 if not found
1126 */
1127 int
1128find_internal_func(
1129 char_u *name) /* name of the function */
1130{
1131 int first = 0;
1132 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1133 int cmp;
1134 int x;
1135
1136 /*
1137 * Find the function name in the table. Binary search.
1138 */
1139 while (first <= last)
1140 {
1141 x = first + ((unsigned)(last - first) >> 1);
1142 cmp = STRCMP(name, functions[x].f_name);
1143 if (cmp < 0)
1144 last = x - 1;
1145 else if (cmp > 0)
1146 first = x + 1;
1147 else
1148 return x;
1149 }
1150 return -1;
1151}
1152
1153 int
1154call_internal_func(
1155 char_u *name,
1156 int argcount,
1157 typval_T *argvars,
1158 typval_T *rettv)
1159{
1160 int i;
1161
1162 i = find_internal_func(name);
1163 if (i < 0)
1164 return ERROR_UNKNOWN;
1165 if (argcount < functions[i].f_min_argc)
1166 return ERROR_TOOFEW;
1167 if (argcount > functions[i].f_max_argc)
1168 return ERROR_TOOMANY;
1169 argvars[argcount].v_type = VAR_UNKNOWN;
1170 functions[i].f_func(argvars, rettv);
1171 return ERROR_NONE;
1172}
1173
1174/*
1175 * Return TRUE for a non-zero Number and a non-empty String.
1176 */
1177 static int
1178non_zero_arg(typval_T *argvars)
1179{
1180 return ((argvars[0].v_type == VAR_NUMBER
1181 && argvars[0].vval.v_number != 0)
1182 || (argvars[0].v_type == VAR_SPECIAL
1183 && argvars[0].vval.v_number == VVAL_TRUE)
1184 || (argvars[0].v_type == VAR_STRING
1185 && argvars[0].vval.v_string != NULL
1186 && *argvars[0].vval.v_string != NUL));
1187}
1188
1189/*
1190 * Get the lnum from the first argument.
1191 * Also accepts ".", "$", etc., but that only works for the current buffer.
1192 * Returns -1 on error.
1193 */
1194 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001195tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001196{
1197 typval_T rettv;
1198 linenr_T lnum;
1199
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201 if (lnum == 0) /* no valid number, try using line() */
1202 {
1203 rettv.v_type = VAR_NUMBER;
1204 f_line(argvars, &rettv);
1205 lnum = (linenr_T)rettv.vval.v_number;
1206 clear_tv(&rettv);
1207 }
1208 return lnum;
1209}
1210
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001211/*
1212 * Get the lnum from the first argument.
1213 * Also accepts "$", then "buf" is used.
1214 * Returns 0 on error.
1215 */
1216 static linenr_T
1217tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1218{
1219 if (argvars[0].v_type == VAR_STRING
1220 && argvars[0].vval.v_string != NULL
1221 && argvars[0].vval.v_string[0] == '$'
1222 && buf != NULL)
1223 return buf->b_ml.ml_line_count;
1224 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1225}
1226
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001227#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001228/*
1229 * Get the float value of "argvars[0]" into "f".
1230 * Returns FAIL when the argument is not a Number or Float.
1231 */
1232 static int
1233get_float_arg(typval_T *argvars, float_T *f)
1234{
1235 if (argvars[0].v_type == VAR_FLOAT)
1236 {
1237 *f = argvars[0].vval.v_float;
1238 return OK;
1239 }
1240 if (argvars[0].v_type == VAR_NUMBER)
1241 {
1242 *f = (float_T)argvars[0].vval.v_number;
1243 return OK;
1244 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001245 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001246 return FAIL;
1247}
1248
1249/*
1250 * "abs(expr)" function
1251 */
1252 static void
1253f_abs(typval_T *argvars, typval_T *rettv)
1254{
1255 if (argvars[0].v_type == VAR_FLOAT)
1256 {
1257 rettv->v_type = VAR_FLOAT;
1258 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1259 }
1260 else
1261 {
1262 varnumber_T n;
1263 int error = FALSE;
1264
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001265 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001266 if (error)
1267 rettv->vval.v_number = -1;
1268 else if (n > 0)
1269 rettv->vval.v_number = n;
1270 else
1271 rettv->vval.v_number = -n;
1272 }
1273}
1274
1275/*
1276 * "acos()" function
1277 */
1278 static void
1279f_acos(typval_T *argvars, typval_T *rettv)
1280{
1281 float_T f = 0.0;
1282
1283 rettv->v_type = VAR_FLOAT;
1284 if (get_float_arg(argvars, &f) == OK)
1285 rettv->vval.v_float = acos(f);
1286 else
1287 rettv->vval.v_float = 0.0;
1288}
1289#endif
1290
1291/*
1292 * "add(list, item)" function
1293 */
1294 static void
1295f_add(typval_T *argvars, typval_T *rettv)
1296{
1297 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001298 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001299
1300 rettv->vval.v_number = 1; /* Default: Failed */
1301 if (argvars[0].v_type == VAR_LIST)
1302 {
1303 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001304 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001305 (char_u *)N_("add() argument"), TRUE)
1306 && list_append_tv(l, &argvars[1]) == OK)
1307 copy_tv(&argvars[0], rettv);
1308 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001309 else if (argvars[0].v_type == VAR_BLOB)
1310 {
1311 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001312 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001313 (char_u *)N_("add() argument"), TRUE))
1314 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001315 int error = FALSE;
1316 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1317
1318 if (!error)
1319 {
1320 ga_append(&b->bv_ga, (int)n);
1321 copy_tv(&argvars[0], rettv);
1322 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001323 }
1324 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001325 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001326 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001327}
1328
1329/*
1330 * "and(expr, expr)" function
1331 */
1332 static void
1333f_and(typval_T *argvars, typval_T *rettv)
1334{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001335 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1336 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001337}
1338
1339/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001340 * If there is a window for "curbuf", make it the current window.
1341 */
1342 static void
1343find_win_for_curbuf(void)
1344{
1345 wininfo_T *wip;
1346
1347 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1348 {
1349 if (wip->wi_win != NULL)
1350 {
1351 curwin = wip->wi_win;
1352 break;
1353 }
1354 }
1355}
1356
1357/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001358 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001359 */
1360 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001361set_buffer_lines(
1362 buf_T *buf,
1363 linenr_T lnum_arg,
1364 int append,
1365 typval_T *lines,
1366 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001367{
Bram Moolenaarca851592018-06-06 21:04:07 +02001368 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1369 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370 list_T *l = NULL;
1371 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 linenr_T append_lnum;
1374 buf_T *curbuf_save = NULL;
1375 win_T *curwin_save = NULL;
1376 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 /* When using the current buffer ml_mfp will be set if needed. Useful when
1379 * setline() is used on startup. For other buffers the buffer must be
1380 * loaded. */
1381 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001382 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001383 rettv->vval.v_number = 1; /* FAIL */
1384 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385 }
1386
Bram Moolenaarca851592018-06-06 21:04:07 +02001387 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001389 curbuf_save = curbuf;
1390 curwin_save = curwin;
1391 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001392 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001393 }
1394
1395 if (append)
1396 // appendbufline() uses the line number below which we insert
1397 append_lnum = lnum - 1;
1398 else
1399 // setbufline() uses the line number above which we insert, we only
1400 // append if it's below the last line
1401 append_lnum = curbuf->b_ml.ml_line_count;
1402
1403 if (lines->v_type == VAR_LIST)
1404 {
1405 l = lines->vval.v_list;
1406 li = l->lv_first;
1407 }
1408 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001409 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001410
1411 /* default result is zero == OK */
1412 for (;;)
1413 {
1414 if (l != NULL)
1415 {
1416 /* list argument, get next string */
1417 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001418 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001419 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001420 li = li->li_next;
1421 }
1422
Bram Moolenaarca851592018-06-06 21:04:07 +02001423 rettv->vval.v_number = 1; /* FAIL */
1424 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1425 break;
1426
1427 /* When coming here from Insert mode, sync undo, so that this can be
1428 * undone separately from what was previously inserted. */
1429 if (u_sync_once == 2)
1430 {
1431 u_sync_once = 1; /* notify that u_sync() was called */
1432 u_sync(TRUE);
1433 }
1434
1435 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1436 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001437 // Existing line, replace it.
1438 // Removes any existing text properties.
1439 if (u_savesub(lnum) == OK && ml_replace_len(
1440 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001441 {
1442 changed_bytes(lnum, 0);
1443 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1444 check_cursor_col();
1445 rettv->vval.v_number = 0; /* OK */
1446 }
1447 }
1448 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1449 {
1450 /* append the line */
1451 ++added;
1452 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1453 rettv->vval.v_number = 0; /* OK */
1454 }
1455
1456 if (l == NULL) /* only one string argument */
1457 break;
1458 ++lnum;
1459 }
1460
1461 if (added > 0)
1462 {
1463 win_T *wp;
1464 tabpage_T *tp;
1465
1466 appended_lines_mark(append_lnum, added);
1467 FOR_ALL_TAB_WINDOWS(tp, wp)
1468 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1469 wp->w_cursor.lnum += added;
1470 check_cursor_col();
1471
Bram Moolenaarf2732452018-06-03 14:47:35 +02001472#ifdef FEAT_JOB_CHANNEL
1473 if (bt_prompt(curbuf) && (State & INSERT))
1474 // show the line with the prompt
1475 update_topline();
1476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001477 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001478
1479 if (!is_curbuf)
1480 {
1481 curbuf = curbuf_save;
1482 curwin = curwin_save;
1483 }
1484}
1485
1486/*
1487 * "append(lnum, string/list)" function
1488 */
1489 static void
1490f_append(typval_T *argvars, typval_T *rettv)
1491{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001492 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001493
1494 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1495}
1496
1497/*
1498 * "appendbufline(buf, lnum, string/list)" function
1499 */
1500 static void
1501f_appendbufline(typval_T *argvars, typval_T *rettv)
1502{
1503 linenr_T lnum;
1504 buf_T *buf;
1505
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001506 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001507 if (buf == NULL)
1508 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001509 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001510 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001511 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001512 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1513 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514}
1515
1516/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001517 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001518 */
1519 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001520f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 win_T *wp;
1523
1524 if (argvars[0].v_type == VAR_UNKNOWN)
1525 // use the current window
1526 rettv->vval.v_number = ARGCOUNT;
1527 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001528 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001529 // use the global argument list
1530 rettv->vval.v_number = GARGCOUNT;
1531 else
1532 {
1533 // use the argument list of the specified window
1534 wp = find_win_by_nr_or_id(&argvars[0]);
1535 if (wp != NULL)
1536 rettv->vval.v_number = WARGCOUNT(wp);
1537 else
1538 rettv->vval.v_number = -1;
1539 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001540}
1541
1542/*
1543 * "argidx()" function
1544 */
1545 static void
1546f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1547{
1548 rettv->vval.v_number = curwin->w_arg_idx;
1549}
1550
1551/*
1552 * "arglistid()" function
1553 */
1554 static void
1555f_arglistid(typval_T *argvars, typval_T *rettv)
1556{
1557 win_T *wp;
1558
1559 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001560 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001561 if (wp != NULL)
1562 rettv->vval.v_number = wp->w_alist->id;
1563}
1564
1565/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001566 * Get the argument list for a given window
1567 */
1568 static void
1569get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1570{
1571 int idx;
1572
1573 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1574 for (idx = 0; idx < argcount; ++idx)
1575 list_append_string(rettv->vval.v_list,
1576 alist_name(&arglist[idx]), -1);
1577}
1578
1579/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001580 * "argv(nr)" function
1581 */
1582 static void
1583f_argv(typval_T *argvars, typval_T *rettv)
1584{
1585 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001586 aentry_T *arglist = NULL;
1587 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001588
1589 if (argvars[0].v_type != VAR_UNKNOWN)
1590 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 if (argvars[1].v_type == VAR_UNKNOWN)
1592 {
1593 arglist = ARGLIST;
1594 argcount = ARGCOUNT;
1595 }
1596 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001597 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001598 {
1599 arglist = GARGLIST;
1600 argcount = GARGCOUNT;
1601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001602 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 {
1604 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1605
1606 if (wp != NULL)
1607 {
1608 /* Use the argument list of the specified window */
1609 arglist = WARGLIST(wp);
1610 argcount = WARGCOUNT(wp);
1611 }
1612 }
1613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001615 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001616 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001617 if (arglist != NULL && idx >= 0 && idx < argcount)
1618 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1619 else if (idx == -1)
1620 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001621 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001622 else
1623 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624}
1625
1626/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001627 * "assert_beeps(cmd [, error])" function
1628 */
1629 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001630f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001631{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001633}
1634
1635/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001636 * "assert_equal(expected, actual[, msg])" function
1637 */
1638 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001639f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001640{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001642}
1643
1644/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001645 * "assert_equalfile(fname-one, fname-two)" function
1646 */
1647 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001648f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001649{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001651}
1652
1653/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001654 * "assert_notequal(expected, actual[, msg])" function
1655 */
1656 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001657f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001658{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660}
1661
1662/*
1663 * "assert_exception(string[, msg])" function
1664 */
1665 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001666f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001667{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669}
1670
1671/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001672 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001673 */
1674 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001675f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678}
1679
1680/*
1681 * "assert_false(actual[, msg])" function
1682 */
1683 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001684f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001685{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001687}
1688
1689/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001690 * "assert_inrange(lower, upper[, msg])" function
1691 */
1692 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001693f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001694{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001696}
1697
1698/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001699 * "assert_match(pattern, actual[, msg])" function
1700 */
1701 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001702f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001703{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705}
1706
1707/*
1708 * "assert_notmatch(pattern, actual[, msg])" function
1709 */
1710 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001711f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001712{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001714}
1715
1716/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001717 * "assert_report(msg)" function
1718 */
1719 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001720f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001721{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001723}
1724
1725/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001726 * "assert_true(actual[, msg])" function
1727 */
1728 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001729f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001730{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001731 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001732}
1733
1734#ifdef FEAT_FLOAT
1735/*
1736 * "asin()" function
1737 */
1738 static void
1739f_asin(typval_T *argvars, typval_T *rettv)
1740{
1741 float_T f = 0.0;
1742
1743 rettv->v_type = VAR_FLOAT;
1744 if (get_float_arg(argvars, &f) == OK)
1745 rettv->vval.v_float = asin(f);
1746 else
1747 rettv->vval.v_float = 0.0;
1748}
1749
1750/*
1751 * "atan()" function
1752 */
1753 static void
1754f_atan(typval_T *argvars, typval_T *rettv)
1755{
1756 float_T f = 0.0;
1757
1758 rettv->v_type = VAR_FLOAT;
1759 if (get_float_arg(argvars, &f) == OK)
1760 rettv->vval.v_float = atan(f);
1761 else
1762 rettv->vval.v_float = 0.0;
1763}
1764
1765/*
1766 * "atan2()" function
1767 */
1768 static void
1769f_atan2(typval_T *argvars, typval_T *rettv)
1770{
1771 float_T fx = 0.0, fy = 0.0;
1772
1773 rettv->v_type = VAR_FLOAT;
1774 if (get_float_arg(argvars, &fx) == OK
1775 && get_float_arg(&argvars[1], &fy) == OK)
1776 rettv->vval.v_float = atan2(fx, fy);
1777 else
1778 rettv->vval.v_float = 0.0;
1779}
1780#endif
1781
1782/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001783 * "balloon_show()" function
1784 */
1785#ifdef FEAT_BEVAL
1786 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001787f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1788{
1789 rettv->v_type = VAR_STRING;
1790 if (balloonEval != NULL)
1791 {
1792 if (balloonEval->msg == NULL)
1793 rettv->vval.v_string = NULL;
1794 else
1795 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1796 }
1797}
1798
1799 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001800f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1801{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001802 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001803 {
1804 if (argvars[0].v_type == VAR_LIST
1805# ifdef FEAT_GUI
1806 && !gui.in_use
1807# endif
1808 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001809 {
1810 list_T *l = argvars[0].vval.v_list;
1811
1812 // empty list removes the balloon
1813 post_balloon(balloonEval, NULL,
1814 l == NULL || l->lv_len == 0 ? NULL : l);
1815 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001816 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001817 {
1818 char_u *mesg = tv_get_string_chk(&argvars[0]);
1819
1820 if (mesg != NULL)
1821 // empty string removes the balloon
1822 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1823 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001824 }
1825}
1826
Bram Moolenaar669a8282017-11-19 20:13:05 +01001827# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001828 static void
1829f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1830{
1831 if (rettv_list_alloc(rettv) == OK)
1832 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001833 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001834
1835 if (msg != NULL)
1836 {
1837 pumitem_T *array;
1838 int size = split_message(msg, &array);
1839 int i;
1840
1841 /* Skip the first and last item, they are always empty. */
1842 for (i = 1; i < size - 1; ++i)
1843 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001844 while (size > 0)
1845 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001846 vim_free(array);
1847 }
1848 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001849}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001850# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001851#endif
1852
1853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001854 * "browse(save, title, initdir, default)" function
1855 */
1856 static void
1857f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1858{
1859#ifdef FEAT_BROWSE
1860 int save;
1861 char_u *title;
1862 char_u *initdir;
1863 char_u *defname;
1864 char_u buf[NUMBUFLEN];
1865 char_u buf2[NUMBUFLEN];
1866 int error = FALSE;
1867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001868 save = (int)tv_get_number_chk(&argvars[0], &error);
1869 title = tv_get_string_chk(&argvars[1]);
1870 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1871 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001872
1873 if (error || title == NULL || initdir == NULL || defname == NULL)
1874 rettv->vval.v_string = NULL;
1875 else
1876 rettv->vval.v_string =
1877 do_browse(save ? BROWSE_SAVE : 0,
1878 title, defname, NULL, initdir, NULL, curbuf);
1879#else
1880 rettv->vval.v_string = NULL;
1881#endif
1882 rettv->v_type = VAR_STRING;
1883}
1884
1885/*
1886 * "browsedir(title, initdir)" function
1887 */
1888 static void
1889f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1890{
1891#ifdef FEAT_BROWSE
1892 char_u *title;
1893 char_u *initdir;
1894 char_u buf[NUMBUFLEN];
1895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001896 title = tv_get_string_chk(&argvars[0]);
1897 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001898
1899 if (title == NULL || initdir == NULL)
1900 rettv->vval.v_string = NULL;
1901 else
1902 rettv->vval.v_string = do_browse(BROWSE_DIR,
1903 title, NULL, NULL, initdir, NULL, curbuf);
1904#else
1905 rettv->vval.v_string = NULL;
1906#endif
1907 rettv->v_type = VAR_STRING;
1908}
1909
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001910/*
1911 * Find a buffer by number or exact name.
1912 */
1913 static buf_T *
1914find_buffer(typval_T *avar)
1915{
1916 buf_T *buf = NULL;
1917
1918 if (avar->v_type == VAR_NUMBER)
1919 buf = buflist_findnr((int)avar->vval.v_number);
1920 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1921 {
1922 buf = buflist_findname_exp(avar->vval.v_string);
1923 if (buf == NULL)
1924 {
1925 /* No full path name match, try a match with a URL or a "nofile"
1926 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001927 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001928 if (buf->b_fname != NULL
1929 && (path_with_url(buf->b_fname)
1930#ifdef FEAT_QUICKFIX
1931 || bt_nofile(buf)
1932#endif
1933 )
1934 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1935 break;
1936 }
1937 }
1938 return buf;
1939}
1940
1941/*
1942 * "bufexists(expr)" function
1943 */
1944 static void
1945f_bufexists(typval_T *argvars, typval_T *rettv)
1946{
1947 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1948}
1949
1950/*
1951 * "buflisted(expr)" function
1952 */
1953 static void
1954f_buflisted(typval_T *argvars, typval_T *rettv)
1955{
1956 buf_T *buf;
1957
1958 buf = find_buffer(&argvars[0]);
1959 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1960}
1961
1962/*
1963 * "bufloaded(expr)" function
1964 */
1965 static void
1966f_bufloaded(typval_T *argvars, typval_T *rettv)
1967{
1968 buf_T *buf;
1969
1970 buf = find_buffer(&argvars[0]);
1971 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1972}
1973
1974 buf_T *
1975buflist_find_by_name(char_u *name, int curtab_only)
1976{
1977 int save_magic;
1978 char_u *save_cpo;
1979 buf_T *buf;
1980
1981 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1982 save_magic = p_magic;
1983 p_magic = TRUE;
1984 save_cpo = p_cpo;
1985 p_cpo = (char_u *)"";
1986
1987 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1988 TRUE, FALSE, curtab_only));
1989
1990 p_magic = save_magic;
1991 p_cpo = save_cpo;
1992 return buf;
1993}
1994
1995/*
1996 * Get buffer by number or pattern.
1997 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001998 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001999tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002000{
2001 char_u *name = tv->vval.v_string;
2002 buf_T *buf;
2003
2004 if (tv->v_type == VAR_NUMBER)
2005 return buflist_findnr((int)tv->vval.v_number);
2006 if (tv->v_type != VAR_STRING)
2007 return NULL;
2008 if (name == NULL || *name == NUL)
2009 return curbuf;
2010 if (name[0] == '$' && name[1] == NUL)
2011 return lastbuf;
2012
2013 buf = buflist_find_by_name(name, curtab_only);
2014
2015 /* If not found, try expanding the name, like done for bufexists(). */
2016 if (buf == NULL)
2017 buf = find_buffer(tv);
2018
2019 return buf;
2020}
2021
2022/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002023 * Get the buffer from "arg" and give an error and return NULL if it is not
2024 * valid.
2025 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002026 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002027get_buf_arg(typval_T *arg)
2028{
2029 buf_T *buf;
2030
2031 ++emsg_off;
2032 buf = tv_get_buf(arg, FALSE);
2033 --emsg_off;
2034 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002036 return buf;
2037}
2038
2039/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002040 * "bufname(expr)" function
2041 */
2042 static void
2043f_bufname(typval_T *argvars, typval_T *rettv)
2044{
2045 buf_T *buf;
2046
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002047 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002048 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002049 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002050 rettv->v_type = VAR_STRING;
2051 if (buf != NULL && buf->b_fname != NULL)
2052 rettv->vval.v_string = vim_strsave(buf->b_fname);
2053 else
2054 rettv->vval.v_string = NULL;
2055 --emsg_off;
2056}
2057
2058/*
2059 * "bufnr(expr)" function
2060 */
2061 static void
2062f_bufnr(typval_T *argvars, typval_T *rettv)
2063{
2064 buf_T *buf;
2065 int error = FALSE;
2066 char_u *name;
2067
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002068 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002069 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002070 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 --emsg_off;
2072
2073 /* If the buffer isn't found and the second argument is not zero create a
2074 * new buffer. */
2075 if (buf == NULL
2076 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002077 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002078 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002079 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002080 && !error)
2081 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2082
2083 if (buf != NULL)
2084 rettv->vval.v_number = buf->b_fnum;
2085 else
2086 rettv->vval.v_number = -1;
2087}
2088
2089 static void
2090buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2091{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002092 win_T *wp;
2093 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002094 buf_T *buf;
2095
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002096 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002098 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002099 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 {
2101 ++winnr;
2102 if (wp->w_buffer == buf)
2103 break;
2104 }
2105 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002106 --emsg_off;
2107}
2108
2109/*
2110 * "bufwinid(nr)" function
2111 */
2112 static void
2113f_bufwinid(typval_T *argvars, typval_T *rettv)
2114{
2115 buf_win_common(argvars, rettv, FALSE);
2116}
2117
2118/*
2119 * "bufwinnr(nr)" function
2120 */
2121 static void
2122f_bufwinnr(typval_T *argvars, typval_T *rettv)
2123{
2124 buf_win_common(argvars, rettv, TRUE);
2125}
2126
2127/*
2128 * "byte2line(byte)" function
2129 */
2130 static void
2131f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2132{
2133#ifndef FEAT_BYTEOFF
2134 rettv->vval.v_number = -1;
2135#else
2136 long boff = 0;
2137
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002138 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002139 if (boff < 0)
2140 rettv->vval.v_number = -1;
2141 else
2142 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2143 (linenr_T)0, &boff);
2144#endif
2145}
2146
2147 static void
2148byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2149{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002150 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002151 char_u *str;
2152 varnumber_T idx;
2153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002154 str = tv_get_string_chk(&argvars[0]);
2155 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 rettv->vval.v_number = -1;
2157 if (str == NULL || idx < 0)
2158 return;
2159
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002160 t = str;
2161 for ( ; idx > 0; idx--)
2162 {
2163 if (*t == NUL) /* EOL reached */
2164 return;
2165 if (enc_utf8 && comp)
2166 t += utf_ptr2len(t);
2167 else
2168 t += (*mb_ptr2len)(t);
2169 }
2170 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002171}
2172
2173/*
2174 * "byteidx()" function
2175 */
2176 static void
2177f_byteidx(typval_T *argvars, typval_T *rettv)
2178{
2179 byteidx(argvars, rettv, FALSE);
2180}
2181
2182/*
2183 * "byteidxcomp()" function
2184 */
2185 static void
2186f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2187{
2188 byteidx(argvars, rettv, TRUE);
2189}
2190
2191/*
2192 * "call(func, arglist [, dict])" function
2193 */
2194 static void
2195f_call(typval_T *argvars, typval_T *rettv)
2196{
2197 char_u *func;
2198 partial_T *partial = NULL;
2199 dict_T *selfdict = NULL;
2200
2201 if (argvars[1].v_type != VAR_LIST)
2202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002203 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002204 return;
2205 }
2206 if (argvars[1].vval.v_list == NULL)
2207 return;
2208
2209 if (argvars[0].v_type == VAR_FUNC)
2210 func = argvars[0].vval.v_string;
2211 else if (argvars[0].v_type == VAR_PARTIAL)
2212 {
2213 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002214 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002215 }
2216 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 if (*func == NUL)
2219 return; /* type error or empty name */
2220
2221 if (argvars[2].v_type != VAR_UNKNOWN)
2222 {
2223 if (argvars[2].v_type != VAR_DICT)
2224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002225 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002226 return;
2227 }
2228 selfdict = argvars[2].vval.v_dict;
2229 }
2230
2231 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2232}
2233
2234#ifdef FEAT_FLOAT
2235/*
2236 * "ceil({float})" function
2237 */
2238 static void
2239f_ceil(typval_T *argvars, typval_T *rettv)
2240{
2241 float_T f = 0.0;
2242
2243 rettv->v_type = VAR_FLOAT;
2244 if (get_float_arg(argvars, &f) == OK)
2245 rettv->vval.v_float = ceil(f);
2246 else
2247 rettv->vval.v_float = 0.0;
2248}
2249#endif
2250
2251#ifdef FEAT_JOB_CHANNEL
2252/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002253 * "ch_canread()" function
2254 */
2255 static void
2256f_ch_canread(typval_T *argvars, typval_T *rettv)
2257{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002258 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002259
2260 rettv->vval.v_number = 0;
2261 if (channel != NULL)
2262 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2263 || channel_has_readahead(channel, PART_OUT)
2264 || channel_has_readahead(channel, PART_ERR);
2265}
2266
2267/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002268 * "ch_close()" function
2269 */
2270 static void
2271f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2272{
2273 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2274
2275 if (channel != NULL)
2276 {
2277 channel_close(channel, FALSE);
2278 channel_clear(channel);
2279 }
2280}
2281
2282/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002283 * "ch_close()" function
2284 */
2285 static void
2286f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2287{
2288 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2289
2290 if (channel != NULL)
2291 channel_close_in(channel);
2292}
2293
2294/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002295 * "ch_getbufnr()" function
2296 */
2297 static void
2298f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2299{
2300 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2301
2302 rettv->vval.v_number = -1;
2303 if (channel != NULL)
2304 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002305 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002306 int part;
2307
2308 if (STRCMP(what, "err") == 0)
2309 part = PART_ERR;
2310 else if (STRCMP(what, "out") == 0)
2311 part = PART_OUT;
2312 else if (STRCMP(what, "in") == 0)
2313 part = PART_IN;
2314 else
2315 part = PART_SOCK;
2316 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2317 rettv->vval.v_number =
2318 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2319 }
2320}
2321
2322/*
2323 * "ch_getjob()" function
2324 */
2325 static void
2326f_ch_getjob(typval_T *argvars, typval_T *rettv)
2327{
2328 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2329
2330 if (channel != NULL)
2331 {
2332 rettv->v_type = VAR_JOB;
2333 rettv->vval.v_job = channel->ch_job;
2334 if (channel->ch_job != NULL)
2335 ++channel->ch_job->jv_refcount;
2336 }
2337}
2338
2339/*
2340 * "ch_info()" function
2341 */
2342 static void
2343f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2344{
2345 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2346
2347 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2348 channel_info(channel, rettv->vval.v_dict);
2349}
2350
2351/*
2352 * "ch_log()" function
2353 */
2354 static void
2355f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2356{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002357 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002358 channel_T *channel = NULL;
2359
2360 if (argvars[1].v_type != VAR_UNKNOWN)
2361 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2362
Bram Moolenaard5359b22018-04-05 22:44:39 +02002363 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002364}
2365
2366/*
2367 * "ch_logfile()" function
2368 */
2369 static void
2370f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2371{
2372 char_u *fname;
2373 char_u *opt = (char_u *)"";
2374 char_u buf[NUMBUFLEN];
2375
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002376 /* Don't open a file in restricted mode. */
2377 if (check_restricted() || check_secure())
2378 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002379 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002380 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002381 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002382 ch_logfile(fname, opt);
2383}
2384
2385/*
2386 * "ch_open()" function
2387 */
2388 static void
2389f_ch_open(typval_T *argvars, typval_T *rettv)
2390{
2391 rettv->v_type = VAR_CHANNEL;
2392 if (check_restricted() || check_secure())
2393 return;
2394 rettv->vval.v_channel = channel_open_func(argvars);
2395}
2396
2397/*
2398 * "ch_read()" function
2399 */
2400 static void
2401f_ch_read(typval_T *argvars, typval_T *rettv)
2402{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002403 common_channel_read(argvars, rettv, FALSE, FALSE);
2404}
2405
2406/*
2407 * "ch_readblob()" function
2408 */
2409 static void
2410f_ch_readblob(typval_T *argvars, typval_T *rettv)
2411{
2412 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002413}
2414
2415/*
2416 * "ch_readraw()" function
2417 */
2418 static void
2419f_ch_readraw(typval_T *argvars, typval_T *rettv)
2420{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002421 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002422}
2423
2424/*
2425 * "ch_evalexpr()" function
2426 */
2427 static void
2428f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2429{
2430 ch_expr_common(argvars, rettv, TRUE);
2431}
2432
2433/*
2434 * "ch_sendexpr()" function
2435 */
2436 static void
2437f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2438{
2439 ch_expr_common(argvars, rettv, FALSE);
2440}
2441
2442/*
2443 * "ch_evalraw()" function
2444 */
2445 static void
2446f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2447{
2448 ch_raw_common(argvars, rettv, TRUE);
2449}
2450
2451/*
2452 * "ch_sendraw()" function
2453 */
2454 static void
2455f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2456{
2457 ch_raw_common(argvars, rettv, FALSE);
2458}
2459
2460/*
2461 * "ch_setoptions()" function
2462 */
2463 static void
2464f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2465{
2466 channel_T *channel;
2467 jobopt_T opt;
2468
2469 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2470 if (channel == NULL)
2471 return;
2472 clear_job_options(&opt);
2473 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002474 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002475 channel_set_options(channel, &opt);
2476 free_job_options(&opt);
2477}
2478
2479/*
2480 * "ch_status()" function
2481 */
2482 static void
2483f_ch_status(typval_T *argvars, typval_T *rettv)
2484{
2485 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002486 jobopt_T opt;
2487 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002488
2489 /* return an empty string by default */
2490 rettv->v_type = VAR_STRING;
2491 rettv->vval.v_string = NULL;
2492
2493 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002494
2495 if (argvars[1].v_type != VAR_UNKNOWN)
2496 {
2497 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002498 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002499 && (opt.jo_set & JO_PART))
2500 part = opt.jo_part;
2501 }
2502
2503 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002504}
2505#endif
2506
2507/*
2508 * "changenr()" function
2509 */
2510 static void
2511f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2512{
2513 rettv->vval.v_number = curbuf->b_u_seq_cur;
2514}
2515
2516/*
2517 * "char2nr(string)" function
2518 */
2519 static void
2520f_char2nr(typval_T *argvars, typval_T *rettv)
2521{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002522 if (has_mbyte)
2523 {
2524 int utf8 = 0;
2525
2526 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002527 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002528
2529 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002530 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533 }
2534 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002535 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536}
2537
2538/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002539 * "chdir(dir)" function
2540 */
2541 static void
2542f_chdir(typval_T *argvars, typval_T *rettv)
2543{
2544 char_u *cwd;
2545 cdscope_T scope = CDSCOPE_GLOBAL;
2546
2547 rettv->v_type = VAR_STRING;
2548 rettv->vval.v_string = NULL;
2549
2550 if (argvars[0].v_type != VAR_STRING)
2551 return;
2552
2553 // Return the current directory
2554 cwd = alloc(MAXPATHL);
2555 if (cwd != NULL)
2556 {
2557 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2558 {
2559#ifdef BACKSLASH_IN_FILENAME
2560 slash_adjust(cwd);
2561#endif
2562 rettv->vval.v_string = vim_strsave(cwd);
2563 }
2564 vim_free(cwd);
2565 }
2566
2567 if (curwin->w_localdir != NULL)
2568 scope = CDSCOPE_WINDOW;
2569 else if (curtab->tp_localdir != NULL)
2570 scope = CDSCOPE_TABPAGE;
2571
2572 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2573 // Directory change failed
2574 VIM_CLEAR(rettv->vval.v_string);
2575}
2576
2577/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002578 * "cindent(lnum)" function
2579 */
2580 static void
2581f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2582{
2583#ifdef FEAT_CINDENT
2584 pos_T pos;
2585 linenr_T lnum;
2586
2587 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002588 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002589 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2590 {
2591 curwin->w_cursor.lnum = lnum;
2592 rettv->vval.v_number = get_c_indent();
2593 curwin->w_cursor = pos;
2594 }
2595 else
2596#endif
2597 rettv->vval.v_number = -1;
2598}
2599
Bram Moolenaaraff74912019-03-30 18:11:49 +01002600 static win_T *
2601get_optional_window(typval_T *argvars, int idx)
2602{
2603 win_T *win = curwin;
2604
2605 if (argvars[idx].v_type != VAR_UNKNOWN)
2606 {
2607 win = find_win_by_nr_or_id(&argvars[idx]);
2608 if (win == NULL)
2609 {
2610 emsg(_(e_invalwindow));
2611 return NULL;
2612 }
2613 }
2614 return win;
2615}
2616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002617/*
2618 * "clearmatches()" function
2619 */
2620 static void
2621f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2622{
2623#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002624 win_T *win = get_optional_window(argvars, 0);
2625
2626 if (win != NULL)
2627 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002628#endif
2629}
2630
2631/*
2632 * "col(string)" function
2633 */
2634 static void
2635f_col(typval_T *argvars, typval_T *rettv)
2636{
2637 colnr_T col = 0;
2638 pos_T *fp;
2639 int fnum = curbuf->b_fnum;
2640
2641 fp = var2fpos(&argvars[0], FALSE, &fnum);
2642 if (fp != NULL && fnum == curbuf->b_fnum)
2643 {
2644 if (fp->col == MAXCOL)
2645 {
2646 /* '> can be MAXCOL, get the length of the line then */
2647 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2648 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2649 else
2650 col = MAXCOL;
2651 }
2652 else
2653 {
2654 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002655 /* col(".") when the cursor is on the NUL at the end of the line
2656 * because of "coladd" can be seen as an extra column. */
2657 if (virtual_active() && fp == &curwin->w_cursor)
2658 {
2659 char_u *p = ml_get_cursor();
2660
2661 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2662 curwin->w_virtcol - curwin->w_cursor.coladd))
2663 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002664 int l;
2665
2666 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2667 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002668 }
2669 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002670 }
2671 }
2672 rettv->vval.v_number = col;
2673}
2674
2675#if defined(FEAT_INS_EXPAND)
2676/*
2677 * "complete()" function
2678 */
2679 static void
2680f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2681{
2682 int startcol;
2683
2684 if ((State & INSERT) == 0)
2685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002686 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002687 return;
2688 }
2689
2690 /* Check for undo allowed here, because if something was already inserted
2691 * the line was already saved for undo and this check isn't done. */
2692 if (!undo_allowed())
2693 return;
2694
2695 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002697 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002698 return;
2699 }
2700
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002701 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002702 if (startcol <= 0)
2703 return;
2704
2705 set_completion(startcol - 1, argvars[1].vval.v_list);
2706}
2707
2708/*
2709 * "complete_add()" function
2710 */
2711 static void
2712f_complete_add(typval_T *argvars, typval_T *rettv)
2713{
2714 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2715}
2716
2717/*
2718 * "complete_check()" function
2719 */
2720 static void
2721f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2722{
2723 int saved = RedrawingDisabled;
2724
2725 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002726 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002727 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002728 RedrawingDisabled = saved;
2729}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002730
2731/*
2732 * "complete_info()" function
2733 */
2734 static void
2735f_complete_info(typval_T *argvars, typval_T *rettv)
2736{
2737 list_T *what_list = NULL;
2738
2739 if (rettv_dict_alloc(rettv) != OK)
2740 return;
2741
2742 if (argvars[0].v_type != VAR_UNKNOWN)
2743 {
2744 if (argvars[0].v_type != VAR_LIST)
2745 {
2746 emsg(_(e_listreq));
2747 return;
2748 }
2749 what_list = argvars[0].vval.v_list;
2750 }
2751 get_complete_info(what_list, rettv->vval.v_dict);
2752}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002753#endif
2754
2755/*
2756 * "confirm(message, buttons[, default [, type]])" function
2757 */
2758 static void
2759f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2760{
2761#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2762 char_u *message;
2763 char_u *buttons = NULL;
2764 char_u buf[NUMBUFLEN];
2765 char_u buf2[NUMBUFLEN];
2766 int def = 1;
2767 int type = VIM_GENERIC;
2768 char_u *typestr;
2769 int error = FALSE;
2770
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002771 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002772 if (message == NULL)
2773 error = TRUE;
2774 if (argvars[1].v_type != VAR_UNKNOWN)
2775 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 if (buttons == NULL)
2778 error = TRUE;
2779 if (argvars[2].v_type != VAR_UNKNOWN)
2780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (argvars[3].v_type != VAR_UNKNOWN)
2783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002784 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (typestr == NULL)
2786 error = TRUE;
2787 else
2788 {
2789 switch (TOUPPER_ASC(*typestr))
2790 {
2791 case 'E': type = VIM_ERROR; break;
2792 case 'Q': type = VIM_QUESTION; break;
2793 case 'I': type = VIM_INFO; break;
2794 case 'W': type = VIM_WARNING; break;
2795 case 'G': type = VIM_GENERIC; break;
2796 }
2797 }
2798 }
2799 }
2800 }
2801
2802 if (buttons == NULL || *buttons == NUL)
2803 buttons = (char_u *)_("&Ok");
2804
2805 if (!error)
2806 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2807 def, NULL, FALSE);
2808#endif
2809}
2810
2811/*
2812 * "copy()" function
2813 */
2814 static void
2815f_copy(typval_T *argvars, typval_T *rettv)
2816{
2817 item_copy(&argvars[0], rettv, FALSE, 0);
2818}
2819
2820#ifdef FEAT_FLOAT
2821/*
2822 * "cos()" function
2823 */
2824 static void
2825f_cos(typval_T *argvars, typval_T *rettv)
2826{
2827 float_T f = 0.0;
2828
2829 rettv->v_type = VAR_FLOAT;
2830 if (get_float_arg(argvars, &f) == OK)
2831 rettv->vval.v_float = cos(f);
2832 else
2833 rettv->vval.v_float = 0.0;
2834}
2835
2836/*
2837 * "cosh()" function
2838 */
2839 static void
2840f_cosh(typval_T *argvars, typval_T *rettv)
2841{
2842 float_T f = 0.0;
2843
2844 rettv->v_type = VAR_FLOAT;
2845 if (get_float_arg(argvars, &f) == OK)
2846 rettv->vval.v_float = cosh(f);
2847 else
2848 rettv->vval.v_float = 0.0;
2849}
2850#endif
2851
2852/*
2853 * "count()" function
2854 */
2855 static void
2856f_count(typval_T *argvars, typval_T *rettv)
2857{
2858 long n = 0;
2859 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002860 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002861
Bram Moolenaar9966b212017-07-28 16:46:57 +02002862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002863 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002864
2865 if (argvars[0].v_type == VAR_STRING)
2866 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002867 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002868 char_u *p = argvars[0].vval.v_string;
2869 char_u *next;
2870
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002871 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002872 {
2873 if (ic)
2874 {
2875 size_t len = STRLEN(expr);
2876
2877 while (*p != NUL)
2878 {
2879 if (MB_STRNICMP(p, expr, len) == 0)
2880 {
2881 ++n;
2882 p += len;
2883 }
2884 else
2885 MB_PTR_ADV(p);
2886 }
2887 }
2888 else
2889 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2890 != NULL)
2891 {
2892 ++n;
2893 p = next + STRLEN(expr);
2894 }
2895 }
2896
2897 }
2898 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002899 {
2900 listitem_T *li;
2901 list_T *l;
2902 long idx;
2903
2904 if ((l = argvars[0].vval.v_list) != NULL)
2905 {
2906 li = l->lv_first;
2907 if (argvars[2].v_type != VAR_UNKNOWN)
2908 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002909 if (argvars[3].v_type != VAR_UNKNOWN)
2910 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002911 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912 if (!error)
2913 {
2914 li = list_find(l, idx);
2915 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002916 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 }
2918 }
2919 if (error)
2920 li = NULL;
2921 }
2922
2923 for ( ; li != NULL; li = li->li_next)
2924 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2925 ++n;
2926 }
2927 }
2928 else if (argvars[0].v_type == VAR_DICT)
2929 {
2930 int todo;
2931 dict_T *d;
2932 hashitem_T *hi;
2933
2934 if ((d = argvars[0].vval.v_dict) != NULL)
2935 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002936 if (argvars[2].v_type != VAR_UNKNOWN)
2937 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002938 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002939 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002940 }
2941
2942 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2943 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2944 {
2945 if (!HASHITEM_EMPTY(hi))
2946 {
2947 --todo;
2948 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2949 ++n;
2950 }
2951 }
2952 }
2953 }
2954 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002956 rettv->vval.v_number = n;
2957}
2958
2959/*
2960 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2961 *
2962 * Checks the existence of a cscope connection.
2963 */
2964 static void
2965f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2966{
2967#ifdef FEAT_CSCOPE
2968 int num = 0;
2969 char_u *dbpath = NULL;
2970 char_u *prepend = NULL;
2971 char_u buf[NUMBUFLEN];
2972
2973 if (argvars[0].v_type != VAR_UNKNOWN
2974 && argvars[1].v_type != VAR_UNKNOWN)
2975 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002976 num = (int)tv_get_number(&argvars[0]);
2977 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002980 }
2981
2982 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2983#endif
2984}
2985
2986/*
2987 * "cursor(lnum, col)" function, or
2988 * "cursor(list)"
2989 *
2990 * Moves the cursor to the specified line and column.
2991 * Returns 0 when the position could be set, -1 otherwise.
2992 */
2993 static void
2994f_cursor(typval_T *argvars, typval_T *rettv)
2995{
2996 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002997 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002998 int set_curswant = TRUE;
2999
3000 rettv->vval.v_number = -1;
3001 if (argvars[1].v_type == VAR_UNKNOWN)
3002 {
3003 pos_T pos;
3004 colnr_T curswant = -1;
3005
3006 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003009 return;
3010 }
3011 line = pos.lnum;
3012 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003013 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 if (curswant >= 0)
3015 {
3016 curwin->w_curswant = curswant - 1;
3017 set_curswant = FALSE;
3018 }
3019 }
3020 else
3021 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003022 line = tv_get_lnum(argvars);
3023 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003024 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003025 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003027 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003028 return; /* type error; errmsg already given */
3029 if (line > 0)
3030 curwin->w_cursor.lnum = line;
3031 if (col > 0)
3032 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003034
3035 /* Make sure the cursor is in a valid position. */
3036 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037 /* Correct cursor for multi-byte character. */
3038 if (has_mbyte)
3039 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040
3041 curwin->w_set_curswant = set_curswant;
3042 rettv->vval.v_number = 0;
3043}
3044
Bram Moolenaar4f974752019-02-17 17:44:42 +01003045#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003046/*
3047 * "debugbreak()" function
3048 */
3049 static void
3050f_debugbreak(typval_T *argvars, typval_T *rettv)
3051{
3052 int pid;
3053
3054 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003055 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003056 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003057 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003058 else
3059 {
3060 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3061
3062 if (hProcess != NULL)
3063 {
3064 DebugBreakProcess(hProcess);
3065 CloseHandle(hProcess);
3066 rettv->vval.v_number = OK;
3067 }
3068 }
3069}
3070#endif
3071
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003072/*
3073 * "deepcopy()" function
3074 */
3075 static void
3076f_deepcopy(typval_T *argvars, typval_T *rettv)
3077{
3078 int noref = 0;
3079 int copyID;
3080
3081 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003082 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003083 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003084 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003085 else
3086 {
3087 copyID = get_copyID();
3088 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3089 }
3090}
3091
3092/*
3093 * "delete()" function
3094 */
3095 static void
3096f_delete(typval_T *argvars, typval_T *rettv)
3097{
3098 char_u nbuf[NUMBUFLEN];
3099 char_u *name;
3100 char_u *flags;
3101
3102 rettv->vval.v_number = -1;
3103 if (check_restricted() || check_secure())
3104 return;
3105
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003106 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003107 if (name == NULL || *name == NUL)
3108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003109 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 return;
3111 }
3112
3113 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003114 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 else
3116 flags = (char_u *)"";
3117
3118 if (*flags == NUL)
3119 /* delete a file */
3120 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3121 else if (STRCMP(flags, "d") == 0)
3122 /* delete an empty directory */
3123 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3124 else if (STRCMP(flags, "rf") == 0)
3125 /* delete a directory recursively */
3126 rettv->vval.v_number = delete_recursive(name);
3127 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003128 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003129}
3130
3131/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003132 * "deletebufline()" function
3133 */
3134 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003135f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003136{
3137 buf_T *buf;
3138 linenr_T first, last;
3139 linenr_T lnum;
3140 long count;
3141 int is_curbuf;
3142 buf_T *curbuf_save = NULL;
3143 win_T *curwin_save = NULL;
3144 tabpage_T *tp;
3145 win_T *wp;
3146
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003147 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003148 if (buf == NULL)
3149 {
3150 rettv->vval.v_number = 1; /* FAIL */
3151 return;
3152 }
3153 is_curbuf = buf == curbuf;
3154
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003155 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003157 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003158 else
3159 last = first;
3160
3161 if (buf->b_ml.ml_mfp == NULL || first < 1
3162 || first > buf->b_ml.ml_line_count || last < first)
3163 {
3164 rettv->vval.v_number = 1; /* FAIL */
3165 return;
3166 }
3167
3168 if (!is_curbuf)
3169 {
3170 curbuf_save = curbuf;
3171 curwin_save = curwin;
3172 curbuf = buf;
3173 find_win_for_curbuf();
3174 }
3175 if (last > curbuf->b_ml.ml_line_count)
3176 last = curbuf->b_ml.ml_line_count;
3177 count = last - first + 1;
3178
3179 // When coming here from Insert mode, sync undo, so that this can be
3180 // undone separately from what was previously inserted.
3181 if (u_sync_once == 2)
3182 {
3183 u_sync_once = 1; // notify that u_sync() was called
3184 u_sync(TRUE);
3185 }
3186
3187 if (u_save(first - 1, last + 1) == FAIL)
3188 {
3189 rettv->vval.v_number = 1; /* FAIL */
3190 return;
3191 }
3192
3193 for (lnum = first; lnum <= last; ++lnum)
3194 ml_delete(first, TRUE);
3195
3196 FOR_ALL_TAB_WINDOWS(tp, wp)
3197 if (wp->w_buffer == buf)
3198 {
3199 if (wp->w_cursor.lnum > last)
3200 wp->w_cursor.lnum -= count;
3201 else if (wp->w_cursor.lnum> first)
3202 wp->w_cursor.lnum = first;
3203 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3204 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3205 }
3206 check_cursor_col();
3207 deleted_lines_mark(first, count);
3208
3209 if (!is_curbuf)
3210 {
3211 curbuf = curbuf_save;
3212 curwin = curwin_save;
3213 }
3214}
3215
3216/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003217 * "did_filetype()" function
3218 */
3219 static void
3220f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3221{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003223}
3224
3225/*
3226 * "diff_filler()" function
3227 */
3228 static void
3229f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3230{
3231#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003232 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003233#endif
3234}
3235
3236/*
3237 * "diff_hlID()" function
3238 */
3239 static void
3240f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3241{
3242#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003243 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003244 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003245 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003246 static int fnum = 0;
3247 static int change_start = 0;
3248 static int change_end = 0;
3249 static hlf_T hlID = (hlf_T)0;
3250 int filler_lines;
3251 int col;
3252
3253 if (lnum < 0) /* ignore type error in {lnum} arg */
3254 lnum = 0;
3255 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003256 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003257 || fnum != curbuf->b_fnum)
3258 {
3259 /* New line, buffer, change: need to get the values. */
3260 filler_lines = diff_check(curwin, lnum);
3261 if (filler_lines < 0)
3262 {
3263 if (filler_lines == -1)
3264 {
3265 change_start = MAXCOL;
3266 change_end = -1;
3267 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3268 hlID = HLF_ADD; /* added line */
3269 else
3270 hlID = HLF_CHD; /* changed line */
3271 }
3272 else
3273 hlID = HLF_ADD; /* added line */
3274 }
3275 else
3276 hlID = (hlf_T)0;
3277 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003278 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003279 fnum = curbuf->b_fnum;
3280 }
3281
3282 if (hlID == HLF_CHD || hlID == HLF_TXD)
3283 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003284 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003285 if (col >= change_start && col <= change_end)
3286 hlID = HLF_TXD; /* changed text */
3287 else
3288 hlID = HLF_CHD; /* changed line */
3289 }
3290 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3291#endif
3292}
3293
3294/*
3295 * "empty({expr})" function
3296 */
3297 static void
3298f_empty(typval_T *argvars, typval_T *rettv)
3299{
3300 int n = FALSE;
3301
3302 switch (argvars[0].v_type)
3303 {
3304 case VAR_STRING:
3305 case VAR_FUNC:
3306 n = argvars[0].vval.v_string == NULL
3307 || *argvars[0].vval.v_string == NUL;
3308 break;
3309 case VAR_PARTIAL:
3310 n = FALSE;
3311 break;
3312 case VAR_NUMBER:
3313 n = argvars[0].vval.v_number == 0;
3314 break;
3315 case VAR_FLOAT:
3316#ifdef FEAT_FLOAT
3317 n = argvars[0].vval.v_float == 0.0;
3318 break;
3319#endif
3320 case VAR_LIST:
3321 n = argvars[0].vval.v_list == NULL
3322 || argvars[0].vval.v_list->lv_first == NULL;
3323 break;
3324 case VAR_DICT:
3325 n = argvars[0].vval.v_dict == NULL
3326 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3327 break;
3328 case VAR_SPECIAL:
3329 n = argvars[0].vval.v_number != VVAL_TRUE;
3330 break;
3331
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003332 case VAR_BLOB:
3333 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003334 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3335 break;
3336
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003337 case VAR_JOB:
3338#ifdef FEAT_JOB_CHANNEL
3339 n = argvars[0].vval.v_job == NULL
3340 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3341 break;
3342#endif
3343 case VAR_CHANNEL:
3344#ifdef FEAT_JOB_CHANNEL
3345 n = argvars[0].vval.v_channel == NULL
3346 || !channel_is_open(argvars[0].vval.v_channel);
3347 break;
3348#endif
3349 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003350 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003351 n = TRUE;
3352 break;
3353 }
3354
3355 rettv->vval.v_number = n;
3356}
3357
3358/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003359 * "environ()" function
3360 */
3361 static void
3362f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3363{
3364#if !defined(AMIGA)
3365 int i = 0;
3366 char_u *entry, *value;
3367# ifdef MSWIN
3368 extern wchar_t **_wenviron;
3369# else
3370 extern char **environ;
3371# endif
3372
3373 if (rettv_dict_alloc(rettv) != OK)
3374 return;
3375
3376# ifdef MSWIN
3377 if (*_wenviron == NULL)
3378 return;
3379# else
3380 if (*environ == NULL)
3381 return;
3382# endif
3383
3384 for (i = 0; ; ++i)
3385 {
3386# ifdef MSWIN
3387 short_u *p;
3388
3389 if ((p = (short_u *)_wenviron[i]) == NULL)
3390 return;
3391 entry = utf16_to_enc(p, NULL);
3392# else
3393 if ((entry = (char_u *)environ[i]) == NULL)
3394 return;
3395 entry = vim_strsave(entry);
3396# endif
3397 if (entry == NULL) // out of memory
3398 return;
3399 if ((value = vim_strchr(entry, '=')) == NULL)
3400 {
3401 vim_free(entry);
3402 continue;
3403 }
3404 *value++ = NUL;
3405 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3406 vim_free(entry);
3407 }
3408#endif
3409}
3410
3411/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003412 * "escape({string}, {chars})" function
3413 */
3414 static void
3415f_escape(typval_T *argvars, typval_T *rettv)
3416{
3417 char_u buf[NUMBUFLEN];
3418
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003419 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3420 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003421 rettv->v_type = VAR_STRING;
3422}
3423
3424/*
3425 * "eval()" function
3426 */
3427 static void
3428f_eval(typval_T *argvars, typval_T *rettv)
3429{
3430 char_u *s, *p;
3431
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003432 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003433 if (s != NULL)
3434 s = skipwhite(s);
3435
3436 p = s;
3437 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3438 {
3439 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003440 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003441 need_clr_eos = FALSE;
3442 rettv->v_type = VAR_NUMBER;
3443 rettv->vval.v_number = 0;
3444 }
3445 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003446 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003447}
3448
3449/*
3450 * "eventhandler()" function
3451 */
3452 static void
3453f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3454{
3455 rettv->vval.v_number = vgetc_busy;
3456}
3457
3458/*
3459 * "executable()" function
3460 */
3461 static void
3462f_executable(typval_T *argvars, typval_T *rettv)
3463{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003464 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003465
3466 /* Check in $PATH and also check directly if there is a directory name. */
3467 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3468 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3469}
3470
3471static garray_T redir_execute_ga;
3472
3473/*
3474 * Append "value[value_len]" to the execute() output.
3475 */
3476 void
3477execute_redir_str(char_u *value, int value_len)
3478{
3479 int len;
3480
3481 if (value_len == -1)
3482 len = (int)STRLEN(value); /* Append the entire string */
3483 else
3484 len = value_len; /* Append only "value_len" characters */
3485 if (ga_grow(&redir_execute_ga, len) == OK)
3486 {
3487 mch_memmove((char *)redir_execute_ga.ga_data
3488 + redir_execute_ga.ga_len, value, len);
3489 redir_execute_ga.ga_len += len;
3490 }
3491}
3492
3493/*
3494 * Get next line from a list.
3495 * Called by do_cmdline() to get the next line.
3496 * Returns allocated string, or NULL for end of function.
3497 */
3498
3499 static char_u *
3500get_list_line(
3501 int c UNUSED,
3502 void *cookie,
3503 int indent UNUSED)
3504{
3505 listitem_T **p = (listitem_T **)cookie;
3506 listitem_T *item = *p;
3507 char_u buf[NUMBUFLEN];
3508 char_u *s;
3509
3510 if (item == NULL)
3511 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003512 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003513 *p = item->li_next;
3514 return s == NULL ? NULL : vim_strsave(s);
3515}
3516
3517/*
3518 * "execute()" function
3519 */
3520 static void
3521f_execute(typval_T *argvars, typval_T *rettv)
3522{
3523 char_u *cmd = NULL;
3524 list_T *list = NULL;
3525 int save_msg_silent = msg_silent;
3526 int save_emsg_silent = emsg_silent;
3527 int save_emsg_noredir = emsg_noredir;
3528 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003529 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003530 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003531 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003532 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533
3534 rettv->vval.v_string = NULL;
3535 rettv->v_type = VAR_STRING;
3536
3537 if (argvars[0].v_type == VAR_LIST)
3538 {
3539 list = argvars[0].vval.v_list;
3540 if (list == NULL || list->lv_first == NULL)
3541 /* empty list, no commands, empty output */
3542 return;
3543 ++list->lv_refcount;
3544 }
3545 else
3546 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003547 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003548 if (cmd == NULL)
3549 return;
3550 }
3551
3552 if (argvars[1].v_type != VAR_UNKNOWN)
3553 {
3554 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003555 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556
3557 if (s == NULL)
3558 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003559 if (*s == NUL)
3560 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561 if (STRNCMP(s, "silent", 6) == 0)
3562 ++msg_silent;
3563 if (STRCMP(s, "silent!") == 0)
3564 {
3565 emsg_silent = TRUE;
3566 emsg_noredir = TRUE;
3567 }
3568 }
3569 else
3570 ++msg_silent;
3571
3572 if (redir_execute)
3573 save_ga = redir_execute_ga;
3574 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3575 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003576 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003577 if (!echo_output)
3578 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003579
3580 if (cmd != NULL)
3581 do_cmdline_cmd(cmd);
3582 else
3583 {
3584 listitem_T *item = list->lv_first;
3585
3586 do_cmdline(NULL, get_list_line, (void *)&item,
3587 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3588 --list->lv_refcount;
3589 }
3590
Bram Moolenaard297f352017-01-29 20:31:21 +01003591 /* Need to append a NUL to the result. */
3592 if (ga_grow(&redir_execute_ga, 1) == OK)
3593 {
3594 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3595 rettv->vval.v_string = redir_execute_ga.ga_data;
3596 }
3597 else
3598 {
3599 ga_clear(&redir_execute_ga);
3600 rettv->vval.v_string = NULL;
3601 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003602 msg_silent = save_msg_silent;
3603 emsg_silent = save_emsg_silent;
3604 emsg_noredir = save_emsg_noredir;
3605
3606 redir_execute = save_redir_execute;
3607 if (redir_execute)
3608 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003609 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003610
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003611 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003612 if (echo_output)
3613 // When not working silently: put it in column zero. A following
3614 // "echon" will overwrite the message, unavoidably.
3615 msg_col = 0;
3616 else
3617 // When working silently: Put it back where it was, since nothing
3618 // should have been written.
3619 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003620}
3621
3622/*
3623 * "exepath()" function
3624 */
3625 static void
3626f_exepath(typval_T *argvars, typval_T *rettv)
3627{
3628 char_u *p = NULL;
3629
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003630 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003631 rettv->v_type = VAR_STRING;
3632 rettv->vval.v_string = p;
3633}
3634
3635/*
3636 * "exists()" function
3637 */
3638 static void
3639f_exists(typval_T *argvars, typval_T *rettv)
3640{
3641 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003642 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003644 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 if (*p == '$') /* environment variable */
3646 {
3647 /* first try "normal" environment variables (fast) */
3648 if (mch_getenv(p + 1) != NULL)
3649 n = TRUE;
3650 else
3651 {
3652 /* try expanding things like $VIM and ${HOME} */
3653 p = expand_env_save(p);
3654 if (p != NULL && *p != '$')
3655 n = TRUE;
3656 vim_free(p);
3657 }
3658 }
3659 else if (*p == '&' || *p == '+') /* option */
3660 {
3661 n = (get_option_tv(&p, NULL, TRUE) == OK);
3662 if (*skipwhite(p) != NUL)
3663 n = FALSE; /* trailing garbage */
3664 }
3665 else if (*p == '*') /* internal or user defined function */
3666 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003667 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 }
3669 else if (*p == ':')
3670 {
3671 n = cmd_exists(p + 1);
3672 }
3673 else if (*p == '#')
3674 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003675 if (p[1] == '#')
3676 n = autocmd_supported(p + 2);
3677 else
3678 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003679 }
3680 else /* internal variable */
3681 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003682 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003683 }
3684
3685 rettv->vval.v_number = n;
3686}
3687
3688#ifdef FEAT_FLOAT
3689/*
3690 * "exp()" function
3691 */
3692 static void
3693f_exp(typval_T *argvars, typval_T *rettv)
3694{
3695 float_T f = 0.0;
3696
3697 rettv->v_type = VAR_FLOAT;
3698 if (get_float_arg(argvars, &f) == OK)
3699 rettv->vval.v_float = exp(f);
3700 else
3701 rettv->vval.v_float = 0.0;
3702}
3703#endif
3704
3705/*
3706 * "expand()" function
3707 */
3708 static void
3709f_expand(typval_T *argvars, typval_T *rettv)
3710{
3711 char_u *s;
3712 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003713 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003714 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3715 expand_T xpc;
3716 int error = FALSE;
3717 char_u *result;
3718
3719 rettv->v_type = VAR_STRING;
3720 if (argvars[1].v_type != VAR_UNKNOWN
3721 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003722 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003723 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003724 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003725
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003726 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003727 if (*s == '%' || *s == '#' || *s == '<')
3728 {
3729 ++emsg_off;
3730 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3731 --emsg_off;
3732 if (rettv->v_type == VAR_LIST)
3733 {
3734 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3735 list_append_string(rettv->vval.v_list, result, -1);
3736 else
3737 vim_free(result);
3738 }
3739 else
3740 rettv->vval.v_string = result;
3741 }
3742 else
3743 {
3744 /* When the optional second argument is non-zero, don't remove matches
3745 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3746 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003747 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003748 options |= WILD_KEEP_ALL;
3749 if (!error)
3750 {
3751 ExpandInit(&xpc);
3752 xpc.xp_context = EXPAND_FILES;
3753 if (p_wic)
3754 options += WILD_ICASE;
3755 if (rettv->v_type == VAR_STRING)
3756 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3757 options, WILD_ALL);
3758 else if (rettv_list_alloc(rettv) != FAIL)
3759 {
3760 int i;
3761
3762 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3763 for (i = 0; i < xpc.xp_numfiles; i++)
3764 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3765 ExpandCleanup(&xpc);
3766 }
3767 }
3768 else
3769 rettv->vval.v_string = NULL;
3770 }
3771}
3772
3773/*
3774 * "extend(list, list [, idx])" function
3775 * "extend(dict, dict [, action])" function
3776 */
3777 static void
3778f_extend(typval_T *argvars, typval_T *rettv)
3779{
3780 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3781
3782 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3783 {
3784 list_T *l1, *l2;
3785 listitem_T *item;
3786 long before;
3787 int error = FALSE;
3788
3789 l1 = argvars[0].vval.v_list;
3790 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003791 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003792 && l2 != NULL)
3793 {
3794 if (argvars[2].v_type != VAR_UNKNOWN)
3795 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003796 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003797 if (error)
3798 return; /* type error; errmsg already given */
3799
3800 if (before == l1->lv_len)
3801 item = NULL;
3802 else
3803 {
3804 item = list_find(l1, before);
3805 if (item == NULL)
3806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003807 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003808 return;
3809 }
3810 }
3811 }
3812 else
3813 item = NULL;
3814 list_extend(l1, l2, item);
3815
3816 copy_tv(&argvars[0], rettv);
3817 }
3818 }
3819 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3820 {
3821 dict_T *d1, *d2;
3822 char_u *action;
3823 int i;
3824
3825 d1 = argvars[0].vval.v_dict;
3826 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003827 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003828 && d2 != NULL)
3829 {
3830 /* Check the third argument. */
3831 if (argvars[2].v_type != VAR_UNKNOWN)
3832 {
3833 static char *(av[]) = {"keep", "force", "error"};
3834
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003835 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003836 if (action == NULL)
3837 return; /* type error; errmsg already given */
3838 for (i = 0; i < 3; ++i)
3839 if (STRCMP(action, av[i]) == 0)
3840 break;
3841 if (i == 3)
3842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003843 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003844 return;
3845 }
3846 }
3847 else
3848 action = (char_u *)"force";
3849
3850 dict_extend(d1, d2, action);
3851
3852 copy_tv(&argvars[0], rettv);
3853 }
3854 }
3855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003857}
3858
3859/*
3860 * "feedkeys()" function
3861 */
3862 static void
3863f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3864{
3865 int remap = TRUE;
3866 int insert = FALSE;
3867 char_u *keys, *flags;
3868 char_u nbuf[NUMBUFLEN];
3869 int typed = FALSE;
3870 int execute = FALSE;
3871 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003872 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003873 char_u *keys_esc;
3874
3875 /* This is not allowed in the sandbox. If the commands would still be
3876 * executed in the sandbox it would be OK, but it probably happens later,
3877 * when "sandbox" is no longer set. */
3878 if (check_secure())
3879 return;
3880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003881 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003882
3883 if (argvars[1].v_type != VAR_UNKNOWN)
3884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003885 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003886 for ( ; *flags != NUL; ++flags)
3887 {
3888 switch (*flags)
3889 {
3890 case 'n': remap = FALSE; break;
3891 case 'm': remap = TRUE; break;
3892 case 't': typed = TRUE; break;
3893 case 'i': insert = TRUE; break;
3894 case 'x': execute = TRUE; break;
3895 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003896 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003897 }
3898 }
3899 }
3900
3901 if (*keys != NUL || execute)
3902 {
3903 /* Need to escape K_SPECIAL and CSI before putting the string in the
3904 * typeahead buffer. */
3905 keys_esc = vim_strsave_escape_csi(keys);
3906 if (keys_esc != NULL)
3907 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003908 if (lowlevel)
3909 {
3910#ifdef USE_INPUT_BUF
3911 add_to_input_buf(keys, (int)STRLEN(keys));
3912#else
3913 emsg(_("E980: lowlevel input not supported"));
3914#endif
3915 }
3916 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003917 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003918 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003919 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003920 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003921#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003922 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003923#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003924 )
3925 typebuf_was_filled = TRUE;
3926 }
3927 vim_free(keys_esc);
3928
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003929 if (execute)
3930 {
3931 int save_msg_scroll = msg_scroll;
3932
3933 /* Avoid a 1 second delay when the keys start Insert mode. */
3934 msg_scroll = FALSE;
3935
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003936 if (!dangerous)
3937 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003938 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003939 if (!dangerous)
3940 --ex_normal_busy;
3941
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003942 msg_scroll |= save_msg_scroll;
3943 }
3944 }
3945 }
3946}
3947
3948/*
3949 * "filereadable()" function
3950 */
3951 static void
3952f_filereadable(typval_T *argvars, typval_T *rettv)
3953{
3954 int fd;
3955 char_u *p;
3956 int n;
3957
3958#ifndef O_NONBLOCK
3959# define O_NONBLOCK 0
3960#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003961 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003962 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3963 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3964 {
3965 n = TRUE;
3966 close(fd);
3967 }
3968 else
3969 n = FALSE;
3970
3971 rettv->vval.v_number = n;
3972}
3973
3974/*
3975 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3976 * rights to write into.
3977 */
3978 static void
3979f_filewritable(typval_T *argvars, typval_T *rettv)
3980{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003981 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003982}
3983
3984 static void
3985findfilendir(
3986 typval_T *argvars UNUSED,
3987 typval_T *rettv,
3988 int find_what UNUSED)
3989{
3990#ifdef FEAT_SEARCHPATH
3991 char_u *fname;
3992 char_u *fresult = NULL;
3993 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3994 char_u *p;
3995 char_u pathbuf[NUMBUFLEN];
3996 int count = 1;
3997 int first = TRUE;
3998 int error = FALSE;
3999#endif
4000
4001 rettv->vval.v_string = NULL;
4002 rettv->v_type = VAR_STRING;
4003
4004#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004005 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004006
4007 if (argvars[1].v_type != VAR_UNKNOWN)
4008 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004009 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004010 if (p == NULL)
4011 error = TRUE;
4012 else
4013 {
4014 if (*p != NUL)
4015 path = p;
4016
4017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004018 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004019 }
4020 }
4021
4022 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4023 error = TRUE;
4024
4025 if (*fname != NUL && !error)
4026 {
4027 do
4028 {
4029 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4030 vim_free(fresult);
4031 fresult = find_file_in_path_option(first ? fname : NULL,
4032 first ? (int)STRLEN(fname) : 0,
4033 0, first, path,
4034 find_what,
4035 curbuf->b_ffname,
4036 find_what == FINDFILE_DIR
4037 ? (char_u *)"" : curbuf->b_p_sua);
4038 first = FALSE;
4039
4040 if (fresult != NULL && rettv->v_type == VAR_LIST)
4041 list_append_string(rettv->vval.v_list, fresult, -1);
4042
4043 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4044 }
4045
4046 if (rettv->v_type == VAR_STRING)
4047 rettv->vval.v_string = fresult;
4048#endif
4049}
4050
4051/*
4052 * "filter()" function
4053 */
4054 static void
4055f_filter(typval_T *argvars, typval_T *rettv)
4056{
4057 filter_map(argvars, rettv, FALSE);
4058}
4059
4060/*
4061 * "finddir({fname}[, {path}[, {count}]])" function
4062 */
4063 static void
4064f_finddir(typval_T *argvars, typval_T *rettv)
4065{
4066 findfilendir(argvars, rettv, FINDFILE_DIR);
4067}
4068
4069/*
4070 * "findfile({fname}[, {path}[, {count}]])" function
4071 */
4072 static void
4073f_findfile(typval_T *argvars, typval_T *rettv)
4074{
4075 findfilendir(argvars, rettv, FINDFILE_FILE);
4076}
4077
4078#ifdef FEAT_FLOAT
4079/*
4080 * "float2nr({float})" function
4081 */
4082 static void
4083f_float2nr(typval_T *argvars, typval_T *rettv)
4084{
4085 float_T f = 0.0;
4086
4087 if (get_float_arg(argvars, &f) == OK)
4088 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004089 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004090 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004091 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004092 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004093 else
4094 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004095 }
4096}
4097
4098/*
4099 * "floor({float})" function
4100 */
4101 static void
4102f_floor(typval_T *argvars, typval_T *rettv)
4103{
4104 float_T f = 0.0;
4105
4106 rettv->v_type = VAR_FLOAT;
4107 if (get_float_arg(argvars, &f) == OK)
4108 rettv->vval.v_float = floor(f);
4109 else
4110 rettv->vval.v_float = 0.0;
4111}
4112
4113/*
4114 * "fmod()" function
4115 */
4116 static void
4117f_fmod(typval_T *argvars, typval_T *rettv)
4118{
4119 float_T fx = 0.0, fy = 0.0;
4120
4121 rettv->v_type = VAR_FLOAT;
4122 if (get_float_arg(argvars, &fx) == OK
4123 && get_float_arg(&argvars[1], &fy) == OK)
4124 rettv->vval.v_float = fmod(fx, fy);
4125 else
4126 rettv->vval.v_float = 0.0;
4127}
4128#endif
4129
4130/*
4131 * "fnameescape({string})" function
4132 */
4133 static void
4134f_fnameescape(typval_T *argvars, typval_T *rettv)
4135{
4136 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004137 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004138 rettv->v_type = VAR_STRING;
4139}
4140
4141/*
4142 * "fnamemodify({fname}, {mods})" function
4143 */
4144 static void
4145f_fnamemodify(typval_T *argvars, typval_T *rettv)
4146{
4147 char_u *fname;
4148 char_u *mods;
4149 int usedlen = 0;
4150 int len;
4151 char_u *fbuf = NULL;
4152 char_u buf[NUMBUFLEN];
4153
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004154 fname = tv_get_string_chk(&argvars[0]);
4155 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004156 if (fname == NULL || mods == NULL)
4157 fname = NULL;
4158 else
4159 {
4160 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004161 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004162 }
4163
4164 rettv->v_type = VAR_STRING;
4165 if (fname == NULL)
4166 rettv->vval.v_string = NULL;
4167 else
4168 rettv->vval.v_string = vim_strnsave(fname, len);
4169 vim_free(fbuf);
4170}
4171
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004172/*
4173 * "foldclosed()" function
4174 */
4175 static void
4176foldclosed_both(
4177 typval_T *argvars UNUSED,
4178 typval_T *rettv,
4179 int end UNUSED)
4180{
4181#ifdef FEAT_FOLDING
4182 linenr_T lnum;
4183 linenr_T first, last;
4184
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004185 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4187 {
4188 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4189 {
4190 if (end)
4191 rettv->vval.v_number = (varnumber_T)last;
4192 else
4193 rettv->vval.v_number = (varnumber_T)first;
4194 return;
4195 }
4196 }
4197#endif
4198 rettv->vval.v_number = -1;
4199}
4200
4201/*
4202 * "foldclosed()" function
4203 */
4204 static void
4205f_foldclosed(typval_T *argvars, typval_T *rettv)
4206{
4207 foldclosed_both(argvars, rettv, FALSE);
4208}
4209
4210/*
4211 * "foldclosedend()" function
4212 */
4213 static void
4214f_foldclosedend(typval_T *argvars, typval_T *rettv)
4215{
4216 foldclosed_both(argvars, rettv, TRUE);
4217}
4218
4219/*
4220 * "foldlevel()" function
4221 */
4222 static void
4223f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4224{
4225#ifdef FEAT_FOLDING
4226 linenr_T lnum;
4227
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004228 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4230 rettv->vval.v_number = foldLevel(lnum);
4231#endif
4232}
4233
4234/*
4235 * "foldtext()" function
4236 */
4237 static void
4238f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4239{
4240#ifdef FEAT_FOLDING
4241 linenr_T foldstart;
4242 linenr_T foldend;
4243 char_u *dashes;
4244 linenr_T lnum;
4245 char_u *s;
4246 char_u *r;
4247 int len;
4248 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004249 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004250#endif
4251
4252 rettv->v_type = VAR_STRING;
4253 rettv->vval.v_string = NULL;
4254#ifdef FEAT_FOLDING
4255 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4256 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4257 dashes = get_vim_var_str(VV_FOLDDASHES);
4258 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4259 && dashes != NULL)
4260 {
4261 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004262 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004263 if (!linewhite(lnum))
4264 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004265
4266 /* Find interesting text in this line. */
4267 s = skipwhite(ml_get(lnum));
4268 /* skip C comment-start */
4269 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4270 {
4271 s = skipwhite(s + 2);
4272 if (*skipwhite(s) == NUL
4273 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4274 {
4275 s = skipwhite(ml_get(lnum + 1));
4276 if (*s == '*')
4277 s = skipwhite(s + 1);
4278 }
4279 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004280 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004281 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004282 r = alloc(STRLEN(txt)
4283 + STRLEN(dashes) // for %s
4284 + 20 // for %3ld
4285 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004286 if (r != NULL)
4287 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004288 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004289 len = (int)STRLEN(r);
4290 STRCAT(r, s);
4291 /* remove 'foldmarker' and 'commentstring' */
4292 foldtext_cleanup(r + len);
4293 rettv->vval.v_string = r;
4294 }
4295 }
4296#endif
4297}
4298
4299/*
4300 * "foldtextresult(lnum)" function
4301 */
4302 static void
4303f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4304{
4305#ifdef FEAT_FOLDING
4306 linenr_T lnum;
4307 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004308 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004309 foldinfo_T foldinfo;
4310 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004311 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004312#endif
4313
4314 rettv->v_type = VAR_STRING;
4315 rettv->vval.v_string = NULL;
4316#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004317 if (entered)
4318 return; /* reject recursive use */
4319 entered = TRUE;
4320
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004321 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004322 /* treat illegal types and illegal string values for {lnum} the same */
4323 if (lnum < 0)
4324 lnum = 0;
4325 fold_count = foldedCount(curwin, lnum, &foldinfo);
4326 if (fold_count > 0)
4327 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004328 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4329 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004330 if (text == buf)
4331 text = vim_strsave(text);
4332 rettv->vval.v_string = text;
4333 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004334
4335 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336#endif
4337}
4338
4339/*
4340 * "foreground()" function
4341 */
4342 static void
4343f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4344{
4345#ifdef FEAT_GUI
4346 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004347 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004349 return;
4350 }
4351#endif
4352#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004353 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004354#endif
4355}
4356
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004357 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004358common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004359{
4360 char_u *s;
4361 char_u *name;
4362 int use_string = FALSE;
4363 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004364 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365
4366 if (argvars[0].v_type == VAR_FUNC)
4367 {
4368 /* function(MyFunc, [arg], dict) */
4369 s = argvars[0].vval.v_string;
4370 }
4371 else if (argvars[0].v_type == VAR_PARTIAL
4372 && argvars[0].vval.v_partial != NULL)
4373 {
4374 /* function(dict.MyFunc, [arg]) */
4375 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004376 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377 }
4378 else
4379 {
4380 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004381 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004382 use_string = TRUE;
4383 }
4384
Bram Moolenaar843b8842016-08-21 14:36:15 +02004385 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004386 {
4387 name = s;
4388 trans_name = trans_function_name(&name, FALSE,
4389 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4390 if (*name != NUL)
4391 s = NULL;
4392 }
4393
Bram Moolenaar843b8842016-08-21 14:36:15 +02004394 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4395 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004397 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004398 else if (trans_name != NULL && (is_funcref
4399 ? find_func(trans_name) == NULL
4400 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004401 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004402 else
4403 {
4404 int dict_idx = 0;
4405 int arg_idx = 0;
4406 list_T *list = NULL;
4407
4408 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4409 {
4410 char sid_buf[25];
4411 int off = *s == 's' ? 2 : 5;
4412
4413 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4414 * also be called from another script. Using trans_function_name()
4415 * would also work, but some plugins depend on the name being
4416 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004417 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004418 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004419 if (name != NULL)
4420 {
4421 STRCPY(name, sid_buf);
4422 STRCAT(name, s + off);
4423 }
4424 }
4425 else
4426 name = vim_strsave(s);
4427
4428 if (argvars[1].v_type != VAR_UNKNOWN)
4429 {
4430 if (argvars[2].v_type != VAR_UNKNOWN)
4431 {
4432 /* function(name, [args], dict) */
4433 arg_idx = 1;
4434 dict_idx = 2;
4435 }
4436 else if (argvars[1].v_type == VAR_DICT)
4437 /* function(name, dict) */
4438 dict_idx = 1;
4439 else
4440 /* function(name, [args]) */
4441 arg_idx = 1;
4442 if (dict_idx > 0)
4443 {
4444 if (argvars[dict_idx].v_type != VAR_DICT)
4445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004446 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004447 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004448 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004449 }
4450 if (argvars[dict_idx].vval.v_dict == NULL)
4451 dict_idx = 0;
4452 }
4453 if (arg_idx > 0)
4454 {
4455 if (argvars[arg_idx].v_type != VAR_LIST)
4456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004457 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004458 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 }
4461 list = argvars[arg_idx].vval.v_list;
4462 if (list == NULL || list->lv_len == 0)
4463 arg_idx = 0;
4464 }
4465 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004466 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004467 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004468 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004469
4470 /* result is a VAR_PARTIAL */
4471 if (pt == NULL)
4472 vim_free(name);
4473 else
4474 {
4475 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4476 {
4477 listitem_T *li;
4478 int i = 0;
4479 int arg_len = 0;
4480 int lv_len = 0;
4481
4482 if (arg_pt != NULL)
4483 arg_len = arg_pt->pt_argc;
4484 if (list != NULL)
4485 lv_len = list->lv_len;
4486 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004487 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004488 if (pt->pt_argv == NULL)
4489 {
4490 vim_free(pt);
4491 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004492 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004493 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004494 for (i = 0; i < arg_len; i++)
4495 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4496 if (lv_len > 0)
4497 for (li = list->lv_first; li != NULL;
4498 li = li->li_next)
4499 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 }
4501
4502 /* For "function(dict.func, [], dict)" and "func" is a partial
4503 * use "dict". That is backwards compatible. */
4504 if (dict_idx > 0)
4505 {
4506 /* The dict is bound explicitly, pt_auto is FALSE. */
4507 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4508 ++pt->pt_dict->dv_refcount;
4509 }
4510 else if (arg_pt != NULL)
4511 {
4512 /* If the dict was bound automatically the result is also
4513 * bound automatically. */
4514 pt->pt_dict = arg_pt->pt_dict;
4515 pt->pt_auto = arg_pt->pt_auto;
4516 if (pt->pt_dict != NULL)
4517 ++pt->pt_dict->dv_refcount;
4518 }
4519
4520 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004521 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4522 {
4523 pt->pt_func = arg_pt->pt_func;
4524 func_ptr_ref(pt->pt_func);
4525 vim_free(name);
4526 }
4527 else if (is_funcref)
4528 {
4529 pt->pt_func = find_func(trans_name);
4530 func_ptr_ref(pt->pt_func);
4531 vim_free(name);
4532 }
4533 else
4534 {
4535 pt->pt_name = name;
4536 func_ref(name);
4537 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004538 }
4539 rettv->v_type = VAR_PARTIAL;
4540 rettv->vval.v_partial = pt;
4541 }
4542 else
4543 {
4544 /* result is a VAR_FUNC */
4545 rettv->v_type = VAR_FUNC;
4546 rettv->vval.v_string = name;
4547 func_ref(name);
4548 }
4549 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004550theend:
4551 vim_free(trans_name);
4552}
4553
4554/*
4555 * "funcref()" function
4556 */
4557 static void
4558f_funcref(typval_T *argvars, typval_T *rettv)
4559{
4560 common_function(argvars, rettv, TRUE);
4561}
4562
4563/*
4564 * "function()" function
4565 */
4566 static void
4567f_function(typval_T *argvars, typval_T *rettv)
4568{
4569 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004570}
4571
4572/*
4573 * "garbagecollect()" function
4574 */
4575 static void
4576f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4577{
4578 /* This is postponed until we are back at the toplevel, because we may be
4579 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4580 want_garbage_collect = TRUE;
4581
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004582 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004583 garbage_collect_at_exit = TRUE;
4584}
4585
4586/*
4587 * "get()" function
4588 */
4589 static void
4590f_get(typval_T *argvars, typval_T *rettv)
4591{
4592 listitem_T *li;
4593 list_T *l;
4594 dictitem_T *di;
4595 dict_T *d;
4596 typval_T *tv = NULL;
4597
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004598 if (argvars[0].v_type == VAR_BLOB)
4599 {
4600 int error = FALSE;
4601 int idx = tv_get_number_chk(&argvars[1], &error);
4602
4603 if (!error)
4604 {
4605 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004606 if (idx < 0)
4607 idx = blob_len(argvars[0].vval.v_blob) + idx;
4608 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4609 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004610 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004611 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004612 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004613 tv = rettv;
4614 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004615 }
4616 }
4617 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 {
4619 if ((l = argvars[0].vval.v_list) != NULL)
4620 {
4621 int error = FALSE;
4622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004623 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004624 if (!error && li != NULL)
4625 tv = &li->li_tv;
4626 }
4627 }
4628 else if (argvars[0].v_type == VAR_DICT)
4629 {
4630 if ((d = argvars[0].vval.v_dict) != NULL)
4631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004632 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004633 if (di != NULL)
4634 tv = &di->di_tv;
4635 }
4636 }
4637 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4638 {
4639 partial_T *pt;
4640 partial_T fref_pt;
4641
4642 if (argvars[0].v_type == VAR_PARTIAL)
4643 pt = argvars[0].vval.v_partial;
4644 else
4645 {
4646 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4647 fref_pt.pt_name = argvars[0].vval.v_string;
4648 pt = &fref_pt;
4649 }
4650
4651 if (pt != NULL)
4652 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004653 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004654 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004655
4656 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4657 {
4658 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004659 n = partial_name(pt);
4660 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004661 rettv->vval.v_string = NULL;
4662 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004663 {
4664 rettv->vval.v_string = vim_strsave(n);
4665 if (rettv->v_type == VAR_FUNC)
4666 func_ref(rettv->vval.v_string);
4667 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004668 }
4669 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004670 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004671 else if (STRCMP(what, "args") == 0)
4672 {
4673 rettv->v_type = VAR_LIST;
4674 if (rettv_list_alloc(rettv) == OK)
4675 {
4676 int i;
4677
4678 for (i = 0; i < pt->pt_argc; ++i)
4679 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4680 }
4681 }
4682 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004684 return;
4685 }
4686 }
4687 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004688 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004689
4690 if (tv == NULL)
4691 {
4692 if (argvars[2].v_type != VAR_UNKNOWN)
4693 copy_tv(&argvars[2], rettv);
4694 }
4695 else
4696 copy_tv(tv, rettv);
4697}
4698
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004699/*
4700 * Returns buffer options, variables and other attributes in a dictionary.
4701 */
4702 static dict_T *
4703get_buffer_info(buf_T *buf)
4704{
4705 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004706 tabpage_T *tp;
4707 win_T *wp;
4708 list_T *windows;
4709
4710 dict = dict_alloc();
4711 if (dict == NULL)
4712 return NULL;
4713
Bram Moolenaare0be1672018-07-08 16:50:37 +02004714 dict_add_number(dict, "bufnr", buf->b_fnum);
4715 dict_add_string(dict, "name", buf->b_ffname);
4716 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4717 : buflist_findlnum(buf));
4718 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4719 dict_add_number(dict, "listed", buf->b_p_bl);
4720 dict_add_number(dict, "changed", bufIsChanged(buf));
4721 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4722 dict_add_number(dict, "hidden",
4723 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004724
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004725 /* Get a reference to buffer variables */
4726 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004727
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004728 /* List of windows displaying this buffer */
4729 windows = list_alloc();
4730 if (windows != NULL)
4731 {
4732 FOR_ALL_TAB_WINDOWS(tp, wp)
4733 if (wp->w_buffer == buf)
4734 list_append_number(windows, (varnumber_T)wp->w_id);
4735 dict_add_list(dict, "windows", windows);
4736 }
4737
4738#ifdef FEAT_SIGNS
4739 if (buf->b_signlist != NULL)
4740 {
4741 /* List of signs placed in this buffer */
4742 list_T *signs = list_alloc();
4743 if (signs != NULL)
4744 {
4745 get_buffer_signs(buf, signs);
4746 dict_add_list(dict, "signs", signs);
4747 }
4748 }
4749#endif
4750
4751 return dict;
4752}
4753
4754/*
4755 * "getbufinfo()" function
4756 */
4757 static void
4758f_getbufinfo(typval_T *argvars, typval_T *rettv)
4759{
4760 buf_T *buf = NULL;
4761 buf_T *argbuf = NULL;
4762 dict_T *d;
4763 int filtered = FALSE;
4764 int sel_buflisted = FALSE;
4765 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004766 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004767
4768 if (rettv_list_alloc(rettv) != OK)
4769 return;
4770
4771 /* List of all the buffers or selected buffers */
4772 if (argvars[0].v_type == VAR_DICT)
4773 {
4774 dict_T *sel_d = argvars[0].vval.v_dict;
4775
4776 if (sel_d != NULL)
4777 {
4778 dictitem_T *di;
4779
4780 filtered = TRUE;
4781
4782 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004783 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004784 sel_buflisted = TRUE;
4785
4786 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004787 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004788 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004789
4790 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004791 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004792 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004793 }
4794 }
4795 else if (argvars[0].v_type != VAR_UNKNOWN)
4796 {
4797 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004798 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004799 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004800 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004801 --emsg_off;
4802 if (argbuf == NULL)
4803 return;
4804 }
4805
4806 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004807 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004808 {
4809 if (argbuf != NULL && argbuf != buf)
4810 continue;
4811 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004812 || (sel_buflisted && !buf->b_p_bl)
4813 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004814 continue;
4815
4816 d = get_buffer_info(buf);
4817 if (d != NULL)
4818 list_append_dict(rettv->vval.v_list, d);
4819 if (argbuf != NULL)
4820 return;
4821 }
4822}
4823
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004824/*
4825 * Get line or list of lines from buffer "buf" into "rettv".
4826 * Return a range (from start to end) of lines in rettv from the specified
4827 * buffer.
4828 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4829 */
4830 static void
4831get_buffer_lines(
4832 buf_T *buf,
4833 linenr_T start,
4834 linenr_T end,
4835 int retlist,
4836 typval_T *rettv)
4837{
4838 char_u *p;
4839
4840 rettv->v_type = VAR_STRING;
4841 rettv->vval.v_string = NULL;
4842 if (retlist && rettv_list_alloc(rettv) == FAIL)
4843 return;
4844
4845 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4846 return;
4847
4848 if (!retlist)
4849 {
4850 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4851 p = ml_get_buf(buf, start, FALSE);
4852 else
4853 p = (char_u *)"";
4854 rettv->vval.v_string = vim_strsave(p);
4855 }
4856 else
4857 {
4858 if (end < start)
4859 return;
4860
4861 if (start < 1)
4862 start = 1;
4863 if (end > buf->b_ml.ml_line_count)
4864 end = buf->b_ml.ml_line_count;
4865 while (start <= end)
4866 if (list_append_string(rettv->vval.v_list,
4867 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4868 break;
4869 }
4870}
4871
4872/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004873 * "getbufline()" function
4874 */
4875 static void
4876f_getbufline(typval_T *argvars, typval_T *rettv)
4877{
4878 linenr_T lnum;
4879 linenr_T end;
4880 buf_T *buf;
4881
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004882 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004883 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004884 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 --emsg_off;
4886
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004887 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004888 if (argvars[2].v_type == VAR_UNKNOWN)
4889 end = lnum;
4890 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004891 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004892
4893 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4894}
4895
4896/*
4897 * "getbufvar()" function
4898 */
4899 static void
4900f_getbufvar(typval_T *argvars, typval_T *rettv)
4901{
4902 buf_T *buf;
4903 buf_T *save_curbuf;
4904 char_u *varname;
4905 dictitem_T *v;
4906 int done = FALSE;
4907
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004908 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4909 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004910 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004911 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004912
4913 rettv->v_type = VAR_STRING;
4914 rettv->vval.v_string = NULL;
4915
4916 if (buf != NULL && varname != NULL)
4917 {
4918 /* set curbuf to be our buf, temporarily */
4919 save_curbuf = curbuf;
4920 curbuf = buf;
4921
Bram Moolenaar30567352016-08-27 21:25:44 +02004922 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004923 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004924 if (varname[1] == NUL)
4925 {
4926 /* get all buffer-local options in a dict */
4927 dict_T *opts = get_winbuf_options(TRUE);
4928
4929 if (opts != NULL)
4930 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004931 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004932 done = TRUE;
4933 }
4934 }
4935 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4936 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 done = TRUE;
4938 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004939 else
4940 {
4941 /* Look up the variable. */
4942 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4943 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4944 'b', varname, FALSE);
4945 if (v != NULL)
4946 {
4947 copy_tv(&v->di_tv, rettv);
4948 done = TRUE;
4949 }
4950 }
4951
4952 /* restore previous notion of curbuf */
4953 curbuf = save_curbuf;
4954 }
4955
4956 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4957 /* use the default value */
4958 copy_tv(&argvars[2], rettv);
4959
4960 --emsg_off;
4961}
4962
4963/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004964 * "getchangelist()" function
4965 */
4966 static void
4967f_getchangelist(typval_T *argvars, typval_T *rettv)
4968{
4969#ifdef FEAT_JUMPLIST
4970 buf_T *buf;
4971 int i;
4972 list_T *l;
4973 dict_T *d;
4974#endif
4975
4976 if (rettv_list_alloc(rettv) != OK)
4977 return;
4978
4979#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004980 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004981 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004982 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004983 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004984 if (buf == NULL)
4985 return;
4986
4987 l = list_alloc();
4988 if (l == NULL)
4989 return;
4990
4991 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4992 return;
4993 /*
4994 * The current window change list index tracks only the position in the
4995 * current buffer change list. For other buffers, use the change list
4996 * length as the current index.
4997 */
4998 list_append_number(rettv->vval.v_list,
4999 (varnumber_T)((buf == curwin->w_buffer)
5000 ? curwin->w_changelistidx : buf->b_changelistlen));
5001
5002 for (i = 0; i < buf->b_changelistlen; ++i)
5003 {
5004 if (buf->b_changelist[i].lnum == 0)
5005 continue;
5006 if ((d = dict_alloc()) == NULL)
5007 return;
5008 if (list_append_dict(l, d) == FAIL)
5009 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005010 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5011 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005012 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005013 }
5014#endif
5015}
5016/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005017 * "getchar()" function
5018 */
5019 static void
5020f_getchar(typval_T *argvars, typval_T *rettv)
5021{
5022 varnumber_T n;
5023 int error = FALSE;
5024
Bram Moolenaar84d93902018-09-11 20:10:20 +02005025#ifdef MESSAGE_QUEUE
5026 // vpeekc() used to check for messages, but that caused problems, invoking
5027 // a callback where it was not expected. Some plugins use getchar(1) in a
5028 // loop to await a message, therefore make sure we check for messages here.
5029 parse_queued_messages();
5030#endif
5031
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005032 /* Position the cursor. Needed after a message that ends in a space. */
5033 windgoto(msg_row, msg_col);
5034
5035 ++no_mapping;
5036 ++allow_keys;
5037 for (;;)
5038 {
5039 if (argvars[0].v_type == VAR_UNKNOWN)
5040 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005041 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005042 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005043 /* getchar(1): only check if char avail */
5044 n = vpeekc_any();
5045 else if (error || vpeekc_any() == NUL)
5046 /* illegal argument or getchar(0) and no char avail: return zero */
5047 n = 0;
5048 else
5049 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005050 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005051
5052 if (n == K_IGNORE)
5053 continue;
5054 break;
5055 }
5056 --no_mapping;
5057 --allow_keys;
5058
5059 set_vim_var_nr(VV_MOUSE_WIN, 0);
5060 set_vim_var_nr(VV_MOUSE_WINID, 0);
5061 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5062 set_vim_var_nr(VV_MOUSE_COL, 0);
5063
5064 rettv->vval.v_number = n;
5065 if (IS_SPECIAL(n) || mod_mask != 0)
5066 {
5067 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5068 int i = 0;
5069
5070 /* Turn a special key into three bytes, plus modifier. */
5071 if (mod_mask != 0)
5072 {
5073 temp[i++] = K_SPECIAL;
5074 temp[i++] = KS_MODIFIER;
5075 temp[i++] = mod_mask;
5076 }
5077 if (IS_SPECIAL(n))
5078 {
5079 temp[i++] = K_SPECIAL;
5080 temp[i++] = K_SECOND(n);
5081 temp[i++] = K_THIRD(n);
5082 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005083 else if (has_mbyte)
5084 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005085 else
5086 temp[i++] = n;
5087 temp[i++] = NUL;
5088 rettv->v_type = VAR_STRING;
5089 rettv->vval.v_string = vim_strsave(temp);
5090
5091#ifdef FEAT_MOUSE
5092 if (is_mouse_key(n))
5093 {
5094 int row = mouse_row;
5095 int col = mouse_col;
5096 win_T *win;
5097 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005098 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 int winnr = 1;
5100
5101 if (row >= 0 && col >= 0)
5102 {
5103 /* Find the window at the mouse coordinates and compute the
5104 * text position. */
5105 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005106 if (win == NULL)
5107 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005108 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005109 for (wp = firstwin; wp != win; wp = wp->w_next)
5110 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5112 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5113 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5114 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5115 }
5116 }
5117#endif
5118 }
5119}
5120
5121/*
5122 * "getcharmod()" function
5123 */
5124 static void
5125f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5126{
5127 rettv->vval.v_number = mod_mask;
5128}
5129
5130/*
5131 * "getcharsearch()" function
5132 */
5133 static void
5134f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5135{
5136 if (rettv_dict_alloc(rettv) != FAIL)
5137 {
5138 dict_T *dict = rettv->vval.v_dict;
5139
Bram Moolenaare0be1672018-07-08 16:50:37 +02005140 dict_add_string(dict, "char", last_csearch());
5141 dict_add_number(dict, "forward", last_csearch_forward());
5142 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005143 }
5144}
5145
5146/*
5147 * "getcmdline()" function
5148 */
5149 static void
5150f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5151{
5152 rettv->v_type = VAR_STRING;
5153 rettv->vval.v_string = get_cmdline_str();
5154}
5155
5156/*
5157 * "getcmdpos()" function
5158 */
5159 static void
5160f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5161{
5162 rettv->vval.v_number = get_cmdline_pos() + 1;
5163}
5164
5165/*
5166 * "getcmdtype()" function
5167 */
5168 static void
5169f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5170{
5171 rettv->v_type = VAR_STRING;
5172 rettv->vval.v_string = alloc(2);
5173 if (rettv->vval.v_string != NULL)
5174 {
5175 rettv->vval.v_string[0] = get_cmdline_type();
5176 rettv->vval.v_string[1] = NUL;
5177 }
5178}
5179
5180/*
5181 * "getcmdwintype()" function
5182 */
5183 static void
5184f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5185{
5186 rettv->v_type = VAR_STRING;
5187 rettv->vval.v_string = NULL;
5188#ifdef FEAT_CMDWIN
5189 rettv->vval.v_string = alloc(2);
5190 if (rettv->vval.v_string != NULL)
5191 {
5192 rettv->vval.v_string[0] = cmdwin_type;
5193 rettv->vval.v_string[1] = NUL;
5194 }
5195#endif
5196}
5197
5198#if defined(FEAT_CMDL_COMPL)
5199/*
5200 * "getcompletion()" function
5201 */
5202 static void
5203f_getcompletion(typval_T *argvars, typval_T *rettv)
5204{
5205 char_u *pat;
5206 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005207 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005208 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5209 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005210
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005211 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005212 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005213
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005214 if (p_wic)
5215 options |= WILD_ICASE;
5216
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005217 /* For filtered results, 'wildignore' is used */
5218 if (!filtered)
5219 options |= WILD_KEEP_ALL;
5220
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005221 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005222 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005224 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225 if (xpc.xp_context == EXPAND_NOTHING)
5226 {
5227 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005228 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005229 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005230 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005231 return;
5232 }
5233
5234# if defined(FEAT_MENU)
5235 if (xpc.xp_context == EXPAND_MENUS)
5236 {
5237 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5238 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5239 }
5240# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005241#ifdef FEAT_CSCOPE
5242 if (xpc.xp_context == EXPAND_CSCOPE)
5243 {
5244 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5245 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5246 }
5247#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005248#ifdef FEAT_SIGNS
5249 if (xpc.xp_context == EXPAND_SIGN)
5250 {
5251 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5252 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5253 }
5254#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005255
5256 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5257 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5258 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005259 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005260
5261 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5262
5263 for (i = 0; i < xpc.xp_numfiles; i++)
5264 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5265 }
5266 vim_free(pat);
5267 ExpandCleanup(&xpc);
5268}
5269#endif
5270
5271/*
5272 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005273 *
5274 * Return the current working directory of a window in a tab page.
5275 * First optional argument 'winnr' is the window number or -1 and the second
5276 * optional argument 'tabnr' is the tab page number.
5277 *
5278 * If no arguments are supplied, then return the directory of the current
5279 * window.
5280 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5281 * the specified window.
5282 * If 'winnr' is 0 then return the directory of the current window.
5283 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5284 * directory of the specified tab page. Otherwise return the directory of the
5285 * specified window in the specified tab page.
5286 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 */
5288 static void
5289f_getcwd(typval_T *argvars, typval_T *rettv)
5290{
5291 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005292 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005293 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005294 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005295
5296 rettv->v_type = VAR_STRING;
5297 rettv->vval.v_string = NULL;
5298
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005299 if (argvars[0].v_type == VAR_NUMBER
5300 && argvars[0].vval.v_number == -1
5301 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005302 global = TRUE;
5303 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005304 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005305
5306 if (wp != NULL && wp->w_localdir != NULL)
5307 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005308 else if (tp != NULL && tp->tp_localdir != NULL)
5309 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5310 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005311 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005312 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005313 rettv->vval.v_string = vim_strsave(globaldir);
5314 else
5315 {
5316 cwd = alloc(MAXPATHL);
5317 if (cwd != NULL)
5318 {
5319 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5320 rettv->vval.v_string = vim_strsave(cwd);
5321 vim_free(cwd);
5322 }
5323 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005324 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005325#ifdef BACKSLASH_IN_FILENAME
5326 if (rettv->vval.v_string != NULL)
5327 slash_adjust(rettv->vval.v_string);
5328#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005329}
5330
5331/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005332 * "getenv()" function
5333 */
5334 static void
5335f_getenv(typval_T *argvars, typval_T *rettv)
5336{
5337 int mustfree = FALSE;
5338 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5339
5340 if (p == NULL)
5341 {
5342 rettv->v_type = VAR_SPECIAL;
5343 rettv->vval.v_number = VVAL_NULL;
5344 return;
5345 }
5346 if (!mustfree)
5347 p = vim_strsave(p);
5348 rettv->vval.v_string = p;
5349 rettv->v_type = VAR_STRING;
5350}
5351
5352/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005353 * "getfontname()" function
5354 */
5355 static void
5356f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5357{
5358 rettv->v_type = VAR_STRING;
5359 rettv->vval.v_string = NULL;
5360#ifdef FEAT_GUI
5361 if (gui.in_use)
5362 {
5363 GuiFont font;
5364 char_u *name = NULL;
5365
5366 if (argvars[0].v_type == VAR_UNKNOWN)
5367 {
5368 /* Get the "Normal" font. Either the name saved by
5369 * hl_set_font_name() or from the font ID. */
5370 font = gui.norm_font;
5371 name = hl_get_font_name();
5372 }
5373 else
5374 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005375 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005376 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5377 return;
5378 font = gui_mch_get_font(name, FALSE);
5379 if (font == NOFONT)
5380 return; /* Invalid font name, return empty string. */
5381 }
5382 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5383 if (argvars[0].v_type != VAR_UNKNOWN)
5384 gui_mch_free_font(font);
5385 }
5386#endif
5387}
5388
5389/*
5390 * "getfperm({fname})" function
5391 */
5392 static void
5393f_getfperm(typval_T *argvars, typval_T *rettv)
5394{
5395 char_u *fname;
5396 stat_T st;
5397 char_u *perm = NULL;
5398 char_u flags[] = "rwx";
5399 int i;
5400
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005401 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005402
5403 rettv->v_type = VAR_STRING;
5404 if (mch_stat((char *)fname, &st) >= 0)
5405 {
5406 perm = vim_strsave((char_u *)"---------");
5407 if (perm != NULL)
5408 {
5409 for (i = 0; i < 9; i++)
5410 {
5411 if (st.st_mode & (1 << (8 - i)))
5412 perm[i] = flags[i % 3];
5413 }
5414 }
5415 }
5416 rettv->vval.v_string = perm;
5417}
5418
5419/*
5420 * "getfsize({fname})" function
5421 */
5422 static void
5423f_getfsize(typval_T *argvars, typval_T *rettv)
5424{
5425 char_u *fname;
5426 stat_T st;
5427
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005428 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005429
5430 rettv->v_type = VAR_NUMBER;
5431
5432 if (mch_stat((char *)fname, &st) >= 0)
5433 {
5434 if (mch_isdir(fname))
5435 rettv->vval.v_number = 0;
5436 else
5437 {
5438 rettv->vval.v_number = (varnumber_T)st.st_size;
5439
5440 /* non-perfect check for overflow */
5441 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5442 rettv->vval.v_number = -2;
5443 }
5444 }
5445 else
5446 rettv->vval.v_number = -1;
5447}
5448
5449/*
5450 * "getftime({fname})" function
5451 */
5452 static void
5453f_getftime(typval_T *argvars, typval_T *rettv)
5454{
5455 char_u *fname;
5456 stat_T st;
5457
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005458 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005459
5460 if (mch_stat((char *)fname, &st) >= 0)
5461 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5462 else
5463 rettv->vval.v_number = -1;
5464}
5465
5466/*
5467 * "getftype({fname})" function
5468 */
5469 static void
5470f_getftype(typval_T *argvars, typval_T *rettv)
5471{
5472 char_u *fname;
5473 stat_T st;
5474 char_u *type = NULL;
5475 char *t;
5476
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005477 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005478
5479 rettv->v_type = VAR_STRING;
5480 if (mch_lstat((char *)fname, &st) >= 0)
5481 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 if (S_ISREG(st.st_mode))
5483 t = "file";
5484 else if (S_ISDIR(st.st_mode))
5485 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 else if (S_ISLNK(st.st_mode))
5487 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 else if (S_ISBLK(st.st_mode))
5489 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 else if (S_ISCHR(st.st_mode))
5491 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 else if (S_ISFIFO(st.st_mode))
5493 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005495 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 else
5497 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 type = vim_strsave((char_u *)t);
5499 }
5500 rettv->vval.v_string = type;
5501}
5502
5503/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005504 * "getjumplist()" function
5505 */
5506 static void
5507f_getjumplist(typval_T *argvars, typval_T *rettv)
5508{
5509#ifdef FEAT_JUMPLIST
5510 win_T *wp;
5511 int i;
5512 list_T *l;
5513 dict_T *d;
5514#endif
5515
5516 if (rettv_list_alloc(rettv) != OK)
5517 return;
5518
5519#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005520 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005521 if (wp == NULL)
5522 return;
5523
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005524 cleanup_jumplist(wp, TRUE);
5525
Bram Moolenaar4f505882018-02-10 21:06:32 +01005526 l = list_alloc();
5527 if (l == NULL)
5528 return;
5529
5530 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5531 return;
5532 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5533
5534 for (i = 0; i < wp->w_jumplistlen; ++i)
5535 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005536 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5537 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005538 if ((d = dict_alloc()) == NULL)
5539 return;
5540 if (list_append_dict(l, d) == FAIL)
5541 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005542 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5543 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005544 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005545 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005546 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005547 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005548 }
5549#endif
5550}
5551
5552/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005553 * "getline(lnum, [end])" function
5554 */
5555 static void
5556f_getline(typval_T *argvars, typval_T *rettv)
5557{
5558 linenr_T lnum;
5559 linenr_T end;
5560 int retlist;
5561
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005562 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005563 if (argvars[1].v_type == VAR_UNKNOWN)
5564 {
5565 end = 0;
5566 retlist = FALSE;
5567 }
5568 else
5569 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005570 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005571 retlist = TRUE;
5572 }
5573
5574 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5575}
5576
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005577#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005578 static void
5579get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5580{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005581 if (what_arg->v_type == VAR_UNKNOWN)
5582 {
5583 if (rettv_list_alloc(rettv) == OK)
5584 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005585 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005586 }
5587 else
5588 {
5589 if (rettv_dict_alloc(rettv) == OK)
5590 if (is_qf || (wp != NULL))
5591 {
5592 if (what_arg->v_type == VAR_DICT)
5593 {
5594 dict_T *d = what_arg->vval.v_dict;
5595
5596 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005597 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005598 }
5599 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005600 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005601 }
5602 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005603}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005604#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005605
5606/*
5607 * "getloclist()" function
5608 */
5609 static void
5610f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5611{
5612#ifdef FEAT_QUICKFIX
5613 win_T *wp;
5614
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005615 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005616 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5617#endif
5618}
5619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005620/*
5621 * "getmatches()" function
5622 */
5623 static void
5624f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5625{
5626#ifdef FEAT_SEARCH_EXTRA
5627 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005628 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005629 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005630 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631
Bram Moolenaaraff74912019-03-30 18:11:49 +01005632 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5633 return;
5634
5635 cur = win->w_match_head;
5636 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005637 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005638 dict = dict_alloc();
5639 if (dict == NULL)
5640 return;
5641 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005642 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005643 /* match added with matchaddpos() */
5644 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005646 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005647 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005648 list_T *l;
5649
5650 llpos = &cur->pos.pos[i];
5651 if (llpos->lnum == 0)
5652 break;
5653 l = list_alloc();
5654 if (l == NULL)
5655 break;
5656 list_append_number(l, (varnumber_T)llpos->lnum);
5657 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005659 list_append_number(l, (varnumber_T)llpos->col);
5660 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005661 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005662 sprintf(buf, "pos%d", i + 1);
5663 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005664 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005665 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005666 else
5667 {
5668 dict_add_string(dict, "pattern", cur->pattern);
5669 }
5670 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5671 dict_add_number(dict, "priority", (long)cur->priority);
5672 dict_add_number(dict, "id", (long)cur->id);
5673# if defined(FEAT_CONCEAL)
5674 if (cur->conceal_char)
5675 {
5676 char_u buf[MB_MAXBYTES + 1];
5677
5678 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5679 dict_add_string(dict, "conceal", (char_u *)&buf);
5680 }
5681# endif
5682 list_append_dict(rettv->vval.v_list, dict);
5683 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005684 }
5685#endif
5686}
5687
5688/*
5689 * "getpid()" function
5690 */
5691 static void
5692f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5693{
5694 rettv->vval.v_number = mch_get_pid();
5695}
5696
5697 static void
5698getpos_both(
5699 typval_T *argvars,
5700 typval_T *rettv,
5701 int getcurpos)
5702{
5703 pos_T *fp;
5704 list_T *l;
5705 int fnum = -1;
5706
5707 if (rettv_list_alloc(rettv) == OK)
5708 {
5709 l = rettv->vval.v_list;
5710 if (getcurpos)
5711 fp = &curwin->w_cursor;
5712 else
5713 fp = var2fpos(&argvars[0], TRUE, &fnum);
5714 if (fnum != -1)
5715 list_append_number(l, (varnumber_T)fnum);
5716 else
5717 list_append_number(l, (varnumber_T)0);
5718 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5719 : (varnumber_T)0);
5720 list_append_number(l, (fp != NULL)
5721 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5722 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005723 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005724 (varnumber_T)0);
5725 if (getcurpos)
5726 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005727 int save_set_curswant = curwin->w_set_curswant;
5728 colnr_T save_curswant = curwin->w_curswant;
5729 colnr_T save_virtcol = curwin->w_virtcol;
5730
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005731 update_curswant();
5732 list_append_number(l, curwin->w_curswant == MAXCOL ?
5733 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005734
5735 // Do not change "curswant", as it is unexpected that a get
5736 // function has a side effect.
5737 if (save_set_curswant)
5738 {
5739 curwin->w_set_curswant = save_set_curswant;
5740 curwin->w_curswant = save_curswant;
5741 curwin->w_virtcol = save_virtcol;
5742 curwin->w_valid &= ~VALID_VIRTCOL;
5743 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744 }
5745 }
5746 else
5747 rettv->vval.v_number = FALSE;
5748}
5749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005750/*
5751 * "getcurpos()" function
5752 */
5753 static void
5754f_getcurpos(typval_T *argvars, typval_T *rettv)
5755{
5756 getpos_both(argvars, rettv, TRUE);
5757}
5758
5759/*
5760 * "getpos(string)" function
5761 */
5762 static void
5763f_getpos(typval_T *argvars, typval_T *rettv)
5764{
5765 getpos_both(argvars, rettv, FALSE);
5766}
5767
5768/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005769 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770 */
5771 static void
5772f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5773{
5774#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005775 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005776#endif
5777}
5778
5779/*
5780 * "getreg()" function
5781 */
5782 static void
5783f_getreg(typval_T *argvars, typval_T *rettv)
5784{
5785 char_u *strregname;
5786 int regname;
5787 int arg2 = FALSE;
5788 int return_list = FALSE;
5789 int error = FALSE;
5790
5791 if (argvars[0].v_type != VAR_UNKNOWN)
5792 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005793 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 error = strregname == NULL;
5795 if (argvars[1].v_type != VAR_UNKNOWN)
5796 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005797 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005798 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005799 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005800 }
5801 }
5802 else
5803 strregname = get_vim_var_str(VV_REG);
5804
5805 if (error)
5806 return;
5807
5808 regname = (strregname == NULL ? '"' : *strregname);
5809 if (regname == 0)
5810 regname = '"';
5811
5812 if (return_list)
5813 {
5814 rettv->v_type = VAR_LIST;
5815 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5816 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5817 if (rettv->vval.v_list == NULL)
5818 (void)rettv_list_alloc(rettv);
5819 else
5820 ++rettv->vval.v_list->lv_refcount;
5821 }
5822 else
5823 {
5824 rettv->v_type = VAR_STRING;
5825 rettv->vval.v_string = get_reg_contents(regname,
5826 arg2 ? GREG_EXPR_SRC : 0);
5827 }
5828}
5829
5830/*
5831 * "getregtype()" function
5832 */
5833 static void
5834f_getregtype(typval_T *argvars, typval_T *rettv)
5835{
5836 char_u *strregname;
5837 int regname;
5838 char_u buf[NUMBUFLEN + 2];
5839 long reglen = 0;
5840
5841 if (argvars[0].v_type != VAR_UNKNOWN)
5842 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005843 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005844 if (strregname == NULL) /* type error; errmsg already given */
5845 {
5846 rettv->v_type = VAR_STRING;
5847 rettv->vval.v_string = NULL;
5848 return;
5849 }
5850 }
5851 else
5852 /* Default to v:register */
5853 strregname = get_vim_var_str(VV_REG);
5854
5855 regname = (strregname == NULL ? '"' : *strregname);
5856 if (regname == 0)
5857 regname = '"';
5858
5859 buf[0] = NUL;
5860 buf[1] = NUL;
5861 switch (get_reg_type(regname, &reglen))
5862 {
5863 case MLINE: buf[0] = 'V'; break;
5864 case MCHAR: buf[0] = 'v'; break;
5865 case MBLOCK:
5866 buf[0] = Ctrl_V;
5867 sprintf((char *)buf + 1, "%ld", reglen + 1);
5868 break;
5869 }
5870 rettv->v_type = VAR_STRING;
5871 rettv->vval.v_string = vim_strsave(buf);
5872}
5873
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005874/*
5875 * Returns information (variables, options, etc.) about a tab page
5876 * as a dictionary.
5877 */
5878 static dict_T *
5879get_tabpage_info(tabpage_T *tp, int tp_idx)
5880{
5881 win_T *wp;
5882 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005883 list_T *l;
5884
5885 dict = dict_alloc();
5886 if (dict == NULL)
5887 return NULL;
5888
Bram Moolenaare0be1672018-07-08 16:50:37 +02005889 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005890
5891 l = list_alloc();
5892 if (l != NULL)
5893 {
5894 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5895 wp; wp = wp->w_next)
5896 list_append_number(l, (varnumber_T)wp->w_id);
5897 dict_add_list(dict, "windows", l);
5898 }
5899
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005900 /* Make a reference to tabpage variables */
5901 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902
5903 return dict;
5904}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005905
5906/*
5907 * "gettabinfo()" function
5908 */
5909 static void
5910f_gettabinfo(typval_T *argvars, typval_T *rettv)
5911{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005912 tabpage_T *tp, *tparg = NULL;
5913 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005914 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005915
5916 if (rettv_list_alloc(rettv) != OK)
5917 return;
5918
5919 if (argvars[0].v_type != VAR_UNKNOWN)
5920 {
5921 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005922 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 if (tparg == NULL)
5924 return;
5925 }
5926
5927 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005928 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005929 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005930 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005931 if (tparg != NULL && tp != tparg)
5932 continue;
5933 d = get_tabpage_info(tp, tpnr);
5934 if (d != NULL)
5935 list_append_dict(rettv->vval.v_list, d);
5936 if (tparg != NULL)
5937 return;
5938 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005939}
5940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005941/*
5942 * "gettabvar()" function
5943 */
5944 static void
5945f_gettabvar(typval_T *argvars, typval_T *rettv)
5946{
5947 win_T *oldcurwin;
5948 tabpage_T *tp, *oldtabpage;
5949 dictitem_T *v;
5950 char_u *varname;
5951 int done = FALSE;
5952
5953 rettv->v_type = VAR_STRING;
5954 rettv->vval.v_string = NULL;
5955
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005956 varname = tv_get_string_chk(&argvars[1]);
5957 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005958 if (tp != NULL && varname != NULL)
5959 {
5960 /* Set tp to be our tabpage, temporarily. Also set the window to the
5961 * first window in the tabpage, otherwise the window is not valid. */
5962 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005963 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5964 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005965 {
5966 /* look up the variable */
5967 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5968 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5969 if (v != NULL)
5970 {
5971 copy_tv(&v->di_tv, rettv);
5972 done = TRUE;
5973 }
5974 }
5975
5976 /* restore previous notion of curwin */
5977 restore_win(oldcurwin, oldtabpage, TRUE);
5978 }
5979
5980 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5981 copy_tv(&argvars[2], rettv);
5982}
5983
5984/*
5985 * "gettabwinvar()" function
5986 */
5987 static void
5988f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5989{
5990 getwinvar(argvars, rettv, 1);
5991}
5992
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005993/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005994 * "gettagstack()" function
5995 */
5996 static void
5997f_gettagstack(typval_T *argvars, typval_T *rettv)
5998{
5999 win_T *wp = curwin; // default is current window
6000
6001 if (rettv_dict_alloc(rettv) != OK)
6002 return;
6003
6004 if (argvars[0].v_type != VAR_UNKNOWN)
6005 {
6006 wp = find_win_by_nr_or_id(&argvars[0]);
6007 if (wp == NULL)
6008 return;
6009 }
6010
6011 get_tagstack(wp, rettv->vval.v_dict);
6012}
6013
6014/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006015 * Returns information about a window as a dictionary.
6016 */
6017 static dict_T *
6018get_win_info(win_T *wp, short tpnr, short winnr)
6019{
6020 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006021
6022 dict = dict_alloc();
6023 if (dict == NULL)
6024 return NULL;
6025
Bram Moolenaare0be1672018-07-08 16:50:37 +02006026 dict_add_number(dict, "tabnr", tpnr);
6027 dict_add_number(dict, "winnr", winnr);
6028 dict_add_number(dict, "winid", wp->w_id);
6029 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006030 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006031 dict_add_number(dict, "topline", wp->w_topline);
6032 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006033#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006034 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006035#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006036 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006037 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006038 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006039
Bram Moolenaar69905d12017-08-13 18:14:47 +02006040#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006041 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006042#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006043#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006044 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6045 dict_add_number(dict, "loclist",
6046 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006047#endif
6048
Bram Moolenaar30567352016-08-27 21:25:44 +02006049 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006050 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006051
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006052 return dict;
6053}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006054
6055/*
6056 * "getwininfo()" function
6057 */
6058 static void
6059f_getwininfo(typval_T *argvars, typval_T *rettv)
6060{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006061 tabpage_T *tp;
6062 win_T *wp = NULL, *wparg = NULL;
6063 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006064 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006065
6066 if (rettv_list_alloc(rettv) != OK)
6067 return;
6068
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006069 if (argvars[0].v_type != VAR_UNKNOWN)
6070 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006071 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006072 if (wparg == NULL)
6073 return;
6074 }
6075
6076 /* Collect information about either all the windows across all the tab
6077 * pages or one particular window.
6078 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006079 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006080 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006081 tabnr++;
6082 winnr = 0;
6083 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006084 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006085 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006086 if (wparg != NULL && wp != wparg)
6087 continue;
6088 d = get_win_info(wp, tabnr, winnr);
6089 if (d != NULL)
6090 list_append_dict(rettv->vval.v_list, d);
6091 if (wparg != NULL)
6092 /* found information about a specific window */
6093 return;
6094 }
6095 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006096}
6097
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006098/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006099 * "win_findbuf()" function
6100 */
6101 static void
6102f_win_findbuf(typval_T *argvars, typval_T *rettv)
6103{
6104 if (rettv_list_alloc(rettv) != FAIL)
6105 win_findbuf(argvars, rettv->vval.v_list);
6106}
6107
6108/*
6109 * "win_getid()" function
6110 */
6111 static void
6112f_win_getid(typval_T *argvars, typval_T *rettv)
6113{
6114 rettv->vval.v_number = win_getid(argvars);
6115}
6116
6117/*
6118 * "win_gotoid()" function
6119 */
6120 static void
6121f_win_gotoid(typval_T *argvars, typval_T *rettv)
6122{
6123 rettv->vval.v_number = win_gotoid(argvars);
6124}
6125
6126/*
6127 * "win_id2tabwin()" function
6128 */
6129 static void
6130f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6131{
6132 if (rettv_list_alloc(rettv) != FAIL)
6133 win_id2tabwin(argvars, rettv->vval.v_list);
6134}
6135
6136/*
6137 * "win_id2win()" function
6138 */
6139 static void
6140f_win_id2win(typval_T *argvars, typval_T *rettv)
6141{
6142 rettv->vval.v_number = win_id2win(argvars);
6143}
6144
6145/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006146 * "win_screenpos()" function
6147 */
6148 static void
6149f_win_screenpos(typval_T *argvars, typval_T *rettv)
6150{
6151 win_T *wp;
6152
6153 if (rettv_list_alloc(rettv) == FAIL)
6154 return;
6155
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006156 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006157 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6158 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6159}
6160
6161/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006162 * "getwinpos({timeout})" function
6163 */
6164 static void
6165f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6166{
6167 int x = -1;
6168 int y = -1;
6169
6170 if (rettv_list_alloc(rettv) == FAIL)
6171 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006172#if defined(FEAT_GUI) \
6173 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6174 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006175 {
6176 varnumber_T timeout = 100;
6177
6178 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006179 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006180
6181 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006182 }
6183#endif
6184 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6185 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6186}
6187
6188
6189/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006190 * "getwinposx()" function
6191 */
6192 static void
6193f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6194{
6195 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006196#if defined(FEAT_GUI) \
6197 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6198 || defined(MSWIN)
6199
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006200 {
6201 int x, y;
6202
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006203 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006204 rettv->vval.v_number = x;
6205 }
6206#endif
6207}
6208
6209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006210 * "getwinposy()" function
6211 */
6212 static void
6213f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6214{
6215 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006216#if defined(FEAT_GUI) \
6217 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6218 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006219 {
6220 int x, y;
6221
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006222 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006223 rettv->vval.v_number = y;
6224 }
6225#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006226}
6227
6228/*
6229 * "getwinvar()" function
6230 */
6231 static void
6232f_getwinvar(typval_T *argvars, typval_T *rettv)
6233{
6234 getwinvar(argvars, rettv, 0);
6235}
6236
6237/*
6238 * "glob()" function
6239 */
6240 static void
6241f_glob(typval_T *argvars, typval_T *rettv)
6242{
6243 int options = WILD_SILENT|WILD_USE_NL;
6244 expand_T xpc;
6245 int error = FALSE;
6246
6247 /* When the optional second argument is non-zero, don't remove matches
6248 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6249 rettv->v_type = VAR_STRING;
6250 if (argvars[1].v_type != VAR_UNKNOWN)
6251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006252 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006253 options |= WILD_KEEP_ALL;
6254 if (argvars[2].v_type != VAR_UNKNOWN)
6255 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006256 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006257 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006258 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006259 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006260 options |= WILD_ALLLINKS;
6261 }
6262 }
6263 if (!error)
6264 {
6265 ExpandInit(&xpc);
6266 xpc.xp_context = EXPAND_FILES;
6267 if (p_wic)
6268 options += WILD_ICASE;
6269 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 NULL, options, WILD_ALL);
6272 else if (rettv_list_alloc(rettv) != FAIL)
6273 {
6274 int i;
6275
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006276 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006277 NULL, options, WILD_ALL_KEEP);
6278 for (i = 0; i < xpc.xp_numfiles; i++)
6279 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6280
6281 ExpandCleanup(&xpc);
6282 }
6283 }
6284 else
6285 rettv->vval.v_string = NULL;
6286}
6287
6288/*
6289 * "globpath()" function
6290 */
6291 static void
6292f_globpath(typval_T *argvars, typval_T *rettv)
6293{
6294 int flags = 0;
6295 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 int error = FALSE;
6298 garray_T ga;
6299 int i;
6300
6301 /* When the optional second argument is non-zero, don't remove matches
6302 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6303 rettv->v_type = VAR_STRING;
6304 if (argvars[2].v_type != VAR_UNKNOWN)
6305 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006306 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307 flags |= WILD_KEEP_ALL;
6308 if (argvars[3].v_type != VAR_UNKNOWN)
6309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006310 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006311 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006312 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006313 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 flags |= WILD_ALLLINKS;
6315 }
6316 }
6317 if (file != NULL && !error)
6318 {
6319 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006320 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006321 if (rettv->v_type == VAR_STRING)
6322 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6323 else if (rettv_list_alloc(rettv) != FAIL)
6324 for (i = 0; i < ga.ga_len; ++i)
6325 list_append_string(rettv->vval.v_list,
6326 ((char_u **)(ga.ga_data))[i], -1);
6327 ga_clear_strings(&ga);
6328 }
6329 else
6330 rettv->vval.v_string = NULL;
6331}
6332
6333/*
6334 * "glob2regpat()" function
6335 */
6336 static void
6337f_glob2regpat(typval_T *argvars, typval_T *rettv)
6338{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006339 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006340
6341 rettv->v_type = VAR_STRING;
6342 rettv->vval.v_string = (pat == NULL)
6343 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6344}
6345
6346/* for VIM_VERSION_ defines */
6347#include "version.h"
6348
6349/*
6350 * "has()" function
6351 */
6352 static void
6353f_has(typval_T *argvars, typval_T *rettv)
6354{
6355 int i;
6356 char_u *name;
6357 int n = FALSE;
6358 static char *(has_list[]) =
6359 {
6360#ifdef AMIGA
6361 "amiga",
6362# ifdef FEAT_ARP
6363 "arp",
6364# endif
6365#endif
6366#ifdef __BEOS__
6367 "beos",
6368#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006369#if defined(BSD) && !defined(MACOS_X)
6370 "bsd",
6371#endif
6372#ifdef hpux
6373 "hpux",
6374#endif
6375#ifdef __linux__
6376 "linux",
6377#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006378#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006379 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6380 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006381# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006382 "macunix", /* Mac OS X, with the darwin feature */
6383 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006384# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006385#endif
6386#ifdef __QNX__
6387 "qnx",
6388#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006389#ifdef SUN_SYSTEM
6390 "sun",
6391#else
6392 "moon",
6393#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006394#ifdef UNIX
6395 "unix",
6396#endif
6397#ifdef VMS
6398 "vms",
6399#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006400#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "win32",
6402#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006403#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006404 "win32unix",
6405#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006406#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006407 "win64",
6408#endif
6409#ifdef EBCDIC
6410 "ebcdic",
6411#endif
6412#ifndef CASE_INSENSITIVE_FILENAME
6413 "fname_case",
6414#endif
6415#ifdef HAVE_ACL
6416 "acl",
6417#endif
6418#ifdef FEAT_ARABIC
6419 "arabic",
6420#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006422#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006423 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006424#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006425#ifdef FEAT_AUTOSERVERNAME
6426 "autoservername",
6427#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006428#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006429 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006430# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431 "balloon_multiline",
6432# endif
6433#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006434#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006435 "balloon_eval_term",
6436#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6438 "builtin_terms",
6439# ifdef ALL_BUILTIN_TCAPS
6440 "all_builtin_terms",
6441# endif
6442#endif
6443#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006444 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006445 || defined(FEAT_GUI_MOTIF))
6446 "browsefilter",
6447#endif
6448#ifdef FEAT_BYTEOFF
6449 "byte_offset",
6450#endif
6451#ifdef FEAT_JOB_CHANNEL
6452 "channel",
6453#endif
6454#ifdef FEAT_CINDENT
6455 "cindent",
6456#endif
6457#ifdef FEAT_CLIENTSERVER
6458 "clientserver",
6459#endif
6460#ifdef FEAT_CLIPBOARD
6461 "clipboard",
6462#endif
6463#ifdef FEAT_CMDL_COMPL
6464 "cmdline_compl",
6465#endif
6466#ifdef FEAT_CMDHIST
6467 "cmdline_hist",
6468#endif
6469#ifdef FEAT_COMMENTS
6470 "comments",
6471#endif
6472#ifdef FEAT_CONCEAL
6473 "conceal",
6474#endif
6475#ifdef FEAT_CRYPT
6476 "cryptv",
6477 "crypt-blowfish",
6478 "crypt-blowfish2",
6479#endif
6480#ifdef FEAT_CSCOPE
6481 "cscope",
6482#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006483 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006484#ifdef CURSOR_SHAPE
6485 "cursorshape",
6486#endif
6487#ifdef DEBUG
6488 "debug",
6489#endif
6490#ifdef FEAT_CON_DIALOG
6491 "dialog_con",
6492#endif
6493#ifdef FEAT_GUI_DIALOG
6494 "dialog_gui",
6495#endif
6496#ifdef FEAT_DIFF
6497 "diff",
6498#endif
6499#ifdef FEAT_DIGRAPHS
6500 "digraphs",
6501#endif
6502#ifdef FEAT_DIRECTX
6503 "directx",
6504#endif
6505#ifdef FEAT_DND
6506 "dnd",
6507#endif
6508#ifdef FEAT_EMACS_TAGS
6509 "emacs_tags",
6510#endif
6511 "eval", /* always present, of course! */
6512 "ex_extra", /* graduated feature */
6513#ifdef FEAT_SEARCH_EXTRA
6514 "extra_search",
6515#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006516#ifdef FEAT_SEARCHPATH
6517 "file_in_path",
6518#endif
6519#ifdef FEAT_FILTERPIPE
6520 "filterpipe",
6521#endif
6522#ifdef FEAT_FIND_ID
6523 "find_in_path",
6524#endif
6525#ifdef FEAT_FLOAT
6526 "float",
6527#endif
6528#ifdef FEAT_FOLDING
6529 "folding",
6530#endif
6531#ifdef FEAT_FOOTER
6532 "footer",
6533#endif
6534#if !defined(USE_SYSTEM) && defined(UNIX)
6535 "fork",
6536#endif
6537#ifdef FEAT_GETTEXT
6538 "gettext",
6539#endif
6540#ifdef FEAT_GUI
6541 "gui",
6542#endif
6543#ifdef FEAT_GUI_ATHENA
6544# ifdef FEAT_GUI_NEXTAW
6545 "gui_neXtaw",
6546# else
6547 "gui_athena",
6548# endif
6549#endif
6550#ifdef FEAT_GUI_GTK
6551 "gui_gtk",
6552# ifdef USE_GTK3
6553 "gui_gtk3",
6554# else
6555 "gui_gtk2",
6556# endif
6557#endif
6558#ifdef FEAT_GUI_GNOME
6559 "gui_gnome",
6560#endif
6561#ifdef FEAT_GUI_MAC
6562 "gui_mac",
6563#endif
6564#ifdef FEAT_GUI_MOTIF
6565 "gui_motif",
6566#endif
6567#ifdef FEAT_GUI_PHOTON
6568 "gui_photon",
6569#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006570#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006571 "gui_win32",
6572#endif
6573#ifdef FEAT_HANGULIN
6574 "hangul_input",
6575#endif
6576#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6577 "iconv",
6578#endif
6579#ifdef FEAT_INS_EXPAND
6580 "insert_expand",
6581#endif
6582#ifdef FEAT_JOB_CHANNEL
6583 "job",
6584#endif
6585#ifdef FEAT_JUMPLIST
6586 "jumplist",
6587#endif
6588#ifdef FEAT_KEYMAP
6589 "keymap",
6590#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006591 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006592#ifdef FEAT_LANGMAP
6593 "langmap",
6594#endif
6595#ifdef FEAT_LIBCALL
6596 "libcall",
6597#endif
6598#ifdef FEAT_LINEBREAK
6599 "linebreak",
6600#endif
6601#ifdef FEAT_LISP
6602 "lispindent",
6603#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006604 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006605#ifdef FEAT_LOCALMAP
6606 "localmap",
6607#endif
6608#ifdef FEAT_LUA
6609# ifndef DYNAMIC_LUA
6610 "lua",
6611# endif
6612#endif
6613#ifdef FEAT_MENU
6614 "menu",
6615#endif
6616#ifdef FEAT_SESSION
6617 "mksession",
6618#endif
6619#ifdef FEAT_MODIFY_FNAME
6620 "modify_fname",
6621#endif
6622#ifdef FEAT_MOUSE
6623 "mouse",
6624#endif
6625#ifdef FEAT_MOUSESHAPE
6626 "mouseshape",
6627#endif
6628#if defined(UNIX) || defined(VMS)
6629# ifdef FEAT_MOUSE_DEC
6630 "mouse_dec",
6631# endif
6632# ifdef FEAT_MOUSE_GPM
6633 "mouse_gpm",
6634# endif
6635# ifdef FEAT_MOUSE_JSB
6636 "mouse_jsbterm",
6637# endif
6638# ifdef FEAT_MOUSE_NET
6639 "mouse_netterm",
6640# endif
6641# ifdef FEAT_MOUSE_PTERM
6642 "mouse_pterm",
6643# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006644# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006645 "mouse_sgr",
6646# endif
6647# ifdef FEAT_SYSMOUSE
6648 "mouse_sysmouse",
6649# endif
6650# ifdef FEAT_MOUSE_URXVT
6651 "mouse_urxvt",
6652# endif
6653# ifdef FEAT_MOUSE_XTERM
6654 "mouse_xterm",
6655# endif
6656#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006657 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006658#ifdef FEAT_MBYTE_IME
6659 "multi_byte_ime",
6660#endif
6661#ifdef FEAT_MULTI_LANG
6662 "multi_lang",
6663#endif
6664#ifdef FEAT_MZSCHEME
6665#ifndef DYNAMIC_MZSCHEME
6666 "mzscheme",
6667#endif
6668#endif
6669#ifdef FEAT_NUM64
6670 "num64",
6671#endif
6672#ifdef FEAT_OLE
6673 "ole",
6674#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006675#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006676 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006677#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006678#ifdef FEAT_PATH_EXTRA
6679 "path_extra",
6680#endif
6681#ifdef FEAT_PERL
6682#ifndef DYNAMIC_PERL
6683 "perl",
6684#endif
6685#endif
6686#ifdef FEAT_PERSISTENT_UNDO
6687 "persistent_undo",
6688#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006689#if defined(FEAT_PYTHON)
6690 "python_compiled",
6691# if defined(DYNAMIC_PYTHON)
6692 "python_dynamic",
6693# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006695 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006696# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006698#if defined(FEAT_PYTHON3)
6699 "python3_compiled",
6700# if defined(DYNAMIC_PYTHON3)
6701 "python3_dynamic",
6702# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006703 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006704 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006705# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006706#endif
6707#ifdef FEAT_POSTSCRIPT
6708 "postscript",
6709#endif
6710#ifdef FEAT_PRINTER
6711 "printer",
6712#endif
6713#ifdef FEAT_PROFILE
6714 "profile",
6715#endif
6716#ifdef FEAT_RELTIME
6717 "reltime",
6718#endif
6719#ifdef FEAT_QUICKFIX
6720 "quickfix",
6721#endif
6722#ifdef FEAT_RIGHTLEFT
6723 "rightleft",
6724#endif
6725#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6726 "ruby",
6727#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006728 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006729#ifdef FEAT_CMDL_INFO
6730 "showcmd",
6731 "cmdline_info",
6732#endif
6733#ifdef FEAT_SIGNS
6734 "signs",
6735#endif
6736#ifdef FEAT_SMARTINDENT
6737 "smartindent",
6738#endif
6739#ifdef STARTUPTIME
6740 "startuptime",
6741#endif
6742#ifdef FEAT_STL_OPT
6743 "statusline",
6744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006745#ifdef FEAT_NETBEANS_INTG
6746 "netbeans_intg",
6747#endif
6748#ifdef FEAT_SPELL
6749 "spell",
6750#endif
6751#ifdef FEAT_SYN_HL
6752 "syntax",
6753#endif
6754#if defined(USE_SYSTEM) || !defined(UNIX)
6755 "system",
6756#endif
6757#ifdef FEAT_TAG_BINS
6758 "tag_binary",
6759#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006760#ifdef FEAT_TCL
6761# ifndef DYNAMIC_TCL
6762 "tcl",
6763# endif
6764#endif
6765#ifdef FEAT_TERMGUICOLORS
6766 "termguicolors",
6767#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006768#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006769 "terminal",
6770#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006771#ifdef TERMINFO
6772 "terminfo",
6773#endif
6774#ifdef FEAT_TERMRESPONSE
6775 "termresponse",
6776#endif
6777#ifdef FEAT_TEXTOBJ
6778 "textobjects",
6779#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006780#ifdef FEAT_TEXT_PROP
6781 "textprop",
6782#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006783#ifdef HAVE_TGETENT
6784 "tgetent",
6785#endif
6786#ifdef FEAT_TIMERS
6787 "timers",
6788#endif
6789#ifdef FEAT_TITLE
6790 "title",
6791#endif
6792#ifdef FEAT_TOOLBAR
6793 "toolbar",
6794#endif
6795#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6796 "unnamedplus",
6797#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798 "user-commands", /* was accidentally included in 5.4 */
6799 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006800#ifdef FEAT_VARTABS
6801 "vartabs",
6802#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006803 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804#ifdef FEAT_VIMINFO
6805 "viminfo",
6806#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006807 "vimscript-1",
6808 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006809 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006810 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006812 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006813 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006814#ifdef FEAT_VTP
6815 "vtp",
6816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817#ifdef FEAT_WILDIGN
6818 "wildignore",
6819#endif
6820#ifdef FEAT_WILDMENU
6821 "wildmenu",
6822#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006823 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006824#ifdef FEAT_WAK
6825 "winaltkeys",
6826#endif
6827#ifdef FEAT_WRITEBACKUP
6828 "writebackup",
6829#endif
6830#ifdef FEAT_XIM
6831 "xim",
6832#endif
6833#ifdef FEAT_XFONTSET
6834 "xfontset",
6835#endif
6836#ifdef FEAT_XPM_W32
6837 "xpm",
6838 "xpm_w32", /* for backward compatibility */
6839#else
6840# if defined(HAVE_XPM)
6841 "xpm",
6842# endif
6843#endif
6844#ifdef USE_XSMP
6845 "xsmp",
6846#endif
6847#ifdef USE_XSMP_INTERACT
6848 "xsmp_interact",
6849#endif
6850#ifdef FEAT_XCLIPBOARD
6851 "xterm_clipboard",
6852#endif
6853#ifdef FEAT_XTERM_SAVE
6854 "xterm_save",
6855#endif
6856#if defined(UNIX) && defined(FEAT_X11)
6857 "X11",
6858#endif
6859 NULL
6860 };
6861
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006862 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006863 for (i = 0; has_list[i] != NULL; ++i)
6864 if (STRICMP(name, has_list[i]) == 0)
6865 {
6866 n = TRUE;
6867 break;
6868 }
6869
6870 if (n == FALSE)
6871 {
6872 if (STRNICMP(name, "patch", 5) == 0)
6873 {
6874 if (name[5] == '-'
6875 && STRLEN(name) >= 11
6876 && vim_isdigit(name[6])
6877 && vim_isdigit(name[8])
6878 && vim_isdigit(name[10]))
6879 {
6880 int major = atoi((char *)name + 6);
6881 int minor = atoi((char *)name + 8);
6882
6883 /* Expect "patch-9.9.01234". */
6884 n = (major < VIM_VERSION_MAJOR
6885 || (major == VIM_VERSION_MAJOR
6886 && (minor < VIM_VERSION_MINOR
6887 || (minor == VIM_VERSION_MINOR
6888 && has_patch(atoi((char *)name + 10))))));
6889 }
6890 else
6891 n = has_patch(atoi((char *)name + 5));
6892 }
6893 else if (STRICMP(name, "vim_starting") == 0)
6894 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006895 else if (STRICMP(name, "ttyin") == 0)
6896 n = mch_input_isatty();
6897 else if (STRICMP(name, "ttyout") == 0)
6898 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 else if (STRICMP(name, "multi_byte_encoding") == 0)
6900 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006901#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006902 else if (STRICMP(name, "balloon_multiline") == 0)
6903 n = multiline_balloon_available();
6904#endif
6905#ifdef DYNAMIC_TCL
6906 else if (STRICMP(name, "tcl") == 0)
6907 n = tcl_enabled(FALSE);
6908#endif
6909#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6910 else if (STRICMP(name, "iconv") == 0)
6911 n = iconv_enabled(FALSE);
6912#endif
6913#ifdef DYNAMIC_LUA
6914 else if (STRICMP(name, "lua") == 0)
6915 n = lua_enabled(FALSE);
6916#endif
6917#ifdef DYNAMIC_MZSCHEME
6918 else if (STRICMP(name, "mzscheme") == 0)
6919 n = mzscheme_enabled(FALSE);
6920#endif
6921#ifdef DYNAMIC_RUBY
6922 else if (STRICMP(name, "ruby") == 0)
6923 n = ruby_enabled(FALSE);
6924#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006925#ifdef DYNAMIC_PYTHON
6926 else if (STRICMP(name, "python") == 0)
6927 n = python_enabled(FALSE);
6928#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006929#ifdef DYNAMIC_PYTHON3
6930 else if (STRICMP(name, "python3") == 0)
6931 n = python3_enabled(FALSE);
6932#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006933#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6934 else if (STRICMP(name, "pythonx") == 0)
6935 {
6936# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6937 if (p_pyx == 0)
6938 n = python3_enabled(FALSE) || python_enabled(FALSE);
6939 else if (p_pyx == 3)
6940 n = python3_enabled(FALSE);
6941 else if (p_pyx == 2)
6942 n = python_enabled(FALSE);
6943# elif defined(DYNAMIC_PYTHON)
6944 n = python_enabled(FALSE);
6945# elif defined(DYNAMIC_PYTHON3)
6946 n = python3_enabled(FALSE);
6947# endif
6948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006949#endif
6950#ifdef DYNAMIC_PERL
6951 else if (STRICMP(name, "perl") == 0)
6952 n = perl_enabled(FALSE);
6953#endif
6954#ifdef FEAT_GUI
6955 else if (STRICMP(name, "gui_running") == 0)
6956 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006957# ifdef FEAT_BROWSE
6958 else if (STRICMP(name, "browse") == 0)
6959 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6960# endif
6961#endif
6962#ifdef FEAT_SYN_HL
6963 else if (STRICMP(name, "syntax_items") == 0)
6964 n = syntax_present(curwin);
6965#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006966#ifdef FEAT_VTP
6967 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006968 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006969#endif
6970#ifdef FEAT_NETBEANS_INTG
6971 else if (STRICMP(name, "netbeans_enabled") == 0)
6972 n = netbeans_active();
6973#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006974#ifdef FEAT_MOUSE_GPM
6975 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6976 n = gpm_enabled();
6977#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006978#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006979 else if (STRICMP(name, "terminal") == 0)
6980 n = terminal_enabled();
6981#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006982#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006983 else if (STRICMP(name, "conpty") == 0)
6984 n = use_conpty();
6985#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986 }
6987
6988 rettv->vval.v_number = n;
6989}
6990
6991/*
6992 * "has_key()" function
6993 */
6994 static void
6995f_has_key(typval_T *argvars, typval_T *rettv)
6996{
6997 if (argvars[0].v_type != VAR_DICT)
6998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006999 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007000 return;
7001 }
7002 if (argvars[0].vval.v_dict == NULL)
7003 return;
7004
7005 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007006 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007007}
7008
7009/*
7010 * "haslocaldir()" function
7011 */
7012 static void
7013f_haslocaldir(typval_T *argvars, typval_T *rettv)
7014{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007015 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007016 win_T *wp = NULL;
7017
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007018 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7019
7020 // Check for window-local and tab-local directories
7021 if (wp != NULL && wp->w_localdir != NULL)
7022 rettv->vval.v_number = 1;
7023 else if (tp != NULL && tp->tp_localdir != NULL)
7024 rettv->vval.v_number = 2;
7025 else
7026 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007027}
7028
7029/*
7030 * "hasmapto()" function
7031 */
7032 static void
7033f_hasmapto(typval_T *argvars, typval_T *rettv)
7034{
7035 char_u *name;
7036 char_u *mode;
7037 char_u buf[NUMBUFLEN];
7038 int abbr = FALSE;
7039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007040 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007041 if (argvars[1].v_type == VAR_UNKNOWN)
7042 mode = (char_u *)"nvo";
7043 else
7044 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007045 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007046 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007047 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007048 }
7049
7050 if (map_to_exists(name, mode, abbr))
7051 rettv->vval.v_number = TRUE;
7052 else
7053 rettv->vval.v_number = FALSE;
7054}
7055
7056/*
7057 * "histadd()" function
7058 */
7059 static void
7060f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7061{
7062#ifdef FEAT_CMDHIST
7063 int histype;
7064 char_u *str;
7065 char_u buf[NUMBUFLEN];
7066#endif
7067
7068 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007069 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007070 return;
7071#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007072 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007073 histype = str != NULL ? get_histtype(str) : -1;
7074 if (histype >= 0)
7075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007076 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (*str != NUL)
7078 {
7079 init_history();
7080 add_to_history(histype, str, FALSE, NUL);
7081 rettv->vval.v_number = TRUE;
7082 return;
7083 }
7084 }
7085#endif
7086}
7087
7088/*
7089 * "histdel()" function
7090 */
7091 static void
7092f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7093{
7094#ifdef FEAT_CMDHIST
7095 int n;
7096 char_u buf[NUMBUFLEN];
7097 char_u *str;
7098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007099 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007100 if (str == NULL)
7101 n = 0;
7102 else if (argvars[1].v_type == VAR_UNKNOWN)
7103 /* only one argument: clear entire history */
7104 n = clr_history(get_histtype(str));
7105 else if (argvars[1].v_type == VAR_NUMBER)
7106 /* index given: remove that entry */
7107 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007108 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 else
7110 /* string given: remove all matching entries */
7111 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007112 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 rettv->vval.v_number = n;
7114#endif
7115}
7116
7117/*
7118 * "histget()" function
7119 */
7120 static void
7121f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7122{
7123#ifdef FEAT_CMDHIST
7124 int type;
7125 int idx;
7126 char_u *str;
7127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007128 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007129 if (str == NULL)
7130 rettv->vval.v_string = NULL;
7131 else
7132 {
7133 type = get_histtype(str);
7134 if (argvars[1].v_type == VAR_UNKNOWN)
7135 idx = get_history_idx(type);
7136 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007137 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007138 /* -1 on type error */
7139 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7140 }
7141#else
7142 rettv->vval.v_string = NULL;
7143#endif
7144 rettv->v_type = VAR_STRING;
7145}
7146
7147/*
7148 * "histnr()" function
7149 */
7150 static void
7151f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7152{
7153 int i;
7154
7155#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007156 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007157
7158 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7159 if (i >= HIST_CMD && i < HIST_COUNT)
7160 i = get_history_idx(i);
7161 else
7162#endif
7163 i = -1;
7164 rettv->vval.v_number = i;
7165}
7166
7167/*
7168 * "highlightID(name)" function
7169 */
7170 static void
7171f_hlID(typval_T *argvars, typval_T *rettv)
7172{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007173 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174}
7175
7176/*
7177 * "highlight_exists()" function
7178 */
7179 static void
7180f_hlexists(typval_T *argvars, typval_T *rettv)
7181{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007182 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007183}
7184
7185/*
7186 * "hostname()" function
7187 */
7188 static void
7189f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7190{
7191 char_u hostname[256];
7192
7193 mch_get_host_name(hostname, 256);
7194 rettv->v_type = VAR_STRING;
7195 rettv->vval.v_string = vim_strsave(hostname);
7196}
7197
7198/*
7199 * iconv() function
7200 */
7201 static void
7202f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7203{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007204 char_u buf1[NUMBUFLEN];
7205 char_u buf2[NUMBUFLEN];
7206 char_u *from, *to, *str;
7207 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007208
7209 rettv->v_type = VAR_STRING;
7210 rettv->vval.v_string = NULL;
7211
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007212 str = tv_get_string(&argvars[0]);
7213 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7214 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007215 vimconv.vc_type = CONV_NONE;
7216 convert_setup(&vimconv, from, to);
7217
7218 /* If the encodings are equal, no conversion needed. */
7219 if (vimconv.vc_type == CONV_NONE)
7220 rettv->vval.v_string = vim_strsave(str);
7221 else
7222 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7223
7224 convert_setup(&vimconv, NULL, NULL);
7225 vim_free(from);
7226 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007227}
7228
7229/*
7230 * "indent()" function
7231 */
7232 static void
7233f_indent(typval_T *argvars, typval_T *rettv)
7234{
7235 linenr_T lnum;
7236
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007237 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7239 rettv->vval.v_number = get_indent_lnum(lnum);
7240 else
7241 rettv->vval.v_number = -1;
7242}
7243
7244/*
7245 * "index()" function
7246 */
7247 static void
7248f_index(typval_T *argvars, typval_T *rettv)
7249{
7250 list_T *l;
7251 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007252 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007253 long idx = 0;
7254 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007255 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007256
7257 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007258 if (argvars[0].v_type == VAR_BLOB)
7259 {
7260 typval_T tv;
7261 int start = 0;
7262
7263 if (argvars[2].v_type != VAR_UNKNOWN)
7264 {
7265 start = tv_get_number_chk(&argvars[2], &error);
7266 if (error)
7267 return;
7268 }
7269 b = argvars[0].vval.v_blob;
7270 if (b == NULL)
7271 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007272 if (start < 0)
7273 {
7274 start = blob_len(b) + start;
7275 if (start < 0)
7276 start = 0;
7277 }
7278
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007279 for (idx = start; idx < blob_len(b); ++idx)
7280 {
7281 tv.v_type = VAR_NUMBER;
7282 tv.vval.v_number = blob_get(b, idx);
7283 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7284 {
7285 rettv->vval.v_number = idx;
7286 return;
7287 }
7288 }
7289 return;
7290 }
7291 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007293 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007294 return;
7295 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007296
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 l = argvars[0].vval.v_list;
7298 if (l != NULL)
7299 {
7300 item = l->lv_first;
7301 if (argvars[2].v_type != VAR_UNKNOWN)
7302 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 /* Start at specified item. Use the cached index that list_find()
7304 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007305 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007306 idx = l->lv_idx;
7307 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007308 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007309 if (error)
7310 item = NULL;
7311 }
7312
7313 for ( ; item != NULL; item = item->li_next, ++idx)
7314 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7315 {
7316 rettv->vval.v_number = idx;
7317 break;
7318 }
7319 }
7320}
7321
7322static int inputsecret_flag = 0;
7323
7324/*
7325 * "input()" function
7326 * Also handles inputsecret() when inputsecret is set.
7327 */
7328 static void
7329f_input(typval_T *argvars, typval_T *rettv)
7330{
7331 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7332}
7333
7334/*
7335 * "inputdialog()" function
7336 */
7337 static void
7338f_inputdialog(typval_T *argvars, typval_T *rettv)
7339{
7340#if defined(FEAT_GUI_TEXTDIALOG)
7341 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7342 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7343 {
7344 char_u *message;
7345 char_u buf[NUMBUFLEN];
7346 char_u *defstr = (char_u *)"";
7347
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007348 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007349 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007350 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7352 else
7353 IObuff[0] = NUL;
7354 if (message != NULL && defstr != NULL
7355 && do_dialog(VIM_QUESTION, NULL, message,
7356 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7357 rettv->vval.v_string = vim_strsave(IObuff);
7358 else
7359 {
7360 if (message != NULL && defstr != NULL
7361 && argvars[1].v_type != VAR_UNKNOWN
7362 && argvars[2].v_type != VAR_UNKNOWN)
7363 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007364 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007365 else
7366 rettv->vval.v_string = NULL;
7367 }
7368 rettv->v_type = VAR_STRING;
7369 }
7370 else
7371#endif
7372 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7373}
7374
7375/*
7376 * "inputlist()" function
7377 */
7378 static void
7379f_inputlist(typval_T *argvars, typval_T *rettv)
7380{
7381 listitem_T *li;
7382 int selected;
7383 int mouse_used;
7384
7385#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007386 /* While starting up, there is no place to enter text. When running tests
7387 * with --not-a-term we assume feedkeys() will be used. */
7388 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007389 return;
7390#endif
7391 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007393 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007394 return;
7395 }
7396
7397 msg_start();
7398 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7399 lines_left = Rows; /* avoid more prompt */
7400 msg_scroll = TRUE;
7401 msg_clr_eos();
7402
7403 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7404 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007405 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007406 msg_putchar('\n');
7407 }
7408
7409 /* Ask for choice. */
7410 selected = prompt_for_number(&mouse_used);
7411 if (mouse_used)
7412 selected -= lines_left;
7413
7414 rettv->vval.v_number = selected;
7415}
7416
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007417static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7418
7419/*
7420 * "inputrestore()" function
7421 */
7422 static void
7423f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7424{
7425 if (ga_userinput.ga_len > 0)
7426 {
7427 --ga_userinput.ga_len;
7428 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7429 + ga_userinput.ga_len);
7430 /* default return is zero == OK */
7431 }
7432 else if (p_verbose > 1)
7433 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007434 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007435 rettv->vval.v_number = 1; /* Failed */
7436 }
7437}
7438
7439/*
7440 * "inputsave()" function
7441 */
7442 static void
7443f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7444{
7445 /* Add an entry to the stack of typeahead storage. */
7446 if (ga_grow(&ga_userinput, 1) == OK)
7447 {
7448 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7449 + ga_userinput.ga_len);
7450 ++ga_userinput.ga_len;
7451 /* default return is zero == OK */
7452 }
7453 else
7454 rettv->vval.v_number = 1; /* Failed */
7455}
7456
7457/*
7458 * "inputsecret()" function
7459 */
7460 static void
7461f_inputsecret(typval_T *argvars, typval_T *rettv)
7462{
7463 ++cmdline_star;
7464 ++inputsecret_flag;
7465 f_input(argvars, rettv);
7466 --cmdline_star;
7467 --inputsecret_flag;
7468}
7469
7470/*
7471 * "insert()" function
7472 */
7473 static void
7474f_insert(typval_T *argvars, typval_T *rettv)
7475{
7476 long before = 0;
7477 listitem_T *item;
7478 list_T *l;
7479 int error = FALSE;
7480
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007481 if (argvars[0].v_type == VAR_BLOB)
7482 {
7483 int val, len;
7484 char_u *p;
7485
7486 len = blob_len(argvars[0].vval.v_blob);
7487 if (argvars[2].v_type != VAR_UNKNOWN)
7488 {
7489 before = (long)tv_get_number_chk(&argvars[2], &error);
7490 if (error)
7491 return; // type error; errmsg already given
7492 if (before < 0 || before > len)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007495 return;
7496 }
7497 }
7498 val = tv_get_number_chk(&argvars[1], &error);
7499 if (error)
7500 return;
7501 if (val < 0 || val > 255)
7502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007504 return;
7505 }
7506
7507 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7508 return;
7509 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7510 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7511 *(p + before) = val;
7512 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7513
7514 copy_tv(&argvars[0], rettv);
7515 }
7516 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007517 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007518 else if ((l = argvars[0].vval.v_list) != NULL
7519 && !var_check_lock(l->lv_lock,
7520 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007521 {
7522 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007523 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007524 if (error)
7525 return; /* type error; errmsg already given */
7526
7527 if (before == l->lv_len)
7528 item = NULL;
7529 else
7530 {
7531 item = list_find(l, before);
7532 if (item == NULL)
7533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007535 l = NULL;
7536 }
7537 }
7538 if (l != NULL)
7539 {
7540 list_insert_tv(l, &argvars[1], item);
7541 copy_tv(&argvars[0], rettv);
7542 }
7543 }
7544}
7545
7546/*
7547 * "invert(expr)" function
7548 */
7549 static void
7550f_invert(typval_T *argvars, typval_T *rettv)
7551{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007552 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007553}
7554
7555/*
7556 * "isdirectory()" function
7557 */
7558 static void
7559f_isdirectory(typval_T *argvars, typval_T *rettv)
7560{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007561 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007562}
7563
7564/*
7565 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7566 * or it refers to a List or Dictionary that is locked.
7567 */
7568 static int
7569tv_islocked(typval_T *tv)
7570{
7571 return (tv->v_lock & VAR_LOCKED)
7572 || (tv->v_type == VAR_LIST
7573 && tv->vval.v_list != NULL
7574 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7575 || (tv->v_type == VAR_DICT
7576 && tv->vval.v_dict != NULL
7577 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7578}
7579
7580/*
7581 * "islocked()" function
7582 */
7583 static void
7584f_islocked(typval_T *argvars, typval_T *rettv)
7585{
7586 lval_T lv;
7587 char_u *end;
7588 dictitem_T *di;
7589
7590 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007591 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007592 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007593 if (end != NULL && lv.ll_name != NULL)
7594 {
7595 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007596 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 else
7598 {
7599 if (lv.ll_tv == NULL)
7600 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007601 di = find_var(lv.ll_name, NULL, TRUE);
7602 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007603 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007604 /* Consider a variable locked when:
7605 * 1. the variable itself is locked
7606 * 2. the value of the variable is locked.
7607 * 3. the List or Dict value is locked.
7608 */
7609 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7610 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 }
7612 }
7613 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007614 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007615 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007616 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007617 else if (lv.ll_list != NULL)
7618 /* List item. */
7619 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7620 else
7621 /* Dictionary item. */
7622 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7623 }
7624 }
7625
7626 clear_lval(&lv);
7627}
7628
7629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7630/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007631 * "isinf()" function
7632 */
7633 static void
7634f_isinf(typval_T *argvars, typval_T *rettv)
7635{
7636 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7637 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7638}
7639
7640/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007641 * "isnan()" function
7642 */
7643 static void
7644f_isnan(typval_T *argvars, typval_T *rettv)
7645{
7646 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7647 && isnan(argvars[0].vval.v_float);
7648}
7649#endif
7650
7651/*
7652 * "items(dict)" function
7653 */
7654 static void
7655f_items(typval_T *argvars, typval_T *rettv)
7656{
7657 dict_list(argvars, rettv, 2);
7658}
7659
7660#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7661/*
7662 * Get the job from the argument.
7663 * Returns NULL if the job is invalid.
7664 */
7665 static job_T *
7666get_job_arg(typval_T *tv)
7667{
7668 job_T *job;
7669
7670 if (tv->v_type != VAR_JOB)
7671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 return NULL;
7674 }
7675 job = tv->vval.v_job;
7676
7677 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007678 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679 return job;
7680}
7681
7682/*
7683 * "job_getchannel()" function
7684 */
7685 static void
7686f_job_getchannel(typval_T *argvars, typval_T *rettv)
7687{
7688 job_T *job = get_job_arg(&argvars[0]);
7689
7690 if (job != NULL)
7691 {
7692 rettv->v_type = VAR_CHANNEL;
7693 rettv->vval.v_channel = job->jv_channel;
7694 if (job->jv_channel != NULL)
7695 ++job->jv_channel->ch_refcount;
7696 }
7697}
7698
7699/*
7700 * "job_info()" function
7701 */
7702 static void
7703f_job_info(typval_T *argvars, typval_T *rettv)
7704{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007705 if (argvars[0].v_type != VAR_UNKNOWN)
7706 {
7707 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007709 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7710 job_info(job, rettv->vval.v_dict);
7711 }
7712 else if (rettv_list_alloc(rettv) == OK)
7713 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007714}
7715
7716/*
7717 * "job_setoptions()" function
7718 */
7719 static void
7720f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7721{
7722 job_T *job = get_job_arg(&argvars[0]);
7723 jobopt_T opt;
7724
7725 if (job == NULL)
7726 return;
7727 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007728 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007729 job_set_options(job, &opt);
7730 free_job_options(&opt);
7731}
7732
7733/*
7734 * "job_start()" function
7735 */
7736 static void
7737f_job_start(typval_T *argvars, typval_T *rettv)
7738{
7739 rettv->v_type = VAR_JOB;
7740 if (check_restricted() || check_secure())
7741 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007742 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007743}
7744
7745/*
7746 * "job_status()" function
7747 */
7748 static void
7749f_job_status(typval_T *argvars, typval_T *rettv)
7750{
7751 job_T *job = get_job_arg(&argvars[0]);
7752
7753 if (job != NULL)
7754 {
7755 rettv->v_type = VAR_STRING;
7756 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7757 }
7758}
7759
7760/*
7761 * "job_stop()" function
7762 */
7763 static void
7764f_job_stop(typval_T *argvars, typval_T *rettv)
7765{
7766 job_T *job = get_job_arg(&argvars[0]);
7767
7768 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007769 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007770}
7771#endif
7772
7773/*
7774 * "join()" function
7775 */
7776 static void
7777f_join(typval_T *argvars, typval_T *rettv)
7778{
7779 garray_T ga;
7780 char_u *sep;
7781
7782 if (argvars[0].v_type != VAR_LIST)
7783 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007784 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007785 return;
7786 }
7787 if (argvars[0].vval.v_list == NULL)
7788 return;
7789 if (argvars[1].v_type == VAR_UNKNOWN)
7790 sep = (char_u *)" ";
7791 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007792 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007793
7794 rettv->v_type = VAR_STRING;
7795
7796 if (sep != NULL)
7797 {
7798 ga_init2(&ga, (int)sizeof(char), 80);
7799 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7800 ga_append(&ga, NUL);
7801 rettv->vval.v_string = (char_u *)ga.ga_data;
7802 }
7803 else
7804 rettv->vval.v_string = NULL;
7805}
7806
7807/*
7808 * "js_decode()" function
7809 */
7810 static void
7811f_js_decode(typval_T *argvars, typval_T *rettv)
7812{
7813 js_read_T reader;
7814
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007815 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 reader.js_fill = NULL;
7817 reader.js_used = 0;
7818 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007819 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007820}
7821
7822/*
7823 * "js_encode()" function
7824 */
7825 static void
7826f_js_encode(typval_T *argvars, typval_T *rettv)
7827{
7828 rettv->v_type = VAR_STRING;
7829 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7830}
7831
7832/*
7833 * "json_decode()" function
7834 */
7835 static void
7836f_json_decode(typval_T *argvars, typval_T *rettv)
7837{
7838 js_read_T reader;
7839
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007840 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007841 reader.js_fill = NULL;
7842 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007843 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007844}
7845
7846/*
7847 * "json_encode()" function
7848 */
7849 static void
7850f_json_encode(typval_T *argvars, typval_T *rettv)
7851{
7852 rettv->v_type = VAR_STRING;
7853 rettv->vval.v_string = json_encode(&argvars[0], 0);
7854}
7855
7856/*
7857 * "keys()" function
7858 */
7859 static void
7860f_keys(typval_T *argvars, typval_T *rettv)
7861{
7862 dict_list(argvars, rettv, 0);
7863}
7864
7865/*
7866 * "last_buffer_nr()" function.
7867 */
7868 static void
7869f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7870{
7871 int n = 0;
7872 buf_T *buf;
7873
Bram Moolenaar29323592016-07-24 22:04:11 +02007874 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007875 if (n < buf->b_fnum)
7876 n = buf->b_fnum;
7877
7878 rettv->vval.v_number = n;
7879}
7880
7881/*
7882 * "len()" function
7883 */
7884 static void
7885f_len(typval_T *argvars, typval_T *rettv)
7886{
7887 switch (argvars[0].v_type)
7888 {
7889 case VAR_STRING:
7890 case VAR_NUMBER:
7891 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007892 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007893 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007894 case VAR_BLOB:
7895 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7896 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007897 case VAR_LIST:
7898 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7899 break;
7900 case VAR_DICT:
7901 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7902 break;
7903 case VAR_UNKNOWN:
7904 case VAR_SPECIAL:
7905 case VAR_FLOAT:
7906 case VAR_FUNC:
7907 case VAR_PARTIAL:
7908 case VAR_JOB:
7909 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007910 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 break;
7912 }
7913}
7914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007915 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007916libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007917{
7918#ifdef FEAT_LIBCALL
7919 char_u *string_in;
7920 char_u **string_result;
7921 int nr_result;
7922#endif
7923
7924 rettv->v_type = type;
7925 if (type != VAR_NUMBER)
7926 rettv->vval.v_string = NULL;
7927
7928 if (check_restricted() || check_secure())
7929 return;
7930
7931#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007932 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7934 {
7935 string_in = NULL;
7936 if (argvars[2].v_type == VAR_STRING)
7937 string_in = argvars[2].vval.v_string;
7938 if (type == VAR_NUMBER)
7939 string_result = NULL;
7940 else
7941 string_result = &rettv->vval.v_string;
7942 if (mch_libcall(argvars[0].vval.v_string,
7943 argvars[1].vval.v_string,
7944 string_in,
7945 argvars[2].vval.v_number,
7946 string_result,
7947 &nr_result) == OK
7948 && type == VAR_NUMBER)
7949 rettv->vval.v_number = nr_result;
7950 }
7951#endif
7952}
7953
7954/*
7955 * "libcall()" function
7956 */
7957 static void
7958f_libcall(typval_T *argvars, typval_T *rettv)
7959{
7960 libcall_common(argvars, rettv, VAR_STRING);
7961}
7962
7963/*
7964 * "libcallnr()" function
7965 */
7966 static void
7967f_libcallnr(typval_T *argvars, typval_T *rettv)
7968{
7969 libcall_common(argvars, rettv, VAR_NUMBER);
7970}
7971
7972/*
7973 * "line(string)" function
7974 */
7975 static void
7976f_line(typval_T *argvars, typval_T *rettv)
7977{
7978 linenr_T lnum = 0;
7979 pos_T *fp;
7980 int fnum;
7981
7982 fp = var2fpos(&argvars[0], TRUE, &fnum);
7983 if (fp != NULL)
7984 lnum = fp->lnum;
7985 rettv->vval.v_number = lnum;
7986}
7987
7988/*
7989 * "line2byte(lnum)" function
7990 */
7991 static void
7992f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7993{
7994#ifndef FEAT_BYTEOFF
7995 rettv->vval.v_number = -1;
7996#else
7997 linenr_T lnum;
7998
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007999 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008000 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8001 rettv->vval.v_number = -1;
8002 else
8003 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8004 if (rettv->vval.v_number >= 0)
8005 ++rettv->vval.v_number;
8006#endif
8007}
8008
8009/*
8010 * "lispindent(lnum)" function
8011 */
8012 static void
8013f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8014{
8015#ifdef FEAT_LISP
8016 pos_T pos;
8017 linenr_T lnum;
8018
8019 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008020 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8022 {
8023 curwin->w_cursor.lnum = lnum;
8024 rettv->vval.v_number = get_lisp_indent();
8025 curwin->w_cursor = pos;
8026 }
8027 else
8028#endif
8029 rettv->vval.v_number = -1;
8030}
8031
8032/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008033 * "list2str()" function
8034 */
8035 static void
8036f_list2str(typval_T *argvars, typval_T *rettv)
8037{
8038 list_T *l;
8039 listitem_T *li;
8040 garray_T ga;
8041 int utf8 = FALSE;
8042
8043 rettv->v_type = VAR_STRING;
8044 rettv->vval.v_string = NULL;
8045 if (argvars[0].v_type != VAR_LIST)
8046 {
8047 emsg(_(e_invarg));
8048 return;
8049 }
8050
8051 l = argvars[0].vval.v_list;
8052 if (l == NULL)
8053 return; // empty list results in empty string
8054
8055 if (argvars[1].v_type != VAR_UNKNOWN)
8056 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8057
8058 ga_init2(&ga, 1, 80);
8059 if (has_mbyte || utf8)
8060 {
8061 char_u buf[MB_MAXBYTES + 1];
8062 int (*char2bytes)(int, char_u *);
8063
8064 if (utf8 || enc_utf8)
8065 char2bytes = utf_char2bytes;
8066 else
8067 char2bytes = mb_char2bytes;
8068
8069 for (li = l->lv_first; li != NULL; li = li->li_next)
8070 {
8071 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8072 ga_concat(&ga, buf);
8073 }
8074 ga_append(&ga, NUL);
8075 }
8076 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8077 {
8078 for (li = l->lv_first; li != NULL; li = li->li_next)
8079 ga_append(&ga, tv_get_number(&li->li_tv));
8080 ga_append(&ga, NUL);
8081 }
8082
8083 rettv->v_type = VAR_STRING;
8084 rettv->vval.v_string = ga.ga_data;
8085}
8086
8087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008088 * "localtime()" function
8089 */
8090 static void
8091f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8092{
8093 rettv->vval.v_number = (varnumber_T)time(NULL);
8094}
8095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008096 static void
8097get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8098{
8099 char_u *keys;
8100 char_u *which;
8101 char_u buf[NUMBUFLEN];
8102 char_u *keys_buf = NULL;
8103 char_u *rhs;
8104 int mode;
8105 int abbr = FALSE;
8106 int get_dict = FALSE;
8107 mapblock_T *mp;
8108 int buffer_local;
8109
8110 /* return empty string for failure */
8111 rettv->v_type = VAR_STRING;
8112 rettv->vval.v_string = NULL;
8113
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (*keys == NUL)
8116 return;
8117
8118 if (argvars[1].v_type != VAR_UNKNOWN)
8119 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008120 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008121 if (argvars[2].v_type != VAR_UNKNOWN)
8122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008123 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008125 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008126 }
8127 }
8128 else
8129 which = (char_u *)"";
8130 if (which == NULL)
8131 return;
8132
8133 mode = get_map_mode(&which, 0);
8134
8135 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8136 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8137 vim_free(keys_buf);
8138
8139 if (!get_dict)
8140 {
8141 /* Return a string. */
8142 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008143 {
8144 if (*rhs == NUL)
8145 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8146 else
8147 rettv->vval.v_string = str2special_save(rhs, FALSE);
8148 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008149
8150 }
8151 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8152 {
8153 /* Return a dictionary. */
8154 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8155 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8156 dict_T *dict = rettv->vval.v_dict;
8157
Bram Moolenaare0be1672018-07-08 16:50:37 +02008158 dict_add_string(dict, "lhs", lhs);
8159 dict_add_string(dict, "rhs", mp->m_orig_str);
8160 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8161 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8162 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008163 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8164 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008165 dict_add_number(dict, "buffer", (long)buffer_local);
8166 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8167 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008168
8169 vim_free(lhs);
8170 vim_free(mapmode);
8171 }
8172}
8173
8174#ifdef FEAT_FLOAT
8175/*
8176 * "log()" function
8177 */
8178 static void
8179f_log(typval_T *argvars, typval_T *rettv)
8180{
8181 float_T f = 0.0;
8182
8183 rettv->v_type = VAR_FLOAT;
8184 if (get_float_arg(argvars, &f) == OK)
8185 rettv->vval.v_float = log(f);
8186 else
8187 rettv->vval.v_float = 0.0;
8188}
8189
8190/*
8191 * "log10()" function
8192 */
8193 static void
8194f_log10(typval_T *argvars, typval_T *rettv)
8195{
8196 float_T f = 0.0;
8197
8198 rettv->v_type = VAR_FLOAT;
8199 if (get_float_arg(argvars, &f) == OK)
8200 rettv->vval.v_float = log10(f);
8201 else
8202 rettv->vval.v_float = 0.0;
8203}
8204#endif
8205
8206#ifdef FEAT_LUA
8207/*
8208 * "luaeval()" function
8209 */
8210 static void
8211f_luaeval(typval_T *argvars, typval_T *rettv)
8212{
8213 char_u *str;
8214 char_u buf[NUMBUFLEN];
8215
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008216 if (check_restricted() || check_secure())
8217 return;
8218
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008219 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008220 do_luaeval(str, argvars + 1, rettv);
8221}
8222#endif
8223
8224/*
8225 * "map()" function
8226 */
8227 static void
8228f_map(typval_T *argvars, typval_T *rettv)
8229{
8230 filter_map(argvars, rettv, TRUE);
8231}
8232
8233/*
8234 * "maparg()" function
8235 */
8236 static void
8237f_maparg(typval_T *argvars, typval_T *rettv)
8238{
8239 get_maparg(argvars, rettv, TRUE);
8240}
8241
8242/*
8243 * "mapcheck()" function
8244 */
8245 static void
8246f_mapcheck(typval_T *argvars, typval_T *rettv)
8247{
8248 get_maparg(argvars, rettv, FALSE);
8249}
8250
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008251typedef enum
8252{
8253 MATCH_END, /* matchend() */
8254 MATCH_MATCH, /* match() */
8255 MATCH_STR, /* matchstr() */
8256 MATCH_LIST, /* matchlist() */
8257 MATCH_POS /* matchstrpos() */
8258} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008259
8260 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008261find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008262{
8263 char_u *str = NULL;
8264 long len = 0;
8265 char_u *expr = NULL;
8266 char_u *pat;
8267 regmatch_T regmatch;
8268 char_u patbuf[NUMBUFLEN];
8269 char_u strbuf[NUMBUFLEN];
8270 char_u *save_cpo;
8271 long start = 0;
8272 long nth = 1;
8273 colnr_T startcol = 0;
8274 int match = 0;
8275 list_T *l = NULL;
8276 listitem_T *li = NULL;
8277 long idx = 0;
8278 char_u *tofree = NULL;
8279
8280 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8281 save_cpo = p_cpo;
8282 p_cpo = (char_u *)"";
8283
8284 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008285 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008287 /* type MATCH_LIST: return empty list when there are no matches.
8288 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008289 if (rettv_list_alloc(rettv) == FAIL)
8290 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008291 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008292 && (list_append_string(rettv->vval.v_list,
8293 (char_u *)"", 0) == FAIL
8294 || list_append_number(rettv->vval.v_list,
8295 (varnumber_T)-1) == FAIL
8296 || list_append_number(rettv->vval.v_list,
8297 (varnumber_T)-1) == FAIL
8298 || list_append_number(rettv->vval.v_list,
8299 (varnumber_T)-1) == FAIL))
8300 {
8301 list_free(rettv->vval.v_list);
8302 rettv->vval.v_list = NULL;
8303 goto theend;
8304 }
8305 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008306 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008307 {
8308 rettv->v_type = VAR_STRING;
8309 rettv->vval.v_string = NULL;
8310 }
8311
8312 if (argvars[0].v_type == VAR_LIST)
8313 {
8314 if ((l = argvars[0].vval.v_list) == NULL)
8315 goto theend;
8316 li = l->lv_first;
8317 }
8318 else
8319 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008320 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008321 len = (long)STRLEN(str);
8322 }
8323
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008324 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (pat == NULL)
8326 goto theend;
8327
8328 if (argvars[2].v_type != VAR_UNKNOWN)
8329 {
8330 int error = FALSE;
8331
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008332 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008333 if (error)
8334 goto theend;
8335 if (l != NULL)
8336 {
8337 li = list_find(l, start);
8338 if (li == NULL)
8339 goto theend;
8340 idx = l->lv_idx; /* use the cached index */
8341 }
8342 else
8343 {
8344 if (start < 0)
8345 start = 0;
8346 if (start > len)
8347 goto theend;
8348 /* When "count" argument is there ignore matches before "start",
8349 * otherwise skip part of the string. Differs when pattern is "^"
8350 * or "\<". */
8351 if (argvars[3].v_type != VAR_UNKNOWN)
8352 startcol = start;
8353 else
8354 {
8355 str += start;
8356 len -= start;
8357 }
8358 }
8359
8360 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (error)
8363 goto theend;
8364 }
8365
8366 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8367 if (regmatch.regprog != NULL)
8368 {
8369 regmatch.rm_ic = p_ic;
8370
8371 for (;;)
8372 {
8373 if (l != NULL)
8374 {
8375 if (li == NULL)
8376 {
8377 match = FALSE;
8378 break;
8379 }
8380 vim_free(tofree);
8381 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8382 if (str == NULL)
8383 break;
8384 }
8385
8386 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8387
8388 if (match && --nth <= 0)
8389 break;
8390 if (l == NULL && !match)
8391 break;
8392
8393 /* Advance to just after the match. */
8394 if (l != NULL)
8395 {
8396 li = li->li_next;
8397 ++idx;
8398 }
8399 else
8400 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008401 startcol = (colnr_T)(regmatch.startp[0]
8402 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008403 if (startcol > (colnr_T)len
8404 || str + startcol <= regmatch.startp[0])
8405 {
8406 match = FALSE;
8407 break;
8408 }
8409 }
8410 }
8411
8412 if (match)
8413 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008414 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008415 {
8416 listitem_T *li1 = rettv->vval.v_list->lv_first;
8417 listitem_T *li2 = li1->li_next;
8418 listitem_T *li3 = li2->li_next;
8419 listitem_T *li4 = li3->li_next;
8420
8421 vim_free(li1->li_tv.vval.v_string);
8422 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8423 (int)(regmatch.endp[0] - regmatch.startp[0]));
8424 li3->li_tv.vval.v_number =
8425 (varnumber_T)(regmatch.startp[0] - expr);
8426 li4->li_tv.vval.v_number =
8427 (varnumber_T)(regmatch.endp[0] - expr);
8428 if (l != NULL)
8429 li2->li_tv.vval.v_number = (varnumber_T)idx;
8430 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008431 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008432 {
8433 int i;
8434
8435 /* return list with matched string and submatches */
8436 for (i = 0; i < NSUBEXP; ++i)
8437 {
8438 if (regmatch.endp[i] == NULL)
8439 {
8440 if (list_append_string(rettv->vval.v_list,
8441 (char_u *)"", 0) == FAIL)
8442 break;
8443 }
8444 else if (list_append_string(rettv->vval.v_list,
8445 regmatch.startp[i],
8446 (int)(regmatch.endp[i] - regmatch.startp[i]))
8447 == FAIL)
8448 break;
8449 }
8450 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008451 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 {
8453 /* return matched string */
8454 if (l != NULL)
8455 copy_tv(&li->li_tv, rettv);
8456 else
8457 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8458 (int)(regmatch.endp[0] - regmatch.startp[0]));
8459 }
8460 else if (l != NULL)
8461 rettv->vval.v_number = idx;
8462 else
8463 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008464 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008465 rettv->vval.v_number =
8466 (varnumber_T)(regmatch.startp[0] - str);
8467 else
8468 rettv->vval.v_number =
8469 (varnumber_T)(regmatch.endp[0] - str);
8470 rettv->vval.v_number += (varnumber_T)(str - expr);
8471 }
8472 }
8473 vim_regfree(regmatch.regprog);
8474 }
8475
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008476theend:
8477 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008478 /* matchstrpos() without a list: drop the second item. */
8479 listitem_remove(rettv->vval.v_list,
8480 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008481 vim_free(tofree);
8482 p_cpo = save_cpo;
8483}
8484
8485/*
8486 * "match()" function
8487 */
8488 static void
8489f_match(typval_T *argvars, typval_T *rettv)
8490{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008491 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008492}
8493
Bram Moolenaar95e51472018-07-28 16:55:56 +02008494#ifdef FEAT_SEARCH_EXTRA
8495 static int
8496matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8497{
8498 dictitem_T *di;
8499
8500 if (tv->v_type != VAR_DICT)
8501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008502 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008503 return FAIL;
8504 }
8505
8506 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008507 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008508 (char_u *)"conceal", FALSE);
8509
8510 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8511 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008512 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008513 if (*win == NULL)
8514 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008515 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008516 return FAIL;
8517 }
8518 }
8519
8520 return OK;
8521}
8522#endif
8523
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008524/*
8525 * "matchadd()" function
8526 */
8527 static void
8528f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8529{
8530#ifdef FEAT_SEARCH_EXTRA
8531 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008532 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8533 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008534 int prio = 10; /* default priority */
8535 int id = -1;
8536 int error = FALSE;
8537 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008538 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008539
8540 rettv->vval.v_number = -1;
8541
8542 if (grp == NULL || pat == NULL)
8543 return;
8544 if (argvars[2].v_type != VAR_UNKNOWN)
8545 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008546 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 if (argvars[3].v_type != VAR_UNKNOWN)
8548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008549 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008550 if (argvars[4].v_type != VAR_UNKNOWN
8551 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8552 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008553 }
8554 }
8555 if (error == TRUE)
8556 return;
8557 if (id >= 1 && id <= 3)
8558 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008559 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560 return;
8561 }
8562
Bram Moolenaar95e51472018-07-28 16:55:56 +02008563 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008564 conceal_char);
8565#endif
8566}
8567
8568/*
8569 * "matchaddpos()" function
8570 */
8571 static void
8572f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8573{
8574#ifdef FEAT_SEARCH_EXTRA
8575 char_u buf[NUMBUFLEN];
8576 char_u *group;
8577 int prio = 10;
8578 int id = -1;
8579 int error = FALSE;
8580 list_T *l;
8581 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008582 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583
8584 rettv->vval.v_number = -1;
8585
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008586 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 if (group == NULL)
8588 return;
8589
8590 if (argvars[1].v_type != VAR_LIST)
8591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008593 return;
8594 }
8595 l = argvars[1].vval.v_list;
8596 if (l == NULL)
8597 return;
8598
8599 if (argvars[2].v_type != VAR_UNKNOWN)
8600 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008601 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008602 if (argvars[3].v_type != VAR_UNKNOWN)
8603 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008604 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008605
8606 if (argvars[4].v_type != VAR_UNKNOWN
8607 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8608 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008609 }
8610 }
8611 if (error == TRUE)
8612 return;
8613
8614 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8615 if (id == 1 || id == 2)
8616 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008617 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008618 return;
8619 }
8620
Bram Moolenaar95e51472018-07-28 16:55:56 +02008621 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008622 conceal_char);
8623#endif
8624}
8625
8626/*
8627 * "matcharg()" function
8628 */
8629 static void
8630f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8631{
8632 if (rettv_list_alloc(rettv) == OK)
8633 {
8634#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008635 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008636 matchitem_T *m;
8637
8638 if (id >= 1 && id <= 3)
8639 {
8640 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8641 {
8642 list_append_string(rettv->vval.v_list,
8643 syn_id2name(m->hlg_id), -1);
8644 list_append_string(rettv->vval.v_list, m->pattern, -1);
8645 }
8646 else
8647 {
8648 list_append_string(rettv->vval.v_list, NULL, -1);
8649 list_append_string(rettv->vval.v_list, NULL, -1);
8650 }
8651 }
8652#endif
8653 }
8654}
8655
8656/*
8657 * "matchdelete()" function
8658 */
8659 static void
8660f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8661{
8662#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008663 win_T *win = get_optional_window(argvars, 1);
8664
8665 if (win == NULL)
8666 rettv->vval.v_number = -1;
8667 else
8668 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008669 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008670#endif
8671}
8672
8673/*
8674 * "matchend()" function
8675 */
8676 static void
8677f_matchend(typval_T *argvars, typval_T *rettv)
8678{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008679 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008680}
8681
8682/*
8683 * "matchlist()" function
8684 */
8685 static void
8686f_matchlist(typval_T *argvars, typval_T *rettv)
8687{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008688 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008689}
8690
8691/*
8692 * "matchstr()" function
8693 */
8694 static void
8695f_matchstr(typval_T *argvars, typval_T *rettv)
8696{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008697 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008698}
8699
8700/*
8701 * "matchstrpos()" function
8702 */
8703 static void
8704f_matchstrpos(typval_T *argvars, typval_T *rettv)
8705{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008706 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707}
8708
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008709 static void
8710max_min(typval_T *argvars, typval_T *rettv, int domax)
8711{
8712 varnumber_T n = 0;
8713 varnumber_T i;
8714 int error = FALSE;
8715
8716 if (argvars[0].v_type == VAR_LIST)
8717 {
8718 list_T *l;
8719 listitem_T *li;
8720
8721 l = argvars[0].vval.v_list;
8722 if (l != NULL)
8723 {
8724 li = l->lv_first;
8725 if (li != NULL)
8726 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 for (;;)
8729 {
8730 li = li->li_next;
8731 if (li == NULL)
8732 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008733 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 if (domax ? i > n : i < n)
8735 n = i;
8736 }
8737 }
8738 }
8739 }
8740 else if (argvars[0].v_type == VAR_DICT)
8741 {
8742 dict_T *d;
8743 int first = TRUE;
8744 hashitem_T *hi;
8745 int todo;
8746
8747 d = argvars[0].vval.v_dict;
8748 if (d != NULL)
8749 {
8750 todo = (int)d->dv_hashtab.ht_used;
8751 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8752 {
8753 if (!HASHITEM_EMPTY(hi))
8754 {
8755 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008756 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008757 if (first)
8758 {
8759 n = i;
8760 first = FALSE;
8761 }
8762 else if (domax ? i > n : i < n)
8763 n = i;
8764 }
8765 }
8766 }
8767 }
8768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008769 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 rettv->vval.v_number = error ? 0 : n;
8771}
8772
8773/*
8774 * "max()" function
8775 */
8776 static void
8777f_max(typval_T *argvars, typval_T *rettv)
8778{
8779 max_min(argvars, rettv, TRUE);
8780}
8781
8782/*
8783 * "min()" function
8784 */
8785 static void
8786f_min(typval_T *argvars, typval_T *rettv)
8787{
8788 max_min(argvars, rettv, FALSE);
8789}
8790
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008791/*
8792 * Create the directory in which "dir" is located, and higher levels when
8793 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008794 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008795 */
8796 static int
8797mkdir_recurse(char_u *dir, int prot)
8798{
8799 char_u *p;
8800 char_u *updir;
8801 int r = FAIL;
8802
8803 /* Get end of directory name in "dir".
8804 * We're done when it's "/" or "c:/". */
8805 p = gettail_sep(dir);
8806 if (p <= get_past_head(dir))
8807 return OK;
8808
8809 /* If the directory exists we're done. Otherwise: create it.*/
8810 updir = vim_strnsave(dir, (int)(p - dir));
8811 if (updir == NULL)
8812 return FAIL;
8813 if (mch_isdir(updir))
8814 r = OK;
8815 else if (mkdir_recurse(updir, prot) == OK)
8816 r = vim_mkdir_emsg(updir, prot);
8817 vim_free(updir);
8818 return r;
8819}
8820
8821#ifdef vim_mkdir
8822/*
8823 * "mkdir()" function
8824 */
8825 static void
8826f_mkdir(typval_T *argvars, typval_T *rettv)
8827{
8828 char_u *dir;
8829 char_u buf[NUMBUFLEN];
8830 int prot = 0755;
8831
8832 rettv->vval.v_number = FAIL;
8833 if (check_restricted() || check_secure())
8834 return;
8835
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008836 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008837 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008838 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008839
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008840 if (*gettail(dir) == NUL)
8841 /* remove trailing slashes */
8842 *gettail_sep(dir) = NUL;
8843
8844 if (argvars[1].v_type != VAR_UNKNOWN)
8845 {
8846 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008848 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008849 if (prot == -1)
8850 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008851 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008852 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008853 {
8854 if (mch_isdir(dir))
8855 {
8856 /* With the "p" flag it's OK if the dir already exists. */
8857 rettv->vval.v_number = OK;
8858 return;
8859 }
8860 mkdir_recurse(dir, prot);
8861 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008862 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008863 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008864}
8865#endif
8866
8867/*
8868 * "mode()" function
8869 */
8870 static void
8871f_mode(typval_T *argvars, typval_T *rettv)
8872{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008873 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874
Bram Moolenaar612cc382018-07-29 15:34:26 +02008875 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876
8877 if (time_for_testing == 93784)
8878 {
8879 /* Testing the two-character code. */
8880 buf[0] = 'x';
8881 buf[1] = '!';
8882 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008883#ifdef FEAT_TERMINAL
8884 else if (term_use_loop())
8885 buf[0] = 't';
8886#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 else if (VIsual_active)
8888 {
8889 if (VIsual_select)
8890 buf[0] = VIsual_mode + 's' - 'v';
8891 else
8892 buf[0] = VIsual_mode;
8893 }
8894 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8895 || State == CONFIRM)
8896 {
8897 buf[0] = 'r';
8898 if (State == ASKMORE)
8899 buf[1] = 'm';
8900 else if (State == CONFIRM)
8901 buf[1] = '?';
8902 }
8903 else if (State == EXTERNCMD)
8904 buf[0] = '!';
8905 else if (State & INSERT)
8906 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008907 if (State & VREPLACE_FLAG)
8908 {
8909 buf[0] = 'R';
8910 buf[1] = 'v';
8911 }
8912 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008913 {
8914 if (State & REPLACE_FLAG)
8915 buf[0] = 'R';
8916 else
8917 buf[0] = 'i';
8918#ifdef FEAT_INS_EXPAND
8919 if (ins_compl_active())
8920 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008921 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008922 buf[1] = 'x';
8923#endif
8924 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008925 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008926 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 {
8928 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008929 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008930 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008931 else if (exmode_active == EXMODE_NORMAL)
8932 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 }
8934 else
8935 {
8936 buf[0] = 'n';
8937 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008938 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008939 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008940 // to be able to detect force-linewise/blockwise/characterwise operations
8941 buf[2] = motion_force;
8942 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008943 else if (restart_edit == 'I' || restart_edit == 'R'
8944 || restart_edit == 'V')
8945 {
8946 buf[1] = 'i';
8947 buf[2] = restart_edit;
8948 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008949 }
8950
8951 /* Clear out the minor mode when the argument is not a non-zero number or
8952 * non-empty string. */
8953 if (!non_zero_arg(&argvars[0]))
8954 buf[1] = NUL;
8955
8956 rettv->vval.v_string = vim_strsave(buf);
8957 rettv->v_type = VAR_STRING;
8958}
8959
8960#if defined(FEAT_MZSCHEME) || defined(PROTO)
8961/*
8962 * "mzeval()" function
8963 */
8964 static void
8965f_mzeval(typval_T *argvars, typval_T *rettv)
8966{
8967 char_u *str;
8968 char_u buf[NUMBUFLEN];
8969
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008970 if (check_restricted() || check_secure())
8971 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008972 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008973 do_mzeval(str, rettv);
8974}
8975
8976 void
8977mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8978{
8979 typval_T argvars[3];
8980
8981 argvars[0].v_type = VAR_STRING;
8982 argvars[0].vval.v_string = name;
8983 copy_tv(args, &argvars[1]);
8984 argvars[2].v_type = VAR_UNKNOWN;
8985 f_call(argvars, rettv);
8986 clear_tv(&argvars[1]);
8987}
8988#endif
8989
8990/*
8991 * "nextnonblank()" function
8992 */
8993 static void
8994f_nextnonblank(typval_T *argvars, typval_T *rettv)
8995{
8996 linenr_T lnum;
8997
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008998 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008999 {
9000 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9001 {
9002 lnum = 0;
9003 break;
9004 }
9005 if (*skipwhite(ml_get(lnum)) != NUL)
9006 break;
9007 }
9008 rettv->vval.v_number = lnum;
9009}
9010
9011/*
9012 * "nr2char()" function
9013 */
9014 static void
9015f_nr2char(typval_T *argvars, typval_T *rettv)
9016{
9017 char_u buf[NUMBUFLEN];
9018
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (has_mbyte)
9020 {
9021 int utf8 = 0;
9022
9023 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009024 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009026 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009028 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009029 }
9030 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009031 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009032 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009033 buf[1] = NUL;
9034 }
9035 rettv->v_type = VAR_STRING;
9036 rettv->vval.v_string = vim_strsave(buf);
9037}
9038
9039/*
9040 * "or(expr, expr)" function
9041 */
9042 static void
9043f_or(typval_T *argvars, typval_T *rettv)
9044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009045 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9046 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009047}
9048
9049/*
9050 * "pathshorten()" function
9051 */
9052 static void
9053f_pathshorten(typval_T *argvars, typval_T *rettv)
9054{
9055 char_u *p;
9056
9057 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009058 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009059 if (p == NULL)
9060 rettv->vval.v_string = NULL;
9061 else
9062 {
9063 p = vim_strsave(p);
9064 rettv->vval.v_string = p;
9065 if (p != NULL)
9066 shorten_dir(p);
9067 }
9068}
9069
9070#ifdef FEAT_PERL
9071/*
9072 * "perleval()" function
9073 */
9074 static void
9075f_perleval(typval_T *argvars, typval_T *rettv)
9076{
9077 char_u *str;
9078 char_u buf[NUMBUFLEN];
9079
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009080 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009081 do_perleval(str, rettv);
9082}
9083#endif
9084
9085#ifdef FEAT_FLOAT
9086/*
9087 * "pow()" function
9088 */
9089 static void
9090f_pow(typval_T *argvars, typval_T *rettv)
9091{
9092 float_T fx = 0.0, fy = 0.0;
9093
9094 rettv->v_type = VAR_FLOAT;
9095 if (get_float_arg(argvars, &fx) == OK
9096 && get_float_arg(&argvars[1], &fy) == OK)
9097 rettv->vval.v_float = pow(fx, fy);
9098 else
9099 rettv->vval.v_float = 0.0;
9100}
9101#endif
9102
9103/*
9104 * "prevnonblank()" function
9105 */
9106 static void
9107f_prevnonblank(typval_T *argvars, typval_T *rettv)
9108{
9109 linenr_T lnum;
9110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009111 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009112 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9113 lnum = 0;
9114 else
9115 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9116 --lnum;
9117 rettv->vval.v_number = lnum;
9118}
9119
9120/* This dummy va_list is here because:
9121 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9122 * - locally in the function results in a "used before set" warning
9123 * - using va_start() to initialize it gives "function with fixed args" error */
9124static va_list ap;
9125
9126/*
9127 * "printf()" function
9128 */
9129 static void
9130f_printf(typval_T *argvars, typval_T *rettv)
9131{
9132 char_u buf[NUMBUFLEN];
9133 int len;
9134 char_u *s;
9135 int saved_did_emsg = did_emsg;
9136 char *fmt;
9137
9138 rettv->v_type = VAR_STRING;
9139 rettv->vval.v_string = NULL;
9140
9141 /* Get the required length, allocate the buffer and do it for real. */
9142 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009143 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009144 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009145 if (!did_emsg)
9146 {
9147 s = alloc(len + 1);
9148 if (s != NULL)
9149 {
9150 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009151 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9152 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009153 }
9154 }
9155 did_emsg |= saved_did_emsg;
9156}
9157
Bram Moolenaarf2732452018-06-03 14:47:35 +02009158#ifdef FEAT_JOB_CHANNEL
9159/*
9160 * "prompt_setcallback({buffer}, {callback})" function
9161 */
9162 static void
9163f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9164{
9165 buf_T *buf;
9166 char_u *callback;
9167 partial_T *partial;
9168
9169 if (check_secure())
9170 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009171 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009172 if (buf == NULL)
9173 return;
9174
9175 callback = get_callback(&argvars[1], &partial);
9176 if (callback == NULL)
9177 return;
9178
9179 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9180 if (partial == NULL)
9181 buf->b_prompt_callback = vim_strsave(callback);
9182 else
9183 /* pointer into the partial */
9184 buf->b_prompt_callback = callback;
9185 buf->b_prompt_partial = partial;
9186}
9187
9188/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009189 * "prompt_setinterrupt({buffer}, {callback})" function
9190 */
9191 static void
9192f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9193{
9194 buf_T *buf;
9195 char_u *callback;
9196 partial_T *partial;
9197
9198 if (check_secure())
9199 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009200 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009201 if (buf == NULL)
9202 return;
9203
9204 callback = get_callback(&argvars[1], &partial);
9205 if (callback == NULL)
9206 return;
9207
9208 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9209 if (partial == NULL)
9210 buf->b_prompt_interrupt = vim_strsave(callback);
9211 else
9212 /* pointer into the partial */
9213 buf->b_prompt_interrupt = callback;
9214 buf->b_prompt_int_partial = partial;
9215}
9216
9217/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009218 * "prompt_setprompt({buffer}, {text})" function
9219 */
9220 static void
9221f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9222{
9223 buf_T *buf;
9224 char_u *text;
9225
9226 if (check_secure())
9227 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009228 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009229 if (buf == NULL)
9230 return;
9231
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009232 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009233 vim_free(buf->b_prompt_text);
9234 buf->b_prompt_text = vim_strsave(text);
9235}
9236#endif
9237
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009238/*
9239 * "pumvisible()" function
9240 */
9241 static void
9242f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9243{
9244#ifdef FEAT_INS_EXPAND
9245 if (pum_visible())
9246 rettv->vval.v_number = 1;
9247#endif
9248}
9249
9250#ifdef FEAT_PYTHON3
9251/*
9252 * "py3eval()" function
9253 */
9254 static void
9255f_py3eval(typval_T *argvars, typval_T *rettv)
9256{
9257 char_u *str;
9258 char_u buf[NUMBUFLEN];
9259
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009260 if (check_restricted() || check_secure())
9261 return;
9262
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009263 if (p_pyx == 0)
9264 p_pyx = 3;
9265
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009266 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009267 do_py3eval(str, rettv);
9268}
9269#endif
9270
9271#ifdef FEAT_PYTHON
9272/*
9273 * "pyeval()" function
9274 */
9275 static void
9276f_pyeval(typval_T *argvars, typval_T *rettv)
9277{
9278 char_u *str;
9279 char_u buf[NUMBUFLEN];
9280
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009281 if (check_restricted() || check_secure())
9282 return;
9283
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009284 if (p_pyx == 0)
9285 p_pyx = 2;
9286
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009287 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009288 do_pyeval(str, rettv);
9289}
9290#endif
9291
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009292#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9293/*
9294 * "pyxeval()" function
9295 */
9296 static void
9297f_pyxeval(typval_T *argvars, typval_T *rettv)
9298{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009299 if (check_restricted() || check_secure())
9300 return;
9301
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009302# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9303 init_pyxversion();
9304 if (p_pyx == 2)
9305 f_pyeval(argvars, rettv);
9306 else
9307 f_py3eval(argvars, rettv);
9308# elif defined(FEAT_PYTHON)
9309 f_pyeval(argvars, rettv);
9310# elif defined(FEAT_PYTHON3)
9311 f_py3eval(argvars, rettv);
9312# endif
9313}
9314#endif
9315
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009316/*
9317 * "range()" function
9318 */
9319 static void
9320f_range(typval_T *argvars, typval_T *rettv)
9321{
9322 varnumber_T start;
9323 varnumber_T end;
9324 varnumber_T stride = 1;
9325 varnumber_T i;
9326 int error = FALSE;
9327
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009328 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009329 if (argvars[1].v_type == VAR_UNKNOWN)
9330 {
9331 end = start - 1;
9332 start = 0;
9333 }
9334 else
9335 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009336 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009337 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009338 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 }
9340
9341 if (error)
9342 return; /* type error; errmsg already given */
9343 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009344 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009345 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009346 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009347 else
9348 {
9349 if (rettv_list_alloc(rettv) == OK)
9350 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9351 if (list_append_number(rettv->vval.v_list,
9352 (varnumber_T)i) == FAIL)
9353 break;
9354 }
9355}
9356
9357/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009358 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009359 */
9360 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009361readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009362{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009363 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009364 typval_T save_val;
9365 typval_T rettv;
9366 typval_T argv[2];
9367 int retval = 0;
9368 int error = FALSE;
9369
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009370 if (expr->v_type == VAR_UNKNOWN)
9371 return 1;
9372
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009373 prepare_vimvar(VV_VAL, &save_val);
9374 set_vim_var_string(VV_VAL, name, -1);
9375 argv[0].v_type = VAR_STRING;
9376 argv[0].vval.v_string = name;
9377
9378 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9379 goto theend;
9380
9381 retval = tv_get_number_chk(&rettv, &error);
9382 if (error)
9383 retval = -1;
9384 clear_tv(&rettv);
9385
9386theend:
9387 set_vim_var_string(VV_VAL, NULL, 0);
9388 restore_vimvar(VV_VAL, &save_val);
9389 return retval;
9390}
9391
9392/*
9393 * "readdir()" function
9394 */
9395 static void
9396f_readdir(typval_T *argvars, typval_T *rettv)
9397{
9398 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009399 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009400 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009401 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009402 garray_T ga;
9403 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009404
9405 if (rettv_list_alloc(rettv) == FAIL)
9406 return;
9407 path = tv_get_string(&argvars[0]);
9408 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009409
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009410 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9411 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009412 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009413 for (i = 0; i < ga.ga_len; i++)
9414 {
9415 p = ((char_u **)ga.ga_data)[i];
9416 list_append_string(rettv->vval.v_list, p, -1);
9417 }
9418 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009419 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009420}
9421
9422/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009423 * "readfile()" function
9424 */
9425 static void
9426f_readfile(typval_T *argvars, typval_T *rettv)
9427{
9428 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009429 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009430 int failed = FALSE;
9431 char_u *fname;
9432 FILE *fd;
9433 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9434 int io_size = sizeof(buf);
9435 int readlen; /* size of last fread() */
9436 char_u *prev = NULL; /* previously read bytes, if any */
9437 long prevlen = 0; /* length of data in prev */
9438 long prevsize = 0; /* size of prev buffer */
9439 long maxline = MAXLNUM;
9440 long cnt = 0;
9441 char_u *p; /* position in buf */
9442 char_u *start; /* start of current line */
9443
9444 if (argvars[1].v_type != VAR_UNKNOWN)
9445 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009448 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9449 blob = TRUE;
9450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009452 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009453 }
9454
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009455 if (blob)
9456 {
9457 if (rettv_blob_alloc(rettv) == FAIL)
9458 return;
9459 }
9460 else
9461 {
9462 if (rettv_list_alloc(rettv) == FAIL)
9463 return;
9464 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009465
9466 /* Always open the file in binary mode, library functions have a mind of
9467 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009468 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009469 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009471 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 return;
9473 }
9474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009475 if (blob)
9476 {
9477 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009480 blob_free(rettv->vval.v_blob);
9481 }
9482 fclose(fd);
9483 return;
9484 }
9485
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009486 while (cnt < maxline || maxline < 0)
9487 {
9488 readlen = (int)fread(buf, 1, io_size, fd);
9489
9490 /* This for loop processes what was read, but is also entered at end
9491 * of file so that either:
9492 * - an incomplete line gets written
9493 * - a "binary" file gets an empty line at the end if it ends in a
9494 * newline. */
9495 for (p = buf, start = buf;
9496 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9497 ++p)
9498 {
9499 if (*p == '\n' || readlen <= 0)
9500 {
9501 listitem_T *li;
9502 char_u *s = NULL;
9503 long_u len = p - start;
9504
9505 /* Finished a line. Remove CRs before NL. */
9506 if (readlen > 0 && !binary)
9507 {
9508 while (len > 0 && start[len - 1] == '\r')
9509 --len;
9510 /* removal may cross back to the "prev" string */
9511 if (len == 0)
9512 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9513 --prevlen;
9514 }
9515 if (prevlen == 0)
9516 s = vim_strnsave(start, (int)len);
9517 else
9518 {
9519 /* Change "prev" buffer to be the right size. This way
9520 * the bytes are only copied once, and very long lines are
9521 * allocated only once. */
9522 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9523 {
9524 mch_memmove(s + prevlen, start, len);
9525 s[prevlen + len] = NUL;
9526 prev = NULL; /* the list will own the string */
9527 prevlen = prevsize = 0;
9528 }
9529 }
9530 if (s == NULL)
9531 {
9532 do_outofmem_msg((long_u) prevlen + len + 1);
9533 failed = TRUE;
9534 break;
9535 }
9536
9537 if ((li = listitem_alloc()) == NULL)
9538 {
9539 vim_free(s);
9540 failed = TRUE;
9541 break;
9542 }
9543 li->li_tv.v_type = VAR_STRING;
9544 li->li_tv.v_lock = 0;
9545 li->li_tv.vval.v_string = s;
9546 list_append(rettv->vval.v_list, li);
9547
9548 start = p + 1; /* step over newline */
9549 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9550 break;
9551 }
9552 else if (*p == NUL)
9553 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009554 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9555 * when finding the BF and check the previous two bytes. */
9556 else if (*p == 0xbf && enc_utf8 && !binary)
9557 {
9558 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9559 * + 1, these may be in the "prev" string. */
9560 char_u back1 = p >= buf + 1 ? p[-1]
9561 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9562 char_u back2 = p >= buf + 2 ? p[-2]
9563 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9564 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9565
9566 if (back2 == 0xef && back1 == 0xbb)
9567 {
9568 char_u *dest = p - 2;
9569
9570 /* Usually a BOM is at the beginning of a file, and so at
9571 * the beginning of a line; then we can just step over it.
9572 */
9573 if (start == dest)
9574 start = p + 1;
9575 else
9576 {
9577 /* have to shuffle buf to close gap */
9578 int adjust_prevlen = 0;
9579
9580 if (dest < buf)
9581 {
9582 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9583 dest = buf;
9584 }
9585 if (readlen > p - buf + 1)
9586 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9587 readlen -= 3 - adjust_prevlen;
9588 prevlen -= adjust_prevlen;
9589 p = dest - 1;
9590 }
9591 }
9592 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009593 } /* for */
9594
9595 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9596 break;
9597 if (start < p)
9598 {
9599 /* There's part of a line in buf, store it in "prev". */
9600 if (p - start + prevlen >= prevsize)
9601 {
9602 /* need bigger "prev" buffer */
9603 char_u *newprev;
9604
9605 /* A common use case is ordinary text files and "prev" gets a
9606 * fragment of a line, so the first allocation is made
9607 * small, to avoid repeatedly 'allocing' large and
9608 * 'reallocing' small. */
9609 if (prevsize == 0)
9610 prevsize = (long)(p - start);
9611 else
9612 {
9613 long grow50pc = (prevsize * 3) / 2;
9614 long growmin = (long)((p - start) * 2 + prevlen);
9615 prevsize = grow50pc > growmin ? grow50pc : growmin;
9616 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009617 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 if (newprev == NULL)
9619 {
9620 do_outofmem_msg((long_u)prevsize);
9621 failed = TRUE;
9622 break;
9623 }
9624 prev = newprev;
9625 }
9626 /* Add the line part to end of "prev". */
9627 mch_memmove(prev + prevlen, start, p - start);
9628 prevlen += (long)(p - start);
9629 }
9630 } /* while */
9631
9632 /*
9633 * For a negative line count use only the lines at the end of the file,
9634 * free the rest.
9635 */
9636 if (!failed && maxline < 0)
9637 while (cnt > -maxline)
9638 {
9639 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9640 --cnt;
9641 }
9642
9643 if (failed)
9644 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009645 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009646 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009647 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009648 }
9649
9650 vim_free(prev);
9651 fclose(fd);
9652}
9653
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009654 static void
9655return_register(int regname, typval_T *rettv)
9656{
9657 char_u buf[2] = {0, 0};
9658
9659 buf[0] = (char_u)regname;
9660 rettv->v_type = VAR_STRING;
9661 rettv->vval.v_string = vim_strsave(buf);
9662}
9663
9664/*
9665 * "reg_executing()" function
9666 */
9667 static void
9668f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9669{
9670 return_register(reg_executing, rettv);
9671}
9672
9673/*
9674 * "reg_recording()" function
9675 */
9676 static void
9677f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9678{
9679 return_register(reg_recording, rettv);
9680}
9681
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009683/*
9684 * Convert a List to proftime_T.
9685 * Return FAIL when there is something wrong.
9686 */
9687 static int
9688list2proftime(typval_T *arg, proftime_T *tm)
9689{
9690 long n1, n2;
9691 int error = FALSE;
9692
9693 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9694 || arg->vval.v_list->lv_len != 2)
9695 return FAIL;
9696 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9697 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009698# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009699 tm->HighPart = n1;
9700 tm->LowPart = n2;
9701# else
9702 tm->tv_sec = n1;
9703 tm->tv_usec = n2;
9704# endif
9705 return error ? FAIL : OK;
9706}
9707#endif /* FEAT_RELTIME */
9708
9709/*
9710 * "reltime()" function
9711 */
9712 static void
9713f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9714{
9715#ifdef FEAT_RELTIME
9716 proftime_T res;
9717 proftime_T start;
9718
9719 if (argvars[0].v_type == VAR_UNKNOWN)
9720 {
9721 /* No arguments: get current time. */
9722 profile_start(&res);
9723 }
9724 else if (argvars[1].v_type == VAR_UNKNOWN)
9725 {
9726 if (list2proftime(&argvars[0], &res) == FAIL)
9727 return;
9728 profile_end(&res);
9729 }
9730 else
9731 {
9732 /* Two arguments: compute the difference. */
9733 if (list2proftime(&argvars[0], &start) == FAIL
9734 || list2proftime(&argvars[1], &res) == FAIL)
9735 return;
9736 profile_sub(&res, &start);
9737 }
9738
9739 if (rettv_list_alloc(rettv) == OK)
9740 {
9741 long n1, n2;
9742
Bram Moolenaar4f974752019-02-17 17:44:42 +01009743# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009744 n1 = res.HighPart;
9745 n2 = res.LowPart;
9746# else
9747 n1 = res.tv_sec;
9748 n2 = res.tv_usec;
9749# endif
9750 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9751 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9752 }
9753#endif
9754}
9755
9756#ifdef FEAT_FLOAT
9757/*
9758 * "reltimefloat()" function
9759 */
9760 static void
9761f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9762{
9763# ifdef FEAT_RELTIME
9764 proftime_T tm;
9765# endif
9766
9767 rettv->v_type = VAR_FLOAT;
9768 rettv->vval.v_float = 0;
9769# ifdef FEAT_RELTIME
9770 if (list2proftime(&argvars[0], &tm) == OK)
9771 rettv->vval.v_float = profile_float(&tm);
9772# endif
9773}
9774#endif
9775
9776/*
9777 * "reltimestr()" function
9778 */
9779 static void
9780f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9781{
9782#ifdef FEAT_RELTIME
9783 proftime_T tm;
9784#endif
9785
9786 rettv->v_type = VAR_STRING;
9787 rettv->vval.v_string = NULL;
9788#ifdef FEAT_RELTIME
9789 if (list2proftime(&argvars[0], &tm) == OK)
9790 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9791#endif
9792}
9793
9794#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009795 static void
9796make_connection(void)
9797{
9798 if (X_DISPLAY == NULL
9799# ifdef FEAT_GUI
9800 && !gui.in_use
9801# endif
9802 )
9803 {
9804 x_force_connect = TRUE;
9805 setup_term_clip();
9806 x_force_connect = FALSE;
9807 }
9808}
9809
9810 static int
9811check_connection(void)
9812{
9813 make_connection();
9814 if (X_DISPLAY == NULL)
9815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009816 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009817 return FAIL;
9818 }
9819 return OK;
9820}
9821#endif
9822
9823#ifdef FEAT_CLIENTSERVER
9824 static void
9825remote_common(typval_T *argvars, typval_T *rettv, int expr)
9826{
9827 char_u *server_name;
9828 char_u *keys;
9829 char_u *r = NULL;
9830 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009831 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009832# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009833 HWND w;
9834# else
9835 Window w;
9836# endif
9837
9838 if (check_restricted() || check_secure())
9839 return;
9840
9841# ifdef FEAT_X11
9842 if (check_connection() == FAIL)
9843 return;
9844# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009845 if (argvars[2].v_type != VAR_UNKNOWN
9846 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009847 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009849 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850 if (server_name == NULL)
9851 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009852 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009853# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009854 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009855# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009856 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9857 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858# endif
9859 {
9860 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009862 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009863 vim_free(r);
9864 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009865 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009866 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009867 return;
9868 }
9869
9870 rettv->vval.v_string = r;
9871
9872 if (argvars[2].v_type != VAR_UNKNOWN)
9873 {
9874 dictitem_T v;
9875 char_u str[30];
9876 char_u *idvar;
9877
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009878 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009879 if (idvar != NULL && *idvar != NUL)
9880 {
9881 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9882 v.di_tv.v_type = VAR_STRING;
9883 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009885 vim_free(v.di_tv.vval.v_string);
9886 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009887 }
9888}
9889#endif
9890
9891/*
9892 * "remote_expr()" function
9893 */
9894 static void
9895f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9896{
9897 rettv->v_type = VAR_STRING;
9898 rettv->vval.v_string = NULL;
9899#ifdef FEAT_CLIENTSERVER
9900 remote_common(argvars, rettv, TRUE);
9901#endif
9902}
9903
9904/*
9905 * "remote_foreground()" function
9906 */
9907 static void
9908f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9909{
9910#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009911# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912 /* On Win32 it's done in this application. */
9913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009914 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009915
9916 if (server_name != NULL)
9917 serverForeground(server_name);
9918 }
9919# else
9920 /* Send a foreground() expression to the server. */
9921 argvars[1].v_type = VAR_STRING;
9922 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9923 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009924 rettv->v_type = VAR_STRING;
9925 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009926 remote_common(argvars, rettv, TRUE);
9927 vim_free(argvars[1].vval.v_string);
9928# endif
9929#endif
9930}
9931
9932 static void
9933f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9934{
9935#ifdef FEAT_CLIENTSERVER
9936 dictitem_T v;
9937 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009938# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009939 long_u n = 0;
9940# endif
9941 char_u *serverid;
9942
9943 if (check_restricted() || check_secure())
9944 {
9945 rettv->vval.v_number = -1;
9946 return;
9947 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009948 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009949 if (serverid == NULL)
9950 {
9951 rettv->vval.v_number = -1;
9952 return; /* type error; errmsg already given */
9953 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009954# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009955 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9956 if (n == 0)
9957 rettv->vval.v_number = -1;
9958 else
9959 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009960 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009961 rettv->vval.v_number = (s != NULL);
9962 }
9963# else
9964 if (check_connection() == FAIL)
9965 return;
9966
9967 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9968 serverStrToWin(serverid), &s);
9969# endif
9970
9971 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9972 {
9973 char_u *retvar;
9974
9975 v.di_tv.v_type = VAR_STRING;
9976 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009977 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009978 if (retvar != NULL)
9979 set_var(retvar, &v.di_tv, FALSE);
9980 vim_free(v.di_tv.vval.v_string);
9981 }
9982#else
9983 rettv->vval.v_number = -1;
9984#endif
9985}
9986
9987 static void
9988f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9989{
9990 char_u *r = NULL;
9991
9992#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009993 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009994
9995 if (serverid != NULL && !check_restricted() && !check_secure())
9996 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009997 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009998# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009999 /* The server's HWND is encoded in the 'id' parameter */
10000 long_u n = 0;
10001# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010002
10003 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010004 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010005
Bram Moolenaar4f974752019-02-17 17:44:42 +010010006# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010007 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10008 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010009 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010 if (r == NULL)
10011# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010012 if (check_connection() == FAIL
10013 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10014 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010015# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010016 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010017 }
10018#endif
10019 rettv->v_type = VAR_STRING;
10020 rettv->vval.v_string = r;
10021}
10022
10023/*
10024 * "remote_send()" function
10025 */
10026 static void
10027f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10028{
10029 rettv->v_type = VAR_STRING;
10030 rettv->vval.v_string = NULL;
10031#ifdef FEAT_CLIENTSERVER
10032 remote_common(argvars, rettv, FALSE);
10033#endif
10034}
10035
10036/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010037 * "remote_startserver()" function
10038 */
10039 static void
10040f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10041{
10042#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010043 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010044
10045 if (server == NULL)
10046 return; /* type error; errmsg already given */
10047 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010048 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010049 else
10050 {
10051# ifdef FEAT_X11
10052 if (check_connection() == OK)
10053 serverRegisterName(X_DISPLAY, server);
10054# else
10055 serverSetName(server);
10056# endif
10057 }
10058#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010059 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010060#endif
10061}
10062
10063/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010064 * "remove()" function
10065 */
10066 static void
10067f_remove(typval_T *argvars, typval_T *rettv)
10068{
10069 list_T *l;
10070 listitem_T *item, *item2;
10071 listitem_T *li;
10072 long idx;
10073 long end;
10074 char_u *key;
10075 dict_T *d;
10076 dictitem_T *di;
10077 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010078 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010079
10080 if (argvars[0].v_type == VAR_DICT)
10081 {
10082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010083 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010084 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010085 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010086 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010087 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 if (key != NULL)
10089 {
10090 di = dict_find(d, key, -1);
10091 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010092 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010093 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10094 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10095 {
10096 *rettv = di->di_tv;
10097 init_tv(&di->di_tv);
10098 dictitem_remove(d, di);
10099 }
10100 }
10101 }
10102 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010103 else if (argvars[0].v_type == VAR_BLOB)
10104 {
10105 idx = (long)tv_get_number_chk(&argvars[1], &error);
10106 if (!error)
10107 {
10108 blob_T *b = argvars[0].vval.v_blob;
10109 int len = blob_len(b);
10110 char_u *p;
10111
10112 if (idx < 0)
10113 // count from the end
10114 idx = len + idx;
10115 if (idx < 0 || idx >= len)
10116 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010117 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010118 return;
10119 }
10120 if (argvars[2].v_type == VAR_UNKNOWN)
10121 {
10122 // Remove one item, return its value.
10123 p = (char_u *)b->bv_ga.ga_data;
10124 rettv->vval.v_number = (varnumber_T) *(p + idx);
10125 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10126 --b->bv_ga.ga_len;
10127 }
10128 else
10129 {
10130 blob_T *blob;
10131
10132 // Remove range of items, return list with values.
10133 end = (long)tv_get_number_chk(&argvars[2], &error);
10134 if (error)
10135 return;
10136 if (end < 0)
10137 // count from the end
10138 end = len + end;
10139 if (end >= len || idx > end)
10140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010141 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010142 return;
10143 }
10144 blob = blob_alloc();
10145 if (blob == NULL)
10146 return;
10147 blob->bv_ga.ga_len = end - idx + 1;
10148 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10149 {
10150 vim_free(blob);
10151 return;
10152 }
10153 p = (char_u *)b->bv_ga.ga_data;
10154 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10155 (size_t)(end - idx + 1));
10156 ++blob->bv_refcount;
10157 rettv->v_type = VAR_BLOB;
10158 rettv->vval.v_blob = blob;
10159
10160 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10161 b->bv_ga.ga_len -= end - idx + 1;
10162 }
10163 }
10164 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010165 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010166 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010167 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010168 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010169 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010170 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010171 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010172 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010173 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010174 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010175 else
10176 {
10177 if (argvars[2].v_type == VAR_UNKNOWN)
10178 {
10179 /* Remove one item, return its value. */
10180 vimlist_remove(l, item, item);
10181 *rettv = item->li_tv;
10182 vim_free(item);
10183 }
10184 else
10185 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010186 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010187 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010188 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010189 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010191 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 else
10193 {
10194 int cnt = 0;
10195
10196 for (li = item; li != NULL; li = li->li_next)
10197 {
10198 ++cnt;
10199 if (li == item2)
10200 break;
10201 }
10202 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010203 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010204 else
10205 {
10206 vimlist_remove(l, item, item2);
10207 if (rettv_list_alloc(rettv) == OK)
10208 {
10209 l = rettv->vval.v_list;
10210 l->lv_first = item;
10211 l->lv_last = item2;
10212 item->li_prev = NULL;
10213 item2->li_next = NULL;
10214 l->lv_len = cnt;
10215 }
10216 }
10217 }
10218 }
10219 }
10220 }
10221}
10222
10223/*
10224 * "rename({from}, {to})" function
10225 */
10226 static void
10227f_rename(typval_T *argvars, typval_T *rettv)
10228{
10229 char_u buf[NUMBUFLEN];
10230
10231 if (check_restricted() || check_secure())
10232 rettv->vval.v_number = -1;
10233 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010234 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10235 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010236}
10237
10238/*
10239 * "repeat()" function
10240 */
10241 static void
10242f_repeat(typval_T *argvars, typval_T *rettv)
10243{
10244 char_u *p;
10245 int n;
10246 int slen;
10247 int len;
10248 char_u *r;
10249 int i;
10250
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010251 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010252 if (argvars[0].v_type == VAR_LIST)
10253 {
10254 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10255 while (n-- > 0)
10256 if (list_extend(rettv->vval.v_list,
10257 argvars[0].vval.v_list, NULL) == FAIL)
10258 break;
10259 }
10260 else
10261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010262 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010263 rettv->v_type = VAR_STRING;
10264 rettv->vval.v_string = NULL;
10265
10266 slen = (int)STRLEN(p);
10267 len = slen * n;
10268 if (len <= 0)
10269 return;
10270
10271 r = alloc(len + 1);
10272 if (r != NULL)
10273 {
10274 for (i = 0; i < n; i++)
10275 mch_memmove(r + i * slen, p, (size_t)slen);
10276 r[len] = NUL;
10277 }
10278
10279 rettv->vval.v_string = r;
10280 }
10281}
10282
10283/*
10284 * "resolve()" function
10285 */
10286 static void
10287f_resolve(typval_T *argvars, typval_T *rettv)
10288{
10289 char_u *p;
10290#ifdef HAVE_READLINK
10291 char_u *buf = NULL;
10292#endif
10293
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010294 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295#ifdef FEAT_SHORTCUT
10296 {
10297 char_u *v = NULL;
10298
Bram Moolenaardce1e892019-02-10 23:18:53 +010010299 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010300 if (v != NULL)
10301 rettv->vval.v_string = v;
10302 else
10303 rettv->vval.v_string = vim_strsave(p);
10304 }
10305#else
10306# ifdef HAVE_READLINK
10307 {
10308 char_u *cpy;
10309 int len;
10310 char_u *remain = NULL;
10311 char_u *q;
10312 int is_relative_to_current = FALSE;
10313 int has_trailing_pathsep = FALSE;
10314 int limit = 100;
10315
10316 p = vim_strsave(p);
10317
10318 if (p[0] == '.' && (vim_ispathsep(p[1])
10319 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10320 is_relative_to_current = TRUE;
10321
10322 len = STRLEN(p);
10323 if (len > 0 && after_pathsep(p, p + len))
10324 {
10325 has_trailing_pathsep = TRUE;
10326 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10327 }
10328
10329 q = getnextcomp(p);
10330 if (*q != NUL)
10331 {
10332 /* Separate the first path component in "p", and keep the
10333 * remainder (beginning with the path separator). */
10334 remain = vim_strsave(q - 1);
10335 q[-1] = NUL;
10336 }
10337
10338 buf = alloc(MAXPATHL + 1);
10339 if (buf == NULL)
10340 goto fail;
10341
10342 for (;;)
10343 {
10344 for (;;)
10345 {
10346 len = readlink((char *)p, (char *)buf, MAXPATHL);
10347 if (len <= 0)
10348 break;
10349 buf[len] = NUL;
10350
10351 if (limit-- == 0)
10352 {
10353 vim_free(p);
10354 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010355 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010356 rettv->vval.v_string = NULL;
10357 goto fail;
10358 }
10359
10360 /* Ensure that the result will have a trailing path separator
10361 * if the argument has one. */
10362 if (remain == NULL && has_trailing_pathsep)
10363 add_pathsep(buf);
10364
10365 /* Separate the first path component in the link value and
10366 * concatenate the remainders. */
10367 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10368 if (*q != NUL)
10369 {
10370 if (remain == NULL)
10371 remain = vim_strsave(q - 1);
10372 else
10373 {
10374 cpy = concat_str(q - 1, remain);
10375 if (cpy != NULL)
10376 {
10377 vim_free(remain);
10378 remain = cpy;
10379 }
10380 }
10381 q[-1] = NUL;
10382 }
10383
10384 q = gettail(p);
10385 if (q > p && *q == NUL)
10386 {
10387 /* Ignore trailing path separator. */
10388 q[-1] = NUL;
10389 q = gettail(p);
10390 }
10391 if (q > p && !mch_isFullName(buf))
10392 {
10393 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010394 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010395 if (cpy != NULL)
10396 {
10397 STRCPY(cpy, p);
10398 STRCPY(gettail(cpy), buf);
10399 vim_free(p);
10400 p = cpy;
10401 }
10402 }
10403 else
10404 {
10405 vim_free(p);
10406 p = vim_strsave(buf);
10407 }
10408 }
10409
10410 if (remain == NULL)
10411 break;
10412
10413 /* Append the first path component of "remain" to "p". */
10414 q = getnextcomp(remain + 1);
10415 len = q - remain - (*q != NUL);
10416 cpy = vim_strnsave(p, STRLEN(p) + len);
10417 if (cpy != NULL)
10418 {
10419 STRNCAT(cpy, remain, len);
10420 vim_free(p);
10421 p = cpy;
10422 }
10423 /* Shorten "remain". */
10424 if (*q != NUL)
10425 STRMOVE(remain, q - 1);
10426 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010427 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010428 }
10429
10430 /* If the result is a relative path name, make it explicitly relative to
10431 * the current directory if and only if the argument had this form. */
10432 if (!vim_ispathsep(*p))
10433 {
10434 if (is_relative_to_current
10435 && *p != NUL
10436 && !(p[0] == '.'
10437 && (p[1] == NUL
10438 || vim_ispathsep(p[1])
10439 || (p[1] == '.'
10440 && (p[2] == NUL
10441 || vim_ispathsep(p[2]))))))
10442 {
10443 /* Prepend "./". */
10444 cpy = concat_str((char_u *)"./", p);
10445 if (cpy != NULL)
10446 {
10447 vim_free(p);
10448 p = cpy;
10449 }
10450 }
10451 else if (!is_relative_to_current)
10452 {
10453 /* Strip leading "./". */
10454 q = p;
10455 while (q[0] == '.' && vim_ispathsep(q[1]))
10456 q += 2;
10457 if (q > p)
10458 STRMOVE(p, p + 2);
10459 }
10460 }
10461
10462 /* Ensure that the result will have no trailing path separator
10463 * if the argument had none. But keep "/" or "//". */
10464 if (!has_trailing_pathsep)
10465 {
10466 q = p + STRLEN(p);
10467 if (after_pathsep(p, q))
10468 *gettail_sep(p) = NUL;
10469 }
10470
10471 rettv->vval.v_string = p;
10472 }
10473# else
10474 rettv->vval.v_string = vim_strsave(p);
10475# endif
10476#endif
10477
10478 simplify_filename(rettv->vval.v_string);
10479
10480#ifdef HAVE_READLINK
10481fail:
10482 vim_free(buf);
10483#endif
10484 rettv->v_type = VAR_STRING;
10485}
10486
10487/*
10488 * "reverse({list})" function
10489 */
10490 static void
10491f_reverse(typval_T *argvars, typval_T *rettv)
10492{
10493 list_T *l;
10494 listitem_T *li, *ni;
10495
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010496 if (argvars[0].v_type == VAR_BLOB)
10497 {
10498 blob_T *b = argvars[0].vval.v_blob;
10499 int i, len = blob_len(b);
10500
10501 for (i = 0; i < len / 2; i++)
10502 {
10503 int tmp = blob_get(b, i);
10504
10505 blob_set(b, i, blob_get(b, len - i - 1));
10506 blob_set(b, len - i - 1, tmp);
10507 }
10508 rettv_blob_set(rettv, b);
10509 return;
10510 }
10511
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010512 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010513 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010514 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010515 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010516 (char_u *)N_("reverse() argument"), TRUE))
10517 {
10518 li = l->lv_last;
10519 l->lv_first = l->lv_last = NULL;
10520 l->lv_len = 0;
10521 while (li != NULL)
10522 {
10523 ni = li->li_prev;
10524 list_append(l, li);
10525 li = ni;
10526 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010527 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010528 l->lv_idx = l->lv_len - l->lv_idx - 1;
10529 }
10530}
10531
10532#define SP_NOMOVE 0x01 /* don't move cursor */
10533#define SP_REPEAT 0x02 /* repeat to find outer pair */
10534#define SP_RETCOUNT 0x04 /* return matchcount */
10535#define SP_SETPCMARK 0x08 /* set previous context mark */
10536#define SP_START 0x10 /* accept match at start position */
10537#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10538#define SP_END 0x40 /* leave cursor at end of match */
10539#define SP_COLUMN 0x80 /* start at cursor column */
10540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010541/*
10542 * Get flags for a search function.
10543 * Possibly sets "p_ws".
10544 * Returns BACKWARD, FORWARD or zero (for an error).
10545 */
10546 static int
10547get_search_arg(typval_T *varp, int *flagsp)
10548{
10549 int dir = FORWARD;
10550 char_u *flags;
10551 char_u nbuf[NUMBUFLEN];
10552 int mask;
10553
10554 if (varp->v_type != VAR_UNKNOWN)
10555 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010556 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010557 if (flags == NULL)
10558 return 0; /* type error; errmsg already given */
10559 while (*flags != NUL)
10560 {
10561 switch (*flags)
10562 {
10563 case 'b': dir = BACKWARD; break;
10564 case 'w': p_ws = TRUE; break;
10565 case 'W': p_ws = FALSE; break;
10566 default: mask = 0;
10567 if (flagsp != NULL)
10568 switch (*flags)
10569 {
10570 case 'c': mask = SP_START; break;
10571 case 'e': mask = SP_END; break;
10572 case 'm': mask = SP_RETCOUNT; break;
10573 case 'n': mask = SP_NOMOVE; break;
10574 case 'p': mask = SP_SUBPAT; break;
10575 case 'r': mask = SP_REPEAT; break;
10576 case 's': mask = SP_SETPCMARK; break;
10577 case 'z': mask = SP_COLUMN; break;
10578 }
10579 if (mask == 0)
10580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010581 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 dir = 0;
10583 }
10584 else
10585 *flagsp |= mask;
10586 }
10587 if (dir == 0)
10588 break;
10589 ++flags;
10590 }
10591 }
10592 return dir;
10593}
10594
10595/*
10596 * Shared by search() and searchpos() functions.
10597 */
10598 static int
10599search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10600{
10601 int flags;
10602 char_u *pat;
10603 pos_T pos;
10604 pos_T save_cursor;
10605 int save_p_ws = p_ws;
10606 int dir;
10607 int retval = 0; /* default: FAIL */
10608 long lnum_stop = 0;
10609 proftime_T tm;
10610#ifdef FEAT_RELTIME
10611 long time_limit = 0;
10612#endif
10613 int options = SEARCH_KEEP;
10614 int subpatnum;
10615
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010616 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010617 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10618 if (dir == 0)
10619 goto theend;
10620 flags = *flagsp;
10621 if (flags & SP_START)
10622 options |= SEARCH_START;
10623 if (flags & SP_END)
10624 options |= SEARCH_END;
10625 if (flags & SP_COLUMN)
10626 options |= SEARCH_COL;
10627
10628 /* Optional arguments: line number to stop searching and timeout. */
10629 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10630 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010631 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010632 if (lnum_stop < 0)
10633 goto theend;
10634#ifdef FEAT_RELTIME
10635 if (argvars[3].v_type != VAR_UNKNOWN)
10636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010637 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010638 if (time_limit < 0)
10639 goto theend;
10640 }
10641#endif
10642 }
10643
10644#ifdef FEAT_RELTIME
10645 /* Set the time limit, if there is one. */
10646 profile_setlimit(time_limit, &tm);
10647#endif
10648
10649 /*
10650 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10651 * Check to make sure only those flags are set.
10652 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10653 * flags cannot be set. Check for that condition also.
10654 */
10655 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10656 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010658 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010659 goto theend;
10660 }
10661
10662 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010663 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010664 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010665 if (subpatnum != FAIL)
10666 {
10667 if (flags & SP_SUBPAT)
10668 retval = subpatnum;
10669 else
10670 retval = pos.lnum;
10671 if (flags & SP_SETPCMARK)
10672 setpcmark();
10673 curwin->w_cursor = pos;
10674 if (match_pos != NULL)
10675 {
10676 /* Store the match cursor position */
10677 match_pos->lnum = pos.lnum;
10678 match_pos->col = pos.col + 1;
10679 }
10680 /* "/$" will put the cursor after the end of the line, may need to
10681 * correct that here */
10682 check_cursor();
10683 }
10684
10685 /* If 'n' flag is used: restore cursor position. */
10686 if (flags & SP_NOMOVE)
10687 curwin->w_cursor = save_cursor;
10688 else
10689 curwin->w_set_curswant = TRUE;
10690theend:
10691 p_ws = save_p_ws;
10692
10693 return retval;
10694}
10695
10696#ifdef FEAT_FLOAT
10697
10698/*
10699 * round() is not in C90, use ceil() or floor() instead.
10700 */
10701 float_T
10702vim_round(float_T f)
10703{
10704 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10705}
10706
10707/*
10708 * "round({float})" function
10709 */
10710 static void
10711f_round(typval_T *argvars, typval_T *rettv)
10712{
10713 float_T f = 0.0;
10714
10715 rettv->v_type = VAR_FLOAT;
10716 if (get_float_arg(argvars, &f) == OK)
10717 rettv->vval.v_float = vim_round(f);
10718 else
10719 rettv->vval.v_float = 0.0;
10720}
10721#endif
10722
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010723#ifdef FEAT_RUBY
10724/*
10725 * "rubyeval()" function
10726 */
10727 static void
10728f_rubyeval(typval_T *argvars, typval_T *rettv)
10729{
10730 char_u *str;
10731 char_u buf[NUMBUFLEN];
10732
10733 str = tv_get_string_buf(&argvars[0], buf);
10734 do_rubyeval(str, rettv);
10735}
10736#endif
10737
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010738/*
10739 * "screenattr()" function
10740 */
10741 static void
10742f_screenattr(typval_T *argvars, typval_T *rettv)
10743{
10744 int row;
10745 int col;
10746 int c;
10747
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010748 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10749 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750 if (row < 0 || row >= screen_Rows
10751 || col < 0 || col >= screen_Columns)
10752 c = -1;
10753 else
10754 c = ScreenAttrs[LineOffset[row] + col];
10755 rettv->vval.v_number = c;
10756}
10757
10758/*
10759 * "screenchar()" function
10760 */
10761 static void
10762f_screenchar(typval_T *argvars, typval_T *rettv)
10763{
10764 int row;
10765 int col;
10766 int off;
10767 int c;
10768
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010769 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10770 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010771 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010772 c = -1;
10773 else
10774 {
10775 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010776 if (enc_utf8 && ScreenLinesUC[off] != 0)
10777 c = ScreenLinesUC[off];
10778 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010779 c = ScreenLines[off];
10780 }
10781 rettv->vval.v_number = c;
10782}
10783
10784/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010785 * "screenchars()" function
10786 */
10787 static void
10788f_screenchars(typval_T *argvars, typval_T *rettv)
10789{
10790 int row;
10791 int col;
10792 int off;
10793 int c;
10794 int i;
10795
10796 if (rettv_list_alloc(rettv) == FAIL)
10797 return;
10798 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10799 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10800 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10801 return;
10802
10803 off = LineOffset[row] + col;
10804 if (enc_utf8 && ScreenLinesUC[off] != 0)
10805 c = ScreenLinesUC[off];
10806 else
10807 c = ScreenLines[off];
10808 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10809
10810 if (enc_utf8)
10811
10812 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10813 list_append_number(rettv->vval.v_list,
10814 (varnumber_T)ScreenLinesC[i][off]);
10815}
10816
10817/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 * "screencol()" function
10819 *
10820 * First column is 1 to be consistent with virtcol().
10821 */
10822 static void
10823f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10824{
10825 rettv->vval.v_number = screen_screencol() + 1;
10826}
10827
10828/*
10829 * "screenrow()" function
10830 */
10831 static void
10832f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10833{
10834 rettv->vval.v_number = screen_screenrow() + 1;
10835}
10836
10837/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010838 * "screenstring()" function
10839 */
10840 static void
10841f_screenstring(typval_T *argvars, typval_T *rettv)
10842{
10843 int row;
10844 int col;
10845 int off;
10846 int c;
10847 int i;
10848 char_u buf[MB_MAXBYTES + 1];
10849 int buflen = 0;
10850
10851 rettv->vval.v_string = NULL;
10852 rettv->v_type = VAR_STRING;
10853
10854 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10855 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10856 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10857 return;
10858
10859 off = LineOffset[row] + col;
10860 if (enc_utf8 && ScreenLinesUC[off] != 0)
10861 c = ScreenLinesUC[off];
10862 else
10863 c = ScreenLines[off];
10864 buflen += mb_char2bytes(c, buf);
10865
10866 if (enc_utf8)
10867 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10868 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10869
10870 buf[buflen] = NUL;
10871 rettv->vval.v_string = vim_strsave(buf);
10872}
10873
10874/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010875 * "search()" function
10876 */
10877 static void
10878f_search(typval_T *argvars, typval_T *rettv)
10879{
10880 int flags = 0;
10881
10882 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10883}
10884
10885/*
10886 * "searchdecl()" function
10887 */
10888 static void
10889f_searchdecl(typval_T *argvars, typval_T *rettv)
10890{
10891 int locally = 1;
10892 int thisblock = 0;
10893 int error = FALSE;
10894 char_u *name;
10895
10896 rettv->vval.v_number = 1; /* default: FAIL */
10897
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 if (argvars[1].v_type != VAR_UNKNOWN)
10900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010901 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010902 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010903 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010904 }
10905 if (!error && name != NULL)
10906 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10907 locally, thisblock, SEARCH_KEEP) == FAIL;
10908}
10909
10910/*
10911 * Used by searchpair() and searchpairpos()
10912 */
10913 static int
10914searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10915{
10916 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010917 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010918 int save_p_ws = p_ws;
10919 int dir;
10920 int flags = 0;
10921 char_u nbuf1[NUMBUFLEN];
10922 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010923 int retval = 0; /* default: FAIL */
10924 long lnum_stop = 0;
10925 long time_limit = 0;
10926
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010927 /* Get the three pattern arguments: start, middle, end. Will result in an
10928 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010929 spat = tv_get_string_chk(&argvars[0]);
10930 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10931 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010932 if (spat == NULL || mpat == NULL || epat == NULL)
10933 goto theend; /* type error */
10934
10935 /* Handle the optional fourth argument: flags */
10936 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10937 if (dir == 0)
10938 goto theend;
10939
10940 /* Don't accept SP_END or SP_SUBPAT.
10941 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10942 */
10943 if ((flags & (SP_END | SP_SUBPAT)) != 0
10944 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10945 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010946 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010947 goto theend;
10948 }
10949
10950 /* Using 'r' implies 'W', otherwise it doesn't work. */
10951 if (flags & SP_REPEAT)
10952 p_ws = FALSE;
10953
10954 /* Optional fifth argument: skip expression */
10955 if (argvars[3].v_type == VAR_UNKNOWN
10956 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010957 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010958 else
10959 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010960 skip = &argvars[4];
10961 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10962 && skip->v_type != VAR_STRING)
10963 {
10964 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010965 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010966 goto theend;
10967 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (argvars[5].v_type != VAR_UNKNOWN)
10969 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010970 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010973 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010974 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010975 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010976#ifdef FEAT_RELTIME
10977 if (argvars[6].v_type != VAR_UNKNOWN)
10978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010979 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010982 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010984 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010985 }
10986#endif
10987 }
10988 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010989
10990 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10991 match_pos, lnum_stop, time_limit);
10992
10993theend:
10994 p_ws = save_p_ws;
10995
10996 return retval;
10997}
10998
10999/*
11000 * "searchpair()" function
11001 */
11002 static void
11003f_searchpair(typval_T *argvars, typval_T *rettv)
11004{
11005 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11006}
11007
11008/*
11009 * "searchpairpos()" function
11010 */
11011 static void
11012f_searchpairpos(typval_T *argvars, typval_T *rettv)
11013{
11014 pos_T match_pos;
11015 int lnum = 0;
11016 int col = 0;
11017
11018 if (rettv_list_alloc(rettv) == FAIL)
11019 return;
11020
11021 if (searchpair_cmn(argvars, &match_pos) > 0)
11022 {
11023 lnum = match_pos.lnum;
11024 col = match_pos.col;
11025 }
11026
11027 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11028 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11029}
11030
11031/*
11032 * Search for a start/middle/end thing.
11033 * Used by searchpair(), see its documentation for the details.
11034 * Returns 0 or -1 for no match,
11035 */
11036 long
11037do_searchpair(
11038 char_u *spat, /* start pattern */
11039 char_u *mpat, /* middle pattern */
11040 char_u *epat, /* end pattern */
11041 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011042 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011043 int flags, /* SP_SETPCMARK and other SP_ values */
11044 pos_T *match_pos,
11045 linenr_T lnum_stop, /* stop at this line if not zero */
11046 long time_limit UNUSED) /* stop after this many msec */
11047{
11048 char_u *save_cpo;
11049 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11050 long retval = 0;
11051 pos_T pos;
11052 pos_T firstpos;
11053 pos_T foundpos;
11054 pos_T save_cursor;
11055 pos_T save_pos;
11056 int n;
11057 int r;
11058 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011059 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011060 int err;
11061 int options = SEARCH_KEEP;
11062 proftime_T tm;
11063
11064 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11065 save_cpo = p_cpo;
11066 p_cpo = empty_option;
11067
11068#ifdef FEAT_RELTIME
11069 /* Set the time limit, if there is one. */
11070 profile_setlimit(time_limit, &tm);
11071#endif
11072
11073 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11074 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011075 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11076 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011077 if (pat2 == NULL || pat3 == NULL)
11078 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011079 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011080 if (*mpat == NUL)
11081 STRCPY(pat3, pat2);
11082 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011083 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011084 spat, epat, mpat);
11085 if (flags & SP_START)
11086 options |= SEARCH_START;
11087
Bram Moolenaar48570482017-10-30 21:48:41 +010011088 if (skip != NULL)
11089 {
11090 /* Empty string means to not use the skip expression. */
11091 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11092 use_skip = skip->vval.v_string != NULL
11093 && *skip->vval.v_string != NUL;
11094 }
11095
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096 save_cursor = curwin->w_cursor;
11097 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011098 CLEAR_POS(&firstpos);
11099 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011100 pat = pat3;
11101 for (;;)
11102 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011103 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011104 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011105 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011106 /* didn't find it or found the first match again: FAIL */
11107 break;
11108
11109 if (firstpos.lnum == 0)
11110 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011111 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011112 {
11113 /* Found the same position again. Can happen with a pattern that
11114 * has "\zs" at the end and searching backwards. Advance one
11115 * character and try again. */
11116 if (dir == BACKWARD)
11117 decl(&pos);
11118 else
11119 incl(&pos);
11120 }
11121 foundpos = pos;
11122
11123 /* clear the start flag to avoid getting stuck here */
11124 options &= ~SEARCH_START;
11125
11126 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011127 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 {
11129 save_pos = curwin->w_cursor;
11130 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011131 err = FALSE;
11132 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011133 curwin->w_cursor = save_pos;
11134 if (err)
11135 {
11136 /* Evaluating {skip} caused an error, break here. */
11137 curwin->w_cursor = save_cursor;
11138 retval = -1;
11139 break;
11140 }
11141 if (r)
11142 continue;
11143 }
11144
11145 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11146 {
11147 /* Found end when searching backwards or start when searching
11148 * forward: nested pair. */
11149 ++nest;
11150 pat = pat2; /* nested, don't search for middle */
11151 }
11152 else
11153 {
11154 /* Found end when searching forward or start when searching
11155 * backward: end of (nested) pair; or found middle in outer pair. */
11156 if (--nest == 1)
11157 pat = pat3; /* outer level, search for middle */
11158 }
11159
11160 if (nest == 0)
11161 {
11162 /* Found the match: return matchcount or line number. */
11163 if (flags & SP_RETCOUNT)
11164 ++retval;
11165 else
11166 retval = pos.lnum;
11167 if (flags & SP_SETPCMARK)
11168 setpcmark();
11169 curwin->w_cursor = pos;
11170 if (!(flags & SP_REPEAT))
11171 break;
11172 nest = 1; /* search for next unmatched */
11173 }
11174 }
11175
11176 if (match_pos != NULL)
11177 {
11178 /* Store the match cursor position */
11179 match_pos->lnum = curwin->w_cursor.lnum;
11180 match_pos->col = curwin->w_cursor.col + 1;
11181 }
11182
11183 /* If 'n' flag is used or search failed: restore cursor position. */
11184 if ((flags & SP_NOMOVE) || retval == 0)
11185 curwin->w_cursor = save_cursor;
11186
11187theend:
11188 vim_free(pat2);
11189 vim_free(pat3);
11190 if (p_cpo == empty_option)
11191 p_cpo = save_cpo;
11192 else
11193 /* Darn, evaluating the {skip} expression changed the value. */
11194 free_string_option(save_cpo);
11195
11196 return retval;
11197}
11198
11199/*
11200 * "searchpos()" function
11201 */
11202 static void
11203f_searchpos(typval_T *argvars, typval_T *rettv)
11204{
11205 pos_T match_pos;
11206 int lnum = 0;
11207 int col = 0;
11208 int n;
11209 int flags = 0;
11210
11211 if (rettv_list_alloc(rettv) == FAIL)
11212 return;
11213
11214 n = search_cmn(argvars, &match_pos, &flags);
11215 if (n > 0)
11216 {
11217 lnum = match_pos.lnum;
11218 col = match_pos.col;
11219 }
11220
11221 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11222 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11223 if (flags & SP_SUBPAT)
11224 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11225}
11226
11227 static void
11228f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11229{
11230#ifdef FEAT_CLIENTSERVER
11231 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011232 char_u *server = tv_get_string_chk(&argvars[0]);
11233 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011234
11235 rettv->vval.v_number = -1;
11236 if (server == NULL || reply == NULL)
11237 return;
11238 if (check_restricted() || check_secure())
11239 return;
11240# ifdef FEAT_X11
11241 if (check_connection() == FAIL)
11242 return;
11243# endif
11244
11245 if (serverSendReply(server, reply) < 0)
11246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011247 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011248 return;
11249 }
11250 rettv->vval.v_number = 0;
11251#else
11252 rettv->vval.v_number = -1;
11253#endif
11254}
11255
11256 static void
11257f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11258{
11259 char_u *r = NULL;
11260
11261#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011262# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011263 r = serverGetVimNames();
11264# else
11265 make_connection();
11266 if (X_DISPLAY != NULL)
11267 r = serverGetVimNames(X_DISPLAY);
11268# endif
11269#endif
11270 rettv->v_type = VAR_STRING;
11271 rettv->vval.v_string = r;
11272}
11273
11274/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011275 * "setbufline()" function
11276 */
11277 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011278f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011279{
11280 linenr_T lnum;
11281 buf_T *buf;
11282
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011283 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011284 if (buf == NULL)
11285 rettv->vval.v_number = 1; /* FAIL */
11286 else
11287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011288 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011289 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011290 }
11291}
11292
11293/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011294 * "setbufvar()" function
11295 */
11296 static void
11297f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11298{
11299 buf_T *buf;
11300 char_u *varname, *bufvarname;
11301 typval_T *varp;
11302 char_u nbuf[NUMBUFLEN];
11303
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011304 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011305 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011306 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11307 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011308 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011309 varp = &argvars[2];
11310
11311 if (buf != NULL && varname != NULL && varp != NULL)
11312 {
11313 if (*varname == '&')
11314 {
11315 long numval;
11316 char_u *strval;
11317 int error = FALSE;
11318 aco_save_T aco;
11319
11320 /* set curbuf to be our buf, temporarily */
11321 aucmd_prepbuf(&aco, buf);
11322
11323 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011324 numval = (long)tv_get_number_chk(varp, &error);
11325 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011326 if (!error && strval != NULL)
11327 set_option_value(varname, numval, strval, OPT_LOCAL);
11328
11329 /* reset notion of buffer */
11330 aucmd_restbuf(&aco);
11331 }
11332 else
11333 {
11334 buf_T *save_curbuf = curbuf;
11335
Bram Moolenaar964b3742019-05-24 18:54:09 +020011336 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011337 if (bufvarname != NULL)
11338 {
11339 curbuf = buf;
11340 STRCPY(bufvarname, "b:");
11341 STRCPY(bufvarname + 2, varname);
11342 set_var(bufvarname, varp, TRUE);
11343 vim_free(bufvarname);
11344 curbuf = save_curbuf;
11345 }
11346 }
11347 }
11348}
11349
11350 static void
11351f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11352{
11353 dict_T *d;
11354 dictitem_T *di;
11355 char_u *csearch;
11356
11357 if (argvars[0].v_type != VAR_DICT)
11358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011359 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011360 return;
11361 }
11362
11363 if ((d = argvars[0].vval.v_dict) != NULL)
11364 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011365 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011366 if (csearch != NULL)
11367 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011368 if (enc_utf8)
11369 {
11370 int pcc[MAX_MCO];
11371 int c = utfc_ptr2char(csearch, pcc);
11372
11373 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11374 }
11375 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011376 set_last_csearch(PTR2CHAR(csearch),
11377 csearch, MB_PTR2LEN(csearch));
11378 }
11379
11380 di = dict_find(d, (char_u *)"forward", -1);
11381 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011382 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 ? FORWARD : BACKWARD);
11384
11385 di = dict_find(d, (char_u *)"until", -1);
11386 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011387 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011388 }
11389}
11390
11391/*
11392 * "setcmdpos()" function
11393 */
11394 static void
11395f_setcmdpos(typval_T *argvars, typval_T *rettv)
11396{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011397 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011398
11399 if (pos >= 0)
11400 rettv->vval.v_number = set_cmdline_pos(pos);
11401}
11402
11403/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011404 * "setenv()" function
11405 */
11406 static void
11407f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11408{
11409 char_u namebuf[NUMBUFLEN];
11410 char_u valbuf[NUMBUFLEN];
11411 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11412
11413 if (argvars[1].v_type == VAR_SPECIAL
11414 && argvars[1].vval.v_number == VVAL_NULL)
11415 vim_unsetenv(name);
11416 else
11417 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11418}
11419
11420/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011421 * "setfperm({fname}, {mode})" function
11422 */
11423 static void
11424f_setfperm(typval_T *argvars, typval_T *rettv)
11425{
11426 char_u *fname;
11427 char_u modebuf[NUMBUFLEN];
11428 char_u *mode_str;
11429 int i;
11430 int mask;
11431 int mode = 0;
11432
11433 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011434 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011435 if (fname == NULL)
11436 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011437 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 if (mode_str == NULL)
11439 return;
11440 if (STRLEN(mode_str) != 9)
11441 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011442 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011443 return;
11444 }
11445
11446 mask = 1;
11447 for (i = 8; i >= 0; --i)
11448 {
11449 if (mode_str[i] != '-')
11450 mode |= mask;
11451 mask = mask << 1;
11452 }
11453 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11454}
11455
11456/*
11457 * "setline()" function
11458 */
11459 static void
11460f_setline(typval_T *argvars, typval_T *rettv)
11461{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011462 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463
Bram Moolenaarca851592018-06-06 21:04:07 +020011464 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011465}
11466
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011467/*
11468 * Used by "setqflist()" and "setloclist()" functions
11469 */
11470 static void
11471set_qf_ll_list(
11472 win_T *wp UNUSED,
11473 typval_T *list_arg UNUSED,
11474 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011475 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011476 typval_T *rettv)
11477{
11478#ifdef FEAT_QUICKFIX
11479 static char *e_invact = N_("E927: Invalid action: '%s'");
11480 char_u *act;
11481 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011482 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011483#endif
11484
11485 rettv->vval.v_number = -1;
11486
11487#ifdef FEAT_QUICKFIX
11488 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011489 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011490 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011491 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492 else
11493 {
11494 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011495 dict_T *d = NULL;
11496 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011497
11498 if (action_arg->v_type == VAR_STRING)
11499 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011500 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 if (act == NULL)
11502 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011503 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11504 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011505 action = *act;
11506 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011507 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508 }
11509 else if (action_arg->v_type == VAR_UNKNOWN)
11510 action = ' ';
11511 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011512 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011513
Bram Moolenaard823fa92016-08-12 16:29:27 +020011514 if (action_arg->v_type != VAR_UNKNOWN
11515 && what_arg->v_type != VAR_UNKNOWN)
11516 {
11517 if (what_arg->v_type == VAR_DICT)
11518 d = what_arg->vval.v_dict;
11519 else
11520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011521 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011522 valid_dict = FALSE;
11523 }
11524 }
11525
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011526 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011527 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011528 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11529 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011530 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011531 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011532 }
11533#endif
11534}
11535
11536/*
11537 * "setloclist()" function
11538 */
11539 static void
11540f_setloclist(typval_T *argvars, typval_T *rettv)
11541{
11542 win_T *win;
11543
11544 rettv->vval.v_number = -1;
11545
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011546 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011547 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011548 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549}
11550
11551/*
11552 * "setmatches()" function
11553 */
11554 static void
11555f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11556{
11557#ifdef FEAT_SEARCH_EXTRA
11558 list_T *l;
11559 listitem_T *li;
11560 dict_T *d;
11561 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011562 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011563
11564 rettv->vval.v_number = -1;
11565 if (argvars[0].v_type != VAR_LIST)
11566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011567 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 return;
11569 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011570 if (win == NULL)
11571 return;
11572
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011573 if ((l = argvars[0].vval.v_list) != NULL)
11574 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011575 /* To some extent make sure that we are dealing with a list from
11576 * "getmatches()". */
11577 li = l->lv_first;
11578 while (li != NULL)
11579 {
11580 if (li->li_tv.v_type != VAR_DICT
11581 || (d = li->li_tv.vval.v_dict) == NULL)
11582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011583 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011584 return;
11585 }
11586 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11587 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11588 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11589 && dict_find(d, (char_u *)"priority", -1) != NULL
11590 && dict_find(d, (char_u *)"id", -1) != NULL))
11591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011592 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011593 return;
11594 }
11595 li = li->li_next;
11596 }
11597
Bram Moolenaaraff74912019-03-30 18:11:49 +010011598 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011599 li = l->lv_first;
11600 while (li != NULL)
11601 {
11602 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011603 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 dictitem_T *di;
11605 char_u *group;
11606 int priority;
11607 int id;
11608 char_u *conceal;
11609
11610 d = li->li_tv.vval.v_dict;
11611 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11612 {
11613 if (s == NULL)
11614 {
11615 s = list_alloc();
11616 if (s == NULL)
11617 return;
11618 }
11619
11620 /* match from matchaddpos() */
11621 for (i = 1; i < 9; i++)
11622 {
11623 sprintf((char *)buf, (char *)"pos%d", i);
11624 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11625 {
11626 if (di->di_tv.v_type != VAR_LIST)
11627 return;
11628
11629 list_append_tv(s, &di->di_tv);
11630 s->lv_refcount++;
11631 }
11632 else
11633 break;
11634 }
11635 }
11636
Bram Moolenaar8f667172018-12-14 15:38:31 +010011637 group = dict_get_string(d, (char_u *)"group", TRUE);
11638 priority = (int)dict_get_number(d, (char_u *)"priority");
11639 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011641 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011642 : NULL;
11643 if (i == 0)
11644 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011645 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011646 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011647 priority, id, NULL, conceal);
11648 }
11649 else
11650 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011651 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011652 list_unref(s);
11653 s = NULL;
11654 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011655 vim_free(group);
11656 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011657
11658 li = li->li_next;
11659 }
11660 rettv->vval.v_number = 0;
11661 }
11662#endif
11663}
11664
11665/*
11666 * "setpos()" function
11667 */
11668 static void
11669f_setpos(typval_T *argvars, typval_T *rettv)
11670{
11671 pos_T pos;
11672 int fnum;
11673 char_u *name;
11674 colnr_T curswant = -1;
11675
11676 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011677 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 if (name != NULL)
11679 {
11680 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11681 {
11682 if (--pos.col < 0)
11683 pos.col = 0;
11684 if (name[0] == '.' && name[1] == NUL)
11685 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011686 /* set cursor; "fnum" is ignored */
11687 curwin->w_cursor = pos;
11688 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011689 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011690 curwin->w_curswant = curswant - 1;
11691 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011692 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011693 check_cursor();
11694 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011695 }
11696 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11697 {
11698 /* set mark */
11699 if (setmark_pos(name[1], &pos, fnum) == OK)
11700 rettv->vval.v_number = 0;
11701 }
11702 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011703 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011704 }
11705 }
11706}
11707
11708/*
11709 * "setqflist()" function
11710 */
11711 static void
11712f_setqflist(typval_T *argvars, typval_T *rettv)
11713{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011714 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011715}
11716
11717/*
11718 * "setreg()" function
11719 */
11720 static void
11721f_setreg(typval_T *argvars, typval_T *rettv)
11722{
11723 int regname;
11724 char_u *strregname;
11725 char_u *stropt;
11726 char_u *strval;
11727 int append;
11728 char_u yank_type;
11729 long block_len;
11730
11731 block_len = -1;
11732 yank_type = MAUTO;
11733 append = FALSE;
11734
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011735 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011736 rettv->vval.v_number = 1; /* FAIL is default */
11737
11738 if (strregname == NULL)
11739 return; /* type error; errmsg already given */
11740 regname = *strregname;
11741 if (regname == 0 || regname == '@')
11742 regname = '"';
11743
11744 if (argvars[2].v_type != VAR_UNKNOWN)
11745 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011746 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011747 if (stropt == NULL)
11748 return; /* type error */
11749 for (; *stropt != NUL; ++stropt)
11750 switch (*stropt)
11751 {
11752 case 'a': case 'A': /* append */
11753 append = TRUE;
11754 break;
11755 case 'v': case 'c': /* character-wise selection */
11756 yank_type = MCHAR;
11757 break;
11758 case 'V': case 'l': /* line-wise selection */
11759 yank_type = MLINE;
11760 break;
11761 case 'b': case Ctrl_V: /* block-wise selection */
11762 yank_type = MBLOCK;
11763 if (VIM_ISDIGIT(stropt[1]))
11764 {
11765 ++stropt;
11766 block_len = getdigits(&stropt) - 1;
11767 --stropt;
11768 }
11769 break;
11770 }
11771 }
11772
11773 if (argvars[1].v_type == VAR_LIST)
11774 {
11775 char_u **lstval;
11776 char_u **allocval;
11777 char_u buf[NUMBUFLEN];
11778 char_u **curval;
11779 char_u **curallocval;
11780 list_T *ll = argvars[1].vval.v_list;
11781 listitem_T *li;
11782 int len;
11783
11784 /* If the list is NULL handle like an empty list. */
11785 len = ll == NULL ? 0 : ll->lv_len;
11786
11787 /* First half: use for pointers to result lines; second half: use for
11788 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011789 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011790 if (lstval == NULL)
11791 return;
11792 curval = lstval;
11793 allocval = lstval + len + 2;
11794 curallocval = allocval;
11795
11796 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11797 li = li->li_next)
11798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011799 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011800 if (strval == NULL)
11801 goto free_lstval;
11802 if (strval == buf)
11803 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011804 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011805 * overwrite the string. */
11806 strval = vim_strsave(buf);
11807 if (strval == NULL)
11808 goto free_lstval;
11809 *curallocval++ = strval;
11810 }
11811 *curval++ = strval;
11812 }
11813 *curval++ = NULL;
11814
11815 write_reg_contents_lst(regname, lstval, -1,
11816 append, yank_type, block_len);
11817free_lstval:
11818 while (curallocval > allocval)
11819 vim_free(*--curallocval);
11820 vim_free(lstval);
11821 }
11822 else
11823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011824 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011825 if (strval == NULL)
11826 return;
11827 write_reg_contents_ex(regname, strval, -1,
11828 append, yank_type, block_len);
11829 }
11830 rettv->vval.v_number = 0;
11831}
11832
11833/*
11834 * "settabvar()" function
11835 */
11836 static void
11837f_settabvar(typval_T *argvars, typval_T *rettv)
11838{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011839 tabpage_T *save_curtab;
11840 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011841 char_u *varname, *tabvarname;
11842 typval_T *varp;
11843
11844 rettv->vval.v_number = 0;
11845
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011846 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011847 return;
11848
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011849 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11850 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011851 varp = &argvars[2];
11852
Bram Moolenaar4033c552017-09-16 20:54:51 +020011853 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011855 save_curtab = curtab;
11856 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011857
Bram Moolenaar964b3742019-05-24 18:54:09 +020011858 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011859 if (tabvarname != NULL)
11860 {
11861 STRCPY(tabvarname, "t:");
11862 STRCPY(tabvarname + 2, varname);
11863 set_var(tabvarname, varp, TRUE);
11864 vim_free(tabvarname);
11865 }
11866
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011867 /* Restore current tabpage */
11868 if (valid_tabpage(save_curtab))
11869 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011870 }
11871}
11872
11873/*
11874 * "settabwinvar()" function
11875 */
11876 static void
11877f_settabwinvar(typval_T *argvars, typval_T *rettv)
11878{
11879 setwinvar(argvars, rettv, 1);
11880}
11881
11882/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011883 * "settagstack()" function
11884 */
11885 static void
11886f_settagstack(typval_T *argvars, typval_T *rettv)
11887{
11888 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11889 win_T *wp;
11890 dict_T *d;
11891 int action = 'r';
11892
11893 rettv->vval.v_number = -1;
11894
11895 // first argument: window number or id
11896 wp = find_win_by_nr_or_id(&argvars[0]);
11897 if (wp == NULL)
11898 return;
11899
11900 // second argument: dict with items to set in the tag stack
11901 if (argvars[1].v_type != VAR_DICT)
11902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011903 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011904 return;
11905 }
11906 d = argvars[1].vval.v_dict;
11907 if (d == NULL)
11908 return;
11909
11910 // third argument: action - 'a' for append and 'r' for replace.
11911 // default is to replace the stack.
11912 if (argvars[2].v_type == VAR_UNKNOWN)
11913 action = 'r';
11914 else if (argvars[2].v_type == VAR_STRING)
11915 {
11916 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011917 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011918 if (actstr == NULL)
11919 return;
11920 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11921 action = *actstr;
11922 else
11923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011924 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011925 return;
11926 }
11927 }
11928 else
11929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011930 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011931 return;
11932 }
11933
11934 if (set_tagstack(wp, d, action) == OK)
11935 rettv->vval.v_number = 0;
11936}
11937
11938/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011939 * "setwinvar()" function
11940 */
11941 static void
11942f_setwinvar(typval_T *argvars, typval_T *rettv)
11943{
11944 setwinvar(argvars, rettv, 0);
11945}
11946
11947#ifdef FEAT_CRYPT
11948/*
11949 * "sha256({string})" function
11950 */
11951 static void
11952f_sha256(typval_T *argvars, typval_T *rettv)
11953{
11954 char_u *p;
11955
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011956 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011957 rettv->vval.v_string = vim_strsave(
11958 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11959 rettv->v_type = VAR_STRING;
11960}
11961#endif /* FEAT_CRYPT */
11962
11963/*
11964 * "shellescape({string})" function
11965 */
11966 static void
11967f_shellescape(typval_T *argvars, typval_T *rettv)
11968{
Bram Moolenaar20615522017-06-05 18:46:26 +020011969 int do_special = non_zero_arg(&argvars[1]);
11970
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011971 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011972 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011973 rettv->v_type = VAR_STRING;
11974}
11975
11976/*
11977 * shiftwidth() function
11978 */
11979 static void
11980f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11981{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011982 rettv->vval.v_number = 0;
11983
11984 if (argvars[0].v_type != VAR_UNKNOWN)
11985 {
11986 long col;
11987
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011988 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011989 if (col < 0)
11990 return; // type error; errmsg already given
11991#ifdef FEAT_VARTABS
11992 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11993 return;
11994#endif
11995 }
11996
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011997 rettv->vval.v_number = get_sw_value(curbuf);
11998}
11999
Bram Moolenaar162b7142018-12-21 15:17:36 +010012000#ifdef FEAT_SIGNS
12001/*
12002 * "sign_define()" function
12003 */
12004 static void
12005f_sign_define(typval_T *argvars, typval_T *rettv)
12006{
12007 char_u *name;
12008 dict_T *dict;
12009 char_u *icon = NULL;
12010 char_u *linehl = NULL;
12011 char_u *text = NULL;
12012 char_u *texthl = NULL;
12013
12014 rettv->vval.v_number = -1;
12015
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012016 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012017 if (name == NULL)
12018 return;
12019
12020 if (argvars[1].v_type != VAR_UNKNOWN)
12021 {
12022 if (argvars[1].v_type != VAR_DICT)
12023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012024 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012025 return;
12026 }
12027
12028 // sign attributes
12029 dict = argvars[1].vval.v_dict;
12030 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12031 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12032 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12033 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12034 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12035 text = dict_get_string(dict, (char_u *)"text", TRUE);
12036 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12037 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12038 }
12039
12040 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12041 rettv->vval.v_number = 0;
12042
12043 vim_free(icon);
12044 vim_free(linehl);
12045 vim_free(text);
12046 vim_free(texthl);
12047}
12048
12049/*
12050 * "sign_getdefined()" function
12051 */
12052 static void
12053f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12054{
12055 char_u *name = NULL;
12056
12057 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12058 return;
12059
12060 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012061 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012062
12063 sign_getlist(name, rettv->vval.v_list);
12064}
12065
12066/*
12067 * "sign_getplaced()" function
12068 */
12069 static void
12070f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12071{
12072 buf_T *buf = NULL;
12073 dict_T *dict;
12074 dictitem_T *di;
12075 linenr_T lnum = 0;
12076 int sign_id = 0;
12077 char_u *group = NULL;
12078 int notanum = FALSE;
12079
12080 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12081 return;
12082
12083 if (argvars[0].v_type != VAR_UNKNOWN)
12084 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012085 // get signs placed in the specified buffer
12086 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012087 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012088 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012089
12090 if (argvars[1].v_type != VAR_UNKNOWN)
12091 {
12092 if (argvars[1].v_type != VAR_DICT ||
12093 ((dict = argvars[1].vval.v_dict) == NULL))
12094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012095 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012096 return;
12097 }
12098 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12099 {
12100 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012101 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012102 if (notanum)
12103 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012104 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012105 }
12106 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12107 {
12108 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012109 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012110 if (notanum)
12111 return;
12112 }
12113 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12114 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012115 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012116 if (group == NULL)
12117 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012118 if (*group == '\0') // empty string means global group
12119 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012120 }
12121 }
12122 }
12123
12124 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12125}
12126
12127/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012128 * "sign_jump()" function
12129 */
12130 static void
12131f_sign_jump(typval_T *argvars, typval_T *rettv)
12132{
12133 int sign_id;
12134 char_u *sign_group = NULL;
12135 buf_T *buf;
12136 int notanum = FALSE;
12137
12138 rettv->vval.v_number = -1;
12139
Bram Moolenaarbdace832019-03-02 10:13:42 +010012140 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012141 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12142 if (notanum)
12143 return;
12144 if (sign_id <= 0)
12145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012146 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012147 return;
12148 }
12149
12150 // Sign group
12151 sign_group = tv_get_string_chk(&argvars[1]);
12152 if (sign_group == NULL)
12153 return;
12154 if (sign_group[0] == '\0')
12155 sign_group = NULL; // global sign group
12156 else
12157 {
12158 sign_group = vim_strsave(sign_group);
12159 if (sign_group == NULL)
12160 return;
12161 }
12162
12163 // Buffer to place the sign
12164 buf = get_buf_arg(&argvars[2]);
12165 if (buf == NULL)
12166 goto cleanup;
12167
12168 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12169
12170cleanup:
12171 vim_free(sign_group);
12172}
12173
12174/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012175 * "sign_place()" function
12176 */
12177 static void
12178f_sign_place(typval_T *argvars, typval_T *rettv)
12179{
12180 int sign_id;
12181 char_u *group = NULL;
12182 char_u *sign_name;
12183 buf_T *buf;
12184 dict_T *dict;
12185 dictitem_T *di;
12186 linenr_T lnum = 0;
12187 int prio = SIGN_DEF_PRIO;
12188 int notanum = FALSE;
12189
12190 rettv->vval.v_number = -1;
12191
Bram Moolenaarbdace832019-03-02 10:13:42 +010012192 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012193 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012194 if (notanum)
12195 return;
12196 if (sign_id < 0)
12197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012198 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012199 return;
12200 }
12201
12202 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012203 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012204 if (group == NULL)
12205 return;
12206 if (group[0] == '\0')
12207 group = NULL; // global sign group
12208 else
12209 {
12210 group = vim_strsave(group);
12211 if (group == NULL)
12212 return;
12213 }
12214
12215 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012216 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012217 if (sign_name == NULL)
12218 goto cleanup;
12219
12220 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012221 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012222 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012223 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224
12225 if (argvars[4].v_type != VAR_UNKNOWN)
12226 {
12227 if (argvars[4].v_type != VAR_DICT ||
12228 ((dict = argvars[4].vval.v_dict) == NULL))
12229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012230 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012231 goto cleanup;
12232 }
12233
12234 // Line number where the sign is to be placed
12235 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12236 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012237 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012238 if (notanum)
12239 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012240 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012241 }
12242 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12243 {
12244 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012245 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012246 if (notanum)
12247 goto cleanup;
12248 }
12249 }
12250
12251 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12252 rettv->vval.v_number = sign_id;
12253
12254cleanup:
12255 vim_free(group);
12256}
12257
12258/*
12259 * "sign_undefine()" function
12260 */
12261 static void
12262f_sign_undefine(typval_T *argvars, typval_T *rettv)
12263{
12264 char_u *name;
12265
12266 rettv->vval.v_number = -1;
12267
12268 if (argvars[0].v_type == VAR_UNKNOWN)
12269 {
12270 // Free all the signs
12271 free_signs();
12272 rettv->vval.v_number = 0;
12273 }
12274 else
12275 {
12276 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012277 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012278 if (name == NULL)
12279 return;
12280
12281 if (sign_undefine_by_name(name) == OK)
12282 rettv->vval.v_number = 0;
12283 }
12284}
12285
12286/*
12287 * "sign_unplace()" function
12288 */
12289 static void
12290f_sign_unplace(typval_T *argvars, typval_T *rettv)
12291{
12292 dict_T *dict;
12293 dictitem_T *di;
12294 int sign_id = 0;
12295 buf_T *buf = NULL;
12296 char_u *group = NULL;
12297
12298 rettv->vval.v_number = -1;
12299
12300 if (argvars[0].v_type != VAR_STRING)
12301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012302 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012303 return;
12304 }
12305
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012306 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012307 if (group[0] == '\0')
12308 group = NULL; // global sign group
12309 else
12310 {
12311 group = vim_strsave(group);
12312 if (group == NULL)
12313 return;
12314 }
12315
12316 if (argvars[1].v_type != VAR_UNKNOWN)
12317 {
12318 if (argvars[1].v_type != VAR_DICT)
12319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012320 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012321 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012322 }
12323 dict = argvars[1].vval.v_dict;
12324
12325 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12326 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012327 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012328 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012329 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012330 }
12331 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12332 sign_id = dict_get_number(dict, (char_u *)"id");
12333 }
12334
12335 if (buf == NULL)
12336 {
12337 // Delete the sign in all the buffers
12338 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012339 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012340 rettv->vval.v_number = 0;
12341 }
12342 else
12343 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012344 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012345 rettv->vval.v_number = 0;
12346 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012347
12348cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012349 vim_free(group);
12350}
12351#endif
12352
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012353/*
12354 * "simplify()" function
12355 */
12356 static void
12357f_simplify(typval_T *argvars, typval_T *rettv)
12358{
12359 char_u *p;
12360
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012361 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012362 rettv->vval.v_string = vim_strsave(p);
12363 simplify_filename(rettv->vval.v_string); /* simplify in place */
12364 rettv->v_type = VAR_STRING;
12365}
12366
12367#ifdef FEAT_FLOAT
12368/*
12369 * "sin()" function
12370 */
12371 static void
12372f_sin(typval_T *argvars, typval_T *rettv)
12373{
12374 float_T f = 0.0;
12375
12376 rettv->v_type = VAR_FLOAT;
12377 if (get_float_arg(argvars, &f) == OK)
12378 rettv->vval.v_float = sin(f);
12379 else
12380 rettv->vval.v_float = 0.0;
12381}
12382
12383/*
12384 * "sinh()" function
12385 */
12386 static void
12387f_sinh(typval_T *argvars, typval_T *rettv)
12388{
12389 float_T f = 0.0;
12390
12391 rettv->v_type = VAR_FLOAT;
12392 if (get_float_arg(argvars, &f) == OK)
12393 rettv->vval.v_float = sinh(f);
12394 else
12395 rettv->vval.v_float = 0.0;
12396}
12397#endif
12398
Bram Moolenaareae1b912019-05-09 15:12:55 +020012399static int item_compare(const void *s1, const void *s2);
12400static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012401
12402/* struct used in the array that's given to qsort() */
12403typedef struct
12404{
12405 listitem_T *item;
12406 int idx;
12407} sortItem_T;
12408
12409/* struct storing information about current sort */
12410typedef struct
12411{
12412 int item_compare_ic;
12413 int item_compare_numeric;
12414 int item_compare_numbers;
12415#ifdef FEAT_FLOAT
12416 int item_compare_float;
12417#endif
12418 char_u *item_compare_func;
12419 partial_T *item_compare_partial;
12420 dict_T *item_compare_selfdict;
12421 int item_compare_func_err;
12422 int item_compare_keep_zero;
12423} sortinfo_T;
12424static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012425#define ITEM_COMPARE_FAIL 999
12426
12427/*
12428 * Compare functions for f_sort() and f_uniq() below.
12429 */
12430 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012431item_compare(const void *s1, const void *s2)
12432{
12433 sortItem_T *si1, *si2;
12434 typval_T *tv1, *tv2;
12435 char_u *p1, *p2;
12436 char_u *tofree1 = NULL, *tofree2 = NULL;
12437 int res;
12438 char_u numbuf1[NUMBUFLEN];
12439 char_u numbuf2[NUMBUFLEN];
12440
12441 si1 = (sortItem_T *)s1;
12442 si2 = (sortItem_T *)s2;
12443 tv1 = &si1->item->li_tv;
12444 tv2 = &si2->item->li_tv;
12445
12446 if (sortinfo->item_compare_numbers)
12447 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012448 varnumber_T v1 = tv_get_number(tv1);
12449 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450
12451 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12452 }
12453
12454#ifdef FEAT_FLOAT
12455 if (sortinfo->item_compare_float)
12456 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012457 float_T v1 = tv_get_float(tv1);
12458 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012459
12460 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12461 }
12462#endif
12463
12464 /* tv2string() puts quotes around a string and allocates memory. Don't do
12465 * that for string variables. Use a single quote when comparing with a
12466 * non-string to do what the docs promise. */
12467 if (tv1->v_type == VAR_STRING)
12468 {
12469 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12470 p1 = (char_u *)"'";
12471 else
12472 p1 = tv1->vval.v_string;
12473 }
12474 else
12475 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12476 if (tv2->v_type == VAR_STRING)
12477 {
12478 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12479 p2 = (char_u *)"'";
12480 else
12481 p2 = tv2->vval.v_string;
12482 }
12483 else
12484 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12485 if (p1 == NULL)
12486 p1 = (char_u *)"";
12487 if (p2 == NULL)
12488 p2 = (char_u *)"";
12489 if (!sortinfo->item_compare_numeric)
12490 {
12491 if (sortinfo->item_compare_ic)
12492 res = STRICMP(p1, p2);
12493 else
12494 res = STRCMP(p1, p2);
12495 }
12496 else
12497 {
12498 double n1, n2;
12499 n1 = strtod((char *)p1, (char **)&p1);
12500 n2 = strtod((char *)p2, (char **)&p2);
12501 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12502 }
12503
12504 /* When the result would be zero, compare the item indexes. Makes the
12505 * sort stable. */
12506 if (res == 0 && !sortinfo->item_compare_keep_zero)
12507 res = si1->idx > si2->idx ? 1 : -1;
12508
12509 vim_free(tofree1);
12510 vim_free(tofree2);
12511 return res;
12512}
12513
12514 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012515item_compare2(const void *s1, const void *s2)
12516{
12517 sortItem_T *si1, *si2;
12518 int res;
12519 typval_T rettv;
12520 typval_T argv[3];
12521 int dummy;
12522 char_u *func_name;
12523 partial_T *partial = sortinfo->item_compare_partial;
12524
12525 /* shortcut after failure in previous call; compare all items equal */
12526 if (sortinfo->item_compare_func_err)
12527 return 0;
12528
12529 si1 = (sortItem_T *)s1;
12530 si2 = (sortItem_T *)s2;
12531
12532 if (partial == NULL)
12533 func_name = sortinfo->item_compare_func;
12534 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012535 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012536
12537 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12538 * in the copy without changing the original list items. */
12539 copy_tv(&si1->item->li_tv, &argv[0]);
12540 copy_tv(&si2->item->li_tv, &argv[1]);
12541
12542 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012543 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012544 partial, sortinfo->item_compare_selfdict);
12545 clear_tv(&argv[0]);
12546 clear_tv(&argv[1]);
12547
12548 if (res == FAIL)
12549 res = ITEM_COMPARE_FAIL;
12550 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012551 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012552 if (sortinfo->item_compare_func_err)
12553 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12554 clear_tv(&rettv);
12555
12556 /* When the result would be zero, compare the pointers themselves. Makes
12557 * the sort stable. */
12558 if (res == 0 && !sortinfo->item_compare_keep_zero)
12559 res = si1->idx > si2->idx ? 1 : -1;
12560
12561 return res;
12562}
12563
12564/*
12565 * "sort({list})" function
12566 */
12567 static void
12568do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12569{
12570 list_T *l;
12571 listitem_T *li;
12572 sortItem_T *ptrs;
12573 sortinfo_T *old_sortinfo;
12574 sortinfo_T info;
12575 long len;
12576 long i;
12577
12578 /* Pointer to current info struct used in compare function. Save and
12579 * restore the current one for nested calls. */
12580 old_sortinfo = sortinfo;
12581 sortinfo = &info;
12582
12583 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012584 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012585 else
12586 {
12587 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012588 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012589 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12590 TRUE))
12591 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012592 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012593
12594 len = list_len(l);
12595 if (len <= 1)
12596 goto theend; /* short list sorts pretty quickly */
12597
12598 info.item_compare_ic = FALSE;
12599 info.item_compare_numeric = FALSE;
12600 info.item_compare_numbers = FALSE;
12601#ifdef FEAT_FLOAT
12602 info.item_compare_float = FALSE;
12603#endif
12604 info.item_compare_func = NULL;
12605 info.item_compare_partial = NULL;
12606 info.item_compare_selfdict = NULL;
12607 if (argvars[1].v_type != VAR_UNKNOWN)
12608 {
12609 /* optional second argument: {func} */
12610 if (argvars[1].v_type == VAR_FUNC)
12611 info.item_compare_func = argvars[1].vval.v_string;
12612 else if (argvars[1].v_type == VAR_PARTIAL)
12613 info.item_compare_partial = argvars[1].vval.v_partial;
12614 else
12615 {
12616 int error = FALSE;
12617
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012618 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012619 if (error)
12620 goto theend; /* type error; errmsg already given */
12621 if (i == 1)
12622 info.item_compare_ic = TRUE;
12623 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012624 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 else if (i != 0)
12626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012627 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012628 goto theend;
12629 }
12630 if (info.item_compare_func != NULL)
12631 {
12632 if (*info.item_compare_func == NUL)
12633 {
12634 /* empty string means default sort */
12635 info.item_compare_func = NULL;
12636 }
12637 else if (STRCMP(info.item_compare_func, "n") == 0)
12638 {
12639 info.item_compare_func = NULL;
12640 info.item_compare_numeric = TRUE;
12641 }
12642 else if (STRCMP(info.item_compare_func, "N") == 0)
12643 {
12644 info.item_compare_func = NULL;
12645 info.item_compare_numbers = TRUE;
12646 }
12647#ifdef FEAT_FLOAT
12648 else if (STRCMP(info.item_compare_func, "f") == 0)
12649 {
12650 info.item_compare_func = NULL;
12651 info.item_compare_float = TRUE;
12652 }
12653#endif
12654 else if (STRCMP(info.item_compare_func, "i") == 0)
12655 {
12656 info.item_compare_func = NULL;
12657 info.item_compare_ic = TRUE;
12658 }
12659 }
12660 }
12661
12662 if (argvars[2].v_type != VAR_UNKNOWN)
12663 {
12664 /* optional third argument: {dict} */
12665 if (argvars[2].v_type != VAR_DICT)
12666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012667 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012668 goto theend;
12669 }
12670 info.item_compare_selfdict = argvars[2].vval.v_dict;
12671 }
12672 }
12673
12674 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012675 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012676 if (ptrs == NULL)
12677 goto theend;
12678
12679 i = 0;
12680 if (sort)
12681 {
12682 /* sort(): ptrs will be the list to sort */
12683 for (li = l->lv_first; li != NULL; li = li->li_next)
12684 {
12685 ptrs[i].item = li;
12686 ptrs[i].idx = i;
12687 ++i;
12688 }
12689
12690 info.item_compare_func_err = FALSE;
12691 info.item_compare_keep_zero = FALSE;
12692 /* test the compare function */
12693 if ((info.item_compare_func != NULL
12694 || info.item_compare_partial != NULL)
12695 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12696 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012697 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012698 else
12699 {
12700 /* Sort the array with item pointers. */
12701 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12702 info.item_compare_func == NULL
12703 && info.item_compare_partial == NULL
12704 ? item_compare : item_compare2);
12705
12706 if (!info.item_compare_func_err)
12707 {
12708 /* Clear the List and append the items in sorted order. */
12709 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12710 l->lv_len = 0;
12711 for (i = 0; i < len; ++i)
12712 list_append(l, ptrs[i].item);
12713 }
12714 }
12715 }
12716 else
12717 {
12718 int (*item_compare_func_ptr)(const void *, const void *);
12719
12720 /* f_uniq(): ptrs will be a stack of items to remove */
12721 info.item_compare_func_err = FALSE;
12722 info.item_compare_keep_zero = TRUE;
12723 item_compare_func_ptr = info.item_compare_func != NULL
12724 || info.item_compare_partial != NULL
12725 ? item_compare2 : item_compare;
12726
12727 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12728 li = li->li_next)
12729 {
12730 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12731 == 0)
12732 ptrs[i++].item = li;
12733 if (info.item_compare_func_err)
12734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012735 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012736 break;
12737 }
12738 }
12739
12740 if (!info.item_compare_func_err)
12741 {
12742 while (--i >= 0)
12743 {
12744 li = ptrs[i].item->li_next;
12745 ptrs[i].item->li_next = li->li_next;
12746 if (li->li_next != NULL)
12747 li->li_next->li_prev = ptrs[i].item;
12748 else
12749 l->lv_last = ptrs[i].item;
12750 list_fix_watch(l, li);
12751 listitem_free(li);
12752 l->lv_len--;
12753 }
12754 }
12755 }
12756
12757 vim_free(ptrs);
12758 }
12759theend:
12760 sortinfo = old_sortinfo;
12761}
12762
12763/*
12764 * "sort({list})" function
12765 */
12766 static void
12767f_sort(typval_T *argvars, typval_T *rettv)
12768{
12769 do_sort_uniq(argvars, rettv, TRUE);
12770}
12771
12772/*
12773 * "uniq({list})" function
12774 */
12775 static void
12776f_uniq(typval_T *argvars, typval_T *rettv)
12777{
12778 do_sort_uniq(argvars, rettv, FALSE);
12779}
12780
12781/*
12782 * "soundfold({word})" function
12783 */
12784 static void
12785f_soundfold(typval_T *argvars, typval_T *rettv)
12786{
12787 char_u *s;
12788
12789 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012790 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012791#ifdef FEAT_SPELL
12792 rettv->vval.v_string = eval_soundfold(s);
12793#else
12794 rettv->vval.v_string = vim_strsave(s);
12795#endif
12796}
12797
12798/*
12799 * "spellbadword()" function
12800 */
12801 static void
12802f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12803{
12804 char_u *word = (char_u *)"";
12805 hlf_T attr = HLF_COUNT;
12806 int len = 0;
12807
12808 if (rettv_list_alloc(rettv) == FAIL)
12809 return;
12810
12811#ifdef FEAT_SPELL
12812 if (argvars[0].v_type == VAR_UNKNOWN)
12813 {
12814 /* Find the start and length of the badly spelled word. */
12815 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12816 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012817 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012818 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012819 curwin->w_set_curswant = TRUE;
12820 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012821 }
12822 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012824 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012825 int capcol = -1;
12826
12827 if (str != NULL)
12828 {
12829 /* Check the argument for spelling. */
12830 while (*str != NUL)
12831 {
12832 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12833 if (attr != HLF_COUNT)
12834 {
12835 word = str;
12836 break;
12837 }
12838 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012839 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012840 }
12841 }
12842 }
12843#endif
12844
12845 list_append_string(rettv->vval.v_list, word, len);
12846 list_append_string(rettv->vval.v_list, (char_u *)(
12847 attr == HLF_SPB ? "bad" :
12848 attr == HLF_SPR ? "rare" :
12849 attr == HLF_SPL ? "local" :
12850 attr == HLF_SPC ? "caps" :
12851 ""), -1);
12852}
12853
12854/*
12855 * "spellsuggest()" function
12856 */
12857 static void
12858f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12859{
12860#ifdef FEAT_SPELL
12861 char_u *str;
12862 int typeerr = FALSE;
12863 int maxcount;
12864 garray_T ga;
12865 int i;
12866 listitem_T *li;
12867 int need_capital = FALSE;
12868#endif
12869
12870 if (rettv_list_alloc(rettv) == FAIL)
12871 return;
12872
12873#ifdef FEAT_SPELL
12874 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12875 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012876 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012877 if (argvars[1].v_type != VAR_UNKNOWN)
12878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012879 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 if (maxcount <= 0)
12881 return;
12882 if (argvars[2].v_type != VAR_UNKNOWN)
12883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012884 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012885 if (typeerr)
12886 return;
12887 }
12888 }
12889 else
12890 maxcount = 25;
12891
12892 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12893
12894 for (i = 0; i < ga.ga_len; ++i)
12895 {
12896 str = ((char_u **)ga.ga_data)[i];
12897
12898 li = listitem_alloc();
12899 if (li == NULL)
12900 vim_free(str);
12901 else
12902 {
12903 li->li_tv.v_type = VAR_STRING;
12904 li->li_tv.v_lock = 0;
12905 li->li_tv.vval.v_string = str;
12906 list_append(rettv->vval.v_list, li);
12907 }
12908 }
12909 ga_clear(&ga);
12910 }
12911#endif
12912}
12913
12914 static void
12915f_split(typval_T *argvars, typval_T *rettv)
12916{
12917 char_u *str;
12918 char_u *end;
12919 char_u *pat = NULL;
12920 regmatch_T regmatch;
12921 char_u patbuf[NUMBUFLEN];
12922 char_u *save_cpo;
12923 int match;
12924 colnr_T col = 0;
12925 int keepempty = FALSE;
12926 int typeerr = FALSE;
12927
12928 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12929 save_cpo = p_cpo;
12930 p_cpo = (char_u *)"";
12931
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012932 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012933 if (argvars[1].v_type != VAR_UNKNOWN)
12934 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012935 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012936 if (pat == NULL)
12937 typeerr = TRUE;
12938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012939 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012940 }
12941 if (pat == NULL || *pat == NUL)
12942 pat = (char_u *)"[\\x01- ]\\+";
12943
12944 if (rettv_list_alloc(rettv) == FAIL)
12945 return;
12946 if (typeerr)
12947 return;
12948
12949 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12950 if (regmatch.regprog != NULL)
12951 {
12952 regmatch.rm_ic = FALSE;
12953 while (*str != NUL || keepempty)
12954 {
12955 if (*str == NUL)
12956 match = FALSE; /* empty item at the end */
12957 else
12958 match = vim_regexec_nl(&regmatch, str, col);
12959 if (match)
12960 end = regmatch.startp[0];
12961 else
12962 end = str + STRLEN(str);
12963 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12964 && *str != NUL && match && end < regmatch.endp[0]))
12965 {
12966 if (list_append_string(rettv->vval.v_list, str,
12967 (int)(end - str)) == FAIL)
12968 break;
12969 }
12970 if (!match)
12971 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012972 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 if (regmatch.endp[0] > str)
12974 col = 0;
12975 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012976 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012977 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012978 str = regmatch.endp[0];
12979 }
12980
12981 vim_regfree(regmatch.regprog);
12982 }
12983
12984 p_cpo = save_cpo;
12985}
12986
12987#ifdef FEAT_FLOAT
12988/*
12989 * "sqrt()" function
12990 */
12991 static void
12992f_sqrt(typval_T *argvars, typval_T *rettv)
12993{
12994 float_T f = 0.0;
12995
12996 rettv->v_type = VAR_FLOAT;
12997 if (get_float_arg(argvars, &f) == OK)
12998 rettv->vval.v_float = sqrt(f);
12999 else
13000 rettv->vval.v_float = 0.0;
13001}
13002
13003/*
13004 * "str2float()" function
13005 */
13006 static void
13007f_str2float(typval_T *argvars, typval_T *rettv)
13008{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013009 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013010 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013011
Bram Moolenaar08243d22017-01-10 16:12:29 +010013012 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013 p = skipwhite(p + 1);
13014 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013015 if (isneg)
13016 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013017 rettv->v_type = VAR_FLOAT;
13018}
13019#endif
13020
13021/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013022 * "str2list()" function
13023 */
13024 static void
13025f_str2list(typval_T *argvars, typval_T *rettv)
13026{
13027 char_u *p;
13028 int utf8 = FALSE;
13029
13030 if (rettv_list_alloc(rettv) == FAIL)
13031 return;
13032
13033 if (argvars[1].v_type != VAR_UNKNOWN)
13034 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13035
13036 p = tv_get_string(&argvars[0]);
13037
13038 if (has_mbyte || utf8)
13039 {
13040 int (*ptr2len)(char_u *);
13041 int (*ptr2char)(char_u *);
13042
13043 if (utf8 || enc_utf8)
13044 {
13045 ptr2len = utf_ptr2len;
13046 ptr2char = utf_ptr2char;
13047 }
13048 else
13049 {
13050 ptr2len = mb_ptr2len;
13051 ptr2char = mb_ptr2char;
13052 }
13053
13054 for ( ; *p != NUL; p += (*ptr2len)(p))
13055 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13056 }
13057 else
13058 for ( ; *p != NUL; ++p)
13059 list_append_number(rettv->vval.v_list, *p);
13060}
13061
13062/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013063 * "str2nr()" function
13064 */
13065 static void
13066f_str2nr(typval_T *argvars, typval_T *rettv)
13067{
13068 int base = 10;
13069 char_u *p;
13070 varnumber_T n;
13071 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013072 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013073
13074 if (argvars[1].v_type != VAR_UNKNOWN)
13075 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013076 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013077 if (base != 2 && base != 8 && base != 10 && base != 16)
13078 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013079 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013080 return;
13081 }
13082 }
13083
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013084 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013085 isneg = (*p == '-');
13086 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013087 p = skipwhite(p + 1);
13088 switch (base)
13089 {
13090 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13091 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13092 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13093 default: what = 0;
13094 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013095 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13096 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013097 if (isneg)
13098 rettv->vval.v_number = -n;
13099 else
13100 rettv->vval.v_number = n;
13101
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102}
13103
13104#ifdef HAVE_STRFTIME
13105/*
13106 * "strftime({format}[, {time}])" function
13107 */
13108 static void
13109f_strftime(typval_T *argvars, typval_T *rettv)
13110{
13111 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013112# ifdef HAVE_LOCALTIME_R
13113 struct tm tmval;
13114# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013115 struct tm *curtime;
13116 time_t seconds;
13117 char_u *p;
13118
13119 rettv->v_type = VAR_STRING;
13120
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013121 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013122 if (argvars[1].v_type == VAR_UNKNOWN)
13123 seconds = time(NULL);
13124 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013125 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013126# ifdef HAVE_LOCALTIME_R
13127 curtime = localtime_r(&seconds, &tmval);
13128# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013129 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013130# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 /* MSVC returns NULL for an invalid value of seconds. */
13132 if (curtime == NULL)
13133 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13134 else
13135 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013136 vimconv_T conv;
13137 char_u *enc;
13138
13139 conv.vc_type = CONV_NONE;
13140 enc = enc_locale();
13141 convert_setup(&conv, p_enc, enc);
13142 if (conv.vc_type != CONV_NONE)
13143 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013144 if (p != NULL)
13145 (void)strftime((char *)result_buf, sizeof(result_buf),
13146 (char *)p, curtime);
13147 else
13148 result_buf[0] = NUL;
13149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013150 if (conv.vc_type != CONV_NONE)
13151 vim_free(p);
13152 convert_setup(&conv, enc, p_enc);
13153 if (conv.vc_type != CONV_NONE)
13154 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13155 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 rettv->vval.v_string = vim_strsave(result_buf);
13157
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 /* Release conversion descriptors */
13159 convert_setup(&conv, NULL, NULL);
13160 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 }
13162}
13163#endif
13164
13165/*
13166 * "strgetchar()" function
13167 */
13168 static void
13169f_strgetchar(typval_T *argvars, typval_T *rettv)
13170{
13171 char_u *str;
13172 int len;
13173 int error = FALSE;
13174 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013175 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013176
13177 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013178 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013179 if (str == NULL)
13180 return;
13181 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013182 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183 if (error)
13184 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013185
Bram Moolenaar13505972019-01-24 15:04:48 +010013186 while (charidx >= 0 && byteidx < len)
13187 {
13188 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013189 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013190 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13191 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013192 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013193 --charidx;
13194 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013195 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013196}
13197
13198/*
13199 * "stridx()" function
13200 */
13201 static void
13202f_stridx(typval_T *argvars, typval_T *rettv)
13203{
13204 char_u buf[NUMBUFLEN];
13205 char_u *needle;
13206 char_u *haystack;
13207 char_u *save_haystack;
13208 char_u *pos;
13209 int start_idx;
13210
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013211 needle = tv_get_string_chk(&argvars[1]);
13212 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013213 rettv->vval.v_number = -1;
13214 if (needle == NULL || haystack == NULL)
13215 return; /* type error; errmsg already given */
13216
13217 if (argvars[2].v_type != VAR_UNKNOWN)
13218 {
13219 int error = FALSE;
13220
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013221 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013222 if (error || start_idx >= (int)STRLEN(haystack))
13223 return;
13224 if (start_idx >= 0)
13225 haystack += start_idx;
13226 }
13227
13228 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13229 if (pos != NULL)
13230 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13231}
13232
13233/*
13234 * "string()" function
13235 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013236 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013237f_string(typval_T *argvars, typval_T *rettv)
13238{
13239 char_u *tofree;
13240 char_u numbuf[NUMBUFLEN];
13241
13242 rettv->v_type = VAR_STRING;
13243 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13244 get_copyID());
13245 /* Make a copy if we have a value but it's not in allocated memory. */
13246 if (rettv->vval.v_string != NULL && tofree == NULL)
13247 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13248}
13249
13250/*
13251 * "strlen()" function
13252 */
13253 static void
13254f_strlen(typval_T *argvars, typval_T *rettv)
13255{
13256 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013257 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258}
13259
13260/*
13261 * "strchars()" function
13262 */
13263 static void
13264f_strchars(typval_T *argvars, typval_T *rettv)
13265{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013266 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013267 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 varnumber_T len = 0;
13269 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270
13271 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013272 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013274 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013275 else
13276 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013277 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13278 while (*s != NUL)
13279 {
13280 func_mb_ptr2char_adv(&s);
13281 ++len;
13282 }
13283 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013284 }
13285}
13286
13287/*
13288 * "strdisplaywidth()" function
13289 */
13290 static void
13291f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13292{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013293 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294 int col = 0;
13295
13296 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013297 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298
13299 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13300}
13301
13302/*
13303 * "strwidth()" function
13304 */
13305 static void
13306f_strwidth(typval_T *argvars, typval_T *rettv)
13307{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013308 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309
Bram Moolenaar13505972019-01-24 15:04:48 +010013310 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311}
13312
13313/*
13314 * "strcharpart()" function
13315 */
13316 static void
13317f_strcharpart(typval_T *argvars, typval_T *rettv)
13318{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 char_u *p;
13320 int nchar;
13321 int nbyte = 0;
13322 int charlen;
13323 int len = 0;
13324 int slen;
13325 int error = FALSE;
13326
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013327 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013328 slen = (int)STRLEN(p);
13329
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013330 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013331 if (!error)
13332 {
13333 if (nchar > 0)
13334 while (nchar > 0 && nbyte < slen)
13335 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013336 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013337 --nchar;
13338 }
13339 else
13340 nbyte = nchar;
13341 if (argvars[2].v_type != VAR_UNKNOWN)
13342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013343 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 while (charlen > 0 && nbyte + len < slen)
13345 {
13346 int off = nbyte + len;
13347
13348 if (off < 0)
13349 len += 1;
13350 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013351 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013352 --charlen;
13353 }
13354 }
13355 else
13356 len = slen - nbyte; /* default: all bytes that are available. */
13357 }
13358
13359 /*
13360 * Only return the overlap between the specified part and the actual
13361 * string.
13362 */
13363 if (nbyte < 0)
13364 {
13365 len += nbyte;
13366 nbyte = 0;
13367 }
13368 else if (nbyte > slen)
13369 nbyte = slen;
13370 if (len < 0)
13371 len = 0;
13372 else if (nbyte + len > slen)
13373 len = slen - nbyte;
13374
13375 rettv->v_type = VAR_STRING;
13376 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377}
13378
13379/*
13380 * "strpart()" function
13381 */
13382 static void
13383f_strpart(typval_T *argvars, typval_T *rettv)
13384{
13385 char_u *p;
13386 int n;
13387 int len;
13388 int slen;
13389 int error = FALSE;
13390
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013391 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013392 slen = (int)STRLEN(p);
13393
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013394 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013395 if (error)
13396 len = 0;
13397 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013398 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013399 else
13400 len = slen - n; /* default len: all bytes that are available. */
13401
13402 /*
13403 * Only return the overlap between the specified part and the actual
13404 * string.
13405 */
13406 if (n < 0)
13407 {
13408 len += n;
13409 n = 0;
13410 }
13411 else if (n > slen)
13412 n = slen;
13413 if (len < 0)
13414 len = 0;
13415 else if (n + len > slen)
13416 len = slen - n;
13417
13418 rettv->v_type = VAR_STRING;
13419 rettv->vval.v_string = vim_strnsave(p + n, len);
13420}
13421
13422/*
13423 * "strridx()" function
13424 */
13425 static void
13426f_strridx(typval_T *argvars, typval_T *rettv)
13427{
13428 char_u buf[NUMBUFLEN];
13429 char_u *needle;
13430 char_u *haystack;
13431 char_u *rest;
13432 char_u *lastmatch = NULL;
13433 int haystack_len, end_idx;
13434
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013435 needle = tv_get_string_chk(&argvars[1]);
13436 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013437
13438 rettv->vval.v_number = -1;
13439 if (needle == NULL || haystack == NULL)
13440 return; /* type error; errmsg already given */
13441
13442 haystack_len = (int)STRLEN(haystack);
13443 if (argvars[2].v_type != VAR_UNKNOWN)
13444 {
13445 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013446 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013447 if (end_idx < 0)
13448 return; /* can never find a match */
13449 }
13450 else
13451 end_idx = haystack_len;
13452
13453 if (*needle == NUL)
13454 {
13455 /* Empty string matches past the end. */
13456 lastmatch = haystack + end_idx;
13457 }
13458 else
13459 {
13460 for (rest = haystack; *rest != '\0'; ++rest)
13461 {
13462 rest = (char_u *)strstr((char *)rest, (char *)needle);
13463 if (rest == NULL || rest > haystack + end_idx)
13464 break;
13465 lastmatch = rest;
13466 }
13467 }
13468
13469 if (lastmatch == NULL)
13470 rettv->vval.v_number = -1;
13471 else
13472 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13473}
13474
13475/*
13476 * "strtrans()" function
13477 */
13478 static void
13479f_strtrans(typval_T *argvars, typval_T *rettv)
13480{
13481 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013482 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483}
13484
13485/*
13486 * "submatch()" function
13487 */
13488 static void
13489f_submatch(typval_T *argvars, typval_T *rettv)
13490{
13491 int error = FALSE;
13492 int no;
13493 int retList = 0;
13494
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013495 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013496 if (error)
13497 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013498 if (no < 0 || no >= NSUBEXP)
13499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013500 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013501 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013502 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013503 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013504 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013505 if (error)
13506 return;
13507
13508 if (retList == 0)
13509 {
13510 rettv->v_type = VAR_STRING;
13511 rettv->vval.v_string = reg_submatch(no);
13512 }
13513 else
13514 {
13515 rettv->v_type = VAR_LIST;
13516 rettv->vval.v_list = reg_submatch_list(no);
13517 }
13518}
13519
13520/*
13521 * "substitute()" function
13522 */
13523 static void
13524f_substitute(typval_T *argvars, typval_T *rettv)
13525{
13526 char_u patbuf[NUMBUFLEN];
13527 char_u subbuf[NUMBUFLEN];
13528 char_u flagsbuf[NUMBUFLEN];
13529
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013530 char_u *str = tv_get_string_chk(&argvars[0]);
13531 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013532 char_u *sub = NULL;
13533 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013534 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013535
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013536 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13537 expr = &argvars[2];
13538 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013539 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013540
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013541 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013542 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13543 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013544 rettv->vval.v_string = NULL;
13545 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013546 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013547}
13548
13549/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013550 * "swapinfo(swap_filename)" function
13551 */
13552 static void
13553f_swapinfo(typval_T *argvars, typval_T *rettv)
13554{
13555 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013556 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013557}
13558
13559/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013560 * "swapname(expr)" function
13561 */
13562 static void
13563f_swapname(typval_T *argvars, typval_T *rettv)
13564{
13565 buf_T *buf;
13566
13567 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013568 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013569 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13570 || buf->b_ml.ml_mfp->mf_fname == NULL)
13571 rettv->vval.v_string = NULL;
13572 else
13573 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13574}
13575
13576/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577 * "synID(lnum, col, trans)" function
13578 */
13579 static void
13580f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13581{
13582 int id = 0;
13583#ifdef FEAT_SYN_HL
13584 linenr_T lnum;
13585 colnr_T col;
13586 int trans;
13587 int transerr = FALSE;
13588
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013589 lnum = tv_get_lnum(argvars); /* -1 on type error */
13590 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13591 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013592
13593 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13594 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13595 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13596#endif
13597
13598 rettv->vval.v_number = id;
13599}
13600
13601/*
13602 * "synIDattr(id, what [, mode])" function
13603 */
13604 static void
13605f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13606{
13607 char_u *p = NULL;
13608#ifdef FEAT_SYN_HL
13609 int id;
13610 char_u *what;
13611 char_u *mode;
13612 char_u modebuf[NUMBUFLEN];
13613 int modec;
13614
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013615 id = (int)tv_get_number(&argvars[0]);
13616 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617 if (argvars[2].v_type != VAR_UNKNOWN)
13618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013619 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013620 modec = TOLOWER_ASC(mode[0]);
13621 if (modec != 't' && modec != 'c' && modec != 'g')
13622 modec = 0; /* replace invalid with current */
13623 }
13624 else
13625 {
13626#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13627 if (USE_24BIT)
13628 modec = 'g';
13629 else
13630#endif
13631 if (t_colors > 1)
13632 modec = 'c';
13633 else
13634 modec = 't';
13635 }
13636
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013637 switch (TOLOWER_ASC(what[0]))
13638 {
13639 case 'b':
13640 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13641 p = highlight_color(id, what, modec);
13642 else /* bold */
13643 p = highlight_has_attr(id, HL_BOLD, modec);
13644 break;
13645
13646 case 'f': /* fg[#] or font */
13647 p = highlight_color(id, what, modec);
13648 break;
13649
13650 case 'i':
13651 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13652 p = highlight_has_attr(id, HL_INVERSE, modec);
13653 else /* italic */
13654 p = highlight_has_attr(id, HL_ITALIC, modec);
13655 break;
13656
13657 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013658 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013659 break;
13660
13661 case 'r': /* reverse */
13662 p = highlight_has_attr(id, HL_INVERSE, modec);
13663 break;
13664
13665 case 's':
13666 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13667 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013668 /* strikeout */
13669 else if (TOLOWER_ASC(what[1]) == 't' &&
13670 TOLOWER_ASC(what[2]) == 'r')
13671 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013672 else /* standout */
13673 p = highlight_has_attr(id, HL_STANDOUT, modec);
13674 break;
13675
13676 case 'u':
13677 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13678 /* underline */
13679 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13680 else
13681 /* undercurl */
13682 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13683 break;
13684 }
13685
13686 if (p != NULL)
13687 p = vim_strsave(p);
13688#endif
13689 rettv->v_type = VAR_STRING;
13690 rettv->vval.v_string = p;
13691}
13692
13693/*
13694 * "synIDtrans(id)" function
13695 */
13696 static void
13697f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13698{
13699 int id;
13700
13701#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013702 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013703
13704 if (id > 0)
13705 id = syn_get_final_id(id);
13706 else
13707#endif
13708 id = 0;
13709
13710 rettv->vval.v_number = id;
13711}
13712
13713/*
13714 * "synconcealed(lnum, col)" function
13715 */
13716 static void
13717f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13718{
13719#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13720 linenr_T lnum;
13721 colnr_T col;
13722 int syntax_flags = 0;
13723 int cchar;
13724 int matchid = 0;
13725 char_u str[NUMBUFLEN];
13726#endif
13727
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013728 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013729
13730#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013731 lnum = tv_get_lnum(argvars); /* -1 on type error */
13732 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013733
13734 vim_memset(str, NUL, sizeof(str));
13735
13736 if (rettv_list_alloc(rettv) != FAIL)
13737 {
13738 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13739 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13740 && curwin->w_p_cole > 0)
13741 {
13742 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13743 syntax_flags = get_syntax_info(&matchid);
13744
13745 /* get the conceal character */
13746 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13747 {
13748 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013749 if (cchar == NUL && curwin->w_p_cole == 1)
13750 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013751 if (cchar != NUL)
13752 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013753 if (has_mbyte)
13754 (*mb_char2bytes)(cchar, str);
13755 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013756 str[0] = cchar;
13757 }
13758 }
13759 }
13760
13761 list_append_number(rettv->vval.v_list,
13762 (syntax_flags & HL_CONCEAL) != 0);
13763 /* -1 to auto-determine strlen */
13764 list_append_string(rettv->vval.v_list, str, -1);
13765 list_append_number(rettv->vval.v_list, matchid);
13766 }
13767#endif
13768}
13769
13770/*
13771 * "synstack(lnum, col)" function
13772 */
13773 static void
13774f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13775{
13776#ifdef FEAT_SYN_HL
13777 linenr_T lnum;
13778 colnr_T col;
13779 int i;
13780 int id;
13781#endif
13782
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013783 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013784
13785#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013786 lnum = tv_get_lnum(argvars); /* -1 on type error */
13787 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013788
13789 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13790 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13791 && rettv_list_alloc(rettv) != FAIL)
13792 {
13793 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13794 for (i = 0; ; ++i)
13795 {
13796 id = syn_get_stack_item(i);
13797 if (id < 0)
13798 break;
13799 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13800 break;
13801 }
13802 }
13803#endif
13804}
13805
13806 static void
13807get_cmd_output_as_rettv(
13808 typval_T *argvars,
13809 typval_T *rettv,
13810 int retlist)
13811{
13812 char_u *res = NULL;
13813 char_u *p;
13814 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013815 int err = FALSE;
13816 FILE *fd;
13817 list_T *list = NULL;
13818 int flags = SHELL_SILENT;
13819
13820 rettv->v_type = VAR_STRING;
13821 rettv->vval.v_string = NULL;
13822 if (check_restricted() || check_secure())
13823 goto errret;
13824
13825 if (argvars[1].v_type != VAR_UNKNOWN)
13826 {
13827 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013828 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 * command.
13830 */
13831 if ((infile = vim_tempname('i', TRUE)) == NULL)
13832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013833 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013834 goto errret;
13835 }
13836
13837 fd = mch_fopen((char *)infile, WRITEBIN);
13838 if (fd == NULL)
13839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013840 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013841 goto errret;
13842 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013843 if (argvars[1].v_type == VAR_NUMBER)
13844 {
13845 linenr_T lnum;
13846 buf_T *buf;
13847
13848 buf = buflist_findnr(argvars[1].vval.v_number);
13849 if (buf == NULL)
13850 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013851 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013852 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013853 goto errret;
13854 }
13855
13856 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13857 {
13858 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13859 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13860 {
13861 err = TRUE;
13862 break;
13863 }
13864 if (putc(NL, fd) == EOF)
13865 {
13866 err = TRUE;
13867 break;
13868 }
13869 }
13870 }
13871 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013872 {
13873 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13874 err = TRUE;
13875 }
13876 else
13877 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013878 size_t len;
13879 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013880
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013881 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013882 if (p == NULL)
13883 {
13884 fclose(fd);
13885 goto errret; /* type error; errmsg already given */
13886 }
13887 len = STRLEN(p);
13888 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13889 err = TRUE;
13890 }
13891 if (fclose(fd) != 0)
13892 err = TRUE;
13893 if (err)
13894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013895 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013896 goto errret;
13897 }
13898 }
13899
13900 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13901 * echoes typeahead, that messes up the display. */
13902 if (!msg_silent)
13903 flags += SHELL_COOKED;
13904
13905 if (retlist)
13906 {
13907 int len;
13908 listitem_T *li;
13909 char_u *s = NULL;
13910 char_u *start;
13911 char_u *end;
13912 int i;
13913
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013914 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013915 if (res == NULL)
13916 goto errret;
13917
13918 list = list_alloc();
13919 if (list == NULL)
13920 goto errret;
13921
13922 for (i = 0; i < len; ++i)
13923 {
13924 start = res + i;
13925 while (i < len && res[i] != NL)
13926 ++i;
13927 end = res + i;
13928
Bram Moolenaar964b3742019-05-24 18:54:09 +020013929 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013930 if (s == NULL)
13931 goto errret;
13932
13933 for (p = s; start < end; ++p, ++start)
13934 *p = *start == NUL ? NL : *start;
13935 *p = NUL;
13936
13937 li = listitem_alloc();
13938 if (li == NULL)
13939 {
13940 vim_free(s);
13941 goto errret;
13942 }
13943 li->li_tv.v_type = VAR_STRING;
13944 li->li_tv.v_lock = 0;
13945 li->li_tv.vval.v_string = s;
13946 list_append(list, li);
13947 }
13948
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013949 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013950 list = NULL;
13951 }
13952 else
13953 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013954 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013955#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013956 /* translate <CR><NL> into <NL> */
13957 if (res != NULL)
13958 {
13959 char_u *s, *d;
13960
13961 d = res;
13962 for (s = res; *s; ++s)
13963 {
13964 if (s[0] == CAR && s[1] == NL)
13965 ++s;
13966 *d++ = *s;
13967 }
13968 *d = NUL;
13969 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013970#endif
13971 rettv->vval.v_string = res;
13972 res = NULL;
13973 }
13974
13975errret:
13976 if (infile != NULL)
13977 {
13978 mch_remove(infile);
13979 vim_free(infile);
13980 }
13981 if (res != NULL)
13982 vim_free(res);
13983 if (list != NULL)
13984 list_free(list);
13985}
13986
13987/*
13988 * "system()" function
13989 */
13990 static void
13991f_system(typval_T *argvars, typval_T *rettv)
13992{
13993 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13994}
13995
13996/*
13997 * "systemlist()" function
13998 */
13999 static void
14000f_systemlist(typval_T *argvars, typval_T *rettv)
14001{
14002 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14003}
14004
14005/*
14006 * "tabpagebuflist()" function
14007 */
14008 static void
14009f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14010{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014011 tabpage_T *tp;
14012 win_T *wp = NULL;
14013
14014 if (argvars[0].v_type == VAR_UNKNOWN)
14015 wp = firstwin;
14016 else
14017 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014018 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014019 if (tp != NULL)
14020 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14021 }
14022 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14023 {
14024 for (; wp != NULL; wp = wp->w_next)
14025 if (list_append_number(rettv->vval.v_list,
14026 wp->w_buffer->b_fnum) == FAIL)
14027 break;
14028 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014029}
14030
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014031/*
14032 * "tabpagenr()" function
14033 */
14034 static void
14035f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14036{
14037 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014038 char_u *arg;
14039
14040 if (argvars[0].v_type != VAR_UNKNOWN)
14041 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014042 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014043 nr = 0;
14044 if (arg != NULL)
14045 {
14046 if (STRCMP(arg, "$") == 0)
14047 nr = tabpage_index(NULL) - 1;
14048 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014049 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014050 }
14051 }
14052 else
14053 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054 rettv->vval.v_number = nr;
14055}
14056
14057
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014058/*
14059 * Common code for tabpagewinnr() and winnr().
14060 */
14061 static int
14062get_winnr(tabpage_T *tp, typval_T *argvar)
14063{
14064 win_T *twin;
14065 int nr = 1;
14066 win_T *wp;
14067 char_u *arg;
14068
14069 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14070 if (argvar->v_type != VAR_UNKNOWN)
14071 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014072 int invalid_arg = FALSE;
14073
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014074 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 if (arg == NULL)
14076 nr = 0; /* type error; errmsg already given */
14077 else if (STRCMP(arg, "$") == 0)
14078 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14079 else if (STRCMP(arg, "#") == 0)
14080 {
14081 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14082 if (twin == NULL)
14083 nr = 0;
14084 }
14085 else
14086 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014087 long count;
14088 char_u *endp;
14089
14090 // Extract the window count (if specified). e.g. winnr('3j')
14091 count = strtol((char *)arg, (char **)&endp, 10);
14092 if (count <= 0)
14093 count = 1; // if count is not specified, default to 1
14094 if (endp != NULL && *endp != '\0')
14095 {
14096 if (STRCMP(endp, "j") == 0)
14097 twin = win_vert_neighbor(tp, twin, FALSE, count);
14098 else if (STRCMP(endp, "k") == 0)
14099 twin = win_vert_neighbor(tp, twin, TRUE, count);
14100 else if (STRCMP(endp, "h") == 0)
14101 twin = win_horz_neighbor(tp, twin, TRUE, count);
14102 else if (STRCMP(endp, "l") == 0)
14103 twin = win_horz_neighbor(tp, twin, FALSE, count);
14104 else
14105 invalid_arg = TRUE;
14106 }
14107 else
14108 invalid_arg = TRUE;
14109 }
14110
14111 if (invalid_arg)
14112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014113 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014114 nr = 0;
14115 }
14116 }
14117
14118 if (nr > 0)
14119 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14120 wp != twin; wp = wp->w_next)
14121 {
14122 if (wp == NULL)
14123 {
14124 /* didn't find it in this tabpage */
14125 nr = 0;
14126 break;
14127 }
14128 ++nr;
14129 }
14130 return nr;
14131}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014132
14133/*
14134 * "tabpagewinnr()" function
14135 */
14136 static void
14137f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14138{
14139 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014140 tabpage_T *tp;
14141
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014142 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014143 if (tp == NULL)
14144 nr = 0;
14145 else
14146 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014147 rettv->vval.v_number = nr;
14148}
14149
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150/*
14151 * "tagfiles()" function
14152 */
14153 static void
14154f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14155{
14156 char_u *fname;
14157 tagname_T tn;
14158 int first;
14159
14160 if (rettv_list_alloc(rettv) == FAIL)
14161 return;
14162 fname = alloc(MAXPATHL);
14163 if (fname == NULL)
14164 return;
14165
14166 for (first = TRUE; ; first = FALSE)
14167 if (get_tagfname(&tn, first, fname) == FAIL
14168 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14169 break;
14170 tagname_free(&tn);
14171 vim_free(fname);
14172}
14173
14174/*
14175 * "taglist()" function
14176 */
14177 static void
14178f_taglist(typval_T *argvars, typval_T *rettv)
14179{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014180 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014181 char_u *tag_pattern;
14182
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014183 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014184
14185 rettv->vval.v_number = FALSE;
14186 if (*tag_pattern == NUL)
14187 return;
14188
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014189 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014190 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014191 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014192 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014193}
14194
14195/*
14196 * "tempname()" function
14197 */
14198 static void
14199f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14200{
14201 static int x = 'A';
14202
14203 rettv->v_type = VAR_STRING;
14204 rettv->vval.v_string = vim_tempname(x, FALSE);
14205
14206 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14207 * names. Skip 'I' and 'O', they are used for shell redirection. */
14208 do
14209 {
14210 if (x == 'Z')
14211 x = '0';
14212 else if (x == '9')
14213 x = 'A';
14214 else
14215 {
14216#ifdef EBCDIC
14217 if (x == 'I')
14218 x = 'J';
14219 else if (x == 'R')
14220 x = 'S';
14221 else
14222#endif
14223 ++x;
14224 }
14225 } while (x == 'I' || x == 'O');
14226}
14227
14228#ifdef FEAT_FLOAT
14229/*
14230 * "tan()" function
14231 */
14232 static void
14233f_tan(typval_T *argvars, typval_T *rettv)
14234{
14235 float_T f = 0.0;
14236
14237 rettv->v_type = VAR_FLOAT;
14238 if (get_float_arg(argvars, &f) == OK)
14239 rettv->vval.v_float = tan(f);
14240 else
14241 rettv->vval.v_float = 0.0;
14242}
14243
14244/*
14245 * "tanh()" function
14246 */
14247 static void
14248f_tanh(typval_T *argvars, typval_T *rettv)
14249{
14250 float_T f = 0.0;
14251
14252 rettv->v_type = VAR_FLOAT;
14253 if (get_float_arg(argvars, &f) == OK)
14254 rettv->vval.v_float = tanh(f);
14255 else
14256 rettv->vval.v_float = 0.0;
14257}
14258#endif
14259
14260/*
14261 * "test_alloc_fail(id, countdown, repeat)" function
14262 */
14263 static void
14264f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14265{
14266 if (argvars[0].v_type != VAR_NUMBER
14267 || argvars[0].vval.v_number <= 0
14268 || argvars[1].v_type != VAR_NUMBER
14269 || argvars[1].vval.v_number < 0
14270 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014271 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014272 else
14273 {
14274 alloc_fail_id = argvars[0].vval.v_number;
14275 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014276 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014277 alloc_fail_countdown = argvars[1].vval.v_number;
14278 alloc_fail_repeat = argvars[2].vval.v_number;
14279 did_outofmem_msg = FALSE;
14280 }
14281}
14282
14283/*
14284 * "test_autochdir()"
14285 */
14286 static void
14287f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14288{
14289#if defined(FEAT_AUTOCHDIR)
14290 test_autochdir = TRUE;
14291#endif
14292}
14293
14294/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014295 * "test_feedinput()"
14296 */
14297 static void
14298f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14299{
14300#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014301 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014302
14303 if (val != NULL)
14304 {
14305 trash_input_buf();
14306 add_to_input_buf_csi(val, (int)STRLEN(val));
14307 }
14308#endif
14309}
14310
14311/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014312 * "test_getvalue({name})" function
14313 */
14314 static void
14315f_test_getvalue(typval_T *argvars, typval_T *rettv)
14316{
14317 if (argvars[0].v_type != VAR_STRING)
14318 emsg(_(e_invarg));
14319 else
14320 {
14321 char_u *name = tv_get_string(&argvars[0]);
14322
14323 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14324 rettv->vval.v_number = need_fileinfo;
14325 else
14326 semsg(_(e_invarg2), name);
14327 }
14328}
14329
14330/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014331 * "test_option_not_set({name})" function
14332 */
14333 static void
14334f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14335{
14336 char_u *name = (char_u *)"";
14337
14338 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014339 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014340 else
14341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014342 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014343 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014344 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014345 }
14346}
14347
14348/*
14349 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014350 */
14351 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014352f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014353{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014354 char_u *name = (char_u *)"";
14355 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014356 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014357
14358 if (argvars[0].v_type != VAR_STRING
14359 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014360 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014361 else
14362 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014363 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014364 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014365
14366 if (STRCMP(name, (char_u *)"redraw") == 0)
14367 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014368 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14369 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014370 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14371 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014372 else if (STRCMP(name, (char_u *)"starting") == 0)
14373 {
14374 if (val)
14375 {
14376 if (save_starting < 0)
14377 save_starting = starting;
14378 starting = 0;
14379 }
14380 else
14381 {
14382 starting = save_starting;
14383 save_starting = -1;
14384 }
14385 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014386 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14387 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014388 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14389 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014390 else if (STRCMP(name, (char_u *)"ALL") == 0)
14391 {
14392 disable_char_avail_for_testing = FALSE;
14393 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014394 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014395 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014396 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014397 if (save_starting >= 0)
14398 {
14399 starting = save_starting;
14400 save_starting = -1;
14401 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014402 }
14403 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014404 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014405 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014406}
14407
14408/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014409 * "test_refcount({expr})" function
14410 */
14411 static void
14412f_test_refcount(typval_T *argvars, typval_T *rettv)
14413{
14414 int retval = -1;
14415
14416 switch (argvars[0].v_type)
14417 {
14418 case VAR_UNKNOWN:
14419 case VAR_NUMBER:
14420 case VAR_FLOAT:
14421 case VAR_SPECIAL:
14422 case VAR_STRING:
14423 break;
14424 case VAR_JOB:
14425#ifdef FEAT_JOB_CHANNEL
14426 if (argvars[0].vval.v_job != NULL)
14427 retval = argvars[0].vval.v_job->jv_refcount - 1;
14428#endif
14429 break;
14430 case VAR_CHANNEL:
14431#ifdef FEAT_JOB_CHANNEL
14432 if (argvars[0].vval.v_channel != NULL)
14433 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14434#endif
14435 break;
14436 case VAR_FUNC:
14437 if (argvars[0].vval.v_string != NULL)
14438 {
14439 ufunc_T *fp;
14440
14441 fp = find_func(argvars[0].vval.v_string);
14442 if (fp != NULL)
14443 retval = fp->uf_refcount;
14444 }
14445 break;
14446 case VAR_PARTIAL:
14447 if (argvars[0].vval.v_partial != NULL)
14448 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14449 break;
14450 case VAR_BLOB:
14451 if (argvars[0].vval.v_blob != NULL)
14452 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14453 break;
14454 case VAR_LIST:
14455 if (argvars[0].vval.v_list != NULL)
14456 retval = argvars[0].vval.v_list->lv_refcount - 1;
14457 break;
14458 case VAR_DICT:
14459 if (argvars[0].vval.v_dict != NULL)
14460 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14461 break;
14462 }
14463
14464 rettv->v_type = VAR_NUMBER;
14465 rettv->vval.v_number = retval;
14466
14467}
14468
14469/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014470 * "test_garbagecollect_now()" function
14471 */
14472 static void
14473f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14474{
14475 /* This is dangerous, any Lists and Dicts used internally may be freed
14476 * while still in use. */
14477 garbage_collect(TRUE);
14478}
14479
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014480/*
14481 * "test_ignore_error()" function
14482 */
14483 static void
14484f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14485{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014486 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014487}
14488
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014489 static void
14490f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14491{
14492 rettv->v_type = VAR_BLOB;
14493 rettv->vval.v_blob = NULL;
14494}
14495
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014496#ifdef FEAT_JOB_CHANNEL
14497 static void
14498f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14499{
14500 rettv->v_type = VAR_CHANNEL;
14501 rettv->vval.v_channel = NULL;
14502}
14503#endif
14504
14505 static void
14506f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14507{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014508 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014509}
14510
14511#ifdef FEAT_JOB_CHANNEL
14512 static void
14513f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14514{
14515 rettv->v_type = VAR_JOB;
14516 rettv->vval.v_job = NULL;
14517}
14518#endif
14519
14520 static void
14521f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14522{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014523 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014524}
14525
14526 static void
14527f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14528{
14529 rettv->v_type = VAR_PARTIAL;
14530 rettv->vval.v_partial = NULL;
14531}
14532
14533 static void
14534f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14535{
14536 rettv->v_type = VAR_STRING;
14537 rettv->vval.v_string = NULL;
14538}
14539
Bram Moolenaarab186732018-09-14 21:27:06 +020014540#ifdef FEAT_GUI
14541 static void
14542f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14543{
14544 char_u *which;
14545 long value;
14546 int dragging;
14547 scrollbar_T *sb = NULL;
14548
14549 if (argvars[0].v_type != VAR_STRING
14550 || (argvars[1].v_type) != VAR_NUMBER
14551 || (argvars[2].v_type) != VAR_NUMBER)
14552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014553 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014554 return;
14555 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014556 which = tv_get_string(&argvars[0]);
14557 value = tv_get_number(&argvars[1]);
14558 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014559
14560 if (STRCMP(which, "left") == 0)
14561 sb = &curwin->w_scrollbars[SBAR_LEFT];
14562 else if (STRCMP(which, "right") == 0)
14563 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14564 else if (STRCMP(which, "hor") == 0)
14565 sb = &gui.bottom_sbar;
14566 if (sb == NULL)
14567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014568 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014569 return;
14570 }
14571 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014572# ifndef USE_ON_FLY_SCROLL
14573 // need to loop through normal_cmd() to handle the scroll events
14574 exec_normal(FALSE, TRUE, FALSE);
14575# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014576}
14577#endif
14578
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014579#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014580 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014581f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14582{
14583 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14584 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14585}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014586#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014587
14588 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14590{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014591 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014592}
14593
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014594/*
14595 * Get a callback from "arg". It can be a Funcref or a function name.
14596 * When "arg" is zero return an empty string.
14597 * Return NULL for an invalid argument.
14598 */
14599 char_u *
14600get_callback(typval_T *arg, partial_T **pp)
14601{
14602 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14603 {
14604 *pp = arg->vval.v_partial;
14605 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014606 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014607 }
14608 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014609 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014610 {
14611 func_ref(arg->vval.v_string);
14612 return arg->vval.v_string;
14613 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014614 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14615 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014616 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617 return NULL;
14618}
14619
14620/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014621 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014622 */
14623 void
14624free_callback(char_u *callback, partial_T *partial)
14625{
14626 if (partial != NULL)
14627 partial_unref(partial);
14628 else if (callback != NULL)
14629 {
14630 func_unref(callback);
14631 vim_free(callback);
14632 }
14633}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014634
14635#ifdef FEAT_TIMERS
14636/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014637 * "timer_info([timer])" function
14638 */
14639 static void
14640f_timer_info(typval_T *argvars, typval_T *rettv)
14641{
14642 timer_T *timer = NULL;
14643
14644 if (rettv_list_alloc(rettv) != OK)
14645 return;
14646 if (argvars[0].v_type != VAR_UNKNOWN)
14647 {
14648 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014649 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014650 else
14651 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014652 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014653 if (timer != NULL)
14654 add_timer_info(rettv, timer);
14655 }
14656 }
14657 else
14658 add_timer_info_all(rettv);
14659}
14660
14661/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014662 * "timer_pause(timer, paused)" function
14663 */
14664 static void
14665f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14666{
14667 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014668 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014669
14670 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014671 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014672 else
14673 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014674 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014675 if (timer != NULL)
14676 timer->tr_paused = paused;
14677 }
14678}
14679
14680/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014681 * "timer_start(time, callback [, options])" function
14682 */
14683 static void
14684f_timer_start(typval_T *argvars, typval_T *rettv)
14685{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014686 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014687 timer_T *timer;
14688 int repeat = 0;
14689 char_u *callback;
14690 dict_T *dict;
14691 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014692
Bram Moolenaar75537a92016-09-05 22:45:28 +020014693 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014694 if (check_secure())
14695 return;
14696 if (argvars[2].v_type != VAR_UNKNOWN)
14697 {
14698 if (argvars[2].v_type != VAR_DICT
14699 || (dict = argvars[2].vval.v_dict) == NULL)
14700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014701 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014702 return;
14703 }
14704 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014705 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014706 }
14707
Bram Moolenaar75537a92016-09-05 22:45:28 +020014708 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014709 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014710 return;
14711
14712 timer = create_timer(msec, repeat);
14713 if (timer == NULL)
14714 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014715 else
14716 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014717 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014718 timer->tr_callback = vim_strsave(callback);
14719 else
14720 /* pointer into the partial */
14721 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014722 timer->tr_partial = partial;
14723 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014724 }
14725}
14726
14727/*
14728 * "timer_stop(timer)" function
14729 */
14730 static void
14731f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14732{
14733 timer_T *timer;
14734
14735 if (argvars[0].v_type != VAR_NUMBER)
14736 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014737 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014738 return;
14739 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014740 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014741 if (timer != NULL)
14742 stop_timer(timer);
14743}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014744
14745/*
14746 * "timer_stopall()" function
14747 */
14748 static void
14749f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14750{
14751 stop_all_timers();
14752}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014753#endif
14754
14755/*
14756 * "tolower(string)" function
14757 */
14758 static void
14759f_tolower(typval_T *argvars, typval_T *rettv)
14760{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014761 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014762 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014763}
14764
14765/*
14766 * "toupper(string)" function
14767 */
14768 static void
14769f_toupper(typval_T *argvars, typval_T *rettv)
14770{
14771 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014772 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014773}
14774
14775/*
14776 * "tr(string, fromstr, tostr)" function
14777 */
14778 static void
14779f_tr(typval_T *argvars, typval_T *rettv)
14780{
14781 char_u *in_str;
14782 char_u *fromstr;
14783 char_u *tostr;
14784 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014785 int inlen;
14786 int fromlen;
14787 int tolen;
14788 int idx;
14789 char_u *cpstr;
14790 int cplen;
14791 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792 char_u buf[NUMBUFLEN];
14793 char_u buf2[NUMBUFLEN];
14794 garray_T ga;
14795
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014796 in_str = tv_get_string(&argvars[0]);
14797 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14798 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014799
14800 /* Default return value: empty string. */
14801 rettv->v_type = VAR_STRING;
14802 rettv->vval.v_string = NULL;
14803 if (fromstr == NULL || tostr == NULL)
14804 return; /* type error; errmsg already given */
14805 ga_init2(&ga, (int)sizeof(char), 80);
14806
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014807 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 /* not multi-byte: fromstr and tostr must be the same length */
14809 if (STRLEN(fromstr) != STRLEN(tostr))
14810 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014811error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014812 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014813 ga_clear(&ga);
14814 return;
14815 }
14816
14817 /* fromstr and tostr have to contain the same number of chars */
14818 while (*in_str != NUL)
14819 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014820 if (has_mbyte)
14821 {
14822 inlen = (*mb_ptr2len)(in_str);
14823 cpstr = in_str;
14824 cplen = inlen;
14825 idx = 0;
14826 for (p = fromstr; *p != NUL; p += fromlen)
14827 {
14828 fromlen = (*mb_ptr2len)(p);
14829 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14830 {
14831 for (p = tostr; *p != NUL; p += tolen)
14832 {
14833 tolen = (*mb_ptr2len)(p);
14834 if (idx-- == 0)
14835 {
14836 cplen = tolen;
14837 cpstr = p;
14838 break;
14839 }
14840 }
14841 if (*p == NUL) /* tostr is shorter than fromstr */
14842 goto error;
14843 break;
14844 }
14845 ++idx;
14846 }
14847
14848 if (first && cpstr == in_str)
14849 {
14850 /* Check that fromstr and tostr have the same number of
14851 * (multi-byte) characters. Done only once when a character
14852 * of in_str doesn't appear in fromstr. */
14853 first = FALSE;
14854 for (p = tostr; *p != NUL; p += tolen)
14855 {
14856 tolen = (*mb_ptr2len)(p);
14857 --idx;
14858 }
14859 if (idx != 0)
14860 goto error;
14861 }
14862
14863 (void)ga_grow(&ga, cplen);
14864 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14865 ga.ga_len += cplen;
14866
14867 in_str += inlen;
14868 }
14869 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014870 {
14871 /* When not using multi-byte chars we can do it faster. */
14872 p = vim_strchr(fromstr, *in_str);
14873 if (p != NULL)
14874 ga_append(&ga, tostr[p - fromstr]);
14875 else
14876 ga_append(&ga, *in_str);
14877 ++in_str;
14878 }
14879 }
14880
14881 /* add a terminating NUL */
14882 (void)ga_grow(&ga, 1);
14883 ga_append(&ga, NUL);
14884
14885 rettv->vval.v_string = ga.ga_data;
14886}
14887
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014888/*
14889 * "trim({expr})" function
14890 */
14891 static void
14892f_trim(typval_T *argvars, typval_T *rettv)
14893{
14894 char_u buf1[NUMBUFLEN];
14895 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014896 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014897 char_u *mask = NULL;
14898 char_u *tail;
14899 char_u *prev;
14900 char_u *p;
14901 int c1;
14902
14903 rettv->v_type = VAR_STRING;
14904 if (head == NULL)
14905 {
14906 rettv->vval.v_string = NULL;
14907 return;
14908 }
14909
14910 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014911 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014912
14913 while (*head != NUL)
14914 {
14915 c1 = PTR2CHAR(head);
14916 if (mask == NULL)
14917 {
14918 if (c1 > ' ' && c1 != 0xa0)
14919 break;
14920 }
14921 else
14922 {
14923 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14924 if (c1 == PTR2CHAR(p))
14925 break;
14926 if (*p == NUL)
14927 break;
14928 }
14929 MB_PTR_ADV(head);
14930 }
14931
14932 for (tail = head + STRLEN(head); tail > head; tail = prev)
14933 {
14934 prev = tail;
14935 MB_PTR_BACK(head, prev);
14936 c1 = PTR2CHAR(prev);
14937 if (mask == NULL)
14938 {
14939 if (c1 > ' ' && c1 != 0xa0)
14940 break;
14941 }
14942 else
14943 {
14944 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14945 if (c1 == PTR2CHAR(p))
14946 break;
14947 if (*p == NUL)
14948 break;
14949 }
14950 }
14951 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14952}
14953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014954#ifdef FEAT_FLOAT
14955/*
14956 * "trunc({float})" function
14957 */
14958 static void
14959f_trunc(typval_T *argvars, typval_T *rettv)
14960{
14961 float_T f = 0.0;
14962
14963 rettv->v_type = VAR_FLOAT;
14964 if (get_float_arg(argvars, &f) == OK)
14965 /* trunc() is not in C90, use floor() or ceil() instead. */
14966 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14967 else
14968 rettv->vval.v_float = 0.0;
14969}
14970#endif
14971
14972/*
14973 * "type(expr)" function
14974 */
14975 static void
14976f_type(typval_T *argvars, typval_T *rettv)
14977{
14978 int n = -1;
14979
14980 switch (argvars[0].v_type)
14981 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014982 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14983 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014984 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014985 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14986 case VAR_LIST: n = VAR_TYPE_LIST; break;
14987 case VAR_DICT: n = VAR_TYPE_DICT; break;
14988 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014989 case VAR_SPECIAL:
14990 if (argvars[0].vval.v_number == VVAL_FALSE
14991 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014992 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014993 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014994 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014995 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014996 case VAR_JOB: n = VAR_TYPE_JOB; break;
14997 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014998 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014999 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015000 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015001 n = -1;
15002 break;
15003 }
15004 rettv->vval.v_number = n;
15005}
15006
15007/*
15008 * "undofile(name)" function
15009 */
15010 static void
15011f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15012{
15013 rettv->v_type = VAR_STRING;
15014#ifdef FEAT_PERSISTENT_UNDO
15015 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015016 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015017
15018 if (*fname == NUL)
15019 {
15020 /* If there is no file name there will be no undo file. */
15021 rettv->vval.v_string = NULL;
15022 }
15023 else
15024 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015025 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015026
15027 if (ffname != NULL)
15028 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15029 vim_free(ffname);
15030 }
15031 }
15032#else
15033 rettv->vval.v_string = NULL;
15034#endif
15035}
15036
15037/*
15038 * "undotree()" function
15039 */
15040 static void
15041f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15042{
15043 if (rettv_dict_alloc(rettv) == OK)
15044 {
15045 dict_T *dict = rettv->vval.v_dict;
15046 list_T *list;
15047
Bram Moolenaare0be1672018-07-08 16:50:37 +020015048 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15049 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15050 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15051 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15052 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15053 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015054
15055 list = list_alloc();
15056 if (list != NULL)
15057 {
15058 u_eval_tree(curbuf->b_u_oldhead, list);
15059 dict_add_list(dict, "entries", list);
15060 }
15061 }
15062}
15063
15064/*
15065 * "values(dict)" function
15066 */
15067 static void
15068f_values(typval_T *argvars, typval_T *rettv)
15069{
15070 dict_list(argvars, rettv, 1);
15071}
15072
15073/*
15074 * "virtcol(string)" function
15075 */
15076 static void
15077f_virtcol(typval_T *argvars, typval_T *rettv)
15078{
15079 colnr_T vcol = 0;
15080 pos_T *fp;
15081 int fnum = curbuf->b_fnum;
15082
15083 fp = var2fpos(&argvars[0], FALSE, &fnum);
15084 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15085 && fnum == curbuf->b_fnum)
15086 {
15087 getvvcol(curwin, fp, NULL, NULL, &vcol);
15088 ++vcol;
15089 }
15090
15091 rettv->vval.v_number = vcol;
15092}
15093
15094/*
15095 * "visualmode()" function
15096 */
15097 static void
15098f_visualmode(typval_T *argvars, typval_T *rettv)
15099{
15100 char_u str[2];
15101
15102 rettv->v_type = VAR_STRING;
15103 str[0] = curbuf->b_visual_mode_eval;
15104 str[1] = NUL;
15105 rettv->vval.v_string = vim_strsave(str);
15106
15107 /* A non-zero number or non-empty string argument: reset mode. */
15108 if (non_zero_arg(&argvars[0]))
15109 curbuf->b_visual_mode_eval = NUL;
15110}
15111
15112/*
15113 * "wildmenumode()" function
15114 */
15115 static void
15116f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15117{
15118#ifdef FEAT_WILDMENU
15119 if (wild_menu_showing)
15120 rettv->vval.v_number = 1;
15121#endif
15122}
15123
15124/*
15125 * "winbufnr(nr)" function
15126 */
15127 static void
15128f_winbufnr(typval_T *argvars, typval_T *rettv)
15129{
15130 win_T *wp;
15131
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015132 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015133 if (wp == NULL)
15134 rettv->vval.v_number = -1;
15135 else
15136 rettv->vval.v_number = wp->w_buffer->b_fnum;
15137}
15138
15139/*
15140 * "wincol()" function
15141 */
15142 static void
15143f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15144{
15145 validate_cursor();
15146 rettv->vval.v_number = curwin->w_wcol + 1;
15147}
15148
15149/*
15150 * "winheight(nr)" function
15151 */
15152 static void
15153f_winheight(typval_T *argvars, typval_T *rettv)
15154{
15155 win_T *wp;
15156
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015157 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015158 if (wp == NULL)
15159 rettv->vval.v_number = -1;
15160 else
15161 rettv->vval.v_number = wp->w_height;
15162}
15163
15164/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015165 * "winlayout()" function
15166 */
15167 static void
15168f_winlayout(typval_T *argvars, typval_T *rettv)
15169{
15170 tabpage_T *tp;
15171
15172 if (rettv_list_alloc(rettv) != OK)
15173 return;
15174
15175 if (argvars[0].v_type == VAR_UNKNOWN)
15176 tp = curtab;
15177 else
15178 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015179 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015180 if (tp == NULL)
15181 return;
15182 }
15183
15184 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15185}
15186
15187/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015188 * "winline()" function
15189 */
15190 static void
15191f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15192{
15193 validate_cursor();
15194 rettv->vval.v_number = curwin->w_wrow + 1;
15195}
15196
15197/*
15198 * "winnr()" function
15199 */
15200 static void
15201f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15202{
15203 int nr = 1;
15204
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015205 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015206 rettv->vval.v_number = nr;
15207}
15208
15209/*
15210 * "winrestcmd()" function
15211 */
15212 static void
15213f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15214{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015215 win_T *wp;
15216 int winnr = 1;
15217 garray_T ga;
15218 char_u buf[50];
15219
15220 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015221 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015222 {
15223 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15224 ga_concat(&ga, buf);
15225 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15226 ga_concat(&ga, buf);
15227 ++winnr;
15228 }
15229 ga_append(&ga, NUL);
15230
15231 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015232 rettv->v_type = VAR_STRING;
15233}
15234
15235/*
15236 * "winrestview()" function
15237 */
15238 static void
15239f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15240{
15241 dict_T *dict;
15242
15243 if (argvars[0].v_type != VAR_DICT
15244 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015245 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015246 else
15247 {
15248 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015249 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015250 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015251 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015252 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015253 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015254 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15255 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015256 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015257 curwin->w_set_curswant = FALSE;
15258 }
15259
15260 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015261 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015262#ifdef FEAT_DIFF
15263 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015264 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015265#endif
15266 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015267 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015268 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015269 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015270
15271 check_cursor();
15272 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015273 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015274 changed_window_setting();
15275
15276 if (curwin->w_topline <= 0)
15277 curwin->w_topline = 1;
15278 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15279 curwin->w_topline = curbuf->b_ml.ml_line_count;
15280#ifdef FEAT_DIFF
15281 check_topfill(curwin, TRUE);
15282#endif
15283 }
15284}
15285
15286/*
15287 * "winsaveview()" function
15288 */
15289 static void
15290f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15291{
15292 dict_T *dict;
15293
15294 if (rettv_dict_alloc(rettv) == FAIL)
15295 return;
15296 dict = rettv->vval.v_dict;
15297
Bram Moolenaare0be1672018-07-08 16:50:37 +020015298 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15299 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015300 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015301 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015302 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015303
Bram Moolenaare0be1672018-07-08 16:50:37 +020015304 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015305#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015306 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015307#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015308 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15309 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015310}
15311
15312/*
15313 * "winwidth(nr)" function
15314 */
15315 static void
15316f_winwidth(typval_T *argvars, typval_T *rettv)
15317{
15318 win_T *wp;
15319
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015320 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015321 if (wp == NULL)
15322 rettv->vval.v_number = -1;
15323 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015324 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015325}
15326
15327/*
15328 * "wordcount()" function
15329 */
15330 static void
15331f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15332{
15333 if (rettv_dict_alloc(rettv) == FAIL)
15334 return;
15335 cursor_pos_info(rettv->vval.v_dict);
15336}
15337
15338/*
15339 * "writefile()" function
15340 */
15341 static void
15342f_writefile(typval_T *argvars, typval_T *rettv)
15343{
15344 int binary = FALSE;
15345 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015346#ifdef HAVE_FSYNC
15347 int do_fsync = p_fs;
15348#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015349 char_u *fname;
15350 FILE *fd;
15351 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015352 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015353 list_T *list = NULL;
15354 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015355
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015356 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015357 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015358 return;
15359
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015360 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015361 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015362 list = argvars[0].vval.v_list;
15363 if (list == NULL)
15364 return;
15365 for (li = list->lv_first; li != NULL; li = li->li_next)
15366 if (tv_get_string_chk(&li->li_tv) == NULL)
15367 return;
15368 }
15369 else if (argvars[0].v_type == VAR_BLOB)
15370 {
15371 blob = argvars[0].vval.v_blob;
15372 if (blob == NULL)
15373 return;
15374 }
15375 else
15376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015377 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015378 return;
15379 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015380
15381 if (argvars[2].v_type != VAR_UNKNOWN)
15382 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015383 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015384
15385 if (arg2 == NULL)
15386 return;
15387 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015388 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015389 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015390 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015391#ifdef HAVE_FSYNC
15392 if (vim_strchr(arg2, 's') != NULL)
15393 do_fsync = TRUE;
15394 else if (vim_strchr(arg2, 'S') != NULL)
15395 do_fsync = FALSE;
15396#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015397 }
15398
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015399 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015400 if (fname == NULL)
15401 return;
15402
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015403 /* Always open the file in binary mode, library functions have a mind of
15404 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015405 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15406 append ? APPENDBIN : WRITEBIN)) == NULL)
15407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015408 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015409 ret = -1;
15410 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015411 else if (blob)
15412 {
15413 if (write_blob(fd, blob) == FAIL)
15414 ret = -1;
15415#ifdef HAVE_FSYNC
15416 else if (do_fsync)
15417 // Ignore the error, the user wouldn't know what to do about it.
15418 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015419 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015420#endif
15421 fclose(fd);
15422 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015423 else
15424 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015425 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015426 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015427#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015428 else if (do_fsync)
15429 /* Ignore the error, the user wouldn't know what to do about it.
15430 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015431 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015433 fclose(fd);
15434 }
15435
15436 rettv->vval.v_number = ret;
15437}
15438
15439/*
15440 * "xor(expr, expr)" function
15441 */
15442 static void
15443f_xor(typval_T *argvars, typval_T *rettv)
15444{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015445 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15446 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015447}
15448
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015449#endif /* FEAT_EVAL */