blob: ccb8b3e89092e1d91ae3d305f499f6479d1d09d5 [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
Bram Moolenaarcc31ad92019-05-30 19:25:06 +0200812 {"popup_atcursor", 2, 2, f_popup_atcursor},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200813 {"popup_close", 1, 1, f_popup_close},
814 {"popup_create", 2, 2, f_popup_create},
Bram Moolenaar8c2a6002019-05-30 14:29:45 +0200815 {"popup_getoptions", 1, 1, f_popup_getoptions},
Bram Moolenaarccd6e342019-05-30 22:35:18 +0200816 {"popup_getpos", 1, 1, f_popup_getpos},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200817 {"popup_hide", 1, 1, f_popup_hide},
Bram Moolenaar60cdb302019-05-27 21:54:10 +0200818 {"popup_move", 2, 2, f_popup_move},
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +0200819 {"popup_show", 1, 1, f_popup_show},
Bram Moolenaar4d784b22019-05-25 19:51:39 +0200820#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200821#ifdef FEAT_FLOAT
822 {"pow", 2, 2, f_pow},
823#endif
824 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaarc71807d2018-03-03 15:06:52 +0100825 {"printf", 1, 19, f_printf},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200826#ifdef FEAT_JOB_CHANNEL
827 {"prompt_setcallback", 2, 2, f_prompt_setcallback},
Bram Moolenaar0e5979a2018-06-17 19:36:33 +0200828 {"prompt_setinterrupt", 2, 2, f_prompt_setinterrupt},
Bram Moolenaarf2732452018-06-03 14:47:35 +0200829 {"prompt_setprompt", 2, 2, f_prompt_setprompt},
830#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100831#ifdef FEAT_TEXT_PROP
832 {"prop_add", 3, 3, f_prop_add},
833 {"prop_clear", 1, 3, f_prop_clear},
834 {"prop_list", 1, 2, f_prop_list},
Bram Moolenaar0a2f5782019-03-22 13:20:43 +0100835 {"prop_remove", 1, 3, f_prop_remove},
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100836 {"prop_type_add", 2, 2, f_prop_type_add},
837 {"prop_type_change", 2, 2, f_prop_type_change},
838 {"prop_type_delete", 1, 2, f_prop_type_delete},
839 {"prop_type_get", 1, 2, f_prop_type_get},
840 {"prop_type_list", 0, 1, f_prop_type_list},
841#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200842 {"pumvisible", 0, 0, f_pumvisible},
843#ifdef FEAT_PYTHON3
844 {"py3eval", 1, 1, f_py3eval},
845#endif
846#ifdef FEAT_PYTHON
847 {"pyeval", 1, 1, f_pyeval},
848#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100849#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
850 {"pyxeval", 1, 1, f_pyxeval},
851#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200852 {"range", 1, 3, f_range},
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200853 {"readdir", 1, 2, f_readdir},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200854 {"readfile", 1, 3, f_readfile},
Bram Moolenaar0b6d9112018-05-22 20:35:17 +0200855 {"reg_executing", 0, 0, f_reg_executing},
856 {"reg_recording", 0, 0, f_reg_recording},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200857 {"reltime", 0, 2, f_reltime},
858#ifdef FEAT_FLOAT
859 {"reltimefloat", 1, 1, f_reltimefloat},
860#endif
861 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100862 {"remote_expr", 2, 4, f_remote_expr},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200863 {"remote_foreground", 1, 1, f_remote_foreground},
864 {"remote_peek", 1, 2, f_remote_peek},
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +0100865 {"remote_read", 1, 2, f_remote_read},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200866 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar7416f3e2017-03-18 18:10:13 +0100867 {"remote_startserver", 1, 1, f_remote_startserver},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200868 {"remove", 2, 3, f_remove},
869 {"rename", 2, 2, f_rename},
870 {"repeat", 2, 2, f_repeat},
871 {"resolve", 1, 1, f_resolve},
872 {"reverse", 1, 1, f_reverse},
873#ifdef FEAT_FLOAT
874 {"round", 1, 1, f_round},
875#endif
Bram Moolenaare99be0e2019-03-26 22:51:09 +0100876#ifdef FEAT_RUBY
877 {"rubyeval", 1, 1, f_rubyeval},
878#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200879 {"screenattr", 2, 2, f_screenattr},
880 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100881 {"screenchars", 2, 2, f_screenchars},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200882 {"screencol", 0, 0, f_screencol},
883 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar2912abb2019-03-29 14:16:42 +0100884 {"screenstring", 2, 2, f_screenstring},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200885 {"search", 1, 4, f_search},
886 {"searchdecl", 1, 3, f_searchdecl},
887 {"searchpair", 3, 7, f_searchpair},
888 {"searchpairpos", 3, 7, f_searchpairpos},
889 {"searchpos", 1, 4, f_searchpos},
890 {"server2client", 2, 2, f_server2client},
891 {"serverlist", 0, 0, f_serverlist},
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +0200892 {"setbufline", 3, 3, f_setbufline},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200893 {"setbufvar", 3, 3, f_setbufvar},
894 {"setcharsearch", 1, 1, f_setcharsearch},
895 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar691ddee2019-05-09 14:52:41 +0200896 {"setenv", 2, 2, f_setenv},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200897 {"setfperm", 2, 2, f_setfperm},
898 {"setline", 2, 2, f_setline},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200899 {"setloclist", 2, 4, f_setloclist},
Bram Moolenaaraff74912019-03-30 18:11:49 +0100900 {"setmatches", 1, 2, f_setmatches},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200901 {"setpos", 2, 2, f_setpos},
Bram Moolenaard823fa92016-08-12 16:29:27 +0200902 {"setqflist", 1, 3, f_setqflist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200903 {"setreg", 2, 3, f_setreg},
904 {"settabvar", 3, 3, f_settabvar},
905 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaarf49cc602018-11-11 15:21:05 +0100906 {"settagstack", 2, 3, f_settagstack},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200907 {"setwinvar", 3, 3, f_setwinvar},
908#ifdef FEAT_CRYPT
909 {"sha256", 1, 1, f_sha256},
910#endif
911 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaarf9514162018-11-22 03:08:29 +0100912 {"shiftwidth", 0, 1, f_shiftwidth},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100913#ifdef FEAT_SIGNS
914 {"sign_define", 1, 2, f_sign_define},
915 {"sign_getdefined", 0, 1, f_sign_getdefined},
916 {"sign_getplaced", 0, 2, f_sign_getplaced},
Bram Moolenaar6b7b7192019-01-11 13:42:41 +0100917 {"sign_jump", 3, 3, f_sign_jump},
Bram Moolenaar162b7142018-12-21 15:17:36 +0100918 {"sign_place", 4, 5, f_sign_place},
919 {"sign_undefine", 0, 1, f_sign_undefine},
920 {"sign_unplace", 1, 2, f_sign_unplace},
921#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200922 {"simplify", 1, 1, f_simplify},
923#ifdef FEAT_FLOAT
924 {"sin", 1, 1, f_sin},
925 {"sinh", 1, 1, f_sinh},
926#endif
927 {"sort", 1, 3, f_sort},
928 {"soundfold", 1, 1, f_soundfold},
929 {"spellbadword", 0, 1, f_spellbadword},
930 {"spellsuggest", 1, 3, f_spellsuggest},
931 {"split", 1, 3, f_split},
932#ifdef FEAT_FLOAT
933 {"sqrt", 1, 1, f_sqrt},
934 {"str2float", 1, 1, f_str2float},
935#endif
Bram Moolenaar9d401282019-04-06 13:18:12 +0200936 {"str2list", 1, 2, f_str2list},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200937 {"str2nr", 1, 2, f_str2nr},
938 {"strcharpart", 2, 3, f_strcharpart},
939 {"strchars", 1, 2, f_strchars},
940 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
941#ifdef HAVE_STRFTIME
942 {"strftime", 1, 2, f_strftime},
943#endif
944 {"strgetchar", 2, 2, f_strgetchar},
945 {"stridx", 2, 3, f_stridx},
946 {"string", 1, 1, f_string},
947 {"strlen", 1, 1, f_strlen},
948 {"strpart", 2, 3, f_strpart},
949 {"strridx", 2, 3, f_strridx},
950 {"strtrans", 1, 1, f_strtrans},
951 {"strwidth", 1, 1, f_strwidth},
952 {"submatch", 1, 2, f_submatch},
953 {"substitute", 4, 4, f_substitute},
Bram Moolenaar00f123a2018-08-21 20:28:54 +0200954 {"swapinfo", 1, 1, f_swapinfo},
Bram Moolenaar110bd602018-09-16 18:46:59 +0200955 {"swapname", 1, 1, f_swapname},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200956 {"synID", 3, 3, f_synID},
957 {"synIDattr", 2, 3, f_synIDattr},
958 {"synIDtrans", 1, 1, f_synIDtrans},
959 {"synconcealed", 2, 2, f_synconcealed},
960 {"synstack", 2, 2, f_synstack},
961 {"system", 1, 2, f_system},
962 {"systemlist", 1, 2, f_systemlist},
963 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
964 {"tabpagenr", 0, 1, f_tabpagenr},
965 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
966 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaarc6aafba2017-03-21 17:09:10 +0100967 {"taglist", 1, 2, f_taglist},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200968#ifdef FEAT_FLOAT
969 {"tan", 1, 1, f_tan},
970 {"tanh", 1, 1, f_tanh},
971#endif
972 {"tempname", 0, 0, f_tempname},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200973#ifdef FEAT_TERMINAL
Bram Moolenaard96ff162018-02-18 22:13:29 +0100974 {"term_dumpdiff", 2, 3, f_term_dumpdiff},
975 {"term_dumpload", 1, 2, f_term_dumpload},
Bram Moolenaarcafafb32018-02-22 21:07:09 +0100976 {"term_dumpwrite", 2, 3, f_term_dumpwrite},
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200977 {"term_getaltscreen", 1, 1, f_term_getaltscreen},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200978# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
979 {"term_getansicolors", 1, 1, f_term_getansicolors},
980# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200981 {"term_getattr", 2, 2, f_term_getattr},
Bram Moolenaar97870002017-07-30 18:28:38 +0200982 {"term_getcursor", 1, 1, f_term_getcursor},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200983 {"term_getjob", 1, 1, f_term_getjob},
Bram Moolenaar45356542017-08-06 17:53:31 +0200984 {"term_getline", 2, 2, f_term_getline},
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200985 {"term_getscrolled", 1, 1, f_term_getscrolled},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200986 {"term_getsize", 1, 1, f_term_getsize},
Bram Moolenaarb000e322017-07-30 19:38:21 +0200987 {"term_getstatus", 1, 1, f_term_getstatus},
988 {"term_gettitle", 1, 1, f_term_gettitle},
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200989 {"term_gettty", 1, 2, f_term_gettty},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200990 {"term_list", 0, 0, f_term_list},
Bram Moolenaar45356542017-08-06 17:53:31 +0200991 {"term_scrape", 2, 2, f_term_scrape},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200992 {"term_sendkeys", 2, 2, f_term_sendkeys},
Bram Moolenaarf59c6e82018-04-10 15:59:11 +0200993# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
994 {"term_setansicolors", 2, 2, f_term_setansicolors},
995# endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996 {"term_setkill", 2, 2, f_term_setkill},
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100997 {"term_setrestore", 2, 2, f_term_setrestore},
Bram Moolenaara42d3632018-04-14 17:05:38 +0200998 {"term_setsize", 3, 3, f_term_setsize},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200999 {"term_start", 1, 2, f_term_start},
Bram Moolenaarf3402b12017-08-06 19:07:08 +02001000 {"term_wait", 1, 2, f_term_wait},
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001001#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001002 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
1003 {"test_autochdir", 0, 0, f_test_autochdir},
Bram Moolenaar5e80de32017-09-03 15:48:12 +02001004 {"test_feedinput", 1, 1, f_test_feedinput},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001005 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
Bram Moolenaareda65222019-05-16 20:29:44 +02001006 {"test_getvalue", 1, 1, f_test_getvalue},
Bram Moolenaare0c31f62017-03-01 15:07:05 +01001007 {"test_ignore_error", 1, 1, f_test_ignore_error},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001008 {"test_null_blob", 0, 0, f_test_null_blob},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001009#ifdef FEAT_JOB_CHANNEL
1010 {"test_null_channel", 0, 0, f_test_null_channel},
1011#endif
1012 {"test_null_dict", 0, 0, f_test_null_dict},
1013#ifdef FEAT_JOB_CHANNEL
1014 {"test_null_job", 0, 0, f_test_null_job},
1015#endif
1016 {"test_null_list", 0, 0, f_test_null_list},
1017 {"test_null_partial", 0, 0, f_test_null_partial},
1018 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaarfe8ef982018-09-13 20:31:54 +02001019 {"test_option_not_set", 1, 1, f_test_option_not_set},
Bram Moolenaarc3e92c12019-03-23 14:23:07 +01001020 {"test_override", 2, 2, f_test_override},
1021 {"test_refcount", 1, 1, f_test_refcount},
Bram Moolenaarab186732018-09-14 21:27:06 +02001022#ifdef FEAT_GUI
1023 {"test_scrollbar", 3, 3, f_test_scrollbar},
1024#endif
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001025#ifdef FEAT_MOUSE
Bram Moolenaarbb8476b2019-05-04 15:47:48 +02001026 {"test_setmouse", 2, 2, f_test_setmouse},
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +02001027#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001028 {"test_settime", 1, 1, f_test_settime},
1029#ifdef FEAT_TIMERS
Bram Moolenaar8e97bd72016-08-06 22:05:07 +02001030 {"timer_info", 0, 1, f_timer_info},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001031 {"timer_pause", 2, 2, f_timer_pause},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001032 {"timer_start", 2, 3, f_timer_start},
1033 {"timer_stop", 1, 1, f_timer_stop},
Bram Moolenaarb73598e2016-08-07 18:22:53 +02001034 {"timer_stopall", 0, 0, f_timer_stopall},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001035#endif
1036 {"tolower", 1, 1, f_tolower},
1037 {"toupper", 1, 1, f_toupper},
1038 {"tr", 3, 3, f_tr},
Bram Moolenaar295ac5a2018-03-22 23:04:02 +01001039 {"trim", 1, 2, f_trim},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001040#ifdef FEAT_FLOAT
1041 {"trunc", 1, 1, f_trunc},
1042#endif
1043 {"type", 1, 1, f_type},
1044 {"undofile", 1, 1, f_undofile},
1045 {"undotree", 0, 0, f_undotree},
1046 {"uniq", 1, 3, f_uniq},
1047 {"values", 1, 1, f_values},
1048 {"virtcol", 1, 1, f_virtcol},
1049 {"visualmode", 0, 1, f_visualmode},
1050 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar868b7b62019-05-29 21:44:40 +02001051 {"win_execute", 2, 3, f_win_execute},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001052 {"win_findbuf", 1, 1, f_win_findbuf},
1053 {"win_getid", 0, 2, f_win_getid},
1054 {"win_gotoid", 1, 1, f_win_gotoid},
1055 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
1056 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar22044dc2017-12-02 15:43:37 +01001057 {"win_screenpos", 1, 1, f_win_screenpos},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001058 {"winbufnr", 1, 1, f_winbufnr},
1059 {"wincol", 0, 0, f_wincol},
1060 {"winheight", 1, 1, f_winheight},
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02001061 {"winlayout", 0, 1, f_winlayout},
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001062 {"winline", 0, 0, f_winline},
1063 {"winnr", 0, 1, f_winnr},
1064 {"winrestcmd", 0, 0, f_winrestcmd},
1065 {"winrestview", 1, 1, f_winrestview},
1066 {"winsaveview", 0, 0, f_winsaveview},
1067 {"winwidth", 1, 1, f_winwidth},
1068 {"wordcount", 0, 0, f_wordcount},
1069 {"writefile", 2, 3, f_writefile},
1070 {"xor", 2, 2, f_xor},
1071};
1072
1073#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1074
1075/*
1076 * Function given to ExpandGeneric() to obtain the list of internal
1077 * or user defined function names.
1078 */
1079 char_u *
1080get_function_name(expand_T *xp, int idx)
1081{
1082 static int intidx = -1;
1083 char_u *name;
1084
1085 if (idx == 0)
1086 intidx = -1;
1087 if (intidx < 0)
1088 {
1089 name = get_user_func_name(xp, idx);
1090 if (name != NULL)
1091 return name;
1092 }
1093 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
1094 {
1095 STRCPY(IObuff, functions[intidx].f_name);
1096 STRCAT(IObuff, "(");
1097 if (functions[intidx].f_max_argc == 0)
1098 STRCAT(IObuff, ")");
1099 return IObuff;
1100 }
1101
1102 return NULL;
1103}
1104
1105/*
1106 * Function given to ExpandGeneric() to obtain the list of internal or
1107 * user defined variable or function names.
1108 */
1109 char_u *
1110get_expr_name(expand_T *xp, int idx)
1111{
1112 static int intidx = -1;
1113 char_u *name;
1114
1115 if (idx == 0)
1116 intidx = -1;
1117 if (intidx < 0)
1118 {
1119 name = get_function_name(xp, idx);
1120 if (name != NULL)
1121 return name;
1122 }
1123 return get_user_var_name(xp, ++intidx);
1124}
1125
1126#endif /* FEAT_CMDL_COMPL */
1127
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001128/*
1129 * Find internal function in table above.
1130 * Return index, or -1 if not found
1131 */
1132 int
1133find_internal_func(
1134 char_u *name) /* name of the function */
1135{
1136 int first = 0;
1137 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
1138 int cmp;
1139 int x;
1140
1141 /*
1142 * Find the function name in the table. Binary search.
1143 */
1144 while (first <= last)
1145 {
1146 x = first + ((unsigned)(last - first) >> 1);
1147 cmp = STRCMP(name, functions[x].f_name);
1148 if (cmp < 0)
1149 last = x - 1;
1150 else if (cmp > 0)
1151 first = x + 1;
1152 else
1153 return x;
1154 }
1155 return -1;
1156}
1157
1158 int
1159call_internal_func(
1160 char_u *name,
1161 int argcount,
1162 typval_T *argvars,
1163 typval_T *rettv)
1164{
1165 int i;
1166
1167 i = find_internal_func(name);
1168 if (i < 0)
1169 return ERROR_UNKNOWN;
1170 if (argcount < functions[i].f_min_argc)
1171 return ERROR_TOOFEW;
1172 if (argcount > functions[i].f_max_argc)
1173 return ERROR_TOOMANY;
1174 argvars[argcount].v_type = VAR_UNKNOWN;
1175 functions[i].f_func(argvars, rettv);
1176 return ERROR_NONE;
1177}
1178
1179/*
1180 * Return TRUE for a non-zero Number and a non-empty String.
1181 */
1182 static int
1183non_zero_arg(typval_T *argvars)
1184{
1185 return ((argvars[0].v_type == VAR_NUMBER
1186 && argvars[0].vval.v_number != 0)
1187 || (argvars[0].v_type == VAR_SPECIAL
1188 && argvars[0].vval.v_number == VVAL_TRUE)
1189 || (argvars[0].v_type == VAR_STRING
1190 && argvars[0].vval.v_string != NULL
1191 && *argvars[0].vval.v_string != NUL));
1192}
1193
1194/*
1195 * Get the lnum from the first argument.
1196 * Also accepts ".", "$", etc., but that only works for the current buffer.
1197 * Returns -1 on error.
1198 */
1199 static linenr_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001200tv_get_lnum(typval_T *argvars)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001201{
1202 typval_T rettv;
1203 linenr_T lnum;
1204
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001205 lnum = (linenr_T)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001206 if (lnum == 0) /* no valid number, try using line() */
1207 {
1208 rettv.v_type = VAR_NUMBER;
1209 f_line(argvars, &rettv);
1210 lnum = (linenr_T)rettv.vval.v_number;
1211 clear_tv(&rettv);
1212 }
1213 return lnum;
1214}
1215
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001216/*
1217 * Get the lnum from the first argument.
1218 * Also accepts "$", then "buf" is used.
1219 * Returns 0 on error.
1220 */
1221 static linenr_T
1222tv_get_lnum_buf(typval_T *argvars, buf_T *buf)
1223{
1224 if (argvars[0].v_type == VAR_STRING
1225 && argvars[0].vval.v_string != NULL
1226 && argvars[0].vval.v_string[0] == '$'
1227 && buf != NULL)
1228 return buf->b_ml.ml_line_count;
1229 return (linenr_T)tv_get_number_chk(&argvars[0], NULL);
1230}
1231
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001232#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001233/*
1234 * Get the float value of "argvars[0]" into "f".
1235 * Returns FAIL when the argument is not a Number or Float.
1236 */
1237 static int
1238get_float_arg(typval_T *argvars, float_T *f)
1239{
1240 if (argvars[0].v_type == VAR_FLOAT)
1241 {
1242 *f = argvars[0].vval.v_float;
1243 return OK;
1244 }
1245 if (argvars[0].v_type == VAR_NUMBER)
1246 {
1247 *f = (float_T)argvars[0].vval.v_number;
1248 return OK;
1249 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001250 emsg(_("E808: Number or Float required"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001251 return FAIL;
1252}
1253
1254/*
1255 * "abs(expr)" function
1256 */
1257 static void
1258f_abs(typval_T *argvars, typval_T *rettv)
1259{
1260 if (argvars[0].v_type == VAR_FLOAT)
1261 {
1262 rettv->v_type = VAR_FLOAT;
1263 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
1264 }
1265 else
1266 {
1267 varnumber_T n;
1268 int error = FALSE;
1269
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001270 n = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001271 if (error)
1272 rettv->vval.v_number = -1;
1273 else if (n > 0)
1274 rettv->vval.v_number = n;
1275 else
1276 rettv->vval.v_number = -n;
1277 }
1278}
1279
1280/*
1281 * "acos()" function
1282 */
1283 static void
1284f_acos(typval_T *argvars, typval_T *rettv)
1285{
1286 float_T f = 0.0;
1287
1288 rettv->v_type = VAR_FLOAT;
1289 if (get_float_arg(argvars, &f) == OK)
1290 rettv->vval.v_float = acos(f);
1291 else
1292 rettv->vval.v_float = 0.0;
1293}
1294#endif
1295
1296/*
1297 * "add(list, item)" function
1298 */
1299 static void
1300f_add(typval_T *argvars, typval_T *rettv)
1301{
1302 list_T *l;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001303 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001304
1305 rettv->vval.v_number = 1; /* Default: Failed */
1306 if (argvars[0].v_type == VAR_LIST)
1307 {
1308 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001309 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001310 (char_u *)N_("add() argument"), TRUE)
1311 && list_append_tv(l, &argvars[1]) == OK)
1312 copy_tv(&argvars[0], rettv);
1313 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001314 else if (argvars[0].v_type == VAR_BLOB)
1315 {
1316 if ((b = argvars[0].vval.v_blob) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001317 && !var_check_lock(b->bv_lock,
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001318 (char_u *)N_("add() argument"), TRUE))
1319 {
Bram Moolenaar05500ec2019-01-13 19:10:33 +01001320 int error = FALSE;
1321 varnumber_T n = tv_get_number_chk(&argvars[1], &error);
1322
1323 if (!error)
1324 {
1325 ga_append(&b->bv_ga, (int)n);
1326 copy_tv(&argvars[0], rettv);
1327 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001328 }
1329 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001330 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001331 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001332}
1333
1334/*
1335 * "and(expr, expr)" function
1336 */
1337 static void
1338f_and(typval_T *argvars, typval_T *rettv)
1339{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001340 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
1341 & tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaarca851592018-06-06 21:04:07 +02001342}
1343
1344/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02001345 * If there is a window for "curbuf", make it the current window.
1346 */
1347 static void
1348find_win_for_curbuf(void)
1349{
1350 wininfo_T *wip;
1351
1352 for (wip = curbuf->b_wininfo; wip != NULL; wip = wip->wi_next)
1353 {
1354 if (wip->wi_win != NULL)
1355 {
1356 curwin = wip->wi_win;
1357 break;
1358 }
1359 }
1360}
1361
1362/*
Bram Moolenaarca851592018-06-06 21:04:07 +02001363 * Set line or list of lines in buffer "buf".
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001364 */
1365 static void
Bram Moolenaarca851592018-06-06 21:04:07 +02001366set_buffer_lines(
1367 buf_T *buf,
1368 linenr_T lnum_arg,
1369 int append,
1370 typval_T *lines,
1371 typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001372{
Bram Moolenaarca851592018-06-06 21:04:07 +02001373 linenr_T lnum = lnum_arg + (append ? 1 : 0);
1374 char_u *line = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001375 list_T *l = NULL;
1376 listitem_T *li = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001377 long added = 0;
Bram Moolenaarca851592018-06-06 21:04:07 +02001378 linenr_T append_lnum;
1379 buf_T *curbuf_save = NULL;
1380 win_T *curwin_save = NULL;
1381 int is_curbuf = buf == curbuf;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001382
Bram Moolenaarca851592018-06-06 21:04:07 +02001383 /* When using the current buffer ml_mfp will be set if needed. Useful when
1384 * setline() is used on startup. For other buffers the buffer must be
1385 * loaded. */
1386 if (buf == NULL || (!is_curbuf && buf->b_ml.ml_mfp == NULL) || lnum < 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001387 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001388 rettv->vval.v_number = 1; /* FAIL */
1389 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001390 }
1391
Bram Moolenaarca851592018-06-06 21:04:07 +02001392 if (!is_curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001393 {
Bram Moolenaarca851592018-06-06 21:04:07 +02001394 curbuf_save = curbuf;
1395 curwin_save = curwin;
1396 curbuf = buf;
Bram Moolenaard79a2622018-06-07 18:17:46 +02001397 find_win_for_curbuf();
Bram Moolenaarca851592018-06-06 21:04:07 +02001398 }
1399
1400 if (append)
1401 // appendbufline() uses the line number below which we insert
1402 append_lnum = lnum - 1;
1403 else
1404 // setbufline() uses the line number above which we insert, we only
1405 // append if it's below the last line
1406 append_lnum = curbuf->b_ml.ml_line_count;
1407
1408 if (lines->v_type == VAR_LIST)
1409 {
1410 l = lines->vval.v_list;
1411 li = l->lv_first;
1412 }
1413 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001414 line = tv_get_string_chk(lines);
Bram Moolenaarca851592018-06-06 21:04:07 +02001415
1416 /* default result is zero == OK */
1417 for (;;)
1418 {
1419 if (l != NULL)
1420 {
1421 /* list argument, get next string */
1422 if (li == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001423 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001424 line = tv_get_string_chk(&li->li_tv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001425 li = li->li_next;
1426 }
1427
Bram Moolenaarca851592018-06-06 21:04:07 +02001428 rettv->vval.v_number = 1; /* FAIL */
1429 if (line == NULL || lnum > curbuf->b_ml.ml_line_count + 1)
1430 break;
1431
1432 /* When coming here from Insert mode, sync undo, so that this can be
1433 * undone separately from what was previously inserted. */
1434 if (u_sync_once == 2)
1435 {
1436 u_sync_once = 1; /* notify that u_sync() was called */
1437 u_sync(TRUE);
1438 }
1439
1440 if (!append && lnum <= curbuf->b_ml.ml_line_count)
1441 {
Bram Moolenaar21b50382019-01-04 18:07:24 +01001442 // Existing line, replace it.
1443 // Removes any existing text properties.
1444 if (u_savesub(lnum) == OK && ml_replace_len(
1445 lnum, line, (colnr_T)STRLEN(line) + 1, TRUE, TRUE) == OK)
Bram Moolenaarca851592018-06-06 21:04:07 +02001446 {
1447 changed_bytes(lnum, 0);
1448 if (is_curbuf && lnum == curwin->w_cursor.lnum)
1449 check_cursor_col();
1450 rettv->vval.v_number = 0; /* OK */
1451 }
1452 }
1453 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
1454 {
1455 /* append the line */
1456 ++added;
1457 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
1458 rettv->vval.v_number = 0; /* OK */
1459 }
1460
1461 if (l == NULL) /* only one string argument */
1462 break;
1463 ++lnum;
1464 }
1465
1466 if (added > 0)
1467 {
1468 win_T *wp;
1469 tabpage_T *tp;
1470
1471 appended_lines_mark(append_lnum, added);
1472 FOR_ALL_TAB_WINDOWS(tp, wp)
1473 if (wp->w_buffer == buf && wp->w_cursor.lnum > append_lnum)
1474 wp->w_cursor.lnum += added;
1475 check_cursor_col();
1476
Bram Moolenaarf2732452018-06-03 14:47:35 +02001477#ifdef FEAT_JOB_CHANNEL
1478 if (bt_prompt(curbuf) && (State & INSERT))
1479 // show the line with the prompt
1480 update_topline();
1481#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001482 }
Bram Moolenaarca851592018-06-06 21:04:07 +02001483
1484 if (!is_curbuf)
1485 {
1486 curbuf = curbuf_save;
1487 curwin = curwin_save;
1488 }
1489}
1490
1491/*
1492 * "append(lnum, string/list)" function
1493 */
1494 static void
1495f_append(typval_T *argvars, typval_T *rettv)
1496{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001497 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaarca851592018-06-06 21:04:07 +02001498
1499 set_buffer_lines(curbuf, lnum, TRUE, &argvars[1], rettv);
1500}
1501
1502/*
1503 * "appendbufline(buf, lnum, string/list)" function
1504 */
1505 static void
1506f_appendbufline(typval_T *argvars, typval_T *rettv)
1507{
1508 linenr_T lnum;
1509 buf_T *buf;
1510
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01001511 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarca851592018-06-06 21:04:07 +02001512 if (buf == NULL)
1513 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001514 else
Bram Moolenaarca851592018-06-06 21:04:07 +02001515 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001516 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +02001517 set_buffer_lines(buf, lnum, TRUE, &argvars[2], rettv);
1518 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001519}
1520
1521/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001522 * "argc([window id])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001523 */
1524 static void
Bram Moolenaare6e39892018-10-25 12:32:11 +02001525f_argc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001526{
Bram Moolenaare6e39892018-10-25 12:32:11 +02001527 win_T *wp;
1528
1529 if (argvars[0].v_type == VAR_UNKNOWN)
1530 // use the current window
1531 rettv->vval.v_number = ARGCOUNT;
1532 else if (argvars[0].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001533 && tv_get_number(&argvars[0]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001534 // use the global argument list
1535 rettv->vval.v_number = GARGCOUNT;
1536 else
1537 {
1538 // use the argument list of the specified window
1539 wp = find_win_by_nr_or_id(&argvars[0]);
1540 if (wp != NULL)
1541 rettv->vval.v_number = WARGCOUNT(wp);
1542 else
1543 rettv->vval.v_number = -1;
1544 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001545}
1546
1547/*
1548 * "argidx()" function
1549 */
1550 static void
1551f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
1552{
1553 rettv->vval.v_number = curwin->w_arg_idx;
1554}
1555
1556/*
1557 * "arglistid()" function
1558 */
1559 static void
1560f_arglistid(typval_T *argvars, typval_T *rettv)
1561{
1562 win_T *wp;
1563
1564 rettv->vval.v_number = -1;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02001565 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001566 if (wp != NULL)
1567 rettv->vval.v_number = wp->w_alist->id;
1568}
1569
1570/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02001571 * Get the argument list for a given window
1572 */
1573 static void
1574get_arglist_as_rettv(aentry_T *arglist, int argcount, typval_T *rettv)
1575{
1576 int idx;
1577
1578 if (rettv_list_alloc(rettv) == OK && arglist != NULL)
1579 for (idx = 0; idx < argcount; ++idx)
1580 list_append_string(rettv->vval.v_list,
1581 alist_name(&arglist[idx]), -1);
1582}
1583
1584/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001585 * "argv(nr)" function
1586 */
1587 static void
1588f_argv(typval_T *argvars, typval_T *rettv)
1589{
1590 int idx;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001591 aentry_T *arglist = NULL;
1592 int argcount = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001593
1594 if (argvars[0].v_type != VAR_UNKNOWN)
1595 {
Bram Moolenaare6e39892018-10-25 12:32:11 +02001596 if (argvars[1].v_type == VAR_UNKNOWN)
1597 {
1598 arglist = ARGLIST;
1599 argcount = ARGCOUNT;
1600 }
1601 else if (argvars[1].v_type == VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001602 && tv_get_number(&argvars[1]) == -1)
Bram Moolenaare6e39892018-10-25 12:32:11 +02001603 {
1604 arglist = GARGLIST;
1605 argcount = GARGCOUNT;
1606 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001607 else
Bram Moolenaare6e39892018-10-25 12:32:11 +02001608 {
1609 win_T *wp = find_win_by_nr_or_id(&argvars[1]);
1610
1611 if (wp != NULL)
1612 {
1613 /* Use the argument list of the specified window */
1614 arglist = WARGLIST(wp);
1615 argcount = WARGCOUNT(wp);
1616 }
1617 }
1618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001619 rettv->v_type = VAR_STRING;
Bram Moolenaare6e39892018-10-25 12:32:11 +02001620 rettv->vval.v_string = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001621 idx = tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02001622 if (arglist != NULL && idx >= 0 && idx < argcount)
1623 rettv->vval.v_string = vim_strsave(alist_name(&arglist[idx]));
1624 else if (idx == -1)
1625 get_arglist_as_rettv(arglist, argcount, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001626 }
Bram Moolenaare6e39892018-10-25 12:32:11 +02001627 else
1628 get_arglist_as_rettv(ARGLIST, ARGCOUNT, rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001629}
1630
1631/*
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001632 * "assert_beeps(cmd [, error])" function
1633 */
1634 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001635f_assert_beeps(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001636{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001637 rettv->vval.v_number = assert_beeps(argvars);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01001638}
1639
1640/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001641 * "assert_equal(expected, actual[, msg])" function
1642 */
1643 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001644f_assert_equal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001645{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001646 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_EQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001647}
1648
1649/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01001650 * "assert_equalfile(fname-one, fname-two)" function
1651 */
1652 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001653f_assert_equalfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaard96ff162018-02-18 22:13:29 +01001654{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001655 rettv->vval.v_number = assert_equalfile(argvars);
Bram Moolenaard96ff162018-02-18 22:13:29 +01001656}
1657
1658/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001659 * "assert_notequal(expected, actual[, msg])" function
1660 */
1661 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001662f_assert_notequal(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001663{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001664 rettv->vval.v_number = assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001665}
1666
1667/*
1668 * "assert_exception(string[, msg])" function
1669 */
1670 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001671f_assert_exception(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001672{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001673 rettv->vval.v_number = assert_exception(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001674}
1675
1676/*
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02001677 * "assert_fails(cmd [, error[, msg]])" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001678 */
1679 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001680f_assert_fails(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001681{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001682 rettv->vval.v_number = assert_fails(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001683}
1684
1685/*
1686 * "assert_false(actual[, msg])" function
1687 */
1688 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001689f_assert_false(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001690{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001691 rettv->vval.v_number = assert_bool(argvars, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001692}
1693
1694/*
Bram Moolenaar61c04492016-07-23 15:35:35 +02001695 * "assert_inrange(lower, upper[, msg])" function
1696 */
1697 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001698f_assert_inrange(typval_T *argvars, typval_T *rettv)
Bram Moolenaar61c04492016-07-23 15:35:35 +02001699{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001700 rettv->vval.v_number = assert_inrange(argvars);
Bram Moolenaar61c04492016-07-23 15:35:35 +02001701}
1702
1703/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001704 * "assert_match(pattern, actual[, msg])" function
1705 */
1706 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001707f_assert_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001708{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001709 rettv->vval.v_number = assert_match_common(argvars, ASSERT_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001710}
1711
1712/*
1713 * "assert_notmatch(pattern, actual[, msg])" function
1714 */
1715 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001716f_assert_notmatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001717{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001718 rettv->vval.v_number = assert_match_common(argvars, ASSERT_NOTMATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001719}
1720
1721/*
Bram Moolenaar42205552017-03-18 19:42:22 +01001722 * "assert_report(msg)" function
1723 */
1724 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001725f_assert_report(typval_T *argvars, typval_T *rettv)
Bram Moolenaar42205552017-03-18 19:42:22 +01001726{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001727 rettv->vval.v_number = assert_report(argvars);
Bram Moolenaar42205552017-03-18 19:42:22 +01001728}
1729
1730/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001731 * "assert_true(actual[, msg])" function
1732 */
1733 static void
Bram Moolenaar65a54642018-04-28 16:56:53 +02001734f_assert_true(typval_T *argvars, typval_T *rettv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001735{
Bram Moolenaar65a54642018-04-28 16:56:53 +02001736 rettv->vval.v_number = assert_bool(argvars, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001737}
1738
1739#ifdef FEAT_FLOAT
1740/*
1741 * "asin()" function
1742 */
1743 static void
1744f_asin(typval_T *argvars, typval_T *rettv)
1745{
1746 float_T f = 0.0;
1747
1748 rettv->v_type = VAR_FLOAT;
1749 if (get_float_arg(argvars, &f) == OK)
1750 rettv->vval.v_float = asin(f);
1751 else
1752 rettv->vval.v_float = 0.0;
1753}
1754
1755/*
1756 * "atan()" function
1757 */
1758 static void
1759f_atan(typval_T *argvars, typval_T *rettv)
1760{
1761 float_T f = 0.0;
1762
1763 rettv->v_type = VAR_FLOAT;
1764 if (get_float_arg(argvars, &f) == OK)
1765 rettv->vval.v_float = atan(f);
1766 else
1767 rettv->vval.v_float = 0.0;
1768}
1769
1770/*
1771 * "atan2()" function
1772 */
1773 static void
1774f_atan2(typval_T *argvars, typval_T *rettv)
1775{
1776 float_T fx = 0.0, fy = 0.0;
1777
1778 rettv->v_type = VAR_FLOAT;
1779 if (get_float_arg(argvars, &fx) == OK
1780 && get_float_arg(&argvars[1], &fy) == OK)
1781 rettv->vval.v_float = atan2(fx, fy);
1782 else
1783 rettv->vval.v_float = 0.0;
1784}
1785#endif
1786
1787/*
Bram Moolenaar59716a22017-03-01 20:32:44 +01001788 * "balloon_show()" function
1789 */
1790#ifdef FEAT_BEVAL
1791 static void
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001792f_balloon_gettext(typval_T *argvars UNUSED, typval_T *rettv)
1793{
1794 rettv->v_type = VAR_STRING;
1795 if (balloonEval != NULL)
1796 {
1797 if (balloonEval->msg == NULL)
1798 rettv->vval.v_string = NULL;
1799 else
1800 rettv->vval.v_string = vim_strsave(balloonEval->msg);
1801 }
1802}
1803
1804 static void
Bram Moolenaar59716a22017-03-01 20:32:44 +01001805f_balloon_show(typval_T *argvars, typval_T *rettv UNUSED)
1806{
Bram Moolenaarcaf64342017-03-02 22:11:33 +01001807 if (balloonEval != NULL)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001808 {
1809 if (argvars[0].v_type == VAR_LIST
1810# ifdef FEAT_GUI
1811 && !gui.in_use
1812# endif
1813 )
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001814 {
1815 list_T *l = argvars[0].vval.v_list;
1816
1817 // empty list removes the balloon
1818 post_balloon(balloonEval, NULL,
1819 l == NULL || l->lv_len == 0 ? NULL : l);
1820 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001821 else
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02001822 {
1823 char_u *mesg = tv_get_string_chk(&argvars[0]);
1824
1825 if (mesg != NULL)
1826 // empty string removes the balloon
1827 post_balloon(balloonEval, *mesg == NUL ? NULL : mesg, NULL);
1828 }
Bram Moolenaar246fe032017-11-19 19:56:27 +01001829 }
1830}
1831
Bram Moolenaar669a8282017-11-19 20:13:05 +01001832# if defined(FEAT_BEVAL_TERM)
Bram Moolenaar246fe032017-11-19 19:56:27 +01001833 static void
1834f_balloon_split(typval_T *argvars, typval_T *rettv UNUSED)
1835{
1836 if (rettv_list_alloc(rettv) == OK)
1837 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001838 char_u *msg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001839
1840 if (msg != NULL)
1841 {
1842 pumitem_T *array;
1843 int size = split_message(msg, &array);
1844 int i;
1845
1846 /* Skip the first and last item, they are always empty. */
1847 for (i = 1; i < size - 1; ++i)
1848 list_append_string(rettv->vval.v_list, array[i].pum_text, -1);
Bram Moolenaarb301f6b2018-02-10 15:38:35 +01001849 while (size > 0)
1850 vim_free(array[--size].pum_text);
Bram Moolenaar246fe032017-11-19 19:56:27 +01001851 vim_free(array);
1852 }
1853 }
Bram Moolenaar59716a22017-03-01 20:32:44 +01001854}
Bram Moolenaar669a8282017-11-19 20:13:05 +01001855# endif
Bram Moolenaar59716a22017-03-01 20:32:44 +01001856#endif
1857
1858/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001859 * "browse(save, title, initdir, default)" function
1860 */
1861 static void
1862f_browse(typval_T *argvars UNUSED, typval_T *rettv)
1863{
1864#ifdef FEAT_BROWSE
1865 int save;
1866 char_u *title;
1867 char_u *initdir;
1868 char_u *defname;
1869 char_u buf[NUMBUFLEN];
1870 char_u buf2[NUMBUFLEN];
1871 int error = FALSE;
1872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001873 save = (int)tv_get_number_chk(&argvars[0], &error);
1874 title = tv_get_string_chk(&argvars[1]);
1875 initdir = tv_get_string_buf_chk(&argvars[2], buf);
1876 defname = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001877
1878 if (error || title == NULL || initdir == NULL || defname == NULL)
1879 rettv->vval.v_string = NULL;
1880 else
1881 rettv->vval.v_string =
1882 do_browse(save ? BROWSE_SAVE : 0,
1883 title, defname, NULL, initdir, NULL, curbuf);
1884#else
1885 rettv->vval.v_string = NULL;
1886#endif
1887 rettv->v_type = VAR_STRING;
1888}
1889
1890/*
1891 * "browsedir(title, initdir)" function
1892 */
1893 static void
1894f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
1895{
1896#ifdef FEAT_BROWSE
1897 char_u *title;
1898 char_u *initdir;
1899 char_u buf[NUMBUFLEN];
1900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001901 title = tv_get_string_chk(&argvars[0]);
1902 initdir = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001903
1904 if (title == NULL || initdir == NULL)
1905 rettv->vval.v_string = NULL;
1906 else
1907 rettv->vval.v_string = do_browse(BROWSE_DIR,
1908 title, NULL, NULL, initdir, NULL, curbuf);
1909#else
1910 rettv->vval.v_string = NULL;
1911#endif
1912 rettv->v_type = VAR_STRING;
1913}
1914
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001915/*
1916 * Find a buffer by number or exact name.
1917 */
1918 static buf_T *
1919find_buffer(typval_T *avar)
1920{
1921 buf_T *buf = NULL;
1922
1923 if (avar->v_type == VAR_NUMBER)
1924 buf = buflist_findnr((int)avar->vval.v_number);
1925 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
1926 {
1927 buf = buflist_findname_exp(avar->vval.v_string);
1928 if (buf == NULL)
1929 {
1930 /* No full path name match, try a match with a URL or a "nofile"
1931 * buffer, these don't use the full path. */
Bram Moolenaar29323592016-07-24 22:04:11 +02001932 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02001933 if (buf->b_fname != NULL
1934 && (path_with_url(buf->b_fname)
1935#ifdef FEAT_QUICKFIX
1936 || bt_nofile(buf)
1937#endif
1938 )
1939 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
1940 break;
1941 }
1942 }
1943 return buf;
1944}
1945
1946/*
1947 * "bufexists(expr)" function
1948 */
1949 static void
1950f_bufexists(typval_T *argvars, typval_T *rettv)
1951{
1952 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
1953}
1954
1955/*
1956 * "buflisted(expr)" function
1957 */
1958 static void
1959f_buflisted(typval_T *argvars, typval_T *rettv)
1960{
1961 buf_T *buf;
1962
1963 buf = find_buffer(&argvars[0]);
1964 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
1965}
1966
1967/*
1968 * "bufloaded(expr)" function
1969 */
1970 static void
1971f_bufloaded(typval_T *argvars, typval_T *rettv)
1972{
1973 buf_T *buf;
1974
1975 buf = find_buffer(&argvars[0]);
1976 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
1977}
1978
1979 buf_T *
1980buflist_find_by_name(char_u *name, int curtab_only)
1981{
1982 int save_magic;
1983 char_u *save_cpo;
1984 buf_T *buf;
1985
1986 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
1987 save_magic = p_magic;
1988 p_magic = TRUE;
1989 save_cpo = p_cpo;
1990 p_cpo = (char_u *)"";
1991
1992 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
1993 TRUE, FALSE, curtab_only));
1994
1995 p_magic = save_magic;
1996 p_cpo = save_cpo;
1997 return buf;
1998}
1999
2000/*
2001 * Get buffer by number or pattern.
2002 */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002003 buf_T *
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002004tv_get_buf(typval_T *tv, int curtab_only)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002005{
2006 char_u *name = tv->vval.v_string;
2007 buf_T *buf;
2008
2009 if (tv->v_type == VAR_NUMBER)
2010 return buflist_findnr((int)tv->vval.v_number);
2011 if (tv->v_type != VAR_STRING)
2012 return NULL;
2013 if (name == NULL || *name == NUL)
2014 return curbuf;
2015 if (name[0] == '$' && name[1] == NUL)
2016 return lastbuf;
2017
2018 buf = buflist_find_by_name(name, curtab_only);
2019
2020 /* If not found, try expanding the name, like done for bufexists(). */
2021 if (buf == NULL)
2022 buf = find_buffer(tv);
2023
2024 return buf;
2025}
2026
2027/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002028 * Get the buffer from "arg" and give an error and return NULL if it is not
2029 * valid.
2030 */
Bram Moolenaara3347722019-05-11 21:14:24 +02002031 buf_T *
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002032get_buf_arg(typval_T *arg)
2033{
2034 buf_T *buf;
2035
2036 ++emsg_off;
2037 buf = tv_get_buf(arg, FALSE);
2038 --emsg_off;
2039 if (buf == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002040 semsg(_("E158: Invalid buffer name: %s"), tv_get_string(arg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +01002041 return buf;
2042}
2043
2044/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002045 * "bufname(expr)" function
2046 */
2047 static void
2048f_bufname(typval_T *argvars, typval_T *rettv)
2049{
2050 buf_T *buf;
2051
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002052 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002053 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002054 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002055 rettv->v_type = VAR_STRING;
2056 if (buf != NULL && buf->b_fname != NULL)
2057 rettv->vval.v_string = vim_strsave(buf->b_fname);
2058 else
2059 rettv->vval.v_string = NULL;
2060 --emsg_off;
2061}
2062
2063/*
2064 * "bufnr(expr)" function
2065 */
2066 static void
2067f_bufnr(typval_T *argvars, typval_T *rettv)
2068{
2069 buf_T *buf;
2070 int error = FALSE;
2071 char_u *name;
2072
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002073 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002074 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002075 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002076 --emsg_off;
2077
2078 /* If the buffer isn't found and the second argument is not zero create a
2079 * new buffer. */
2080 if (buf == NULL
2081 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002082 && tv_get_number_chk(&argvars[1], &error) != 0
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002083 && !error
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002084 && (name = tv_get_string_chk(&argvars[0])) != NULL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002085 && !error)
2086 buf = buflist_new(name, NULL, (linenr_T)1, 0);
2087
2088 if (buf != NULL)
2089 rettv->vval.v_number = buf->b_fnum;
2090 else
2091 rettv->vval.v_number = -1;
2092}
2093
2094 static void
2095buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
2096{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002097 win_T *wp;
2098 int winnr = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002099 buf_T *buf;
2100
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002101 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002102 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01002103 buf = tv_get_buf(&argvars[0], TRUE);
Bram Moolenaar29323592016-07-24 22:04:11 +02002104 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002105 {
2106 ++winnr;
2107 if (wp->w_buffer == buf)
2108 break;
2109 }
2110 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002111 --emsg_off;
2112}
2113
2114/*
2115 * "bufwinid(nr)" function
2116 */
2117 static void
2118f_bufwinid(typval_T *argvars, typval_T *rettv)
2119{
2120 buf_win_common(argvars, rettv, FALSE);
2121}
2122
2123/*
2124 * "bufwinnr(nr)" function
2125 */
2126 static void
2127f_bufwinnr(typval_T *argvars, typval_T *rettv)
2128{
2129 buf_win_common(argvars, rettv, TRUE);
2130}
2131
2132/*
2133 * "byte2line(byte)" function
2134 */
2135 static void
2136f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
2137{
2138#ifndef FEAT_BYTEOFF
2139 rettv->vval.v_number = -1;
2140#else
2141 long boff = 0;
2142
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002143 boff = tv_get_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002144 if (boff < 0)
2145 rettv->vval.v_number = -1;
2146 else
2147 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
2148 (linenr_T)0, &boff);
2149#endif
2150}
2151
2152 static void
2153byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
2154{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002155 char_u *t;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002156 char_u *str;
2157 varnumber_T idx;
2158
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002159 str = tv_get_string_chk(&argvars[0]);
2160 idx = tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002161 rettv->vval.v_number = -1;
2162 if (str == NULL || idx < 0)
2163 return;
2164
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002165 t = str;
2166 for ( ; idx > 0; idx--)
2167 {
2168 if (*t == NUL) /* EOL reached */
2169 return;
2170 if (enc_utf8 && comp)
2171 t += utf_ptr2len(t);
2172 else
2173 t += (*mb_ptr2len)(t);
2174 }
2175 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002176}
2177
2178/*
2179 * "byteidx()" function
2180 */
2181 static void
2182f_byteidx(typval_T *argvars, typval_T *rettv)
2183{
2184 byteidx(argvars, rettv, FALSE);
2185}
2186
2187/*
2188 * "byteidxcomp()" function
2189 */
2190 static void
2191f_byteidxcomp(typval_T *argvars, typval_T *rettv)
2192{
2193 byteidx(argvars, rettv, TRUE);
2194}
2195
2196/*
2197 * "call(func, arglist [, dict])" function
2198 */
2199 static void
2200f_call(typval_T *argvars, typval_T *rettv)
2201{
2202 char_u *func;
2203 partial_T *partial = NULL;
2204 dict_T *selfdict = NULL;
2205
2206 if (argvars[1].v_type != VAR_LIST)
2207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002208 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002209 return;
2210 }
2211 if (argvars[1].vval.v_list == NULL)
2212 return;
2213
2214 if (argvars[0].v_type == VAR_FUNC)
2215 func = argvars[0].vval.v_string;
2216 else if (argvars[0].v_type == VAR_PARTIAL)
2217 {
2218 partial = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002219 func = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002220 }
2221 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002222 func = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002223 if (*func == NUL)
2224 return; /* type error or empty name */
2225
2226 if (argvars[2].v_type != VAR_UNKNOWN)
2227 {
2228 if (argvars[2].v_type != VAR_DICT)
2229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002230 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002231 return;
2232 }
2233 selfdict = argvars[2].vval.v_dict;
2234 }
2235
2236 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
2237}
2238
2239#ifdef FEAT_FLOAT
2240/*
2241 * "ceil({float})" function
2242 */
2243 static void
2244f_ceil(typval_T *argvars, typval_T *rettv)
2245{
2246 float_T f = 0.0;
2247
2248 rettv->v_type = VAR_FLOAT;
2249 if (get_float_arg(argvars, &f) == OK)
2250 rettv->vval.v_float = ceil(f);
2251 else
2252 rettv->vval.v_float = 0.0;
2253}
2254#endif
2255
2256#ifdef FEAT_JOB_CHANNEL
2257/*
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002258 * "ch_canread()" function
2259 */
2260 static void
2261f_ch_canread(typval_T *argvars, typval_T *rettv)
2262{
Bram Moolenaar958dc692016-12-01 15:34:12 +01002263 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar4b785f62016-11-29 21:54:44 +01002264
2265 rettv->vval.v_number = 0;
2266 if (channel != NULL)
2267 rettv->vval.v_number = channel_has_readahead(channel, PART_SOCK)
2268 || channel_has_readahead(channel, PART_OUT)
2269 || channel_has_readahead(channel, PART_ERR);
2270}
2271
2272/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002273 * "ch_close()" function
2274 */
2275 static void
2276f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
2277{
2278 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2279
2280 if (channel != NULL)
2281 {
2282 channel_close(channel, FALSE);
2283 channel_clear(channel);
2284 }
2285}
2286
2287/*
Bram Moolenaar0874a832016-09-01 15:11:51 +02002288 * "ch_close()" function
2289 */
2290 static void
2291f_ch_close_in(typval_T *argvars, typval_T *rettv UNUSED)
2292{
2293 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
2294
2295 if (channel != NULL)
2296 channel_close_in(channel);
2297}
2298
2299/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002300 * "ch_getbufnr()" function
2301 */
2302 static void
2303f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
2304{
2305 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2306
2307 rettv->vval.v_number = -1;
2308 if (channel != NULL)
2309 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002310 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002311 int part;
2312
2313 if (STRCMP(what, "err") == 0)
2314 part = PART_ERR;
2315 else if (STRCMP(what, "out") == 0)
2316 part = PART_OUT;
2317 else if (STRCMP(what, "in") == 0)
2318 part = PART_IN;
2319 else
2320 part = PART_SOCK;
2321 if (channel->ch_part[part].ch_bufref.br_buf != NULL)
2322 rettv->vval.v_number =
2323 channel->ch_part[part].ch_bufref.br_buf->b_fnum;
2324 }
2325}
2326
2327/*
2328 * "ch_getjob()" function
2329 */
2330 static void
2331f_ch_getjob(typval_T *argvars, typval_T *rettv)
2332{
2333 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2334
2335 if (channel != NULL)
2336 {
2337 rettv->v_type = VAR_JOB;
2338 rettv->vval.v_job = channel->ch_job;
2339 if (channel->ch_job != NULL)
2340 ++channel->ch_job->jv_refcount;
2341 }
2342}
2343
2344/*
2345 * "ch_info()" function
2346 */
2347 static void
2348f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
2349{
2350 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2351
2352 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
2353 channel_info(channel, rettv->vval.v_dict);
2354}
2355
2356/*
2357 * "ch_log()" function
2358 */
2359 static void
2360f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
2361{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002362 char_u *msg = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002363 channel_T *channel = NULL;
2364
2365 if (argvars[1].v_type != VAR_UNKNOWN)
2366 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
2367
Bram Moolenaard5359b22018-04-05 22:44:39 +02002368 ch_log(channel, "%s", msg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002369}
2370
2371/*
2372 * "ch_logfile()" function
2373 */
2374 static void
2375f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
2376{
2377 char_u *fname;
2378 char_u *opt = (char_u *)"";
2379 char_u buf[NUMBUFLEN];
2380
Bram Moolenaar6d87e9e2017-08-07 20:51:51 +02002381 /* Don't open a file in restricted mode. */
2382 if (check_restricted() || check_secure())
2383 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002384 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002385 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002386 opt = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002387 ch_logfile(fname, opt);
2388}
2389
2390/*
2391 * "ch_open()" function
2392 */
2393 static void
2394f_ch_open(typval_T *argvars, typval_T *rettv)
2395{
2396 rettv->v_type = VAR_CHANNEL;
2397 if (check_restricted() || check_secure())
2398 return;
2399 rettv->vval.v_channel = channel_open_func(argvars);
2400}
2401
2402/*
2403 * "ch_read()" function
2404 */
2405 static void
2406f_ch_read(typval_T *argvars, typval_T *rettv)
2407{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002408 common_channel_read(argvars, rettv, FALSE, FALSE);
2409}
2410
2411/*
2412 * "ch_readblob()" function
2413 */
2414 static void
2415f_ch_readblob(typval_T *argvars, typval_T *rettv)
2416{
2417 common_channel_read(argvars, rettv, TRUE, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002418}
2419
2420/*
2421 * "ch_readraw()" function
2422 */
2423 static void
2424f_ch_readraw(typval_T *argvars, typval_T *rettv)
2425{
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002426 common_channel_read(argvars, rettv, TRUE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002427}
2428
2429/*
2430 * "ch_evalexpr()" function
2431 */
2432 static void
2433f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
2434{
2435 ch_expr_common(argvars, rettv, TRUE);
2436}
2437
2438/*
2439 * "ch_sendexpr()" function
2440 */
2441 static void
2442f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
2443{
2444 ch_expr_common(argvars, rettv, FALSE);
2445}
2446
2447/*
2448 * "ch_evalraw()" function
2449 */
2450 static void
2451f_ch_evalraw(typval_T *argvars, typval_T *rettv)
2452{
2453 ch_raw_common(argvars, rettv, TRUE);
2454}
2455
2456/*
2457 * "ch_sendraw()" function
2458 */
2459 static void
2460f_ch_sendraw(typval_T *argvars, typval_T *rettv)
2461{
2462 ch_raw_common(argvars, rettv, FALSE);
2463}
2464
2465/*
2466 * "ch_setoptions()" function
2467 */
2468 static void
2469f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
2470{
2471 channel_T *channel;
2472 jobopt_T opt;
2473
2474 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
2475 if (channel == NULL)
2476 return;
2477 clear_job_options(&opt);
2478 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002479 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002480 channel_set_options(channel, &opt);
2481 free_job_options(&opt);
2482}
2483
2484/*
2485 * "ch_status()" function
2486 */
2487 static void
2488f_ch_status(typval_T *argvars, typval_T *rettv)
2489{
2490 channel_T *channel;
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002491 jobopt_T opt;
2492 int part = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002493
2494 /* return an empty string by default */
2495 rettv->v_type = VAR_STRING;
2496 rettv->vval.v_string = NULL;
2497
2498 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002499
2500 if (argvars[1].v_type != VAR_UNKNOWN)
2501 {
2502 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002503 if (get_job_options(&argvars[1], &opt, JO_PART, 0) == OK
Bram Moolenaar7ef38102016-09-26 22:36:58 +02002504 && (opt.jo_set & JO_PART))
2505 part = opt.jo_part;
2506 }
2507
2508 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel, part));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002509}
2510#endif
2511
2512/*
2513 * "changenr()" function
2514 */
2515 static void
2516f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
2517{
2518 rettv->vval.v_number = curbuf->b_u_seq_cur;
2519}
2520
2521/*
2522 * "char2nr(string)" function
2523 */
2524 static void
2525f_char2nr(typval_T *argvars, typval_T *rettv)
2526{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002527 if (has_mbyte)
2528 {
2529 int utf8 = 0;
2530
2531 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002532 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002533
2534 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01002535 rettv->vval.v_number = utf_ptr2char(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002536 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002537 rettv->vval.v_number = (*mb_ptr2char)(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002538 }
2539 else
Bram Moolenaar13505972019-01-24 15:04:48 +01002540 rettv->vval.v_number = tv_get_string(&argvars[0])[0];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002541}
2542
2543/*
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02002544 * "chdir(dir)" function
2545 */
2546 static void
2547f_chdir(typval_T *argvars, typval_T *rettv)
2548{
2549 char_u *cwd;
2550 cdscope_T scope = CDSCOPE_GLOBAL;
2551
2552 rettv->v_type = VAR_STRING;
2553 rettv->vval.v_string = NULL;
2554
2555 if (argvars[0].v_type != VAR_STRING)
2556 return;
2557
2558 // Return the current directory
2559 cwd = alloc(MAXPATHL);
2560 if (cwd != NULL)
2561 {
2562 if (mch_dirname(cwd, MAXPATHL) != FAIL)
2563 {
2564#ifdef BACKSLASH_IN_FILENAME
2565 slash_adjust(cwd);
2566#endif
2567 rettv->vval.v_string = vim_strsave(cwd);
2568 }
2569 vim_free(cwd);
2570 }
2571
2572 if (curwin->w_localdir != NULL)
2573 scope = CDSCOPE_WINDOW;
2574 else if (curtab->tp_localdir != NULL)
2575 scope = CDSCOPE_TABPAGE;
2576
2577 if (!changedir_func(argvars[0].vval.v_string, TRUE, scope))
2578 // Directory change failed
2579 VIM_CLEAR(rettv->vval.v_string);
2580}
2581
2582/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002583 * "cindent(lnum)" function
2584 */
2585 static void
2586f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
2587{
2588#ifdef FEAT_CINDENT
2589 pos_T pos;
2590 linenr_T lnum;
2591
2592 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002593 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002594 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
2595 {
2596 curwin->w_cursor.lnum = lnum;
2597 rettv->vval.v_number = get_c_indent();
2598 curwin->w_cursor = pos;
2599 }
2600 else
2601#endif
2602 rettv->vval.v_number = -1;
2603}
2604
Bram Moolenaaraff74912019-03-30 18:11:49 +01002605 static win_T *
2606get_optional_window(typval_T *argvars, int idx)
2607{
2608 win_T *win = curwin;
2609
2610 if (argvars[idx].v_type != VAR_UNKNOWN)
2611 {
2612 win = find_win_by_nr_or_id(&argvars[idx]);
2613 if (win == NULL)
2614 {
2615 emsg(_(e_invalwindow));
2616 return NULL;
2617 }
2618 }
2619 return win;
2620}
2621
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002622/*
2623 * "clearmatches()" function
2624 */
2625 static void
2626f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2627{
2628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01002629 win_T *win = get_optional_window(argvars, 0);
2630
2631 if (win != NULL)
2632 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002633#endif
2634}
2635
2636/*
2637 * "col(string)" function
2638 */
2639 static void
2640f_col(typval_T *argvars, typval_T *rettv)
2641{
2642 colnr_T col = 0;
2643 pos_T *fp;
2644 int fnum = curbuf->b_fnum;
2645
2646 fp = var2fpos(&argvars[0], FALSE, &fnum);
2647 if (fp != NULL && fnum == curbuf->b_fnum)
2648 {
2649 if (fp->col == MAXCOL)
2650 {
2651 /* '> can be MAXCOL, get the length of the line then */
2652 if (fp->lnum <= curbuf->b_ml.ml_line_count)
2653 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
2654 else
2655 col = MAXCOL;
2656 }
2657 else
2658 {
2659 col = fp->col + 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002660 /* col(".") when the cursor is on the NUL at the end of the line
2661 * because of "coladd" can be seen as an extra column. */
2662 if (virtual_active() && fp == &curwin->w_cursor)
2663 {
2664 char_u *p = ml_get_cursor();
2665
2666 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
2667 curwin->w_virtcol - curwin->w_cursor.coladd))
2668 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002669 int l;
2670
2671 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
2672 col += l;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002673 }
2674 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002675 }
2676 }
2677 rettv->vval.v_number = col;
2678}
2679
2680#if defined(FEAT_INS_EXPAND)
2681/*
2682 * "complete()" function
2683 */
2684 static void
2685f_complete(typval_T *argvars, typval_T *rettv UNUSED)
2686{
2687 int startcol;
2688
2689 if ((State & INSERT) == 0)
2690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002691 emsg(_("E785: complete() can only be used in Insert mode"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002692 return;
2693 }
2694
2695 /* Check for undo allowed here, because if something was already inserted
2696 * the line was already saved for undo and this check isn't done. */
2697 if (!undo_allowed())
2698 return;
2699
2700 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
2701 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002702 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002703 return;
2704 }
2705
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002706 startcol = (int)tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002707 if (startcol <= 0)
2708 return;
2709
2710 set_completion(startcol - 1, argvars[1].vval.v_list);
2711}
2712
2713/*
2714 * "complete_add()" function
2715 */
2716 static void
2717f_complete_add(typval_T *argvars, typval_T *rettv)
2718{
2719 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
2720}
2721
2722/*
2723 * "complete_check()" function
2724 */
2725 static void
2726f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
2727{
2728 int saved = RedrawingDisabled;
2729
2730 RedrawingDisabled = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02002731 ins_compl_check_keys(0, TRUE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01002732 rettv->vval.v_number = ins_compl_interrupted();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002733 RedrawingDisabled = saved;
2734}
Bram Moolenaarfd133322019-03-29 12:20:27 +01002735
2736/*
2737 * "complete_info()" function
2738 */
2739 static void
2740f_complete_info(typval_T *argvars, typval_T *rettv)
2741{
2742 list_T *what_list = NULL;
2743
2744 if (rettv_dict_alloc(rettv) != OK)
2745 return;
2746
2747 if (argvars[0].v_type != VAR_UNKNOWN)
2748 {
2749 if (argvars[0].v_type != VAR_LIST)
2750 {
2751 emsg(_(e_listreq));
2752 return;
2753 }
2754 what_list = argvars[0].vval.v_list;
2755 }
2756 get_complete_info(what_list, rettv->vval.v_dict);
2757}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002758#endif
2759
2760/*
2761 * "confirm(message, buttons[, default [, type]])" function
2762 */
2763 static void
2764f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2765{
2766#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2767 char_u *message;
2768 char_u *buttons = NULL;
2769 char_u buf[NUMBUFLEN];
2770 char_u buf2[NUMBUFLEN];
2771 int def = 1;
2772 int type = VIM_GENERIC;
2773 char_u *typestr;
2774 int error = FALSE;
2775
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002776 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002777 if (message == NULL)
2778 error = TRUE;
2779 if (argvars[1].v_type != VAR_UNKNOWN)
2780 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002781 buttons = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002782 if (buttons == NULL)
2783 error = TRUE;
2784 if (argvars[2].v_type != VAR_UNKNOWN)
2785 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002786 def = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002787 if (argvars[3].v_type != VAR_UNKNOWN)
2788 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002789 typestr = tv_get_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002790 if (typestr == NULL)
2791 error = TRUE;
2792 else
2793 {
2794 switch (TOUPPER_ASC(*typestr))
2795 {
2796 case 'E': type = VIM_ERROR; break;
2797 case 'Q': type = VIM_QUESTION; break;
2798 case 'I': type = VIM_INFO; break;
2799 case 'W': type = VIM_WARNING; break;
2800 case 'G': type = VIM_GENERIC; break;
2801 }
2802 }
2803 }
2804 }
2805 }
2806
2807 if (buttons == NULL || *buttons == NUL)
2808 buttons = (char_u *)_("&Ok");
2809
2810 if (!error)
2811 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
2812 def, NULL, FALSE);
2813#endif
2814}
2815
2816/*
2817 * "copy()" function
2818 */
2819 static void
2820f_copy(typval_T *argvars, typval_T *rettv)
2821{
2822 item_copy(&argvars[0], rettv, FALSE, 0);
2823}
2824
2825#ifdef FEAT_FLOAT
2826/*
2827 * "cos()" function
2828 */
2829 static void
2830f_cos(typval_T *argvars, typval_T *rettv)
2831{
2832 float_T f = 0.0;
2833
2834 rettv->v_type = VAR_FLOAT;
2835 if (get_float_arg(argvars, &f) == OK)
2836 rettv->vval.v_float = cos(f);
2837 else
2838 rettv->vval.v_float = 0.0;
2839}
2840
2841/*
2842 * "cosh()" function
2843 */
2844 static void
2845f_cosh(typval_T *argvars, typval_T *rettv)
2846{
2847 float_T f = 0.0;
2848
2849 rettv->v_type = VAR_FLOAT;
2850 if (get_float_arg(argvars, &f) == OK)
2851 rettv->vval.v_float = cosh(f);
2852 else
2853 rettv->vval.v_float = 0.0;
2854}
2855#endif
2856
2857/*
2858 * "count()" function
2859 */
2860 static void
2861f_count(typval_T *argvars, typval_T *rettv)
2862{
2863 long n = 0;
2864 int ic = FALSE;
Bram Moolenaar9966b212017-07-28 16:46:57 +02002865 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002866
Bram Moolenaar9966b212017-07-28 16:46:57 +02002867 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002868 ic = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002869
2870 if (argvars[0].v_type == VAR_STRING)
2871 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002872 char_u *expr = tv_get_string_chk(&argvars[1]);
Bram Moolenaar9966b212017-07-28 16:46:57 +02002873 char_u *p = argvars[0].vval.v_string;
2874 char_u *next;
2875
Bram Moolenaar338e47f2017-12-19 11:55:26 +01002876 if (!error && expr != NULL && *expr != NUL && p != NULL)
Bram Moolenaar9966b212017-07-28 16:46:57 +02002877 {
2878 if (ic)
2879 {
2880 size_t len = STRLEN(expr);
2881
2882 while (*p != NUL)
2883 {
2884 if (MB_STRNICMP(p, expr, len) == 0)
2885 {
2886 ++n;
2887 p += len;
2888 }
2889 else
2890 MB_PTR_ADV(p);
2891 }
2892 }
2893 else
2894 while ((next = (char_u *)strstr((char *)p, (char *)expr))
2895 != NULL)
2896 {
2897 ++n;
2898 p = next + STRLEN(expr);
2899 }
2900 }
2901
2902 }
2903 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002904 {
2905 listitem_T *li;
2906 list_T *l;
2907 long idx;
2908
2909 if ((l = argvars[0].vval.v_list) != NULL)
2910 {
2911 li = l->lv_first;
2912 if (argvars[2].v_type != VAR_UNKNOWN)
2913 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002914 if (argvars[3].v_type != VAR_UNKNOWN)
2915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002916 idx = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002917 if (!error)
2918 {
2919 li = list_find(l, idx);
2920 if (li == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002922 }
2923 }
2924 if (error)
2925 li = NULL;
2926 }
2927
2928 for ( ; li != NULL; li = li->li_next)
2929 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
2930 ++n;
2931 }
2932 }
2933 else if (argvars[0].v_type == VAR_DICT)
2934 {
2935 int todo;
2936 dict_T *d;
2937 hashitem_T *hi;
2938
2939 if ((d = argvars[0].vval.v_dict) != NULL)
2940 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002941 if (argvars[2].v_type != VAR_UNKNOWN)
2942 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002943 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002944 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002945 }
2946
2947 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
2948 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2949 {
2950 if (!HASHITEM_EMPTY(hi))
2951 {
2952 --todo;
2953 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
2954 ++n;
2955 }
2956 }
2957 }
2958 }
2959 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002960 semsg(_(e_listdictarg), "count()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002961 rettv->vval.v_number = n;
2962}
2963
2964/*
2965 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
2966 *
2967 * Checks the existence of a cscope connection.
2968 */
2969 static void
2970f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
2971{
2972#ifdef FEAT_CSCOPE
2973 int num = 0;
2974 char_u *dbpath = NULL;
2975 char_u *prepend = NULL;
2976 char_u buf[NUMBUFLEN];
2977
2978 if (argvars[0].v_type != VAR_UNKNOWN
2979 && argvars[1].v_type != VAR_UNKNOWN)
2980 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002981 num = (int)tv_get_number(&argvars[0]);
2982 dbpath = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002983 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002984 prepend = tv_get_string_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02002985 }
2986
2987 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
2988#endif
2989}
2990
2991/*
2992 * "cursor(lnum, col)" function, or
2993 * "cursor(list)"
2994 *
2995 * Moves the cursor to the specified line and column.
2996 * Returns 0 when the position could be set, -1 otherwise.
2997 */
2998 static void
2999f_cursor(typval_T *argvars, typval_T *rettv)
3000{
3001 long line, col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003002 long coladd = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003003 int set_curswant = TRUE;
3004
3005 rettv->vval.v_number = -1;
3006 if (argvars[1].v_type == VAR_UNKNOWN)
3007 {
3008 pos_T pos;
3009 colnr_T curswant = -1;
3010
3011 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
3012 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003014 return;
3015 }
3016 line = pos.lnum;
3017 col = pos.col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003018 coladd = pos.coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003019 if (curswant >= 0)
3020 {
3021 curwin->w_curswant = curswant - 1;
3022 set_curswant = FALSE;
3023 }
3024 }
3025 else
3026 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003027 line = tv_get_lnum(argvars);
3028 col = (long)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003029 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003030 coladd = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003031 }
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01003032 if (line < 0 || col < 0 || coladd < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003033 return; /* type error; errmsg already given */
3034 if (line > 0)
3035 curwin->w_cursor.lnum = line;
3036 if (col > 0)
3037 curwin->w_cursor.col = col - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003038 curwin->w_cursor.coladd = coladd;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003039
3040 /* Make sure the cursor is in a valid position. */
3041 check_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003042 /* Correct cursor for multi-byte character. */
3043 if (has_mbyte)
3044 mb_adjust_cursor();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003045
3046 curwin->w_set_curswant = set_curswant;
3047 rettv->vval.v_number = 0;
3048}
3049
Bram Moolenaar4f974752019-02-17 17:44:42 +01003050#ifdef MSWIN
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003051/*
3052 * "debugbreak()" function
3053 */
3054 static void
3055f_debugbreak(typval_T *argvars, typval_T *rettv)
3056{
3057 int pid;
3058
3059 rettv->vval.v_number = FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003060 pid = (int)tv_get_number(&argvars[0]);
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003061 if (pid == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 emsg(_(e_invarg));
Bram Moolenaar4551c0a2018-06-20 22:38:21 +02003063 else
3064 {
3065 HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
3066
3067 if (hProcess != NULL)
3068 {
3069 DebugBreakProcess(hProcess);
3070 CloseHandle(hProcess);
3071 rettv->vval.v_number = OK;
3072 }
3073 }
3074}
3075#endif
3076
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003077/*
3078 * "deepcopy()" function
3079 */
3080 static void
3081f_deepcopy(typval_T *argvars, typval_T *rettv)
3082{
3083 int noref = 0;
3084 int copyID;
3085
3086 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003087 noref = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003088 if (noref < 0 || noref > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003089 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003090 else
3091 {
3092 copyID = get_copyID();
3093 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? copyID : 0);
3094 }
3095}
3096
3097/*
3098 * "delete()" function
3099 */
3100 static void
3101f_delete(typval_T *argvars, typval_T *rettv)
3102{
3103 char_u nbuf[NUMBUFLEN];
3104 char_u *name;
3105 char_u *flags;
3106
3107 rettv->vval.v_number = -1;
3108 if (check_restricted() || check_secure())
3109 return;
3110
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003111 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003112 if (name == NULL || *name == NUL)
3113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003114 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003115 return;
3116 }
3117
3118 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003119 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003120 else
3121 flags = (char_u *)"";
3122
3123 if (*flags == NUL)
3124 /* delete a file */
3125 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
3126 else if (STRCMP(flags, "d") == 0)
3127 /* delete an empty directory */
3128 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
3129 else if (STRCMP(flags, "rf") == 0)
3130 /* delete a directory recursively */
3131 rettv->vval.v_number = delete_recursive(name);
3132 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003133 semsg(_(e_invexpr2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003134}
3135
3136/*
Bram Moolenaard79a2622018-06-07 18:17:46 +02003137 * "deletebufline()" function
3138 */
3139 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02003140f_deletebufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaard79a2622018-06-07 18:17:46 +02003141{
3142 buf_T *buf;
3143 linenr_T first, last;
3144 linenr_T lnum;
3145 long count;
3146 int is_curbuf;
3147 buf_T *curbuf_save = NULL;
3148 win_T *curwin_save = NULL;
3149 tabpage_T *tp;
3150 win_T *wp;
3151
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003152 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003153 if (buf == NULL)
3154 {
3155 rettv->vval.v_number = 1; /* FAIL */
3156 return;
3157 }
3158 is_curbuf = buf == curbuf;
3159
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003160 first = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003161 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003162 last = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaard79a2622018-06-07 18:17:46 +02003163 else
3164 last = first;
3165
3166 if (buf->b_ml.ml_mfp == NULL || first < 1
3167 || first > buf->b_ml.ml_line_count || last < first)
3168 {
3169 rettv->vval.v_number = 1; /* FAIL */
3170 return;
3171 }
3172
3173 if (!is_curbuf)
3174 {
3175 curbuf_save = curbuf;
3176 curwin_save = curwin;
3177 curbuf = buf;
3178 find_win_for_curbuf();
3179 }
3180 if (last > curbuf->b_ml.ml_line_count)
3181 last = curbuf->b_ml.ml_line_count;
3182 count = last - first + 1;
3183
3184 // When coming here from Insert mode, sync undo, so that this can be
3185 // undone separately from what was previously inserted.
3186 if (u_sync_once == 2)
3187 {
3188 u_sync_once = 1; // notify that u_sync() was called
3189 u_sync(TRUE);
3190 }
3191
3192 if (u_save(first - 1, last + 1) == FAIL)
3193 {
3194 rettv->vval.v_number = 1; /* FAIL */
3195 return;
3196 }
3197
3198 for (lnum = first; lnum <= last; ++lnum)
3199 ml_delete(first, TRUE);
3200
3201 FOR_ALL_TAB_WINDOWS(tp, wp)
3202 if (wp->w_buffer == buf)
3203 {
3204 if (wp->w_cursor.lnum > last)
3205 wp->w_cursor.lnum -= count;
3206 else if (wp->w_cursor.lnum> first)
3207 wp->w_cursor.lnum = first;
3208 if (wp->w_cursor.lnum > wp->w_buffer->b_ml.ml_line_count)
3209 wp->w_cursor.lnum = wp->w_buffer->b_ml.ml_line_count;
3210 }
3211 check_cursor_col();
3212 deleted_lines_mark(first, count);
3213
3214 if (!is_curbuf)
3215 {
3216 curbuf = curbuf_save;
3217 curwin = curwin_save;
3218 }
3219}
3220
3221/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003222 * "did_filetype()" function
3223 */
3224 static void
3225f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3226{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003227 rettv->vval.v_number = did_filetype;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003228}
3229
3230/*
3231 * "diff_filler()" function
3232 */
3233 static void
3234f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3235{
3236#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003237 rettv->vval.v_number = diff_check_fill(curwin, tv_get_lnum(argvars));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003238#endif
3239}
3240
3241/*
3242 * "diff_hlID()" function
3243 */
3244 static void
3245f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
3246{
3247#ifdef FEAT_DIFF
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003248 linenr_T lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003249 static linenr_T prev_lnum = 0;
Bram Moolenaar79518e22017-02-17 16:31:35 +01003250 static varnumber_T changedtick = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003251 static int fnum = 0;
3252 static int change_start = 0;
3253 static int change_end = 0;
3254 static hlf_T hlID = (hlf_T)0;
3255 int filler_lines;
3256 int col;
3257
3258 if (lnum < 0) /* ignore type error in {lnum} arg */
3259 lnum = 0;
3260 if (lnum != prev_lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003261 || changedtick != CHANGEDTICK(curbuf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003262 || fnum != curbuf->b_fnum)
3263 {
3264 /* New line, buffer, change: need to get the values. */
3265 filler_lines = diff_check(curwin, lnum);
3266 if (filler_lines < 0)
3267 {
3268 if (filler_lines == -1)
3269 {
3270 change_start = MAXCOL;
3271 change_end = -1;
3272 if (diff_find_change(curwin, lnum, &change_start, &change_end))
3273 hlID = HLF_ADD; /* added line */
3274 else
3275 hlID = HLF_CHD; /* changed line */
3276 }
3277 else
3278 hlID = HLF_ADD; /* added line */
3279 }
3280 else
3281 hlID = (hlf_T)0;
3282 prev_lnum = lnum;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01003283 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003284 fnum = curbuf->b_fnum;
3285 }
3286
3287 if (hlID == HLF_CHD || hlID == HLF_TXD)
3288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003289 col = tv_get_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003290 if (col >= change_start && col <= change_end)
3291 hlID = HLF_TXD; /* changed text */
3292 else
3293 hlID = HLF_CHD; /* changed line */
3294 }
3295 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
3296#endif
3297}
3298
3299/*
3300 * "empty({expr})" function
3301 */
3302 static void
3303f_empty(typval_T *argvars, typval_T *rettv)
3304{
3305 int n = FALSE;
3306
3307 switch (argvars[0].v_type)
3308 {
3309 case VAR_STRING:
3310 case VAR_FUNC:
3311 n = argvars[0].vval.v_string == NULL
3312 || *argvars[0].vval.v_string == NUL;
3313 break;
3314 case VAR_PARTIAL:
3315 n = FALSE;
3316 break;
3317 case VAR_NUMBER:
3318 n = argvars[0].vval.v_number == 0;
3319 break;
3320 case VAR_FLOAT:
3321#ifdef FEAT_FLOAT
3322 n = argvars[0].vval.v_float == 0.0;
3323 break;
3324#endif
3325 case VAR_LIST:
3326 n = argvars[0].vval.v_list == NULL
3327 || argvars[0].vval.v_list->lv_first == NULL;
3328 break;
3329 case VAR_DICT:
3330 n = argvars[0].vval.v_dict == NULL
3331 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
3332 break;
3333 case VAR_SPECIAL:
3334 n = argvars[0].vval.v_number != VVAL_TRUE;
3335 break;
3336
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003337 case VAR_BLOB:
3338 n = argvars[0].vval.v_blob == NULL
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003339 || argvars[0].vval.v_blob->bv_ga.ga_len == 0;
3340 break;
3341
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003342 case VAR_JOB:
3343#ifdef FEAT_JOB_CHANNEL
3344 n = argvars[0].vval.v_job == NULL
3345 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
3346 break;
3347#endif
3348 case VAR_CHANNEL:
3349#ifdef FEAT_JOB_CHANNEL
3350 n = argvars[0].vval.v_channel == NULL
3351 || !channel_is_open(argvars[0].vval.v_channel);
3352 break;
3353#endif
3354 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01003355 internal_error("f_empty(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003356 n = TRUE;
3357 break;
3358 }
3359
3360 rettv->vval.v_number = n;
3361}
3362
3363/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02003364 * "environ()" function
3365 */
3366 static void
3367f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3368{
3369#if !defined(AMIGA)
3370 int i = 0;
3371 char_u *entry, *value;
3372# ifdef MSWIN
3373 extern wchar_t **_wenviron;
3374# else
3375 extern char **environ;
3376# endif
3377
3378 if (rettv_dict_alloc(rettv) != OK)
3379 return;
3380
3381# ifdef MSWIN
3382 if (*_wenviron == NULL)
3383 return;
3384# else
3385 if (*environ == NULL)
3386 return;
3387# endif
3388
3389 for (i = 0; ; ++i)
3390 {
3391# ifdef MSWIN
3392 short_u *p;
3393
3394 if ((p = (short_u *)_wenviron[i]) == NULL)
3395 return;
3396 entry = utf16_to_enc(p, NULL);
3397# else
3398 if ((entry = (char_u *)environ[i]) == NULL)
3399 return;
3400 entry = vim_strsave(entry);
3401# endif
3402 if (entry == NULL) // out of memory
3403 return;
3404 if ((value = vim_strchr(entry, '=')) == NULL)
3405 {
3406 vim_free(entry);
3407 continue;
3408 }
3409 *value++ = NUL;
3410 dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3411 vim_free(entry);
3412 }
3413#endif
3414}
3415
3416/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003417 * "escape({string}, {chars})" function
3418 */
3419 static void
3420f_escape(typval_T *argvars, typval_T *rettv)
3421{
3422 char_u buf[NUMBUFLEN];
3423
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003424 rettv->vval.v_string = vim_strsave_escaped(tv_get_string(&argvars[0]),
3425 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003426 rettv->v_type = VAR_STRING;
3427}
3428
3429/*
3430 * "eval()" function
3431 */
3432 static void
3433f_eval(typval_T *argvars, typval_T *rettv)
3434{
3435 char_u *s, *p;
3436
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003437 s = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003438 if (s != NULL)
3439 s = skipwhite(s);
3440
3441 p = s;
3442 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
3443 {
3444 if (p != NULL && !aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003445 semsg(_(e_invexpr2), p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003446 need_clr_eos = FALSE;
3447 rettv->v_type = VAR_NUMBER;
3448 rettv->vval.v_number = 0;
3449 }
3450 else if (*s != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003451 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003452}
3453
3454/*
3455 * "eventhandler()" function
3456 */
3457 static void
3458f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
3459{
3460 rettv->vval.v_number = vgetc_busy;
3461}
3462
3463/*
3464 * "executable()" function
3465 */
3466 static void
3467f_executable(typval_T *argvars, typval_T *rettv)
3468{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003469 char_u *name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003470
3471 /* Check in $PATH and also check directly if there is a directory name. */
3472 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
3473 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
3474}
3475
3476static garray_T redir_execute_ga;
3477
3478/*
3479 * Append "value[value_len]" to the execute() output.
3480 */
3481 void
3482execute_redir_str(char_u *value, int value_len)
3483{
3484 int len;
3485
3486 if (value_len == -1)
3487 len = (int)STRLEN(value); /* Append the entire string */
3488 else
3489 len = value_len; /* Append only "value_len" characters */
3490 if (ga_grow(&redir_execute_ga, len) == OK)
3491 {
3492 mch_memmove((char *)redir_execute_ga.ga_data
3493 + redir_execute_ga.ga_len, value, len);
3494 redir_execute_ga.ga_len += len;
3495 }
3496}
3497
3498/*
3499 * Get next line from a list.
3500 * Called by do_cmdline() to get the next line.
3501 * Returns allocated string, or NULL for end of function.
3502 */
3503
3504 static char_u *
3505get_list_line(
3506 int c UNUSED,
3507 void *cookie,
3508 int indent UNUSED)
3509{
3510 listitem_T **p = (listitem_T **)cookie;
3511 listitem_T *item = *p;
3512 char_u buf[NUMBUFLEN];
3513 char_u *s;
3514
3515 if (item == NULL)
3516 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003517 s = tv_get_string_buf_chk(&item->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003518 *p = item->li_next;
3519 return s == NULL ? NULL : vim_strsave(s);
3520}
3521
3522/*
3523 * "execute()" function
3524 */
3525 static void
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003526execute_common(typval_T *argvars, typval_T *rettv, int arg_off)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003527{
3528 char_u *cmd = NULL;
3529 list_T *list = NULL;
3530 int save_msg_silent = msg_silent;
3531 int save_emsg_silent = emsg_silent;
3532 int save_emsg_noredir = emsg_noredir;
3533 int save_redir_execute = redir_execute;
Bram Moolenaar20951482017-12-25 13:44:43 +01003534 int save_redir_off = redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003535 garray_T save_ga;
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003536 int save_msg_col = msg_col;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003537 int echo_output = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003538
3539 rettv->vval.v_string = NULL;
3540 rettv->v_type = VAR_STRING;
3541
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003542 if (argvars[arg_off].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003543 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003544 list = argvars[arg_off].vval.v_list;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003545 if (list == NULL || list->lv_first == NULL)
3546 /* empty list, no commands, empty output */
3547 return;
3548 ++list->lv_refcount;
3549 }
3550 else
3551 {
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003552 cmd = tv_get_string_chk(&argvars[arg_off]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003553 if (cmd == NULL)
3554 return;
3555 }
3556
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003557 if (argvars[arg_off + 1].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003558 {
3559 char_u buf[NUMBUFLEN];
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003560 char_u *s = tv_get_string_buf_chk(&argvars[arg_off + 1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003561
3562 if (s == NULL)
3563 return;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003564 if (*s == NUL)
3565 echo_output = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003566 if (STRNCMP(s, "silent", 6) == 0)
3567 ++msg_silent;
3568 if (STRCMP(s, "silent!") == 0)
3569 {
3570 emsg_silent = TRUE;
3571 emsg_noredir = TRUE;
3572 }
3573 }
3574 else
3575 ++msg_silent;
3576
3577 if (redir_execute)
3578 save_ga = redir_execute_ga;
3579 ga_init2(&redir_execute_ga, (int)sizeof(char), 500);
3580 redir_execute = TRUE;
Bram Moolenaar20951482017-12-25 13:44:43 +01003581 redir_off = FALSE;
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003582 if (!echo_output)
3583 msg_col = 0; // prevent leading spaces
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003584
3585 if (cmd != NULL)
3586 do_cmdline_cmd(cmd);
3587 else
3588 {
3589 listitem_T *item = list->lv_first;
3590
3591 do_cmdline(NULL, get_list_line, (void *)&item,
3592 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT|DOCMD_KEYTYPED);
3593 --list->lv_refcount;
3594 }
3595
Bram Moolenaard297f352017-01-29 20:31:21 +01003596 /* Need to append a NUL to the result. */
3597 if (ga_grow(&redir_execute_ga, 1) == OK)
3598 {
3599 ((char *)redir_execute_ga.ga_data)[redir_execute_ga.ga_len] = NUL;
3600 rettv->vval.v_string = redir_execute_ga.ga_data;
3601 }
3602 else
3603 {
3604 ga_clear(&redir_execute_ga);
3605 rettv->vval.v_string = NULL;
3606 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003607 msg_silent = save_msg_silent;
3608 emsg_silent = save_emsg_silent;
3609 emsg_noredir = save_emsg_noredir;
3610
3611 redir_execute = save_redir_execute;
3612 if (redir_execute)
3613 redir_execute_ga = save_ga;
Bram Moolenaar20951482017-12-25 13:44:43 +01003614 redir_off = save_redir_off;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003615
Bram Moolenaar10ccaa12018-12-07 16:38:23 +01003616 // "silent reg" or "silent echo x" leaves msg_col somewhere in the line.
Bram Moolenaar446e7a32018-12-08 13:57:42 +01003617 if (echo_output)
3618 // When not working silently: put it in column zero. A following
3619 // "echon" will overwrite the message, unavoidably.
3620 msg_col = 0;
3621 else
3622 // When working silently: Put it back where it was, since nothing
3623 // should have been written.
3624 msg_col = save_msg_col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003625}
3626
3627/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02003628 * "execute()" function
3629 */
3630 static void
3631f_execute(typval_T *argvars, typval_T *rettv)
3632{
3633 execute_common(argvars, rettv, 0);
3634}
3635
3636/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003637 * "exepath()" function
3638 */
3639 static void
3640f_exepath(typval_T *argvars, typval_T *rettv)
3641{
3642 char_u *p = NULL;
3643
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003644 (void)mch_can_exe(tv_get_string(&argvars[0]), &p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003645 rettv->v_type = VAR_STRING;
3646 rettv->vval.v_string = p;
3647}
3648
3649/*
3650 * "exists()" function
3651 */
3652 static void
3653f_exists(typval_T *argvars, typval_T *rettv)
3654{
3655 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003656 int n = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003657
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003658 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003659 if (*p == '$') /* environment variable */
3660 {
3661 /* first try "normal" environment variables (fast) */
3662 if (mch_getenv(p + 1) != NULL)
3663 n = TRUE;
3664 else
3665 {
3666 /* try expanding things like $VIM and ${HOME} */
3667 p = expand_env_save(p);
3668 if (p != NULL && *p != '$')
3669 n = TRUE;
3670 vim_free(p);
3671 }
3672 }
3673 else if (*p == '&' || *p == '+') /* option */
3674 {
3675 n = (get_option_tv(&p, NULL, TRUE) == OK);
3676 if (*skipwhite(p) != NUL)
3677 n = FALSE; /* trailing garbage */
3678 }
3679 else if (*p == '*') /* internal or user defined function */
3680 {
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02003681 n = function_exists(p + 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003682 }
3683 else if (*p == ':')
3684 {
3685 n = cmd_exists(p + 1);
3686 }
3687 else if (*p == '#')
3688 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003689 if (p[1] == '#')
3690 n = autocmd_supported(p + 2);
3691 else
3692 n = au_exists(p + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003693 }
3694 else /* internal variable */
3695 {
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003696 n = var_exists(p);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003697 }
3698
3699 rettv->vval.v_number = n;
3700}
3701
3702#ifdef FEAT_FLOAT
3703/*
3704 * "exp()" function
3705 */
3706 static void
3707f_exp(typval_T *argvars, typval_T *rettv)
3708{
3709 float_T f = 0.0;
3710
3711 rettv->v_type = VAR_FLOAT;
3712 if (get_float_arg(argvars, &f) == OK)
3713 rettv->vval.v_float = exp(f);
3714 else
3715 rettv->vval.v_float = 0.0;
3716}
3717#endif
3718
3719/*
3720 * "expand()" function
3721 */
3722 static void
3723f_expand(typval_T *argvars, typval_T *rettv)
3724{
3725 char_u *s;
3726 int len;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003727 char *errormsg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003728 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
3729 expand_T xpc;
3730 int error = FALSE;
3731 char_u *result;
3732
3733 rettv->v_type = VAR_STRING;
3734 if (argvars[1].v_type != VAR_UNKNOWN
3735 && argvars[2].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003736 && tv_get_number_chk(&argvars[2], &error)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003737 && !error)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003738 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003739
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003740 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003741 if (*s == '%' || *s == '#' || *s == '<')
3742 {
3743 ++emsg_off;
3744 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
3745 --emsg_off;
3746 if (rettv->v_type == VAR_LIST)
3747 {
3748 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
3749 list_append_string(rettv->vval.v_list, result, -1);
3750 else
3751 vim_free(result);
3752 }
3753 else
3754 rettv->vval.v_string = result;
3755 }
3756 else
3757 {
3758 /* When the optional second argument is non-zero, don't remove matches
3759 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
3760 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003761 && tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003762 options |= WILD_KEEP_ALL;
3763 if (!error)
3764 {
3765 ExpandInit(&xpc);
3766 xpc.xp_context = EXPAND_FILES;
3767 if (p_wic)
3768 options += WILD_ICASE;
3769 if (rettv->v_type == VAR_STRING)
3770 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
3771 options, WILD_ALL);
3772 else if (rettv_list_alloc(rettv) != FAIL)
3773 {
3774 int i;
3775
3776 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
3777 for (i = 0; i < xpc.xp_numfiles; i++)
3778 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
3779 ExpandCleanup(&xpc);
3780 }
3781 }
3782 else
3783 rettv->vval.v_string = NULL;
3784 }
3785}
3786
3787/*
3788 * "extend(list, list [, idx])" function
3789 * "extend(dict, dict [, action])" function
3790 */
3791 static void
3792f_extend(typval_T *argvars, typval_T *rettv)
3793{
3794 char_u *arg_errmsg = (char_u *)N_("extend() argument");
3795
3796 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
3797 {
3798 list_T *l1, *l2;
3799 listitem_T *item;
3800 long before;
3801 int error = FALSE;
3802
3803 l1 = argvars[0].vval.v_list;
3804 l2 = argvars[1].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003805 if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003806 && l2 != NULL)
3807 {
3808 if (argvars[2].v_type != VAR_UNKNOWN)
3809 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003810 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003811 if (error)
3812 return; /* type error; errmsg already given */
3813
3814 if (before == l1->lv_len)
3815 item = NULL;
3816 else
3817 {
3818 item = list_find(l1, before);
3819 if (item == NULL)
3820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003821 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003822 return;
3823 }
3824 }
3825 }
3826 else
3827 item = NULL;
3828 list_extend(l1, l2, item);
3829
3830 copy_tv(&argvars[0], rettv);
3831 }
3832 }
3833 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
3834 {
3835 dict_T *d1, *d2;
3836 char_u *action;
3837 int i;
3838
3839 d1 = argvars[0].vval.v_dict;
3840 d2 = argvars[1].vval.v_dict;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003841 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003842 && d2 != NULL)
3843 {
3844 /* Check the third argument. */
3845 if (argvars[2].v_type != VAR_UNKNOWN)
3846 {
3847 static char *(av[]) = {"keep", "force", "error"};
3848
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003849 action = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003850 if (action == NULL)
3851 return; /* type error; errmsg already given */
3852 for (i = 0; i < 3; ++i)
3853 if (STRCMP(action, av[i]) == 0)
3854 break;
3855 if (i == 3)
3856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003857 semsg(_(e_invarg2), action);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003858 return;
3859 }
3860 }
3861 else
3862 action = (char_u *)"force";
3863
3864 dict_extend(d1, d2, action);
3865
3866 copy_tv(&argvars[0], rettv);
3867 }
3868 }
3869 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003870 semsg(_(e_listdictarg), "extend()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003871}
3872
3873/*
3874 * "feedkeys()" function
3875 */
3876 static void
3877f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
3878{
3879 int remap = TRUE;
3880 int insert = FALSE;
3881 char_u *keys, *flags;
3882 char_u nbuf[NUMBUFLEN];
3883 int typed = FALSE;
3884 int execute = FALSE;
3885 int dangerous = FALSE;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003886 int lowlevel = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003887 char_u *keys_esc;
3888
3889 /* This is not allowed in the sandbox. If the commands would still be
3890 * executed in the sandbox it would be OK, but it probably happens later,
3891 * when "sandbox" is no longer set. */
3892 if (check_secure())
3893 return;
3894
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003895 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003896
3897 if (argvars[1].v_type != VAR_UNKNOWN)
3898 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003899 flags = tv_get_string_buf(&argvars[1], nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003900 for ( ; *flags != NUL; ++flags)
3901 {
3902 switch (*flags)
3903 {
3904 case 'n': remap = FALSE; break;
3905 case 'm': remap = TRUE; break;
3906 case 't': typed = TRUE; break;
3907 case 'i': insert = TRUE; break;
3908 case 'x': execute = TRUE; break;
3909 case '!': dangerous = TRUE; break;
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003910 case 'L': lowlevel = TRUE; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003911 }
3912 }
3913 }
3914
3915 if (*keys != NUL || execute)
3916 {
3917 /* Need to escape K_SPECIAL and CSI before putting the string in the
3918 * typeahead buffer. */
3919 keys_esc = vim_strsave_escape_csi(keys);
3920 if (keys_esc != NULL)
3921 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003922 if (lowlevel)
3923 {
3924#ifdef USE_INPUT_BUF
3925 add_to_input_buf(keys, (int)STRLEN(keys));
3926#else
3927 emsg(_("E980: lowlevel input not supported"));
3928#endif
3929 }
3930 else
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003931 {
Bram Moolenaar5e66b422019-01-24 21:58:10 +01003932 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003933 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003934 if (vgetc_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003935#ifdef FEAT_TIMERS
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003936 || timer_busy
Bram Moolenaar5d7be4f2017-06-25 13:40:17 +02003937#endif
Bram Moolenaar8d4ce562019-01-30 22:01:40 +01003938 )
3939 typebuf_was_filled = TRUE;
3940 }
3941 vim_free(keys_esc);
3942
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003943 if (execute)
3944 {
3945 int save_msg_scroll = msg_scroll;
3946
3947 /* Avoid a 1 second delay when the keys start Insert mode. */
3948 msg_scroll = FALSE;
3949
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003950 if (!dangerous)
3951 ++ex_normal_busy;
Bram Moolenaar905dd902019-04-07 14:21:47 +02003952 exec_normal(TRUE, lowlevel, TRUE);
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02003953 if (!dangerous)
3954 --ex_normal_busy;
3955
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003956 msg_scroll |= save_msg_scroll;
3957 }
3958 }
3959 }
3960}
3961
3962/*
3963 * "filereadable()" function
3964 */
3965 static void
3966f_filereadable(typval_T *argvars, typval_T *rettv)
3967{
3968 int fd;
3969 char_u *p;
3970 int n;
3971
3972#ifndef O_NONBLOCK
3973# define O_NONBLOCK 0
3974#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003975 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003976 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
3977 O_RDONLY | O_NONBLOCK, 0)) >= 0)
3978 {
3979 n = TRUE;
3980 close(fd);
3981 }
3982 else
3983 n = FALSE;
3984
3985 rettv->vval.v_number = n;
3986}
3987
3988/*
3989 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
3990 * rights to write into.
3991 */
3992 static void
3993f_filewritable(typval_T *argvars, typval_T *rettv)
3994{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003995 rettv->vval.v_number = filewritable(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003996}
3997
3998 static void
3999findfilendir(
4000 typval_T *argvars UNUSED,
4001 typval_T *rettv,
4002 int find_what UNUSED)
4003{
4004#ifdef FEAT_SEARCHPATH
4005 char_u *fname;
4006 char_u *fresult = NULL;
4007 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
4008 char_u *p;
4009 char_u pathbuf[NUMBUFLEN];
4010 int count = 1;
4011 int first = TRUE;
4012 int error = FALSE;
4013#endif
4014
4015 rettv->vval.v_string = NULL;
4016 rettv->v_type = VAR_STRING;
4017
4018#ifdef FEAT_SEARCHPATH
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004019 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004020
4021 if (argvars[1].v_type != VAR_UNKNOWN)
4022 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004023 p = tv_get_string_buf_chk(&argvars[1], pathbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004024 if (p == NULL)
4025 error = TRUE;
4026 else
4027 {
4028 if (*p != NUL)
4029 path = p;
4030
4031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004032 count = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004033 }
4034 }
4035
4036 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
4037 error = TRUE;
4038
4039 if (*fname != NUL && !error)
4040 {
4041 do
4042 {
4043 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
4044 vim_free(fresult);
4045 fresult = find_file_in_path_option(first ? fname : NULL,
4046 first ? (int)STRLEN(fname) : 0,
4047 0, first, path,
4048 find_what,
4049 curbuf->b_ffname,
4050 find_what == FINDFILE_DIR
4051 ? (char_u *)"" : curbuf->b_p_sua);
4052 first = FALSE;
4053
4054 if (fresult != NULL && rettv->v_type == VAR_LIST)
4055 list_append_string(rettv->vval.v_list, fresult, -1);
4056
4057 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
4058 }
4059
4060 if (rettv->v_type == VAR_STRING)
4061 rettv->vval.v_string = fresult;
4062#endif
4063}
4064
4065/*
4066 * "filter()" function
4067 */
4068 static void
4069f_filter(typval_T *argvars, typval_T *rettv)
4070{
4071 filter_map(argvars, rettv, FALSE);
4072}
4073
4074/*
4075 * "finddir({fname}[, {path}[, {count}]])" function
4076 */
4077 static void
4078f_finddir(typval_T *argvars, typval_T *rettv)
4079{
4080 findfilendir(argvars, rettv, FINDFILE_DIR);
4081}
4082
4083/*
4084 * "findfile({fname}[, {path}[, {count}]])" function
4085 */
4086 static void
4087f_findfile(typval_T *argvars, typval_T *rettv)
4088{
4089 findfilendir(argvars, rettv, FINDFILE_FILE);
4090}
4091
4092#ifdef FEAT_FLOAT
4093/*
4094 * "float2nr({float})" function
4095 */
4096 static void
4097f_float2nr(typval_T *argvars, typval_T *rettv)
4098{
4099 float_T f = 0.0;
4100
4101 if (get_float_arg(argvars, &f) == OK)
4102 {
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004103 if (f <= -VARNUM_MAX + DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004104 rettv->vval.v_number = -VARNUM_MAX;
Bram Moolenaar863e80b2017-06-04 20:30:00 +02004105 else if (f >= VARNUM_MAX - DBL_EPSILON)
Bram Moolenaar7a40ea22017-01-22 18:34:57 +01004106 rettv->vval.v_number = VARNUM_MAX;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004107 else
4108 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004109 }
4110}
4111
4112/*
4113 * "floor({float})" function
4114 */
4115 static void
4116f_floor(typval_T *argvars, typval_T *rettv)
4117{
4118 float_T f = 0.0;
4119
4120 rettv->v_type = VAR_FLOAT;
4121 if (get_float_arg(argvars, &f) == OK)
4122 rettv->vval.v_float = floor(f);
4123 else
4124 rettv->vval.v_float = 0.0;
4125}
4126
4127/*
4128 * "fmod()" function
4129 */
4130 static void
4131f_fmod(typval_T *argvars, typval_T *rettv)
4132{
4133 float_T fx = 0.0, fy = 0.0;
4134
4135 rettv->v_type = VAR_FLOAT;
4136 if (get_float_arg(argvars, &fx) == OK
4137 && get_float_arg(&argvars[1], &fy) == OK)
4138 rettv->vval.v_float = fmod(fx, fy);
4139 else
4140 rettv->vval.v_float = 0.0;
4141}
4142#endif
4143
4144/*
4145 * "fnameescape({string})" function
4146 */
4147 static void
4148f_fnameescape(typval_T *argvars, typval_T *rettv)
4149{
4150 rettv->vval.v_string = vim_strsave_fnameescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004151 tv_get_string(&argvars[0]), FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004152 rettv->v_type = VAR_STRING;
4153}
4154
4155/*
4156 * "fnamemodify({fname}, {mods})" function
4157 */
4158 static void
4159f_fnamemodify(typval_T *argvars, typval_T *rettv)
4160{
4161 char_u *fname;
4162 char_u *mods;
4163 int usedlen = 0;
4164 int len;
4165 char_u *fbuf = NULL;
4166 char_u buf[NUMBUFLEN];
4167
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004168 fname = tv_get_string_chk(&argvars[0]);
4169 mods = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004170 if (fname == NULL || mods == NULL)
4171 fname = NULL;
4172 else
4173 {
4174 len = (int)STRLEN(fname);
Bram Moolenaar00136dc2018-07-25 21:19:13 +02004175 (void)modify_fname(mods, FALSE, &usedlen, &fname, &fbuf, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004176 }
4177
4178 rettv->v_type = VAR_STRING;
4179 if (fname == NULL)
4180 rettv->vval.v_string = NULL;
4181 else
4182 rettv->vval.v_string = vim_strnsave(fname, len);
4183 vim_free(fbuf);
4184}
4185
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004186/*
4187 * "foldclosed()" function
4188 */
4189 static void
4190foldclosed_both(
4191 typval_T *argvars UNUSED,
4192 typval_T *rettv,
4193 int end UNUSED)
4194{
4195#ifdef FEAT_FOLDING
4196 linenr_T lnum;
4197 linenr_T first, last;
4198
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004199 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4201 {
4202 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
4203 {
4204 if (end)
4205 rettv->vval.v_number = (varnumber_T)last;
4206 else
4207 rettv->vval.v_number = (varnumber_T)first;
4208 return;
4209 }
4210 }
4211#endif
4212 rettv->vval.v_number = -1;
4213}
4214
4215/*
4216 * "foldclosed()" function
4217 */
4218 static void
4219f_foldclosed(typval_T *argvars, typval_T *rettv)
4220{
4221 foldclosed_both(argvars, rettv, FALSE);
4222}
4223
4224/*
4225 * "foldclosedend()" function
4226 */
4227 static void
4228f_foldclosedend(typval_T *argvars, typval_T *rettv)
4229{
4230 foldclosed_both(argvars, rettv, TRUE);
4231}
4232
4233/*
4234 * "foldlevel()" function
4235 */
4236 static void
4237f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4238{
4239#ifdef FEAT_FOLDING
4240 linenr_T lnum;
4241
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004242 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
4244 rettv->vval.v_number = foldLevel(lnum);
4245#endif
4246}
4247
4248/*
4249 * "foldtext()" function
4250 */
4251 static void
4252f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
4253{
4254#ifdef FEAT_FOLDING
4255 linenr_T foldstart;
4256 linenr_T foldend;
4257 char_u *dashes;
4258 linenr_T lnum;
4259 char_u *s;
4260 char_u *r;
4261 int len;
4262 char *txt;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004263 long count;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004264#endif
4265
4266 rettv->v_type = VAR_STRING;
4267 rettv->vval.v_string = NULL;
4268#ifdef FEAT_FOLDING
4269 foldstart = (linenr_T)get_vim_var_nr(VV_FOLDSTART);
4270 foldend = (linenr_T)get_vim_var_nr(VV_FOLDEND);
4271 dashes = get_vim_var_str(VV_FOLDDASHES);
4272 if (foldstart > 0 && foldend <= curbuf->b_ml.ml_line_count
4273 && dashes != NULL)
4274 {
4275 /* Find first non-empty line in the fold. */
Bram Moolenaar69aa0992016-07-17 22:33:53 +02004276 for (lnum = foldstart; lnum < foldend; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004277 if (!linewhite(lnum))
4278 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004279
4280 /* Find interesting text in this line. */
4281 s = skipwhite(ml_get(lnum));
4282 /* skip C comment-start */
4283 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
4284 {
4285 s = skipwhite(s + 2);
4286 if (*skipwhite(s) == NUL
4287 && lnum + 1 < (linenr_T)get_vim_var_nr(VV_FOLDEND))
4288 {
4289 s = skipwhite(ml_get(lnum + 1));
4290 if (*s == '*')
4291 s = skipwhite(s + 1);
4292 }
4293 }
Bram Moolenaaree695f72016-08-03 22:08:45 +02004294 count = (long)(foldend - foldstart + 1);
Bram Moolenaar1c465442017-03-12 20:10:05 +01004295 txt = NGETTEXT("+-%s%3ld line: ", "+-%s%3ld lines: ", count);
Bram Moolenaar964b3742019-05-24 18:54:09 +02004296 r = alloc(STRLEN(txt)
4297 + STRLEN(dashes) // for %s
4298 + 20 // for %3ld
4299 + STRLEN(s)); // concatenated
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004300 if (r != NULL)
4301 {
Bram Moolenaaree695f72016-08-03 22:08:45 +02004302 sprintf((char *)r, txt, dashes, count);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004303 len = (int)STRLEN(r);
4304 STRCAT(r, s);
4305 /* remove 'foldmarker' and 'commentstring' */
4306 foldtext_cleanup(r + len);
4307 rettv->vval.v_string = r;
4308 }
4309 }
4310#endif
4311}
4312
4313/*
4314 * "foldtextresult(lnum)" function
4315 */
4316 static void
4317f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
4318{
4319#ifdef FEAT_FOLDING
4320 linenr_T lnum;
4321 char_u *text;
Bram Moolenaaree695f72016-08-03 22:08:45 +02004322 char_u buf[FOLD_TEXT_LEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004323 foldinfo_T foldinfo;
4324 int fold_count;
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004325 static int entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004326#endif
4327
4328 rettv->v_type = VAR_STRING;
4329 rettv->vval.v_string = NULL;
4330#ifdef FEAT_FOLDING
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004331 if (entered)
4332 return; /* reject recursive use */
4333 entered = TRUE;
4334
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004335 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004336 /* treat illegal types and illegal string values for {lnum} the same */
4337 if (lnum < 0)
4338 lnum = 0;
4339 fold_count = foldedCount(curwin, lnum, &foldinfo);
4340 if (fold_count > 0)
4341 {
Bram Moolenaarc6aafba2017-03-21 17:09:10 +01004342 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
4343 &foldinfo, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004344 if (text == buf)
4345 text = vim_strsave(text);
4346 rettv->vval.v_string = text;
4347 }
Bram Moolenaar495b7dd2017-09-16 17:19:22 +02004348
4349 entered = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004350#endif
4351}
4352
4353/*
4354 * "foreground()" function
4355 */
4356 static void
4357f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4358{
4359#ifdef FEAT_GUI
4360 if (gui.in_use)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004361 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004362 gui_mch_set_foreground();
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004363 return;
4364 }
4365#endif
4366#if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004367 win32_set_foreground();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004368#endif
4369}
4370
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004371 static void
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004372common_function(typval_T *argvars, typval_T *rettv, int is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004373{
4374 char_u *s;
4375 char_u *name;
4376 int use_string = FALSE;
4377 partial_T *arg_pt = NULL;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004378 char_u *trans_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004379
4380 if (argvars[0].v_type == VAR_FUNC)
4381 {
4382 /* function(MyFunc, [arg], dict) */
4383 s = argvars[0].vval.v_string;
4384 }
4385 else if (argvars[0].v_type == VAR_PARTIAL
4386 && argvars[0].vval.v_partial != NULL)
4387 {
4388 /* function(dict.MyFunc, [arg]) */
4389 arg_pt = argvars[0].vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004390 s = partial_name(arg_pt);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004391 }
4392 else
4393 {
4394 /* function('MyFunc', [arg], dict) */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004395 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004396 use_string = TRUE;
4397 }
4398
Bram Moolenaar843b8842016-08-21 14:36:15 +02004399 if ((use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL) || is_funcref)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004400 {
4401 name = s;
4402 trans_name = trans_function_name(&name, FALSE,
4403 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
4404 if (*name != NUL)
4405 s = NULL;
4406 }
4407
Bram Moolenaar843b8842016-08-21 14:36:15 +02004408 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s))
4409 || (is_funcref && trans_name == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004410 semsg(_(e_invarg2), use_string ? tv_get_string(&argvars[0]) : s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004411 /* Don't check an autoload name for existence here. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004412 else if (trans_name != NULL && (is_funcref
4413 ? find_func(trans_name) == NULL
4414 : !translated_function_exists(trans_name)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004415 semsg(_("E700: Unknown function: %s"), s);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004416 else
4417 {
4418 int dict_idx = 0;
4419 int arg_idx = 0;
4420 list_T *list = NULL;
4421
4422 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
4423 {
4424 char sid_buf[25];
4425 int off = *s == 's' ? 2 : 5;
4426
4427 /* Expand s: and <SID> into <SNR>nr_, so that the function can
4428 * also be called from another script. Using trans_function_name()
4429 * would also work, but some plugins depend on the name being
4430 * printable text. */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02004431 sprintf(sid_buf, "<SNR>%ld_", (long)current_sctx.sc_sid);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004432 name = alloc(STRLEN(sid_buf) + STRLEN(s + off) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004433 if (name != NULL)
4434 {
4435 STRCPY(name, sid_buf);
4436 STRCAT(name, s + off);
4437 }
4438 }
4439 else
4440 name = vim_strsave(s);
4441
4442 if (argvars[1].v_type != VAR_UNKNOWN)
4443 {
4444 if (argvars[2].v_type != VAR_UNKNOWN)
4445 {
4446 /* function(name, [args], dict) */
4447 arg_idx = 1;
4448 dict_idx = 2;
4449 }
4450 else if (argvars[1].v_type == VAR_DICT)
4451 /* function(name, dict) */
4452 dict_idx = 1;
4453 else
4454 /* function(name, [args]) */
4455 arg_idx = 1;
4456 if (dict_idx > 0)
4457 {
4458 if (argvars[dict_idx].v_type != VAR_DICT)
4459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004460 emsg(_("E922: expected a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004461 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004462 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004463 }
4464 if (argvars[dict_idx].vval.v_dict == NULL)
4465 dict_idx = 0;
4466 }
4467 if (arg_idx > 0)
4468 {
4469 if (argvars[arg_idx].v_type != VAR_LIST)
4470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004471 emsg(_("E923: Second argument of function() must be a list or a dict"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004472 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004473 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004474 }
4475 list = argvars[arg_idx].vval.v_list;
4476 if (list == NULL || list->lv_len == 0)
4477 arg_idx = 0;
4478 }
4479 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004480 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL || is_funcref)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004481 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004482 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004483
4484 /* result is a VAR_PARTIAL */
4485 if (pt == NULL)
4486 vim_free(name);
4487 else
4488 {
4489 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
4490 {
4491 listitem_T *li;
4492 int i = 0;
4493 int arg_len = 0;
4494 int lv_len = 0;
4495
4496 if (arg_pt != NULL)
4497 arg_len = arg_pt->pt_argc;
4498 if (list != NULL)
4499 lv_len = list->lv_len;
4500 pt->pt_argc = arg_len + lv_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004501 pt->pt_argv = ALLOC_MULT(typval_T, pt->pt_argc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004502 if (pt->pt_argv == NULL)
4503 {
4504 vim_free(pt);
4505 vim_free(name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004506 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004507 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004508 for (i = 0; i < arg_len; i++)
4509 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
4510 if (lv_len > 0)
4511 for (li = list->lv_first; li != NULL;
4512 li = li->li_next)
4513 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004514 }
4515
4516 /* For "function(dict.func, [], dict)" and "func" is a partial
4517 * use "dict". That is backwards compatible. */
4518 if (dict_idx > 0)
4519 {
4520 /* The dict is bound explicitly, pt_auto is FALSE. */
4521 pt->pt_dict = argvars[dict_idx].vval.v_dict;
4522 ++pt->pt_dict->dv_refcount;
4523 }
4524 else if (arg_pt != NULL)
4525 {
4526 /* If the dict was bound automatically the result is also
4527 * bound automatically. */
4528 pt->pt_dict = arg_pt->pt_dict;
4529 pt->pt_auto = arg_pt->pt_auto;
4530 if (pt->pt_dict != NULL)
4531 ++pt->pt_dict->dv_refcount;
4532 }
4533
4534 pt->pt_refcount = 1;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004535 if (arg_pt != NULL && arg_pt->pt_func != NULL)
4536 {
4537 pt->pt_func = arg_pt->pt_func;
4538 func_ptr_ref(pt->pt_func);
4539 vim_free(name);
4540 }
4541 else if (is_funcref)
4542 {
4543 pt->pt_func = find_func(trans_name);
4544 func_ptr_ref(pt->pt_func);
4545 vim_free(name);
4546 }
4547 else
4548 {
4549 pt->pt_name = name;
4550 func_ref(name);
4551 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004552 }
4553 rettv->v_type = VAR_PARTIAL;
4554 rettv->vval.v_partial = pt;
4555 }
4556 else
4557 {
4558 /* result is a VAR_FUNC */
4559 rettv->v_type = VAR_FUNC;
4560 rettv->vval.v_string = name;
4561 func_ref(name);
4562 }
4563 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004564theend:
4565 vim_free(trans_name);
4566}
4567
4568/*
4569 * "funcref()" function
4570 */
4571 static void
4572f_funcref(typval_T *argvars, typval_T *rettv)
4573{
4574 common_function(argvars, rettv, TRUE);
4575}
4576
4577/*
4578 * "function()" function
4579 */
4580 static void
4581f_function(typval_T *argvars, typval_T *rettv)
4582{
4583 common_function(argvars, rettv, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004584}
4585
4586/*
4587 * "garbagecollect()" function
4588 */
4589 static void
4590f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
4591{
4592 /* This is postponed until we are back at the toplevel, because we may be
4593 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
4594 want_garbage_collect = TRUE;
4595
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004596 if (argvars[0].v_type != VAR_UNKNOWN && tv_get_number(&argvars[0]) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004597 garbage_collect_at_exit = TRUE;
4598}
4599
4600/*
4601 * "get()" function
4602 */
4603 static void
4604f_get(typval_T *argvars, typval_T *rettv)
4605{
4606 listitem_T *li;
4607 list_T *l;
4608 dictitem_T *di;
4609 dict_T *d;
4610 typval_T *tv = NULL;
4611
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004612 if (argvars[0].v_type == VAR_BLOB)
4613 {
4614 int error = FALSE;
4615 int idx = tv_get_number_chk(&argvars[1], &error);
4616
4617 if (!error)
4618 {
4619 rettv->v_type = VAR_NUMBER;
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004620 if (idx < 0)
4621 idx = blob_len(argvars[0].vval.v_blob) + idx;
4622 if (idx < 0 || idx >= blob_len(argvars[0].vval.v_blob))
4623 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004624 else
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004625 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004626 rettv->vval.v_number = blob_get(argvars[0].vval.v_blob, idx);
Bram Moolenaar2ea773b2019-01-15 22:16:42 +01004627 tv = rettv;
4628 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004629 }
4630 }
4631 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004632 {
4633 if ((l = argvars[0].vval.v_list) != NULL)
4634 {
4635 int error = FALSE;
4636
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004637 li = list_find(l, (long)tv_get_number_chk(&argvars[1], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004638 if (!error && li != NULL)
4639 tv = &li->li_tv;
4640 }
4641 }
4642 else if (argvars[0].v_type == VAR_DICT)
4643 {
4644 if ((d = argvars[0].vval.v_dict) != NULL)
4645 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004646 di = dict_find(d, tv_get_string(&argvars[1]), -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004647 if (di != NULL)
4648 tv = &di->di_tv;
4649 }
4650 }
4651 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
4652 {
4653 partial_T *pt;
4654 partial_T fref_pt;
4655
4656 if (argvars[0].v_type == VAR_PARTIAL)
4657 pt = argvars[0].vval.v_partial;
4658 else
4659 {
4660 vim_memset(&fref_pt, 0, sizeof(fref_pt));
4661 fref_pt.pt_name = argvars[0].vval.v_string;
4662 pt = &fref_pt;
4663 }
4664
4665 if (pt != NULL)
4666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004667 char_u *what = tv_get_string(&argvars[1]);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004668 char_u *n;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004669
4670 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
4671 {
4672 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004673 n = partial_name(pt);
4674 if (n == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004675 rettv->vval.v_string = NULL;
4676 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004677 {
4678 rettv->vval.v_string = vim_strsave(n);
4679 if (rettv->v_type == VAR_FUNC)
4680 func_ref(rettv->vval.v_string);
4681 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004682 }
4683 else if (STRCMP(what, "dict") == 0)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004684 rettv_dict_set(rettv, pt->pt_dict);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004685 else if (STRCMP(what, "args") == 0)
4686 {
4687 rettv->v_type = VAR_LIST;
4688 if (rettv_list_alloc(rettv) == OK)
4689 {
4690 int i;
4691
4692 for (i = 0; i < pt->pt_argc; ++i)
4693 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
4694 }
4695 }
4696 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004697 semsg(_(e_invarg2), what);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004698 return;
4699 }
4700 }
4701 else
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01004702 semsg(_(e_listdictblobarg), "get()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004703
4704 if (tv == NULL)
4705 {
4706 if (argvars[2].v_type != VAR_UNKNOWN)
4707 copy_tv(&argvars[2], rettv);
4708 }
4709 else
4710 copy_tv(tv, rettv);
4711}
4712
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004713/*
4714 * Returns buffer options, variables and other attributes in a dictionary.
4715 */
4716 static dict_T *
4717get_buffer_info(buf_T *buf)
4718{
4719 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004720 tabpage_T *tp;
4721 win_T *wp;
4722 list_T *windows;
4723
4724 dict = dict_alloc();
4725 if (dict == NULL)
4726 return NULL;
4727
Bram Moolenaare0be1672018-07-08 16:50:37 +02004728 dict_add_number(dict, "bufnr", buf->b_fnum);
4729 dict_add_string(dict, "name", buf->b_ffname);
4730 dict_add_number(dict, "lnum", buf == curbuf ? curwin->w_cursor.lnum
4731 : buflist_findlnum(buf));
4732 dict_add_number(dict, "loaded", buf->b_ml.ml_mfp != NULL);
4733 dict_add_number(dict, "listed", buf->b_p_bl);
4734 dict_add_number(dict, "changed", bufIsChanged(buf));
4735 dict_add_number(dict, "changedtick", CHANGEDTICK(buf));
4736 dict_add_number(dict, "hidden",
4737 buf->b_ml.ml_mfp != NULL && buf->b_nwindows == 0);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004738
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02004739 /* Get a reference to buffer variables */
4740 dict_add_dict(dict, "variables", buf->b_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004741
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004742 /* List of windows displaying this buffer */
4743 windows = list_alloc();
4744 if (windows != NULL)
4745 {
4746 FOR_ALL_TAB_WINDOWS(tp, wp)
4747 if (wp->w_buffer == buf)
4748 list_append_number(windows, (varnumber_T)wp->w_id);
4749 dict_add_list(dict, "windows", windows);
4750 }
4751
4752#ifdef FEAT_SIGNS
4753 if (buf->b_signlist != NULL)
4754 {
4755 /* List of signs placed in this buffer */
4756 list_T *signs = list_alloc();
4757 if (signs != NULL)
4758 {
4759 get_buffer_signs(buf, signs);
4760 dict_add_list(dict, "signs", signs);
4761 }
4762 }
4763#endif
4764
4765 return dict;
4766}
4767
4768/*
4769 * "getbufinfo()" function
4770 */
4771 static void
4772f_getbufinfo(typval_T *argvars, typval_T *rettv)
4773{
4774 buf_T *buf = NULL;
4775 buf_T *argbuf = NULL;
4776 dict_T *d;
4777 int filtered = FALSE;
4778 int sel_buflisted = FALSE;
4779 int sel_bufloaded = FALSE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004780 int sel_bufmodified = FALSE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004781
4782 if (rettv_list_alloc(rettv) != OK)
4783 return;
4784
4785 /* List of all the buffers or selected buffers */
4786 if (argvars[0].v_type == VAR_DICT)
4787 {
4788 dict_T *sel_d = argvars[0].vval.v_dict;
4789
4790 if (sel_d != NULL)
4791 {
4792 dictitem_T *di;
4793
4794 filtered = TRUE;
4795
4796 di = dict_find(sel_d, (char_u *)"buflisted", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004797 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004798 sel_buflisted = TRUE;
4799
4800 di = dict_find(sel_d, (char_u *)"bufloaded", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004801 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004802 sel_bufloaded = TRUE;
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004803
4804 di = dict_find(sel_d, (char_u *)"bufmodified", -1);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004805 if (di != NULL && tv_get_number(&di->di_tv))
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004806 sel_bufmodified = TRUE;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004807 }
4808 }
4809 else if (argvars[0].v_type != VAR_UNKNOWN)
4810 {
4811 /* Information about one buffer. Argument specifies the buffer */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004812 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004813 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004814 argbuf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004815 --emsg_off;
4816 if (argbuf == NULL)
4817 return;
4818 }
4819
4820 /* Return information about all the buffers or a specified buffer */
Bram Moolenaar386600f2016-08-15 22:16:25 +02004821 FOR_ALL_BUFFERS(buf)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004822 {
4823 if (argbuf != NULL && argbuf != buf)
4824 continue;
4825 if (filtered && ((sel_bufloaded && buf->b_ml.ml_mfp == NULL)
Bram Moolenaar8e6a31d2017-12-10 21:06:22 +01004826 || (sel_buflisted && !buf->b_p_bl)
4827 || (sel_bufmodified && !buf->b_changed)))
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02004828 continue;
4829
4830 d = get_buffer_info(buf);
4831 if (d != NULL)
4832 list_append_dict(rettv->vval.v_list, d);
4833 if (argbuf != NULL)
4834 return;
4835 }
4836}
4837
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004838/*
4839 * Get line or list of lines from buffer "buf" into "rettv".
4840 * Return a range (from start to end) of lines in rettv from the specified
4841 * buffer.
4842 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
4843 */
4844 static void
4845get_buffer_lines(
4846 buf_T *buf,
4847 linenr_T start,
4848 linenr_T end,
4849 int retlist,
4850 typval_T *rettv)
4851{
4852 char_u *p;
4853
4854 rettv->v_type = VAR_STRING;
4855 rettv->vval.v_string = NULL;
4856 if (retlist && rettv_list_alloc(rettv) == FAIL)
4857 return;
4858
4859 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
4860 return;
4861
4862 if (!retlist)
4863 {
4864 if (start >= 1 && start <= buf->b_ml.ml_line_count)
4865 p = ml_get_buf(buf, start, FALSE);
4866 else
4867 p = (char_u *)"";
4868 rettv->vval.v_string = vim_strsave(p);
4869 }
4870 else
4871 {
4872 if (end < start)
4873 return;
4874
4875 if (start < 1)
4876 start = 1;
4877 if (end > buf->b_ml.ml_line_count)
4878 end = buf->b_ml.ml_line_count;
4879 while (start <= end)
4880 if (list_append_string(rettv->vval.v_list,
4881 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
4882 break;
4883 }
4884}
4885
4886/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004887 * "getbufline()" function
4888 */
4889 static void
4890f_getbufline(typval_T *argvars, typval_T *rettv)
4891{
4892 linenr_T lnum;
4893 linenr_T end;
4894 buf_T *buf;
4895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004896 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004897 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004898 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004899 --emsg_off;
4900
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004901 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004902 if (argvars[2].v_type == VAR_UNKNOWN)
4903 end = lnum;
4904 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004905 end = tv_get_lnum_buf(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004906
4907 get_buffer_lines(buf, lnum, end, TRUE, rettv);
4908}
4909
4910/*
4911 * "getbufvar()" function
4912 */
4913 static void
4914f_getbufvar(typval_T *argvars, typval_T *rettv)
4915{
4916 buf_T *buf;
4917 buf_T *save_curbuf;
4918 char_u *varname;
4919 dictitem_T *v;
4920 int done = FALSE;
4921
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004922 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
4923 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004924 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004925 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004926
4927 rettv->v_type = VAR_STRING;
4928 rettv->vval.v_string = NULL;
4929
4930 if (buf != NULL && varname != NULL)
4931 {
4932 /* set curbuf to be our buf, temporarily */
4933 save_curbuf = curbuf;
4934 curbuf = buf;
4935
Bram Moolenaar30567352016-08-27 21:25:44 +02004936 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004937 {
Bram Moolenaar30567352016-08-27 21:25:44 +02004938 if (varname[1] == NUL)
4939 {
4940 /* get all buffer-local options in a dict */
4941 dict_T *opts = get_winbuf_options(TRUE);
4942
4943 if (opts != NULL)
4944 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004945 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02004946 done = TRUE;
4947 }
4948 }
4949 else if (get_option_tv(&varname, rettv, TRUE) == OK)
4950 /* buffer-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004951 done = TRUE;
4952 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004953 else
4954 {
4955 /* Look up the variable. */
4956 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
4957 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
4958 'b', varname, FALSE);
4959 if (v != NULL)
4960 {
4961 copy_tv(&v->di_tv, rettv);
4962 done = TRUE;
4963 }
4964 }
4965
4966 /* restore previous notion of curbuf */
4967 curbuf = save_curbuf;
4968 }
4969
4970 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4971 /* use the default value */
4972 copy_tv(&argvars[2], rettv);
4973
4974 --emsg_off;
4975}
4976
4977/*
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004978 * "getchangelist()" function
4979 */
4980 static void
4981f_getchangelist(typval_T *argvars, typval_T *rettv)
4982{
4983#ifdef FEAT_JUMPLIST
4984 buf_T *buf;
4985 int i;
4986 list_T *l;
4987 dict_T *d;
4988#endif
4989
4990 if (rettv_list_alloc(rettv) != OK)
4991 return;
4992
4993#ifdef FEAT_JUMPLIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004994 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004995 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004996 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar341a64c2018-02-13 19:21:17 +01004997 --emsg_off;
Bram Moolenaar07ad8162018-02-13 13:59:59 +01004998 if (buf == NULL)
4999 return;
5000
5001 l = list_alloc();
5002 if (l == NULL)
5003 return;
5004
5005 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5006 return;
5007 /*
5008 * The current window change list index tracks only the position in the
5009 * current buffer change list. For other buffers, use the change list
5010 * length as the current index.
5011 */
5012 list_append_number(rettv->vval.v_list,
5013 (varnumber_T)((buf == curwin->w_buffer)
5014 ? curwin->w_changelistidx : buf->b_changelistlen));
5015
5016 for (i = 0; i < buf->b_changelistlen; ++i)
5017 {
5018 if (buf->b_changelist[i].lnum == 0)
5019 continue;
5020 if ((d = dict_alloc()) == NULL)
5021 return;
5022 if (list_append_dict(l, d) == FAIL)
5023 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005024 dict_add_number(d, "lnum", (long)buf->b_changelist[i].lnum);
5025 dict_add_number(d, "col", (long)buf->b_changelist[i].col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005026 dict_add_number(d, "coladd", (long)buf->b_changelist[i].coladd);
Bram Moolenaar07ad8162018-02-13 13:59:59 +01005027 }
5028#endif
5029}
5030/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005031 * "getchar()" function
5032 */
5033 static void
5034f_getchar(typval_T *argvars, typval_T *rettv)
5035{
5036 varnumber_T n;
5037 int error = FALSE;
5038
Bram Moolenaar84d93902018-09-11 20:10:20 +02005039#ifdef MESSAGE_QUEUE
5040 // vpeekc() used to check for messages, but that caused problems, invoking
5041 // a callback where it was not expected. Some plugins use getchar(1) in a
5042 // loop to await a message, therefore make sure we check for messages here.
5043 parse_queued_messages();
5044#endif
5045
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005046 /* Position the cursor. Needed after a message that ends in a space. */
5047 windgoto(msg_row, msg_col);
5048
5049 ++no_mapping;
5050 ++allow_keys;
5051 for (;;)
5052 {
5053 if (argvars[0].v_type == VAR_UNKNOWN)
5054 /* getchar(): blocking wait. */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005055 n = plain_vgetc();
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005056 else if (tv_get_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005057 /* getchar(1): only check if char avail */
5058 n = vpeekc_any();
5059 else if (error || vpeekc_any() == NUL)
5060 /* illegal argument or getchar(0) and no char avail: return zero */
5061 n = 0;
5062 else
5063 /* getchar(0) and char avail: return char */
Bram Moolenaarec2da362017-01-21 20:04:22 +01005064 n = plain_vgetc();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005065
5066 if (n == K_IGNORE)
5067 continue;
5068 break;
5069 }
5070 --no_mapping;
5071 --allow_keys;
5072
5073 set_vim_var_nr(VV_MOUSE_WIN, 0);
5074 set_vim_var_nr(VV_MOUSE_WINID, 0);
5075 set_vim_var_nr(VV_MOUSE_LNUM, 0);
5076 set_vim_var_nr(VV_MOUSE_COL, 0);
5077
5078 rettv->vval.v_number = n;
5079 if (IS_SPECIAL(n) || mod_mask != 0)
5080 {
5081 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
5082 int i = 0;
5083
5084 /* Turn a special key into three bytes, plus modifier. */
5085 if (mod_mask != 0)
5086 {
5087 temp[i++] = K_SPECIAL;
5088 temp[i++] = KS_MODIFIER;
5089 temp[i++] = mod_mask;
5090 }
5091 if (IS_SPECIAL(n))
5092 {
5093 temp[i++] = K_SPECIAL;
5094 temp[i++] = K_SECOND(n);
5095 temp[i++] = K_THIRD(n);
5096 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005097 else if (has_mbyte)
5098 i += (*mb_char2bytes)(n, temp + i);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005099 else
5100 temp[i++] = n;
5101 temp[i++] = NUL;
5102 rettv->v_type = VAR_STRING;
5103 rettv->vval.v_string = vim_strsave(temp);
5104
5105#ifdef FEAT_MOUSE
5106 if (is_mouse_key(n))
5107 {
5108 int row = mouse_row;
5109 int col = mouse_col;
5110 win_T *win;
5111 linenr_T lnum;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005112 win_T *wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005113 int winnr = 1;
5114
5115 if (row >= 0 && col >= 0)
5116 {
5117 /* Find the window at the mouse coordinates and compute the
5118 * text position. */
5119 win = mouse_find_win(&row, &col);
Bram Moolenaar989a70c2017-08-16 22:46:01 +02005120 if (win == NULL)
5121 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005122 (void)mouse_comp_pos(win, &row, &col, &lnum);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005123 for (wp = firstwin; wp != win; wp = wp->w_next)
5124 ++winnr;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005125 set_vim_var_nr(VV_MOUSE_WIN, winnr);
5126 set_vim_var_nr(VV_MOUSE_WINID, win->w_id);
5127 set_vim_var_nr(VV_MOUSE_LNUM, lnum);
5128 set_vim_var_nr(VV_MOUSE_COL, col + 1);
5129 }
5130 }
5131#endif
5132 }
5133}
5134
5135/*
5136 * "getcharmod()" function
5137 */
5138 static void
5139f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
5140{
5141 rettv->vval.v_number = mod_mask;
5142}
5143
5144/*
5145 * "getcharsearch()" function
5146 */
5147 static void
5148f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
5149{
5150 if (rettv_dict_alloc(rettv) != FAIL)
5151 {
5152 dict_T *dict = rettv->vval.v_dict;
5153
Bram Moolenaare0be1672018-07-08 16:50:37 +02005154 dict_add_string(dict, "char", last_csearch());
5155 dict_add_number(dict, "forward", last_csearch_forward());
5156 dict_add_number(dict, "until", last_csearch_until());
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005157 }
5158}
5159
5160/*
5161 * "getcmdline()" function
5162 */
5163 static void
5164f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
5165{
5166 rettv->v_type = VAR_STRING;
5167 rettv->vval.v_string = get_cmdline_str();
5168}
5169
5170/*
5171 * "getcmdpos()" function
5172 */
5173 static void
5174f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
5175{
5176 rettv->vval.v_number = get_cmdline_pos() + 1;
5177}
5178
5179/*
5180 * "getcmdtype()" function
5181 */
5182 static void
5183f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
5184{
5185 rettv->v_type = VAR_STRING;
5186 rettv->vval.v_string = alloc(2);
5187 if (rettv->vval.v_string != NULL)
5188 {
5189 rettv->vval.v_string[0] = get_cmdline_type();
5190 rettv->vval.v_string[1] = NUL;
5191 }
5192}
5193
5194/*
5195 * "getcmdwintype()" function
5196 */
5197 static void
5198f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
5199{
5200 rettv->v_type = VAR_STRING;
5201 rettv->vval.v_string = NULL;
5202#ifdef FEAT_CMDWIN
5203 rettv->vval.v_string = alloc(2);
5204 if (rettv->vval.v_string != NULL)
5205 {
5206 rettv->vval.v_string[0] = cmdwin_type;
5207 rettv->vval.v_string[1] = NUL;
5208 }
5209#endif
5210}
5211
5212#if defined(FEAT_CMDL_COMPL)
5213/*
5214 * "getcompletion()" function
5215 */
5216 static void
5217f_getcompletion(typval_T *argvars, typval_T *rettv)
5218{
5219 char_u *pat;
5220 expand_T xpc;
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005221 int filtered = FALSE;
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005222 int options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
5223 | WILD_NO_BEEP;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005224
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005225 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005226 filtered = tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005227
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005228 if (p_wic)
5229 options |= WILD_ICASE;
5230
Bram Moolenaare9d58a62016-08-13 15:07:41 +02005231 /* For filtered results, 'wildignore' is used */
5232 if (!filtered)
5233 options |= WILD_KEEP_ALL;
5234
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005235 ExpandInit(&xpc);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005236 xpc.xp_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005237 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005238 xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005239 if (xpc.xp_context == EXPAND_NOTHING)
5240 {
5241 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005242 semsg(_(e_invarg2), argvars[1].vval.v_string);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005243 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005244 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005245 return;
5246 }
5247
5248# if defined(FEAT_MENU)
5249 if (xpc.xp_context == EXPAND_MENUS)
5250 {
5251 set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
5252 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5253 }
5254# endif
Bram Moolenaarb650b982016-08-05 20:35:13 +02005255#ifdef FEAT_CSCOPE
5256 if (xpc.xp_context == EXPAND_CSCOPE)
5257 {
5258 set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
5259 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5260 }
5261#endif
Bram Moolenaar7522f692016-08-06 14:12:50 +02005262#ifdef FEAT_SIGNS
5263 if (xpc.xp_context == EXPAND_SIGN)
5264 {
5265 set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
5266 xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
5267 }
5268#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005269
5270 pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
5271 if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
5272 {
Bram Moolenaarb56195e2016-07-28 22:53:37 +02005273 int i;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005274
5275 ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
5276
5277 for (i = 0; i < xpc.xp_numfiles; i++)
5278 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
5279 }
5280 vim_free(pat);
5281 ExpandCleanup(&xpc);
5282}
5283#endif
5284
5285/*
5286 * "getcwd()" function
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005287 *
5288 * Return the current working directory of a window in a tab page.
5289 * First optional argument 'winnr' is the window number or -1 and the second
5290 * optional argument 'tabnr' is the tab page number.
5291 *
5292 * If no arguments are supplied, then return the directory of the current
5293 * window.
5294 * If only 'winnr' is specified and is not -1 or 0 then return the directory of
5295 * the specified window.
5296 * If 'winnr' is 0 then return the directory of the current window.
5297 * If both 'winnr and 'tabnr' are specified and 'winnr' is -1 then return the
5298 * directory of the specified tab page. Otherwise return the directory of the
5299 * specified window in the specified tab page.
5300 * If the window or the tab page doesn't exist then return NULL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005301 */
5302 static void
5303f_getcwd(typval_T *argvars, typval_T *rettv)
5304{
5305 win_T *wp = NULL;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005306 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005307 char_u *cwd;
Bram Moolenaar54591292018-02-09 20:53:59 +01005308 int global = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005309
5310 rettv->v_type = VAR_STRING;
5311 rettv->vval.v_string = NULL;
5312
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005313 if (argvars[0].v_type == VAR_NUMBER
5314 && argvars[0].vval.v_number == -1
5315 && argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar54591292018-02-09 20:53:59 +01005316 global = TRUE;
5317 else
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005318 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
Bram Moolenaar54591292018-02-09 20:53:59 +01005319
5320 if (wp != NULL && wp->w_localdir != NULL)
5321 rettv->vval.v_string = vim_strsave(wp->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005322 else if (tp != NULL && tp->tp_localdir != NULL)
5323 rettv->vval.v_string = vim_strsave(tp->tp_localdir);
5324 else if (wp != NULL || tp != NULL || global)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005325 {
Bram Moolenaar54591292018-02-09 20:53:59 +01005326 if (globaldir != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005327 rettv->vval.v_string = vim_strsave(globaldir);
5328 else
5329 {
5330 cwd = alloc(MAXPATHL);
5331 if (cwd != NULL)
5332 {
5333 if (mch_dirname(cwd, MAXPATHL) != FAIL)
5334 rettv->vval.v_string = vim_strsave(cwd);
5335 vim_free(cwd);
5336 }
5337 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005338 }
Bram Moolenaar3c5b8cd2018-09-02 14:25:05 +02005339#ifdef BACKSLASH_IN_FILENAME
5340 if (rettv->vval.v_string != NULL)
5341 slash_adjust(rettv->vval.v_string);
5342#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005343}
5344
5345/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +02005346 * "getenv()" function
5347 */
5348 static void
5349f_getenv(typval_T *argvars, typval_T *rettv)
5350{
5351 int mustfree = FALSE;
5352 char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5353
5354 if (p == NULL)
5355 {
5356 rettv->v_type = VAR_SPECIAL;
5357 rettv->vval.v_number = VVAL_NULL;
5358 return;
5359 }
5360 if (!mustfree)
5361 p = vim_strsave(p);
5362 rettv->vval.v_string = p;
5363 rettv->v_type = VAR_STRING;
5364}
5365
5366/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005367 * "getfontname()" function
5368 */
5369 static void
5370f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
5371{
5372 rettv->v_type = VAR_STRING;
5373 rettv->vval.v_string = NULL;
5374#ifdef FEAT_GUI
5375 if (gui.in_use)
5376 {
5377 GuiFont font;
5378 char_u *name = NULL;
5379
5380 if (argvars[0].v_type == VAR_UNKNOWN)
5381 {
5382 /* Get the "Normal" font. Either the name saved by
5383 * hl_set_font_name() or from the font ID. */
5384 font = gui.norm_font;
5385 name = hl_get_font_name();
5386 }
5387 else
5388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005389 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005390 if (STRCMP(name, "*") == 0) /* don't use font dialog */
5391 return;
5392 font = gui_mch_get_font(name, FALSE);
5393 if (font == NOFONT)
5394 return; /* Invalid font name, return empty string. */
5395 }
5396 rettv->vval.v_string = gui_mch_get_fontname(font, name);
5397 if (argvars[0].v_type != VAR_UNKNOWN)
5398 gui_mch_free_font(font);
5399 }
5400#endif
5401}
5402
5403/*
5404 * "getfperm({fname})" function
5405 */
5406 static void
5407f_getfperm(typval_T *argvars, typval_T *rettv)
5408{
5409 char_u *fname;
5410 stat_T st;
5411 char_u *perm = NULL;
5412 char_u flags[] = "rwx";
5413 int i;
5414
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005415 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005416
5417 rettv->v_type = VAR_STRING;
5418 if (mch_stat((char *)fname, &st) >= 0)
5419 {
5420 perm = vim_strsave((char_u *)"---------");
5421 if (perm != NULL)
5422 {
5423 for (i = 0; i < 9; i++)
5424 {
5425 if (st.st_mode & (1 << (8 - i)))
5426 perm[i] = flags[i % 3];
5427 }
5428 }
5429 }
5430 rettv->vval.v_string = perm;
5431}
5432
5433/*
5434 * "getfsize({fname})" function
5435 */
5436 static void
5437f_getfsize(typval_T *argvars, typval_T *rettv)
5438{
5439 char_u *fname;
5440 stat_T st;
5441
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005442 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005443
5444 rettv->v_type = VAR_NUMBER;
5445
5446 if (mch_stat((char *)fname, &st) >= 0)
5447 {
5448 if (mch_isdir(fname))
5449 rettv->vval.v_number = 0;
5450 else
5451 {
5452 rettv->vval.v_number = (varnumber_T)st.st_size;
5453
5454 /* non-perfect check for overflow */
5455 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
5456 rettv->vval.v_number = -2;
5457 }
5458 }
5459 else
5460 rettv->vval.v_number = -1;
5461}
5462
5463/*
5464 * "getftime({fname})" function
5465 */
5466 static void
5467f_getftime(typval_T *argvars, typval_T *rettv)
5468{
5469 char_u *fname;
5470 stat_T st;
5471
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005472 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005473
5474 if (mch_stat((char *)fname, &st) >= 0)
5475 rettv->vval.v_number = (varnumber_T)st.st_mtime;
5476 else
5477 rettv->vval.v_number = -1;
5478}
5479
5480/*
5481 * "getftype({fname})" function
5482 */
5483 static void
5484f_getftype(typval_T *argvars, typval_T *rettv)
5485{
5486 char_u *fname;
5487 stat_T st;
5488 char_u *type = NULL;
5489 char *t;
5490
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005491 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005492
5493 rettv->v_type = VAR_STRING;
5494 if (mch_lstat((char *)fname, &st) >= 0)
5495 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005496 if (S_ISREG(st.st_mode))
5497 t = "file";
5498 else if (S_ISDIR(st.st_mode))
5499 t = "dir";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005500 else if (S_ISLNK(st.st_mode))
5501 t = "link";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005502 else if (S_ISBLK(st.st_mode))
5503 t = "bdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005504 else if (S_ISCHR(st.st_mode))
5505 t = "cdev";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005506 else if (S_ISFIFO(st.st_mode))
5507 t = "fifo";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005508 else if (S_ISSOCK(st.st_mode))
Bram Moolenaar1598f992018-08-09 22:08:57 +02005509 t = "socket";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005510 else
5511 t = "other";
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 type = vim_strsave((char_u *)t);
5513 }
5514 rettv->vval.v_string = type;
5515}
5516
5517/*
Bram Moolenaar4f505882018-02-10 21:06:32 +01005518 * "getjumplist()" function
5519 */
5520 static void
5521f_getjumplist(typval_T *argvars, typval_T *rettv)
5522{
5523#ifdef FEAT_JUMPLIST
5524 win_T *wp;
5525 int i;
5526 list_T *l;
5527 dict_T *d;
5528#endif
5529
5530 if (rettv_list_alloc(rettv) != OK)
5531 return;
5532
5533#ifdef FEAT_JUMPLIST
Bram Moolenaar00aa0692019-04-27 20:37:57 +02005534 wp = find_tabwin(&argvars[0], &argvars[1], NULL);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005535 if (wp == NULL)
5536 return;
5537
Bram Moolenaar57ee2b62019-02-12 22:15:06 +01005538 cleanup_jumplist(wp, TRUE);
5539
Bram Moolenaar4f505882018-02-10 21:06:32 +01005540 l = list_alloc();
5541 if (l == NULL)
5542 return;
5543
5544 if (list_append_list(rettv->vval.v_list, l) == FAIL)
5545 return;
5546 list_append_number(rettv->vval.v_list, (varnumber_T)wp->w_jumplistidx);
5547
5548 for (i = 0; i < wp->w_jumplistlen; ++i)
5549 {
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005550 if (wp->w_jumplist[i].fmark.mark.lnum == 0)
5551 continue;
Bram Moolenaar4f505882018-02-10 21:06:32 +01005552 if ((d = dict_alloc()) == NULL)
5553 return;
5554 if (list_append_dict(l, d) == FAIL)
5555 return;
Bram Moolenaare0be1672018-07-08 16:50:37 +02005556 dict_add_number(d, "lnum", (long)wp->w_jumplist[i].fmark.mark.lnum);
5557 dict_add_number(d, "col", (long)wp->w_jumplist[i].fmark.mark.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005558 dict_add_number(d, "coladd", (long)wp->w_jumplist[i].fmark.mark.coladd);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005559 dict_add_number(d, "bufnr", (long)wp->w_jumplist[i].fmark.fnum);
Bram Moolenaara7e18d22018-02-11 14:29:49 +01005560 if (wp->w_jumplist[i].fname != NULL)
Bram Moolenaare0be1672018-07-08 16:50:37 +02005561 dict_add_string(d, "filename", wp->w_jumplist[i].fname);
Bram Moolenaar4f505882018-02-10 21:06:32 +01005562 }
5563#endif
5564}
5565
5566/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005567 * "getline(lnum, [end])" function
5568 */
5569 static void
5570f_getline(typval_T *argvars, typval_T *rettv)
5571{
5572 linenr_T lnum;
5573 linenr_T end;
5574 int retlist;
5575
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005576 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005577 if (argvars[1].v_type == VAR_UNKNOWN)
5578 {
5579 end = 0;
5580 retlist = FALSE;
5581 }
5582 else
5583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005584 end = tv_get_lnum(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005585 retlist = TRUE;
5586 }
5587
5588 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
5589}
5590
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005591#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005592 static void
5593get_qf_loc_list(int is_qf, win_T *wp, typval_T *what_arg, typval_T *rettv)
5594{
Bram Moolenaard823fa92016-08-12 16:29:27 +02005595 if (what_arg->v_type == VAR_UNKNOWN)
5596 {
5597 if (rettv_list_alloc(rettv) == OK)
5598 if (is_qf || wp != NULL)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02005599 (void)get_errorlist(NULL, wp, -1, rettv->vval.v_list);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005600 }
5601 else
5602 {
5603 if (rettv_dict_alloc(rettv) == OK)
5604 if (is_qf || (wp != NULL))
5605 {
5606 if (what_arg->v_type == VAR_DICT)
5607 {
5608 dict_T *d = what_arg->vval.v_dict;
5609
5610 if (d != NULL)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02005611 qf_get_properties(wp, d, rettv->vval.v_dict);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005612 }
5613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +02005615 }
5616 }
Bram Moolenaard823fa92016-08-12 16:29:27 +02005617}
Bram Moolenaar4ae20952016-08-13 15:29:14 +02005618#endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02005619
5620/*
5621 * "getloclist()" function
5622 */
5623 static void
5624f_getloclist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5625{
5626#ifdef FEAT_QUICKFIX
5627 win_T *wp;
5628
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02005629 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaard823fa92016-08-12 16:29:27 +02005630 get_qf_loc_list(FALSE, wp, &argvars[1], rettv);
5631#endif
5632}
5633
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005634/*
5635 * "getmatches()" function
5636 */
5637 static void
5638f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5639{
5640#ifdef FEAT_SEARCH_EXTRA
5641 dict_T *dict;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005642 matchitem_T *cur;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005643 int i;
Bram Moolenaaraff74912019-03-30 18:11:49 +01005644 win_T *win = get_optional_window(argvars, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005645
Bram Moolenaaraff74912019-03-30 18:11:49 +01005646 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
5647 return;
5648
5649 cur = win->w_match_head;
5650 while (cur != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005651 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005652 dict = dict_alloc();
5653 if (dict == NULL)
5654 return;
5655 if (cur->match.regprog == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005656 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005657 /* match added with matchaddpos() */
5658 for (i = 0; i < MAXPOSMATCH; ++i)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005659 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005660 llpos_T *llpos;
Bram Moolenaar54315892019-04-26 22:33:49 +02005661 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaaraff74912019-03-30 18:11:49 +01005662 list_T *l;
5663
5664 llpos = &cur->pos.pos[i];
5665 if (llpos->lnum == 0)
5666 break;
5667 l = list_alloc();
5668 if (l == NULL)
5669 break;
5670 list_append_number(l, (varnumber_T)llpos->lnum);
5671 if (llpos->col > 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005672 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01005673 list_append_number(l, (varnumber_T)llpos->col);
5674 list_append_number(l, (varnumber_T)llpos->len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005675 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005676 sprintf(buf, "pos%d", i + 1);
5677 dict_add_list(dict, buf, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005678 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005679 }
Bram Moolenaaraff74912019-03-30 18:11:49 +01005680 else
5681 {
5682 dict_add_string(dict, "pattern", cur->pattern);
5683 }
5684 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
5685 dict_add_number(dict, "priority", (long)cur->priority);
5686 dict_add_number(dict, "id", (long)cur->id);
5687# if defined(FEAT_CONCEAL)
5688 if (cur->conceal_char)
5689 {
5690 char_u buf[MB_MAXBYTES + 1];
5691
5692 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
5693 dict_add_string(dict, "conceal", (char_u *)&buf);
5694 }
5695# endif
5696 list_append_dict(rettv->vval.v_list, dict);
5697 cur = cur->next;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005698 }
5699#endif
5700}
5701
5702/*
5703 * "getpid()" function
5704 */
5705 static void
5706f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
5707{
5708 rettv->vval.v_number = mch_get_pid();
5709}
5710
5711 static void
5712getpos_both(
5713 typval_T *argvars,
5714 typval_T *rettv,
5715 int getcurpos)
5716{
5717 pos_T *fp;
5718 list_T *l;
5719 int fnum = -1;
5720
5721 if (rettv_list_alloc(rettv) == OK)
5722 {
5723 l = rettv->vval.v_list;
5724 if (getcurpos)
5725 fp = &curwin->w_cursor;
5726 else
5727 fp = var2fpos(&argvars[0], TRUE, &fnum);
5728 if (fnum != -1)
5729 list_append_number(l, (varnumber_T)fnum);
5730 else
5731 list_append_number(l, (varnumber_T)0);
5732 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
5733 : (varnumber_T)0);
5734 list_append_number(l, (fp != NULL)
5735 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
5736 : (varnumber_T)0);
Bram Moolenaar29ddebe2019-01-26 17:28:26 +01005737 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->coladd :
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005738 (varnumber_T)0);
5739 if (getcurpos)
5740 {
Bram Moolenaar19a66852019-03-07 11:25:32 +01005741 int save_set_curswant = curwin->w_set_curswant;
5742 colnr_T save_curswant = curwin->w_curswant;
5743 colnr_T save_virtcol = curwin->w_virtcol;
5744
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005745 update_curswant();
5746 list_append_number(l, curwin->w_curswant == MAXCOL ?
5747 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar19a66852019-03-07 11:25:32 +01005748
5749 // Do not change "curswant", as it is unexpected that a get
5750 // function has a side effect.
5751 if (save_set_curswant)
5752 {
5753 curwin->w_set_curswant = save_set_curswant;
5754 curwin->w_curswant = save_curswant;
5755 curwin->w_virtcol = save_virtcol;
5756 curwin->w_valid &= ~VALID_VIRTCOL;
5757 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005758 }
5759 }
5760 else
5761 rettv->vval.v_number = FALSE;
5762}
5763
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005764/*
5765 * "getcurpos()" function
5766 */
5767 static void
5768f_getcurpos(typval_T *argvars, typval_T *rettv)
5769{
5770 getpos_both(argvars, rettv, TRUE);
5771}
5772
5773/*
5774 * "getpos(string)" function
5775 */
5776 static void
5777f_getpos(typval_T *argvars, typval_T *rettv)
5778{
5779 getpos_both(argvars, rettv, FALSE);
5780}
5781
5782/*
Bram Moolenaard823fa92016-08-12 16:29:27 +02005783 * "getqflist()" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005784 */
5785 static void
5786f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5787{
5788#ifdef FEAT_QUICKFIX
Bram Moolenaard823fa92016-08-12 16:29:27 +02005789 get_qf_loc_list(TRUE, NULL, &argvars[0], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005790#endif
5791}
5792
5793/*
5794 * "getreg()" function
5795 */
5796 static void
5797f_getreg(typval_T *argvars, typval_T *rettv)
5798{
5799 char_u *strregname;
5800 int regname;
5801 int arg2 = FALSE;
5802 int return_list = FALSE;
5803 int error = FALSE;
5804
5805 if (argvars[0].v_type != VAR_UNKNOWN)
5806 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005807 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005808 error = strregname == NULL;
5809 if (argvars[1].v_type != VAR_UNKNOWN)
5810 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005811 arg2 = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005812 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005813 return_list = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005814 }
5815 }
5816 else
5817 strregname = get_vim_var_str(VV_REG);
5818
5819 if (error)
5820 return;
5821
5822 regname = (strregname == NULL ? '"' : *strregname);
5823 if (regname == 0)
5824 regname = '"';
5825
5826 if (return_list)
5827 {
5828 rettv->v_type = VAR_LIST;
5829 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
5830 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
5831 if (rettv->vval.v_list == NULL)
5832 (void)rettv_list_alloc(rettv);
5833 else
5834 ++rettv->vval.v_list->lv_refcount;
5835 }
5836 else
5837 {
5838 rettv->v_type = VAR_STRING;
5839 rettv->vval.v_string = get_reg_contents(regname,
5840 arg2 ? GREG_EXPR_SRC : 0);
5841 }
5842}
5843
5844/*
5845 * "getregtype()" function
5846 */
5847 static void
5848f_getregtype(typval_T *argvars, typval_T *rettv)
5849{
5850 char_u *strregname;
5851 int regname;
5852 char_u buf[NUMBUFLEN + 2];
5853 long reglen = 0;
5854
5855 if (argvars[0].v_type != VAR_UNKNOWN)
5856 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005857 strregname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005858 if (strregname == NULL) /* type error; errmsg already given */
5859 {
5860 rettv->v_type = VAR_STRING;
5861 rettv->vval.v_string = NULL;
5862 return;
5863 }
5864 }
5865 else
5866 /* Default to v:register */
5867 strregname = get_vim_var_str(VV_REG);
5868
5869 regname = (strregname == NULL ? '"' : *strregname);
5870 if (regname == 0)
5871 regname = '"';
5872
5873 buf[0] = NUL;
5874 buf[1] = NUL;
5875 switch (get_reg_type(regname, &reglen))
5876 {
5877 case MLINE: buf[0] = 'V'; break;
5878 case MCHAR: buf[0] = 'v'; break;
5879 case MBLOCK:
5880 buf[0] = Ctrl_V;
5881 sprintf((char *)buf + 1, "%ld", reglen + 1);
5882 break;
5883 }
5884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = vim_strsave(buf);
5886}
5887
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005888/*
5889 * Returns information (variables, options, etc.) about a tab page
5890 * as a dictionary.
5891 */
5892 static dict_T *
5893get_tabpage_info(tabpage_T *tp, int tp_idx)
5894{
5895 win_T *wp;
5896 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005897 list_T *l;
5898
5899 dict = dict_alloc();
5900 if (dict == NULL)
5901 return NULL;
5902
Bram Moolenaare0be1672018-07-08 16:50:37 +02005903 dict_add_number(dict, "tabnr", tp_idx);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005904
5905 l = list_alloc();
5906 if (l != NULL)
5907 {
5908 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
5909 wp; wp = wp->w_next)
5910 list_append_number(l, (varnumber_T)wp->w_id);
5911 dict_add_list(dict, "windows", l);
5912 }
5913
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02005914 /* Make a reference to tabpage variables */
5915 dict_add_dict(dict, "variables", tp->tp_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005916
5917 return dict;
5918}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005919
5920/*
5921 * "gettabinfo()" function
5922 */
5923 static void
5924f_gettabinfo(typval_T *argvars, typval_T *rettv)
5925{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005926 tabpage_T *tp, *tparg = NULL;
5927 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02005928 int tpnr = 0;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005929
5930 if (rettv_list_alloc(rettv) != OK)
5931 return;
5932
5933 if (argvars[0].v_type != VAR_UNKNOWN)
5934 {
5935 /* Information about one tab page */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005936 tparg = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005937 if (tparg == NULL)
5938 return;
5939 }
5940
5941 /* Get information about a specific tab page or all tab pages */
Bram Moolenaar386600f2016-08-15 22:16:25 +02005942 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005943 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02005944 tpnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005945 if (tparg != NULL && tp != tparg)
5946 continue;
5947 d = get_tabpage_info(tp, tpnr);
5948 if (d != NULL)
5949 list_append_dict(rettv->vval.v_list, d);
5950 if (tparg != NULL)
5951 return;
5952 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02005953}
5954
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005955/*
5956 * "gettabvar()" function
5957 */
5958 static void
5959f_gettabvar(typval_T *argvars, typval_T *rettv)
5960{
5961 win_T *oldcurwin;
5962 tabpage_T *tp, *oldtabpage;
5963 dictitem_T *v;
5964 char_u *varname;
5965 int done = FALSE;
5966
5967 rettv->v_type = VAR_STRING;
5968 rettv->vval.v_string = NULL;
5969
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005970 varname = tv_get_string_chk(&argvars[1]);
5971 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005972 if (tp != NULL && varname != NULL)
5973 {
5974 /* Set tp to be our tabpage, temporarily. Also set the window to the
5975 * first window in the tabpage, otherwise the window is not valid. */
5976 if (switch_win(&oldcurwin, &oldtabpage,
Bram Moolenaar816968d2017-09-29 21:29:18 +02005977 tp == curtab || tp->tp_firstwin == NULL ? firstwin
5978 : tp->tp_firstwin, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005979 {
5980 /* look up the variable */
5981 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
5982 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
5983 if (v != NULL)
5984 {
5985 copy_tv(&v->di_tv, rettv);
5986 done = TRUE;
5987 }
5988 }
5989
5990 /* restore previous notion of curwin */
5991 restore_win(oldcurwin, oldtabpage, TRUE);
5992 }
5993
5994 if (!done && argvars[2].v_type != VAR_UNKNOWN)
5995 copy_tv(&argvars[2], rettv);
5996}
5997
5998/*
5999 * "gettabwinvar()" function
6000 */
6001 static void
6002f_gettabwinvar(typval_T *argvars, typval_T *rettv)
6003{
6004 getwinvar(argvars, rettv, 1);
6005}
6006
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006007/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +01006008 * "gettagstack()" function
6009 */
6010 static void
6011f_gettagstack(typval_T *argvars, typval_T *rettv)
6012{
6013 win_T *wp = curwin; // default is current window
6014
6015 if (rettv_dict_alloc(rettv) != OK)
6016 return;
6017
6018 if (argvars[0].v_type != VAR_UNKNOWN)
6019 {
6020 wp = find_win_by_nr_or_id(&argvars[0]);
6021 if (wp == NULL)
6022 return;
6023 }
6024
6025 get_tagstack(wp, rettv->vval.v_dict);
6026}
6027
6028/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006029 * Returns information about a window as a dictionary.
6030 */
6031 static dict_T *
6032get_win_info(win_T *wp, short tpnr, short winnr)
6033{
6034 dict_T *dict;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006035
6036 dict = dict_alloc();
6037 if (dict == NULL)
6038 return NULL;
6039
Bram Moolenaare0be1672018-07-08 16:50:37 +02006040 dict_add_number(dict, "tabnr", tpnr);
6041 dict_add_number(dict, "winnr", winnr);
6042 dict_add_number(dict, "winid", wp->w_id);
6043 dict_add_number(dict, "height", wp->w_height);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006044 dict_add_number(dict, "winrow", wp->w_winrow + 1);
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006045 dict_add_number(dict, "topline", wp->w_topline);
6046 dict_add_number(dict, "botline", wp->w_botline - 1);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006047#ifdef FEAT_MENU
Bram Moolenaare0be1672018-07-08 16:50:37 +02006048 dict_add_number(dict, "winbar", wp->w_winbar_height);
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006049#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +02006050 dict_add_number(dict, "width", wp->w_width);
Bram Moolenaar7132ddc2018-07-15 17:01:11 +02006051 dict_add_number(dict, "wincol", wp->w_wincol + 1);
Bram Moolenaare0be1672018-07-08 16:50:37 +02006052 dict_add_number(dict, "bufnr", wp->w_buffer->b_fnum);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006053
Bram Moolenaar69905d12017-08-13 18:14:47 +02006054#ifdef FEAT_TERMINAL
Bram Moolenaare0be1672018-07-08 16:50:37 +02006055 dict_add_number(dict, "terminal", bt_terminal(wp->w_buffer));
Bram Moolenaar69905d12017-08-13 18:14:47 +02006056#endif
Bram Moolenaar386600f2016-08-15 22:16:25 +02006057#ifdef FEAT_QUICKFIX
Bram Moolenaare0be1672018-07-08 16:50:37 +02006058 dict_add_number(dict, "quickfix", bt_quickfix(wp->w_buffer));
6059 dict_add_number(dict, "loclist",
6060 (bt_quickfix(wp->w_buffer) && wp->w_llist_ref != NULL));
Bram Moolenaar386600f2016-08-15 22:16:25 +02006061#endif
6062
Bram Moolenaar30567352016-08-27 21:25:44 +02006063 /* Add a reference to window variables */
Bram Moolenaar9f8187c2016-08-27 20:34:01 +02006064 dict_add_dict(dict, "variables", wp->w_vars);
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006065
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006066 return dict;
6067}
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006068
6069/*
6070 * "getwininfo()" function
6071 */
6072 static void
6073f_getwininfo(typval_T *argvars, typval_T *rettv)
6074{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006075 tabpage_T *tp;
6076 win_T *wp = NULL, *wparg = NULL;
6077 dict_T *d;
Bram Moolenaar386600f2016-08-15 22:16:25 +02006078 short tabnr = 0, winnr;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006079
6080 if (rettv_list_alloc(rettv) != OK)
6081 return;
6082
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006083 if (argvars[0].v_type != VAR_UNKNOWN)
6084 {
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006085 wparg = win_id2wp(tv_get_number(&argvars[0]));
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006086 if (wparg == NULL)
6087 return;
6088 }
6089
6090 /* Collect information about either all the windows across all the tab
6091 * pages or one particular window.
6092 */
Bram Moolenaar386600f2016-08-15 22:16:25 +02006093 FOR_ALL_TABPAGES(tp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006094 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006095 tabnr++;
6096 winnr = 0;
6097 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006098 {
Bram Moolenaar386600f2016-08-15 22:16:25 +02006099 winnr++;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006100 if (wparg != NULL && wp != wparg)
6101 continue;
6102 d = get_win_info(wp, tabnr, winnr);
6103 if (d != NULL)
6104 list_append_dict(rettv->vval.v_list, d);
6105 if (wparg != NULL)
6106 /* found information about a specific window */
6107 return;
6108 }
6109 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006110}
6111
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006112/*
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006113 * "win_execute()" function
6114 */
6115 static void
6116f_win_execute(typval_T *argvars, typval_T *rettv)
6117{
6118 int id = (int)tv_get_number(argvars);
6119 win_T *wp = win_id2wp(id);
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006120 win_T *save_curwin;
6121 tabpage_T *save_curtab;
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006122
6123 if (wp != NULL)
6124 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006125 if (switch_win_noblock(&save_curwin, &save_curtab, wp, curtab, TRUE)
6126 == OK)
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006127 {
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006128 check_cursor();
6129 execute_common(argvars, rettv, 1);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006130 }
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006131 restore_win_noblock(save_curwin, save_curtab, TRUE);
Bram Moolenaar868b7b62019-05-29 21:44:40 +02006132 }
6133}
6134
6135/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006136 * "win_findbuf()" function
6137 */
6138 static void
6139f_win_findbuf(typval_T *argvars, typval_T *rettv)
6140{
6141 if (rettv_list_alloc(rettv) != FAIL)
6142 win_findbuf(argvars, rettv->vval.v_list);
6143}
6144
6145/*
6146 * "win_getid()" function
6147 */
6148 static void
6149f_win_getid(typval_T *argvars, typval_T *rettv)
6150{
6151 rettv->vval.v_number = win_getid(argvars);
6152}
6153
6154/*
6155 * "win_gotoid()" function
6156 */
6157 static void
6158f_win_gotoid(typval_T *argvars, typval_T *rettv)
6159{
6160 rettv->vval.v_number = win_gotoid(argvars);
6161}
6162
6163/*
6164 * "win_id2tabwin()" function
6165 */
6166 static void
6167f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
6168{
6169 if (rettv_list_alloc(rettv) != FAIL)
6170 win_id2tabwin(argvars, rettv->vval.v_list);
6171}
6172
6173/*
6174 * "win_id2win()" function
6175 */
6176 static void
6177f_win_id2win(typval_T *argvars, typval_T *rettv)
6178{
6179 rettv->vval.v_number = win_id2win(argvars);
6180}
6181
6182/*
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006183 * "win_screenpos()" function
6184 */
6185 static void
6186f_win_screenpos(typval_T *argvars, typval_T *rettv)
6187{
6188 win_T *wp;
6189
6190 if (rettv_list_alloc(rettv) == FAIL)
6191 return;
6192
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02006193 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar22044dc2017-12-02 15:43:37 +01006194 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_winrow + 1);
6195 list_append_number(rettv->vval.v_list, wp == NULL ? 0 : wp->w_wincol + 1);
6196}
6197
6198/*
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006199 * "getwinpos({timeout})" function
6200 */
6201 static void
6202f_getwinpos(typval_T *argvars UNUSED, typval_T *rettv)
6203{
6204 int x = -1;
6205 int y = -1;
6206
6207 if (rettv_list_alloc(rettv) == FAIL)
6208 return;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006209#if defined(FEAT_GUI) \
6210 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6211 || defined(MSWIN)
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006212 {
6213 varnumber_T timeout = 100;
6214
6215 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006216 timeout = tv_get_number(&argvars[0]);
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006217
6218 (void)ui_get_winpos(&x, &y, timeout);
Bram Moolenaar3f54fd32018-03-03 21:29:55 +01006219 }
6220#endif
6221 list_append_number(rettv->vval.v_list, (varnumber_T)x);
6222 list_append_number(rettv->vval.v_list, (varnumber_T)y);
6223}
6224
6225
6226/*
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006227 * "getwinposx()" function
6228 */
6229 static void
6230f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
6231{
6232 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006233#if defined(FEAT_GUI) \
6234 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6235 || defined(MSWIN)
6236
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006237 {
6238 int x, y;
6239
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006240 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006241 rettv->vval.v_number = x;
6242 }
6243#endif
6244}
6245
6246/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006247 * "getwinposy()" function
6248 */
6249 static void
6250f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
6251{
6252 rettv->vval.v_number = -1;
Bram Moolenaar16c34c32019-04-06 22:01:24 +02006253#if defined(FEAT_GUI) \
6254 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
6255 || defined(MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006256 {
6257 int x, y;
6258
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02006259 if (ui_get_winpos(&x, &y, 100) == OK)
Bram Moolenaarba6ec182017-04-04 22:41:10 +02006260 rettv->vval.v_number = y;
6261 }
6262#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263}
6264
6265/*
6266 * "getwinvar()" function
6267 */
6268 static void
6269f_getwinvar(typval_T *argvars, typval_T *rettv)
6270{
6271 getwinvar(argvars, rettv, 0);
6272}
6273
6274/*
6275 * "glob()" function
6276 */
6277 static void
6278f_glob(typval_T *argvars, typval_T *rettv)
6279{
6280 int options = WILD_SILENT|WILD_USE_NL;
6281 expand_T xpc;
6282 int error = FALSE;
6283
6284 /* When the optional second argument is non-zero, don't remove matches
6285 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6286 rettv->v_type = VAR_STRING;
6287 if (argvars[1].v_type != VAR_UNKNOWN)
6288 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006289 if (tv_get_number_chk(&argvars[1], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006290 options |= WILD_KEEP_ALL;
6291 if (argvars[2].v_type != VAR_UNKNOWN)
6292 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006293 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006294 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006295 if (argvars[3].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006296 && tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006297 options |= WILD_ALLLINKS;
6298 }
6299 }
6300 if (!error)
6301 {
6302 ExpandInit(&xpc);
6303 xpc.xp_context = EXPAND_FILES;
6304 if (p_wic)
6305 options += WILD_ICASE;
6306 if (rettv->v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006307 rettv->vval.v_string = ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006308 NULL, options, WILD_ALL);
6309 else if (rettv_list_alloc(rettv) != FAIL)
6310 {
6311 int i;
6312
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006313 ExpandOne(&xpc, tv_get_string(&argvars[0]),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006314 NULL, options, WILD_ALL_KEEP);
6315 for (i = 0; i < xpc.xp_numfiles; i++)
6316 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
6317
6318 ExpandCleanup(&xpc);
6319 }
6320 }
6321 else
6322 rettv->vval.v_string = NULL;
6323}
6324
6325/*
6326 * "globpath()" function
6327 */
6328 static void
6329f_globpath(typval_T *argvars, typval_T *rettv)
6330{
6331 int flags = 0;
6332 char_u buf1[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006333 char_u *file = tv_get_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006334 int error = FALSE;
6335 garray_T ga;
6336 int i;
6337
6338 /* When the optional second argument is non-zero, don't remove matches
6339 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
6340 rettv->v_type = VAR_STRING;
6341 if (argvars[2].v_type != VAR_UNKNOWN)
6342 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006343 if (tv_get_number_chk(&argvars[2], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006344 flags |= WILD_KEEP_ALL;
6345 if (argvars[3].v_type != VAR_UNKNOWN)
6346 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006347 if (tv_get_number_chk(&argvars[3], &error))
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02006348 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006349 if (argvars[4].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006350 && tv_get_number_chk(&argvars[4], &error))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006351 flags |= WILD_ALLLINKS;
6352 }
6353 }
6354 if (file != NULL && !error)
6355 {
6356 ga_init2(&ga, (int)sizeof(char_u *), 10);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006357 globpath(tv_get_string(&argvars[0]), file, &ga, flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006358 if (rettv->v_type == VAR_STRING)
6359 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
6360 else if (rettv_list_alloc(rettv) != FAIL)
6361 for (i = 0; i < ga.ga_len; ++i)
6362 list_append_string(rettv->vval.v_list,
6363 ((char_u **)(ga.ga_data))[i], -1);
6364 ga_clear_strings(&ga);
6365 }
6366 else
6367 rettv->vval.v_string = NULL;
6368}
6369
6370/*
6371 * "glob2regpat()" function
6372 */
6373 static void
6374f_glob2regpat(typval_T *argvars, typval_T *rettv)
6375{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006376 char_u *pat = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006377
6378 rettv->v_type = VAR_STRING;
6379 rettv->vval.v_string = (pat == NULL)
6380 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
6381}
6382
6383/* for VIM_VERSION_ defines */
6384#include "version.h"
6385
6386/*
6387 * "has()" function
6388 */
6389 static void
6390f_has(typval_T *argvars, typval_T *rettv)
6391{
6392 int i;
6393 char_u *name;
6394 int n = FALSE;
6395 static char *(has_list[]) =
6396 {
6397#ifdef AMIGA
6398 "amiga",
6399# ifdef FEAT_ARP
6400 "arp",
6401# endif
6402#endif
6403#ifdef __BEOS__
6404 "beos",
6405#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006406#if defined(BSD) && !defined(MACOS_X)
6407 "bsd",
6408#endif
6409#ifdef hpux
6410 "hpux",
6411#endif
6412#ifdef __linux__
6413 "linux",
6414#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02006415#ifdef MACOS_X
Bram Moolenaar4f505882018-02-10 21:06:32 +01006416 "mac", /* Mac OS X (and, once, Mac OS Classic) */
6417 "osx", /* Mac OS X */
Bram Moolenaard0573012017-10-28 21:11:06 +02006418# ifdef MACOS_X_DARWIN
Bram Moolenaar4f505882018-02-10 21:06:32 +01006419 "macunix", /* Mac OS X, with the darwin feature */
6420 "osxdarwin", /* synonym for macunix */
Bram Moolenaard0573012017-10-28 21:11:06 +02006421# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006422#endif
6423#ifdef __QNX__
6424 "qnx",
6425#endif
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006426#ifdef SUN_SYSTEM
6427 "sun",
6428#else
6429 "moon",
6430#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006431#ifdef UNIX
6432 "unix",
6433#endif
6434#ifdef VMS
6435 "vms",
6436#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006437#ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006438 "win32",
6439#endif
Bram Moolenaar1eed5322019-02-26 17:03:54 +01006440#if defined(UNIX) && defined(__CYGWIN__)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006441 "win32unix",
6442#endif
Bram Moolenaar44b443c2019-02-18 22:14:18 +01006443#ifdef _WIN64
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006444 "win64",
6445#endif
6446#ifdef EBCDIC
6447 "ebcdic",
6448#endif
6449#ifndef CASE_INSENSITIVE_FILENAME
6450 "fname_case",
6451#endif
6452#ifdef HAVE_ACL
6453 "acl",
6454#endif
6455#ifdef FEAT_ARABIC
6456 "arabic",
6457#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006458 "autocmd",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006459#ifdef FEAT_AUTOCHDIR
Bram Moolenaar39536dd2019-01-29 22:58:21 +01006460 "autochdir",
Bram Moolenaar83ec2a72018-07-27 22:08:59 +02006461#endif
Bram Moolenaare42a6d22017-11-12 19:21:51 +01006462#ifdef FEAT_AUTOSERVERNAME
6463 "autoservername",
6464#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006465#ifdef FEAT_BEVAL_GUI
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006466 "balloon_eval",
Bram Moolenaar4f974752019-02-17 17:44:42 +01006467# ifndef FEAT_GUI_MSWIN /* other GUIs always have multiline balloons */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006468 "balloon_multiline",
6469# endif
6470#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01006471#ifdef FEAT_BEVAL_TERM
Bram Moolenaar51b0f372017-11-18 18:52:04 +01006472 "balloon_eval_term",
6473#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006474#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6475 "builtin_terms",
6476# ifdef ALL_BUILTIN_TCAPS
6477 "all_builtin_terms",
6478# endif
6479#endif
6480#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
Bram Moolenaar4f974752019-02-17 17:44:42 +01006481 || defined(FEAT_GUI_MSWIN) \
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006482 || defined(FEAT_GUI_MOTIF))
6483 "browsefilter",
6484#endif
6485#ifdef FEAT_BYTEOFF
6486 "byte_offset",
6487#endif
6488#ifdef FEAT_JOB_CHANNEL
6489 "channel",
6490#endif
6491#ifdef FEAT_CINDENT
6492 "cindent",
6493#endif
6494#ifdef FEAT_CLIENTSERVER
6495 "clientserver",
6496#endif
6497#ifdef FEAT_CLIPBOARD
6498 "clipboard",
6499#endif
6500#ifdef FEAT_CMDL_COMPL
6501 "cmdline_compl",
6502#endif
6503#ifdef FEAT_CMDHIST
6504 "cmdline_hist",
6505#endif
6506#ifdef FEAT_COMMENTS
6507 "comments",
6508#endif
6509#ifdef FEAT_CONCEAL
6510 "conceal",
6511#endif
6512#ifdef FEAT_CRYPT
6513 "cryptv",
6514 "crypt-blowfish",
6515 "crypt-blowfish2",
6516#endif
6517#ifdef FEAT_CSCOPE
6518 "cscope",
6519#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006520 "cursorbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006521#ifdef CURSOR_SHAPE
6522 "cursorshape",
6523#endif
6524#ifdef DEBUG
6525 "debug",
6526#endif
6527#ifdef FEAT_CON_DIALOG
6528 "dialog_con",
6529#endif
6530#ifdef FEAT_GUI_DIALOG
6531 "dialog_gui",
6532#endif
6533#ifdef FEAT_DIFF
6534 "diff",
6535#endif
6536#ifdef FEAT_DIGRAPHS
6537 "digraphs",
6538#endif
6539#ifdef FEAT_DIRECTX
6540 "directx",
6541#endif
6542#ifdef FEAT_DND
6543 "dnd",
6544#endif
6545#ifdef FEAT_EMACS_TAGS
6546 "emacs_tags",
6547#endif
6548 "eval", /* always present, of course! */
6549 "ex_extra", /* graduated feature */
6550#ifdef FEAT_SEARCH_EXTRA
6551 "extra_search",
6552#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006553#ifdef FEAT_SEARCHPATH
6554 "file_in_path",
6555#endif
6556#ifdef FEAT_FILTERPIPE
6557 "filterpipe",
6558#endif
6559#ifdef FEAT_FIND_ID
6560 "find_in_path",
6561#endif
6562#ifdef FEAT_FLOAT
6563 "float",
6564#endif
6565#ifdef FEAT_FOLDING
6566 "folding",
6567#endif
6568#ifdef FEAT_FOOTER
6569 "footer",
6570#endif
6571#if !defined(USE_SYSTEM) && defined(UNIX)
6572 "fork",
6573#endif
6574#ifdef FEAT_GETTEXT
6575 "gettext",
6576#endif
6577#ifdef FEAT_GUI
6578 "gui",
6579#endif
6580#ifdef FEAT_GUI_ATHENA
6581# ifdef FEAT_GUI_NEXTAW
6582 "gui_neXtaw",
6583# else
6584 "gui_athena",
6585# endif
6586#endif
6587#ifdef FEAT_GUI_GTK
6588 "gui_gtk",
6589# ifdef USE_GTK3
6590 "gui_gtk3",
6591# else
6592 "gui_gtk2",
6593# endif
6594#endif
6595#ifdef FEAT_GUI_GNOME
6596 "gui_gnome",
6597#endif
6598#ifdef FEAT_GUI_MAC
6599 "gui_mac",
6600#endif
6601#ifdef FEAT_GUI_MOTIF
6602 "gui_motif",
6603#endif
6604#ifdef FEAT_GUI_PHOTON
6605 "gui_photon",
6606#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006607#ifdef FEAT_GUI_MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006608 "gui_win32",
6609#endif
6610#ifdef FEAT_HANGULIN
6611 "hangul_input",
6612#endif
6613#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6614 "iconv",
6615#endif
6616#ifdef FEAT_INS_EXPAND
6617 "insert_expand",
6618#endif
6619#ifdef FEAT_JOB_CHANNEL
6620 "job",
6621#endif
6622#ifdef FEAT_JUMPLIST
6623 "jumplist",
6624#endif
6625#ifdef FEAT_KEYMAP
6626 "keymap",
6627#endif
Bram Moolenaar9532fe72016-07-29 22:50:35 +02006628 "lambda", /* always with FEAT_EVAL, since 7.4.2120 with closure */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006629#ifdef FEAT_LANGMAP
6630 "langmap",
6631#endif
6632#ifdef FEAT_LIBCALL
6633 "libcall",
6634#endif
6635#ifdef FEAT_LINEBREAK
6636 "linebreak",
6637#endif
6638#ifdef FEAT_LISP
6639 "lispindent",
6640#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006641 "listcmds",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006642#ifdef FEAT_LOCALMAP
6643 "localmap",
6644#endif
6645#ifdef FEAT_LUA
6646# ifndef DYNAMIC_LUA
6647 "lua",
6648# endif
6649#endif
6650#ifdef FEAT_MENU
6651 "menu",
6652#endif
6653#ifdef FEAT_SESSION
6654 "mksession",
6655#endif
6656#ifdef FEAT_MODIFY_FNAME
6657 "modify_fname",
6658#endif
6659#ifdef FEAT_MOUSE
6660 "mouse",
6661#endif
6662#ifdef FEAT_MOUSESHAPE
6663 "mouseshape",
6664#endif
6665#if defined(UNIX) || defined(VMS)
6666# ifdef FEAT_MOUSE_DEC
6667 "mouse_dec",
6668# endif
6669# ifdef FEAT_MOUSE_GPM
6670 "mouse_gpm",
6671# endif
6672# ifdef FEAT_MOUSE_JSB
6673 "mouse_jsbterm",
6674# endif
6675# ifdef FEAT_MOUSE_NET
6676 "mouse_netterm",
6677# endif
6678# ifdef FEAT_MOUSE_PTERM
6679 "mouse_pterm",
6680# endif
Bram Moolenaar2ace1bd2019-03-22 12:03:30 +01006681# ifdef FEAT_MOUSE_XTERM
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006682 "mouse_sgr",
6683# endif
6684# ifdef FEAT_SYSMOUSE
6685 "mouse_sysmouse",
6686# endif
6687# ifdef FEAT_MOUSE_URXVT
6688 "mouse_urxvt",
6689# endif
6690# ifdef FEAT_MOUSE_XTERM
6691 "mouse_xterm",
6692# endif
6693#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006694 "multi_byte",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006695#ifdef FEAT_MBYTE_IME
6696 "multi_byte_ime",
6697#endif
6698#ifdef FEAT_MULTI_LANG
6699 "multi_lang",
6700#endif
6701#ifdef FEAT_MZSCHEME
6702#ifndef DYNAMIC_MZSCHEME
6703 "mzscheme",
6704#endif
6705#endif
6706#ifdef FEAT_NUM64
6707 "num64",
6708#endif
6709#ifdef FEAT_OLE
6710 "ole",
6711#endif
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006712#ifdef FEAT_EVAL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006713 "packages",
Bram Moolenaar6183ccb2018-07-22 05:08:11 +02006714#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006715#ifdef FEAT_PATH_EXTRA
6716 "path_extra",
6717#endif
6718#ifdef FEAT_PERL
6719#ifndef DYNAMIC_PERL
6720 "perl",
6721#endif
6722#endif
6723#ifdef FEAT_PERSISTENT_UNDO
6724 "persistent_undo",
6725#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006726#if defined(FEAT_PYTHON)
6727 "python_compiled",
6728# if defined(DYNAMIC_PYTHON)
6729 "python_dynamic",
6730# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006731 "python",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006732 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006733# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006734#endif
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006735#if defined(FEAT_PYTHON3)
6736 "python3_compiled",
6737# if defined(DYNAMIC_PYTHON3)
6738 "python3_dynamic",
6739# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006740 "python3",
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006741 "pythonx",
Bram Moolenaar84b242c2018-01-28 17:45:49 +01006742# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006743#endif
6744#ifdef FEAT_POSTSCRIPT
6745 "postscript",
6746#endif
6747#ifdef FEAT_PRINTER
6748 "printer",
6749#endif
6750#ifdef FEAT_PROFILE
6751 "profile",
6752#endif
6753#ifdef FEAT_RELTIME
6754 "reltime",
6755#endif
6756#ifdef FEAT_QUICKFIX
6757 "quickfix",
6758#endif
6759#ifdef FEAT_RIGHTLEFT
6760 "rightleft",
6761#endif
6762#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
6763 "ruby",
6764#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006765 "scrollbind",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006766#ifdef FEAT_CMDL_INFO
6767 "showcmd",
6768 "cmdline_info",
6769#endif
6770#ifdef FEAT_SIGNS
6771 "signs",
6772#endif
6773#ifdef FEAT_SMARTINDENT
6774 "smartindent",
6775#endif
6776#ifdef STARTUPTIME
6777 "startuptime",
6778#endif
6779#ifdef FEAT_STL_OPT
6780 "statusline",
6781#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006782#ifdef FEAT_NETBEANS_INTG
6783 "netbeans_intg",
6784#endif
6785#ifdef FEAT_SPELL
6786 "spell",
6787#endif
6788#ifdef FEAT_SYN_HL
6789 "syntax",
6790#endif
6791#if defined(USE_SYSTEM) || !defined(UNIX)
6792 "system",
6793#endif
6794#ifdef FEAT_TAG_BINS
6795 "tag_binary",
6796#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006797#ifdef FEAT_TCL
6798# ifndef DYNAMIC_TCL
6799 "tcl",
6800# endif
6801#endif
6802#ifdef FEAT_TERMGUICOLORS
6803 "termguicolors",
6804#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01006805#if defined(FEAT_TERMINAL) && !defined(MSWIN)
Bram Moolenaare4f25e42017-07-07 11:54:15 +02006806 "terminal",
6807#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006808#ifdef TERMINFO
6809 "terminfo",
6810#endif
6811#ifdef FEAT_TERMRESPONSE
6812 "termresponse",
6813#endif
6814#ifdef FEAT_TEXTOBJ
6815 "textobjects",
6816#endif
Bram Moolenaar98aefe72018-12-13 22:20:09 +01006817#ifdef FEAT_TEXT_PROP
6818 "textprop",
6819#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006820#ifdef HAVE_TGETENT
6821 "tgetent",
6822#endif
6823#ifdef FEAT_TIMERS
6824 "timers",
6825#endif
6826#ifdef FEAT_TITLE
6827 "title",
6828#endif
6829#ifdef FEAT_TOOLBAR
6830 "toolbar",
6831#endif
6832#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
6833 "unnamedplus",
6834#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006835 "user-commands", /* was accidentally included in 5.4 */
6836 "user_commands",
Bram Moolenaar04958cb2018-06-23 19:23:02 +02006837#ifdef FEAT_VARTABS
6838 "vartabs",
6839#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006840 "vertsplit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006841#ifdef FEAT_VIMINFO
6842 "viminfo",
6843#endif
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02006844 "vimscript-1",
6845 "vimscript-2",
Bram Moolenaar93a48792019-04-20 21:54:28 +02006846 "vimscript-3",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006847 "virtualedit",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006848 "visual",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006849 "visualextra",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006850 "vreplace",
Bram Moolenaarff1e8792018-03-12 22:16:37 +01006851#ifdef FEAT_VTP
6852 "vtp",
6853#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006854#ifdef FEAT_WILDIGN
6855 "wildignore",
6856#endif
6857#ifdef FEAT_WILDMENU
6858 "wildmenu",
6859#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006860 "windows",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006861#ifdef FEAT_WAK
6862 "winaltkeys",
6863#endif
6864#ifdef FEAT_WRITEBACKUP
6865 "writebackup",
6866#endif
6867#ifdef FEAT_XIM
6868 "xim",
6869#endif
6870#ifdef FEAT_XFONTSET
6871 "xfontset",
6872#endif
6873#ifdef FEAT_XPM_W32
6874 "xpm",
6875 "xpm_w32", /* for backward compatibility */
6876#else
6877# if defined(HAVE_XPM)
6878 "xpm",
6879# endif
6880#endif
6881#ifdef USE_XSMP
6882 "xsmp",
6883#endif
6884#ifdef USE_XSMP_INTERACT
6885 "xsmp_interact",
6886#endif
6887#ifdef FEAT_XCLIPBOARD
6888 "xterm_clipboard",
6889#endif
6890#ifdef FEAT_XTERM_SAVE
6891 "xterm_save",
6892#endif
6893#if defined(UNIX) && defined(FEAT_X11)
6894 "X11",
6895#endif
6896 NULL
6897 };
6898
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006899 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006900 for (i = 0; has_list[i] != NULL; ++i)
6901 if (STRICMP(name, has_list[i]) == 0)
6902 {
6903 n = TRUE;
6904 break;
6905 }
6906
6907 if (n == FALSE)
6908 {
6909 if (STRNICMP(name, "patch", 5) == 0)
6910 {
6911 if (name[5] == '-'
6912 && STRLEN(name) >= 11
6913 && vim_isdigit(name[6])
6914 && vim_isdigit(name[8])
6915 && vim_isdigit(name[10]))
6916 {
6917 int major = atoi((char *)name + 6);
6918 int minor = atoi((char *)name + 8);
6919
6920 /* Expect "patch-9.9.01234". */
6921 n = (major < VIM_VERSION_MAJOR
6922 || (major == VIM_VERSION_MAJOR
6923 && (minor < VIM_VERSION_MINOR
6924 || (minor == VIM_VERSION_MINOR
6925 && has_patch(atoi((char *)name + 10))))));
6926 }
6927 else
6928 n = has_patch(atoi((char *)name + 5));
6929 }
6930 else if (STRICMP(name, "vim_starting") == 0)
6931 n = (starting != 0);
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01006932 else if (STRICMP(name, "ttyin") == 0)
6933 n = mch_input_isatty();
6934 else if (STRICMP(name, "ttyout") == 0)
6935 n = stdout_isatty;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006936 else if (STRICMP(name, "multi_byte_encoding") == 0)
6937 n = has_mbyte;
Bram Moolenaar4f974752019-02-17 17:44:42 +01006938#if defined(FEAT_BEVAL) && defined(FEAT_GUI_MSWIN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006939 else if (STRICMP(name, "balloon_multiline") == 0)
6940 n = multiline_balloon_available();
6941#endif
6942#ifdef DYNAMIC_TCL
6943 else if (STRICMP(name, "tcl") == 0)
6944 n = tcl_enabled(FALSE);
6945#endif
6946#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
6947 else if (STRICMP(name, "iconv") == 0)
6948 n = iconv_enabled(FALSE);
6949#endif
6950#ifdef DYNAMIC_LUA
6951 else if (STRICMP(name, "lua") == 0)
6952 n = lua_enabled(FALSE);
6953#endif
6954#ifdef DYNAMIC_MZSCHEME
6955 else if (STRICMP(name, "mzscheme") == 0)
6956 n = mzscheme_enabled(FALSE);
6957#endif
6958#ifdef DYNAMIC_RUBY
6959 else if (STRICMP(name, "ruby") == 0)
6960 n = ruby_enabled(FALSE);
6961#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006962#ifdef DYNAMIC_PYTHON
6963 else if (STRICMP(name, "python") == 0)
6964 n = python_enabled(FALSE);
6965#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006966#ifdef DYNAMIC_PYTHON3
6967 else if (STRICMP(name, "python3") == 0)
6968 n = python3_enabled(FALSE);
6969#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006970#if defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3)
6971 else if (STRICMP(name, "pythonx") == 0)
6972 {
6973# if defined(DYNAMIC_PYTHON) && defined(DYNAMIC_PYTHON3)
6974 if (p_pyx == 0)
6975 n = python3_enabled(FALSE) || python_enabled(FALSE);
6976 else if (p_pyx == 3)
6977 n = python3_enabled(FALSE);
6978 else if (p_pyx == 2)
6979 n = python_enabled(FALSE);
6980# elif defined(DYNAMIC_PYTHON)
6981 n = python_enabled(FALSE);
6982# elif defined(DYNAMIC_PYTHON3)
6983 n = python3_enabled(FALSE);
6984# endif
6985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006986#endif
6987#ifdef DYNAMIC_PERL
6988 else if (STRICMP(name, "perl") == 0)
6989 n = perl_enabled(FALSE);
6990#endif
6991#ifdef FEAT_GUI
6992 else if (STRICMP(name, "gui_running") == 0)
6993 n = (gui.in_use || gui.starting);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006994# ifdef FEAT_BROWSE
6995 else if (STRICMP(name, "browse") == 0)
6996 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
6997# endif
6998#endif
6999#ifdef FEAT_SYN_HL
7000 else if (STRICMP(name, "syntax_items") == 0)
7001 n = syntax_present(curwin);
7002#endif
Bram Moolenaarcafafb32018-02-22 21:07:09 +01007003#ifdef FEAT_VTP
7004 else if (STRICMP(name, "vcon") == 0)
Bram Moolenaard8b37a52018-06-28 15:50:28 +02007005 n = is_term_win32() && has_vtp_working();
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007006#endif
7007#ifdef FEAT_NETBEANS_INTG
7008 else if (STRICMP(name, "netbeans_enabled") == 0)
7009 n = netbeans_active();
7010#endif
Bram Moolenaar4b8366b2019-05-04 17:34:34 +02007011#ifdef FEAT_MOUSE_GPM
7012 else if (STRICMP(name, "mouse_gpm_enabled") == 0)
7013 n = gpm_enabled();
7014#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007015#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaara83e3962017-08-17 14:39:07 +02007016 else if (STRICMP(name, "terminal") == 0)
7017 n = terminal_enabled();
7018#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01007019#if defined(FEAT_TERMINAL) && defined(MSWIN)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01007020 else if (STRICMP(name, "conpty") == 0)
7021 n = use_conpty();
7022#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007023 }
7024
7025 rettv->vval.v_number = n;
7026}
7027
7028/*
7029 * "has_key()" function
7030 */
7031 static void
7032f_has_key(typval_T *argvars, typval_T *rettv)
7033{
7034 if (argvars[0].v_type != VAR_DICT)
7035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007036 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007037 return;
7038 }
7039 if (argvars[0].vval.v_dict == NULL)
7040 return;
7041
7042 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007043 tv_get_string(&argvars[1]), -1) != NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007044}
7045
7046/*
7047 * "haslocaldir()" function
7048 */
7049 static void
7050f_haslocaldir(typval_T *argvars, typval_T *rettv)
7051{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007052 tabpage_T *tp = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007053 win_T *wp = NULL;
7054
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007055 wp = find_tabwin(&argvars[0], &argvars[1], &tp);
7056
7057 // Check for window-local and tab-local directories
7058 if (wp != NULL && wp->w_localdir != NULL)
7059 rettv->vval.v_number = 1;
7060 else if (tp != NULL && tp->tp_localdir != NULL)
7061 rettv->vval.v_number = 2;
7062 else
7063 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007064}
7065
7066/*
7067 * "hasmapto()" function
7068 */
7069 static void
7070f_hasmapto(typval_T *argvars, typval_T *rettv)
7071{
7072 char_u *name;
7073 char_u *mode;
7074 char_u buf[NUMBUFLEN];
7075 int abbr = FALSE;
7076
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007077 name = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007078 if (argvars[1].v_type == VAR_UNKNOWN)
7079 mode = (char_u *)"nvo";
7080 else
7081 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007082 mode = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007084 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007085 }
7086
7087 if (map_to_exists(name, mode, abbr))
7088 rettv->vval.v_number = TRUE;
7089 else
7090 rettv->vval.v_number = FALSE;
7091}
7092
7093/*
7094 * "histadd()" function
7095 */
7096 static void
7097f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
7098{
7099#ifdef FEAT_CMDHIST
7100 int histype;
7101 char_u *str;
7102 char_u buf[NUMBUFLEN];
7103#endif
7104
7105 rettv->vval.v_number = FALSE;
Bram Moolenaar8c62a082019-02-08 14:34:10 +01007106 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007107 return;
7108#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007109 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007110 histype = str != NULL ? get_histtype(str) : -1;
7111 if (histype >= 0)
7112 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007113 str = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007114 if (*str != NUL)
7115 {
7116 init_history();
7117 add_to_history(histype, str, FALSE, NUL);
7118 rettv->vval.v_number = TRUE;
7119 return;
7120 }
7121 }
7122#endif
7123}
7124
7125/*
7126 * "histdel()" function
7127 */
7128 static void
7129f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
7130{
7131#ifdef FEAT_CMDHIST
7132 int n;
7133 char_u buf[NUMBUFLEN];
7134 char_u *str;
7135
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007136 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007137 if (str == NULL)
7138 n = 0;
7139 else if (argvars[1].v_type == VAR_UNKNOWN)
7140 /* only one argument: clear entire history */
7141 n = clr_history(get_histtype(str));
7142 else if (argvars[1].v_type == VAR_NUMBER)
7143 /* index given: remove that entry */
7144 n = del_history_idx(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007145 (int)tv_get_number(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007146 else
7147 /* string given: remove all matching entries */
7148 n = del_history_entry(get_histtype(str),
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007149 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007150 rettv->vval.v_number = n;
7151#endif
7152}
7153
7154/*
7155 * "histget()" function
7156 */
7157 static void
7158f_histget(typval_T *argvars UNUSED, typval_T *rettv)
7159{
7160#ifdef FEAT_CMDHIST
7161 int type;
7162 int idx;
7163 char_u *str;
7164
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007165 str = tv_get_string_chk(&argvars[0]); /* NULL on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007166 if (str == NULL)
7167 rettv->vval.v_string = NULL;
7168 else
7169 {
7170 type = get_histtype(str);
7171 if (argvars[1].v_type == VAR_UNKNOWN)
7172 idx = get_history_idx(type);
7173 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007174 idx = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007175 /* -1 on type error */
7176 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
7177 }
7178#else
7179 rettv->vval.v_string = NULL;
7180#endif
7181 rettv->v_type = VAR_STRING;
7182}
7183
7184/*
7185 * "histnr()" function
7186 */
7187 static void
7188f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
7189{
7190 int i;
7191
7192#ifdef FEAT_CMDHIST
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007193 char_u *history = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007194
7195 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
7196 if (i >= HIST_CMD && i < HIST_COUNT)
7197 i = get_history_idx(i);
7198 else
7199#endif
7200 i = -1;
7201 rettv->vval.v_number = i;
7202}
7203
7204/*
7205 * "highlightID(name)" function
7206 */
7207 static void
7208f_hlID(typval_T *argvars, typval_T *rettv)
7209{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007210 rettv->vval.v_number = syn_name2id(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007211}
7212
7213/*
7214 * "highlight_exists()" function
7215 */
7216 static void
7217f_hlexists(typval_T *argvars, typval_T *rettv)
7218{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007219 rettv->vval.v_number = highlight_exists(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007220}
7221
7222/*
7223 * "hostname()" function
7224 */
7225 static void
7226f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
7227{
7228 char_u hostname[256];
7229
7230 mch_get_host_name(hostname, 256);
7231 rettv->v_type = VAR_STRING;
7232 rettv->vval.v_string = vim_strsave(hostname);
7233}
7234
7235/*
7236 * iconv() function
7237 */
7238 static void
7239f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
7240{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007241 char_u buf1[NUMBUFLEN];
7242 char_u buf2[NUMBUFLEN];
7243 char_u *from, *to, *str;
7244 vimconv_T vimconv;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007245
7246 rettv->v_type = VAR_STRING;
7247 rettv->vval.v_string = NULL;
7248
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007249 str = tv_get_string(&argvars[0]);
7250 from = enc_canonize(enc_skip(tv_get_string_buf(&argvars[1], buf1)));
7251 to = enc_canonize(enc_skip(tv_get_string_buf(&argvars[2], buf2)));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007252 vimconv.vc_type = CONV_NONE;
7253 convert_setup(&vimconv, from, to);
7254
7255 /* If the encodings are equal, no conversion needed. */
7256 if (vimconv.vc_type == CONV_NONE)
7257 rettv->vval.v_string = vim_strsave(str);
7258 else
7259 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
7260
7261 convert_setup(&vimconv, NULL, NULL);
7262 vim_free(from);
7263 vim_free(to);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007264}
7265
7266/*
7267 * "indent()" function
7268 */
7269 static void
7270f_indent(typval_T *argvars, typval_T *rettv)
7271{
7272 linenr_T lnum;
7273
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007274 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007275 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7276 rettv->vval.v_number = get_indent_lnum(lnum);
7277 else
7278 rettv->vval.v_number = -1;
7279}
7280
7281/*
7282 * "index()" function
7283 */
7284 static void
7285f_index(typval_T *argvars, typval_T *rettv)
7286{
7287 list_T *l;
7288 listitem_T *item;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007289 blob_T *b;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007290 long idx = 0;
7291 int ic = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007292 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007293
7294 rettv->vval.v_number = -1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007295 if (argvars[0].v_type == VAR_BLOB)
7296 {
7297 typval_T tv;
7298 int start = 0;
7299
7300 if (argvars[2].v_type != VAR_UNKNOWN)
7301 {
7302 start = tv_get_number_chk(&argvars[2], &error);
7303 if (error)
7304 return;
7305 }
7306 b = argvars[0].vval.v_blob;
7307 if (b == NULL)
7308 return;
Bram Moolenaar05500ec2019-01-13 19:10:33 +01007309 if (start < 0)
7310 {
7311 start = blob_len(b) + start;
7312 if (start < 0)
7313 start = 0;
7314 }
7315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007316 for (idx = start; idx < blob_len(b); ++idx)
7317 {
7318 tv.v_type = VAR_NUMBER;
7319 tv.vval.v_number = blob_get(b, idx);
7320 if (tv_equal(&tv, &argvars[1], ic, FALSE))
7321 {
7322 rettv->vval.v_number = idx;
7323 return;
7324 }
7325 }
7326 return;
7327 }
7328 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007329 {
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007330 emsg(_(e_listblobreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007331 return;
7332 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007333
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007334 l = argvars[0].vval.v_list;
7335 if (l != NULL)
7336 {
7337 item = l->lv_first;
7338 if (argvars[2].v_type != VAR_UNKNOWN)
7339 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007340 /* Start at specified item. Use the cached index that list_find()
7341 * sets, so that a negative number also works. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007342 item = list_find(l, (long)tv_get_number_chk(&argvars[2], &error));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007343 idx = l->lv_idx;
7344 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007345 ic = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007346 if (error)
7347 item = NULL;
7348 }
7349
7350 for ( ; item != NULL; item = item->li_next, ++idx)
7351 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
7352 {
7353 rettv->vval.v_number = idx;
7354 break;
7355 }
7356 }
7357}
7358
7359static int inputsecret_flag = 0;
7360
7361/*
7362 * "input()" function
7363 * Also handles inputsecret() when inputsecret is set.
7364 */
7365 static void
7366f_input(typval_T *argvars, typval_T *rettv)
7367{
7368 get_user_input(argvars, rettv, FALSE, inputsecret_flag);
7369}
7370
7371/*
7372 * "inputdialog()" function
7373 */
7374 static void
7375f_inputdialog(typval_T *argvars, typval_T *rettv)
7376{
7377#if defined(FEAT_GUI_TEXTDIALOG)
7378 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7379 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7380 {
7381 char_u *message;
7382 char_u buf[NUMBUFLEN];
7383 char_u *defstr = (char_u *)"";
7384
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007385 message = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007386 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007387 && (defstr = tv_get_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007388 vim_strncpy(IObuff, defstr, IOSIZE - 1);
7389 else
7390 IObuff[0] = NUL;
7391 if (message != NULL && defstr != NULL
7392 && do_dialog(VIM_QUESTION, NULL, message,
7393 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
7394 rettv->vval.v_string = vim_strsave(IObuff);
7395 else
7396 {
7397 if (message != NULL && defstr != NULL
7398 && argvars[1].v_type != VAR_UNKNOWN
7399 && argvars[2].v_type != VAR_UNKNOWN)
7400 rettv->vval.v_string = vim_strsave(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007401 tv_get_string_buf(&argvars[2], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007402 else
7403 rettv->vval.v_string = NULL;
7404 }
7405 rettv->v_type = VAR_STRING;
7406 }
7407 else
7408#endif
7409 get_user_input(argvars, rettv, TRUE, inputsecret_flag);
7410}
7411
7412/*
7413 * "inputlist()" function
7414 */
7415 static void
7416f_inputlist(typval_T *argvars, typval_T *rettv)
7417{
7418 listitem_T *li;
7419 int selected;
7420 int mouse_used;
7421
7422#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02007423 /* While starting up, there is no place to enter text. When running tests
7424 * with --not-a-term we assume feedkeys() will be used. */
7425 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007426 return;
7427#endif
7428 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
7429 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 semsg(_(e_listarg), "inputlist()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007431 return;
7432 }
7433
7434 msg_start();
7435 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
7436 lines_left = Rows; /* avoid more prompt */
7437 msg_scroll = TRUE;
7438 msg_clr_eos();
7439
7440 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
7441 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007442 msg_puts((char *)tv_get_string(&li->li_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007443 msg_putchar('\n');
7444 }
7445
7446 /* Ask for choice. */
7447 selected = prompt_for_number(&mouse_used);
7448 if (mouse_used)
7449 selected -= lines_left;
7450
7451 rettv->vval.v_number = selected;
7452}
7453
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007454static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7455
7456/*
7457 * "inputrestore()" function
7458 */
7459 static void
7460f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
7461{
7462 if (ga_userinput.ga_len > 0)
7463 {
7464 --ga_userinput.ga_len;
7465 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7466 + ga_userinput.ga_len);
7467 /* default return is zero == OK */
7468 }
7469 else if (p_verbose > 1)
7470 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007471 verb_msg(_("called inputrestore() more often than inputsave()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007472 rettv->vval.v_number = 1; /* Failed */
7473 }
7474}
7475
7476/*
7477 * "inputsave()" function
7478 */
7479 static void
7480f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
7481{
7482 /* Add an entry to the stack of typeahead storage. */
7483 if (ga_grow(&ga_userinput, 1) == OK)
7484 {
7485 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7486 + ga_userinput.ga_len);
7487 ++ga_userinput.ga_len;
7488 /* default return is zero == OK */
7489 }
7490 else
7491 rettv->vval.v_number = 1; /* Failed */
7492}
7493
7494/*
7495 * "inputsecret()" function
7496 */
7497 static void
7498f_inputsecret(typval_T *argvars, typval_T *rettv)
7499{
7500 ++cmdline_star;
7501 ++inputsecret_flag;
7502 f_input(argvars, rettv);
7503 --cmdline_star;
7504 --inputsecret_flag;
7505}
7506
7507/*
7508 * "insert()" function
7509 */
7510 static void
7511f_insert(typval_T *argvars, typval_T *rettv)
7512{
7513 long before = 0;
7514 listitem_T *item;
7515 list_T *l;
7516 int error = FALSE;
7517
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007518 if (argvars[0].v_type == VAR_BLOB)
7519 {
7520 int val, len;
7521 char_u *p;
7522
7523 len = blob_len(argvars[0].vval.v_blob);
7524 if (argvars[2].v_type != VAR_UNKNOWN)
7525 {
7526 before = (long)tv_get_number_chk(&argvars[2], &error);
7527 if (error)
7528 return; // type error; errmsg already given
7529 if (before < 0 || before > len)
7530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007532 return;
7533 }
7534 }
7535 val = tv_get_number_chk(&argvars[1], &error);
7536 if (error)
7537 return;
7538 if (val < 0 || val > 255)
7539 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007541 return;
7542 }
7543
7544 if (ga_grow(&argvars[0].vval.v_blob->bv_ga, 1) == FAIL)
7545 return;
7546 p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7547 mch_memmove(p + before + 1, p + before, (size_t)len - before);
7548 *(p + before) = val;
7549 ++argvars[0].vval.v_blob->bv_ga.ga_len;
7550
7551 copy_tv(&argvars[0], rettv);
7552 }
7553 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01007554 semsg(_(e_listblobarg), "insert()");
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007555 else if ((l = argvars[0].vval.v_list) != NULL
7556 && !var_check_lock(l->lv_lock,
7557 (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007558 {
7559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007560 before = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007561 if (error)
7562 return; /* type error; errmsg already given */
7563
7564 if (before == l->lv_len)
7565 item = NULL;
7566 else
7567 {
7568 item = list_find(l, before);
7569 if (item == NULL)
7570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007571 semsg(_(e_listidx), before);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 l = NULL;
7573 }
7574 }
7575 if (l != NULL)
7576 {
7577 list_insert_tv(l, &argvars[1], item);
7578 copy_tv(&argvars[0], rettv);
7579 }
7580 }
7581}
7582
7583/*
7584 * "invert(expr)" function
7585 */
7586 static void
7587f_invert(typval_T *argvars, typval_T *rettv)
7588{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007589 rettv->vval.v_number = ~tv_get_number_chk(&argvars[0], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007590}
7591
7592/*
7593 * "isdirectory()" function
7594 */
7595 static void
7596f_isdirectory(typval_T *argvars, typval_T *rettv)
7597{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007598 rettv->vval.v_number = mch_isdir(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007599}
7600
7601/*
7602 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
7603 * or it refers to a List or Dictionary that is locked.
7604 */
7605 static int
7606tv_islocked(typval_T *tv)
7607{
7608 return (tv->v_lock & VAR_LOCKED)
7609 || (tv->v_type == VAR_LIST
7610 && tv->vval.v_list != NULL
7611 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
7612 || (tv->v_type == VAR_DICT
7613 && tv->vval.v_dict != NULL
7614 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
7615}
7616
7617/*
7618 * "islocked()" function
7619 */
7620 static void
7621f_islocked(typval_T *argvars, typval_T *rettv)
7622{
7623 lval_T lv;
7624 char_u *end;
7625 dictitem_T *di;
7626
7627 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007628 end = get_lval(tv_get_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
Bram Moolenaar3a257732017-02-21 20:47:13 +01007629 GLV_NO_AUTOLOAD | GLV_READ_ONLY, FNE_CHECK_START);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007630 if (end != NULL && lv.ll_name != NULL)
7631 {
7632 if (*end != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_(e_trailing));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007634 else
7635 {
7636 if (lv.ll_tv == NULL)
7637 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007638 di = find_var(lv.ll_name, NULL, TRUE);
7639 if (di != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007640 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01007641 /* Consider a variable locked when:
7642 * 1. the variable itself is locked
7643 * 2. the value of the variable is locked.
7644 * 3. the List or Dict value is locked.
7645 */
7646 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
7647 || tv_islocked(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648 }
7649 }
7650 else if (lv.ll_range)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 emsg(_("E786: Range not allowed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007652 else if (lv.ll_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 semsg(_(e_dictkey), lv.ll_newkey);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 else if (lv.ll_list != NULL)
7655 /* List item. */
7656 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
7657 else
7658 /* Dictionary item. */
7659 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
7660 }
7661 }
7662
7663 clear_lval(&lv);
7664}
7665
7666#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
7667/*
Bram Moolenaarfda1bff2019-04-04 13:44:37 +02007668 * "isinf()" function
7669 */
7670 static void
7671f_isinf(typval_T *argvars, typval_T *rettv)
7672{
7673 if (argvars[0].v_type == VAR_FLOAT && isinf(argvars[0].vval.v_float))
7674 rettv->vval.v_number = argvars[0].vval.v_float > 0.0 ? 1 : -1;
7675}
7676
7677/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007678 * "isnan()" function
7679 */
7680 static void
7681f_isnan(typval_T *argvars, typval_T *rettv)
7682{
7683 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
7684 && isnan(argvars[0].vval.v_float);
7685}
7686#endif
7687
7688/*
7689 * "items(dict)" function
7690 */
7691 static void
7692f_items(typval_T *argvars, typval_T *rettv)
7693{
7694 dict_list(argvars, rettv, 2);
7695}
7696
7697#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7698/*
7699 * Get the job from the argument.
7700 * Returns NULL if the job is invalid.
7701 */
7702 static job_T *
7703get_job_arg(typval_T *tv)
7704{
7705 job_T *job;
7706
7707 if (tv->v_type != VAR_JOB)
7708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007709 semsg(_(e_invarg2), tv_get_string(tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007710 return NULL;
7711 }
7712 job = tv->vval.v_job;
7713
7714 if (job == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007715 emsg(_("E916: not a valid job"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007716 return job;
7717}
7718
7719/*
7720 * "job_getchannel()" function
7721 */
7722 static void
7723f_job_getchannel(typval_T *argvars, typval_T *rettv)
7724{
7725 job_T *job = get_job_arg(&argvars[0]);
7726
7727 if (job != NULL)
7728 {
7729 rettv->v_type = VAR_CHANNEL;
7730 rettv->vval.v_channel = job->jv_channel;
7731 if (job->jv_channel != NULL)
7732 ++job->jv_channel->ch_refcount;
7733 }
7734}
7735
7736/*
7737 * "job_info()" function
7738 */
7739 static void
7740f_job_info(typval_T *argvars, typval_T *rettv)
7741{
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007742 if (argvars[0].v_type != VAR_UNKNOWN)
7743 {
7744 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007745
Bram Moolenaare1fc5152018-04-21 19:49:08 +02007746 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
7747 job_info(job, rettv->vval.v_dict);
7748 }
7749 else if (rettv_list_alloc(rettv) == OK)
7750 job_info_all(rettv->vval.v_list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007751}
7752
7753/*
7754 * "job_setoptions()" function
7755 */
7756 static void
7757f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
7758{
7759 job_T *job = get_job_arg(&argvars[0]);
7760 jobopt_T opt;
7761
7762 if (job == NULL)
7763 return;
7764 clear_job_options(&opt);
Bram Moolenaar08d384f2017-08-11 21:51:23 +02007765 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB, 0) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007766 job_set_options(job, &opt);
7767 free_job_options(&opt);
7768}
7769
7770/*
7771 * "job_start()" function
7772 */
7773 static void
7774f_job_start(typval_T *argvars, typval_T *rettv)
7775{
7776 rettv->v_type = VAR_JOB;
7777 if (check_restricted() || check_secure())
7778 return;
Bram Moolenaar493359e2018-06-12 20:25:52 +02007779 rettv->vval.v_job = job_start(argvars, NULL, NULL, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007780}
7781
7782/*
7783 * "job_status()" function
7784 */
7785 static void
7786f_job_status(typval_T *argvars, typval_T *rettv)
7787{
7788 job_T *job = get_job_arg(&argvars[0]);
7789
7790 if (job != NULL)
7791 {
7792 rettv->v_type = VAR_STRING;
7793 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
7794 }
7795}
7796
7797/*
7798 * "job_stop()" function
7799 */
7800 static void
7801f_job_stop(typval_T *argvars, typval_T *rettv)
7802{
7803 job_T *job = get_job_arg(&argvars[0]);
7804
7805 if (job != NULL)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02007806 rettv->vval.v_number = job_stop(job, argvars, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007807}
7808#endif
7809
7810/*
7811 * "join()" function
7812 */
7813 static void
7814f_join(typval_T *argvars, typval_T *rettv)
7815{
7816 garray_T ga;
7817 char_u *sep;
7818
7819 if (argvars[0].v_type != VAR_LIST)
7820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007821 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007822 return;
7823 }
7824 if (argvars[0].vval.v_list == NULL)
7825 return;
7826 if (argvars[1].v_type == VAR_UNKNOWN)
7827 sep = (char_u *)" ";
7828 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007829 sep = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007830
7831 rettv->v_type = VAR_STRING;
7832
7833 if (sep != NULL)
7834 {
7835 ga_init2(&ga, (int)sizeof(char), 80);
7836 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
7837 ga_append(&ga, NUL);
7838 rettv->vval.v_string = (char_u *)ga.ga_data;
7839 }
7840 else
7841 rettv->vval.v_string = NULL;
7842}
7843
7844/*
7845 * "js_decode()" function
7846 */
7847 static void
7848f_js_decode(typval_T *argvars, typval_T *rettv)
7849{
7850 js_read_T reader;
7851
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007852 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007853 reader.js_fill = NULL;
7854 reader.js_used = 0;
7855 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007856 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007857}
7858
7859/*
7860 * "js_encode()" function
7861 */
7862 static void
7863f_js_encode(typval_T *argvars, typval_T *rettv)
7864{
7865 rettv->v_type = VAR_STRING;
7866 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
7867}
7868
7869/*
7870 * "json_decode()" function
7871 */
7872 static void
7873f_json_decode(typval_T *argvars, typval_T *rettv)
7874{
7875 js_read_T reader;
7876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007877 reader.js_buf = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007878 reader.js_fill = NULL;
7879 reader.js_used = 0;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01007880 json_decode_all(&reader, rettv, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007881}
7882
7883/*
7884 * "json_encode()" function
7885 */
7886 static void
7887f_json_encode(typval_T *argvars, typval_T *rettv)
7888{
7889 rettv->v_type = VAR_STRING;
7890 rettv->vval.v_string = json_encode(&argvars[0], 0);
7891}
7892
7893/*
7894 * "keys()" function
7895 */
7896 static void
7897f_keys(typval_T *argvars, typval_T *rettv)
7898{
7899 dict_list(argvars, rettv, 0);
7900}
7901
7902/*
7903 * "last_buffer_nr()" function.
7904 */
7905 static void
7906f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
7907{
7908 int n = 0;
7909 buf_T *buf;
7910
Bram Moolenaar29323592016-07-24 22:04:11 +02007911 FOR_ALL_BUFFERS(buf)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007912 if (n < buf->b_fnum)
7913 n = buf->b_fnum;
7914
7915 rettv->vval.v_number = n;
7916}
7917
7918/*
7919 * "len()" function
7920 */
7921 static void
7922f_len(typval_T *argvars, typval_T *rettv)
7923{
7924 switch (argvars[0].v_type)
7925 {
7926 case VAR_STRING:
7927 case VAR_NUMBER:
7928 rettv->vval.v_number = (varnumber_T)STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007929 tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007930 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007931 case VAR_BLOB:
7932 rettv->vval.v_number = blob_len(argvars[0].vval.v_blob);
7933 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007934 case VAR_LIST:
7935 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
7936 break;
7937 case VAR_DICT:
7938 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
7939 break;
7940 case VAR_UNKNOWN:
7941 case VAR_SPECIAL:
7942 case VAR_FLOAT:
7943 case VAR_FUNC:
7944 case VAR_PARTIAL:
7945 case VAR_JOB:
7946 case VAR_CHANNEL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007947 emsg(_("E701: Invalid type for len()"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007948 break;
7949 }
7950}
7951
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007952 static void
Bram Moolenaar6d721c72017-01-17 16:56:28 +01007953libcall_common(typval_T *argvars UNUSED, typval_T *rettv, int type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007954{
7955#ifdef FEAT_LIBCALL
7956 char_u *string_in;
7957 char_u **string_result;
7958 int nr_result;
7959#endif
7960
7961 rettv->v_type = type;
7962 if (type != VAR_NUMBER)
7963 rettv->vval.v_string = NULL;
7964
7965 if (check_restricted() || check_secure())
7966 return;
7967
7968#ifdef FEAT_LIBCALL
Bram Moolenaar9af41842016-09-25 21:45:05 +02007969 /* The first two args must be strings, otherwise it's meaningless */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007970 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7971 {
7972 string_in = NULL;
7973 if (argvars[2].v_type == VAR_STRING)
7974 string_in = argvars[2].vval.v_string;
7975 if (type == VAR_NUMBER)
7976 string_result = NULL;
7977 else
7978 string_result = &rettv->vval.v_string;
7979 if (mch_libcall(argvars[0].vval.v_string,
7980 argvars[1].vval.v_string,
7981 string_in,
7982 argvars[2].vval.v_number,
7983 string_result,
7984 &nr_result) == OK
7985 && type == VAR_NUMBER)
7986 rettv->vval.v_number = nr_result;
7987 }
7988#endif
7989}
7990
7991/*
7992 * "libcall()" function
7993 */
7994 static void
7995f_libcall(typval_T *argvars, typval_T *rettv)
7996{
7997 libcall_common(argvars, rettv, VAR_STRING);
7998}
7999
8000/*
8001 * "libcallnr()" function
8002 */
8003 static void
8004f_libcallnr(typval_T *argvars, typval_T *rettv)
8005{
8006 libcall_common(argvars, rettv, VAR_NUMBER);
8007}
8008
8009/*
8010 * "line(string)" function
8011 */
8012 static void
8013f_line(typval_T *argvars, typval_T *rettv)
8014{
8015 linenr_T lnum = 0;
8016 pos_T *fp;
8017 int fnum;
8018
8019 fp = var2fpos(&argvars[0], TRUE, &fnum);
8020 if (fp != NULL)
8021 lnum = fp->lnum;
8022 rettv->vval.v_number = lnum;
8023}
8024
8025/*
8026 * "line2byte(lnum)" function
8027 */
8028 static void
8029f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
8030{
8031#ifndef FEAT_BYTEOFF
8032 rettv->vval.v_number = -1;
8033#else
8034 linenr_T lnum;
8035
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008036 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008037 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
8038 rettv->vval.v_number = -1;
8039 else
8040 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8041 if (rettv->vval.v_number >= 0)
8042 ++rettv->vval.v_number;
8043#endif
8044}
8045
8046/*
8047 * "lispindent(lnum)" function
8048 */
8049 static void
8050f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
8051{
8052#ifdef FEAT_LISP
8053 pos_T pos;
8054 linenr_T lnum;
8055
8056 pos = curwin->w_cursor;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008057 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008058 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8059 {
8060 curwin->w_cursor.lnum = lnum;
8061 rettv->vval.v_number = get_lisp_indent();
8062 curwin->w_cursor = pos;
8063 }
8064 else
8065#endif
8066 rettv->vval.v_number = -1;
8067}
8068
8069/*
Bram Moolenaar9d401282019-04-06 13:18:12 +02008070 * "list2str()" function
8071 */
8072 static void
8073f_list2str(typval_T *argvars, typval_T *rettv)
8074{
8075 list_T *l;
8076 listitem_T *li;
8077 garray_T ga;
8078 int utf8 = FALSE;
8079
8080 rettv->v_type = VAR_STRING;
8081 rettv->vval.v_string = NULL;
8082 if (argvars[0].v_type != VAR_LIST)
8083 {
8084 emsg(_(e_invarg));
8085 return;
8086 }
8087
8088 l = argvars[0].vval.v_list;
8089 if (l == NULL)
8090 return; // empty list results in empty string
8091
8092 if (argvars[1].v_type != VAR_UNKNOWN)
8093 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
8094
8095 ga_init2(&ga, 1, 80);
8096 if (has_mbyte || utf8)
8097 {
8098 char_u buf[MB_MAXBYTES + 1];
8099 int (*char2bytes)(int, char_u *);
8100
8101 if (utf8 || enc_utf8)
8102 char2bytes = utf_char2bytes;
8103 else
8104 char2bytes = mb_char2bytes;
8105
8106 for (li = l->lv_first; li != NULL; li = li->li_next)
8107 {
8108 buf[(*char2bytes)(tv_get_number(&li->li_tv), buf)] = NUL;
8109 ga_concat(&ga, buf);
8110 }
8111 ga_append(&ga, NUL);
8112 }
8113 else if (ga_grow(&ga, list_len(l) + 1) == OK)
8114 {
8115 for (li = l->lv_first; li != NULL; li = li->li_next)
8116 ga_append(&ga, tv_get_number(&li->li_tv));
8117 ga_append(&ga, NUL);
8118 }
8119
8120 rettv->v_type = VAR_STRING;
8121 rettv->vval.v_string = ga.ga_data;
8122}
8123
8124/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008125 * "localtime()" function
8126 */
8127 static void
8128f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
8129{
8130 rettv->vval.v_number = (varnumber_T)time(NULL);
8131}
8132
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008133 static void
8134get_maparg(typval_T *argvars, typval_T *rettv, int exact)
8135{
8136 char_u *keys;
8137 char_u *which;
8138 char_u buf[NUMBUFLEN];
8139 char_u *keys_buf = NULL;
8140 char_u *rhs;
8141 int mode;
8142 int abbr = FALSE;
8143 int get_dict = FALSE;
8144 mapblock_T *mp;
8145 int buffer_local;
8146
8147 /* return empty string for failure */
8148 rettv->v_type = VAR_STRING;
8149 rettv->vval.v_string = NULL;
8150
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008151 keys = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008152 if (*keys == NUL)
8153 return;
8154
8155 if (argvars[1].v_type != VAR_UNKNOWN)
8156 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008157 which = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008158 if (argvars[2].v_type != VAR_UNKNOWN)
8159 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008160 abbr = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008161 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008162 get_dict = (int)tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008163 }
8164 }
8165 else
8166 which = (char_u *)"";
8167 if (which == NULL)
8168 return;
8169
8170 mode = get_map_mode(&which, 0);
8171
8172 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
8173 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
8174 vim_free(keys_buf);
8175
8176 if (!get_dict)
8177 {
8178 /* Return a string. */
8179 if (rhs != NULL)
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +02008180 {
8181 if (*rhs == NUL)
8182 rettv->vval.v_string = vim_strsave((char_u *)"<Nop>");
8183 else
8184 rettv->vval.v_string = str2special_save(rhs, FALSE);
8185 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008186
8187 }
8188 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
8189 {
8190 /* Return a dictionary. */
8191 char_u *lhs = str2special_save(mp->m_keys, TRUE);
8192 char_u *mapmode = map_mode_to_chars(mp->m_mode);
8193 dict_T *dict = rettv->vval.v_dict;
8194
Bram Moolenaare0be1672018-07-08 16:50:37 +02008195 dict_add_string(dict, "lhs", lhs);
8196 dict_add_string(dict, "rhs", mp->m_orig_str);
8197 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
8198 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
8199 dict_add_number(dict, "silent", mp->m_silent ? 1L : 0L);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008200 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
8201 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
Bram Moolenaare0be1672018-07-08 16:50:37 +02008202 dict_add_number(dict, "buffer", (long)buffer_local);
8203 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
8204 dict_add_string(dict, "mode", mapmode);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008205
8206 vim_free(lhs);
8207 vim_free(mapmode);
8208 }
8209}
8210
8211#ifdef FEAT_FLOAT
8212/*
8213 * "log()" function
8214 */
8215 static void
8216f_log(typval_T *argvars, typval_T *rettv)
8217{
8218 float_T f = 0.0;
8219
8220 rettv->v_type = VAR_FLOAT;
8221 if (get_float_arg(argvars, &f) == OK)
8222 rettv->vval.v_float = log(f);
8223 else
8224 rettv->vval.v_float = 0.0;
8225}
8226
8227/*
8228 * "log10()" function
8229 */
8230 static void
8231f_log10(typval_T *argvars, typval_T *rettv)
8232{
8233 float_T f = 0.0;
8234
8235 rettv->v_type = VAR_FLOAT;
8236 if (get_float_arg(argvars, &f) == OK)
8237 rettv->vval.v_float = log10(f);
8238 else
8239 rettv->vval.v_float = 0.0;
8240}
8241#endif
8242
8243#ifdef FEAT_LUA
8244/*
8245 * "luaeval()" function
8246 */
8247 static void
8248f_luaeval(typval_T *argvars, typval_T *rettv)
8249{
8250 char_u *str;
8251 char_u buf[NUMBUFLEN];
8252
Bram Moolenaar8c62a082019-02-08 14:34:10 +01008253 if (check_restricted() || check_secure())
8254 return;
8255
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008256 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008257 do_luaeval(str, argvars + 1, rettv);
8258}
8259#endif
8260
8261/*
8262 * "map()" function
8263 */
8264 static void
8265f_map(typval_T *argvars, typval_T *rettv)
8266{
8267 filter_map(argvars, rettv, TRUE);
8268}
8269
8270/*
8271 * "maparg()" function
8272 */
8273 static void
8274f_maparg(typval_T *argvars, typval_T *rettv)
8275{
8276 get_maparg(argvars, rettv, TRUE);
8277}
8278
8279/*
8280 * "mapcheck()" function
8281 */
8282 static void
8283f_mapcheck(typval_T *argvars, typval_T *rettv)
8284{
8285 get_maparg(argvars, rettv, FALSE);
8286}
8287
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008288typedef enum
8289{
8290 MATCH_END, /* matchend() */
8291 MATCH_MATCH, /* match() */
8292 MATCH_STR, /* matchstr() */
8293 MATCH_LIST, /* matchlist() */
8294 MATCH_POS /* matchstrpos() */
8295} matchtype_T;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008296
8297 static void
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008298find_some_match(typval_T *argvars, typval_T *rettv, matchtype_T type)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008299{
8300 char_u *str = NULL;
8301 long len = 0;
8302 char_u *expr = NULL;
8303 char_u *pat;
8304 regmatch_T regmatch;
8305 char_u patbuf[NUMBUFLEN];
8306 char_u strbuf[NUMBUFLEN];
8307 char_u *save_cpo;
8308 long start = 0;
8309 long nth = 1;
8310 colnr_T startcol = 0;
8311 int match = 0;
8312 list_T *l = NULL;
8313 listitem_T *li = NULL;
8314 long idx = 0;
8315 char_u *tofree = NULL;
8316
8317 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8318 save_cpo = p_cpo;
8319 p_cpo = (char_u *)"";
8320
8321 rettv->vval.v_number = -1;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008322 if (type == MATCH_LIST || type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008323 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008324 /* type MATCH_LIST: return empty list when there are no matches.
8325 * type MATCH_POS: return ["", -1, -1, -1] */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008326 if (rettv_list_alloc(rettv) == FAIL)
8327 goto theend;
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008328 if (type == MATCH_POS
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008329 && (list_append_string(rettv->vval.v_list,
8330 (char_u *)"", 0) == FAIL
8331 || list_append_number(rettv->vval.v_list,
8332 (varnumber_T)-1) == FAIL
8333 || list_append_number(rettv->vval.v_list,
8334 (varnumber_T)-1) == FAIL
8335 || list_append_number(rettv->vval.v_list,
8336 (varnumber_T)-1) == FAIL))
8337 {
8338 list_free(rettv->vval.v_list);
8339 rettv->vval.v_list = NULL;
8340 goto theend;
8341 }
8342 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008343 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008344 {
8345 rettv->v_type = VAR_STRING;
8346 rettv->vval.v_string = NULL;
8347 }
8348
8349 if (argvars[0].v_type == VAR_LIST)
8350 {
8351 if ((l = argvars[0].vval.v_list) == NULL)
8352 goto theend;
8353 li = l->lv_first;
8354 }
8355 else
8356 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008357 expr = str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 len = (long)STRLEN(str);
8359 }
8360
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008361 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008362 if (pat == NULL)
8363 goto theend;
8364
8365 if (argvars[2].v_type != VAR_UNKNOWN)
8366 {
8367 int error = FALSE;
8368
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008369 start = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008370 if (error)
8371 goto theend;
8372 if (l != NULL)
8373 {
8374 li = list_find(l, start);
8375 if (li == NULL)
8376 goto theend;
8377 idx = l->lv_idx; /* use the cached index */
8378 }
8379 else
8380 {
8381 if (start < 0)
8382 start = 0;
8383 if (start > len)
8384 goto theend;
8385 /* When "count" argument is there ignore matches before "start",
8386 * otherwise skip part of the string. Differs when pattern is "^"
8387 * or "\<". */
8388 if (argvars[3].v_type != VAR_UNKNOWN)
8389 startcol = start;
8390 else
8391 {
8392 str += start;
8393 len -= start;
8394 }
8395 }
8396
8397 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008398 nth = (long)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008399 if (error)
8400 goto theend;
8401 }
8402
8403 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8404 if (regmatch.regprog != NULL)
8405 {
8406 regmatch.rm_ic = p_ic;
8407
8408 for (;;)
8409 {
8410 if (l != NULL)
8411 {
8412 if (li == NULL)
8413 {
8414 match = FALSE;
8415 break;
8416 }
8417 vim_free(tofree);
8418 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
8419 if (str == NULL)
8420 break;
8421 }
8422
8423 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
8424
8425 if (match && --nth <= 0)
8426 break;
8427 if (l == NULL && !match)
8428 break;
8429
8430 /* Advance to just after the match. */
8431 if (l != NULL)
8432 {
8433 li = li->li_next;
8434 ++idx;
8435 }
8436 else
8437 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008438 startcol = (colnr_T)(regmatch.startp[0]
8439 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008440 if (startcol > (colnr_T)len
8441 || str + startcol <= regmatch.startp[0])
8442 {
8443 match = FALSE;
8444 break;
8445 }
8446 }
8447 }
8448
8449 if (match)
8450 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008451 if (type == MATCH_POS)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008452 {
8453 listitem_T *li1 = rettv->vval.v_list->lv_first;
8454 listitem_T *li2 = li1->li_next;
8455 listitem_T *li3 = li2->li_next;
8456 listitem_T *li4 = li3->li_next;
8457
8458 vim_free(li1->li_tv.vval.v_string);
8459 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
8460 (int)(regmatch.endp[0] - regmatch.startp[0]));
8461 li3->li_tv.vval.v_number =
8462 (varnumber_T)(regmatch.startp[0] - expr);
8463 li4->li_tv.vval.v_number =
8464 (varnumber_T)(regmatch.endp[0] - expr);
8465 if (l != NULL)
8466 li2->li_tv.vval.v_number = (varnumber_T)idx;
8467 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008468 else if (type == MATCH_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008469 {
8470 int i;
8471
8472 /* return list with matched string and submatches */
8473 for (i = 0; i < NSUBEXP; ++i)
8474 {
8475 if (regmatch.endp[i] == NULL)
8476 {
8477 if (list_append_string(rettv->vval.v_list,
8478 (char_u *)"", 0) == FAIL)
8479 break;
8480 }
8481 else if (list_append_string(rettv->vval.v_list,
8482 regmatch.startp[i],
8483 (int)(regmatch.endp[i] - regmatch.startp[i]))
8484 == FAIL)
8485 break;
8486 }
8487 }
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008488 else if (type == MATCH_STR)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008489 {
8490 /* return matched string */
8491 if (l != NULL)
8492 copy_tv(&li->li_tv, rettv);
8493 else
8494 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
8495 (int)(regmatch.endp[0] - regmatch.startp[0]));
8496 }
8497 else if (l != NULL)
8498 rettv->vval.v_number = idx;
8499 else
8500 {
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008501 if (type != MATCH_END)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008502 rettv->vval.v_number =
8503 (varnumber_T)(regmatch.startp[0] - str);
8504 else
8505 rettv->vval.v_number =
8506 (varnumber_T)(regmatch.endp[0] - str);
8507 rettv->vval.v_number += (varnumber_T)(str - expr);
8508 }
8509 }
8510 vim_regfree(regmatch.regprog);
8511 }
8512
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008513theend:
8514 if (type == MATCH_POS && l == NULL && rettv->vval.v_list != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008515 /* matchstrpos() without a list: drop the second item. */
8516 listitem_remove(rettv->vval.v_list,
8517 rettv->vval.v_list->lv_first->li_next);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008518 vim_free(tofree);
8519 p_cpo = save_cpo;
8520}
8521
8522/*
8523 * "match()" function
8524 */
8525 static void
8526f_match(typval_T *argvars, typval_T *rettv)
8527{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008528 find_some_match(argvars, rettv, MATCH_MATCH);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008529}
8530
Bram Moolenaar95e51472018-07-28 16:55:56 +02008531#ifdef FEAT_SEARCH_EXTRA
8532 static int
8533matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
8534{
8535 dictitem_T *di;
8536
8537 if (tv->v_type != VAR_DICT)
8538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 emsg(_(e_dictreq));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008540 return FAIL;
8541 }
8542
8543 if (dict_find(tv->vval.v_dict, (char_u *)"conceal", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01008544 *conceal_char = dict_get_string(tv->vval.v_dict,
Bram Moolenaar95e51472018-07-28 16:55:56 +02008545 (char_u *)"conceal", FALSE);
8546
8547 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
8548 {
Bram Moolenaarbabfcf52018-10-25 13:11:16 +02008549 *win = find_win_by_nr_or_id(&di->di_tv);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008550 if (*win == NULL)
8551 {
Bram Moolenaaraff74912019-03-30 18:11:49 +01008552 emsg(_(e_invalwindow));
Bram Moolenaar95e51472018-07-28 16:55:56 +02008553 return FAIL;
8554 }
8555 }
8556
8557 return OK;
8558}
8559#endif
8560
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008561/*
8562 * "matchadd()" function
8563 */
8564 static void
8565f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8566{
8567#ifdef FEAT_SEARCH_EXTRA
8568 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008569 char_u *grp = tv_get_string_buf_chk(&argvars[0], buf); /* group */
8570 char_u *pat = tv_get_string_buf_chk(&argvars[1], buf); /* pattern */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008571 int prio = 10; /* default priority */
8572 int id = -1;
8573 int error = FALSE;
8574 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008575 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008576
8577 rettv->vval.v_number = -1;
8578
8579 if (grp == NULL || pat == NULL)
8580 return;
8581 if (argvars[2].v_type != VAR_UNKNOWN)
8582 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008583 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008584 if (argvars[3].v_type != VAR_UNKNOWN)
8585 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008586 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008587 if (argvars[4].v_type != VAR_UNKNOWN
8588 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8589 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008590 }
8591 }
8592 if (error == TRUE)
8593 return;
8594 if (id >= 1 && id <= 3)
8595 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008596 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008597 return;
8598 }
8599
Bram Moolenaar95e51472018-07-28 16:55:56 +02008600 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008601 conceal_char);
8602#endif
8603}
8604
8605/*
8606 * "matchaddpos()" function
8607 */
8608 static void
8609f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8610{
8611#ifdef FEAT_SEARCH_EXTRA
8612 char_u buf[NUMBUFLEN];
8613 char_u *group;
8614 int prio = 10;
8615 int id = -1;
8616 int error = FALSE;
8617 list_T *l;
8618 char_u *conceal_char = NULL;
Bram Moolenaar95e51472018-07-28 16:55:56 +02008619 win_T *win = curwin;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008620
8621 rettv->vval.v_number = -1;
8622
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008623 group = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008624 if (group == NULL)
8625 return;
8626
8627 if (argvars[1].v_type != VAR_LIST)
8628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 semsg(_(e_listarg), "matchaddpos()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630 return;
8631 }
8632 l = argvars[1].vval.v_list;
8633 if (l == NULL)
8634 return;
8635
8636 if (argvars[2].v_type != VAR_UNKNOWN)
8637 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008638 prio = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008639 if (argvars[3].v_type != VAR_UNKNOWN)
8640 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008641 id = (int)tv_get_number_chk(&argvars[3], &error);
Bram Moolenaar95e51472018-07-28 16:55:56 +02008642
8643 if (argvars[4].v_type != VAR_UNKNOWN
8644 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
8645 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 }
8647 }
8648 if (error == TRUE)
8649 return;
8650
8651 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
8652 if (id == 1 || id == 2)
8653 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01008654 semsg(_("E798: ID is reserved for \":match\": %d"), id);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008655 return;
8656 }
8657
Bram Moolenaar95e51472018-07-28 16:55:56 +02008658 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008659 conceal_char);
8660#endif
8661}
8662
8663/*
8664 * "matcharg()" function
8665 */
8666 static void
8667f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
8668{
8669 if (rettv_list_alloc(rettv) == OK)
8670 {
8671#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008672 int id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008673 matchitem_T *m;
8674
8675 if (id >= 1 && id <= 3)
8676 {
8677 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
8678 {
8679 list_append_string(rettv->vval.v_list,
8680 syn_id2name(m->hlg_id), -1);
8681 list_append_string(rettv->vval.v_list, m->pattern, -1);
8682 }
8683 else
8684 {
8685 list_append_string(rettv->vval.v_list, NULL, -1);
8686 list_append_string(rettv->vval.v_list, NULL, -1);
8687 }
8688 }
8689#endif
8690 }
8691}
8692
8693/*
8694 * "matchdelete()" function
8695 */
8696 static void
8697f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
8698{
8699#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaaraff74912019-03-30 18:11:49 +01008700 win_T *win = get_optional_window(argvars, 1);
8701
8702 if (win == NULL)
8703 rettv->vval.v_number = -1;
8704 else
8705 rettv->vval.v_number = match_delete(win,
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008706 (int)tv_get_number(&argvars[0]), TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008707#endif
8708}
8709
8710/*
8711 * "matchend()" function
8712 */
8713 static void
8714f_matchend(typval_T *argvars, typval_T *rettv)
8715{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008716 find_some_match(argvars, rettv, MATCH_END);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717}
8718
8719/*
8720 * "matchlist()" function
8721 */
8722 static void
8723f_matchlist(typval_T *argvars, typval_T *rettv)
8724{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008725 find_some_match(argvars, rettv, MATCH_LIST);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008726}
8727
8728/*
8729 * "matchstr()" function
8730 */
8731 static void
8732f_matchstr(typval_T *argvars, typval_T *rettv)
8733{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008734 find_some_match(argvars, rettv, MATCH_STR);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008735}
8736
8737/*
8738 * "matchstrpos()" function
8739 */
8740 static void
8741f_matchstrpos(typval_T *argvars, typval_T *rettv)
8742{
Bram Moolenaar8d9f0ef2017-08-27 13:51:01 +02008743 find_some_match(argvars, rettv, MATCH_POS);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008744}
8745
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008746 static void
8747max_min(typval_T *argvars, typval_T *rettv, int domax)
8748{
8749 varnumber_T n = 0;
8750 varnumber_T i;
8751 int error = FALSE;
8752
8753 if (argvars[0].v_type == VAR_LIST)
8754 {
8755 list_T *l;
8756 listitem_T *li;
8757
8758 l = argvars[0].vval.v_list;
8759 if (l != NULL)
8760 {
8761 li = l->lv_first;
8762 if (li != NULL)
8763 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008764 n = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008765 for (;;)
8766 {
8767 li = li->li_next;
8768 if (li == NULL)
8769 break;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008770 i = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008771 if (domax ? i > n : i < n)
8772 n = i;
8773 }
8774 }
8775 }
8776 }
8777 else if (argvars[0].v_type == VAR_DICT)
8778 {
8779 dict_T *d;
8780 int first = TRUE;
8781 hashitem_T *hi;
8782 int todo;
8783
8784 d = argvars[0].vval.v_dict;
8785 if (d != NULL)
8786 {
8787 todo = (int)d->dv_hashtab.ht_used;
8788 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
8789 {
8790 if (!HASHITEM_EMPTY(hi))
8791 {
8792 --todo;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008793 i = tv_get_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 if (first)
8795 {
8796 n = i;
8797 first = FALSE;
8798 }
8799 else if (domax ? i > n : i < n)
8800 n = i;
8801 }
8802 }
8803 }
8804 }
8805 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008806 semsg(_(e_listdictarg), domax ? "max()" : "min()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008807 rettv->vval.v_number = error ? 0 : n;
8808}
8809
8810/*
8811 * "max()" function
8812 */
8813 static void
8814f_max(typval_T *argvars, typval_T *rettv)
8815{
8816 max_min(argvars, rettv, TRUE);
8817}
8818
8819/*
8820 * "min()" function
8821 */
8822 static void
8823f_min(typval_T *argvars, typval_T *rettv)
8824{
8825 max_min(argvars, rettv, FALSE);
8826}
8827
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008828/*
8829 * Create the directory in which "dir" is located, and higher levels when
8830 * needed.
Bram Moolenaar7860bac2017-04-09 15:03:15 +02008831 * Return OK or FAIL.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008832 */
8833 static int
8834mkdir_recurse(char_u *dir, int prot)
8835{
8836 char_u *p;
8837 char_u *updir;
8838 int r = FAIL;
8839
8840 /* Get end of directory name in "dir".
8841 * We're done when it's "/" or "c:/". */
8842 p = gettail_sep(dir);
8843 if (p <= get_past_head(dir))
8844 return OK;
8845
8846 /* If the directory exists we're done. Otherwise: create it.*/
8847 updir = vim_strnsave(dir, (int)(p - dir));
8848 if (updir == NULL)
8849 return FAIL;
8850 if (mch_isdir(updir))
8851 r = OK;
8852 else if (mkdir_recurse(updir, prot) == OK)
8853 r = vim_mkdir_emsg(updir, prot);
8854 vim_free(updir);
8855 return r;
8856}
8857
8858#ifdef vim_mkdir
8859/*
8860 * "mkdir()" function
8861 */
8862 static void
8863f_mkdir(typval_T *argvars, typval_T *rettv)
8864{
8865 char_u *dir;
8866 char_u buf[NUMBUFLEN];
8867 int prot = 0755;
8868
8869 rettv->vval.v_number = FAIL;
8870 if (check_restricted() || check_secure())
8871 return;
8872
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008873 dir = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008874 if (*dir == NUL)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008875 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008876
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008877 if (*gettail(dir) == NUL)
8878 /* remove trailing slashes */
8879 *gettail_sep(dir) = NUL;
8880
8881 if (argvars[1].v_type != VAR_UNKNOWN)
8882 {
8883 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008884 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008885 prot = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008886 if (prot == -1)
8887 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008888 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008889 if (STRCMP(tv_get_string(&argvars[1]), "p") == 0)
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008890 {
8891 if (mch_isdir(dir))
8892 {
8893 /* With the "p" flag it's OK if the dir already exists. */
8894 rettv->vval.v_number = OK;
8895 return;
8896 }
8897 mkdir_recurse(dir, prot);
8898 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008899 }
Bram Moolenaar78a16b02018-04-14 13:51:55 +02008900 rettv->vval.v_number = vim_mkdir_emsg(dir, prot);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008901}
8902#endif
8903
8904/*
8905 * "mode()" function
8906 */
8907 static void
8908f_mode(typval_T *argvars, typval_T *rettv)
8909{
Bram Moolenaar612cc382018-07-29 15:34:26 +02008910 char_u buf[4];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008911
Bram Moolenaar612cc382018-07-29 15:34:26 +02008912 vim_memset(buf, 0, sizeof(buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008913
8914 if (time_for_testing == 93784)
8915 {
8916 /* Testing the two-character code. */
8917 buf[0] = 'x';
8918 buf[1] = '!';
8919 }
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +02008920#ifdef FEAT_TERMINAL
8921 else if (term_use_loop())
8922 buf[0] = 't';
8923#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008924 else if (VIsual_active)
8925 {
8926 if (VIsual_select)
8927 buf[0] = VIsual_mode + 's' - 'v';
8928 else
8929 buf[0] = VIsual_mode;
8930 }
8931 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
8932 || State == CONFIRM)
8933 {
8934 buf[0] = 'r';
8935 if (State == ASKMORE)
8936 buf[1] = 'm';
8937 else if (State == CONFIRM)
8938 buf[1] = '?';
8939 }
8940 else if (State == EXTERNCMD)
8941 buf[0] = '!';
8942 else if (State & INSERT)
8943 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008944 if (State & VREPLACE_FLAG)
8945 {
8946 buf[0] = 'R';
8947 buf[1] = 'v';
8948 }
8949 else
Bram Moolenaare90858d2017-02-01 17:24:34 +01008950 {
8951 if (State & REPLACE_FLAG)
8952 buf[0] = 'R';
8953 else
8954 buf[0] = 'i';
8955#ifdef FEAT_INS_EXPAND
8956 if (ins_compl_active())
8957 buf[1] = 'c';
Bram Moolenaar05625322018-02-09 12:28:00 +01008958 else if (ctrl_x_mode_not_defined_yet())
Bram Moolenaare90858d2017-02-01 17:24:34 +01008959 buf[1] = 'x';
8960#endif
8961 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008962 }
Bram Moolenaare90858d2017-02-01 17:24:34 +01008963 else if ((State & CMDLINE) || exmode_active)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008964 {
8965 buf[0] = 'c';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008966 if (exmode_active == EXMODE_VIM)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008967 buf[1] = 'v';
Bram Moolenaare90858d2017-02-01 17:24:34 +01008968 else if (exmode_active == EXMODE_NORMAL)
8969 buf[1] = 'e';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008970 }
8971 else
8972 {
8973 buf[0] = 'n';
8974 if (finish_op)
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008975 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008976 buf[1] = 'o';
Bram Moolenaar5976f8f2018-12-27 23:44:44 +01008977 // to be able to detect force-linewise/blockwise/characterwise operations
8978 buf[2] = motion_force;
8979 }
Bram Moolenaar612cc382018-07-29 15:34:26 +02008980 else if (restart_edit == 'I' || restart_edit == 'R'
8981 || restart_edit == 'V')
8982 {
8983 buf[1] = 'i';
8984 buf[2] = restart_edit;
8985 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008986 }
8987
8988 /* Clear out the minor mode when the argument is not a non-zero number or
8989 * non-empty string. */
8990 if (!non_zero_arg(&argvars[0]))
8991 buf[1] = NUL;
8992
8993 rettv->vval.v_string = vim_strsave(buf);
8994 rettv->v_type = VAR_STRING;
8995}
8996
8997#if defined(FEAT_MZSCHEME) || defined(PROTO)
8998/*
8999 * "mzeval()" function
9000 */
9001 static void
9002f_mzeval(typval_T *argvars, typval_T *rettv)
9003{
9004 char_u *str;
9005 char_u buf[NUMBUFLEN];
9006
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009007 if (check_restricted() || check_secure())
9008 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009009 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009010 do_mzeval(str, rettv);
9011}
9012
9013 void
9014mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
9015{
9016 typval_T argvars[3];
9017
9018 argvars[0].v_type = VAR_STRING;
9019 argvars[0].vval.v_string = name;
9020 copy_tv(args, &argvars[1]);
9021 argvars[2].v_type = VAR_UNKNOWN;
9022 f_call(argvars, rettv);
9023 clear_tv(&argvars[1]);
9024}
9025#endif
9026
9027/*
9028 * "nextnonblank()" function
9029 */
9030 static void
9031f_nextnonblank(typval_T *argvars, typval_T *rettv)
9032{
9033 linenr_T lnum;
9034
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009035 for (lnum = tv_get_lnum(argvars); ; ++lnum)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009036 {
9037 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
9038 {
9039 lnum = 0;
9040 break;
9041 }
9042 if (*skipwhite(ml_get(lnum)) != NUL)
9043 break;
9044 }
9045 rettv->vval.v_number = lnum;
9046}
9047
9048/*
9049 * "nr2char()" function
9050 */
9051 static void
9052f_nr2char(typval_T *argvars, typval_T *rettv)
9053{
9054 char_u buf[NUMBUFLEN];
9055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009056 if (has_mbyte)
9057 {
9058 int utf8 = 0;
9059
9060 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009061 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009062 if (utf8)
Bram Moolenaarbdace832019-03-02 10:13:42 +01009063 buf[utf_char2bytes((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009064 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009065 buf[(*mb_char2bytes)((int)tv_get_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009066 }
9067 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009068 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009069 buf[0] = (char_u)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009070 buf[1] = NUL;
9071 }
9072 rettv->v_type = VAR_STRING;
9073 rettv->vval.v_string = vim_strsave(buf);
9074}
9075
9076/*
9077 * "or(expr, expr)" function
9078 */
9079 static void
9080f_or(typval_T *argvars, typval_T *rettv)
9081{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009082 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
9083 | tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009084}
9085
9086/*
9087 * "pathshorten()" function
9088 */
9089 static void
9090f_pathshorten(typval_T *argvars, typval_T *rettv)
9091{
9092 char_u *p;
9093
9094 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009095 p = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009096 if (p == NULL)
9097 rettv->vval.v_string = NULL;
9098 else
9099 {
9100 p = vim_strsave(p);
9101 rettv->vval.v_string = p;
9102 if (p != NULL)
9103 shorten_dir(p);
9104 }
9105}
9106
9107#ifdef FEAT_PERL
9108/*
9109 * "perleval()" function
9110 */
9111 static void
9112f_perleval(typval_T *argvars, typval_T *rettv)
9113{
9114 char_u *str;
9115 char_u buf[NUMBUFLEN];
9116
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009117 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009118 do_perleval(str, rettv);
9119}
9120#endif
9121
9122#ifdef FEAT_FLOAT
9123/*
9124 * "pow()" function
9125 */
9126 static void
9127f_pow(typval_T *argvars, typval_T *rettv)
9128{
9129 float_T fx = 0.0, fy = 0.0;
9130
9131 rettv->v_type = VAR_FLOAT;
9132 if (get_float_arg(argvars, &fx) == OK
9133 && get_float_arg(&argvars[1], &fy) == OK)
9134 rettv->vval.v_float = pow(fx, fy);
9135 else
9136 rettv->vval.v_float = 0.0;
9137}
9138#endif
9139
9140/*
9141 * "prevnonblank()" function
9142 */
9143 static void
9144f_prevnonblank(typval_T *argvars, typval_T *rettv)
9145{
9146 linenr_T lnum;
9147
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009148 lnum = tv_get_lnum(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009149 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9150 lnum = 0;
9151 else
9152 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9153 --lnum;
9154 rettv->vval.v_number = lnum;
9155}
9156
9157/* This dummy va_list is here because:
9158 * - passing a NULL pointer doesn't work when va_list isn't a pointer
9159 * - locally in the function results in a "used before set" warning
9160 * - using va_start() to initialize it gives "function with fixed args" error */
9161static va_list ap;
9162
9163/*
9164 * "printf()" function
9165 */
9166 static void
9167f_printf(typval_T *argvars, typval_T *rettv)
9168{
9169 char_u buf[NUMBUFLEN];
9170 int len;
9171 char_u *s;
9172 int saved_did_emsg = did_emsg;
9173 char *fmt;
9174
9175 rettv->v_type = VAR_STRING;
9176 rettv->vval.v_string = NULL;
9177
9178 /* Get the required length, allocate the buffer and do it for real. */
9179 did_emsg = FALSE;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009180 fmt = (char *)tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009181 len = vim_vsnprintf_typval(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009182 if (!did_emsg)
9183 {
9184 s = alloc(len + 1);
9185 if (s != NULL)
9186 {
9187 rettv->vval.v_string = s;
Bram Moolenaar8327d1d2017-07-11 22:34:51 +02009188 (void)vim_vsnprintf_typval((char *)s, len + 1, fmt,
9189 ap, argvars + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009190 }
9191 }
9192 did_emsg |= saved_did_emsg;
9193}
9194
Bram Moolenaarf2732452018-06-03 14:47:35 +02009195#ifdef FEAT_JOB_CHANNEL
9196/*
9197 * "prompt_setcallback({buffer}, {callback})" function
9198 */
9199 static void
9200f_prompt_setcallback(typval_T *argvars, typval_T *rettv UNUSED)
9201{
9202 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009203 callback_T callback;
Bram Moolenaarf2732452018-06-03 14:47:35 +02009204
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
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009211 callback = get_callback(&argvars[1]);
9212 if (callback.cb_name == NULL)
Bram Moolenaarf2732452018-06-03 14:47:35 +02009213 return;
9214
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009215 free_callback(&buf->b_prompt_callback);
9216 set_callback(&buf->b_prompt_callback, &callback);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009217}
9218
9219/*
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009220 * "prompt_setinterrupt({buffer}, {callback})" function
9221 */
9222 static void
9223f_prompt_setinterrupt(typval_T *argvars, typval_T *rettv UNUSED)
9224{
9225 buf_T *buf;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009226 callback_T callback;
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009227
9228 if (check_secure())
9229 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009230 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009231 if (buf == NULL)
9232 return;
9233
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009234 callback = get_callback(&argvars[1]);
9235 if (callback.cb_name == NULL)
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009236 return;
9237
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02009238 free_callback(&buf->b_prompt_interrupt);
9239 set_callback(&buf->b_prompt_interrupt, &callback);
Bram Moolenaar0e5979a2018-06-17 19:36:33 +02009240}
9241
9242/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02009243 * "prompt_setprompt({buffer}, {text})" function
9244 */
9245 static void
9246f_prompt_setprompt(typval_T *argvars, typval_T *rettv UNUSED)
9247{
9248 buf_T *buf;
9249 char_u *text;
9250
9251 if (check_secure())
9252 return;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01009253 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009254 if (buf == NULL)
9255 return;
9256
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009257 text = tv_get_string(&argvars[1]);
Bram Moolenaarf2732452018-06-03 14:47:35 +02009258 vim_free(buf->b_prompt_text);
9259 buf->b_prompt_text = vim_strsave(text);
9260}
9261#endif
9262
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009263/*
9264 * "pumvisible()" function
9265 */
9266 static void
9267f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9268{
9269#ifdef FEAT_INS_EXPAND
9270 if (pum_visible())
9271 rettv->vval.v_number = 1;
9272#endif
9273}
9274
9275#ifdef FEAT_PYTHON3
9276/*
9277 * "py3eval()" function
9278 */
9279 static void
9280f_py3eval(typval_T *argvars, typval_T *rettv)
9281{
9282 char_u *str;
9283 char_u buf[NUMBUFLEN];
9284
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009285 if (check_restricted() || check_secure())
9286 return;
9287
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009288 if (p_pyx == 0)
9289 p_pyx = 3;
9290
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009291 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009292 do_py3eval(str, rettv);
9293}
9294#endif
9295
9296#ifdef FEAT_PYTHON
9297/*
9298 * "pyeval()" function
9299 */
9300 static void
9301f_pyeval(typval_T *argvars, typval_T *rettv)
9302{
9303 char_u *str;
9304 char_u buf[NUMBUFLEN];
9305
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009306 if (check_restricted() || check_secure())
9307 return;
9308
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009309 if (p_pyx == 0)
9310 p_pyx = 2;
9311
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009312 str = tv_get_string_buf(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009313 do_pyeval(str, rettv);
9314}
9315#endif
9316
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009317#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3)
9318/*
9319 * "pyxeval()" function
9320 */
9321 static void
9322f_pyxeval(typval_T *argvars, typval_T *rettv)
9323{
Bram Moolenaar8c62a082019-02-08 14:34:10 +01009324 if (check_restricted() || check_secure())
9325 return;
9326
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01009327# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
9328 init_pyxversion();
9329 if (p_pyx == 2)
9330 f_pyeval(argvars, rettv);
9331 else
9332 f_py3eval(argvars, rettv);
9333# elif defined(FEAT_PYTHON)
9334 f_pyeval(argvars, rettv);
9335# elif defined(FEAT_PYTHON3)
9336 f_py3eval(argvars, rettv);
9337# endif
9338}
9339#endif
9340
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009341/*
9342 * "range()" function
9343 */
9344 static void
9345f_range(typval_T *argvars, typval_T *rettv)
9346{
9347 varnumber_T start;
9348 varnumber_T end;
9349 varnumber_T stride = 1;
9350 varnumber_T i;
9351 int error = FALSE;
9352
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009353 start = tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009354 if (argvars[1].v_type == VAR_UNKNOWN)
9355 {
9356 end = start - 1;
9357 start = 0;
9358 }
9359 else
9360 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009361 end = tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009362 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009363 stride = tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009364 }
9365
9366 if (error)
9367 return; /* type error; errmsg already given */
9368 if (stride == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009369 emsg(_("E726: Stride is zero"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009370 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009371 emsg(_("E727: Start past end"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009372 else
9373 {
9374 if (rettv_list_alloc(rettv) == OK)
9375 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9376 if (list_append_number(rettv->vval.v_list,
9377 (varnumber_T)i) == FAIL)
9378 break;
9379 }
9380}
9381
9382/*
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009383 * Evaluate "expr" (= "context") for readdir().
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009384 */
9385 static int
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009386readdir_checkitem(void *context, char_u *name)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009387{
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009388 typval_T *expr = (typval_T *)context;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009389 typval_T save_val;
9390 typval_T rettv;
9391 typval_T argv[2];
9392 int retval = 0;
9393 int error = FALSE;
9394
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009395 if (expr->v_type == VAR_UNKNOWN)
9396 return 1;
9397
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009398 prepare_vimvar(VV_VAL, &save_val);
9399 set_vim_var_string(VV_VAL, name, -1);
9400 argv[0].v_type = VAR_STRING;
9401 argv[0].vval.v_string = name;
9402
9403 if (eval_expr_typval(expr, argv, 1, &rettv) == FAIL)
9404 goto theend;
9405
9406 retval = tv_get_number_chk(&rettv, &error);
9407 if (error)
9408 retval = -1;
9409 clear_tv(&rettv);
9410
9411theend:
9412 set_vim_var_string(VV_VAL, NULL, 0);
9413 restore_vimvar(VV_VAL, &save_val);
9414 return retval;
9415}
9416
9417/*
9418 * "readdir()" function
9419 */
9420 static void
9421f_readdir(typval_T *argvars, typval_T *rettv)
9422{
9423 typval_T *expr;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009424 int ret;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009425 char_u *path;
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009426 char_u *p;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009427 garray_T ga;
9428 int i;
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009429
9430 if (rettv_list_alloc(rettv) == FAIL)
9431 return;
9432 path = tv_get_string(&argvars[0]);
9433 expr = &argvars[1];
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009434
Bram Moolenaar701ff0a2019-05-24 14:14:14 +02009435 ret = readdir_core(&ga, path, (void *)expr, readdir_checkitem);
9436 if (ret == OK && rettv->vval.v_list != NULL && ga.ga_len > 0)
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009437 {
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009438 for (i = 0; i < ga.ga_len; i++)
9439 {
9440 p = ((char_u **)ga.ga_data)[i];
9441 list_append_string(rettv->vval.v_list, p, -1);
9442 }
9443 }
Bram Moolenaar334ad412019-04-19 15:20:46 +02009444 ga_clear_strings(&ga);
Bram Moolenaar543c9b12019-04-05 22:50:40 +02009445}
9446
9447/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009448 * "readfile()" function
9449 */
9450 static void
9451f_readfile(typval_T *argvars, typval_T *rettv)
9452{
9453 int binary = FALSE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009454 int blob = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009455 int failed = FALSE;
9456 char_u *fname;
9457 FILE *fd;
9458 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
9459 int io_size = sizeof(buf);
9460 int readlen; /* size of last fread() */
9461 char_u *prev = NULL; /* previously read bytes, if any */
9462 long prevlen = 0; /* length of data in prev */
9463 long prevsize = 0; /* size of prev buffer */
9464 long maxline = MAXLNUM;
9465 long cnt = 0;
9466 char_u *p; /* position in buf */
9467 char_u *start; /* start of current line */
9468
9469 if (argvars[1].v_type != VAR_UNKNOWN)
9470 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009471 if (STRCMP(tv_get_string(&argvars[1]), "b") == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009472 binary = TRUE;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009473 if (STRCMP(tv_get_string(&argvars[1]), "B") == 0)
9474 blob = TRUE;
9475
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009476 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009477 maxline = (long)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009478 }
9479
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009480 if (blob)
9481 {
9482 if (rettv_blob_alloc(rettv) == FAIL)
9483 return;
9484 }
9485 else
9486 {
9487 if (rettv_list_alloc(rettv) == FAIL)
9488 return;
9489 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009490
9491 /* Always open the file in binary mode, library functions have a mind of
9492 * their own about CR-LF conversion. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009493 fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009494 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
9495 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009496 semsg(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009497 return;
9498 }
9499
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009500 if (blob)
9501 {
9502 if (read_blob(fd, rettv->vval.v_blob) == FAIL)
9503 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009504 emsg("cannot read file");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009505 blob_free(rettv->vval.v_blob);
9506 }
9507 fclose(fd);
9508 return;
9509 }
9510
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009511 while (cnt < maxline || maxline < 0)
9512 {
9513 readlen = (int)fread(buf, 1, io_size, fd);
9514
9515 /* This for loop processes what was read, but is also entered at end
9516 * of file so that either:
9517 * - an incomplete line gets written
9518 * - a "binary" file gets an empty line at the end if it ends in a
9519 * newline. */
9520 for (p = buf, start = buf;
9521 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
9522 ++p)
9523 {
9524 if (*p == '\n' || readlen <= 0)
9525 {
9526 listitem_T *li;
9527 char_u *s = NULL;
9528 long_u len = p - start;
9529
9530 /* Finished a line. Remove CRs before NL. */
9531 if (readlen > 0 && !binary)
9532 {
9533 while (len > 0 && start[len - 1] == '\r')
9534 --len;
9535 /* removal may cross back to the "prev" string */
9536 if (len == 0)
9537 while (prevlen > 0 && prev[prevlen - 1] == '\r')
9538 --prevlen;
9539 }
9540 if (prevlen == 0)
9541 s = vim_strnsave(start, (int)len);
9542 else
9543 {
9544 /* Change "prev" buffer to be the right size. This way
9545 * the bytes are only copied once, and very long lines are
9546 * allocated only once. */
9547 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
9548 {
9549 mch_memmove(s + prevlen, start, len);
9550 s[prevlen + len] = NUL;
9551 prev = NULL; /* the list will own the string */
9552 prevlen = prevsize = 0;
9553 }
9554 }
9555 if (s == NULL)
9556 {
9557 do_outofmem_msg((long_u) prevlen + len + 1);
9558 failed = TRUE;
9559 break;
9560 }
9561
9562 if ((li = listitem_alloc()) == NULL)
9563 {
9564 vim_free(s);
9565 failed = TRUE;
9566 break;
9567 }
9568 li->li_tv.v_type = VAR_STRING;
9569 li->li_tv.v_lock = 0;
9570 li->li_tv.vval.v_string = s;
9571 list_append(rettv->vval.v_list, li);
9572
9573 start = p + 1; /* step over newline */
9574 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
9575 break;
9576 }
9577 else if (*p == NUL)
9578 *p = '\n';
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009579 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
9580 * when finding the BF and check the previous two bytes. */
9581 else if (*p == 0xbf && enc_utf8 && !binary)
9582 {
9583 /* Find the two bytes before the 0xbf. If p is at buf, or buf
9584 * + 1, these may be in the "prev" string. */
9585 char_u back1 = p >= buf + 1 ? p[-1]
9586 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
9587 char_u back2 = p >= buf + 2 ? p[-2]
9588 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
9589 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
9590
9591 if (back2 == 0xef && back1 == 0xbb)
9592 {
9593 char_u *dest = p - 2;
9594
9595 /* Usually a BOM is at the beginning of a file, and so at
9596 * the beginning of a line; then we can just step over it.
9597 */
9598 if (start == dest)
9599 start = p + 1;
9600 else
9601 {
9602 /* have to shuffle buf to close gap */
9603 int adjust_prevlen = 0;
9604
9605 if (dest < buf)
9606 {
9607 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
9608 dest = buf;
9609 }
9610 if (readlen > p - buf + 1)
9611 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
9612 readlen -= 3 - adjust_prevlen;
9613 prevlen -= adjust_prevlen;
9614 p = dest - 1;
9615 }
9616 }
9617 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009618 } /* for */
9619
9620 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
9621 break;
9622 if (start < p)
9623 {
9624 /* There's part of a line in buf, store it in "prev". */
9625 if (p - start + prevlen >= prevsize)
9626 {
9627 /* need bigger "prev" buffer */
9628 char_u *newprev;
9629
9630 /* A common use case is ordinary text files and "prev" gets a
9631 * fragment of a line, so the first allocation is made
9632 * small, to avoid repeatedly 'allocing' large and
9633 * 'reallocing' small. */
9634 if (prevsize == 0)
9635 prevsize = (long)(p - start);
9636 else
9637 {
9638 long grow50pc = (prevsize * 3) / 2;
9639 long growmin = (long)((p - start) * 2 + prevlen);
9640 prevsize = grow50pc > growmin ? grow50pc : growmin;
9641 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02009642 newprev = vim_realloc(prev, prevsize);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009643 if (newprev == NULL)
9644 {
9645 do_outofmem_msg((long_u)prevsize);
9646 failed = TRUE;
9647 break;
9648 }
9649 prev = newprev;
9650 }
9651 /* Add the line part to end of "prev". */
9652 mch_memmove(prev + prevlen, start, p - start);
9653 prevlen += (long)(p - start);
9654 }
9655 } /* while */
9656
9657 /*
9658 * For a negative line count use only the lines at the end of the file,
9659 * free the rest.
9660 */
9661 if (!failed && maxline < 0)
9662 while (cnt > -maxline)
9663 {
9664 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
9665 --cnt;
9666 }
9667
9668 if (failed)
9669 {
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009670 // an empty list is returned on error
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009671 list_free(rettv->vval.v_list);
Bram Moolenaar6ed88192019-05-11 18:37:44 +02009672 rettv_list_alloc(rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009673 }
9674
9675 vim_free(prev);
9676 fclose(fd);
9677}
9678
Bram Moolenaar0b6d9112018-05-22 20:35:17 +02009679 static void
9680return_register(int regname, typval_T *rettv)
9681{
9682 char_u buf[2] = {0, 0};
9683
9684 buf[0] = (char_u)regname;
9685 rettv->v_type = VAR_STRING;
9686 rettv->vval.v_string = vim_strsave(buf);
9687}
9688
9689/*
9690 * "reg_executing()" function
9691 */
9692 static void
9693f_reg_executing(typval_T *argvars UNUSED, typval_T *rettv)
9694{
9695 return_register(reg_executing, rettv);
9696}
9697
9698/*
9699 * "reg_recording()" function
9700 */
9701 static void
9702f_reg_recording(typval_T *argvars UNUSED, typval_T *rettv)
9703{
9704 return_register(reg_recording, rettv);
9705}
9706
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009707#if defined(FEAT_RELTIME)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009708/*
9709 * Convert a List to proftime_T.
9710 * Return FAIL when there is something wrong.
9711 */
9712 static int
9713list2proftime(typval_T *arg, proftime_T *tm)
9714{
9715 long n1, n2;
9716 int error = FALSE;
9717
9718 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
9719 || arg->vval.v_list->lv_len != 2)
9720 return FAIL;
9721 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
9722 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009723# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009724 tm->HighPart = n1;
9725 tm->LowPart = n2;
9726# else
9727 tm->tv_sec = n1;
9728 tm->tv_usec = n2;
9729# endif
9730 return error ? FAIL : OK;
9731}
9732#endif /* FEAT_RELTIME */
9733
9734/*
9735 * "reltime()" function
9736 */
9737 static void
9738f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9739{
9740#ifdef FEAT_RELTIME
9741 proftime_T res;
9742 proftime_T start;
9743
9744 if (argvars[0].v_type == VAR_UNKNOWN)
9745 {
9746 /* No arguments: get current time. */
9747 profile_start(&res);
9748 }
9749 else if (argvars[1].v_type == VAR_UNKNOWN)
9750 {
9751 if (list2proftime(&argvars[0], &res) == FAIL)
9752 return;
9753 profile_end(&res);
9754 }
9755 else
9756 {
9757 /* Two arguments: compute the difference. */
9758 if (list2proftime(&argvars[0], &start) == FAIL
9759 || list2proftime(&argvars[1], &res) == FAIL)
9760 return;
9761 profile_sub(&res, &start);
9762 }
9763
9764 if (rettv_list_alloc(rettv) == OK)
9765 {
9766 long n1, n2;
9767
Bram Moolenaar4f974752019-02-17 17:44:42 +01009768# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009769 n1 = res.HighPart;
9770 n2 = res.LowPart;
9771# else
9772 n1 = res.tv_sec;
9773 n2 = res.tv_usec;
9774# endif
9775 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
9776 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
9777 }
9778#endif
9779}
9780
9781#ifdef FEAT_FLOAT
9782/*
9783 * "reltimefloat()" function
9784 */
9785 static void
9786f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
9787{
9788# ifdef FEAT_RELTIME
9789 proftime_T tm;
9790# endif
9791
9792 rettv->v_type = VAR_FLOAT;
9793 rettv->vval.v_float = 0;
9794# ifdef FEAT_RELTIME
9795 if (list2proftime(&argvars[0], &tm) == OK)
9796 rettv->vval.v_float = profile_float(&tm);
9797# endif
9798}
9799#endif
9800
9801/*
9802 * "reltimestr()" function
9803 */
9804 static void
9805f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
9806{
9807#ifdef FEAT_RELTIME
9808 proftime_T tm;
9809#endif
9810
9811 rettv->v_type = VAR_STRING;
9812 rettv->vval.v_string = NULL;
9813#ifdef FEAT_RELTIME
9814 if (list2proftime(&argvars[0], &tm) == OK)
9815 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
9816#endif
9817}
9818
9819#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009820 static void
9821make_connection(void)
9822{
9823 if (X_DISPLAY == NULL
9824# ifdef FEAT_GUI
9825 && !gui.in_use
9826# endif
9827 )
9828 {
9829 x_force_connect = TRUE;
9830 setup_term_clip();
9831 x_force_connect = FALSE;
9832 }
9833}
9834
9835 static int
9836check_connection(void)
9837{
9838 make_connection();
9839 if (X_DISPLAY == NULL)
9840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009841 emsg(_("E240: No connection to the X server"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009842 return FAIL;
9843 }
9844 return OK;
9845}
9846#endif
9847
9848#ifdef FEAT_CLIENTSERVER
9849 static void
9850remote_common(typval_T *argvars, typval_T *rettv, int expr)
9851{
9852 char_u *server_name;
9853 char_u *keys;
9854 char_u *r = NULL;
9855 char_u buf[NUMBUFLEN];
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009856 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009857# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009858 HWND w;
9859# else
9860 Window w;
9861# endif
9862
9863 if (check_restricted() || check_secure())
9864 return;
9865
9866# ifdef FEAT_X11
9867 if (check_connection() == FAIL)
9868 return;
9869# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009870 if (argvars[2].v_type != VAR_UNKNOWN
9871 && argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009872 timeout = tv_get_number(&argvars[3]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009873
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009874 server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009875 if (server_name == NULL)
9876 return; /* type error; errmsg already given */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009877 keys = tv_get_string_buf(&argvars[1], buf);
Bram Moolenaar4f974752019-02-17 17:44:42 +01009878# ifdef MSWIN
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009879 if (serverSendToVim(server_name, keys, &r, &w, expr, timeout, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009880# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009881 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, timeout,
9882 0, TRUE) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009883# endif
9884 {
9885 if (r != NULL)
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009887 emsg((char *)r); // sending worked but evaluation failed
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009888 vim_free(r);
9889 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009890 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009891 semsg(_("E241: Unable to send to %s"), server_name);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009892 return;
9893 }
9894
9895 rettv->vval.v_string = r;
9896
9897 if (argvars[2].v_type != VAR_UNKNOWN)
9898 {
9899 dictitem_T v;
9900 char_u str[30];
9901 char_u *idvar;
9902
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009903 idvar = tv_get_string_chk(&argvars[2]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009904 if (idvar != NULL && *idvar != NUL)
9905 {
9906 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
9907 v.di_tv.v_type = VAR_STRING;
9908 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009909 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009910 vim_free(v.di_tv.vval.v_string);
9911 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009912 }
9913}
9914#endif
9915
9916/*
9917 * "remote_expr()" function
9918 */
9919 static void
9920f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
9921{
9922 rettv->v_type = VAR_STRING;
9923 rettv->vval.v_string = NULL;
9924#ifdef FEAT_CLIENTSERVER
9925 remote_common(argvars, rettv, TRUE);
9926#endif
9927}
9928
9929/*
9930 * "remote_foreground()" function
9931 */
9932 static void
9933f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
9934{
9935#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +01009936# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009937 /* On Win32 it's done in this application. */
9938 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009939 char_u *server_name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009940
9941 if (server_name != NULL)
9942 serverForeground(server_name);
9943 }
9944# else
9945 /* Send a foreground() expression to the server. */
9946 argvars[1].v_type = VAR_STRING;
9947 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9948 argvars[2].v_type = VAR_UNKNOWN;
Bram Moolenaar09d6c382017-09-09 16:25:54 +02009949 rettv->v_type = VAR_STRING;
9950 rettv->vval.v_string = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009951 remote_common(argvars, rettv, TRUE);
9952 vim_free(argvars[1].vval.v_string);
9953# endif
9954#endif
9955}
9956
9957 static void
9958f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
9959{
9960#ifdef FEAT_CLIENTSERVER
9961 dictitem_T v;
9962 char_u *s = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01009963# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009964 long_u n = 0;
9965# endif
9966 char_u *serverid;
9967
9968 if (check_restricted() || check_secure())
9969 {
9970 rettv->vval.v_number = -1;
9971 return;
9972 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009973 serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009974 if (serverid == NULL)
9975 {
9976 rettv->vval.v_number = -1;
9977 return; /* type error; errmsg already given */
9978 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01009979# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009980 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
9981 if (n == 0)
9982 rettv->vval.v_number = -1;
9983 else
9984 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01009985 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE, 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009986 rettv->vval.v_number = (s != NULL);
9987 }
9988# else
9989 if (check_connection() == FAIL)
9990 return;
9991
9992 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9993 serverStrToWin(serverid), &s);
9994# endif
9995
9996 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9997 {
9998 char_u *retvar;
9999
10000 v.di_tv.v_type = VAR_STRING;
10001 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010002 retvar = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010003 if (retvar != NULL)
10004 set_var(retvar, &v.di_tv, FALSE);
10005 vim_free(v.di_tv.vval.v_string);
10006 }
10007#else
10008 rettv->vval.v_number = -1;
10009#endif
10010}
10011
10012 static void
10013f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
10014{
10015 char_u *r = NULL;
10016
10017#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010018 char_u *serverid = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010019
10020 if (serverid != NULL && !check_restricted() && !check_secure())
10021 {
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010022 int timeout = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010023# ifdef MSWIN
Bram Moolenaar1662ce12017-03-19 21:47:50 +010010024 /* The server's HWND is encoded in the 'id' parameter */
10025 long_u n = 0;
10026# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010027
10028 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010029 timeout = tv_get_number(&argvars[1]);
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010030
Bram Moolenaar4f974752019-02-17 17:44:42 +010010031# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010032 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
10033 if (n != 0)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010034 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE, timeout);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010035 if (r == NULL)
10036# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +010010037 if (check_connection() == FAIL
10038 || serverReadReply(X_DISPLAY, serverStrToWin(serverid),
10039 &r, FALSE, timeout) < 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010040# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010041 emsg(_("E277: Unable to read a server reply"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010042 }
10043#endif
10044 rettv->v_type = VAR_STRING;
10045 rettv->vval.v_string = r;
10046}
10047
10048/*
10049 * "remote_send()" function
10050 */
10051 static void
10052f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
10053{
10054 rettv->v_type = VAR_STRING;
10055 rettv->vval.v_string = NULL;
10056#ifdef FEAT_CLIENTSERVER
10057 remote_common(argvars, rettv, FALSE);
10058#endif
10059}
10060
10061/*
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010062 * "remote_startserver()" function
10063 */
10064 static void
10065f_remote_startserver(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
10066{
10067#ifdef FEAT_CLIENTSERVER
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010068 char_u *server = tv_get_string_chk(&argvars[0]);
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010069
10070 if (server == NULL)
10071 return; /* type error; errmsg already given */
10072 if (serverName != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010073 emsg(_("E941: already started a server"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010074 else
10075 {
10076# ifdef FEAT_X11
10077 if (check_connection() == OK)
10078 serverRegisterName(X_DISPLAY, server);
10079# else
10080 serverSetName(server);
10081# endif
10082 }
10083#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010084 emsg(_("E942: +clientserver feature not available"));
Bram Moolenaar7416f3e2017-03-18 18:10:13 +010010085#endif
10086}
10087
10088/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010089 * "remove()" function
10090 */
10091 static void
10092f_remove(typval_T *argvars, typval_T *rettv)
10093{
10094 list_T *l;
10095 listitem_T *item, *item2;
10096 listitem_T *li;
10097 long idx;
10098 long end;
10099 char_u *key;
10100 dict_T *d;
10101 dictitem_T *di;
10102 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010103 int error = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010104
10105 if (argvars[0].v_type == VAR_DICT)
10106 {
10107 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010108 semsg(_(e_toomanyarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010109 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010110 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010111 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010112 key = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010113 if (key != NULL)
10114 {
10115 di = dict_find(d, key, -1);
10116 if (di == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010117 semsg(_(e_dictkey), key);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010118 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10119 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
10120 {
10121 *rettv = di->di_tv;
10122 init_tv(&di->di_tv);
10123 dictitem_remove(d, di);
10124 }
10125 }
10126 }
10127 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010128 else if (argvars[0].v_type == VAR_BLOB)
10129 {
10130 idx = (long)tv_get_number_chk(&argvars[1], &error);
10131 if (!error)
10132 {
10133 blob_T *b = argvars[0].vval.v_blob;
10134 int len = blob_len(b);
10135 char_u *p;
10136
10137 if (idx < 0)
10138 // count from the end
10139 idx = len + idx;
10140 if (idx < 0 || idx >= len)
10141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010142 semsg(_(e_blobidx), idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010143 return;
10144 }
10145 if (argvars[2].v_type == VAR_UNKNOWN)
10146 {
10147 // Remove one item, return its value.
10148 p = (char_u *)b->bv_ga.ga_data;
10149 rettv->vval.v_number = (varnumber_T) *(p + idx);
10150 mch_memmove(p + idx, p + idx + 1, (size_t)len - idx - 1);
10151 --b->bv_ga.ga_len;
10152 }
10153 else
10154 {
10155 blob_T *blob;
10156
10157 // Remove range of items, return list with values.
10158 end = (long)tv_get_number_chk(&argvars[2], &error);
10159 if (error)
10160 return;
10161 if (end < 0)
10162 // count from the end
10163 end = len + end;
10164 if (end >= len || idx > end)
10165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010166 semsg(_(e_blobidx), end);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010167 return;
10168 }
10169 blob = blob_alloc();
10170 if (blob == NULL)
10171 return;
10172 blob->bv_ga.ga_len = end - idx + 1;
10173 if (ga_grow(&blob->bv_ga, end - idx + 1) == FAIL)
10174 {
10175 vim_free(blob);
10176 return;
10177 }
10178 p = (char_u *)b->bv_ga.ga_data;
10179 mch_memmove((char_u *)blob->bv_ga.ga_data, p + idx,
10180 (size_t)(end - idx + 1));
10181 ++blob->bv_refcount;
10182 rettv->v_type = VAR_BLOB;
10183 rettv->vval.v_blob = blob;
10184
10185 mch_memmove(p + idx, p + end + 1, (size_t)(len - end));
10186 b->bv_ga.ga_len -= end - idx + 1;
10187 }
10188 }
10189 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010190 else if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010191 semsg(_(e_listdictblobarg), "remove()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010192 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010193 && !var_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010194 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010195 idx = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010196 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010197 ; // type error: do nothing, errmsg already given
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010198 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010199 semsg(_(e_listidx), idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010200 else
10201 {
10202 if (argvars[2].v_type == VAR_UNKNOWN)
10203 {
10204 /* Remove one item, return its value. */
10205 vimlist_remove(l, item, item);
10206 *rettv = item->li_tv;
10207 vim_free(item);
10208 }
10209 else
10210 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010211 // Remove range of items, return list with values.
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010212 end = (long)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010213 if (error)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010214 ; // type error: do nothing
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010215 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010216 semsg(_(e_listidx), end);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010217 else
10218 {
10219 int cnt = 0;
10220
10221 for (li = item; li != NULL; li = li->li_next)
10222 {
10223 ++cnt;
10224 if (li == item2)
10225 break;
10226 }
10227 if (li == NULL) /* didn't find "item2" after "item" */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010228 emsg(_(e_invrange));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010229 else
10230 {
10231 vimlist_remove(l, item, item2);
10232 if (rettv_list_alloc(rettv) == OK)
10233 {
10234 l = rettv->vval.v_list;
10235 l->lv_first = item;
10236 l->lv_last = item2;
10237 item->li_prev = NULL;
10238 item2->li_next = NULL;
10239 l->lv_len = cnt;
10240 }
10241 }
10242 }
10243 }
10244 }
10245 }
10246}
10247
10248/*
10249 * "rename({from}, {to})" function
10250 */
10251 static void
10252f_rename(typval_T *argvars, typval_T *rettv)
10253{
10254 char_u buf[NUMBUFLEN];
10255
10256 if (check_restricted() || check_secure())
10257 rettv->vval.v_number = -1;
10258 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010259 rettv->vval.v_number = vim_rename(tv_get_string(&argvars[0]),
10260 tv_get_string_buf(&argvars[1], buf));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010261}
10262
10263/*
10264 * "repeat()" function
10265 */
10266 static void
10267f_repeat(typval_T *argvars, typval_T *rettv)
10268{
10269 char_u *p;
10270 int n;
10271 int slen;
10272 int len;
10273 char_u *r;
10274 int i;
10275
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010276 n = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010277 if (argvars[0].v_type == VAR_LIST)
10278 {
10279 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
10280 while (n-- > 0)
10281 if (list_extend(rettv->vval.v_list,
10282 argvars[0].vval.v_list, NULL) == FAIL)
10283 break;
10284 }
10285 else
10286 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010287 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010288 rettv->v_type = VAR_STRING;
10289 rettv->vval.v_string = NULL;
10290
10291 slen = (int)STRLEN(p);
10292 len = slen * n;
10293 if (len <= 0)
10294 return;
10295
10296 r = alloc(len + 1);
10297 if (r != NULL)
10298 {
10299 for (i = 0; i < n; i++)
10300 mch_memmove(r + i * slen, p, (size_t)slen);
10301 r[len] = NUL;
10302 }
10303
10304 rettv->vval.v_string = r;
10305 }
10306}
10307
10308/*
10309 * "resolve()" function
10310 */
10311 static void
10312f_resolve(typval_T *argvars, typval_T *rettv)
10313{
10314 char_u *p;
10315#ifdef HAVE_READLINK
10316 char_u *buf = NULL;
10317#endif
10318
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010319 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010320#ifdef FEAT_SHORTCUT
10321 {
10322 char_u *v = NULL;
10323
Bram Moolenaardce1e892019-02-10 23:18:53 +010010324 v = mch_resolve_path(p, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010325 if (v != NULL)
10326 rettv->vval.v_string = v;
10327 else
10328 rettv->vval.v_string = vim_strsave(p);
10329 }
10330#else
10331# ifdef HAVE_READLINK
10332 {
10333 char_u *cpy;
10334 int len;
10335 char_u *remain = NULL;
10336 char_u *q;
10337 int is_relative_to_current = FALSE;
10338 int has_trailing_pathsep = FALSE;
10339 int limit = 100;
10340
10341 p = vim_strsave(p);
10342
10343 if (p[0] == '.' && (vim_ispathsep(p[1])
10344 || (p[1] == '.' && (vim_ispathsep(p[2])))))
10345 is_relative_to_current = TRUE;
10346
10347 len = STRLEN(p);
10348 if (len > 0 && after_pathsep(p, p + len))
10349 {
10350 has_trailing_pathsep = TRUE;
10351 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
10352 }
10353
10354 q = getnextcomp(p);
10355 if (*q != NUL)
10356 {
10357 /* Separate the first path component in "p", and keep the
10358 * remainder (beginning with the path separator). */
10359 remain = vim_strsave(q - 1);
10360 q[-1] = NUL;
10361 }
10362
10363 buf = alloc(MAXPATHL + 1);
10364 if (buf == NULL)
10365 goto fail;
10366
10367 for (;;)
10368 {
10369 for (;;)
10370 {
10371 len = readlink((char *)p, (char *)buf, MAXPATHL);
10372 if (len <= 0)
10373 break;
10374 buf[len] = NUL;
10375
10376 if (limit-- == 0)
10377 {
10378 vim_free(p);
10379 vim_free(remain);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010380 emsg(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010381 rettv->vval.v_string = NULL;
10382 goto fail;
10383 }
10384
10385 /* Ensure that the result will have a trailing path separator
10386 * if the argument has one. */
10387 if (remain == NULL && has_trailing_pathsep)
10388 add_pathsep(buf);
10389
10390 /* Separate the first path component in the link value and
10391 * concatenate the remainders. */
10392 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
10393 if (*q != NUL)
10394 {
10395 if (remain == NULL)
10396 remain = vim_strsave(q - 1);
10397 else
10398 {
10399 cpy = concat_str(q - 1, remain);
10400 if (cpy != NULL)
10401 {
10402 vim_free(remain);
10403 remain = cpy;
10404 }
10405 }
10406 q[-1] = NUL;
10407 }
10408
10409 q = gettail(p);
10410 if (q > p && *q == NUL)
10411 {
10412 /* Ignore trailing path separator. */
10413 q[-1] = NUL;
10414 q = gettail(p);
10415 }
10416 if (q > p && !mch_isFullName(buf))
10417 {
10418 /* symlink is relative to directory of argument */
Bram Moolenaar964b3742019-05-24 18:54:09 +020010419 cpy = alloc(STRLEN(p) + STRLEN(buf) + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010420 if (cpy != NULL)
10421 {
10422 STRCPY(cpy, p);
10423 STRCPY(gettail(cpy), buf);
10424 vim_free(p);
10425 p = cpy;
10426 }
10427 }
10428 else
10429 {
10430 vim_free(p);
10431 p = vim_strsave(buf);
10432 }
10433 }
10434
10435 if (remain == NULL)
10436 break;
10437
10438 /* Append the first path component of "remain" to "p". */
10439 q = getnextcomp(remain + 1);
10440 len = q - remain - (*q != NUL);
10441 cpy = vim_strnsave(p, STRLEN(p) + len);
10442 if (cpy != NULL)
10443 {
10444 STRNCAT(cpy, remain, len);
10445 vim_free(p);
10446 p = cpy;
10447 }
10448 /* Shorten "remain". */
10449 if (*q != NUL)
10450 STRMOVE(remain, q - 1);
10451 else
Bram Moolenaard23a8232018-02-10 18:45:26 +010010452 VIM_CLEAR(remain);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010453 }
10454
10455 /* If the result is a relative path name, make it explicitly relative to
10456 * the current directory if and only if the argument had this form. */
10457 if (!vim_ispathsep(*p))
10458 {
10459 if (is_relative_to_current
10460 && *p != NUL
10461 && !(p[0] == '.'
10462 && (p[1] == NUL
10463 || vim_ispathsep(p[1])
10464 || (p[1] == '.'
10465 && (p[2] == NUL
10466 || vim_ispathsep(p[2]))))))
10467 {
10468 /* Prepend "./". */
10469 cpy = concat_str((char_u *)"./", p);
10470 if (cpy != NULL)
10471 {
10472 vim_free(p);
10473 p = cpy;
10474 }
10475 }
10476 else if (!is_relative_to_current)
10477 {
10478 /* Strip leading "./". */
10479 q = p;
10480 while (q[0] == '.' && vim_ispathsep(q[1]))
10481 q += 2;
10482 if (q > p)
10483 STRMOVE(p, p + 2);
10484 }
10485 }
10486
10487 /* Ensure that the result will have no trailing path separator
10488 * if the argument had none. But keep "/" or "//". */
10489 if (!has_trailing_pathsep)
10490 {
10491 q = p + STRLEN(p);
10492 if (after_pathsep(p, q))
10493 *gettail_sep(p) = NUL;
10494 }
10495
10496 rettv->vval.v_string = p;
10497 }
10498# else
10499 rettv->vval.v_string = vim_strsave(p);
10500# endif
10501#endif
10502
10503 simplify_filename(rettv->vval.v_string);
10504
10505#ifdef HAVE_READLINK
10506fail:
10507 vim_free(buf);
10508#endif
10509 rettv->v_type = VAR_STRING;
10510}
10511
10512/*
10513 * "reverse({list})" function
10514 */
10515 static void
10516f_reverse(typval_T *argvars, typval_T *rettv)
10517{
10518 list_T *l;
10519 listitem_T *li, *ni;
10520
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010521 if (argvars[0].v_type == VAR_BLOB)
10522 {
10523 blob_T *b = argvars[0].vval.v_blob;
10524 int i, len = blob_len(b);
10525
10526 for (i = 0; i < len / 2; i++)
10527 {
10528 int tmp = blob_get(b, i);
10529
10530 blob_set(b, i, blob_get(b, len - i - 1));
10531 blob_set(b, len - i - 1, tmp);
10532 }
10533 rettv_blob_set(rettv, b);
10534 return;
10535 }
10536
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010537 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +010010538 semsg(_(e_listblobarg), "reverse()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010539 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010540 && !var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010541 (char_u *)N_("reverse() argument"), TRUE))
10542 {
10543 li = l->lv_last;
10544 l->lv_first = l->lv_last = NULL;
10545 l->lv_len = 0;
10546 while (li != NULL)
10547 {
10548 ni = li->li_prev;
10549 list_append(l, li);
10550 li = ni;
10551 }
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020010552 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010553 l->lv_idx = l->lv_len - l->lv_idx - 1;
10554 }
10555}
10556
10557#define SP_NOMOVE 0x01 /* don't move cursor */
10558#define SP_REPEAT 0x02 /* repeat to find outer pair */
10559#define SP_RETCOUNT 0x04 /* return matchcount */
10560#define SP_SETPCMARK 0x08 /* set previous context mark */
10561#define SP_START 0x10 /* accept match at start position */
10562#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
10563#define SP_END 0x40 /* leave cursor at end of match */
10564#define SP_COLUMN 0x80 /* start at cursor column */
10565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010566/*
10567 * Get flags for a search function.
10568 * Possibly sets "p_ws".
10569 * Returns BACKWARD, FORWARD or zero (for an error).
10570 */
10571 static int
10572get_search_arg(typval_T *varp, int *flagsp)
10573{
10574 int dir = FORWARD;
10575 char_u *flags;
10576 char_u nbuf[NUMBUFLEN];
10577 int mask;
10578
10579 if (varp->v_type != VAR_UNKNOWN)
10580 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010581 flags = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010582 if (flags == NULL)
10583 return 0; /* type error; errmsg already given */
10584 while (*flags != NUL)
10585 {
10586 switch (*flags)
10587 {
10588 case 'b': dir = BACKWARD; break;
10589 case 'w': p_ws = TRUE; break;
10590 case 'W': p_ws = FALSE; break;
10591 default: mask = 0;
10592 if (flagsp != NULL)
10593 switch (*flags)
10594 {
10595 case 'c': mask = SP_START; break;
10596 case 'e': mask = SP_END; break;
10597 case 'm': mask = SP_RETCOUNT; break;
10598 case 'n': mask = SP_NOMOVE; break;
10599 case 'p': mask = SP_SUBPAT; break;
10600 case 'r': mask = SP_REPEAT; break;
10601 case 's': mask = SP_SETPCMARK; break;
10602 case 'z': mask = SP_COLUMN; break;
10603 }
10604 if (mask == 0)
10605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010606 semsg(_(e_invarg2), flags);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010607 dir = 0;
10608 }
10609 else
10610 *flagsp |= mask;
10611 }
10612 if (dir == 0)
10613 break;
10614 ++flags;
10615 }
10616 }
10617 return dir;
10618}
10619
10620/*
10621 * Shared by search() and searchpos() functions.
10622 */
10623 static int
10624search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
10625{
10626 int flags;
10627 char_u *pat;
10628 pos_T pos;
10629 pos_T save_cursor;
10630 int save_p_ws = p_ws;
10631 int dir;
10632 int retval = 0; /* default: FAIL */
10633 long lnum_stop = 0;
10634 proftime_T tm;
10635#ifdef FEAT_RELTIME
10636 long time_limit = 0;
10637#endif
10638 int options = SEARCH_KEEP;
10639 int subpatnum;
10640
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010641 pat = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010642 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
10643 if (dir == 0)
10644 goto theend;
10645 flags = *flagsp;
10646 if (flags & SP_START)
10647 options |= SEARCH_START;
10648 if (flags & SP_END)
10649 options |= SEARCH_END;
10650 if (flags & SP_COLUMN)
10651 options |= SEARCH_COL;
10652
10653 /* Optional arguments: line number to stop searching and timeout. */
10654 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
10655 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010656 lnum_stop = (long)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010657 if (lnum_stop < 0)
10658 goto theend;
10659#ifdef FEAT_RELTIME
10660 if (argvars[3].v_type != VAR_UNKNOWN)
10661 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010662 time_limit = (long)tv_get_number_chk(&argvars[3], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010663 if (time_limit < 0)
10664 goto theend;
10665 }
10666#endif
10667 }
10668
10669#ifdef FEAT_RELTIME
10670 /* Set the time limit, if there is one. */
10671 profile_setlimit(time_limit, &tm);
10672#endif
10673
10674 /*
10675 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
10676 * Check to make sure only those flags are set.
10677 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
10678 * flags cannot be set. Check for that condition also.
10679 */
10680 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
10681 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010683 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010684 goto theend;
10685 }
10686
10687 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar5d24a222018-12-23 19:10:09 +010010688 subpatnum = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020010689 options, RE_SEARCH, (linenr_T)lnum_stop, &tm, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010690 if (subpatnum != FAIL)
10691 {
10692 if (flags & SP_SUBPAT)
10693 retval = subpatnum;
10694 else
10695 retval = pos.lnum;
10696 if (flags & SP_SETPCMARK)
10697 setpcmark();
10698 curwin->w_cursor = pos;
10699 if (match_pos != NULL)
10700 {
10701 /* Store the match cursor position */
10702 match_pos->lnum = pos.lnum;
10703 match_pos->col = pos.col + 1;
10704 }
10705 /* "/$" will put the cursor after the end of the line, may need to
10706 * correct that here */
10707 check_cursor();
10708 }
10709
10710 /* If 'n' flag is used: restore cursor position. */
10711 if (flags & SP_NOMOVE)
10712 curwin->w_cursor = save_cursor;
10713 else
10714 curwin->w_set_curswant = TRUE;
10715theend:
10716 p_ws = save_p_ws;
10717
10718 return retval;
10719}
10720
10721#ifdef FEAT_FLOAT
10722
10723/*
10724 * round() is not in C90, use ceil() or floor() instead.
10725 */
10726 float_T
10727vim_round(float_T f)
10728{
10729 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
10730}
10731
10732/*
10733 * "round({float})" function
10734 */
10735 static void
10736f_round(typval_T *argvars, typval_T *rettv)
10737{
10738 float_T f = 0.0;
10739
10740 rettv->v_type = VAR_FLOAT;
10741 if (get_float_arg(argvars, &f) == OK)
10742 rettv->vval.v_float = vim_round(f);
10743 else
10744 rettv->vval.v_float = 0.0;
10745}
10746#endif
10747
Bram Moolenaare99be0e2019-03-26 22:51:09 +010010748#ifdef FEAT_RUBY
10749/*
10750 * "rubyeval()" function
10751 */
10752 static void
10753f_rubyeval(typval_T *argvars, typval_T *rettv)
10754{
10755 char_u *str;
10756 char_u buf[NUMBUFLEN];
10757
10758 str = tv_get_string_buf(&argvars[0], buf);
10759 do_rubyeval(str, rettv);
10760}
10761#endif
10762
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010763/*
10764 * "screenattr()" function
10765 */
10766 static void
10767f_screenattr(typval_T *argvars, typval_T *rettv)
10768{
10769 int row;
10770 int col;
10771 int c;
10772
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010773 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10774 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010775 if (row < 0 || row >= screen_Rows
10776 || col < 0 || col >= screen_Columns)
10777 c = -1;
10778 else
10779 c = ScreenAttrs[LineOffset[row] + col];
10780 rettv->vval.v_number = c;
10781}
10782
10783/*
10784 * "screenchar()" function
10785 */
10786 static void
10787f_screenchar(typval_T *argvars, typval_T *rettv)
10788{
10789 int row;
10790 int col;
10791 int off;
10792 int c;
10793
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010794 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10795 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010796 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010797 c = -1;
10798 else
10799 {
10800 off = LineOffset[row] + col;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010801 if (enc_utf8 && ScreenLinesUC[off] != 0)
10802 c = ScreenLinesUC[off];
10803 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 c = ScreenLines[off];
10805 }
10806 rettv->vval.v_number = c;
10807}
10808
10809/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010810 * "screenchars()" function
10811 */
10812 static void
10813f_screenchars(typval_T *argvars, typval_T *rettv)
10814{
10815 int row;
10816 int col;
10817 int off;
10818 int c;
10819 int i;
10820
10821 if (rettv_list_alloc(rettv) == FAIL)
10822 return;
10823 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10824 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10825 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10826 return;
10827
10828 off = LineOffset[row] + col;
10829 if (enc_utf8 && ScreenLinesUC[off] != 0)
10830 c = ScreenLinesUC[off];
10831 else
10832 c = ScreenLines[off];
10833 list_append_number(rettv->vval.v_list, (varnumber_T)c);
10834
10835 if (enc_utf8)
10836
10837 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10838 list_append_number(rettv->vval.v_list,
10839 (varnumber_T)ScreenLinesC[i][off]);
10840}
10841
10842/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010843 * "screencol()" function
10844 *
10845 * First column is 1 to be consistent with virtcol().
10846 */
10847 static void
10848f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
10849{
10850 rettv->vval.v_number = screen_screencol() + 1;
10851}
10852
10853/*
10854 * "screenrow()" function
10855 */
10856 static void
10857f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
10858{
10859 rettv->vval.v_number = screen_screenrow() + 1;
10860}
10861
10862/*
Bram Moolenaar2912abb2019-03-29 14:16:42 +010010863 * "screenstring()" function
10864 */
10865 static void
10866f_screenstring(typval_T *argvars, typval_T *rettv)
10867{
10868 int row;
10869 int col;
10870 int off;
10871 int c;
10872 int i;
10873 char_u buf[MB_MAXBYTES + 1];
10874 int buflen = 0;
10875
10876 rettv->vval.v_string = NULL;
10877 rettv->v_type = VAR_STRING;
10878
10879 row = (int)tv_get_number_chk(&argvars[0], NULL) - 1;
10880 col = (int)tv_get_number_chk(&argvars[1], NULL) - 1;
10881 if (row < 0 || row >= screen_Rows || col < 0 || col >= screen_Columns)
10882 return;
10883
10884 off = LineOffset[row] + col;
10885 if (enc_utf8 && ScreenLinesUC[off] != 0)
10886 c = ScreenLinesUC[off];
10887 else
10888 c = ScreenLines[off];
10889 buflen += mb_char2bytes(c, buf);
10890
10891 if (enc_utf8)
10892 for (i = 0; i < Screen_mco && ScreenLinesC[i][off] != 0; ++i)
10893 buflen += mb_char2bytes(ScreenLinesC[i][off], buf + buflen);
10894
10895 buf[buflen] = NUL;
10896 rettv->vval.v_string = vim_strsave(buf);
10897}
10898
10899/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010900 * "search()" function
10901 */
10902 static void
10903f_search(typval_T *argvars, typval_T *rettv)
10904{
10905 int flags = 0;
10906
10907 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
10908}
10909
10910/*
10911 * "searchdecl()" function
10912 */
10913 static void
10914f_searchdecl(typval_T *argvars, typval_T *rettv)
10915{
10916 int locally = 1;
10917 int thisblock = 0;
10918 int error = FALSE;
10919 char_u *name;
10920
10921 rettv->vval.v_number = 1; /* default: FAIL */
10922
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010923 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 if (argvars[1].v_type != VAR_UNKNOWN)
10925 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010926 locally = (int)tv_get_number_chk(&argvars[1], &error) == 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010927 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010928 thisblock = (int)tv_get_number_chk(&argvars[2], &error) != 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010929 }
10930 if (!error && name != NULL)
10931 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
10932 locally, thisblock, SEARCH_KEEP) == FAIL;
10933}
10934
10935/*
10936 * Used by searchpair() and searchpairpos()
10937 */
10938 static int
10939searchpair_cmn(typval_T *argvars, pos_T *match_pos)
10940{
10941 char_u *spat, *mpat, *epat;
Bram Moolenaar48570482017-10-30 21:48:41 +010010942 typval_T *skip;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010943 int save_p_ws = p_ws;
10944 int dir;
10945 int flags = 0;
10946 char_u nbuf1[NUMBUFLEN];
10947 char_u nbuf2[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010948 int retval = 0; /* default: FAIL */
10949 long lnum_stop = 0;
10950 long time_limit = 0;
10951
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010952 /* Get the three pattern arguments: start, middle, end. Will result in an
10953 * error if not a valid argument. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010954 spat = tv_get_string_chk(&argvars[0]);
10955 mpat = tv_get_string_buf_chk(&argvars[1], nbuf1);
10956 epat = tv_get_string_buf_chk(&argvars[2], nbuf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010957 if (spat == NULL || mpat == NULL || epat == NULL)
10958 goto theend; /* type error */
10959
10960 /* Handle the optional fourth argument: flags */
10961 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
10962 if (dir == 0)
10963 goto theend;
10964
10965 /* Don't accept SP_END or SP_SUBPAT.
10966 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
10967 */
10968 if ((flags & (SP_END | SP_SUBPAT)) != 0
10969 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
10970 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010971 semsg(_(e_invarg2), tv_get_string(&argvars[3]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010972 goto theend;
10973 }
10974
10975 /* Using 'r' implies 'W', otherwise it doesn't work. */
10976 if (flags & SP_REPEAT)
10977 p_ws = FALSE;
10978
10979 /* Optional fifth argument: skip expression */
10980 if (argvars[3].v_type == VAR_UNKNOWN
10981 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar48570482017-10-30 21:48:41 +010010982 skip = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010983 else
10984 {
Bram Moolenaar48570482017-10-30 21:48:41 +010010985 skip = &argvars[4];
10986 if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
10987 && skip->v_type != VAR_STRING)
10988 {
10989 /* Type error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010990 semsg(_(e_invarg2), tv_get_string(&argvars[4]));
Bram Moolenaar48570482017-10-30 21:48:41 +010010991 goto theend;
10992 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010993 if (argvars[5].v_type != VAR_UNKNOWN)
10994 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010995 lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010996 if (lnum_stop < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020010997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010998 semsg(_(e_invarg2), tv_get_string(&argvars[5]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010999 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011000 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011001#ifdef FEAT_RELTIME
11002 if (argvars[6].v_type != VAR_UNKNOWN)
11003 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011004 time_limit = (long)tv_get_number_chk(&argvars[6], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011005 if (time_limit < 0)
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011007 semsg(_(e_invarg2), tv_get_string(&argvars[6]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011008 goto theend;
Bram Moolenaar3dddb092018-06-24 19:01:59 +020011009 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011010 }
11011#endif
11012 }
11013 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011014
11015 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
11016 match_pos, lnum_stop, time_limit);
11017
11018theend:
11019 p_ws = save_p_ws;
11020
11021 return retval;
11022}
11023
11024/*
11025 * "searchpair()" function
11026 */
11027 static void
11028f_searchpair(typval_T *argvars, typval_T *rettv)
11029{
11030 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
11031}
11032
11033/*
11034 * "searchpairpos()" function
11035 */
11036 static void
11037f_searchpairpos(typval_T *argvars, typval_T *rettv)
11038{
11039 pos_T match_pos;
11040 int lnum = 0;
11041 int col = 0;
11042
11043 if (rettv_list_alloc(rettv) == FAIL)
11044 return;
11045
11046 if (searchpair_cmn(argvars, &match_pos) > 0)
11047 {
11048 lnum = match_pos.lnum;
11049 col = match_pos.col;
11050 }
11051
11052 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11053 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11054}
11055
11056/*
11057 * Search for a start/middle/end thing.
11058 * Used by searchpair(), see its documentation for the details.
11059 * Returns 0 or -1 for no match,
11060 */
11061 long
11062do_searchpair(
11063 char_u *spat, /* start pattern */
11064 char_u *mpat, /* middle pattern */
11065 char_u *epat, /* end pattern */
11066 int dir, /* BACKWARD or FORWARD */
Bram Moolenaar48570482017-10-30 21:48:41 +010011067 typval_T *skip, /* skip expression */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011068 int flags, /* SP_SETPCMARK and other SP_ values */
11069 pos_T *match_pos,
11070 linenr_T lnum_stop, /* stop at this line if not zero */
11071 long time_limit UNUSED) /* stop after this many msec */
11072{
11073 char_u *save_cpo;
11074 char_u *pat, *pat2 = NULL, *pat3 = NULL;
11075 long retval = 0;
11076 pos_T pos;
11077 pos_T firstpos;
11078 pos_T foundpos;
11079 pos_T save_cursor;
11080 pos_T save_pos;
11081 int n;
11082 int r;
11083 int nest = 1;
Bram Moolenaar48570482017-10-30 21:48:41 +010011084 int use_skip = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011085 int err;
11086 int options = SEARCH_KEEP;
11087 proftime_T tm;
11088
11089 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11090 save_cpo = p_cpo;
11091 p_cpo = empty_option;
11092
11093#ifdef FEAT_RELTIME
11094 /* Set the time limit, if there is one. */
11095 profile_setlimit(time_limit, &tm);
11096#endif
11097
11098 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11099 * start/middle/end (pat3, for the top pair). */
Bram Moolenaar964b3742019-05-24 18:54:09 +020011100 pat2 = alloc(STRLEN(spat) + STRLEN(epat) + 17);
11101 pat3 = alloc(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 25);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011102 if (pat2 == NULL || pat3 == NULL)
11103 goto theend;
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011104 sprintf((char *)pat2, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011105 if (*mpat == NUL)
11106 STRCPY(pat3, pat2);
11107 else
Bram Moolenaar6e450a52017-01-06 20:03:58 +010011108 sprintf((char *)pat3, "\\m\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011109 spat, epat, mpat);
11110 if (flags & SP_START)
11111 options |= SEARCH_START;
11112
Bram Moolenaar48570482017-10-30 21:48:41 +010011113 if (skip != NULL)
11114 {
11115 /* Empty string means to not use the skip expression. */
11116 if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
11117 use_skip = skip->vval.v_string != NULL
11118 && *skip->vval.v_string != NUL;
11119 }
11120
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011121 save_cursor = curwin->w_cursor;
11122 pos = curwin->w_cursor;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011123 CLEAR_POS(&firstpos);
11124 CLEAR_POS(&foundpos);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011125 pat = pat3;
11126 for (;;)
11127 {
Bram Moolenaar5d24a222018-12-23 19:10:09 +010011128 n = searchit(curwin, curbuf, &pos, NULL, dir, pat, 1L,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +020011129 options, RE_SEARCH, lnum_stop, &tm, NULL);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011130 if (n == FAIL || (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011131 /* didn't find it or found the first match again: FAIL */
11132 break;
11133
11134 if (firstpos.lnum == 0)
11135 firstpos = pos;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +010011136 if (EQUAL_POS(pos, foundpos))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011137 {
11138 /* Found the same position again. Can happen with a pattern that
11139 * has "\zs" at the end and searching backwards. Advance one
11140 * character and try again. */
11141 if (dir == BACKWARD)
11142 decl(&pos);
11143 else
11144 incl(&pos);
11145 }
11146 foundpos = pos;
11147
11148 /* clear the start flag to avoid getting stuck here */
11149 options &= ~SEARCH_START;
11150
11151 /* If the skip pattern matches, ignore this match. */
Bram Moolenaar48570482017-10-30 21:48:41 +010011152 if (use_skip)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011153 {
11154 save_pos = curwin->w_cursor;
11155 curwin->w_cursor = pos;
Bram Moolenaar48570482017-10-30 21:48:41 +010011156 err = FALSE;
11157 r = eval_expr_to_bool(skip, &err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011158 curwin->w_cursor = save_pos;
11159 if (err)
11160 {
11161 /* Evaluating {skip} caused an error, break here. */
11162 curwin->w_cursor = save_cursor;
11163 retval = -1;
11164 break;
11165 }
11166 if (r)
11167 continue;
11168 }
11169
11170 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11171 {
11172 /* Found end when searching backwards or start when searching
11173 * forward: nested pair. */
11174 ++nest;
11175 pat = pat2; /* nested, don't search for middle */
11176 }
11177 else
11178 {
11179 /* Found end when searching forward or start when searching
11180 * backward: end of (nested) pair; or found middle in outer pair. */
11181 if (--nest == 1)
11182 pat = pat3; /* outer level, search for middle */
11183 }
11184
11185 if (nest == 0)
11186 {
11187 /* Found the match: return matchcount or line number. */
11188 if (flags & SP_RETCOUNT)
11189 ++retval;
11190 else
11191 retval = pos.lnum;
11192 if (flags & SP_SETPCMARK)
11193 setpcmark();
11194 curwin->w_cursor = pos;
11195 if (!(flags & SP_REPEAT))
11196 break;
11197 nest = 1; /* search for next unmatched */
11198 }
11199 }
11200
11201 if (match_pos != NULL)
11202 {
11203 /* Store the match cursor position */
11204 match_pos->lnum = curwin->w_cursor.lnum;
11205 match_pos->col = curwin->w_cursor.col + 1;
11206 }
11207
11208 /* If 'n' flag is used or search failed: restore cursor position. */
11209 if ((flags & SP_NOMOVE) || retval == 0)
11210 curwin->w_cursor = save_cursor;
11211
11212theend:
11213 vim_free(pat2);
11214 vim_free(pat3);
11215 if (p_cpo == empty_option)
11216 p_cpo = save_cpo;
11217 else
11218 /* Darn, evaluating the {skip} expression changed the value. */
11219 free_string_option(save_cpo);
11220
11221 return retval;
11222}
11223
11224/*
11225 * "searchpos()" function
11226 */
11227 static void
11228f_searchpos(typval_T *argvars, typval_T *rettv)
11229{
11230 pos_T match_pos;
11231 int lnum = 0;
11232 int col = 0;
11233 int n;
11234 int flags = 0;
11235
11236 if (rettv_list_alloc(rettv) == FAIL)
11237 return;
11238
11239 n = search_cmn(argvars, &match_pos, &flags);
11240 if (n > 0)
11241 {
11242 lnum = match_pos.lnum;
11243 col = match_pos.col;
11244 }
11245
11246 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
11247 list_append_number(rettv->vval.v_list, (varnumber_T)col);
11248 if (flags & SP_SUBPAT)
11249 list_append_number(rettv->vval.v_list, (varnumber_T)n);
11250}
11251
11252 static void
11253f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
11254{
11255#ifdef FEAT_CLIENTSERVER
11256 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011257 char_u *server = tv_get_string_chk(&argvars[0]);
11258 char_u *reply = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011259
11260 rettv->vval.v_number = -1;
11261 if (server == NULL || reply == NULL)
11262 return;
11263 if (check_restricted() || check_secure())
11264 return;
11265# ifdef FEAT_X11
11266 if (check_connection() == FAIL)
11267 return;
11268# endif
11269
11270 if (serverSendReply(server, reply) < 0)
11271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011272 emsg(_("E258: Unable to send to client"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011273 return;
11274 }
11275 rettv->vval.v_number = 0;
11276#else
11277 rettv->vval.v_number = -1;
11278#endif
11279}
11280
11281 static void
11282f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
11283{
11284 char_u *r = NULL;
11285
11286#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4f974752019-02-17 17:44:42 +010011287# ifdef MSWIN
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011288 r = serverGetVimNames();
11289# else
11290 make_connection();
11291 if (X_DISPLAY != NULL)
11292 r = serverGetVimNames(X_DISPLAY);
11293# endif
11294#endif
11295 rettv->v_type = VAR_STRING;
11296 rettv->vval.v_string = r;
11297}
11298
11299/*
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011300 * "setbufline()" function
11301 */
11302 static void
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +020011303f_setbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011304{
11305 linenr_T lnum;
11306 buf_T *buf;
11307
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011308 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011309 if (buf == NULL)
11310 rettv->vval.v_number = 1; /* FAIL */
11311 else
11312 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011313 lnum = tv_get_lnum_buf(&argvars[1], buf);
Bram Moolenaarca851592018-06-06 21:04:07 +020011314 set_buffer_lines(buf, lnum, FALSE, &argvars[2], rettv);
Bram Moolenaarb31cf2b2017-09-02 19:45:19 +020011315 }
11316}
11317
11318/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011319 * "setbufvar()" function
11320 */
11321 static void
11322f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
11323{
11324 buf_T *buf;
11325 char_u *varname, *bufvarname;
11326 typval_T *varp;
11327 char_u nbuf[NUMBUFLEN];
11328
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011329 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011330 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011331 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
11332 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010011333 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011334 varp = &argvars[2];
11335
11336 if (buf != NULL && varname != NULL && varp != NULL)
11337 {
11338 if (*varname == '&')
11339 {
11340 long numval;
11341 char_u *strval;
11342 int error = FALSE;
11343 aco_save_T aco;
11344
11345 /* set curbuf to be our buf, temporarily */
11346 aucmd_prepbuf(&aco, buf);
11347
11348 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011349 numval = (long)tv_get_number_chk(varp, &error);
11350 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011351 if (!error && strval != NULL)
11352 set_option_value(varname, numval, strval, OPT_LOCAL);
11353
11354 /* reset notion of buffer */
11355 aucmd_restbuf(&aco);
11356 }
11357 else
11358 {
11359 buf_T *save_curbuf = curbuf;
11360
Bram Moolenaar964b3742019-05-24 18:54:09 +020011361 bufvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011362 if (bufvarname != NULL)
11363 {
11364 curbuf = buf;
11365 STRCPY(bufvarname, "b:");
11366 STRCPY(bufvarname + 2, varname);
11367 set_var(bufvarname, varp, TRUE);
11368 vim_free(bufvarname);
11369 curbuf = save_curbuf;
11370 }
11371 }
11372 }
11373}
11374
11375 static void
11376f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
11377{
11378 dict_T *d;
11379 dictitem_T *di;
11380 char_u *csearch;
11381
11382 if (argvars[0].v_type != VAR_DICT)
11383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011384 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011385 return;
11386 }
11387
11388 if ((d = argvars[0].vval.v_dict) != NULL)
11389 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010011390 csearch = dict_get_string(d, (char_u *)"char", FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011391 if (csearch != NULL)
11392 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011393 if (enc_utf8)
11394 {
11395 int pcc[MAX_MCO];
11396 int c = utfc_ptr2char(csearch, pcc);
11397
11398 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
11399 }
11400 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011401 set_last_csearch(PTR2CHAR(csearch),
11402 csearch, MB_PTR2LEN(csearch));
11403 }
11404
11405 di = dict_find(d, (char_u *)"forward", -1);
11406 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011407 set_csearch_direction((int)tv_get_number(&di->di_tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011408 ? FORWARD : BACKWARD);
11409
11410 di = dict_find(d, (char_u *)"until", -1);
11411 if (di != NULL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011412 set_csearch_until(!!tv_get_number(&di->di_tv));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011413 }
11414}
11415
11416/*
11417 * "setcmdpos()" function
11418 */
11419 static void
11420f_setcmdpos(typval_T *argvars, typval_T *rettv)
11421{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011422 int pos = (int)tv_get_number(&argvars[0]) - 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011423
11424 if (pos >= 0)
11425 rettv->vval.v_number = set_cmdline_pos(pos);
11426}
11427
11428/*
Bram Moolenaar691ddee2019-05-09 14:52:41 +020011429 * "setenv()" function
11430 */
11431 static void
11432f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11433{
11434 char_u namebuf[NUMBUFLEN];
11435 char_u valbuf[NUMBUFLEN];
11436 char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11437
11438 if (argvars[1].v_type == VAR_SPECIAL
11439 && argvars[1].vval.v_number == VVAL_NULL)
11440 vim_unsetenv(name);
11441 else
11442 vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11443}
11444
11445/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011446 * "setfperm({fname}, {mode})" function
11447 */
11448 static void
11449f_setfperm(typval_T *argvars, typval_T *rettv)
11450{
11451 char_u *fname;
11452 char_u modebuf[NUMBUFLEN];
11453 char_u *mode_str;
11454 int i;
11455 int mask;
11456 int mode = 0;
11457
11458 rettv->vval.v_number = 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011459 fname = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011460 if (fname == NULL)
11461 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011462 mode_str = tv_get_string_buf_chk(&argvars[1], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011463 if (mode_str == NULL)
11464 return;
11465 if (STRLEN(mode_str) != 9)
11466 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011467 semsg(_(e_invarg2), mode_str);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011468 return;
11469 }
11470
11471 mask = 1;
11472 for (i = 8; i >= 0; --i)
11473 {
11474 if (mode_str[i] != '-')
11475 mode |= mask;
11476 mask = mask << 1;
11477 }
11478 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
11479}
11480
11481/*
11482 * "setline()" function
11483 */
11484 static void
11485f_setline(typval_T *argvars, typval_T *rettv)
11486{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011487 linenr_T lnum = tv_get_lnum(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011488
Bram Moolenaarca851592018-06-06 21:04:07 +020011489 set_buffer_lines(curbuf, lnum, FALSE, &argvars[1], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011490}
11491
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011492/*
11493 * Used by "setqflist()" and "setloclist()" functions
11494 */
11495 static void
11496set_qf_ll_list(
11497 win_T *wp UNUSED,
11498 typval_T *list_arg UNUSED,
11499 typval_T *action_arg UNUSED,
Bram Moolenaard823fa92016-08-12 16:29:27 +020011500 typval_T *what_arg UNUSED,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011501 typval_T *rettv)
11502{
11503#ifdef FEAT_QUICKFIX
11504 static char *e_invact = N_("E927: Invalid action: '%s'");
11505 char_u *act;
11506 int action = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011507 static int recursive = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011508#endif
11509
11510 rettv->vval.v_number = -1;
11511
11512#ifdef FEAT_QUICKFIX
11513 if (list_arg->v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011514 emsg(_(e_listreq));
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011515 else if (recursive != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011516 emsg(_(e_au_recursive));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011517 else
11518 {
11519 list_T *l = list_arg->vval.v_list;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011520 dict_T *d = NULL;
11521 int valid_dict = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011522
11523 if (action_arg->v_type == VAR_STRING)
11524 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011525 act = tv_get_string_chk(action_arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011526 if (act == NULL)
11527 return; /* type error; errmsg already given */
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020011528 if ((*act == 'a' || *act == 'r' || *act == ' ' || *act == 'f') &&
11529 act[1] == NUL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011530 action = *act;
11531 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011532 semsg(_(e_invact), act);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011533 }
11534 else if (action_arg->v_type == VAR_UNKNOWN)
11535 action = ' ';
11536 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011537 emsg(_(e_stringreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011538
Bram Moolenaard823fa92016-08-12 16:29:27 +020011539 if (action_arg->v_type != VAR_UNKNOWN
11540 && what_arg->v_type != VAR_UNKNOWN)
11541 {
11542 if (what_arg->v_type == VAR_DICT)
11543 d = what_arg->vval.v_dict;
11544 else
11545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011546 emsg(_(e_dictreq));
Bram Moolenaard823fa92016-08-12 16:29:27 +020011547 valid_dict = FALSE;
11548 }
11549 }
11550
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011551 ++recursive;
Bram Moolenaard823fa92016-08-12 16:29:27 +020011552 if (l != NULL && action && valid_dict && set_errorlist(wp, l, action,
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011553 (char_u *)(wp == NULL ? ":setqflist()" : ":setloclist()"),
11554 d) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011555 rettv->vval.v_number = 0;
Bram Moolenaar2f82ca72018-06-17 19:22:52 +020011556 --recursive;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011557 }
11558#endif
11559}
11560
11561/*
11562 * "setloclist()" function
11563 */
11564 static void
11565f_setloclist(typval_T *argvars, typval_T *rettv)
11566{
11567 win_T *win;
11568
11569 rettv->vval.v_number = -1;
11570
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020011571 win = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011572 if (win != NULL)
Bram Moolenaard823fa92016-08-12 16:29:27 +020011573 set_qf_ll_list(win, &argvars[1], &argvars[2], &argvars[3], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011574}
11575
11576/*
11577 * "setmatches()" function
11578 */
11579 static void
11580f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
11581{
11582#ifdef FEAT_SEARCH_EXTRA
11583 list_T *l;
11584 listitem_T *li;
11585 dict_T *d;
11586 list_T *s = NULL;
Bram Moolenaaraff74912019-03-30 18:11:49 +010011587 win_T *win = get_optional_window(argvars, 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011588
11589 rettv->vval.v_number = -1;
11590 if (argvars[0].v_type != VAR_LIST)
11591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011592 emsg(_(e_listreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011593 return;
11594 }
Bram Moolenaaraff74912019-03-30 18:11:49 +010011595 if (win == NULL)
11596 return;
11597
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011598 if ((l = argvars[0].vval.v_list) != NULL)
11599 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011600 /* To some extent make sure that we are dealing with a list from
11601 * "getmatches()". */
11602 li = l->lv_first;
11603 while (li != NULL)
11604 {
11605 if (li->li_tv.v_type != VAR_DICT
11606 || (d = li->li_tv.vval.v_dict) == NULL)
11607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011608 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011609 return;
11610 }
11611 if (!(dict_find(d, (char_u *)"group", -1) != NULL
11612 && (dict_find(d, (char_u *)"pattern", -1) != NULL
11613 || dict_find(d, (char_u *)"pos1", -1) != NULL)
11614 && dict_find(d, (char_u *)"priority", -1) != NULL
11615 && dict_find(d, (char_u *)"id", -1) != NULL))
11616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011617 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011618 return;
11619 }
11620 li = li->li_next;
11621 }
11622
Bram Moolenaaraff74912019-03-30 18:11:49 +010011623 clear_matches(win);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011624 li = l->lv_first;
11625 while (li != NULL)
11626 {
11627 int i = 0;
Bram Moolenaar54315892019-04-26 22:33:49 +020011628 char buf[30]; // use 30 to avoid compiler warning
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011629 dictitem_T *di;
11630 char_u *group;
11631 int priority;
11632 int id;
11633 char_u *conceal;
11634
11635 d = li->li_tv.vval.v_dict;
11636 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
11637 {
11638 if (s == NULL)
11639 {
11640 s = list_alloc();
11641 if (s == NULL)
11642 return;
11643 }
11644
11645 /* match from matchaddpos() */
11646 for (i = 1; i < 9; i++)
11647 {
11648 sprintf((char *)buf, (char *)"pos%d", i);
11649 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
11650 {
11651 if (di->di_tv.v_type != VAR_LIST)
11652 return;
11653
11654 list_append_tv(s, &di->di_tv);
11655 s->lv_refcount++;
11656 }
11657 else
11658 break;
11659 }
11660 }
11661
Bram Moolenaar8f667172018-12-14 15:38:31 +010011662 group = dict_get_string(d, (char_u *)"group", TRUE);
11663 priority = (int)dict_get_number(d, (char_u *)"priority");
11664 id = (int)dict_get_number(d, (char_u *)"id");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011665 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
Bram Moolenaar8f667172018-12-14 15:38:31 +010011666 ? dict_get_string(d, (char_u *)"conceal", TRUE)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011667 : NULL;
11668 if (i == 0)
11669 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011670 match_add(win, group,
Bram Moolenaar8f667172018-12-14 15:38:31 +010011671 dict_get_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011672 priority, id, NULL, conceal);
11673 }
11674 else
11675 {
Bram Moolenaaraff74912019-03-30 18:11:49 +010011676 match_add(win, group, NULL, priority, id, s, conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011677 list_unref(s);
11678 s = NULL;
11679 }
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +020011680 vim_free(group);
11681 vim_free(conceal);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011682
11683 li = li->li_next;
11684 }
11685 rettv->vval.v_number = 0;
11686 }
11687#endif
11688}
11689
11690/*
11691 * "setpos()" function
11692 */
11693 static void
11694f_setpos(typval_T *argvars, typval_T *rettv)
11695{
11696 pos_T pos;
11697 int fnum;
11698 char_u *name;
11699 colnr_T curswant = -1;
11700
11701 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011702 name = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011703 if (name != NULL)
11704 {
11705 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
11706 {
11707 if (--pos.col < 0)
11708 pos.col = 0;
11709 if (name[0] == '.' && name[1] == NUL)
11710 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011711 /* set cursor; "fnum" is ignored */
11712 curwin->w_cursor = pos;
11713 if (curswant >= 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011714 {
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011715 curwin->w_curswant = curswant - 1;
11716 curwin->w_set_curswant = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011717 }
Bram Moolenaar3a29abc2017-01-28 18:31:41 +010011718 check_cursor();
11719 rettv->vval.v_number = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011720 }
11721 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
11722 {
11723 /* set mark */
11724 if (setmark_pos(name[1], &pos, fnum) == OK)
11725 rettv->vval.v_number = 0;
11726 }
11727 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011728 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011729 }
11730 }
11731}
11732
11733/*
11734 * "setqflist()" function
11735 */
11736 static void
11737f_setqflist(typval_T *argvars, typval_T *rettv)
11738{
Bram Moolenaard823fa92016-08-12 16:29:27 +020011739 set_qf_ll_list(NULL, &argvars[0], &argvars[1], &argvars[2], rettv);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011740}
11741
11742/*
11743 * "setreg()" function
11744 */
11745 static void
11746f_setreg(typval_T *argvars, typval_T *rettv)
11747{
11748 int regname;
11749 char_u *strregname;
11750 char_u *stropt;
11751 char_u *strval;
11752 int append;
11753 char_u yank_type;
11754 long block_len;
11755
11756 block_len = -1;
11757 yank_type = MAUTO;
11758 append = FALSE;
11759
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011760 strregname = tv_get_string_chk(argvars);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011761 rettv->vval.v_number = 1; /* FAIL is default */
11762
11763 if (strregname == NULL)
11764 return; /* type error; errmsg already given */
11765 regname = *strregname;
11766 if (regname == 0 || regname == '@')
11767 regname = '"';
11768
11769 if (argvars[2].v_type != VAR_UNKNOWN)
11770 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011771 stropt = tv_get_string_chk(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011772 if (stropt == NULL)
11773 return; /* type error */
11774 for (; *stropt != NUL; ++stropt)
11775 switch (*stropt)
11776 {
11777 case 'a': case 'A': /* append */
11778 append = TRUE;
11779 break;
11780 case 'v': case 'c': /* character-wise selection */
11781 yank_type = MCHAR;
11782 break;
11783 case 'V': case 'l': /* line-wise selection */
11784 yank_type = MLINE;
11785 break;
11786 case 'b': case Ctrl_V: /* block-wise selection */
11787 yank_type = MBLOCK;
11788 if (VIM_ISDIGIT(stropt[1]))
11789 {
11790 ++stropt;
11791 block_len = getdigits(&stropt) - 1;
11792 --stropt;
11793 }
11794 break;
11795 }
11796 }
11797
11798 if (argvars[1].v_type == VAR_LIST)
11799 {
11800 char_u **lstval;
11801 char_u **allocval;
11802 char_u buf[NUMBUFLEN];
11803 char_u **curval;
11804 char_u **curallocval;
11805 list_T *ll = argvars[1].vval.v_list;
11806 listitem_T *li;
11807 int len;
11808
11809 /* If the list is NULL handle like an empty list. */
11810 len = ll == NULL ? 0 : ll->lv_len;
11811
11812 /* First half: use for pointers to result lines; second half: use for
11813 * pointers to allocated copies. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020011814 lstval = ALLOC_MULT(char_u *, (len + 1) * 2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011815 if (lstval == NULL)
11816 return;
11817 curval = lstval;
11818 allocval = lstval + len + 2;
11819 curallocval = allocval;
11820
11821 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
11822 li = li->li_next)
11823 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011824 strval = tv_get_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011825 if (strval == NULL)
11826 goto free_lstval;
11827 if (strval == buf)
11828 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011829 /* Need to make a copy, next tv_get_string_buf_chk() will
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011830 * overwrite the string. */
11831 strval = vim_strsave(buf);
11832 if (strval == NULL)
11833 goto free_lstval;
11834 *curallocval++ = strval;
11835 }
11836 *curval++ = strval;
11837 }
11838 *curval++ = NULL;
11839
11840 write_reg_contents_lst(regname, lstval, -1,
11841 append, yank_type, block_len);
11842free_lstval:
11843 while (curallocval > allocval)
11844 vim_free(*--curallocval);
11845 vim_free(lstval);
11846 }
11847 else
11848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011849 strval = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011850 if (strval == NULL)
11851 return;
11852 write_reg_contents_ex(regname, strval, -1,
11853 append, yank_type, block_len);
11854 }
11855 rettv->vval.v_number = 0;
11856}
11857
11858/*
11859 * "settabvar()" function
11860 */
11861 static void
11862f_settabvar(typval_T *argvars, typval_T *rettv)
11863{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011864 tabpage_T *save_curtab;
11865 tabpage_T *tp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011866 char_u *varname, *tabvarname;
11867 typval_T *varp;
11868
11869 rettv->vval.v_number = 0;
11870
Bram Moolenaar8c62a082019-02-08 14:34:10 +010011871 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011872 return;
11873
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011874 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
11875 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011876 varp = &argvars[2];
11877
Bram Moolenaar4033c552017-09-16 20:54:51 +020011878 if (varname != NULL && varp != NULL && tp != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011879 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011880 save_curtab = curtab;
11881 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011882
Bram Moolenaar964b3742019-05-24 18:54:09 +020011883 tabvarname = alloc(STRLEN(varname) + 3);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011884 if (tabvarname != NULL)
11885 {
11886 STRCPY(tabvarname, "t:");
11887 STRCPY(tabvarname + 2, varname);
11888 set_var(tabvarname, varp, TRUE);
11889 vim_free(tabvarname);
11890 }
11891
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011892 /* Restore current tabpage */
11893 if (valid_tabpage(save_curtab))
11894 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011895 }
11896}
11897
11898/*
11899 * "settabwinvar()" function
11900 */
11901 static void
11902f_settabwinvar(typval_T *argvars, typval_T *rettv)
11903{
11904 setwinvar(argvars, rettv, 1);
11905}
11906
11907/*
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011908 * "settagstack()" function
11909 */
11910 static void
11911f_settagstack(typval_T *argvars, typval_T *rettv)
11912{
11913 static char *e_invact2 = N_("E962: Invalid action: '%s'");
11914 win_T *wp;
11915 dict_T *d;
11916 int action = 'r';
11917
11918 rettv->vval.v_number = -1;
11919
11920 // first argument: window number or id
11921 wp = find_win_by_nr_or_id(&argvars[0]);
11922 if (wp == NULL)
11923 return;
11924
11925 // second argument: dict with items to set in the tag stack
11926 if (argvars[1].v_type != VAR_DICT)
11927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011928 emsg(_(e_dictreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011929 return;
11930 }
11931 d = argvars[1].vval.v_dict;
11932 if (d == NULL)
11933 return;
11934
11935 // third argument: action - 'a' for append and 'r' for replace.
11936 // default is to replace the stack.
11937 if (argvars[2].v_type == VAR_UNKNOWN)
11938 action = 'r';
11939 else if (argvars[2].v_type == VAR_STRING)
11940 {
11941 char_u *actstr;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011942 actstr = tv_get_string_chk(&argvars[2]);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011943 if (actstr == NULL)
11944 return;
11945 if ((*actstr == 'r' || *actstr == 'a') && actstr[1] == NUL)
11946 action = *actstr;
11947 else
11948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011949 semsg(_(e_invact2), actstr);
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011950 return;
11951 }
11952 }
11953 else
11954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011955 emsg(_(e_stringreq));
Bram Moolenaarf49cc602018-11-11 15:21:05 +010011956 return;
11957 }
11958
11959 if (set_tagstack(wp, d, action) == OK)
11960 rettv->vval.v_number = 0;
11961}
11962
11963/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011964 * "setwinvar()" function
11965 */
11966 static void
11967f_setwinvar(typval_T *argvars, typval_T *rettv)
11968{
11969 setwinvar(argvars, rettv, 0);
11970}
11971
11972#ifdef FEAT_CRYPT
11973/*
11974 * "sha256({string})" function
11975 */
11976 static void
11977f_sha256(typval_T *argvars, typval_T *rettv)
11978{
11979 char_u *p;
11980
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011981 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011982 rettv->vval.v_string = vim_strsave(
11983 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
11984 rettv->v_type = VAR_STRING;
11985}
11986#endif /* FEAT_CRYPT */
11987
11988/*
11989 * "shellescape({string})" function
11990 */
11991 static void
11992f_shellescape(typval_T *argvars, typval_T *rettv)
11993{
Bram Moolenaar20615522017-06-05 18:46:26 +020011994 int do_special = non_zero_arg(&argvars[1]);
11995
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011996 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010011997 tv_get_string(&argvars[0]), do_special, do_special);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020011998 rettv->v_type = VAR_STRING;
11999}
12000
12001/*
12002 * shiftwidth() function
12003 */
12004 static void
12005f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
12006{
Bram Moolenaarf9514162018-11-22 03:08:29 +010012007 rettv->vval.v_number = 0;
12008
12009 if (argvars[0].v_type != VAR_UNKNOWN)
12010 {
12011 long col;
12012
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012013 col = (long)tv_get_number_chk(argvars, NULL);
Bram Moolenaarf9514162018-11-22 03:08:29 +010012014 if (col < 0)
12015 return; // type error; errmsg already given
12016#ifdef FEAT_VARTABS
12017 rettv->vval.v_number = get_sw_value_col(curbuf, col);
12018 return;
12019#endif
12020 }
12021
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012022 rettv->vval.v_number = get_sw_value(curbuf);
12023}
12024
Bram Moolenaar162b7142018-12-21 15:17:36 +010012025#ifdef FEAT_SIGNS
12026/*
12027 * "sign_define()" function
12028 */
12029 static void
12030f_sign_define(typval_T *argvars, typval_T *rettv)
12031{
12032 char_u *name;
12033 dict_T *dict;
12034 char_u *icon = NULL;
12035 char_u *linehl = NULL;
12036 char_u *text = NULL;
12037 char_u *texthl = NULL;
12038
12039 rettv->vval.v_number = -1;
12040
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012041 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012042 if (name == NULL)
12043 return;
12044
12045 if (argvars[1].v_type != VAR_UNKNOWN)
12046 {
12047 if (argvars[1].v_type != VAR_DICT)
12048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012049 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012050 return;
12051 }
12052
12053 // sign attributes
12054 dict = argvars[1].vval.v_dict;
12055 if (dict_find(dict, (char_u *)"icon", -1) != NULL)
12056 icon = dict_get_string(dict, (char_u *)"icon", TRUE);
12057 if (dict_find(dict, (char_u *)"linehl", -1) != NULL)
12058 linehl = dict_get_string(dict, (char_u *)"linehl", TRUE);
12059 if (dict_find(dict, (char_u *)"text", -1) != NULL)
12060 text = dict_get_string(dict, (char_u *)"text", TRUE);
12061 if (dict_find(dict, (char_u *)"texthl", -1) != NULL)
12062 texthl = dict_get_string(dict, (char_u *)"texthl", TRUE);
12063 }
12064
12065 if (sign_define_by_name(name, icon, linehl, text, texthl) == OK)
12066 rettv->vval.v_number = 0;
12067
12068 vim_free(icon);
12069 vim_free(linehl);
12070 vim_free(text);
12071 vim_free(texthl);
12072}
12073
12074/*
12075 * "sign_getdefined()" function
12076 */
12077 static void
12078f_sign_getdefined(typval_T *argvars, typval_T *rettv)
12079{
12080 char_u *name = NULL;
12081
12082 if (rettv_list_alloc_id(rettv, aid_sign_getdefined) != OK)
12083 return;
12084
12085 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012086 name = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012087
12088 sign_getlist(name, rettv->vval.v_list);
12089}
12090
12091/*
12092 * "sign_getplaced()" function
12093 */
12094 static void
12095f_sign_getplaced(typval_T *argvars, typval_T *rettv)
12096{
12097 buf_T *buf = NULL;
12098 dict_T *dict;
12099 dictitem_T *di;
12100 linenr_T lnum = 0;
12101 int sign_id = 0;
12102 char_u *group = NULL;
12103 int notanum = FALSE;
12104
12105 if (rettv_list_alloc_id(rettv, aid_sign_getplaced) != OK)
12106 return;
12107
12108 if (argvars[0].v_type != VAR_UNKNOWN)
12109 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012110 // get signs placed in the specified buffer
12111 buf = get_buf_arg(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012112 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012113 return;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012114
12115 if (argvars[1].v_type != VAR_UNKNOWN)
12116 {
12117 if (argvars[1].v_type != VAR_DICT ||
12118 ((dict = argvars[1].vval.v_dict) == NULL))
12119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012120 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012121 return;
12122 }
12123 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12124 {
12125 // get signs placed at this line
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012126 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012127 if (notanum)
12128 return;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012129 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012130 }
12131 if ((di = dict_find(dict, (char_u *)"id", -1)) != NULL)
12132 {
12133 // get sign placed with this identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012134 sign_id = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012135 if (notanum)
12136 return;
12137 }
12138 if ((di = dict_find(dict, (char_u *)"group", -1)) != NULL)
12139 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012140 group = tv_get_string_chk(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012141 if (group == NULL)
12142 return;
Bram Moolenaar6436cd82018-12-27 00:28:33 +010012143 if (*group == '\0') // empty string means global group
12144 group = NULL;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012145 }
12146 }
12147 }
12148
12149 sign_get_placed(buf, lnum, sign_id, group, rettv->vval.v_list);
12150}
12151
12152/*
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012153 * "sign_jump()" function
12154 */
12155 static void
12156f_sign_jump(typval_T *argvars, typval_T *rettv)
12157{
12158 int sign_id;
12159 char_u *sign_group = NULL;
12160 buf_T *buf;
12161 int notanum = FALSE;
12162
12163 rettv->vval.v_number = -1;
12164
Bram Moolenaarbdace832019-03-02 10:13:42 +010012165 // Sign identifier
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012166 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
12167 if (notanum)
12168 return;
12169 if (sign_id <= 0)
12170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012171 emsg(_(e_invarg));
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012172 return;
12173 }
12174
12175 // Sign group
12176 sign_group = tv_get_string_chk(&argvars[1]);
12177 if (sign_group == NULL)
12178 return;
12179 if (sign_group[0] == '\0')
12180 sign_group = NULL; // global sign group
12181 else
12182 {
12183 sign_group = vim_strsave(sign_group);
12184 if (sign_group == NULL)
12185 return;
12186 }
12187
12188 // Buffer to place the sign
12189 buf = get_buf_arg(&argvars[2]);
12190 if (buf == NULL)
12191 goto cleanup;
12192
12193 rettv->vval.v_number = sign_jump(sign_id, sign_group, buf);
12194
12195cleanup:
12196 vim_free(sign_group);
12197}
12198
12199/*
Bram Moolenaar162b7142018-12-21 15:17:36 +010012200 * "sign_place()" function
12201 */
12202 static void
12203f_sign_place(typval_T *argvars, typval_T *rettv)
12204{
12205 int sign_id;
12206 char_u *group = NULL;
12207 char_u *sign_name;
12208 buf_T *buf;
12209 dict_T *dict;
12210 dictitem_T *di;
12211 linenr_T lnum = 0;
12212 int prio = SIGN_DEF_PRIO;
12213 int notanum = FALSE;
12214
12215 rettv->vval.v_number = -1;
12216
Bram Moolenaarbdace832019-03-02 10:13:42 +010012217 // Sign identifier
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012218 sign_id = (int)tv_get_number_chk(&argvars[0], &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012219 if (notanum)
12220 return;
12221 if (sign_id < 0)
12222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012223 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012224 return;
12225 }
12226
12227 // Sign group
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012228 group = tv_get_string_chk(&argvars[1]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012229 if (group == NULL)
12230 return;
12231 if (group[0] == '\0')
12232 group = NULL; // global sign group
12233 else
12234 {
12235 group = vim_strsave(group);
12236 if (group == NULL)
12237 return;
12238 }
12239
12240 // Sign name
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012241 sign_name = tv_get_string_chk(&argvars[2]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012242 if (sign_name == NULL)
12243 goto cleanup;
12244
12245 // Buffer to place the sign
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012246 buf = get_buf_arg(&argvars[3]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012247 if (buf == NULL)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012248 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012249
12250 if (argvars[4].v_type != VAR_UNKNOWN)
12251 {
12252 if (argvars[4].v_type != VAR_DICT ||
12253 ((dict = argvars[4].vval.v_dict) == NULL))
12254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012255 emsg(_(e_dictreq));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012256 goto cleanup;
12257 }
12258
12259 // Line number where the sign is to be placed
12260 if ((di = dict_find(dict, (char_u *)"lnum", -1)) != NULL)
12261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012262 (void)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012263 if (notanum)
12264 goto cleanup;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012265 lnum = tv_get_lnum(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012266 }
12267 if ((di = dict_find(dict, (char_u *)"priority", -1)) != NULL)
12268 {
12269 // Sign priority
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012270 prio = (int)tv_get_number_chk(&di->di_tv, &notanum);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012271 if (notanum)
12272 goto cleanup;
12273 }
12274 }
12275
12276 if (sign_place(&sign_id, group, sign_name, buf, lnum, prio) == OK)
12277 rettv->vval.v_number = sign_id;
12278
12279cleanup:
12280 vim_free(group);
12281}
12282
12283/*
12284 * "sign_undefine()" function
12285 */
12286 static void
12287f_sign_undefine(typval_T *argvars, typval_T *rettv)
12288{
12289 char_u *name;
12290
12291 rettv->vval.v_number = -1;
12292
12293 if (argvars[0].v_type == VAR_UNKNOWN)
12294 {
12295 // Free all the signs
12296 free_signs();
12297 rettv->vval.v_number = 0;
12298 }
12299 else
12300 {
12301 // Free only the specified sign
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012302 name = tv_get_string_chk(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012303 if (name == NULL)
12304 return;
12305
12306 if (sign_undefine_by_name(name) == OK)
12307 rettv->vval.v_number = 0;
12308 }
12309}
12310
12311/*
12312 * "sign_unplace()" function
12313 */
12314 static void
12315f_sign_unplace(typval_T *argvars, typval_T *rettv)
12316{
12317 dict_T *dict;
12318 dictitem_T *di;
12319 int sign_id = 0;
12320 buf_T *buf = NULL;
12321 char_u *group = NULL;
12322
12323 rettv->vval.v_number = -1;
12324
12325 if (argvars[0].v_type != VAR_STRING)
12326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012327 emsg(_(e_invarg));
Bram Moolenaar162b7142018-12-21 15:17:36 +010012328 return;
12329 }
12330
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012331 group = tv_get_string(&argvars[0]);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012332 if (group[0] == '\0')
12333 group = NULL; // global sign group
12334 else
12335 {
12336 group = vim_strsave(group);
12337 if (group == NULL)
12338 return;
12339 }
12340
12341 if (argvars[1].v_type != VAR_UNKNOWN)
12342 {
12343 if (argvars[1].v_type != VAR_DICT)
12344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012345 emsg(_(e_dictreq));
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012346 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012347 }
12348 dict = argvars[1].vval.v_dict;
12349
12350 if ((di = dict_find(dict, (char_u *)"buffer", -1)) != NULL)
12351 {
Bram Moolenaar6b7b7192019-01-11 13:42:41 +010012352 buf = get_buf_arg(&di->di_tv);
Bram Moolenaar162b7142018-12-21 15:17:36 +010012353 if (buf == NULL)
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012354 goto cleanup;
Bram Moolenaar162b7142018-12-21 15:17:36 +010012355 }
12356 if (dict_find(dict, (char_u *)"id", -1) != NULL)
12357 sign_id = dict_get_number(dict, (char_u *)"id");
12358 }
12359
12360 if (buf == NULL)
12361 {
12362 // Delete the sign in all the buffers
12363 FOR_ALL_BUFFERS(buf)
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012364 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012365 rettv->vval.v_number = 0;
12366 }
12367 else
12368 {
Bram Moolenaar7d83bf42018-12-29 18:53:55 +010012369 if (sign_unplace(sign_id, group, buf, 0) == OK)
Bram Moolenaar162b7142018-12-21 15:17:36 +010012370 rettv->vval.v_number = 0;
12371 }
Bram Moolenaar1ea88a32018-12-29 21:00:27 +010012372
12373cleanup:
Bram Moolenaar162b7142018-12-21 15:17:36 +010012374 vim_free(group);
12375}
12376#endif
12377
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012378/*
12379 * "simplify()" function
12380 */
12381 static void
12382f_simplify(typval_T *argvars, typval_T *rettv)
12383{
12384 char_u *p;
12385
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012386 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012387 rettv->vval.v_string = vim_strsave(p);
12388 simplify_filename(rettv->vval.v_string); /* simplify in place */
12389 rettv->v_type = VAR_STRING;
12390}
12391
12392#ifdef FEAT_FLOAT
12393/*
12394 * "sin()" function
12395 */
12396 static void
12397f_sin(typval_T *argvars, typval_T *rettv)
12398{
12399 float_T f = 0.0;
12400
12401 rettv->v_type = VAR_FLOAT;
12402 if (get_float_arg(argvars, &f) == OK)
12403 rettv->vval.v_float = sin(f);
12404 else
12405 rettv->vval.v_float = 0.0;
12406}
12407
12408/*
12409 * "sinh()" function
12410 */
12411 static void
12412f_sinh(typval_T *argvars, typval_T *rettv)
12413{
12414 float_T f = 0.0;
12415
12416 rettv->v_type = VAR_FLOAT;
12417 if (get_float_arg(argvars, &f) == OK)
12418 rettv->vval.v_float = sinh(f);
12419 else
12420 rettv->vval.v_float = 0.0;
12421}
12422#endif
12423
Bram Moolenaareae1b912019-05-09 15:12:55 +020012424static int item_compare(const void *s1, const void *s2);
12425static int item_compare2(const void *s1, const void *s2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012426
12427/* struct used in the array that's given to qsort() */
12428typedef struct
12429{
12430 listitem_T *item;
12431 int idx;
12432} sortItem_T;
12433
12434/* struct storing information about current sort */
12435typedef struct
12436{
12437 int item_compare_ic;
12438 int item_compare_numeric;
12439 int item_compare_numbers;
12440#ifdef FEAT_FLOAT
12441 int item_compare_float;
12442#endif
12443 char_u *item_compare_func;
12444 partial_T *item_compare_partial;
12445 dict_T *item_compare_selfdict;
12446 int item_compare_func_err;
12447 int item_compare_keep_zero;
12448} sortinfo_T;
12449static sortinfo_T *sortinfo = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012450#define ITEM_COMPARE_FAIL 999
12451
12452/*
12453 * Compare functions for f_sort() and f_uniq() below.
12454 */
12455 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012456item_compare(const void *s1, const void *s2)
12457{
12458 sortItem_T *si1, *si2;
12459 typval_T *tv1, *tv2;
12460 char_u *p1, *p2;
12461 char_u *tofree1 = NULL, *tofree2 = NULL;
12462 int res;
12463 char_u numbuf1[NUMBUFLEN];
12464 char_u numbuf2[NUMBUFLEN];
12465
12466 si1 = (sortItem_T *)s1;
12467 si2 = (sortItem_T *)s2;
12468 tv1 = &si1->item->li_tv;
12469 tv2 = &si2->item->li_tv;
12470
12471 if (sortinfo->item_compare_numbers)
12472 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012473 varnumber_T v1 = tv_get_number(tv1);
12474 varnumber_T v2 = tv_get_number(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012475
12476 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12477 }
12478
12479#ifdef FEAT_FLOAT
12480 if (sortinfo->item_compare_float)
12481 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012482 float_T v1 = tv_get_float(tv1);
12483 float_T v2 = tv_get_float(tv2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012484
12485 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
12486 }
12487#endif
12488
12489 /* tv2string() puts quotes around a string and allocates memory. Don't do
12490 * that for string variables. Use a single quote when comparing with a
12491 * non-string to do what the docs promise. */
12492 if (tv1->v_type == VAR_STRING)
12493 {
12494 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12495 p1 = (char_u *)"'";
12496 else
12497 p1 = tv1->vval.v_string;
12498 }
12499 else
12500 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
12501 if (tv2->v_type == VAR_STRING)
12502 {
12503 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
12504 p2 = (char_u *)"'";
12505 else
12506 p2 = tv2->vval.v_string;
12507 }
12508 else
12509 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
12510 if (p1 == NULL)
12511 p1 = (char_u *)"";
12512 if (p2 == NULL)
12513 p2 = (char_u *)"";
12514 if (!sortinfo->item_compare_numeric)
12515 {
12516 if (sortinfo->item_compare_ic)
12517 res = STRICMP(p1, p2);
12518 else
12519 res = STRCMP(p1, p2);
12520 }
12521 else
12522 {
12523 double n1, n2;
12524 n1 = strtod((char *)p1, (char **)&p1);
12525 n2 = strtod((char *)p2, (char **)&p2);
12526 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
12527 }
12528
12529 /* When the result would be zero, compare the item indexes. Makes the
12530 * sort stable. */
12531 if (res == 0 && !sortinfo->item_compare_keep_zero)
12532 res = si1->idx > si2->idx ? 1 : -1;
12533
12534 vim_free(tofree1);
12535 vim_free(tofree2);
12536 return res;
12537}
12538
12539 static int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012540item_compare2(const void *s1, const void *s2)
12541{
12542 sortItem_T *si1, *si2;
12543 int res;
12544 typval_T rettv;
12545 typval_T argv[3];
12546 int dummy;
12547 char_u *func_name;
12548 partial_T *partial = sortinfo->item_compare_partial;
12549
12550 /* shortcut after failure in previous call; compare all items equal */
12551 if (sortinfo->item_compare_func_err)
12552 return 0;
12553
12554 si1 = (sortItem_T *)s1;
12555 si2 = (sortItem_T *)s2;
12556
12557 if (partial == NULL)
12558 func_name = sortinfo->item_compare_func;
12559 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +020012560 func_name = partial_name(partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012561
12562 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
12563 * in the copy without changing the original list items. */
12564 copy_tv(&si1->item->li_tv, &argv[0]);
12565 copy_tv(&si2->item->li_tv, &argv[1]);
12566
12567 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar6ed88192019-05-11 18:37:44 +020012568 res = call_func(func_name, -1, &rettv, 2, argv, NULL, 0L, 0L, &dummy, TRUE,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012569 partial, sortinfo->item_compare_selfdict);
12570 clear_tv(&argv[0]);
12571 clear_tv(&argv[1]);
12572
12573 if (res == FAIL)
12574 res = ITEM_COMPARE_FAIL;
12575 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012576 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012577 if (sortinfo->item_compare_func_err)
12578 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
12579 clear_tv(&rettv);
12580
12581 /* When the result would be zero, compare the pointers themselves. Makes
12582 * the sort stable. */
12583 if (res == 0 && !sortinfo->item_compare_keep_zero)
12584 res = si1->idx > si2->idx ? 1 : -1;
12585
12586 return res;
12587}
12588
12589/*
12590 * "sort({list})" function
12591 */
12592 static void
12593do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
12594{
12595 list_T *l;
12596 listitem_T *li;
12597 sortItem_T *ptrs;
12598 sortinfo_T *old_sortinfo;
12599 sortinfo_T info;
12600 long len;
12601 long i;
12602
12603 /* Pointer to current info struct used in compare function. Save and
12604 * restore the current one for nested calls. */
12605 old_sortinfo = sortinfo;
12606 sortinfo = &info;
12607
12608 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012609 semsg(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012610 else
12611 {
12612 l = argvars[0].vval.v_list;
Bram Moolenaar05c00c02019-02-11 22:00:11 +010012613 if (l == NULL || var_check_lock(l->lv_lock,
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012614 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
12615 TRUE))
12616 goto theend;
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020012617 rettv_list_set(rettv, l);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012618
12619 len = list_len(l);
12620 if (len <= 1)
12621 goto theend; /* short list sorts pretty quickly */
12622
12623 info.item_compare_ic = FALSE;
12624 info.item_compare_numeric = FALSE;
12625 info.item_compare_numbers = FALSE;
12626#ifdef FEAT_FLOAT
12627 info.item_compare_float = FALSE;
12628#endif
12629 info.item_compare_func = NULL;
12630 info.item_compare_partial = NULL;
12631 info.item_compare_selfdict = NULL;
12632 if (argvars[1].v_type != VAR_UNKNOWN)
12633 {
12634 /* optional second argument: {func} */
12635 if (argvars[1].v_type == VAR_FUNC)
12636 info.item_compare_func = argvars[1].vval.v_string;
12637 else if (argvars[1].v_type == VAR_PARTIAL)
12638 info.item_compare_partial = argvars[1].vval.v_partial;
12639 else
12640 {
12641 int error = FALSE;
12642
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012643 i = (long)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012644 if (error)
12645 goto theend; /* type error; errmsg already given */
12646 if (i == 1)
12647 info.item_compare_ic = TRUE;
12648 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012649 info.item_compare_func = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012650 else if (i != 0)
12651 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012652 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012653 goto theend;
12654 }
12655 if (info.item_compare_func != NULL)
12656 {
12657 if (*info.item_compare_func == NUL)
12658 {
12659 /* empty string means default sort */
12660 info.item_compare_func = NULL;
12661 }
12662 else if (STRCMP(info.item_compare_func, "n") == 0)
12663 {
12664 info.item_compare_func = NULL;
12665 info.item_compare_numeric = TRUE;
12666 }
12667 else if (STRCMP(info.item_compare_func, "N") == 0)
12668 {
12669 info.item_compare_func = NULL;
12670 info.item_compare_numbers = TRUE;
12671 }
12672#ifdef FEAT_FLOAT
12673 else if (STRCMP(info.item_compare_func, "f") == 0)
12674 {
12675 info.item_compare_func = NULL;
12676 info.item_compare_float = TRUE;
12677 }
12678#endif
12679 else if (STRCMP(info.item_compare_func, "i") == 0)
12680 {
12681 info.item_compare_func = NULL;
12682 info.item_compare_ic = TRUE;
12683 }
12684 }
12685 }
12686
12687 if (argvars[2].v_type != VAR_UNKNOWN)
12688 {
12689 /* optional third argument: {dict} */
12690 if (argvars[2].v_type != VAR_DICT)
12691 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012692 emsg(_(e_dictreq));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012693 goto theend;
12694 }
12695 info.item_compare_selfdict = argvars[2].vval.v_dict;
12696 }
12697 }
12698
12699 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +020012700 ptrs = ALLOC_MULT(sortItem_T, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012701 if (ptrs == NULL)
12702 goto theend;
12703
12704 i = 0;
12705 if (sort)
12706 {
12707 /* sort(): ptrs will be the list to sort */
12708 for (li = l->lv_first; li != NULL; li = li->li_next)
12709 {
12710 ptrs[i].item = li;
12711 ptrs[i].idx = i;
12712 ++i;
12713 }
12714
12715 info.item_compare_func_err = FALSE;
12716 info.item_compare_keep_zero = FALSE;
12717 /* test the compare function */
12718 if ((info.item_compare_func != NULL
12719 || info.item_compare_partial != NULL)
12720 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12721 == ITEM_COMPARE_FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012722 emsg(_("E702: Sort compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012723 else
12724 {
12725 /* Sort the array with item pointers. */
12726 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
12727 info.item_compare_func == NULL
12728 && info.item_compare_partial == NULL
12729 ? item_compare : item_compare2);
12730
12731 if (!info.item_compare_func_err)
12732 {
12733 /* Clear the List and append the items in sorted order. */
12734 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
12735 l->lv_len = 0;
12736 for (i = 0; i < len; ++i)
12737 list_append(l, ptrs[i].item);
12738 }
12739 }
12740 }
12741 else
12742 {
12743 int (*item_compare_func_ptr)(const void *, const void *);
12744
12745 /* f_uniq(): ptrs will be a stack of items to remove */
12746 info.item_compare_func_err = FALSE;
12747 info.item_compare_keep_zero = TRUE;
12748 item_compare_func_ptr = info.item_compare_func != NULL
12749 || info.item_compare_partial != NULL
12750 ? item_compare2 : item_compare;
12751
12752 for (li = l->lv_first; li != NULL && li->li_next != NULL;
12753 li = li->li_next)
12754 {
12755 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
12756 == 0)
12757 ptrs[i++].item = li;
12758 if (info.item_compare_func_err)
12759 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010012760 emsg(_("E882: Uniq compare function failed"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012761 break;
12762 }
12763 }
12764
12765 if (!info.item_compare_func_err)
12766 {
12767 while (--i >= 0)
12768 {
12769 li = ptrs[i].item->li_next;
12770 ptrs[i].item->li_next = li->li_next;
12771 if (li->li_next != NULL)
12772 li->li_next->li_prev = ptrs[i].item;
12773 else
12774 l->lv_last = ptrs[i].item;
12775 list_fix_watch(l, li);
12776 listitem_free(li);
12777 l->lv_len--;
12778 }
12779 }
12780 }
12781
12782 vim_free(ptrs);
12783 }
12784theend:
12785 sortinfo = old_sortinfo;
12786}
12787
12788/*
12789 * "sort({list})" function
12790 */
12791 static void
12792f_sort(typval_T *argvars, typval_T *rettv)
12793{
12794 do_sort_uniq(argvars, rettv, TRUE);
12795}
12796
12797/*
12798 * "uniq({list})" function
12799 */
12800 static void
12801f_uniq(typval_T *argvars, typval_T *rettv)
12802{
12803 do_sort_uniq(argvars, rettv, FALSE);
12804}
12805
12806/*
12807 * "soundfold({word})" function
12808 */
12809 static void
12810f_soundfold(typval_T *argvars, typval_T *rettv)
12811{
12812 char_u *s;
12813
12814 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012815 s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012816#ifdef FEAT_SPELL
12817 rettv->vval.v_string = eval_soundfold(s);
12818#else
12819 rettv->vval.v_string = vim_strsave(s);
12820#endif
12821}
12822
12823/*
12824 * "spellbadword()" function
12825 */
12826 static void
12827f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
12828{
12829 char_u *word = (char_u *)"";
12830 hlf_T attr = HLF_COUNT;
12831 int len = 0;
12832
12833 if (rettv_list_alloc(rettv) == FAIL)
12834 return;
12835
12836#ifdef FEAT_SPELL
12837 if (argvars[0].v_type == VAR_UNKNOWN)
12838 {
12839 /* Find the start and length of the badly spelled word. */
12840 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
12841 if (len != 0)
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012842 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012843 word = ml_get_cursor();
Bram Moolenaarb73fa622017-12-21 20:27:47 +010012844 curwin->w_set_curswant = TRUE;
12845 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012846 }
12847 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
12848 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012849 char_u *str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012850 int capcol = -1;
12851
12852 if (str != NULL)
12853 {
12854 /* Check the argument for spelling. */
12855 while (*str != NUL)
12856 {
12857 len = spell_check(curwin, str, &attr, &capcol, FALSE);
12858 if (attr != HLF_COUNT)
12859 {
12860 word = str;
12861 break;
12862 }
12863 str += len;
Bram Moolenaar66ab9162018-07-20 20:28:48 +020012864 capcol -= len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012865 }
12866 }
12867 }
12868#endif
12869
12870 list_append_string(rettv->vval.v_list, word, len);
12871 list_append_string(rettv->vval.v_list, (char_u *)(
12872 attr == HLF_SPB ? "bad" :
12873 attr == HLF_SPR ? "rare" :
12874 attr == HLF_SPL ? "local" :
12875 attr == HLF_SPC ? "caps" :
12876 ""), -1);
12877}
12878
12879/*
12880 * "spellsuggest()" function
12881 */
12882 static void
12883f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
12884{
12885#ifdef FEAT_SPELL
12886 char_u *str;
12887 int typeerr = FALSE;
12888 int maxcount;
12889 garray_T ga;
12890 int i;
12891 listitem_T *li;
12892 int need_capital = FALSE;
12893#endif
12894
12895 if (rettv_list_alloc(rettv) == FAIL)
12896 return;
12897
12898#ifdef FEAT_SPELL
12899 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
12900 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012901 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012902 if (argvars[1].v_type != VAR_UNKNOWN)
12903 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012904 maxcount = (int)tv_get_number_chk(&argvars[1], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012905 if (maxcount <= 0)
12906 return;
12907 if (argvars[2].v_type != VAR_UNKNOWN)
12908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012909 need_capital = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012910 if (typeerr)
12911 return;
12912 }
12913 }
12914 else
12915 maxcount = 25;
12916
12917 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
12918
12919 for (i = 0; i < ga.ga_len; ++i)
12920 {
12921 str = ((char_u **)ga.ga_data)[i];
12922
12923 li = listitem_alloc();
12924 if (li == NULL)
12925 vim_free(str);
12926 else
12927 {
12928 li->li_tv.v_type = VAR_STRING;
12929 li->li_tv.v_lock = 0;
12930 li->li_tv.vval.v_string = str;
12931 list_append(rettv->vval.v_list, li);
12932 }
12933 }
12934 ga_clear(&ga);
12935 }
12936#endif
12937}
12938
12939 static void
12940f_split(typval_T *argvars, typval_T *rettv)
12941{
12942 char_u *str;
12943 char_u *end;
12944 char_u *pat = NULL;
12945 regmatch_T regmatch;
12946 char_u patbuf[NUMBUFLEN];
12947 char_u *save_cpo;
12948 int match;
12949 colnr_T col = 0;
12950 int keepempty = FALSE;
12951 int typeerr = FALSE;
12952
12953 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12954 save_cpo = p_cpo;
12955 p_cpo = (char_u *)"";
12956
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012957 str = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012958 if (argvars[1].v_type != VAR_UNKNOWN)
12959 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012960 pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012961 if (pat == NULL)
12962 typeerr = TRUE;
12963 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010012964 keepempty = (int)tv_get_number_chk(&argvars[2], &typeerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012965 }
12966 if (pat == NULL || *pat == NUL)
12967 pat = (char_u *)"[\\x01- ]\\+";
12968
12969 if (rettv_list_alloc(rettv) == FAIL)
12970 return;
12971 if (typeerr)
12972 return;
12973
12974 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12975 if (regmatch.regprog != NULL)
12976 {
12977 regmatch.rm_ic = FALSE;
12978 while (*str != NUL || keepempty)
12979 {
12980 if (*str == NUL)
12981 match = FALSE; /* empty item at the end */
12982 else
12983 match = vim_regexec_nl(&regmatch, str, col);
12984 if (match)
12985 end = regmatch.startp[0];
12986 else
12987 end = str + STRLEN(str);
12988 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
12989 && *str != NUL && match && end < regmatch.endp[0]))
12990 {
12991 if (list_append_string(rettv->vval.v_list, str,
12992 (int)(end - str)) == FAIL)
12993 break;
12994 }
12995 if (!match)
12996 break;
Bram Moolenaar13505972019-01-24 15:04:48 +010012997 // Advance to just after the match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020012998 if (regmatch.endp[0] > str)
12999 col = 0;
13000 else
Bram Moolenaar13505972019-01-24 15:04:48 +010013001 // Don't get stuck at the same match.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013002 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013003 str = regmatch.endp[0];
13004 }
13005
13006 vim_regfree(regmatch.regprog);
13007 }
13008
13009 p_cpo = save_cpo;
13010}
13011
13012#ifdef FEAT_FLOAT
13013/*
13014 * "sqrt()" function
13015 */
13016 static void
13017f_sqrt(typval_T *argvars, typval_T *rettv)
13018{
13019 float_T f = 0.0;
13020
13021 rettv->v_type = VAR_FLOAT;
13022 if (get_float_arg(argvars, &f) == OK)
13023 rettv->vval.v_float = sqrt(f);
13024 else
13025 rettv->vval.v_float = 0.0;
13026}
13027
13028/*
13029 * "str2float()" function
13030 */
13031 static void
13032f_str2float(typval_T *argvars, typval_T *rettv)
13033{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013034 char_u *p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013035 int isneg = (*p == '-');
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013036
Bram Moolenaar08243d22017-01-10 16:12:29 +010013037 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013038 p = skipwhite(p + 1);
13039 (void)string2float(p, &rettv->vval.v_float);
Bram Moolenaar08243d22017-01-10 16:12:29 +010013040 if (isneg)
13041 rettv->vval.v_float *= -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013042 rettv->v_type = VAR_FLOAT;
13043}
13044#endif
13045
13046/*
Bram Moolenaar9d401282019-04-06 13:18:12 +020013047 * "str2list()" function
13048 */
13049 static void
13050f_str2list(typval_T *argvars, typval_T *rettv)
13051{
13052 char_u *p;
13053 int utf8 = FALSE;
13054
13055 if (rettv_list_alloc(rettv) == FAIL)
13056 return;
13057
13058 if (argvars[1].v_type != VAR_UNKNOWN)
13059 utf8 = (int)tv_get_number_chk(&argvars[1], NULL);
13060
13061 p = tv_get_string(&argvars[0]);
13062
13063 if (has_mbyte || utf8)
13064 {
13065 int (*ptr2len)(char_u *);
13066 int (*ptr2char)(char_u *);
13067
13068 if (utf8 || enc_utf8)
13069 {
13070 ptr2len = utf_ptr2len;
13071 ptr2char = utf_ptr2char;
13072 }
13073 else
13074 {
13075 ptr2len = mb_ptr2len;
13076 ptr2char = mb_ptr2char;
13077 }
13078
13079 for ( ; *p != NUL; p += (*ptr2len)(p))
13080 list_append_number(rettv->vval.v_list, (*ptr2char)(p));
13081 }
13082 else
13083 for ( ; *p != NUL; ++p)
13084 list_append_number(rettv->vval.v_list, *p);
13085}
13086
13087/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013088 * "str2nr()" function
13089 */
13090 static void
13091f_str2nr(typval_T *argvars, typval_T *rettv)
13092{
13093 int base = 10;
13094 char_u *p;
13095 varnumber_T n;
13096 int what;
Bram Moolenaar08243d22017-01-10 16:12:29 +010013097 int isneg;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013098
13099 if (argvars[1].v_type != VAR_UNKNOWN)
13100 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013101 base = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013102 if (base != 2 && base != 8 && base != 10 && base != 16)
13103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013104 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013105 return;
13106 }
13107 }
13108
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013109 p = skipwhite(tv_get_string(&argvars[0]));
Bram Moolenaar08243d22017-01-10 16:12:29 +010013110 isneg = (*p == '-');
13111 if (*p == '+' || *p == '-')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013112 p = skipwhite(p + 1);
13113 switch (base)
13114 {
13115 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
13116 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
13117 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
13118 default: what = 0;
13119 }
Bram Moolenaar16e9b852019-05-19 19:59:35 +020013120 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE);
13121 // Text after the number is silently ignored.
Bram Moolenaar08243d22017-01-10 16:12:29 +010013122 if (isneg)
13123 rettv->vval.v_number = -n;
13124 else
13125 rettv->vval.v_number = n;
13126
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013127}
13128
13129#ifdef HAVE_STRFTIME
13130/*
13131 * "strftime({format}[, {time}])" function
13132 */
13133 static void
13134f_strftime(typval_T *argvars, typval_T *rettv)
13135{
13136 char_u result_buf[256];
Bram Moolenaar63d25552019-05-10 21:28:38 +020013137# ifdef HAVE_LOCALTIME_R
13138 struct tm tmval;
13139# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013140 struct tm *curtime;
13141 time_t seconds;
13142 char_u *p;
13143
13144 rettv->v_type = VAR_STRING;
13145
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013146 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013147 if (argvars[1].v_type == VAR_UNKNOWN)
13148 seconds = time(NULL);
13149 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013150 seconds = (time_t)tv_get_number(&argvars[1]);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013151# ifdef HAVE_LOCALTIME_R
13152 curtime = localtime_r(&seconds, &tmval);
13153# else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013154 curtime = localtime(&seconds);
Bram Moolenaar63d25552019-05-10 21:28:38 +020013155# endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013156 /* MSVC returns NULL for an invalid value of seconds. */
13157 if (curtime == NULL)
13158 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
13159 else
13160 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013161 vimconv_T conv;
13162 char_u *enc;
13163
13164 conv.vc_type = CONV_NONE;
13165 enc = enc_locale();
13166 convert_setup(&conv, p_enc, enc);
13167 if (conv.vc_type != CONV_NONE)
13168 p = string_convert(&conv, p, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013169 if (p != NULL)
13170 (void)strftime((char *)result_buf, sizeof(result_buf),
13171 (char *)p, curtime);
13172 else
13173 result_buf[0] = NUL;
13174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013175 if (conv.vc_type != CONV_NONE)
13176 vim_free(p);
13177 convert_setup(&conv, enc, p_enc);
13178 if (conv.vc_type != CONV_NONE)
13179 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
13180 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013181 rettv->vval.v_string = vim_strsave(result_buf);
13182
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013183 /* Release conversion descriptors */
13184 convert_setup(&conv, NULL, NULL);
13185 vim_free(enc);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013186 }
13187}
13188#endif
13189
13190/*
13191 * "strgetchar()" function
13192 */
13193 static void
13194f_strgetchar(typval_T *argvars, typval_T *rettv)
13195{
13196 char_u *str;
13197 int len;
13198 int error = FALSE;
13199 int charidx;
Bram Moolenaar13505972019-01-24 15:04:48 +010013200 int byteidx = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013201
13202 rettv->vval.v_number = -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013203 str = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013204 if (str == NULL)
13205 return;
13206 len = (int)STRLEN(str);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013207 charidx = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013208 if (error)
13209 return;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013210
Bram Moolenaar13505972019-01-24 15:04:48 +010013211 while (charidx >= 0 && byteidx < len)
13212 {
13213 if (charidx == 0)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013214 {
Bram Moolenaar13505972019-01-24 15:04:48 +010013215 rettv->vval.v_number = mb_ptr2char(str + byteidx);
13216 break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013217 }
Bram Moolenaar13505972019-01-24 15:04:48 +010013218 --charidx;
13219 byteidx += MB_CPTR2LEN(str + byteidx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013220 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013221}
13222
13223/*
13224 * "stridx()" function
13225 */
13226 static void
13227f_stridx(typval_T *argvars, typval_T *rettv)
13228{
13229 char_u buf[NUMBUFLEN];
13230 char_u *needle;
13231 char_u *haystack;
13232 char_u *save_haystack;
13233 char_u *pos;
13234 int start_idx;
13235
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013236 needle = tv_get_string_chk(&argvars[1]);
13237 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013238 rettv->vval.v_number = -1;
13239 if (needle == NULL || haystack == NULL)
13240 return; /* type error; errmsg already given */
13241
13242 if (argvars[2].v_type != VAR_UNKNOWN)
13243 {
13244 int error = FALSE;
13245
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013246 start_idx = (int)tv_get_number_chk(&argvars[2], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013247 if (error || start_idx >= (int)STRLEN(haystack))
13248 return;
13249 if (start_idx >= 0)
13250 haystack += start_idx;
13251 }
13252
13253 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13254 if (pos != NULL)
13255 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
13256}
13257
13258/*
13259 * "string()" function
13260 */
Bram Moolenaar461a7fc2018-12-22 13:28:07 +010013261 void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013262f_string(typval_T *argvars, typval_T *rettv)
13263{
13264 char_u *tofree;
13265 char_u numbuf[NUMBUFLEN];
13266
13267 rettv->v_type = VAR_STRING;
13268 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
13269 get_copyID());
13270 /* Make a copy if we have a value but it's not in allocated memory. */
13271 if (rettv->vval.v_string != NULL && tofree == NULL)
13272 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
13273}
13274
13275/*
13276 * "strlen()" function
13277 */
13278 static void
13279f_strlen(typval_T *argvars, typval_T *rettv)
13280{
13281 rettv->vval.v_number = (varnumber_T)(STRLEN(
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013282 tv_get_string(&argvars[0])));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013283}
13284
13285/*
13286 * "strchars()" function
13287 */
13288 static void
13289f_strchars(typval_T *argvars, typval_T *rettv)
13290{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013291 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013292 int skipcc = 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013293 varnumber_T len = 0;
13294 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013295
13296 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013297 skipcc = (int)tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013298 if (skipcc < 0 || skipcc > 1)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013299 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013300 else
13301 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013302 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
13303 while (*s != NUL)
13304 {
13305 func_mb_ptr2char_adv(&s);
13306 ++len;
13307 }
13308 rettv->vval.v_number = len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013309 }
13310}
13311
13312/*
13313 * "strdisplaywidth()" function
13314 */
13315 static void
13316f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
13317{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013318 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013319 int col = 0;
13320
13321 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013322 col = (int)tv_get_number(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013323
13324 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
13325}
13326
13327/*
13328 * "strwidth()" function
13329 */
13330 static void
13331f_strwidth(typval_T *argvars, typval_T *rettv)
13332{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013333 char_u *s = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013334
Bram Moolenaar13505972019-01-24 15:04:48 +010013335 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013336}
13337
13338/*
13339 * "strcharpart()" function
13340 */
13341 static void
13342f_strcharpart(typval_T *argvars, typval_T *rettv)
13343{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013344 char_u *p;
13345 int nchar;
13346 int nbyte = 0;
13347 int charlen;
13348 int len = 0;
13349 int slen;
13350 int error = FALSE;
13351
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013352 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013353 slen = (int)STRLEN(p);
13354
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013355 nchar = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013356 if (!error)
13357 {
13358 if (nchar > 0)
13359 while (nchar > 0 && nbyte < slen)
13360 {
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013361 nbyte += MB_CPTR2LEN(p + nbyte);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013362 --nchar;
13363 }
13364 else
13365 nbyte = nchar;
13366 if (argvars[2].v_type != VAR_UNKNOWN)
13367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013368 charlen = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013369 while (charlen > 0 && nbyte + len < slen)
13370 {
13371 int off = nbyte + len;
13372
13373 if (off < 0)
13374 len += 1;
13375 else
Bram Moolenaard3c907b2016-08-17 21:32:09 +020013376 len += MB_CPTR2LEN(p + off);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013377 --charlen;
13378 }
13379 }
13380 else
13381 len = slen - nbyte; /* default: all bytes that are available. */
13382 }
13383
13384 /*
13385 * Only return the overlap between the specified part and the actual
13386 * string.
13387 */
13388 if (nbyte < 0)
13389 {
13390 len += nbyte;
13391 nbyte = 0;
13392 }
13393 else if (nbyte > slen)
13394 nbyte = slen;
13395 if (len < 0)
13396 len = 0;
13397 else if (nbyte + len > slen)
13398 len = slen - nbyte;
13399
13400 rettv->v_type = VAR_STRING;
13401 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013402}
13403
13404/*
13405 * "strpart()" function
13406 */
13407 static void
13408f_strpart(typval_T *argvars, typval_T *rettv)
13409{
13410 char_u *p;
13411 int n;
13412 int len;
13413 int slen;
13414 int error = FALSE;
13415
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013416 p = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013417 slen = (int)STRLEN(p);
13418
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013419 n = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013420 if (error)
13421 len = 0;
13422 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013423 len = (int)tv_get_number(&argvars[2]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013424 else
13425 len = slen - n; /* default len: all bytes that are available. */
13426
13427 /*
13428 * Only return the overlap between the specified part and the actual
13429 * string.
13430 */
13431 if (n < 0)
13432 {
13433 len += n;
13434 n = 0;
13435 }
13436 else if (n > slen)
13437 n = slen;
13438 if (len < 0)
13439 len = 0;
13440 else if (n + len > slen)
13441 len = slen - n;
13442
13443 rettv->v_type = VAR_STRING;
13444 rettv->vval.v_string = vim_strnsave(p + n, len);
13445}
13446
13447/*
13448 * "strridx()" function
13449 */
13450 static void
13451f_strridx(typval_T *argvars, typval_T *rettv)
13452{
13453 char_u buf[NUMBUFLEN];
13454 char_u *needle;
13455 char_u *haystack;
13456 char_u *rest;
13457 char_u *lastmatch = NULL;
13458 int haystack_len, end_idx;
13459
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013460 needle = tv_get_string_chk(&argvars[1]);
13461 haystack = tv_get_string_buf_chk(&argvars[0], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013462
13463 rettv->vval.v_number = -1;
13464 if (needle == NULL || haystack == NULL)
13465 return; /* type error; errmsg already given */
13466
13467 haystack_len = (int)STRLEN(haystack);
13468 if (argvars[2].v_type != VAR_UNKNOWN)
13469 {
13470 /* Third argument: upper limit for index */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013471 end_idx = (int)tv_get_number_chk(&argvars[2], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013472 if (end_idx < 0)
13473 return; /* can never find a match */
13474 }
13475 else
13476 end_idx = haystack_len;
13477
13478 if (*needle == NUL)
13479 {
13480 /* Empty string matches past the end. */
13481 lastmatch = haystack + end_idx;
13482 }
13483 else
13484 {
13485 for (rest = haystack; *rest != '\0'; ++rest)
13486 {
13487 rest = (char_u *)strstr((char *)rest, (char *)needle);
13488 if (rest == NULL || rest > haystack + end_idx)
13489 break;
13490 lastmatch = rest;
13491 }
13492 }
13493
13494 if (lastmatch == NULL)
13495 rettv->vval.v_number = -1;
13496 else
13497 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13498}
13499
13500/*
13501 * "strtrans()" function
13502 */
13503 static void
13504f_strtrans(typval_T *argvars, typval_T *rettv)
13505{
13506 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013507 rettv->vval.v_string = transstr(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013508}
13509
13510/*
13511 * "submatch()" function
13512 */
13513 static void
13514f_submatch(typval_T *argvars, typval_T *rettv)
13515{
13516 int error = FALSE;
13517 int no;
13518 int retList = 0;
13519
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013520 no = (int)tv_get_number_chk(&argvars[0], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013521 if (error)
13522 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013523 if (no < 0 || no >= NSUBEXP)
13524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013525 semsg(_("E935: invalid submatch number: %d"), no);
Bram Moolenaar79518e22017-02-17 16:31:35 +010013526 return;
Bram Moolenaar989f5922016-08-21 15:26:54 +020013527 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013528 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013529 retList = (int)tv_get_number_chk(&argvars[1], &error);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013530 if (error)
13531 return;
13532
13533 if (retList == 0)
13534 {
13535 rettv->v_type = VAR_STRING;
13536 rettv->vval.v_string = reg_submatch(no);
13537 }
13538 else
13539 {
13540 rettv->v_type = VAR_LIST;
13541 rettv->vval.v_list = reg_submatch_list(no);
13542 }
13543}
13544
13545/*
13546 * "substitute()" function
13547 */
13548 static void
13549f_substitute(typval_T *argvars, typval_T *rettv)
13550{
13551 char_u patbuf[NUMBUFLEN];
13552 char_u subbuf[NUMBUFLEN];
13553 char_u flagsbuf[NUMBUFLEN];
13554
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013555 char_u *str = tv_get_string_chk(&argvars[0]);
13556 char_u *pat = tv_get_string_buf_chk(&argvars[1], patbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013557 char_u *sub = NULL;
13558 typval_T *expr = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013559 char_u *flg = tv_get_string_buf_chk(&argvars[3], flagsbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013560
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013561 if (argvars[2].v_type == VAR_FUNC || argvars[2].v_type == VAR_PARTIAL)
13562 expr = &argvars[2];
13563 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013564 sub = tv_get_string_buf_chk(&argvars[2], subbuf);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013565
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013566 rettv->v_type = VAR_STRING;
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013567 if (str == NULL || pat == NULL || (sub == NULL && expr == NULL)
13568 || flg == NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013569 rettv->vval.v_string = NULL;
13570 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020013571 rettv->vval.v_string = do_string_sub(str, pat, sub, expr, flg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013572}
13573
13574/*
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013575 * "swapinfo(swap_filename)" function
13576 */
13577 static void
13578f_swapinfo(typval_T *argvars, typval_T *rettv)
13579{
13580 if (rettv_dict_alloc(rettv) == OK)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013581 get_b0_dict(tv_get_string(argvars), rettv->vval.v_dict);
Bram Moolenaar00f123a2018-08-21 20:28:54 +020013582}
13583
13584/*
Bram Moolenaar110bd602018-09-16 18:46:59 +020013585 * "swapname(expr)" function
13586 */
13587 static void
13588f_swapname(typval_T *argvars, typval_T *rettv)
13589{
13590 buf_T *buf;
13591
13592 rettv->v_type = VAR_STRING;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +010013593 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar110bd602018-09-16 18:46:59 +020013594 if (buf == NULL || buf->b_ml.ml_mfp == NULL
13595 || buf->b_ml.ml_mfp->mf_fname == NULL)
13596 rettv->vval.v_string = NULL;
13597 else
13598 rettv->vval.v_string = vim_strsave(buf->b_ml.ml_mfp->mf_fname);
13599}
13600
13601/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013602 * "synID(lnum, col, trans)" function
13603 */
13604 static void
13605f_synID(typval_T *argvars UNUSED, typval_T *rettv)
13606{
13607 int id = 0;
13608#ifdef FEAT_SYN_HL
13609 linenr_T lnum;
13610 colnr_T col;
13611 int trans;
13612 int transerr = FALSE;
13613
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013614 lnum = tv_get_lnum(argvars); /* -1 on type error */
13615 col = (linenr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
13616 trans = (int)tv_get_number_chk(&argvars[2], &transerr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013617
13618 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13619 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13620 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
13621#endif
13622
13623 rettv->vval.v_number = id;
13624}
13625
13626/*
13627 * "synIDattr(id, what [, mode])" function
13628 */
13629 static void
13630f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
13631{
13632 char_u *p = NULL;
13633#ifdef FEAT_SYN_HL
13634 int id;
13635 char_u *what;
13636 char_u *mode;
13637 char_u modebuf[NUMBUFLEN];
13638 int modec;
13639
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013640 id = (int)tv_get_number(&argvars[0]);
13641 what = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013642 if (argvars[2].v_type != VAR_UNKNOWN)
13643 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013644 mode = tv_get_string_buf(&argvars[2], modebuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013645 modec = TOLOWER_ASC(mode[0]);
13646 if (modec != 't' && modec != 'c' && modec != 'g')
13647 modec = 0; /* replace invalid with current */
13648 }
13649 else
13650 {
13651#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
13652 if (USE_24BIT)
13653 modec = 'g';
13654 else
13655#endif
13656 if (t_colors > 1)
13657 modec = 'c';
13658 else
13659 modec = 't';
13660 }
13661
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013662 switch (TOLOWER_ASC(what[0]))
13663 {
13664 case 'b':
13665 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13666 p = highlight_color(id, what, modec);
13667 else /* bold */
13668 p = highlight_has_attr(id, HL_BOLD, modec);
13669 break;
13670
13671 case 'f': /* fg[#] or font */
13672 p = highlight_color(id, what, modec);
13673 break;
13674
13675 case 'i':
13676 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13677 p = highlight_has_attr(id, HL_INVERSE, modec);
13678 else /* italic */
13679 p = highlight_has_attr(id, HL_ITALIC, modec);
13680 break;
13681
13682 case 'n': /* name */
Bram Moolenaarc96272e2017-03-26 13:50:09 +020013683 p = get_highlight_name_ext(NULL, id - 1, FALSE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013684 break;
13685
13686 case 'r': /* reverse */
13687 p = highlight_has_attr(id, HL_INVERSE, modec);
13688 break;
13689
13690 case 's':
13691 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
13692 p = highlight_color(id, what, modec);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +020013693 /* strikeout */
13694 else if (TOLOWER_ASC(what[1]) == 't' &&
13695 TOLOWER_ASC(what[2]) == 'r')
13696 p = highlight_has_attr(id, HL_STRIKETHROUGH, modec);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013697 else /* standout */
13698 p = highlight_has_attr(id, HL_STANDOUT, modec);
13699 break;
13700
13701 case 'u':
13702 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
13703 /* underline */
13704 p = highlight_has_attr(id, HL_UNDERLINE, modec);
13705 else
13706 /* undercurl */
13707 p = highlight_has_attr(id, HL_UNDERCURL, modec);
13708 break;
13709 }
13710
13711 if (p != NULL)
13712 p = vim_strsave(p);
13713#endif
13714 rettv->v_type = VAR_STRING;
13715 rettv->vval.v_string = p;
13716}
13717
13718/*
13719 * "synIDtrans(id)" function
13720 */
13721 static void
13722f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
13723{
13724 int id;
13725
13726#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013727 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013728
13729 if (id > 0)
13730 id = syn_get_final_id(id);
13731 else
13732#endif
13733 id = 0;
13734
13735 rettv->vval.v_number = id;
13736}
13737
13738/*
13739 * "synconcealed(lnum, col)" function
13740 */
13741 static void
13742f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
13743{
13744#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
13745 linenr_T lnum;
13746 colnr_T col;
13747 int syntax_flags = 0;
13748 int cchar;
13749 int matchid = 0;
13750 char_u str[NUMBUFLEN];
13751#endif
13752
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013753 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013754
13755#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013756 lnum = tv_get_lnum(argvars); /* -1 on type error */
13757 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013758
13759 vim_memset(str, NUL, sizeof(str));
13760
13761 if (rettv_list_alloc(rettv) != FAIL)
13762 {
13763 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13764 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13765 && curwin->w_p_cole > 0)
13766 {
13767 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
13768 syntax_flags = get_syntax_info(&matchid);
13769
13770 /* get the conceal character */
13771 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
13772 {
13773 cchar = syn_get_sub_char();
Bram Moolenaar4d785892017-06-22 22:00:50 +020013774 if (cchar == NUL && curwin->w_p_cole == 1)
13775 cchar = (lcs_conceal == NUL) ? ' ' : lcs_conceal;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013776 if (cchar != NUL)
13777 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013778 if (has_mbyte)
13779 (*mb_char2bytes)(cchar, str);
13780 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013781 str[0] = cchar;
13782 }
13783 }
13784 }
13785
13786 list_append_number(rettv->vval.v_list,
13787 (syntax_flags & HL_CONCEAL) != 0);
13788 /* -1 to auto-determine strlen */
13789 list_append_string(rettv->vval.v_list, str, -1);
13790 list_append_number(rettv->vval.v_list, matchid);
13791 }
13792#endif
13793}
13794
13795/*
13796 * "synstack(lnum, col)" function
13797 */
13798 static void
13799f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
13800{
13801#ifdef FEAT_SYN_HL
13802 linenr_T lnum;
13803 colnr_T col;
13804 int i;
13805 int id;
13806#endif
13807
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013808 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013809
13810#ifdef FEAT_SYN_HL
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013811 lnum = tv_get_lnum(argvars); /* -1 on type error */
13812 col = (colnr_T)tv_get_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013813
13814 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
13815 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
13816 && rettv_list_alloc(rettv) != FAIL)
13817 {
13818 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
13819 for (i = 0; ; ++i)
13820 {
13821 id = syn_get_stack_item(i);
13822 if (id < 0)
13823 break;
13824 if (list_append_number(rettv->vval.v_list, id) == FAIL)
13825 break;
13826 }
13827 }
13828#endif
13829}
13830
13831 static void
13832get_cmd_output_as_rettv(
13833 typval_T *argvars,
13834 typval_T *rettv,
13835 int retlist)
13836{
13837 char_u *res = NULL;
13838 char_u *p;
13839 char_u *infile = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013840 int err = FALSE;
13841 FILE *fd;
13842 list_T *list = NULL;
13843 int flags = SHELL_SILENT;
13844
13845 rettv->v_type = VAR_STRING;
13846 rettv->vval.v_string = NULL;
13847 if (check_restricted() || check_secure())
13848 goto errret;
13849
13850 if (argvars[1].v_type != VAR_UNKNOWN)
13851 {
13852 /*
Bram Moolenaar12c44922017-01-08 13:26:03 +010013853 * Write the text to a temp file, to be used for input of the shell
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013854 * command.
13855 */
13856 if ((infile = vim_tempname('i', TRUE)) == NULL)
13857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013858 emsg(_(e_notmp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013859 goto errret;
13860 }
13861
13862 fd = mch_fopen((char *)infile, WRITEBIN);
13863 if (fd == NULL)
13864 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013865 semsg(_(e_notopen), infile);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013866 goto errret;
13867 }
Bram Moolenaar12c44922017-01-08 13:26:03 +010013868 if (argvars[1].v_type == VAR_NUMBER)
13869 {
13870 linenr_T lnum;
13871 buf_T *buf;
13872
13873 buf = buflist_findnr(argvars[1].vval.v_number);
13874 if (buf == NULL)
13875 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013876 semsg(_(e_nobufnr), argvars[1].vval.v_number);
Bram Moolenaar23c9e8b2017-01-20 19:59:54 +010013877 fclose(fd);
Bram Moolenaar12c44922017-01-08 13:26:03 +010013878 goto errret;
13879 }
13880
13881 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
13882 {
13883 for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
13884 if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
13885 {
13886 err = TRUE;
13887 break;
13888 }
13889 if (putc(NL, fd) == EOF)
13890 {
13891 err = TRUE;
13892 break;
13893 }
13894 }
13895 }
13896 else if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013897 {
13898 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
13899 err = TRUE;
13900 }
13901 else
13902 {
Bram Moolenaar12c44922017-01-08 13:26:03 +010013903 size_t len;
13904 char_u buf[NUMBUFLEN];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013905
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013906 p = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013907 if (p == NULL)
13908 {
13909 fclose(fd);
13910 goto errret; /* type error; errmsg already given */
13911 }
13912 len = STRLEN(p);
13913 if (len > 0 && fwrite(p, len, 1, fd) != 1)
13914 err = TRUE;
13915 }
13916 if (fclose(fd) != 0)
13917 err = TRUE;
13918 if (err)
13919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010013920 emsg(_("E677: Error writing temp file"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013921 goto errret;
13922 }
13923 }
13924
13925 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
13926 * echoes typeahead, that messes up the display. */
13927 if (!msg_silent)
13928 flags += SHELL_COOKED;
13929
13930 if (retlist)
13931 {
13932 int len;
13933 listitem_T *li;
13934 char_u *s = NULL;
13935 char_u *start;
13936 char_u *end;
13937 int i;
13938
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013939 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013940 if (res == NULL)
13941 goto errret;
13942
13943 list = list_alloc();
13944 if (list == NULL)
13945 goto errret;
13946
13947 for (i = 0; i < len; ++i)
13948 {
13949 start = res + i;
13950 while (i < len && res[i] != NL)
13951 ++i;
13952 end = res + i;
13953
Bram Moolenaar964b3742019-05-24 18:54:09 +020013954 s = alloc(end - start + 1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013955 if (s == NULL)
13956 goto errret;
13957
13958 for (p = s; start < end; ++p, ++start)
13959 *p = *start == NUL ? NL : *start;
13960 *p = NUL;
13961
13962 li = listitem_alloc();
13963 if (li == NULL)
13964 {
13965 vim_free(s);
13966 goto errret;
13967 }
13968 li->li_tv.v_type = VAR_STRING;
13969 li->li_tv.v_lock = 0;
13970 li->li_tv.vval.v_string = s;
13971 list_append(list, li);
13972 }
13973
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020013974 rettv_list_set(rettv, list);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013975 list = NULL;
13976 }
13977 else
13978 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010013979 res = get_cmd_output(tv_get_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar00590742019-02-15 21:06:09 +010013980#ifdef USE_CRNL
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013981 /* translate <CR><NL> into <NL> */
13982 if (res != NULL)
13983 {
13984 char_u *s, *d;
13985
13986 d = res;
13987 for (s = res; *s; ++s)
13988 {
13989 if (s[0] == CAR && s[1] == NL)
13990 ++s;
13991 *d++ = *s;
13992 }
13993 *d = NUL;
13994 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020013995#endif
13996 rettv->vval.v_string = res;
13997 res = NULL;
13998 }
13999
14000errret:
14001 if (infile != NULL)
14002 {
14003 mch_remove(infile);
14004 vim_free(infile);
14005 }
14006 if (res != NULL)
14007 vim_free(res);
14008 if (list != NULL)
14009 list_free(list);
14010}
14011
14012/*
14013 * "system()" function
14014 */
14015 static void
14016f_system(typval_T *argvars, typval_T *rettv)
14017{
14018 get_cmd_output_as_rettv(argvars, rettv, FALSE);
14019}
14020
14021/*
14022 * "systemlist()" function
14023 */
14024 static void
14025f_systemlist(typval_T *argvars, typval_T *rettv)
14026{
14027 get_cmd_output_as_rettv(argvars, rettv, TRUE);
14028}
14029
14030/*
14031 * "tabpagebuflist()" function
14032 */
14033 static void
14034f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14035{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014036 tabpage_T *tp;
14037 win_T *wp = NULL;
14038
14039 if (argvars[0].v_type == VAR_UNKNOWN)
14040 wp = firstwin;
14041 else
14042 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014043 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014044 if (tp != NULL)
14045 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14046 }
14047 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
14048 {
14049 for (; wp != NULL; wp = wp->w_next)
14050 if (list_append_number(rettv->vval.v_list,
14051 wp->w_buffer->b_fnum) == FAIL)
14052 break;
14053 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014054}
14055
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014056/*
14057 * "tabpagenr()" function
14058 */
14059 static void
14060f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
14061{
14062 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014063 char_u *arg;
14064
14065 if (argvars[0].v_type != VAR_UNKNOWN)
14066 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014067 arg = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014068 nr = 0;
14069 if (arg != NULL)
14070 {
14071 if (STRCMP(arg, "$") == 0)
14072 nr = tabpage_index(NULL) - 1;
14073 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014074 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014075 }
14076 }
14077 else
14078 nr = tabpage_index(curtab);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014079 rettv->vval.v_number = nr;
14080}
14081
14082
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014083/*
14084 * Common code for tabpagewinnr() and winnr().
14085 */
14086 static int
14087get_winnr(tabpage_T *tp, typval_T *argvar)
14088{
14089 win_T *twin;
14090 int nr = 1;
14091 win_T *wp;
14092 char_u *arg;
14093
14094 twin = (tp == curtab) ? curwin : tp->tp_curwin;
14095 if (argvar->v_type != VAR_UNKNOWN)
14096 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014097 int invalid_arg = FALSE;
14098
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014099 arg = tv_get_string_chk(argvar);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014100 if (arg == NULL)
14101 nr = 0; /* type error; errmsg already given */
14102 else if (STRCMP(arg, "$") == 0)
14103 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
14104 else if (STRCMP(arg, "#") == 0)
14105 {
14106 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
14107 if (twin == NULL)
14108 nr = 0;
14109 }
14110 else
14111 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +020014112 long count;
14113 char_u *endp;
14114
14115 // Extract the window count (if specified). e.g. winnr('3j')
14116 count = strtol((char *)arg, (char **)&endp, 10);
14117 if (count <= 0)
14118 count = 1; // if count is not specified, default to 1
14119 if (endp != NULL && *endp != '\0')
14120 {
14121 if (STRCMP(endp, "j") == 0)
14122 twin = win_vert_neighbor(tp, twin, FALSE, count);
14123 else if (STRCMP(endp, "k") == 0)
14124 twin = win_vert_neighbor(tp, twin, TRUE, count);
14125 else if (STRCMP(endp, "h") == 0)
14126 twin = win_horz_neighbor(tp, twin, TRUE, count);
14127 else if (STRCMP(endp, "l") == 0)
14128 twin = win_horz_neighbor(tp, twin, FALSE, count);
14129 else
14130 invalid_arg = TRUE;
14131 }
14132 else
14133 invalid_arg = TRUE;
14134 }
14135
14136 if (invalid_arg)
14137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014138 semsg(_(e_invexpr2), arg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014139 nr = 0;
14140 }
14141 }
14142
14143 if (nr > 0)
14144 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
14145 wp != twin; wp = wp->w_next)
14146 {
14147 if (wp == NULL)
14148 {
14149 /* didn't find it in this tabpage */
14150 nr = 0;
14151 break;
14152 }
14153 ++nr;
14154 }
14155 return nr;
14156}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014157
14158/*
14159 * "tabpagewinnr()" function
14160 */
14161 static void
14162f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
14163{
14164 int nr = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014165 tabpage_T *tp;
14166
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014167 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014168 if (tp == NULL)
14169 nr = 0;
14170 else
14171 nr = get_winnr(tp, &argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014172 rettv->vval.v_number = nr;
14173}
14174
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014175/*
14176 * "tagfiles()" function
14177 */
14178 static void
14179f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
14180{
14181 char_u *fname;
14182 tagname_T tn;
14183 int first;
14184
14185 if (rettv_list_alloc(rettv) == FAIL)
14186 return;
14187 fname = alloc(MAXPATHL);
14188 if (fname == NULL)
14189 return;
14190
14191 for (first = TRUE; ; first = FALSE)
14192 if (get_tagfname(&tn, first, fname) == FAIL
14193 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
14194 break;
14195 tagname_free(&tn);
14196 vim_free(fname);
14197}
14198
14199/*
14200 * "taglist()" function
14201 */
14202 static void
14203f_taglist(typval_T *argvars, typval_T *rettv)
14204{
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014205 char_u *fname = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014206 char_u *tag_pattern;
14207
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014208 tag_pattern = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014209
14210 rettv->vval.v_number = FALSE;
14211 if (*tag_pattern == NUL)
14212 return;
14213
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014214 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014215 fname = tv_get_string(&argvars[1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014216 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaarc6aafba2017-03-21 17:09:10 +010014217 (void)get_tags(rettv->vval.v_list, tag_pattern, fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014218}
14219
14220/*
14221 * "tempname()" function
14222 */
14223 static void
14224f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
14225{
14226 static int x = 'A';
14227
14228 rettv->v_type = VAR_STRING;
14229 rettv->vval.v_string = vim_tempname(x, FALSE);
14230
14231 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14232 * names. Skip 'I' and 'O', they are used for shell redirection. */
14233 do
14234 {
14235 if (x == 'Z')
14236 x = '0';
14237 else if (x == '9')
14238 x = 'A';
14239 else
14240 {
14241#ifdef EBCDIC
14242 if (x == 'I')
14243 x = 'J';
14244 else if (x == 'R')
14245 x = 'S';
14246 else
14247#endif
14248 ++x;
14249 }
14250 } while (x == 'I' || x == 'O');
14251}
14252
14253#ifdef FEAT_FLOAT
14254/*
14255 * "tan()" function
14256 */
14257 static void
14258f_tan(typval_T *argvars, typval_T *rettv)
14259{
14260 float_T f = 0.0;
14261
14262 rettv->v_type = VAR_FLOAT;
14263 if (get_float_arg(argvars, &f) == OK)
14264 rettv->vval.v_float = tan(f);
14265 else
14266 rettv->vval.v_float = 0.0;
14267}
14268
14269/*
14270 * "tanh()" function
14271 */
14272 static void
14273f_tanh(typval_T *argvars, typval_T *rettv)
14274{
14275 float_T f = 0.0;
14276
14277 rettv->v_type = VAR_FLOAT;
14278 if (get_float_arg(argvars, &f) == OK)
14279 rettv->vval.v_float = tanh(f);
14280 else
14281 rettv->vval.v_float = 0.0;
14282}
14283#endif
14284
14285/*
14286 * "test_alloc_fail(id, countdown, repeat)" function
14287 */
14288 static void
14289f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
14290{
14291 if (argvars[0].v_type != VAR_NUMBER
14292 || argvars[0].vval.v_number <= 0
14293 || argvars[1].v_type != VAR_NUMBER
14294 || argvars[1].vval.v_number < 0
14295 || argvars[2].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014296 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014297 else
14298 {
14299 alloc_fail_id = argvars[0].vval.v_number;
14300 if (alloc_fail_id >= aid_last)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014301 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014302 alloc_fail_countdown = argvars[1].vval.v_number;
14303 alloc_fail_repeat = argvars[2].vval.v_number;
14304 did_outofmem_msg = FALSE;
14305 }
14306}
14307
14308/*
14309 * "test_autochdir()"
14310 */
14311 static void
14312f_test_autochdir(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14313{
14314#if defined(FEAT_AUTOCHDIR)
14315 test_autochdir = TRUE;
14316#endif
14317}
14318
14319/*
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014320 * "test_feedinput()"
14321 */
14322 static void
14323f_test_feedinput(typval_T *argvars, typval_T *rettv UNUSED)
14324{
14325#ifdef USE_INPUT_BUF
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014326 char_u *val = tv_get_string_chk(&argvars[0]);
Bram Moolenaar5e80de32017-09-03 15:48:12 +020014327
14328 if (val != NULL)
14329 {
14330 trash_input_buf();
14331 add_to_input_buf_csi(val, (int)STRLEN(val));
14332 }
14333#endif
14334}
14335
14336/*
Bram Moolenaareda65222019-05-16 20:29:44 +020014337 * "test_getvalue({name})" function
14338 */
14339 static void
14340f_test_getvalue(typval_T *argvars, typval_T *rettv)
14341{
14342 if (argvars[0].v_type != VAR_STRING)
14343 emsg(_(e_invarg));
14344 else
14345 {
14346 char_u *name = tv_get_string(&argvars[0]);
14347
14348 if (STRCMP(name, (char_u *)"need_fileinfo") == 0)
14349 rettv->vval.v_number = need_fileinfo;
14350 else
14351 semsg(_(e_invarg2), name);
14352 }
14353}
14354
14355/*
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014356 * "test_option_not_set({name})" function
14357 */
14358 static void
14359f_test_option_not_set(typval_T *argvars, typval_T *rettv UNUSED)
14360{
14361 char_u *name = (char_u *)"";
14362
14363 if (argvars[0].v_type != VAR_STRING)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014364 emsg(_(e_invarg));
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014365 else
14366 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014367 name = tv_get_string(&argvars[0]);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014368 if (reset_option_was_set(name) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014369 semsg(_(e_invarg2), name);
Bram Moolenaarfe8ef982018-09-13 20:31:54 +020014370 }
14371}
14372
14373/*
14374 * "test_override({name}, {val})" function
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014375 */
14376 static void
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014377f_test_override(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014378{
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014379 char_u *name = (char_u *)"";
14380 int val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014381 static int save_starting = -1;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014382
14383 if (argvars[0].v_type != VAR_STRING
14384 || (argvars[1].v_type) != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014385 emsg(_(e_invarg));
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014386 else
14387 {
Bram Moolenaare38197d2018-12-24 23:35:13 +010014388 name = tv_get_string(&argvars[0]);
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014389 val = (int)tv_get_number(&argvars[1]);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014390
14391 if (STRCMP(name, (char_u *)"redraw") == 0)
14392 disable_redraw_for_testing = val;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014393 else if (STRCMP(name, (char_u *)"redraw_flag") == 0)
14394 ignore_redraw_flag_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014395 else if (STRCMP(name, (char_u *)"char_avail") == 0)
14396 disable_char_avail_for_testing = val;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014397 else if (STRCMP(name, (char_u *)"starting") == 0)
14398 {
14399 if (val)
14400 {
14401 if (save_starting < 0)
14402 save_starting = starting;
14403 starting = 0;
14404 }
14405 else
14406 {
14407 starting = save_starting;
14408 save_starting = -1;
14409 }
14410 }
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014411 else if (STRCMP(name, (char_u *)"nfa_fail") == 0)
14412 nfa_fail_for_testing = val;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014413 else if (STRCMP(name, (char_u *)"no_query_mouse") == 0)
14414 no_query_mouse_for_testing = val;
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014415 else if (STRCMP(name, (char_u *)"ALL") == 0)
14416 {
14417 disable_char_avail_for_testing = FALSE;
14418 disable_redraw_for_testing = FALSE;
Bram Moolenaared5a9d62018-09-06 13:14:43 +020014419 ignore_redraw_flag_for_testing = FALSE;
Bram Moolenaarbcf94422018-06-23 14:21:42 +020014420 nfa_fail_for_testing = FALSE;
Bram Moolenaar92fd5992019-05-02 23:00:22 +020014421 no_query_mouse_for_testing = FALSE;
Bram Moolenaar182a17b2017-06-25 20:57:18 +020014422 if (save_starting >= 0)
14423 {
14424 starting = save_starting;
14425 save_starting = -1;
14426 }
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014427 }
14428 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014429 semsg(_(e_invarg2), name);
Bram Moolenaareb992cb2017-03-09 18:20:16 +010014430 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014431}
14432
14433/*
Bram Moolenaarc3e92c12019-03-23 14:23:07 +010014434 * "test_refcount({expr})" function
14435 */
14436 static void
14437f_test_refcount(typval_T *argvars, typval_T *rettv)
14438{
14439 int retval = -1;
14440
14441 switch (argvars[0].v_type)
14442 {
14443 case VAR_UNKNOWN:
14444 case VAR_NUMBER:
14445 case VAR_FLOAT:
14446 case VAR_SPECIAL:
14447 case VAR_STRING:
14448 break;
14449 case VAR_JOB:
14450#ifdef FEAT_JOB_CHANNEL
14451 if (argvars[0].vval.v_job != NULL)
14452 retval = argvars[0].vval.v_job->jv_refcount - 1;
14453#endif
14454 break;
14455 case VAR_CHANNEL:
14456#ifdef FEAT_JOB_CHANNEL
14457 if (argvars[0].vval.v_channel != NULL)
14458 retval = argvars[0].vval.v_channel->ch_refcount - 1;
14459#endif
14460 break;
14461 case VAR_FUNC:
14462 if (argvars[0].vval.v_string != NULL)
14463 {
14464 ufunc_T *fp;
14465
14466 fp = find_func(argvars[0].vval.v_string);
14467 if (fp != NULL)
14468 retval = fp->uf_refcount;
14469 }
14470 break;
14471 case VAR_PARTIAL:
14472 if (argvars[0].vval.v_partial != NULL)
14473 retval = argvars[0].vval.v_partial->pt_refcount - 1;
14474 break;
14475 case VAR_BLOB:
14476 if (argvars[0].vval.v_blob != NULL)
14477 retval = argvars[0].vval.v_blob->bv_refcount - 1;
14478 break;
14479 case VAR_LIST:
14480 if (argvars[0].vval.v_list != NULL)
14481 retval = argvars[0].vval.v_list->lv_refcount - 1;
14482 break;
14483 case VAR_DICT:
14484 if (argvars[0].vval.v_dict != NULL)
14485 retval = argvars[0].vval.v_dict->dv_refcount - 1;
14486 break;
14487 }
14488
14489 rettv->v_type = VAR_NUMBER;
14490 rettv->vval.v_number = retval;
14491
14492}
14493
14494/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014495 * "test_garbagecollect_now()" function
14496 */
14497 static void
14498f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14499{
14500 /* This is dangerous, any Lists and Dicts used internally may be freed
14501 * while still in use. */
14502 garbage_collect(TRUE);
14503}
14504
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014505/*
14506 * "test_ignore_error()" function
14507 */
14508 static void
14509f_test_ignore_error(typval_T *argvars, typval_T *rettv UNUSED)
14510{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014511 ignore_error_for_testing(tv_get_string(&argvars[0]));
Bram Moolenaare0c31f62017-03-01 15:07:05 +010014512}
14513
Bram Moolenaarc0f5a782019-01-13 15:16:13 +010014514 static void
14515f_test_null_blob(typval_T *argvars UNUSED, typval_T *rettv)
14516{
14517 rettv->v_type = VAR_BLOB;
14518 rettv->vval.v_blob = NULL;
14519}
14520
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014521#ifdef FEAT_JOB_CHANNEL
14522 static void
14523f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
14524{
14525 rettv->v_type = VAR_CHANNEL;
14526 rettv->vval.v_channel = NULL;
14527}
14528#endif
14529
14530 static void
14531f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
14532{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014533 rettv_dict_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014534}
14535
14536#ifdef FEAT_JOB_CHANNEL
14537 static void
14538f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
14539{
14540 rettv->v_type = VAR_JOB;
14541 rettv->vval.v_job = NULL;
14542}
14543#endif
14544
14545 static void
14546f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
14547{
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020014548 rettv_list_set(rettv, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014549}
14550
14551 static void
14552f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
14553{
14554 rettv->v_type = VAR_PARTIAL;
14555 rettv->vval.v_partial = NULL;
14556}
14557
14558 static void
14559f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
14560{
14561 rettv->v_type = VAR_STRING;
14562 rettv->vval.v_string = NULL;
14563}
14564
Bram Moolenaarab186732018-09-14 21:27:06 +020014565#ifdef FEAT_GUI
14566 static void
14567f_test_scrollbar(typval_T *argvars, typval_T *rettv UNUSED)
14568{
14569 char_u *which;
14570 long value;
14571 int dragging;
14572 scrollbar_T *sb = NULL;
14573
14574 if (argvars[0].v_type != VAR_STRING
14575 || (argvars[1].v_type) != VAR_NUMBER
14576 || (argvars[2].v_type) != VAR_NUMBER)
14577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014578 emsg(_(e_invarg));
Bram Moolenaarab186732018-09-14 21:27:06 +020014579 return;
14580 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014581 which = tv_get_string(&argvars[0]);
14582 value = tv_get_number(&argvars[1]);
14583 dragging = tv_get_number(&argvars[2]);
Bram Moolenaarab186732018-09-14 21:27:06 +020014584
14585 if (STRCMP(which, "left") == 0)
14586 sb = &curwin->w_scrollbars[SBAR_LEFT];
14587 else if (STRCMP(which, "right") == 0)
14588 sb = &curwin->w_scrollbars[SBAR_RIGHT];
14589 else if (STRCMP(which, "hor") == 0)
14590 sb = &gui.bottom_sbar;
14591 if (sb == NULL)
14592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014593 semsg(_(e_invarg2), which);
Bram Moolenaarab186732018-09-14 21:27:06 +020014594 return;
14595 }
14596 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar586c70c2018-10-02 16:23:58 +020014597# ifndef USE_ON_FLY_SCROLL
14598 // need to loop through normal_cmd() to handle the scroll events
14599 exec_normal(FALSE, TRUE, FALSE);
14600# endif
Bram Moolenaarab186732018-09-14 21:27:06 +020014601}
14602#endif
14603
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014604#ifdef FEAT_MOUSE
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014605 static void
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014606f_test_setmouse(typval_T *argvars, typval_T *rettv UNUSED)
14607{
14608 mouse_row = (time_t)tv_get_number(&argvars[0]) - 1;
14609 mouse_col = (time_t)tv_get_number(&argvars[1]) - 1;
14610}
Bram Moolenaar7e1a5af2019-05-07 16:28:13 +020014611#endif
Bram Moolenaarbb8476b2019-05-04 15:47:48 +020014612
14613 static void
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014614f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
14615{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014616 time_for_testing = (time_t)tv_get_number(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014617}
14618
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014619/*
14620 * Get a callback from "arg". It can be a Funcref or a function name.
14621 * When "arg" is zero return an empty string.
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014622 * "cb_name" is not allocated.
14623 * "cb_name" is set to NULL for an invalid argument.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014624 */
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014625 callback_T
14626get_callback(typval_T *arg)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014627{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014628 callback_T res;
14629
14630 res.cb_free_name = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014631 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
14632 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014633 res.cb_partial = arg->vval.v_partial;
14634 ++res.cb_partial->pt_refcount;
14635 res.cb_name = partial_name(res.cb_partial);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014636 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014637 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014638 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014639 res.cb_partial = NULL;
14640 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
14641 {
14642 // Note that we don't make a copy of the string.
14643 res.cb_name = arg->vval.v_string;
14644 func_ref(res.cb_name);
14645 }
14646 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
14647 {
14648 res.cb_name = (char_u *)"";
14649 }
14650 else
14651 {
14652 emsg(_("E921: Invalid callback argument"));
14653 res.cb_name = NULL;
14654 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014655 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014656 return res;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014657}
14658
14659/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014660 * Copy a callback into a typval_T.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014661 */
14662 void
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014663put_callback(callback_T *cb, typval_T *tv)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014664{
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014665 if (cb->cb_partial != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014666 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014667 tv->v_type = VAR_PARTIAL;
14668 tv->vval.v_partial = cb->cb_partial;
14669 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014670 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014671 else
14672 {
14673 tv->v_type = VAR_FUNC;
14674 tv->vval.v_string = vim_strsave(cb->cb_name);
14675 func_ref(cb->cb_name);
14676 }
14677}
14678
14679/*
14680 * Make a copy of "src" into "dest", allocating the function name if needed,
14681 * without incrementing the refcount.
14682 */
14683 void
14684set_callback(callback_T *dest, callback_T *src)
14685{
14686 if (src->cb_partial == NULL)
14687 {
14688 // just a function name, make a copy
14689 dest->cb_name = vim_strsave(src->cb_name);
14690 dest->cb_free_name = TRUE;
14691 }
14692 else
14693 {
14694 // cb_name is a pointer into cb_partial
14695 dest->cb_name = src->cb_name;
14696 dest->cb_free_name = FALSE;
14697 }
14698 dest->cb_partial = src->cb_partial;
14699}
14700
14701/*
14702 * Unref/free "callback" returned by get_callback() or set_callback().
14703 */
14704 void
14705free_callback(callback_T *callback)
14706{
14707 if (callback->cb_partial != NULL)
14708 {
14709 partial_unref(callback->cb_partial);
14710 callback->cb_partial = NULL;
14711 }
14712 else if (callback->cb_name != NULL)
14713 func_unref(callback->cb_name);
14714 if (callback->cb_free_name)
14715 {
14716 vim_free(callback->cb_name);
14717 callback->cb_free_name = FALSE;
14718 }
14719 callback->cb_name = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014720}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014721
14722#ifdef FEAT_TIMERS
14723/*
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014724 * "timer_info([timer])" function
14725 */
14726 static void
14727f_timer_info(typval_T *argvars, typval_T *rettv)
14728{
14729 timer_T *timer = NULL;
14730
14731 if (rettv_list_alloc(rettv) != OK)
14732 return;
14733 if (argvars[0].v_type != VAR_UNKNOWN)
14734 {
14735 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014736 emsg(_(e_number_exp));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014737 else
14738 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014739 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar8e97bd72016-08-06 22:05:07 +020014740 if (timer != NULL)
14741 add_timer_info(rettv, timer);
14742 }
14743 }
14744 else
14745 add_timer_info_all(rettv);
14746}
14747
14748/*
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014749 * "timer_pause(timer, paused)" function
14750 */
14751 static void
14752f_timer_pause(typval_T *argvars, typval_T *rettv UNUSED)
14753{
14754 timer_T *timer = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014755 int paused = (int)tv_get_number(&argvars[1]);
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014756
14757 if (argvars[0].v_type != VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014758 emsg(_(e_number_exp));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014759 else
14760 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014761 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014762 if (timer != NULL)
14763 timer->tr_paused = paused;
14764 }
14765}
14766
14767/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014768 * "timer_start(time, callback [, options])" function
14769 */
14770 static void
14771f_timer_start(typval_T *argvars, typval_T *rettv)
14772{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014773 long msec = (long)tv_get_number(&argvars[0]);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014774 timer_T *timer;
14775 int repeat = 0;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014776 callback_T callback;
Bram Moolenaar75537a92016-09-05 22:45:28 +020014777 dict_T *dict;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014778
Bram Moolenaar75537a92016-09-05 22:45:28 +020014779 rettv->vval.v_number = -1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014780 if (check_secure())
14781 return;
14782 if (argvars[2].v_type != VAR_UNKNOWN)
14783 {
14784 if (argvars[2].v_type != VAR_DICT
14785 || (dict = argvars[2].vval.v_dict) == NULL)
14786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014787 semsg(_(e_invarg2), tv_get_string(&argvars[2]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014788 return;
14789 }
14790 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010014791 repeat = dict_get_number(dict, (char_u *)"repeat");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014792 }
14793
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014794 callback = get_callback(&argvars[1]);
14795 if (callback.cb_name == NULL)
Bram Moolenaar75537a92016-09-05 22:45:28 +020014796 return;
14797
14798 timer = create_timer(msec, repeat);
14799 if (timer == NULL)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014800 free_callback(&callback);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014801 else
14802 {
Bram Moolenaar3a97bb32019-06-01 13:28:35 +020014803 set_callback(&timer->tr_callback, &callback);
Bram Moolenaar75537a92016-09-05 22:45:28 +020014804 rettv->vval.v_number = (varnumber_T)timer->tr_id;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014805 }
14806}
14807
14808/*
14809 * "timer_stop(timer)" function
14810 */
14811 static void
14812f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
14813{
14814 timer_T *timer;
14815
14816 if (argvars[0].v_type != VAR_NUMBER)
14817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014818 emsg(_(e_number_exp));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014819 return;
14820 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014821 timer = find_timer((int)tv_get_number(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014822 if (timer != NULL)
14823 stop_timer(timer);
14824}
Bram Moolenaarb73598e2016-08-07 18:22:53 +020014825
14826/*
14827 * "timer_stopall()" function
14828 */
14829 static void
14830f_timer_stopall(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
14831{
14832 stop_all_timers();
14833}
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014834#endif
14835
14836/*
14837 * "tolower(string)" function
14838 */
14839 static void
14840f_tolower(typval_T *argvars, typval_T *rettv)
14841{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014842 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014843 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014844}
14845
14846/*
14847 * "toupper(string)" function
14848 */
14849 static void
14850f_toupper(typval_T *argvars, typval_T *rettv)
14851{
14852 rettv->v_type = VAR_STRING;
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014853 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0]));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014854}
14855
14856/*
14857 * "tr(string, fromstr, tostr)" function
14858 */
14859 static void
14860f_tr(typval_T *argvars, typval_T *rettv)
14861{
14862 char_u *in_str;
14863 char_u *fromstr;
14864 char_u *tostr;
14865 char_u *p;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014866 int inlen;
14867 int fromlen;
14868 int tolen;
14869 int idx;
14870 char_u *cpstr;
14871 int cplen;
14872 int first = TRUE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014873 char_u buf[NUMBUFLEN];
14874 char_u buf2[NUMBUFLEN];
14875 garray_T ga;
14876
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014877 in_str = tv_get_string(&argvars[0]);
14878 fromstr = tv_get_string_buf_chk(&argvars[1], buf);
14879 tostr = tv_get_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014880
14881 /* Default return value: empty string. */
14882 rettv->v_type = VAR_STRING;
14883 rettv->vval.v_string = NULL;
14884 if (fromstr == NULL || tostr == NULL)
14885 return; /* type error; errmsg already given */
14886 ga_init2(&ga, (int)sizeof(char), 80);
14887
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014888 if (!has_mbyte)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014889 /* not multi-byte: fromstr and tostr must be the same length */
14890 if (STRLEN(fromstr) != STRLEN(tostr))
14891 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014892error:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010014893 semsg(_(e_invarg2), fromstr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014894 ga_clear(&ga);
14895 return;
14896 }
14897
14898 /* fromstr and tostr have to contain the same number of chars */
14899 while (*in_str != NUL)
14900 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014901 if (has_mbyte)
14902 {
14903 inlen = (*mb_ptr2len)(in_str);
14904 cpstr = in_str;
14905 cplen = inlen;
14906 idx = 0;
14907 for (p = fromstr; *p != NUL; p += fromlen)
14908 {
14909 fromlen = (*mb_ptr2len)(p);
14910 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
14911 {
14912 for (p = tostr; *p != NUL; p += tolen)
14913 {
14914 tolen = (*mb_ptr2len)(p);
14915 if (idx-- == 0)
14916 {
14917 cplen = tolen;
14918 cpstr = p;
14919 break;
14920 }
14921 }
14922 if (*p == NUL) /* tostr is shorter than fromstr */
14923 goto error;
14924 break;
14925 }
14926 ++idx;
14927 }
14928
14929 if (first && cpstr == in_str)
14930 {
14931 /* Check that fromstr and tostr have the same number of
14932 * (multi-byte) characters. Done only once when a character
14933 * of in_str doesn't appear in fromstr. */
14934 first = FALSE;
14935 for (p = tostr; *p != NUL; p += tolen)
14936 {
14937 tolen = (*mb_ptr2len)(p);
14938 --idx;
14939 }
14940 if (idx != 0)
14941 goto error;
14942 }
14943
14944 (void)ga_grow(&ga, cplen);
14945 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
14946 ga.ga_len += cplen;
14947
14948 in_str += inlen;
14949 }
14950 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020014951 {
14952 /* When not using multi-byte chars we can do it faster. */
14953 p = vim_strchr(fromstr, *in_str);
14954 if (p != NULL)
14955 ga_append(&ga, tostr[p - fromstr]);
14956 else
14957 ga_append(&ga, *in_str);
14958 ++in_str;
14959 }
14960 }
14961
14962 /* add a terminating NUL */
14963 (void)ga_grow(&ga, 1);
14964 ga_append(&ga, NUL);
14965
14966 rettv->vval.v_string = ga.ga_data;
14967}
14968
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014969/*
14970 * "trim({expr})" function
14971 */
14972 static void
14973f_trim(typval_T *argvars, typval_T *rettv)
14974{
14975 char_u buf1[NUMBUFLEN];
14976 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014977 char_u *head = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014978 char_u *mask = NULL;
14979 char_u *tail;
14980 char_u *prev;
14981 char_u *p;
14982 int c1;
14983
14984 rettv->v_type = VAR_STRING;
14985 if (head == NULL)
14986 {
14987 rettv->vval.v_string = NULL;
14988 return;
14989 }
14990
14991 if (argvars[1].v_type == VAR_STRING)
Bram Moolenaard155d7a2018-12-21 16:04:21 +010014992 mask = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar295ac5a2018-03-22 23:04:02 +010014993
14994 while (*head != NUL)
14995 {
14996 c1 = PTR2CHAR(head);
14997 if (mask == NULL)
14998 {
14999 if (c1 > ' ' && c1 != 0xa0)
15000 break;
15001 }
15002 else
15003 {
15004 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15005 if (c1 == PTR2CHAR(p))
15006 break;
15007 if (*p == NUL)
15008 break;
15009 }
15010 MB_PTR_ADV(head);
15011 }
15012
15013 for (tail = head + STRLEN(head); tail > head; tail = prev)
15014 {
15015 prev = tail;
15016 MB_PTR_BACK(head, prev);
15017 c1 = PTR2CHAR(prev);
15018 if (mask == NULL)
15019 {
15020 if (c1 > ' ' && c1 != 0xa0)
15021 break;
15022 }
15023 else
15024 {
15025 for (p = mask; *p != NUL; MB_PTR_ADV(p))
15026 if (c1 == PTR2CHAR(p))
15027 break;
15028 if (*p == NUL)
15029 break;
15030 }
15031 }
15032 rettv->vval.v_string = vim_strnsave(head, (int)(tail - head));
15033}
15034
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015035#ifdef FEAT_FLOAT
15036/*
15037 * "trunc({float})" function
15038 */
15039 static void
15040f_trunc(typval_T *argvars, typval_T *rettv)
15041{
15042 float_T f = 0.0;
15043
15044 rettv->v_type = VAR_FLOAT;
15045 if (get_float_arg(argvars, &f) == OK)
15046 /* trunc() is not in C90, use floor() or ceil() instead. */
15047 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
15048 else
15049 rettv->vval.v_float = 0.0;
15050}
15051#endif
15052
15053/*
15054 * "type(expr)" function
15055 */
15056 static void
15057f_type(typval_T *argvars, typval_T *rettv)
15058{
15059 int n = -1;
15060
15061 switch (argvars[0].v_type)
15062 {
Bram Moolenaarf562e722016-07-19 17:25:25 +020015063 case VAR_NUMBER: n = VAR_TYPE_NUMBER; break;
15064 case VAR_STRING: n = VAR_TYPE_STRING; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015065 case VAR_PARTIAL:
Bram Moolenaarf562e722016-07-19 17:25:25 +020015066 case VAR_FUNC: n = VAR_TYPE_FUNC; break;
15067 case VAR_LIST: n = VAR_TYPE_LIST; break;
15068 case VAR_DICT: n = VAR_TYPE_DICT; break;
15069 case VAR_FLOAT: n = VAR_TYPE_FLOAT; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015070 case VAR_SPECIAL:
15071 if (argvars[0].vval.v_number == VVAL_FALSE
15072 || argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaarf562e722016-07-19 17:25:25 +020015073 n = VAR_TYPE_BOOL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015074 else
Bram Moolenaarf562e722016-07-19 17:25:25 +020015075 n = VAR_TYPE_NONE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015076 break;
Bram Moolenaarf562e722016-07-19 17:25:25 +020015077 case VAR_JOB: n = VAR_TYPE_JOB; break;
15078 case VAR_CHANNEL: n = VAR_TYPE_CHANNEL; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015079 case VAR_BLOB: n = VAR_TYPE_BLOB; break;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015080 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +010015081 internal_error("f_type(UNKNOWN)");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015082 n = -1;
15083 break;
15084 }
15085 rettv->vval.v_number = n;
15086}
15087
15088/*
15089 * "undofile(name)" function
15090 */
15091 static void
15092f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
15093{
15094 rettv->v_type = VAR_STRING;
15095#ifdef FEAT_PERSISTENT_UNDO
15096 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015097 char_u *fname = tv_get_string(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015098
15099 if (*fname == NUL)
15100 {
15101 /* If there is no file name there will be no undo file. */
15102 rettv->vval.v_string = NULL;
15103 }
15104 else
15105 {
Bram Moolenaare9ebc9a2019-05-19 15:27:14 +020015106 char_u *ffname = FullName_save(fname, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015107
15108 if (ffname != NULL)
15109 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
15110 vim_free(ffname);
15111 }
15112 }
15113#else
15114 rettv->vval.v_string = NULL;
15115#endif
15116}
15117
15118/*
15119 * "undotree()" function
15120 */
15121 static void
15122f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
15123{
15124 if (rettv_dict_alloc(rettv) == OK)
15125 {
15126 dict_T *dict = rettv->vval.v_dict;
15127 list_T *list;
15128
Bram Moolenaare0be1672018-07-08 16:50:37 +020015129 dict_add_number(dict, "synced", (long)curbuf->b_u_synced);
15130 dict_add_number(dict, "seq_last", curbuf->b_u_seq_last);
15131 dict_add_number(dict, "save_last", (long)curbuf->b_u_save_nr_last);
15132 dict_add_number(dict, "seq_cur", curbuf->b_u_seq_cur);
15133 dict_add_number(dict, "time_cur", (long)curbuf->b_u_time_cur);
15134 dict_add_number(dict, "save_cur", (long)curbuf->b_u_save_nr_cur);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015135
15136 list = list_alloc();
15137 if (list != NULL)
15138 {
15139 u_eval_tree(curbuf->b_u_oldhead, list);
15140 dict_add_list(dict, "entries", list);
15141 }
15142 }
15143}
15144
15145/*
15146 * "values(dict)" function
15147 */
15148 static void
15149f_values(typval_T *argvars, typval_T *rettv)
15150{
15151 dict_list(argvars, rettv, 1);
15152}
15153
15154/*
15155 * "virtcol(string)" function
15156 */
15157 static void
15158f_virtcol(typval_T *argvars, typval_T *rettv)
15159{
15160 colnr_T vcol = 0;
15161 pos_T *fp;
15162 int fnum = curbuf->b_fnum;
15163
15164 fp = var2fpos(&argvars[0], FALSE, &fnum);
15165 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
15166 && fnum == curbuf->b_fnum)
15167 {
15168 getvvcol(curwin, fp, NULL, NULL, &vcol);
15169 ++vcol;
15170 }
15171
15172 rettv->vval.v_number = vcol;
15173}
15174
15175/*
15176 * "visualmode()" function
15177 */
15178 static void
15179f_visualmode(typval_T *argvars, typval_T *rettv)
15180{
15181 char_u str[2];
15182
15183 rettv->v_type = VAR_STRING;
15184 str[0] = curbuf->b_visual_mode_eval;
15185 str[1] = NUL;
15186 rettv->vval.v_string = vim_strsave(str);
15187
15188 /* A non-zero number or non-empty string argument: reset mode. */
15189 if (non_zero_arg(&argvars[0]))
15190 curbuf->b_visual_mode_eval = NUL;
15191}
15192
15193/*
15194 * "wildmenumode()" function
15195 */
15196 static void
15197f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15198{
15199#ifdef FEAT_WILDMENU
15200 if (wild_menu_showing)
15201 rettv->vval.v_number = 1;
15202#endif
15203}
15204
15205/*
15206 * "winbufnr(nr)" function
15207 */
15208 static void
15209f_winbufnr(typval_T *argvars, typval_T *rettv)
15210{
15211 win_T *wp;
15212
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015213 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015214 if (wp == NULL)
15215 rettv->vval.v_number = -1;
15216 else
15217 rettv->vval.v_number = wp->w_buffer->b_fnum;
15218}
15219
15220/*
15221 * "wincol()" function
15222 */
15223 static void
15224f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
15225{
15226 validate_cursor();
15227 rettv->vval.v_number = curwin->w_wcol + 1;
15228}
15229
15230/*
15231 * "winheight(nr)" function
15232 */
15233 static void
15234f_winheight(typval_T *argvars, typval_T *rettv)
15235{
15236 win_T *wp;
15237
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015238 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015239 if (wp == NULL)
15240 rettv->vval.v_number = -1;
15241 else
15242 rettv->vval.v_number = wp->w_height;
15243}
15244
15245/*
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015246 * "winlayout()" function
15247 */
15248 static void
15249f_winlayout(typval_T *argvars, typval_T *rettv)
15250{
15251 tabpage_T *tp;
15252
15253 if (rettv_list_alloc(rettv) != OK)
15254 return;
15255
15256 if (argvars[0].v_type == VAR_UNKNOWN)
15257 tp = curtab;
15258 else
15259 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015260 tp = find_tabpage((int)tv_get_number(&argvars[0]));
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +020015261 if (tp == NULL)
15262 return;
15263 }
15264
15265 get_framelayout(tp->tp_topframe, rettv->vval.v_list, TRUE);
15266}
15267
15268/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015269 * "winline()" function
15270 */
15271 static void
15272f_winline(typval_T *argvars UNUSED, typval_T *rettv)
15273{
15274 validate_cursor();
15275 rettv->vval.v_number = curwin->w_wrow + 1;
15276}
15277
15278/*
15279 * "winnr()" function
15280 */
15281 static void
15282f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
15283{
15284 int nr = 1;
15285
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015286 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015287 rettv->vval.v_number = nr;
15288}
15289
15290/*
15291 * "winrestcmd()" function
15292 */
15293 static void
15294f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
15295{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015296 win_T *wp;
15297 int winnr = 1;
15298 garray_T ga;
15299 char_u buf[50];
15300
15301 ga_init2(&ga, (int)sizeof(char), 70);
Bram Moolenaar29323592016-07-24 22:04:11 +020015302 FOR_ALL_WINDOWS(wp)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015303 {
15304 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15305 ga_concat(&ga, buf);
15306 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15307 ga_concat(&ga, buf);
15308 ++winnr;
15309 }
15310 ga_append(&ga, NUL);
15311
15312 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015313 rettv->v_type = VAR_STRING;
15314}
15315
15316/*
15317 * "winrestview()" function
15318 */
15319 static void
15320f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
15321{
15322 dict_T *dict;
15323
15324 if (argvars[0].v_type != VAR_DICT
15325 || (dict = argvars[0].vval.v_dict) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015326 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015327 else
15328 {
15329 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015330 curwin->w_cursor.lnum = (linenr_T)dict_get_number(dict, (char_u *)"lnum");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015331 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015332 curwin->w_cursor.col = (colnr_T)dict_get_number(dict, (char_u *)"col");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015333 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015334 curwin->w_cursor.coladd = (colnr_T)dict_get_number(dict, (char_u *)"coladd");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015335 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
15336 {
Bram Moolenaar8f667172018-12-14 15:38:31 +010015337 curwin->w_curswant = (colnr_T)dict_get_number(dict, (char_u *)"curswant");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015338 curwin->w_set_curswant = FALSE;
15339 }
15340
15341 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015342 set_topline(curwin, (linenr_T)dict_get_number(dict, (char_u *)"topline"));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015343#ifdef FEAT_DIFF
15344 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015345 curwin->w_topfill = (int)dict_get_number(dict, (char_u *)"topfill");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015346#endif
15347 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015348 curwin->w_leftcol = (colnr_T)dict_get_number(dict, (char_u *)"leftcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015349 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +010015350 curwin->w_skipcol = (colnr_T)dict_get_number(dict, (char_u *)"skipcol");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015351
15352 check_cursor();
15353 win_new_height(curwin, curwin->w_height);
Bram Moolenaar02631462017-09-22 15:20:32 +020015354 win_new_width(curwin, curwin->w_width);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015355 changed_window_setting();
15356
15357 if (curwin->w_topline <= 0)
15358 curwin->w_topline = 1;
15359 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
15360 curwin->w_topline = curbuf->b_ml.ml_line_count;
15361#ifdef FEAT_DIFF
15362 check_topfill(curwin, TRUE);
15363#endif
15364 }
15365}
15366
15367/*
15368 * "winsaveview()" function
15369 */
15370 static void
15371f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
15372{
15373 dict_T *dict;
15374
15375 if (rettv_dict_alloc(rettv) == FAIL)
15376 return;
15377 dict = rettv->vval.v_dict;
15378
Bram Moolenaare0be1672018-07-08 16:50:37 +020015379 dict_add_number(dict, "lnum", (long)curwin->w_cursor.lnum);
15380 dict_add_number(dict, "col", (long)curwin->w_cursor.col);
Bram Moolenaare0be1672018-07-08 16:50:37 +020015381 dict_add_number(dict, "coladd", (long)curwin->w_cursor.coladd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015382 update_curswant();
Bram Moolenaare0be1672018-07-08 16:50:37 +020015383 dict_add_number(dict, "curswant", (long)curwin->w_curswant);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015384
Bram Moolenaare0be1672018-07-08 16:50:37 +020015385 dict_add_number(dict, "topline", (long)curwin->w_topline);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015386#ifdef FEAT_DIFF
Bram Moolenaare0be1672018-07-08 16:50:37 +020015387 dict_add_number(dict, "topfill", (long)curwin->w_topfill);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015388#endif
Bram Moolenaare0be1672018-07-08 16:50:37 +020015389 dict_add_number(dict, "leftcol", (long)curwin->w_leftcol);
15390 dict_add_number(dict, "skipcol", (long)curwin->w_skipcol);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015391}
15392
15393/*
15394 * "winwidth(nr)" function
15395 */
15396 static void
15397f_winwidth(typval_T *argvars, typval_T *rettv)
15398{
15399 win_T *wp;
15400
Bram Moolenaarbabfcf52018-10-25 13:11:16 +020015401 wp = find_win_by_nr_or_id(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015402 if (wp == NULL)
15403 rettv->vval.v_number = -1;
15404 else
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015405 rettv->vval.v_number = wp->w_width;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015406}
15407
15408/*
15409 * "wordcount()" function
15410 */
15411 static void
15412f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
15413{
15414 if (rettv_dict_alloc(rettv) == FAIL)
15415 return;
15416 cursor_pos_info(rettv->vval.v_dict);
15417}
15418
15419/*
15420 * "writefile()" function
15421 */
15422 static void
15423f_writefile(typval_T *argvars, typval_T *rettv)
15424{
15425 int binary = FALSE;
15426 int append = FALSE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015427#ifdef HAVE_FSYNC
15428 int do_fsync = p_fs;
15429#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015430 char_u *fname;
15431 FILE *fd;
15432 int ret = 0;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015433 listitem_T *li;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015434 list_T *list = NULL;
15435 blob_T *blob = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015436
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015437 rettv->vval.v_number = -1;
Bram Moolenaar8c62a082019-02-08 14:34:10 +010015438 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015439 return;
15440
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015441 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015442 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015443 list = argvars[0].vval.v_list;
15444 if (list == NULL)
15445 return;
15446 for (li = list->lv_first; li != NULL; li = li->li_next)
15447 if (tv_get_string_chk(&li->li_tv) == NULL)
15448 return;
15449 }
15450 else if (argvars[0].v_type == VAR_BLOB)
15451 {
15452 blob = argvars[0].vval.v_blob;
15453 if (blob == NULL)
15454 return;
15455 }
15456 else
15457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015458 semsg(_(e_invarg2), "writefile()");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015459 return;
15460 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015461
15462 if (argvars[2].v_type != VAR_UNKNOWN)
15463 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015464 char_u *arg2 = tv_get_string_chk(&argvars[2]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015465
15466 if (arg2 == NULL)
15467 return;
15468 if (vim_strchr(arg2, 'b') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015469 binary = TRUE;
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015470 if (vim_strchr(arg2, 'a') != NULL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015471 append = TRUE;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015472#ifdef HAVE_FSYNC
15473 if (vim_strchr(arg2, 's') != NULL)
15474 do_fsync = TRUE;
15475 else if (vim_strchr(arg2, 'S') != NULL)
15476 do_fsync = FALSE;
15477#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015478 }
15479
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015480 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015481 if (fname == NULL)
15482 return;
15483
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015484 /* Always open the file in binary mode, library functions have a mind of
15485 * their own about CR-LF conversion. */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015486 if (*fname == NUL || (fd = mch_fopen((char *)fname,
15487 append ? APPENDBIN : WRITEBIN)) == NULL)
15488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010015489 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015490 ret = -1;
15491 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015492 else if (blob)
15493 {
15494 if (write_blob(fd, blob) == FAIL)
15495 ret = -1;
15496#ifdef HAVE_FSYNC
15497 else if (do_fsync)
15498 // Ignore the error, the user wouldn't know what to do about it.
15499 // May happen for a device.
Bram Moolenaara7870192019-02-14 12:56:36 +010015500 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010015501#endif
15502 fclose(fd);
15503 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015504 else
15505 {
Bram Moolenaar8cf91282017-06-13 19:38:37 +020015506 if (write_list(fd, list, binary) == FAIL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015507 ret = -1;
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015508#ifdef HAVE_FSYNC
Bram Moolenaar291a9d12017-11-25 14:37:11 +010015509 else if (do_fsync)
15510 /* Ignore the error, the user wouldn't know what to do about it.
15511 * May happen for a device. */
Bram Moolenaara7870192019-02-14 12:56:36 +010015512 vim_ignored = vim_fsync(fileno(fd));
Bram Moolenaar7567d0b2017-11-16 23:04:15 +010015513#endif
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015514 fclose(fd);
15515 }
15516
15517 rettv->vval.v_number = ret;
15518}
15519
15520/*
15521 * "xor(expr, expr)" function
15522 */
15523 static void
15524f_xor(typval_T *argvars, typval_T *rettv)
15525{
Bram Moolenaard155d7a2018-12-21 16:04:21 +010015526 rettv->vval.v_number = tv_get_number_chk(&argvars[0], NULL)
15527 ^ tv_get_number_chk(&argvars[1], NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015528}
15529
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020015530#endif /* FEAT_EVAL */