blob: 2abbcfe4a005274b41c0b4686c9b8bd7bcd2abfe [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);
Bram Moolenaar868b7b62019-05-29 21:44:40 +0200495static void f_win_execute(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200496static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
497static void f_win_getid(typval_T *argvars, typval_T *rettv);
498static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
499static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
500static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar22044dc2017-12-02 15:43:37 +0100501static void f_win_screenpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200502static void f_winbufnr(typval_T *argvars, typval_T *rettv);
503static void f_wincol(typval_T *argvars, typval_T *rettv);
504static void f_winheight(typval_T *argvars, typval_T *rettv);
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +0200505static void f_winlayout(typval_T *argvars, typval_T *rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200506static void f_winline(typval_T *argvars, typval_T *rettv);
507static void f_winnr(typval_T *argvars, typval_T *rettv);
508static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
509static void f_winrestview(typval_T *argvars, typval_T *rettv);
510static void f_winsaveview(typval_T *argvars, typval_T *rettv);
511static void f_winwidth(typval_T *argvars, typval_T *rettv);
512static void f_writefile(typval_T *argvars, typval_T *rettv);
513static void f_wordcount(typval_T *argvars, typval_T *rettv);
514static void f_xor(typval_T *argvars, typval_T *rettv);
515
516/*
517 * Array with names and number of arguments of all internal functions
518 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
519 */
520static struct fst
521{
522 char *f_name; /* function name */
523 char f_min_argc; /* minimal number of arguments */
524 char f_max_argc; /* maximal number of arguments */
525 void (*f_func)(typval_T *args, typval_T *rvar);
526 /* implementation of function */
527} functions[] =
528{
529#ifdef FEAT_FLOAT
530 {"abs", 1, 1, f_abs},
531 {"acos", 1, 1, f_acos}, /* WJMc */
532#endif
533 {"add", 2, 2, f_add},
534 {"and", 2, 2, f_and},
535 {"append", 2, 2, f_append},
Bram Moolenaarca851592018-06-06 21:04:07 +0200536 {"appendbufline", 3, 3, f_appendbufline},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200537 {"argc", 0, 1, f_argc},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200538 {"argidx", 0, 0, f_argidx},
539 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare6e39892018-10-25 12:32:11 +0200540 {"argv", 0, 2, f_argv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200541#ifdef FEAT_FLOAT
542 {"asin", 1, 1, f_asin}, /* WJMc */
543#endif
Bram Moolenaarb48e96f2018-02-13 12:26:14 +0100544 {"assert_beeps", 1, 2, f_assert_beeps},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200545 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaard96ff162018-02-18 22:13:29 +0100546 {"assert_equalfile", 2, 2, f_assert_equalfile},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200547 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaar1307d1c2018-10-07 20:16:49 +0200548 {"assert_fails", 1, 3, f_assert_fails},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200549 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaar34215662016-12-04 13:37:41 +0100550 {"assert_inrange", 3, 4, f_assert_inrange},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200551 {"assert_match", 2, 3, f_assert_match},
552 {"assert_notequal", 2, 3, f_assert_notequal},
553 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaar42205552017-03-18 19:42:22 +0100554 {"assert_report", 1, 1, f_assert_report},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200555 {"assert_true", 1, 2, f_assert_true},
556#ifdef FEAT_FLOAT
557 {"atan", 1, 1, f_atan},
558 {"atan2", 2, 2, f_atan2},
559#endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100560#ifdef FEAT_BEVAL
Bram Moolenaarbe0a2592019-05-09 13:50:16 +0200561 {"balloon_gettext", 0, 0, f_balloon_gettext},
Bram Moolenaar59716a22017-03-01 20:32:44 +0100562 {"balloon_show", 1, 1, f_balloon_show},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100563# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +0100564 {"balloon_split", 1, 1, f_balloon_split},
Bram Moolenaar669a8282017-11-19 20:13:05 +0100565# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +0100566#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200567 {"browse", 4, 4, f_browse},
568 {"browsedir", 2, 2, f_browsedir},
569 {"bufexists", 1, 1, f_bufexists},
570 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
571 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
572 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
573 {"buflisted", 1, 1, f_buflisted},
574 {"bufloaded", 1, 1, f_bufloaded},
575 {"bufname", 1, 1, f_bufname},
576 {"bufnr", 1, 2, f_bufnr},
577 {"bufwinid", 1, 1, f_bufwinid},
578 {"bufwinnr", 1, 1, f_bufwinnr},
579 {"byte2line", 1, 1, f_byte2line},
580 {"byteidx", 2, 2, f_byteidx},
581 {"byteidxcomp", 2, 2, f_byteidxcomp},
582 {"call", 2, 3, f_call},
583#ifdef FEAT_FLOAT
584 {"ceil", 1, 1, f_ceil},
585#endif
586#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b785f62016-11-29 21:54:44 +0100587 {"ch_canread", 1, 1, f_ch_canread},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200588 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar0874a832016-09-01 15:11:51 +0200589 {"ch_close_in", 1, 1, f_ch_close_in},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200590 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
591 {"ch_evalraw", 2, 3, f_ch_evalraw},
592 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
593 {"ch_getjob", 1, 1, f_ch_getjob},
594 {"ch_info", 1, 1, f_ch_info},
595 {"ch_log", 1, 2, f_ch_log},
596 {"ch_logfile", 1, 2, f_ch_logfile},
597 {"ch_open", 1, 2, f_ch_open},
598 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100599 {"ch_readblob", 1, 2, f_ch_readblob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200600 {"ch_readraw", 1, 2, f_ch_readraw},
601 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
602 {"ch_sendraw", 2, 3, f_ch_sendraw},
603 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar7ef38102016-09-26 22:36:58 +0200604 {"ch_status", 1, 2, f_ch_status},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200605#endif
606 {"changenr", 0, 0, f_changenr},
607 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar1063f3d2019-05-07 22:06:52 +0200608 {"chdir", 1, 1, f_chdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200609 {"cindent", 1, 1, f_cindent},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100610 {"clearmatches", 0, 1, f_clearmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200611 {"col", 1, 1, f_col},
612#if defined(FEAT_INS_EXPAND)
613 {"complete", 2, 2, f_complete},
614 {"complete_add", 1, 1, f_complete_add},
615 {"complete_check", 0, 0, f_complete_check},
Bram Moolenaarfd133322019-03-29 12:20:27 +0100616 {"complete_info", 0, 1, f_complete_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200617#endif
618 {"confirm", 1, 4, f_confirm},
619 {"copy", 1, 1, f_copy},
620#ifdef FEAT_FLOAT
621 {"cos", 1, 1, f_cos},
622 {"cosh", 1, 1, f_cosh},
623#endif
624 {"count", 2, 4, f_count},
625 {"cscope_connection",0,3, f_cscope_connection},
626 {"cursor", 1, 3, f_cursor},
Bram Moolenaar4f974752019-02-17 17:44:42 +0100627#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +0200628 {"debugbreak", 1, 1, f_debugbreak},
629#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200630 {"deepcopy", 1, 2, f_deepcopy},
631 {"delete", 1, 2, f_delete},
Bram Moolenaard79a2622018-06-07 18:17:46 +0200632 {"deletebufline", 2, 3, f_deletebufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200633 {"did_filetype", 0, 0, f_did_filetype},
634 {"diff_filler", 1, 1, f_diff_filler},
635 {"diff_hlID", 2, 2, f_diff_hlID},
636 {"empty", 1, 1, f_empty},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200637 {"environ", 0, 0, f_environ},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200638 {"escape", 2, 2, f_escape},
639 {"eval", 1, 1, f_eval},
640 {"eventhandler", 0, 0, f_eventhandler},
641 {"executable", 1, 1, f_executable},
642 {"execute", 1, 2, f_execute},
643 {"exepath", 1, 1, f_exepath},
644 {"exists", 1, 1, f_exists},
645#ifdef FEAT_FLOAT
646 {"exp", 1, 1, f_exp},
647#endif
648 {"expand", 1, 3, f_expand},
649 {"extend", 2, 3, f_extend},
650 {"feedkeys", 1, 2, f_feedkeys},
651 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
652 {"filereadable", 1, 1, f_filereadable},
653 {"filewritable", 1, 1, f_filewritable},
654 {"filter", 2, 2, f_filter},
655 {"finddir", 1, 3, f_finddir},
656 {"findfile", 1, 3, f_findfile},
657#ifdef FEAT_FLOAT
658 {"float2nr", 1, 1, f_float2nr},
659 {"floor", 1, 1, f_floor},
660 {"fmod", 2, 2, f_fmod},
661#endif
662 {"fnameescape", 1, 1, f_fnameescape},
663 {"fnamemodify", 2, 2, f_fnamemodify},
664 {"foldclosed", 1, 1, f_foldclosed},
665 {"foldclosedend", 1, 1, f_foldclosedend},
666 {"foldlevel", 1, 1, f_foldlevel},
667 {"foldtext", 0, 0, f_foldtext},
668 {"foldtextresult", 1, 1, f_foldtextresult},
669 {"foreground", 0, 0, f_foreground},
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200670 {"funcref", 1, 3, f_funcref},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200671 {"function", 1, 3, f_function},
672 {"garbagecollect", 0, 1, f_garbagecollect},
673 {"get", 2, 3, f_get},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200674 {"getbufinfo", 0, 1, f_getbufinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200675 {"getbufline", 2, 3, f_getbufline},
676 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar07ad8162018-02-13 13:59:59 +0100677 {"getchangelist", 1, 1, f_getchangelist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200678 {"getchar", 0, 1, f_getchar},
679 {"getcharmod", 0, 0, f_getcharmod},
680 {"getcharsearch", 0, 0, f_getcharsearch},
681 {"getcmdline", 0, 0, f_getcmdline},
682 {"getcmdpos", 0, 0, f_getcmdpos},
683 {"getcmdtype", 0, 0, f_getcmdtype},
684 {"getcmdwintype", 0, 0, f_getcmdwintype},
685#if defined(FEAT_CMDL_COMPL)
Bram Moolenaare9d58a62016-08-13 15:07:41 +0200686 {"getcompletion", 2, 3, f_getcompletion},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200687#endif
688 {"getcurpos", 0, 0, f_getcurpos},
689 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200690 {"getenv", 1, 1, f_getenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200691 {"getfontname", 0, 1, f_getfontname},
692 {"getfperm", 1, 1, f_getfperm},
693 {"getfsize", 1, 1, f_getfsize},
694 {"getftime", 1, 1, f_getftime},
695 {"getftype", 1, 1, f_getftype},
Bram Moolenaar4f505882018-02-10 21:06:32 +0100696 {"getjumplist", 0, 2, f_getjumplist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200697 {"getline", 1, 2, f_getline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200698 {"getloclist", 1, 2, f_getloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100699 {"getmatches", 0, 1, f_getmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200700 {"getpid", 0, 0, f_getpid},
701 {"getpos", 1, 1, f_getpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200702 {"getqflist", 0, 1, f_getqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200703 {"getreg", 0, 3, f_getreg},
704 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200705 {"gettabinfo", 0, 1, f_gettabinfo},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200706 {"gettabvar", 2, 3, f_gettabvar},
707 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100708 {"gettagstack", 0, 1, f_gettagstack},
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200709 {"getwininfo", 0, 1, f_getwininfo},
Bram Moolenaar3f54fd32018-03-03 21:29:55 +0100710 {"getwinpos", 0, 1, f_getwinpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200711 {"getwinposx", 0, 0, f_getwinposx},
712 {"getwinposy", 0, 0, f_getwinposy},
713 {"getwinvar", 2, 3, f_getwinvar},
714 {"glob", 1, 4, f_glob},
715 {"glob2regpat", 1, 1, f_glob2regpat},
716 {"globpath", 2, 5, f_globpath},
717 {"has", 1, 1, f_has},
718 {"has_key", 2, 2, f_has_key},
719 {"haslocaldir", 0, 2, f_haslocaldir},
720 {"hasmapto", 1, 3, f_hasmapto},
721 {"highlightID", 1, 1, f_hlID}, /* obsolete */
722 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
723 {"histadd", 2, 2, f_histadd},
724 {"histdel", 1, 2, f_histdel},
725 {"histget", 1, 2, f_histget},
726 {"histnr", 1, 1, f_histnr},
727 {"hlID", 1, 1, f_hlID},
728 {"hlexists", 1, 1, f_hlexists},
729 {"hostname", 0, 0, f_hostname},
730 {"iconv", 3, 3, f_iconv},
731 {"indent", 1, 1, f_indent},
732 {"index", 2, 4, f_index},
733 {"input", 1, 3, f_input},
734 {"inputdialog", 1, 3, f_inputdialog},
735 {"inputlist", 1, 1, f_inputlist},
736 {"inputrestore", 0, 0, f_inputrestore},
737 {"inputsave", 0, 0, f_inputsave},
738 {"inputsecret", 1, 2, f_inputsecret},
739 {"insert", 2, 3, f_insert},
740 {"invert", 1, 1, f_invert},
741 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaarfda1bff2019-04-04 13:44:37 +0200742#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
743 {"isinf", 1, 1, f_isinf},
744#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200745 {"islocked", 1, 1, f_islocked},
746#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
747 {"isnan", 1, 1, f_isnan},
748#endif
749 {"items", 1, 1, f_items},
750#ifdef FEAT_JOB_CHANNEL
751 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaare1fc5152018-04-21 19:49:08 +0200752 {"job_info", 0, 1, f_job_info},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200753 {"job_setoptions", 2, 2, f_job_setoptions},
754 {"job_start", 1, 2, f_job_start},
755 {"job_status", 1, 1, f_job_status},
756 {"job_stop", 1, 2, f_job_stop},
757#endif
758 {"join", 1, 2, f_join},
759 {"js_decode", 1, 1, f_js_decode},
760 {"js_encode", 1, 1, f_js_encode},
761 {"json_decode", 1, 1, f_json_decode},
762 {"json_encode", 1, 1, f_json_encode},
763 {"keys", 1, 1, f_keys},
764 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
765 {"len", 1, 1, f_len},
766 {"libcall", 3, 3, f_libcall},
767 {"libcallnr", 3, 3, f_libcallnr},
768 {"line", 1, 1, f_line},
769 {"line2byte", 1, 1, f_line2byte},
770 {"lispindent", 1, 1, f_lispindent},
Bram Moolenaar9d401282019-04-06 13:18:12 +0200771 {"list2str", 1, 2, f_list2str},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200772 {"listener_add", 1, 2, f_listener_add},
Bram Moolenaarfe1ade02019-05-14 21:20:36 +0200773 {"listener_flush", 0, 1, f_listener_flush},
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200774 {"listener_remove", 1, 1, f_listener_remove},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200775 {"localtime", 0, 0, f_localtime},
776#ifdef FEAT_FLOAT
777 {"log", 1, 1, f_log},
778 {"log10", 1, 1, f_log10},
779#endif
780#ifdef FEAT_LUA
781 {"luaeval", 1, 2, f_luaeval},
782#endif
783 {"map", 2, 2, f_map},
784 {"maparg", 1, 4, f_maparg},
785 {"mapcheck", 1, 3, f_mapcheck},
786 {"match", 2, 4, f_match},
787 {"matchadd", 2, 5, f_matchadd},
788 {"matchaddpos", 2, 5, f_matchaddpos},
789 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100790 {"matchdelete", 1, 2, f_matchdelete},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200791 {"matchend", 2, 4, f_matchend},
792 {"matchlist", 2, 4, f_matchlist},
793 {"matchstr", 2, 4, f_matchstr},
794 {"matchstrpos", 2, 4, f_matchstrpos},
795 {"max", 1, 1, f_max},
796 {"min", 1, 1, f_min},
797#ifdef vim_mkdir
798 {"mkdir", 1, 3, f_mkdir},
799#endif
800 {"mode", 0, 1, f_mode},
801#ifdef FEAT_MZSCHEME
802 {"mzeval", 1, 1, f_mzeval},
803#endif
804 {"nextnonblank", 1, 1, f_nextnonblank},
805 {"nr2char", 1, 2, f_nr2char},
806 {"or", 2, 2, f_or},
807 {"pathshorten", 1, 1, f_pathshorten},
808#ifdef FEAT_PERL
809 {"perleval", 1, 1, f_perleval},
810#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200811#ifdef FEAT_TEXT_PROP
812 {"popup_close", 1, 1, f_popup_close},
813 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaarbc133542019-05-29 20:26:46 +0200814 {"popup_getposition", 1, 1, f_popup_getposition},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200815 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200816 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200817 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200819#ifdef FEAT_FLOAT
820 {"pow", 2, 2, f_pow},
821#endif
822 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100823 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200824#ifdef FEAT_JOB_CHANNEL
825 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200826 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200827 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
828#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100829#ifdef FEAT_TEXT_PROP
830 {"prop_add", 3, 3, f_prop_add},
831 {"prop_clear", 1, 3, f_prop_clear},
832 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100833 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100834 {"prop_type_add", 2, 2, f_prop_type_add},
835 {"prop_type_change", 2, 2, f_prop_type_change},
836 {"prop_type_delete", 1, 2, f_prop_type_delete},
837 {"prop_type_get", 1, 2, f_prop_type_get},
838 {"prop_type_list", 0, 1, f_prop_type_list},
839#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200840 {"pumvisible", 0, 0, f_pumvisible},
841#ifdef FEAT_PYTHON3
842 {"py3eval", 1, 1, f_py3eval},
843#endif
844#ifdef FEAT_PYTHON
845 {"pyeval", 1, 1, f_pyeval},
846#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100847#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
848 {"pyxeval", 1, 1, f_pyxeval},
849#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200850 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200851 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200853 {"reg_executing", 0, 0, f_reg_executing},
854 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200855 {"reltime", 0, 2, f_reltime},
856#ifdef FEAT_FLOAT
857 {"reltimefloat", 1, 1, f_reltimefloat},
858#endif
859 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100860 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200861 {"remote_foreground", 1, 1, f_remote_foreground},
862 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100863 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200864 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100865 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"remove", 2, 3, f_remove},
867 {"rename", 2, 2, f_rename},
868 {"repeat", 2, 2, f_repeat},
869 {"resolve", 1, 1, f_resolve},
870 {"reverse", 1, 1, f_reverse},
871#ifdef FEAT_FLOAT
872 {"round", 1, 1, f_round},
873#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100874#ifdef FEAT_RUBY
875 {"rubyeval", 1, 1, f_rubyeval},
876#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200877 {"screenattr", 2, 2, f_screenattr},
878 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100879 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200880 {"screencol", 0, 0, f_screencol},
881 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100882 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200883 {"search", 1, 4, f_search},
884 {"searchdecl", 1, 3, f_searchdecl},
885 {"searchpair", 3, 7, f_searchpair},
886 {"searchpairpos", 3, 7, f_searchpairpos},
887 {"searchpos", 1, 4, f_searchpos},
888 {"server2client", 2, 2, f_server2client},
889 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200890 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200891 {"setbufvar", 3, 3, f_setbufvar},
892 {"setcharsearch", 1, 1, f_setcharsearch},
893 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200894 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200895 {"setfperm", 2, 2, f_setfperm},
896 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200897 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100898 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200899 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200900 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200901 {"setreg", 2, 3, f_setreg},
902 {"settabvar", 3, 3, f_settabvar},
903 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100904 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200905 {"setwinvar", 3, 3, f_setwinvar},
906#ifdef FEAT_CRYPT
907 {"sha256", 1, 1, f_sha256},
908#endif
909 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100910 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100911#ifdef FEAT_SIGNS
912 {"sign_define", 1, 2, f_sign_define},
913 {"sign_getdefined", 0, 1, f_sign_getdefined},
914 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100915 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100916 {"sign_place", 4, 5, f_sign_place},
917 {"sign_undefine", 0, 1, f_sign_undefine},
918 {"sign_unplace", 1, 2, f_sign_unplace},
919#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200920 {"simplify", 1, 1, f_simplify},
921#ifdef FEAT_FLOAT
922 {"sin", 1, 1, f_sin},
923 {"sinh", 1, 1, f_sinh},
924#endif
925 {"sort", 1, 3, f_sort},
926 {"soundfold", 1, 1, f_soundfold},
927 {"spellbadword", 0, 1, f_spellbadword},
928 {"spellsuggest", 1, 3, f_spellsuggest},
929 {"split", 1, 3, f_split},
930#ifdef FEAT_FLOAT
931 {"sqrt", 1, 1, f_sqrt},
932 {"str2float", 1, 1, f_str2float},
933#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200934 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200935 {"str2nr", 1, 2, f_str2nr},
936 {"strcharpart", 2, 3, f_strcharpart},
937 {"strchars", 1, 2, f_strchars},
938 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
939#ifdef HAVE_STRFTIME
940 {"strftime", 1, 2, f_strftime},
941#endif
942 {"strgetchar", 2, 2, f_strgetchar},
943 {"stridx", 2, 3, f_stridx},
944 {"string", 1, 1, f_string},
945 {"strlen", 1, 1, f_strlen},
946 {"strpart", 2, 3, f_strpart},
947 {"strridx", 2, 3, f_strridx},
948 {"strtrans", 1, 1, f_strtrans},
949 {"strwidth", 1, 1, f_strwidth},
950 {"submatch", 1, 2, f_submatch},
951 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200952 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200953 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200954 {"synID", 3, 3, f_synID},
955 {"synIDattr", 2, 3, f_synIDattr},
956 {"synIDtrans", 1, 1, f_synIDtrans},
957 {"synconcealed", 2, 2, f_synconcealed},
958 {"synstack", 2, 2, f_synstack},
959 {"system", 1, 2, f_system},
960 {"systemlist", 1, 2, f_systemlist},
961 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
962 {"tabpagenr", 0, 1, f_tabpagenr},
963 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
964 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100965 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200966#ifdef FEAT_FLOAT
967 {"tan", 1, 1, f_tan},
968 {"tanh", 1, 1, f_tanh},
969#endif
970 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100972 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
973 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100974 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200975 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200976# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
977 {"term_getansicolors", 1, 1, f_term_getansicolors},
978# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200979 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200980 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200982 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200983 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200984 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200985 {"term_getstatus", 1, 1, f_term_getstatus},
986 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200987 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200988 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200989 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200991# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
992 {"term_setansicolors", 2, 2, f_term_setansicolors},
993# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100994 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100995 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200996 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200997 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +0200998 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001000 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1001 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001002 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001003 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001004 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001005 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001006 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001007#ifdef FEAT_JOB_CHANNEL
1008 {"test_null_channel", 0, 0, f_test_null_channel},
1009#endif
1010 {"test_null_dict", 0, 0, f_test_null_dict},
1011#ifdef FEAT_JOB_CHANNEL
1012 {"test_null_job", 0, 0, f_test_null_job},
1013#endif
1014 {"test_null_list", 0, 0, f_test_null_list},
1015 {"test_null_partial", 0, 0, f_test_null_partial},
1016 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001017 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001018 {"test_override", 2, 2, f_test_override},
1019 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001020#ifdef FEAT_GUI
1021 {"test_scrollbar", 3, 3, f_test_scrollbar},
1022#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001023#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001024 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001025#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001026 {"test_settime", 1, 1, f_test_settime},
1027#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001028 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001029 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001030 {"timer_start", 2, 3, f_timer_start},
1031 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001032 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001033#endif
1034 {"tolower", 1, 1, f_tolower},
1035 {"toupper", 1, 1, f_toupper},
1036 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001037 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001038#ifdef FEAT_FLOAT
1039 {"trunc", 1, 1, f_trunc},
1040#endif
1041 {"type", 1, 1, f_type},
1042 {"undofile", 1, 1, f_undofile},
1043 {"undotree", 0, 0, f_undotree},
1044 {"uniq", 1, 3, f_uniq},
1045 {"values", 1, 1, f_values},
1046 {"virtcol", 1, 1, f_virtcol},
1047 {"visualmode", 0, 1, f_visualmode},
1048 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001049 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001050 {"win_findbuf", 1, 1, f_win_findbuf},
1051 {"win_getid", 0, 2, f_win_getid},
1052 {"win_gotoid", 1, 1, f_win_gotoid},
1053 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1054 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001055 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001056 {"winbufnr", 1, 1, f_winbufnr},
1057 {"wincol", 0, 0, f_wincol},
1058 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001059 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001060 {"winline", 0, 0, f_winline},
1061 {"winnr", 0, 1, f_winnr},
1062 {"winrestcmd", 0, 0, f_winrestcmd},
1063 {"winrestview", 1, 1, f_winrestview},
1064 {"winsaveview", 0, 0, f_winsaveview},
1065 {"winwidth", 1, 1, f_winwidth},
1066 {"wordcount", 0, 0, f_wordcount},
1067 {"writefile", 2, 3, f_writefile},
1068 {"xor", 2, 2, f_xor},
1069};
1070
1071#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1072
1073/*
1074 * Function given to ExpandGeneric() to obtain the list of internal
1075 * or user defined function names.
1076 */
1077 char_u *
1078get_function_name(expand_T *xp, int idx)
1079{
1080 static int intidx = -1;
1081 char_u *name;
1082
1083 if (idx == 0)
1084 intidx = -1;
1085 if (intidx < 0)
1086 {
1087 name = get_user_func_name(xp, idx);
1088 if (name != NULL)
1089 return name;
1090 }
1091 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1092 {
1093 STRCPY(IObuff, functions[intidx].f_name);
1094 STRCAT(IObuff, "(");
1095 if (functions[intidx].f_max_argc == 0)
1096 STRCAT(IObuff, ")");
1097 return IObuff;
1098 }
1099
1100 return NULL;
1101}
1102
1103/*
1104 * Function given to ExpandGeneric() to obtain the list of internal or
1105 * user defined variable or function names.
1106 */
1107 char_u *
1108get_expr_name(expand_T *xp, int idx)
1109{
1110 static int intidx = -1;
1111 char_u *name;
1112
1113 if (idx == 0)
1114 intidx = -1;
1115 if (intidx < 0)
1116 {
1117 name = get_function_name(xp, idx);
1118 if (name != NULL)
1119 return name;
1120 }
1121 return get_user_var_name(xp, ++intidx);
1122}
1123
1124#endif /* FEAT_CMDL_COMPL */
1125
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001126/*
1127 * Find internal function in table above.
1128 * Return index, or -1 if not found
1129 */
1130 int
1131find_internal_func(
1132 char_u *name) /* name of the function */
1133{
1134 int first = 0;
1135 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1136 int cmp;
1137 int x;
1138
1139 /*
1140 * Find the function name in the table. Binary search.
1141 */
1142 while (first <= last)
1143 {
1144 x = first + ((unsigned)(last - first) >> 1);
1145 cmp = STRCMP(name, functions[x].f_name);
1146 if (cmp < 0)
1147 last = x - 1;
1148 else if (cmp > 0)
1149 first = x + 1;
1150 else
1151 return x;
1152 }
1153 return -1;
1154}
1155
1156 int
1157call_internal_func(
1158 char_u *name,
1159 int argcount,
1160 typval_T *argvars,
1161 typval_T *rettv)
1162{
1163 int i;
1164
1165 i = find_internal_func(name);
1166 if (i < 0)
1167 return ERROR_UNKNOWN;
1168 if (argcount < functions[i].f_min_argc)
1169 return ERROR_TOOFEW;
1170 if (argcount > functions[i].f_max_argc)
1171 return ERROR_TOOMANY;
1172 argvars[argcount].v_type = VAR_UNKNOWN;
1173 functions[i].f_func(argvars, rettv);
1174 return ERROR_NONE;
1175}
1176
1177/*
1178 * Return TRUE for a non-zero Number and a non-empty String.
1179 */
1180 static int
1181non_zero_arg(typval_T *argvars)
1182{
1183 return ((argvars[0].v_type == VAR_NUMBER
1184 && argvars[0].vval.v_number != 0)
1185 || (argvars[0].v_type == VAR_SPECIAL
1186 && argvars[0].vval.v_number == VVAL_TRUE)
1187 || (argvars[0].v_type == VAR_STRING
1188 && argvars[0].vval.v_string != NULL
1189 && *argvars[0].vval.v_string != NUL));
1190}
1191
1192/*
1193 * Get the lnum from the first argument.
1194 * Also accepts ".", "$", etc., but that only works for the current buffer.
1195 * Returns -1 on error.
1196 */
1197 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001198tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001199{
1200 typval_T rettv;
1201 linenr_T lnum;
1202
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001203 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001204 if (lnum == 0) /* no valid number, try using line() */
1205 {
1206 rettv.v_type = VAR_NUMBER;
1207 f_line(argvars, &rettv);
1208 lnum = (linenr_T)rettv.vval.v_number;
1209 clear_tv(&rettv);
1210 }
1211 return lnum;
1212}
1213
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001214/*
1215 * Get the lnum from the first argument.
1216 * Also accepts "$", then "buf" is used.
1217 * Returns 0 on error.
1218 */
1219 static linenr_T
1220tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1221{
1222 if (argvars[0].v_type == VAR_STRING
1223 && argvars[0].vval.v_string != NULL
1224 && argvars[0].vval.v_string[0] == '$'
1225 && buf != NULL)
1226 return buf->b_ml.ml_line_count;
1227 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1228}
1229
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001230#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001231/*
1232 * Get the float value of "argvars[0]" into "f".
1233 * Returns FAIL when the argument is not a Number or Float.
1234 */
1235 static int
1236get_float_arg(typval_T *argvars, float_T *f)
1237{
1238 if (argvars[0].v_type == VAR_FLOAT)
1239 {
1240 *f = argvars[0].vval.v_float;
1241 return OK;
1242 }
1243 if (argvars[0].v_type == VAR_NUMBER)
1244 {
1245 *f = (float_T)argvars[0].vval.v_number;
1246 return OK;
1247 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001248 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001249 return FAIL;
1250}
1251
1252/*
1253 * "abs(expr)" function
1254 */
1255 static void
1256f_abs(typval_T *argvars, typval_T *rettv)
1257{
1258 if (argvars[0].v_type == VAR_FLOAT)
1259 {
1260 rettv->v_type = VAR_FLOAT;
1261 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1262 }
1263 else
1264 {
1265 varnumber_T n;
1266 int error = FALSE;
1267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001268 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001269 if (error)
1270 rettv->vval.v_number = -1;
1271 else if (n > 0)
1272 rettv->vval.v_number = n;
1273 else
1274 rettv->vval.v_number = -n;
1275 }
1276}
1277
1278/*
1279 * "acos()" function
1280 */
1281 static void
1282f_acos(typval_T *argvars, typval_T *rettv)
1283{
1284 float_T f = 0.0;
1285
1286 rettv->v_type = VAR_FLOAT;
1287 if (get_float_arg(argvars, &f) == OK)
1288 rettv->vval.v_float = acos(f);
1289 else
1290 rettv->vval.v_float = 0.0;
1291}
1292#endif
1293
1294/*
1295 * "add(list, item)" function
1296 */
1297 static void
1298f_add(typval_T *argvars, typval_T *rettv)
1299{
1300 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001301 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001302
1303 rettv->vval.v_number = 1; /* Default: Failed */
1304 if (argvars[0].v_type == VAR_LIST)
1305 {
1306 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001307 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001308 (char_u *)N_("add() argument"), TRUE)
1309 && list_append_tv(l, &argvars[1]) == OK)
1310 copy_tv(&argvars[0], rettv);
1311 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001312 else if (argvars[0].v_type == VAR_BLOB)
1313 {
1314 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001315 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001316 (char_u *)N_("add() argument"), TRUE))
1317 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001318 int error = FALSE;
1319 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1320
1321 if (!error)
1322 {
1323 ga_append(&b->bv_ga, (int)n);
1324 copy_tv(&argvars[0], rettv);
1325 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001326 }
1327 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001328 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001329 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330}
1331
1332/*
1333 * "and(expr, expr)" function
1334 */
1335 static void
1336f_and(typval_T *argvars, typval_T *rettv)
1337{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001338 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1339 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001340}
1341
1342/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001343 * If there is a window for "curbuf", make it the current window.
1344 */
1345 static void
1346find_win_for_curbuf(void)
1347{
1348 wininfo_T *wip;
1349
1350 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1351 {
1352 if (wip->wi_win != NULL)
1353 {
1354 curwin = wip->wi_win;
1355 break;
1356 }
1357 }
1358}
1359
1360/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001361 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001362 */
1363 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001364set_buffer_lines(
1365 buf_T *buf,
1366 linenr_T lnum_arg,
1367 int append,
1368 typval_T *lines,
1369 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001370{
Bram Moolenaarca851592018-06-06 21:04:07 +02001371 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1372 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001373 list_T *l = NULL;
1374 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001376 linenr_T append_lnum;
1377 buf_T *curbuf_save = NULL;
1378 win_T *curwin_save = NULL;
1379 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001380
Bram Moolenaarca851592018-06-06 21:04:07 +02001381 /* When using the current buffer ml_mfp will be set if needed. Useful when
1382 * setline() is used on startup. For other buffers the buffer must be
1383 * loaded. */
1384 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001385 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001386 rettv->vval.v_number = 1; /* FAIL */
1387 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001388 }
1389
Bram Moolenaarca851592018-06-06 21:04:07 +02001390 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001391 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 curbuf_save = curbuf;
1393 curwin_save = curwin;
1394 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001395 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001396 }
1397
1398 if (append)
1399 // appendbufline() uses the line number below which we insert
1400 append_lnum = lnum - 1;
1401 else
1402 // setbufline() uses the line number above which we insert, we only
1403 // append if it's below the last line
1404 append_lnum = curbuf->b_ml.ml_line_count;
1405
1406 if (lines->v_type == VAR_LIST)
1407 {
1408 l = lines->vval.v_list;
1409 li = l->lv_first;
1410 }
1411 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001412 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001413
1414 /* default result is zero == OK */
1415 for (;;)
1416 {
1417 if (l != NULL)
1418 {
1419 /* list argument, get next string */
1420 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001421 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001422 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 li = li->li_next;
1424 }
1425
Bram Moolenaarca851592018-06-06 21:04:07 +02001426 rettv->vval.v_number = 1; /* FAIL */
1427 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1428 break;
1429
1430 /* When coming here from Insert mode, sync undo, so that this can be
1431 * undone separately from what was previously inserted. */
1432 if (u_sync_once == 2)
1433 {
1434 u_sync_once = 1; /* notify that u_sync() was called */
1435 u_sync(TRUE);
1436 }
1437
1438 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1439 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001440 // Existing line, replace it.
1441 // Removes any existing text properties.
1442 if (u_savesub(lnum) == OK && ml_replace_len(
1443 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001444 {
1445 changed_bytes(lnum, 0);
1446 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1447 check_cursor_col();
1448 rettv->vval.v_number = 0; /* OK */
1449 }
1450 }
1451 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1452 {
1453 /* append the line */
1454 ++added;
1455 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1456 rettv->vval.v_number = 0; /* OK */
1457 }
1458
1459 if (l == NULL) /* only one string argument */
1460 break;
1461 ++lnum;
1462 }
1463
1464 if (added > 0)
1465 {
1466 win_T *wp;
1467 tabpage_T *tp;
1468
1469 appended_lines_mark(append_lnum, added);
1470 FOR_ALL_TAB_WINDOWS(tp, wp)
1471 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1472 wp->w_cursor.lnum += added;
1473 check_cursor_col();
1474
Bram Moolenaarf2732452018-06-03 14:47:35 +02001475#ifdef FEAT_JOB_CHANNEL
1476 if (bt_prompt(curbuf) && (State & INSERT))
1477 // show the line with the prompt
1478 update_topline();
1479#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001480 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001481
1482 if (!is_curbuf)
1483 {
1484 curbuf = curbuf_save;
1485 curwin = curwin_save;
1486 }
1487}
1488
1489/*
1490 * "append(lnum, string/list)" function
1491 */
1492 static void
1493f_append(typval_T *argvars, typval_T *rettv)
1494{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001495 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001496
1497 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1498}
1499
1500/*
1501 * "appendbufline(buf, lnum, string/list)" function
1502 */
1503 static void
1504f_appendbufline(typval_T *argvars, typval_T *rettv)
1505{
1506 linenr_T lnum;
1507 buf_T *buf;
1508
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001509 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001510 if (buf == NULL)
1511 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001512 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001513 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001514 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001515 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1516 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001517}
1518
1519/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001520 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001521 */
1522 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001523f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001524{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001525 win_T *wp;
1526
1527 if (argvars[0].v_type == VAR_UNKNOWN)
1528 // use the current window
1529 rettv->vval.v_number = ARGCOUNT;
1530 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001531 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001532 // use the global argument list
1533 rettv->vval.v_number = GARGCOUNT;
1534 else
1535 {
1536 // use the argument list of the specified window
1537 wp = find_win_by_nr_or_id(&argvars[0]);
1538 if (wp != NULL)
1539 rettv->vval.v_number = WARGCOUNT(wp);
1540 else
1541 rettv->vval.v_number = -1;
1542 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001543}
1544
1545/*
1546 * "argidx()" function
1547 */
1548 static void
1549f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1550{
1551 rettv->vval.v_number = curwin->w_arg_idx;
1552}
1553
1554/*
1555 * "arglistid()" function
1556 */
1557 static void
1558f_arglistid(typval_T *argvars, typval_T *rettv)
1559{
1560 win_T *wp;
1561
1562 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001563 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001564 if (wp != NULL)
1565 rettv->vval.v_number = wp->w_alist->id;
1566}
1567
1568/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001569 * Get the argument list for a given window
1570 */
1571 static void
1572get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1573{
1574 int idx;
1575
1576 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1577 for (idx = 0; idx < argcount; ++idx)
1578 list_append_string(rettv->vval.v_list,
1579 alist_name(&arglist[idx]), -1);
1580}
1581
1582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001583 * "argv(nr)" function
1584 */
1585 static void
1586f_argv(typval_T *argvars, typval_T *rettv)
1587{
1588 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001589 aentry_T *arglist = NULL;
1590 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001591
1592 if (argvars[0].v_type != VAR_UNKNOWN)
1593 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001594 if (argvars[1].v_type == VAR_UNKNOWN)
1595 {
1596 arglist = ARGLIST;
1597 argcount = ARGCOUNT;
1598 }
1599 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001600 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001601 {
1602 arglist = GARGLIST;
1603 argcount = GARGCOUNT;
1604 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001605 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001606 {
1607 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1608
1609 if (wp != NULL)
1610 {
1611 /* Use the argument list of the specified window */
1612 arglist = WARGLIST(wp);
1613 argcount = WARGCOUNT(wp);
1614 }
1615 }
1616
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001617 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001618 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001619 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001620 if (arglist != NULL && idx >= 0 && idx < argcount)
1621 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1622 else if (idx == -1)
1623 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001624 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001625 else
1626 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001627}
1628
1629/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001630 * "assert_beeps(cmd [, error])" function
1631 */
1632 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001633f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001634{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001636}
1637
1638/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001639 * "assert_equal(expected, actual[, msg])" function
1640 */
1641 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001642f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001643{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645}
1646
1647/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001648 * "assert_equalfile(fname-one, fname-two)" function
1649 */
1650 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001651f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001652{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001654}
1655
1656/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001657 * "assert_notequal(expected, actual[, msg])" function
1658 */
1659 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001660f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001661{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663}
1664
1665/*
1666 * "assert_exception(string[, msg])" function
1667 */
1668 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001669f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001670{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672}
1673
1674/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001675 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001676 */
1677 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001678f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001679{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681}
1682
1683/*
1684 * "assert_false(actual[, msg])" function
1685 */
1686 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001687f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001688{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690}
1691
1692/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001693 * "assert_inrange(lower, upper[, msg])" function
1694 */
1695 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001696f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001697{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001699}
1700
1701/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001702 * "assert_match(pattern, actual[, msg])" function
1703 */
1704 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001705f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001706{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708}
1709
1710/*
1711 * "assert_notmatch(pattern, actual[, msg])" function
1712 */
1713 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001714f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001715{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717}
1718
1719/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001720 * "assert_report(msg)" function
1721 */
1722 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001723f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001724{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001725 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001726}
1727
1728/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001729 * "assert_true(actual[, msg])" function
1730 */
1731 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001732f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001733{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001734 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735}
1736
1737#ifdef FEAT_FLOAT
1738/*
1739 * "asin()" function
1740 */
1741 static void
1742f_asin(typval_T *argvars, typval_T *rettv)
1743{
1744 float_T f = 0.0;
1745
1746 rettv->v_type = VAR_FLOAT;
1747 if (get_float_arg(argvars, &f) == OK)
1748 rettv->vval.v_float = asin(f);
1749 else
1750 rettv->vval.v_float = 0.0;
1751}
1752
1753/*
1754 * "atan()" function
1755 */
1756 static void
1757f_atan(typval_T *argvars, typval_T *rettv)
1758{
1759 float_T f = 0.0;
1760
1761 rettv->v_type = VAR_FLOAT;
1762 if (get_float_arg(argvars, &f) == OK)
1763 rettv->vval.v_float = atan(f);
1764 else
1765 rettv->vval.v_float = 0.0;
1766}
1767
1768/*
1769 * "atan2()" function
1770 */
1771 static void
1772f_atan2(typval_T *argvars, typval_T *rettv)
1773{
1774 float_T fx = 0.0, fy = 0.0;
1775
1776 rettv->v_type = VAR_FLOAT;
1777 if (get_float_arg(argvars, &fx) == OK
1778 && get_float_arg(&argvars[1], &fy) == OK)
1779 rettv->vval.v_float = atan2(fx, fy);
1780 else
1781 rettv->vval.v_float = 0.0;
1782}
1783#endif
1784
1785/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001786 * "balloon_show()" function
1787 */
1788#ifdef FEAT_BEVAL
1789 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001790f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1791{
1792 rettv->v_type = VAR_STRING;
1793 if (balloonEval != NULL)
1794 {
1795 if (balloonEval->msg == NULL)
1796 rettv->vval.v_string = NULL;
1797 else
1798 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1799 }
1800}
1801
1802 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001803f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1804{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001805 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001806 {
1807 if (argvars[0].v_type == VAR_LIST
1808# ifdef FEAT_GUI
1809 && !gui.in_use
1810# endif
1811 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001812 {
1813 list_T *l = argvars[0].vval.v_list;
1814
1815 // empty list removes the balloon
1816 post_balloon(balloonEval, NULL,
1817 l == NULL || l->lv_len == 0 ? NULL : l);
1818 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001819 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001820 {
1821 char_u *mesg = tv_get_string_chk(&argvars[0]);
1822
1823 if (mesg != NULL)
1824 // empty string removes the balloon
1825 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1826 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001827 }
1828}
1829
Bram Moolenaar669a8282017-11-19 20:13:05 +01001830# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001831 static void
1832f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1833{
1834 if (rettv_list_alloc(rettv) == OK)
1835 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001836 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001837
1838 if (msg != NULL)
1839 {
1840 pumitem_T *array;
1841 int size = split_message(msg, &array);
1842 int i;
1843
1844 /* Skip the first and last item, they are always empty. */
1845 for (i = 1; i < size - 1; ++i)
1846 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001847 while (size > 0)
1848 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001849 vim_free(array);
1850 }
1851 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001852}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001853# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001854#endif
1855
1856/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001857 * "browse(save, title, initdir, default)" function
1858 */
1859 static void
1860f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1861{
1862#ifdef FEAT_BROWSE
1863 int save;
1864 char_u *title;
1865 char_u *initdir;
1866 char_u *defname;
1867 char_u buf[NUMBUFLEN];
1868 char_u buf2[NUMBUFLEN];
1869 int error = FALSE;
1870
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001871 save = (int)tv_get_number_chk(&argvars[0], &error);
1872 title = tv_get_string_chk(&argvars[1]);
1873 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1874 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001875
1876 if (error || title == NULL || initdir == NULL || defname == NULL)
1877 rettv->vval.v_string = NULL;
1878 else
1879 rettv->vval.v_string =
1880 do_browse(save ? BROWSE_SAVE : 0,
1881 title, defname, NULL, initdir, NULL, curbuf);
1882#else
1883 rettv->vval.v_string = NULL;
1884#endif
1885 rettv->v_type = VAR_STRING;
1886}
1887
1888/*
1889 * "browsedir(title, initdir)" function
1890 */
1891 static void
1892f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1893{
1894#ifdef FEAT_BROWSE
1895 char_u *title;
1896 char_u *initdir;
1897 char_u buf[NUMBUFLEN];
1898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001899 title = tv_get_string_chk(&argvars[0]);
1900 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001901
1902 if (title == NULL || initdir == NULL)
1903 rettv->vval.v_string = NULL;
1904 else
1905 rettv->vval.v_string = do_browse(BROWSE_DIR,
1906 title, NULL, NULL, initdir, NULL, curbuf);
1907#else
1908 rettv->vval.v_string = NULL;
1909#endif
1910 rettv->v_type = VAR_STRING;
1911}
1912
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001913/*
1914 * Find a buffer by number or exact name.
1915 */
1916 static buf_T *
1917find_buffer(typval_T *avar)
1918{
1919 buf_T *buf = NULL;
1920
1921 if (avar->v_type == VAR_NUMBER)
1922 buf = buflist_findnr((int)avar->vval.v_number);
1923 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1924 {
1925 buf = buflist_findname_exp(avar->vval.v_string);
1926 if (buf == NULL)
1927 {
1928 /* No full path name match, try a match with a URL or a "nofile"
1929 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001930 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001931 if (buf->b_fname != NULL
1932 && (path_with_url(buf->b_fname)
1933#ifdef FEAT_QUICKFIX
1934 || bt_nofile(buf)
1935#endif
1936 )
1937 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1938 break;
1939 }
1940 }
1941 return buf;
1942}
1943
1944/*
1945 * "bufexists(expr)" function
1946 */
1947 static void
1948f_bufexists(typval_T *argvars, typval_T *rettv)
1949{
1950 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1951}
1952
1953/*
1954 * "buflisted(expr)" function
1955 */
1956 static void
1957f_buflisted(typval_T *argvars, typval_T *rettv)
1958{
1959 buf_T *buf;
1960
1961 buf = find_buffer(&argvars[0]);
1962 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1963}
1964
1965/*
1966 * "bufloaded(expr)" function
1967 */
1968 static void
1969f_bufloaded(typval_T *argvars, typval_T *rettv)
1970{
1971 buf_T *buf;
1972
1973 buf = find_buffer(&argvars[0]);
1974 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1975}
1976
1977 buf_T *
1978buflist_find_by_name(char_u *name, int curtab_only)
1979{
1980 int save_magic;
1981 char_u *save_cpo;
1982 buf_T *buf;
1983
1984 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1985 save_magic = p_magic;
1986 p_magic = TRUE;
1987 save_cpo = p_cpo;
1988 p_cpo = (char_u *)"";
1989
1990 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1991 TRUE, FALSE, curtab_only));
1992
1993 p_magic = save_magic;
1994 p_cpo = save_cpo;
1995 return buf;
1996}
1997
1998/*
1999 * Get buffer by number or pattern.
2000 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002001 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002002tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002003{
2004 char_u *name = tv->vval.v_string;
2005 buf_T *buf;
2006
2007 if (tv->v_type == VAR_NUMBER)
2008 return buflist_findnr((int)tv->vval.v_number);
2009 if (tv->v_type != VAR_STRING)
2010 return NULL;
2011 if (name == NULL || *name == NUL)
2012 return curbuf;
2013 if (name[0] == '$' && name[1] == NUL)
2014 return lastbuf;
2015
2016 buf = buflist_find_by_name(name, curtab_only);
2017
2018 /* If not found, try expanding the name, like done for bufexists(). */
2019 if (buf == NULL)
2020 buf = find_buffer(tv);
2021
2022 return buf;
2023}
2024
2025/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002026 * Get the buffer from "arg" and give an error and return NULL if it is not
2027 * valid.
2028 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002029 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002030get_buf_arg(typval_T *arg)
2031{
2032 buf_T *buf;
2033
2034 ++emsg_off;
2035 buf = tv_get_buf(arg, FALSE);
2036 --emsg_off;
2037 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002038 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002039 return buf;
2040}
2041
2042/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002043 * "bufname(expr)" function
2044 */
2045 static void
2046f_bufname(typval_T *argvars, typval_T *rettv)
2047{
2048 buf_T *buf;
2049
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002050 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002051 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002052 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 rettv->v_type = VAR_STRING;
2054 if (buf != NULL && buf->b_fname != NULL)
2055 rettv->vval.v_string = vim_strsave(buf->b_fname);
2056 else
2057 rettv->vval.v_string = NULL;
2058 --emsg_off;
2059}
2060
2061/*
2062 * "bufnr(expr)" function
2063 */
2064 static void
2065f_bufnr(typval_T *argvars, typval_T *rettv)
2066{
2067 buf_T *buf;
2068 int error = FALSE;
2069 char_u *name;
2070
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002071 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002072 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002073 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 --emsg_off;
2075
2076 /* If the buffer isn't found and the second argument is not zero create a
2077 * new buffer. */
2078 if (buf == NULL
2079 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002080 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002081 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002082 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 && !error)
2084 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2085
2086 if (buf != NULL)
2087 rettv->vval.v_number = buf->b_fnum;
2088 else
2089 rettv->vval.v_number = -1;
2090}
2091
2092 static void
2093buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2094{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002095 win_T *wp;
2096 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 buf_T *buf;
2098
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002099 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002100 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002101 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002102 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002103 {
2104 ++winnr;
2105 if (wp->w_buffer == buf)
2106 break;
2107 }
2108 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002109 --emsg_off;
2110}
2111
2112/*
2113 * "bufwinid(nr)" function
2114 */
2115 static void
2116f_bufwinid(typval_T *argvars, typval_T *rettv)
2117{
2118 buf_win_common(argvars, rettv, FALSE);
2119}
2120
2121/*
2122 * "bufwinnr(nr)" function
2123 */
2124 static void
2125f_bufwinnr(typval_T *argvars, typval_T *rettv)
2126{
2127 buf_win_common(argvars, rettv, TRUE);
2128}
2129
2130/*
2131 * "byte2line(byte)" function
2132 */
2133 static void
2134f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2135{
2136#ifndef FEAT_BYTEOFF
2137 rettv->vval.v_number = -1;
2138#else
2139 long boff = 0;
2140
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002141 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002142 if (boff < 0)
2143 rettv->vval.v_number = -1;
2144 else
2145 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2146 (linenr_T)0, &boff);
2147#endif
2148}
2149
2150 static void
2151byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2152{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002153 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002154 char_u *str;
2155 varnumber_T idx;
2156
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002157 str = tv_get_string_chk(&argvars[0]);
2158 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002159 rettv->vval.v_number = -1;
2160 if (str == NULL || idx < 0)
2161 return;
2162
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002163 t = str;
2164 for ( ; idx > 0; idx--)
2165 {
2166 if (*t == NUL) /* EOL reached */
2167 return;
2168 if (enc_utf8 && comp)
2169 t += utf_ptr2len(t);
2170 else
2171 t += (*mb_ptr2len)(t);
2172 }
2173 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002174}
2175
2176/*
2177 * "byteidx()" function
2178 */
2179 static void
2180f_byteidx(typval_T *argvars, typval_T *rettv)
2181{
2182 byteidx(argvars, rettv, FALSE);
2183}
2184
2185/*
2186 * "byteidxcomp()" function
2187 */
2188 static void
2189f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2190{
2191 byteidx(argvars, rettv, TRUE);
2192}
2193
2194/*
2195 * "call(func, arglist [, dict])" function
2196 */
2197 static void
2198f_call(typval_T *argvars, typval_T *rettv)
2199{
2200 char_u *func;
2201 partial_T *partial = NULL;
2202 dict_T *selfdict = NULL;
2203
2204 if (argvars[1].v_type != VAR_LIST)
2205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002207 return;
2208 }
2209 if (argvars[1].vval.v_list == NULL)
2210 return;
2211
2212 if (argvars[0].v_type == VAR_FUNC)
2213 func = argvars[0].vval.v_string;
2214 else if (argvars[0].v_type == VAR_PARTIAL)
2215 {
2216 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002217 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002218 }
2219 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002220 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002221 if (*func == NUL)
2222 return; /* type error or empty name */
2223
2224 if (argvars[2].v_type != VAR_UNKNOWN)
2225 {
2226 if (argvars[2].v_type != VAR_DICT)
2227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002228 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002229 return;
2230 }
2231 selfdict = argvars[2].vval.v_dict;
2232 }
2233
2234 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2235}
2236
2237#ifdef FEAT_FLOAT
2238/*
2239 * "ceil({float})" function
2240 */
2241 static void
2242f_ceil(typval_T *argvars, typval_T *rettv)
2243{
2244 float_T f = 0.0;
2245
2246 rettv->v_type = VAR_FLOAT;
2247 if (get_float_arg(argvars, &f) == OK)
2248 rettv->vval.v_float = ceil(f);
2249 else
2250 rettv->vval.v_float = 0.0;
2251}
2252#endif
2253
2254#ifdef FEAT_JOB_CHANNEL
2255/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002256 * "ch_canread()" function
2257 */
2258 static void
2259f_ch_canread(typval_T *argvars, typval_T *rettv)
2260{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002261 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002262
2263 rettv->vval.v_number = 0;
2264 if (channel != NULL)
2265 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2266 || channel_has_readahead(channel, PART_OUT)
2267 || channel_has_readahead(channel, PART_ERR);
2268}
2269
2270/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002271 * "ch_close()" function
2272 */
2273 static void
2274f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2275{
2276 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2277
2278 if (channel != NULL)
2279 {
2280 channel_close(channel, FALSE);
2281 channel_clear(channel);
2282 }
2283}
2284
2285/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002286 * "ch_close()" function
2287 */
2288 static void
2289f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2290{
2291 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2292
2293 if (channel != NULL)
2294 channel_close_in(channel);
2295}
2296
2297/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002298 * "ch_getbufnr()" function
2299 */
2300 static void
2301f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2302{
2303 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2304
2305 rettv->vval.v_number = -1;
2306 if (channel != NULL)
2307 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002308 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002309 int part;
2310
2311 if (STRCMP(what, "err") == 0)
2312 part = PART_ERR;
2313 else if (STRCMP(what, "out") == 0)
2314 part = PART_OUT;
2315 else if (STRCMP(what, "in") == 0)
2316 part = PART_IN;
2317 else
2318 part = PART_SOCK;
2319 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2320 rettv->vval.v_number =
2321 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2322 }
2323}
2324
2325/*
2326 * "ch_getjob()" function
2327 */
2328 static void
2329f_ch_getjob(typval_T *argvars, typval_T *rettv)
2330{
2331 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2332
2333 if (channel != NULL)
2334 {
2335 rettv->v_type = VAR_JOB;
2336 rettv->vval.v_job = channel->ch_job;
2337 if (channel->ch_job != NULL)
2338 ++channel->ch_job->jv_refcount;
2339 }
2340}
2341
2342/*
2343 * "ch_info()" function
2344 */
2345 static void
2346f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2347{
2348 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2349
2350 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2351 channel_info(channel, rettv->vval.v_dict);
2352}
2353
2354/*
2355 * "ch_log()" function
2356 */
2357 static void
2358f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2359{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002360 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002361 channel_T *channel = NULL;
2362
2363 if (argvars[1].v_type != VAR_UNKNOWN)
2364 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2365
Bram Moolenaard5359b22018-04-05 22:44:39 +02002366 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002367}
2368
2369/*
2370 * "ch_logfile()" function
2371 */
2372 static void
2373f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2374{
2375 char_u *fname;
2376 char_u *opt = (char_u *)"";
2377 char_u buf[NUMBUFLEN];
2378
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002379 /* Don't open a file in restricted mode. */
2380 if (check_restricted() || check_secure())
2381 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002382 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002383 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002384 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 ch_logfile(fname, opt);
2386}
2387
2388/*
2389 * "ch_open()" function
2390 */
2391 static void
2392f_ch_open(typval_T *argvars, typval_T *rettv)
2393{
2394 rettv->v_type = VAR_CHANNEL;
2395 if (check_restricted() || check_secure())
2396 return;
2397 rettv->vval.v_channel = channel_open_func(argvars);
2398}
2399
2400/*
2401 * "ch_read()" function
2402 */
2403 static void
2404f_ch_read(typval_T *argvars, typval_T *rettv)
2405{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002406 common_channel_read(argvars, rettv, FALSE, FALSE);
2407}
2408
2409/*
2410 * "ch_readblob()" function
2411 */
2412 static void
2413f_ch_readblob(typval_T *argvars, typval_T *rettv)
2414{
2415 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002416}
2417
2418/*
2419 * "ch_readraw()" function
2420 */
2421 static void
2422f_ch_readraw(typval_T *argvars, typval_T *rettv)
2423{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002424 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002425}
2426
2427/*
2428 * "ch_evalexpr()" function
2429 */
2430 static void
2431f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2432{
2433 ch_expr_common(argvars, rettv, TRUE);
2434}
2435
2436/*
2437 * "ch_sendexpr()" function
2438 */
2439 static void
2440f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2441{
2442 ch_expr_common(argvars, rettv, FALSE);
2443}
2444
2445/*
2446 * "ch_evalraw()" function
2447 */
2448 static void
2449f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2450{
2451 ch_raw_common(argvars, rettv, TRUE);
2452}
2453
2454/*
2455 * "ch_sendraw()" function
2456 */
2457 static void
2458f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2459{
2460 ch_raw_common(argvars, rettv, FALSE);
2461}
2462
2463/*
2464 * "ch_setoptions()" function
2465 */
2466 static void
2467f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2468{
2469 channel_T *channel;
2470 jobopt_T opt;
2471
2472 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2473 if (channel == NULL)
2474 return;
2475 clear_job_options(&opt);
2476 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002477 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002478 channel_set_options(channel, &opt);
2479 free_job_options(&opt);
2480}
2481
2482/*
2483 * "ch_status()" function
2484 */
2485 static void
2486f_ch_status(typval_T *argvars, typval_T *rettv)
2487{
2488 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002489 jobopt_T opt;
2490 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002491
2492 /* return an empty string by default */
2493 rettv->v_type = VAR_STRING;
2494 rettv->vval.v_string = NULL;
2495
2496 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002497
2498 if (argvars[1].v_type != VAR_UNKNOWN)
2499 {
2500 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002501 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002502 && (opt.jo_set & JO_PART))
2503 part = opt.jo_part;
2504 }
2505
2506 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002507}
2508#endif
2509
2510/*
2511 * "changenr()" function
2512 */
2513 static void
2514f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2515{
2516 rettv->vval.v_number = curbuf->b_u_seq_cur;
2517}
2518
2519/*
2520 * "char2nr(string)" function
2521 */
2522 static void
2523f_char2nr(typval_T *argvars, typval_T *rettv)
2524{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002525 if (has_mbyte)
2526 {
2527 int utf8 = 0;
2528
2529 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002530 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002531
2532 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002533 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002534 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002535 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 }
2537 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002538 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002539}
2540
2541/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002542 * "chdir(dir)" function
2543 */
2544 static void
2545f_chdir(typval_T *argvars, typval_T *rettv)
2546{
2547 char_u *cwd;
2548 cdscope_T scope = CDSCOPE_GLOBAL;
2549
2550 rettv->v_type = VAR_STRING;
2551 rettv->vval.v_string = NULL;
2552
2553 if (argvars[0].v_type != VAR_STRING)
2554 return;
2555
2556 // Return the current directory
2557 cwd = alloc(MAXPATHL);
2558 if (cwd != NULL)
2559 {
2560 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2561 {
2562#ifdef BACKSLASH_IN_FILENAME
2563 slash_adjust(cwd);
2564#endif
2565 rettv->vval.v_string = vim_strsave(cwd);
2566 }
2567 vim_free(cwd);
2568 }
2569
2570 if (curwin->w_localdir != NULL)
2571 scope = CDSCOPE_WINDOW;
2572 else if (curtab->tp_localdir != NULL)
2573 scope = CDSCOPE_TABPAGE;
2574
2575 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2576 // Directory change failed
2577 VIM_CLEAR(rettv->vval.v_string);
2578}
2579
2580/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002581 * "cindent(lnum)" function
2582 */
2583 static void
2584f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2585{
2586#ifdef FEAT_CINDENT
2587 pos_T pos;
2588 linenr_T lnum;
2589
2590 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002591 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002592 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2593 {
2594 curwin->w_cursor.lnum = lnum;
2595 rettv->vval.v_number = get_c_indent();
2596 curwin->w_cursor = pos;
2597 }
2598 else
2599#endif
2600 rettv->vval.v_number = -1;
2601}
2602
Bram Moolenaaraff74912019-03-30 18:11:49 +01002603 static win_T *
2604get_optional_window(typval_T *argvars, int idx)
2605{
2606 win_T *win = curwin;
2607
2608 if (argvars[idx].v_type != VAR_UNKNOWN)
2609 {
2610 win = find_win_by_nr_or_id(&argvars[idx]);
2611 if (win == NULL)
2612 {
2613 emsg(_(e_invalwindow));
2614 return NULL;
2615 }
2616 }
2617 return win;
2618}
2619
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002620/*
2621 * "clearmatches()" function
2622 */
2623 static void
2624f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2625{
2626#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002627 win_T *win = get_optional_window(argvars, 0);
2628
2629 if (win != NULL)
2630 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002631#endif
2632}
2633
2634/*
2635 * "col(string)" function
2636 */
2637 static void
2638f_col(typval_T *argvars, typval_T *rettv)
2639{
2640 colnr_T col = 0;
2641 pos_T *fp;
2642 int fnum = curbuf->b_fnum;
2643
2644 fp = var2fpos(&argvars[0], FALSE, &fnum);
2645 if (fp != NULL && fnum == curbuf->b_fnum)
2646 {
2647 if (fp->col == MAXCOL)
2648 {
2649 /* '> can be MAXCOL, get the length of the line then */
2650 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2651 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2652 else
2653 col = MAXCOL;
2654 }
2655 else
2656 {
2657 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002658 /* col(".") when the cursor is on the NUL at the end of the line
2659 * because of "coladd" can be seen as an extra column. */
2660 if (virtual_active() && fp == &curwin->w_cursor)
2661 {
2662 char_u *p = ml_get_cursor();
2663
2664 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2665 curwin->w_virtcol - curwin->w_cursor.coladd))
2666 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002667 int l;
2668
2669 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2670 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002671 }
2672 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 }
2674 }
2675 rettv->vval.v_number = col;
2676}
2677
2678#if defined(FEAT_INS_EXPAND)
2679/*
2680 * "complete()" function
2681 */
2682 static void
2683f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2684{
2685 int startcol;
2686
2687 if ((State & INSERT) == 0)
2688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002689 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002690 return;
2691 }
2692
2693 /* Check for undo allowed here, because if something was already inserted
2694 * the line was already saved for undo and this check isn't done. */
2695 if (!undo_allowed())
2696 return;
2697
2698 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002700 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002701 return;
2702 }
2703
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002704 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002705 if (startcol <= 0)
2706 return;
2707
2708 set_completion(startcol - 1, argvars[1].vval.v_list);
2709}
2710
2711/*
2712 * "complete_add()" function
2713 */
2714 static void
2715f_complete_add(typval_T *argvars, typval_T *rettv)
2716{
2717 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2718}
2719
2720/*
2721 * "complete_check()" function
2722 */
2723 static void
2724f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2725{
2726 int saved = RedrawingDisabled;
2727
2728 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002729 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002730 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002731 RedrawingDisabled = saved;
2732}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002733
2734/*
2735 * "complete_info()" function
2736 */
2737 static void
2738f_complete_info(typval_T *argvars, typval_T *rettv)
2739{
2740 list_T *what_list = NULL;
2741
2742 if (rettv_dict_alloc(rettv) != OK)
2743 return;
2744
2745 if (argvars[0].v_type != VAR_UNKNOWN)
2746 {
2747 if (argvars[0].v_type != VAR_LIST)
2748 {
2749 emsg(_(e_listreq));
2750 return;
2751 }
2752 what_list = argvars[0].vval.v_list;
2753 }
2754 get_complete_info(what_list, rettv->vval.v_dict);
2755}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002756#endif
2757
2758/*
2759 * "confirm(message, buttons[, default [, type]])" function
2760 */
2761 static void
2762f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2763{
2764#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2765 char_u *message;
2766 char_u *buttons = NULL;
2767 char_u buf[NUMBUFLEN];
2768 char_u buf2[NUMBUFLEN];
2769 int def = 1;
2770 int type = VIM_GENERIC;
2771 char_u *typestr;
2772 int error = FALSE;
2773
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002774 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002775 if (message == NULL)
2776 error = TRUE;
2777 if (argvars[1].v_type != VAR_UNKNOWN)
2778 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002779 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002780 if (buttons == NULL)
2781 error = TRUE;
2782 if (argvars[2].v_type != VAR_UNKNOWN)
2783 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002784 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002785 if (argvars[3].v_type != VAR_UNKNOWN)
2786 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002787 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002788 if (typestr == NULL)
2789 error = TRUE;
2790 else
2791 {
2792 switch (TOUPPER_ASC(*typestr))
2793 {
2794 case 'E': type = VIM_ERROR; break;
2795 case 'Q': type = VIM_QUESTION; break;
2796 case 'I': type = VIM_INFO; break;
2797 case 'W': type = VIM_WARNING; break;
2798 case 'G': type = VIM_GENERIC; break;
2799 }
2800 }
2801 }
2802 }
2803 }
2804
2805 if (buttons == NULL || *buttons == NUL)
2806 buttons = (char_u *)_("&Ok");
2807
2808 if (!error)
2809 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2810 def, NULL, FALSE);
2811#endif
2812}
2813
2814/*
2815 * "copy()" function
2816 */
2817 static void
2818f_copy(typval_T *argvars, typval_T *rettv)
2819{
2820 item_copy(&argvars[0], rettv, FALSE, 0);
2821}
2822
2823#ifdef FEAT_FLOAT
2824/*
2825 * "cos()" function
2826 */
2827 static void
2828f_cos(typval_T *argvars, typval_T *rettv)
2829{
2830 float_T f = 0.0;
2831
2832 rettv->v_type = VAR_FLOAT;
2833 if (get_float_arg(argvars, &f) == OK)
2834 rettv->vval.v_float = cos(f);
2835 else
2836 rettv->vval.v_float = 0.0;
2837}
2838
2839/*
2840 * "cosh()" function
2841 */
2842 static void
2843f_cosh(typval_T *argvars, typval_T *rettv)
2844{
2845 float_T f = 0.0;
2846
2847 rettv->v_type = VAR_FLOAT;
2848 if (get_float_arg(argvars, &f) == OK)
2849 rettv->vval.v_float = cosh(f);
2850 else
2851 rettv->vval.v_float = 0.0;
2852}
2853#endif
2854
2855/*
2856 * "count()" function
2857 */
2858 static void
2859f_count(typval_T *argvars, typval_T *rettv)
2860{
2861 long n = 0;
2862 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002863 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002864
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002866 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002867
2868 if (argvars[0].v_type == VAR_STRING)
2869 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002870 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002871 char_u *p = argvars[0].vval.v_string;
2872 char_u *next;
2873
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002874 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002875 {
2876 if (ic)
2877 {
2878 size_t len = STRLEN(expr);
2879
2880 while (*p != NUL)
2881 {
2882 if (MB_STRNICMP(p, expr, len) == 0)
2883 {
2884 ++n;
2885 p += len;
2886 }
2887 else
2888 MB_PTR_ADV(p);
2889 }
2890 }
2891 else
2892 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2893 != NULL)
2894 {
2895 ++n;
2896 p = next + STRLEN(expr);
2897 }
2898 }
2899
2900 }
2901 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002902 {
2903 listitem_T *li;
2904 list_T *l;
2905 long idx;
2906
2907 if ((l = argvars[0].vval.v_list) != NULL)
2908 {
2909 li = l->lv_first;
2910 if (argvars[2].v_type != VAR_UNKNOWN)
2911 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002912 if (argvars[3].v_type != VAR_UNKNOWN)
2913 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002914 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002915 if (!error)
2916 {
2917 li = list_find(l, idx);
2918 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002919 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002920 }
2921 }
2922 if (error)
2923 li = NULL;
2924 }
2925
2926 for ( ; li != NULL; li = li->li_next)
2927 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2928 ++n;
2929 }
2930 }
2931 else if (argvars[0].v_type == VAR_DICT)
2932 {
2933 int todo;
2934 dict_T *d;
2935 hashitem_T *hi;
2936
2937 if ((d = argvars[0].vval.v_dict) != NULL)
2938 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002939 if (argvars[2].v_type != VAR_UNKNOWN)
2940 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002942 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 }
2944
2945 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2946 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2947 {
2948 if (!HASHITEM_EMPTY(hi))
2949 {
2950 --todo;
2951 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2952 ++n;
2953 }
2954 }
2955 }
2956 }
2957 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002958 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002959 rettv->vval.v_number = n;
2960}
2961
2962/*
2963 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2964 *
2965 * Checks the existence of a cscope connection.
2966 */
2967 static void
2968f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2969{
2970#ifdef FEAT_CSCOPE
2971 int num = 0;
2972 char_u *dbpath = NULL;
2973 char_u *prepend = NULL;
2974 char_u buf[NUMBUFLEN];
2975
2976 if (argvars[0].v_type != VAR_UNKNOWN
2977 && argvars[1].v_type != VAR_UNKNOWN)
2978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002979 num = (int)tv_get_number(&argvars[0]);
2980 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002981 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002982 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 }
2984
2985 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2986#endif
2987}
2988
2989/*
2990 * "cursor(lnum, col)" function, or
2991 * "cursor(list)"
2992 *
2993 * Moves the cursor to the specified line and column.
2994 * Returns 0 when the position could be set, -1 otherwise.
2995 */
2996 static void
2997f_cursor(typval_T *argvars, typval_T *rettv)
2998{
2999 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003000 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003001 int set_curswant = TRUE;
3002
3003 rettv->vval.v_number = -1;
3004 if (argvars[1].v_type == VAR_UNKNOWN)
3005 {
3006 pos_T pos;
3007 colnr_T curswant = -1;
3008
3009 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003011 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003012 return;
3013 }
3014 line = pos.lnum;
3015 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003016 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003017 if (curswant >= 0)
3018 {
3019 curwin->w_curswant = curswant - 1;
3020 set_curswant = FALSE;
3021 }
3022 }
3023 else
3024 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003025 line = tv_get_lnum(argvars);
3026 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003027 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003028 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003030 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 return; /* type error; errmsg already given */
3032 if (line > 0)
3033 curwin->w_cursor.lnum = line;
3034 if (col > 0)
3035 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003036 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003037
3038 /* Make sure the cursor is in a valid position. */
3039 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003040 /* Correct cursor for multi-byte character. */
3041 if (has_mbyte)
3042 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003043
3044 curwin->w_set_curswant = set_curswant;
3045 rettv->vval.v_number = 0;
3046}
3047
Bram Moolenaar4f974752019-02-17 17:44:42 +01003048#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003049/*
3050 * "debugbreak()" function
3051 */
3052 static void
3053f_debugbreak(typval_T *argvars, typval_T *rettv)
3054{
3055 int pid;
3056
3057 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003058 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003059 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003060 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003061 else
3062 {
3063 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3064
3065 if (hProcess != NULL)
3066 {
3067 DebugBreakProcess(hProcess);
3068 CloseHandle(hProcess);
3069 rettv->vval.v_number = OK;
3070 }
3071 }
3072}
3073#endif
3074
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003075/*
3076 * "deepcopy()" function
3077 */
3078 static void
3079f_deepcopy(typval_T *argvars, typval_T *rettv)
3080{
3081 int noref = 0;
3082 int copyID;
3083
3084 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003085 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003086 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003087 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 else
3089 {
3090 copyID = get_copyID();
3091 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3092 }
3093}
3094
3095/*
3096 * "delete()" function
3097 */
3098 static void
3099f_delete(typval_T *argvars, typval_T *rettv)
3100{
3101 char_u nbuf[NUMBUFLEN];
3102 char_u *name;
3103 char_u *flags;
3104
3105 rettv->vval.v_number = -1;
3106 if (check_restricted() || check_secure())
3107 return;
3108
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003109 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003110 if (name == NULL || *name == NUL)
3111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003112 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003113 return;
3114 }
3115
3116 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003117 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003118 else
3119 flags = (char_u *)"";
3120
3121 if (*flags == NUL)
3122 /* delete a file */
3123 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3124 else if (STRCMP(flags, "d") == 0)
3125 /* delete an empty directory */
3126 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3127 else if (STRCMP(flags, "rf") == 0)
3128 /* delete a directory recursively */
3129 rettv->vval.v_number = delete_recursive(name);
3130 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003131 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003132}
3133
3134/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003135 * "deletebufline()" function
3136 */
3137 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003138f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003139{
3140 buf_T *buf;
3141 linenr_T first, last;
3142 linenr_T lnum;
3143 long count;
3144 int is_curbuf;
3145 buf_T *curbuf_save = NULL;
3146 win_T *curwin_save = NULL;
3147 tabpage_T *tp;
3148 win_T *wp;
3149
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003150 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003151 if (buf == NULL)
3152 {
3153 rettv->vval.v_number = 1; /* FAIL */
3154 return;
3155 }
3156 is_curbuf = buf == curbuf;
3157
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003158 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003159 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003160 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003161 else
3162 last = first;
3163
3164 if (buf->b_ml.ml_mfp == NULL || first < 1
3165 || first > buf->b_ml.ml_line_count || last < first)
3166 {
3167 rettv->vval.v_number = 1; /* FAIL */
3168 return;
3169 }
3170
3171 if (!is_curbuf)
3172 {
3173 curbuf_save = curbuf;
3174 curwin_save = curwin;
3175 curbuf = buf;
3176 find_win_for_curbuf();
3177 }
3178 if (last > curbuf->b_ml.ml_line_count)
3179 last = curbuf->b_ml.ml_line_count;
3180 count = last - first + 1;
3181
3182 // When coming here from Insert mode, sync undo, so that this can be
3183 // undone separately from what was previously inserted.
3184 if (u_sync_once == 2)
3185 {
3186 u_sync_once = 1; // notify that u_sync() was called
3187 u_sync(TRUE);
3188 }
3189
3190 if (u_save(first - 1, last + 1) == FAIL)
3191 {
3192 rettv->vval.v_number = 1; /* FAIL */
3193 return;
3194 }
3195
3196 for (lnum = first; lnum <= last; ++lnum)
3197 ml_delete(first, TRUE);
3198
3199 FOR_ALL_TAB_WINDOWS(tp, wp)
3200 if (wp->w_buffer == buf)
3201 {
3202 if (wp->w_cursor.lnum > last)
3203 wp->w_cursor.lnum -= count;
3204 else if (wp->w_cursor.lnum> first)
3205 wp->w_cursor.lnum = first;
3206 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3207 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3208 }
3209 check_cursor_col();
3210 deleted_lines_mark(first, count);
3211
3212 if (!is_curbuf)
3213 {
3214 curbuf = curbuf_save;
3215 curwin = curwin_save;
3216 }
3217}
3218
3219/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003220 * "did_filetype()" function
3221 */
3222 static void
3223f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3224{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003225 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003226}
3227
3228/*
3229 * "diff_filler()" function
3230 */
3231 static void
3232f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3233{
3234#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003235 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003236#endif
3237}
3238
3239/*
3240 * "diff_hlID()" function
3241 */
3242 static void
3243f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3244{
3245#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003246 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003247 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003248 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 static int fnum = 0;
3250 static int change_start = 0;
3251 static int change_end = 0;
3252 static hlf_T hlID = (hlf_T)0;
3253 int filler_lines;
3254 int col;
3255
3256 if (lnum < 0) /* ignore type error in {lnum} arg */
3257 lnum = 0;
3258 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003259 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003260 || fnum != curbuf->b_fnum)
3261 {
3262 /* New line, buffer, change: need to get the values. */
3263 filler_lines = diff_check(curwin, lnum);
3264 if (filler_lines < 0)
3265 {
3266 if (filler_lines == -1)
3267 {
3268 change_start = MAXCOL;
3269 change_end = -1;
3270 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3271 hlID = HLF_ADD; /* added line */
3272 else
3273 hlID = HLF_CHD; /* changed line */
3274 }
3275 else
3276 hlID = HLF_ADD; /* added line */
3277 }
3278 else
3279 hlID = (hlf_T)0;
3280 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003281 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003282 fnum = curbuf->b_fnum;
3283 }
3284
3285 if (hlID == HLF_CHD || hlID == HLF_TXD)
3286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003287 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003288 if (col >= change_start && col <= change_end)
3289 hlID = HLF_TXD; /* changed text */
3290 else
3291 hlID = HLF_CHD; /* changed line */
3292 }
3293 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3294#endif
3295}
3296
3297/*
3298 * "empty({expr})" function
3299 */
3300 static void
3301f_empty(typval_T *argvars, typval_T *rettv)
3302{
3303 int n = FALSE;
3304
3305 switch (argvars[0].v_type)
3306 {
3307 case VAR_STRING:
3308 case VAR_FUNC:
3309 n = argvars[0].vval.v_string == NULL
3310 || *argvars[0].vval.v_string == NUL;
3311 break;
3312 case VAR_PARTIAL:
3313 n = FALSE;
3314 break;
3315 case VAR_NUMBER:
3316 n = argvars[0].vval.v_number == 0;
3317 break;
3318 case VAR_FLOAT:
3319#ifdef FEAT_FLOAT
3320 n = argvars[0].vval.v_float == 0.0;
3321 break;
3322#endif
3323 case VAR_LIST:
3324 n = argvars[0].vval.v_list == NULL
3325 || argvars[0].vval.v_list->lv_first == NULL;
3326 break;
3327 case VAR_DICT:
3328 n = argvars[0].vval.v_dict == NULL
3329 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3330 break;
3331 case VAR_SPECIAL:
3332 n = argvars[0].vval.v_number != VVAL_TRUE;
3333 break;
3334
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003335 case VAR_BLOB:
3336 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003337 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3338 break;
3339
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003340 case VAR_JOB:
3341#ifdef FEAT_JOB_CHANNEL
3342 n = argvars[0].vval.v_job == NULL
3343 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3344 break;
3345#endif
3346 case VAR_CHANNEL:
3347#ifdef FEAT_JOB_CHANNEL
3348 n = argvars[0].vval.v_channel == NULL
3349 || !channel_is_open(argvars[0].vval.v_channel);
3350 break;
3351#endif
3352 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003353 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003354 n = TRUE;
3355 break;
3356 }
3357
3358 rettv->vval.v_number = n;
3359}
3360
3361/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003362 * "environ()" function
3363 */
3364 static void
3365f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3366{
3367#if !defined(AMIGA)
3368 int i = 0;
3369 char_u *entry, *value;
3370# ifdef MSWIN
3371 extern wchar_t **_wenviron;
3372# else
3373 extern char **environ;
3374# endif
3375
3376 if (rettv_dict_alloc(rettv) != OK)
3377 return;
3378
3379# ifdef MSWIN
3380 if (*_wenviron == NULL)
3381 return;
3382# else
3383 if (*environ == NULL)
3384 return;
3385# endif
3386
3387 for (i = 0; ; ++i)
3388 {
3389# ifdef MSWIN
3390 short_u *p;
3391
3392 if ((p = (short_u *)_wenviron[i]) == NULL)
3393 return;
3394 entry = utf16_to_enc(p, NULL);
3395# else
3396 if ((entry = (char_u *)environ[i]) == NULL)
3397 return;
3398 entry = vim_strsave(entry);
3399# endif
3400 if (entry == NULL) // out of memory
3401 return;
3402 if ((value = vim_strchr(entry, '=')) == NULL)
3403 {
3404 vim_free(entry);
3405 continue;
3406 }
3407 *value++ = NUL;
3408 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3409 vim_free(entry);
3410 }
3411#endif
3412}
3413
3414/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003415 * "escape({string}, {chars})" function
3416 */
3417 static void
3418f_escape(typval_T *argvars, typval_T *rettv)
3419{
3420 char_u buf[NUMBUFLEN];
3421
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003422 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3423 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003424 rettv->v_type = VAR_STRING;
3425}
3426
3427/*
3428 * "eval()" function
3429 */
3430 static void
3431f_eval(typval_T *argvars, typval_T *rettv)
3432{
3433 char_u *s, *p;
3434
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003435 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003436 if (s != NULL)
3437 s = skipwhite(s);
3438
3439 p = s;
3440 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3441 {
3442 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003443 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003444 need_clr_eos = FALSE;
3445 rettv->v_type = VAR_NUMBER;
3446 rettv->vval.v_number = 0;
3447 }
3448 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003449 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003450}
3451
3452/*
3453 * "eventhandler()" function
3454 */
3455 static void
3456f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3457{
3458 rettv->vval.v_number = vgetc_busy;
3459}
3460
3461/*
3462 * "executable()" function
3463 */
3464 static void
3465f_executable(typval_T *argvars, typval_T *rettv)
3466{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003467 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003468
3469 /* Check in $PATH and also check directly if there is a directory name. */
3470 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3471 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3472}
3473
3474static garray_T redir_execute_ga;
3475
3476/*
3477 * Append "value[value_len]" to the execute() output.
3478 */
3479 void
3480execute_redir_str(char_u *value, int value_len)
3481{
3482 int len;
3483
3484 if (value_len == -1)
3485 len = (int)STRLEN(value); /* Append the entire string */
3486 else
3487 len = value_len; /* Append only "value_len" characters */
3488 if (ga_grow(&redir_execute_ga, len) == OK)
3489 {
3490 mch_memmove((char *)redir_execute_ga.ga_data
3491 + redir_execute_ga.ga_len, value, len);
3492 redir_execute_ga.ga_len += len;
3493 }
3494}
3495
3496/*
3497 * Get next line from a list.
3498 * Called by do_cmdline() to get the next line.
3499 * Returns allocated string, or NULL for end of function.
3500 */
3501
3502 static char_u *
3503get_list_line(
3504 int c UNUSED,
3505 void *cookie,
3506 int indent UNUSED)
3507{
3508 listitem_T **p = (listitem_T **)cookie;
3509 listitem_T *item = *p;
3510 char_u buf[NUMBUFLEN];
3511 char_u *s;
3512
3513 if (item == NULL)
3514 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003515 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003516 *p = item->li_next;
3517 return s == NULL ? NULL : vim_strsave(s);
3518}
3519
3520/*
3521 * "execute()" function
3522 */
3523 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003524execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003525{
3526 char_u *cmd = NULL;
3527 list_T *list = NULL;
3528 int save_msg_silent = msg_silent;
3529 int save_emsg_silent = emsg_silent;
3530 int save_emsg_noredir = emsg_noredir;
3531 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003532 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003533 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003534 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003535 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003536
3537 rettv->vval.v_string = NULL;
3538 rettv->v_type = VAR_STRING;
3539
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003540 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003541 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003542 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 if (list == NULL || list->lv_first == NULL)
3544 /* empty list, no commands, empty output */
3545 return;
3546 ++list->lv_refcount;
3547 }
3548 else
3549 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003550 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003551 if (cmd == NULL)
3552 return;
3553 }
3554
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003555 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003556 {
3557 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003558 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003559
3560 if (s == NULL)
3561 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003562 if (*s == NUL)
3563 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003564 if (STRNCMP(s, "silent", 6) == 0)
3565 ++msg_silent;
3566 if (STRCMP(s, "silent!") == 0)
3567 {
3568 emsg_silent = TRUE;
3569 emsg_noredir = TRUE;
3570 }
3571 }
3572 else
3573 ++msg_silent;
3574
3575 if (redir_execute)
3576 save_ga = redir_execute_ga;
3577 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3578 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003579 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003580 if (!echo_output)
3581 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003582
3583 if (cmd != NULL)
3584 do_cmdline_cmd(cmd);
3585 else
3586 {
3587 listitem_T *item = list->lv_first;
3588
3589 do_cmdline(NULL, get_list_line, (void *)&item,
3590 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3591 --list->lv_refcount;
3592 }
3593
Bram Moolenaard297f352017-01-29 20:31:21 +01003594 /* Need to append a NUL to the result. */
3595 if (ga_grow(&redir_execute_ga, 1) == OK)
3596 {
3597 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3598 rettv->vval.v_string = redir_execute_ga.ga_data;
3599 }
3600 else
3601 {
3602 ga_clear(&redir_execute_ga);
3603 rettv->vval.v_string = NULL;
3604 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003605 msg_silent = save_msg_silent;
3606 emsg_silent = save_emsg_silent;
3607 emsg_noredir = save_emsg_noredir;
3608
3609 redir_execute = save_redir_execute;
3610 if (redir_execute)
3611 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003612 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003613
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003614 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003615 if (echo_output)
3616 // When not working silently: put it in column zero. A following
3617 // "echon" will overwrite the message, unavoidably.
3618 msg_col = 0;
3619 else
3620 // When working silently: Put it back where it was, since nothing
3621 // should have been written.
3622 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003623}
3624
3625/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003626 * "execute()" function
3627 */
3628 static void
3629f_execute(typval_T *argvars, typval_T *rettv)
3630{
3631 execute_common(argvars, rettv, 0);
3632}
3633
3634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003635 * "exepath()" function
3636 */
3637 static void
3638f_exepath(typval_T *argvars, typval_T *rettv)
3639{
3640 char_u *p = NULL;
3641
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003642 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003643 rettv->v_type = VAR_STRING;
3644 rettv->vval.v_string = p;
3645}
3646
3647/*
3648 * "exists()" function
3649 */
3650 static void
3651f_exists(typval_T *argvars, typval_T *rettv)
3652{
3653 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003654 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003655
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003656 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657 if (*p == '$') /* environment variable */
3658 {
3659 /* first try "normal" environment variables (fast) */
3660 if (mch_getenv(p + 1) != NULL)
3661 n = TRUE;
3662 else
3663 {
3664 /* try expanding things like $VIM and ${HOME} */
3665 p = expand_env_save(p);
3666 if (p != NULL && *p != '$')
3667 n = TRUE;
3668 vim_free(p);
3669 }
3670 }
3671 else if (*p == '&' || *p == '+') /* option */
3672 {
3673 n = (get_option_tv(&p, NULL, TRUE) == OK);
3674 if (*skipwhite(p) != NUL)
3675 n = FALSE; /* trailing garbage */
3676 }
3677 else if (*p == '*') /* internal or user defined function */
3678 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003679 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003680 }
3681 else if (*p == ':')
3682 {
3683 n = cmd_exists(p + 1);
3684 }
3685 else if (*p == '#')
3686 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003687 if (p[1] == '#')
3688 n = autocmd_supported(p + 2);
3689 else
3690 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003691 }
3692 else /* internal variable */
3693 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003694 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003695 }
3696
3697 rettv->vval.v_number = n;
3698}
3699
3700#ifdef FEAT_FLOAT
3701/*
3702 * "exp()" function
3703 */
3704 static void
3705f_exp(typval_T *argvars, typval_T *rettv)
3706{
3707 float_T f = 0.0;
3708
3709 rettv->v_type = VAR_FLOAT;
3710 if (get_float_arg(argvars, &f) == OK)
3711 rettv->vval.v_float = exp(f);
3712 else
3713 rettv->vval.v_float = 0.0;
3714}
3715#endif
3716
3717/*
3718 * "expand()" function
3719 */
3720 static void
3721f_expand(typval_T *argvars, typval_T *rettv)
3722{
3723 char_u *s;
3724 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003725 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003726 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3727 expand_T xpc;
3728 int error = FALSE;
3729 char_u *result;
3730
3731 rettv->v_type = VAR_STRING;
3732 if (argvars[1].v_type != VAR_UNKNOWN
3733 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003734 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003735 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003736 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003738 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739 if (*s == '%' || *s == '#' || *s == '<')
3740 {
3741 ++emsg_off;
3742 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3743 --emsg_off;
3744 if (rettv->v_type == VAR_LIST)
3745 {
3746 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3747 list_append_string(rettv->vval.v_list, result, -1);
3748 else
3749 vim_free(result);
3750 }
3751 else
3752 rettv->vval.v_string = result;
3753 }
3754 else
3755 {
3756 /* When the optional second argument is non-zero, don't remove matches
3757 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3758 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003759 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003760 options |= WILD_KEEP_ALL;
3761 if (!error)
3762 {
3763 ExpandInit(&xpc);
3764 xpc.xp_context = EXPAND_FILES;
3765 if (p_wic)
3766 options += WILD_ICASE;
3767 if (rettv->v_type == VAR_STRING)
3768 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3769 options, WILD_ALL);
3770 else if (rettv_list_alloc(rettv) != FAIL)
3771 {
3772 int i;
3773
3774 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3775 for (i = 0; i < xpc.xp_numfiles; i++)
3776 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3777 ExpandCleanup(&xpc);
3778 }
3779 }
3780 else
3781 rettv->vval.v_string = NULL;
3782 }
3783}
3784
3785/*
3786 * "extend(list, list [, idx])" function
3787 * "extend(dict, dict [, action])" function
3788 */
3789 static void
3790f_extend(typval_T *argvars, typval_T *rettv)
3791{
3792 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3793
3794 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3795 {
3796 list_T *l1, *l2;
3797 listitem_T *item;
3798 long before;
3799 int error = FALSE;
3800
3801 l1 = argvars[0].vval.v_list;
3802 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003803 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003804 && l2 != NULL)
3805 {
3806 if (argvars[2].v_type != VAR_UNKNOWN)
3807 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003808 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003809 if (error)
3810 return; /* type error; errmsg already given */
3811
3812 if (before == l1->lv_len)
3813 item = NULL;
3814 else
3815 {
3816 item = list_find(l1, before);
3817 if (item == NULL)
3818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003819 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003820 return;
3821 }
3822 }
3823 }
3824 else
3825 item = NULL;
3826 list_extend(l1, l2, item);
3827
3828 copy_tv(&argvars[0], rettv);
3829 }
3830 }
3831 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3832 {
3833 dict_T *d1, *d2;
3834 char_u *action;
3835 int i;
3836
3837 d1 = argvars[0].vval.v_dict;
3838 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003839 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003840 && d2 != NULL)
3841 {
3842 /* Check the third argument. */
3843 if (argvars[2].v_type != VAR_UNKNOWN)
3844 {
3845 static char *(av[]) = {"keep", "force", "error"};
3846
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003847 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003848 if (action == NULL)
3849 return; /* type error; errmsg already given */
3850 for (i = 0; i < 3; ++i)
3851 if (STRCMP(action, av[i]) == 0)
3852 break;
3853 if (i == 3)
3854 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003855 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003856 return;
3857 }
3858 }
3859 else
3860 action = (char_u *)"force";
3861
3862 dict_extend(d1, d2, action);
3863
3864 copy_tv(&argvars[0], rettv);
3865 }
3866 }
3867 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003869}
3870
3871/*
3872 * "feedkeys()" function
3873 */
3874 static void
3875f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3876{
3877 int remap = TRUE;
3878 int insert = FALSE;
3879 char_u *keys, *flags;
3880 char_u nbuf[NUMBUFLEN];
3881 int typed = FALSE;
3882 int execute = FALSE;
3883 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003884 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003885 char_u *keys_esc;
3886
3887 /* This is not allowed in the sandbox. If the commands would still be
3888 * executed in the sandbox it would be OK, but it probably happens later,
3889 * when "sandbox" is no longer set. */
3890 if (check_secure())
3891 return;
3892
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003893 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003894
3895 if (argvars[1].v_type != VAR_UNKNOWN)
3896 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003897 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003898 for ( ; *flags != NUL; ++flags)
3899 {
3900 switch (*flags)
3901 {
3902 case 'n': remap = FALSE; break;
3903 case 'm': remap = TRUE; break;
3904 case 't': typed = TRUE; break;
3905 case 'i': insert = TRUE; break;
3906 case 'x': execute = TRUE; break;
3907 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003908 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003909 }
3910 }
3911 }
3912
3913 if (*keys != NUL || execute)
3914 {
3915 /* Need to escape K_SPECIAL and CSI before putting the string in the
3916 * typeahead buffer. */
3917 keys_esc = vim_strsave_escape_csi(keys);
3918 if (keys_esc != NULL)
3919 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003920 if (lowlevel)
3921 {
3922#ifdef USE_INPUT_BUF
3923 add_to_input_buf(keys, (int)STRLEN(keys));
3924#else
3925 emsg(_("E980: lowlevel input not supported"));
3926#endif
3927 }
3928 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003929 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003930 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003931 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003932 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003933#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003934 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003935#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003936 )
3937 typebuf_was_filled = TRUE;
3938 }
3939 vim_free(keys_esc);
3940
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003941 if (execute)
3942 {
3943 int save_msg_scroll = msg_scroll;
3944
3945 /* Avoid a 1 second delay when the keys start Insert mode. */
3946 msg_scroll = FALSE;
3947
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003948 if (!dangerous)
3949 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003950 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003951 if (!dangerous)
3952 --ex_normal_busy;
3953
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003954 msg_scroll |= save_msg_scroll;
3955 }
3956 }
3957 }
3958}
3959
3960/*
3961 * "filereadable()" function
3962 */
3963 static void
3964f_filereadable(typval_T *argvars, typval_T *rettv)
3965{
3966 int fd;
3967 char_u *p;
3968 int n;
3969
3970#ifndef O_NONBLOCK
3971# define O_NONBLOCK 0
3972#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003973 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003974 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3975 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3976 {
3977 n = TRUE;
3978 close(fd);
3979 }
3980 else
3981 n = FALSE;
3982
3983 rettv->vval.v_number = n;
3984}
3985
3986/*
3987 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3988 * rights to write into.
3989 */
3990 static void
3991f_filewritable(typval_T *argvars, typval_T *rettv)
3992{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003993 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003994}
3995
3996 static void
3997findfilendir(
3998 typval_T *argvars UNUSED,
3999 typval_T *rettv,
4000 int find_what UNUSED)
4001{
4002#ifdef FEAT_SEARCHPATH
4003 char_u *fname;
4004 char_u *fresult = NULL;
4005 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4006 char_u *p;
4007 char_u pathbuf[NUMBUFLEN];
4008 int count = 1;
4009 int first = TRUE;
4010 int error = FALSE;
4011#endif
4012
4013 rettv->vval.v_string = NULL;
4014 rettv->v_type = VAR_STRING;
4015
4016#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004017 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004018
4019 if (argvars[1].v_type != VAR_UNKNOWN)
4020 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004021 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004022 if (p == NULL)
4023 error = TRUE;
4024 else
4025 {
4026 if (*p != NUL)
4027 path = p;
4028
4029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004030 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004031 }
4032 }
4033
4034 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4035 error = TRUE;
4036
4037 if (*fname != NUL && !error)
4038 {
4039 do
4040 {
4041 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4042 vim_free(fresult);
4043 fresult = find_file_in_path_option(first ? fname : NULL,
4044 first ? (int)STRLEN(fname) : 0,
4045 0, first, path,
4046 find_what,
4047 curbuf->b_ffname,
4048 find_what == FINDFILE_DIR
4049 ? (char_u *)"" : curbuf->b_p_sua);
4050 first = FALSE;
4051
4052 if (fresult != NULL && rettv->v_type == VAR_LIST)
4053 list_append_string(rettv->vval.v_list, fresult, -1);
4054
4055 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4056 }
4057
4058 if (rettv->v_type == VAR_STRING)
4059 rettv->vval.v_string = fresult;
4060#endif
4061}
4062
4063/*
4064 * "filter()" function
4065 */
4066 static void
4067f_filter(typval_T *argvars, typval_T *rettv)
4068{
4069 filter_map(argvars, rettv, FALSE);
4070}
4071
4072/*
4073 * "finddir({fname}[, {path}[, {count}]])" function
4074 */
4075 static void
4076f_finddir(typval_T *argvars, typval_T *rettv)
4077{
4078 findfilendir(argvars, rettv, FINDFILE_DIR);
4079}
4080
4081/*
4082 * "findfile({fname}[, {path}[, {count}]])" function
4083 */
4084 static void
4085f_findfile(typval_T *argvars, typval_T *rettv)
4086{
4087 findfilendir(argvars, rettv, FINDFILE_FILE);
4088}
4089
4090#ifdef FEAT_FLOAT
4091/*
4092 * "float2nr({float})" function
4093 */
4094 static void
4095f_float2nr(typval_T *argvars, typval_T *rettv)
4096{
4097 float_T f = 0.0;
4098
4099 if (get_float_arg(argvars, &f) == OK)
4100 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004101 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004102 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004103 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004104 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004105 else
4106 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 }
4108}
4109
4110/*
4111 * "floor({float})" function
4112 */
4113 static void
4114f_floor(typval_T *argvars, typval_T *rettv)
4115{
4116 float_T f = 0.0;
4117
4118 rettv->v_type = VAR_FLOAT;
4119 if (get_float_arg(argvars, &f) == OK)
4120 rettv->vval.v_float = floor(f);
4121 else
4122 rettv->vval.v_float = 0.0;
4123}
4124
4125/*
4126 * "fmod()" function
4127 */
4128 static void
4129f_fmod(typval_T *argvars, typval_T *rettv)
4130{
4131 float_T fx = 0.0, fy = 0.0;
4132
4133 rettv->v_type = VAR_FLOAT;
4134 if (get_float_arg(argvars, &fx) == OK
4135 && get_float_arg(&argvars[1], &fy) == OK)
4136 rettv->vval.v_float = fmod(fx, fy);
4137 else
4138 rettv->vval.v_float = 0.0;
4139}
4140#endif
4141
4142/*
4143 * "fnameescape({string})" function
4144 */
4145 static void
4146f_fnameescape(typval_T *argvars, typval_T *rettv)
4147{
4148 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004149 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004150 rettv->v_type = VAR_STRING;
4151}
4152
4153/*
4154 * "fnamemodify({fname}, {mods})" function
4155 */
4156 static void
4157f_fnamemodify(typval_T *argvars, typval_T *rettv)
4158{
4159 char_u *fname;
4160 char_u *mods;
4161 int usedlen = 0;
4162 int len;
4163 char_u *fbuf = NULL;
4164 char_u buf[NUMBUFLEN];
4165
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004166 fname = tv_get_string_chk(&argvars[0]);
4167 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004168 if (fname == NULL || mods == NULL)
4169 fname = NULL;
4170 else
4171 {
4172 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004173 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004174 }
4175
4176 rettv->v_type = VAR_STRING;
4177 if (fname == NULL)
4178 rettv->vval.v_string = NULL;
4179 else
4180 rettv->vval.v_string = vim_strnsave(fname, len);
4181 vim_free(fbuf);
4182}
4183
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004184/*
4185 * "foldclosed()" function
4186 */
4187 static void
4188foldclosed_both(
4189 typval_T *argvars UNUSED,
4190 typval_T *rettv,
4191 int end UNUSED)
4192{
4193#ifdef FEAT_FOLDING
4194 linenr_T lnum;
4195 linenr_T first, last;
4196
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004197 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004198 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4199 {
4200 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4201 {
4202 if (end)
4203 rettv->vval.v_number = (varnumber_T)last;
4204 else
4205 rettv->vval.v_number = (varnumber_T)first;
4206 return;
4207 }
4208 }
4209#endif
4210 rettv->vval.v_number = -1;
4211}
4212
4213/*
4214 * "foldclosed()" function
4215 */
4216 static void
4217f_foldclosed(typval_T *argvars, typval_T *rettv)
4218{
4219 foldclosed_both(argvars, rettv, FALSE);
4220}
4221
4222/*
4223 * "foldclosedend()" function
4224 */
4225 static void
4226f_foldclosedend(typval_T *argvars, typval_T *rettv)
4227{
4228 foldclosed_both(argvars, rettv, TRUE);
4229}
4230
4231/*
4232 * "foldlevel()" function
4233 */
4234 static void
4235f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4236{
4237#ifdef FEAT_FOLDING
4238 linenr_T lnum;
4239
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004240 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004241 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4242 rettv->vval.v_number = foldLevel(lnum);
4243#endif
4244}
4245
4246/*
4247 * "foldtext()" function
4248 */
4249 static void
4250f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4251{
4252#ifdef FEAT_FOLDING
4253 linenr_T foldstart;
4254 linenr_T foldend;
4255 char_u *dashes;
4256 linenr_T lnum;
4257 char_u *s;
4258 char_u *r;
4259 int len;
4260 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004261 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004262#endif
4263
4264 rettv->v_type = VAR_STRING;
4265 rettv->vval.v_string = NULL;
4266#ifdef FEAT_FOLDING
4267 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4268 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4269 dashes = get_vim_var_str(VV_FOLDDASHES);
4270 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4271 && dashes != NULL)
4272 {
4273 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004274 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004275 if (!linewhite(lnum))
4276 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277
4278 /* Find interesting text in this line. */
4279 s = skipwhite(ml_get(lnum));
4280 /* skip C comment-start */
4281 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4282 {
4283 s = skipwhite(s + 2);
4284 if (*skipwhite(s) == NUL
4285 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4286 {
4287 s = skipwhite(ml_get(lnum + 1));
4288 if (*s == '*')
4289 s = skipwhite(s + 1);
4290 }
4291 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004292 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004293 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004294 r = alloc(STRLEN(txt)
4295 + STRLEN(dashes) // for %s
4296 + 20 // for %3ld
4297 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004298 if (r != NULL)
4299 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004300 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004301 len = (int)STRLEN(r);
4302 STRCAT(r, s);
4303 /* remove 'foldmarker' and 'commentstring' */
4304 foldtext_cleanup(r + len);
4305 rettv->vval.v_string = r;
4306 }
4307 }
4308#endif
4309}
4310
4311/*
4312 * "foldtextresult(lnum)" function
4313 */
4314 static void
4315f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4316{
4317#ifdef FEAT_FOLDING
4318 linenr_T lnum;
4319 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004320 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004321 foldinfo_T foldinfo;
4322 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004323 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004324#endif
4325
4326 rettv->v_type = VAR_STRING;
4327 rettv->vval.v_string = NULL;
4328#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004329 if (entered)
4330 return; /* reject recursive use */
4331 entered = TRUE;
4332
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004333 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004334 /* treat illegal types and illegal string values for {lnum} the same */
4335 if (lnum < 0)
4336 lnum = 0;
4337 fold_count = foldedCount(curwin, lnum, &foldinfo);
4338 if (fold_count > 0)
4339 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004340 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4341 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004342 if (text == buf)
4343 text = vim_strsave(text);
4344 rettv->vval.v_string = text;
4345 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004346
4347 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004348#endif
4349}
4350
4351/*
4352 * "foreground()" function
4353 */
4354 static void
4355f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4356{
4357#ifdef FEAT_GUI
4358 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004359 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004360 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004361 return;
4362 }
4363#endif
4364#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004365 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004366#endif
4367}
4368
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004369 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004370common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371{
4372 char_u *s;
4373 char_u *name;
4374 int use_string = FALSE;
4375 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004376 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004377
4378 if (argvars[0].v_type == VAR_FUNC)
4379 {
4380 /* function(MyFunc, [arg], dict) */
4381 s = argvars[0].vval.v_string;
4382 }
4383 else if (argvars[0].v_type == VAR_PARTIAL
4384 && argvars[0].vval.v_partial != NULL)
4385 {
4386 /* function(dict.MyFunc, [arg]) */
4387 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004388 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004389 }
4390 else
4391 {
4392 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004393 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004394 use_string = TRUE;
4395 }
4396
Bram Moolenaar843b8842016-08-21 14:36:15 +02004397 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004398 {
4399 name = s;
4400 trans_name = trans_function_name(&name, FALSE,
4401 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4402 if (*name != NUL)
4403 s = NULL;
4404 }
4405
Bram Moolenaar843b8842016-08-21 14:36:15 +02004406 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4407 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004408 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004409 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004410 else if (trans_name != NULL && (is_funcref
4411 ? find_func(trans_name) == NULL
4412 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004413 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004414 else
4415 {
4416 int dict_idx = 0;
4417 int arg_idx = 0;
4418 list_T *list = NULL;
4419
4420 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4421 {
4422 char sid_buf[25];
4423 int off = *s == 's' ? 2 : 5;
4424
4425 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4426 * also be called from another script. Using trans_function_name()
4427 * would also work, but some plugins depend on the name being
4428 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004429 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004430 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004431 if (name != NULL)
4432 {
4433 STRCPY(name, sid_buf);
4434 STRCAT(name, s + off);
4435 }
4436 }
4437 else
4438 name = vim_strsave(s);
4439
4440 if (argvars[1].v_type != VAR_UNKNOWN)
4441 {
4442 if (argvars[2].v_type != VAR_UNKNOWN)
4443 {
4444 /* function(name, [args], dict) */
4445 arg_idx = 1;
4446 dict_idx = 2;
4447 }
4448 else if (argvars[1].v_type == VAR_DICT)
4449 /* function(name, dict) */
4450 dict_idx = 1;
4451 else
4452 /* function(name, [args]) */
4453 arg_idx = 1;
4454 if (dict_idx > 0)
4455 {
4456 if (argvars[dict_idx].v_type != VAR_DICT)
4457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004458 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004459 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004460 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 }
4462 if (argvars[dict_idx].vval.v_dict == NULL)
4463 dict_idx = 0;
4464 }
4465 if (arg_idx > 0)
4466 {
4467 if (argvars[arg_idx].v_type != VAR_LIST)
4468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004469 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004470 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004471 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472 }
4473 list = argvars[arg_idx].vval.v_list;
4474 if (list == NULL || list->lv_len == 0)
4475 arg_idx = 0;
4476 }
4477 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004478 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004479 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004480 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004481
4482 /* result is a VAR_PARTIAL */
4483 if (pt == NULL)
4484 vim_free(name);
4485 else
4486 {
4487 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4488 {
4489 listitem_T *li;
4490 int i = 0;
4491 int arg_len = 0;
4492 int lv_len = 0;
4493
4494 if (arg_pt != NULL)
4495 arg_len = arg_pt->pt_argc;
4496 if (list != NULL)
4497 lv_len = list->lv_len;
4498 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004499 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004500 if (pt->pt_argv == NULL)
4501 {
4502 vim_free(pt);
4503 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004504 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004505 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004506 for (i = 0; i < arg_len; i++)
4507 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4508 if (lv_len > 0)
4509 for (li = list->lv_first; li != NULL;
4510 li = li->li_next)
4511 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004512 }
4513
4514 /* For "function(dict.func, [], dict)" and "func" is a partial
4515 * use "dict". That is backwards compatible. */
4516 if (dict_idx > 0)
4517 {
4518 /* The dict is bound explicitly, pt_auto is FALSE. */
4519 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4520 ++pt->pt_dict->dv_refcount;
4521 }
4522 else if (arg_pt != NULL)
4523 {
4524 /* If the dict was bound automatically the result is also
4525 * bound automatically. */
4526 pt->pt_dict = arg_pt->pt_dict;
4527 pt->pt_auto = arg_pt->pt_auto;
4528 if (pt->pt_dict != NULL)
4529 ++pt->pt_dict->dv_refcount;
4530 }
4531
4532 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004533 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4534 {
4535 pt->pt_func = arg_pt->pt_func;
4536 func_ptr_ref(pt->pt_func);
4537 vim_free(name);
4538 }
4539 else if (is_funcref)
4540 {
4541 pt->pt_func = find_func(trans_name);
4542 func_ptr_ref(pt->pt_func);
4543 vim_free(name);
4544 }
4545 else
4546 {
4547 pt->pt_name = name;
4548 func_ref(name);
4549 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004550 }
4551 rettv->v_type = VAR_PARTIAL;
4552 rettv->vval.v_partial = pt;
4553 }
4554 else
4555 {
4556 /* result is a VAR_FUNC */
4557 rettv->v_type = VAR_FUNC;
4558 rettv->vval.v_string = name;
4559 func_ref(name);
4560 }
4561 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004562theend:
4563 vim_free(trans_name);
4564}
4565
4566/*
4567 * "funcref()" function
4568 */
4569 static void
4570f_funcref(typval_T *argvars, typval_T *rettv)
4571{
4572 common_function(argvars, rettv, TRUE);
4573}
4574
4575/*
4576 * "function()" function
4577 */
4578 static void
4579f_function(typval_T *argvars, typval_T *rettv)
4580{
4581 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004582}
4583
4584/*
4585 * "garbagecollect()" function
4586 */
4587 static void
4588f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4589{
4590 /* This is postponed until we are back at the toplevel, because we may be
4591 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4592 want_garbage_collect = TRUE;
4593
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004594 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004595 garbage_collect_at_exit = TRUE;
4596}
4597
4598/*
4599 * "get()" function
4600 */
4601 static void
4602f_get(typval_T *argvars, typval_T *rettv)
4603{
4604 listitem_T *li;
4605 list_T *l;
4606 dictitem_T *di;
4607 dict_T *d;
4608 typval_T *tv = NULL;
4609
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004610 if (argvars[0].v_type == VAR_BLOB)
4611 {
4612 int error = FALSE;
4613 int idx = tv_get_number_chk(&argvars[1], &error);
4614
4615 if (!error)
4616 {
4617 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004618 if (idx < 0)
4619 idx = blob_len(argvars[0].vval.v_blob) + idx;
4620 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4621 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004622 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004623 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004624 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004625 tv = rettv;
4626 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004627 }
4628 }
4629 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004630 {
4631 if ((l = argvars[0].vval.v_list) != NULL)
4632 {
4633 int error = FALSE;
4634
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004635 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004636 if (!error && li != NULL)
4637 tv = &li->li_tv;
4638 }
4639 }
4640 else if (argvars[0].v_type == VAR_DICT)
4641 {
4642 if ((d = argvars[0].vval.v_dict) != NULL)
4643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004644 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004645 if (di != NULL)
4646 tv = &di->di_tv;
4647 }
4648 }
4649 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4650 {
4651 partial_T *pt;
4652 partial_T fref_pt;
4653
4654 if (argvars[0].v_type == VAR_PARTIAL)
4655 pt = argvars[0].vval.v_partial;
4656 else
4657 {
4658 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4659 fref_pt.pt_name = argvars[0].vval.v_string;
4660 pt = &fref_pt;
4661 }
4662
4663 if (pt != NULL)
4664 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004665 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004666 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004667
4668 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4669 {
4670 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004671 n = partial_name(pt);
4672 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004673 rettv->vval.v_string = NULL;
4674 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004675 {
4676 rettv->vval.v_string = vim_strsave(n);
4677 if (rettv->v_type == VAR_FUNC)
4678 func_ref(rettv->vval.v_string);
4679 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004680 }
4681 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004682 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004683 else if (STRCMP(what, "args") == 0)
4684 {
4685 rettv->v_type = VAR_LIST;
4686 if (rettv_list_alloc(rettv) == OK)
4687 {
4688 int i;
4689
4690 for (i = 0; i < pt->pt_argc; ++i)
4691 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4692 }
4693 }
4694 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004695 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004696 return;
4697 }
4698 }
4699 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004700 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004701
4702 if (tv == NULL)
4703 {
4704 if (argvars[2].v_type != VAR_UNKNOWN)
4705 copy_tv(&argvars[2], rettv);
4706 }
4707 else
4708 copy_tv(tv, rettv);
4709}
4710
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004711/*
4712 * Returns buffer options, variables and other attributes in a dictionary.
4713 */
4714 static dict_T *
4715get_buffer_info(buf_T *buf)
4716{
4717 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004718 tabpage_T *tp;
4719 win_T *wp;
4720 list_T *windows;
4721
4722 dict = dict_alloc();
4723 if (dict == NULL)
4724 return NULL;
4725
Bram Moolenaare0be1672018-07-08 16:50:37 +02004726 dict_add_number(dict, "bufnr", buf->b_fnum);
4727 dict_add_string(dict, "name", buf->b_ffname);
4728 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4729 : buflist_findlnum(buf));
4730 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4731 dict_add_number(dict, "listed", buf->b_p_bl);
4732 dict_add_number(dict, "changed", bufIsChanged(buf));
4733 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4734 dict_add_number(dict, "hidden",
4735 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004736
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004737 /* Get a reference to buffer variables */
4738 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004739
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004740 /* List of windows displaying this buffer */
4741 windows = list_alloc();
4742 if (windows != NULL)
4743 {
4744 FOR_ALL_TAB_WINDOWS(tp, wp)
4745 if (wp->w_buffer == buf)
4746 list_append_number(windows, (varnumber_T)wp->w_id);
4747 dict_add_list(dict, "windows", windows);
4748 }
4749
4750#ifdef FEAT_SIGNS
4751 if (buf->b_signlist != NULL)
4752 {
4753 /* List of signs placed in this buffer */
4754 list_T *signs = list_alloc();
4755 if (signs != NULL)
4756 {
4757 get_buffer_signs(buf, signs);
4758 dict_add_list(dict, "signs", signs);
4759 }
4760 }
4761#endif
4762
4763 return dict;
4764}
4765
4766/*
4767 * "getbufinfo()" function
4768 */
4769 static void
4770f_getbufinfo(typval_T *argvars, typval_T *rettv)
4771{
4772 buf_T *buf = NULL;
4773 buf_T *argbuf = NULL;
4774 dict_T *d;
4775 int filtered = FALSE;
4776 int sel_buflisted = FALSE;
4777 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004778 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004779
4780 if (rettv_list_alloc(rettv) != OK)
4781 return;
4782
4783 /* List of all the buffers or selected buffers */
4784 if (argvars[0].v_type == VAR_DICT)
4785 {
4786 dict_T *sel_d = argvars[0].vval.v_dict;
4787
4788 if (sel_d != NULL)
4789 {
4790 dictitem_T *di;
4791
4792 filtered = TRUE;
4793
4794 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004795 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004796 sel_buflisted = TRUE;
4797
4798 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004799 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004800 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004801
4802 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004803 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004804 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004805 }
4806 }
4807 else if (argvars[0].v_type != VAR_UNKNOWN)
4808 {
4809 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004810 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004811 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004812 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004813 --emsg_off;
4814 if (argbuf == NULL)
4815 return;
4816 }
4817
4818 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004819 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004820 {
4821 if (argbuf != NULL && argbuf != buf)
4822 continue;
4823 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004824 || (sel_buflisted && !buf->b_p_bl)
4825 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004826 continue;
4827
4828 d = get_buffer_info(buf);
4829 if (d != NULL)
4830 list_append_dict(rettv->vval.v_list, d);
4831 if (argbuf != NULL)
4832 return;
4833 }
4834}
4835
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004836/*
4837 * Get line or list of lines from buffer "buf" into "rettv".
4838 * Return a range (from start to end) of lines in rettv from the specified
4839 * buffer.
4840 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4841 */
4842 static void
4843get_buffer_lines(
4844 buf_T *buf,
4845 linenr_T start,
4846 linenr_T end,
4847 int retlist,
4848 typval_T *rettv)
4849{
4850 char_u *p;
4851
4852 rettv->v_type = VAR_STRING;
4853 rettv->vval.v_string = NULL;
4854 if (retlist && rettv_list_alloc(rettv) == FAIL)
4855 return;
4856
4857 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4858 return;
4859
4860 if (!retlist)
4861 {
4862 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4863 p = ml_get_buf(buf, start, FALSE);
4864 else
4865 p = (char_u *)"";
4866 rettv->vval.v_string = vim_strsave(p);
4867 }
4868 else
4869 {
4870 if (end < start)
4871 return;
4872
4873 if (start < 1)
4874 start = 1;
4875 if (end > buf->b_ml.ml_line_count)
4876 end = buf->b_ml.ml_line_count;
4877 while (start <= end)
4878 if (list_append_string(rettv->vval.v_list,
4879 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4880 break;
4881 }
4882}
4883
4884/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004885 * "getbufline()" function
4886 */
4887 static void
4888f_getbufline(typval_T *argvars, typval_T *rettv)
4889{
4890 linenr_T lnum;
4891 linenr_T end;
4892 buf_T *buf;
4893
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004894 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004895 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004896 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004897 --emsg_off;
4898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004899 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004900 if (argvars[2].v_type == VAR_UNKNOWN)
4901 end = lnum;
4902 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004903 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004904
4905 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4906}
4907
4908/*
4909 * "getbufvar()" function
4910 */
4911 static void
4912f_getbufvar(typval_T *argvars, typval_T *rettv)
4913{
4914 buf_T *buf;
4915 buf_T *save_curbuf;
4916 char_u *varname;
4917 dictitem_T *v;
4918 int done = FALSE;
4919
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004920 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4921 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004922 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004923 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924
4925 rettv->v_type = VAR_STRING;
4926 rettv->vval.v_string = NULL;
4927
4928 if (buf != NULL && varname != NULL)
4929 {
4930 /* set curbuf to be our buf, temporarily */
4931 save_curbuf = curbuf;
4932 curbuf = buf;
4933
Bram Moolenaar30567352016-08-27 21:25:44 +02004934 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004935 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004936 if (varname[1] == NUL)
4937 {
4938 /* get all buffer-local options in a dict */
4939 dict_T *opts = get_winbuf_options(TRUE);
4940
4941 if (opts != NULL)
4942 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004943 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004944 done = TRUE;
4945 }
4946 }
4947 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4948 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004949 done = TRUE;
4950 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 else
4952 {
4953 /* Look up the variable. */
4954 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4955 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4956 'b', varname, FALSE);
4957 if (v != NULL)
4958 {
4959 copy_tv(&v->di_tv, rettv);
4960 done = TRUE;
4961 }
4962 }
4963
4964 /* restore previous notion of curbuf */
4965 curbuf = save_curbuf;
4966 }
4967
4968 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4969 /* use the default value */
4970 copy_tv(&argvars[2], rettv);
4971
4972 --emsg_off;
4973}
4974
4975/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004976 * "getchangelist()" function
4977 */
4978 static void
4979f_getchangelist(typval_T *argvars, typval_T *rettv)
4980{
4981#ifdef FEAT_JUMPLIST
4982 buf_T *buf;
4983 int i;
4984 list_T *l;
4985 dict_T *d;
4986#endif
4987
4988 if (rettv_list_alloc(rettv) != OK)
4989 return;
4990
4991#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004992 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004993 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004994 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004995 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004996 if (buf == NULL)
4997 return;
4998
4999 l = list_alloc();
5000 if (l == NULL)
5001 return;
5002
5003 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5004 return;
5005 /*
5006 * The current window change list index tracks only the position in the
5007 * current buffer change list. For other buffers, use the change list
5008 * length as the current index.
5009 */
5010 list_append_number(rettv->vval.v_list,
5011 (varnumber_T)((buf == curwin->w_buffer)
5012 ? curwin->w_changelistidx : buf->b_changelistlen));
5013
5014 for (i = 0; i < buf->b_changelistlen; ++i)
5015 {
5016 if (buf->b_changelist[i].lnum == 0)
5017 continue;
5018 if ((d = dict_alloc()) == NULL)
5019 return;
5020 if (list_append_dict(l, d) == FAIL)
5021 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005022 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5023 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005024 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005025 }
5026#endif
5027}
5028/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005029 * "getchar()" function
5030 */
5031 static void
5032f_getchar(typval_T *argvars, typval_T *rettv)
5033{
5034 varnumber_T n;
5035 int error = FALSE;
5036
Bram Moolenaar84d93902018-09-11 20:10:20 +02005037#ifdef MESSAGE_QUEUE
5038 // vpeekc() used to check for messages, but that caused problems, invoking
5039 // a callback where it was not expected. Some plugins use getchar(1) in a
5040 // loop to await a message, therefore make sure we check for messages here.
5041 parse_queued_messages();
5042#endif
5043
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005044 /* Position the cursor. Needed after a message that ends in a space. */
5045 windgoto(msg_row, msg_col);
5046
5047 ++no_mapping;
5048 ++allow_keys;
5049 for (;;)
5050 {
5051 if (argvars[0].v_type == VAR_UNKNOWN)
5052 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005053 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005054 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005055 /* getchar(1): only check if char avail */
5056 n = vpeekc_any();
5057 else if (error || vpeekc_any() == NUL)
5058 /* illegal argument or getchar(0) and no char avail: return zero */
5059 n = 0;
5060 else
5061 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005062 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005063
5064 if (n == K_IGNORE)
5065 continue;
5066 break;
5067 }
5068 --no_mapping;
5069 --allow_keys;
5070
5071 set_vim_var_nr(VV_MOUSE_WIN, 0);
5072 set_vim_var_nr(VV_MOUSE_WINID, 0);
5073 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5074 set_vim_var_nr(VV_MOUSE_COL, 0);
5075
5076 rettv->vval.v_number = n;
5077 if (IS_SPECIAL(n) || mod_mask != 0)
5078 {
5079 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5080 int i = 0;
5081
5082 /* Turn a special key into three bytes, plus modifier. */
5083 if (mod_mask != 0)
5084 {
5085 temp[i++] = K_SPECIAL;
5086 temp[i++] = KS_MODIFIER;
5087 temp[i++] = mod_mask;
5088 }
5089 if (IS_SPECIAL(n))
5090 {
5091 temp[i++] = K_SPECIAL;
5092 temp[i++] = K_SECOND(n);
5093 temp[i++] = K_THIRD(n);
5094 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005095 else if (has_mbyte)
5096 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 else
5098 temp[i++] = n;
5099 temp[i++] = NUL;
5100 rettv->v_type = VAR_STRING;
5101 rettv->vval.v_string = vim_strsave(temp);
5102
5103#ifdef FEAT_MOUSE
5104 if (is_mouse_key(n))
5105 {
5106 int row = mouse_row;
5107 int col = mouse_col;
5108 win_T *win;
5109 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005110 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005111 int winnr = 1;
5112
5113 if (row >= 0 && col >= 0)
5114 {
5115 /* Find the window at the mouse coordinates and compute the
5116 * text position. */
5117 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005118 if (win == NULL)
5119 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005120 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005121 for (wp = firstwin; wp != win; wp = wp->w_next)
5122 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5124 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5125 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5126 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5127 }
5128 }
5129#endif
5130 }
5131}
5132
5133/*
5134 * "getcharmod()" function
5135 */
5136 static void
5137f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5138{
5139 rettv->vval.v_number = mod_mask;
5140}
5141
5142/*
5143 * "getcharsearch()" function
5144 */
5145 static void
5146f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5147{
5148 if (rettv_dict_alloc(rettv) != FAIL)
5149 {
5150 dict_T *dict = rettv->vval.v_dict;
5151
Bram Moolenaare0be1672018-07-08 16:50:37 +02005152 dict_add_string(dict, "char", last_csearch());
5153 dict_add_number(dict, "forward", last_csearch_forward());
5154 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005155 }
5156}
5157
5158/*
5159 * "getcmdline()" function
5160 */
5161 static void
5162f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5163{
5164 rettv->v_type = VAR_STRING;
5165 rettv->vval.v_string = get_cmdline_str();
5166}
5167
5168/*
5169 * "getcmdpos()" function
5170 */
5171 static void
5172f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5173{
5174 rettv->vval.v_number = get_cmdline_pos() + 1;
5175}
5176
5177/*
5178 * "getcmdtype()" function
5179 */
5180 static void
5181f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5182{
5183 rettv->v_type = VAR_STRING;
5184 rettv->vval.v_string = alloc(2);
5185 if (rettv->vval.v_string != NULL)
5186 {
5187 rettv->vval.v_string[0] = get_cmdline_type();
5188 rettv->vval.v_string[1] = NUL;
5189 }
5190}
5191
5192/*
5193 * "getcmdwintype()" function
5194 */
5195 static void
5196f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5197{
5198 rettv->v_type = VAR_STRING;
5199 rettv->vval.v_string = NULL;
5200#ifdef FEAT_CMDWIN
5201 rettv->vval.v_string = alloc(2);
5202 if (rettv->vval.v_string != NULL)
5203 {
5204 rettv->vval.v_string[0] = cmdwin_type;
5205 rettv->vval.v_string[1] = NUL;
5206 }
5207#endif
5208}
5209
5210#if defined(FEAT_CMDL_COMPL)
5211/*
5212 * "getcompletion()" function
5213 */
5214 static void
5215f_getcompletion(typval_T *argvars, typval_T *rettv)
5216{
5217 char_u *pat;
5218 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005219 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005220 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5221 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005222
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005223 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005224 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005225
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005226 if (p_wic)
5227 options |= WILD_ICASE;
5228
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005229 /* For filtered results, 'wildignore' is used */
5230 if (!filtered)
5231 options |= WILD_KEEP_ALL;
5232
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005233 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005234 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005235 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005236 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 if (xpc.xp_context == EXPAND_NOTHING)
5238 {
5239 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005240 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243 return;
5244 }
5245
5246# if defined(FEAT_MENU)
5247 if (xpc.xp_context == EXPAND_MENUS)
5248 {
5249 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5250 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5251 }
5252# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005253#ifdef FEAT_CSCOPE
5254 if (xpc.xp_context == EXPAND_CSCOPE)
5255 {
5256 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5257 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5258 }
5259#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005260#ifdef FEAT_SIGNS
5261 if (xpc.xp_context == EXPAND_SIGN)
5262 {
5263 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5264 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5265 }
5266#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005267
5268 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5269 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5270 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005271 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005272
5273 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5274
5275 for (i = 0; i < xpc.xp_numfiles; i++)
5276 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5277 }
5278 vim_free(pat);
5279 ExpandCleanup(&xpc);
5280}
5281#endif
5282
5283/*
5284 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005285 *
5286 * Return the current working directory of a window in a tab page.
5287 * First optional argument 'winnr' is the window number or -1 and the second
5288 * optional argument 'tabnr' is the tab page number.
5289 *
5290 * If no arguments are supplied, then return the directory of the current
5291 * window.
5292 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5293 * the specified window.
5294 * If 'winnr' is 0 then return the directory of the current window.
5295 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5296 * directory of the specified tab page. Otherwise return the directory of the
5297 * specified window in the specified tab page.
5298 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005299 */
5300 static void
5301f_getcwd(typval_T *argvars, typval_T *rettv)
5302{
5303 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005304 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005305 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005306 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307
5308 rettv->v_type = VAR_STRING;
5309 rettv->vval.v_string = NULL;
5310
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005311 if (argvars[0].v_type == VAR_NUMBER
5312 && argvars[0].vval.v_number == -1
5313 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005314 global = TRUE;
5315 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005316 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005317
5318 if (wp != NULL && wp->w_localdir != NULL)
5319 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005320 else if (tp != NULL && tp->tp_localdir != NULL)
5321 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5322 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005323 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005324 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 rettv->vval.v_string = vim_strsave(globaldir);
5326 else
5327 {
5328 cwd = alloc(MAXPATHL);
5329 if (cwd != NULL)
5330 {
5331 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5332 rettv->vval.v_string = vim_strsave(cwd);
5333 vim_free(cwd);
5334 }
5335 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005336 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005337#ifdef BACKSLASH_IN_FILENAME
5338 if (rettv->vval.v_string != NULL)
5339 slash_adjust(rettv->vval.v_string);
5340#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005341}
5342
5343/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005344 * "getenv()" function
5345 */
5346 static void
5347f_getenv(typval_T *argvars, typval_T *rettv)
5348{
5349 int mustfree = FALSE;
5350 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5351
5352 if (p == NULL)
5353 {
5354 rettv->v_type = VAR_SPECIAL;
5355 rettv->vval.v_number = VVAL_NULL;
5356 return;
5357 }
5358 if (!mustfree)
5359 p = vim_strsave(p);
5360 rettv->vval.v_string = p;
5361 rettv->v_type = VAR_STRING;
5362}
5363
5364/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005365 * "getfontname()" function
5366 */
5367 static void
5368f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5369{
5370 rettv->v_type = VAR_STRING;
5371 rettv->vval.v_string = NULL;
5372#ifdef FEAT_GUI
5373 if (gui.in_use)
5374 {
5375 GuiFont font;
5376 char_u *name = NULL;
5377
5378 if (argvars[0].v_type == VAR_UNKNOWN)
5379 {
5380 /* Get the "Normal" font. Either the name saved by
5381 * hl_set_font_name() or from the font ID. */
5382 font = gui.norm_font;
5383 name = hl_get_font_name();
5384 }
5385 else
5386 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005387 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005388 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5389 return;
5390 font = gui_mch_get_font(name, FALSE);
5391 if (font == NOFONT)
5392 return; /* Invalid font name, return empty string. */
5393 }
5394 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5395 if (argvars[0].v_type != VAR_UNKNOWN)
5396 gui_mch_free_font(font);
5397 }
5398#endif
5399}
5400
5401/*
5402 * "getfperm({fname})" function
5403 */
5404 static void
5405f_getfperm(typval_T *argvars, typval_T *rettv)
5406{
5407 char_u *fname;
5408 stat_T st;
5409 char_u *perm = NULL;
5410 char_u flags[] = "rwx";
5411 int i;
5412
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005413 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005414
5415 rettv->v_type = VAR_STRING;
5416 if (mch_stat((char *)fname, &st) >= 0)
5417 {
5418 perm = vim_strsave((char_u *)"---------");
5419 if (perm != NULL)
5420 {
5421 for (i = 0; i < 9; i++)
5422 {
5423 if (st.st_mode & (1 << (8 - i)))
5424 perm[i] = flags[i % 3];
5425 }
5426 }
5427 }
5428 rettv->vval.v_string = perm;
5429}
5430
5431/*
5432 * "getfsize({fname})" function
5433 */
5434 static void
5435f_getfsize(typval_T *argvars, typval_T *rettv)
5436{
5437 char_u *fname;
5438 stat_T st;
5439
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005440 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005441
5442 rettv->v_type = VAR_NUMBER;
5443
5444 if (mch_stat((char *)fname, &st) >= 0)
5445 {
5446 if (mch_isdir(fname))
5447 rettv->vval.v_number = 0;
5448 else
5449 {
5450 rettv->vval.v_number = (varnumber_T)st.st_size;
5451
5452 /* non-perfect check for overflow */
5453 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5454 rettv->vval.v_number = -2;
5455 }
5456 }
5457 else
5458 rettv->vval.v_number = -1;
5459}
5460
5461/*
5462 * "getftime({fname})" function
5463 */
5464 static void
5465f_getftime(typval_T *argvars, typval_T *rettv)
5466{
5467 char_u *fname;
5468 stat_T st;
5469
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005470 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005471
5472 if (mch_stat((char *)fname, &st) >= 0)
5473 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5474 else
5475 rettv->vval.v_number = -1;
5476}
5477
5478/*
5479 * "getftype({fname})" function
5480 */
5481 static void
5482f_getftype(typval_T *argvars, typval_T *rettv)
5483{
5484 char_u *fname;
5485 stat_T st;
5486 char_u *type = NULL;
5487 char *t;
5488
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005489 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005490
5491 rettv->v_type = VAR_STRING;
5492 if (mch_lstat((char *)fname, &st) >= 0)
5493 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005494 if (S_ISREG(st.st_mode))
5495 t = "file";
5496 else if (S_ISDIR(st.st_mode))
5497 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005498 else if (S_ISLNK(st.st_mode))
5499 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 else if (S_ISBLK(st.st_mode))
5501 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502 else if (S_ISCHR(st.st_mode))
5503 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 else if (S_ISFIFO(st.st_mode))
5505 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005507 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 else
5509 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 type = vim_strsave((char_u *)t);
5511 }
5512 rettv->vval.v_string = type;
5513}
5514
5515/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005516 * "getjumplist()" function
5517 */
5518 static void
5519f_getjumplist(typval_T *argvars, typval_T *rettv)
5520{
5521#ifdef FEAT_JUMPLIST
5522 win_T *wp;
5523 int i;
5524 list_T *l;
5525 dict_T *d;
5526#endif
5527
5528 if (rettv_list_alloc(rettv) != OK)
5529 return;
5530
5531#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005532 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005533 if (wp == NULL)
5534 return;
5535
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005536 cleanup_jumplist(wp, TRUE);
5537
Bram Moolenaar4f505882018-02-10 21:06:32 +01005538 l = list_alloc();
5539 if (l == NULL)
5540 return;
5541
5542 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5543 return;
5544 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5545
5546 for (i = 0; i < wp->w_jumplistlen; ++i)
5547 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005548 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5549 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005550 if ((d = dict_alloc()) == NULL)
5551 return;
5552 if (list_append_dict(l, d) == FAIL)
5553 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005554 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5555 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005556 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005557 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005558 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005559 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005560 }
5561#endif
5562}
5563
5564/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005565 * "getline(lnum, [end])" function
5566 */
5567 static void
5568f_getline(typval_T *argvars, typval_T *rettv)
5569{
5570 linenr_T lnum;
5571 linenr_T end;
5572 int retlist;
5573
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005574 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005575 if (argvars[1].v_type == VAR_UNKNOWN)
5576 {
5577 end = 0;
5578 retlist = FALSE;
5579 }
5580 else
5581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005582 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005583 retlist = TRUE;
5584 }
5585
5586 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5587}
5588
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005589#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005590 static void
5591get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5592{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005593 if (what_arg->v_type == VAR_UNKNOWN)
5594 {
5595 if (rettv_list_alloc(rettv) == OK)
5596 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005597 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005598 }
5599 else
5600 {
5601 if (rettv_dict_alloc(rettv) == OK)
5602 if (is_qf || (wp != NULL))
5603 {
5604 if (what_arg->v_type == VAR_DICT)
5605 {
5606 dict_T *d = what_arg->vval.v_dict;
5607
5608 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005609 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005610 }
5611 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005612 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005613 }
5614 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005616#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005617
5618/*
5619 * "getloclist()" function
5620 */
5621 static void
5622f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5623{
5624#ifdef FEAT_QUICKFIX
5625 win_T *wp;
5626
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005627 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005628 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5629#endif
5630}
5631
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005632/*
5633 * "getmatches()" function
5634 */
5635 static void
5636f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5637{
5638#ifdef FEAT_SEARCH_EXTRA
5639 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005640 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005641 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005642 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643
Bram Moolenaaraff74912019-03-30 18:11:49 +01005644 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5645 return;
5646
5647 cur = win->w_match_head;
5648 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005649 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005650 dict = dict_alloc();
5651 if (dict == NULL)
5652 return;
5653 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005654 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005655 /* match added with matchaddpos() */
5656 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005657 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005658 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005659 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 list_T *l;
5661
5662 llpos = &cur->pos.pos[i];
5663 if (llpos->lnum == 0)
5664 break;
5665 l = list_alloc();
5666 if (l == NULL)
5667 break;
5668 list_append_number(l, (varnumber_T)llpos->lnum);
5669 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005670 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005671 list_append_number(l, (varnumber_T)llpos->col);
5672 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005673 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005674 sprintf(buf, "pos%d", i + 1);
5675 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005676 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005677 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005678 else
5679 {
5680 dict_add_string(dict, "pattern", cur->pattern);
5681 }
5682 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5683 dict_add_number(dict, "priority", (long)cur->priority);
5684 dict_add_number(dict, "id", (long)cur->id);
5685# if defined(FEAT_CONCEAL)
5686 if (cur->conceal_char)
5687 {
5688 char_u buf[MB_MAXBYTES + 1];
5689
5690 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5691 dict_add_string(dict, "conceal", (char_u *)&buf);
5692 }
5693# endif
5694 list_append_dict(rettv->vval.v_list, dict);
5695 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005696 }
5697#endif
5698}
5699
5700/*
5701 * "getpid()" function
5702 */
5703 static void
5704f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5705{
5706 rettv->vval.v_number = mch_get_pid();
5707}
5708
5709 static void
5710getpos_both(
5711 typval_T *argvars,
5712 typval_T *rettv,
5713 int getcurpos)
5714{
5715 pos_T *fp;
5716 list_T *l;
5717 int fnum = -1;
5718
5719 if (rettv_list_alloc(rettv) == OK)
5720 {
5721 l = rettv->vval.v_list;
5722 if (getcurpos)
5723 fp = &curwin->w_cursor;
5724 else
5725 fp = var2fpos(&argvars[0], TRUE, &fnum);
5726 if (fnum != -1)
5727 list_append_number(l, (varnumber_T)fnum);
5728 else
5729 list_append_number(l, (varnumber_T)0);
5730 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5731 : (varnumber_T)0);
5732 list_append_number(l, (fp != NULL)
5733 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5734 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005735 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 (varnumber_T)0);
5737 if (getcurpos)
5738 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005739 int save_set_curswant = curwin->w_set_curswant;
5740 colnr_T save_curswant = curwin->w_curswant;
5741 colnr_T save_virtcol = curwin->w_virtcol;
5742
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005743 update_curswant();
5744 list_append_number(l, curwin->w_curswant == MAXCOL ?
5745 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005746
5747 // Do not change "curswant", as it is unexpected that a get
5748 // function has a side effect.
5749 if (save_set_curswant)
5750 {
5751 curwin->w_set_curswant = save_set_curswant;
5752 curwin->w_curswant = save_curswant;
5753 curwin->w_virtcol = save_virtcol;
5754 curwin->w_valid &= ~VALID_VIRTCOL;
5755 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005756 }
5757 }
5758 else
5759 rettv->vval.v_number = FALSE;
5760}
5761
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005762/*
5763 * "getcurpos()" function
5764 */
5765 static void
5766f_getcurpos(typval_T *argvars, typval_T *rettv)
5767{
5768 getpos_both(argvars, rettv, TRUE);
5769}
5770
5771/*
5772 * "getpos(string)" function
5773 */
5774 static void
5775f_getpos(typval_T *argvars, typval_T *rettv)
5776{
5777 getpos_both(argvars, rettv, FALSE);
5778}
5779
5780/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005781 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005782 */
5783 static void
5784f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5785{
5786#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005787 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005788#endif
5789}
5790
5791/*
5792 * "getreg()" function
5793 */
5794 static void
5795f_getreg(typval_T *argvars, typval_T *rettv)
5796{
5797 char_u *strregname;
5798 int regname;
5799 int arg2 = FALSE;
5800 int return_list = FALSE;
5801 int error = FALSE;
5802
5803 if (argvars[0].v_type != VAR_UNKNOWN)
5804 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005806 error = strregname == NULL;
5807 if (argvars[1].v_type != VAR_UNKNOWN)
5808 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005809 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005810 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005811 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005812 }
5813 }
5814 else
5815 strregname = get_vim_var_str(VV_REG);
5816
5817 if (error)
5818 return;
5819
5820 regname = (strregname == NULL ? '"' : *strregname);
5821 if (regname == 0)
5822 regname = '"';
5823
5824 if (return_list)
5825 {
5826 rettv->v_type = VAR_LIST;
5827 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5828 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5829 if (rettv->vval.v_list == NULL)
5830 (void)rettv_list_alloc(rettv);
5831 else
5832 ++rettv->vval.v_list->lv_refcount;
5833 }
5834 else
5835 {
5836 rettv->v_type = VAR_STRING;
5837 rettv->vval.v_string = get_reg_contents(regname,
5838 arg2 ? GREG_EXPR_SRC : 0);
5839 }
5840}
5841
5842/*
5843 * "getregtype()" function
5844 */
5845 static void
5846f_getregtype(typval_T *argvars, typval_T *rettv)
5847{
5848 char_u *strregname;
5849 int regname;
5850 char_u buf[NUMBUFLEN + 2];
5851 long reglen = 0;
5852
5853 if (argvars[0].v_type != VAR_UNKNOWN)
5854 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005856 if (strregname == NULL) /* type error; errmsg already given */
5857 {
5858 rettv->v_type = VAR_STRING;
5859 rettv->vval.v_string = NULL;
5860 return;
5861 }
5862 }
5863 else
5864 /* Default to v:register */
5865 strregname = get_vim_var_str(VV_REG);
5866
5867 regname = (strregname == NULL ? '"' : *strregname);
5868 if (regname == 0)
5869 regname = '"';
5870
5871 buf[0] = NUL;
5872 buf[1] = NUL;
5873 switch (get_reg_type(regname, &reglen))
5874 {
5875 case MLINE: buf[0] = 'V'; break;
5876 case MCHAR: buf[0] = 'v'; break;
5877 case MBLOCK:
5878 buf[0] = Ctrl_V;
5879 sprintf((char *)buf + 1, "%ld", reglen + 1);
5880 break;
5881 }
5882 rettv->v_type = VAR_STRING;
5883 rettv->vval.v_string = vim_strsave(buf);
5884}
5885
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005886/*
5887 * Returns information (variables, options, etc.) about a tab page
5888 * as a dictionary.
5889 */
5890 static dict_T *
5891get_tabpage_info(tabpage_T *tp, int tp_idx)
5892{
5893 win_T *wp;
5894 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005895 list_T *l;
5896
5897 dict = dict_alloc();
5898 if (dict == NULL)
5899 return NULL;
5900
Bram Moolenaare0be1672018-07-08 16:50:37 +02005901 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005902
5903 l = list_alloc();
5904 if (l != NULL)
5905 {
5906 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5907 wp; wp = wp->w_next)
5908 list_append_number(l, (varnumber_T)wp->w_id);
5909 dict_add_list(dict, "windows", l);
5910 }
5911
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005912 /* Make a reference to tabpage variables */
5913 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005914
5915 return dict;
5916}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005917
5918/*
5919 * "gettabinfo()" function
5920 */
5921 static void
5922f_gettabinfo(typval_T *argvars, typval_T *rettv)
5923{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005924 tabpage_T *tp, *tparg = NULL;
5925 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005926 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005927
5928 if (rettv_list_alloc(rettv) != OK)
5929 return;
5930
5931 if (argvars[0].v_type != VAR_UNKNOWN)
5932 {
5933 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005934 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005935 if (tparg == NULL)
5936 return;
5937 }
5938
5939 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005940 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005941 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005942 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005943 if (tparg != NULL && tp != tparg)
5944 continue;
5945 d = get_tabpage_info(tp, tpnr);
5946 if (d != NULL)
5947 list_append_dict(rettv->vval.v_list, d);
5948 if (tparg != NULL)
5949 return;
5950 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005951}
5952
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953/*
5954 * "gettabvar()" function
5955 */
5956 static void
5957f_gettabvar(typval_T *argvars, typval_T *rettv)
5958{
5959 win_T *oldcurwin;
5960 tabpage_T *tp, *oldtabpage;
5961 dictitem_T *v;
5962 char_u *varname;
5963 int done = FALSE;
5964
5965 rettv->v_type = VAR_STRING;
5966 rettv->vval.v_string = NULL;
5967
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005968 varname = tv_get_string_chk(&argvars[1]);
5969 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005970 if (tp != NULL && varname != NULL)
5971 {
5972 /* Set tp to be our tabpage, temporarily. Also set the window to the
5973 * first window in the tabpage, otherwise the window is not valid. */
5974 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005975 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5976 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005977 {
5978 /* look up the variable */
5979 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5980 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5981 if (v != NULL)
5982 {
5983 copy_tv(&v->di_tv, rettv);
5984 done = TRUE;
5985 }
5986 }
5987
5988 /* restore previous notion of curwin */
5989 restore_win(oldcurwin, oldtabpage, TRUE);
5990 }
5991
5992 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5993 copy_tv(&argvars[2], rettv);
5994}
5995
5996/*
5997 * "gettabwinvar()" function
5998 */
5999 static void
6000f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6001{
6002 getwinvar(argvars, rettv, 1);
6003}
6004
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006005/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006006 * "gettagstack()" function
6007 */
6008 static void
6009f_gettagstack(typval_T *argvars, typval_T *rettv)
6010{
6011 win_T *wp = curwin; // default is current window
6012
6013 if (rettv_dict_alloc(rettv) != OK)
6014 return;
6015
6016 if (argvars[0].v_type != VAR_UNKNOWN)
6017 {
6018 wp = find_win_by_nr_or_id(&argvars[0]);
6019 if (wp == NULL)
6020 return;
6021 }
6022
6023 get_tagstack(wp, rettv->vval.v_dict);
6024}
6025
6026/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006027 * Returns information about a window as a dictionary.
6028 */
6029 static dict_T *
6030get_win_info(win_T *wp, short tpnr, short winnr)
6031{
6032 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006033
6034 dict = dict_alloc();
6035 if (dict == NULL)
6036 return NULL;
6037
Bram Moolenaare0be1672018-07-08 16:50:37 +02006038 dict_add_number(dict, "tabnr", tpnr);
6039 dict_add_number(dict, "winnr", winnr);
6040 dict_add_number(dict, "winid", wp->w_id);
6041 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006042 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006043 dict_add_number(dict, "topline", wp->w_topline);
6044 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006045#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006046 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006047#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006048 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006049 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006050 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006051
Bram Moolenaar69905d12017-08-13 18:14:47 +02006052#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006053 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006054#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006055#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006056 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6057 dict_add_number(dict, "loclist",
6058 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006059#endif
6060
Bram Moolenaar30567352016-08-27 21:25:44 +02006061 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006062 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006063
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006064 return dict;
6065}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066
6067/*
6068 * "getwininfo()" function
6069 */
6070 static void
6071f_getwininfo(typval_T *argvars, typval_T *rettv)
6072{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006073 tabpage_T *tp;
6074 win_T *wp = NULL, *wparg = NULL;
6075 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006076 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006077
6078 if (rettv_list_alloc(rettv) != OK)
6079 return;
6080
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006081 if (argvars[0].v_type != VAR_UNKNOWN)
6082 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006083 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006084 if (wparg == NULL)
6085 return;
6086 }
6087
6088 /* Collect information about either all the windows across all the tab
6089 * pages or one particular window.
6090 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006091 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006092 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006093 tabnr++;
6094 winnr = 0;
6095 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006096 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006097 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006098 if (wparg != NULL && wp != wparg)
6099 continue;
6100 d = get_win_info(wp, tabnr, winnr);
6101 if (d != NULL)
6102 list_append_dict(rettv->vval.v_list, d);
6103 if (wparg != NULL)
6104 /* found information about a specific window */
6105 return;
6106 }
6107 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006108}
6109
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006110/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006111 * "win_execute()" function
6112 */
6113 static void
6114f_win_execute(typval_T *argvars, typval_T *rettv)
6115{
6116 int id = (int)tv_get_number(argvars);
6117 win_T *wp = win_id2wp(id);
6118 win_T *save_curwin = curwin;
6119
6120 if (wp != NULL)
6121 {
6122 curwin = wp;
6123 curbuf = curwin->w_buffer;
6124 check_cursor();
6125 execute_common(argvars, rettv, 1);
6126 if (win_valid(save_curwin))
6127 {
6128 curwin = save_curwin;
6129 curbuf = curwin->w_buffer;
6130 }
6131 }
6132}
6133
6134/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006135 * "win_findbuf()" function
6136 */
6137 static void
6138f_win_findbuf(typval_T *argvars, typval_T *rettv)
6139{
6140 if (rettv_list_alloc(rettv) != FAIL)
6141 win_findbuf(argvars, rettv->vval.v_list);
6142}
6143
6144/*
6145 * "win_getid()" function
6146 */
6147 static void
6148f_win_getid(typval_T *argvars, typval_T *rettv)
6149{
6150 rettv->vval.v_number = win_getid(argvars);
6151}
6152
6153/*
6154 * "win_gotoid()" function
6155 */
6156 static void
6157f_win_gotoid(typval_T *argvars, typval_T *rettv)
6158{
6159 rettv->vval.v_number = win_gotoid(argvars);
6160}
6161
6162/*
6163 * "win_id2tabwin()" function
6164 */
6165 static void
6166f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6167{
6168 if (rettv_list_alloc(rettv) != FAIL)
6169 win_id2tabwin(argvars, rettv->vval.v_list);
6170}
6171
6172/*
6173 * "win_id2win()" function
6174 */
6175 static void
6176f_win_id2win(typval_T *argvars, typval_T *rettv)
6177{
6178 rettv->vval.v_number = win_id2win(argvars);
6179}
6180
6181/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006182 * "win_screenpos()" function
6183 */
6184 static void
6185f_win_screenpos(typval_T *argvars, typval_T *rettv)
6186{
6187 win_T *wp;
6188
6189 if (rettv_list_alloc(rettv) == FAIL)
6190 return;
6191
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006192 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006193 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6194 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6195}
6196
6197/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006198 * "getwinpos({timeout})" function
6199 */
6200 static void
6201f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6202{
6203 int x = -1;
6204 int y = -1;
6205
6206 if (rettv_list_alloc(rettv) == FAIL)
6207 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006208#if defined(FEAT_GUI) \
6209 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6210 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006211 {
6212 varnumber_T timeout = 100;
6213
6214 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006215 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006216
6217 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006218 }
6219#endif
6220 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6221 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6222}
6223
6224
6225/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006226 * "getwinposx()" function
6227 */
6228 static void
6229f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6230{
6231 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006232#if defined(FEAT_GUI) \
6233 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6234 || defined(MSWIN)
6235
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006236 {
6237 int x, y;
6238
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006239 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006240 rettv->vval.v_number = x;
6241 }
6242#endif
6243}
6244
6245/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006246 * "getwinposy()" function
6247 */
6248 static void
6249f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6250{
6251 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006252#if defined(FEAT_GUI) \
6253 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6254 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006255 {
6256 int x, y;
6257
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006258 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006259 rettv->vval.v_number = y;
6260 }
6261#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006262}
6263
6264/*
6265 * "getwinvar()" function
6266 */
6267 static void
6268f_getwinvar(typval_T *argvars, typval_T *rettv)
6269{
6270 getwinvar(argvars, rettv, 0);
6271}
6272
6273/*
6274 * "glob()" function
6275 */
6276 static void
6277f_glob(typval_T *argvars, typval_T *rettv)
6278{
6279 int options = WILD_SILENT|WILD_USE_NL;
6280 expand_T xpc;
6281 int error = FALSE;
6282
6283 /* When the optional second argument is non-zero, don't remove matches
6284 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6285 rettv->v_type = VAR_STRING;
6286 if (argvars[1].v_type != VAR_UNKNOWN)
6287 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006288 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006289 options |= WILD_KEEP_ALL;
6290 if (argvars[2].v_type != VAR_UNKNOWN)
6291 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006292 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006293 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006294 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006295 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006296 options |= WILD_ALLLINKS;
6297 }
6298 }
6299 if (!error)
6300 {
6301 ExpandInit(&xpc);
6302 xpc.xp_context = EXPAND_FILES;
6303 if (p_wic)
6304 options += WILD_ICASE;
6305 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006306 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006307 NULL, options, WILD_ALL);
6308 else if (rettv_list_alloc(rettv) != FAIL)
6309 {
6310 int i;
6311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006312 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006313 NULL, options, WILD_ALL_KEEP);
6314 for (i = 0; i < xpc.xp_numfiles; i++)
6315 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6316
6317 ExpandCleanup(&xpc);
6318 }
6319 }
6320 else
6321 rettv->vval.v_string = NULL;
6322}
6323
6324/*
6325 * "globpath()" function
6326 */
6327 static void
6328f_globpath(typval_T *argvars, typval_T *rettv)
6329{
6330 int flags = 0;
6331 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006332 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006333 int error = FALSE;
6334 garray_T ga;
6335 int i;
6336
6337 /* When the optional second argument is non-zero, don't remove matches
6338 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6339 rettv->v_type = VAR_STRING;
6340 if (argvars[2].v_type != VAR_UNKNOWN)
6341 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006342 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006343 flags |= WILD_KEEP_ALL;
6344 if (argvars[3].v_type != VAR_UNKNOWN)
6345 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006346 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006347 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006348 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006349 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006350 flags |= WILD_ALLLINKS;
6351 }
6352 }
6353 if (file != NULL && !error)
6354 {
6355 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006356 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006357 if (rettv->v_type == VAR_STRING)
6358 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6359 else if (rettv_list_alloc(rettv) != FAIL)
6360 for (i = 0; i < ga.ga_len; ++i)
6361 list_append_string(rettv->vval.v_list,
6362 ((char_u **)(ga.ga_data))[i], -1);
6363 ga_clear_strings(&ga);
6364 }
6365 else
6366 rettv->vval.v_string = NULL;
6367}
6368
6369/*
6370 * "glob2regpat()" function
6371 */
6372 static void
6373f_glob2regpat(typval_T *argvars, typval_T *rettv)
6374{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006375 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006376
6377 rettv->v_type = VAR_STRING;
6378 rettv->vval.v_string = (pat == NULL)
6379 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6380}
6381
6382/* for VIM_VERSION_ defines */
6383#include "version.h"
6384
6385/*
6386 * "has()" function
6387 */
6388 static void
6389f_has(typval_T *argvars, typval_T *rettv)
6390{
6391 int i;
6392 char_u *name;
6393 int n = FALSE;
6394 static char *(has_list[]) =
6395 {
6396#ifdef AMIGA
6397 "amiga",
6398# ifdef FEAT_ARP
6399 "arp",
6400# endif
6401#endif
6402#ifdef __BEOS__
6403 "beos",
6404#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006405#if defined(BSD) && !defined(MACOS_X)
6406 "bsd",
6407#endif
6408#ifdef hpux
6409 "hpux",
6410#endif
6411#ifdef __linux__
6412 "linux",
6413#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006414#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006415 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6416 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006417# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006418 "macunix", /* Mac OS X, with the darwin feature */
6419 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006420# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006421#endif
6422#ifdef __QNX__
6423 "qnx",
6424#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006425#ifdef SUN_SYSTEM
6426 "sun",
6427#else
6428 "moon",
6429#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006430#ifdef UNIX
6431 "unix",
6432#endif
6433#ifdef VMS
6434 "vms",
6435#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006436#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006437 "win32",
6438#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006439#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006440 "win32unix",
6441#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006442#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006443 "win64",
6444#endif
6445#ifdef EBCDIC
6446 "ebcdic",
6447#endif
6448#ifndef CASE_INSENSITIVE_FILENAME
6449 "fname_case",
6450#endif
6451#ifdef HAVE_ACL
6452 "acl",
6453#endif
6454#ifdef FEAT_ARABIC
6455 "arabic",
6456#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006457 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006458#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006459 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006460#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006461#ifdef FEAT_AUTOSERVERNAME
6462 "autoservername",
6463#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006464#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006465 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006466# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006467 "balloon_multiline",
6468# endif
6469#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006470#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006471 "balloon_eval_term",
6472#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006473#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6474 "builtin_terms",
6475# ifdef ALL_BUILTIN_TCAPS
6476 "all_builtin_terms",
6477# endif
6478#endif
6479#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006480 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006481 || defined(FEAT_GUI_MOTIF))
6482 "browsefilter",
6483#endif
6484#ifdef FEAT_BYTEOFF
6485 "byte_offset",
6486#endif
6487#ifdef FEAT_JOB_CHANNEL
6488 "channel",
6489#endif
6490#ifdef FEAT_CINDENT
6491 "cindent",
6492#endif
6493#ifdef FEAT_CLIENTSERVER
6494 "clientserver",
6495#endif
6496#ifdef FEAT_CLIPBOARD
6497 "clipboard",
6498#endif
6499#ifdef FEAT_CMDL_COMPL
6500 "cmdline_compl",
6501#endif
6502#ifdef FEAT_CMDHIST
6503 "cmdline_hist",
6504#endif
6505#ifdef FEAT_COMMENTS
6506 "comments",
6507#endif
6508#ifdef FEAT_CONCEAL
6509 "conceal",
6510#endif
6511#ifdef FEAT_CRYPT
6512 "cryptv",
6513 "crypt-blowfish",
6514 "crypt-blowfish2",
6515#endif
6516#ifdef FEAT_CSCOPE
6517 "cscope",
6518#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006519 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520#ifdef CURSOR_SHAPE
6521 "cursorshape",
6522#endif
6523#ifdef DEBUG
6524 "debug",
6525#endif
6526#ifdef FEAT_CON_DIALOG
6527 "dialog_con",
6528#endif
6529#ifdef FEAT_GUI_DIALOG
6530 "dialog_gui",
6531#endif
6532#ifdef FEAT_DIFF
6533 "diff",
6534#endif
6535#ifdef FEAT_DIGRAPHS
6536 "digraphs",
6537#endif
6538#ifdef FEAT_DIRECTX
6539 "directx",
6540#endif
6541#ifdef FEAT_DND
6542 "dnd",
6543#endif
6544#ifdef FEAT_EMACS_TAGS
6545 "emacs_tags",
6546#endif
6547 "eval", /* always present, of course! */
6548 "ex_extra", /* graduated feature */
6549#ifdef FEAT_SEARCH_EXTRA
6550 "extra_search",
6551#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006552#ifdef FEAT_SEARCHPATH
6553 "file_in_path",
6554#endif
6555#ifdef FEAT_FILTERPIPE
6556 "filterpipe",
6557#endif
6558#ifdef FEAT_FIND_ID
6559 "find_in_path",
6560#endif
6561#ifdef FEAT_FLOAT
6562 "float",
6563#endif
6564#ifdef FEAT_FOLDING
6565 "folding",
6566#endif
6567#ifdef FEAT_FOOTER
6568 "footer",
6569#endif
6570#if !defined(USE_SYSTEM) && defined(UNIX)
6571 "fork",
6572#endif
6573#ifdef FEAT_GETTEXT
6574 "gettext",
6575#endif
6576#ifdef FEAT_GUI
6577 "gui",
6578#endif
6579#ifdef FEAT_GUI_ATHENA
6580# ifdef FEAT_GUI_NEXTAW
6581 "gui_neXtaw",
6582# else
6583 "gui_athena",
6584# endif
6585#endif
6586#ifdef FEAT_GUI_GTK
6587 "gui_gtk",
6588# ifdef USE_GTK3
6589 "gui_gtk3",
6590# else
6591 "gui_gtk2",
6592# endif
6593#endif
6594#ifdef FEAT_GUI_GNOME
6595 "gui_gnome",
6596#endif
6597#ifdef FEAT_GUI_MAC
6598 "gui_mac",
6599#endif
6600#ifdef FEAT_GUI_MOTIF
6601 "gui_motif",
6602#endif
6603#ifdef FEAT_GUI_PHOTON
6604 "gui_photon",
6605#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006606#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006607 "gui_win32",
6608#endif
6609#ifdef FEAT_HANGULIN
6610 "hangul_input",
6611#endif
6612#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6613 "iconv",
6614#endif
6615#ifdef FEAT_INS_EXPAND
6616 "insert_expand",
6617#endif
6618#ifdef FEAT_JOB_CHANNEL
6619 "job",
6620#endif
6621#ifdef FEAT_JUMPLIST
6622 "jumplist",
6623#endif
6624#ifdef FEAT_KEYMAP
6625 "keymap",
6626#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006627 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006628#ifdef FEAT_LANGMAP
6629 "langmap",
6630#endif
6631#ifdef FEAT_LIBCALL
6632 "libcall",
6633#endif
6634#ifdef FEAT_LINEBREAK
6635 "linebreak",
6636#endif
6637#ifdef FEAT_LISP
6638 "lispindent",
6639#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006640 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641#ifdef FEAT_LOCALMAP
6642 "localmap",
6643#endif
6644#ifdef FEAT_LUA
6645# ifndef DYNAMIC_LUA
6646 "lua",
6647# endif
6648#endif
6649#ifdef FEAT_MENU
6650 "menu",
6651#endif
6652#ifdef FEAT_SESSION
6653 "mksession",
6654#endif
6655#ifdef FEAT_MODIFY_FNAME
6656 "modify_fname",
6657#endif
6658#ifdef FEAT_MOUSE
6659 "mouse",
6660#endif
6661#ifdef FEAT_MOUSESHAPE
6662 "mouseshape",
6663#endif
6664#if defined(UNIX) || defined(VMS)
6665# ifdef FEAT_MOUSE_DEC
6666 "mouse_dec",
6667# endif
6668# ifdef FEAT_MOUSE_GPM
6669 "mouse_gpm",
6670# endif
6671# ifdef FEAT_MOUSE_JSB
6672 "mouse_jsbterm",
6673# endif
6674# ifdef FEAT_MOUSE_NET
6675 "mouse_netterm",
6676# endif
6677# ifdef FEAT_MOUSE_PTERM
6678 "mouse_pterm",
6679# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006680# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006681 "mouse_sgr",
6682# endif
6683# ifdef FEAT_SYSMOUSE
6684 "mouse_sysmouse",
6685# endif
6686# ifdef FEAT_MOUSE_URXVT
6687 "mouse_urxvt",
6688# endif
6689# ifdef FEAT_MOUSE_XTERM
6690 "mouse_xterm",
6691# endif
6692#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006693 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694#ifdef FEAT_MBYTE_IME
6695 "multi_byte_ime",
6696#endif
6697#ifdef FEAT_MULTI_LANG
6698 "multi_lang",
6699#endif
6700#ifdef FEAT_MZSCHEME
6701#ifndef DYNAMIC_MZSCHEME
6702 "mzscheme",
6703#endif
6704#endif
6705#ifdef FEAT_NUM64
6706 "num64",
6707#endif
6708#ifdef FEAT_OLE
6709 "ole",
6710#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006711#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006712 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006713#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006714#ifdef FEAT_PATH_EXTRA
6715 "path_extra",
6716#endif
6717#ifdef FEAT_PERL
6718#ifndef DYNAMIC_PERL
6719 "perl",
6720#endif
6721#endif
6722#ifdef FEAT_PERSISTENT_UNDO
6723 "persistent_undo",
6724#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006725#if defined(FEAT_PYTHON)
6726 "python_compiled",
6727# if defined(DYNAMIC_PYTHON)
6728 "python_dynamic",
6729# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006730 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006731 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006732# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006733#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006734#if defined(FEAT_PYTHON3)
6735 "python3_compiled",
6736# if defined(DYNAMIC_PYTHON3)
6737 "python3_dynamic",
6738# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006739 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006740 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006741# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006742#endif
6743#ifdef FEAT_POSTSCRIPT
6744 "postscript",
6745#endif
6746#ifdef FEAT_PRINTER
6747 "printer",
6748#endif
6749#ifdef FEAT_PROFILE
6750 "profile",
6751#endif
6752#ifdef FEAT_RELTIME
6753 "reltime",
6754#endif
6755#ifdef FEAT_QUICKFIX
6756 "quickfix",
6757#endif
6758#ifdef FEAT_RIGHTLEFT
6759 "rightleft",
6760#endif
6761#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6762 "ruby",
6763#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006764 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765#ifdef FEAT_CMDL_INFO
6766 "showcmd",
6767 "cmdline_info",
6768#endif
6769#ifdef FEAT_SIGNS
6770 "signs",
6771#endif
6772#ifdef FEAT_SMARTINDENT
6773 "smartindent",
6774#endif
6775#ifdef STARTUPTIME
6776 "startuptime",
6777#endif
6778#ifdef FEAT_STL_OPT
6779 "statusline",
6780#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006781#ifdef FEAT_NETBEANS_INTG
6782 "netbeans_intg",
6783#endif
6784#ifdef FEAT_SPELL
6785 "spell",
6786#endif
6787#ifdef FEAT_SYN_HL
6788 "syntax",
6789#endif
6790#if defined(USE_SYSTEM) || !defined(UNIX)
6791 "system",
6792#endif
6793#ifdef FEAT_TAG_BINS
6794 "tag_binary",
6795#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006796#ifdef FEAT_TCL
6797# ifndef DYNAMIC_TCL
6798 "tcl",
6799# endif
6800#endif
6801#ifdef FEAT_TERMGUICOLORS
6802 "termguicolors",
6803#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006804#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006805 "terminal",
6806#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006807#ifdef TERMINFO
6808 "terminfo",
6809#endif
6810#ifdef FEAT_TERMRESPONSE
6811 "termresponse",
6812#endif
6813#ifdef FEAT_TEXTOBJ
6814 "textobjects",
6815#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006816#ifdef FEAT_TEXT_PROP
6817 "textprop",
6818#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006819#ifdef HAVE_TGETENT
6820 "tgetent",
6821#endif
6822#ifdef FEAT_TIMERS
6823 "timers",
6824#endif
6825#ifdef FEAT_TITLE
6826 "title",
6827#endif
6828#ifdef FEAT_TOOLBAR
6829 "toolbar",
6830#endif
6831#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6832 "unnamedplus",
6833#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006834 "user-commands", /* was accidentally included in 5.4 */
6835 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006836#ifdef FEAT_VARTABS
6837 "vartabs",
6838#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006839 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006840#ifdef FEAT_VIMINFO
6841 "viminfo",
6842#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006843 "vimscript-1",
6844 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006845 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006846 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006850#ifdef FEAT_VTP
6851 "vtp",
6852#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006853#ifdef FEAT_WILDIGN
6854 "wildignore",
6855#endif
6856#ifdef FEAT_WILDMENU
6857 "wildmenu",
6858#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006859 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860#ifdef FEAT_WAK
6861 "winaltkeys",
6862#endif
6863#ifdef FEAT_WRITEBACKUP
6864 "writebackup",
6865#endif
6866#ifdef FEAT_XIM
6867 "xim",
6868#endif
6869#ifdef FEAT_XFONTSET
6870 "xfontset",
6871#endif
6872#ifdef FEAT_XPM_W32
6873 "xpm",
6874 "xpm_w32", /* for backward compatibility */
6875#else
6876# if defined(HAVE_XPM)
6877 "xpm",
6878# endif
6879#endif
6880#ifdef USE_XSMP
6881 "xsmp",
6882#endif
6883#ifdef USE_XSMP_INTERACT
6884 "xsmp_interact",
6885#endif
6886#ifdef FEAT_XCLIPBOARD
6887 "xterm_clipboard",
6888#endif
6889#ifdef FEAT_XTERM_SAVE
6890 "xterm_save",
6891#endif
6892#if defined(UNIX) && defined(FEAT_X11)
6893 "X11",
6894#endif
6895 NULL
6896 };
6897
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006899 for (i = 0; has_list[i] != NULL; ++i)
6900 if (STRICMP(name, has_list[i]) == 0)
6901 {
6902 n = TRUE;
6903 break;
6904 }
6905
6906 if (n == FALSE)
6907 {
6908 if (STRNICMP(name, "patch", 5) == 0)
6909 {
6910 if (name[5] == '-'
6911 && STRLEN(name) >= 11
6912 && vim_isdigit(name[6])
6913 && vim_isdigit(name[8])
6914 && vim_isdigit(name[10]))
6915 {
6916 int major = atoi((char *)name + 6);
6917 int minor = atoi((char *)name + 8);
6918
6919 /* Expect "patch-9.9.01234". */
6920 n = (major < VIM_VERSION_MAJOR
6921 || (major == VIM_VERSION_MAJOR
6922 && (minor < VIM_VERSION_MINOR
6923 || (minor == VIM_VERSION_MINOR
6924 && has_patch(atoi((char *)name + 10))))));
6925 }
6926 else
6927 n = has_patch(atoi((char *)name + 5));
6928 }
6929 else if (STRICMP(name, "vim_starting") == 0)
6930 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006931 else if (STRICMP(name, "ttyin") == 0)
6932 n = mch_input_isatty();
6933 else if (STRICMP(name, "ttyout") == 0)
6934 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006935 else if (STRICMP(name, "multi_byte_encoding") == 0)
6936 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006937#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006938 else if (STRICMP(name, "balloon_multiline") == 0)
6939 n = multiline_balloon_available();
6940#endif
6941#ifdef DYNAMIC_TCL
6942 else if (STRICMP(name, "tcl") == 0)
6943 n = tcl_enabled(FALSE);
6944#endif
6945#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6946 else if (STRICMP(name, "iconv") == 0)
6947 n = iconv_enabled(FALSE);
6948#endif
6949#ifdef DYNAMIC_LUA
6950 else if (STRICMP(name, "lua") == 0)
6951 n = lua_enabled(FALSE);
6952#endif
6953#ifdef DYNAMIC_MZSCHEME
6954 else if (STRICMP(name, "mzscheme") == 0)
6955 n = mzscheme_enabled(FALSE);
6956#endif
6957#ifdef DYNAMIC_RUBY
6958 else if (STRICMP(name, "ruby") == 0)
6959 n = ruby_enabled(FALSE);
6960#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006961#ifdef DYNAMIC_PYTHON
6962 else if (STRICMP(name, "python") == 0)
6963 n = python_enabled(FALSE);
6964#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006965#ifdef DYNAMIC_PYTHON3
6966 else if (STRICMP(name, "python3") == 0)
6967 n = python3_enabled(FALSE);
6968#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006969#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6970 else if (STRICMP(name, "pythonx") == 0)
6971 {
6972# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6973 if (p_pyx == 0)
6974 n = python3_enabled(FALSE) || python_enabled(FALSE);
6975 else if (p_pyx == 3)
6976 n = python3_enabled(FALSE);
6977 else if (p_pyx == 2)
6978 n = python_enabled(FALSE);
6979# elif defined(DYNAMIC_PYTHON)
6980 n = python_enabled(FALSE);
6981# elif defined(DYNAMIC_PYTHON3)
6982 n = python3_enabled(FALSE);
6983# endif
6984 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006985#endif
6986#ifdef DYNAMIC_PERL
6987 else if (STRICMP(name, "perl") == 0)
6988 n = perl_enabled(FALSE);
6989#endif
6990#ifdef FEAT_GUI
6991 else if (STRICMP(name, "gui_running") == 0)
6992 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006993# ifdef FEAT_BROWSE
6994 else if (STRICMP(name, "browse") == 0)
6995 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6996# endif
6997#endif
6998#ifdef FEAT_SYN_HL
6999 else if (STRICMP(name, "syntax_items") == 0)
7000 n = syntax_present(curwin);
7001#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007002#ifdef FEAT_VTP
7003 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007004 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007005#endif
7006#ifdef FEAT_NETBEANS_INTG
7007 else if (STRICMP(name, "netbeans_enabled") == 0)
7008 n = netbeans_active();
7009#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007010#ifdef FEAT_MOUSE_GPM
7011 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7012 n = gpm_enabled();
7013#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007014#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007015 else if (STRICMP(name, "terminal") == 0)
7016 n = terminal_enabled();
7017#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007018#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007019 else if (STRICMP(name, "conpty") == 0)
7020 n = use_conpty();
7021#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007022 }
7023
7024 rettv->vval.v_number = n;
7025}
7026
7027/*
7028 * "has_key()" function
7029 */
7030 static void
7031f_has_key(typval_T *argvars, typval_T *rettv)
7032{
7033 if (argvars[0].v_type != VAR_DICT)
7034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007035 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007036 return;
7037 }
7038 if (argvars[0].vval.v_dict == NULL)
7039 return;
7040
7041 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007042 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007043}
7044
7045/*
7046 * "haslocaldir()" function
7047 */
7048 static void
7049f_haslocaldir(typval_T *argvars, typval_T *rettv)
7050{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007051 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007052 win_T *wp = NULL;
7053
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007054 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7055
7056 // Check for window-local and tab-local directories
7057 if (wp != NULL && wp->w_localdir != NULL)
7058 rettv->vval.v_number = 1;
7059 else if (tp != NULL && tp->tp_localdir != NULL)
7060 rettv->vval.v_number = 2;
7061 else
7062 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007063}
7064
7065/*
7066 * "hasmapto()" function
7067 */
7068 static void
7069f_hasmapto(typval_T *argvars, typval_T *rettv)
7070{
7071 char_u *name;
7072 char_u *mode;
7073 char_u buf[NUMBUFLEN];
7074 int abbr = FALSE;
7075
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007076 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007077 if (argvars[1].v_type == VAR_UNKNOWN)
7078 mode = (char_u *)"nvo";
7079 else
7080 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007081 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007083 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007084 }
7085
7086 if (map_to_exists(name, mode, abbr))
7087 rettv->vval.v_number = TRUE;
7088 else
7089 rettv->vval.v_number = FALSE;
7090}
7091
7092/*
7093 * "histadd()" function
7094 */
7095 static void
7096f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7097{
7098#ifdef FEAT_CMDHIST
7099 int histype;
7100 char_u *str;
7101 char_u buf[NUMBUFLEN];
7102#endif
7103
7104 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007105 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007106 return;
7107#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007108 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007109 histype = str != NULL ? get_histtype(str) : -1;
7110 if (histype >= 0)
7111 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007112 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007113 if (*str != NUL)
7114 {
7115 init_history();
7116 add_to_history(histype, str, FALSE, NUL);
7117 rettv->vval.v_number = TRUE;
7118 return;
7119 }
7120 }
7121#endif
7122}
7123
7124/*
7125 * "histdel()" function
7126 */
7127 static void
7128f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7129{
7130#ifdef FEAT_CMDHIST
7131 int n;
7132 char_u buf[NUMBUFLEN];
7133 char_u *str;
7134
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007135 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007136 if (str == NULL)
7137 n = 0;
7138 else if (argvars[1].v_type == VAR_UNKNOWN)
7139 /* only one argument: clear entire history */
7140 n = clr_history(get_histtype(str));
7141 else if (argvars[1].v_type == VAR_NUMBER)
7142 /* index given: remove that entry */
7143 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007144 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007145 else
7146 /* string given: remove all matching entries */
7147 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007148 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007149 rettv->vval.v_number = n;
7150#endif
7151}
7152
7153/*
7154 * "histget()" function
7155 */
7156 static void
7157f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7158{
7159#ifdef FEAT_CMDHIST
7160 int type;
7161 int idx;
7162 char_u *str;
7163
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007164 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007165 if (str == NULL)
7166 rettv->vval.v_string = NULL;
7167 else
7168 {
7169 type = get_histtype(str);
7170 if (argvars[1].v_type == VAR_UNKNOWN)
7171 idx = get_history_idx(type);
7172 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007173 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007174 /* -1 on type error */
7175 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7176 }
7177#else
7178 rettv->vval.v_string = NULL;
7179#endif
7180 rettv->v_type = VAR_STRING;
7181}
7182
7183/*
7184 * "histnr()" function
7185 */
7186 static void
7187f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7188{
7189 int i;
7190
7191#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007192 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007193
7194 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7195 if (i >= HIST_CMD && i < HIST_COUNT)
7196 i = get_history_idx(i);
7197 else
7198#endif
7199 i = -1;
7200 rettv->vval.v_number = i;
7201}
7202
7203/*
7204 * "highlightID(name)" function
7205 */
7206 static void
7207f_hlID(typval_T *argvars, typval_T *rettv)
7208{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007209 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007210}
7211
7212/*
7213 * "highlight_exists()" function
7214 */
7215 static void
7216f_hlexists(typval_T *argvars, typval_T *rettv)
7217{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007218 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007219}
7220
7221/*
7222 * "hostname()" function
7223 */
7224 static void
7225f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7226{
7227 char_u hostname[256];
7228
7229 mch_get_host_name(hostname, 256);
7230 rettv->v_type = VAR_STRING;
7231 rettv->vval.v_string = vim_strsave(hostname);
7232}
7233
7234/*
7235 * iconv() function
7236 */
7237 static void
7238f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7239{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007240 char_u buf1[NUMBUFLEN];
7241 char_u buf2[NUMBUFLEN];
7242 char_u *from, *to, *str;
7243 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007244
7245 rettv->v_type = VAR_STRING;
7246 rettv->vval.v_string = NULL;
7247
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007248 str = tv_get_string(&argvars[0]);
7249 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7250 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007251 vimconv.vc_type = CONV_NONE;
7252 convert_setup(&vimconv, from, to);
7253
7254 /* If the encodings are equal, no conversion needed. */
7255 if (vimconv.vc_type == CONV_NONE)
7256 rettv->vval.v_string = vim_strsave(str);
7257 else
7258 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7259
7260 convert_setup(&vimconv, NULL, NULL);
7261 vim_free(from);
7262 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007263}
7264
7265/*
7266 * "indent()" function
7267 */
7268 static void
7269f_indent(typval_T *argvars, typval_T *rettv)
7270{
7271 linenr_T lnum;
7272
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007273 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007274 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7275 rettv->vval.v_number = get_indent_lnum(lnum);
7276 else
7277 rettv->vval.v_number = -1;
7278}
7279
7280/*
7281 * "index()" function
7282 */
7283 static void
7284f_index(typval_T *argvars, typval_T *rettv)
7285{
7286 list_T *l;
7287 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007288 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007289 long idx = 0;
7290 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007291 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007292
7293 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007294 if (argvars[0].v_type == VAR_BLOB)
7295 {
7296 typval_T tv;
7297 int start = 0;
7298
7299 if (argvars[2].v_type != VAR_UNKNOWN)
7300 {
7301 start = tv_get_number_chk(&argvars[2], &error);
7302 if (error)
7303 return;
7304 }
7305 b = argvars[0].vval.v_blob;
7306 if (b == NULL)
7307 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007308 if (start < 0)
7309 {
7310 start = blob_len(b) + start;
7311 if (start < 0)
7312 start = 0;
7313 }
7314
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007315 for (idx = start; idx < blob_len(b); ++idx)
7316 {
7317 tv.v_type = VAR_NUMBER;
7318 tv.vval.v_number = blob_get(b, idx);
7319 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7320 {
7321 rettv->vval.v_number = idx;
7322 return;
7323 }
7324 }
7325 return;
7326 }
7327 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007328 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007329 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007330 return;
7331 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007332
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007333 l = argvars[0].vval.v_list;
7334 if (l != NULL)
7335 {
7336 item = l->lv_first;
7337 if (argvars[2].v_type != VAR_UNKNOWN)
7338 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007339 /* Start at specified item. Use the cached index that list_find()
7340 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007341 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007342 idx = l->lv_idx;
7343 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007344 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007345 if (error)
7346 item = NULL;
7347 }
7348
7349 for ( ; item != NULL; item = item->li_next, ++idx)
7350 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7351 {
7352 rettv->vval.v_number = idx;
7353 break;
7354 }
7355 }
7356}
7357
7358static int inputsecret_flag = 0;
7359
7360/*
7361 * "input()" function
7362 * Also handles inputsecret() when inputsecret is set.
7363 */
7364 static void
7365f_input(typval_T *argvars, typval_T *rettv)
7366{
7367 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7368}
7369
7370/*
7371 * "inputdialog()" function
7372 */
7373 static void
7374f_inputdialog(typval_T *argvars, typval_T *rettv)
7375{
7376#if defined(FEAT_GUI_TEXTDIALOG)
7377 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7378 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7379 {
7380 char_u *message;
7381 char_u buf[NUMBUFLEN];
7382 char_u *defstr = (char_u *)"";
7383
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007384 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007385 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007386 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007387 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7388 else
7389 IObuff[0] = NUL;
7390 if (message != NULL && defstr != NULL
7391 && do_dialog(VIM_QUESTION, NULL, message,
7392 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7393 rettv->vval.v_string = vim_strsave(IObuff);
7394 else
7395 {
7396 if (message != NULL && defstr != NULL
7397 && argvars[1].v_type != VAR_UNKNOWN
7398 && argvars[2].v_type != VAR_UNKNOWN)
7399 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007401 else
7402 rettv->vval.v_string = NULL;
7403 }
7404 rettv->v_type = VAR_STRING;
7405 }
7406 else
7407#endif
7408 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7409}
7410
7411/*
7412 * "inputlist()" function
7413 */
7414 static void
7415f_inputlist(typval_T *argvars, typval_T *rettv)
7416{
7417 listitem_T *li;
7418 int selected;
7419 int mouse_used;
7420
7421#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007422 /* While starting up, there is no place to enter text. When running tests
7423 * with --not-a-term we assume feedkeys() will be used. */
7424 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007425 return;
7426#endif
7427 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007430 return;
7431 }
7432
7433 msg_start();
7434 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7435 lines_left = Rows; /* avoid more prompt */
7436 msg_scroll = TRUE;
7437 msg_clr_eos();
7438
7439 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7440 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007441 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007442 msg_putchar('\n');
7443 }
7444
7445 /* Ask for choice. */
7446 selected = prompt_for_number(&mouse_used);
7447 if (mouse_used)
7448 selected -= lines_left;
7449
7450 rettv->vval.v_number = selected;
7451}
7452
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007453static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7454
7455/*
7456 * "inputrestore()" function
7457 */
7458 static void
7459f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7460{
7461 if (ga_userinput.ga_len > 0)
7462 {
7463 --ga_userinput.ga_len;
7464 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7465 + ga_userinput.ga_len);
7466 /* default return is zero == OK */
7467 }
7468 else if (p_verbose > 1)
7469 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007470 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007471 rettv->vval.v_number = 1; /* Failed */
7472 }
7473}
7474
7475/*
7476 * "inputsave()" function
7477 */
7478 static void
7479f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7480{
7481 /* Add an entry to the stack of typeahead storage. */
7482 if (ga_grow(&ga_userinput, 1) == OK)
7483 {
7484 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7485 + ga_userinput.ga_len);
7486 ++ga_userinput.ga_len;
7487 /* default return is zero == OK */
7488 }
7489 else
7490 rettv->vval.v_number = 1; /* Failed */
7491}
7492
7493/*
7494 * "inputsecret()" function
7495 */
7496 static void
7497f_inputsecret(typval_T *argvars, typval_T *rettv)
7498{
7499 ++cmdline_star;
7500 ++inputsecret_flag;
7501 f_input(argvars, rettv);
7502 --cmdline_star;
7503 --inputsecret_flag;
7504}
7505
7506/*
7507 * "insert()" function
7508 */
7509 static void
7510f_insert(typval_T *argvars, typval_T *rettv)
7511{
7512 long before = 0;
7513 listitem_T *item;
7514 list_T *l;
7515 int error = FALSE;
7516
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007517 if (argvars[0].v_type == VAR_BLOB)
7518 {
7519 int val, len;
7520 char_u *p;
7521
7522 len = blob_len(argvars[0].vval.v_blob);
7523 if (argvars[2].v_type != VAR_UNKNOWN)
7524 {
7525 before = (long)tv_get_number_chk(&argvars[2], &error);
7526 if (error)
7527 return; // type error; errmsg already given
7528 if (before < 0 || before > len)
7529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007530 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007531 return;
7532 }
7533 }
7534 val = tv_get_number_chk(&argvars[1], &error);
7535 if (error)
7536 return;
7537 if (val < 0 || val > 255)
7538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007539 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007540 return;
7541 }
7542
7543 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7544 return;
7545 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7546 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7547 *(p + before) = val;
7548 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7549
7550 copy_tv(&argvars[0], rettv);
7551 }
7552 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007553 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007554 else if ((l = argvars[0].vval.v_list) != NULL
7555 && !var_check_lock(l->lv_lock,
7556 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007557 {
7558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007559 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007560 if (error)
7561 return; /* type error; errmsg already given */
7562
7563 if (before == l->lv_len)
7564 item = NULL;
7565 else
7566 {
7567 item = list_find(l, before);
7568 if (item == NULL)
7569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007570 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007571 l = NULL;
7572 }
7573 }
7574 if (l != NULL)
7575 {
7576 list_insert_tv(l, &argvars[1], item);
7577 copy_tv(&argvars[0], rettv);
7578 }
7579 }
7580}
7581
7582/*
7583 * "invert(expr)" function
7584 */
7585 static void
7586f_invert(typval_T *argvars, typval_T *rettv)
7587{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007588 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007589}
7590
7591/*
7592 * "isdirectory()" function
7593 */
7594 static void
7595f_isdirectory(typval_T *argvars, typval_T *rettv)
7596{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007597 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007598}
7599
7600/*
7601 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7602 * or it refers to a List or Dictionary that is locked.
7603 */
7604 static int
7605tv_islocked(typval_T *tv)
7606{
7607 return (tv->v_lock & VAR_LOCKED)
7608 || (tv->v_type == VAR_LIST
7609 && tv->vval.v_list != NULL
7610 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7611 || (tv->v_type == VAR_DICT
7612 && tv->vval.v_dict != NULL
7613 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7614}
7615
7616/*
7617 * "islocked()" function
7618 */
7619 static void
7620f_islocked(typval_T *argvars, typval_T *rettv)
7621{
7622 lval_T lv;
7623 char_u *end;
7624 dictitem_T *di;
7625
7626 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007627 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007628 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007629 if (end != NULL && lv.ll_name != NULL)
7630 {
7631 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007632 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007633 else
7634 {
7635 if (lv.ll_tv == NULL)
7636 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007637 di = find_var(lv.ll_name, NULL, TRUE);
7638 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007639 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007640 /* Consider a variable locked when:
7641 * 1. the variable itself is locked
7642 * 2. the value of the variable is locked.
7643 * 3. the List or Dict value is locked.
7644 */
7645 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7646 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 }
7648 }
7649 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007650 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007651 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007653 else if (lv.ll_list != NULL)
7654 /* List item. */
7655 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7656 else
7657 /* Dictionary item. */
7658 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7659 }
7660 }
7661
7662 clear_lval(&lv);
7663}
7664
7665#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7666/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007667 * "isinf()" function
7668 */
7669 static void
7670f_isinf(typval_T *argvars, typval_T *rettv)
7671{
7672 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7673 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7674}
7675
7676/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007677 * "isnan()" function
7678 */
7679 static void
7680f_isnan(typval_T *argvars, typval_T *rettv)
7681{
7682 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7683 && isnan(argvars[0].vval.v_float);
7684}
7685#endif
7686
7687/*
7688 * "items(dict)" function
7689 */
7690 static void
7691f_items(typval_T *argvars, typval_T *rettv)
7692{
7693 dict_list(argvars, rettv, 2);
7694}
7695
7696#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7697/*
7698 * Get the job from the argument.
7699 * Returns NULL if the job is invalid.
7700 */
7701 static job_T *
7702get_job_arg(typval_T *tv)
7703{
7704 job_T *job;
7705
7706 if (tv->v_type != VAR_JOB)
7707 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007708 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 return NULL;
7710 }
7711 job = tv->vval.v_job;
7712
7713 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007714 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007715 return job;
7716}
7717
7718/*
7719 * "job_getchannel()" function
7720 */
7721 static void
7722f_job_getchannel(typval_T *argvars, typval_T *rettv)
7723{
7724 job_T *job = get_job_arg(&argvars[0]);
7725
7726 if (job != NULL)
7727 {
7728 rettv->v_type = VAR_CHANNEL;
7729 rettv->vval.v_channel = job->jv_channel;
7730 if (job->jv_channel != NULL)
7731 ++job->jv_channel->ch_refcount;
7732 }
7733}
7734
7735/*
7736 * "job_info()" function
7737 */
7738 static void
7739f_job_info(typval_T *argvars, typval_T *rettv)
7740{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007741 if (argvars[0].v_type != VAR_UNKNOWN)
7742 {
7743 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007744
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007745 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7746 job_info(job, rettv->vval.v_dict);
7747 }
7748 else if (rettv_list_alloc(rettv) == OK)
7749 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007750}
7751
7752/*
7753 * "job_setoptions()" function
7754 */
7755 static void
7756f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7757{
7758 job_T *job = get_job_arg(&argvars[0]);
7759 jobopt_T opt;
7760
7761 if (job == NULL)
7762 return;
7763 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007764 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007765 job_set_options(job, &opt);
7766 free_job_options(&opt);
7767}
7768
7769/*
7770 * "job_start()" function
7771 */
7772 static void
7773f_job_start(typval_T *argvars, typval_T *rettv)
7774{
7775 rettv->v_type = VAR_JOB;
7776 if (check_restricted() || check_secure())
7777 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007778 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007779}
7780
7781/*
7782 * "job_status()" function
7783 */
7784 static void
7785f_job_status(typval_T *argvars, typval_T *rettv)
7786{
7787 job_T *job = get_job_arg(&argvars[0]);
7788
7789 if (job != NULL)
7790 {
7791 rettv->v_type = VAR_STRING;
7792 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7793 }
7794}
7795
7796/*
7797 * "job_stop()" function
7798 */
7799 static void
7800f_job_stop(typval_T *argvars, typval_T *rettv)
7801{
7802 job_T *job = get_job_arg(&argvars[0]);
7803
7804 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007805 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007806}
7807#endif
7808
7809/*
7810 * "join()" function
7811 */
7812 static void
7813f_join(typval_T *argvars, typval_T *rettv)
7814{
7815 garray_T ga;
7816 char_u *sep;
7817
7818 if (argvars[0].v_type != VAR_LIST)
7819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007820 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007821 return;
7822 }
7823 if (argvars[0].vval.v_list == NULL)
7824 return;
7825 if (argvars[1].v_type == VAR_UNKNOWN)
7826 sep = (char_u *)" ";
7827 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007828 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007829
7830 rettv->v_type = VAR_STRING;
7831
7832 if (sep != NULL)
7833 {
7834 ga_init2(&ga, (int)sizeof(char), 80);
7835 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7836 ga_append(&ga, NUL);
7837 rettv->vval.v_string = (char_u *)ga.ga_data;
7838 }
7839 else
7840 rettv->vval.v_string = NULL;
7841}
7842
7843/*
7844 * "js_decode()" function
7845 */
7846 static void
7847f_js_decode(typval_T *argvars, typval_T *rettv)
7848{
7849 js_read_T reader;
7850
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007851 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007852 reader.js_fill = NULL;
7853 reader.js_used = 0;
7854 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007855 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007856}
7857
7858/*
7859 * "js_encode()" function
7860 */
7861 static void
7862f_js_encode(typval_T *argvars, typval_T *rettv)
7863{
7864 rettv->v_type = VAR_STRING;
7865 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7866}
7867
7868/*
7869 * "json_decode()" function
7870 */
7871 static void
7872f_json_decode(typval_T *argvars, typval_T *rettv)
7873{
7874 js_read_T reader;
7875
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007876 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007877 reader.js_fill = NULL;
7878 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007879 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880}
7881
7882/*
7883 * "json_encode()" function
7884 */
7885 static void
7886f_json_encode(typval_T *argvars, typval_T *rettv)
7887{
7888 rettv->v_type = VAR_STRING;
7889 rettv->vval.v_string = json_encode(&argvars[0], 0);
7890}
7891
7892/*
7893 * "keys()" function
7894 */
7895 static void
7896f_keys(typval_T *argvars, typval_T *rettv)
7897{
7898 dict_list(argvars, rettv, 0);
7899}
7900
7901/*
7902 * "last_buffer_nr()" function.
7903 */
7904 static void
7905f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7906{
7907 int n = 0;
7908 buf_T *buf;
7909
Bram Moolenaar29323592016-07-24 22:04:11 +02007910 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007911 if (n < buf->b_fnum)
7912 n = buf->b_fnum;
7913
7914 rettv->vval.v_number = n;
7915}
7916
7917/*
7918 * "len()" function
7919 */
7920 static void
7921f_len(typval_T *argvars, typval_T *rettv)
7922{
7923 switch (argvars[0].v_type)
7924 {
7925 case VAR_STRING:
7926 case VAR_NUMBER:
7927 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007928 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007929 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007930 case VAR_BLOB:
7931 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7932 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007933 case VAR_LIST:
7934 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7935 break;
7936 case VAR_DICT:
7937 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7938 break;
7939 case VAR_UNKNOWN:
7940 case VAR_SPECIAL:
7941 case VAR_FLOAT:
7942 case VAR_FUNC:
7943 case VAR_PARTIAL:
7944 case VAR_JOB:
7945 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007946 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007947 break;
7948 }
7949}
7950
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007951 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007952libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007953{
7954#ifdef FEAT_LIBCALL
7955 char_u *string_in;
7956 char_u **string_result;
7957 int nr_result;
7958#endif
7959
7960 rettv->v_type = type;
7961 if (type != VAR_NUMBER)
7962 rettv->vval.v_string = NULL;
7963
7964 if (check_restricted() || check_secure())
7965 return;
7966
7967#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007968 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007969 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7970 {
7971 string_in = NULL;
7972 if (argvars[2].v_type == VAR_STRING)
7973 string_in = argvars[2].vval.v_string;
7974 if (type == VAR_NUMBER)
7975 string_result = NULL;
7976 else
7977 string_result = &rettv->vval.v_string;
7978 if (mch_libcall(argvars[0].vval.v_string,
7979 argvars[1].vval.v_string,
7980 string_in,
7981 argvars[2].vval.v_number,
7982 string_result,
7983 &nr_result) == OK
7984 && type == VAR_NUMBER)
7985 rettv->vval.v_number = nr_result;
7986 }
7987#endif
7988}
7989
7990/*
7991 * "libcall()" function
7992 */
7993 static void
7994f_libcall(typval_T *argvars, typval_T *rettv)
7995{
7996 libcall_common(argvars, rettv, VAR_STRING);
7997}
7998
7999/*
8000 * "libcallnr()" function
8001 */
8002 static void
8003f_libcallnr(typval_T *argvars, typval_T *rettv)
8004{
8005 libcall_common(argvars, rettv, VAR_NUMBER);
8006}
8007
8008/*
8009 * "line(string)" function
8010 */
8011 static void
8012f_line(typval_T *argvars, typval_T *rettv)
8013{
8014 linenr_T lnum = 0;
8015 pos_T *fp;
8016 int fnum;
8017
8018 fp = var2fpos(&argvars[0], TRUE, &fnum);
8019 if (fp != NULL)
8020 lnum = fp->lnum;
8021 rettv->vval.v_number = lnum;
8022}
8023
8024/*
8025 * "line2byte(lnum)" function
8026 */
8027 static void
8028f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8029{
8030#ifndef FEAT_BYTEOFF
8031 rettv->vval.v_number = -1;
8032#else
8033 linenr_T lnum;
8034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008035 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008036 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8037 rettv->vval.v_number = -1;
8038 else
8039 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8040 if (rettv->vval.v_number >= 0)
8041 ++rettv->vval.v_number;
8042#endif
8043}
8044
8045/*
8046 * "lispindent(lnum)" function
8047 */
8048 static void
8049f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8050{
8051#ifdef FEAT_LISP
8052 pos_T pos;
8053 linenr_T lnum;
8054
8055 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008056 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008057 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8058 {
8059 curwin->w_cursor.lnum = lnum;
8060 rettv->vval.v_number = get_lisp_indent();
8061 curwin->w_cursor = pos;
8062 }
8063 else
8064#endif
8065 rettv->vval.v_number = -1;
8066}
8067
8068/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008069 * "list2str()" function
8070 */
8071 static void
8072f_list2str(typval_T *argvars, typval_T *rettv)
8073{
8074 list_T *l;
8075 listitem_T *li;
8076 garray_T ga;
8077 int utf8 = FALSE;
8078
8079 rettv->v_type = VAR_STRING;
8080 rettv->vval.v_string = NULL;
8081 if (argvars[0].v_type != VAR_LIST)
8082 {
8083 emsg(_(e_invarg));
8084 return;
8085 }
8086
8087 l = argvars[0].vval.v_list;
8088 if (l == NULL)
8089 return; // empty list results in empty string
8090
8091 if (argvars[1].v_type != VAR_UNKNOWN)
8092 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8093
8094 ga_init2(&ga, 1, 80);
8095 if (has_mbyte || utf8)
8096 {
8097 char_u buf[MB_MAXBYTES + 1];
8098 int (*char2bytes)(int, char_u *);
8099
8100 if (utf8 || enc_utf8)
8101 char2bytes = utf_char2bytes;
8102 else
8103 char2bytes = mb_char2bytes;
8104
8105 for (li = l->lv_first; li != NULL; li = li->li_next)
8106 {
8107 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8108 ga_concat(&ga, buf);
8109 }
8110 ga_append(&ga, NUL);
8111 }
8112 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8113 {
8114 for (li = l->lv_first; li != NULL; li = li->li_next)
8115 ga_append(&ga, tv_get_number(&li->li_tv));
8116 ga_append(&ga, NUL);
8117 }
8118
8119 rettv->v_type = VAR_STRING;
8120 rettv->vval.v_string = ga.ga_data;
8121}
8122
8123/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008124 * "localtime()" function
8125 */
8126 static void
8127f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8128{
8129 rettv->vval.v_number = (varnumber_T)time(NULL);
8130}
8131
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008132 static void
8133get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8134{
8135 char_u *keys;
8136 char_u *which;
8137 char_u buf[NUMBUFLEN];
8138 char_u *keys_buf = NULL;
8139 char_u *rhs;
8140 int mode;
8141 int abbr = FALSE;
8142 int get_dict = FALSE;
8143 mapblock_T *mp;
8144 int buffer_local;
8145
8146 /* return empty string for failure */
8147 rettv->v_type = VAR_STRING;
8148 rettv->vval.v_string = NULL;
8149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008150 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008151 if (*keys == NUL)
8152 return;
8153
8154 if (argvars[1].v_type != VAR_UNKNOWN)
8155 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008156 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008157 if (argvars[2].v_type != VAR_UNKNOWN)
8158 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008159 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008160 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008161 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008162 }
8163 }
8164 else
8165 which = (char_u *)"";
8166 if (which == NULL)
8167 return;
8168
8169 mode = get_map_mode(&which, 0);
8170
8171 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8172 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8173 vim_free(keys_buf);
8174
8175 if (!get_dict)
8176 {
8177 /* Return a string. */
8178 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008179 {
8180 if (*rhs == NUL)
8181 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8182 else
8183 rettv->vval.v_string = str2special_save(rhs, FALSE);
8184 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008185
8186 }
8187 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8188 {
8189 /* Return a dictionary. */
8190 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8191 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8192 dict_T *dict = rettv->vval.v_dict;
8193
Bram Moolenaare0be1672018-07-08 16:50:37 +02008194 dict_add_string(dict, "lhs", lhs);
8195 dict_add_string(dict, "rhs", mp->m_orig_str);
8196 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8197 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8198 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008199 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8200 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008201 dict_add_number(dict, "buffer", (long)buffer_local);
8202 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8203 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008204
8205 vim_free(lhs);
8206 vim_free(mapmode);
8207 }
8208}
8209
8210#ifdef FEAT_FLOAT
8211/*
8212 * "log()" function
8213 */
8214 static void
8215f_log(typval_T *argvars, typval_T *rettv)
8216{
8217 float_T f = 0.0;
8218
8219 rettv->v_type = VAR_FLOAT;
8220 if (get_float_arg(argvars, &f) == OK)
8221 rettv->vval.v_float = log(f);
8222 else
8223 rettv->vval.v_float = 0.0;
8224}
8225
8226/*
8227 * "log10()" function
8228 */
8229 static void
8230f_log10(typval_T *argvars, typval_T *rettv)
8231{
8232 float_T f = 0.0;
8233
8234 rettv->v_type = VAR_FLOAT;
8235 if (get_float_arg(argvars, &f) == OK)
8236 rettv->vval.v_float = log10(f);
8237 else
8238 rettv->vval.v_float = 0.0;
8239}
8240#endif
8241
8242#ifdef FEAT_LUA
8243/*
8244 * "luaeval()" function
8245 */
8246 static void
8247f_luaeval(typval_T *argvars, typval_T *rettv)
8248{
8249 char_u *str;
8250 char_u buf[NUMBUFLEN];
8251
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008252 if (check_restricted() || check_secure())
8253 return;
8254
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008255 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008256 do_luaeval(str, argvars + 1, rettv);
8257}
8258#endif
8259
8260/*
8261 * "map()" function
8262 */
8263 static void
8264f_map(typval_T *argvars, typval_T *rettv)
8265{
8266 filter_map(argvars, rettv, TRUE);
8267}
8268
8269/*
8270 * "maparg()" function
8271 */
8272 static void
8273f_maparg(typval_T *argvars, typval_T *rettv)
8274{
8275 get_maparg(argvars, rettv, TRUE);
8276}
8277
8278/*
8279 * "mapcheck()" function
8280 */
8281 static void
8282f_mapcheck(typval_T *argvars, typval_T *rettv)
8283{
8284 get_maparg(argvars, rettv, FALSE);
8285}
8286
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008287typedef enum
8288{
8289 MATCH_END, /* matchend() */
8290 MATCH_MATCH, /* match() */
8291 MATCH_STR, /* matchstr() */
8292 MATCH_LIST, /* matchlist() */
8293 MATCH_POS /* matchstrpos() */
8294} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008295
8296 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008297find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008298{
8299 char_u *str = NULL;
8300 long len = 0;
8301 char_u *expr = NULL;
8302 char_u *pat;
8303 regmatch_T regmatch;
8304 char_u patbuf[NUMBUFLEN];
8305 char_u strbuf[NUMBUFLEN];
8306 char_u *save_cpo;
8307 long start = 0;
8308 long nth = 1;
8309 colnr_T startcol = 0;
8310 int match = 0;
8311 list_T *l = NULL;
8312 listitem_T *li = NULL;
8313 long idx = 0;
8314 char_u *tofree = NULL;
8315
8316 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8317 save_cpo = p_cpo;
8318 p_cpo = (char_u *)"";
8319
8320 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008321 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008322 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008323 /* type MATCH_LIST: return empty list when there are no matches.
8324 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008325 if (rettv_list_alloc(rettv) == FAIL)
8326 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008327 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008328 && (list_append_string(rettv->vval.v_list,
8329 (char_u *)"", 0) == FAIL
8330 || list_append_number(rettv->vval.v_list,
8331 (varnumber_T)-1) == FAIL
8332 || list_append_number(rettv->vval.v_list,
8333 (varnumber_T)-1) == FAIL
8334 || list_append_number(rettv->vval.v_list,
8335 (varnumber_T)-1) == FAIL))
8336 {
8337 list_free(rettv->vval.v_list);
8338 rettv->vval.v_list = NULL;
8339 goto theend;
8340 }
8341 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008342 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008343 {
8344 rettv->v_type = VAR_STRING;
8345 rettv->vval.v_string = NULL;
8346 }
8347
8348 if (argvars[0].v_type == VAR_LIST)
8349 {
8350 if ((l = argvars[0].vval.v_list) == NULL)
8351 goto theend;
8352 li = l->lv_first;
8353 }
8354 else
8355 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008356 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008357 len = (long)STRLEN(str);
8358 }
8359
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008360 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008361 if (pat == NULL)
8362 goto theend;
8363
8364 if (argvars[2].v_type != VAR_UNKNOWN)
8365 {
8366 int error = FALSE;
8367
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008368 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008369 if (error)
8370 goto theend;
8371 if (l != NULL)
8372 {
8373 li = list_find(l, start);
8374 if (li == NULL)
8375 goto theend;
8376 idx = l->lv_idx; /* use the cached index */
8377 }
8378 else
8379 {
8380 if (start < 0)
8381 start = 0;
8382 if (start > len)
8383 goto theend;
8384 /* When "count" argument is there ignore matches before "start",
8385 * otherwise skip part of the string. Differs when pattern is "^"
8386 * or "\<". */
8387 if (argvars[3].v_type != VAR_UNKNOWN)
8388 startcol = start;
8389 else
8390 {
8391 str += start;
8392 len -= start;
8393 }
8394 }
8395
8396 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008397 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008398 if (error)
8399 goto theend;
8400 }
8401
8402 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8403 if (regmatch.regprog != NULL)
8404 {
8405 regmatch.rm_ic = p_ic;
8406
8407 for (;;)
8408 {
8409 if (l != NULL)
8410 {
8411 if (li == NULL)
8412 {
8413 match = FALSE;
8414 break;
8415 }
8416 vim_free(tofree);
8417 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8418 if (str == NULL)
8419 break;
8420 }
8421
8422 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8423
8424 if (match && --nth <= 0)
8425 break;
8426 if (l == NULL && !match)
8427 break;
8428
8429 /* Advance to just after the match. */
8430 if (l != NULL)
8431 {
8432 li = li->li_next;
8433 ++idx;
8434 }
8435 else
8436 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008437 startcol = (colnr_T)(regmatch.startp[0]
8438 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008439 if (startcol > (colnr_T)len
8440 || str + startcol <= regmatch.startp[0])
8441 {
8442 match = FALSE;
8443 break;
8444 }
8445 }
8446 }
8447
8448 if (match)
8449 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008450 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008451 {
8452 listitem_T *li1 = rettv->vval.v_list->lv_first;
8453 listitem_T *li2 = li1->li_next;
8454 listitem_T *li3 = li2->li_next;
8455 listitem_T *li4 = li3->li_next;
8456
8457 vim_free(li1->li_tv.vval.v_string);
8458 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8459 (int)(regmatch.endp[0] - regmatch.startp[0]));
8460 li3->li_tv.vval.v_number =
8461 (varnumber_T)(regmatch.startp[0] - expr);
8462 li4->li_tv.vval.v_number =
8463 (varnumber_T)(regmatch.endp[0] - expr);
8464 if (l != NULL)
8465 li2->li_tv.vval.v_number = (varnumber_T)idx;
8466 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008467 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008468 {
8469 int i;
8470
8471 /* return list with matched string and submatches */
8472 for (i = 0; i < NSUBEXP; ++i)
8473 {
8474 if (regmatch.endp[i] == NULL)
8475 {
8476 if (list_append_string(rettv->vval.v_list,
8477 (char_u *)"", 0) == FAIL)
8478 break;
8479 }
8480 else if (list_append_string(rettv->vval.v_list,
8481 regmatch.startp[i],
8482 (int)(regmatch.endp[i] - regmatch.startp[i]))
8483 == FAIL)
8484 break;
8485 }
8486 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008487 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008488 {
8489 /* return matched string */
8490 if (l != NULL)
8491 copy_tv(&li->li_tv, rettv);
8492 else
8493 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8494 (int)(regmatch.endp[0] - regmatch.startp[0]));
8495 }
8496 else if (l != NULL)
8497 rettv->vval.v_number = idx;
8498 else
8499 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008500 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008501 rettv->vval.v_number =
8502 (varnumber_T)(regmatch.startp[0] - str);
8503 else
8504 rettv->vval.v_number =
8505 (varnumber_T)(regmatch.endp[0] - str);
8506 rettv->vval.v_number += (varnumber_T)(str - expr);
8507 }
8508 }
8509 vim_regfree(regmatch.regprog);
8510 }
8511
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008512theend:
8513 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008514 /* matchstrpos() without a list: drop the second item. */
8515 listitem_remove(rettv->vval.v_list,
8516 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008517 vim_free(tofree);
8518 p_cpo = save_cpo;
8519}
8520
8521/*
8522 * "match()" function
8523 */
8524 static void
8525f_match(typval_T *argvars, typval_T *rettv)
8526{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008527 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008528}
8529
Bram Moolenaar95e51472018-07-28 16:55:56 +02008530#ifdef FEAT_SEARCH_EXTRA
8531 static int
8532matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8533{
8534 dictitem_T *di;
8535
8536 if (tv->v_type != VAR_DICT)
8537 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008538 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008539 return FAIL;
8540 }
8541
8542 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008543 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008544 (char_u *)"conceal", FALSE);
8545
8546 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8547 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008548 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008549 if (*win == NULL)
8550 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008551 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008552 return FAIL;
8553 }
8554 }
8555
8556 return OK;
8557}
8558#endif
8559
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008560/*
8561 * "matchadd()" function
8562 */
8563 static void
8564f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8565{
8566#ifdef FEAT_SEARCH_EXTRA
8567 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008568 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8569 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008570 int prio = 10; /* default priority */
8571 int id = -1;
8572 int error = FALSE;
8573 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008574 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008575
8576 rettv->vval.v_number = -1;
8577
8578 if (grp == NULL || pat == NULL)
8579 return;
8580 if (argvars[2].v_type != VAR_UNKNOWN)
8581 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008582 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008583 if (argvars[3].v_type != VAR_UNKNOWN)
8584 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008585 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008586 if (argvars[4].v_type != VAR_UNKNOWN
8587 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8588 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008589 }
8590 }
8591 if (error == TRUE)
8592 return;
8593 if (id >= 1 && id <= 3)
8594 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008595 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008596 return;
8597 }
8598
Bram Moolenaar95e51472018-07-28 16:55:56 +02008599 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008600 conceal_char);
8601#endif
8602}
8603
8604/*
8605 * "matchaddpos()" function
8606 */
8607 static void
8608f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8609{
8610#ifdef FEAT_SEARCH_EXTRA
8611 char_u buf[NUMBUFLEN];
8612 char_u *group;
8613 int prio = 10;
8614 int id = -1;
8615 int error = FALSE;
8616 list_T *l;
8617 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008618 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008619
8620 rettv->vval.v_number = -1;
8621
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008622 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008623 if (group == NULL)
8624 return;
8625
8626 if (argvars[1].v_type != VAR_LIST)
8627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008628 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008629 return;
8630 }
8631 l = argvars[1].vval.v_list;
8632 if (l == NULL)
8633 return;
8634
8635 if (argvars[2].v_type != VAR_UNKNOWN)
8636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008637 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 if (argvars[3].v_type != VAR_UNKNOWN)
8639 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008640 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008641
8642 if (argvars[4].v_type != VAR_UNKNOWN
8643 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8644 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008645 }
8646 }
8647 if (error == TRUE)
8648 return;
8649
8650 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8651 if (id == 1 || id == 2)
8652 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008653 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008654 return;
8655 }
8656
Bram Moolenaar95e51472018-07-28 16:55:56 +02008657 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008658 conceal_char);
8659#endif
8660}
8661
8662/*
8663 * "matcharg()" function
8664 */
8665 static void
8666f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8667{
8668 if (rettv_list_alloc(rettv) == OK)
8669 {
8670#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008671 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008672 matchitem_T *m;
8673
8674 if (id >= 1 && id <= 3)
8675 {
8676 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8677 {
8678 list_append_string(rettv->vval.v_list,
8679 syn_id2name(m->hlg_id), -1);
8680 list_append_string(rettv->vval.v_list, m->pattern, -1);
8681 }
8682 else
8683 {
8684 list_append_string(rettv->vval.v_list, NULL, -1);
8685 list_append_string(rettv->vval.v_list, NULL, -1);
8686 }
8687 }
8688#endif
8689 }
8690}
8691
8692/*
8693 * "matchdelete()" function
8694 */
8695 static void
8696f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8697{
8698#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008699 win_T *win = get_optional_window(argvars, 1);
8700
8701 if (win == NULL)
8702 rettv->vval.v_number = -1;
8703 else
8704 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008705 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008706#endif
8707}
8708
8709/*
8710 * "matchend()" function
8711 */
8712 static void
8713f_matchend(typval_T *argvars, typval_T *rettv)
8714{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008715 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008716}
8717
8718/*
8719 * "matchlist()" function
8720 */
8721 static void
8722f_matchlist(typval_T *argvars, typval_T *rettv)
8723{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008724 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008725}
8726
8727/*
8728 * "matchstr()" function
8729 */
8730 static void
8731f_matchstr(typval_T *argvars, typval_T *rettv)
8732{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008733 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734}
8735
8736/*
8737 * "matchstrpos()" function
8738 */
8739 static void
8740f_matchstrpos(typval_T *argvars, typval_T *rettv)
8741{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008742 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008743}
8744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008745 static void
8746max_min(typval_T *argvars, typval_T *rettv, int domax)
8747{
8748 varnumber_T n = 0;
8749 varnumber_T i;
8750 int error = FALSE;
8751
8752 if (argvars[0].v_type == VAR_LIST)
8753 {
8754 list_T *l;
8755 listitem_T *li;
8756
8757 l = argvars[0].vval.v_list;
8758 if (l != NULL)
8759 {
8760 li = l->lv_first;
8761 if (li != NULL)
8762 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008763 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008764 for (;;)
8765 {
8766 li = li->li_next;
8767 if (li == NULL)
8768 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008769 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008770 if (domax ? i > n : i < n)
8771 n = i;
8772 }
8773 }
8774 }
8775 }
8776 else if (argvars[0].v_type == VAR_DICT)
8777 {
8778 dict_T *d;
8779 int first = TRUE;
8780 hashitem_T *hi;
8781 int todo;
8782
8783 d = argvars[0].vval.v_dict;
8784 if (d != NULL)
8785 {
8786 todo = (int)d->dv_hashtab.ht_used;
8787 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8788 {
8789 if (!HASHITEM_EMPTY(hi))
8790 {
8791 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008792 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008793 if (first)
8794 {
8795 n = i;
8796 first = FALSE;
8797 }
8798 else if (domax ? i > n : i < n)
8799 n = i;
8800 }
8801 }
8802 }
8803 }
8804 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008805 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 rettv->vval.v_number = error ? 0 : n;
8807}
8808
8809/*
8810 * "max()" function
8811 */
8812 static void
8813f_max(typval_T *argvars, typval_T *rettv)
8814{
8815 max_min(argvars, rettv, TRUE);
8816}
8817
8818/*
8819 * "min()" function
8820 */
8821 static void
8822f_min(typval_T *argvars, typval_T *rettv)
8823{
8824 max_min(argvars, rettv, FALSE);
8825}
8826
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008827/*
8828 * Create the directory in which "dir" is located, and higher levels when
8829 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008830 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008831 */
8832 static int
8833mkdir_recurse(char_u *dir, int prot)
8834{
8835 char_u *p;
8836 char_u *updir;
8837 int r = FAIL;
8838
8839 /* Get end of directory name in "dir".
8840 * We're done when it's "/" or "c:/". */
8841 p = gettail_sep(dir);
8842 if (p <= get_past_head(dir))
8843 return OK;
8844
8845 /* If the directory exists we're done. Otherwise: create it.*/
8846 updir = vim_strnsave(dir, (int)(p - dir));
8847 if (updir == NULL)
8848 return FAIL;
8849 if (mch_isdir(updir))
8850 r = OK;
8851 else if (mkdir_recurse(updir, prot) == OK)
8852 r = vim_mkdir_emsg(updir, prot);
8853 vim_free(updir);
8854 return r;
8855}
8856
8857#ifdef vim_mkdir
8858/*
8859 * "mkdir()" function
8860 */
8861 static void
8862f_mkdir(typval_T *argvars, typval_T *rettv)
8863{
8864 char_u *dir;
8865 char_u buf[NUMBUFLEN];
8866 int prot = 0755;
8867
8868 rettv->vval.v_number = FAIL;
8869 if (check_restricted() || check_secure())
8870 return;
8871
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008872 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008873 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008874 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008875
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008876 if (*gettail(dir) == NUL)
8877 /* remove trailing slashes */
8878 *gettail_sep(dir) = NUL;
8879
8880 if (argvars[1].v_type != VAR_UNKNOWN)
8881 {
8882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008883 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008884 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008885 if (prot == -1)
8886 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008887 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008888 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008889 {
8890 if (mch_isdir(dir))
8891 {
8892 /* With the "p" flag it's OK if the dir already exists. */
8893 rettv->vval.v_number = OK;
8894 return;
8895 }
8896 mkdir_recurse(dir, prot);
8897 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008898 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008899 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008900}
8901#endif
8902
8903/*
8904 * "mode()" function
8905 */
8906 static void
8907f_mode(typval_T *argvars, typval_T *rettv)
8908{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008909 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008910
Bram Moolenaar612cc382018-07-29 15:34:26 +02008911 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008912
8913 if (time_for_testing == 93784)
8914 {
8915 /* Testing the two-character code. */
8916 buf[0] = 'x';
8917 buf[1] = '!';
8918 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008919#ifdef FEAT_TERMINAL
8920 else if (term_use_loop())
8921 buf[0] = 't';
8922#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008923 else if (VIsual_active)
8924 {
8925 if (VIsual_select)
8926 buf[0] = VIsual_mode + 's' - 'v';
8927 else
8928 buf[0] = VIsual_mode;
8929 }
8930 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8931 || State == CONFIRM)
8932 {
8933 buf[0] = 'r';
8934 if (State == ASKMORE)
8935 buf[1] = 'm';
8936 else if (State == CONFIRM)
8937 buf[1] = '?';
8938 }
8939 else if (State == EXTERNCMD)
8940 buf[0] = '!';
8941 else if (State & INSERT)
8942 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008943 if (State & VREPLACE_FLAG)
8944 {
8945 buf[0] = 'R';
8946 buf[1] = 'v';
8947 }
8948 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008949 {
8950 if (State & REPLACE_FLAG)
8951 buf[0] = 'R';
8952 else
8953 buf[0] = 'i';
8954#ifdef FEAT_INS_EXPAND
8955 if (ins_compl_active())
8956 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008957 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008958 buf[1] = 'x';
8959#endif
8960 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008961 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008962 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008963 {
8964 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008965 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008966 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008967 else if (exmode_active == EXMODE_NORMAL)
8968 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008969 }
8970 else
8971 {
8972 buf[0] = 'n';
8973 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008974 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008975 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008976 // to be able to detect force-linewise/blockwise/characterwise operations
8977 buf[2] = motion_force;
8978 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008979 else if (restart_edit == 'I' || restart_edit == 'R'
8980 || restart_edit == 'V')
8981 {
8982 buf[1] = 'i';
8983 buf[2] = restart_edit;
8984 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008985 }
8986
8987 /* Clear out the minor mode when the argument is not a non-zero number or
8988 * non-empty string. */
8989 if (!non_zero_arg(&argvars[0]))
8990 buf[1] = NUL;
8991
8992 rettv->vval.v_string = vim_strsave(buf);
8993 rettv->v_type = VAR_STRING;
8994}
8995
8996#if defined(FEAT_MZSCHEME) || defined(PROTO)
8997/*
8998 * "mzeval()" function
8999 */
9000 static void
9001f_mzeval(typval_T *argvars, typval_T *rettv)
9002{
9003 char_u *str;
9004 char_u buf[NUMBUFLEN];
9005
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009006 if (check_restricted() || check_secure())
9007 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009008 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009009 do_mzeval(str, rettv);
9010}
9011
9012 void
9013mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9014{
9015 typval_T argvars[3];
9016
9017 argvars[0].v_type = VAR_STRING;
9018 argvars[0].vval.v_string = name;
9019 copy_tv(args, &argvars[1]);
9020 argvars[2].v_type = VAR_UNKNOWN;
9021 f_call(argvars, rettv);
9022 clear_tv(&argvars[1]);
9023}
9024#endif
9025
9026/*
9027 * "nextnonblank()" function
9028 */
9029 static void
9030f_nextnonblank(typval_T *argvars, typval_T *rettv)
9031{
9032 linenr_T lnum;
9033
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009034 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009035 {
9036 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9037 {
9038 lnum = 0;
9039 break;
9040 }
9041 if (*skipwhite(ml_get(lnum)) != NUL)
9042 break;
9043 }
9044 rettv->vval.v_number = lnum;
9045}
9046
9047/*
9048 * "nr2char()" function
9049 */
9050 static void
9051f_nr2char(typval_T *argvars, typval_T *rettv)
9052{
9053 char_u buf[NUMBUFLEN];
9054
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009055 if (has_mbyte)
9056 {
9057 int utf8 = 0;
9058
9059 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009060 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009061 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009062 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009063 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009064 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009065 }
9066 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009067 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009068 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009069 buf[1] = NUL;
9070 }
9071 rettv->v_type = VAR_STRING;
9072 rettv->vval.v_string = vim_strsave(buf);
9073}
9074
9075/*
9076 * "or(expr, expr)" function
9077 */
9078 static void
9079f_or(typval_T *argvars, typval_T *rettv)
9080{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009081 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9082 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009083}
9084
9085/*
9086 * "pathshorten()" function
9087 */
9088 static void
9089f_pathshorten(typval_T *argvars, typval_T *rettv)
9090{
9091 char_u *p;
9092
9093 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009094 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009095 if (p == NULL)
9096 rettv->vval.v_string = NULL;
9097 else
9098 {
9099 p = vim_strsave(p);
9100 rettv->vval.v_string = p;
9101 if (p != NULL)
9102 shorten_dir(p);
9103 }
9104}
9105
9106#ifdef FEAT_PERL
9107/*
9108 * "perleval()" function
9109 */
9110 static void
9111f_perleval(typval_T *argvars, typval_T *rettv)
9112{
9113 char_u *str;
9114 char_u buf[NUMBUFLEN];
9115
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009116 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009117 do_perleval(str, rettv);
9118}
9119#endif
9120
9121#ifdef FEAT_FLOAT
9122/*
9123 * "pow()" function
9124 */
9125 static void
9126f_pow(typval_T *argvars, typval_T *rettv)
9127{
9128 float_T fx = 0.0, fy = 0.0;
9129
9130 rettv->v_type = VAR_FLOAT;
9131 if (get_float_arg(argvars, &fx) == OK
9132 && get_float_arg(&argvars[1], &fy) == OK)
9133 rettv->vval.v_float = pow(fx, fy);
9134 else
9135 rettv->vval.v_float = 0.0;
9136}
9137#endif
9138
9139/*
9140 * "prevnonblank()" function
9141 */
9142 static void
9143f_prevnonblank(typval_T *argvars, typval_T *rettv)
9144{
9145 linenr_T lnum;
9146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009147 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009148 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9149 lnum = 0;
9150 else
9151 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9152 --lnum;
9153 rettv->vval.v_number = lnum;
9154}
9155
9156/* This dummy va_list is here because:
9157 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9158 * - locally in the function results in a "used before set" warning
9159 * - using va_start() to initialize it gives "function with fixed args" error */
9160static va_list ap;
9161
9162/*
9163 * "printf()" function
9164 */
9165 static void
9166f_printf(typval_T *argvars, typval_T *rettv)
9167{
9168 char_u buf[NUMBUFLEN];
9169 int len;
9170 char_u *s;
9171 int saved_did_emsg = did_emsg;
9172 char *fmt;
9173
9174 rettv->v_type = VAR_STRING;
9175 rettv->vval.v_string = NULL;
9176
9177 /* Get the required length, allocate the buffer and do it for real. */
9178 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009179 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009180 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009181 if (!did_emsg)
9182 {
9183 s = alloc(len + 1);
9184 if (s != NULL)
9185 {
9186 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009187 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9188 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009189 }
9190 }
9191 did_emsg |= saved_did_emsg;
9192}
9193
Bram Moolenaarf2732452018-06-03 14:47:35 +02009194#ifdef FEAT_JOB_CHANNEL
9195/*
9196 * "prompt_setcallback({buffer}, {callback})" function
9197 */
9198 static void
9199f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9200{
9201 buf_T *buf;
9202 char_u *callback;
9203 partial_T *partial;
9204
9205 if (check_secure())
9206 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009207 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009208 if (buf == NULL)
9209 return;
9210
9211 callback = get_callback(&argvars[1], &partial);
9212 if (callback == NULL)
9213 return;
9214
9215 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
9216 if (partial == NULL)
9217 buf->b_prompt_callback = vim_strsave(callback);
9218 else
9219 /* pointer into the partial */
9220 buf->b_prompt_callback = callback;
9221 buf->b_prompt_partial = partial;
9222}
9223
9224/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009225 * "prompt_setinterrupt({buffer}, {callback})" function
9226 */
9227 static void
9228f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9229{
9230 buf_T *buf;
9231 char_u *callback;
9232 partial_T *partial;
9233
9234 if (check_secure())
9235 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009236 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009237 if (buf == NULL)
9238 return;
9239
9240 callback = get_callback(&argvars[1], &partial);
9241 if (callback == NULL)
9242 return;
9243
9244 free_callback(buf->b_prompt_interrupt, buf->b_prompt_int_partial);
9245 if (partial == NULL)
9246 buf->b_prompt_interrupt = vim_strsave(callback);
9247 else
9248 /* pointer into the partial */
9249 buf->b_prompt_interrupt = callback;
9250 buf->b_prompt_int_partial = partial;
9251}
9252
9253/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009254 * "prompt_setprompt({buffer}, {text})" function
9255 */
9256 static void
9257f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9258{
9259 buf_T *buf;
9260 char_u *text;
9261
9262 if (check_secure())
9263 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009264 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009265 if (buf == NULL)
9266 return;
9267
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009268 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009269 vim_free(buf->b_prompt_text);
9270 buf->b_prompt_text = vim_strsave(text);
9271}
9272#endif
9273
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009274/*
9275 * "pumvisible()" function
9276 */
9277 static void
9278f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9279{
9280#ifdef FEAT_INS_EXPAND
9281 if (pum_visible())
9282 rettv->vval.v_number = 1;
9283#endif
9284}
9285
9286#ifdef FEAT_PYTHON3
9287/*
9288 * "py3eval()" function
9289 */
9290 static void
9291f_py3eval(typval_T *argvars, typval_T *rettv)
9292{
9293 char_u *str;
9294 char_u buf[NUMBUFLEN];
9295
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009296 if (check_restricted() || check_secure())
9297 return;
9298
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009299 if (p_pyx == 0)
9300 p_pyx = 3;
9301
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009302 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009303 do_py3eval(str, rettv);
9304}
9305#endif
9306
9307#ifdef FEAT_PYTHON
9308/*
9309 * "pyeval()" function
9310 */
9311 static void
9312f_pyeval(typval_T *argvars, typval_T *rettv)
9313{
9314 char_u *str;
9315 char_u buf[NUMBUFLEN];
9316
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009317 if (check_restricted() || check_secure())
9318 return;
9319
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009320 if (p_pyx == 0)
9321 p_pyx = 2;
9322
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009323 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009324 do_pyeval(str, rettv);
9325}
9326#endif
9327
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009328#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9329/*
9330 * "pyxeval()" function
9331 */
9332 static void
9333f_pyxeval(typval_T *argvars, typval_T *rettv)
9334{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009335 if (check_restricted() || check_secure())
9336 return;
9337
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009338# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9339 init_pyxversion();
9340 if (p_pyx == 2)
9341 f_pyeval(argvars, rettv);
9342 else
9343 f_py3eval(argvars, rettv);
9344# elif defined(FEAT_PYTHON)
9345 f_pyeval(argvars, rettv);
9346# elif defined(FEAT_PYTHON3)
9347 f_py3eval(argvars, rettv);
9348# endif
9349}
9350#endif
9351
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009352/*
9353 * "range()" function
9354 */
9355 static void
9356f_range(typval_T *argvars, typval_T *rettv)
9357{
9358 varnumber_T start;
9359 varnumber_T end;
9360 varnumber_T stride = 1;
9361 varnumber_T i;
9362 int error = FALSE;
9363
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009364 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009365 if (argvars[1].v_type == VAR_UNKNOWN)
9366 {
9367 end = start - 1;
9368 start = 0;
9369 }
9370 else
9371 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009372 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009373 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009374 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009375 }
9376
9377 if (error)
9378 return; /* type error; errmsg already given */
9379 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009380 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009382 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009383 else
9384 {
9385 if (rettv_list_alloc(rettv) == OK)
9386 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9387 if (list_append_number(rettv->vval.v_list,
9388 (varnumber_T)i) == FAIL)
9389 break;
9390 }
9391}
9392
9393/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009394 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009395 */
9396 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009397readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009398{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009399 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009400 typval_T save_val;
9401 typval_T rettv;
9402 typval_T argv[2];
9403 int retval = 0;
9404 int error = FALSE;
9405
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009406 if (expr->v_type == VAR_UNKNOWN)
9407 return 1;
9408
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009409 prepare_vimvar(VV_VAL, &save_val);
9410 set_vim_var_string(VV_VAL, name, -1);
9411 argv[0].v_type = VAR_STRING;
9412 argv[0].vval.v_string = name;
9413
9414 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9415 goto theend;
9416
9417 retval = tv_get_number_chk(&rettv, &error);
9418 if (error)
9419 retval = -1;
9420 clear_tv(&rettv);
9421
9422theend:
9423 set_vim_var_string(VV_VAL, NULL, 0);
9424 restore_vimvar(VV_VAL, &save_val);
9425 return retval;
9426}
9427
9428/*
9429 * "readdir()" function
9430 */
9431 static void
9432f_readdir(typval_T *argvars, typval_T *rettv)
9433{
9434 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009435 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009436 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009437 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009438 garray_T ga;
9439 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009440
9441 if (rettv_list_alloc(rettv) == FAIL)
9442 return;
9443 path = tv_get_string(&argvars[0]);
9444 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009445
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009446 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9447 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009448 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009449 for (i = 0; i < ga.ga_len; i++)
9450 {
9451 p = ((char_u **)ga.ga_data)[i];
9452 list_append_string(rettv->vval.v_list, p, -1);
9453 }
9454 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009455 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009456}
9457
9458/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009459 * "readfile()" function
9460 */
9461 static void
9462f_readfile(typval_T *argvars, typval_T *rettv)
9463{
9464 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009465 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 int failed = FALSE;
9467 char_u *fname;
9468 FILE *fd;
9469 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9470 int io_size = sizeof(buf);
9471 int readlen; /* size of last fread() */
9472 char_u *prev = NULL; /* previously read bytes, if any */
9473 long prevlen = 0; /* length of data in prev */
9474 long prevsize = 0; /* size of prev buffer */
9475 long maxline = MAXLNUM;
9476 long cnt = 0;
9477 char_u *p; /* position in buf */
9478 char_u *start; /* start of current line */
9479
9480 if (argvars[1].v_type != VAR_UNKNOWN)
9481 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009482 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009483 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009484 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9485 blob = TRUE;
9486
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009487 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009488 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009489 }
9490
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009491 if (blob)
9492 {
9493 if (rettv_blob_alloc(rettv) == FAIL)
9494 return;
9495 }
9496 else
9497 {
9498 if (rettv_list_alloc(rettv) == FAIL)
9499 return;
9500 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009501
9502 /* Always open the file in binary mode, library functions have a mind of
9503 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009504 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009505 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009507 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009508 return;
9509 }
9510
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009511 if (blob)
9512 {
9513 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9514 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009515 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009516 blob_free(rettv->vval.v_blob);
9517 }
9518 fclose(fd);
9519 return;
9520 }
9521
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009522 while (cnt < maxline || maxline < 0)
9523 {
9524 readlen = (int)fread(buf, 1, io_size, fd);
9525
9526 /* This for loop processes what was read, but is also entered at end
9527 * of file so that either:
9528 * - an incomplete line gets written
9529 * - a "binary" file gets an empty line at the end if it ends in a
9530 * newline. */
9531 for (p = buf, start = buf;
9532 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9533 ++p)
9534 {
9535 if (*p == '\n' || readlen <= 0)
9536 {
9537 listitem_T *li;
9538 char_u *s = NULL;
9539 long_u len = p - start;
9540
9541 /* Finished a line. Remove CRs before NL. */
9542 if (readlen > 0 && !binary)
9543 {
9544 while (len > 0 && start[len - 1] == '\r')
9545 --len;
9546 /* removal may cross back to the "prev" string */
9547 if (len == 0)
9548 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9549 --prevlen;
9550 }
9551 if (prevlen == 0)
9552 s = vim_strnsave(start, (int)len);
9553 else
9554 {
9555 /* Change "prev" buffer to be the right size. This way
9556 * the bytes are only copied once, and very long lines are
9557 * allocated only once. */
9558 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9559 {
9560 mch_memmove(s + prevlen, start, len);
9561 s[prevlen + len] = NUL;
9562 prev = NULL; /* the list will own the string */
9563 prevlen = prevsize = 0;
9564 }
9565 }
9566 if (s == NULL)
9567 {
9568 do_outofmem_msg((long_u) prevlen + len + 1);
9569 failed = TRUE;
9570 break;
9571 }
9572
9573 if ((li = listitem_alloc()) == NULL)
9574 {
9575 vim_free(s);
9576 failed = TRUE;
9577 break;
9578 }
9579 li->li_tv.v_type = VAR_STRING;
9580 li->li_tv.v_lock = 0;
9581 li->li_tv.vval.v_string = s;
9582 list_append(rettv->vval.v_list, li);
9583
9584 start = p + 1; /* step over newline */
9585 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9586 break;
9587 }
9588 else if (*p == NUL)
9589 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009590 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9591 * when finding the BF and check the previous two bytes. */
9592 else if (*p == 0xbf && enc_utf8 && !binary)
9593 {
9594 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9595 * + 1, these may be in the "prev" string. */
9596 char_u back1 = p >= buf + 1 ? p[-1]
9597 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9598 char_u back2 = p >= buf + 2 ? p[-2]
9599 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9600 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9601
9602 if (back2 == 0xef && back1 == 0xbb)
9603 {
9604 char_u *dest = p - 2;
9605
9606 /* Usually a BOM is at the beginning of a file, and so at
9607 * the beginning of a line; then we can just step over it.
9608 */
9609 if (start == dest)
9610 start = p + 1;
9611 else
9612 {
9613 /* have to shuffle buf to close gap */
9614 int adjust_prevlen = 0;
9615
9616 if (dest < buf)
9617 {
9618 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9619 dest = buf;
9620 }
9621 if (readlen > p - buf + 1)
9622 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9623 readlen -= 3 - adjust_prevlen;
9624 prevlen -= adjust_prevlen;
9625 p = dest - 1;
9626 }
9627 }
9628 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009629 } /* for */
9630
9631 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9632 break;
9633 if (start < p)
9634 {
9635 /* There's part of a line in buf, store it in "prev". */
9636 if (p - start + prevlen >= prevsize)
9637 {
9638 /* need bigger "prev" buffer */
9639 char_u *newprev;
9640
9641 /* A common use case is ordinary text files and "prev" gets a
9642 * fragment of a line, so the first allocation is made
9643 * small, to avoid repeatedly 'allocing' large and
9644 * 'reallocing' small. */
9645 if (prevsize == 0)
9646 prevsize = (long)(p - start);
9647 else
9648 {
9649 long grow50pc = (prevsize * 3) / 2;
9650 long growmin = (long)((p - start) * 2 + prevlen);
9651 prevsize = grow50pc > growmin ? grow50pc : growmin;
9652 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009653 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009654 if (newprev == NULL)
9655 {
9656 do_outofmem_msg((long_u)prevsize);
9657 failed = TRUE;
9658 break;
9659 }
9660 prev = newprev;
9661 }
9662 /* Add the line part to end of "prev". */
9663 mch_memmove(prev + prevlen, start, p - start);
9664 prevlen += (long)(p - start);
9665 }
9666 } /* while */
9667
9668 /*
9669 * For a negative line count use only the lines at the end of the file,
9670 * free the rest.
9671 */
9672 if (!failed && maxline < 0)
9673 while (cnt > -maxline)
9674 {
9675 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9676 --cnt;
9677 }
9678
9679 if (failed)
9680 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009681 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009682 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009683 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009684 }
9685
9686 vim_free(prev);
9687 fclose(fd);
9688}
9689
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009690 static void
9691return_register(int regname, typval_T *rettv)
9692{
9693 char_u buf[2] = {0, 0};
9694
9695 buf[0] = (char_u)regname;
9696 rettv->v_type = VAR_STRING;
9697 rettv->vval.v_string = vim_strsave(buf);
9698}
9699
9700/*
9701 * "reg_executing()" function
9702 */
9703 static void
9704f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9705{
9706 return_register(reg_executing, rettv);
9707}
9708
9709/*
9710 * "reg_recording()" function
9711 */
9712 static void
9713f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9714{
9715 return_register(reg_recording, rettv);
9716}
9717
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009718#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009719/*
9720 * Convert a List to proftime_T.
9721 * Return FAIL when there is something wrong.
9722 */
9723 static int
9724list2proftime(typval_T *arg, proftime_T *tm)
9725{
9726 long n1, n2;
9727 int error = FALSE;
9728
9729 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9730 || arg->vval.v_list->lv_len != 2)
9731 return FAIL;
9732 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9733 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009734# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009735 tm->HighPart = n1;
9736 tm->LowPart = n2;
9737# else
9738 tm->tv_sec = n1;
9739 tm->tv_usec = n2;
9740# endif
9741 return error ? FAIL : OK;
9742}
9743#endif /* FEAT_RELTIME */
9744
9745/*
9746 * "reltime()" function
9747 */
9748 static void
9749f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9750{
9751#ifdef FEAT_RELTIME
9752 proftime_T res;
9753 proftime_T start;
9754
9755 if (argvars[0].v_type == VAR_UNKNOWN)
9756 {
9757 /* No arguments: get current time. */
9758 profile_start(&res);
9759 }
9760 else if (argvars[1].v_type == VAR_UNKNOWN)
9761 {
9762 if (list2proftime(&argvars[0], &res) == FAIL)
9763 return;
9764 profile_end(&res);
9765 }
9766 else
9767 {
9768 /* Two arguments: compute the difference. */
9769 if (list2proftime(&argvars[0], &start) == FAIL
9770 || list2proftime(&argvars[1], &res) == FAIL)
9771 return;
9772 profile_sub(&res, &start);
9773 }
9774
9775 if (rettv_list_alloc(rettv) == OK)
9776 {
9777 long n1, n2;
9778
Bram Moolenaar4f974752019-02-17 17:44:42 +01009779# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009780 n1 = res.HighPart;
9781 n2 = res.LowPart;
9782# else
9783 n1 = res.tv_sec;
9784 n2 = res.tv_usec;
9785# endif
9786 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9787 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9788 }
9789#endif
9790}
9791
9792#ifdef FEAT_FLOAT
9793/*
9794 * "reltimefloat()" function
9795 */
9796 static void
9797f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9798{
9799# ifdef FEAT_RELTIME
9800 proftime_T tm;
9801# endif
9802
9803 rettv->v_type = VAR_FLOAT;
9804 rettv->vval.v_float = 0;
9805# ifdef FEAT_RELTIME
9806 if (list2proftime(&argvars[0], &tm) == OK)
9807 rettv->vval.v_float = profile_float(&tm);
9808# endif
9809}
9810#endif
9811
9812/*
9813 * "reltimestr()" function
9814 */
9815 static void
9816f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9817{
9818#ifdef FEAT_RELTIME
9819 proftime_T tm;
9820#endif
9821
9822 rettv->v_type = VAR_STRING;
9823 rettv->vval.v_string = NULL;
9824#ifdef FEAT_RELTIME
9825 if (list2proftime(&argvars[0], &tm) == OK)
9826 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9827#endif
9828}
9829
9830#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009831 static void
9832make_connection(void)
9833{
9834 if (X_DISPLAY == NULL
9835# ifdef FEAT_GUI
9836 && !gui.in_use
9837# endif
9838 )
9839 {
9840 x_force_connect = TRUE;
9841 setup_term_clip();
9842 x_force_connect = FALSE;
9843 }
9844}
9845
9846 static int
9847check_connection(void)
9848{
9849 make_connection();
9850 if (X_DISPLAY == NULL)
9851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009852 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009853 return FAIL;
9854 }
9855 return OK;
9856}
9857#endif
9858
9859#ifdef FEAT_CLIENTSERVER
9860 static void
9861remote_common(typval_T *argvars, typval_T *rettv, int expr)
9862{
9863 char_u *server_name;
9864 char_u *keys;
9865 char_u *r = NULL;
9866 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009867 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009868# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009869 HWND w;
9870# else
9871 Window w;
9872# endif
9873
9874 if (check_restricted() || check_secure())
9875 return;
9876
9877# ifdef FEAT_X11
9878 if (check_connection() == FAIL)
9879 return;
9880# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009881 if (argvars[2].v_type != VAR_UNKNOWN
9882 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009883 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009884
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009885 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009886 if (server_name == NULL)
9887 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009888 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009889# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009890 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009891# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009892 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9893 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009894# endif
9895 {
9896 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009898 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009899 vim_free(r);
9900 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009901 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009902 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009903 return;
9904 }
9905
9906 rettv->vval.v_string = r;
9907
9908 if (argvars[2].v_type != VAR_UNKNOWN)
9909 {
9910 dictitem_T v;
9911 char_u str[30];
9912 char_u *idvar;
9913
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009914 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009915 if (idvar != NULL && *idvar != NUL)
9916 {
9917 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9918 v.di_tv.v_type = VAR_STRING;
9919 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009920 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009921 vim_free(v.di_tv.vval.v_string);
9922 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009923 }
9924}
9925#endif
9926
9927/*
9928 * "remote_expr()" function
9929 */
9930 static void
9931f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9932{
9933 rettv->v_type = VAR_STRING;
9934 rettv->vval.v_string = NULL;
9935#ifdef FEAT_CLIENTSERVER
9936 remote_common(argvars, rettv, TRUE);
9937#endif
9938}
9939
9940/*
9941 * "remote_foreground()" function
9942 */
9943 static void
9944f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9945{
9946#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009947# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009948 /* On Win32 it's done in this application. */
9949 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009950 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951
9952 if (server_name != NULL)
9953 serverForeground(server_name);
9954 }
9955# else
9956 /* Send a foreground() expression to the server. */
9957 argvars[1].v_type = VAR_STRING;
9958 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9959 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009960 rettv->v_type = VAR_STRING;
9961 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009962 remote_common(argvars, rettv, TRUE);
9963 vim_free(argvars[1].vval.v_string);
9964# endif
9965#endif
9966}
9967
9968 static void
9969f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9970{
9971#ifdef FEAT_CLIENTSERVER
9972 dictitem_T v;
9973 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009974# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009975 long_u n = 0;
9976# endif
9977 char_u *serverid;
9978
9979 if (check_restricted() || check_secure())
9980 {
9981 rettv->vval.v_number = -1;
9982 return;
9983 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009984 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009985 if (serverid == NULL)
9986 {
9987 rettv->vval.v_number = -1;
9988 return; /* type error; errmsg already given */
9989 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009990# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009991 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9992 if (n == 0)
9993 rettv->vval.v_number = -1;
9994 else
9995 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009996 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009997 rettv->vval.v_number = (s != NULL);
9998 }
9999# else
10000 if (check_connection() == FAIL)
10001 return;
10002
10003 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
10004 serverStrToWin(serverid), &s);
10005# endif
10006
10007 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
10008 {
10009 char_u *retvar;
10010
10011 v.di_tv.v_type = VAR_STRING;
10012 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010013 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010014 if (retvar != NULL)
10015 set_var(retvar, &v.di_tv, FALSE);
10016 vim_free(v.di_tv.vval.v_string);
10017 }
10018#else
10019 rettv->vval.v_number = -1;
10020#endif
10021}
10022
10023 static void
10024f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10025{
10026 char_u *r = NULL;
10027
10028#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010029 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010030
10031 if (serverid != NULL && !check_restricted() && !check_secure())
10032 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010033 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010034# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010035 /* The server's HWND is encoded in the 'id' parameter */
10036 long_u n = 0;
10037# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010038
10039 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010040 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010041
Bram Moolenaar4f974752019-02-17 17:44:42 +010010042# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010043 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10044 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010045 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010046 if (r == NULL)
10047# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010048 if (check_connection() == FAIL
10049 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10050 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010051# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010052 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010053 }
10054#endif
10055 rettv->v_type = VAR_STRING;
10056 rettv->vval.v_string = r;
10057}
10058
10059/*
10060 * "remote_send()" function
10061 */
10062 static void
10063f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10064{
10065 rettv->v_type = VAR_STRING;
10066 rettv->vval.v_string = NULL;
10067#ifdef FEAT_CLIENTSERVER
10068 remote_common(argvars, rettv, FALSE);
10069#endif
10070}
10071
10072/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010073 * "remote_startserver()" function
10074 */
10075 static void
10076f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10077{
10078#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010079 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010080
10081 if (server == NULL)
10082 return; /* type error; errmsg already given */
10083 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010084 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010085 else
10086 {
10087# ifdef FEAT_X11
10088 if (check_connection() == OK)
10089 serverRegisterName(X_DISPLAY, server);
10090# else
10091 serverSetName(server);
10092# endif
10093 }
10094#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010095 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010096#endif
10097}
10098
10099/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010100 * "remove()" function
10101 */
10102 static void
10103f_remove(typval_T *argvars, typval_T *rettv)
10104{
10105 list_T *l;
10106 listitem_T *item, *item2;
10107 listitem_T *li;
10108 long idx;
10109 long end;
10110 char_u *key;
10111 dict_T *d;
10112 dictitem_T *di;
10113 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010114 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010115
10116 if (argvars[0].v_type == VAR_DICT)
10117 {
10118 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010119 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010120 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010121 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010122 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010123 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010124 if (key != NULL)
10125 {
10126 di = dict_find(d, key, -1);
10127 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010128 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010129 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10130 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10131 {
10132 *rettv = di->di_tv;
10133 init_tv(&di->di_tv);
10134 dictitem_remove(d, di);
10135 }
10136 }
10137 }
10138 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010139 else if (argvars[0].v_type == VAR_BLOB)
10140 {
10141 idx = (long)tv_get_number_chk(&argvars[1], &error);
10142 if (!error)
10143 {
10144 blob_T *b = argvars[0].vval.v_blob;
10145 int len = blob_len(b);
10146 char_u *p;
10147
10148 if (idx < 0)
10149 // count from the end
10150 idx = len + idx;
10151 if (idx < 0 || idx >= len)
10152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010153 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010154 return;
10155 }
10156 if (argvars[2].v_type == VAR_UNKNOWN)
10157 {
10158 // Remove one item, return its value.
10159 p = (char_u *)b->bv_ga.ga_data;
10160 rettv->vval.v_number = (varnumber_T) *(p + idx);
10161 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10162 --b->bv_ga.ga_len;
10163 }
10164 else
10165 {
10166 blob_T *blob;
10167
10168 // Remove range of items, return list with values.
10169 end = (long)tv_get_number_chk(&argvars[2], &error);
10170 if (error)
10171 return;
10172 if (end < 0)
10173 // count from the end
10174 end = len + end;
10175 if (end >= len || idx > end)
10176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010177 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010178 return;
10179 }
10180 blob = blob_alloc();
10181 if (blob == NULL)
10182 return;
10183 blob->bv_ga.ga_len = end - idx + 1;
10184 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10185 {
10186 vim_free(blob);
10187 return;
10188 }
10189 p = (char_u *)b->bv_ga.ga_data;
10190 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10191 (size_t)(end - idx + 1));
10192 ++blob->bv_refcount;
10193 rettv->v_type = VAR_BLOB;
10194 rettv->vval.v_blob = blob;
10195
10196 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10197 b->bv_ga.ga_len -= end - idx + 1;
10198 }
10199 }
10200 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010201 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010202 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010203 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010204 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010205 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010206 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010207 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010208 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010209 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010210 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010211 else
10212 {
10213 if (argvars[2].v_type == VAR_UNKNOWN)
10214 {
10215 /* Remove one item, return its value. */
10216 vimlist_remove(l, item, item);
10217 *rettv = item->li_tv;
10218 vim_free(item);
10219 }
10220 else
10221 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010222 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010223 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010224 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010225 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010226 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010227 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010228 else
10229 {
10230 int cnt = 0;
10231
10232 for (li = item; li != NULL; li = li->li_next)
10233 {
10234 ++cnt;
10235 if (li == item2)
10236 break;
10237 }
10238 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010239 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010240 else
10241 {
10242 vimlist_remove(l, item, item2);
10243 if (rettv_list_alloc(rettv) == OK)
10244 {
10245 l = rettv->vval.v_list;
10246 l->lv_first = item;
10247 l->lv_last = item2;
10248 item->li_prev = NULL;
10249 item2->li_next = NULL;
10250 l->lv_len = cnt;
10251 }
10252 }
10253 }
10254 }
10255 }
10256 }
10257}
10258
10259/*
10260 * "rename({from}, {to})" function
10261 */
10262 static void
10263f_rename(typval_T *argvars, typval_T *rettv)
10264{
10265 char_u buf[NUMBUFLEN];
10266
10267 if (check_restricted() || check_secure())
10268 rettv->vval.v_number = -1;
10269 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010270 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10271 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010272}
10273
10274/*
10275 * "repeat()" function
10276 */
10277 static void
10278f_repeat(typval_T *argvars, typval_T *rettv)
10279{
10280 char_u *p;
10281 int n;
10282 int slen;
10283 int len;
10284 char_u *r;
10285 int i;
10286
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010287 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 if (argvars[0].v_type == VAR_LIST)
10289 {
10290 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10291 while (n-- > 0)
10292 if (list_extend(rettv->vval.v_list,
10293 argvars[0].vval.v_list, NULL) == FAIL)
10294 break;
10295 }
10296 else
10297 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010298 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010299 rettv->v_type = VAR_STRING;
10300 rettv->vval.v_string = NULL;
10301
10302 slen = (int)STRLEN(p);
10303 len = slen * n;
10304 if (len <= 0)
10305 return;
10306
10307 r = alloc(len + 1);
10308 if (r != NULL)
10309 {
10310 for (i = 0; i < n; i++)
10311 mch_memmove(r + i * slen, p, (size_t)slen);
10312 r[len] = NUL;
10313 }
10314
10315 rettv->vval.v_string = r;
10316 }
10317}
10318
10319/*
10320 * "resolve()" function
10321 */
10322 static void
10323f_resolve(typval_T *argvars, typval_T *rettv)
10324{
10325 char_u *p;
10326#ifdef HAVE_READLINK
10327 char_u *buf = NULL;
10328#endif
10329
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010330 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010331#ifdef FEAT_SHORTCUT
10332 {
10333 char_u *v = NULL;
10334
Bram Moolenaardce1e892019-02-10 23:18:53 +010010335 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010336 if (v != NULL)
10337 rettv->vval.v_string = v;
10338 else
10339 rettv->vval.v_string = vim_strsave(p);
10340 }
10341#else
10342# ifdef HAVE_READLINK
10343 {
10344 char_u *cpy;
10345 int len;
10346 char_u *remain = NULL;
10347 char_u *q;
10348 int is_relative_to_current = FALSE;
10349 int has_trailing_pathsep = FALSE;
10350 int limit = 100;
10351
10352 p = vim_strsave(p);
10353
10354 if (p[0] == '.' && (vim_ispathsep(p[1])
10355 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10356 is_relative_to_current = TRUE;
10357
10358 len = STRLEN(p);
10359 if (len > 0 && after_pathsep(p, p + len))
10360 {
10361 has_trailing_pathsep = TRUE;
10362 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10363 }
10364
10365 q = getnextcomp(p);
10366 if (*q != NUL)
10367 {
10368 /* Separate the first path component in "p", and keep the
10369 * remainder (beginning with the path separator). */
10370 remain = vim_strsave(q - 1);
10371 q[-1] = NUL;
10372 }
10373
10374 buf = alloc(MAXPATHL + 1);
10375 if (buf == NULL)
10376 goto fail;
10377
10378 for (;;)
10379 {
10380 for (;;)
10381 {
10382 len = readlink((char *)p, (char *)buf, MAXPATHL);
10383 if (len <= 0)
10384 break;
10385 buf[len] = NUL;
10386
10387 if (limit-- == 0)
10388 {
10389 vim_free(p);
10390 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010391 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010392 rettv->vval.v_string = NULL;
10393 goto fail;
10394 }
10395
10396 /* Ensure that the result will have a trailing path separator
10397 * if the argument has one. */
10398 if (remain == NULL && has_trailing_pathsep)
10399 add_pathsep(buf);
10400
10401 /* Separate the first path component in the link value and
10402 * concatenate the remainders. */
10403 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10404 if (*q != NUL)
10405 {
10406 if (remain == NULL)
10407 remain = vim_strsave(q - 1);
10408 else
10409 {
10410 cpy = concat_str(q - 1, remain);
10411 if (cpy != NULL)
10412 {
10413 vim_free(remain);
10414 remain = cpy;
10415 }
10416 }
10417 q[-1] = NUL;
10418 }
10419
10420 q = gettail(p);
10421 if (q > p && *q == NUL)
10422 {
10423 /* Ignore trailing path separator. */
10424 q[-1] = NUL;
10425 q = gettail(p);
10426 }
10427 if (q > p && !mch_isFullName(buf))
10428 {
10429 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010430 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010431 if (cpy != NULL)
10432 {
10433 STRCPY(cpy, p);
10434 STRCPY(gettail(cpy), buf);
10435 vim_free(p);
10436 p = cpy;
10437 }
10438 }
10439 else
10440 {
10441 vim_free(p);
10442 p = vim_strsave(buf);
10443 }
10444 }
10445
10446 if (remain == NULL)
10447 break;
10448
10449 /* Append the first path component of "remain" to "p". */
10450 q = getnextcomp(remain + 1);
10451 len = q - remain - (*q != NUL);
10452 cpy = vim_strnsave(p, STRLEN(p) + len);
10453 if (cpy != NULL)
10454 {
10455 STRNCAT(cpy, remain, len);
10456 vim_free(p);
10457 p = cpy;
10458 }
10459 /* Shorten "remain". */
10460 if (*q != NUL)
10461 STRMOVE(remain, q - 1);
10462 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010463 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010464 }
10465
10466 /* If the result is a relative path name, make it explicitly relative to
10467 * the current directory if and only if the argument had this form. */
10468 if (!vim_ispathsep(*p))
10469 {
10470 if (is_relative_to_current
10471 && *p != NUL
10472 && !(p[0] == '.'
10473 && (p[1] == NUL
10474 || vim_ispathsep(p[1])
10475 || (p[1] == '.'
10476 && (p[2] == NUL
10477 || vim_ispathsep(p[2]))))))
10478 {
10479 /* Prepend "./". */
10480 cpy = concat_str((char_u *)"./", p);
10481 if (cpy != NULL)
10482 {
10483 vim_free(p);
10484 p = cpy;
10485 }
10486 }
10487 else if (!is_relative_to_current)
10488 {
10489 /* Strip leading "./". */
10490 q = p;
10491 while (q[0] == '.' && vim_ispathsep(q[1]))
10492 q += 2;
10493 if (q > p)
10494 STRMOVE(p, p + 2);
10495 }
10496 }
10497
10498 /* Ensure that the result will have no trailing path separator
10499 * if the argument had none. But keep "/" or "//". */
10500 if (!has_trailing_pathsep)
10501 {
10502 q = p + STRLEN(p);
10503 if (after_pathsep(p, q))
10504 *gettail_sep(p) = NUL;
10505 }
10506
10507 rettv->vval.v_string = p;
10508 }
10509# else
10510 rettv->vval.v_string = vim_strsave(p);
10511# endif
10512#endif
10513
10514 simplify_filename(rettv->vval.v_string);
10515
10516#ifdef HAVE_READLINK
10517fail:
10518 vim_free(buf);
10519#endif
10520 rettv->v_type = VAR_STRING;
10521}
10522
10523/*
10524 * "reverse({list})" function
10525 */
10526 static void
10527f_reverse(typval_T *argvars, typval_T *rettv)
10528{
10529 list_T *l;
10530 listitem_T *li, *ni;
10531
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010532 if (argvars[0].v_type == VAR_BLOB)
10533 {
10534 blob_T *b = argvars[0].vval.v_blob;
10535 int i, len = blob_len(b);
10536
10537 for (i = 0; i < len / 2; i++)
10538 {
10539 int tmp = blob_get(b, i);
10540
10541 blob_set(b, i, blob_get(b, len - i - 1));
10542 blob_set(b, len - i - 1, tmp);
10543 }
10544 rettv_blob_set(rettv, b);
10545 return;
10546 }
10547
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010548 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010549 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010550 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010551 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010552 (char_u *)N_("reverse() argument"), TRUE))
10553 {
10554 li = l->lv_last;
10555 l->lv_first = l->lv_last = NULL;
10556 l->lv_len = 0;
10557 while (li != NULL)
10558 {
10559 ni = li->li_prev;
10560 list_append(l, li);
10561 li = ni;
10562 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010563 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010564 l->lv_idx = l->lv_len - l->lv_idx - 1;
10565 }
10566}
10567
10568#define SP_NOMOVE 0x01 /* don't move cursor */
10569#define SP_REPEAT 0x02 /* repeat to find outer pair */
10570#define SP_RETCOUNT 0x04 /* return matchcount */
10571#define SP_SETPCMARK 0x08 /* set previous context mark */
10572#define SP_START 0x10 /* accept match at start position */
10573#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10574#define SP_END 0x40 /* leave cursor at end of match */
10575#define SP_COLUMN 0x80 /* start at cursor column */
10576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010577/*
10578 * Get flags for a search function.
10579 * Possibly sets "p_ws".
10580 * Returns BACKWARD, FORWARD or zero (for an error).
10581 */
10582 static int
10583get_search_arg(typval_T *varp, int *flagsp)
10584{
10585 int dir = FORWARD;
10586 char_u *flags;
10587 char_u nbuf[NUMBUFLEN];
10588 int mask;
10589
10590 if (varp->v_type != VAR_UNKNOWN)
10591 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010592 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010593 if (flags == NULL)
10594 return 0; /* type error; errmsg already given */
10595 while (*flags != NUL)
10596 {
10597 switch (*flags)
10598 {
10599 case 'b': dir = BACKWARD; break;
10600 case 'w': p_ws = TRUE; break;
10601 case 'W': p_ws = FALSE; break;
10602 default: mask = 0;
10603 if (flagsp != NULL)
10604 switch (*flags)
10605 {
10606 case 'c': mask = SP_START; break;
10607 case 'e': mask = SP_END; break;
10608 case 'm': mask = SP_RETCOUNT; break;
10609 case 'n': mask = SP_NOMOVE; break;
10610 case 'p': mask = SP_SUBPAT; break;
10611 case 'r': mask = SP_REPEAT; break;
10612 case 's': mask = SP_SETPCMARK; break;
10613 case 'z': mask = SP_COLUMN; break;
10614 }
10615 if (mask == 0)
10616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010617 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010618 dir = 0;
10619 }
10620 else
10621 *flagsp |= mask;
10622 }
10623 if (dir == 0)
10624 break;
10625 ++flags;
10626 }
10627 }
10628 return dir;
10629}
10630
10631/*
10632 * Shared by search() and searchpos() functions.
10633 */
10634 static int
10635search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10636{
10637 int flags;
10638 char_u *pat;
10639 pos_T pos;
10640 pos_T save_cursor;
10641 int save_p_ws = p_ws;
10642 int dir;
10643 int retval = 0; /* default: FAIL */
10644 long lnum_stop = 0;
10645 proftime_T tm;
10646#ifdef FEAT_RELTIME
10647 long time_limit = 0;
10648#endif
10649 int options = SEARCH_KEEP;
10650 int subpatnum;
10651
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010652 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010653 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10654 if (dir == 0)
10655 goto theend;
10656 flags = *flagsp;
10657 if (flags & SP_START)
10658 options |= SEARCH_START;
10659 if (flags & SP_END)
10660 options |= SEARCH_END;
10661 if (flags & SP_COLUMN)
10662 options |= SEARCH_COL;
10663
10664 /* Optional arguments: line number to stop searching and timeout. */
10665 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010667 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010668 if (lnum_stop < 0)
10669 goto theend;
10670#ifdef FEAT_RELTIME
10671 if (argvars[3].v_type != VAR_UNKNOWN)
10672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010673 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010674 if (time_limit < 0)
10675 goto theend;
10676 }
10677#endif
10678 }
10679
10680#ifdef FEAT_RELTIME
10681 /* Set the time limit, if there is one. */
10682 profile_setlimit(time_limit, &tm);
10683#endif
10684
10685 /*
10686 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10687 * Check to make sure only those flags are set.
10688 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10689 * flags cannot be set. Check for that condition also.
10690 */
10691 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10692 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010694 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010695 goto theend;
10696 }
10697
10698 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010699 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010700 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010701 if (subpatnum != FAIL)
10702 {
10703 if (flags & SP_SUBPAT)
10704 retval = subpatnum;
10705 else
10706 retval = pos.lnum;
10707 if (flags & SP_SETPCMARK)
10708 setpcmark();
10709 curwin->w_cursor = pos;
10710 if (match_pos != NULL)
10711 {
10712 /* Store the match cursor position */
10713 match_pos->lnum = pos.lnum;
10714 match_pos->col = pos.col + 1;
10715 }
10716 /* "/$" will put the cursor after the end of the line, may need to
10717 * correct that here */
10718 check_cursor();
10719 }
10720
10721 /* If 'n' flag is used: restore cursor position. */
10722 if (flags & SP_NOMOVE)
10723 curwin->w_cursor = save_cursor;
10724 else
10725 curwin->w_set_curswant = TRUE;
10726theend:
10727 p_ws = save_p_ws;
10728
10729 return retval;
10730}
10731
10732#ifdef FEAT_FLOAT
10733
10734/*
10735 * round() is not in C90, use ceil() or floor() instead.
10736 */
10737 float_T
10738vim_round(float_T f)
10739{
10740 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10741}
10742
10743/*
10744 * "round({float})" function
10745 */
10746 static void
10747f_round(typval_T *argvars, typval_T *rettv)
10748{
10749 float_T f = 0.0;
10750
10751 rettv->v_type = VAR_FLOAT;
10752 if (get_float_arg(argvars, &f) == OK)
10753 rettv->vval.v_float = vim_round(f);
10754 else
10755 rettv->vval.v_float = 0.0;
10756}
10757#endif
10758
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010759#ifdef FEAT_RUBY
10760/*
10761 * "rubyeval()" function
10762 */
10763 static void
10764f_rubyeval(typval_T *argvars, typval_T *rettv)
10765{
10766 char_u *str;
10767 char_u buf[NUMBUFLEN];
10768
10769 str = tv_get_string_buf(&argvars[0], buf);
10770 do_rubyeval(str, rettv);
10771}
10772#endif
10773
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010774/*
10775 * "screenattr()" function
10776 */
10777 static void
10778f_screenattr(typval_T *argvars, typval_T *rettv)
10779{
10780 int row;
10781 int col;
10782 int c;
10783
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010784 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10785 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010786 if (row < 0 || row >= screen_Rows
10787 || col < 0 || col >= screen_Columns)
10788 c = -1;
10789 else
10790 c = ScreenAttrs[LineOffset[row] + col];
10791 rettv->vval.v_number = c;
10792}
10793
10794/*
10795 * "screenchar()" function
10796 */
10797 static void
10798f_screenchar(typval_T *argvars, typval_T *rettv)
10799{
10800 int row;
10801 int col;
10802 int off;
10803 int c;
10804
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010805 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10806 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010807 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010808 c = -1;
10809 else
10810 {
10811 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010812 if (enc_utf8 && ScreenLinesUC[off] != 0)
10813 c = ScreenLinesUC[off];
10814 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010815 c = ScreenLines[off];
10816 }
10817 rettv->vval.v_number = c;
10818}
10819
10820/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010821 * "screenchars()" function
10822 */
10823 static void
10824f_screenchars(typval_T *argvars, typval_T *rettv)
10825{
10826 int row;
10827 int col;
10828 int off;
10829 int c;
10830 int i;
10831
10832 if (rettv_list_alloc(rettv) == FAIL)
10833 return;
10834 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10835 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10836 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10837 return;
10838
10839 off = LineOffset[row] + col;
10840 if (enc_utf8 && ScreenLinesUC[off] != 0)
10841 c = ScreenLinesUC[off];
10842 else
10843 c = ScreenLines[off];
10844 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10845
10846 if (enc_utf8)
10847
10848 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10849 list_append_number(rettv->vval.v_list,
10850 (varnumber_T)ScreenLinesC[i][off]);
10851}
10852
10853/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010854 * "screencol()" function
10855 *
10856 * First column is 1 to be consistent with virtcol().
10857 */
10858 static void
10859f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10860{
10861 rettv->vval.v_number = screen_screencol() + 1;
10862}
10863
10864/*
10865 * "screenrow()" function
10866 */
10867 static void
10868f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10869{
10870 rettv->vval.v_number = screen_screenrow() + 1;
10871}
10872
10873/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010874 * "screenstring()" function
10875 */
10876 static void
10877f_screenstring(typval_T *argvars, typval_T *rettv)
10878{
10879 int row;
10880 int col;
10881 int off;
10882 int c;
10883 int i;
10884 char_u buf[MB_MAXBYTES + 1];
10885 int buflen = 0;
10886
10887 rettv->vval.v_string = NULL;
10888 rettv->v_type = VAR_STRING;
10889
10890 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10891 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10892 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10893 return;
10894
10895 off = LineOffset[row] + col;
10896 if (enc_utf8 && ScreenLinesUC[off] != 0)
10897 c = ScreenLinesUC[off];
10898 else
10899 c = ScreenLines[off];
10900 buflen += mb_char2bytes(c, buf);
10901
10902 if (enc_utf8)
10903 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10904 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10905
10906 buf[buflen] = NUL;
10907 rettv->vval.v_string = vim_strsave(buf);
10908}
10909
10910/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010911 * "search()" function
10912 */
10913 static void
10914f_search(typval_T *argvars, typval_T *rettv)
10915{
10916 int flags = 0;
10917
10918 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10919}
10920
10921/*
10922 * "searchdecl()" function
10923 */
10924 static void
10925f_searchdecl(typval_T *argvars, typval_T *rettv)
10926{
10927 int locally = 1;
10928 int thisblock = 0;
10929 int error = FALSE;
10930 char_u *name;
10931
10932 rettv->vval.v_number = 1; /* default: FAIL */
10933
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010934 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010935 if (argvars[1].v_type != VAR_UNKNOWN)
10936 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010937 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010938 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010939 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010940 }
10941 if (!error && name != NULL)
10942 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10943 locally, thisblock, SEARCH_KEEP) == FAIL;
10944}
10945
10946/*
10947 * Used by searchpair() and searchpairpos()
10948 */
10949 static int
10950searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10951{
10952 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010953 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010954 int save_p_ws = p_ws;
10955 int dir;
10956 int flags = 0;
10957 char_u nbuf1[NUMBUFLEN];
10958 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010959 int retval = 0; /* default: FAIL */
10960 long lnum_stop = 0;
10961 long time_limit = 0;
10962
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010963 /* Get the three pattern arguments: start, middle, end. Will result in an
10964 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010965 spat = tv_get_string_chk(&argvars[0]);
10966 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10967 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010968 if (spat == NULL || mpat == NULL || epat == NULL)
10969 goto theend; /* type error */
10970
10971 /* Handle the optional fourth argument: flags */
10972 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10973 if (dir == 0)
10974 goto theend;
10975
10976 /* Don't accept SP_END or SP_SUBPAT.
10977 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10978 */
10979 if ((flags & (SP_END | SP_SUBPAT)) != 0
10980 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010982 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 goto theend;
10984 }
10985
10986 /* Using 'r' implies 'W', otherwise it doesn't work. */
10987 if (flags & SP_REPEAT)
10988 p_ws = FALSE;
10989
10990 /* Optional fifth argument: skip expression */
10991 if (argvars[3].v_type == VAR_UNKNOWN
10992 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010993 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010994 else
10995 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010996 skip = &argvars[4];
10997 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10998 && skip->v_type != VAR_STRING)
10999 {
11000 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010011002 goto theend;
11003 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011004 if (argvars[5].v_type != VAR_UNKNOWN)
11005 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011006 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011007 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011009 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011010 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011011 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011012#ifdef FEAT_RELTIME
11013 if (argvars[6].v_type != VAR_UNKNOWN)
11014 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011015 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011016 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011018 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011019 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011020 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011021 }
11022#endif
11023 }
11024 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011025
11026 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11027 match_pos, lnum_stop, time_limit);
11028
11029theend:
11030 p_ws = save_p_ws;
11031
11032 return retval;
11033}
11034
11035/*
11036 * "searchpair()" function
11037 */
11038 static void
11039f_searchpair(typval_T *argvars, typval_T *rettv)
11040{
11041 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11042}
11043
11044/*
11045 * "searchpairpos()" function
11046 */
11047 static void
11048f_searchpairpos(typval_T *argvars, typval_T *rettv)
11049{
11050 pos_T match_pos;
11051 int lnum = 0;
11052 int col = 0;
11053
11054 if (rettv_list_alloc(rettv) == FAIL)
11055 return;
11056
11057 if (searchpair_cmn(argvars, &match_pos) > 0)
11058 {
11059 lnum = match_pos.lnum;
11060 col = match_pos.col;
11061 }
11062
11063 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11064 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11065}
11066
11067/*
11068 * Search for a start/middle/end thing.
11069 * Used by searchpair(), see its documentation for the details.
11070 * Returns 0 or -1 for no match,
11071 */
11072 long
11073do_searchpair(
11074 char_u *spat, /* start pattern */
11075 char_u *mpat, /* middle pattern */
11076 char_u *epat, /* end pattern */
11077 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011078 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011079 int flags, /* SP_SETPCMARK and other SP_ values */
11080 pos_T *match_pos,
11081 linenr_T lnum_stop, /* stop at this line if not zero */
11082 long time_limit UNUSED) /* stop after this many msec */
11083{
11084 char_u *save_cpo;
11085 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11086 long retval = 0;
11087 pos_T pos;
11088 pos_T firstpos;
11089 pos_T foundpos;
11090 pos_T save_cursor;
11091 pos_T save_pos;
11092 int n;
11093 int r;
11094 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011095 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011096 int err;
11097 int options = SEARCH_KEEP;
11098 proftime_T tm;
11099
11100 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11101 save_cpo = p_cpo;
11102 p_cpo = empty_option;
11103
11104#ifdef FEAT_RELTIME
11105 /* Set the time limit, if there is one. */
11106 profile_setlimit(time_limit, &tm);
11107#endif
11108
11109 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11110 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011111 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11112 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011113 if (pat2 == NULL || pat3 == NULL)
11114 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011115 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011116 if (*mpat == NUL)
11117 STRCPY(pat3, pat2);
11118 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011119 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011120 spat, epat, mpat);
11121 if (flags & SP_START)
11122 options |= SEARCH_START;
11123
Bram Moolenaar48570482017-10-30 21:48:41 +010011124 if (skip != NULL)
11125 {
11126 /* Empty string means to not use the skip expression. */
11127 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11128 use_skip = skip->vval.v_string != NULL
11129 && *skip->vval.v_string != NUL;
11130 }
11131
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011132 save_cursor = curwin->w_cursor;
11133 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011134 CLEAR_POS(&firstpos);
11135 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011136 pat = pat3;
11137 for (;;)
11138 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011139 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011140 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011141 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011142 /* didn't find it or found the first match again: FAIL */
11143 break;
11144
11145 if (firstpos.lnum == 0)
11146 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011147 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011148 {
11149 /* Found the same position again. Can happen with a pattern that
11150 * has "\zs" at the end and searching backwards. Advance one
11151 * character and try again. */
11152 if (dir == BACKWARD)
11153 decl(&pos);
11154 else
11155 incl(&pos);
11156 }
11157 foundpos = pos;
11158
11159 /* clear the start flag to avoid getting stuck here */
11160 options &= ~SEARCH_START;
11161
11162 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011163 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011164 {
11165 save_pos = curwin->w_cursor;
11166 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011167 err = FALSE;
11168 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011169 curwin->w_cursor = save_pos;
11170 if (err)
11171 {
11172 /* Evaluating {skip} caused an error, break here. */
11173 curwin->w_cursor = save_cursor;
11174 retval = -1;
11175 break;
11176 }
11177 if (r)
11178 continue;
11179 }
11180
11181 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11182 {
11183 /* Found end when searching backwards or start when searching
11184 * forward: nested pair. */
11185 ++nest;
11186 pat = pat2; /* nested, don't search for middle */
11187 }
11188 else
11189 {
11190 /* Found end when searching forward or start when searching
11191 * backward: end of (nested) pair; or found middle in outer pair. */
11192 if (--nest == 1)
11193 pat = pat3; /* outer level, search for middle */
11194 }
11195
11196 if (nest == 0)
11197 {
11198 /* Found the match: return matchcount or line number. */
11199 if (flags & SP_RETCOUNT)
11200 ++retval;
11201 else
11202 retval = pos.lnum;
11203 if (flags & SP_SETPCMARK)
11204 setpcmark();
11205 curwin->w_cursor = pos;
11206 if (!(flags & SP_REPEAT))
11207 break;
11208 nest = 1; /* search for next unmatched */
11209 }
11210 }
11211
11212 if (match_pos != NULL)
11213 {
11214 /* Store the match cursor position */
11215 match_pos->lnum = curwin->w_cursor.lnum;
11216 match_pos->col = curwin->w_cursor.col + 1;
11217 }
11218
11219 /* If 'n' flag is used or search failed: restore cursor position. */
11220 if ((flags & SP_NOMOVE) || retval == 0)
11221 curwin->w_cursor = save_cursor;
11222
11223theend:
11224 vim_free(pat2);
11225 vim_free(pat3);
11226 if (p_cpo == empty_option)
11227 p_cpo = save_cpo;
11228 else
11229 /* Darn, evaluating the {skip} expression changed the value. */
11230 free_string_option(save_cpo);
11231
11232 return retval;
11233}
11234
11235/*
11236 * "searchpos()" function
11237 */
11238 static void
11239f_searchpos(typval_T *argvars, typval_T *rettv)
11240{
11241 pos_T match_pos;
11242 int lnum = 0;
11243 int col = 0;
11244 int n;
11245 int flags = 0;
11246
11247 if (rettv_list_alloc(rettv) == FAIL)
11248 return;
11249
11250 n = search_cmn(argvars, &match_pos, &flags);
11251 if (n > 0)
11252 {
11253 lnum = match_pos.lnum;
11254 col = match_pos.col;
11255 }
11256
11257 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11258 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11259 if (flags & SP_SUBPAT)
11260 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11261}
11262
11263 static void
11264f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11265{
11266#ifdef FEAT_CLIENTSERVER
11267 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011268 char_u *server = tv_get_string_chk(&argvars[0]);
11269 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011270
11271 rettv->vval.v_number = -1;
11272 if (server == NULL || reply == NULL)
11273 return;
11274 if (check_restricted() || check_secure())
11275 return;
11276# ifdef FEAT_X11
11277 if (check_connection() == FAIL)
11278 return;
11279# endif
11280
11281 if (serverSendReply(server, reply) < 0)
11282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011283 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011284 return;
11285 }
11286 rettv->vval.v_number = 0;
11287#else
11288 rettv->vval.v_number = -1;
11289#endif
11290}
11291
11292 static void
11293f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11294{
11295 char_u *r = NULL;
11296
11297#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011298# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011299 r = serverGetVimNames();
11300# else
11301 make_connection();
11302 if (X_DISPLAY != NULL)
11303 r = serverGetVimNames(X_DISPLAY);
11304# endif
11305#endif
11306 rettv->v_type = VAR_STRING;
11307 rettv->vval.v_string = r;
11308}
11309
11310/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011311 * "setbufline()" function
11312 */
11313 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011314f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011315{
11316 linenr_T lnum;
11317 buf_T *buf;
11318
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011319 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011320 if (buf == NULL)
11321 rettv->vval.v_number = 1; /* FAIL */
11322 else
11323 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011324 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011325 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011326 }
11327}
11328
11329/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 * "setbufvar()" function
11331 */
11332 static void
11333f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11334{
11335 buf_T *buf;
11336 char_u *varname, *bufvarname;
11337 typval_T *varp;
11338 char_u nbuf[NUMBUFLEN];
11339
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011340 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011341 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011342 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11343 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011344 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011345 varp = &argvars[2];
11346
11347 if (buf != NULL && varname != NULL && varp != NULL)
11348 {
11349 if (*varname == '&')
11350 {
11351 long numval;
11352 char_u *strval;
11353 int error = FALSE;
11354 aco_save_T aco;
11355
11356 /* set curbuf to be our buf, temporarily */
11357 aucmd_prepbuf(&aco, buf);
11358
11359 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011360 numval = (long)tv_get_number_chk(varp, &error);
11361 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011362 if (!error && strval != NULL)
11363 set_option_value(varname, numval, strval, OPT_LOCAL);
11364
11365 /* reset notion of buffer */
11366 aucmd_restbuf(&aco);
11367 }
11368 else
11369 {
11370 buf_T *save_curbuf = curbuf;
11371
Bram Moolenaar964b3742019-05-24 18:54:09 +020011372 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011373 if (bufvarname != NULL)
11374 {
11375 curbuf = buf;
11376 STRCPY(bufvarname, "b:");
11377 STRCPY(bufvarname + 2, varname);
11378 set_var(bufvarname, varp, TRUE);
11379 vim_free(bufvarname);
11380 curbuf = save_curbuf;
11381 }
11382 }
11383 }
11384}
11385
11386 static void
11387f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11388{
11389 dict_T *d;
11390 dictitem_T *di;
11391 char_u *csearch;
11392
11393 if (argvars[0].v_type != VAR_DICT)
11394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011395 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011396 return;
11397 }
11398
11399 if ((d = argvars[0].vval.v_dict) != NULL)
11400 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011401 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011402 if (csearch != NULL)
11403 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011404 if (enc_utf8)
11405 {
11406 int pcc[MAX_MCO];
11407 int c = utfc_ptr2char(csearch, pcc);
11408
11409 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11410 }
11411 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011412 set_last_csearch(PTR2CHAR(csearch),
11413 csearch, MB_PTR2LEN(csearch));
11414 }
11415
11416 di = dict_find(d, (char_u *)"forward", -1);
11417 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011418 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011419 ? FORWARD : BACKWARD);
11420
11421 di = dict_find(d, (char_u *)"until", -1);
11422 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011423 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011424 }
11425}
11426
11427/*
11428 * "setcmdpos()" function
11429 */
11430 static void
11431f_setcmdpos(typval_T *argvars, typval_T *rettv)
11432{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011433 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011434
11435 if (pos >= 0)
11436 rettv->vval.v_number = set_cmdline_pos(pos);
11437}
11438
11439/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011440 * "setenv()" function
11441 */
11442 static void
11443f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11444{
11445 char_u namebuf[NUMBUFLEN];
11446 char_u valbuf[NUMBUFLEN];
11447 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11448
11449 if (argvars[1].v_type == VAR_SPECIAL
11450 && argvars[1].vval.v_number == VVAL_NULL)
11451 vim_unsetenv(name);
11452 else
11453 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11454}
11455
11456/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011457 * "setfperm({fname}, {mode})" function
11458 */
11459 static void
11460f_setfperm(typval_T *argvars, typval_T *rettv)
11461{
11462 char_u *fname;
11463 char_u modebuf[NUMBUFLEN];
11464 char_u *mode_str;
11465 int i;
11466 int mask;
11467 int mode = 0;
11468
11469 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011470 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011471 if (fname == NULL)
11472 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011473 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011474 if (mode_str == NULL)
11475 return;
11476 if (STRLEN(mode_str) != 9)
11477 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011478 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011479 return;
11480 }
11481
11482 mask = 1;
11483 for (i = 8; i >= 0; --i)
11484 {
11485 if (mode_str[i] != '-')
11486 mode |= mask;
11487 mask = mask << 1;
11488 }
11489 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11490}
11491
11492/*
11493 * "setline()" function
11494 */
11495 static void
11496f_setline(typval_T *argvars, typval_T *rettv)
11497{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011498 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011499
Bram Moolenaarca851592018-06-06 21:04:07 +020011500 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501}
11502
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011503/*
11504 * Used by "setqflist()" and "setloclist()" functions
11505 */
11506 static void
11507set_qf_ll_list(
11508 win_T *wp UNUSED,
11509 typval_T *list_arg UNUSED,
11510 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011511 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011512 typval_T *rettv)
11513{
11514#ifdef FEAT_QUICKFIX
11515 static char *e_invact = N_("E927: Invalid action: '%s'");
11516 char_u *act;
11517 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011518 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011519#endif
11520
11521 rettv->vval.v_number = -1;
11522
11523#ifdef FEAT_QUICKFIX
11524 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011525 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011526 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011527 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011528 else
11529 {
11530 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011531 dict_T *d = NULL;
11532 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533
11534 if (action_arg->v_type == VAR_STRING)
11535 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011536 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011537 if (act == NULL)
11538 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011539 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11540 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011541 action = *act;
11542 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011543 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011544 }
11545 else if (action_arg->v_type == VAR_UNKNOWN)
11546 action = ' ';
11547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011548 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011549
Bram Moolenaard823fa92016-08-12 16:29:27 +020011550 if (action_arg->v_type != VAR_UNKNOWN
11551 && what_arg->v_type != VAR_UNKNOWN)
11552 {
11553 if (what_arg->v_type == VAR_DICT)
11554 d = what_arg->vval.v_dict;
11555 else
11556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011557 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011558 valid_dict = FALSE;
11559 }
11560 }
11561
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011562 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011563 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011564 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11565 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011566 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011567 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011568 }
11569#endif
11570}
11571
11572/*
11573 * "setloclist()" function
11574 */
11575 static void
11576f_setloclist(typval_T *argvars, typval_T *rettv)
11577{
11578 win_T *win;
11579
11580 rettv->vval.v_number = -1;
11581
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011582 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011583 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011584 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011585}
11586
11587/*
11588 * "setmatches()" function
11589 */
11590 static void
11591f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11592{
11593#ifdef FEAT_SEARCH_EXTRA
11594 list_T *l;
11595 listitem_T *li;
11596 dict_T *d;
11597 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011598 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011599
11600 rettv->vval.v_number = -1;
11601 if (argvars[0].v_type != VAR_LIST)
11602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011603 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011604 return;
11605 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011606 if (win == NULL)
11607 return;
11608
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011609 if ((l = argvars[0].vval.v_list) != NULL)
11610 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011611 /* To some extent make sure that we are dealing with a list from
11612 * "getmatches()". */
11613 li = l->lv_first;
11614 while (li != NULL)
11615 {
11616 if (li->li_tv.v_type != VAR_DICT
11617 || (d = li->li_tv.vval.v_dict) == NULL)
11618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011619 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011620 return;
11621 }
11622 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11623 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11624 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11625 && dict_find(d, (char_u *)"priority", -1) != NULL
11626 && dict_find(d, (char_u *)"id", -1) != NULL))
11627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011628 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011629 return;
11630 }
11631 li = li->li_next;
11632 }
11633
Bram Moolenaaraff74912019-03-30 18:11:49 +010011634 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011635 li = l->lv_first;
11636 while (li != NULL)
11637 {
11638 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011639 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011640 dictitem_T *di;
11641 char_u *group;
11642 int priority;
11643 int id;
11644 char_u *conceal;
11645
11646 d = li->li_tv.vval.v_dict;
11647 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11648 {
11649 if (s == NULL)
11650 {
11651 s = list_alloc();
11652 if (s == NULL)
11653 return;
11654 }
11655
11656 /* match from matchaddpos() */
11657 for (i = 1; i < 9; i++)
11658 {
11659 sprintf((char *)buf, (char *)"pos%d", i);
11660 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11661 {
11662 if (di->di_tv.v_type != VAR_LIST)
11663 return;
11664
11665 list_append_tv(s, &di->di_tv);
11666 s->lv_refcount++;
11667 }
11668 else
11669 break;
11670 }
11671 }
11672
Bram Moolenaar8f667172018-12-14 15:38:31 +010011673 group = dict_get_string(d, (char_u *)"group", TRUE);
11674 priority = (int)dict_get_number(d, (char_u *)"priority");
11675 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011676 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011677 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011678 : NULL;
11679 if (i == 0)
11680 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011681 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011682 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011683 priority, id, NULL, conceal);
11684 }
11685 else
11686 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011687 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011688 list_unref(s);
11689 s = NULL;
11690 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011691 vim_free(group);
11692 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011693
11694 li = li->li_next;
11695 }
11696 rettv->vval.v_number = 0;
11697 }
11698#endif
11699}
11700
11701/*
11702 * "setpos()" function
11703 */
11704 static void
11705f_setpos(typval_T *argvars, typval_T *rettv)
11706{
11707 pos_T pos;
11708 int fnum;
11709 char_u *name;
11710 colnr_T curswant = -1;
11711
11712 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011713 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011714 if (name != NULL)
11715 {
11716 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11717 {
11718 if (--pos.col < 0)
11719 pos.col = 0;
11720 if (name[0] == '.' && name[1] == NUL)
11721 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011722 /* set cursor; "fnum" is ignored */
11723 curwin->w_cursor = pos;
11724 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011725 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011726 curwin->w_curswant = curswant - 1;
11727 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011728 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011729 check_cursor();
11730 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011731 }
11732 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11733 {
11734 /* set mark */
11735 if (setmark_pos(name[1], &pos, fnum) == OK)
11736 rettv->vval.v_number = 0;
11737 }
11738 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011739 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011740 }
11741 }
11742}
11743
11744/*
11745 * "setqflist()" function
11746 */
11747 static void
11748f_setqflist(typval_T *argvars, typval_T *rettv)
11749{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011750 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011751}
11752
11753/*
11754 * "setreg()" function
11755 */
11756 static void
11757f_setreg(typval_T *argvars, typval_T *rettv)
11758{
11759 int regname;
11760 char_u *strregname;
11761 char_u *stropt;
11762 char_u *strval;
11763 int append;
11764 char_u yank_type;
11765 long block_len;
11766
11767 block_len = -1;
11768 yank_type = MAUTO;
11769 append = FALSE;
11770
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011771 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 rettv->vval.v_number = 1; /* FAIL is default */
11773
11774 if (strregname == NULL)
11775 return; /* type error; errmsg already given */
11776 regname = *strregname;
11777 if (regname == 0 || regname == '@')
11778 regname = '"';
11779
11780 if (argvars[2].v_type != VAR_UNKNOWN)
11781 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011782 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011783 if (stropt == NULL)
11784 return; /* type error */
11785 for (; *stropt != NUL; ++stropt)
11786 switch (*stropt)
11787 {
11788 case 'a': case 'A': /* append */
11789 append = TRUE;
11790 break;
11791 case 'v': case 'c': /* character-wise selection */
11792 yank_type = MCHAR;
11793 break;
11794 case 'V': case 'l': /* line-wise selection */
11795 yank_type = MLINE;
11796 break;
11797 case 'b': case Ctrl_V: /* block-wise selection */
11798 yank_type = MBLOCK;
11799 if (VIM_ISDIGIT(stropt[1]))
11800 {
11801 ++stropt;
11802 block_len = getdigits(&stropt) - 1;
11803 --stropt;
11804 }
11805 break;
11806 }
11807 }
11808
11809 if (argvars[1].v_type == VAR_LIST)
11810 {
11811 char_u **lstval;
11812 char_u **allocval;
11813 char_u buf[NUMBUFLEN];
11814 char_u **curval;
11815 char_u **curallocval;
11816 list_T *ll = argvars[1].vval.v_list;
11817 listitem_T *li;
11818 int len;
11819
11820 /* If the list is NULL handle like an empty list. */
11821 len = ll == NULL ? 0 : ll->lv_len;
11822
11823 /* First half: use for pointers to result lines; second half: use for
11824 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011825 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011826 if (lstval == NULL)
11827 return;
11828 curval = lstval;
11829 allocval = lstval + len + 2;
11830 curallocval = allocval;
11831
11832 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11833 li = li->li_next)
11834 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011835 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011836 if (strval == NULL)
11837 goto free_lstval;
11838 if (strval == buf)
11839 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011840 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011841 * overwrite the string. */
11842 strval = vim_strsave(buf);
11843 if (strval == NULL)
11844 goto free_lstval;
11845 *curallocval++ = strval;
11846 }
11847 *curval++ = strval;
11848 }
11849 *curval++ = NULL;
11850
11851 write_reg_contents_lst(regname, lstval, -1,
11852 append, yank_type, block_len);
11853free_lstval:
11854 while (curallocval > allocval)
11855 vim_free(*--curallocval);
11856 vim_free(lstval);
11857 }
11858 else
11859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011860 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011861 if (strval == NULL)
11862 return;
11863 write_reg_contents_ex(regname, strval, -1,
11864 append, yank_type, block_len);
11865 }
11866 rettv->vval.v_number = 0;
11867}
11868
11869/*
11870 * "settabvar()" function
11871 */
11872 static void
11873f_settabvar(typval_T *argvars, typval_T *rettv)
11874{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011875 tabpage_T *save_curtab;
11876 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011877 char_u *varname, *tabvarname;
11878 typval_T *varp;
11879
11880 rettv->vval.v_number = 0;
11881
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011882 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011883 return;
11884
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011885 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11886 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011887 varp = &argvars[2];
11888
Bram Moolenaar4033c552017-09-16 20:54:51 +020011889 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011890 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011891 save_curtab = curtab;
11892 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011893
Bram Moolenaar964b3742019-05-24 18:54:09 +020011894 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011895 if (tabvarname != NULL)
11896 {
11897 STRCPY(tabvarname, "t:");
11898 STRCPY(tabvarname + 2, varname);
11899 set_var(tabvarname, varp, TRUE);
11900 vim_free(tabvarname);
11901 }
11902
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011903 /* Restore current tabpage */
11904 if (valid_tabpage(save_curtab))
11905 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011906 }
11907}
11908
11909/*
11910 * "settabwinvar()" function
11911 */
11912 static void
11913f_settabwinvar(typval_T *argvars, typval_T *rettv)
11914{
11915 setwinvar(argvars, rettv, 1);
11916}
11917
11918/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011919 * "settagstack()" function
11920 */
11921 static void
11922f_settagstack(typval_T *argvars, typval_T *rettv)
11923{
11924 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11925 win_T *wp;
11926 dict_T *d;
11927 int action = 'r';
11928
11929 rettv->vval.v_number = -1;
11930
11931 // first argument: window number or id
11932 wp = find_win_by_nr_or_id(&argvars[0]);
11933 if (wp == NULL)
11934 return;
11935
11936 // second argument: dict with items to set in the tag stack
11937 if (argvars[1].v_type != VAR_DICT)
11938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011939 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011940 return;
11941 }
11942 d = argvars[1].vval.v_dict;
11943 if (d == NULL)
11944 return;
11945
11946 // third argument: action - 'a' for append and 'r' for replace.
11947 // default is to replace the stack.
11948 if (argvars[2].v_type == VAR_UNKNOWN)
11949 action = 'r';
11950 else if (argvars[2].v_type == VAR_STRING)
11951 {
11952 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011953 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011954 if (actstr == NULL)
11955 return;
11956 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11957 action = *actstr;
11958 else
11959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011960 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011961 return;
11962 }
11963 }
11964 else
11965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011966 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011967 return;
11968 }
11969
11970 if (set_tagstack(wp, d, action) == OK)
11971 rettv->vval.v_number = 0;
11972}
11973
11974/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011975 * "setwinvar()" function
11976 */
11977 static void
11978f_setwinvar(typval_T *argvars, typval_T *rettv)
11979{
11980 setwinvar(argvars, rettv, 0);
11981}
11982
11983#ifdef FEAT_CRYPT
11984/*
11985 * "sha256({string})" function
11986 */
11987 static void
11988f_sha256(typval_T *argvars, typval_T *rettv)
11989{
11990 char_u *p;
11991
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011992 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011993 rettv->vval.v_string = vim_strsave(
11994 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11995 rettv->v_type = VAR_STRING;
11996}
11997#endif /* FEAT_CRYPT */
11998
11999/*
12000 * "shellescape({string})" function
12001 */
12002 static void
12003f_shellescape(typval_T *argvars, typval_T *rettv)
12004{
Bram Moolenaar20615522017-06-05 18:46:26 +020012005 int do_special = non_zero_arg(&argvars[1]);
12006
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012007 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012008 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012009 rettv->v_type = VAR_STRING;
12010}
12011
12012/*
12013 * shiftwidth() function
12014 */
12015 static void
12016f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12017{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012018 rettv->vval.v_number = 0;
12019
12020 if (argvars[0].v_type != VAR_UNKNOWN)
12021 {
12022 long col;
12023
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012024 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012025 if (col < 0)
12026 return; // type error; errmsg already given
12027#ifdef FEAT_VARTABS
12028 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12029 return;
12030#endif
12031 }
12032
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012033 rettv->vval.v_number = get_sw_value(curbuf);
12034}
12035
Bram Moolenaar162b7142018-12-21 15:17:36 +010012036#ifdef FEAT_SIGNS
12037/*
12038 * "sign_define()" function
12039 */
12040 static void
12041f_sign_define(typval_T *argvars, typval_T *rettv)
12042{
12043 char_u *name;
12044 dict_T *dict;
12045 char_u *icon = NULL;
12046 char_u *linehl = NULL;
12047 char_u *text = NULL;
12048 char_u *texthl = NULL;
12049
12050 rettv->vval.v_number = -1;
12051
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012052 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012053 if (name == NULL)
12054 return;
12055
12056 if (argvars[1].v_type != VAR_UNKNOWN)
12057 {
12058 if (argvars[1].v_type != VAR_DICT)
12059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012060 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012061 return;
12062 }
12063
12064 // sign attributes
12065 dict = argvars[1].vval.v_dict;
12066 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12067 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12068 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12069 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12070 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12071 text = dict_get_string(dict, (char_u *)"text", TRUE);
12072 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12073 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12074 }
12075
12076 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12077 rettv->vval.v_number = 0;
12078
12079 vim_free(icon);
12080 vim_free(linehl);
12081 vim_free(text);
12082 vim_free(texthl);
12083}
12084
12085/*
12086 * "sign_getdefined()" function
12087 */
12088 static void
12089f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12090{
12091 char_u *name = NULL;
12092
12093 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12094 return;
12095
12096 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012097 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012098
12099 sign_getlist(name, rettv->vval.v_list);
12100}
12101
12102/*
12103 * "sign_getplaced()" function
12104 */
12105 static void
12106f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12107{
12108 buf_T *buf = NULL;
12109 dict_T *dict;
12110 dictitem_T *di;
12111 linenr_T lnum = 0;
12112 int sign_id = 0;
12113 char_u *group = NULL;
12114 int notanum = FALSE;
12115
12116 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12117 return;
12118
12119 if (argvars[0].v_type != VAR_UNKNOWN)
12120 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012121 // get signs placed in the specified buffer
12122 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012123 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012124 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012125
12126 if (argvars[1].v_type != VAR_UNKNOWN)
12127 {
12128 if (argvars[1].v_type != VAR_DICT ||
12129 ((dict = argvars[1].vval.v_dict) == NULL))
12130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012131 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012132 return;
12133 }
12134 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12135 {
12136 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012137 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012138 if (notanum)
12139 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012140 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012141 }
12142 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12143 {
12144 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012145 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012146 if (notanum)
12147 return;
12148 }
12149 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12150 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012151 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012152 if (group == NULL)
12153 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012154 if (*group == '\0') // empty string means global group
12155 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012156 }
12157 }
12158 }
12159
12160 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12161}
12162
12163/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012164 * "sign_jump()" function
12165 */
12166 static void
12167f_sign_jump(typval_T *argvars, typval_T *rettv)
12168{
12169 int sign_id;
12170 char_u *sign_group = NULL;
12171 buf_T *buf;
12172 int notanum = FALSE;
12173
12174 rettv->vval.v_number = -1;
12175
Bram Moolenaarbdace832019-03-02 10:13:42 +010012176 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012177 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12178 if (notanum)
12179 return;
12180 if (sign_id <= 0)
12181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012182 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012183 return;
12184 }
12185
12186 // Sign group
12187 sign_group = tv_get_string_chk(&argvars[1]);
12188 if (sign_group == NULL)
12189 return;
12190 if (sign_group[0] == '\0')
12191 sign_group = NULL; // global sign group
12192 else
12193 {
12194 sign_group = vim_strsave(sign_group);
12195 if (sign_group == NULL)
12196 return;
12197 }
12198
12199 // Buffer to place the sign
12200 buf = get_buf_arg(&argvars[2]);
12201 if (buf == NULL)
12202 goto cleanup;
12203
12204 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12205
12206cleanup:
12207 vim_free(sign_group);
12208}
12209
12210/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012211 * "sign_place()" function
12212 */
12213 static void
12214f_sign_place(typval_T *argvars, typval_T *rettv)
12215{
12216 int sign_id;
12217 char_u *group = NULL;
12218 char_u *sign_name;
12219 buf_T *buf;
12220 dict_T *dict;
12221 dictitem_T *di;
12222 linenr_T lnum = 0;
12223 int prio = SIGN_DEF_PRIO;
12224 int notanum = FALSE;
12225
12226 rettv->vval.v_number = -1;
12227
Bram Moolenaarbdace832019-03-02 10:13:42 +010012228 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012229 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012230 if (notanum)
12231 return;
12232 if (sign_id < 0)
12233 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012234 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012235 return;
12236 }
12237
12238 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012239 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012240 if (group == NULL)
12241 return;
12242 if (group[0] == '\0')
12243 group = NULL; // global sign group
12244 else
12245 {
12246 group = vim_strsave(group);
12247 if (group == NULL)
12248 return;
12249 }
12250
12251 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012252 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012253 if (sign_name == NULL)
12254 goto cleanup;
12255
12256 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012257 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012258 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012259 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012260
12261 if (argvars[4].v_type != VAR_UNKNOWN)
12262 {
12263 if (argvars[4].v_type != VAR_DICT ||
12264 ((dict = argvars[4].vval.v_dict) == NULL))
12265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012266 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012267 goto cleanup;
12268 }
12269
12270 // Line number where the sign is to be placed
12271 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12272 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012273 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012274 if (notanum)
12275 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012276 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012277 }
12278 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12279 {
12280 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012281 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012282 if (notanum)
12283 goto cleanup;
12284 }
12285 }
12286
12287 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12288 rettv->vval.v_number = sign_id;
12289
12290cleanup:
12291 vim_free(group);
12292}
12293
12294/*
12295 * "sign_undefine()" function
12296 */
12297 static void
12298f_sign_undefine(typval_T *argvars, typval_T *rettv)
12299{
12300 char_u *name;
12301
12302 rettv->vval.v_number = -1;
12303
12304 if (argvars[0].v_type == VAR_UNKNOWN)
12305 {
12306 // Free all the signs
12307 free_signs();
12308 rettv->vval.v_number = 0;
12309 }
12310 else
12311 {
12312 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012313 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012314 if (name == NULL)
12315 return;
12316
12317 if (sign_undefine_by_name(name) == OK)
12318 rettv->vval.v_number = 0;
12319 }
12320}
12321
12322/*
12323 * "sign_unplace()" function
12324 */
12325 static void
12326f_sign_unplace(typval_T *argvars, typval_T *rettv)
12327{
12328 dict_T *dict;
12329 dictitem_T *di;
12330 int sign_id = 0;
12331 buf_T *buf = NULL;
12332 char_u *group = NULL;
12333
12334 rettv->vval.v_number = -1;
12335
12336 if (argvars[0].v_type != VAR_STRING)
12337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012338 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012339 return;
12340 }
12341
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012342 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012343 if (group[0] == '\0')
12344 group = NULL; // global sign group
12345 else
12346 {
12347 group = vim_strsave(group);
12348 if (group == NULL)
12349 return;
12350 }
12351
12352 if (argvars[1].v_type != VAR_UNKNOWN)
12353 {
12354 if (argvars[1].v_type != VAR_DICT)
12355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012356 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012357 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012358 }
12359 dict = argvars[1].vval.v_dict;
12360
12361 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12362 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012363 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012364 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012365 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012366 }
12367 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12368 sign_id = dict_get_number(dict, (char_u *)"id");
12369 }
12370
12371 if (buf == NULL)
12372 {
12373 // Delete the sign in all the buffers
12374 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012375 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012376 rettv->vval.v_number = 0;
12377 }
12378 else
12379 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012380 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012381 rettv->vval.v_number = 0;
12382 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012383
12384cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012385 vim_free(group);
12386}
12387#endif
12388
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012389/*
12390 * "simplify()" function
12391 */
12392 static void
12393f_simplify(typval_T *argvars, typval_T *rettv)
12394{
12395 char_u *p;
12396
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012397 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012398 rettv->vval.v_string = vim_strsave(p);
12399 simplify_filename(rettv->vval.v_string); /* simplify in place */
12400 rettv->v_type = VAR_STRING;
12401}
12402
12403#ifdef FEAT_FLOAT
12404/*
12405 * "sin()" function
12406 */
12407 static void
12408f_sin(typval_T *argvars, typval_T *rettv)
12409{
12410 float_T f = 0.0;
12411
12412 rettv->v_type = VAR_FLOAT;
12413 if (get_float_arg(argvars, &f) == OK)
12414 rettv->vval.v_float = sin(f);
12415 else
12416 rettv->vval.v_float = 0.0;
12417}
12418
12419/*
12420 * "sinh()" function
12421 */
12422 static void
12423f_sinh(typval_T *argvars, typval_T *rettv)
12424{
12425 float_T f = 0.0;
12426
12427 rettv->v_type = VAR_FLOAT;
12428 if (get_float_arg(argvars, &f) == OK)
12429 rettv->vval.v_float = sinh(f);
12430 else
12431 rettv->vval.v_float = 0.0;
12432}
12433#endif
12434
Bram Moolenaareae1b912019-05-09 15:12:55 +020012435static int item_compare(const void *s1, const void *s2);
12436static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012437
12438/* struct used in the array that's given to qsort() */
12439typedef struct
12440{
12441 listitem_T *item;
12442 int idx;
12443} sortItem_T;
12444
12445/* struct storing information about current sort */
12446typedef struct
12447{
12448 int item_compare_ic;
12449 int item_compare_numeric;
12450 int item_compare_numbers;
12451#ifdef FEAT_FLOAT
12452 int item_compare_float;
12453#endif
12454 char_u *item_compare_func;
12455 partial_T *item_compare_partial;
12456 dict_T *item_compare_selfdict;
12457 int item_compare_func_err;
12458 int item_compare_keep_zero;
12459} sortinfo_T;
12460static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012461#define ITEM_COMPARE_FAIL 999
12462
12463/*
12464 * Compare functions for f_sort() and f_uniq() below.
12465 */
12466 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012467item_compare(const void *s1, const void *s2)
12468{
12469 sortItem_T *si1, *si2;
12470 typval_T *tv1, *tv2;
12471 char_u *p1, *p2;
12472 char_u *tofree1 = NULL, *tofree2 = NULL;
12473 int res;
12474 char_u numbuf1[NUMBUFLEN];
12475 char_u numbuf2[NUMBUFLEN];
12476
12477 si1 = (sortItem_T *)s1;
12478 si2 = (sortItem_T *)s2;
12479 tv1 = &si1->item->li_tv;
12480 tv2 = &si2->item->li_tv;
12481
12482 if (sortinfo->item_compare_numbers)
12483 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012484 varnumber_T v1 = tv_get_number(tv1);
12485 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012486
12487 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12488 }
12489
12490#ifdef FEAT_FLOAT
12491 if (sortinfo->item_compare_float)
12492 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012493 float_T v1 = tv_get_float(tv1);
12494 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012495
12496 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12497 }
12498#endif
12499
12500 /* tv2string() puts quotes around a string and allocates memory. Don't do
12501 * that for string variables. Use a single quote when comparing with a
12502 * non-string to do what the docs promise. */
12503 if (tv1->v_type == VAR_STRING)
12504 {
12505 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12506 p1 = (char_u *)"'";
12507 else
12508 p1 = tv1->vval.v_string;
12509 }
12510 else
12511 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12512 if (tv2->v_type == VAR_STRING)
12513 {
12514 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12515 p2 = (char_u *)"'";
12516 else
12517 p2 = tv2->vval.v_string;
12518 }
12519 else
12520 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12521 if (p1 == NULL)
12522 p1 = (char_u *)"";
12523 if (p2 == NULL)
12524 p2 = (char_u *)"";
12525 if (!sortinfo->item_compare_numeric)
12526 {
12527 if (sortinfo->item_compare_ic)
12528 res = STRICMP(p1, p2);
12529 else
12530 res = STRCMP(p1, p2);
12531 }
12532 else
12533 {
12534 double n1, n2;
12535 n1 = strtod((char *)p1, (char **)&p1);
12536 n2 = strtod((char *)p2, (char **)&p2);
12537 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12538 }
12539
12540 /* When the result would be zero, compare the item indexes. Makes the
12541 * sort stable. */
12542 if (res == 0 && !sortinfo->item_compare_keep_zero)
12543 res = si1->idx > si2->idx ? 1 : -1;
12544
12545 vim_free(tofree1);
12546 vim_free(tofree2);
12547 return res;
12548}
12549
12550 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012551item_compare2(const void *s1, const void *s2)
12552{
12553 sortItem_T *si1, *si2;
12554 int res;
12555 typval_T rettv;
12556 typval_T argv[3];
12557 int dummy;
12558 char_u *func_name;
12559 partial_T *partial = sortinfo->item_compare_partial;
12560
12561 /* shortcut after failure in previous call; compare all items equal */
12562 if (sortinfo->item_compare_func_err)
12563 return 0;
12564
12565 si1 = (sortItem_T *)s1;
12566 si2 = (sortItem_T *)s2;
12567
12568 if (partial == NULL)
12569 func_name = sortinfo->item_compare_func;
12570 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012571 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012572
12573 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12574 * in the copy without changing the original list items. */
12575 copy_tv(&si1->item->li_tv, &argv[0]);
12576 copy_tv(&si2->item->li_tv, &argv[1]);
12577
12578 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012579 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012580 partial, sortinfo->item_compare_selfdict);
12581 clear_tv(&argv[0]);
12582 clear_tv(&argv[1]);
12583
12584 if (res == FAIL)
12585 res = ITEM_COMPARE_FAIL;
12586 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012587 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012588 if (sortinfo->item_compare_func_err)
12589 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12590 clear_tv(&rettv);
12591
12592 /* When the result would be zero, compare the pointers themselves. Makes
12593 * the sort stable. */
12594 if (res == 0 && !sortinfo->item_compare_keep_zero)
12595 res = si1->idx > si2->idx ? 1 : -1;
12596
12597 return res;
12598}
12599
12600/*
12601 * "sort({list})" function
12602 */
12603 static void
12604do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12605{
12606 list_T *l;
12607 listitem_T *li;
12608 sortItem_T *ptrs;
12609 sortinfo_T *old_sortinfo;
12610 sortinfo_T info;
12611 long len;
12612 long i;
12613
12614 /* Pointer to current info struct used in compare function. Save and
12615 * restore the current one for nested calls. */
12616 old_sortinfo = sortinfo;
12617 sortinfo = &info;
12618
12619 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012620 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012621 else
12622 {
12623 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012624 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012625 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12626 TRUE))
12627 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012628 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012629
12630 len = list_len(l);
12631 if (len <= 1)
12632 goto theend; /* short list sorts pretty quickly */
12633
12634 info.item_compare_ic = FALSE;
12635 info.item_compare_numeric = FALSE;
12636 info.item_compare_numbers = FALSE;
12637#ifdef FEAT_FLOAT
12638 info.item_compare_float = FALSE;
12639#endif
12640 info.item_compare_func = NULL;
12641 info.item_compare_partial = NULL;
12642 info.item_compare_selfdict = NULL;
12643 if (argvars[1].v_type != VAR_UNKNOWN)
12644 {
12645 /* optional second argument: {func} */
12646 if (argvars[1].v_type == VAR_FUNC)
12647 info.item_compare_func = argvars[1].vval.v_string;
12648 else if (argvars[1].v_type == VAR_PARTIAL)
12649 info.item_compare_partial = argvars[1].vval.v_partial;
12650 else
12651 {
12652 int error = FALSE;
12653
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012654 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012655 if (error)
12656 goto theend; /* type error; errmsg already given */
12657 if (i == 1)
12658 info.item_compare_ic = TRUE;
12659 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012660 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012661 else if (i != 0)
12662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012663 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012664 goto theend;
12665 }
12666 if (info.item_compare_func != NULL)
12667 {
12668 if (*info.item_compare_func == NUL)
12669 {
12670 /* empty string means default sort */
12671 info.item_compare_func = NULL;
12672 }
12673 else if (STRCMP(info.item_compare_func, "n") == 0)
12674 {
12675 info.item_compare_func = NULL;
12676 info.item_compare_numeric = TRUE;
12677 }
12678 else if (STRCMP(info.item_compare_func, "N") == 0)
12679 {
12680 info.item_compare_func = NULL;
12681 info.item_compare_numbers = TRUE;
12682 }
12683#ifdef FEAT_FLOAT
12684 else if (STRCMP(info.item_compare_func, "f") == 0)
12685 {
12686 info.item_compare_func = NULL;
12687 info.item_compare_float = TRUE;
12688 }
12689#endif
12690 else if (STRCMP(info.item_compare_func, "i") == 0)
12691 {
12692 info.item_compare_func = NULL;
12693 info.item_compare_ic = TRUE;
12694 }
12695 }
12696 }
12697
12698 if (argvars[2].v_type != VAR_UNKNOWN)
12699 {
12700 /* optional third argument: {dict} */
12701 if (argvars[2].v_type != VAR_DICT)
12702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012703 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012704 goto theend;
12705 }
12706 info.item_compare_selfdict = argvars[2].vval.v_dict;
12707 }
12708 }
12709
12710 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012711 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012712 if (ptrs == NULL)
12713 goto theend;
12714
12715 i = 0;
12716 if (sort)
12717 {
12718 /* sort(): ptrs will be the list to sort */
12719 for (li = l->lv_first; li != NULL; li = li->li_next)
12720 {
12721 ptrs[i].item = li;
12722 ptrs[i].idx = i;
12723 ++i;
12724 }
12725
12726 info.item_compare_func_err = FALSE;
12727 info.item_compare_keep_zero = FALSE;
12728 /* test the compare function */
12729 if ((info.item_compare_func != NULL
12730 || info.item_compare_partial != NULL)
12731 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12732 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012733 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012734 else
12735 {
12736 /* Sort the array with item pointers. */
12737 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12738 info.item_compare_func == NULL
12739 && info.item_compare_partial == NULL
12740 ? item_compare : item_compare2);
12741
12742 if (!info.item_compare_func_err)
12743 {
12744 /* Clear the List and append the items in sorted order. */
12745 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12746 l->lv_len = 0;
12747 for (i = 0; i < len; ++i)
12748 list_append(l, ptrs[i].item);
12749 }
12750 }
12751 }
12752 else
12753 {
12754 int (*item_compare_func_ptr)(const void *, const void *);
12755
12756 /* f_uniq(): ptrs will be a stack of items to remove */
12757 info.item_compare_func_err = FALSE;
12758 info.item_compare_keep_zero = TRUE;
12759 item_compare_func_ptr = info.item_compare_func != NULL
12760 || info.item_compare_partial != NULL
12761 ? item_compare2 : item_compare;
12762
12763 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12764 li = li->li_next)
12765 {
12766 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12767 == 0)
12768 ptrs[i++].item = li;
12769 if (info.item_compare_func_err)
12770 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012771 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012772 break;
12773 }
12774 }
12775
12776 if (!info.item_compare_func_err)
12777 {
12778 while (--i >= 0)
12779 {
12780 li = ptrs[i].item->li_next;
12781 ptrs[i].item->li_next = li->li_next;
12782 if (li->li_next != NULL)
12783 li->li_next->li_prev = ptrs[i].item;
12784 else
12785 l->lv_last = ptrs[i].item;
12786 list_fix_watch(l, li);
12787 listitem_free(li);
12788 l->lv_len--;
12789 }
12790 }
12791 }
12792
12793 vim_free(ptrs);
12794 }
12795theend:
12796 sortinfo = old_sortinfo;
12797}
12798
12799/*
12800 * "sort({list})" function
12801 */
12802 static void
12803f_sort(typval_T *argvars, typval_T *rettv)
12804{
12805 do_sort_uniq(argvars, rettv, TRUE);
12806}
12807
12808/*
12809 * "uniq({list})" function
12810 */
12811 static void
12812f_uniq(typval_T *argvars, typval_T *rettv)
12813{
12814 do_sort_uniq(argvars, rettv, FALSE);
12815}
12816
12817/*
12818 * "soundfold({word})" function
12819 */
12820 static void
12821f_soundfold(typval_T *argvars, typval_T *rettv)
12822{
12823 char_u *s;
12824
12825 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012826 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012827#ifdef FEAT_SPELL
12828 rettv->vval.v_string = eval_soundfold(s);
12829#else
12830 rettv->vval.v_string = vim_strsave(s);
12831#endif
12832}
12833
12834/*
12835 * "spellbadword()" function
12836 */
12837 static void
12838f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12839{
12840 char_u *word = (char_u *)"";
12841 hlf_T attr = HLF_COUNT;
12842 int len = 0;
12843
12844 if (rettv_list_alloc(rettv) == FAIL)
12845 return;
12846
12847#ifdef FEAT_SPELL
12848 if (argvars[0].v_type == VAR_UNKNOWN)
12849 {
12850 /* Find the start and length of the badly spelled word. */
12851 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12852 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012853 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012854 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012855 curwin->w_set_curswant = TRUE;
12856 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012857 }
12858 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12859 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012860 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012861 int capcol = -1;
12862
12863 if (str != NULL)
12864 {
12865 /* Check the argument for spelling. */
12866 while (*str != NUL)
12867 {
12868 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12869 if (attr != HLF_COUNT)
12870 {
12871 word = str;
12872 break;
12873 }
12874 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012875 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012876 }
12877 }
12878 }
12879#endif
12880
12881 list_append_string(rettv->vval.v_list, word, len);
12882 list_append_string(rettv->vval.v_list, (char_u *)(
12883 attr == HLF_SPB ? "bad" :
12884 attr == HLF_SPR ? "rare" :
12885 attr == HLF_SPL ? "local" :
12886 attr == HLF_SPC ? "caps" :
12887 ""), -1);
12888}
12889
12890/*
12891 * "spellsuggest()" function
12892 */
12893 static void
12894f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12895{
12896#ifdef FEAT_SPELL
12897 char_u *str;
12898 int typeerr = FALSE;
12899 int maxcount;
12900 garray_T ga;
12901 int i;
12902 listitem_T *li;
12903 int need_capital = FALSE;
12904#endif
12905
12906 if (rettv_list_alloc(rettv) == FAIL)
12907 return;
12908
12909#ifdef FEAT_SPELL
12910 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012912 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012913 if (argvars[1].v_type != VAR_UNKNOWN)
12914 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012915 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012916 if (maxcount <= 0)
12917 return;
12918 if (argvars[2].v_type != VAR_UNKNOWN)
12919 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012920 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012921 if (typeerr)
12922 return;
12923 }
12924 }
12925 else
12926 maxcount = 25;
12927
12928 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12929
12930 for (i = 0; i < ga.ga_len; ++i)
12931 {
12932 str = ((char_u **)ga.ga_data)[i];
12933
12934 li = listitem_alloc();
12935 if (li == NULL)
12936 vim_free(str);
12937 else
12938 {
12939 li->li_tv.v_type = VAR_STRING;
12940 li->li_tv.v_lock = 0;
12941 li->li_tv.vval.v_string = str;
12942 list_append(rettv->vval.v_list, li);
12943 }
12944 }
12945 ga_clear(&ga);
12946 }
12947#endif
12948}
12949
12950 static void
12951f_split(typval_T *argvars, typval_T *rettv)
12952{
12953 char_u *str;
12954 char_u *end;
12955 char_u *pat = NULL;
12956 regmatch_T regmatch;
12957 char_u patbuf[NUMBUFLEN];
12958 char_u *save_cpo;
12959 int match;
12960 colnr_T col = 0;
12961 int keepempty = FALSE;
12962 int typeerr = FALSE;
12963
12964 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12965 save_cpo = p_cpo;
12966 p_cpo = (char_u *)"";
12967
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012968 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012969 if (argvars[1].v_type != VAR_UNKNOWN)
12970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012971 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012972 if (pat == NULL)
12973 typeerr = TRUE;
12974 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012975 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012976 }
12977 if (pat == NULL || *pat == NUL)
12978 pat = (char_u *)"[\\x01- ]\\+";
12979
12980 if (rettv_list_alloc(rettv) == FAIL)
12981 return;
12982 if (typeerr)
12983 return;
12984
12985 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12986 if (regmatch.regprog != NULL)
12987 {
12988 regmatch.rm_ic = FALSE;
12989 while (*str != NUL || keepempty)
12990 {
12991 if (*str == NUL)
12992 match = FALSE; /* empty item at the end */
12993 else
12994 match = vim_regexec_nl(&regmatch, str, col);
12995 if (match)
12996 end = regmatch.startp[0];
12997 else
12998 end = str + STRLEN(str);
12999 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
13000 && *str != NUL && match && end < regmatch.endp[0]))
13001 {
13002 if (list_append_string(rettv->vval.v_list, str,
13003 (int)(end - str)) == FAIL)
13004 break;
13005 }
13006 if (!match)
13007 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010013008 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013009 if (regmatch.endp[0] > str)
13010 col = 0;
13011 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013012 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013013 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013014 str = regmatch.endp[0];
13015 }
13016
13017 vim_regfree(regmatch.regprog);
13018 }
13019
13020 p_cpo = save_cpo;
13021}
13022
13023#ifdef FEAT_FLOAT
13024/*
13025 * "sqrt()" function
13026 */
13027 static void
13028f_sqrt(typval_T *argvars, typval_T *rettv)
13029{
13030 float_T f = 0.0;
13031
13032 rettv->v_type = VAR_FLOAT;
13033 if (get_float_arg(argvars, &f) == OK)
13034 rettv->vval.v_float = sqrt(f);
13035 else
13036 rettv->vval.v_float = 0.0;
13037}
13038
13039/*
13040 * "str2float()" function
13041 */
13042 static void
13043f_str2float(typval_T *argvars, typval_T *rettv)
13044{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013045 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013046 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013047
Bram Moolenaar08243d22017-01-10 16:12:29 +010013048 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013049 p = skipwhite(p + 1);
13050 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013051 if (isneg)
13052 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013053 rettv->v_type = VAR_FLOAT;
13054}
13055#endif
13056
13057/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013058 * "str2list()" function
13059 */
13060 static void
13061f_str2list(typval_T *argvars, typval_T *rettv)
13062{
13063 char_u *p;
13064 int utf8 = FALSE;
13065
13066 if (rettv_list_alloc(rettv) == FAIL)
13067 return;
13068
13069 if (argvars[1].v_type != VAR_UNKNOWN)
13070 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13071
13072 p = tv_get_string(&argvars[0]);
13073
13074 if (has_mbyte || utf8)
13075 {
13076 int (*ptr2len)(char_u *);
13077 int (*ptr2char)(char_u *);
13078
13079 if (utf8 || enc_utf8)
13080 {
13081 ptr2len = utf_ptr2len;
13082 ptr2char = utf_ptr2char;
13083 }
13084 else
13085 {
13086 ptr2len = mb_ptr2len;
13087 ptr2char = mb_ptr2char;
13088 }
13089
13090 for ( ; *p != NUL; p += (*ptr2len)(p))
13091 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13092 }
13093 else
13094 for ( ; *p != NUL; ++p)
13095 list_append_number(rettv->vval.v_list, *p);
13096}
13097
13098/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013099 * "str2nr()" function
13100 */
13101 static void
13102f_str2nr(typval_T *argvars, typval_T *rettv)
13103{
13104 int base = 10;
13105 char_u *p;
13106 varnumber_T n;
13107 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013108 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013109
13110 if (argvars[1].v_type != VAR_UNKNOWN)
13111 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013112 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013113 if (base != 2 && base != 8 && base != 10 && base != 16)
13114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013115 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013116 return;
13117 }
13118 }
13119
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013120 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013121 isneg = (*p == '-');
13122 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013123 p = skipwhite(p + 1);
13124 switch (base)
13125 {
13126 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13127 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13128 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13129 default: what = 0;
13130 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013131 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13132 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013133 if (isneg)
13134 rettv->vval.v_number = -n;
13135 else
13136 rettv->vval.v_number = n;
13137
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013138}
13139
13140#ifdef HAVE_STRFTIME
13141/*
13142 * "strftime({format}[, {time}])" function
13143 */
13144 static void
13145f_strftime(typval_T *argvars, typval_T *rettv)
13146{
13147 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013148# ifdef HAVE_LOCALTIME_R
13149 struct tm tmval;
13150# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013151 struct tm *curtime;
13152 time_t seconds;
13153 char_u *p;
13154
13155 rettv->v_type = VAR_STRING;
13156
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013157 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013158 if (argvars[1].v_type == VAR_UNKNOWN)
13159 seconds = time(NULL);
13160 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013161 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013162# ifdef HAVE_LOCALTIME_R
13163 curtime = localtime_r(&seconds, &tmval);
13164# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013165 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013166# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013167 /* MSVC returns NULL for an invalid value of seconds. */
13168 if (curtime == NULL)
13169 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13170 else
13171 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013172 vimconv_T conv;
13173 char_u *enc;
13174
13175 conv.vc_type = CONV_NONE;
13176 enc = enc_locale();
13177 convert_setup(&conv, p_enc, enc);
13178 if (conv.vc_type != CONV_NONE)
13179 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013180 if (p != NULL)
13181 (void)strftime((char *)result_buf, sizeof(result_buf),
13182 (char *)p, curtime);
13183 else
13184 result_buf[0] = NUL;
13185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 if (conv.vc_type != CONV_NONE)
13187 vim_free(p);
13188 convert_setup(&conv, enc, p_enc);
13189 if (conv.vc_type != CONV_NONE)
13190 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13191 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013192 rettv->vval.v_string = vim_strsave(result_buf);
13193
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013194 /* Release conversion descriptors */
13195 convert_setup(&conv, NULL, NULL);
13196 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013197 }
13198}
13199#endif
13200
13201/*
13202 * "strgetchar()" function
13203 */
13204 static void
13205f_strgetchar(typval_T *argvars, typval_T *rettv)
13206{
13207 char_u *str;
13208 int len;
13209 int error = FALSE;
13210 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013211 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013212
13213 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013214 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013215 if (str == NULL)
13216 return;
13217 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013218 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013219 if (error)
13220 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221
Bram Moolenaar13505972019-01-24 15:04:48 +010013222 while (charidx >= 0 && byteidx < len)
13223 {
13224 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013225 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013226 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13227 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013228 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013229 --charidx;
13230 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013231 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013232}
13233
13234/*
13235 * "stridx()" function
13236 */
13237 static void
13238f_stridx(typval_T *argvars, typval_T *rettv)
13239{
13240 char_u buf[NUMBUFLEN];
13241 char_u *needle;
13242 char_u *haystack;
13243 char_u *save_haystack;
13244 char_u *pos;
13245 int start_idx;
13246
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013247 needle = tv_get_string_chk(&argvars[1]);
13248 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013249 rettv->vval.v_number = -1;
13250 if (needle == NULL || haystack == NULL)
13251 return; /* type error; errmsg already given */
13252
13253 if (argvars[2].v_type != VAR_UNKNOWN)
13254 {
13255 int error = FALSE;
13256
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013257 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013258 if (error || start_idx >= (int)STRLEN(haystack))
13259 return;
13260 if (start_idx >= 0)
13261 haystack += start_idx;
13262 }
13263
13264 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13265 if (pos != NULL)
13266 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13267}
13268
13269/*
13270 * "string()" function
13271 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013272 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013273f_string(typval_T *argvars, typval_T *rettv)
13274{
13275 char_u *tofree;
13276 char_u numbuf[NUMBUFLEN];
13277
13278 rettv->v_type = VAR_STRING;
13279 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13280 get_copyID());
13281 /* Make a copy if we have a value but it's not in allocated memory. */
13282 if (rettv->vval.v_string != NULL && tofree == NULL)
13283 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13284}
13285
13286/*
13287 * "strlen()" function
13288 */
13289 static void
13290f_strlen(typval_T *argvars, typval_T *rettv)
13291{
13292 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013293 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013294}
13295
13296/*
13297 * "strchars()" function
13298 */
13299 static void
13300f_strchars(typval_T *argvars, typval_T *rettv)
13301{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013302 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013303 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013304 varnumber_T len = 0;
13305 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013306
13307 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013308 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013310 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013311 else
13312 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013313 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13314 while (*s != NUL)
13315 {
13316 func_mb_ptr2char_adv(&s);
13317 ++len;
13318 }
13319 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013320 }
13321}
13322
13323/*
13324 * "strdisplaywidth()" function
13325 */
13326 static void
13327f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13328{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013329 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013330 int col = 0;
13331
13332 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013333 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334
13335 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13336}
13337
13338/*
13339 * "strwidth()" function
13340 */
13341 static void
13342f_strwidth(typval_T *argvars, typval_T *rettv)
13343{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013344 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013345
Bram Moolenaar13505972019-01-24 15:04:48 +010013346 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013347}
13348
13349/*
13350 * "strcharpart()" function
13351 */
13352 static void
13353f_strcharpart(typval_T *argvars, typval_T *rettv)
13354{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013355 char_u *p;
13356 int nchar;
13357 int nbyte = 0;
13358 int charlen;
13359 int len = 0;
13360 int slen;
13361 int error = FALSE;
13362
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013363 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013364 slen = (int)STRLEN(p);
13365
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013366 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013367 if (!error)
13368 {
13369 if (nchar > 0)
13370 while (nchar > 0 && nbyte < slen)
13371 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013372 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013373 --nchar;
13374 }
13375 else
13376 nbyte = nchar;
13377 if (argvars[2].v_type != VAR_UNKNOWN)
13378 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013379 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013380 while (charlen > 0 && nbyte + len < slen)
13381 {
13382 int off = nbyte + len;
13383
13384 if (off < 0)
13385 len += 1;
13386 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013387 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013388 --charlen;
13389 }
13390 }
13391 else
13392 len = slen - nbyte; /* default: all bytes that are available. */
13393 }
13394
13395 /*
13396 * Only return the overlap between the specified part and the actual
13397 * string.
13398 */
13399 if (nbyte < 0)
13400 {
13401 len += nbyte;
13402 nbyte = 0;
13403 }
13404 else if (nbyte > slen)
13405 nbyte = slen;
13406 if (len < 0)
13407 len = 0;
13408 else if (nbyte + len > slen)
13409 len = slen - nbyte;
13410
13411 rettv->v_type = VAR_STRING;
13412 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013413}
13414
13415/*
13416 * "strpart()" function
13417 */
13418 static void
13419f_strpart(typval_T *argvars, typval_T *rettv)
13420{
13421 char_u *p;
13422 int n;
13423 int len;
13424 int slen;
13425 int error = FALSE;
13426
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013427 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013428 slen = (int)STRLEN(p);
13429
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013430 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013431 if (error)
13432 len = 0;
13433 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013434 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013435 else
13436 len = slen - n; /* default len: all bytes that are available. */
13437
13438 /*
13439 * Only return the overlap between the specified part and the actual
13440 * string.
13441 */
13442 if (n < 0)
13443 {
13444 len += n;
13445 n = 0;
13446 }
13447 else if (n > slen)
13448 n = slen;
13449 if (len < 0)
13450 len = 0;
13451 else if (n + len > slen)
13452 len = slen - n;
13453
13454 rettv->v_type = VAR_STRING;
13455 rettv->vval.v_string = vim_strnsave(p + n, len);
13456}
13457
13458/*
13459 * "strridx()" function
13460 */
13461 static void
13462f_strridx(typval_T *argvars, typval_T *rettv)
13463{
13464 char_u buf[NUMBUFLEN];
13465 char_u *needle;
13466 char_u *haystack;
13467 char_u *rest;
13468 char_u *lastmatch = NULL;
13469 int haystack_len, end_idx;
13470
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013471 needle = tv_get_string_chk(&argvars[1]);
13472 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013473
13474 rettv->vval.v_number = -1;
13475 if (needle == NULL || haystack == NULL)
13476 return; /* type error; errmsg already given */
13477
13478 haystack_len = (int)STRLEN(haystack);
13479 if (argvars[2].v_type != VAR_UNKNOWN)
13480 {
13481 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013482 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013483 if (end_idx < 0)
13484 return; /* can never find a match */
13485 }
13486 else
13487 end_idx = haystack_len;
13488
13489 if (*needle == NUL)
13490 {
13491 /* Empty string matches past the end. */
13492 lastmatch = haystack + end_idx;
13493 }
13494 else
13495 {
13496 for (rest = haystack; *rest != '\0'; ++rest)
13497 {
13498 rest = (char_u *)strstr((char *)rest, (char *)needle);
13499 if (rest == NULL || rest > haystack + end_idx)
13500 break;
13501 lastmatch = rest;
13502 }
13503 }
13504
13505 if (lastmatch == NULL)
13506 rettv->vval.v_number = -1;
13507 else
13508 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13509}
13510
13511/*
13512 * "strtrans()" function
13513 */
13514 static void
13515f_strtrans(typval_T *argvars, typval_T *rettv)
13516{
13517 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013518 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013519}
13520
13521/*
13522 * "submatch()" function
13523 */
13524 static void
13525f_submatch(typval_T *argvars, typval_T *rettv)
13526{
13527 int error = FALSE;
13528 int no;
13529 int retList = 0;
13530
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013531 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013532 if (error)
13533 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013534 if (no < 0 || no >= NSUBEXP)
13535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013536 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013537 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013538 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013539 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013540 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013541 if (error)
13542 return;
13543
13544 if (retList == 0)
13545 {
13546 rettv->v_type = VAR_STRING;
13547 rettv->vval.v_string = reg_submatch(no);
13548 }
13549 else
13550 {
13551 rettv->v_type = VAR_LIST;
13552 rettv->vval.v_list = reg_submatch_list(no);
13553 }
13554}
13555
13556/*
13557 * "substitute()" function
13558 */
13559 static void
13560f_substitute(typval_T *argvars, typval_T *rettv)
13561{
13562 char_u patbuf[NUMBUFLEN];
13563 char_u subbuf[NUMBUFLEN];
13564 char_u flagsbuf[NUMBUFLEN];
13565
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013566 char_u *str = tv_get_string_chk(&argvars[0]);
13567 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013568 char_u *sub = NULL;
13569 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013570 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013571
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013572 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13573 expr = &argvars[2];
13574 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013575 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013576
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013577 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013578 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13579 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013580 rettv->vval.v_string = NULL;
13581 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013582 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013583}
13584
13585/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013586 * "swapinfo(swap_filename)" function
13587 */
13588 static void
13589f_swapinfo(typval_T *argvars, typval_T *rettv)
13590{
13591 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013592 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013593}
13594
13595/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013596 * "swapname(expr)" function
13597 */
13598 static void
13599f_swapname(typval_T *argvars, typval_T *rettv)
13600{
13601 buf_T *buf;
13602
13603 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013604 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013605 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13606 || buf->b_ml.ml_mfp->mf_fname == NULL)
13607 rettv->vval.v_string = NULL;
13608 else
13609 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13610}
13611
13612/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013613 * "synID(lnum, col, trans)" function
13614 */
13615 static void
13616f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13617{
13618 int id = 0;
13619#ifdef FEAT_SYN_HL
13620 linenr_T lnum;
13621 colnr_T col;
13622 int trans;
13623 int transerr = FALSE;
13624
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013625 lnum = tv_get_lnum(argvars); /* -1 on type error */
13626 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13627 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013628
13629 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13630 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13631 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13632#endif
13633
13634 rettv->vval.v_number = id;
13635}
13636
13637/*
13638 * "synIDattr(id, what [, mode])" function
13639 */
13640 static void
13641f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13642{
13643 char_u *p = NULL;
13644#ifdef FEAT_SYN_HL
13645 int id;
13646 char_u *what;
13647 char_u *mode;
13648 char_u modebuf[NUMBUFLEN];
13649 int modec;
13650
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013651 id = (int)tv_get_number(&argvars[0]);
13652 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013653 if (argvars[2].v_type != VAR_UNKNOWN)
13654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013655 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013656 modec = TOLOWER_ASC(mode[0]);
13657 if (modec != 't' && modec != 'c' && modec != 'g')
13658 modec = 0; /* replace invalid with current */
13659 }
13660 else
13661 {
13662#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13663 if (USE_24BIT)
13664 modec = 'g';
13665 else
13666#endif
13667 if (t_colors > 1)
13668 modec = 'c';
13669 else
13670 modec = 't';
13671 }
13672
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013673 switch (TOLOWER_ASC(what[0]))
13674 {
13675 case 'b':
13676 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13677 p = highlight_color(id, what, modec);
13678 else /* bold */
13679 p = highlight_has_attr(id, HL_BOLD, modec);
13680 break;
13681
13682 case 'f': /* fg[#] or font */
13683 p = highlight_color(id, what, modec);
13684 break;
13685
13686 case 'i':
13687 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13688 p = highlight_has_attr(id, HL_INVERSE, modec);
13689 else /* italic */
13690 p = highlight_has_attr(id, HL_ITALIC, modec);
13691 break;
13692
13693 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013694 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013695 break;
13696
13697 case 'r': /* reverse */
13698 p = highlight_has_attr(id, HL_INVERSE, modec);
13699 break;
13700
13701 case 's':
13702 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13703 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013704 /* strikeout */
13705 else if (TOLOWER_ASC(what[1]) == 't' &&
13706 TOLOWER_ASC(what[2]) == 'r')
13707 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013708 else /* standout */
13709 p = highlight_has_attr(id, HL_STANDOUT, modec);
13710 break;
13711
13712 case 'u':
13713 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13714 /* underline */
13715 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13716 else
13717 /* undercurl */
13718 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13719 break;
13720 }
13721
13722 if (p != NULL)
13723 p = vim_strsave(p);
13724#endif
13725 rettv->v_type = VAR_STRING;
13726 rettv->vval.v_string = p;
13727}
13728
13729/*
13730 * "synIDtrans(id)" function
13731 */
13732 static void
13733f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13734{
13735 int id;
13736
13737#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013738 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013739
13740 if (id > 0)
13741 id = syn_get_final_id(id);
13742 else
13743#endif
13744 id = 0;
13745
13746 rettv->vval.v_number = id;
13747}
13748
13749/*
13750 * "synconcealed(lnum, col)" function
13751 */
13752 static void
13753f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13754{
13755#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13756 linenr_T lnum;
13757 colnr_T col;
13758 int syntax_flags = 0;
13759 int cchar;
13760 int matchid = 0;
13761 char_u str[NUMBUFLEN];
13762#endif
13763
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013764 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013765
13766#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013767 lnum = tv_get_lnum(argvars); /* -1 on type error */
13768 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013769
13770 vim_memset(str, NUL, sizeof(str));
13771
13772 if (rettv_list_alloc(rettv) != FAIL)
13773 {
13774 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13775 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13776 && curwin->w_p_cole > 0)
13777 {
13778 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13779 syntax_flags = get_syntax_info(&matchid);
13780
13781 /* get the conceal character */
13782 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13783 {
13784 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013785 if (cchar == NUL && curwin->w_p_cole == 1)
13786 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013787 if (cchar != NUL)
13788 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013789 if (has_mbyte)
13790 (*mb_char2bytes)(cchar, str);
13791 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013792 str[0] = cchar;
13793 }
13794 }
13795 }
13796
13797 list_append_number(rettv->vval.v_list,
13798 (syntax_flags & HL_CONCEAL) != 0);
13799 /* -1 to auto-determine strlen */
13800 list_append_string(rettv->vval.v_list, str, -1);
13801 list_append_number(rettv->vval.v_list, matchid);
13802 }
13803#endif
13804}
13805
13806/*
13807 * "synstack(lnum, col)" function
13808 */
13809 static void
13810f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13811{
13812#ifdef FEAT_SYN_HL
13813 linenr_T lnum;
13814 colnr_T col;
13815 int i;
13816 int id;
13817#endif
13818
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013819 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013820
13821#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013822 lnum = tv_get_lnum(argvars); /* -1 on type error */
13823 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013824
13825 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13826 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13827 && rettv_list_alloc(rettv) != FAIL)
13828 {
13829 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13830 for (i = 0; ; ++i)
13831 {
13832 id = syn_get_stack_item(i);
13833 if (id < 0)
13834 break;
13835 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13836 break;
13837 }
13838 }
13839#endif
13840}
13841
13842 static void
13843get_cmd_output_as_rettv(
13844 typval_T *argvars,
13845 typval_T *rettv,
13846 int retlist)
13847{
13848 char_u *res = NULL;
13849 char_u *p;
13850 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013851 int err = FALSE;
13852 FILE *fd;
13853 list_T *list = NULL;
13854 int flags = SHELL_SILENT;
13855
13856 rettv->v_type = VAR_STRING;
13857 rettv->vval.v_string = NULL;
13858 if (check_restricted() || check_secure())
13859 goto errret;
13860
13861 if (argvars[1].v_type != VAR_UNKNOWN)
13862 {
13863 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013864 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013865 * command.
13866 */
13867 if ((infile = vim_tempname('i', TRUE)) == NULL)
13868 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013869 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013870 goto errret;
13871 }
13872
13873 fd = mch_fopen((char *)infile, WRITEBIN);
13874 if (fd == NULL)
13875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013876 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013877 goto errret;
13878 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013879 if (argvars[1].v_type == VAR_NUMBER)
13880 {
13881 linenr_T lnum;
13882 buf_T *buf;
13883
13884 buf = buflist_findnr(argvars[1].vval.v_number);
13885 if (buf == NULL)
13886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013887 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013888 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013889 goto errret;
13890 }
13891
13892 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13893 {
13894 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13895 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13896 {
13897 err = TRUE;
13898 break;
13899 }
13900 if (putc(NL, fd) == EOF)
13901 {
13902 err = TRUE;
13903 break;
13904 }
13905 }
13906 }
13907 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013908 {
13909 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13910 err = TRUE;
13911 }
13912 else
13913 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013914 size_t len;
13915 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013916
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013917 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013918 if (p == NULL)
13919 {
13920 fclose(fd);
13921 goto errret; /* type error; errmsg already given */
13922 }
13923 len = STRLEN(p);
13924 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13925 err = TRUE;
13926 }
13927 if (fclose(fd) != 0)
13928 err = TRUE;
13929 if (err)
13930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013931 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013932 goto errret;
13933 }
13934 }
13935
13936 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13937 * echoes typeahead, that messes up the display. */
13938 if (!msg_silent)
13939 flags += SHELL_COOKED;
13940
13941 if (retlist)
13942 {
13943 int len;
13944 listitem_T *li;
13945 char_u *s = NULL;
13946 char_u *start;
13947 char_u *end;
13948 int i;
13949
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013950 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013951 if (res == NULL)
13952 goto errret;
13953
13954 list = list_alloc();
13955 if (list == NULL)
13956 goto errret;
13957
13958 for (i = 0; i < len; ++i)
13959 {
13960 start = res + i;
13961 while (i < len && res[i] != NL)
13962 ++i;
13963 end = res + i;
13964
Bram Moolenaar964b3742019-05-24 18:54:09 +020013965 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013966 if (s == NULL)
13967 goto errret;
13968
13969 for (p = s; start < end; ++p, ++start)
13970 *p = *start == NUL ? NL : *start;
13971 *p = NUL;
13972
13973 li = listitem_alloc();
13974 if (li == NULL)
13975 {
13976 vim_free(s);
13977 goto errret;
13978 }
13979 li->li_tv.v_type = VAR_STRING;
13980 li->li_tv.v_lock = 0;
13981 li->li_tv.vval.v_string = s;
13982 list_append(list, li);
13983 }
13984
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013985 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013986 list = NULL;
13987 }
13988 else
13989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013990 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013991#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013992 /* translate <CR><NL> into <NL> */
13993 if (res != NULL)
13994 {
13995 char_u *s, *d;
13996
13997 d = res;
13998 for (s = res; *s; ++s)
13999 {
14000 if (s[0] == CAR && s[1] == NL)
14001 ++s;
14002 *d++ = *s;
14003 }
14004 *d = NUL;
14005 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014006#endif
14007 rettv->vval.v_string = res;
14008 res = NULL;
14009 }
14010
14011errret:
14012 if (infile != NULL)
14013 {
14014 mch_remove(infile);
14015 vim_free(infile);
14016 }
14017 if (res != NULL)
14018 vim_free(res);
14019 if (list != NULL)
14020 list_free(list);
14021}
14022
14023/*
14024 * "system()" function
14025 */
14026 static void
14027f_system(typval_T *argvars, typval_T *rettv)
14028{
14029 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14030}
14031
14032/*
14033 * "systemlist()" function
14034 */
14035 static void
14036f_systemlist(typval_T *argvars, typval_T *rettv)
14037{
14038 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14039}
14040
14041/*
14042 * "tabpagebuflist()" function
14043 */
14044 static void
14045f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14046{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014047 tabpage_T *tp;
14048 win_T *wp = NULL;
14049
14050 if (argvars[0].v_type == VAR_UNKNOWN)
14051 wp = firstwin;
14052 else
14053 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014054 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014055 if (tp != NULL)
14056 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14057 }
14058 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14059 {
14060 for (; wp != NULL; wp = wp->w_next)
14061 if (list_append_number(rettv->vval.v_list,
14062 wp->w_buffer->b_fnum) == FAIL)
14063 break;
14064 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014065}
14066
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014067/*
14068 * "tabpagenr()" function
14069 */
14070 static void
14071f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14072{
14073 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014074 char_u *arg;
14075
14076 if (argvars[0].v_type != VAR_UNKNOWN)
14077 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014078 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014079 nr = 0;
14080 if (arg != NULL)
14081 {
14082 if (STRCMP(arg, "$") == 0)
14083 nr = tabpage_index(NULL) - 1;
14084 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014085 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014086 }
14087 }
14088 else
14089 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014090 rettv->vval.v_number = nr;
14091}
14092
14093
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014094/*
14095 * Common code for tabpagewinnr() and winnr().
14096 */
14097 static int
14098get_winnr(tabpage_T *tp, typval_T *argvar)
14099{
14100 win_T *twin;
14101 int nr = 1;
14102 win_T *wp;
14103 char_u *arg;
14104
14105 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14106 if (argvar->v_type != VAR_UNKNOWN)
14107 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014108 int invalid_arg = FALSE;
14109
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014110 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014111 if (arg == NULL)
14112 nr = 0; /* type error; errmsg already given */
14113 else if (STRCMP(arg, "$") == 0)
14114 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14115 else if (STRCMP(arg, "#") == 0)
14116 {
14117 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14118 if (twin == NULL)
14119 nr = 0;
14120 }
14121 else
14122 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014123 long count;
14124 char_u *endp;
14125
14126 // Extract the window count (if specified). e.g. winnr('3j')
14127 count = strtol((char *)arg, (char **)&endp, 10);
14128 if (count <= 0)
14129 count = 1; // if count is not specified, default to 1
14130 if (endp != NULL && *endp != '\0')
14131 {
14132 if (STRCMP(endp, "j") == 0)
14133 twin = win_vert_neighbor(tp, twin, FALSE, count);
14134 else if (STRCMP(endp, "k") == 0)
14135 twin = win_vert_neighbor(tp, twin, TRUE, count);
14136 else if (STRCMP(endp, "h") == 0)
14137 twin = win_horz_neighbor(tp, twin, TRUE, count);
14138 else if (STRCMP(endp, "l") == 0)
14139 twin = win_horz_neighbor(tp, twin, FALSE, count);
14140 else
14141 invalid_arg = TRUE;
14142 }
14143 else
14144 invalid_arg = TRUE;
14145 }
14146
14147 if (invalid_arg)
14148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014149 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014150 nr = 0;
14151 }
14152 }
14153
14154 if (nr > 0)
14155 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14156 wp != twin; wp = wp->w_next)
14157 {
14158 if (wp == NULL)
14159 {
14160 /* didn't find it in this tabpage */
14161 nr = 0;
14162 break;
14163 }
14164 ++nr;
14165 }
14166 return nr;
14167}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168
14169/*
14170 * "tabpagewinnr()" function
14171 */
14172 static void
14173f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14174{
14175 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014176 tabpage_T *tp;
14177
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014178 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014179 if (tp == NULL)
14180 nr = 0;
14181 else
14182 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014183 rettv->vval.v_number = nr;
14184}
14185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014186/*
14187 * "tagfiles()" function
14188 */
14189 static void
14190f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14191{
14192 char_u *fname;
14193 tagname_T tn;
14194 int first;
14195
14196 if (rettv_list_alloc(rettv) == FAIL)
14197 return;
14198 fname = alloc(MAXPATHL);
14199 if (fname == NULL)
14200 return;
14201
14202 for (first = TRUE; ; first = FALSE)
14203 if (get_tagfname(&tn, first, fname) == FAIL
14204 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14205 break;
14206 tagname_free(&tn);
14207 vim_free(fname);
14208}
14209
14210/*
14211 * "taglist()" function
14212 */
14213 static void
14214f_taglist(typval_T *argvars, typval_T *rettv)
14215{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014216 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014217 char_u *tag_pattern;
14218
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014219 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014220
14221 rettv->vval.v_number = FALSE;
14222 if (*tag_pattern == NUL)
14223 return;
14224
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014225 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014226 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014227 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014228 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014229}
14230
14231/*
14232 * "tempname()" function
14233 */
14234 static void
14235f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14236{
14237 static int x = 'A';
14238
14239 rettv->v_type = VAR_STRING;
14240 rettv->vval.v_string = vim_tempname(x, FALSE);
14241
14242 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14243 * names. Skip 'I' and 'O', they are used for shell redirection. */
14244 do
14245 {
14246 if (x == 'Z')
14247 x = '0';
14248 else if (x == '9')
14249 x = 'A';
14250 else
14251 {
14252#ifdef EBCDIC
14253 if (x == 'I')
14254 x = 'J';
14255 else if (x == 'R')
14256 x = 'S';
14257 else
14258#endif
14259 ++x;
14260 }
14261 } while (x == 'I' || x == 'O');
14262}
14263
14264#ifdef FEAT_FLOAT
14265/*
14266 * "tan()" function
14267 */
14268 static void
14269f_tan(typval_T *argvars, typval_T *rettv)
14270{
14271 float_T f = 0.0;
14272
14273 rettv->v_type = VAR_FLOAT;
14274 if (get_float_arg(argvars, &f) == OK)
14275 rettv->vval.v_float = tan(f);
14276 else
14277 rettv->vval.v_float = 0.0;
14278}
14279
14280/*
14281 * "tanh()" function
14282 */
14283 static void
14284f_tanh(typval_T *argvars, typval_T *rettv)
14285{
14286 float_T f = 0.0;
14287
14288 rettv->v_type = VAR_FLOAT;
14289 if (get_float_arg(argvars, &f) == OK)
14290 rettv->vval.v_float = tanh(f);
14291 else
14292 rettv->vval.v_float = 0.0;
14293}
14294#endif
14295
14296/*
14297 * "test_alloc_fail(id, countdown, repeat)" function
14298 */
14299 static void
14300f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14301{
14302 if (argvars[0].v_type != VAR_NUMBER
14303 || argvars[0].vval.v_number <= 0
14304 || argvars[1].v_type != VAR_NUMBER
14305 || argvars[1].vval.v_number < 0
14306 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014307 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014308 else
14309 {
14310 alloc_fail_id = argvars[0].vval.v_number;
14311 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014312 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014313 alloc_fail_countdown = argvars[1].vval.v_number;
14314 alloc_fail_repeat = argvars[2].vval.v_number;
14315 did_outofmem_msg = FALSE;
14316 }
14317}
14318
14319/*
14320 * "test_autochdir()"
14321 */
14322 static void
14323f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14324{
14325#if defined(FEAT_AUTOCHDIR)
14326 test_autochdir = TRUE;
14327#endif
14328}
14329
14330/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014331 * "test_feedinput()"
14332 */
14333 static void
14334f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14335{
14336#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014337 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014338
14339 if (val != NULL)
14340 {
14341 trash_input_buf();
14342 add_to_input_buf_csi(val, (int)STRLEN(val));
14343 }
14344#endif
14345}
14346
14347/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014348 * "test_getvalue({name})" function
14349 */
14350 static void
14351f_test_getvalue(typval_T *argvars, typval_T *rettv)
14352{
14353 if (argvars[0].v_type != VAR_STRING)
14354 emsg(_(e_invarg));
14355 else
14356 {
14357 char_u *name = tv_get_string(&argvars[0]);
14358
14359 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14360 rettv->vval.v_number = need_fileinfo;
14361 else
14362 semsg(_(e_invarg2), name);
14363 }
14364}
14365
14366/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014367 * "test_option_not_set({name})" function
14368 */
14369 static void
14370f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14371{
14372 char_u *name = (char_u *)"";
14373
14374 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014375 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014376 else
14377 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014378 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014379 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014380 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014381 }
14382}
14383
14384/*
14385 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014386 */
14387 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014388f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014389{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014390 char_u *name = (char_u *)"";
14391 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014392 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014393
14394 if (argvars[0].v_type != VAR_STRING
14395 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014396 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014397 else
14398 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014399 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014400 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014401
14402 if (STRCMP(name, (char_u *)"redraw") == 0)
14403 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014404 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14405 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014406 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14407 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014408 else if (STRCMP(name, (char_u *)"starting") == 0)
14409 {
14410 if (val)
14411 {
14412 if (save_starting < 0)
14413 save_starting = starting;
14414 starting = 0;
14415 }
14416 else
14417 {
14418 starting = save_starting;
14419 save_starting = -1;
14420 }
14421 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014422 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14423 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014424 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14425 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014426 else if (STRCMP(name, (char_u *)"ALL") == 0)
14427 {
14428 disable_char_avail_for_testing = FALSE;
14429 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014430 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014431 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014432 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014433 if (save_starting >= 0)
14434 {
14435 starting = save_starting;
14436 save_starting = -1;
14437 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014438 }
14439 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014440 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014441 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014442}
14443
14444/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014445 * "test_refcount({expr})" function
14446 */
14447 static void
14448f_test_refcount(typval_T *argvars, typval_T *rettv)
14449{
14450 int retval = -1;
14451
14452 switch (argvars[0].v_type)
14453 {
14454 case VAR_UNKNOWN:
14455 case VAR_NUMBER:
14456 case VAR_FLOAT:
14457 case VAR_SPECIAL:
14458 case VAR_STRING:
14459 break;
14460 case VAR_JOB:
14461#ifdef FEAT_JOB_CHANNEL
14462 if (argvars[0].vval.v_job != NULL)
14463 retval = argvars[0].vval.v_job->jv_refcount - 1;
14464#endif
14465 break;
14466 case VAR_CHANNEL:
14467#ifdef FEAT_JOB_CHANNEL
14468 if (argvars[0].vval.v_channel != NULL)
14469 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14470#endif
14471 break;
14472 case VAR_FUNC:
14473 if (argvars[0].vval.v_string != NULL)
14474 {
14475 ufunc_T *fp;
14476
14477 fp = find_func(argvars[0].vval.v_string);
14478 if (fp != NULL)
14479 retval = fp->uf_refcount;
14480 }
14481 break;
14482 case VAR_PARTIAL:
14483 if (argvars[0].vval.v_partial != NULL)
14484 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14485 break;
14486 case VAR_BLOB:
14487 if (argvars[0].vval.v_blob != NULL)
14488 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14489 break;
14490 case VAR_LIST:
14491 if (argvars[0].vval.v_list != NULL)
14492 retval = argvars[0].vval.v_list->lv_refcount - 1;
14493 break;
14494 case VAR_DICT:
14495 if (argvars[0].vval.v_dict != NULL)
14496 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14497 break;
14498 }
14499
14500 rettv->v_type = VAR_NUMBER;
14501 rettv->vval.v_number = retval;
14502
14503}
14504
14505/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014506 * "test_garbagecollect_now()" function
14507 */
14508 static void
14509f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14510{
14511 /* This is dangerous, any Lists and Dicts used internally may be freed
14512 * while still in use. */
14513 garbage_collect(TRUE);
14514}
14515
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014516/*
14517 * "test_ignore_error()" function
14518 */
14519 static void
14520f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14521{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014522 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014523}
14524
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014525 static void
14526f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14527{
14528 rettv->v_type = VAR_BLOB;
14529 rettv->vval.v_blob = NULL;
14530}
14531
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014532#ifdef FEAT_JOB_CHANNEL
14533 static void
14534f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14535{
14536 rettv->v_type = VAR_CHANNEL;
14537 rettv->vval.v_channel = NULL;
14538}
14539#endif
14540
14541 static void
14542f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14543{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014544 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014545}
14546
14547#ifdef FEAT_JOB_CHANNEL
14548 static void
14549f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14550{
14551 rettv->v_type = VAR_JOB;
14552 rettv->vval.v_job = NULL;
14553}
14554#endif
14555
14556 static void
14557f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14558{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014559 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014560}
14561
14562 static void
14563f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14564{
14565 rettv->v_type = VAR_PARTIAL;
14566 rettv->vval.v_partial = NULL;
14567}
14568
14569 static void
14570f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14571{
14572 rettv->v_type = VAR_STRING;
14573 rettv->vval.v_string = NULL;
14574}
14575
Bram Moolenaarab186732018-09-14 21:27:06 +020014576#ifdef FEAT_GUI
14577 static void
14578f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14579{
14580 char_u *which;
14581 long value;
14582 int dragging;
14583 scrollbar_T *sb = NULL;
14584
14585 if (argvars[0].v_type != VAR_STRING
14586 || (argvars[1].v_type) != VAR_NUMBER
14587 || (argvars[2].v_type) != VAR_NUMBER)
14588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014589 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014590 return;
14591 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014592 which = tv_get_string(&argvars[0]);
14593 value = tv_get_number(&argvars[1]);
14594 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014595
14596 if (STRCMP(which, "left") == 0)
14597 sb = &curwin->w_scrollbars[SBAR_LEFT];
14598 else if (STRCMP(which, "right") == 0)
14599 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14600 else if (STRCMP(which, "hor") == 0)
14601 sb = &gui.bottom_sbar;
14602 if (sb == NULL)
14603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014604 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014605 return;
14606 }
14607 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014608# ifndef USE_ON_FLY_SCROLL
14609 // need to loop through normal_cmd() to handle the scroll events
14610 exec_normal(FALSE, TRUE, FALSE);
14611# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014612}
14613#endif
14614
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014615#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014616 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014617f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14618{
14619 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14620 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14621}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014622#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014623
14624 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014625f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14626{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014627 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014628}
14629
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014630/*
14631 * Get a callback from "arg". It can be a Funcref or a function name.
14632 * When "arg" is zero return an empty string.
14633 * Return NULL for an invalid argument.
14634 */
14635 char_u *
14636get_callback(typval_T *arg, partial_T **pp)
14637{
14638 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14639 {
14640 *pp = arg->vval.v_partial;
14641 ++(*pp)->pt_refcount;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014642 return partial_name(*pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014643 }
14644 *pp = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +020014645 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014646 {
14647 func_ref(arg->vval.v_string);
14648 return arg->vval.v_string;
14649 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014650 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14651 return (char_u *)"";
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014652 emsg(_("E921: Invalid callback argument"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014653 return NULL;
14654}
14655
14656/*
Bram Moolenaard5359b22018-04-05 22:44:39 +020014657 * Unref/free "callback" and "partial" returned by get_callback().
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014658 */
14659 void
14660free_callback(char_u *callback, partial_T *partial)
14661{
14662 if (partial != NULL)
14663 partial_unref(partial);
14664 else if (callback != NULL)
14665 {
14666 func_unref(callback);
14667 vim_free(callback);
14668 }
14669}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670
14671#ifdef FEAT_TIMERS
14672/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014673 * "timer_info([timer])" function
14674 */
14675 static void
14676f_timer_info(typval_T *argvars, typval_T *rettv)
14677{
14678 timer_T *timer = NULL;
14679
14680 if (rettv_list_alloc(rettv) != OK)
14681 return;
14682 if (argvars[0].v_type != VAR_UNKNOWN)
14683 {
14684 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014685 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014686 else
14687 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014688 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014689 if (timer != NULL)
14690 add_timer_info(rettv, timer);
14691 }
14692 }
14693 else
14694 add_timer_info_all(rettv);
14695}
14696
14697/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014698 * "timer_pause(timer, paused)" function
14699 */
14700 static void
14701f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14702{
14703 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014704 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014705
14706 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014707 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014708 else
14709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014710 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014711 if (timer != NULL)
14712 timer->tr_paused = paused;
14713 }
14714}
14715
14716/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014717 * "timer_start(time, callback [, options])" function
14718 */
14719 static void
14720f_timer_start(typval_T *argvars, typval_T *rettv)
14721{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014722 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014723 timer_T *timer;
14724 int repeat = 0;
14725 char_u *callback;
14726 dict_T *dict;
14727 partial_T *partial;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014728
Bram Moolenaar75537a92016-09-05 22:45:28 +020014729 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014730 if (check_secure())
14731 return;
14732 if (argvars[2].v_type != VAR_UNKNOWN)
14733 {
14734 if (argvars[2].v_type != VAR_DICT
14735 || (dict = argvars[2].vval.v_dict) == NULL)
14736 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014737 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014738 return;
14739 }
14740 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014741 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014742 }
14743
Bram Moolenaar75537a92016-09-05 22:45:28 +020014744 callback = get_callback(&argvars[1], &partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014745 if (callback == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014746 return;
14747
14748 timer = create_timer(msec, repeat);
14749 if (timer == NULL)
14750 free_callback(callback, partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014751 else
14752 {
Bram Moolenaar26fe0d52016-09-10 14:27:30 +020014753 if (partial == NULL)
Bram Moolenaar3ab14352016-07-30 22:32:11 +020014754 timer->tr_callback = vim_strsave(callback);
14755 else
14756 /* pointer into the partial */
14757 timer->tr_callback = callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014758 timer->tr_partial = partial;
14759 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014760 }
14761}
14762
14763/*
14764 * "timer_stop(timer)" function
14765 */
14766 static void
14767f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14768{
14769 timer_T *timer;
14770
14771 if (argvars[0].v_type != VAR_NUMBER)
14772 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014773 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014774 return;
14775 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014776 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014777 if (timer != NULL)
14778 stop_timer(timer);
14779}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014780
14781/*
14782 * "timer_stopall()" function
14783 */
14784 static void
14785f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14786{
14787 stop_all_timers();
14788}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014789#endif
14790
14791/*
14792 * "tolower(string)" function
14793 */
14794 static void
14795f_tolower(typval_T *argvars, typval_T *rettv)
14796{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014797 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014798 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014799}
14800
14801/*
14802 * "toupper(string)" function
14803 */
14804 static void
14805f_toupper(typval_T *argvars, typval_T *rettv)
14806{
14807 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014808 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014809}
14810
14811/*
14812 * "tr(string, fromstr, tostr)" function
14813 */
14814 static void
14815f_tr(typval_T *argvars, typval_T *rettv)
14816{
14817 char_u *in_str;
14818 char_u *fromstr;
14819 char_u *tostr;
14820 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014821 int inlen;
14822 int fromlen;
14823 int tolen;
14824 int idx;
14825 char_u *cpstr;
14826 int cplen;
14827 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014828 char_u buf[NUMBUFLEN];
14829 char_u buf2[NUMBUFLEN];
14830 garray_T ga;
14831
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014832 in_str = tv_get_string(&argvars[0]);
14833 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14834 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014835
14836 /* Default return value: empty string. */
14837 rettv->v_type = VAR_STRING;
14838 rettv->vval.v_string = NULL;
14839 if (fromstr == NULL || tostr == NULL)
14840 return; /* type error; errmsg already given */
14841 ga_init2(&ga, (int)sizeof(char), 80);
14842
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014843 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014844 /* not multi-byte: fromstr and tostr must be the same length */
14845 if (STRLEN(fromstr) != STRLEN(tostr))
14846 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014847error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014848 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014849 ga_clear(&ga);
14850 return;
14851 }
14852
14853 /* fromstr and tostr have to contain the same number of chars */
14854 while (*in_str != NUL)
14855 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014856 if (has_mbyte)
14857 {
14858 inlen = (*mb_ptr2len)(in_str);
14859 cpstr = in_str;
14860 cplen = inlen;
14861 idx = 0;
14862 for (p = fromstr; *p != NUL; p += fromlen)
14863 {
14864 fromlen = (*mb_ptr2len)(p);
14865 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14866 {
14867 for (p = tostr; *p != NUL; p += tolen)
14868 {
14869 tolen = (*mb_ptr2len)(p);
14870 if (idx-- == 0)
14871 {
14872 cplen = tolen;
14873 cpstr = p;
14874 break;
14875 }
14876 }
14877 if (*p == NUL) /* tostr is shorter than fromstr */
14878 goto error;
14879 break;
14880 }
14881 ++idx;
14882 }
14883
14884 if (first && cpstr == in_str)
14885 {
14886 /* Check that fromstr and tostr have the same number of
14887 * (multi-byte) characters. Done only once when a character
14888 * of in_str doesn't appear in fromstr. */
14889 first = FALSE;
14890 for (p = tostr; *p != NUL; p += tolen)
14891 {
14892 tolen = (*mb_ptr2len)(p);
14893 --idx;
14894 }
14895 if (idx != 0)
14896 goto error;
14897 }
14898
14899 (void)ga_grow(&ga, cplen);
14900 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14901 ga.ga_len += cplen;
14902
14903 in_str += inlen;
14904 }
14905 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014906 {
14907 /* When not using multi-byte chars we can do it faster. */
14908 p = vim_strchr(fromstr, *in_str);
14909 if (p != NULL)
14910 ga_append(&ga, tostr[p - fromstr]);
14911 else
14912 ga_append(&ga, *in_str);
14913 ++in_str;
14914 }
14915 }
14916
14917 /* add a terminating NUL */
14918 (void)ga_grow(&ga, 1);
14919 ga_append(&ga, NUL);
14920
14921 rettv->vval.v_string = ga.ga_data;
14922}
14923
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014924/*
14925 * "trim({expr})" function
14926 */
14927 static void
14928f_trim(typval_T *argvars, typval_T *rettv)
14929{
14930 char_u buf1[NUMBUFLEN];
14931 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014932 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014933 char_u *mask = NULL;
14934 char_u *tail;
14935 char_u *prev;
14936 char_u *p;
14937 int c1;
14938
14939 rettv->v_type = VAR_STRING;
14940 if (head == NULL)
14941 {
14942 rettv->vval.v_string = NULL;
14943 return;
14944 }
14945
14946 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014947 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014948
14949 while (*head != NUL)
14950 {
14951 c1 = PTR2CHAR(head);
14952 if (mask == NULL)
14953 {
14954 if (c1 > ' ' && c1 != 0xa0)
14955 break;
14956 }
14957 else
14958 {
14959 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14960 if (c1 == PTR2CHAR(p))
14961 break;
14962 if (*p == NUL)
14963 break;
14964 }
14965 MB_PTR_ADV(head);
14966 }
14967
14968 for (tail = head + STRLEN(head); tail > head; tail = prev)
14969 {
14970 prev = tail;
14971 MB_PTR_BACK(head, prev);
14972 c1 = PTR2CHAR(prev);
14973 if (mask == NULL)
14974 {
14975 if (c1 > ' ' && c1 != 0xa0)
14976 break;
14977 }
14978 else
14979 {
14980 for (p = mask; *p != NUL; MB_PTR_ADV(p))
14981 if (c1 == PTR2CHAR(p))
14982 break;
14983 if (*p == NUL)
14984 break;
14985 }
14986 }
14987 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
14988}
14989
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014990#ifdef FEAT_FLOAT
14991/*
14992 * "trunc({float})" function
14993 */
14994 static void
14995f_trunc(typval_T *argvars, typval_T *rettv)
14996{
14997 float_T f = 0.0;
14998
14999 rettv->v_type = VAR_FLOAT;
15000 if (get_float_arg(argvars, &f) == OK)
15001 /* trunc() is not in C90, use floor() or ceil() instead. */
15002 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15003 else
15004 rettv->vval.v_float = 0.0;
15005}
15006#endif
15007
15008/*
15009 * "type(expr)" function
15010 */
15011 static void
15012f_type(typval_T *argvars, typval_T *rettv)
15013{
15014 int n = -1;
15015
15016 switch (argvars[0].v_type)
15017 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015018 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15019 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015020 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015021 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15022 case VAR_LIST: n = VAR_TYPE_LIST; break;
15023 case VAR_DICT: n = VAR_TYPE_DICT; break;
15024 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015025 case VAR_SPECIAL:
15026 if (argvars[0].vval.v_number == VVAL_FALSE
15027 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015028 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015029 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015030 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015031 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015032 case VAR_JOB: n = VAR_TYPE_JOB; break;
15033 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015034 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015035 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015036 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015037 n = -1;
15038 break;
15039 }
15040 rettv->vval.v_number = n;
15041}
15042
15043/*
15044 * "undofile(name)" function
15045 */
15046 static void
15047f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15048{
15049 rettv->v_type = VAR_STRING;
15050#ifdef FEAT_PERSISTENT_UNDO
15051 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015052 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015053
15054 if (*fname == NUL)
15055 {
15056 /* If there is no file name there will be no undo file. */
15057 rettv->vval.v_string = NULL;
15058 }
15059 else
15060 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015061 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015062
15063 if (ffname != NULL)
15064 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15065 vim_free(ffname);
15066 }
15067 }
15068#else
15069 rettv->vval.v_string = NULL;
15070#endif
15071}
15072
15073/*
15074 * "undotree()" function
15075 */
15076 static void
15077f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15078{
15079 if (rettv_dict_alloc(rettv) == OK)
15080 {
15081 dict_T *dict = rettv->vval.v_dict;
15082 list_T *list;
15083
Bram Moolenaare0be1672018-07-08 16:50:37 +020015084 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15085 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15086 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15087 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15088 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15089 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015090
15091 list = list_alloc();
15092 if (list != NULL)
15093 {
15094 u_eval_tree(curbuf->b_u_oldhead, list);
15095 dict_add_list(dict, "entries", list);
15096 }
15097 }
15098}
15099
15100/*
15101 * "values(dict)" function
15102 */
15103 static void
15104f_values(typval_T *argvars, typval_T *rettv)
15105{
15106 dict_list(argvars, rettv, 1);
15107}
15108
15109/*
15110 * "virtcol(string)" function
15111 */
15112 static void
15113f_virtcol(typval_T *argvars, typval_T *rettv)
15114{
15115 colnr_T vcol = 0;
15116 pos_T *fp;
15117 int fnum = curbuf->b_fnum;
15118
15119 fp = var2fpos(&argvars[0], FALSE, &fnum);
15120 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15121 && fnum == curbuf->b_fnum)
15122 {
15123 getvvcol(curwin, fp, NULL, NULL, &vcol);
15124 ++vcol;
15125 }
15126
15127 rettv->vval.v_number = vcol;
15128}
15129
15130/*
15131 * "visualmode()" function
15132 */
15133 static void
15134f_visualmode(typval_T *argvars, typval_T *rettv)
15135{
15136 char_u str[2];
15137
15138 rettv->v_type = VAR_STRING;
15139 str[0] = curbuf->b_visual_mode_eval;
15140 str[1] = NUL;
15141 rettv->vval.v_string = vim_strsave(str);
15142
15143 /* A non-zero number or non-empty string argument: reset mode. */
15144 if (non_zero_arg(&argvars[0]))
15145 curbuf->b_visual_mode_eval = NUL;
15146}
15147
15148/*
15149 * "wildmenumode()" function
15150 */
15151 static void
15152f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15153{
15154#ifdef FEAT_WILDMENU
15155 if (wild_menu_showing)
15156 rettv->vval.v_number = 1;
15157#endif
15158}
15159
15160/*
15161 * "winbufnr(nr)" function
15162 */
15163 static void
15164f_winbufnr(typval_T *argvars, typval_T *rettv)
15165{
15166 win_T *wp;
15167
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015168 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015169 if (wp == NULL)
15170 rettv->vval.v_number = -1;
15171 else
15172 rettv->vval.v_number = wp->w_buffer->b_fnum;
15173}
15174
15175/*
15176 * "wincol()" function
15177 */
15178 static void
15179f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15180{
15181 validate_cursor();
15182 rettv->vval.v_number = curwin->w_wcol + 1;
15183}
15184
15185/*
15186 * "winheight(nr)" function
15187 */
15188 static void
15189f_winheight(typval_T *argvars, typval_T *rettv)
15190{
15191 win_T *wp;
15192
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015193 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015194 if (wp == NULL)
15195 rettv->vval.v_number = -1;
15196 else
15197 rettv->vval.v_number = wp->w_height;
15198}
15199
15200/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015201 * "winlayout()" function
15202 */
15203 static void
15204f_winlayout(typval_T *argvars, typval_T *rettv)
15205{
15206 tabpage_T *tp;
15207
15208 if (rettv_list_alloc(rettv) != OK)
15209 return;
15210
15211 if (argvars[0].v_type == VAR_UNKNOWN)
15212 tp = curtab;
15213 else
15214 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015215 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015216 if (tp == NULL)
15217 return;
15218 }
15219
15220 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15221}
15222
15223/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015224 * "winline()" function
15225 */
15226 static void
15227f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15228{
15229 validate_cursor();
15230 rettv->vval.v_number = curwin->w_wrow + 1;
15231}
15232
15233/*
15234 * "winnr()" function
15235 */
15236 static void
15237f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15238{
15239 int nr = 1;
15240
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015241 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015242 rettv->vval.v_number = nr;
15243}
15244
15245/*
15246 * "winrestcmd()" function
15247 */
15248 static void
15249f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15250{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015251 win_T *wp;
15252 int winnr = 1;
15253 garray_T ga;
15254 char_u buf[50];
15255
15256 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015257 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015258 {
15259 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15260 ga_concat(&ga, buf);
15261 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15262 ga_concat(&ga, buf);
15263 ++winnr;
15264 }
15265 ga_append(&ga, NUL);
15266
15267 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015268 rettv->v_type = VAR_STRING;
15269}
15270
15271/*
15272 * "winrestview()" function
15273 */
15274 static void
15275f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15276{
15277 dict_T *dict;
15278
15279 if (argvars[0].v_type != VAR_DICT
15280 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015281 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015282 else
15283 {
15284 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015285 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015286 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015287 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015288 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015289 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015290 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15291 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015292 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015293 curwin->w_set_curswant = FALSE;
15294 }
15295
15296 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015297 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015298#ifdef FEAT_DIFF
15299 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015300 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015301#endif
15302 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015303 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015304 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015305 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015306
15307 check_cursor();
15308 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015309 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015310 changed_window_setting();
15311
15312 if (curwin->w_topline <= 0)
15313 curwin->w_topline = 1;
15314 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15315 curwin->w_topline = curbuf->b_ml.ml_line_count;
15316#ifdef FEAT_DIFF
15317 check_topfill(curwin, TRUE);
15318#endif
15319 }
15320}
15321
15322/*
15323 * "winsaveview()" function
15324 */
15325 static void
15326f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15327{
15328 dict_T *dict;
15329
15330 if (rettv_dict_alloc(rettv) == FAIL)
15331 return;
15332 dict = rettv->vval.v_dict;
15333
Bram Moolenaare0be1672018-07-08 16:50:37 +020015334 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15335 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015336 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015337 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015338 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015339
Bram Moolenaare0be1672018-07-08 16:50:37 +020015340 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015341#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015342 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015343#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015344 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15345 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015346}
15347
15348/*
15349 * "winwidth(nr)" function
15350 */
15351 static void
15352f_winwidth(typval_T *argvars, typval_T *rettv)
15353{
15354 win_T *wp;
15355
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015356 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015357 if (wp == NULL)
15358 rettv->vval.v_number = -1;
15359 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015360 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015361}
15362
15363/*
15364 * "wordcount()" function
15365 */
15366 static void
15367f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15368{
15369 if (rettv_dict_alloc(rettv) == FAIL)
15370 return;
15371 cursor_pos_info(rettv->vval.v_dict);
15372}
15373
15374/*
15375 * "writefile()" function
15376 */
15377 static void
15378f_writefile(typval_T *argvars, typval_T *rettv)
15379{
15380 int binary = FALSE;
15381 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015382#ifdef HAVE_FSYNC
15383 int do_fsync = p_fs;
15384#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015385 char_u *fname;
15386 FILE *fd;
15387 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015388 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015389 list_T *list = NULL;
15390 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015392 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015393 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015394 return;
15395
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015396 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015397 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015398 list = argvars[0].vval.v_list;
15399 if (list == NULL)
15400 return;
15401 for (li = list->lv_first; li != NULL; li = li->li_next)
15402 if (tv_get_string_chk(&li->li_tv) == NULL)
15403 return;
15404 }
15405 else if (argvars[0].v_type == VAR_BLOB)
15406 {
15407 blob = argvars[0].vval.v_blob;
15408 if (blob == NULL)
15409 return;
15410 }
15411 else
15412 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015413 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015414 return;
15415 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015416
15417 if (argvars[2].v_type != VAR_UNKNOWN)
15418 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015419 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015420
15421 if (arg2 == NULL)
15422 return;
15423 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015424 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015425 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015426 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015427#ifdef HAVE_FSYNC
15428 if (vim_strchr(arg2, 's') != NULL)
15429 do_fsync = TRUE;
15430 else if (vim_strchr(arg2, 'S') != NULL)
15431 do_fsync = FALSE;
15432#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015433 }
15434
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015435 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015436 if (fname == NULL)
15437 return;
15438
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015439 /* Always open the file in binary mode, library functions have a mind of
15440 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015441 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15442 append ? APPENDBIN : WRITEBIN)) == NULL)
15443 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015444 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015445 ret = -1;
15446 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015447 else if (blob)
15448 {
15449 if (write_blob(fd, blob) == FAIL)
15450 ret = -1;
15451#ifdef HAVE_FSYNC
15452 else if (do_fsync)
15453 // Ignore the error, the user wouldn't know what to do about it.
15454 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015455 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015456#endif
15457 fclose(fd);
15458 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015459 else
15460 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015461 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015462 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015463#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015464 else if (do_fsync)
15465 /* Ignore the error, the user wouldn't know what to do about it.
15466 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015467 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015468#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015469 fclose(fd);
15470 }
15471
15472 rettv->vval.v_number = ret;
15473}
15474
15475/*
15476 * "xor(expr, expr)" function
15477 */
15478 static void
15479f_xor(typval_T *argvars, typval_T *rettv)
15480{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015481 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15482 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015483}
15484
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015485#endif /* FEAT_EVAL */