blob: 58ea7103fc8876e95e091108c78770a653edbad4 [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
810#ifdef FEAT_FLOAT
811 {"pow", 2, 2, f_pow},
812#endif
813 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100814 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200815#ifdef FEAT_JOB_CHANNEL
816 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200817 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200818 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
819#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100820#ifdef FEAT_TEXT_PROP
821 {"prop_add", 3, 3, f_prop_add},
822 {"prop_clear", 1, 3, f_prop_clear},
823 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100824 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100825 {"prop_type_add", 2, 2, f_prop_type_add},
826 {"prop_type_change", 2, 2, f_prop_type_change},
827 {"prop_type_delete", 1, 2, f_prop_type_delete},
828 {"prop_type_get", 1, 2, f_prop_type_get},
829 {"prop_type_list", 0, 1, f_prop_type_list},
830#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200831 {"pumvisible", 0, 0, f_pumvisible},
832#ifdef FEAT_PYTHON3
833 {"py3eval", 1, 1, f_py3eval},
834#endif
835#ifdef FEAT_PYTHON
836 {"pyeval", 1, 1, f_pyeval},
837#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100838#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
839 {"pyxeval", 1, 1, f_pyxeval},
840#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200841 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200842 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200843 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200844 {"reg_executing", 0, 0, f_reg_executing},
845 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200846 {"reltime", 0, 2, f_reltime},
847#ifdef FEAT_FLOAT
848 {"reltimefloat", 1, 1, f_reltimefloat},
849#endif
850 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100851 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"remote_foreground", 1, 1, f_remote_foreground},
853 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100854 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100856 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"remove", 2, 3, f_remove},
858 {"rename", 2, 2, f_rename},
859 {"repeat", 2, 2, f_repeat},
860 {"resolve", 1, 1, f_resolve},
861 {"reverse", 1, 1, f_reverse},
862#ifdef FEAT_FLOAT
863 {"round", 1, 1, f_round},
864#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100865#ifdef FEAT_RUBY
866 {"rubyeval", 1, 1, f_rubyeval},
867#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"screenattr", 2, 2, f_screenattr},
869 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100870 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200871 {"screencol", 0, 0, f_screencol},
872 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100873 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200874 {"search", 1, 4, f_search},
875 {"searchdecl", 1, 3, f_searchdecl},
876 {"searchpair", 3, 7, f_searchpair},
877 {"searchpairpos", 3, 7, f_searchpairpos},
878 {"searchpos", 1, 4, f_searchpos},
879 {"server2client", 2, 2, f_server2client},
880 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200881 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"setbufvar", 3, 3, f_setbufvar},
883 {"setcharsearch", 1, 1, f_setcharsearch},
884 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200885 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200886 {"setfperm", 2, 2, f_setfperm},
887 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200888 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100889 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200890 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200891 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200892 {"setreg", 2, 3, f_setreg},
893 {"settabvar", 3, 3, f_settabvar},
894 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100895 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200896 {"setwinvar", 3, 3, f_setwinvar},
897#ifdef FEAT_CRYPT
898 {"sha256", 1, 1, f_sha256},
899#endif
900 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100901 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100902#ifdef FEAT_SIGNS
903 {"sign_define", 1, 2, f_sign_define},
904 {"sign_getdefined", 0, 1, f_sign_getdefined},
905 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100906 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100907 {"sign_place", 4, 5, f_sign_place},
908 {"sign_undefine", 0, 1, f_sign_undefine},
909 {"sign_unplace", 1, 2, f_sign_unplace},
910#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200911 {"simplify", 1, 1, f_simplify},
912#ifdef FEAT_FLOAT
913 {"sin", 1, 1, f_sin},
914 {"sinh", 1, 1, f_sinh},
915#endif
916 {"sort", 1, 3, f_sort},
917 {"soundfold", 1, 1, f_soundfold},
918 {"spellbadword", 0, 1, f_spellbadword},
919 {"spellsuggest", 1, 3, f_spellsuggest},
920 {"split", 1, 3, f_split},
921#ifdef FEAT_FLOAT
922 {"sqrt", 1, 1, f_sqrt},
923 {"str2float", 1, 1, f_str2float},
924#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200925 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200926 {"str2nr", 1, 2, f_str2nr},
927 {"strcharpart", 2, 3, f_strcharpart},
928 {"strchars", 1, 2, f_strchars},
929 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
930#ifdef HAVE_STRFTIME
931 {"strftime", 1, 2, f_strftime},
932#endif
933 {"strgetchar", 2, 2, f_strgetchar},
934 {"stridx", 2, 3, f_stridx},
935 {"string", 1, 1, f_string},
936 {"strlen", 1, 1, f_strlen},
937 {"strpart", 2, 3, f_strpart},
938 {"strridx", 2, 3, f_strridx},
939 {"strtrans", 1, 1, f_strtrans},
940 {"strwidth", 1, 1, f_strwidth},
941 {"submatch", 1, 2, f_submatch},
942 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200943 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200944 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200945 {"synID", 3, 3, f_synID},
946 {"synIDattr", 2, 3, f_synIDattr},
947 {"synIDtrans", 1, 1, f_synIDtrans},
948 {"synconcealed", 2, 2, f_synconcealed},
949 {"synstack", 2, 2, f_synstack},
950 {"system", 1, 2, f_system},
951 {"systemlist", 1, 2, f_systemlist},
952 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
953 {"tabpagenr", 0, 1, f_tabpagenr},
954 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
955 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100956 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200957#ifdef FEAT_FLOAT
958 {"tan", 1, 1, f_tan},
959 {"tanh", 1, 1, f_tanh},
960#endif
961 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200962#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100963 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
964 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100965 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200966 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200967# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
968 {"term_getansicolors", 1, 1, f_term_getansicolors},
969# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200970 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200971 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200972 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200973 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200974 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200975 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200976 {"term_getstatus", 1, 1, f_term_getstatus},
977 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200978 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200980 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200982# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
983 {"term_setansicolors", 2, 2, f_term_setansicolors},
984# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100985 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100986 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200987 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200989 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200991 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
992 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +0200993 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200994 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +0200995 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +0100996 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +0100997 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200998#ifdef FEAT_JOB_CHANNEL
999 {"test_null_channel", 0, 0, f_test_null_channel},
1000#endif
1001 {"test_null_dict", 0, 0, f_test_null_dict},
1002#ifdef FEAT_JOB_CHANNEL
1003 {"test_null_job", 0, 0, f_test_null_job},
1004#endif
1005 {"test_null_list", 0, 0, f_test_null_list},
1006 {"test_null_partial", 0, 0, f_test_null_partial},
1007 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001008 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001009 {"test_override", 2, 2, f_test_override},
1010 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001011#ifdef FEAT_GUI
1012 {"test_scrollbar", 3, 3, f_test_scrollbar},
1013#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001014#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001015 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001016#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001017 {"test_settime", 1, 1, f_test_settime},
1018#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001019 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001020 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001021 {"timer_start", 2, 3, f_timer_start},
1022 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001023 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001024#endif
1025 {"tolower", 1, 1, f_tolower},
1026 {"toupper", 1, 1, f_toupper},
1027 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001028 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001029#ifdef FEAT_FLOAT
1030 {"trunc", 1, 1, f_trunc},
1031#endif
1032 {"type", 1, 1, f_type},
1033 {"undofile", 1, 1, f_undofile},
1034 {"undotree", 0, 0, f_undotree},
1035 {"uniq", 1, 3, f_uniq},
1036 {"values", 1, 1, f_values},
1037 {"virtcol", 1, 1, f_virtcol},
1038 {"visualmode", 0, 1, f_visualmode},
1039 {"wildmenumode", 0, 0, f_wildmenumode},
1040 {"win_findbuf", 1, 1, f_win_findbuf},
1041 {"win_getid", 0, 2, f_win_getid},
1042 {"win_gotoid", 1, 1, f_win_gotoid},
1043 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1044 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001045 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001046 {"winbufnr", 1, 1, f_winbufnr},
1047 {"wincol", 0, 0, f_wincol},
1048 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001049 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001050 {"winline", 0, 0, f_winline},
1051 {"winnr", 0, 1, f_winnr},
1052 {"winrestcmd", 0, 0, f_winrestcmd},
1053 {"winrestview", 1, 1, f_winrestview},
1054 {"winsaveview", 0, 0, f_winsaveview},
1055 {"winwidth", 1, 1, f_winwidth},
1056 {"wordcount", 0, 0, f_wordcount},
1057 {"writefile", 2, 3, f_writefile},
1058 {"xor", 2, 2, f_xor},
1059};
1060
1061#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1062
1063/*
1064 * Function given to ExpandGeneric() to obtain the list of internal
1065 * or user defined function names.
1066 */
1067 char_u *
1068get_function_name(expand_T *xp, int idx)
1069{
1070 static int intidx = -1;
1071 char_u *name;
1072
1073 if (idx == 0)
1074 intidx = -1;
1075 if (intidx < 0)
1076 {
1077 name = get_user_func_name(xp, idx);
1078 if (name != NULL)
1079 return name;
1080 }
1081 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1082 {
1083 STRCPY(IObuff, functions[intidx].f_name);
1084 STRCAT(IObuff, "(");
1085 if (functions[intidx].f_max_argc == 0)
1086 STRCAT(IObuff, ")");
1087 return IObuff;
1088 }
1089
1090 return NULL;
1091}
1092
1093/*
1094 * Function given to ExpandGeneric() to obtain the list of internal or
1095 * user defined variable or function names.
1096 */
1097 char_u *
1098get_expr_name(expand_T *xp, int idx)
1099{
1100 static int intidx = -1;
1101 char_u *name;
1102
1103 if (idx == 0)
1104 intidx = -1;
1105 if (intidx < 0)
1106 {
1107 name = get_function_name(xp, idx);
1108 if (name != NULL)
1109 return name;
1110 }
1111 return get_user_var_name(xp, ++intidx);
1112}
1113
1114#endif /* FEAT_CMDL_COMPL */
1115
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001116/*
1117 * Find internal function in table above.
1118 * Return index, or -1 if not found
1119 */
1120 int
1121find_internal_func(
1122 char_u *name) /* name of the function */
1123{
1124 int first = 0;
1125 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1126 int cmp;
1127 int x;
1128
1129 /*
1130 * Find the function name in the table. Binary search.
1131 */
1132 while (first <= last)
1133 {
1134 x = first + ((unsigned)(last - first) >> 1);
1135 cmp = STRCMP(name, functions[x].f_name);
1136 if (cmp < 0)
1137 last = x - 1;
1138 else if (cmp > 0)
1139 first = x + 1;
1140 else
1141 return x;
1142 }
1143 return -1;
1144}
1145
1146 int
1147call_internal_func(
1148 char_u *name,
1149 int argcount,
1150 typval_T *argvars,
1151 typval_T *rettv)
1152{
1153 int i;
1154
1155 i = find_internal_func(name);
1156 if (i < 0)
1157 return ERROR_UNKNOWN;
1158 if (argcount < functions[i].f_min_argc)
1159 return ERROR_TOOFEW;
1160 if (argcount > functions[i].f_max_argc)
1161 return ERROR_TOOMANY;
1162 argvars[argcount].v_type = VAR_UNKNOWN;
1163 functions[i].f_func(argvars, rettv);
1164 return ERROR_NONE;
1165}
1166
1167/*
1168 * Return TRUE for a non-zero Number and a non-empty String.
1169 */
1170 static int
1171non_zero_arg(typval_T *argvars)
1172{
1173 return ((argvars[0].v_type == VAR_NUMBER
1174 && argvars[0].vval.v_number != 0)
1175 || (argvars[0].v_type == VAR_SPECIAL
1176 && argvars[0].vval.v_number == VVAL_TRUE)
1177 || (argvars[0].v_type == VAR_STRING
1178 && argvars[0].vval.v_string != NULL
1179 && *argvars[0].vval.v_string != NUL));
1180}
1181
1182/*
1183 * Get the lnum from the first argument.
1184 * Also accepts ".", "$", etc., but that only works for the current buffer.
1185 * Returns -1 on error.
1186 */
1187 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001188tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001189{
1190 typval_T rettv;
1191 linenr_T lnum;
1192
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001193 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001194 if (lnum == 0) /* no valid number, try using line() */
1195 {
1196 rettv.v_type = VAR_NUMBER;
1197 f_line(argvars, &rettv);
1198 lnum = (linenr_T)rettv.vval.v_number;
1199 clear_tv(&rettv);
1200 }
1201 return lnum;
1202}
1203
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001204/*
1205 * Get the lnum from the first argument.
1206 * Also accepts "$", then "buf" is used.
1207 * Returns 0 on error.
1208 */
1209 static linenr_T
1210tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1211{
1212 if (argvars[0].v_type == VAR_STRING
1213 && argvars[0].vval.v_string != NULL
1214 && argvars[0].vval.v_string[0] == '$'
1215 && buf != NULL)
1216 return buf->b_ml.ml_line_count;
1217 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1218}
1219
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001220#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001221/*
1222 * Get the float value of "argvars[0]" into "f".
1223 * Returns FAIL when the argument is not a Number or Float.
1224 */
1225 static int
1226get_float_arg(typval_T *argvars, float_T *f)
1227{
1228 if (argvars[0].v_type == VAR_FLOAT)
1229 {
1230 *f = argvars[0].vval.v_float;
1231 return OK;
1232 }
1233 if (argvars[0].v_type == VAR_NUMBER)
1234 {
1235 *f = (float_T)argvars[0].vval.v_number;
1236 return OK;
1237 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001238 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001239 return FAIL;
1240}
1241
1242/*
1243 * "abs(expr)" function
1244 */
1245 static void
1246f_abs(typval_T *argvars, typval_T *rettv)
1247{
1248 if (argvars[0].v_type == VAR_FLOAT)
1249 {
1250 rettv->v_type = VAR_FLOAT;
1251 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1252 }
1253 else
1254 {
1255 varnumber_T n;
1256 int error = FALSE;
1257
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001258 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001259 if (error)
1260 rettv->vval.v_number = -1;
1261 else if (n > 0)
1262 rettv->vval.v_number = n;
1263 else
1264 rettv->vval.v_number = -n;
1265 }
1266}
1267
1268/*
1269 * "acos()" function
1270 */
1271 static void
1272f_acos(typval_T *argvars, typval_T *rettv)
1273{
1274 float_T f = 0.0;
1275
1276 rettv->v_type = VAR_FLOAT;
1277 if (get_float_arg(argvars, &f) == OK)
1278 rettv->vval.v_float = acos(f);
1279 else
1280 rettv->vval.v_float = 0.0;
1281}
1282#endif
1283
1284/*
1285 * "add(list, item)" function
1286 */
1287 static void
1288f_add(typval_T *argvars, typval_T *rettv)
1289{
1290 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001292
1293 rettv->vval.v_number = 1; /* Default: Failed */
1294 if (argvars[0].v_type == VAR_LIST)
1295 {
1296 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001297 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001298 (char_u *)N_("add() argument"), TRUE)
1299 && list_append_tv(l, &argvars[1]) == OK)
1300 copy_tv(&argvars[0], rettv);
1301 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001302 else if (argvars[0].v_type == VAR_BLOB)
1303 {
1304 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001305 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001306 (char_u *)N_("add() argument"), TRUE))
1307 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001308 int error = FALSE;
1309 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1310
1311 if (!error)
1312 {
1313 ga_append(&b->bv_ga, (int)n);
1314 copy_tv(&argvars[0], rettv);
1315 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 }
1317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001318 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001319 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001320}
1321
1322/*
1323 * "and(expr, expr)" function
1324 */
1325 static void
1326f_and(typval_T *argvars, typval_T *rettv)
1327{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001328 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1329 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001330}
1331
1332/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001333 * If there is a window for "curbuf", make it the current window.
1334 */
1335 static void
1336find_win_for_curbuf(void)
1337{
1338 wininfo_T *wip;
1339
1340 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1341 {
1342 if (wip->wi_win != NULL)
1343 {
1344 curwin = wip->wi_win;
1345 break;
1346 }
1347 }
1348}
1349
1350/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001351 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001352 */
1353 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001354set_buffer_lines(
1355 buf_T *buf,
1356 linenr_T lnum_arg,
1357 int append,
1358 typval_T *lines,
1359 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001360{
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1362 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001363 list_T *l = NULL;
1364 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001365 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001366 linenr_T append_lnum;
1367 buf_T *curbuf_save = NULL;
1368 win_T *curwin_save = NULL;
1369 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 /* When using the current buffer ml_mfp will be set if needed. Useful when
1372 * setline() is used on startup. For other buffers the buffer must be
1373 * loaded. */
1374 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 rettv->vval.v_number = 1; /* FAIL */
1377 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001378 }
1379
Bram Moolenaarca851592018-06-06 21:04:07 +02001380 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001381 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001382 curbuf_save = curbuf;
1383 curwin_save = curwin;
1384 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001385 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 }
1387
1388 if (append)
1389 // appendbufline() uses the line number below which we insert
1390 append_lnum = lnum - 1;
1391 else
1392 // setbufline() uses the line number above which we insert, we only
1393 // append if it's below the last line
1394 append_lnum = curbuf->b_ml.ml_line_count;
1395
1396 if (lines->v_type == VAR_LIST)
1397 {
1398 l = lines->vval.v_list;
1399 li = l->lv_first;
1400 }
1401 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001402 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001403
1404 /* default result is zero == OK */
1405 for (;;)
1406 {
1407 if (l != NULL)
1408 {
1409 /* list argument, get next string */
1410 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001411 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001412 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001413 li = li->li_next;
1414 }
1415
Bram Moolenaarca851592018-06-06 21:04:07 +02001416 rettv->vval.v_number = 1; /* FAIL */
1417 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1418 break;
1419
1420 /* When coming here from Insert mode, sync undo, so that this can be
1421 * undone separately from what was previously inserted. */
1422 if (u_sync_once == 2)
1423 {
1424 u_sync_once = 1; /* notify that u_sync() was called */
1425 u_sync(TRUE);
1426 }
1427
1428 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1429 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001430 // Existing line, replace it.
1431 // Removes any existing text properties.
1432 if (u_savesub(lnum) == OK && ml_replace_len(
1433 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001434 {
1435 changed_bytes(lnum, 0);
1436 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1437 check_cursor_col();
1438 rettv->vval.v_number = 0; /* OK */
1439 }
1440 }
1441 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1442 {
1443 /* append the line */
1444 ++added;
1445 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1446 rettv->vval.v_number = 0; /* OK */
1447 }
1448
1449 if (l == NULL) /* only one string argument */
1450 break;
1451 ++lnum;
1452 }
1453
1454 if (added > 0)
1455 {
1456 win_T *wp;
1457 tabpage_T *tp;
1458
1459 appended_lines_mark(append_lnum, added);
1460 FOR_ALL_TAB_WINDOWS(tp, wp)
1461 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1462 wp->w_cursor.lnum += added;
1463 check_cursor_col();
1464
Bram Moolenaarf2732452018-06-03 14:47:35 +02001465#ifdef FEAT_JOB_CHANNEL
1466 if (bt_prompt(curbuf) && (State & INSERT))
1467 // show the line with the prompt
1468 update_topline();
1469#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001470 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001471
1472 if (!is_curbuf)
1473 {
1474 curbuf = curbuf_save;
1475 curwin = curwin_save;
1476 }
1477}
1478
1479/*
1480 * "append(lnum, string/list)" function
1481 */
1482 static void
1483f_append(typval_T *argvars, typval_T *rettv)
1484{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001485 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001486
1487 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1488}
1489
1490/*
1491 * "appendbufline(buf, lnum, string/list)" function
1492 */
1493 static void
1494f_appendbufline(typval_T *argvars, typval_T *rettv)
1495{
1496 linenr_T lnum;
1497 buf_T *buf;
1498
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001499 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001500 if (buf == NULL)
1501 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001502 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001503 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001504 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001505 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1506 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001507}
1508
1509/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001510 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001511 */
1512 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001513f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001515 win_T *wp;
1516
1517 if (argvars[0].v_type == VAR_UNKNOWN)
1518 // use the current window
1519 rettv->vval.v_number = ARGCOUNT;
1520 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001521 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 // use the global argument list
1523 rettv->vval.v_number = GARGCOUNT;
1524 else
1525 {
1526 // use the argument list of the specified window
1527 wp = find_win_by_nr_or_id(&argvars[0]);
1528 if (wp != NULL)
1529 rettv->vval.v_number = WARGCOUNT(wp);
1530 else
1531 rettv->vval.v_number = -1;
1532 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001533}
1534
1535/*
1536 * "argidx()" function
1537 */
1538 static void
1539f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1540{
1541 rettv->vval.v_number = curwin->w_arg_idx;
1542}
1543
1544/*
1545 * "arglistid()" function
1546 */
1547 static void
1548f_arglistid(typval_T *argvars, typval_T *rettv)
1549{
1550 win_T *wp;
1551
1552 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001553 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001554 if (wp != NULL)
1555 rettv->vval.v_number = wp->w_alist->id;
1556}
1557
1558/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001559 * Get the argument list for a given window
1560 */
1561 static void
1562get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1563{
1564 int idx;
1565
1566 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1567 for (idx = 0; idx < argcount; ++idx)
1568 list_append_string(rettv->vval.v_list,
1569 alist_name(&arglist[idx]), -1);
1570}
1571
1572/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001573 * "argv(nr)" function
1574 */
1575 static void
1576f_argv(typval_T *argvars, typval_T *rettv)
1577{
1578 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001579 aentry_T *arglist = NULL;
1580 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001581
1582 if (argvars[0].v_type != VAR_UNKNOWN)
1583 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001584 if (argvars[1].v_type == VAR_UNKNOWN)
1585 {
1586 arglist = ARGLIST;
1587 argcount = ARGCOUNT;
1588 }
1589 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001590 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 {
1592 arglist = GARGLIST;
1593 argcount = GARGCOUNT;
1594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001595 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 {
1597 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1598
1599 if (wp != NULL)
1600 {
1601 /* Use the argument list of the specified window */
1602 arglist = WARGLIST(wp);
1603 argcount = WARGCOUNT(wp);
1604 }
1605 }
1606
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001609 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001610 if (arglist != NULL && idx >= 0 && idx < argcount)
1611 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1612 else if (idx == -1)
1613 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001614 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001615 else
1616 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617}
1618
1619/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001620 * "assert_beeps(cmd [, error])" function
1621 */
1622 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001623f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001624{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001625 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001626}
1627
1628/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629 * "assert_equal(expected, actual[, msg])" function
1630 */
1631 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001632f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001633{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001634 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001635}
1636
1637/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001638 * "assert_equalfile(fname-one, fname-two)" function
1639 */
1640 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001641f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001642{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001643 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001644}
1645
1646/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647 * "assert_notequal(expected, actual[, msg])" function
1648 */
1649 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001650f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001651{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001652 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001653}
1654
1655/*
1656 * "assert_exception(string[, msg])" function
1657 */
1658 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001659f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001660{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001661 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001662}
1663
1664/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001665 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001666 */
1667 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001668f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001669{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001670 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001671}
1672
1673/*
1674 * "assert_false(actual[, msg])" function
1675 */
1676 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001677f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001679 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001680}
1681
1682/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001683 * "assert_inrange(lower, upper[, msg])" function
1684 */
1685 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001686f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001687{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001688 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001689}
1690
1691/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692 * "assert_match(pattern, actual[, msg])" function
1693 */
1694 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001695f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001696{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001697 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001698}
1699
1700/*
1701 * "assert_notmatch(pattern, actual[, msg])" function
1702 */
1703 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001704f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001705{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001706 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001707}
1708
1709/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001710 * "assert_report(msg)" function
1711 */
1712 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001713f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001714{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001715 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001716}
1717
1718/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719 * "assert_true(actual[, msg])" function
1720 */
1721 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001722f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001723{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001724 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001725}
1726
1727#ifdef FEAT_FLOAT
1728/*
1729 * "asin()" function
1730 */
1731 static void
1732f_asin(typval_T *argvars, typval_T *rettv)
1733{
1734 float_T f = 0.0;
1735
1736 rettv->v_type = VAR_FLOAT;
1737 if (get_float_arg(argvars, &f) == OK)
1738 rettv->vval.v_float = asin(f);
1739 else
1740 rettv->vval.v_float = 0.0;
1741}
1742
1743/*
1744 * "atan()" function
1745 */
1746 static void
1747f_atan(typval_T *argvars, typval_T *rettv)
1748{
1749 float_T f = 0.0;
1750
1751 rettv->v_type = VAR_FLOAT;
1752 if (get_float_arg(argvars, &f) == OK)
1753 rettv->vval.v_float = atan(f);
1754 else
1755 rettv->vval.v_float = 0.0;
1756}
1757
1758/*
1759 * "atan2()" function
1760 */
1761 static void
1762f_atan2(typval_T *argvars, typval_T *rettv)
1763{
1764 float_T fx = 0.0, fy = 0.0;
1765
1766 rettv->v_type = VAR_FLOAT;
1767 if (get_float_arg(argvars, &fx) == OK
1768 && get_float_arg(&argvars[1], &fy) == OK)
1769 rettv->vval.v_float = atan2(fx, fy);
1770 else
1771 rettv->vval.v_float = 0.0;
1772}
1773#endif
1774
1775/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001776 * "balloon_show()" function
1777 */
1778#ifdef FEAT_BEVAL
1779 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001780f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1781{
1782 rettv->v_type = VAR_STRING;
1783 if (balloonEval != NULL)
1784 {
1785 if (balloonEval->msg == NULL)
1786 rettv->vval.v_string = NULL;
1787 else
1788 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1789 }
1790}
1791
1792 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001793f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1794{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001795 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001796 {
1797 if (argvars[0].v_type == VAR_LIST
1798# ifdef FEAT_GUI
1799 && !gui.in_use
1800# endif
1801 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001802 {
1803 list_T *l = argvars[0].vval.v_list;
1804
1805 // empty list removes the balloon
1806 post_balloon(balloonEval, NULL,
1807 l == NULL || l->lv_len == 0 ? NULL : l);
1808 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001809 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001810 {
1811 char_u *mesg = tv_get_string_chk(&argvars[0]);
1812
1813 if (mesg != NULL)
1814 // empty string removes the balloon
1815 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1816 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001817 }
1818}
1819
Bram Moolenaar669a8282017-11-19 20:13:05 +01001820# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001821 static void
1822f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1823{
1824 if (rettv_list_alloc(rettv) == OK)
1825 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001826 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827
1828 if (msg != NULL)
1829 {
1830 pumitem_T *array;
1831 int size = split_message(msg, &array);
1832 int i;
1833
1834 /* Skip the first and last item, they are always empty. */
1835 for (i = 1; i < size - 1; ++i)
1836 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001837 while (size > 0)
1838 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001839 vim_free(array);
1840 }
1841 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001842}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001843# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001844#endif
1845
1846/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001847 * "browse(save, title, initdir, default)" function
1848 */
1849 static void
1850f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1851{
1852#ifdef FEAT_BROWSE
1853 int save;
1854 char_u *title;
1855 char_u *initdir;
1856 char_u *defname;
1857 char_u buf[NUMBUFLEN];
1858 char_u buf2[NUMBUFLEN];
1859 int error = FALSE;
1860
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001861 save = (int)tv_get_number_chk(&argvars[0], &error);
1862 title = tv_get_string_chk(&argvars[1]);
1863 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1864 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001865
1866 if (error || title == NULL || initdir == NULL || defname == NULL)
1867 rettv->vval.v_string = NULL;
1868 else
1869 rettv->vval.v_string =
1870 do_browse(save ? BROWSE_SAVE : 0,
1871 title, defname, NULL, initdir, NULL, curbuf);
1872#else
1873 rettv->vval.v_string = NULL;
1874#endif
1875 rettv->v_type = VAR_STRING;
1876}
1877
1878/*
1879 * "browsedir(title, initdir)" function
1880 */
1881 static void
1882f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1883{
1884#ifdef FEAT_BROWSE
1885 char_u *title;
1886 char_u *initdir;
1887 char_u buf[NUMBUFLEN];
1888
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001889 title = tv_get_string_chk(&argvars[0]);
1890 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001891
1892 if (title == NULL || initdir == NULL)
1893 rettv->vval.v_string = NULL;
1894 else
1895 rettv->vval.v_string = do_browse(BROWSE_DIR,
1896 title, NULL, NULL, initdir, NULL, curbuf);
1897#else
1898 rettv->vval.v_string = NULL;
1899#endif
1900 rettv->v_type = VAR_STRING;
1901}
1902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903/*
1904 * Find a buffer by number or exact name.
1905 */
1906 static buf_T *
1907find_buffer(typval_T *avar)
1908{
1909 buf_T *buf = NULL;
1910
1911 if (avar->v_type == VAR_NUMBER)
1912 buf = buflist_findnr((int)avar->vval.v_number);
1913 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1914 {
1915 buf = buflist_findname_exp(avar->vval.v_string);
1916 if (buf == NULL)
1917 {
1918 /* No full path name match, try a match with a URL or a "nofile"
1919 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001920 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001921 if (buf->b_fname != NULL
1922 && (path_with_url(buf->b_fname)
1923#ifdef FEAT_QUICKFIX
1924 || bt_nofile(buf)
1925#endif
1926 )
1927 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1928 break;
1929 }
1930 }
1931 return buf;
1932}
1933
1934/*
1935 * "bufexists(expr)" function
1936 */
1937 static void
1938f_bufexists(typval_T *argvars, typval_T *rettv)
1939{
1940 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1941}
1942
1943/*
1944 * "buflisted(expr)" function
1945 */
1946 static void
1947f_buflisted(typval_T *argvars, typval_T *rettv)
1948{
1949 buf_T *buf;
1950
1951 buf = find_buffer(&argvars[0]);
1952 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1953}
1954
1955/*
1956 * "bufloaded(expr)" function
1957 */
1958 static void
1959f_bufloaded(typval_T *argvars, typval_T *rettv)
1960{
1961 buf_T *buf;
1962
1963 buf = find_buffer(&argvars[0]);
1964 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1965}
1966
1967 buf_T *
1968buflist_find_by_name(char_u *name, int curtab_only)
1969{
1970 int save_magic;
1971 char_u *save_cpo;
1972 buf_T *buf;
1973
1974 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1975 save_magic = p_magic;
1976 p_magic = TRUE;
1977 save_cpo = p_cpo;
1978 p_cpo = (char_u *)"";
1979
1980 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1981 TRUE, FALSE, curtab_only));
1982
1983 p_magic = save_magic;
1984 p_cpo = save_cpo;
1985 return buf;
1986}
1987
1988/*
1989 * Get buffer by number or pattern.
1990 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001991 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001992tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001993{
1994 char_u *name = tv->vval.v_string;
1995 buf_T *buf;
1996
1997 if (tv->v_type == VAR_NUMBER)
1998 return buflist_findnr((int)tv->vval.v_number);
1999 if (tv->v_type != VAR_STRING)
2000 return NULL;
2001 if (name == NULL || *name == NUL)
2002 return curbuf;
2003 if (name[0] == '$' && name[1] == NUL)
2004 return lastbuf;
2005
2006 buf = buflist_find_by_name(name, curtab_only);
2007
2008 /* If not found, try expanding the name, like done for bufexists(). */
2009 if (buf == NULL)
2010 buf = find_buffer(tv);
2011
2012 return buf;
2013}
2014
2015/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002016 * Get the buffer from "arg" and give an error and return NULL if it is not
2017 * valid.
2018 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002019 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002020get_buf_arg(typval_T *arg)
2021{
2022 buf_T *buf;
2023
2024 ++emsg_off;
2025 buf = tv_get_buf(arg, FALSE);
2026 --emsg_off;
2027 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002029 return buf;
2030}
2031
2032/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002033 * "bufname(expr)" function
2034 */
2035 static void
2036f_bufname(typval_T *argvars, typval_T *rettv)
2037{
2038 buf_T *buf;
2039
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002040 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002041 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002042 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 rettv->v_type = VAR_STRING;
2044 if (buf != NULL && buf->b_fname != NULL)
2045 rettv->vval.v_string = vim_strsave(buf->b_fname);
2046 else
2047 rettv->vval.v_string = NULL;
2048 --emsg_off;
2049}
2050
2051/*
2052 * "bufnr(expr)" function
2053 */
2054 static void
2055f_bufnr(typval_T *argvars, typval_T *rettv)
2056{
2057 buf_T *buf;
2058 int error = FALSE;
2059 char_u *name;
2060
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002061 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002062 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002063 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002064 --emsg_off;
2065
2066 /* If the buffer isn't found and the second argument is not zero create a
2067 * new buffer. */
2068 if (buf == NULL
2069 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002070 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002071 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002072 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002073 && !error)
2074 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2075
2076 if (buf != NULL)
2077 rettv->vval.v_number = buf->b_fnum;
2078 else
2079 rettv->vval.v_number = -1;
2080}
2081
2082 static void
2083buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2084{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 win_T *wp;
2086 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002087 buf_T *buf;
2088
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002089 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002090 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002091 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002092 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002093 {
2094 ++winnr;
2095 if (wp->w_buffer == buf)
2096 break;
2097 }
2098 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 --emsg_off;
2100}
2101
2102/*
2103 * "bufwinid(nr)" function
2104 */
2105 static void
2106f_bufwinid(typval_T *argvars, typval_T *rettv)
2107{
2108 buf_win_common(argvars, rettv, FALSE);
2109}
2110
2111/*
2112 * "bufwinnr(nr)" function
2113 */
2114 static void
2115f_bufwinnr(typval_T *argvars, typval_T *rettv)
2116{
2117 buf_win_common(argvars, rettv, TRUE);
2118}
2119
2120/*
2121 * "byte2line(byte)" function
2122 */
2123 static void
2124f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2125{
2126#ifndef FEAT_BYTEOFF
2127 rettv->vval.v_number = -1;
2128#else
2129 long boff = 0;
2130
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002131 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002132 if (boff < 0)
2133 rettv->vval.v_number = -1;
2134 else
2135 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2136 (linenr_T)0, &boff);
2137#endif
2138}
2139
2140 static void
2141byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2142{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002143 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144 char_u *str;
2145 varnumber_T idx;
2146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002147 str = tv_get_string_chk(&argvars[0]);
2148 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002149 rettv->vval.v_number = -1;
2150 if (str == NULL || idx < 0)
2151 return;
2152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 t = str;
2154 for ( ; idx > 0; idx--)
2155 {
2156 if (*t == NUL) /* EOL reached */
2157 return;
2158 if (enc_utf8 && comp)
2159 t += utf_ptr2len(t);
2160 else
2161 t += (*mb_ptr2len)(t);
2162 }
2163 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002164}
2165
2166/*
2167 * "byteidx()" function
2168 */
2169 static void
2170f_byteidx(typval_T *argvars, typval_T *rettv)
2171{
2172 byteidx(argvars, rettv, FALSE);
2173}
2174
2175/*
2176 * "byteidxcomp()" function
2177 */
2178 static void
2179f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2180{
2181 byteidx(argvars, rettv, TRUE);
2182}
2183
2184/*
2185 * "call(func, arglist [, dict])" function
2186 */
2187 static void
2188f_call(typval_T *argvars, typval_T *rettv)
2189{
2190 char_u *func;
2191 partial_T *partial = NULL;
2192 dict_T *selfdict = NULL;
2193
2194 if (argvars[1].v_type != VAR_LIST)
2195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002197 return;
2198 }
2199 if (argvars[1].vval.v_list == NULL)
2200 return;
2201
2202 if (argvars[0].v_type == VAR_FUNC)
2203 func = argvars[0].vval.v_string;
2204 else if (argvars[0].v_type == VAR_PARTIAL)
2205 {
2206 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002207 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002208 }
2209 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002210 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002211 if (*func == NUL)
2212 return; /* type error or empty name */
2213
2214 if (argvars[2].v_type != VAR_UNKNOWN)
2215 {
2216 if (argvars[2].v_type != VAR_DICT)
2217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002219 return;
2220 }
2221 selfdict = argvars[2].vval.v_dict;
2222 }
2223
2224 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2225}
2226
2227#ifdef FEAT_FLOAT
2228/*
2229 * "ceil({float})" function
2230 */
2231 static void
2232f_ceil(typval_T *argvars, typval_T *rettv)
2233{
2234 float_T f = 0.0;
2235
2236 rettv->v_type = VAR_FLOAT;
2237 if (get_float_arg(argvars, &f) == OK)
2238 rettv->vval.v_float = ceil(f);
2239 else
2240 rettv->vval.v_float = 0.0;
2241}
2242#endif
2243
2244#ifdef FEAT_JOB_CHANNEL
2245/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002246 * "ch_canread()" function
2247 */
2248 static void
2249f_ch_canread(typval_T *argvars, typval_T *rettv)
2250{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002251 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002252
2253 rettv->vval.v_number = 0;
2254 if (channel != NULL)
2255 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2256 || channel_has_readahead(channel, PART_OUT)
2257 || channel_has_readahead(channel, PART_ERR);
2258}
2259
2260/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002261 * "ch_close()" function
2262 */
2263 static void
2264f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2265{
2266 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2267
2268 if (channel != NULL)
2269 {
2270 channel_close(channel, FALSE);
2271 channel_clear(channel);
2272 }
2273}
2274
2275/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002276 * "ch_close()" function
2277 */
2278 static void
2279f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2280{
2281 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2282
2283 if (channel != NULL)
2284 channel_close_in(channel);
2285}
2286
2287/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002288 * "ch_getbufnr()" function
2289 */
2290 static void
2291f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2292{
2293 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2294
2295 rettv->vval.v_number = -1;
2296 if (channel != NULL)
2297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002298 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002299 int part;
2300
2301 if (STRCMP(what, "err") == 0)
2302 part = PART_ERR;
2303 else if (STRCMP(what, "out") == 0)
2304 part = PART_OUT;
2305 else if (STRCMP(what, "in") == 0)
2306 part = PART_IN;
2307 else
2308 part = PART_SOCK;
2309 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2310 rettv->vval.v_number =
2311 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2312 }
2313}
2314
2315/*
2316 * "ch_getjob()" function
2317 */
2318 static void
2319f_ch_getjob(typval_T *argvars, typval_T *rettv)
2320{
2321 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2322
2323 if (channel != NULL)
2324 {
2325 rettv->v_type = VAR_JOB;
2326 rettv->vval.v_job = channel->ch_job;
2327 if (channel->ch_job != NULL)
2328 ++channel->ch_job->jv_refcount;
2329 }
2330}
2331
2332/*
2333 * "ch_info()" function
2334 */
2335 static void
2336f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2337{
2338 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2339
2340 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2341 channel_info(channel, rettv->vval.v_dict);
2342}
2343
2344/*
2345 * "ch_log()" function
2346 */
2347 static void
2348f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2349{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002350 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002351 channel_T *channel = NULL;
2352
2353 if (argvars[1].v_type != VAR_UNKNOWN)
2354 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2355
Bram Moolenaard5359b22018-04-05 22:44:39 +02002356 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002357}
2358
2359/*
2360 * "ch_logfile()" function
2361 */
2362 static void
2363f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2364{
2365 char_u *fname;
2366 char_u *opt = (char_u *)"";
2367 char_u buf[NUMBUFLEN];
2368
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002369 /* Don't open a file in restricted mode. */
2370 if (check_restricted() || check_secure())
2371 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002372 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002373 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002374 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002375 ch_logfile(fname, opt);
2376}
2377
2378/*
2379 * "ch_open()" function
2380 */
2381 static void
2382f_ch_open(typval_T *argvars, typval_T *rettv)
2383{
2384 rettv->v_type = VAR_CHANNEL;
2385 if (check_restricted() || check_secure())
2386 return;
2387 rettv->vval.v_channel = channel_open_func(argvars);
2388}
2389
2390/*
2391 * "ch_read()" function
2392 */
2393 static void
2394f_ch_read(typval_T *argvars, typval_T *rettv)
2395{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002396 common_channel_read(argvars, rettv, FALSE, FALSE);
2397}
2398
2399/*
2400 * "ch_readblob()" function
2401 */
2402 static void
2403f_ch_readblob(typval_T *argvars, typval_T *rettv)
2404{
2405 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002406}
2407
2408/*
2409 * "ch_readraw()" function
2410 */
2411 static void
2412f_ch_readraw(typval_T *argvars, typval_T *rettv)
2413{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002414 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002415}
2416
2417/*
2418 * "ch_evalexpr()" function
2419 */
2420 static void
2421f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2422{
2423 ch_expr_common(argvars, rettv, TRUE);
2424}
2425
2426/*
2427 * "ch_sendexpr()" function
2428 */
2429 static void
2430f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2431{
2432 ch_expr_common(argvars, rettv, FALSE);
2433}
2434
2435/*
2436 * "ch_evalraw()" function
2437 */
2438 static void
2439f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2440{
2441 ch_raw_common(argvars, rettv, TRUE);
2442}
2443
2444/*
2445 * "ch_sendraw()" function
2446 */
2447 static void
2448f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2449{
2450 ch_raw_common(argvars, rettv, FALSE);
2451}
2452
2453/*
2454 * "ch_setoptions()" function
2455 */
2456 static void
2457f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2458{
2459 channel_T *channel;
2460 jobopt_T opt;
2461
2462 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2463 if (channel == NULL)
2464 return;
2465 clear_job_options(&opt);
2466 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002467 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002468 channel_set_options(channel, &opt);
2469 free_job_options(&opt);
2470}
2471
2472/*
2473 * "ch_status()" function
2474 */
2475 static void
2476f_ch_status(typval_T *argvars, typval_T *rettv)
2477{
2478 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002479 jobopt_T opt;
2480 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002481
2482 /* return an empty string by default */
2483 rettv->v_type = VAR_STRING;
2484 rettv->vval.v_string = NULL;
2485
2486 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002487
2488 if (argvars[1].v_type != VAR_UNKNOWN)
2489 {
2490 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002491 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002492 && (opt.jo_set & JO_PART))
2493 part = opt.jo_part;
2494 }
2495
2496 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002497}
2498#endif
2499
2500/*
2501 * "changenr()" function
2502 */
2503 static void
2504f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2505{
2506 rettv->vval.v_number = curbuf->b_u_seq_cur;
2507}
2508
2509/*
2510 * "char2nr(string)" function
2511 */
2512 static void
2513f_char2nr(typval_T *argvars, typval_T *rettv)
2514{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002515 if (has_mbyte)
2516 {
2517 int utf8 = 0;
2518
2519 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002520 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002521
2522 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002523 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002524 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002525 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002526 }
2527 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002528 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002529}
2530
2531/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002532 * "chdir(dir)" function
2533 */
2534 static void
2535f_chdir(typval_T *argvars, typval_T *rettv)
2536{
2537 char_u *cwd;
2538 cdscope_T scope = CDSCOPE_GLOBAL;
2539
2540 rettv->v_type = VAR_STRING;
2541 rettv->vval.v_string = NULL;
2542
2543 if (argvars[0].v_type != VAR_STRING)
2544 return;
2545
2546 // Return the current directory
2547 cwd = alloc(MAXPATHL);
2548 if (cwd != NULL)
2549 {
2550 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2551 {
2552#ifdef BACKSLASH_IN_FILENAME
2553 slash_adjust(cwd);
2554#endif
2555 rettv->vval.v_string = vim_strsave(cwd);
2556 }
2557 vim_free(cwd);
2558 }
2559
2560 if (curwin->w_localdir != NULL)
2561 scope = CDSCOPE_WINDOW;
2562 else if (curtab->tp_localdir != NULL)
2563 scope = CDSCOPE_TABPAGE;
2564
2565 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2566 // Directory change failed
2567 VIM_CLEAR(rettv->vval.v_string);
2568}
2569
2570/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002571 * "cindent(lnum)" function
2572 */
2573 static void
2574f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2575{
2576#ifdef FEAT_CINDENT
2577 pos_T pos;
2578 linenr_T lnum;
2579
2580 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002581 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002582 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2583 {
2584 curwin->w_cursor.lnum = lnum;
2585 rettv->vval.v_number = get_c_indent();
2586 curwin->w_cursor = pos;
2587 }
2588 else
2589#endif
2590 rettv->vval.v_number = -1;
2591}
2592
Bram Moolenaaraff74912019-03-30 18:11:49 +01002593 static win_T *
2594get_optional_window(typval_T *argvars, int idx)
2595{
2596 win_T *win = curwin;
2597
2598 if (argvars[idx].v_type != VAR_UNKNOWN)
2599 {
2600 win = find_win_by_nr_or_id(&argvars[idx]);
2601 if (win == NULL)
2602 {
2603 emsg(_(e_invalwindow));
2604 return NULL;
2605 }
2606 }
2607 return win;
2608}
2609
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002610/*
2611 * "clearmatches()" function
2612 */
2613 static void
2614f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2615{
2616#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002617 win_T *win = get_optional_window(argvars, 0);
2618
2619 if (win != NULL)
2620 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002621#endif
2622}
2623
2624/*
2625 * "col(string)" function
2626 */
2627 static void
2628f_col(typval_T *argvars, typval_T *rettv)
2629{
2630 colnr_T col = 0;
2631 pos_T *fp;
2632 int fnum = curbuf->b_fnum;
2633
2634 fp = var2fpos(&argvars[0], FALSE, &fnum);
2635 if (fp != NULL && fnum == curbuf->b_fnum)
2636 {
2637 if (fp->col == MAXCOL)
2638 {
2639 /* '> can be MAXCOL, get the length of the line then */
2640 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2641 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2642 else
2643 col = MAXCOL;
2644 }
2645 else
2646 {
2647 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002648 /* col(".") when the cursor is on the NUL at the end of the line
2649 * because of "coladd" can be seen as an extra column. */
2650 if (virtual_active() && fp == &curwin->w_cursor)
2651 {
2652 char_u *p = ml_get_cursor();
2653
2654 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2655 curwin->w_virtcol - curwin->w_cursor.coladd))
2656 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002657 int l;
2658
2659 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2660 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002661 }
2662 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002663 }
2664 }
2665 rettv->vval.v_number = col;
2666}
2667
2668#if defined(FEAT_INS_EXPAND)
2669/*
2670 * "complete()" function
2671 */
2672 static void
2673f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2674{
2675 int startcol;
2676
2677 if ((State & INSERT) == 0)
2678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002679 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002680 return;
2681 }
2682
2683 /* Check for undo allowed here, because if something was already inserted
2684 * the line was already saved for undo and this check isn't done. */
2685 if (!undo_allowed())
2686 return;
2687
2688 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002690 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002691 return;
2692 }
2693
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002694 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002695 if (startcol <= 0)
2696 return;
2697
2698 set_completion(startcol - 1, argvars[1].vval.v_list);
2699}
2700
2701/*
2702 * "complete_add()" function
2703 */
2704 static void
2705f_complete_add(typval_T *argvars, typval_T *rettv)
2706{
2707 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2708}
2709
2710/*
2711 * "complete_check()" function
2712 */
2713 static void
2714f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2715{
2716 int saved = RedrawingDisabled;
2717
2718 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002719 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002720 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002721 RedrawingDisabled = saved;
2722}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002723
2724/*
2725 * "complete_info()" function
2726 */
2727 static void
2728f_complete_info(typval_T *argvars, typval_T *rettv)
2729{
2730 list_T *what_list = NULL;
2731
2732 if (rettv_dict_alloc(rettv) != OK)
2733 return;
2734
2735 if (argvars[0].v_type != VAR_UNKNOWN)
2736 {
2737 if (argvars[0].v_type != VAR_LIST)
2738 {
2739 emsg(_(e_listreq));
2740 return;
2741 }
2742 what_list = argvars[0].vval.v_list;
2743 }
2744 get_complete_info(what_list, rettv->vval.v_dict);
2745}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002746#endif
2747
2748/*
2749 * "confirm(message, buttons[, default [, type]])" function
2750 */
2751 static void
2752f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2753{
2754#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2755 char_u *message;
2756 char_u *buttons = NULL;
2757 char_u buf[NUMBUFLEN];
2758 char_u buf2[NUMBUFLEN];
2759 int def = 1;
2760 int type = VIM_GENERIC;
2761 char_u *typestr;
2762 int error = FALSE;
2763
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002764 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002765 if (message == NULL)
2766 error = TRUE;
2767 if (argvars[1].v_type != VAR_UNKNOWN)
2768 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002769 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002770 if (buttons == NULL)
2771 error = TRUE;
2772 if (argvars[2].v_type != VAR_UNKNOWN)
2773 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 if (argvars[3].v_type != VAR_UNKNOWN)
2776 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002777 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002778 if (typestr == NULL)
2779 error = TRUE;
2780 else
2781 {
2782 switch (TOUPPER_ASC(*typestr))
2783 {
2784 case 'E': type = VIM_ERROR; break;
2785 case 'Q': type = VIM_QUESTION; break;
2786 case 'I': type = VIM_INFO; break;
2787 case 'W': type = VIM_WARNING; break;
2788 case 'G': type = VIM_GENERIC; break;
2789 }
2790 }
2791 }
2792 }
2793 }
2794
2795 if (buttons == NULL || *buttons == NUL)
2796 buttons = (char_u *)_("&Ok");
2797
2798 if (!error)
2799 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2800 def, NULL, FALSE);
2801#endif
2802}
2803
2804/*
2805 * "copy()" function
2806 */
2807 static void
2808f_copy(typval_T *argvars, typval_T *rettv)
2809{
2810 item_copy(&argvars[0], rettv, FALSE, 0);
2811}
2812
2813#ifdef FEAT_FLOAT
2814/*
2815 * "cos()" function
2816 */
2817 static void
2818f_cos(typval_T *argvars, typval_T *rettv)
2819{
2820 float_T f = 0.0;
2821
2822 rettv->v_type = VAR_FLOAT;
2823 if (get_float_arg(argvars, &f) == OK)
2824 rettv->vval.v_float = cos(f);
2825 else
2826 rettv->vval.v_float = 0.0;
2827}
2828
2829/*
2830 * "cosh()" function
2831 */
2832 static void
2833f_cosh(typval_T *argvars, typval_T *rettv)
2834{
2835 float_T f = 0.0;
2836
2837 rettv->v_type = VAR_FLOAT;
2838 if (get_float_arg(argvars, &f) == OK)
2839 rettv->vval.v_float = cosh(f);
2840 else
2841 rettv->vval.v_float = 0.0;
2842}
2843#endif
2844
2845/*
2846 * "count()" function
2847 */
2848 static void
2849f_count(typval_T *argvars, typval_T *rettv)
2850{
2851 long n = 0;
2852 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002853 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002854
Bram Moolenaar9966b212017-07-28 16:46:57 +02002855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002856 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002857
2858 if (argvars[0].v_type == VAR_STRING)
2859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002860 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002861 char_u *p = argvars[0].vval.v_string;
2862 char_u *next;
2863
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002864 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865 {
2866 if (ic)
2867 {
2868 size_t len = STRLEN(expr);
2869
2870 while (*p != NUL)
2871 {
2872 if (MB_STRNICMP(p, expr, len) == 0)
2873 {
2874 ++n;
2875 p += len;
2876 }
2877 else
2878 MB_PTR_ADV(p);
2879 }
2880 }
2881 else
2882 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2883 != NULL)
2884 {
2885 ++n;
2886 p = next + STRLEN(expr);
2887 }
2888 }
2889
2890 }
2891 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002892 {
2893 listitem_T *li;
2894 list_T *l;
2895 long idx;
2896
2897 if ((l = argvars[0].vval.v_list) != NULL)
2898 {
2899 li = l->lv_first;
2900 if (argvars[2].v_type != VAR_UNKNOWN)
2901 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 if (argvars[3].v_type != VAR_UNKNOWN)
2903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002904 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002905 if (!error)
2906 {
2907 li = list_find(l, idx);
2908 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002910 }
2911 }
2912 if (error)
2913 li = NULL;
2914 }
2915
2916 for ( ; li != NULL; li = li->li_next)
2917 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2918 ++n;
2919 }
2920 }
2921 else if (argvars[0].v_type == VAR_DICT)
2922 {
2923 int todo;
2924 dict_T *d;
2925 hashitem_T *hi;
2926
2927 if ((d = argvars[0].vval.v_dict) != NULL)
2928 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002929 if (argvars[2].v_type != VAR_UNKNOWN)
2930 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002931 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002932 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002933 }
2934
2935 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2936 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2937 {
2938 if (!HASHITEM_EMPTY(hi))
2939 {
2940 --todo;
2941 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2942 ++n;
2943 }
2944 }
2945 }
2946 }
2947 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002948 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002949 rettv->vval.v_number = n;
2950}
2951
2952/*
2953 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2954 *
2955 * Checks the existence of a cscope connection.
2956 */
2957 static void
2958f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2959{
2960#ifdef FEAT_CSCOPE
2961 int num = 0;
2962 char_u *dbpath = NULL;
2963 char_u *prepend = NULL;
2964 char_u buf[NUMBUFLEN];
2965
2966 if (argvars[0].v_type != VAR_UNKNOWN
2967 && argvars[1].v_type != VAR_UNKNOWN)
2968 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002969 num = (int)tv_get_number(&argvars[0]);
2970 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002971 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002972 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002973 }
2974
2975 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2976#endif
2977}
2978
2979/*
2980 * "cursor(lnum, col)" function, or
2981 * "cursor(list)"
2982 *
2983 * Moves the cursor to the specified line and column.
2984 * Returns 0 when the position could be set, -1 otherwise.
2985 */
2986 static void
2987f_cursor(typval_T *argvars, typval_T *rettv)
2988{
2989 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002990 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002991 int set_curswant = TRUE;
2992
2993 rettv->vval.v_number = -1;
2994 if (argvars[1].v_type == VAR_UNKNOWN)
2995 {
2996 pos_T pos;
2997 colnr_T curswant = -1;
2998
2999 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003001 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 return;
3003 }
3004 line = pos.lnum;
3005 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003006 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003007 if (curswant >= 0)
3008 {
3009 curwin->w_curswant = curswant - 1;
3010 set_curswant = FALSE;
3011 }
3012 }
3013 else
3014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003015 line = tv_get_lnum(argvars);
3016 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003018 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003020 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003021 return; /* type error; errmsg already given */
3022 if (line > 0)
3023 curwin->w_cursor.lnum = line;
3024 if (col > 0)
3025 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003026 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027
3028 /* Make sure the cursor is in a valid position. */
3029 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003030 /* Correct cursor for multi-byte character. */
3031 if (has_mbyte)
3032 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033
3034 curwin->w_set_curswant = set_curswant;
3035 rettv->vval.v_number = 0;
3036}
3037
Bram Moolenaar4f974752019-02-17 17:44:42 +01003038#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003039/*
3040 * "debugbreak()" function
3041 */
3042 static void
3043f_debugbreak(typval_T *argvars, typval_T *rettv)
3044{
3045 int pid;
3046
3047 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003048 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003049 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003050 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003051 else
3052 {
3053 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3054
3055 if (hProcess != NULL)
3056 {
3057 DebugBreakProcess(hProcess);
3058 CloseHandle(hProcess);
3059 rettv->vval.v_number = OK;
3060 }
3061 }
3062}
3063#endif
3064
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003065/*
3066 * "deepcopy()" function
3067 */
3068 static void
3069f_deepcopy(typval_T *argvars, typval_T *rettv)
3070{
3071 int noref = 0;
3072 int copyID;
3073
3074 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003075 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003076 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003077 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003078 else
3079 {
3080 copyID = get_copyID();
3081 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3082 }
3083}
3084
3085/*
3086 * "delete()" function
3087 */
3088 static void
3089f_delete(typval_T *argvars, typval_T *rettv)
3090{
3091 char_u nbuf[NUMBUFLEN];
3092 char_u *name;
3093 char_u *flags;
3094
3095 rettv->vval.v_number = -1;
3096 if (check_restricted() || check_secure())
3097 return;
3098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003099 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003100 if (name == NULL || *name == NUL)
3101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003102 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003103 return;
3104 }
3105
3106 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003107 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003108 else
3109 flags = (char_u *)"";
3110
3111 if (*flags == NUL)
3112 /* delete a file */
3113 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3114 else if (STRCMP(flags, "d") == 0)
3115 /* delete an empty directory */
3116 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3117 else if (STRCMP(flags, "rf") == 0)
3118 /* delete a directory recursively */
3119 rettv->vval.v_number = delete_recursive(name);
3120 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003121 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003122}
3123
3124/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003125 * "deletebufline()" function
3126 */
3127 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003128f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003129{
3130 buf_T *buf;
3131 linenr_T first, last;
3132 linenr_T lnum;
3133 long count;
3134 int is_curbuf;
3135 buf_T *curbuf_save = NULL;
3136 win_T *curwin_save = NULL;
3137 tabpage_T *tp;
3138 win_T *wp;
3139
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003140 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003141 if (buf == NULL)
3142 {
3143 rettv->vval.v_number = 1; /* FAIL */
3144 return;
3145 }
3146 is_curbuf = buf == curbuf;
3147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003148 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003149 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003150 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003151 else
3152 last = first;
3153
3154 if (buf->b_ml.ml_mfp == NULL || first < 1
3155 || first > buf->b_ml.ml_line_count || last < first)
3156 {
3157 rettv->vval.v_number = 1; /* FAIL */
3158 return;
3159 }
3160
3161 if (!is_curbuf)
3162 {
3163 curbuf_save = curbuf;
3164 curwin_save = curwin;
3165 curbuf = buf;
3166 find_win_for_curbuf();
3167 }
3168 if (last > curbuf->b_ml.ml_line_count)
3169 last = curbuf->b_ml.ml_line_count;
3170 count = last - first + 1;
3171
3172 // When coming here from Insert mode, sync undo, so that this can be
3173 // undone separately from what was previously inserted.
3174 if (u_sync_once == 2)
3175 {
3176 u_sync_once = 1; // notify that u_sync() was called
3177 u_sync(TRUE);
3178 }
3179
3180 if (u_save(first - 1, last + 1) == FAIL)
3181 {
3182 rettv->vval.v_number = 1; /* FAIL */
3183 return;
3184 }
3185
3186 for (lnum = first; lnum <= last; ++lnum)
3187 ml_delete(first, TRUE);
3188
3189 FOR_ALL_TAB_WINDOWS(tp, wp)
3190 if (wp->w_buffer == buf)
3191 {
3192 if (wp->w_cursor.lnum > last)
3193 wp->w_cursor.lnum -= count;
3194 else if (wp->w_cursor.lnum> first)
3195 wp->w_cursor.lnum = first;
3196 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3197 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3198 }
3199 check_cursor_col();
3200 deleted_lines_mark(first, count);
3201
3202 if (!is_curbuf)
3203 {
3204 curbuf = curbuf_save;
3205 curwin = curwin_save;
3206 }
3207}
3208
3209/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003210 * "did_filetype()" function
3211 */
3212 static void
3213f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3214{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003215 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003216}
3217
3218/*
3219 * "diff_filler()" function
3220 */
3221 static void
3222f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3223{
3224#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003225 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226#endif
3227}
3228
3229/*
3230 * "diff_hlID()" function
3231 */
3232 static void
3233f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3234{
3235#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003236 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003237 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003238 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003239 static int fnum = 0;
3240 static int change_start = 0;
3241 static int change_end = 0;
3242 static hlf_T hlID = (hlf_T)0;
3243 int filler_lines;
3244 int col;
3245
3246 if (lnum < 0) /* ignore type error in {lnum} arg */
3247 lnum = 0;
3248 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003249 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003250 || fnum != curbuf->b_fnum)
3251 {
3252 /* New line, buffer, change: need to get the values. */
3253 filler_lines = diff_check(curwin, lnum);
3254 if (filler_lines < 0)
3255 {
3256 if (filler_lines == -1)
3257 {
3258 change_start = MAXCOL;
3259 change_end = -1;
3260 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3261 hlID = HLF_ADD; /* added line */
3262 else
3263 hlID = HLF_CHD; /* changed line */
3264 }
3265 else
3266 hlID = HLF_ADD; /* added line */
3267 }
3268 else
3269 hlID = (hlf_T)0;
3270 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003271 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003272 fnum = curbuf->b_fnum;
3273 }
3274
3275 if (hlID == HLF_CHD || hlID == HLF_TXD)
3276 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003277 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003278 if (col >= change_start && col <= change_end)
3279 hlID = HLF_TXD; /* changed text */
3280 else
3281 hlID = HLF_CHD; /* changed line */
3282 }
3283 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3284#endif
3285}
3286
3287/*
3288 * "empty({expr})" function
3289 */
3290 static void
3291f_empty(typval_T *argvars, typval_T *rettv)
3292{
3293 int n = FALSE;
3294
3295 switch (argvars[0].v_type)
3296 {
3297 case VAR_STRING:
3298 case VAR_FUNC:
3299 n = argvars[0].vval.v_string == NULL
3300 || *argvars[0].vval.v_string == NUL;
3301 break;
3302 case VAR_PARTIAL:
3303 n = FALSE;
3304 break;
3305 case VAR_NUMBER:
3306 n = argvars[0].vval.v_number == 0;
3307 break;
3308 case VAR_FLOAT:
3309#ifdef FEAT_FLOAT
3310 n = argvars[0].vval.v_float == 0.0;
3311 break;
3312#endif
3313 case VAR_LIST:
3314 n = argvars[0].vval.v_list == NULL
3315 || argvars[0].vval.v_list->lv_first == NULL;
3316 break;
3317 case VAR_DICT:
3318 n = argvars[0].vval.v_dict == NULL
3319 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3320 break;
3321 case VAR_SPECIAL:
3322 n = argvars[0].vval.v_number != VVAL_TRUE;
3323 break;
3324
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003325 case VAR_BLOB:
3326 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003327 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3328 break;
3329
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003330 case VAR_JOB:
3331#ifdef FEAT_JOB_CHANNEL
3332 n = argvars[0].vval.v_job == NULL
3333 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3334 break;
3335#endif
3336 case VAR_CHANNEL:
3337#ifdef FEAT_JOB_CHANNEL
3338 n = argvars[0].vval.v_channel == NULL
3339 || !channel_is_open(argvars[0].vval.v_channel);
3340 break;
3341#endif
3342 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003343 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003344 n = TRUE;
3345 break;
3346 }
3347
3348 rettv->vval.v_number = n;
3349}
3350
3351/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003352 * "environ()" function
3353 */
3354 static void
3355f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3356{
3357#if !defined(AMIGA)
3358 int i = 0;
3359 char_u *entry, *value;
3360# ifdef MSWIN
3361 extern wchar_t **_wenviron;
3362# else
3363 extern char **environ;
3364# endif
3365
3366 if (rettv_dict_alloc(rettv) != OK)
3367 return;
3368
3369# ifdef MSWIN
3370 if (*_wenviron == NULL)
3371 return;
3372# else
3373 if (*environ == NULL)
3374 return;
3375# endif
3376
3377 for (i = 0; ; ++i)
3378 {
3379# ifdef MSWIN
3380 short_u *p;
3381
3382 if ((p = (short_u *)_wenviron[i]) == NULL)
3383 return;
3384 entry = utf16_to_enc(p, NULL);
3385# else
3386 if ((entry = (char_u *)environ[i]) == NULL)
3387 return;
3388 entry = vim_strsave(entry);
3389# endif
3390 if (entry == NULL) // out of memory
3391 return;
3392 if ((value = vim_strchr(entry, '=')) == NULL)
3393 {
3394 vim_free(entry);
3395 continue;
3396 }
3397 *value++ = NUL;
3398 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3399 vim_free(entry);
3400 }
3401#endif
3402}
3403
3404/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003405 * "escape({string}, {chars})" function
3406 */
3407 static void
3408f_escape(typval_T *argvars, typval_T *rettv)
3409{
3410 char_u buf[NUMBUFLEN];
3411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003412 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3413 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003414 rettv->v_type = VAR_STRING;
3415}
3416
3417/*
3418 * "eval()" function
3419 */
3420 static void
3421f_eval(typval_T *argvars, typval_T *rettv)
3422{
3423 char_u *s, *p;
3424
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003425 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 if (s != NULL)
3427 s = skipwhite(s);
3428
3429 p = s;
3430 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3431 {
3432 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003433 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003434 need_clr_eos = FALSE;
3435 rettv->v_type = VAR_NUMBER;
3436 rettv->vval.v_number = 0;
3437 }
3438 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003439 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003440}
3441
3442/*
3443 * "eventhandler()" function
3444 */
3445 static void
3446f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3447{
3448 rettv->vval.v_number = vgetc_busy;
3449}
3450
3451/*
3452 * "executable()" function
3453 */
3454 static void
3455f_executable(typval_T *argvars, typval_T *rettv)
3456{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003457 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003458
3459 /* Check in $PATH and also check directly if there is a directory name. */
3460 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3461 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3462}
3463
3464static garray_T redir_execute_ga;
3465
3466/*
3467 * Append "value[value_len]" to the execute() output.
3468 */
3469 void
3470execute_redir_str(char_u *value, int value_len)
3471{
3472 int len;
3473
3474 if (value_len == -1)
3475 len = (int)STRLEN(value); /* Append the entire string */
3476 else
3477 len = value_len; /* Append only "value_len" characters */
3478 if (ga_grow(&redir_execute_ga, len) == OK)
3479 {
3480 mch_memmove((char *)redir_execute_ga.ga_data
3481 + redir_execute_ga.ga_len, value, len);
3482 redir_execute_ga.ga_len += len;
3483 }
3484}
3485
3486/*
3487 * Get next line from a list.
3488 * Called by do_cmdline() to get the next line.
3489 * Returns allocated string, or NULL for end of function.
3490 */
3491
3492 static char_u *
3493get_list_line(
3494 int c UNUSED,
3495 void *cookie,
3496 int indent UNUSED)
3497{
3498 listitem_T **p = (listitem_T **)cookie;
3499 listitem_T *item = *p;
3500 char_u buf[NUMBUFLEN];
3501 char_u *s;
3502
3503 if (item == NULL)
3504 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003505 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003506 *p = item->li_next;
3507 return s == NULL ? NULL : vim_strsave(s);
3508}
3509
3510/*
3511 * "execute()" function
3512 */
3513 static void
3514f_execute(typval_T *argvars, typval_T *rettv)
3515{
3516 char_u *cmd = NULL;
3517 list_T *list = NULL;
3518 int save_msg_silent = msg_silent;
3519 int save_emsg_silent = emsg_silent;
3520 int save_emsg_noredir = emsg_noredir;
3521 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003522 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003523 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003524 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003525 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003526
3527 rettv->vval.v_string = NULL;
3528 rettv->v_type = VAR_STRING;
3529
3530 if (argvars[0].v_type == VAR_LIST)
3531 {
3532 list = argvars[0].vval.v_list;
3533 if (list == NULL || list->lv_first == NULL)
3534 /* empty list, no commands, empty output */
3535 return;
3536 ++list->lv_refcount;
3537 }
3538 else
3539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003540 cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 if (cmd == NULL)
3542 return;
3543 }
3544
3545 if (argvars[1].v_type != VAR_UNKNOWN)
3546 {
3547 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003548 char_u *s = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003549
3550 if (s == NULL)
3551 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003552 if (*s == NUL)
3553 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003554 if (STRNCMP(s, "silent", 6) == 0)
3555 ++msg_silent;
3556 if (STRCMP(s, "silent!") == 0)
3557 {
3558 emsg_silent = TRUE;
3559 emsg_noredir = TRUE;
3560 }
3561 }
3562 else
3563 ++msg_silent;
3564
3565 if (redir_execute)
3566 save_ga = redir_execute_ga;
3567 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3568 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003569 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003570 if (!echo_output)
3571 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003572
3573 if (cmd != NULL)
3574 do_cmdline_cmd(cmd);
3575 else
3576 {
3577 listitem_T *item = list->lv_first;
3578
3579 do_cmdline(NULL, get_list_line, (void *)&item,
3580 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3581 --list->lv_refcount;
3582 }
3583
Bram Moolenaard297f352017-01-29 20:31:21 +01003584 /* Need to append a NUL to the result. */
3585 if (ga_grow(&redir_execute_ga, 1) == OK)
3586 {
3587 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3588 rettv->vval.v_string = redir_execute_ga.ga_data;
3589 }
3590 else
3591 {
3592 ga_clear(&redir_execute_ga);
3593 rettv->vval.v_string = NULL;
3594 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003595 msg_silent = save_msg_silent;
3596 emsg_silent = save_emsg_silent;
3597 emsg_noredir = save_emsg_noredir;
3598
3599 redir_execute = save_redir_execute;
3600 if (redir_execute)
3601 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003602 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003603
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003604 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003605 if (echo_output)
3606 // When not working silently: put it in column zero. A following
3607 // "echon" will overwrite the message, unavoidably.
3608 msg_col = 0;
3609 else
3610 // When working silently: Put it back where it was, since nothing
3611 // should have been written.
3612 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613}
3614
3615/*
3616 * "exepath()" function
3617 */
3618 static void
3619f_exepath(typval_T *argvars, typval_T *rettv)
3620{
3621 char_u *p = NULL;
3622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003623 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003624 rettv->v_type = VAR_STRING;
3625 rettv->vval.v_string = p;
3626}
3627
3628/*
3629 * "exists()" function
3630 */
3631 static void
3632f_exists(typval_T *argvars, typval_T *rettv)
3633{
3634 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003637 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003638 if (*p == '$') /* environment variable */
3639 {
3640 /* first try "normal" environment variables (fast) */
3641 if (mch_getenv(p + 1) != NULL)
3642 n = TRUE;
3643 else
3644 {
3645 /* try expanding things like $VIM and ${HOME} */
3646 p = expand_env_save(p);
3647 if (p != NULL && *p != '$')
3648 n = TRUE;
3649 vim_free(p);
3650 }
3651 }
3652 else if (*p == '&' || *p == '+') /* option */
3653 {
3654 n = (get_option_tv(&p, NULL, TRUE) == OK);
3655 if (*skipwhite(p) != NUL)
3656 n = FALSE; /* trailing garbage */
3657 }
3658 else if (*p == '*') /* internal or user defined function */
3659 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003660 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003661 }
3662 else if (*p == ':')
3663 {
3664 n = cmd_exists(p + 1);
3665 }
3666 else if (*p == '#')
3667 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003668 if (p[1] == '#')
3669 n = autocmd_supported(p + 2);
3670 else
3671 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003672 }
3673 else /* internal variable */
3674 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003675 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003676 }
3677
3678 rettv->vval.v_number = n;
3679}
3680
3681#ifdef FEAT_FLOAT
3682/*
3683 * "exp()" function
3684 */
3685 static void
3686f_exp(typval_T *argvars, typval_T *rettv)
3687{
3688 float_T f = 0.0;
3689
3690 rettv->v_type = VAR_FLOAT;
3691 if (get_float_arg(argvars, &f) == OK)
3692 rettv->vval.v_float = exp(f);
3693 else
3694 rettv->vval.v_float = 0.0;
3695}
3696#endif
3697
3698/*
3699 * "expand()" function
3700 */
3701 static void
3702f_expand(typval_T *argvars, typval_T *rettv)
3703{
3704 char_u *s;
3705 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003706 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003707 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3708 expand_T xpc;
3709 int error = FALSE;
3710 char_u *result;
3711
3712 rettv->v_type = VAR_STRING;
3713 if (argvars[1].v_type != VAR_UNKNOWN
3714 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003715 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003716 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003717 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003718
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003719 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003720 if (*s == '%' || *s == '#' || *s == '<')
3721 {
3722 ++emsg_off;
3723 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3724 --emsg_off;
3725 if (rettv->v_type == VAR_LIST)
3726 {
3727 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3728 list_append_string(rettv->vval.v_list, result, -1);
3729 else
3730 vim_free(result);
3731 }
3732 else
3733 rettv->vval.v_string = result;
3734 }
3735 else
3736 {
3737 /* When the optional second argument is non-zero, don't remove matches
3738 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3739 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 options |= WILD_KEEP_ALL;
3742 if (!error)
3743 {
3744 ExpandInit(&xpc);
3745 xpc.xp_context = EXPAND_FILES;
3746 if (p_wic)
3747 options += WILD_ICASE;
3748 if (rettv->v_type == VAR_STRING)
3749 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3750 options, WILD_ALL);
3751 else if (rettv_list_alloc(rettv) != FAIL)
3752 {
3753 int i;
3754
3755 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3756 for (i = 0; i < xpc.xp_numfiles; i++)
3757 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3758 ExpandCleanup(&xpc);
3759 }
3760 }
3761 else
3762 rettv->vval.v_string = NULL;
3763 }
3764}
3765
3766/*
3767 * "extend(list, list [, idx])" function
3768 * "extend(dict, dict [, action])" function
3769 */
3770 static void
3771f_extend(typval_T *argvars, typval_T *rettv)
3772{
3773 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3774
3775 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3776 {
3777 list_T *l1, *l2;
3778 listitem_T *item;
3779 long before;
3780 int error = FALSE;
3781
3782 l1 = argvars[0].vval.v_list;
3783 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003784 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003785 && l2 != NULL)
3786 {
3787 if (argvars[2].v_type != VAR_UNKNOWN)
3788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003789 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003790 if (error)
3791 return; /* type error; errmsg already given */
3792
3793 if (before == l1->lv_len)
3794 item = NULL;
3795 else
3796 {
3797 item = list_find(l1, before);
3798 if (item == NULL)
3799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003800 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003801 return;
3802 }
3803 }
3804 }
3805 else
3806 item = NULL;
3807 list_extend(l1, l2, item);
3808
3809 copy_tv(&argvars[0], rettv);
3810 }
3811 }
3812 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3813 {
3814 dict_T *d1, *d2;
3815 char_u *action;
3816 int i;
3817
3818 d1 = argvars[0].vval.v_dict;
3819 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003820 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003821 && d2 != NULL)
3822 {
3823 /* Check the third argument. */
3824 if (argvars[2].v_type != VAR_UNKNOWN)
3825 {
3826 static char *(av[]) = {"keep", "force", "error"};
3827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003828 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003829 if (action == NULL)
3830 return; /* type error; errmsg already given */
3831 for (i = 0; i < 3; ++i)
3832 if (STRCMP(action, av[i]) == 0)
3833 break;
3834 if (i == 3)
3835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003837 return;
3838 }
3839 }
3840 else
3841 action = (char_u *)"force";
3842
3843 dict_extend(d1, d2, action);
3844
3845 copy_tv(&argvars[0], rettv);
3846 }
3847 }
3848 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003849 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850}
3851
3852/*
3853 * "feedkeys()" function
3854 */
3855 static void
3856f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3857{
3858 int remap = TRUE;
3859 int insert = FALSE;
3860 char_u *keys, *flags;
3861 char_u nbuf[NUMBUFLEN];
3862 int typed = FALSE;
3863 int execute = FALSE;
3864 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003865 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003866 char_u *keys_esc;
3867
3868 /* This is not allowed in the sandbox. If the commands would still be
3869 * executed in the sandbox it would be OK, but it probably happens later,
3870 * when "sandbox" is no longer set. */
3871 if (check_secure())
3872 return;
3873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003874 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003875
3876 if (argvars[1].v_type != VAR_UNKNOWN)
3877 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003878 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003879 for ( ; *flags != NUL; ++flags)
3880 {
3881 switch (*flags)
3882 {
3883 case 'n': remap = FALSE; break;
3884 case 'm': remap = TRUE; break;
3885 case 't': typed = TRUE; break;
3886 case 'i': insert = TRUE; break;
3887 case 'x': execute = TRUE; break;
3888 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003889 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003890 }
3891 }
3892 }
3893
3894 if (*keys != NUL || execute)
3895 {
3896 /* Need to escape K_SPECIAL and CSI before putting the string in the
3897 * typeahead buffer. */
3898 keys_esc = vim_strsave_escape_csi(keys);
3899 if (keys_esc != NULL)
3900 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003901 if (lowlevel)
3902 {
3903#ifdef USE_INPUT_BUF
3904 add_to_input_buf(keys, (int)STRLEN(keys));
3905#else
3906 emsg(_("E980: lowlevel input not supported"));
3907#endif
3908 }
3909 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003910 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003911 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003912 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003913 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003914#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003915 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003916#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003917 )
3918 typebuf_was_filled = TRUE;
3919 }
3920 vim_free(keys_esc);
3921
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003922 if (execute)
3923 {
3924 int save_msg_scroll = msg_scroll;
3925
3926 /* Avoid a 1 second delay when the keys start Insert mode. */
3927 msg_scroll = FALSE;
3928
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003929 if (!dangerous)
3930 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003931 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003932 if (!dangerous)
3933 --ex_normal_busy;
3934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003935 msg_scroll |= save_msg_scroll;
3936 }
3937 }
3938 }
3939}
3940
3941/*
3942 * "filereadable()" function
3943 */
3944 static void
3945f_filereadable(typval_T *argvars, typval_T *rettv)
3946{
3947 int fd;
3948 char_u *p;
3949 int n;
3950
3951#ifndef O_NONBLOCK
3952# define O_NONBLOCK 0
3953#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003954 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003955 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3956 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3957 {
3958 n = TRUE;
3959 close(fd);
3960 }
3961 else
3962 n = FALSE;
3963
3964 rettv->vval.v_number = n;
3965}
3966
3967/*
3968 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3969 * rights to write into.
3970 */
3971 static void
3972f_filewritable(typval_T *argvars, typval_T *rettv)
3973{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003974 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003975}
3976
3977 static void
3978findfilendir(
3979 typval_T *argvars UNUSED,
3980 typval_T *rettv,
3981 int find_what UNUSED)
3982{
3983#ifdef FEAT_SEARCHPATH
3984 char_u *fname;
3985 char_u *fresult = NULL;
3986 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
3987 char_u *p;
3988 char_u pathbuf[NUMBUFLEN];
3989 int count = 1;
3990 int first = TRUE;
3991 int error = FALSE;
3992#endif
3993
3994 rettv->vval.v_string = NULL;
3995 rettv->v_type = VAR_STRING;
3996
3997#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003998 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003999
4000 if (argvars[1].v_type != VAR_UNKNOWN)
4001 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004002 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004003 if (p == NULL)
4004 error = TRUE;
4005 else
4006 {
4007 if (*p != NUL)
4008 path = p;
4009
4010 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004011 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004012 }
4013 }
4014
4015 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4016 error = TRUE;
4017
4018 if (*fname != NUL && !error)
4019 {
4020 do
4021 {
4022 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4023 vim_free(fresult);
4024 fresult = find_file_in_path_option(first ? fname : NULL,
4025 first ? (int)STRLEN(fname) : 0,
4026 0, first, path,
4027 find_what,
4028 curbuf->b_ffname,
4029 find_what == FINDFILE_DIR
4030 ? (char_u *)"" : curbuf->b_p_sua);
4031 first = FALSE;
4032
4033 if (fresult != NULL && rettv->v_type == VAR_LIST)
4034 list_append_string(rettv->vval.v_list, fresult, -1);
4035
4036 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4037 }
4038
4039 if (rettv->v_type == VAR_STRING)
4040 rettv->vval.v_string = fresult;
4041#endif
4042}
4043
4044/*
4045 * "filter()" function
4046 */
4047 static void
4048f_filter(typval_T *argvars, typval_T *rettv)
4049{
4050 filter_map(argvars, rettv, FALSE);
4051}
4052
4053/*
4054 * "finddir({fname}[, {path}[, {count}]])" function
4055 */
4056 static void
4057f_finddir(typval_T *argvars, typval_T *rettv)
4058{
4059 findfilendir(argvars, rettv, FINDFILE_DIR);
4060}
4061
4062/*
4063 * "findfile({fname}[, {path}[, {count}]])" function
4064 */
4065 static void
4066f_findfile(typval_T *argvars, typval_T *rettv)
4067{
4068 findfilendir(argvars, rettv, FINDFILE_FILE);
4069}
4070
4071#ifdef FEAT_FLOAT
4072/*
4073 * "float2nr({float})" function
4074 */
4075 static void
4076f_float2nr(typval_T *argvars, typval_T *rettv)
4077{
4078 float_T f = 0.0;
4079
4080 if (get_float_arg(argvars, &f) == OK)
4081 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004082 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004083 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004084 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004085 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004086 else
4087 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004088 }
4089}
4090
4091/*
4092 * "floor({float})" function
4093 */
4094 static void
4095f_floor(typval_T *argvars, typval_T *rettv)
4096{
4097 float_T f = 0.0;
4098
4099 rettv->v_type = VAR_FLOAT;
4100 if (get_float_arg(argvars, &f) == OK)
4101 rettv->vval.v_float = floor(f);
4102 else
4103 rettv->vval.v_float = 0.0;
4104}
4105
4106/*
4107 * "fmod()" function
4108 */
4109 static void
4110f_fmod(typval_T *argvars, typval_T *rettv)
4111{
4112 float_T fx = 0.0, fy = 0.0;
4113
4114 rettv->v_type = VAR_FLOAT;
4115 if (get_float_arg(argvars, &fx) == OK
4116 && get_float_arg(&argvars[1], &fy) == OK)
4117 rettv->vval.v_float = fmod(fx, fy);
4118 else
4119 rettv->vval.v_float = 0.0;
4120}
4121#endif
4122
4123/*
4124 * "fnameescape({string})" function
4125 */
4126 static void
4127f_fnameescape(typval_T *argvars, typval_T *rettv)
4128{
4129 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004130 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004131 rettv->v_type = VAR_STRING;
4132}
4133
4134/*
4135 * "fnamemodify({fname}, {mods})" function
4136 */
4137 static void
4138f_fnamemodify(typval_T *argvars, typval_T *rettv)
4139{
4140 char_u *fname;
4141 char_u *mods;
4142 int usedlen = 0;
4143 int len;
4144 char_u *fbuf = NULL;
4145 char_u buf[NUMBUFLEN];
4146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004147 fname = tv_get_string_chk(&argvars[0]);
4148 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004149 if (fname == NULL || mods == NULL)
4150 fname = NULL;
4151 else
4152 {
4153 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004154 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004155 }
4156
4157 rettv->v_type = VAR_STRING;
4158 if (fname == NULL)
4159 rettv->vval.v_string = NULL;
4160 else
4161 rettv->vval.v_string = vim_strnsave(fname, len);
4162 vim_free(fbuf);
4163}
4164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004165/*
4166 * "foldclosed()" function
4167 */
4168 static void
4169foldclosed_both(
4170 typval_T *argvars UNUSED,
4171 typval_T *rettv,
4172 int end UNUSED)
4173{
4174#ifdef FEAT_FOLDING
4175 linenr_T lnum;
4176 linenr_T first, last;
4177
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004178 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004179 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4180 {
4181 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4182 {
4183 if (end)
4184 rettv->vval.v_number = (varnumber_T)last;
4185 else
4186 rettv->vval.v_number = (varnumber_T)first;
4187 return;
4188 }
4189 }
4190#endif
4191 rettv->vval.v_number = -1;
4192}
4193
4194/*
4195 * "foldclosed()" function
4196 */
4197 static void
4198f_foldclosed(typval_T *argvars, typval_T *rettv)
4199{
4200 foldclosed_both(argvars, rettv, FALSE);
4201}
4202
4203/*
4204 * "foldclosedend()" function
4205 */
4206 static void
4207f_foldclosedend(typval_T *argvars, typval_T *rettv)
4208{
4209 foldclosed_both(argvars, rettv, TRUE);
4210}
4211
4212/*
4213 * "foldlevel()" function
4214 */
4215 static void
4216f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4217{
4218#ifdef FEAT_FOLDING
4219 linenr_T lnum;
4220
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004221 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004222 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4223 rettv->vval.v_number = foldLevel(lnum);
4224#endif
4225}
4226
4227/*
4228 * "foldtext()" function
4229 */
4230 static void
4231f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4232{
4233#ifdef FEAT_FOLDING
4234 linenr_T foldstart;
4235 linenr_T foldend;
4236 char_u *dashes;
4237 linenr_T lnum;
4238 char_u *s;
4239 char_u *r;
4240 int len;
4241 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004242 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243#endif
4244
4245 rettv->v_type = VAR_STRING;
4246 rettv->vval.v_string = NULL;
4247#ifdef FEAT_FOLDING
4248 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4249 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4250 dashes = get_vim_var_str(VV_FOLDDASHES);
4251 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4252 && dashes != NULL)
4253 {
4254 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004255 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004256 if (!linewhite(lnum))
4257 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004258
4259 /* Find interesting text in this line. */
4260 s = skipwhite(ml_get(lnum));
4261 /* skip C comment-start */
4262 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4263 {
4264 s = skipwhite(s + 2);
4265 if (*skipwhite(s) == NUL
4266 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4267 {
4268 s = skipwhite(ml_get(lnum + 1));
4269 if (*s == '*')
4270 s = skipwhite(s + 1);
4271 }
4272 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004273 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004274 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004275 r = alloc(STRLEN(txt)
4276 + STRLEN(dashes) // for %s
4277 + 20 // for %3ld
4278 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279 if (r != NULL)
4280 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004281 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004282 len = (int)STRLEN(r);
4283 STRCAT(r, s);
4284 /* remove 'foldmarker' and 'commentstring' */
4285 foldtext_cleanup(r + len);
4286 rettv->vval.v_string = r;
4287 }
4288 }
4289#endif
4290}
4291
4292/*
4293 * "foldtextresult(lnum)" function
4294 */
4295 static void
4296f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4297{
4298#ifdef FEAT_FOLDING
4299 linenr_T lnum;
4300 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004301 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004302 foldinfo_T foldinfo;
4303 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004304 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004305#endif
4306
4307 rettv->v_type = VAR_STRING;
4308 rettv->vval.v_string = NULL;
4309#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004310 if (entered)
4311 return; /* reject recursive use */
4312 entered = TRUE;
4313
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004314 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004315 /* treat illegal types and illegal string values for {lnum} the same */
4316 if (lnum < 0)
4317 lnum = 0;
4318 fold_count = foldedCount(curwin, lnum, &foldinfo);
4319 if (fold_count > 0)
4320 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004321 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4322 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 if (text == buf)
4324 text = vim_strsave(text);
4325 rettv->vval.v_string = text;
4326 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004327
4328 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004329#endif
4330}
4331
4332/*
4333 * "foreground()" function
4334 */
4335 static void
4336f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4337{
4338#ifdef FEAT_GUI
4339 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004340 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004341 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004342 return;
4343 }
4344#endif
4345#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004346 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004347#endif
4348}
4349
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004351common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004352{
4353 char_u *s;
4354 char_u *name;
4355 int use_string = FALSE;
4356 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004357 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004358
4359 if (argvars[0].v_type == VAR_FUNC)
4360 {
4361 /* function(MyFunc, [arg], dict) */
4362 s = argvars[0].vval.v_string;
4363 }
4364 else if (argvars[0].v_type == VAR_PARTIAL
4365 && argvars[0].vval.v_partial != NULL)
4366 {
4367 /* function(dict.MyFunc, [arg]) */
4368 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004369 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004370 }
4371 else
4372 {
4373 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004374 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004375 use_string = TRUE;
4376 }
4377
Bram Moolenaar843b8842016-08-21 14:36:15 +02004378 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004379 {
4380 name = s;
4381 trans_name = trans_function_name(&name, FALSE,
4382 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4383 if (*name != NUL)
4384 s = NULL;
4385 }
4386
Bram Moolenaar843b8842016-08-21 14:36:15 +02004387 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4388 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004390 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004391 else if (trans_name != NULL && (is_funcref
4392 ? find_func(trans_name) == NULL
4393 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004394 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004395 else
4396 {
4397 int dict_idx = 0;
4398 int arg_idx = 0;
4399 list_T *list = NULL;
4400
4401 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4402 {
4403 char sid_buf[25];
4404 int off = *s == 's' ? 2 : 5;
4405
4406 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4407 * also be called from another script. Using trans_function_name()
4408 * would also work, but some plugins depend on the name being
4409 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004410 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
4412 if (name != NULL)
4413 {
4414 STRCPY(name, sid_buf);
4415 STRCAT(name, s + off);
4416 }
4417 }
4418 else
4419 name = vim_strsave(s);
4420
4421 if (argvars[1].v_type != VAR_UNKNOWN)
4422 {
4423 if (argvars[2].v_type != VAR_UNKNOWN)
4424 {
4425 /* function(name, [args], dict) */
4426 arg_idx = 1;
4427 dict_idx = 2;
4428 }
4429 else if (argvars[1].v_type == VAR_DICT)
4430 /* function(name, dict) */
4431 dict_idx = 1;
4432 else
4433 /* function(name, [args]) */
4434 arg_idx = 1;
4435 if (dict_idx > 0)
4436 {
4437 if (argvars[dict_idx].v_type != VAR_DICT)
4438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004439 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004440 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004441 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004442 }
4443 if (argvars[dict_idx].vval.v_dict == NULL)
4444 dict_idx = 0;
4445 }
4446 if (arg_idx > 0)
4447 {
4448 if (argvars[arg_idx].v_type != VAR_LIST)
4449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004450 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004451 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004452 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004453 }
4454 list = argvars[arg_idx].vval.v_list;
4455 if (list == NULL || list->lv_len == 0)
4456 arg_idx = 0;
4457 }
4458 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004459 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004460 {
4461 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
4462
4463 /* result is a VAR_PARTIAL */
4464 if (pt == NULL)
4465 vim_free(name);
4466 else
4467 {
4468 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4469 {
4470 listitem_T *li;
4471 int i = 0;
4472 int arg_len = 0;
4473 int lv_len = 0;
4474
4475 if (arg_pt != NULL)
4476 arg_len = arg_pt->pt_argc;
4477 if (list != NULL)
4478 lv_len = list->lv_len;
4479 pt->pt_argc = arg_len + lv_len;
4480 pt->pt_argv = (typval_T *)alloc(
4481 sizeof(typval_T) * pt->pt_argc);
4482 if (pt->pt_argv == NULL)
4483 {
4484 vim_free(pt);
4485 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004486 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004487 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004488 for (i = 0; i < arg_len; i++)
4489 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4490 if (lv_len > 0)
4491 for (li = list->lv_first; li != NULL;
4492 li = li->li_next)
4493 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004494 }
4495
4496 /* For "function(dict.func, [], dict)" and "func" is a partial
4497 * use "dict". That is backwards compatible. */
4498 if (dict_idx > 0)
4499 {
4500 /* The dict is bound explicitly, pt_auto is FALSE. */
4501 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4502 ++pt->pt_dict->dv_refcount;
4503 }
4504 else if (arg_pt != NULL)
4505 {
4506 /* If the dict was bound automatically the result is also
4507 * bound automatically. */
4508 pt->pt_dict = arg_pt->pt_dict;
4509 pt->pt_auto = arg_pt->pt_auto;
4510 if (pt->pt_dict != NULL)
4511 ++pt->pt_dict->dv_refcount;
4512 }
4513
4514 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004515 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4516 {
4517 pt->pt_func = arg_pt->pt_func;
4518 func_ptr_ref(pt->pt_func);
4519 vim_free(name);
4520 }
4521 else if (is_funcref)
4522 {
4523 pt->pt_func = find_func(trans_name);
4524 func_ptr_ref(pt->pt_func);
4525 vim_free(name);
4526 }
4527 else
4528 {
4529 pt->pt_name = name;
4530 func_ref(name);
4531 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004532 }
4533 rettv->v_type = VAR_PARTIAL;
4534 rettv->vval.v_partial = pt;
4535 }
4536 else
4537 {
4538 /* result is a VAR_FUNC */
4539 rettv->v_type = VAR_FUNC;
4540 rettv->vval.v_string = name;
4541 func_ref(name);
4542 }
4543 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544theend:
4545 vim_free(trans_name);
4546}
4547
4548/*
4549 * "funcref()" function
4550 */
4551 static void
4552f_funcref(typval_T *argvars, typval_T *rettv)
4553{
4554 common_function(argvars, rettv, TRUE);
4555}
4556
4557/*
4558 * "function()" function
4559 */
4560 static void
4561f_function(typval_T *argvars, typval_T *rettv)
4562{
4563 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004564}
4565
4566/*
4567 * "garbagecollect()" function
4568 */
4569 static void
4570f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4571{
4572 /* This is postponed until we are back at the toplevel, because we may be
4573 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4574 want_garbage_collect = TRUE;
4575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004576 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004577 garbage_collect_at_exit = TRUE;
4578}
4579
4580/*
4581 * "get()" function
4582 */
4583 static void
4584f_get(typval_T *argvars, typval_T *rettv)
4585{
4586 listitem_T *li;
4587 list_T *l;
4588 dictitem_T *di;
4589 dict_T *d;
4590 typval_T *tv = NULL;
4591
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004592 if (argvars[0].v_type == VAR_BLOB)
4593 {
4594 int error = FALSE;
4595 int idx = tv_get_number_chk(&argvars[1], &error);
4596
4597 if (!error)
4598 {
4599 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004600 if (idx < 0)
4601 idx = blob_len(argvars[0].vval.v_blob) + idx;
4602 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4603 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004604 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004605 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004606 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004607 tv = rettv;
4608 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004609 }
4610 }
4611 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004612 {
4613 if ((l = argvars[0].vval.v_list) != NULL)
4614 {
4615 int error = FALSE;
4616
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004617 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004618 if (!error && li != NULL)
4619 tv = &li->li_tv;
4620 }
4621 }
4622 else if (argvars[0].v_type == VAR_DICT)
4623 {
4624 if ((d = argvars[0].vval.v_dict) != NULL)
4625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004626 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004627 if (di != NULL)
4628 tv = &di->di_tv;
4629 }
4630 }
4631 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4632 {
4633 partial_T *pt;
4634 partial_T fref_pt;
4635
4636 if (argvars[0].v_type == VAR_PARTIAL)
4637 pt = argvars[0].vval.v_partial;
4638 else
4639 {
4640 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4641 fref_pt.pt_name = argvars[0].vval.v_string;
4642 pt = &fref_pt;
4643 }
4644
4645 if (pt != NULL)
4646 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004647 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004648 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004649
4650 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4651 {
4652 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004653 n = partial_name(pt);
4654 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004655 rettv->vval.v_string = NULL;
4656 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004657 {
4658 rettv->vval.v_string = vim_strsave(n);
4659 if (rettv->v_type == VAR_FUNC)
4660 func_ref(rettv->vval.v_string);
4661 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004662 }
4663 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004664 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004665 else if (STRCMP(what, "args") == 0)
4666 {
4667 rettv->v_type = VAR_LIST;
4668 if (rettv_list_alloc(rettv) == OK)
4669 {
4670 int i;
4671
4672 for (i = 0; i < pt->pt_argc; ++i)
4673 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4674 }
4675 }
4676 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004678 return;
4679 }
4680 }
4681 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004682 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683
4684 if (tv == NULL)
4685 {
4686 if (argvars[2].v_type != VAR_UNKNOWN)
4687 copy_tv(&argvars[2], rettv);
4688 }
4689 else
4690 copy_tv(tv, rettv);
4691}
4692
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004693/*
4694 * Returns buffer options, variables and other attributes in a dictionary.
4695 */
4696 static dict_T *
4697get_buffer_info(buf_T *buf)
4698{
4699 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004700 tabpage_T *tp;
4701 win_T *wp;
4702 list_T *windows;
4703
4704 dict = dict_alloc();
4705 if (dict == NULL)
4706 return NULL;
4707
Bram Moolenaare0be1672018-07-08 16:50:37 +02004708 dict_add_number(dict, "bufnr", buf->b_fnum);
4709 dict_add_string(dict, "name", buf->b_ffname);
4710 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4711 : buflist_findlnum(buf));
4712 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4713 dict_add_number(dict, "listed", buf->b_p_bl);
4714 dict_add_number(dict, "changed", bufIsChanged(buf));
4715 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4716 dict_add_number(dict, "hidden",
4717 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004718
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004719 /* Get a reference to buffer variables */
4720 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004721
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004722 /* List of windows displaying this buffer */
4723 windows = list_alloc();
4724 if (windows != NULL)
4725 {
4726 FOR_ALL_TAB_WINDOWS(tp, wp)
4727 if (wp->w_buffer == buf)
4728 list_append_number(windows, (varnumber_T)wp->w_id);
4729 dict_add_list(dict, "windows", windows);
4730 }
4731
4732#ifdef FEAT_SIGNS
4733 if (buf->b_signlist != NULL)
4734 {
4735 /* List of signs placed in this buffer */
4736 list_T *signs = list_alloc();
4737 if (signs != NULL)
4738 {
4739 get_buffer_signs(buf, signs);
4740 dict_add_list(dict, "signs", signs);
4741 }
4742 }
4743#endif
4744
4745 return dict;
4746}
4747
4748/*
4749 * "getbufinfo()" function
4750 */
4751 static void
4752f_getbufinfo(typval_T *argvars, typval_T *rettv)
4753{
4754 buf_T *buf = NULL;
4755 buf_T *argbuf = NULL;
4756 dict_T *d;
4757 int filtered = FALSE;
4758 int sel_buflisted = FALSE;
4759 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004760 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004761
4762 if (rettv_list_alloc(rettv) != OK)
4763 return;
4764
4765 /* List of all the buffers or selected buffers */
4766 if (argvars[0].v_type == VAR_DICT)
4767 {
4768 dict_T *sel_d = argvars[0].vval.v_dict;
4769
4770 if (sel_d != NULL)
4771 {
4772 dictitem_T *di;
4773
4774 filtered = TRUE;
4775
4776 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004777 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004778 sel_buflisted = TRUE;
4779
4780 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004781 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004782 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004783
4784 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004785 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004786 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004787 }
4788 }
4789 else if (argvars[0].v_type != VAR_UNKNOWN)
4790 {
4791 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004792 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004793 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004794 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004795 --emsg_off;
4796 if (argbuf == NULL)
4797 return;
4798 }
4799
4800 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004801 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 {
4803 if (argbuf != NULL && argbuf != buf)
4804 continue;
4805 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004806 || (sel_buflisted && !buf->b_p_bl)
4807 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004808 continue;
4809
4810 d = get_buffer_info(buf);
4811 if (d != NULL)
4812 list_append_dict(rettv->vval.v_list, d);
4813 if (argbuf != NULL)
4814 return;
4815 }
4816}
4817
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004818/*
4819 * Get line or list of lines from buffer "buf" into "rettv".
4820 * Return a range (from start to end) of lines in rettv from the specified
4821 * buffer.
4822 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4823 */
4824 static void
4825get_buffer_lines(
4826 buf_T *buf,
4827 linenr_T start,
4828 linenr_T end,
4829 int retlist,
4830 typval_T *rettv)
4831{
4832 char_u *p;
4833
4834 rettv->v_type = VAR_STRING;
4835 rettv->vval.v_string = NULL;
4836 if (retlist && rettv_list_alloc(rettv) == FAIL)
4837 return;
4838
4839 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4840 return;
4841
4842 if (!retlist)
4843 {
4844 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4845 p = ml_get_buf(buf, start, FALSE);
4846 else
4847 p = (char_u *)"";
4848 rettv->vval.v_string = vim_strsave(p);
4849 }
4850 else
4851 {
4852 if (end < start)
4853 return;
4854
4855 if (start < 1)
4856 start = 1;
4857 if (end > buf->b_ml.ml_line_count)
4858 end = buf->b_ml.ml_line_count;
4859 while (start <= end)
4860 if (list_append_string(rettv->vval.v_list,
4861 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4862 break;
4863 }
4864}
4865
4866/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004867 * "getbufline()" function
4868 */
4869 static void
4870f_getbufline(typval_T *argvars, typval_T *rettv)
4871{
4872 linenr_T lnum;
4873 linenr_T end;
4874 buf_T *buf;
4875
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004876 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004877 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004878 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004879 --emsg_off;
4880
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004881 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882 if (argvars[2].v_type == VAR_UNKNOWN)
4883 end = lnum;
4884 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004885 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004886
4887 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4888}
4889
4890/*
4891 * "getbufvar()" function
4892 */
4893 static void
4894f_getbufvar(typval_T *argvars, typval_T *rettv)
4895{
4896 buf_T *buf;
4897 buf_T *save_curbuf;
4898 char_u *varname;
4899 dictitem_T *v;
4900 int done = FALSE;
4901
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004902 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4903 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004905 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 rettv->v_type = VAR_STRING;
4908 rettv->vval.v_string = NULL;
4909
4910 if (buf != NULL && varname != NULL)
4911 {
4912 /* set curbuf to be our buf, temporarily */
4913 save_curbuf = curbuf;
4914 curbuf = buf;
4915
Bram Moolenaar30567352016-08-27 21:25:44 +02004916 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004917 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004918 if (varname[1] == NUL)
4919 {
4920 /* get all buffer-local options in a dict */
4921 dict_T *opts = get_winbuf_options(TRUE);
4922
4923 if (opts != NULL)
4924 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004925 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004926 done = TRUE;
4927 }
4928 }
4929 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4930 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004931 done = TRUE;
4932 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004933 else
4934 {
4935 /* Look up the variable. */
4936 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4937 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4938 'b', varname, FALSE);
4939 if (v != NULL)
4940 {
4941 copy_tv(&v->di_tv, rettv);
4942 done = TRUE;
4943 }
4944 }
4945
4946 /* restore previous notion of curbuf */
4947 curbuf = save_curbuf;
4948 }
4949
4950 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4951 /* use the default value */
4952 copy_tv(&argvars[2], rettv);
4953
4954 --emsg_off;
4955}
4956
4957/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004958 * "getchangelist()" function
4959 */
4960 static void
4961f_getchangelist(typval_T *argvars, typval_T *rettv)
4962{
4963#ifdef FEAT_JUMPLIST
4964 buf_T *buf;
4965 int i;
4966 list_T *l;
4967 dict_T *d;
4968#endif
4969
4970 if (rettv_list_alloc(rettv) != OK)
4971 return;
4972
4973#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004974 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004975 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004976 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004977 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004978 if (buf == NULL)
4979 return;
4980
4981 l = list_alloc();
4982 if (l == NULL)
4983 return;
4984
4985 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4986 return;
4987 /*
4988 * The current window change list index tracks only the position in the
4989 * current buffer change list. For other buffers, use the change list
4990 * length as the current index.
4991 */
4992 list_append_number(rettv->vval.v_list,
4993 (varnumber_T)((buf == curwin->w_buffer)
4994 ? curwin->w_changelistidx : buf->b_changelistlen));
4995
4996 for (i = 0; i < buf->b_changelistlen; ++i)
4997 {
4998 if (buf->b_changelist[i].lnum == 0)
4999 continue;
5000 if ((d = dict_alloc()) == NULL)
5001 return;
5002 if (list_append_dict(l, d) == FAIL)
5003 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005004 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5005 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005006 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005007 }
5008#endif
5009}
5010/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005011 * "getchar()" function
5012 */
5013 static void
5014f_getchar(typval_T *argvars, typval_T *rettv)
5015{
5016 varnumber_T n;
5017 int error = FALSE;
5018
Bram Moolenaar84d93902018-09-11 20:10:20 +02005019#ifdef MESSAGE_QUEUE
5020 // vpeekc() used to check for messages, but that caused problems, invoking
5021 // a callback where it was not expected. Some plugins use getchar(1) in a
5022 // loop to await a message, therefore make sure we check for messages here.
5023 parse_queued_messages();
5024#endif
5025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005026 /* Position the cursor. Needed after a message that ends in a space. */
5027 windgoto(msg_row, msg_col);
5028
5029 ++no_mapping;
5030 ++allow_keys;
5031 for (;;)
5032 {
5033 if (argvars[0].v_type == VAR_UNKNOWN)
5034 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005035 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005036 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005037 /* getchar(1): only check if char avail */
5038 n = vpeekc_any();
5039 else if (error || vpeekc_any() == NUL)
5040 /* illegal argument or getchar(0) and no char avail: return zero */
5041 n = 0;
5042 else
5043 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005044 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005045
5046 if (n == K_IGNORE)
5047 continue;
5048 break;
5049 }
5050 --no_mapping;
5051 --allow_keys;
5052
5053 set_vim_var_nr(VV_MOUSE_WIN, 0);
5054 set_vim_var_nr(VV_MOUSE_WINID, 0);
5055 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5056 set_vim_var_nr(VV_MOUSE_COL, 0);
5057
5058 rettv->vval.v_number = n;
5059 if (IS_SPECIAL(n) || mod_mask != 0)
5060 {
5061 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5062 int i = 0;
5063
5064 /* Turn a special key into three bytes, plus modifier. */
5065 if (mod_mask != 0)
5066 {
5067 temp[i++] = K_SPECIAL;
5068 temp[i++] = KS_MODIFIER;
5069 temp[i++] = mod_mask;
5070 }
5071 if (IS_SPECIAL(n))
5072 {
5073 temp[i++] = K_SPECIAL;
5074 temp[i++] = K_SECOND(n);
5075 temp[i++] = K_THIRD(n);
5076 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005077 else if (has_mbyte)
5078 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005079 else
5080 temp[i++] = n;
5081 temp[i++] = NUL;
5082 rettv->v_type = VAR_STRING;
5083 rettv->vval.v_string = vim_strsave(temp);
5084
5085#ifdef FEAT_MOUSE
5086 if (is_mouse_key(n))
5087 {
5088 int row = mouse_row;
5089 int col = mouse_col;
5090 win_T *win;
5091 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005092 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005093 int winnr = 1;
5094
5095 if (row >= 0 && col >= 0)
5096 {
5097 /* Find the window at the mouse coordinates and compute the
5098 * text position. */
5099 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005100 if (win == NULL)
5101 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005102 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005103 for (wp = firstwin; wp != win; wp = wp->w_next)
5104 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005105 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5106 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5107 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5108 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5109 }
5110 }
5111#endif
5112 }
5113}
5114
5115/*
5116 * "getcharmod()" function
5117 */
5118 static void
5119f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5120{
5121 rettv->vval.v_number = mod_mask;
5122}
5123
5124/*
5125 * "getcharsearch()" function
5126 */
5127 static void
5128f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5129{
5130 if (rettv_dict_alloc(rettv) != FAIL)
5131 {
5132 dict_T *dict = rettv->vval.v_dict;
5133
Bram Moolenaare0be1672018-07-08 16:50:37 +02005134 dict_add_string(dict, "char", last_csearch());
5135 dict_add_number(dict, "forward", last_csearch_forward());
5136 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005137 }
5138}
5139
5140/*
5141 * "getcmdline()" function
5142 */
5143 static void
5144f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5145{
5146 rettv->v_type = VAR_STRING;
5147 rettv->vval.v_string = get_cmdline_str();
5148}
5149
5150/*
5151 * "getcmdpos()" function
5152 */
5153 static void
5154f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5155{
5156 rettv->vval.v_number = get_cmdline_pos() + 1;
5157}
5158
5159/*
5160 * "getcmdtype()" function
5161 */
5162 static void
5163f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5164{
5165 rettv->v_type = VAR_STRING;
5166 rettv->vval.v_string = alloc(2);
5167 if (rettv->vval.v_string != NULL)
5168 {
5169 rettv->vval.v_string[0] = get_cmdline_type();
5170 rettv->vval.v_string[1] = NUL;
5171 }
5172}
5173
5174/*
5175 * "getcmdwintype()" function
5176 */
5177 static void
5178f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5179{
5180 rettv->v_type = VAR_STRING;
5181 rettv->vval.v_string = NULL;
5182#ifdef FEAT_CMDWIN
5183 rettv->vval.v_string = alloc(2);
5184 if (rettv->vval.v_string != NULL)
5185 {
5186 rettv->vval.v_string[0] = cmdwin_type;
5187 rettv->vval.v_string[1] = NUL;
5188 }
5189#endif
5190}
5191
5192#if defined(FEAT_CMDL_COMPL)
5193/*
5194 * "getcompletion()" function
5195 */
5196 static void
5197f_getcompletion(typval_T *argvars, typval_T *rettv)
5198{
5199 char_u *pat;
5200 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005201 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005202 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5203 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005204
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005205 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005206 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005207
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005208 if (p_wic)
5209 options |= WILD_ICASE;
5210
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005211 /* For filtered results, 'wildignore' is used */
5212 if (!filtered)
5213 options |= WILD_KEEP_ALL;
5214
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005215 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005216 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005217 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005218 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005219 if (xpc.xp_context == EXPAND_NOTHING)
5220 {
5221 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005222 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005223 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005224 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005225 return;
5226 }
5227
5228# if defined(FEAT_MENU)
5229 if (xpc.xp_context == EXPAND_MENUS)
5230 {
5231 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5232 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5233 }
5234# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005235#ifdef FEAT_CSCOPE
5236 if (xpc.xp_context == EXPAND_CSCOPE)
5237 {
5238 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5239 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5240 }
5241#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005242#ifdef FEAT_SIGNS
5243 if (xpc.xp_context == EXPAND_SIGN)
5244 {
5245 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5246 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5247 }
5248#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005249
5250 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5251 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5252 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005253 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005254
5255 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5256
5257 for (i = 0; i < xpc.xp_numfiles; i++)
5258 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5259 }
5260 vim_free(pat);
5261 ExpandCleanup(&xpc);
5262}
5263#endif
5264
5265/*
5266 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005267 *
5268 * Return the current working directory of a window in a tab page.
5269 * First optional argument 'winnr' is the window number or -1 and the second
5270 * optional argument 'tabnr' is the tab page number.
5271 *
5272 * If no arguments are supplied, then return the directory of the current
5273 * window.
5274 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5275 * the specified window.
5276 * If 'winnr' is 0 then return the directory of the current window.
5277 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5278 * directory of the specified tab page. Otherwise return the directory of the
5279 * specified window in the specified tab page.
5280 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005281 */
5282 static void
5283f_getcwd(typval_T *argvars, typval_T *rettv)
5284{
5285 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005286 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005287 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005288 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005289
5290 rettv->v_type = VAR_STRING;
5291 rettv->vval.v_string = NULL;
5292
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005293 if (argvars[0].v_type == VAR_NUMBER
5294 && argvars[0].vval.v_number == -1
5295 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005296 global = TRUE;
5297 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005298 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005299
5300 if (wp != NULL && wp->w_localdir != NULL)
5301 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005302 else if (tp != NULL && tp->tp_localdir != NULL)
5303 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5304 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005306 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 rettv->vval.v_string = vim_strsave(globaldir);
5308 else
5309 {
5310 cwd = alloc(MAXPATHL);
5311 if (cwd != NULL)
5312 {
5313 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5314 rettv->vval.v_string = vim_strsave(cwd);
5315 vim_free(cwd);
5316 }
5317 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005318 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005319#ifdef BACKSLASH_IN_FILENAME
5320 if (rettv->vval.v_string != NULL)
5321 slash_adjust(rettv->vval.v_string);
5322#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323}
5324
5325/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005326 * "getenv()" function
5327 */
5328 static void
5329f_getenv(typval_T *argvars, typval_T *rettv)
5330{
5331 int mustfree = FALSE;
5332 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5333
5334 if (p == NULL)
5335 {
5336 rettv->v_type = VAR_SPECIAL;
5337 rettv->vval.v_number = VVAL_NULL;
5338 return;
5339 }
5340 if (!mustfree)
5341 p = vim_strsave(p);
5342 rettv->vval.v_string = p;
5343 rettv->v_type = VAR_STRING;
5344}
5345
5346/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005347 * "getfontname()" function
5348 */
5349 static void
5350f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5351{
5352 rettv->v_type = VAR_STRING;
5353 rettv->vval.v_string = NULL;
5354#ifdef FEAT_GUI
5355 if (gui.in_use)
5356 {
5357 GuiFont font;
5358 char_u *name = NULL;
5359
5360 if (argvars[0].v_type == VAR_UNKNOWN)
5361 {
5362 /* Get the "Normal" font. Either the name saved by
5363 * hl_set_font_name() or from the font ID. */
5364 font = gui.norm_font;
5365 name = hl_get_font_name();
5366 }
5367 else
5368 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005369 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005370 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5371 return;
5372 font = gui_mch_get_font(name, FALSE);
5373 if (font == NOFONT)
5374 return; /* Invalid font name, return empty string. */
5375 }
5376 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5377 if (argvars[0].v_type != VAR_UNKNOWN)
5378 gui_mch_free_font(font);
5379 }
5380#endif
5381}
5382
5383/*
5384 * "getfperm({fname})" function
5385 */
5386 static void
5387f_getfperm(typval_T *argvars, typval_T *rettv)
5388{
5389 char_u *fname;
5390 stat_T st;
5391 char_u *perm = NULL;
5392 char_u flags[] = "rwx";
5393 int i;
5394
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005395 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005396
5397 rettv->v_type = VAR_STRING;
5398 if (mch_stat((char *)fname, &st) >= 0)
5399 {
5400 perm = vim_strsave((char_u *)"---------");
5401 if (perm != NULL)
5402 {
5403 for (i = 0; i < 9; i++)
5404 {
5405 if (st.st_mode & (1 << (8 - i)))
5406 perm[i] = flags[i % 3];
5407 }
5408 }
5409 }
5410 rettv->vval.v_string = perm;
5411}
5412
5413/*
5414 * "getfsize({fname})" function
5415 */
5416 static void
5417f_getfsize(typval_T *argvars, typval_T *rettv)
5418{
5419 char_u *fname;
5420 stat_T st;
5421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005422 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005423
5424 rettv->v_type = VAR_NUMBER;
5425
5426 if (mch_stat((char *)fname, &st) >= 0)
5427 {
5428 if (mch_isdir(fname))
5429 rettv->vval.v_number = 0;
5430 else
5431 {
5432 rettv->vval.v_number = (varnumber_T)st.st_size;
5433
5434 /* non-perfect check for overflow */
5435 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5436 rettv->vval.v_number = -2;
5437 }
5438 }
5439 else
5440 rettv->vval.v_number = -1;
5441}
5442
5443/*
5444 * "getftime({fname})" function
5445 */
5446 static void
5447f_getftime(typval_T *argvars, typval_T *rettv)
5448{
5449 char_u *fname;
5450 stat_T st;
5451
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005452 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005453
5454 if (mch_stat((char *)fname, &st) >= 0)
5455 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5456 else
5457 rettv->vval.v_number = -1;
5458}
5459
5460/*
5461 * "getftype({fname})" function
5462 */
5463 static void
5464f_getftype(typval_T *argvars, typval_T *rettv)
5465{
5466 char_u *fname;
5467 stat_T st;
5468 char_u *type = NULL;
5469 char *t;
5470
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005471 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005472
5473 rettv->v_type = VAR_STRING;
5474 if (mch_lstat((char *)fname, &st) >= 0)
5475 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005476 if (S_ISREG(st.st_mode))
5477 t = "file";
5478 else if (S_ISDIR(st.st_mode))
5479 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005480 else if (S_ISLNK(st.st_mode))
5481 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005482 else if (S_ISBLK(st.st_mode))
5483 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005484 else if (S_ISCHR(st.st_mode))
5485 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005486 else if (S_ISFIFO(st.st_mode))
5487 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005488 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005489 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490 else
5491 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492 type = vim_strsave((char_u *)t);
5493 }
5494 rettv->vval.v_string = type;
5495}
5496
5497/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005498 * "getjumplist()" function
5499 */
5500 static void
5501f_getjumplist(typval_T *argvars, typval_T *rettv)
5502{
5503#ifdef FEAT_JUMPLIST
5504 win_T *wp;
5505 int i;
5506 list_T *l;
5507 dict_T *d;
5508#endif
5509
5510 if (rettv_list_alloc(rettv) != OK)
5511 return;
5512
5513#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005514 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005515 if (wp == NULL)
5516 return;
5517
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005518 cleanup_jumplist(wp, TRUE);
5519
Bram Moolenaar4f505882018-02-10 21:06:32 +01005520 l = list_alloc();
5521 if (l == NULL)
5522 return;
5523
5524 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5525 return;
5526 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5527
5528 for (i = 0; i < wp->w_jumplistlen; ++i)
5529 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005530 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5531 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005532 if ((d = dict_alloc()) == NULL)
5533 return;
5534 if (list_append_dict(l, d) == FAIL)
5535 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005536 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5537 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005538 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005539 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005540 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005541 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005542 }
5543#endif
5544}
5545
5546/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005547 * "getline(lnum, [end])" function
5548 */
5549 static void
5550f_getline(typval_T *argvars, typval_T *rettv)
5551{
5552 linenr_T lnum;
5553 linenr_T end;
5554 int retlist;
5555
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005556 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005557 if (argvars[1].v_type == VAR_UNKNOWN)
5558 {
5559 end = 0;
5560 retlist = FALSE;
5561 }
5562 else
5563 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005564 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565 retlist = TRUE;
5566 }
5567
5568 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5569}
5570
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005571#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005572 static void
5573get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5574{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005575 if (what_arg->v_type == VAR_UNKNOWN)
5576 {
5577 if (rettv_list_alloc(rettv) == OK)
5578 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005579 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005580 }
5581 else
5582 {
5583 if (rettv_dict_alloc(rettv) == OK)
5584 if (is_qf || (wp != NULL))
5585 {
5586 if (what_arg->v_type == VAR_DICT)
5587 {
5588 dict_T *d = what_arg->vval.v_dict;
5589
5590 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005591 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005592 }
5593 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005594 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005595 }
5596 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005597}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005598#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005599
5600/*
5601 * "getloclist()" function
5602 */
5603 static void
5604f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5605{
5606#ifdef FEAT_QUICKFIX
5607 win_T *wp;
5608
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005609 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005610 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5611#endif
5612}
5613
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005614/*
5615 * "getmatches()" function
5616 */
5617 static void
5618f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5619{
5620#ifdef FEAT_SEARCH_EXTRA
5621 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005622 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005623 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005624 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005625
Bram Moolenaaraff74912019-03-30 18:11:49 +01005626 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5627 return;
5628
5629 cur = win->w_match_head;
5630 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005631 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005632 dict = dict_alloc();
5633 if (dict == NULL)
5634 return;
5635 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005636 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005637 /* match added with matchaddpos() */
5638 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005639 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005640 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005641 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005642 list_T *l;
5643
5644 llpos = &cur->pos.pos[i];
5645 if (llpos->lnum == 0)
5646 break;
5647 l = list_alloc();
5648 if (l == NULL)
5649 break;
5650 list_append_number(l, (varnumber_T)llpos->lnum);
5651 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005652 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005653 list_append_number(l, (varnumber_T)llpos->col);
5654 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005655 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005656 sprintf(buf, "pos%d", i + 1);
5657 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005658 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 else
5661 {
5662 dict_add_string(dict, "pattern", cur->pattern);
5663 }
5664 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5665 dict_add_number(dict, "priority", (long)cur->priority);
5666 dict_add_number(dict, "id", (long)cur->id);
5667# if defined(FEAT_CONCEAL)
5668 if (cur->conceal_char)
5669 {
5670 char_u buf[MB_MAXBYTES + 1];
5671
5672 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5673 dict_add_string(dict, "conceal", (char_u *)&buf);
5674 }
5675# endif
5676 list_append_dict(rettv->vval.v_list, dict);
5677 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 }
5679#endif
5680}
5681
5682/*
5683 * "getpid()" function
5684 */
5685 static void
5686f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5687{
5688 rettv->vval.v_number = mch_get_pid();
5689}
5690
5691 static void
5692getpos_both(
5693 typval_T *argvars,
5694 typval_T *rettv,
5695 int getcurpos)
5696{
5697 pos_T *fp;
5698 list_T *l;
5699 int fnum = -1;
5700
5701 if (rettv_list_alloc(rettv) == OK)
5702 {
5703 l = rettv->vval.v_list;
5704 if (getcurpos)
5705 fp = &curwin->w_cursor;
5706 else
5707 fp = var2fpos(&argvars[0], TRUE, &fnum);
5708 if (fnum != -1)
5709 list_append_number(l, (varnumber_T)fnum);
5710 else
5711 list_append_number(l, (varnumber_T)0);
5712 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5713 : (varnumber_T)0);
5714 list_append_number(l, (fp != NULL)
5715 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5716 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005717 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005718 (varnumber_T)0);
5719 if (getcurpos)
5720 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005721 int save_set_curswant = curwin->w_set_curswant;
5722 colnr_T save_curswant = curwin->w_curswant;
5723 colnr_T save_virtcol = curwin->w_virtcol;
5724
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005725 update_curswant();
5726 list_append_number(l, curwin->w_curswant == MAXCOL ?
5727 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005728
5729 // Do not change "curswant", as it is unexpected that a get
5730 // function has a side effect.
5731 if (save_set_curswant)
5732 {
5733 curwin->w_set_curswant = save_set_curswant;
5734 curwin->w_curswant = save_curswant;
5735 curwin->w_virtcol = save_virtcol;
5736 curwin->w_valid &= ~VALID_VIRTCOL;
5737 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 }
5739 }
5740 else
5741 rettv->vval.v_number = FALSE;
5742}
5743
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005744/*
5745 * "getcurpos()" function
5746 */
5747 static void
5748f_getcurpos(typval_T *argvars, typval_T *rettv)
5749{
5750 getpos_both(argvars, rettv, TRUE);
5751}
5752
5753/*
5754 * "getpos(string)" function
5755 */
5756 static void
5757f_getpos(typval_T *argvars, typval_T *rettv)
5758{
5759 getpos_both(argvars, rettv, FALSE);
5760}
5761
5762/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005763 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764 */
5765 static void
5766f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5767{
5768#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005769 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005770#endif
5771}
5772
5773/*
5774 * "getreg()" function
5775 */
5776 static void
5777f_getreg(typval_T *argvars, typval_T *rettv)
5778{
5779 char_u *strregname;
5780 int regname;
5781 int arg2 = FALSE;
5782 int return_list = FALSE;
5783 int error = FALSE;
5784
5785 if (argvars[0].v_type != VAR_UNKNOWN)
5786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005787 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788 error = strregname == NULL;
5789 if (argvars[1].v_type != VAR_UNKNOWN)
5790 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005791 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005792 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005793 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005794 }
5795 }
5796 else
5797 strregname = get_vim_var_str(VV_REG);
5798
5799 if (error)
5800 return;
5801
5802 regname = (strregname == NULL ? '"' : *strregname);
5803 if (regname == 0)
5804 regname = '"';
5805
5806 if (return_list)
5807 {
5808 rettv->v_type = VAR_LIST;
5809 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5810 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5811 if (rettv->vval.v_list == NULL)
5812 (void)rettv_list_alloc(rettv);
5813 else
5814 ++rettv->vval.v_list->lv_refcount;
5815 }
5816 else
5817 {
5818 rettv->v_type = VAR_STRING;
5819 rettv->vval.v_string = get_reg_contents(regname,
5820 arg2 ? GREG_EXPR_SRC : 0);
5821 }
5822}
5823
5824/*
5825 * "getregtype()" function
5826 */
5827 static void
5828f_getregtype(typval_T *argvars, typval_T *rettv)
5829{
5830 char_u *strregname;
5831 int regname;
5832 char_u buf[NUMBUFLEN + 2];
5833 long reglen = 0;
5834
5835 if (argvars[0].v_type != VAR_UNKNOWN)
5836 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005837 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005838 if (strregname == NULL) /* type error; errmsg already given */
5839 {
5840 rettv->v_type = VAR_STRING;
5841 rettv->vval.v_string = NULL;
5842 return;
5843 }
5844 }
5845 else
5846 /* Default to v:register */
5847 strregname = get_vim_var_str(VV_REG);
5848
5849 regname = (strregname == NULL ? '"' : *strregname);
5850 if (regname == 0)
5851 regname = '"';
5852
5853 buf[0] = NUL;
5854 buf[1] = NUL;
5855 switch (get_reg_type(regname, &reglen))
5856 {
5857 case MLINE: buf[0] = 'V'; break;
5858 case MCHAR: buf[0] = 'v'; break;
5859 case MBLOCK:
5860 buf[0] = Ctrl_V;
5861 sprintf((char *)buf + 1, "%ld", reglen + 1);
5862 break;
5863 }
5864 rettv->v_type = VAR_STRING;
5865 rettv->vval.v_string = vim_strsave(buf);
5866}
5867
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005868/*
5869 * Returns information (variables, options, etc.) about a tab page
5870 * as a dictionary.
5871 */
5872 static dict_T *
5873get_tabpage_info(tabpage_T *tp, int tp_idx)
5874{
5875 win_T *wp;
5876 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005877 list_T *l;
5878
5879 dict = dict_alloc();
5880 if (dict == NULL)
5881 return NULL;
5882
Bram Moolenaare0be1672018-07-08 16:50:37 +02005883 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005884
5885 l = list_alloc();
5886 if (l != NULL)
5887 {
5888 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5889 wp; wp = wp->w_next)
5890 list_append_number(l, (varnumber_T)wp->w_id);
5891 dict_add_list(dict, "windows", l);
5892 }
5893
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005894 /* Make a reference to tabpage variables */
5895 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005896
5897 return dict;
5898}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005899
5900/*
5901 * "gettabinfo()" function
5902 */
5903 static void
5904f_gettabinfo(typval_T *argvars, typval_T *rettv)
5905{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005906 tabpage_T *tp, *tparg = NULL;
5907 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005908 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005909
5910 if (rettv_list_alloc(rettv) != OK)
5911 return;
5912
5913 if (argvars[0].v_type != VAR_UNKNOWN)
5914 {
5915 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005916 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005917 if (tparg == NULL)
5918 return;
5919 }
5920
5921 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005922 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005923 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005924 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005925 if (tparg != NULL && tp != tparg)
5926 continue;
5927 d = get_tabpage_info(tp, tpnr);
5928 if (d != NULL)
5929 list_append_dict(rettv->vval.v_list, d);
5930 if (tparg != NULL)
5931 return;
5932 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005933}
5934
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005935/*
5936 * "gettabvar()" function
5937 */
5938 static void
5939f_gettabvar(typval_T *argvars, typval_T *rettv)
5940{
5941 win_T *oldcurwin;
5942 tabpage_T *tp, *oldtabpage;
5943 dictitem_T *v;
5944 char_u *varname;
5945 int done = FALSE;
5946
5947 rettv->v_type = VAR_STRING;
5948 rettv->vval.v_string = NULL;
5949
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005950 varname = tv_get_string_chk(&argvars[1]);
5951 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005952 if (tp != NULL && varname != NULL)
5953 {
5954 /* Set tp to be our tabpage, temporarily. Also set the window to the
5955 * first window in the tabpage, otherwise the window is not valid. */
5956 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005957 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5958 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005959 {
5960 /* look up the variable */
5961 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5962 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5963 if (v != NULL)
5964 {
5965 copy_tv(&v->di_tv, rettv);
5966 done = TRUE;
5967 }
5968 }
5969
5970 /* restore previous notion of curwin */
5971 restore_win(oldcurwin, oldtabpage, TRUE);
5972 }
5973
5974 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5975 copy_tv(&argvars[2], rettv);
5976}
5977
5978/*
5979 * "gettabwinvar()" function
5980 */
5981 static void
5982f_gettabwinvar(typval_T *argvars, typval_T *rettv)
5983{
5984 getwinvar(argvars, rettv, 1);
5985}
5986
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005987/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01005988 * "gettagstack()" function
5989 */
5990 static void
5991f_gettagstack(typval_T *argvars, typval_T *rettv)
5992{
5993 win_T *wp = curwin; // default is current window
5994
5995 if (rettv_dict_alloc(rettv) != OK)
5996 return;
5997
5998 if (argvars[0].v_type != VAR_UNKNOWN)
5999 {
6000 wp = find_win_by_nr_or_id(&argvars[0]);
6001 if (wp == NULL)
6002 return;
6003 }
6004
6005 get_tagstack(wp, rettv->vval.v_dict);
6006}
6007
6008/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006009 * Returns information about a window as a dictionary.
6010 */
6011 static dict_T *
6012get_win_info(win_T *wp, short tpnr, short winnr)
6013{
6014 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006015
6016 dict = dict_alloc();
6017 if (dict == NULL)
6018 return NULL;
6019
Bram Moolenaare0be1672018-07-08 16:50:37 +02006020 dict_add_number(dict, "tabnr", tpnr);
6021 dict_add_number(dict, "winnr", winnr);
6022 dict_add_number(dict, "winid", wp->w_id);
6023 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006024 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006025 dict_add_number(dict, "topline", wp->w_topline);
6026 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006027#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006028 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006029#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006030 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006031 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006032 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006033
Bram Moolenaar69905d12017-08-13 18:14:47 +02006034#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006035 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006036#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006037#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006038 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6039 dict_add_number(dict, "loclist",
6040 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006041#endif
6042
Bram Moolenaar30567352016-08-27 21:25:44 +02006043 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006044 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006045
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006046 return dict;
6047}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006048
6049/*
6050 * "getwininfo()" function
6051 */
6052 static void
6053f_getwininfo(typval_T *argvars, typval_T *rettv)
6054{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006055 tabpage_T *tp;
6056 win_T *wp = NULL, *wparg = NULL;
6057 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006058 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006059
6060 if (rettv_list_alloc(rettv) != OK)
6061 return;
6062
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006063 if (argvars[0].v_type != VAR_UNKNOWN)
6064 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006065 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066 if (wparg == NULL)
6067 return;
6068 }
6069
6070 /* Collect information about either all the windows across all the tab
6071 * pages or one particular window.
6072 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006073 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006074 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006075 tabnr++;
6076 winnr = 0;
6077 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006078 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006079 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006080 if (wparg != NULL && wp != wparg)
6081 continue;
6082 d = get_win_info(wp, tabnr, winnr);
6083 if (d != NULL)
6084 list_append_dict(rettv->vval.v_list, d);
6085 if (wparg != NULL)
6086 /* found information about a specific window */
6087 return;
6088 }
6089 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006090}
6091
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006092/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006093 * "win_findbuf()" function
6094 */
6095 static void
6096f_win_findbuf(typval_T *argvars, typval_T *rettv)
6097{
6098 if (rettv_list_alloc(rettv) != FAIL)
6099 win_findbuf(argvars, rettv->vval.v_list);
6100}
6101
6102/*
6103 * "win_getid()" function
6104 */
6105 static void
6106f_win_getid(typval_T *argvars, typval_T *rettv)
6107{
6108 rettv->vval.v_number = win_getid(argvars);
6109}
6110
6111/*
6112 * "win_gotoid()" function
6113 */
6114 static void
6115f_win_gotoid(typval_T *argvars, typval_T *rettv)
6116{
6117 rettv->vval.v_number = win_gotoid(argvars);
6118}
6119
6120/*
6121 * "win_id2tabwin()" function
6122 */
6123 static void
6124f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6125{
6126 if (rettv_list_alloc(rettv) != FAIL)
6127 win_id2tabwin(argvars, rettv->vval.v_list);
6128}
6129
6130/*
6131 * "win_id2win()" function
6132 */
6133 static void
6134f_win_id2win(typval_T *argvars, typval_T *rettv)
6135{
6136 rettv->vval.v_number = win_id2win(argvars);
6137}
6138
6139/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006140 * "win_screenpos()" function
6141 */
6142 static void
6143f_win_screenpos(typval_T *argvars, typval_T *rettv)
6144{
6145 win_T *wp;
6146
6147 if (rettv_list_alloc(rettv) == FAIL)
6148 return;
6149
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006150 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006151 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6152 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6153}
6154
6155/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006156 * "getwinpos({timeout})" function
6157 */
6158 static void
6159f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6160{
6161 int x = -1;
6162 int y = -1;
6163
6164 if (rettv_list_alloc(rettv) == FAIL)
6165 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006166#if defined(FEAT_GUI) \
6167 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6168 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006169 {
6170 varnumber_T timeout = 100;
6171
6172 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006173 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006174
6175 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006176 }
6177#endif
6178 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6179 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6180}
6181
6182
6183/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006184 * "getwinposx()" function
6185 */
6186 static void
6187f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6188{
6189 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006190#if defined(FEAT_GUI) \
6191 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6192 || defined(MSWIN)
6193
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006194 {
6195 int x, y;
6196
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006197 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006198 rettv->vval.v_number = x;
6199 }
6200#endif
6201}
6202
6203/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006204 * "getwinposy()" function
6205 */
6206 static void
6207f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6208{
6209 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006210#if defined(FEAT_GUI) \
6211 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6212 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006213 {
6214 int x, y;
6215
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006216 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006217 rettv->vval.v_number = y;
6218 }
6219#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220}
6221
6222/*
6223 * "getwinvar()" function
6224 */
6225 static void
6226f_getwinvar(typval_T *argvars, typval_T *rettv)
6227{
6228 getwinvar(argvars, rettv, 0);
6229}
6230
6231/*
6232 * "glob()" function
6233 */
6234 static void
6235f_glob(typval_T *argvars, typval_T *rettv)
6236{
6237 int options = WILD_SILENT|WILD_USE_NL;
6238 expand_T xpc;
6239 int error = FALSE;
6240
6241 /* When the optional second argument is non-zero, don't remove matches
6242 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6243 rettv->v_type = VAR_STRING;
6244 if (argvars[1].v_type != VAR_UNKNOWN)
6245 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006246 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 options |= WILD_KEEP_ALL;
6248 if (argvars[2].v_type != VAR_UNKNOWN)
6249 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006250 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006251 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006252 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006253 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 options |= WILD_ALLLINKS;
6255 }
6256 }
6257 if (!error)
6258 {
6259 ExpandInit(&xpc);
6260 xpc.xp_context = EXPAND_FILES;
6261 if (p_wic)
6262 options += WILD_ICASE;
6263 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006264 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006265 NULL, options, WILD_ALL);
6266 else if (rettv_list_alloc(rettv) != FAIL)
6267 {
6268 int i;
6269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006270 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006271 NULL, options, WILD_ALL_KEEP);
6272 for (i = 0; i < xpc.xp_numfiles; i++)
6273 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6274
6275 ExpandCleanup(&xpc);
6276 }
6277 }
6278 else
6279 rettv->vval.v_string = NULL;
6280}
6281
6282/*
6283 * "globpath()" function
6284 */
6285 static void
6286f_globpath(typval_T *argvars, typval_T *rettv)
6287{
6288 int flags = 0;
6289 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006290 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 int error = FALSE;
6292 garray_T ga;
6293 int i;
6294
6295 /* When the optional second argument is non-zero, don't remove matches
6296 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6297 rettv->v_type = VAR_STRING;
6298 if (argvars[2].v_type != VAR_UNKNOWN)
6299 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006300 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 flags |= WILD_KEEP_ALL;
6302 if (argvars[3].v_type != VAR_UNKNOWN)
6303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006304 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006305 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006306 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 flags |= WILD_ALLLINKS;
6309 }
6310 }
6311 if (file != NULL && !error)
6312 {
6313 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006314 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006315 if (rettv->v_type == VAR_STRING)
6316 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6317 else if (rettv_list_alloc(rettv) != FAIL)
6318 for (i = 0; i < ga.ga_len; ++i)
6319 list_append_string(rettv->vval.v_list,
6320 ((char_u **)(ga.ga_data))[i], -1);
6321 ga_clear_strings(&ga);
6322 }
6323 else
6324 rettv->vval.v_string = NULL;
6325}
6326
6327/*
6328 * "glob2regpat()" function
6329 */
6330 static void
6331f_glob2regpat(typval_T *argvars, typval_T *rettv)
6332{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334
6335 rettv->v_type = VAR_STRING;
6336 rettv->vval.v_string = (pat == NULL)
6337 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6338}
6339
6340/* for VIM_VERSION_ defines */
6341#include "version.h"
6342
6343/*
6344 * "has()" function
6345 */
6346 static void
6347f_has(typval_T *argvars, typval_T *rettv)
6348{
6349 int i;
6350 char_u *name;
6351 int n = FALSE;
6352 static char *(has_list[]) =
6353 {
6354#ifdef AMIGA
6355 "amiga",
6356# ifdef FEAT_ARP
6357 "arp",
6358# endif
6359#endif
6360#ifdef __BEOS__
6361 "beos",
6362#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006363#if defined(BSD) && !defined(MACOS_X)
6364 "bsd",
6365#endif
6366#ifdef hpux
6367 "hpux",
6368#endif
6369#ifdef __linux__
6370 "linux",
6371#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006372#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006373 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6374 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006375# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006376 "macunix", /* Mac OS X, with the darwin feature */
6377 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006378# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006379#endif
6380#ifdef __QNX__
6381 "qnx",
6382#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006383#ifdef SUN_SYSTEM
6384 "sun",
6385#else
6386 "moon",
6387#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006388#ifdef UNIX
6389 "unix",
6390#endif
6391#ifdef VMS
6392 "vms",
6393#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006394#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006395 "win32",
6396#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006397#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006398 "win32unix",
6399#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006400#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006401 "win64",
6402#endif
6403#ifdef EBCDIC
6404 "ebcdic",
6405#endif
6406#ifndef CASE_INSENSITIVE_FILENAME
6407 "fname_case",
6408#endif
6409#ifdef HAVE_ACL
6410 "acl",
6411#endif
6412#ifdef FEAT_ARABIC
6413 "arabic",
6414#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006415 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006416#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006417 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006418#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006419#ifdef FEAT_AUTOSERVERNAME
6420 "autoservername",
6421#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006422#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006423 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006424# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006425 "balloon_multiline",
6426# endif
6427#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006428#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006429 "balloon_eval_term",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6432 "builtin_terms",
6433# ifdef ALL_BUILTIN_TCAPS
6434 "all_builtin_terms",
6435# endif
6436#endif
6437#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006438 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 || defined(FEAT_GUI_MOTIF))
6440 "browsefilter",
6441#endif
6442#ifdef FEAT_BYTEOFF
6443 "byte_offset",
6444#endif
6445#ifdef FEAT_JOB_CHANNEL
6446 "channel",
6447#endif
6448#ifdef FEAT_CINDENT
6449 "cindent",
6450#endif
6451#ifdef FEAT_CLIENTSERVER
6452 "clientserver",
6453#endif
6454#ifdef FEAT_CLIPBOARD
6455 "clipboard",
6456#endif
6457#ifdef FEAT_CMDL_COMPL
6458 "cmdline_compl",
6459#endif
6460#ifdef FEAT_CMDHIST
6461 "cmdline_hist",
6462#endif
6463#ifdef FEAT_COMMENTS
6464 "comments",
6465#endif
6466#ifdef FEAT_CONCEAL
6467 "conceal",
6468#endif
6469#ifdef FEAT_CRYPT
6470 "cryptv",
6471 "crypt-blowfish",
6472 "crypt-blowfish2",
6473#endif
6474#ifdef FEAT_CSCOPE
6475 "cscope",
6476#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006477 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006478#ifdef CURSOR_SHAPE
6479 "cursorshape",
6480#endif
6481#ifdef DEBUG
6482 "debug",
6483#endif
6484#ifdef FEAT_CON_DIALOG
6485 "dialog_con",
6486#endif
6487#ifdef FEAT_GUI_DIALOG
6488 "dialog_gui",
6489#endif
6490#ifdef FEAT_DIFF
6491 "diff",
6492#endif
6493#ifdef FEAT_DIGRAPHS
6494 "digraphs",
6495#endif
6496#ifdef FEAT_DIRECTX
6497 "directx",
6498#endif
6499#ifdef FEAT_DND
6500 "dnd",
6501#endif
6502#ifdef FEAT_EMACS_TAGS
6503 "emacs_tags",
6504#endif
6505 "eval", /* always present, of course! */
6506 "ex_extra", /* graduated feature */
6507#ifdef FEAT_SEARCH_EXTRA
6508 "extra_search",
6509#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006510#ifdef FEAT_SEARCHPATH
6511 "file_in_path",
6512#endif
6513#ifdef FEAT_FILTERPIPE
6514 "filterpipe",
6515#endif
6516#ifdef FEAT_FIND_ID
6517 "find_in_path",
6518#endif
6519#ifdef FEAT_FLOAT
6520 "float",
6521#endif
6522#ifdef FEAT_FOLDING
6523 "folding",
6524#endif
6525#ifdef FEAT_FOOTER
6526 "footer",
6527#endif
6528#if !defined(USE_SYSTEM) && defined(UNIX)
6529 "fork",
6530#endif
6531#ifdef FEAT_GETTEXT
6532 "gettext",
6533#endif
6534#ifdef FEAT_GUI
6535 "gui",
6536#endif
6537#ifdef FEAT_GUI_ATHENA
6538# ifdef FEAT_GUI_NEXTAW
6539 "gui_neXtaw",
6540# else
6541 "gui_athena",
6542# endif
6543#endif
6544#ifdef FEAT_GUI_GTK
6545 "gui_gtk",
6546# ifdef USE_GTK3
6547 "gui_gtk3",
6548# else
6549 "gui_gtk2",
6550# endif
6551#endif
6552#ifdef FEAT_GUI_GNOME
6553 "gui_gnome",
6554#endif
6555#ifdef FEAT_GUI_MAC
6556 "gui_mac",
6557#endif
6558#ifdef FEAT_GUI_MOTIF
6559 "gui_motif",
6560#endif
6561#ifdef FEAT_GUI_PHOTON
6562 "gui_photon",
6563#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006564#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006565 "gui_win32",
6566#endif
6567#ifdef FEAT_HANGULIN
6568 "hangul_input",
6569#endif
6570#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6571 "iconv",
6572#endif
6573#ifdef FEAT_INS_EXPAND
6574 "insert_expand",
6575#endif
6576#ifdef FEAT_JOB_CHANNEL
6577 "job",
6578#endif
6579#ifdef FEAT_JUMPLIST
6580 "jumplist",
6581#endif
6582#ifdef FEAT_KEYMAP
6583 "keymap",
6584#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006585 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006586#ifdef FEAT_LANGMAP
6587 "langmap",
6588#endif
6589#ifdef FEAT_LIBCALL
6590 "libcall",
6591#endif
6592#ifdef FEAT_LINEBREAK
6593 "linebreak",
6594#endif
6595#ifdef FEAT_LISP
6596 "lispindent",
6597#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006598 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006599#ifdef FEAT_LOCALMAP
6600 "localmap",
6601#endif
6602#ifdef FEAT_LUA
6603# ifndef DYNAMIC_LUA
6604 "lua",
6605# endif
6606#endif
6607#ifdef FEAT_MENU
6608 "menu",
6609#endif
6610#ifdef FEAT_SESSION
6611 "mksession",
6612#endif
6613#ifdef FEAT_MODIFY_FNAME
6614 "modify_fname",
6615#endif
6616#ifdef FEAT_MOUSE
6617 "mouse",
6618#endif
6619#ifdef FEAT_MOUSESHAPE
6620 "mouseshape",
6621#endif
6622#if defined(UNIX) || defined(VMS)
6623# ifdef FEAT_MOUSE_DEC
6624 "mouse_dec",
6625# endif
6626# ifdef FEAT_MOUSE_GPM
6627 "mouse_gpm",
6628# endif
6629# ifdef FEAT_MOUSE_JSB
6630 "mouse_jsbterm",
6631# endif
6632# ifdef FEAT_MOUSE_NET
6633 "mouse_netterm",
6634# endif
6635# ifdef FEAT_MOUSE_PTERM
6636 "mouse_pterm",
6637# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006638# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006639 "mouse_sgr",
6640# endif
6641# ifdef FEAT_SYSMOUSE
6642 "mouse_sysmouse",
6643# endif
6644# ifdef FEAT_MOUSE_URXVT
6645 "mouse_urxvt",
6646# endif
6647# ifdef FEAT_MOUSE_XTERM
6648 "mouse_xterm",
6649# endif
6650#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006651 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006652#ifdef FEAT_MBYTE_IME
6653 "multi_byte_ime",
6654#endif
6655#ifdef FEAT_MULTI_LANG
6656 "multi_lang",
6657#endif
6658#ifdef FEAT_MZSCHEME
6659#ifndef DYNAMIC_MZSCHEME
6660 "mzscheme",
6661#endif
6662#endif
6663#ifdef FEAT_NUM64
6664 "num64",
6665#endif
6666#ifdef FEAT_OLE
6667 "ole",
6668#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006669#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006670 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006671#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006672#ifdef FEAT_PATH_EXTRA
6673 "path_extra",
6674#endif
6675#ifdef FEAT_PERL
6676#ifndef DYNAMIC_PERL
6677 "perl",
6678#endif
6679#endif
6680#ifdef FEAT_PERSISTENT_UNDO
6681 "persistent_undo",
6682#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006683#if defined(FEAT_PYTHON)
6684 "python_compiled",
6685# if defined(DYNAMIC_PYTHON)
6686 "python_dynamic",
6687# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006688 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006689 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006690# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006691#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006692#if defined(FEAT_PYTHON3)
6693 "python3_compiled",
6694# if defined(DYNAMIC_PYTHON3)
6695 "python3_dynamic",
6696# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006697 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006698 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006699# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006700#endif
6701#ifdef FEAT_POSTSCRIPT
6702 "postscript",
6703#endif
6704#ifdef FEAT_PRINTER
6705 "printer",
6706#endif
6707#ifdef FEAT_PROFILE
6708 "profile",
6709#endif
6710#ifdef FEAT_RELTIME
6711 "reltime",
6712#endif
6713#ifdef FEAT_QUICKFIX
6714 "quickfix",
6715#endif
6716#ifdef FEAT_RIGHTLEFT
6717 "rightleft",
6718#endif
6719#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6720 "ruby",
6721#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006722 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006723#ifdef FEAT_CMDL_INFO
6724 "showcmd",
6725 "cmdline_info",
6726#endif
6727#ifdef FEAT_SIGNS
6728 "signs",
6729#endif
6730#ifdef FEAT_SMARTINDENT
6731 "smartindent",
6732#endif
6733#ifdef STARTUPTIME
6734 "startuptime",
6735#endif
6736#ifdef FEAT_STL_OPT
6737 "statusline",
6738#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739#ifdef FEAT_NETBEANS_INTG
6740 "netbeans_intg",
6741#endif
6742#ifdef FEAT_SPELL
6743 "spell",
6744#endif
6745#ifdef FEAT_SYN_HL
6746 "syntax",
6747#endif
6748#if defined(USE_SYSTEM) || !defined(UNIX)
6749 "system",
6750#endif
6751#ifdef FEAT_TAG_BINS
6752 "tag_binary",
6753#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006754#ifdef FEAT_TCL
6755# ifndef DYNAMIC_TCL
6756 "tcl",
6757# endif
6758#endif
6759#ifdef FEAT_TERMGUICOLORS
6760 "termguicolors",
6761#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006762#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006763 "terminal",
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef TERMINFO
6766 "terminfo",
6767#endif
6768#ifdef FEAT_TERMRESPONSE
6769 "termresponse",
6770#endif
6771#ifdef FEAT_TEXTOBJ
6772 "textobjects",
6773#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006774#ifdef FEAT_TEXT_PROP
6775 "textprop",
6776#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006777#ifdef HAVE_TGETENT
6778 "tgetent",
6779#endif
6780#ifdef FEAT_TIMERS
6781 "timers",
6782#endif
6783#ifdef FEAT_TITLE
6784 "title",
6785#endif
6786#ifdef FEAT_TOOLBAR
6787 "toolbar",
6788#endif
6789#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6790 "unnamedplus",
6791#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006792 "user-commands", /* was accidentally included in 5.4 */
6793 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006794#ifdef FEAT_VARTABS
6795 "vartabs",
6796#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006797 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006798#ifdef FEAT_VIMINFO
6799 "viminfo",
6800#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006801 "vimscript-1",
6802 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006803 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006804 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006805 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006806 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006808#ifdef FEAT_VTP
6809 "vtp",
6810#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006811#ifdef FEAT_WILDIGN
6812 "wildignore",
6813#endif
6814#ifdef FEAT_WILDMENU
6815 "wildmenu",
6816#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006817 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006818#ifdef FEAT_WAK
6819 "winaltkeys",
6820#endif
6821#ifdef FEAT_WRITEBACKUP
6822 "writebackup",
6823#endif
6824#ifdef FEAT_XIM
6825 "xim",
6826#endif
6827#ifdef FEAT_XFONTSET
6828 "xfontset",
6829#endif
6830#ifdef FEAT_XPM_W32
6831 "xpm",
6832 "xpm_w32", /* for backward compatibility */
6833#else
6834# if defined(HAVE_XPM)
6835 "xpm",
6836# endif
6837#endif
6838#ifdef USE_XSMP
6839 "xsmp",
6840#endif
6841#ifdef USE_XSMP_INTERACT
6842 "xsmp_interact",
6843#endif
6844#ifdef FEAT_XCLIPBOARD
6845 "xterm_clipboard",
6846#endif
6847#ifdef FEAT_XTERM_SAVE
6848 "xterm_save",
6849#endif
6850#if defined(UNIX) && defined(FEAT_X11)
6851 "X11",
6852#endif
6853 NULL
6854 };
6855
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006856 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006857 for (i = 0; has_list[i] != NULL; ++i)
6858 if (STRICMP(name, has_list[i]) == 0)
6859 {
6860 n = TRUE;
6861 break;
6862 }
6863
6864 if (n == FALSE)
6865 {
6866 if (STRNICMP(name, "patch", 5) == 0)
6867 {
6868 if (name[5] == '-'
6869 && STRLEN(name) >= 11
6870 && vim_isdigit(name[6])
6871 && vim_isdigit(name[8])
6872 && vim_isdigit(name[10]))
6873 {
6874 int major = atoi((char *)name + 6);
6875 int minor = atoi((char *)name + 8);
6876
6877 /* Expect "patch-9.9.01234". */
6878 n = (major < VIM_VERSION_MAJOR
6879 || (major == VIM_VERSION_MAJOR
6880 && (minor < VIM_VERSION_MINOR
6881 || (minor == VIM_VERSION_MINOR
6882 && has_patch(atoi((char *)name + 10))))));
6883 }
6884 else
6885 n = has_patch(atoi((char *)name + 5));
6886 }
6887 else if (STRICMP(name, "vim_starting") == 0)
6888 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006889 else if (STRICMP(name, "ttyin") == 0)
6890 n = mch_input_isatty();
6891 else if (STRICMP(name, "ttyout") == 0)
6892 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006893 else if (STRICMP(name, "multi_byte_encoding") == 0)
6894 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006895#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006896 else if (STRICMP(name, "balloon_multiline") == 0)
6897 n = multiline_balloon_available();
6898#endif
6899#ifdef DYNAMIC_TCL
6900 else if (STRICMP(name, "tcl") == 0)
6901 n = tcl_enabled(FALSE);
6902#endif
6903#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6904 else if (STRICMP(name, "iconv") == 0)
6905 n = iconv_enabled(FALSE);
6906#endif
6907#ifdef DYNAMIC_LUA
6908 else if (STRICMP(name, "lua") == 0)
6909 n = lua_enabled(FALSE);
6910#endif
6911#ifdef DYNAMIC_MZSCHEME
6912 else if (STRICMP(name, "mzscheme") == 0)
6913 n = mzscheme_enabled(FALSE);
6914#endif
6915#ifdef DYNAMIC_RUBY
6916 else if (STRICMP(name, "ruby") == 0)
6917 n = ruby_enabled(FALSE);
6918#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006919#ifdef DYNAMIC_PYTHON
6920 else if (STRICMP(name, "python") == 0)
6921 n = python_enabled(FALSE);
6922#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006923#ifdef DYNAMIC_PYTHON3
6924 else if (STRICMP(name, "python3") == 0)
6925 n = python3_enabled(FALSE);
6926#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006927#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6928 else if (STRICMP(name, "pythonx") == 0)
6929 {
6930# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6931 if (p_pyx == 0)
6932 n = python3_enabled(FALSE) || python_enabled(FALSE);
6933 else if (p_pyx == 3)
6934 n = python3_enabled(FALSE);
6935 else if (p_pyx == 2)
6936 n = python_enabled(FALSE);
6937# elif defined(DYNAMIC_PYTHON)
6938 n = python_enabled(FALSE);
6939# elif defined(DYNAMIC_PYTHON3)
6940 n = python3_enabled(FALSE);
6941# endif
6942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006943#endif
6944#ifdef DYNAMIC_PERL
6945 else if (STRICMP(name, "perl") == 0)
6946 n = perl_enabled(FALSE);
6947#endif
6948#ifdef FEAT_GUI
6949 else if (STRICMP(name, "gui_running") == 0)
6950 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006951# ifdef FEAT_BROWSE
6952 else if (STRICMP(name, "browse") == 0)
6953 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6954# endif
6955#endif
6956#ifdef FEAT_SYN_HL
6957 else if (STRICMP(name, "syntax_items") == 0)
6958 n = syntax_present(curwin);
6959#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01006960#ifdef FEAT_VTP
6961 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02006962 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006963#endif
6964#ifdef FEAT_NETBEANS_INTG
6965 else if (STRICMP(name, "netbeans_enabled") == 0)
6966 n = netbeans_active();
6967#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02006968#ifdef FEAT_MOUSE_GPM
6969 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
6970 n = gpm_enabled();
6971#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006972#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02006973 else if (STRICMP(name, "terminal") == 0)
6974 n = terminal_enabled();
6975#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006976#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006977 else if (STRICMP(name, "conpty") == 0)
6978 n = use_conpty();
6979#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006980 }
6981
6982 rettv->vval.v_number = n;
6983}
6984
6985/*
6986 * "has_key()" function
6987 */
6988 static void
6989f_has_key(typval_T *argvars, typval_T *rettv)
6990{
6991 if (argvars[0].v_type != VAR_DICT)
6992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006993 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994 return;
6995 }
6996 if (argvars[0].vval.v_dict == NULL)
6997 return;
6998
6999 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007000 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007001}
7002
7003/*
7004 * "haslocaldir()" function
7005 */
7006 static void
7007f_haslocaldir(typval_T *argvars, typval_T *rettv)
7008{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007009 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007010 win_T *wp = NULL;
7011
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007012 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7013
7014 // Check for window-local and tab-local directories
7015 if (wp != NULL && wp->w_localdir != NULL)
7016 rettv->vval.v_number = 1;
7017 else if (tp != NULL && tp->tp_localdir != NULL)
7018 rettv->vval.v_number = 2;
7019 else
7020 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007021}
7022
7023/*
7024 * "hasmapto()" function
7025 */
7026 static void
7027f_hasmapto(typval_T *argvars, typval_T *rettv)
7028{
7029 char_u *name;
7030 char_u *mode;
7031 char_u buf[NUMBUFLEN];
7032 int abbr = FALSE;
7033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007034 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007035 if (argvars[1].v_type == VAR_UNKNOWN)
7036 mode = (char_u *)"nvo";
7037 else
7038 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007039 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007040 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007041 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007042 }
7043
7044 if (map_to_exists(name, mode, abbr))
7045 rettv->vval.v_number = TRUE;
7046 else
7047 rettv->vval.v_number = FALSE;
7048}
7049
7050/*
7051 * "histadd()" function
7052 */
7053 static void
7054f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7055{
7056#ifdef FEAT_CMDHIST
7057 int histype;
7058 char_u *str;
7059 char_u buf[NUMBUFLEN];
7060#endif
7061
7062 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007063 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064 return;
7065#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007066 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007067 histype = str != NULL ? get_histtype(str) : -1;
7068 if (histype >= 0)
7069 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007070 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007071 if (*str != NUL)
7072 {
7073 init_history();
7074 add_to_history(histype, str, FALSE, NUL);
7075 rettv->vval.v_number = TRUE;
7076 return;
7077 }
7078 }
7079#endif
7080}
7081
7082/*
7083 * "histdel()" function
7084 */
7085 static void
7086f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7087{
7088#ifdef FEAT_CMDHIST
7089 int n;
7090 char_u buf[NUMBUFLEN];
7091 char_u *str;
7092
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007093 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007094 if (str == NULL)
7095 n = 0;
7096 else if (argvars[1].v_type == VAR_UNKNOWN)
7097 /* only one argument: clear entire history */
7098 n = clr_history(get_histtype(str));
7099 else if (argvars[1].v_type == VAR_NUMBER)
7100 /* index given: remove that entry */
7101 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007102 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007103 else
7104 /* string given: remove all matching entries */
7105 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007106 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 rettv->vval.v_number = n;
7108#endif
7109}
7110
7111/*
7112 * "histget()" function
7113 */
7114 static void
7115f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7116{
7117#ifdef FEAT_CMDHIST
7118 int type;
7119 int idx;
7120 char_u *str;
7121
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007122 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007123 if (str == NULL)
7124 rettv->vval.v_string = NULL;
7125 else
7126 {
7127 type = get_histtype(str);
7128 if (argvars[1].v_type == VAR_UNKNOWN)
7129 idx = get_history_idx(type);
7130 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007131 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007132 /* -1 on type error */
7133 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7134 }
7135#else
7136 rettv->vval.v_string = NULL;
7137#endif
7138 rettv->v_type = VAR_STRING;
7139}
7140
7141/*
7142 * "histnr()" function
7143 */
7144 static void
7145f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7146{
7147 int i;
7148
7149#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007150 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007151
7152 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7153 if (i >= HIST_CMD && i < HIST_COUNT)
7154 i = get_history_idx(i);
7155 else
7156#endif
7157 i = -1;
7158 rettv->vval.v_number = i;
7159}
7160
7161/*
7162 * "highlightID(name)" function
7163 */
7164 static void
7165f_hlID(typval_T *argvars, typval_T *rettv)
7166{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007167 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007168}
7169
7170/*
7171 * "highlight_exists()" function
7172 */
7173 static void
7174f_hlexists(typval_T *argvars, typval_T *rettv)
7175{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007176 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007177}
7178
7179/*
7180 * "hostname()" function
7181 */
7182 static void
7183f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7184{
7185 char_u hostname[256];
7186
7187 mch_get_host_name(hostname, 256);
7188 rettv->v_type = VAR_STRING;
7189 rettv->vval.v_string = vim_strsave(hostname);
7190}
7191
7192/*
7193 * iconv() function
7194 */
7195 static void
7196f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7197{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007198 char_u buf1[NUMBUFLEN];
7199 char_u buf2[NUMBUFLEN];
7200 char_u *from, *to, *str;
7201 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007202
7203 rettv->v_type = VAR_STRING;
7204 rettv->vval.v_string = NULL;
7205
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007206 str = tv_get_string(&argvars[0]);
7207 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7208 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007209 vimconv.vc_type = CONV_NONE;
7210 convert_setup(&vimconv, from, to);
7211
7212 /* If the encodings are equal, no conversion needed. */
7213 if (vimconv.vc_type == CONV_NONE)
7214 rettv->vval.v_string = vim_strsave(str);
7215 else
7216 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7217
7218 convert_setup(&vimconv, NULL, NULL);
7219 vim_free(from);
7220 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007221}
7222
7223/*
7224 * "indent()" function
7225 */
7226 static void
7227f_indent(typval_T *argvars, typval_T *rettv)
7228{
7229 linenr_T lnum;
7230
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007231 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007232 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7233 rettv->vval.v_number = get_indent_lnum(lnum);
7234 else
7235 rettv->vval.v_number = -1;
7236}
7237
7238/*
7239 * "index()" function
7240 */
7241 static void
7242f_index(typval_T *argvars, typval_T *rettv)
7243{
7244 list_T *l;
7245 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007246 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007247 long idx = 0;
7248 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007249 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007250
7251 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007252 if (argvars[0].v_type == VAR_BLOB)
7253 {
7254 typval_T tv;
7255 int start = 0;
7256
7257 if (argvars[2].v_type != VAR_UNKNOWN)
7258 {
7259 start = tv_get_number_chk(&argvars[2], &error);
7260 if (error)
7261 return;
7262 }
7263 b = argvars[0].vval.v_blob;
7264 if (b == NULL)
7265 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007266 if (start < 0)
7267 {
7268 start = blob_len(b) + start;
7269 if (start < 0)
7270 start = 0;
7271 }
7272
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007273 for (idx = start; idx < blob_len(b); ++idx)
7274 {
7275 tv.v_type = VAR_NUMBER;
7276 tv.vval.v_number = blob_get(b, idx);
7277 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7278 {
7279 rettv->vval.v_number = idx;
7280 return;
7281 }
7282 }
7283 return;
7284 }
7285 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007286 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007287 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007288 return;
7289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007291 l = argvars[0].vval.v_list;
7292 if (l != NULL)
7293 {
7294 item = l->lv_first;
7295 if (argvars[2].v_type != VAR_UNKNOWN)
7296 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007297 /* Start at specified item. Use the cached index that list_find()
7298 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007299 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007300 idx = l->lv_idx;
7301 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007302 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007303 if (error)
7304 item = NULL;
7305 }
7306
7307 for ( ; item != NULL; item = item->li_next, ++idx)
7308 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7309 {
7310 rettv->vval.v_number = idx;
7311 break;
7312 }
7313 }
7314}
7315
7316static int inputsecret_flag = 0;
7317
7318/*
7319 * "input()" function
7320 * Also handles inputsecret() when inputsecret is set.
7321 */
7322 static void
7323f_input(typval_T *argvars, typval_T *rettv)
7324{
7325 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7326}
7327
7328/*
7329 * "inputdialog()" function
7330 */
7331 static void
7332f_inputdialog(typval_T *argvars, typval_T *rettv)
7333{
7334#if defined(FEAT_GUI_TEXTDIALOG)
7335 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7336 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7337 {
7338 char_u *message;
7339 char_u buf[NUMBUFLEN];
7340 char_u *defstr = (char_u *)"";
7341
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007344 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7346 else
7347 IObuff[0] = NUL;
7348 if (message != NULL && defstr != NULL
7349 && do_dialog(VIM_QUESTION, NULL, message,
7350 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7351 rettv->vval.v_string = vim_strsave(IObuff);
7352 else
7353 {
7354 if (message != NULL && defstr != NULL
7355 && argvars[1].v_type != VAR_UNKNOWN
7356 && argvars[2].v_type != VAR_UNKNOWN)
7357 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007358 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007359 else
7360 rettv->vval.v_string = NULL;
7361 }
7362 rettv->v_type = VAR_STRING;
7363 }
7364 else
7365#endif
7366 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7367}
7368
7369/*
7370 * "inputlist()" function
7371 */
7372 static void
7373f_inputlist(typval_T *argvars, typval_T *rettv)
7374{
7375 listitem_T *li;
7376 int selected;
7377 int mouse_used;
7378
7379#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007380 /* While starting up, there is no place to enter text. When running tests
7381 * with --not-a-term we assume feedkeys() will be used. */
7382 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007383 return;
7384#endif
7385 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 return;
7389 }
7390
7391 msg_start();
7392 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7393 lines_left = Rows; /* avoid more prompt */
7394 msg_scroll = TRUE;
7395 msg_clr_eos();
7396
7397 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7398 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007399 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007400 msg_putchar('\n');
7401 }
7402
7403 /* Ask for choice. */
7404 selected = prompt_for_number(&mouse_used);
7405 if (mouse_used)
7406 selected -= lines_left;
7407
7408 rettv->vval.v_number = selected;
7409}
7410
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007411static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7412
7413/*
7414 * "inputrestore()" function
7415 */
7416 static void
7417f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7418{
7419 if (ga_userinput.ga_len > 0)
7420 {
7421 --ga_userinput.ga_len;
7422 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7423 + ga_userinput.ga_len);
7424 /* default return is zero == OK */
7425 }
7426 else if (p_verbose > 1)
7427 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007428 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007429 rettv->vval.v_number = 1; /* Failed */
7430 }
7431}
7432
7433/*
7434 * "inputsave()" function
7435 */
7436 static void
7437f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7438{
7439 /* Add an entry to the stack of typeahead storage. */
7440 if (ga_grow(&ga_userinput, 1) == OK)
7441 {
7442 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7443 + ga_userinput.ga_len);
7444 ++ga_userinput.ga_len;
7445 /* default return is zero == OK */
7446 }
7447 else
7448 rettv->vval.v_number = 1; /* Failed */
7449}
7450
7451/*
7452 * "inputsecret()" function
7453 */
7454 static void
7455f_inputsecret(typval_T *argvars, typval_T *rettv)
7456{
7457 ++cmdline_star;
7458 ++inputsecret_flag;
7459 f_input(argvars, rettv);
7460 --cmdline_star;
7461 --inputsecret_flag;
7462}
7463
7464/*
7465 * "insert()" function
7466 */
7467 static void
7468f_insert(typval_T *argvars, typval_T *rettv)
7469{
7470 long before = 0;
7471 listitem_T *item;
7472 list_T *l;
7473 int error = FALSE;
7474
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007475 if (argvars[0].v_type == VAR_BLOB)
7476 {
7477 int val, len;
7478 char_u *p;
7479
7480 len = blob_len(argvars[0].vval.v_blob);
7481 if (argvars[2].v_type != VAR_UNKNOWN)
7482 {
7483 before = (long)tv_get_number_chk(&argvars[2], &error);
7484 if (error)
7485 return; // type error; errmsg already given
7486 if (before < 0 || before > len)
7487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007489 return;
7490 }
7491 }
7492 val = tv_get_number_chk(&argvars[1], &error);
7493 if (error)
7494 return;
7495 if (val < 0 || val > 255)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007498 return;
7499 }
7500
7501 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7502 return;
7503 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7504 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7505 *(p + before) = val;
7506 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7507
7508 copy_tv(&argvars[0], rettv);
7509 }
7510 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007511 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007512 else if ((l = argvars[0].vval.v_list) != NULL
7513 && !var_check_lock(l->lv_lock,
7514 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007515 {
7516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007517 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007518 if (error)
7519 return; /* type error; errmsg already given */
7520
7521 if (before == l->lv_len)
7522 item = NULL;
7523 else
7524 {
7525 item = list_find(l, before);
7526 if (item == NULL)
7527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007528 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007529 l = NULL;
7530 }
7531 }
7532 if (l != NULL)
7533 {
7534 list_insert_tv(l, &argvars[1], item);
7535 copy_tv(&argvars[0], rettv);
7536 }
7537 }
7538}
7539
7540/*
7541 * "invert(expr)" function
7542 */
7543 static void
7544f_invert(typval_T *argvars, typval_T *rettv)
7545{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007546 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007547}
7548
7549/*
7550 * "isdirectory()" function
7551 */
7552 static void
7553f_isdirectory(typval_T *argvars, typval_T *rettv)
7554{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007555 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007556}
7557
7558/*
7559 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7560 * or it refers to a List or Dictionary that is locked.
7561 */
7562 static int
7563tv_islocked(typval_T *tv)
7564{
7565 return (tv->v_lock & VAR_LOCKED)
7566 || (tv->v_type == VAR_LIST
7567 && tv->vval.v_list != NULL
7568 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7569 || (tv->v_type == VAR_DICT
7570 && tv->vval.v_dict != NULL
7571 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7572}
7573
7574/*
7575 * "islocked()" function
7576 */
7577 static void
7578f_islocked(typval_T *argvars, typval_T *rettv)
7579{
7580 lval_T lv;
7581 char_u *end;
7582 dictitem_T *di;
7583
7584 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007585 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007586 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007587 if (end != NULL && lv.ll_name != NULL)
7588 {
7589 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007590 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007591 else
7592 {
7593 if (lv.ll_tv == NULL)
7594 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007595 di = find_var(lv.ll_name, NULL, TRUE);
7596 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007597 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007598 /* Consider a variable locked when:
7599 * 1. the variable itself is locked
7600 * 2. the value of the variable is locked.
7601 * 3. the List or Dict value is locked.
7602 */
7603 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7604 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007605 }
7606 }
7607 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007609 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007610 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007611 else if (lv.ll_list != NULL)
7612 /* List item. */
7613 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7614 else
7615 /* Dictionary item. */
7616 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7617 }
7618 }
7619
7620 clear_lval(&lv);
7621}
7622
7623#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7624/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007625 * "isinf()" function
7626 */
7627 static void
7628f_isinf(typval_T *argvars, typval_T *rettv)
7629{
7630 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7631 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7632}
7633
7634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007635 * "isnan()" function
7636 */
7637 static void
7638f_isnan(typval_T *argvars, typval_T *rettv)
7639{
7640 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7641 && isnan(argvars[0].vval.v_float);
7642}
7643#endif
7644
7645/*
7646 * "items(dict)" function
7647 */
7648 static void
7649f_items(typval_T *argvars, typval_T *rettv)
7650{
7651 dict_list(argvars, rettv, 2);
7652}
7653
7654#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7655/*
7656 * Get the job from the argument.
7657 * Returns NULL if the job is invalid.
7658 */
7659 static job_T *
7660get_job_arg(typval_T *tv)
7661{
7662 job_T *job;
7663
7664 if (tv->v_type != VAR_JOB)
7665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007666 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 return NULL;
7668 }
7669 job = tv->vval.v_job;
7670
7671 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007673 return job;
7674}
7675
7676/*
7677 * "job_getchannel()" function
7678 */
7679 static void
7680f_job_getchannel(typval_T *argvars, typval_T *rettv)
7681{
7682 job_T *job = get_job_arg(&argvars[0]);
7683
7684 if (job != NULL)
7685 {
7686 rettv->v_type = VAR_CHANNEL;
7687 rettv->vval.v_channel = job->jv_channel;
7688 if (job->jv_channel != NULL)
7689 ++job->jv_channel->ch_refcount;
7690 }
7691}
7692
7693/*
7694 * "job_info()" function
7695 */
7696 static void
7697f_job_info(typval_T *argvars, typval_T *rettv)
7698{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007699 if (argvars[0].v_type != VAR_UNKNOWN)
7700 {
7701 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007702
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007703 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7704 job_info(job, rettv->vval.v_dict);
7705 }
7706 else if (rettv_list_alloc(rettv) == OK)
7707 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007708}
7709
7710/*
7711 * "job_setoptions()" function
7712 */
7713 static void
7714f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7715{
7716 job_T *job = get_job_arg(&argvars[0]);
7717 jobopt_T opt;
7718
7719 if (job == NULL)
7720 return;
7721 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007722 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 job_set_options(job, &opt);
7724 free_job_options(&opt);
7725}
7726
7727/*
7728 * "job_start()" function
7729 */
7730 static void
7731f_job_start(typval_T *argvars, typval_T *rettv)
7732{
7733 rettv->v_type = VAR_JOB;
7734 if (check_restricted() || check_secure())
7735 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007736 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007737}
7738
7739/*
7740 * "job_status()" function
7741 */
7742 static void
7743f_job_status(typval_T *argvars, typval_T *rettv)
7744{
7745 job_T *job = get_job_arg(&argvars[0]);
7746
7747 if (job != NULL)
7748 {
7749 rettv->v_type = VAR_STRING;
7750 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7751 }
7752}
7753
7754/*
7755 * "job_stop()" function
7756 */
7757 static void
7758f_job_stop(typval_T *argvars, typval_T *rettv)
7759{
7760 job_T *job = get_job_arg(&argvars[0]);
7761
7762 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007763 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007764}
7765#endif
7766
7767/*
7768 * "join()" function
7769 */
7770 static void
7771f_join(typval_T *argvars, typval_T *rettv)
7772{
7773 garray_T ga;
7774 char_u *sep;
7775
7776 if (argvars[0].v_type != VAR_LIST)
7777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007778 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779 return;
7780 }
7781 if (argvars[0].vval.v_list == NULL)
7782 return;
7783 if (argvars[1].v_type == VAR_UNKNOWN)
7784 sep = (char_u *)" ";
7785 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007786 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007787
7788 rettv->v_type = VAR_STRING;
7789
7790 if (sep != NULL)
7791 {
7792 ga_init2(&ga, (int)sizeof(char), 80);
7793 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7794 ga_append(&ga, NUL);
7795 rettv->vval.v_string = (char_u *)ga.ga_data;
7796 }
7797 else
7798 rettv->vval.v_string = NULL;
7799}
7800
7801/*
7802 * "js_decode()" function
7803 */
7804 static void
7805f_js_decode(typval_T *argvars, typval_T *rettv)
7806{
7807 js_read_T reader;
7808
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007809 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 reader.js_fill = NULL;
7811 reader.js_used = 0;
7812 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007813 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007814}
7815
7816/*
7817 * "js_encode()" function
7818 */
7819 static void
7820f_js_encode(typval_T *argvars, typval_T *rettv)
7821{
7822 rettv->v_type = VAR_STRING;
7823 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7824}
7825
7826/*
7827 * "json_decode()" function
7828 */
7829 static void
7830f_json_decode(typval_T *argvars, typval_T *rettv)
7831{
7832 js_read_T reader;
7833
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007834 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007835 reader.js_fill = NULL;
7836 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007837 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007838}
7839
7840/*
7841 * "json_encode()" function
7842 */
7843 static void
7844f_json_encode(typval_T *argvars, typval_T *rettv)
7845{
7846 rettv->v_type = VAR_STRING;
7847 rettv->vval.v_string = json_encode(&argvars[0], 0);
7848}
7849
7850/*
7851 * "keys()" function
7852 */
7853 static void
7854f_keys(typval_T *argvars, typval_T *rettv)
7855{
7856 dict_list(argvars, rettv, 0);
7857}
7858
7859/*
7860 * "last_buffer_nr()" function.
7861 */
7862 static void
7863f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7864{
7865 int n = 0;
7866 buf_T *buf;
7867
Bram Moolenaar29323592016-07-24 22:04:11 +02007868 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007869 if (n < buf->b_fnum)
7870 n = buf->b_fnum;
7871
7872 rettv->vval.v_number = n;
7873}
7874
7875/*
7876 * "len()" function
7877 */
7878 static void
7879f_len(typval_T *argvars, typval_T *rettv)
7880{
7881 switch (argvars[0].v_type)
7882 {
7883 case VAR_STRING:
7884 case VAR_NUMBER:
7885 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007886 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007887 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007888 case VAR_BLOB:
7889 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7890 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007891 case VAR_LIST:
7892 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7893 break;
7894 case VAR_DICT:
7895 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7896 break;
7897 case VAR_UNKNOWN:
7898 case VAR_SPECIAL:
7899 case VAR_FLOAT:
7900 case VAR_FUNC:
7901 case VAR_PARTIAL:
7902 case VAR_JOB:
7903 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007904 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007905 break;
7906 }
7907}
7908
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007909 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007910libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911{
7912#ifdef FEAT_LIBCALL
7913 char_u *string_in;
7914 char_u **string_result;
7915 int nr_result;
7916#endif
7917
7918 rettv->v_type = type;
7919 if (type != VAR_NUMBER)
7920 rettv->vval.v_string = NULL;
7921
7922 if (check_restricted() || check_secure())
7923 return;
7924
7925#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007926 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007927 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7928 {
7929 string_in = NULL;
7930 if (argvars[2].v_type == VAR_STRING)
7931 string_in = argvars[2].vval.v_string;
7932 if (type == VAR_NUMBER)
7933 string_result = NULL;
7934 else
7935 string_result = &rettv->vval.v_string;
7936 if (mch_libcall(argvars[0].vval.v_string,
7937 argvars[1].vval.v_string,
7938 string_in,
7939 argvars[2].vval.v_number,
7940 string_result,
7941 &nr_result) == OK
7942 && type == VAR_NUMBER)
7943 rettv->vval.v_number = nr_result;
7944 }
7945#endif
7946}
7947
7948/*
7949 * "libcall()" function
7950 */
7951 static void
7952f_libcall(typval_T *argvars, typval_T *rettv)
7953{
7954 libcall_common(argvars, rettv, VAR_STRING);
7955}
7956
7957/*
7958 * "libcallnr()" function
7959 */
7960 static void
7961f_libcallnr(typval_T *argvars, typval_T *rettv)
7962{
7963 libcall_common(argvars, rettv, VAR_NUMBER);
7964}
7965
7966/*
7967 * "line(string)" function
7968 */
7969 static void
7970f_line(typval_T *argvars, typval_T *rettv)
7971{
7972 linenr_T lnum = 0;
7973 pos_T *fp;
7974 int fnum;
7975
7976 fp = var2fpos(&argvars[0], TRUE, &fnum);
7977 if (fp != NULL)
7978 lnum = fp->lnum;
7979 rettv->vval.v_number = lnum;
7980}
7981
7982/*
7983 * "line2byte(lnum)" function
7984 */
7985 static void
7986f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
7987{
7988#ifndef FEAT_BYTEOFF
7989 rettv->vval.v_number = -1;
7990#else
7991 linenr_T lnum;
7992
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007993 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007994 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
7995 rettv->vval.v_number = -1;
7996 else
7997 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7998 if (rettv->vval.v_number >= 0)
7999 ++rettv->vval.v_number;
8000#endif
8001}
8002
8003/*
8004 * "lispindent(lnum)" function
8005 */
8006 static void
8007f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8008{
8009#ifdef FEAT_LISP
8010 pos_T pos;
8011 linenr_T lnum;
8012
8013 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008014 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008015 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8016 {
8017 curwin->w_cursor.lnum = lnum;
8018 rettv->vval.v_number = get_lisp_indent();
8019 curwin->w_cursor = pos;
8020 }
8021 else
8022#endif
8023 rettv->vval.v_number = -1;
8024}
8025
8026/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008027 * "list2str()" function
8028 */
8029 static void
8030f_list2str(typval_T *argvars, typval_T *rettv)
8031{
8032 list_T *l;
8033 listitem_T *li;
8034 garray_T ga;
8035 int utf8 = FALSE;
8036
8037 rettv->v_type = VAR_STRING;
8038 rettv->vval.v_string = NULL;
8039 if (argvars[0].v_type != VAR_LIST)
8040 {
8041 emsg(_(e_invarg));
8042 return;
8043 }
8044
8045 l = argvars[0].vval.v_list;
8046 if (l == NULL)
8047 return; // empty list results in empty string
8048
8049 if (argvars[1].v_type != VAR_UNKNOWN)
8050 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8051
8052 ga_init2(&ga, 1, 80);
8053 if (has_mbyte || utf8)
8054 {
8055 char_u buf[MB_MAXBYTES + 1];
8056 int (*char2bytes)(int, char_u *);
8057
8058 if (utf8 || enc_utf8)
8059 char2bytes = utf_char2bytes;
8060 else
8061 char2bytes = mb_char2bytes;
8062
8063 for (li = l->lv_first; li != NULL; li = li->li_next)
8064 {
8065 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8066 ga_concat(&ga, buf);
8067 }
8068 ga_append(&ga, NUL);
8069 }
8070 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8071 {
8072 for (li = l->lv_first; li != NULL; li = li->li_next)
8073 ga_append(&ga, tv_get_number(&li->li_tv));
8074 ga_append(&ga, NUL);
8075 }
8076
8077 rettv->v_type = VAR_STRING;
8078 rettv->vval.v_string = ga.ga_data;
8079}
8080
8081/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008082 * "localtime()" function
8083 */
8084 static void
8085f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8086{
8087 rettv->vval.v_number = (varnumber_T)time(NULL);
8088}
8089
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008090 static void
8091get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8092{
8093 char_u *keys;
8094 char_u *which;
8095 char_u buf[NUMBUFLEN];
8096 char_u *keys_buf = NULL;
8097 char_u *rhs;
8098 int mode;
8099 int abbr = FALSE;
8100 int get_dict = FALSE;
8101 mapblock_T *mp;
8102 int buffer_local;
8103
8104 /* return empty string for failure */
8105 rettv->v_type = VAR_STRING;
8106 rettv->vval.v_string = NULL;
8107
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008108 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008109 if (*keys == NUL)
8110 return;
8111
8112 if (argvars[1].v_type != VAR_UNKNOWN)
8113 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008114 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008115 if (argvars[2].v_type != VAR_UNKNOWN)
8116 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008117 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008118 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008119 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008120 }
8121 }
8122 else
8123 which = (char_u *)"";
8124 if (which == NULL)
8125 return;
8126
8127 mode = get_map_mode(&which, 0);
8128
8129 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8130 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8131 vim_free(keys_buf);
8132
8133 if (!get_dict)
8134 {
8135 /* Return a string. */
8136 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008137 {
8138 if (*rhs == NUL)
8139 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8140 else
8141 rettv->vval.v_string = str2special_save(rhs, FALSE);
8142 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008143
8144 }
8145 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8146 {
8147 /* Return a dictionary. */
8148 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8149 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8150 dict_T *dict = rettv->vval.v_dict;
8151
Bram Moolenaare0be1672018-07-08 16:50:37 +02008152 dict_add_string(dict, "lhs", lhs);
8153 dict_add_string(dict, "rhs", mp->m_orig_str);
8154 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8155 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8156 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008157 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8158 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008159 dict_add_number(dict, "buffer", (long)buffer_local);
8160 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8161 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162
8163 vim_free(lhs);
8164 vim_free(mapmode);
8165 }
8166}
8167
8168#ifdef FEAT_FLOAT
8169/*
8170 * "log()" function
8171 */
8172 static void
8173f_log(typval_T *argvars, typval_T *rettv)
8174{
8175 float_T f = 0.0;
8176
8177 rettv->v_type = VAR_FLOAT;
8178 if (get_float_arg(argvars, &f) == OK)
8179 rettv->vval.v_float = log(f);
8180 else
8181 rettv->vval.v_float = 0.0;
8182}
8183
8184/*
8185 * "log10()" function
8186 */
8187 static void
8188f_log10(typval_T *argvars, typval_T *rettv)
8189{
8190 float_T f = 0.0;
8191
8192 rettv->v_type = VAR_FLOAT;
8193 if (get_float_arg(argvars, &f) == OK)
8194 rettv->vval.v_float = log10(f);
8195 else
8196 rettv->vval.v_float = 0.0;
8197}
8198#endif
8199
8200#ifdef FEAT_LUA
8201/*
8202 * "luaeval()" function
8203 */
8204 static void
8205f_luaeval(typval_T *argvars, typval_T *rettv)
8206{
8207 char_u *str;
8208 char_u buf[NUMBUFLEN];
8209
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008210 if (check_restricted() || check_secure())
8211 return;
8212
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008213 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008214 do_luaeval(str, argvars + 1, rettv);
8215}
8216#endif
8217
8218/*
8219 * "map()" function
8220 */
8221 static void
8222f_map(typval_T *argvars, typval_T *rettv)
8223{
8224 filter_map(argvars, rettv, TRUE);
8225}
8226
8227/*
8228 * "maparg()" function
8229 */
8230 static void
8231f_maparg(typval_T *argvars, typval_T *rettv)
8232{
8233 get_maparg(argvars, rettv, TRUE);
8234}
8235
8236/*
8237 * "mapcheck()" function
8238 */
8239 static void
8240f_mapcheck(typval_T *argvars, typval_T *rettv)
8241{
8242 get_maparg(argvars, rettv, FALSE);
8243}
8244
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008245typedef enum
8246{
8247 MATCH_END, /* matchend() */
8248 MATCH_MATCH, /* match() */
8249 MATCH_STR, /* matchstr() */
8250 MATCH_LIST, /* matchlist() */
8251 MATCH_POS /* matchstrpos() */
8252} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008253
8254 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008255find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256{
8257 char_u *str = NULL;
8258 long len = 0;
8259 char_u *expr = NULL;
8260 char_u *pat;
8261 regmatch_T regmatch;
8262 char_u patbuf[NUMBUFLEN];
8263 char_u strbuf[NUMBUFLEN];
8264 char_u *save_cpo;
8265 long start = 0;
8266 long nth = 1;
8267 colnr_T startcol = 0;
8268 int match = 0;
8269 list_T *l = NULL;
8270 listitem_T *li = NULL;
8271 long idx = 0;
8272 char_u *tofree = NULL;
8273
8274 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8275 save_cpo = p_cpo;
8276 p_cpo = (char_u *)"";
8277
8278 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008279 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008280 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008281 /* type MATCH_LIST: return empty list when there are no matches.
8282 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008283 if (rettv_list_alloc(rettv) == FAIL)
8284 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008285 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008286 && (list_append_string(rettv->vval.v_list,
8287 (char_u *)"", 0) == FAIL
8288 || list_append_number(rettv->vval.v_list,
8289 (varnumber_T)-1) == FAIL
8290 || list_append_number(rettv->vval.v_list,
8291 (varnumber_T)-1) == FAIL
8292 || list_append_number(rettv->vval.v_list,
8293 (varnumber_T)-1) == FAIL))
8294 {
8295 list_free(rettv->vval.v_list);
8296 rettv->vval.v_list = NULL;
8297 goto theend;
8298 }
8299 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008300 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008301 {
8302 rettv->v_type = VAR_STRING;
8303 rettv->vval.v_string = NULL;
8304 }
8305
8306 if (argvars[0].v_type == VAR_LIST)
8307 {
8308 if ((l = argvars[0].vval.v_list) == NULL)
8309 goto theend;
8310 li = l->lv_first;
8311 }
8312 else
8313 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008314 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008315 len = (long)STRLEN(str);
8316 }
8317
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008318 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008319 if (pat == NULL)
8320 goto theend;
8321
8322 if (argvars[2].v_type != VAR_UNKNOWN)
8323 {
8324 int error = FALSE;
8325
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008326 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008327 if (error)
8328 goto theend;
8329 if (l != NULL)
8330 {
8331 li = list_find(l, start);
8332 if (li == NULL)
8333 goto theend;
8334 idx = l->lv_idx; /* use the cached index */
8335 }
8336 else
8337 {
8338 if (start < 0)
8339 start = 0;
8340 if (start > len)
8341 goto theend;
8342 /* When "count" argument is there ignore matches before "start",
8343 * otherwise skip part of the string. Differs when pattern is "^"
8344 * or "\<". */
8345 if (argvars[3].v_type != VAR_UNKNOWN)
8346 startcol = start;
8347 else
8348 {
8349 str += start;
8350 len -= start;
8351 }
8352 }
8353
8354 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008355 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008356 if (error)
8357 goto theend;
8358 }
8359
8360 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8361 if (regmatch.regprog != NULL)
8362 {
8363 regmatch.rm_ic = p_ic;
8364
8365 for (;;)
8366 {
8367 if (l != NULL)
8368 {
8369 if (li == NULL)
8370 {
8371 match = FALSE;
8372 break;
8373 }
8374 vim_free(tofree);
8375 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8376 if (str == NULL)
8377 break;
8378 }
8379
8380 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8381
8382 if (match && --nth <= 0)
8383 break;
8384 if (l == NULL && !match)
8385 break;
8386
8387 /* Advance to just after the match. */
8388 if (l != NULL)
8389 {
8390 li = li->li_next;
8391 ++idx;
8392 }
8393 else
8394 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 startcol = (colnr_T)(regmatch.startp[0]
8396 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008397 if (startcol > (colnr_T)len
8398 || str + startcol <= regmatch.startp[0])
8399 {
8400 match = FALSE;
8401 break;
8402 }
8403 }
8404 }
8405
8406 if (match)
8407 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008408 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008409 {
8410 listitem_T *li1 = rettv->vval.v_list->lv_first;
8411 listitem_T *li2 = li1->li_next;
8412 listitem_T *li3 = li2->li_next;
8413 listitem_T *li4 = li3->li_next;
8414
8415 vim_free(li1->li_tv.vval.v_string);
8416 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8417 (int)(regmatch.endp[0] - regmatch.startp[0]));
8418 li3->li_tv.vval.v_number =
8419 (varnumber_T)(regmatch.startp[0] - expr);
8420 li4->li_tv.vval.v_number =
8421 (varnumber_T)(regmatch.endp[0] - expr);
8422 if (l != NULL)
8423 li2->li_tv.vval.v_number = (varnumber_T)idx;
8424 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008425 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008426 {
8427 int i;
8428
8429 /* return list with matched string and submatches */
8430 for (i = 0; i < NSUBEXP; ++i)
8431 {
8432 if (regmatch.endp[i] == NULL)
8433 {
8434 if (list_append_string(rettv->vval.v_list,
8435 (char_u *)"", 0) == FAIL)
8436 break;
8437 }
8438 else if (list_append_string(rettv->vval.v_list,
8439 regmatch.startp[i],
8440 (int)(regmatch.endp[i] - regmatch.startp[i]))
8441 == FAIL)
8442 break;
8443 }
8444 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008445 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008446 {
8447 /* return matched string */
8448 if (l != NULL)
8449 copy_tv(&li->li_tv, rettv);
8450 else
8451 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8452 (int)(regmatch.endp[0] - regmatch.startp[0]));
8453 }
8454 else if (l != NULL)
8455 rettv->vval.v_number = idx;
8456 else
8457 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008458 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008459 rettv->vval.v_number =
8460 (varnumber_T)(regmatch.startp[0] - str);
8461 else
8462 rettv->vval.v_number =
8463 (varnumber_T)(regmatch.endp[0] - str);
8464 rettv->vval.v_number += (varnumber_T)(str - expr);
8465 }
8466 }
8467 vim_regfree(regmatch.regprog);
8468 }
8469
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008470theend:
8471 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008472 /* matchstrpos() without a list: drop the second item. */
8473 listitem_remove(rettv->vval.v_list,
8474 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008475 vim_free(tofree);
8476 p_cpo = save_cpo;
8477}
8478
8479/*
8480 * "match()" function
8481 */
8482 static void
8483f_match(typval_T *argvars, typval_T *rettv)
8484{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008485 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008486}
8487
Bram Moolenaar95e51472018-07-28 16:55:56 +02008488#ifdef FEAT_SEARCH_EXTRA
8489 static int
8490matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8491{
8492 dictitem_T *di;
8493
8494 if (tv->v_type != VAR_DICT)
8495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008496 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008497 return FAIL;
8498 }
8499
8500 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008501 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008502 (char_u *)"conceal", FALSE);
8503
8504 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8505 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008506 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008507 if (*win == NULL)
8508 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008509 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008510 return FAIL;
8511 }
8512 }
8513
8514 return OK;
8515}
8516#endif
8517
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518/*
8519 * "matchadd()" function
8520 */
8521 static void
8522f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8523{
8524#ifdef FEAT_SEARCH_EXTRA
8525 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008526 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8527 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528 int prio = 10; /* default priority */
8529 int id = -1;
8530 int error = FALSE;
8531 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008532 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008533
8534 rettv->vval.v_number = -1;
8535
8536 if (grp == NULL || pat == NULL)
8537 return;
8538 if (argvars[2].v_type != VAR_UNKNOWN)
8539 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008540 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008541 if (argvars[3].v_type != VAR_UNKNOWN)
8542 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008543 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008544 if (argvars[4].v_type != VAR_UNKNOWN
8545 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8546 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008547 }
8548 }
8549 if (error == TRUE)
8550 return;
8551 if (id >= 1 && id <= 3)
8552 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008553 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008554 return;
8555 }
8556
Bram Moolenaar95e51472018-07-28 16:55:56 +02008557 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008558 conceal_char);
8559#endif
8560}
8561
8562/*
8563 * "matchaddpos()" function
8564 */
8565 static void
8566f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8567{
8568#ifdef FEAT_SEARCH_EXTRA
8569 char_u buf[NUMBUFLEN];
8570 char_u *group;
8571 int prio = 10;
8572 int id = -1;
8573 int error = FALSE;
8574 list_T *l;
8575 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008576 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008577
8578 rettv->vval.v_number = -1;
8579
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008580 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008581 if (group == NULL)
8582 return;
8583
8584 if (argvars[1].v_type != VAR_LIST)
8585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008587 return;
8588 }
8589 l = argvars[1].vval.v_list;
8590 if (l == NULL)
8591 return;
8592
8593 if (argvars[2].v_type != VAR_UNKNOWN)
8594 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008595 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 if (argvars[3].v_type != VAR_UNKNOWN)
8597 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008598 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008599
8600 if (argvars[4].v_type != VAR_UNKNOWN
8601 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8602 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008603 }
8604 }
8605 if (error == TRUE)
8606 return;
8607
8608 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8609 if (id == 1 || id == 2)
8610 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008611 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008612 return;
8613 }
8614
Bram Moolenaar95e51472018-07-28 16:55:56 +02008615 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008616 conceal_char);
8617#endif
8618}
8619
8620/*
8621 * "matcharg()" function
8622 */
8623 static void
8624f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8625{
8626 if (rettv_list_alloc(rettv) == OK)
8627 {
8628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008629 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 matchitem_T *m;
8631
8632 if (id >= 1 && id <= 3)
8633 {
8634 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8635 {
8636 list_append_string(rettv->vval.v_list,
8637 syn_id2name(m->hlg_id), -1);
8638 list_append_string(rettv->vval.v_list, m->pattern, -1);
8639 }
8640 else
8641 {
8642 list_append_string(rettv->vval.v_list, NULL, -1);
8643 list_append_string(rettv->vval.v_list, NULL, -1);
8644 }
8645 }
8646#endif
8647 }
8648}
8649
8650/*
8651 * "matchdelete()" function
8652 */
8653 static void
8654f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8655{
8656#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008657 win_T *win = get_optional_window(argvars, 1);
8658
8659 if (win == NULL)
8660 rettv->vval.v_number = -1;
8661 else
8662 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008663 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008664#endif
8665}
8666
8667/*
8668 * "matchend()" function
8669 */
8670 static void
8671f_matchend(typval_T *argvars, typval_T *rettv)
8672{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008673 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008674}
8675
8676/*
8677 * "matchlist()" function
8678 */
8679 static void
8680f_matchlist(typval_T *argvars, typval_T *rettv)
8681{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008682 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008683}
8684
8685/*
8686 * "matchstr()" function
8687 */
8688 static void
8689f_matchstr(typval_T *argvars, typval_T *rettv)
8690{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008691 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008692}
8693
8694/*
8695 * "matchstrpos()" function
8696 */
8697 static void
8698f_matchstrpos(typval_T *argvars, typval_T *rettv)
8699{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008700 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008701}
8702
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008703 static void
8704max_min(typval_T *argvars, typval_T *rettv, int domax)
8705{
8706 varnumber_T n = 0;
8707 varnumber_T i;
8708 int error = FALSE;
8709
8710 if (argvars[0].v_type == VAR_LIST)
8711 {
8712 list_T *l;
8713 listitem_T *li;
8714
8715 l = argvars[0].vval.v_list;
8716 if (l != NULL)
8717 {
8718 li = l->lv_first;
8719 if (li != NULL)
8720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008721 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008722 for (;;)
8723 {
8724 li = li->li_next;
8725 if (li == NULL)
8726 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008727 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 if (domax ? i > n : i < n)
8729 n = i;
8730 }
8731 }
8732 }
8733 }
8734 else if (argvars[0].v_type == VAR_DICT)
8735 {
8736 dict_T *d;
8737 int first = TRUE;
8738 hashitem_T *hi;
8739 int todo;
8740
8741 d = argvars[0].vval.v_dict;
8742 if (d != NULL)
8743 {
8744 todo = (int)d->dv_hashtab.ht_used;
8745 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8746 {
8747 if (!HASHITEM_EMPTY(hi))
8748 {
8749 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008750 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008751 if (first)
8752 {
8753 n = i;
8754 first = FALSE;
8755 }
8756 else if (domax ? i > n : i < n)
8757 n = i;
8758 }
8759 }
8760 }
8761 }
8762 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008763 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 rettv->vval.v_number = error ? 0 : n;
8765}
8766
8767/*
8768 * "max()" function
8769 */
8770 static void
8771f_max(typval_T *argvars, typval_T *rettv)
8772{
8773 max_min(argvars, rettv, TRUE);
8774}
8775
8776/*
8777 * "min()" function
8778 */
8779 static void
8780f_min(typval_T *argvars, typval_T *rettv)
8781{
8782 max_min(argvars, rettv, FALSE);
8783}
8784
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785/*
8786 * Create the directory in which "dir" is located, and higher levels when
8787 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008788 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008789 */
8790 static int
8791mkdir_recurse(char_u *dir, int prot)
8792{
8793 char_u *p;
8794 char_u *updir;
8795 int r = FAIL;
8796
8797 /* Get end of directory name in "dir".
8798 * We're done when it's "/" or "c:/". */
8799 p = gettail_sep(dir);
8800 if (p <= get_past_head(dir))
8801 return OK;
8802
8803 /* If the directory exists we're done. Otherwise: create it.*/
8804 updir = vim_strnsave(dir, (int)(p - dir));
8805 if (updir == NULL)
8806 return FAIL;
8807 if (mch_isdir(updir))
8808 r = OK;
8809 else if (mkdir_recurse(updir, prot) == OK)
8810 r = vim_mkdir_emsg(updir, prot);
8811 vim_free(updir);
8812 return r;
8813}
8814
8815#ifdef vim_mkdir
8816/*
8817 * "mkdir()" function
8818 */
8819 static void
8820f_mkdir(typval_T *argvars, typval_T *rettv)
8821{
8822 char_u *dir;
8823 char_u buf[NUMBUFLEN];
8824 int prot = 0755;
8825
8826 rettv->vval.v_number = FAIL;
8827 if (check_restricted() || check_secure())
8828 return;
8829
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008830 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008832 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008833
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008834 if (*gettail(dir) == NUL)
8835 /* remove trailing slashes */
8836 *gettail_sep(dir) = NUL;
8837
8838 if (argvars[1].v_type != VAR_UNKNOWN)
8839 {
8840 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008841 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008842 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008843 if (prot == -1)
8844 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008845 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008846 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008847 {
8848 if (mch_isdir(dir))
8849 {
8850 /* With the "p" flag it's OK if the dir already exists. */
8851 rettv->vval.v_number = OK;
8852 return;
8853 }
8854 mkdir_recurse(dir, prot);
8855 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008856 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008857 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008858}
8859#endif
8860
8861/*
8862 * "mode()" function
8863 */
8864 static void
8865f_mode(typval_T *argvars, typval_T *rettv)
8866{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008867 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008868
Bram Moolenaar612cc382018-07-29 15:34:26 +02008869 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008870
8871 if (time_for_testing == 93784)
8872 {
8873 /* Testing the two-character code. */
8874 buf[0] = 'x';
8875 buf[1] = '!';
8876 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008877#ifdef FEAT_TERMINAL
8878 else if (term_use_loop())
8879 buf[0] = 't';
8880#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008881 else if (VIsual_active)
8882 {
8883 if (VIsual_select)
8884 buf[0] = VIsual_mode + 's' - 'v';
8885 else
8886 buf[0] = VIsual_mode;
8887 }
8888 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8889 || State == CONFIRM)
8890 {
8891 buf[0] = 'r';
8892 if (State == ASKMORE)
8893 buf[1] = 'm';
8894 else if (State == CONFIRM)
8895 buf[1] = '?';
8896 }
8897 else if (State == EXTERNCMD)
8898 buf[0] = '!';
8899 else if (State & INSERT)
8900 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901 if (State & VREPLACE_FLAG)
8902 {
8903 buf[0] = 'R';
8904 buf[1] = 'v';
8905 }
8906 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008907 {
8908 if (State & REPLACE_FLAG)
8909 buf[0] = 'R';
8910 else
8911 buf[0] = 'i';
8912#ifdef FEAT_INS_EXPAND
8913 if (ins_compl_active())
8914 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008915 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008916 buf[1] = 'x';
8917#endif
8918 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008919 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008920 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008921 {
8922 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008923 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008925 else if (exmode_active == EXMODE_NORMAL)
8926 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008927 }
8928 else
8929 {
8930 buf[0] = 'n';
8931 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008932 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008933 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008934 // to be able to detect force-linewise/blockwise/characterwise operations
8935 buf[2] = motion_force;
8936 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008937 else if (restart_edit == 'I' || restart_edit == 'R'
8938 || restart_edit == 'V')
8939 {
8940 buf[1] = 'i';
8941 buf[2] = restart_edit;
8942 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 }
8944
8945 /* Clear out the minor mode when the argument is not a non-zero number or
8946 * non-empty string. */
8947 if (!non_zero_arg(&argvars[0]))
8948 buf[1] = NUL;
8949
8950 rettv->vval.v_string = vim_strsave(buf);
8951 rettv->v_type = VAR_STRING;
8952}
8953
8954#if defined(FEAT_MZSCHEME) || defined(PROTO)
8955/*
8956 * "mzeval()" function
8957 */
8958 static void
8959f_mzeval(typval_T *argvars, typval_T *rettv)
8960{
8961 char_u *str;
8962 char_u buf[NUMBUFLEN];
8963
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008964 if (check_restricted() || check_secure())
8965 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008966 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 do_mzeval(str, rettv);
8968}
8969
8970 void
8971mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
8972{
8973 typval_T argvars[3];
8974
8975 argvars[0].v_type = VAR_STRING;
8976 argvars[0].vval.v_string = name;
8977 copy_tv(args, &argvars[1]);
8978 argvars[2].v_type = VAR_UNKNOWN;
8979 f_call(argvars, rettv);
8980 clear_tv(&argvars[1]);
8981}
8982#endif
8983
8984/*
8985 * "nextnonblank()" function
8986 */
8987 static void
8988f_nextnonblank(typval_T *argvars, typval_T *rettv)
8989{
8990 linenr_T lnum;
8991
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008992 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008993 {
8994 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
8995 {
8996 lnum = 0;
8997 break;
8998 }
8999 if (*skipwhite(ml_get(lnum)) != NUL)
9000 break;
9001 }
9002 rettv->vval.v_number = lnum;
9003}
9004
9005/*
9006 * "nr2char()" function
9007 */
9008 static void
9009f_nr2char(typval_T *argvars, typval_T *rettv)
9010{
9011 char_u buf[NUMBUFLEN];
9012
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009013 if (has_mbyte)
9014 {
9015 int utf8 = 0;
9016
9017 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009018 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009019 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009020 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009021 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009022 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009023 }
9024 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009025 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009026 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009027 buf[1] = NUL;
9028 }
9029 rettv->v_type = VAR_STRING;
9030 rettv->vval.v_string = vim_strsave(buf);
9031}
9032
9033/*
9034 * "or(expr, expr)" function
9035 */
9036 static void
9037f_or(typval_T *argvars, typval_T *rettv)
9038{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009039 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9040 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009041}
9042
9043/*
9044 * "pathshorten()" function
9045 */
9046 static void
9047f_pathshorten(typval_T *argvars, typval_T *rettv)
9048{
9049 char_u *p;
9050
9051 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009052 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009053 if (p == NULL)
9054 rettv->vval.v_string = NULL;
9055 else
9056 {
9057 p = vim_strsave(p);
9058 rettv->vval.v_string = p;
9059 if (p != NULL)
9060 shorten_dir(p);
9061 }
9062}
9063
9064#ifdef FEAT_PERL
9065/*
9066 * "perleval()" function
9067 */
9068 static void
9069f_perleval(typval_T *argvars, typval_T *rettv)
9070{
9071 char_u *str;
9072 char_u buf[NUMBUFLEN];
9073
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009074 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009075 do_perleval(str, rettv);
9076}
9077#endif
9078
9079#ifdef FEAT_FLOAT
9080/*
9081 * "pow()" function
9082 */
9083 static void
9084f_pow(typval_T *argvars, typval_T *rettv)
9085{
9086 float_T fx = 0.0, fy = 0.0;
9087
9088 rettv->v_type = VAR_FLOAT;
9089 if (get_float_arg(argvars, &fx) == OK
9090 && get_float_arg(&argvars[1], &fy) == OK)
9091 rettv->vval.v_float = pow(fx, fy);
9092 else
9093 rettv->vval.v_float = 0.0;
9094}
9095#endif
9096
9097/*
9098 * "prevnonblank()" function
9099 */
9100 static void
9101f_prevnonblank(typval_T *argvars, typval_T *rettv)
9102{
9103 linenr_T lnum;
9104
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009105 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009106 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9107 lnum = 0;
9108 else
9109 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9110 --lnum;
9111 rettv->vval.v_number = lnum;
9112}
9113
9114/* This dummy va_list is here because:
9115 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9116 * - locally in the function results in a "used before set" warning
9117 * - using va_start() to initialize it gives "function with fixed args" error */
9118static va_list ap;
9119
9120/*
9121 * "printf()" function
9122 */
9123 static void
9124f_printf(typval_T *argvars, typval_T *rettv)
9125{
9126 char_u buf[NUMBUFLEN];
9127 int len;
9128 char_u *s;
9129 int saved_did_emsg = did_emsg;
9130 char *fmt;
9131
9132 rettv->v_type = VAR_STRING;
9133 rettv->vval.v_string = NULL;
9134
9135 /* Get the required length, allocate the buffer and do it for real. */
9136 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009137 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009138 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009139 if (!did_emsg)
9140 {
9141 s = alloc(len + 1);
9142 if (s != NULL)
9143 {
9144 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009145 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9146 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009147 }
9148 }
9149 did_emsg |= saved_did_emsg;
9150}
9151
Bram Moolenaarf2732452018-06-03 14:47:35 +02009152#ifdef FEAT_JOB_CHANNEL
9153/*
9154 * "prompt_setcallback({buffer}, {callback})" function
9155 */
9156 static void
9157f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9158{
9159 buf_T *buf;
9160 char_u *callback;
9161 partial_T *partial;
9162
9163 if (check_secure())
9164 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009165 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009166 if (buf == NULL)
9167 return;
9168
9169 callback = get_callback(&argvars[1], &partial);
9170 if (callback == NULL)
9171 return;
9172
9173 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9174 if (partial == NULL)
9175 buf->b_prompt_callback = vim_strsave(callback);
9176 else
9177 /* pointer into the partial */
9178 buf->b_prompt_callback = callback;
9179 buf->b_prompt_partial = partial;
9180}
9181
9182/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009183 * "prompt_setinterrupt({buffer}, {callback})" function
9184 */
9185 static void
9186f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9187{
9188 buf_T *buf;
9189 char_u *callback;
9190 partial_T *partial;
9191
9192 if (check_secure())
9193 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009194 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009195 if (buf == NULL)
9196 return;
9197
9198 callback = get_callback(&argvars[1], &partial);
9199 if (callback == NULL)
9200 return;
9201
9202 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9203 if (partial == NULL)
9204 buf->b_prompt_interrupt = vim_strsave(callback);
9205 else
9206 /* pointer into the partial */
9207 buf->b_prompt_interrupt = callback;
9208 buf->b_prompt_int_partial = partial;
9209}
9210
9211/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009212 * "prompt_setprompt({buffer}, {text})" function
9213 */
9214 static void
9215f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9216{
9217 buf_T *buf;
9218 char_u *text;
9219
9220 if (check_secure())
9221 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009222 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009223 if (buf == NULL)
9224 return;
9225
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009226 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009227 vim_free(buf->b_prompt_text);
9228 buf->b_prompt_text = vim_strsave(text);
9229}
9230#endif
9231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009232/*
9233 * "pumvisible()" function
9234 */
9235 static void
9236f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9237{
9238#ifdef FEAT_INS_EXPAND
9239 if (pum_visible())
9240 rettv->vval.v_number = 1;
9241#endif
9242}
9243
9244#ifdef FEAT_PYTHON3
9245/*
9246 * "py3eval()" function
9247 */
9248 static void
9249f_py3eval(typval_T *argvars, typval_T *rettv)
9250{
9251 char_u *str;
9252 char_u buf[NUMBUFLEN];
9253
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009254 if (check_restricted() || check_secure())
9255 return;
9256
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009257 if (p_pyx == 0)
9258 p_pyx = 3;
9259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009260 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009261 do_py3eval(str, rettv);
9262}
9263#endif
9264
9265#ifdef FEAT_PYTHON
9266/*
9267 * "pyeval()" function
9268 */
9269 static void
9270f_pyeval(typval_T *argvars, typval_T *rettv)
9271{
9272 char_u *str;
9273 char_u buf[NUMBUFLEN];
9274
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009275 if (check_restricted() || check_secure())
9276 return;
9277
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009278 if (p_pyx == 0)
9279 p_pyx = 2;
9280
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009281 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009282 do_pyeval(str, rettv);
9283}
9284#endif
9285
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009286#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9287/*
9288 * "pyxeval()" function
9289 */
9290 static void
9291f_pyxeval(typval_T *argvars, typval_T *rettv)
9292{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009293 if (check_restricted() || check_secure())
9294 return;
9295
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009296# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9297 init_pyxversion();
9298 if (p_pyx == 2)
9299 f_pyeval(argvars, rettv);
9300 else
9301 f_py3eval(argvars, rettv);
9302# elif defined(FEAT_PYTHON)
9303 f_pyeval(argvars, rettv);
9304# elif defined(FEAT_PYTHON3)
9305 f_py3eval(argvars, rettv);
9306# endif
9307}
9308#endif
9309
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009310/*
9311 * "range()" function
9312 */
9313 static void
9314f_range(typval_T *argvars, typval_T *rettv)
9315{
9316 varnumber_T start;
9317 varnumber_T end;
9318 varnumber_T stride = 1;
9319 varnumber_T i;
9320 int error = FALSE;
9321
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009322 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009323 if (argvars[1].v_type == VAR_UNKNOWN)
9324 {
9325 end = start - 1;
9326 start = 0;
9327 }
9328 else
9329 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009330 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009332 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009333 }
9334
9335 if (error)
9336 return; /* type error; errmsg already given */
9337 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009338 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009339 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009340 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341 else
9342 {
9343 if (rettv_list_alloc(rettv) == OK)
9344 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9345 if (list_append_number(rettv->vval.v_list,
9346 (varnumber_T)i) == FAIL)
9347 break;
9348 }
9349}
9350
9351/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009352 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009353 */
9354 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009355readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009356{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009357 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009358 typval_T save_val;
9359 typval_T rettv;
9360 typval_T argv[2];
9361 int retval = 0;
9362 int error = FALSE;
9363
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009364 if (expr->v_type == VAR_UNKNOWN)
9365 return 1;
9366
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009367 prepare_vimvar(VV_VAL, &save_val);
9368 set_vim_var_string(VV_VAL, name, -1);
9369 argv[0].v_type = VAR_STRING;
9370 argv[0].vval.v_string = name;
9371
9372 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9373 goto theend;
9374
9375 retval = tv_get_number_chk(&rettv, &error);
9376 if (error)
9377 retval = -1;
9378 clear_tv(&rettv);
9379
9380theend:
9381 set_vim_var_string(VV_VAL, NULL, 0);
9382 restore_vimvar(VV_VAL, &save_val);
9383 return retval;
9384}
9385
9386/*
9387 * "readdir()" function
9388 */
9389 static void
9390f_readdir(typval_T *argvars, typval_T *rettv)
9391{
9392 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009393 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009394 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009395 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009396 garray_T ga;
9397 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009398
9399 if (rettv_list_alloc(rettv) == FAIL)
9400 return;
9401 path = tv_get_string(&argvars[0]);
9402 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009403
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009404 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9405 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009406 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009407 for (i = 0; i < ga.ga_len; i++)
9408 {
9409 p = ((char_u **)ga.ga_data)[i];
9410 list_append_string(rettv->vval.v_list, p, -1);
9411 }
9412 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009413 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009414}
9415
9416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009417 * "readfile()" function
9418 */
9419 static void
9420f_readfile(typval_T *argvars, typval_T *rettv)
9421{
9422 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009423 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009424 int failed = FALSE;
9425 char_u *fname;
9426 FILE *fd;
9427 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9428 int io_size = sizeof(buf);
9429 int readlen; /* size of last fread() */
9430 char_u *prev = NULL; /* previously read bytes, if any */
9431 long prevlen = 0; /* length of data in prev */
9432 long prevsize = 0; /* size of prev buffer */
9433 long maxline = MAXLNUM;
9434 long cnt = 0;
9435 char_u *p; /* position in buf */
9436 char_u *start; /* start of current line */
9437
9438 if (argvars[1].v_type != VAR_UNKNOWN)
9439 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009440 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009441 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009442 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9443 blob = TRUE;
9444
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009446 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009447 }
9448
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009449 if (blob)
9450 {
9451 if (rettv_blob_alloc(rettv) == FAIL)
9452 return;
9453 }
9454 else
9455 {
9456 if (rettv_list_alloc(rettv) == FAIL)
9457 return;
9458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459
9460 /* Always open the file in binary mode, library functions have a mind of
9461 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009462 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009463 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009465 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 return;
9467 }
9468
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009469 if (blob)
9470 {
9471 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009473 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009474 blob_free(rettv->vval.v_blob);
9475 }
9476 fclose(fd);
9477 return;
9478 }
9479
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009480 while (cnt < maxline || maxline < 0)
9481 {
9482 readlen = (int)fread(buf, 1, io_size, fd);
9483
9484 /* This for loop processes what was read, but is also entered at end
9485 * of file so that either:
9486 * - an incomplete line gets written
9487 * - a "binary" file gets an empty line at the end if it ends in a
9488 * newline. */
9489 for (p = buf, start = buf;
9490 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9491 ++p)
9492 {
9493 if (*p == '\n' || readlen <= 0)
9494 {
9495 listitem_T *li;
9496 char_u *s = NULL;
9497 long_u len = p - start;
9498
9499 /* Finished a line. Remove CRs before NL. */
9500 if (readlen > 0 && !binary)
9501 {
9502 while (len > 0 && start[len - 1] == '\r')
9503 --len;
9504 /* removal may cross back to the "prev" string */
9505 if (len == 0)
9506 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9507 --prevlen;
9508 }
9509 if (prevlen == 0)
9510 s = vim_strnsave(start, (int)len);
9511 else
9512 {
9513 /* Change "prev" buffer to be the right size. This way
9514 * the bytes are only copied once, and very long lines are
9515 * allocated only once. */
9516 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9517 {
9518 mch_memmove(s + prevlen, start, len);
9519 s[prevlen + len] = NUL;
9520 prev = NULL; /* the list will own the string */
9521 prevlen = prevsize = 0;
9522 }
9523 }
9524 if (s == NULL)
9525 {
9526 do_outofmem_msg((long_u) prevlen + len + 1);
9527 failed = TRUE;
9528 break;
9529 }
9530
9531 if ((li = listitem_alloc()) == NULL)
9532 {
9533 vim_free(s);
9534 failed = TRUE;
9535 break;
9536 }
9537 li->li_tv.v_type = VAR_STRING;
9538 li->li_tv.v_lock = 0;
9539 li->li_tv.vval.v_string = s;
9540 list_append(rettv->vval.v_list, li);
9541
9542 start = p + 1; /* step over newline */
9543 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9544 break;
9545 }
9546 else if (*p == NUL)
9547 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9549 * when finding the BF and check the previous two bytes. */
9550 else if (*p == 0xbf && enc_utf8 && !binary)
9551 {
9552 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9553 * + 1, these may be in the "prev" string. */
9554 char_u back1 = p >= buf + 1 ? p[-1]
9555 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9556 char_u back2 = p >= buf + 2 ? p[-2]
9557 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9558 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9559
9560 if (back2 == 0xef && back1 == 0xbb)
9561 {
9562 char_u *dest = p - 2;
9563
9564 /* Usually a BOM is at the beginning of a file, and so at
9565 * the beginning of a line; then we can just step over it.
9566 */
9567 if (start == dest)
9568 start = p + 1;
9569 else
9570 {
9571 /* have to shuffle buf to close gap */
9572 int adjust_prevlen = 0;
9573
9574 if (dest < buf)
9575 {
9576 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9577 dest = buf;
9578 }
9579 if (readlen > p - buf + 1)
9580 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9581 readlen -= 3 - adjust_prevlen;
9582 prevlen -= adjust_prevlen;
9583 p = dest - 1;
9584 }
9585 }
9586 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 } /* for */
9588
9589 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9590 break;
9591 if (start < p)
9592 {
9593 /* There's part of a line in buf, store it in "prev". */
9594 if (p - start + prevlen >= prevsize)
9595 {
9596 /* need bigger "prev" buffer */
9597 char_u *newprev;
9598
9599 /* A common use case is ordinary text files and "prev" gets a
9600 * fragment of a line, so the first allocation is made
9601 * small, to avoid repeatedly 'allocing' large and
9602 * 'reallocing' small. */
9603 if (prevsize == 0)
9604 prevsize = (long)(p - start);
9605 else
9606 {
9607 long grow50pc = (prevsize * 3) / 2;
9608 long growmin = (long)((p - start) * 2 + prevlen);
9609 prevsize = grow50pc > growmin ? grow50pc : growmin;
9610 }
9611 newprev = prev == NULL ? alloc(prevsize)
9612 : vim_realloc(prev, prevsize);
9613 if (newprev == NULL)
9614 {
9615 do_outofmem_msg((long_u)prevsize);
9616 failed = TRUE;
9617 break;
9618 }
9619 prev = newprev;
9620 }
9621 /* Add the line part to end of "prev". */
9622 mch_memmove(prev + prevlen, start, p - start);
9623 prevlen += (long)(p - start);
9624 }
9625 } /* while */
9626
9627 /*
9628 * For a negative line count use only the lines at the end of the file,
9629 * free the rest.
9630 */
9631 if (!failed && maxline < 0)
9632 while (cnt > -maxline)
9633 {
9634 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9635 --cnt;
9636 }
9637
9638 if (failed)
9639 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009640 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009641 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009642 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 }
9644
9645 vim_free(prev);
9646 fclose(fd);
9647}
9648
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009649 static void
9650return_register(int regname, typval_T *rettv)
9651{
9652 char_u buf[2] = {0, 0};
9653
9654 buf[0] = (char_u)regname;
9655 rettv->v_type = VAR_STRING;
9656 rettv->vval.v_string = vim_strsave(buf);
9657}
9658
9659/*
9660 * "reg_executing()" function
9661 */
9662 static void
9663f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9664{
9665 return_register(reg_executing, rettv);
9666}
9667
9668/*
9669 * "reg_recording()" function
9670 */
9671 static void
9672f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9673{
9674 return_register(reg_recording, rettv);
9675}
9676
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009677#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009678/*
9679 * Convert a List to proftime_T.
9680 * Return FAIL when there is something wrong.
9681 */
9682 static int
9683list2proftime(typval_T *arg, proftime_T *tm)
9684{
9685 long n1, n2;
9686 int error = FALSE;
9687
9688 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9689 || arg->vval.v_list->lv_len != 2)
9690 return FAIL;
9691 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9692 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009693# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009694 tm->HighPart = n1;
9695 tm->LowPart = n2;
9696# else
9697 tm->tv_sec = n1;
9698 tm->tv_usec = n2;
9699# endif
9700 return error ? FAIL : OK;
9701}
9702#endif /* FEAT_RELTIME */
9703
9704/*
9705 * "reltime()" function
9706 */
9707 static void
9708f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9709{
9710#ifdef FEAT_RELTIME
9711 proftime_T res;
9712 proftime_T start;
9713
9714 if (argvars[0].v_type == VAR_UNKNOWN)
9715 {
9716 /* No arguments: get current time. */
9717 profile_start(&res);
9718 }
9719 else if (argvars[1].v_type == VAR_UNKNOWN)
9720 {
9721 if (list2proftime(&argvars[0], &res) == FAIL)
9722 return;
9723 profile_end(&res);
9724 }
9725 else
9726 {
9727 /* Two arguments: compute the difference. */
9728 if (list2proftime(&argvars[0], &start) == FAIL
9729 || list2proftime(&argvars[1], &res) == FAIL)
9730 return;
9731 profile_sub(&res, &start);
9732 }
9733
9734 if (rettv_list_alloc(rettv) == OK)
9735 {
9736 long n1, n2;
9737
Bram Moolenaar4f974752019-02-17 17:44:42 +01009738# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009739 n1 = res.HighPart;
9740 n2 = res.LowPart;
9741# else
9742 n1 = res.tv_sec;
9743 n2 = res.tv_usec;
9744# endif
9745 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9746 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9747 }
9748#endif
9749}
9750
9751#ifdef FEAT_FLOAT
9752/*
9753 * "reltimefloat()" function
9754 */
9755 static void
9756f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9757{
9758# ifdef FEAT_RELTIME
9759 proftime_T tm;
9760# endif
9761
9762 rettv->v_type = VAR_FLOAT;
9763 rettv->vval.v_float = 0;
9764# ifdef FEAT_RELTIME
9765 if (list2proftime(&argvars[0], &tm) == OK)
9766 rettv->vval.v_float = profile_float(&tm);
9767# endif
9768}
9769#endif
9770
9771/*
9772 * "reltimestr()" function
9773 */
9774 static void
9775f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9776{
9777#ifdef FEAT_RELTIME
9778 proftime_T tm;
9779#endif
9780
9781 rettv->v_type = VAR_STRING;
9782 rettv->vval.v_string = NULL;
9783#ifdef FEAT_RELTIME
9784 if (list2proftime(&argvars[0], &tm) == OK)
9785 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9786#endif
9787}
9788
9789#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009790 static void
9791make_connection(void)
9792{
9793 if (X_DISPLAY == NULL
9794# ifdef FEAT_GUI
9795 && !gui.in_use
9796# endif
9797 )
9798 {
9799 x_force_connect = TRUE;
9800 setup_term_clip();
9801 x_force_connect = FALSE;
9802 }
9803}
9804
9805 static int
9806check_connection(void)
9807{
9808 make_connection();
9809 if (X_DISPLAY == NULL)
9810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009811 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009812 return FAIL;
9813 }
9814 return OK;
9815}
9816#endif
9817
9818#ifdef FEAT_CLIENTSERVER
9819 static void
9820remote_common(typval_T *argvars, typval_T *rettv, int expr)
9821{
9822 char_u *server_name;
9823 char_u *keys;
9824 char_u *r = NULL;
9825 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009826 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009827# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009828 HWND w;
9829# else
9830 Window w;
9831# endif
9832
9833 if (check_restricted() || check_secure())
9834 return;
9835
9836# ifdef FEAT_X11
9837 if (check_connection() == FAIL)
9838 return;
9839# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009840 if (argvars[2].v_type != VAR_UNKNOWN
9841 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009842 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009843
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009844 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009845 if (server_name == NULL)
9846 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009847 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009848# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009849 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009850# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009851 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9852 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853# endif
9854 {
9855 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009857 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009858 vim_free(r);
9859 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009860 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009861 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009862 return;
9863 }
9864
9865 rettv->vval.v_string = r;
9866
9867 if (argvars[2].v_type != VAR_UNKNOWN)
9868 {
9869 dictitem_T v;
9870 char_u str[30];
9871 char_u *idvar;
9872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009873 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009874 if (idvar != NULL && *idvar != NUL)
9875 {
9876 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9877 v.di_tv.v_type = VAR_STRING;
9878 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009879 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009880 vim_free(v.di_tv.vval.v_string);
9881 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009882 }
9883}
9884#endif
9885
9886/*
9887 * "remote_expr()" function
9888 */
9889 static void
9890f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9891{
9892 rettv->v_type = VAR_STRING;
9893 rettv->vval.v_string = NULL;
9894#ifdef FEAT_CLIENTSERVER
9895 remote_common(argvars, rettv, TRUE);
9896#endif
9897}
9898
9899/*
9900 * "remote_foreground()" function
9901 */
9902 static void
9903f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9904{
9905#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009906# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009907 /* On Win32 it's done in this application. */
9908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009909 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009910
9911 if (server_name != NULL)
9912 serverForeground(server_name);
9913 }
9914# else
9915 /* Send a foreground() expression to the server. */
9916 argvars[1].v_type = VAR_STRING;
9917 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9918 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009919 rettv->v_type = VAR_STRING;
9920 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009921 remote_common(argvars, rettv, TRUE);
9922 vim_free(argvars[1].vval.v_string);
9923# endif
9924#endif
9925}
9926
9927 static void
9928f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9929{
9930#ifdef FEAT_CLIENTSERVER
9931 dictitem_T v;
9932 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009933# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009934 long_u n = 0;
9935# endif
9936 char_u *serverid;
9937
9938 if (check_restricted() || check_secure())
9939 {
9940 rettv->vval.v_number = -1;
9941 return;
9942 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009943 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009944 if (serverid == NULL)
9945 {
9946 rettv->vval.v_number = -1;
9947 return; /* type error; errmsg already given */
9948 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009949# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009950 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9951 if (n == 0)
9952 rettv->vval.v_number = -1;
9953 else
9954 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009955 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009956 rettv->vval.v_number = (s != NULL);
9957 }
9958# else
9959 if (check_connection() == FAIL)
9960 return;
9961
9962 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9963 serverStrToWin(serverid), &s);
9964# endif
9965
9966 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9967 {
9968 char_u *retvar;
9969
9970 v.di_tv.v_type = VAR_STRING;
9971 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009972 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009973 if (retvar != NULL)
9974 set_var(retvar, &v.di_tv, FALSE);
9975 vim_free(v.di_tv.vval.v_string);
9976 }
9977#else
9978 rettv->vval.v_number = -1;
9979#endif
9980}
9981
9982 static void
9983f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
9984{
9985 char_u *r = NULL;
9986
9987#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009988 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009989
9990 if (serverid != NULL && !check_restricted() && !check_secure())
9991 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009992 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009993# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +01009994 /* The server's HWND is encoded in the 'id' parameter */
9995 long_u n = 0;
9996# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009997
9998 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009999 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010000
Bram Moolenaar4f974752019-02-17 17:44:42 +010010001# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010002 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10003 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010004 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010005 if (r == NULL)
10006# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010007 if (check_connection() == FAIL
10008 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10009 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010010# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010011 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010012 }
10013#endif
10014 rettv->v_type = VAR_STRING;
10015 rettv->vval.v_string = r;
10016}
10017
10018/*
10019 * "remote_send()" function
10020 */
10021 static void
10022f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10023{
10024 rettv->v_type = VAR_STRING;
10025 rettv->vval.v_string = NULL;
10026#ifdef FEAT_CLIENTSERVER
10027 remote_common(argvars, rettv, FALSE);
10028#endif
10029}
10030
10031/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010032 * "remote_startserver()" function
10033 */
10034 static void
10035f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10036{
10037#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010038 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010039
10040 if (server == NULL)
10041 return; /* type error; errmsg already given */
10042 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010043 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010044 else
10045 {
10046# ifdef FEAT_X11
10047 if (check_connection() == OK)
10048 serverRegisterName(X_DISPLAY, server);
10049# else
10050 serverSetName(server);
10051# endif
10052 }
10053#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010054 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010055#endif
10056}
10057
10058/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010059 * "remove()" function
10060 */
10061 static void
10062f_remove(typval_T *argvars, typval_T *rettv)
10063{
10064 list_T *l;
10065 listitem_T *item, *item2;
10066 listitem_T *li;
10067 long idx;
10068 long end;
10069 char_u *key;
10070 dict_T *d;
10071 dictitem_T *di;
10072 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010073 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010074
10075 if (argvars[0].v_type == VAR_DICT)
10076 {
10077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010078 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010079 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010080 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010082 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010083 if (key != NULL)
10084 {
10085 di = dict_find(d, key, -1);
10086 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010087 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010088 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10089 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10090 {
10091 *rettv = di->di_tv;
10092 init_tv(&di->di_tv);
10093 dictitem_remove(d, di);
10094 }
10095 }
10096 }
10097 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010098 else if (argvars[0].v_type == VAR_BLOB)
10099 {
10100 idx = (long)tv_get_number_chk(&argvars[1], &error);
10101 if (!error)
10102 {
10103 blob_T *b = argvars[0].vval.v_blob;
10104 int len = blob_len(b);
10105 char_u *p;
10106
10107 if (idx < 0)
10108 // count from the end
10109 idx = len + idx;
10110 if (idx < 0 || idx >= len)
10111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010112 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010113 return;
10114 }
10115 if (argvars[2].v_type == VAR_UNKNOWN)
10116 {
10117 // Remove one item, return its value.
10118 p = (char_u *)b->bv_ga.ga_data;
10119 rettv->vval.v_number = (varnumber_T) *(p + idx);
10120 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10121 --b->bv_ga.ga_len;
10122 }
10123 else
10124 {
10125 blob_T *blob;
10126
10127 // Remove range of items, return list with values.
10128 end = (long)tv_get_number_chk(&argvars[2], &error);
10129 if (error)
10130 return;
10131 if (end < 0)
10132 // count from the end
10133 end = len + end;
10134 if (end >= len || idx > end)
10135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010136 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010137 return;
10138 }
10139 blob = blob_alloc();
10140 if (blob == NULL)
10141 return;
10142 blob->bv_ga.ga_len = end - idx + 1;
10143 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10144 {
10145 vim_free(blob);
10146 return;
10147 }
10148 p = (char_u *)b->bv_ga.ga_data;
10149 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10150 (size_t)(end - idx + 1));
10151 ++blob->bv_refcount;
10152 rettv->v_type = VAR_BLOB;
10153 rettv->vval.v_blob = blob;
10154
10155 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10156 b->bv_ga.ga_len -= end - idx + 1;
10157 }
10158 }
10159 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010160 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010161 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010162 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010163 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010164 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010165 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010166 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010167 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010168 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010169 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010170 else
10171 {
10172 if (argvars[2].v_type == VAR_UNKNOWN)
10173 {
10174 /* Remove one item, return its value. */
10175 vimlist_remove(l, item, item);
10176 *rettv = item->li_tv;
10177 vim_free(item);
10178 }
10179 else
10180 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010181 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010182 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010183 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010184 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010185 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010186 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010187 else
10188 {
10189 int cnt = 0;
10190
10191 for (li = item; li != NULL; li = li->li_next)
10192 {
10193 ++cnt;
10194 if (li == item2)
10195 break;
10196 }
10197 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010198 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010199 else
10200 {
10201 vimlist_remove(l, item, item2);
10202 if (rettv_list_alloc(rettv) == OK)
10203 {
10204 l = rettv->vval.v_list;
10205 l->lv_first = item;
10206 l->lv_last = item2;
10207 item->li_prev = NULL;
10208 item2->li_next = NULL;
10209 l->lv_len = cnt;
10210 }
10211 }
10212 }
10213 }
10214 }
10215 }
10216}
10217
10218/*
10219 * "rename({from}, {to})" function
10220 */
10221 static void
10222f_rename(typval_T *argvars, typval_T *rettv)
10223{
10224 char_u buf[NUMBUFLEN];
10225
10226 if (check_restricted() || check_secure())
10227 rettv->vval.v_number = -1;
10228 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010229 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10230 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010231}
10232
10233/*
10234 * "repeat()" function
10235 */
10236 static void
10237f_repeat(typval_T *argvars, typval_T *rettv)
10238{
10239 char_u *p;
10240 int n;
10241 int slen;
10242 int len;
10243 char_u *r;
10244 int i;
10245
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010246 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010247 if (argvars[0].v_type == VAR_LIST)
10248 {
10249 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10250 while (n-- > 0)
10251 if (list_extend(rettv->vval.v_list,
10252 argvars[0].vval.v_list, NULL) == FAIL)
10253 break;
10254 }
10255 else
10256 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010257 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010258 rettv->v_type = VAR_STRING;
10259 rettv->vval.v_string = NULL;
10260
10261 slen = (int)STRLEN(p);
10262 len = slen * n;
10263 if (len <= 0)
10264 return;
10265
10266 r = alloc(len + 1);
10267 if (r != NULL)
10268 {
10269 for (i = 0; i < n; i++)
10270 mch_memmove(r + i * slen, p, (size_t)slen);
10271 r[len] = NUL;
10272 }
10273
10274 rettv->vval.v_string = r;
10275 }
10276}
10277
10278/*
10279 * "resolve()" function
10280 */
10281 static void
10282f_resolve(typval_T *argvars, typval_T *rettv)
10283{
10284 char_u *p;
10285#ifdef HAVE_READLINK
10286 char_u *buf = NULL;
10287#endif
10288
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010289 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010290#ifdef FEAT_SHORTCUT
10291 {
10292 char_u *v = NULL;
10293
Bram Moolenaardce1e892019-02-10 23:18:53 +010010294 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010295 if (v != NULL)
10296 rettv->vval.v_string = v;
10297 else
10298 rettv->vval.v_string = vim_strsave(p);
10299 }
10300#else
10301# ifdef HAVE_READLINK
10302 {
10303 char_u *cpy;
10304 int len;
10305 char_u *remain = NULL;
10306 char_u *q;
10307 int is_relative_to_current = FALSE;
10308 int has_trailing_pathsep = FALSE;
10309 int limit = 100;
10310
10311 p = vim_strsave(p);
10312
10313 if (p[0] == '.' && (vim_ispathsep(p[1])
10314 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10315 is_relative_to_current = TRUE;
10316
10317 len = STRLEN(p);
10318 if (len > 0 && after_pathsep(p, p + len))
10319 {
10320 has_trailing_pathsep = TRUE;
10321 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10322 }
10323
10324 q = getnextcomp(p);
10325 if (*q != NUL)
10326 {
10327 /* Separate the first path component in "p", and keep the
10328 * remainder (beginning with the path separator). */
10329 remain = vim_strsave(q - 1);
10330 q[-1] = NUL;
10331 }
10332
10333 buf = alloc(MAXPATHL + 1);
10334 if (buf == NULL)
10335 goto fail;
10336
10337 for (;;)
10338 {
10339 for (;;)
10340 {
10341 len = readlink((char *)p, (char *)buf, MAXPATHL);
10342 if (len <= 0)
10343 break;
10344 buf[len] = NUL;
10345
10346 if (limit-- == 0)
10347 {
10348 vim_free(p);
10349 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010350 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010351 rettv->vval.v_string = NULL;
10352 goto fail;
10353 }
10354
10355 /* Ensure that the result will have a trailing path separator
10356 * if the argument has one. */
10357 if (remain == NULL && has_trailing_pathsep)
10358 add_pathsep(buf);
10359
10360 /* Separate the first path component in the link value and
10361 * concatenate the remainders. */
10362 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10363 if (*q != NUL)
10364 {
10365 if (remain == NULL)
10366 remain = vim_strsave(q - 1);
10367 else
10368 {
10369 cpy = concat_str(q - 1, remain);
10370 if (cpy != NULL)
10371 {
10372 vim_free(remain);
10373 remain = cpy;
10374 }
10375 }
10376 q[-1] = NUL;
10377 }
10378
10379 q = gettail(p);
10380 if (q > p && *q == NUL)
10381 {
10382 /* Ignore trailing path separator. */
10383 q[-1] = NUL;
10384 q = gettail(p);
10385 }
10386 if (q > p && !mch_isFullName(buf))
10387 {
10388 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010389 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010390 if (cpy != NULL)
10391 {
10392 STRCPY(cpy, p);
10393 STRCPY(gettail(cpy), buf);
10394 vim_free(p);
10395 p = cpy;
10396 }
10397 }
10398 else
10399 {
10400 vim_free(p);
10401 p = vim_strsave(buf);
10402 }
10403 }
10404
10405 if (remain == NULL)
10406 break;
10407
10408 /* Append the first path component of "remain" to "p". */
10409 q = getnextcomp(remain + 1);
10410 len = q - remain - (*q != NUL);
10411 cpy = vim_strnsave(p, STRLEN(p) + len);
10412 if (cpy != NULL)
10413 {
10414 STRNCAT(cpy, remain, len);
10415 vim_free(p);
10416 p = cpy;
10417 }
10418 /* Shorten "remain". */
10419 if (*q != NUL)
10420 STRMOVE(remain, q - 1);
10421 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010422 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010423 }
10424
10425 /* If the result is a relative path name, make it explicitly relative to
10426 * the current directory if and only if the argument had this form. */
10427 if (!vim_ispathsep(*p))
10428 {
10429 if (is_relative_to_current
10430 && *p != NUL
10431 && !(p[0] == '.'
10432 && (p[1] == NUL
10433 || vim_ispathsep(p[1])
10434 || (p[1] == '.'
10435 && (p[2] == NUL
10436 || vim_ispathsep(p[2]))))))
10437 {
10438 /* Prepend "./". */
10439 cpy = concat_str((char_u *)"./", p);
10440 if (cpy != NULL)
10441 {
10442 vim_free(p);
10443 p = cpy;
10444 }
10445 }
10446 else if (!is_relative_to_current)
10447 {
10448 /* Strip leading "./". */
10449 q = p;
10450 while (q[0] == '.' && vim_ispathsep(q[1]))
10451 q += 2;
10452 if (q > p)
10453 STRMOVE(p, p + 2);
10454 }
10455 }
10456
10457 /* Ensure that the result will have no trailing path separator
10458 * if the argument had none. But keep "/" or "//". */
10459 if (!has_trailing_pathsep)
10460 {
10461 q = p + STRLEN(p);
10462 if (after_pathsep(p, q))
10463 *gettail_sep(p) = NUL;
10464 }
10465
10466 rettv->vval.v_string = p;
10467 }
10468# else
10469 rettv->vval.v_string = vim_strsave(p);
10470# endif
10471#endif
10472
10473 simplify_filename(rettv->vval.v_string);
10474
10475#ifdef HAVE_READLINK
10476fail:
10477 vim_free(buf);
10478#endif
10479 rettv->v_type = VAR_STRING;
10480}
10481
10482/*
10483 * "reverse({list})" function
10484 */
10485 static void
10486f_reverse(typval_T *argvars, typval_T *rettv)
10487{
10488 list_T *l;
10489 listitem_T *li, *ni;
10490
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010491 if (argvars[0].v_type == VAR_BLOB)
10492 {
10493 blob_T *b = argvars[0].vval.v_blob;
10494 int i, len = blob_len(b);
10495
10496 for (i = 0; i < len / 2; i++)
10497 {
10498 int tmp = blob_get(b, i);
10499
10500 blob_set(b, i, blob_get(b, len - i - 1));
10501 blob_set(b, len - i - 1, tmp);
10502 }
10503 rettv_blob_set(rettv, b);
10504 return;
10505 }
10506
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010507 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010508 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010509 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010510 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010511 (char_u *)N_("reverse() argument"), TRUE))
10512 {
10513 li = l->lv_last;
10514 l->lv_first = l->lv_last = NULL;
10515 l->lv_len = 0;
10516 while (li != NULL)
10517 {
10518 ni = li->li_prev;
10519 list_append(l, li);
10520 li = ni;
10521 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010522 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010523 l->lv_idx = l->lv_len - l->lv_idx - 1;
10524 }
10525}
10526
10527#define SP_NOMOVE 0x01 /* don't move cursor */
10528#define SP_REPEAT 0x02 /* repeat to find outer pair */
10529#define SP_RETCOUNT 0x04 /* return matchcount */
10530#define SP_SETPCMARK 0x08 /* set previous context mark */
10531#define SP_START 0x10 /* accept match at start position */
10532#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10533#define SP_END 0x40 /* leave cursor at end of match */
10534#define SP_COLUMN 0x80 /* start at cursor column */
10535
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010536/*
10537 * Get flags for a search function.
10538 * Possibly sets "p_ws".
10539 * Returns BACKWARD, FORWARD or zero (for an error).
10540 */
10541 static int
10542get_search_arg(typval_T *varp, int *flagsp)
10543{
10544 int dir = FORWARD;
10545 char_u *flags;
10546 char_u nbuf[NUMBUFLEN];
10547 int mask;
10548
10549 if (varp->v_type != VAR_UNKNOWN)
10550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010551 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 if (flags == NULL)
10553 return 0; /* type error; errmsg already given */
10554 while (*flags != NUL)
10555 {
10556 switch (*flags)
10557 {
10558 case 'b': dir = BACKWARD; break;
10559 case 'w': p_ws = TRUE; break;
10560 case 'W': p_ws = FALSE; break;
10561 default: mask = 0;
10562 if (flagsp != NULL)
10563 switch (*flags)
10564 {
10565 case 'c': mask = SP_START; break;
10566 case 'e': mask = SP_END; break;
10567 case 'm': mask = SP_RETCOUNT; break;
10568 case 'n': mask = SP_NOMOVE; break;
10569 case 'p': mask = SP_SUBPAT; break;
10570 case 'r': mask = SP_REPEAT; break;
10571 case 's': mask = SP_SETPCMARK; break;
10572 case 'z': mask = SP_COLUMN; break;
10573 }
10574 if (mask == 0)
10575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010576 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577 dir = 0;
10578 }
10579 else
10580 *flagsp |= mask;
10581 }
10582 if (dir == 0)
10583 break;
10584 ++flags;
10585 }
10586 }
10587 return dir;
10588}
10589
10590/*
10591 * Shared by search() and searchpos() functions.
10592 */
10593 static int
10594search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10595{
10596 int flags;
10597 char_u *pat;
10598 pos_T pos;
10599 pos_T save_cursor;
10600 int save_p_ws = p_ws;
10601 int dir;
10602 int retval = 0; /* default: FAIL */
10603 long lnum_stop = 0;
10604 proftime_T tm;
10605#ifdef FEAT_RELTIME
10606 long time_limit = 0;
10607#endif
10608 int options = SEARCH_KEEP;
10609 int subpatnum;
10610
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010611 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010612 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10613 if (dir == 0)
10614 goto theend;
10615 flags = *flagsp;
10616 if (flags & SP_START)
10617 options |= SEARCH_START;
10618 if (flags & SP_END)
10619 options |= SEARCH_END;
10620 if (flags & SP_COLUMN)
10621 options |= SEARCH_COL;
10622
10623 /* Optional arguments: line number to stop searching and timeout. */
10624 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10625 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010626 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010627 if (lnum_stop < 0)
10628 goto theend;
10629#ifdef FEAT_RELTIME
10630 if (argvars[3].v_type != VAR_UNKNOWN)
10631 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010632 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010633 if (time_limit < 0)
10634 goto theend;
10635 }
10636#endif
10637 }
10638
10639#ifdef FEAT_RELTIME
10640 /* Set the time limit, if there is one. */
10641 profile_setlimit(time_limit, &tm);
10642#endif
10643
10644 /*
10645 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10646 * Check to make sure only those flags are set.
10647 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10648 * flags cannot be set. Check for that condition also.
10649 */
10650 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10651 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010653 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010654 goto theend;
10655 }
10656
10657 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010658 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010659 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010660 if (subpatnum != FAIL)
10661 {
10662 if (flags & SP_SUBPAT)
10663 retval = subpatnum;
10664 else
10665 retval = pos.lnum;
10666 if (flags & SP_SETPCMARK)
10667 setpcmark();
10668 curwin->w_cursor = pos;
10669 if (match_pos != NULL)
10670 {
10671 /* Store the match cursor position */
10672 match_pos->lnum = pos.lnum;
10673 match_pos->col = pos.col + 1;
10674 }
10675 /* "/$" will put the cursor after the end of the line, may need to
10676 * correct that here */
10677 check_cursor();
10678 }
10679
10680 /* If 'n' flag is used: restore cursor position. */
10681 if (flags & SP_NOMOVE)
10682 curwin->w_cursor = save_cursor;
10683 else
10684 curwin->w_set_curswant = TRUE;
10685theend:
10686 p_ws = save_p_ws;
10687
10688 return retval;
10689}
10690
10691#ifdef FEAT_FLOAT
10692
10693/*
10694 * round() is not in C90, use ceil() or floor() instead.
10695 */
10696 float_T
10697vim_round(float_T f)
10698{
10699 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10700}
10701
10702/*
10703 * "round({float})" function
10704 */
10705 static void
10706f_round(typval_T *argvars, typval_T *rettv)
10707{
10708 float_T f = 0.0;
10709
10710 rettv->v_type = VAR_FLOAT;
10711 if (get_float_arg(argvars, &f) == OK)
10712 rettv->vval.v_float = vim_round(f);
10713 else
10714 rettv->vval.v_float = 0.0;
10715}
10716#endif
10717
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010718#ifdef FEAT_RUBY
10719/*
10720 * "rubyeval()" function
10721 */
10722 static void
10723f_rubyeval(typval_T *argvars, typval_T *rettv)
10724{
10725 char_u *str;
10726 char_u buf[NUMBUFLEN];
10727
10728 str = tv_get_string_buf(&argvars[0], buf);
10729 do_rubyeval(str, rettv);
10730}
10731#endif
10732
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010733/*
10734 * "screenattr()" function
10735 */
10736 static void
10737f_screenattr(typval_T *argvars, typval_T *rettv)
10738{
10739 int row;
10740 int col;
10741 int c;
10742
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010743 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10744 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010745 if (row < 0 || row >= screen_Rows
10746 || col < 0 || col >= screen_Columns)
10747 c = -1;
10748 else
10749 c = ScreenAttrs[LineOffset[row] + col];
10750 rettv->vval.v_number = c;
10751}
10752
10753/*
10754 * "screenchar()" function
10755 */
10756 static void
10757f_screenchar(typval_T *argvars, typval_T *rettv)
10758{
10759 int row;
10760 int col;
10761 int off;
10762 int c;
10763
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010764 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10765 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010766 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010767 c = -1;
10768 else
10769 {
10770 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010771 if (enc_utf8 && ScreenLinesUC[off] != 0)
10772 c = ScreenLinesUC[off];
10773 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774 c = ScreenLines[off];
10775 }
10776 rettv->vval.v_number = c;
10777}
10778
10779/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010780 * "screenchars()" function
10781 */
10782 static void
10783f_screenchars(typval_T *argvars, typval_T *rettv)
10784{
10785 int row;
10786 int col;
10787 int off;
10788 int c;
10789 int i;
10790
10791 if (rettv_list_alloc(rettv) == FAIL)
10792 return;
10793 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10794 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10795 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10796 return;
10797
10798 off = LineOffset[row] + col;
10799 if (enc_utf8 && ScreenLinesUC[off] != 0)
10800 c = ScreenLinesUC[off];
10801 else
10802 c = ScreenLines[off];
10803 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10804
10805 if (enc_utf8)
10806
10807 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10808 list_append_number(rettv->vval.v_list,
10809 (varnumber_T)ScreenLinesC[i][off]);
10810}
10811
10812/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010813 * "screencol()" function
10814 *
10815 * First column is 1 to be consistent with virtcol().
10816 */
10817 static void
10818f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10819{
10820 rettv->vval.v_number = screen_screencol() + 1;
10821}
10822
10823/*
10824 * "screenrow()" function
10825 */
10826 static void
10827f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10828{
10829 rettv->vval.v_number = screen_screenrow() + 1;
10830}
10831
10832/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010833 * "screenstring()" function
10834 */
10835 static void
10836f_screenstring(typval_T *argvars, typval_T *rettv)
10837{
10838 int row;
10839 int col;
10840 int off;
10841 int c;
10842 int i;
10843 char_u buf[MB_MAXBYTES + 1];
10844 int buflen = 0;
10845
10846 rettv->vval.v_string = NULL;
10847 rettv->v_type = VAR_STRING;
10848
10849 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10850 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10851 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10852 return;
10853
10854 off = LineOffset[row] + col;
10855 if (enc_utf8 && ScreenLinesUC[off] != 0)
10856 c = ScreenLinesUC[off];
10857 else
10858 c = ScreenLines[off];
10859 buflen += mb_char2bytes(c, buf);
10860
10861 if (enc_utf8)
10862 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10863 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10864
10865 buf[buflen] = NUL;
10866 rettv->vval.v_string = vim_strsave(buf);
10867}
10868
10869/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010870 * "search()" function
10871 */
10872 static void
10873f_search(typval_T *argvars, typval_T *rettv)
10874{
10875 int flags = 0;
10876
10877 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10878}
10879
10880/*
10881 * "searchdecl()" function
10882 */
10883 static void
10884f_searchdecl(typval_T *argvars, typval_T *rettv)
10885{
10886 int locally = 1;
10887 int thisblock = 0;
10888 int error = FALSE;
10889 char_u *name;
10890
10891 rettv->vval.v_number = 1; /* default: FAIL */
10892
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010893 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010894 if (argvars[1].v_type != VAR_UNKNOWN)
10895 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010896 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010897 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010898 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010899 }
10900 if (!error && name != NULL)
10901 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10902 locally, thisblock, SEARCH_KEEP) == FAIL;
10903}
10904
10905/*
10906 * Used by searchpair() and searchpairpos()
10907 */
10908 static int
10909searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10910{
10911 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010912 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010913 int save_p_ws = p_ws;
10914 int dir;
10915 int flags = 0;
10916 char_u nbuf1[NUMBUFLEN];
10917 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010918 int retval = 0; /* default: FAIL */
10919 long lnum_stop = 0;
10920 long time_limit = 0;
10921
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010922 /* Get the three pattern arguments: start, middle, end. Will result in an
10923 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010924 spat = tv_get_string_chk(&argvars[0]);
10925 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10926 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 if (spat == NULL || mpat == NULL || epat == NULL)
10928 goto theend; /* type error */
10929
10930 /* Handle the optional fourth argument: flags */
10931 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10932 if (dir == 0)
10933 goto theend;
10934
10935 /* Don't accept SP_END or SP_SUBPAT.
10936 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10937 */
10938 if ((flags & (SP_END | SP_SUBPAT)) != 0
10939 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010941 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010942 goto theend;
10943 }
10944
10945 /* Using 'r' implies 'W', otherwise it doesn't work. */
10946 if (flags & SP_REPEAT)
10947 p_ws = FALSE;
10948
10949 /* Optional fifth argument: skip expression */
10950 if (argvars[3].v_type == VAR_UNKNOWN
10951 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010952 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010953 else
10954 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010955 skip = &argvars[4];
10956 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10957 && skip->v_type != VAR_STRING)
10958 {
10959 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010960 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010961 goto theend;
10962 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010963 if (argvars[5].v_type != VAR_UNKNOWN)
10964 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010965 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010966 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010968 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010969 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010970 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010971#ifdef FEAT_RELTIME
10972 if (argvars[6].v_type != VAR_UNKNOWN)
10973 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010974 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010975 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010977 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010978 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010979 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010980 }
10981#endif
10982 }
10983 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010984
10985 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
10986 match_pos, lnum_stop, time_limit);
10987
10988theend:
10989 p_ws = save_p_ws;
10990
10991 return retval;
10992}
10993
10994/*
10995 * "searchpair()" function
10996 */
10997 static void
10998f_searchpair(typval_T *argvars, typval_T *rettv)
10999{
11000 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11001}
11002
11003/*
11004 * "searchpairpos()" function
11005 */
11006 static void
11007f_searchpairpos(typval_T *argvars, typval_T *rettv)
11008{
11009 pos_T match_pos;
11010 int lnum = 0;
11011 int col = 0;
11012
11013 if (rettv_list_alloc(rettv) == FAIL)
11014 return;
11015
11016 if (searchpair_cmn(argvars, &match_pos) > 0)
11017 {
11018 lnum = match_pos.lnum;
11019 col = match_pos.col;
11020 }
11021
11022 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11023 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11024}
11025
11026/*
11027 * Search for a start/middle/end thing.
11028 * Used by searchpair(), see its documentation for the details.
11029 * Returns 0 or -1 for no match,
11030 */
11031 long
11032do_searchpair(
11033 char_u *spat, /* start pattern */
11034 char_u *mpat, /* middle pattern */
11035 char_u *epat, /* end pattern */
11036 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011037 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011038 int flags, /* SP_SETPCMARK and other SP_ values */
11039 pos_T *match_pos,
11040 linenr_T lnum_stop, /* stop at this line if not zero */
11041 long time_limit UNUSED) /* stop after this many msec */
11042{
11043 char_u *save_cpo;
11044 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11045 long retval = 0;
11046 pos_T pos;
11047 pos_T firstpos;
11048 pos_T foundpos;
11049 pos_T save_cursor;
11050 pos_T save_pos;
11051 int n;
11052 int r;
11053 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011054 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011055 int err;
11056 int options = SEARCH_KEEP;
11057 proftime_T tm;
11058
11059 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11060 save_cpo = p_cpo;
11061 p_cpo = empty_option;
11062
11063#ifdef FEAT_RELTIME
11064 /* Set the time limit, if there is one. */
11065 profile_setlimit(time_limit, &tm);
11066#endif
11067
11068 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11069 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011070 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11071 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011072 if (pat2 == NULL || pat3 == NULL)
11073 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011074 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011075 if (*mpat == NUL)
11076 STRCPY(pat3, pat2);
11077 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011078 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 spat, epat, mpat);
11080 if (flags & SP_START)
11081 options |= SEARCH_START;
11082
Bram Moolenaar48570482017-10-30 21:48:41 +010011083 if (skip != NULL)
11084 {
11085 /* Empty string means to not use the skip expression. */
11086 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11087 use_skip = skip->vval.v_string != NULL
11088 && *skip->vval.v_string != NUL;
11089 }
11090
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011091 save_cursor = curwin->w_cursor;
11092 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011093 CLEAR_POS(&firstpos);
11094 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011095 pat = pat3;
11096 for (;;)
11097 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011098 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011099 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011100 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011101 /* didn't find it or found the first match again: FAIL */
11102 break;
11103
11104 if (firstpos.lnum == 0)
11105 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011106 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011107 {
11108 /* Found the same position again. Can happen with a pattern that
11109 * has "\zs" at the end and searching backwards. Advance one
11110 * character and try again. */
11111 if (dir == BACKWARD)
11112 decl(&pos);
11113 else
11114 incl(&pos);
11115 }
11116 foundpos = pos;
11117
11118 /* clear the start flag to avoid getting stuck here */
11119 options &= ~SEARCH_START;
11120
11121 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011122 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011123 {
11124 save_pos = curwin->w_cursor;
11125 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011126 err = FALSE;
11127 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011128 curwin->w_cursor = save_pos;
11129 if (err)
11130 {
11131 /* Evaluating {skip} caused an error, break here. */
11132 curwin->w_cursor = save_cursor;
11133 retval = -1;
11134 break;
11135 }
11136 if (r)
11137 continue;
11138 }
11139
11140 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11141 {
11142 /* Found end when searching backwards or start when searching
11143 * forward: nested pair. */
11144 ++nest;
11145 pat = pat2; /* nested, don't search for middle */
11146 }
11147 else
11148 {
11149 /* Found end when searching forward or start when searching
11150 * backward: end of (nested) pair; or found middle in outer pair. */
11151 if (--nest == 1)
11152 pat = pat3; /* outer level, search for middle */
11153 }
11154
11155 if (nest == 0)
11156 {
11157 /* Found the match: return matchcount or line number. */
11158 if (flags & SP_RETCOUNT)
11159 ++retval;
11160 else
11161 retval = pos.lnum;
11162 if (flags & SP_SETPCMARK)
11163 setpcmark();
11164 curwin->w_cursor = pos;
11165 if (!(flags & SP_REPEAT))
11166 break;
11167 nest = 1; /* search for next unmatched */
11168 }
11169 }
11170
11171 if (match_pos != NULL)
11172 {
11173 /* Store the match cursor position */
11174 match_pos->lnum = curwin->w_cursor.lnum;
11175 match_pos->col = curwin->w_cursor.col + 1;
11176 }
11177
11178 /* If 'n' flag is used or search failed: restore cursor position. */
11179 if ((flags & SP_NOMOVE) || retval == 0)
11180 curwin->w_cursor = save_cursor;
11181
11182theend:
11183 vim_free(pat2);
11184 vim_free(pat3);
11185 if (p_cpo == empty_option)
11186 p_cpo = save_cpo;
11187 else
11188 /* Darn, evaluating the {skip} expression changed the value. */
11189 free_string_option(save_cpo);
11190
11191 return retval;
11192}
11193
11194/*
11195 * "searchpos()" function
11196 */
11197 static void
11198f_searchpos(typval_T *argvars, typval_T *rettv)
11199{
11200 pos_T match_pos;
11201 int lnum = 0;
11202 int col = 0;
11203 int n;
11204 int flags = 0;
11205
11206 if (rettv_list_alloc(rettv) == FAIL)
11207 return;
11208
11209 n = search_cmn(argvars, &match_pos, &flags);
11210 if (n > 0)
11211 {
11212 lnum = match_pos.lnum;
11213 col = match_pos.col;
11214 }
11215
11216 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11217 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11218 if (flags & SP_SUBPAT)
11219 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11220}
11221
11222 static void
11223f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11224{
11225#ifdef FEAT_CLIENTSERVER
11226 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011227 char_u *server = tv_get_string_chk(&argvars[0]);
11228 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011229
11230 rettv->vval.v_number = -1;
11231 if (server == NULL || reply == NULL)
11232 return;
11233 if (check_restricted() || check_secure())
11234 return;
11235# ifdef FEAT_X11
11236 if (check_connection() == FAIL)
11237 return;
11238# endif
11239
11240 if (serverSendReply(server, reply) < 0)
11241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011242 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011243 return;
11244 }
11245 rettv->vval.v_number = 0;
11246#else
11247 rettv->vval.v_number = -1;
11248#endif
11249}
11250
11251 static void
11252f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11253{
11254 char_u *r = NULL;
11255
11256#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011257# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011258 r = serverGetVimNames();
11259# else
11260 make_connection();
11261 if (X_DISPLAY != NULL)
11262 r = serverGetVimNames(X_DISPLAY);
11263# endif
11264#endif
11265 rettv->v_type = VAR_STRING;
11266 rettv->vval.v_string = r;
11267}
11268
11269/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011270 * "setbufline()" function
11271 */
11272 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011273f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011274{
11275 linenr_T lnum;
11276 buf_T *buf;
11277
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011278 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011279 if (buf == NULL)
11280 rettv->vval.v_number = 1; /* FAIL */
11281 else
11282 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011283 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011284 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011285 }
11286}
11287
11288/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011289 * "setbufvar()" function
11290 */
11291 static void
11292f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11293{
11294 buf_T *buf;
11295 char_u *varname, *bufvarname;
11296 typval_T *varp;
11297 char_u nbuf[NUMBUFLEN];
11298
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011299 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011300 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011301 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11302 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011303 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011304 varp = &argvars[2];
11305
11306 if (buf != NULL && varname != NULL && varp != NULL)
11307 {
11308 if (*varname == '&')
11309 {
11310 long numval;
11311 char_u *strval;
11312 int error = FALSE;
11313 aco_save_T aco;
11314
11315 /* set curbuf to be our buf, temporarily */
11316 aucmd_prepbuf(&aco, buf);
11317
11318 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011319 numval = (long)tv_get_number_chk(varp, &error);
11320 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011321 if (!error && strval != NULL)
11322 set_option_value(varname, numval, strval, OPT_LOCAL);
11323
11324 /* reset notion of buffer */
11325 aucmd_restbuf(&aco);
11326 }
11327 else
11328 {
11329 buf_T *save_curbuf = curbuf;
11330
Bram Moolenaar964b3742019-05-24 18:54:09 +020011331 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011332 if (bufvarname != NULL)
11333 {
11334 curbuf = buf;
11335 STRCPY(bufvarname, "b:");
11336 STRCPY(bufvarname + 2, varname);
11337 set_var(bufvarname, varp, TRUE);
11338 vim_free(bufvarname);
11339 curbuf = save_curbuf;
11340 }
11341 }
11342 }
11343}
11344
11345 static void
11346f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11347{
11348 dict_T *d;
11349 dictitem_T *di;
11350 char_u *csearch;
11351
11352 if (argvars[0].v_type != VAR_DICT)
11353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011354 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011355 return;
11356 }
11357
11358 if ((d = argvars[0].vval.v_dict) != NULL)
11359 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011360 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011361 if (csearch != NULL)
11362 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011363 if (enc_utf8)
11364 {
11365 int pcc[MAX_MCO];
11366 int c = utfc_ptr2char(csearch, pcc);
11367
11368 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11369 }
11370 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011371 set_last_csearch(PTR2CHAR(csearch),
11372 csearch, MB_PTR2LEN(csearch));
11373 }
11374
11375 di = dict_find(d, (char_u *)"forward", -1);
11376 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011377 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011378 ? FORWARD : BACKWARD);
11379
11380 di = dict_find(d, (char_u *)"until", -1);
11381 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011382 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011383 }
11384}
11385
11386/*
11387 * "setcmdpos()" function
11388 */
11389 static void
11390f_setcmdpos(typval_T *argvars, typval_T *rettv)
11391{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011392 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393
11394 if (pos >= 0)
11395 rettv->vval.v_number = set_cmdline_pos(pos);
11396}
11397
11398/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011399 * "setenv()" function
11400 */
11401 static void
11402f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11403{
11404 char_u namebuf[NUMBUFLEN];
11405 char_u valbuf[NUMBUFLEN];
11406 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11407
11408 if (argvars[1].v_type == VAR_SPECIAL
11409 && argvars[1].vval.v_number == VVAL_NULL)
11410 vim_unsetenv(name);
11411 else
11412 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11413}
11414
11415/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011416 * "setfperm({fname}, {mode})" function
11417 */
11418 static void
11419f_setfperm(typval_T *argvars, typval_T *rettv)
11420{
11421 char_u *fname;
11422 char_u modebuf[NUMBUFLEN];
11423 char_u *mode_str;
11424 int i;
11425 int mask;
11426 int mode = 0;
11427
11428 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011429 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011430 if (fname == NULL)
11431 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011432 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011433 if (mode_str == NULL)
11434 return;
11435 if (STRLEN(mode_str) != 9)
11436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011437 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011438 return;
11439 }
11440
11441 mask = 1;
11442 for (i = 8; i >= 0; --i)
11443 {
11444 if (mode_str[i] != '-')
11445 mode |= mask;
11446 mask = mask << 1;
11447 }
11448 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11449}
11450
11451/*
11452 * "setline()" function
11453 */
11454 static void
11455f_setline(typval_T *argvars, typval_T *rettv)
11456{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011457 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011458
Bram Moolenaarca851592018-06-06 21:04:07 +020011459 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460}
11461
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011462/*
11463 * Used by "setqflist()" and "setloclist()" functions
11464 */
11465 static void
11466set_qf_ll_list(
11467 win_T *wp UNUSED,
11468 typval_T *list_arg UNUSED,
11469 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011470 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 typval_T *rettv)
11472{
11473#ifdef FEAT_QUICKFIX
11474 static char *e_invact = N_("E927: Invalid action: '%s'");
11475 char_u *act;
11476 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011477 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011478#endif
11479
11480 rettv->vval.v_number = -1;
11481
11482#ifdef FEAT_QUICKFIX
11483 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011484 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011485 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011486 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011487 else
11488 {
11489 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011490 dict_T *d = NULL;
11491 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492
11493 if (action_arg->v_type == VAR_STRING)
11494 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011495 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011496 if (act == NULL)
11497 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011498 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11499 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011500 action = *act;
11501 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011502 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503 }
11504 else if (action_arg->v_type == VAR_UNKNOWN)
11505 action = ' ';
11506 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011507 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508
Bram Moolenaard823fa92016-08-12 16:29:27 +020011509 if (action_arg->v_type != VAR_UNKNOWN
11510 && what_arg->v_type != VAR_UNKNOWN)
11511 {
11512 if (what_arg->v_type == VAR_DICT)
11513 d = what_arg->vval.v_dict;
11514 else
11515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011516 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011517 valid_dict = FALSE;
11518 }
11519 }
11520
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011521 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011522 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011523 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11524 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011525 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011526 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011527 }
11528#endif
11529}
11530
11531/*
11532 * "setloclist()" function
11533 */
11534 static void
11535f_setloclist(typval_T *argvars, typval_T *rettv)
11536{
11537 win_T *win;
11538
11539 rettv->vval.v_number = -1;
11540
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011541 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011542 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011543 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544}
11545
11546/*
11547 * "setmatches()" function
11548 */
11549 static void
11550f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11551{
11552#ifdef FEAT_SEARCH_EXTRA
11553 list_T *l;
11554 listitem_T *li;
11555 dict_T *d;
11556 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011557 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011558
11559 rettv->vval.v_number = -1;
11560 if (argvars[0].v_type != VAR_LIST)
11561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011562 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011563 return;
11564 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011565 if (win == NULL)
11566 return;
11567
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 if ((l = argvars[0].vval.v_list) != NULL)
11569 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011570 /* To some extent make sure that we are dealing with a list from
11571 * "getmatches()". */
11572 li = l->lv_first;
11573 while (li != NULL)
11574 {
11575 if (li->li_tv.v_type != VAR_DICT
11576 || (d = li->li_tv.vval.v_dict) == NULL)
11577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011578 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011579 return;
11580 }
11581 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11582 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11583 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11584 && dict_find(d, (char_u *)"priority", -1) != NULL
11585 && dict_find(d, (char_u *)"id", -1) != NULL))
11586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011587 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011588 return;
11589 }
11590 li = li->li_next;
11591 }
11592
Bram Moolenaaraff74912019-03-30 18:11:49 +010011593 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011594 li = l->lv_first;
11595 while (li != NULL)
11596 {
11597 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011598 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011599 dictitem_T *di;
11600 char_u *group;
11601 int priority;
11602 int id;
11603 char_u *conceal;
11604
11605 d = li->li_tv.vval.v_dict;
11606 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11607 {
11608 if (s == NULL)
11609 {
11610 s = list_alloc();
11611 if (s == NULL)
11612 return;
11613 }
11614
11615 /* match from matchaddpos() */
11616 for (i = 1; i < 9; i++)
11617 {
11618 sprintf((char *)buf, (char *)"pos%d", i);
11619 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11620 {
11621 if (di->di_tv.v_type != VAR_LIST)
11622 return;
11623
11624 list_append_tv(s, &di->di_tv);
11625 s->lv_refcount++;
11626 }
11627 else
11628 break;
11629 }
11630 }
11631
Bram Moolenaar8f667172018-12-14 15:38:31 +010011632 group = dict_get_string(d, (char_u *)"group", TRUE);
11633 priority = (int)dict_get_number(d, (char_u *)"priority");
11634 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011635 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011636 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011637 : NULL;
11638 if (i == 0)
11639 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011640 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011641 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011642 priority, id, NULL, conceal);
11643 }
11644 else
11645 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011646 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011647 list_unref(s);
11648 s = NULL;
11649 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011650 vim_free(group);
11651 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011652
11653 li = li->li_next;
11654 }
11655 rettv->vval.v_number = 0;
11656 }
11657#endif
11658}
11659
11660/*
11661 * "setpos()" function
11662 */
11663 static void
11664f_setpos(typval_T *argvars, typval_T *rettv)
11665{
11666 pos_T pos;
11667 int fnum;
11668 char_u *name;
11669 colnr_T curswant = -1;
11670
11671 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011672 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011673 if (name != NULL)
11674 {
11675 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11676 {
11677 if (--pos.col < 0)
11678 pos.col = 0;
11679 if (name[0] == '.' && name[1] == NUL)
11680 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011681 /* set cursor; "fnum" is ignored */
11682 curwin->w_cursor = pos;
11683 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011684 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011685 curwin->w_curswant = curswant - 1;
11686 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011687 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011688 check_cursor();
11689 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011690 }
11691 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11692 {
11693 /* set mark */
11694 if (setmark_pos(name[1], &pos, fnum) == OK)
11695 rettv->vval.v_number = 0;
11696 }
11697 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011698 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011699 }
11700 }
11701}
11702
11703/*
11704 * "setqflist()" function
11705 */
11706 static void
11707f_setqflist(typval_T *argvars, typval_T *rettv)
11708{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011709 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011710}
11711
11712/*
11713 * "setreg()" function
11714 */
11715 static void
11716f_setreg(typval_T *argvars, typval_T *rettv)
11717{
11718 int regname;
11719 char_u *strregname;
11720 char_u *stropt;
11721 char_u *strval;
11722 int append;
11723 char_u yank_type;
11724 long block_len;
11725
11726 block_len = -1;
11727 yank_type = MAUTO;
11728 append = FALSE;
11729
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011730 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011731 rettv->vval.v_number = 1; /* FAIL is default */
11732
11733 if (strregname == NULL)
11734 return; /* type error; errmsg already given */
11735 regname = *strregname;
11736 if (regname == 0 || regname == '@')
11737 regname = '"';
11738
11739 if (argvars[2].v_type != VAR_UNKNOWN)
11740 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011741 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011742 if (stropt == NULL)
11743 return; /* type error */
11744 for (; *stropt != NUL; ++stropt)
11745 switch (*stropt)
11746 {
11747 case 'a': case 'A': /* append */
11748 append = TRUE;
11749 break;
11750 case 'v': case 'c': /* character-wise selection */
11751 yank_type = MCHAR;
11752 break;
11753 case 'V': case 'l': /* line-wise selection */
11754 yank_type = MLINE;
11755 break;
11756 case 'b': case Ctrl_V: /* block-wise selection */
11757 yank_type = MBLOCK;
11758 if (VIM_ISDIGIT(stropt[1]))
11759 {
11760 ++stropt;
11761 block_len = getdigits(&stropt) - 1;
11762 --stropt;
11763 }
11764 break;
11765 }
11766 }
11767
11768 if (argvars[1].v_type == VAR_LIST)
11769 {
11770 char_u **lstval;
11771 char_u **allocval;
11772 char_u buf[NUMBUFLEN];
11773 char_u **curval;
11774 char_u **curallocval;
11775 list_T *ll = argvars[1].vval.v_list;
11776 listitem_T *li;
11777 int len;
11778
11779 /* If the list is NULL handle like an empty list. */
11780 len = ll == NULL ? 0 : ll->lv_len;
11781
11782 /* First half: use for pointers to result lines; second half: use for
11783 * pointers to allocated copies. */
11784 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
11785 if (lstval == NULL)
11786 return;
11787 curval = lstval;
11788 allocval = lstval + len + 2;
11789 curallocval = allocval;
11790
11791 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11792 li = li->li_next)
11793 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011794 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011795 if (strval == NULL)
11796 goto free_lstval;
11797 if (strval == buf)
11798 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011799 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011800 * overwrite the string. */
11801 strval = vim_strsave(buf);
11802 if (strval == NULL)
11803 goto free_lstval;
11804 *curallocval++ = strval;
11805 }
11806 *curval++ = strval;
11807 }
11808 *curval++ = NULL;
11809
11810 write_reg_contents_lst(regname, lstval, -1,
11811 append, yank_type, block_len);
11812free_lstval:
11813 while (curallocval > allocval)
11814 vim_free(*--curallocval);
11815 vim_free(lstval);
11816 }
11817 else
11818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011819 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011820 if (strval == NULL)
11821 return;
11822 write_reg_contents_ex(regname, strval, -1,
11823 append, yank_type, block_len);
11824 }
11825 rettv->vval.v_number = 0;
11826}
11827
11828/*
11829 * "settabvar()" function
11830 */
11831 static void
11832f_settabvar(typval_T *argvars, typval_T *rettv)
11833{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011834 tabpage_T *save_curtab;
11835 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011836 char_u *varname, *tabvarname;
11837 typval_T *varp;
11838
11839 rettv->vval.v_number = 0;
11840
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011841 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011842 return;
11843
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011844 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11845 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011846 varp = &argvars[2];
11847
Bram Moolenaar4033c552017-09-16 20:54:51 +020011848 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011849 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 save_curtab = curtab;
11851 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011852
Bram Moolenaar964b3742019-05-24 18:54:09 +020011853 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011854 if (tabvarname != NULL)
11855 {
11856 STRCPY(tabvarname, "t:");
11857 STRCPY(tabvarname + 2, varname);
11858 set_var(tabvarname, varp, TRUE);
11859 vim_free(tabvarname);
11860 }
11861
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011862 /* Restore current tabpage */
11863 if (valid_tabpage(save_curtab))
11864 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011865 }
11866}
11867
11868/*
11869 * "settabwinvar()" function
11870 */
11871 static void
11872f_settabwinvar(typval_T *argvars, typval_T *rettv)
11873{
11874 setwinvar(argvars, rettv, 1);
11875}
11876
11877/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011878 * "settagstack()" function
11879 */
11880 static void
11881f_settagstack(typval_T *argvars, typval_T *rettv)
11882{
11883 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11884 win_T *wp;
11885 dict_T *d;
11886 int action = 'r';
11887
11888 rettv->vval.v_number = -1;
11889
11890 // first argument: window number or id
11891 wp = find_win_by_nr_or_id(&argvars[0]);
11892 if (wp == NULL)
11893 return;
11894
11895 // second argument: dict with items to set in the tag stack
11896 if (argvars[1].v_type != VAR_DICT)
11897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011898 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011899 return;
11900 }
11901 d = argvars[1].vval.v_dict;
11902 if (d == NULL)
11903 return;
11904
11905 // third argument: action - 'a' for append and 'r' for replace.
11906 // default is to replace the stack.
11907 if (argvars[2].v_type == VAR_UNKNOWN)
11908 action = 'r';
11909 else if (argvars[2].v_type == VAR_STRING)
11910 {
11911 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011912 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011913 if (actstr == NULL)
11914 return;
11915 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11916 action = *actstr;
11917 else
11918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011919 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011920 return;
11921 }
11922 }
11923 else
11924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011925 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011926 return;
11927 }
11928
11929 if (set_tagstack(wp, d, action) == OK)
11930 rettv->vval.v_number = 0;
11931}
11932
11933/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011934 * "setwinvar()" function
11935 */
11936 static void
11937f_setwinvar(typval_T *argvars, typval_T *rettv)
11938{
11939 setwinvar(argvars, rettv, 0);
11940}
11941
11942#ifdef FEAT_CRYPT
11943/*
11944 * "sha256({string})" function
11945 */
11946 static void
11947f_sha256(typval_T *argvars, typval_T *rettv)
11948{
11949 char_u *p;
11950
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011951 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011952 rettv->vval.v_string = vim_strsave(
11953 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11954 rettv->v_type = VAR_STRING;
11955}
11956#endif /* FEAT_CRYPT */
11957
11958/*
11959 * "shellescape({string})" function
11960 */
11961 static void
11962f_shellescape(typval_T *argvars, typval_T *rettv)
11963{
Bram Moolenaar20615522017-06-05 18:46:26 +020011964 int do_special = non_zero_arg(&argvars[1]);
11965
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011966 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011967 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011968 rettv->v_type = VAR_STRING;
11969}
11970
11971/*
11972 * shiftwidth() function
11973 */
11974 static void
11975f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
11976{
Bram Moolenaarf9514162018-11-22 03:08:29 +010011977 rettv->vval.v_number = 0;
11978
11979 if (argvars[0].v_type != VAR_UNKNOWN)
11980 {
11981 long col;
11982
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011983 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010011984 if (col < 0)
11985 return; // type error; errmsg already given
11986#ifdef FEAT_VARTABS
11987 rettv->vval.v_number = get_sw_value_col(curbuf, col);
11988 return;
11989#endif
11990 }
11991
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011992 rettv->vval.v_number = get_sw_value(curbuf);
11993}
11994
Bram Moolenaar162b7142018-12-21 15:17:36 +010011995#ifdef FEAT_SIGNS
11996/*
11997 * "sign_define()" function
11998 */
11999 static void
12000f_sign_define(typval_T *argvars, typval_T *rettv)
12001{
12002 char_u *name;
12003 dict_T *dict;
12004 char_u *icon = NULL;
12005 char_u *linehl = NULL;
12006 char_u *text = NULL;
12007 char_u *texthl = NULL;
12008
12009 rettv->vval.v_number = -1;
12010
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012011 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012012 if (name == NULL)
12013 return;
12014
12015 if (argvars[1].v_type != VAR_UNKNOWN)
12016 {
12017 if (argvars[1].v_type != VAR_DICT)
12018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012019 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012020 return;
12021 }
12022
12023 // sign attributes
12024 dict = argvars[1].vval.v_dict;
12025 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12026 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12027 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12028 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12029 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12030 text = dict_get_string(dict, (char_u *)"text", TRUE);
12031 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12032 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12033 }
12034
12035 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12036 rettv->vval.v_number = 0;
12037
12038 vim_free(icon);
12039 vim_free(linehl);
12040 vim_free(text);
12041 vim_free(texthl);
12042}
12043
12044/*
12045 * "sign_getdefined()" function
12046 */
12047 static void
12048f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12049{
12050 char_u *name = NULL;
12051
12052 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12053 return;
12054
12055 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012056 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012057
12058 sign_getlist(name, rettv->vval.v_list);
12059}
12060
12061/*
12062 * "sign_getplaced()" function
12063 */
12064 static void
12065f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12066{
12067 buf_T *buf = NULL;
12068 dict_T *dict;
12069 dictitem_T *di;
12070 linenr_T lnum = 0;
12071 int sign_id = 0;
12072 char_u *group = NULL;
12073 int notanum = FALSE;
12074
12075 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12076 return;
12077
12078 if (argvars[0].v_type != VAR_UNKNOWN)
12079 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012080 // get signs placed in the specified buffer
12081 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012082 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012083 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012084
12085 if (argvars[1].v_type != VAR_UNKNOWN)
12086 {
12087 if (argvars[1].v_type != VAR_DICT ||
12088 ((dict = argvars[1].vval.v_dict) == NULL))
12089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012090 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012091 return;
12092 }
12093 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12094 {
12095 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012096 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012097 if (notanum)
12098 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012099 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012100 }
12101 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12102 {
12103 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012104 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012105 if (notanum)
12106 return;
12107 }
12108 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12109 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012110 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012111 if (group == NULL)
12112 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012113 if (*group == '\0') // empty string means global group
12114 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012115 }
12116 }
12117 }
12118
12119 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12120}
12121
12122/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012123 * "sign_jump()" function
12124 */
12125 static void
12126f_sign_jump(typval_T *argvars, typval_T *rettv)
12127{
12128 int sign_id;
12129 char_u *sign_group = NULL;
12130 buf_T *buf;
12131 int notanum = FALSE;
12132
12133 rettv->vval.v_number = -1;
12134
Bram Moolenaarbdace832019-03-02 10:13:42 +010012135 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012136 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12137 if (notanum)
12138 return;
12139 if (sign_id <= 0)
12140 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012141 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012142 return;
12143 }
12144
12145 // Sign group
12146 sign_group = tv_get_string_chk(&argvars[1]);
12147 if (sign_group == NULL)
12148 return;
12149 if (sign_group[0] == '\0')
12150 sign_group = NULL; // global sign group
12151 else
12152 {
12153 sign_group = vim_strsave(sign_group);
12154 if (sign_group == NULL)
12155 return;
12156 }
12157
12158 // Buffer to place the sign
12159 buf = get_buf_arg(&argvars[2]);
12160 if (buf == NULL)
12161 goto cleanup;
12162
12163 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12164
12165cleanup:
12166 vim_free(sign_group);
12167}
12168
12169/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012170 * "sign_place()" function
12171 */
12172 static void
12173f_sign_place(typval_T *argvars, typval_T *rettv)
12174{
12175 int sign_id;
12176 char_u *group = NULL;
12177 char_u *sign_name;
12178 buf_T *buf;
12179 dict_T *dict;
12180 dictitem_T *di;
12181 linenr_T lnum = 0;
12182 int prio = SIGN_DEF_PRIO;
12183 int notanum = FALSE;
12184
12185 rettv->vval.v_number = -1;
12186
Bram Moolenaarbdace832019-03-02 10:13:42 +010012187 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012188 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012189 if (notanum)
12190 return;
12191 if (sign_id < 0)
12192 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012193 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012194 return;
12195 }
12196
12197 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012198 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012199 if (group == NULL)
12200 return;
12201 if (group[0] == '\0')
12202 group = NULL; // global sign group
12203 else
12204 {
12205 group = vim_strsave(group);
12206 if (group == NULL)
12207 return;
12208 }
12209
12210 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012211 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012212 if (sign_name == NULL)
12213 goto cleanup;
12214
12215 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012216 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012217 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012218 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012219
12220 if (argvars[4].v_type != VAR_UNKNOWN)
12221 {
12222 if (argvars[4].v_type != VAR_DICT ||
12223 ((dict = argvars[4].vval.v_dict) == NULL))
12224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012225 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012226 goto cleanup;
12227 }
12228
12229 // Line number where the sign is to be placed
12230 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12231 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012232 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012233 if (notanum)
12234 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012235 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012236 }
12237 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12238 {
12239 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012240 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012241 if (notanum)
12242 goto cleanup;
12243 }
12244 }
12245
12246 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12247 rettv->vval.v_number = sign_id;
12248
12249cleanup:
12250 vim_free(group);
12251}
12252
12253/*
12254 * "sign_undefine()" function
12255 */
12256 static void
12257f_sign_undefine(typval_T *argvars, typval_T *rettv)
12258{
12259 char_u *name;
12260
12261 rettv->vval.v_number = -1;
12262
12263 if (argvars[0].v_type == VAR_UNKNOWN)
12264 {
12265 // Free all the signs
12266 free_signs();
12267 rettv->vval.v_number = 0;
12268 }
12269 else
12270 {
12271 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012272 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012273 if (name == NULL)
12274 return;
12275
12276 if (sign_undefine_by_name(name) == OK)
12277 rettv->vval.v_number = 0;
12278 }
12279}
12280
12281/*
12282 * "sign_unplace()" function
12283 */
12284 static void
12285f_sign_unplace(typval_T *argvars, typval_T *rettv)
12286{
12287 dict_T *dict;
12288 dictitem_T *di;
12289 int sign_id = 0;
12290 buf_T *buf = NULL;
12291 char_u *group = NULL;
12292
12293 rettv->vval.v_number = -1;
12294
12295 if (argvars[0].v_type != VAR_STRING)
12296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012297 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012298 return;
12299 }
12300
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012301 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012302 if (group[0] == '\0')
12303 group = NULL; // global sign group
12304 else
12305 {
12306 group = vim_strsave(group);
12307 if (group == NULL)
12308 return;
12309 }
12310
12311 if (argvars[1].v_type != VAR_UNKNOWN)
12312 {
12313 if (argvars[1].v_type != VAR_DICT)
12314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012315 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012316 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012317 }
12318 dict = argvars[1].vval.v_dict;
12319
12320 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12321 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012322 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012323 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012324 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012325 }
12326 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12327 sign_id = dict_get_number(dict, (char_u *)"id");
12328 }
12329
12330 if (buf == NULL)
12331 {
12332 // Delete the sign in all the buffers
12333 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012334 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012335 rettv->vval.v_number = 0;
12336 }
12337 else
12338 {
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 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012342
12343cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012344 vim_free(group);
12345}
12346#endif
12347
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012348/*
12349 * "simplify()" function
12350 */
12351 static void
12352f_simplify(typval_T *argvars, typval_T *rettv)
12353{
12354 char_u *p;
12355
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012356 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012357 rettv->vval.v_string = vim_strsave(p);
12358 simplify_filename(rettv->vval.v_string); /* simplify in place */
12359 rettv->v_type = VAR_STRING;
12360}
12361
12362#ifdef FEAT_FLOAT
12363/*
12364 * "sin()" function
12365 */
12366 static void
12367f_sin(typval_T *argvars, typval_T *rettv)
12368{
12369 float_T f = 0.0;
12370
12371 rettv->v_type = VAR_FLOAT;
12372 if (get_float_arg(argvars, &f) == OK)
12373 rettv->vval.v_float = sin(f);
12374 else
12375 rettv->vval.v_float = 0.0;
12376}
12377
12378/*
12379 * "sinh()" function
12380 */
12381 static void
12382f_sinh(typval_T *argvars, typval_T *rettv)
12383{
12384 float_T f = 0.0;
12385
12386 rettv->v_type = VAR_FLOAT;
12387 if (get_float_arg(argvars, &f) == OK)
12388 rettv->vval.v_float = sinh(f);
12389 else
12390 rettv->vval.v_float = 0.0;
12391}
12392#endif
12393
Bram Moolenaareae1b912019-05-09 15:12:55 +020012394static int item_compare(const void *s1, const void *s2);
12395static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012396
12397/* struct used in the array that's given to qsort() */
12398typedef struct
12399{
12400 listitem_T *item;
12401 int idx;
12402} sortItem_T;
12403
12404/* struct storing information about current sort */
12405typedef struct
12406{
12407 int item_compare_ic;
12408 int item_compare_numeric;
12409 int item_compare_numbers;
12410#ifdef FEAT_FLOAT
12411 int item_compare_float;
12412#endif
12413 char_u *item_compare_func;
12414 partial_T *item_compare_partial;
12415 dict_T *item_compare_selfdict;
12416 int item_compare_func_err;
12417 int item_compare_keep_zero;
12418} sortinfo_T;
12419static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012420#define ITEM_COMPARE_FAIL 999
12421
12422/*
12423 * Compare functions for f_sort() and f_uniq() below.
12424 */
12425 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426item_compare(const void *s1, const void *s2)
12427{
12428 sortItem_T *si1, *si2;
12429 typval_T *tv1, *tv2;
12430 char_u *p1, *p2;
12431 char_u *tofree1 = NULL, *tofree2 = NULL;
12432 int res;
12433 char_u numbuf1[NUMBUFLEN];
12434 char_u numbuf2[NUMBUFLEN];
12435
12436 si1 = (sortItem_T *)s1;
12437 si2 = (sortItem_T *)s2;
12438 tv1 = &si1->item->li_tv;
12439 tv2 = &si2->item->li_tv;
12440
12441 if (sortinfo->item_compare_numbers)
12442 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012443 varnumber_T v1 = tv_get_number(tv1);
12444 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012445
12446 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12447 }
12448
12449#ifdef FEAT_FLOAT
12450 if (sortinfo->item_compare_float)
12451 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012452 float_T v1 = tv_get_float(tv1);
12453 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012454
12455 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12456 }
12457#endif
12458
12459 /* tv2string() puts quotes around a string and allocates memory. Don't do
12460 * that for string variables. Use a single quote when comparing with a
12461 * non-string to do what the docs promise. */
12462 if (tv1->v_type == VAR_STRING)
12463 {
12464 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12465 p1 = (char_u *)"'";
12466 else
12467 p1 = tv1->vval.v_string;
12468 }
12469 else
12470 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12471 if (tv2->v_type == VAR_STRING)
12472 {
12473 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12474 p2 = (char_u *)"'";
12475 else
12476 p2 = tv2->vval.v_string;
12477 }
12478 else
12479 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12480 if (p1 == NULL)
12481 p1 = (char_u *)"";
12482 if (p2 == NULL)
12483 p2 = (char_u *)"";
12484 if (!sortinfo->item_compare_numeric)
12485 {
12486 if (sortinfo->item_compare_ic)
12487 res = STRICMP(p1, p2);
12488 else
12489 res = STRCMP(p1, p2);
12490 }
12491 else
12492 {
12493 double n1, n2;
12494 n1 = strtod((char *)p1, (char **)&p1);
12495 n2 = strtod((char *)p2, (char **)&p2);
12496 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12497 }
12498
12499 /* When the result would be zero, compare the item indexes. Makes the
12500 * sort stable. */
12501 if (res == 0 && !sortinfo->item_compare_keep_zero)
12502 res = si1->idx > si2->idx ? 1 : -1;
12503
12504 vim_free(tofree1);
12505 vim_free(tofree2);
12506 return res;
12507}
12508
12509 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012510item_compare2(const void *s1, const void *s2)
12511{
12512 sortItem_T *si1, *si2;
12513 int res;
12514 typval_T rettv;
12515 typval_T argv[3];
12516 int dummy;
12517 char_u *func_name;
12518 partial_T *partial = sortinfo->item_compare_partial;
12519
12520 /* shortcut after failure in previous call; compare all items equal */
12521 if (sortinfo->item_compare_func_err)
12522 return 0;
12523
12524 si1 = (sortItem_T *)s1;
12525 si2 = (sortItem_T *)s2;
12526
12527 if (partial == NULL)
12528 func_name = sortinfo->item_compare_func;
12529 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012530 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012531
12532 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12533 * in the copy without changing the original list items. */
12534 copy_tv(&si1->item->li_tv, &argv[0]);
12535 copy_tv(&si2->item->li_tv, &argv[1]);
12536
12537 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012538 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012539 partial, sortinfo->item_compare_selfdict);
12540 clear_tv(&argv[0]);
12541 clear_tv(&argv[1]);
12542
12543 if (res == FAIL)
12544 res = ITEM_COMPARE_FAIL;
12545 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012546 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012547 if (sortinfo->item_compare_func_err)
12548 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12549 clear_tv(&rettv);
12550
12551 /* When the result would be zero, compare the pointers themselves. Makes
12552 * the sort stable. */
12553 if (res == 0 && !sortinfo->item_compare_keep_zero)
12554 res = si1->idx > si2->idx ? 1 : -1;
12555
12556 return res;
12557}
12558
12559/*
12560 * "sort({list})" function
12561 */
12562 static void
12563do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12564{
12565 list_T *l;
12566 listitem_T *li;
12567 sortItem_T *ptrs;
12568 sortinfo_T *old_sortinfo;
12569 sortinfo_T info;
12570 long len;
12571 long i;
12572
12573 /* Pointer to current info struct used in compare function. Save and
12574 * restore the current one for nested calls. */
12575 old_sortinfo = sortinfo;
12576 sortinfo = &info;
12577
12578 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012579 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 else
12581 {
12582 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012583 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012584 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12585 TRUE))
12586 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012587 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588
12589 len = list_len(l);
12590 if (len <= 1)
12591 goto theend; /* short list sorts pretty quickly */
12592
12593 info.item_compare_ic = FALSE;
12594 info.item_compare_numeric = FALSE;
12595 info.item_compare_numbers = FALSE;
12596#ifdef FEAT_FLOAT
12597 info.item_compare_float = FALSE;
12598#endif
12599 info.item_compare_func = NULL;
12600 info.item_compare_partial = NULL;
12601 info.item_compare_selfdict = NULL;
12602 if (argvars[1].v_type != VAR_UNKNOWN)
12603 {
12604 /* optional second argument: {func} */
12605 if (argvars[1].v_type == VAR_FUNC)
12606 info.item_compare_func = argvars[1].vval.v_string;
12607 else if (argvars[1].v_type == VAR_PARTIAL)
12608 info.item_compare_partial = argvars[1].vval.v_partial;
12609 else
12610 {
12611 int error = FALSE;
12612
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012613 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614 if (error)
12615 goto theend; /* type error; errmsg already given */
12616 if (i == 1)
12617 info.item_compare_ic = TRUE;
12618 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012619 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012620 else if (i != 0)
12621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012622 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012623 goto theend;
12624 }
12625 if (info.item_compare_func != NULL)
12626 {
12627 if (*info.item_compare_func == NUL)
12628 {
12629 /* empty string means default sort */
12630 info.item_compare_func = NULL;
12631 }
12632 else if (STRCMP(info.item_compare_func, "n") == 0)
12633 {
12634 info.item_compare_func = NULL;
12635 info.item_compare_numeric = TRUE;
12636 }
12637 else if (STRCMP(info.item_compare_func, "N") == 0)
12638 {
12639 info.item_compare_func = NULL;
12640 info.item_compare_numbers = TRUE;
12641 }
12642#ifdef FEAT_FLOAT
12643 else if (STRCMP(info.item_compare_func, "f") == 0)
12644 {
12645 info.item_compare_func = NULL;
12646 info.item_compare_float = TRUE;
12647 }
12648#endif
12649 else if (STRCMP(info.item_compare_func, "i") == 0)
12650 {
12651 info.item_compare_func = NULL;
12652 info.item_compare_ic = TRUE;
12653 }
12654 }
12655 }
12656
12657 if (argvars[2].v_type != VAR_UNKNOWN)
12658 {
12659 /* optional third argument: {dict} */
12660 if (argvars[2].v_type != VAR_DICT)
12661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012662 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012663 goto theend;
12664 }
12665 info.item_compare_selfdict = argvars[2].vval.v_dict;
12666 }
12667 }
12668
12669 /* Make an array with each entry pointing to an item in the List. */
12670 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
12671 if (ptrs == NULL)
12672 goto theend;
12673
12674 i = 0;
12675 if (sort)
12676 {
12677 /* sort(): ptrs will be the list to sort */
12678 for (li = l->lv_first; li != NULL; li = li->li_next)
12679 {
12680 ptrs[i].item = li;
12681 ptrs[i].idx = i;
12682 ++i;
12683 }
12684
12685 info.item_compare_func_err = FALSE;
12686 info.item_compare_keep_zero = FALSE;
12687 /* test the compare function */
12688 if ((info.item_compare_func != NULL
12689 || info.item_compare_partial != NULL)
12690 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12691 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012692 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012693 else
12694 {
12695 /* Sort the array with item pointers. */
12696 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12697 info.item_compare_func == NULL
12698 && info.item_compare_partial == NULL
12699 ? item_compare : item_compare2);
12700
12701 if (!info.item_compare_func_err)
12702 {
12703 /* Clear the List and append the items in sorted order. */
12704 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12705 l->lv_len = 0;
12706 for (i = 0; i < len; ++i)
12707 list_append(l, ptrs[i].item);
12708 }
12709 }
12710 }
12711 else
12712 {
12713 int (*item_compare_func_ptr)(const void *, const void *);
12714
12715 /* f_uniq(): ptrs will be a stack of items to remove */
12716 info.item_compare_func_err = FALSE;
12717 info.item_compare_keep_zero = TRUE;
12718 item_compare_func_ptr = info.item_compare_func != NULL
12719 || info.item_compare_partial != NULL
12720 ? item_compare2 : item_compare;
12721
12722 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12723 li = li->li_next)
12724 {
12725 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12726 == 0)
12727 ptrs[i++].item = li;
12728 if (info.item_compare_func_err)
12729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012730 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012731 break;
12732 }
12733 }
12734
12735 if (!info.item_compare_func_err)
12736 {
12737 while (--i >= 0)
12738 {
12739 li = ptrs[i].item->li_next;
12740 ptrs[i].item->li_next = li->li_next;
12741 if (li->li_next != NULL)
12742 li->li_next->li_prev = ptrs[i].item;
12743 else
12744 l->lv_last = ptrs[i].item;
12745 list_fix_watch(l, li);
12746 listitem_free(li);
12747 l->lv_len--;
12748 }
12749 }
12750 }
12751
12752 vim_free(ptrs);
12753 }
12754theend:
12755 sortinfo = old_sortinfo;
12756}
12757
12758/*
12759 * "sort({list})" function
12760 */
12761 static void
12762f_sort(typval_T *argvars, typval_T *rettv)
12763{
12764 do_sort_uniq(argvars, rettv, TRUE);
12765}
12766
12767/*
12768 * "uniq({list})" function
12769 */
12770 static void
12771f_uniq(typval_T *argvars, typval_T *rettv)
12772{
12773 do_sort_uniq(argvars, rettv, FALSE);
12774}
12775
12776/*
12777 * "soundfold({word})" function
12778 */
12779 static void
12780f_soundfold(typval_T *argvars, typval_T *rettv)
12781{
12782 char_u *s;
12783
12784 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012785 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012786#ifdef FEAT_SPELL
12787 rettv->vval.v_string = eval_soundfold(s);
12788#else
12789 rettv->vval.v_string = vim_strsave(s);
12790#endif
12791}
12792
12793/*
12794 * "spellbadword()" function
12795 */
12796 static void
12797f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12798{
12799 char_u *word = (char_u *)"";
12800 hlf_T attr = HLF_COUNT;
12801 int len = 0;
12802
12803 if (rettv_list_alloc(rettv) == FAIL)
12804 return;
12805
12806#ifdef FEAT_SPELL
12807 if (argvars[0].v_type == VAR_UNKNOWN)
12808 {
12809 /* Find the start and length of the badly spelled word. */
12810 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12811 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012812 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012813 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012814 curwin->w_set_curswant = TRUE;
12815 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816 }
12817 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12818 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012819 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012820 int capcol = -1;
12821
12822 if (str != NULL)
12823 {
12824 /* Check the argument for spelling. */
12825 while (*str != NUL)
12826 {
12827 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12828 if (attr != HLF_COUNT)
12829 {
12830 word = str;
12831 break;
12832 }
12833 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012834 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012835 }
12836 }
12837 }
12838#endif
12839
12840 list_append_string(rettv->vval.v_list, word, len);
12841 list_append_string(rettv->vval.v_list, (char_u *)(
12842 attr == HLF_SPB ? "bad" :
12843 attr == HLF_SPR ? "rare" :
12844 attr == HLF_SPL ? "local" :
12845 attr == HLF_SPC ? "caps" :
12846 ""), -1);
12847}
12848
12849/*
12850 * "spellsuggest()" function
12851 */
12852 static void
12853f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12854{
12855#ifdef FEAT_SPELL
12856 char_u *str;
12857 int typeerr = FALSE;
12858 int maxcount;
12859 garray_T ga;
12860 int i;
12861 listitem_T *li;
12862 int need_capital = FALSE;
12863#endif
12864
12865 if (rettv_list_alloc(rettv) == FAIL)
12866 return;
12867
12868#ifdef FEAT_SPELL
12869 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12870 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012871 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012872 if (argvars[1].v_type != VAR_UNKNOWN)
12873 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012874 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012875 if (maxcount <= 0)
12876 return;
12877 if (argvars[2].v_type != VAR_UNKNOWN)
12878 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012879 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012880 if (typeerr)
12881 return;
12882 }
12883 }
12884 else
12885 maxcount = 25;
12886
12887 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12888
12889 for (i = 0; i < ga.ga_len; ++i)
12890 {
12891 str = ((char_u **)ga.ga_data)[i];
12892
12893 li = listitem_alloc();
12894 if (li == NULL)
12895 vim_free(str);
12896 else
12897 {
12898 li->li_tv.v_type = VAR_STRING;
12899 li->li_tv.v_lock = 0;
12900 li->li_tv.vval.v_string = str;
12901 list_append(rettv->vval.v_list, li);
12902 }
12903 }
12904 ga_clear(&ga);
12905 }
12906#endif
12907}
12908
12909 static void
12910f_split(typval_T *argvars, typval_T *rettv)
12911{
12912 char_u *str;
12913 char_u *end;
12914 char_u *pat = NULL;
12915 regmatch_T regmatch;
12916 char_u patbuf[NUMBUFLEN];
12917 char_u *save_cpo;
12918 int match;
12919 colnr_T col = 0;
12920 int keepempty = FALSE;
12921 int typeerr = FALSE;
12922
12923 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12924 save_cpo = p_cpo;
12925 p_cpo = (char_u *)"";
12926
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012927 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012928 if (argvars[1].v_type != VAR_UNKNOWN)
12929 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012930 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012931 if (pat == NULL)
12932 typeerr = TRUE;
12933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012934 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012935 }
12936 if (pat == NULL || *pat == NUL)
12937 pat = (char_u *)"[\\x01- ]\\+";
12938
12939 if (rettv_list_alloc(rettv) == FAIL)
12940 return;
12941 if (typeerr)
12942 return;
12943
12944 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12945 if (regmatch.regprog != NULL)
12946 {
12947 regmatch.rm_ic = FALSE;
12948 while (*str != NUL || keepempty)
12949 {
12950 if (*str == NUL)
12951 match = FALSE; /* empty item at the end */
12952 else
12953 match = vim_regexec_nl(&regmatch, str, col);
12954 if (match)
12955 end = regmatch.startp[0];
12956 else
12957 end = str + STRLEN(str);
12958 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12959 && *str != NUL && match && end < regmatch.endp[0]))
12960 {
12961 if (list_append_string(rettv->vval.v_list, str,
12962 (int)(end - str)) == FAIL)
12963 break;
12964 }
12965 if (!match)
12966 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012967 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012968 if (regmatch.endp[0] > str)
12969 col = 0;
12970 else
Bram Moolenaar13505972019-01-24 15:04:48 +010012971 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012973 str = regmatch.endp[0];
12974 }
12975
12976 vim_regfree(regmatch.regprog);
12977 }
12978
12979 p_cpo = save_cpo;
12980}
12981
12982#ifdef FEAT_FLOAT
12983/*
12984 * "sqrt()" function
12985 */
12986 static void
12987f_sqrt(typval_T *argvars, typval_T *rettv)
12988{
12989 float_T f = 0.0;
12990
12991 rettv->v_type = VAR_FLOAT;
12992 if (get_float_arg(argvars, &f) == OK)
12993 rettv->vval.v_float = sqrt(f);
12994 else
12995 rettv->vval.v_float = 0.0;
12996}
12997
12998/*
12999 * "str2float()" function
13000 */
13001 static void
13002f_str2float(typval_T *argvars, typval_T *rettv)
13003{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013004 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013005 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013006
Bram Moolenaar08243d22017-01-10 16:12:29 +010013007 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013008 p = skipwhite(p + 1);
13009 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013010 if (isneg)
13011 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013012 rettv->v_type = VAR_FLOAT;
13013}
13014#endif
13015
13016/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013017 * "str2list()" function
13018 */
13019 static void
13020f_str2list(typval_T *argvars, typval_T *rettv)
13021{
13022 char_u *p;
13023 int utf8 = FALSE;
13024
13025 if (rettv_list_alloc(rettv) == FAIL)
13026 return;
13027
13028 if (argvars[1].v_type != VAR_UNKNOWN)
13029 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13030
13031 p = tv_get_string(&argvars[0]);
13032
13033 if (has_mbyte || utf8)
13034 {
13035 int (*ptr2len)(char_u *);
13036 int (*ptr2char)(char_u *);
13037
13038 if (utf8 || enc_utf8)
13039 {
13040 ptr2len = utf_ptr2len;
13041 ptr2char = utf_ptr2char;
13042 }
13043 else
13044 {
13045 ptr2len = mb_ptr2len;
13046 ptr2char = mb_ptr2char;
13047 }
13048
13049 for ( ; *p != NUL; p += (*ptr2len)(p))
13050 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13051 }
13052 else
13053 for ( ; *p != NUL; ++p)
13054 list_append_number(rettv->vval.v_list, *p);
13055}
13056
13057/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013058 * "str2nr()" function
13059 */
13060 static void
13061f_str2nr(typval_T *argvars, typval_T *rettv)
13062{
13063 int base = 10;
13064 char_u *p;
13065 varnumber_T n;
13066 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013067 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013068
13069 if (argvars[1].v_type != VAR_UNKNOWN)
13070 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013071 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013072 if (base != 2 && base != 8 && base != 10 && base != 16)
13073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013074 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013075 return;
13076 }
13077 }
13078
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013079 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013080 isneg = (*p == '-');
13081 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013082 p = skipwhite(p + 1);
13083 switch (base)
13084 {
13085 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13086 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13087 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13088 default: what = 0;
13089 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013090 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13091 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013092 if (isneg)
13093 rettv->vval.v_number = -n;
13094 else
13095 rettv->vval.v_number = n;
13096
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013097}
13098
13099#ifdef HAVE_STRFTIME
13100/*
13101 * "strftime({format}[, {time}])" function
13102 */
13103 static void
13104f_strftime(typval_T *argvars, typval_T *rettv)
13105{
13106 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013107# ifdef HAVE_LOCALTIME_R
13108 struct tm tmval;
13109# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013110 struct tm *curtime;
13111 time_t seconds;
13112 char_u *p;
13113
13114 rettv->v_type = VAR_STRING;
13115
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013116 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013117 if (argvars[1].v_type == VAR_UNKNOWN)
13118 seconds = time(NULL);
13119 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013120 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013121# ifdef HAVE_LOCALTIME_R
13122 curtime = localtime_r(&seconds, &tmval);
13123# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013124 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013125# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013126 /* MSVC returns NULL for an invalid value of seconds. */
13127 if (curtime == NULL)
13128 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13129 else
13130 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013131 vimconv_T conv;
13132 char_u *enc;
13133
13134 conv.vc_type = CONV_NONE;
13135 enc = enc_locale();
13136 convert_setup(&conv, p_enc, enc);
13137 if (conv.vc_type != CONV_NONE)
13138 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013139 if (p != NULL)
13140 (void)strftime((char *)result_buf, sizeof(result_buf),
13141 (char *)p, curtime);
13142 else
13143 result_buf[0] = NUL;
13144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013145 if (conv.vc_type != CONV_NONE)
13146 vim_free(p);
13147 convert_setup(&conv, enc, p_enc);
13148 if (conv.vc_type != CONV_NONE)
13149 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13150 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 rettv->vval.v_string = vim_strsave(result_buf);
13152
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013153 /* Release conversion descriptors */
13154 convert_setup(&conv, NULL, NULL);
13155 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 }
13157}
13158#endif
13159
13160/*
13161 * "strgetchar()" function
13162 */
13163 static void
13164f_strgetchar(typval_T *argvars, typval_T *rettv)
13165{
13166 char_u *str;
13167 int len;
13168 int error = FALSE;
13169 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013170 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013171
13172 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013173 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013174 if (str == NULL)
13175 return;
13176 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013177 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013178 if (error)
13179 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180
Bram Moolenaar13505972019-01-24 15:04:48 +010013181 while (charidx >= 0 && byteidx < len)
13182 {
13183 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013184 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013185 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13186 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013187 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013188 --charidx;
13189 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013190 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013191}
13192
13193/*
13194 * "stridx()" function
13195 */
13196 static void
13197f_stridx(typval_T *argvars, typval_T *rettv)
13198{
13199 char_u buf[NUMBUFLEN];
13200 char_u *needle;
13201 char_u *haystack;
13202 char_u *save_haystack;
13203 char_u *pos;
13204 int start_idx;
13205
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013206 needle = tv_get_string_chk(&argvars[1]);
13207 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 rettv->vval.v_number = -1;
13209 if (needle == NULL || haystack == NULL)
13210 return; /* type error; errmsg already given */
13211
13212 if (argvars[2].v_type != VAR_UNKNOWN)
13213 {
13214 int error = FALSE;
13215
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013216 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 if (error || start_idx >= (int)STRLEN(haystack))
13218 return;
13219 if (start_idx >= 0)
13220 haystack += start_idx;
13221 }
13222
13223 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13224 if (pos != NULL)
13225 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13226}
13227
13228/*
13229 * "string()" function
13230 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013231 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232f_string(typval_T *argvars, typval_T *rettv)
13233{
13234 char_u *tofree;
13235 char_u numbuf[NUMBUFLEN];
13236
13237 rettv->v_type = VAR_STRING;
13238 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13239 get_copyID());
13240 /* Make a copy if we have a value but it's not in allocated memory. */
13241 if (rettv->vval.v_string != NULL && tofree == NULL)
13242 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13243}
13244
13245/*
13246 * "strlen()" function
13247 */
13248 static void
13249f_strlen(typval_T *argvars, typval_T *rettv)
13250{
13251 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013252 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013253}
13254
13255/*
13256 * "strchars()" function
13257 */
13258 static void
13259f_strchars(typval_T *argvars, typval_T *rettv)
13260{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013261 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013263 varnumber_T len = 0;
13264 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013265
13266 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013267 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013268 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013269 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013270 else
13271 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013272 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13273 while (*s != NUL)
13274 {
13275 func_mb_ptr2char_adv(&s);
13276 ++len;
13277 }
13278 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013279 }
13280}
13281
13282/*
13283 * "strdisplaywidth()" function
13284 */
13285 static void
13286f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13287{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013288 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013289 int col = 0;
13290
13291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013292 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293
13294 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13295}
13296
13297/*
13298 * "strwidth()" function
13299 */
13300 static void
13301f_strwidth(typval_T *argvars, typval_T *rettv)
13302{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013303 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304
Bram Moolenaar13505972019-01-24 15:04:48 +010013305 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306}
13307
13308/*
13309 * "strcharpart()" function
13310 */
13311 static void
13312f_strcharpart(typval_T *argvars, typval_T *rettv)
13313{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013314 char_u *p;
13315 int nchar;
13316 int nbyte = 0;
13317 int charlen;
13318 int len = 0;
13319 int slen;
13320 int error = FALSE;
13321
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013322 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323 slen = (int)STRLEN(p);
13324
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013325 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013326 if (!error)
13327 {
13328 if (nchar > 0)
13329 while (nchar > 0 && nbyte < slen)
13330 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013331 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013332 --nchar;
13333 }
13334 else
13335 nbyte = nchar;
13336 if (argvars[2].v_type != VAR_UNKNOWN)
13337 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013338 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013339 while (charlen > 0 && nbyte + len < slen)
13340 {
13341 int off = nbyte + len;
13342
13343 if (off < 0)
13344 len += 1;
13345 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013346 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013347 --charlen;
13348 }
13349 }
13350 else
13351 len = slen - nbyte; /* default: all bytes that are available. */
13352 }
13353
13354 /*
13355 * Only return the overlap between the specified part and the actual
13356 * string.
13357 */
13358 if (nbyte < 0)
13359 {
13360 len += nbyte;
13361 nbyte = 0;
13362 }
13363 else if (nbyte > slen)
13364 nbyte = slen;
13365 if (len < 0)
13366 len = 0;
13367 else if (nbyte + len > slen)
13368 len = slen - nbyte;
13369
13370 rettv->v_type = VAR_STRING;
13371 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013372}
13373
13374/*
13375 * "strpart()" function
13376 */
13377 static void
13378f_strpart(typval_T *argvars, typval_T *rettv)
13379{
13380 char_u *p;
13381 int n;
13382 int len;
13383 int slen;
13384 int error = FALSE;
13385
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013386 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013387 slen = (int)STRLEN(p);
13388
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013389 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013390 if (error)
13391 len = 0;
13392 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013393 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013394 else
13395 len = slen - n; /* default len: all bytes that are available. */
13396
13397 /*
13398 * Only return the overlap between the specified part and the actual
13399 * string.
13400 */
13401 if (n < 0)
13402 {
13403 len += n;
13404 n = 0;
13405 }
13406 else if (n > slen)
13407 n = slen;
13408 if (len < 0)
13409 len = 0;
13410 else if (n + len > slen)
13411 len = slen - n;
13412
13413 rettv->v_type = VAR_STRING;
13414 rettv->vval.v_string = vim_strnsave(p + n, len);
13415}
13416
13417/*
13418 * "strridx()" function
13419 */
13420 static void
13421f_strridx(typval_T *argvars, typval_T *rettv)
13422{
13423 char_u buf[NUMBUFLEN];
13424 char_u *needle;
13425 char_u *haystack;
13426 char_u *rest;
13427 char_u *lastmatch = NULL;
13428 int haystack_len, end_idx;
13429
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013430 needle = tv_get_string_chk(&argvars[1]);
13431 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013432
13433 rettv->vval.v_number = -1;
13434 if (needle == NULL || haystack == NULL)
13435 return; /* type error; errmsg already given */
13436
13437 haystack_len = (int)STRLEN(haystack);
13438 if (argvars[2].v_type != VAR_UNKNOWN)
13439 {
13440 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013441 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013442 if (end_idx < 0)
13443 return; /* can never find a match */
13444 }
13445 else
13446 end_idx = haystack_len;
13447
13448 if (*needle == NUL)
13449 {
13450 /* Empty string matches past the end. */
13451 lastmatch = haystack + end_idx;
13452 }
13453 else
13454 {
13455 for (rest = haystack; *rest != '\0'; ++rest)
13456 {
13457 rest = (char_u *)strstr((char *)rest, (char *)needle);
13458 if (rest == NULL || rest > haystack + end_idx)
13459 break;
13460 lastmatch = rest;
13461 }
13462 }
13463
13464 if (lastmatch == NULL)
13465 rettv->vval.v_number = -1;
13466 else
13467 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13468}
13469
13470/*
13471 * "strtrans()" function
13472 */
13473 static void
13474f_strtrans(typval_T *argvars, typval_T *rettv)
13475{
13476 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013477 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013478}
13479
13480/*
13481 * "submatch()" function
13482 */
13483 static void
13484f_submatch(typval_T *argvars, typval_T *rettv)
13485{
13486 int error = FALSE;
13487 int no;
13488 int retList = 0;
13489
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013490 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013491 if (error)
13492 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013493 if (no < 0 || no >= NSUBEXP)
13494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013495 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013496 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013497 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013498 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013499 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013500 if (error)
13501 return;
13502
13503 if (retList == 0)
13504 {
13505 rettv->v_type = VAR_STRING;
13506 rettv->vval.v_string = reg_submatch(no);
13507 }
13508 else
13509 {
13510 rettv->v_type = VAR_LIST;
13511 rettv->vval.v_list = reg_submatch_list(no);
13512 }
13513}
13514
13515/*
13516 * "substitute()" function
13517 */
13518 static void
13519f_substitute(typval_T *argvars, typval_T *rettv)
13520{
13521 char_u patbuf[NUMBUFLEN];
13522 char_u subbuf[NUMBUFLEN];
13523 char_u flagsbuf[NUMBUFLEN];
13524
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013525 char_u *str = tv_get_string_chk(&argvars[0]);
13526 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013527 char_u *sub = NULL;
13528 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013529 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013530
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013531 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13532 expr = &argvars[2];
13533 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013534 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013535
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013536 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013537 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13538 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013539 rettv->vval.v_string = NULL;
13540 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013541 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013542}
13543
13544/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013545 * "swapinfo(swap_filename)" function
13546 */
13547 static void
13548f_swapinfo(typval_T *argvars, typval_T *rettv)
13549{
13550 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013551 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013552}
13553
13554/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013555 * "swapname(expr)" function
13556 */
13557 static void
13558f_swapname(typval_T *argvars, typval_T *rettv)
13559{
13560 buf_T *buf;
13561
13562 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013563 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013564 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13565 || buf->b_ml.ml_mfp->mf_fname == NULL)
13566 rettv->vval.v_string = NULL;
13567 else
13568 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13569}
13570
13571/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572 * "synID(lnum, col, trans)" function
13573 */
13574 static void
13575f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13576{
13577 int id = 0;
13578#ifdef FEAT_SYN_HL
13579 linenr_T lnum;
13580 colnr_T col;
13581 int trans;
13582 int transerr = FALSE;
13583
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013584 lnum = tv_get_lnum(argvars); /* -1 on type error */
13585 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13586 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013587
13588 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13589 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13590 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13591#endif
13592
13593 rettv->vval.v_number = id;
13594}
13595
13596/*
13597 * "synIDattr(id, what [, mode])" function
13598 */
13599 static void
13600f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13601{
13602 char_u *p = NULL;
13603#ifdef FEAT_SYN_HL
13604 int id;
13605 char_u *what;
13606 char_u *mode;
13607 char_u modebuf[NUMBUFLEN];
13608 int modec;
13609
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013610 id = (int)tv_get_number(&argvars[0]);
13611 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013612 if (argvars[2].v_type != VAR_UNKNOWN)
13613 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013614 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013615 modec = TOLOWER_ASC(mode[0]);
13616 if (modec != 't' && modec != 'c' && modec != 'g')
13617 modec = 0; /* replace invalid with current */
13618 }
13619 else
13620 {
13621#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13622 if (USE_24BIT)
13623 modec = 'g';
13624 else
13625#endif
13626 if (t_colors > 1)
13627 modec = 'c';
13628 else
13629 modec = 't';
13630 }
13631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013632 switch (TOLOWER_ASC(what[0]))
13633 {
13634 case 'b':
13635 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13636 p = highlight_color(id, what, modec);
13637 else /* bold */
13638 p = highlight_has_attr(id, HL_BOLD, modec);
13639 break;
13640
13641 case 'f': /* fg[#] or font */
13642 p = highlight_color(id, what, modec);
13643 break;
13644
13645 case 'i':
13646 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13647 p = highlight_has_attr(id, HL_INVERSE, modec);
13648 else /* italic */
13649 p = highlight_has_attr(id, HL_ITALIC, modec);
13650 break;
13651
13652 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013653 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013654 break;
13655
13656 case 'r': /* reverse */
13657 p = highlight_has_attr(id, HL_INVERSE, modec);
13658 break;
13659
13660 case 's':
13661 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13662 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013663 /* strikeout */
13664 else if (TOLOWER_ASC(what[1]) == 't' &&
13665 TOLOWER_ASC(what[2]) == 'r')
13666 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013667 else /* standout */
13668 p = highlight_has_attr(id, HL_STANDOUT, modec);
13669 break;
13670
13671 case 'u':
13672 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13673 /* underline */
13674 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13675 else
13676 /* undercurl */
13677 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13678 break;
13679 }
13680
13681 if (p != NULL)
13682 p = vim_strsave(p);
13683#endif
13684 rettv->v_type = VAR_STRING;
13685 rettv->vval.v_string = p;
13686}
13687
13688/*
13689 * "synIDtrans(id)" function
13690 */
13691 static void
13692f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13693{
13694 int id;
13695
13696#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013697 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013698
13699 if (id > 0)
13700 id = syn_get_final_id(id);
13701 else
13702#endif
13703 id = 0;
13704
13705 rettv->vval.v_number = id;
13706}
13707
13708/*
13709 * "synconcealed(lnum, col)" function
13710 */
13711 static void
13712f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13713{
13714#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13715 linenr_T lnum;
13716 colnr_T col;
13717 int syntax_flags = 0;
13718 int cchar;
13719 int matchid = 0;
13720 char_u str[NUMBUFLEN];
13721#endif
13722
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013723 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013724
13725#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013726 lnum = tv_get_lnum(argvars); /* -1 on type error */
13727 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013728
13729 vim_memset(str, NUL, sizeof(str));
13730
13731 if (rettv_list_alloc(rettv) != FAIL)
13732 {
13733 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13734 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13735 && curwin->w_p_cole > 0)
13736 {
13737 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13738 syntax_flags = get_syntax_info(&matchid);
13739
13740 /* get the conceal character */
13741 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13742 {
13743 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013744 if (cchar == NUL && curwin->w_p_cole == 1)
13745 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013746 if (cchar != NUL)
13747 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013748 if (has_mbyte)
13749 (*mb_char2bytes)(cchar, str);
13750 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013751 str[0] = cchar;
13752 }
13753 }
13754 }
13755
13756 list_append_number(rettv->vval.v_list,
13757 (syntax_flags & HL_CONCEAL) != 0);
13758 /* -1 to auto-determine strlen */
13759 list_append_string(rettv->vval.v_list, str, -1);
13760 list_append_number(rettv->vval.v_list, matchid);
13761 }
13762#endif
13763}
13764
13765/*
13766 * "synstack(lnum, col)" function
13767 */
13768 static void
13769f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13770{
13771#ifdef FEAT_SYN_HL
13772 linenr_T lnum;
13773 colnr_T col;
13774 int i;
13775 int id;
13776#endif
13777
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013778 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013779
13780#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013781 lnum = tv_get_lnum(argvars); /* -1 on type error */
13782 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013783
13784 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13785 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13786 && rettv_list_alloc(rettv) != FAIL)
13787 {
13788 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13789 for (i = 0; ; ++i)
13790 {
13791 id = syn_get_stack_item(i);
13792 if (id < 0)
13793 break;
13794 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13795 break;
13796 }
13797 }
13798#endif
13799}
13800
13801 static void
13802get_cmd_output_as_rettv(
13803 typval_T *argvars,
13804 typval_T *rettv,
13805 int retlist)
13806{
13807 char_u *res = NULL;
13808 char_u *p;
13809 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013810 int err = FALSE;
13811 FILE *fd;
13812 list_T *list = NULL;
13813 int flags = SHELL_SILENT;
13814
13815 rettv->v_type = VAR_STRING;
13816 rettv->vval.v_string = NULL;
13817 if (check_restricted() || check_secure())
13818 goto errret;
13819
13820 if (argvars[1].v_type != VAR_UNKNOWN)
13821 {
13822 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013823 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824 * command.
13825 */
13826 if ((infile = vim_tempname('i', TRUE)) == NULL)
13827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013828 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013829 goto errret;
13830 }
13831
13832 fd = mch_fopen((char *)infile, WRITEBIN);
13833 if (fd == NULL)
13834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013835 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013836 goto errret;
13837 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013838 if (argvars[1].v_type == VAR_NUMBER)
13839 {
13840 linenr_T lnum;
13841 buf_T *buf;
13842
13843 buf = buflist_findnr(argvars[1].vval.v_number);
13844 if (buf == NULL)
13845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013846 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013847 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013848 goto errret;
13849 }
13850
13851 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13852 {
13853 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13854 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13855 {
13856 err = TRUE;
13857 break;
13858 }
13859 if (putc(NL, fd) == EOF)
13860 {
13861 err = TRUE;
13862 break;
13863 }
13864 }
13865 }
13866 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013867 {
13868 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13869 err = TRUE;
13870 }
13871 else
13872 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013873 size_t len;
13874 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013875
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013876 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877 if (p == NULL)
13878 {
13879 fclose(fd);
13880 goto errret; /* type error; errmsg already given */
13881 }
13882 len = STRLEN(p);
13883 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13884 err = TRUE;
13885 }
13886 if (fclose(fd) != 0)
13887 err = TRUE;
13888 if (err)
13889 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013890 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013891 goto errret;
13892 }
13893 }
13894
13895 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13896 * echoes typeahead, that messes up the display. */
13897 if (!msg_silent)
13898 flags += SHELL_COOKED;
13899
13900 if (retlist)
13901 {
13902 int len;
13903 listitem_T *li;
13904 char_u *s = NULL;
13905 char_u *start;
13906 char_u *end;
13907 int i;
13908
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013909 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013910 if (res == NULL)
13911 goto errret;
13912
13913 list = list_alloc();
13914 if (list == NULL)
13915 goto errret;
13916
13917 for (i = 0; i < len; ++i)
13918 {
13919 start = res + i;
13920 while (i < len && res[i] != NL)
13921 ++i;
13922 end = res + i;
13923
Bram Moolenaar964b3742019-05-24 18:54:09 +020013924 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013925 if (s == NULL)
13926 goto errret;
13927
13928 for (p = s; start < end; ++p, ++start)
13929 *p = *start == NUL ? NL : *start;
13930 *p = NUL;
13931
13932 li = listitem_alloc();
13933 if (li == NULL)
13934 {
13935 vim_free(s);
13936 goto errret;
13937 }
13938 li->li_tv.v_type = VAR_STRING;
13939 li->li_tv.v_lock = 0;
13940 li->li_tv.vval.v_string = s;
13941 list_append(list, li);
13942 }
13943
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013944 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013945 list = NULL;
13946 }
13947 else
13948 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013949 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013950#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013951 /* translate <CR><NL> into <NL> */
13952 if (res != NULL)
13953 {
13954 char_u *s, *d;
13955
13956 d = res;
13957 for (s = res; *s; ++s)
13958 {
13959 if (s[0] == CAR && s[1] == NL)
13960 ++s;
13961 *d++ = *s;
13962 }
13963 *d = NUL;
13964 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013965#endif
13966 rettv->vval.v_string = res;
13967 res = NULL;
13968 }
13969
13970errret:
13971 if (infile != NULL)
13972 {
13973 mch_remove(infile);
13974 vim_free(infile);
13975 }
13976 if (res != NULL)
13977 vim_free(res);
13978 if (list != NULL)
13979 list_free(list);
13980}
13981
13982/*
13983 * "system()" function
13984 */
13985 static void
13986f_system(typval_T *argvars, typval_T *rettv)
13987{
13988 get_cmd_output_as_rettv(argvars, rettv, FALSE);
13989}
13990
13991/*
13992 * "systemlist()" function
13993 */
13994 static void
13995f_systemlist(typval_T *argvars, typval_T *rettv)
13996{
13997 get_cmd_output_as_rettv(argvars, rettv, TRUE);
13998}
13999
14000/*
14001 * "tabpagebuflist()" function
14002 */
14003 static void
14004f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14005{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014006 tabpage_T *tp;
14007 win_T *wp = NULL;
14008
14009 if (argvars[0].v_type == VAR_UNKNOWN)
14010 wp = firstwin;
14011 else
14012 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014013 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014014 if (tp != NULL)
14015 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14016 }
14017 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14018 {
14019 for (; wp != NULL; wp = wp->w_next)
14020 if (list_append_number(rettv->vval.v_list,
14021 wp->w_buffer->b_fnum) == FAIL)
14022 break;
14023 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014024}
14025
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014026/*
14027 * "tabpagenr()" function
14028 */
14029 static void
14030f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14031{
14032 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014033 char_u *arg;
14034
14035 if (argvars[0].v_type != VAR_UNKNOWN)
14036 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014037 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014038 nr = 0;
14039 if (arg != NULL)
14040 {
14041 if (STRCMP(arg, "$") == 0)
14042 nr = tabpage_index(NULL) - 1;
14043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014044 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014045 }
14046 }
14047 else
14048 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014049 rettv->vval.v_number = nr;
14050}
14051
14052
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014053/*
14054 * Common code for tabpagewinnr() and winnr().
14055 */
14056 static int
14057get_winnr(tabpage_T *tp, typval_T *argvar)
14058{
14059 win_T *twin;
14060 int nr = 1;
14061 win_T *wp;
14062 char_u *arg;
14063
14064 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14065 if (argvar->v_type != VAR_UNKNOWN)
14066 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014067 int invalid_arg = FALSE;
14068
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014069 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014070 if (arg == NULL)
14071 nr = 0; /* type error; errmsg already given */
14072 else if (STRCMP(arg, "$") == 0)
14073 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14074 else if (STRCMP(arg, "#") == 0)
14075 {
14076 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14077 if (twin == NULL)
14078 nr = 0;
14079 }
14080 else
14081 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014082 long count;
14083 char_u *endp;
14084
14085 // Extract the window count (if specified). e.g. winnr('3j')
14086 count = strtol((char *)arg, (char **)&endp, 10);
14087 if (count <= 0)
14088 count = 1; // if count is not specified, default to 1
14089 if (endp != NULL && *endp != '\0')
14090 {
14091 if (STRCMP(endp, "j") == 0)
14092 twin = win_vert_neighbor(tp, twin, FALSE, count);
14093 else if (STRCMP(endp, "k") == 0)
14094 twin = win_vert_neighbor(tp, twin, TRUE, count);
14095 else if (STRCMP(endp, "h") == 0)
14096 twin = win_horz_neighbor(tp, twin, TRUE, count);
14097 else if (STRCMP(endp, "l") == 0)
14098 twin = win_horz_neighbor(tp, twin, FALSE, count);
14099 else
14100 invalid_arg = TRUE;
14101 }
14102 else
14103 invalid_arg = TRUE;
14104 }
14105
14106 if (invalid_arg)
14107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014108 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014109 nr = 0;
14110 }
14111 }
14112
14113 if (nr > 0)
14114 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14115 wp != twin; wp = wp->w_next)
14116 {
14117 if (wp == NULL)
14118 {
14119 /* didn't find it in this tabpage */
14120 nr = 0;
14121 break;
14122 }
14123 ++nr;
14124 }
14125 return nr;
14126}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014127
14128/*
14129 * "tabpagewinnr()" function
14130 */
14131 static void
14132f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14133{
14134 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014135 tabpage_T *tp;
14136
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014137 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014138 if (tp == NULL)
14139 nr = 0;
14140 else
14141 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014142 rettv->vval.v_number = nr;
14143}
14144
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014145/*
14146 * "tagfiles()" function
14147 */
14148 static void
14149f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14150{
14151 char_u *fname;
14152 tagname_T tn;
14153 int first;
14154
14155 if (rettv_list_alloc(rettv) == FAIL)
14156 return;
14157 fname = alloc(MAXPATHL);
14158 if (fname == NULL)
14159 return;
14160
14161 for (first = TRUE; ; first = FALSE)
14162 if (get_tagfname(&tn, first, fname) == FAIL
14163 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14164 break;
14165 tagname_free(&tn);
14166 vim_free(fname);
14167}
14168
14169/*
14170 * "taglist()" function
14171 */
14172 static void
14173f_taglist(typval_T *argvars, typval_T *rettv)
14174{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014175 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014176 char_u *tag_pattern;
14177
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014178 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179
14180 rettv->vval.v_number = FALSE;
14181 if (*tag_pattern == NUL)
14182 return;
14183
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014184 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014185 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014187 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014188}
14189
14190/*
14191 * "tempname()" function
14192 */
14193 static void
14194f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14195{
14196 static int x = 'A';
14197
14198 rettv->v_type = VAR_STRING;
14199 rettv->vval.v_string = vim_tempname(x, FALSE);
14200
14201 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14202 * names. Skip 'I' and 'O', they are used for shell redirection. */
14203 do
14204 {
14205 if (x == 'Z')
14206 x = '0';
14207 else if (x == '9')
14208 x = 'A';
14209 else
14210 {
14211#ifdef EBCDIC
14212 if (x == 'I')
14213 x = 'J';
14214 else if (x == 'R')
14215 x = 'S';
14216 else
14217#endif
14218 ++x;
14219 }
14220 } while (x == 'I' || x == 'O');
14221}
14222
14223#ifdef FEAT_FLOAT
14224/*
14225 * "tan()" function
14226 */
14227 static void
14228f_tan(typval_T *argvars, typval_T *rettv)
14229{
14230 float_T f = 0.0;
14231
14232 rettv->v_type = VAR_FLOAT;
14233 if (get_float_arg(argvars, &f) == OK)
14234 rettv->vval.v_float = tan(f);
14235 else
14236 rettv->vval.v_float = 0.0;
14237}
14238
14239/*
14240 * "tanh()" function
14241 */
14242 static void
14243f_tanh(typval_T *argvars, typval_T *rettv)
14244{
14245 float_T f = 0.0;
14246
14247 rettv->v_type = VAR_FLOAT;
14248 if (get_float_arg(argvars, &f) == OK)
14249 rettv->vval.v_float = tanh(f);
14250 else
14251 rettv->vval.v_float = 0.0;
14252}
14253#endif
14254
14255/*
14256 * "test_alloc_fail(id, countdown, repeat)" function
14257 */
14258 static void
14259f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14260{
14261 if (argvars[0].v_type != VAR_NUMBER
14262 || argvars[0].vval.v_number <= 0
14263 || argvars[1].v_type != VAR_NUMBER
14264 || argvars[1].vval.v_number < 0
14265 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014266 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014267 else
14268 {
14269 alloc_fail_id = argvars[0].vval.v_number;
14270 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014271 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014272 alloc_fail_countdown = argvars[1].vval.v_number;
14273 alloc_fail_repeat = argvars[2].vval.v_number;
14274 did_outofmem_msg = FALSE;
14275 }
14276}
14277
14278/*
14279 * "test_autochdir()"
14280 */
14281 static void
14282f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14283{
14284#if defined(FEAT_AUTOCHDIR)
14285 test_autochdir = TRUE;
14286#endif
14287}
14288
14289/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014290 * "test_feedinput()"
14291 */
14292 static void
14293f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14294{
14295#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014296 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014297
14298 if (val != NULL)
14299 {
14300 trash_input_buf();
14301 add_to_input_buf_csi(val, (int)STRLEN(val));
14302 }
14303#endif
14304}
14305
14306/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014307 * "test_getvalue({name})" function
14308 */
14309 static void
14310f_test_getvalue(typval_T *argvars, typval_T *rettv)
14311{
14312 if (argvars[0].v_type != VAR_STRING)
14313 emsg(_(e_invarg));
14314 else
14315 {
14316 char_u *name = tv_get_string(&argvars[0]);
14317
14318 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14319 rettv->vval.v_number = need_fileinfo;
14320 else
14321 semsg(_(e_invarg2), name);
14322 }
14323}
14324
14325/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014326 * "test_option_not_set({name})" function
14327 */
14328 static void
14329f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14330{
14331 char_u *name = (char_u *)"";
14332
14333 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014334 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014335 else
14336 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014337 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014338 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014339 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014340 }
14341}
14342
14343/*
14344 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014345 */
14346 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014347f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014348{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014349 char_u *name = (char_u *)"";
14350 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014351 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014352
14353 if (argvars[0].v_type != VAR_STRING
14354 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014355 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014356 else
14357 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014358 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014359 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014360
14361 if (STRCMP(name, (char_u *)"redraw") == 0)
14362 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014363 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14364 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014365 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14366 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014367 else if (STRCMP(name, (char_u *)"starting") == 0)
14368 {
14369 if (val)
14370 {
14371 if (save_starting < 0)
14372 save_starting = starting;
14373 starting = 0;
14374 }
14375 else
14376 {
14377 starting = save_starting;
14378 save_starting = -1;
14379 }
14380 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014381 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14382 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014383 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14384 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014385 else if (STRCMP(name, (char_u *)"ALL") == 0)
14386 {
14387 disable_char_avail_for_testing = FALSE;
14388 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014389 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014390 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014391 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014392 if (save_starting >= 0)
14393 {
14394 starting = save_starting;
14395 save_starting = -1;
14396 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014397 }
14398 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014399 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014400 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014401}
14402
14403/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014404 * "test_refcount({expr})" function
14405 */
14406 static void
14407f_test_refcount(typval_T *argvars, typval_T *rettv)
14408{
14409 int retval = -1;
14410
14411 switch (argvars[0].v_type)
14412 {
14413 case VAR_UNKNOWN:
14414 case VAR_NUMBER:
14415 case VAR_FLOAT:
14416 case VAR_SPECIAL:
14417 case VAR_STRING:
14418 break;
14419 case VAR_JOB:
14420#ifdef FEAT_JOB_CHANNEL
14421 if (argvars[0].vval.v_job != NULL)
14422 retval = argvars[0].vval.v_job->jv_refcount - 1;
14423#endif
14424 break;
14425 case VAR_CHANNEL:
14426#ifdef FEAT_JOB_CHANNEL
14427 if (argvars[0].vval.v_channel != NULL)
14428 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14429#endif
14430 break;
14431 case VAR_FUNC:
14432 if (argvars[0].vval.v_string != NULL)
14433 {
14434 ufunc_T *fp;
14435
14436 fp = find_func(argvars[0].vval.v_string);
14437 if (fp != NULL)
14438 retval = fp->uf_refcount;
14439 }
14440 break;
14441 case VAR_PARTIAL:
14442 if (argvars[0].vval.v_partial != NULL)
14443 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14444 break;
14445 case VAR_BLOB:
14446 if (argvars[0].vval.v_blob != NULL)
14447 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14448 break;
14449 case VAR_LIST:
14450 if (argvars[0].vval.v_list != NULL)
14451 retval = argvars[0].vval.v_list->lv_refcount - 1;
14452 break;
14453 case VAR_DICT:
14454 if (argvars[0].vval.v_dict != NULL)
14455 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14456 break;
14457 }
14458
14459 rettv->v_type = VAR_NUMBER;
14460 rettv->vval.v_number = retval;
14461
14462}
14463
14464/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014465 * "test_garbagecollect_now()" function
14466 */
14467 static void
14468f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14469{
14470 /* This is dangerous, any Lists and Dicts used internally may be freed
14471 * while still in use. */
14472 garbage_collect(TRUE);
14473}
14474
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014475/*
14476 * "test_ignore_error()" function
14477 */
14478 static void
14479f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14480{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014481 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014482}
14483
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014484 static void
14485f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14486{
14487 rettv->v_type = VAR_BLOB;
14488 rettv->vval.v_blob = NULL;
14489}
14490
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014491#ifdef FEAT_JOB_CHANNEL
14492 static void
14493f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14494{
14495 rettv->v_type = VAR_CHANNEL;
14496 rettv->vval.v_channel = NULL;
14497}
14498#endif
14499
14500 static void
14501f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14502{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014503 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014504}
14505
14506#ifdef FEAT_JOB_CHANNEL
14507 static void
14508f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14509{
14510 rettv->v_type = VAR_JOB;
14511 rettv->vval.v_job = NULL;
14512}
14513#endif
14514
14515 static void
14516f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14517{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014518 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014519}
14520
14521 static void
14522f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14523{
14524 rettv->v_type = VAR_PARTIAL;
14525 rettv->vval.v_partial = NULL;
14526}
14527
14528 static void
14529f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14530{
14531 rettv->v_type = VAR_STRING;
14532 rettv->vval.v_string = NULL;
14533}
14534
Bram Moolenaarab186732018-09-14 21:27:06 +020014535#ifdef FEAT_GUI
14536 static void
14537f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14538{
14539 char_u *which;
14540 long value;
14541 int dragging;
14542 scrollbar_T *sb = NULL;
14543
14544 if (argvars[0].v_type != VAR_STRING
14545 || (argvars[1].v_type) != VAR_NUMBER
14546 || (argvars[2].v_type) != VAR_NUMBER)
14547 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014548 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014549 return;
14550 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014551 which = tv_get_string(&argvars[0]);
14552 value = tv_get_number(&argvars[1]);
14553 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014554
14555 if (STRCMP(which, "left") == 0)
14556 sb = &curwin->w_scrollbars[SBAR_LEFT];
14557 else if (STRCMP(which, "right") == 0)
14558 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14559 else if (STRCMP(which, "hor") == 0)
14560 sb = &gui.bottom_sbar;
14561 if (sb == NULL)
14562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014563 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014564 return;
14565 }
14566 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014567# ifndef USE_ON_FLY_SCROLL
14568 // need to loop through normal_cmd() to handle the scroll events
14569 exec_normal(FALSE, TRUE, FALSE);
14570# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014571}
14572#endif
14573
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014574#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014575 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014576f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14577{
14578 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14579 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14580}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014581#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014582
14583 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014584f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14585{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014586 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014587}
14588
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014589/*
14590 * Get a callback from "arg". It can be a Funcref or a function name.
14591 * When "arg" is zero return an empty string.
14592 * Return NULL for an invalid argument.
14593 */
14594 char_u *
14595get_callback(typval_T *arg, partial_T **pp)
14596{
14597 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14598 {
14599 *pp = arg->vval.v_partial;
14600 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014601 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014602 }
14603 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014604 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605 {
14606 func_ref(arg->vval.v_string);
14607 return arg->vval.v_string;
14608 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014609 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14610 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014611 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014612 return NULL;
14613}
14614
14615/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014616 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617 */
14618 void
14619free_callback(char_u *callback, partial_T *partial)
14620{
14621 if (partial != NULL)
14622 partial_unref(partial);
14623 else if (callback != NULL)
14624 {
14625 func_unref(callback);
14626 vim_free(callback);
14627 }
14628}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014629
14630#ifdef FEAT_TIMERS
14631/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014632 * "timer_info([timer])" function
14633 */
14634 static void
14635f_timer_info(typval_T *argvars, typval_T *rettv)
14636{
14637 timer_T *timer = NULL;
14638
14639 if (rettv_list_alloc(rettv) != OK)
14640 return;
14641 if (argvars[0].v_type != VAR_UNKNOWN)
14642 {
14643 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014644 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014645 else
14646 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014647 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014648 if (timer != NULL)
14649 add_timer_info(rettv, timer);
14650 }
14651 }
14652 else
14653 add_timer_info_all(rettv);
14654}
14655
14656/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014657 * "timer_pause(timer, paused)" function
14658 */
14659 static void
14660f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14661{
14662 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014663 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014664
14665 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014666 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014667 else
14668 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014669 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014670 if (timer != NULL)
14671 timer->tr_paused = paused;
14672 }
14673}
14674
14675/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014676 * "timer_start(time, callback [, options])" function
14677 */
14678 static void
14679f_timer_start(typval_T *argvars, typval_T *rettv)
14680{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014681 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014682 timer_T *timer;
14683 int repeat = 0;
14684 char_u *callback;
14685 dict_T *dict;
14686 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014687
Bram Moolenaar75537a92016-09-05 22:45:28 +020014688 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014689 if (check_secure())
14690 return;
14691 if (argvars[2].v_type != VAR_UNKNOWN)
14692 {
14693 if (argvars[2].v_type != VAR_DICT
14694 || (dict = argvars[2].vval.v_dict) == NULL)
14695 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014696 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014697 return;
14698 }
14699 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014700 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014701 }
14702
Bram Moolenaar75537a92016-09-05 22:45:28 +020014703 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014704 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014705 return;
14706
14707 timer = create_timer(msec, repeat);
14708 if (timer == NULL)
14709 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014710 else
14711 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014712 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014713 timer->tr_callback = vim_strsave(callback);
14714 else
14715 /* pointer into the partial */
14716 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014717 timer->tr_partial = partial;
14718 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014719 }
14720}
14721
14722/*
14723 * "timer_stop(timer)" function
14724 */
14725 static void
14726f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14727{
14728 timer_T *timer;
14729
14730 if (argvars[0].v_type != VAR_NUMBER)
14731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014732 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014733 return;
14734 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014735 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014736 if (timer != NULL)
14737 stop_timer(timer);
14738}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014739
14740/*
14741 * "timer_stopall()" function
14742 */
14743 static void
14744f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14745{
14746 stop_all_timers();
14747}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014748#endif
14749
14750/*
14751 * "tolower(string)" function
14752 */
14753 static void
14754f_tolower(typval_T *argvars, typval_T *rettv)
14755{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014756 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014757 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014758}
14759
14760/*
14761 * "toupper(string)" function
14762 */
14763 static void
14764f_toupper(typval_T *argvars, typval_T *rettv)
14765{
14766 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014767 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014768}
14769
14770/*
14771 * "tr(string, fromstr, tostr)" function
14772 */
14773 static void
14774f_tr(typval_T *argvars, typval_T *rettv)
14775{
14776 char_u *in_str;
14777 char_u *fromstr;
14778 char_u *tostr;
14779 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014780 int inlen;
14781 int fromlen;
14782 int tolen;
14783 int idx;
14784 char_u *cpstr;
14785 int cplen;
14786 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014787 char_u buf[NUMBUFLEN];
14788 char_u buf2[NUMBUFLEN];
14789 garray_T ga;
14790
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014791 in_str = tv_get_string(&argvars[0]);
14792 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14793 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014794
14795 /* Default return value: empty string. */
14796 rettv->v_type = VAR_STRING;
14797 rettv->vval.v_string = NULL;
14798 if (fromstr == NULL || tostr == NULL)
14799 return; /* type error; errmsg already given */
14800 ga_init2(&ga, (int)sizeof(char), 80);
14801
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014802 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014803 /* not multi-byte: fromstr and tostr must be the same length */
14804 if (STRLEN(fromstr) != STRLEN(tostr))
14805 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014806error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014807 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014808 ga_clear(&ga);
14809 return;
14810 }
14811
14812 /* fromstr and tostr have to contain the same number of chars */
14813 while (*in_str != NUL)
14814 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014815 if (has_mbyte)
14816 {
14817 inlen = (*mb_ptr2len)(in_str);
14818 cpstr = in_str;
14819 cplen = inlen;
14820 idx = 0;
14821 for (p = fromstr; *p != NUL; p += fromlen)
14822 {
14823 fromlen = (*mb_ptr2len)(p);
14824 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14825 {
14826 for (p = tostr; *p != NUL; p += tolen)
14827 {
14828 tolen = (*mb_ptr2len)(p);
14829 if (idx-- == 0)
14830 {
14831 cplen = tolen;
14832 cpstr = p;
14833 break;
14834 }
14835 }
14836 if (*p == NUL) /* tostr is shorter than fromstr */
14837 goto error;
14838 break;
14839 }
14840 ++idx;
14841 }
14842
14843 if (first && cpstr == in_str)
14844 {
14845 /* Check that fromstr and tostr have the same number of
14846 * (multi-byte) characters. Done only once when a character
14847 * of in_str doesn't appear in fromstr. */
14848 first = FALSE;
14849 for (p = tostr; *p != NUL; p += tolen)
14850 {
14851 tolen = (*mb_ptr2len)(p);
14852 --idx;
14853 }
14854 if (idx != 0)
14855 goto error;
14856 }
14857
14858 (void)ga_grow(&ga, cplen);
14859 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14860 ga.ga_len += cplen;
14861
14862 in_str += inlen;
14863 }
14864 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014865 {
14866 /* When not using multi-byte chars we can do it faster. */
14867 p = vim_strchr(fromstr, *in_str);
14868 if (p != NULL)
14869 ga_append(&ga, tostr[p - fromstr]);
14870 else
14871 ga_append(&ga, *in_str);
14872 ++in_str;
14873 }
14874 }
14875
14876 /* add a terminating NUL */
14877 (void)ga_grow(&ga, 1);
14878 ga_append(&ga, NUL);
14879
14880 rettv->vval.v_string = ga.ga_data;
14881}
14882
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014883/*
14884 * "trim({expr})" function
14885 */
14886 static void
14887f_trim(typval_T *argvars, typval_T *rettv)
14888{
14889 char_u buf1[NUMBUFLEN];
14890 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014891 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014892 char_u *mask = NULL;
14893 char_u *tail;
14894 char_u *prev;
14895 char_u *p;
14896 int c1;
14897
14898 rettv->v_type = VAR_STRING;
14899 if (head == NULL)
14900 {
14901 rettv->vval.v_string = NULL;
14902 return;
14903 }
14904
14905 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014906 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014907
14908 while (*head != NUL)
14909 {
14910 c1 = PTR2CHAR(head);
14911 if (mask == NULL)
14912 {
14913 if (c1 > ' ' && c1 != 0xa0)
14914 break;
14915 }
14916 else
14917 {
14918 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14919 if (c1 == PTR2CHAR(p))
14920 break;
14921 if (*p == NUL)
14922 break;
14923 }
14924 MB_PTR_ADV(head);
14925 }
14926
14927 for (tail = head + STRLEN(head); tail > head; tail = prev)
14928 {
14929 prev = tail;
14930 MB_PTR_BACK(head, prev);
14931 c1 = PTR2CHAR(prev);
14932 if (mask == NULL)
14933 {
14934 if (c1 > ' ' && c1 != 0xa0)
14935 break;
14936 }
14937 else
14938 {
14939 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14940 if (c1 == PTR2CHAR(p))
14941 break;
14942 if (*p == NUL)
14943 break;
14944 }
14945 }
14946 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14947}
14948
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014949#ifdef FEAT_FLOAT
14950/*
14951 * "trunc({float})" function
14952 */
14953 static void
14954f_trunc(typval_T *argvars, typval_T *rettv)
14955{
14956 float_T f = 0.0;
14957
14958 rettv->v_type = VAR_FLOAT;
14959 if (get_float_arg(argvars, &f) == OK)
14960 /* trunc() is not in C90, use floor() or ceil() instead. */
14961 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
14962 else
14963 rettv->vval.v_float = 0.0;
14964}
14965#endif
14966
14967/*
14968 * "type(expr)" function
14969 */
14970 static void
14971f_type(typval_T *argvars, typval_T *rettv)
14972{
14973 int n = -1;
14974
14975 switch (argvars[0].v_type)
14976 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020014977 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
14978 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014979 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020014980 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
14981 case VAR_LIST: n = VAR_TYPE_LIST; break;
14982 case VAR_DICT: n = VAR_TYPE_DICT; break;
14983 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014984 case VAR_SPECIAL:
14985 if (argvars[0].vval.v_number == VVAL_FALSE
14986 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020014987 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014988 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020014989 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014990 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020014991 case VAR_JOB: n = VAR_TYPE_JOB; break;
14992 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010014993 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014994 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010014995 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014996 n = -1;
14997 break;
14998 }
14999 rettv->vval.v_number = n;
15000}
15001
15002/*
15003 * "undofile(name)" function
15004 */
15005 static void
15006f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15007{
15008 rettv->v_type = VAR_STRING;
15009#ifdef FEAT_PERSISTENT_UNDO
15010 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015011 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015012
15013 if (*fname == NUL)
15014 {
15015 /* If there is no file name there will be no undo file. */
15016 rettv->vval.v_string = NULL;
15017 }
15018 else
15019 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015020 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015021
15022 if (ffname != NULL)
15023 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15024 vim_free(ffname);
15025 }
15026 }
15027#else
15028 rettv->vval.v_string = NULL;
15029#endif
15030}
15031
15032/*
15033 * "undotree()" function
15034 */
15035 static void
15036f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15037{
15038 if (rettv_dict_alloc(rettv) == OK)
15039 {
15040 dict_T *dict = rettv->vval.v_dict;
15041 list_T *list;
15042
Bram Moolenaare0be1672018-07-08 16:50:37 +020015043 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15044 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15045 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15046 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15047 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15048 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015049
15050 list = list_alloc();
15051 if (list != NULL)
15052 {
15053 u_eval_tree(curbuf->b_u_oldhead, list);
15054 dict_add_list(dict, "entries", list);
15055 }
15056 }
15057}
15058
15059/*
15060 * "values(dict)" function
15061 */
15062 static void
15063f_values(typval_T *argvars, typval_T *rettv)
15064{
15065 dict_list(argvars, rettv, 1);
15066}
15067
15068/*
15069 * "virtcol(string)" function
15070 */
15071 static void
15072f_virtcol(typval_T *argvars, typval_T *rettv)
15073{
15074 colnr_T vcol = 0;
15075 pos_T *fp;
15076 int fnum = curbuf->b_fnum;
15077
15078 fp = var2fpos(&argvars[0], FALSE, &fnum);
15079 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15080 && fnum == curbuf->b_fnum)
15081 {
15082 getvvcol(curwin, fp, NULL, NULL, &vcol);
15083 ++vcol;
15084 }
15085
15086 rettv->vval.v_number = vcol;
15087}
15088
15089/*
15090 * "visualmode()" function
15091 */
15092 static void
15093f_visualmode(typval_T *argvars, typval_T *rettv)
15094{
15095 char_u str[2];
15096
15097 rettv->v_type = VAR_STRING;
15098 str[0] = curbuf->b_visual_mode_eval;
15099 str[1] = NUL;
15100 rettv->vval.v_string = vim_strsave(str);
15101
15102 /* A non-zero number or non-empty string argument: reset mode. */
15103 if (non_zero_arg(&argvars[0]))
15104 curbuf->b_visual_mode_eval = NUL;
15105}
15106
15107/*
15108 * "wildmenumode()" function
15109 */
15110 static void
15111f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15112{
15113#ifdef FEAT_WILDMENU
15114 if (wild_menu_showing)
15115 rettv->vval.v_number = 1;
15116#endif
15117}
15118
15119/*
15120 * "winbufnr(nr)" function
15121 */
15122 static void
15123f_winbufnr(typval_T *argvars, typval_T *rettv)
15124{
15125 win_T *wp;
15126
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015127 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015128 if (wp == NULL)
15129 rettv->vval.v_number = -1;
15130 else
15131 rettv->vval.v_number = wp->w_buffer->b_fnum;
15132}
15133
15134/*
15135 * "wincol()" function
15136 */
15137 static void
15138f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15139{
15140 validate_cursor();
15141 rettv->vval.v_number = curwin->w_wcol + 1;
15142}
15143
15144/*
15145 * "winheight(nr)" function
15146 */
15147 static void
15148f_winheight(typval_T *argvars, typval_T *rettv)
15149{
15150 win_T *wp;
15151
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015152 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015153 if (wp == NULL)
15154 rettv->vval.v_number = -1;
15155 else
15156 rettv->vval.v_number = wp->w_height;
15157}
15158
15159/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015160 * "winlayout()" function
15161 */
15162 static void
15163f_winlayout(typval_T *argvars, typval_T *rettv)
15164{
15165 tabpage_T *tp;
15166
15167 if (rettv_list_alloc(rettv) != OK)
15168 return;
15169
15170 if (argvars[0].v_type == VAR_UNKNOWN)
15171 tp = curtab;
15172 else
15173 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015174 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015175 if (tp == NULL)
15176 return;
15177 }
15178
15179 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15180}
15181
15182/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015183 * "winline()" function
15184 */
15185 static void
15186f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15187{
15188 validate_cursor();
15189 rettv->vval.v_number = curwin->w_wrow + 1;
15190}
15191
15192/*
15193 * "winnr()" function
15194 */
15195 static void
15196f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15197{
15198 int nr = 1;
15199
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015200 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015201 rettv->vval.v_number = nr;
15202}
15203
15204/*
15205 * "winrestcmd()" function
15206 */
15207 static void
15208f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15209{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015210 win_T *wp;
15211 int winnr = 1;
15212 garray_T ga;
15213 char_u buf[50];
15214
15215 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015216 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015217 {
15218 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15219 ga_concat(&ga, buf);
15220 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15221 ga_concat(&ga, buf);
15222 ++winnr;
15223 }
15224 ga_append(&ga, NUL);
15225
15226 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015227 rettv->v_type = VAR_STRING;
15228}
15229
15230/*
15231 * "winrestview()" function
15232 */
15233 static void
15234f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15235{
15236 dict_T *dict;
15237
15238 if (argvars[0].v_type != VAR_DICT
15239 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015240 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015241 else
15242 {
15243 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015244 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015245 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015246 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015247 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015248 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015249 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15250 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015251 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015252 curwin->w_set_curswant = FALSE;
15253 }
15254
15255 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015256 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015257#ifdef FEAT_DIFF
15258 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015259 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015260#endif
15261 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015262 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015263 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015264 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015265
15266 check_cursor();
15267 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015268 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015269 changed_window_setting();
15270
15271 if (curwin->w_topline <= 0)
15272 curwin->w_topline = 1;
15273 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15274 curwin->w_topline = curbuf->b_ml.ml_line_count;
15275#ifdef FEAT_DIFF
15276 check_topfill(curwin, TRUE);
15277#endif
15278 }
15279}
15280
15281/*
15282 * "winsaveview()" function
15283 */
15284 static void
15285f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15286{
15287 dict_T *dict;
15288
15289 if (rettv_dict_alloc(rettv) == FAIL)
15290 return;
15291 dict = rettv->vval.v_dict;
15292
Bram Moolenaare0be1672018-07-08 16:50:37 +020015293 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15294 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015295 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015296 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015297 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015298
Bram Moolenaare0be1672018-07-08 16:50:37 +020015299 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015300#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015301 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015302#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015303 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15304 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015305}
15306
15307/*
15308 * "winwidth(nr)" function
15309 */
15310 static void
15311f_winwidth(typval_T *argvars, typval_T *rettv)
15312{
15313 win_T *wp;
15314
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015315 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015316 if (wp == NULL)
15317 rettv->vval.v_number = -1;
15318 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015319 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015320}
15321
15322/*
15323 * "wordcount()" function
15324 */
15325 static void
15326f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15327{
15328 if (rettv_dict_alloc(rettv) == FAIL)
15329 return;
15330 cursor_pos_info(rettv->vval.v_dict);
15331}
15332
15333/*
15334 * "writefile()" function
15335 */
15336 static void
15337f_writefile(typval_T *argvars, typval_T *rettv)
15338{
15339 int binary = FALSE;
15340 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015341#ifdef HAVE_FSYNC
15342 int do_fsync = p_fs;
15343#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015344 char_u *fname;
15345 FILE *fd;
15346 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015347 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015348 list_T *list = NULL;
15349 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015350
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015351 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015352 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015353 return;
15354
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015355 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015356 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015357 list = argvars[0].vval.v_list;
15358 if (list == NULL)
15359 return;
15360 for (li = list->lv_first; li != NULL; li = li->li_next)
15361 if (tv_get_string_chk(&li->li_tv) == NULL)
15362 return;
15363 }
15364 else if (argvars[0].v_type == VAR_BLOB)
15365 {
15366 blob = argvars[0].vval.v_blob;
15367 if (blob == NULL)
15368 return;
15369 }
15370 else
15371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015372 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015373 return;
15374 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015375
15376 if (argvars[2].v_type != VAR_UNKNOWN)
15377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015378 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015379
15380 if (arg2 == NULL)
15381 return;
15382 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015383 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015384 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015385 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015386#ifdef HAVE_FSYNC
15387 if (vim_strchr(arg2, 's') != NULL)
15388 do_fsync = TRUE;
15389 else if (vim_strchr(arg2, 'S') != NULL)
15390 do_fsync = FALSE;
15391#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015392 }
15393
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015394 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015395 if (fname == NULL)
15396 return;
15397
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015398 /* Always open the file in binary mode, library functions have a mind of
15399 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015400 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15401 append ? APPENDBIN : WRITEBIN)) == NULL)
15402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015403 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015404 ret = -1;
15405 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015406 else if (blob)
15407 {
15408 if (write_blob(fd, blob) == FAIL)
15409 ret = -1;
15410#ifdef HAVE_FSYNC
15411 else if (do_fsync)
15412 // Ignore the error, the user wouldn't know what to do about it.
15413 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015414 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015415#endif
15416 fclose(fd);
15417 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015418 else
15419 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015420 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015421 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015422#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015423 else if (do_fsync)
15424 /* Ignore the error, the user wouldn't know what to do about it.
15425 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015426 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015427#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015428 fclose(fd);
15429 }
15430
15431 rettv->vval.v_number = ret;
15432}
15433
15434/*
15435 * "xor(expr, expr)" function
15436 */
15437 static void
15438f_xor(typval_T *argvars, typval_T *rettv)
15439{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015440 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15441 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015442}
15443
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015444#endif /* FEAT_EVAL */